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.0
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  //Any error to report?
111  if(error)
112  return error;
113  }
114 #endif
115 
116  //Successful processing
117  return NO_ERROR;
118 }
119 
120 
121 /**
122  * @brief Establish network connection
123  * @param[in] context Pointer to the HTTP client context
124  * @param[in] serverIpAddr IP address of the HTTP server to connect to
125  * @param[in] serverPort TCP port number that will be used to establish the
126  * connection
127  * @return Error code
128  **/
129 
131  const IpAddr *serverIpAddr, uint16_t serverPort)
132 {
133  error_t error;
134 
135  //Establish TCP connection
136  error = socketConnect(context->socket, serverIpAddr, serverPort);
137  //Any error to report?
138  if(error)
139  return error;
140 
141 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
142  //TLS-secured connection?
143  if(context->tlsContext != NULL)
144  {
145  //Establish TLS connection
146  error = tlsConnect(context->tlsContext);
147  //Any error to report?
148  if(error)
149  return error;
150  }
151 #endif
152 
153  //Successful processing
154  return NO_ERROR;
155 }
156 
157 
158 /**
159  * @brief Shutdown network connection
160  * @param[in] context Pointer to the HTTP client context
161  * @return Error code
162  **/
163 
165 {
166  error_t error;
167 
168  //Initialize status code
169  error = NO_ERROR;
170 
171 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
172  //Valid TLS context?
173  if(context->tlsContext != NULL)
174  {
175  //Shutdown TLS session
176  error = tlsShutdown(context->tlsContext);
177  }
178 #endif
179 
180  //Check status code
181  if(!error)
182  {
183  //Valid TCP socket?
184  if(context->socket != NULL)
185  {
186  //Shutdown TCP connection
187  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
188  }
189  }
190 
191  //Return status code
192  return error;
193 }
194 
195 
196 /**
197  * @brief Close network connection
198  * @param[in] context Pointer to the HTTP client context
199  **/
200 
202 {
203 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
204  //Release TLS context
205  if(context->tlsContext != NULL)
206  {
207  tlsFree(context->tlsContext);
208  context->tlsContext = NULL;
209  }
210 #endif
211 
212  //Close TCP connection
213  if(context->socket != NULL)
214  {
215  socketClose(context->socket);
216  context->socket = NULL;
217  }
218 }
219 
220 
221 /**
222  * @brief Send data using the relevant transport protocol
223  * @param[in] context Pointer to the HTTP client context
224  * @param[in] data Pointer to a buffer containing the data to be transmitted
225  * @param[in] length Number of bytes to be transmitted
226  * @param[out] written Actual number of bytes written (optional parameter)
227  * @param[in] flags Set of flags that influences the behavior of this function
228  * @return Error code
229  **/
230 
232  size_t length, size_t *written, uint_t flags)
233 {
234  error_t error;
235 
236 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
237  //TLS-secured connection?
238  if(context->tlsContext != NULL)
239  {
240  //Send TLS-encrypted data
241  error = tlsWrite(context->tlsContext, data, length, written, flags);
242  }
243  else
244 #endif
245  {
246  //Transmit data
247  error = socketSend(context->socket, data, length, written, flags);
248  }
249 
250  //Return status code
251  return error;
252 }
253 
254 
255 /**
256  * @brief Receive data using the relevant transport protocol
257  * @param[in] context Pointer to the HTTP client context
258  * @param[out] data Buffer into which received data will be placed
259  * @param[in] size Maximum number of bytes that can be received
260  * @param[out] received Number of bytes that have been received
261  * @param[in] flags Set of flags that influences the behavior of this function
262  * @return Error code
263  **/
264 
266  size_t size, size_t *received, uint_t flags)
267 {
268  error_t error;
269 
270 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
271  //TLS-secured connection?
272  if(context->tlsContext != NULL)
273  {
274  //Receive TLS-encrypted data
275  error = tlsRead(context->tlsContext, data, size, received, flags);
276  }
277  else
278 #endif
279  {
280  //Receive data
281  error = socketReceive(context->socket, data, size, received, flags);
282  }
283 
284  //Return status code
285  return error;
286 }
287 
288 
289 /**
290  * @brief Save TLS session
291  * @param[in] context Pointer to the HTTP client context
292  * @return Error code
293  **/
294 
296 {
297  error_t error;
298 
299  //Initialize status code
300  error = NO_ERROR;
301 
302 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
303  //TLS-secured connection?
304  if(context->tlsContext != NULL)
305  {
306  //Save TLS session
307  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
308  }
309 #endif
310 
311  //Return status code
312  return error;
313 }
314 
315 #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:357
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:222
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2067
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:2818
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2423
#define tlsSetSocket(context, socket)
Definition: tls.h:949
#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:1697
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:2025
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2749
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:2105
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
uint8_t flags
Definition: tcp.h:358
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:529
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:990
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:1970
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2585
error_t httpClientEstablishConnection(HttpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
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:1730
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define HTTP_CLIENT_TLS_RX_BUFFER_SIZE
Definition: http_client.h:110