http_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file http_client_transport.c
3  * @brief Transport protocol abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL HTTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "http/http_client.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (HTTP_CLIENT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open network connection
46  * @param[in] context Pointer to the HTTP client context
47  * @return Error code
48  **/
49 
51 {
52  error_t error;
53 
54  //Open a TCP socket
56  //Failed to open socket?
57  if(context->socket == NULL)
58  return ERROR_OPEN_FAILED;
59 
60  //Associate the socket with the relevant interface
61  error = socketBindToInterface(context->socket, context->interface);
62  //Any error to report?
63  if(error)
64  return error;
65 
66  //Set timeout
67  error = socketSetTimeout(context->socket, context->timeout);
68  //Any error to report?
69  if(error)
70  return error;
71 
72 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
73  //TLS-secured connection?
74  if(context->tlsInitCallback != NULL)
75  {
76  //Allocate TLS context
77  context->tlsContext = tlsInit();
78  //Failed to allocate TLS context?
79  if(context->tlsContext == NULL)
80  return ERROR_OPEN_FAILED;
81 
82  //Select client operation mode
83  error = tlsSetConnectionEnd(context->tlsContext,
85  //Any error to report?
86  if(error)
87  return error;
88 
89  //Bind TLS to the relevant socket
90  error = tlsSetSocket(context->tlsContext, context->socket);
91  //Any error to report?
92  if(error)
93  return error;
94 
95  //Set TX and RX buffer size
96  error = tlsSetBufferSize(context->tlsContext,
98  //Any error to report?
99  if(error)
100  return error;
101 
102  //Restore TLS session
103  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
104  //Any error to report?
105  if(error)
106  return error;
107 
108  //Perform TLS related initialization
109  error = context->tlsInitCallback(context, context->tlsContext,
110  context->tlsInitParam);
111  //Any error to report?
112  if(error)
113  return error;
114  }
115 #endif
116 
117  //Successful processing
118  return NO_ERROR;
119 }
120 
121 
122 /**
123  * @brief Establish network connection
124  * @param[in] context Pointer to the HTTP client context
125  * @param[in] serverIpAddr IP address of the HTTP server to connect to
126  * @param[in] serverPort TCP port number that will be used to establish the
127  * connection
128  * @return Error code
129  **/
130 
132  const IpAddr *serverIpAddr, uint16_t serverPort)
133 {
134  error_t error;
135 
136  //Establish TCP connection
137  error = socketConnect(context->socket, serverIpAddr, serverPort);
138  //Any error to report?
139  if(error)
140  return error;
141 
142 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
143  //TLS-secured connection?
144  if(context->tlsContext != NULL)
145  {
146  //Establish TLS connection
147  error = tlsConnect(context->tlsContext);
148  //Any error to report?
149  if(error)
150  return error;
151  }
152 #endif
153 
154  //Successful processing
155  return NO_ERROR;
156 }
157 
158 
159 /**
160  * @brief Shutdown network connection
161  * @param[in] context Pointer to the HTTP client context
162  * @return Error code
163  **/
164 
166 {
167  error_t error;
168 
169  //Initialize status code
170  error = NO_ERROR;
171 
172 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
173  //Valid TLS context?
174  if(context->tlsContext != NULL)
175  {
176  //Shutdown TLS session
177  error = tlsShutdown(context->tlsContext);
178  }
179 #endif
180 
181  //Check status code
182  if(!error)
183  {
184  //Valid TCP socket?
185  if(context->socket != NULL)
186  {
187  //Shutdown TCP connection
188  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
189  }
190  }
191 
192  //Return status code
193  return error;
194 }
195 
196 
197 /**
198  * @brief Close network connection
199  * @param[in] context Pointer to the HTTP client context
200  **/
201 
203 {
204 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
205  //Release TLS context
206  if(context->tlsContext != NULL)
207  {
208  tlsFree(context->tlsContext);
209  context->tlsContext = NULL;
210  }
211 #endif
212 
213  //Close TCP connection
214  if(context->socket != NULL)
215  {
216  socketClose(context->socket);
217  context->socket = NULL;
218  }
219 }
220 
221 
222 /**
223  * @brief Send data using the relevant transport protocol
224  * @param[in] context Pointer to the HTTP client context
225  * @param[in] data Pointer to a buffer containing the data to be transmitted
226  * @param[in] length Number of bytes to be transmitted
227  * @param[out] written Actual number of bytes written (optional parameter)
228  * @param[in] flags Set of flags that influences the behavior of this function
229  * @return Error code
230  **/
231 
233  size_t length, size_t *written, uint_t flags)
234 {
235  error_t error;
236 
237 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
238  //TLS-secured connection?
239  if(context->tlsContext != NULL)
240  {
241  //Send TLS-encrypted data
242  error = tlsWrite(context->tlsContext, data, length, written, flags);
243  }
244  else
245 #endif
246  {
247  //Transmit data
248  error = socketSend(context->socket, data, length, written, flags);
249  }
250 
251  //Return status code
252  return error;
253 }
254 
255 
256 /**
257  * @brief Receive data using the relevant transport protocol
258  * @param[in] context Pointer to the HTTP client context
259  * @param[out] data Buffer into which received data will be placed
260  * @param[in] size Maximum number of bytes that can be received
261  * @param[out] received Number of bytes that have been received
262  * @param[in] flags Set of flags that influences the behavior of this function
263  * @return Error code
264  **/
265 
267  size_t size, size_t *received, uint_t flags)
268 {
269  error_t error;
270 
271 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
272  //TLS-secured connection?
273  if(context->tlsContext != NULL)
274  {
275  //Receive TLS-encrypted data
276  error = tlsRead(context->tlsContext, data, size, received, flags);
277  }
278  else
279 #endif
280  {
281  //Receive data
282  error = socketReceive(context->socket, data, size, received, flags);
283  }
284 
285  //Return status code
286  return error;
287 }
288 
289 
290 /**
291  * @brief Save TLS session
292  * @param[in] context Pointer to the HTTP client context
293  * @return Error code
294  **/
295 
297 {
298  error_t error;
299 
300  //Initialize status code
301  error = NO_ERROR;
302 
303 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
304  //TLS-secured connection?
305  if(context->tlsContext != NULL)
306  {
307  //Save TLS session
308  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
309  }
310 #endif
311 
312  //Return status code
313  return error;
314 }
315 
316 #endif
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:1491
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:67
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:364
Transport protocol abstraction layer.
error_t httpClientOpenConnection(HttpClientContext *context)
Open network connection.
IP network address.
Definition: ip.h:90
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2071
error_t httpClientSendData(HttpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:2991
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2582
#define tlsSetSocket(context, socket)
Definition: tls.h:970
#define HttpClientContext
Definition: http_client.h:198
error_t
Error codes.
Definition: error.h:43
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:1701
void httpClientCloseConnection(HttpClientContext *context)
Close network connection.
error_t httpClientSaveSession(HttpClientContext *context)
Save TLS session.
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1354
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2029
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2922
uint8_t length
Definition: tcp.h:375
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
error_t tlsRead(TlsContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive application data from a the remote host using TLS.
Definition: tls.c:2264
error_t httpClientShutdownConnection(HttpClientContext *context)
Shutdown network connection.
HTTP client (HyperText Transfer Protocol)
#define socketBindToInterface
Definition: net_legacy.h:193
#define HTTP_CLIENT_TLS_TX_BUFFER_SIZE
Definition: http_client.h:103
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:536
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1012
error_t tlsWrite(TlsContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send application data to the remote host using TLS.
Definition: tls.c:2127
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2744
error_t httpClientEstablishConnection(HttpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
error_t httpClientReceiveData(HttpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
TCP/IP stack core.
@ SOCKET_SD_BOTH
Definition: socket.h:161
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1798
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define HTTP_CLIENT_TLS_RX_BUFFER_SIZE
Definition: http_client.h:110