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-2026 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.6.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
55  context->socket = socketOpenEx(context->netContext, SOCKET_TYPE_STREAM,
57  //Failed to open socket?
58  if(context->socket == NULL)
59  return ERROR_OPEN_FAILED;
60 
61  //Associate the socket with the relevant interface
62  error = socketBindToInterface(context->socket, context->interface);
63  //Any error to report?
64  if(error)
65  return error;
66 
67  //Set timeout
68  error = socketSetTimeout(context->socket, context->timeout);
69  //Any error to report?
70  if(error)
71  return error;
72 
73 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
74  //TLS-secured connection?
75  if(context->tlsInitCallback != NULL)
76  {
77  //Allocate TLS context
78  context->tlsContext = tlsInit();
79  //Failed to allocate TLS context?
80  if(context->tlsContext == NULL)
81  return ERROR_OPEN_FAILED;
82 
83  //Select client operation mode
84  error = tlsSetConnectionEnd(context->tlsContext,
86  //Any error to report?
87  if(error)
88  return error;
89 
90  //Bind TLS to the relevant socket
91  error = tlsSetSocket(context->tlsContext, context->socket);
92  //Any error to report?
93  if(error)
94  return error;
95 
96  //Set TX and RX buffer size
97  error = tlsSetBufferSize(context->tlsContext,
99  //Any error to report?
100  if(error)
101  return error;
102 
103  //Restore TLS session
104  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
105  //Any error to report?
106  if(error)
107  return error;
108 
109  //Perform TLS related initialization
110  error = context->tlsInitCallback(context, context->tlsContext,
111  context->tlsInitParam);
112  //Any error to report?
113  if(error)
114  return error;
115  }
116 #endif
117 
118  //Successful processing
119  return NO_ERROR;
120 }
121 
122 
123 /**
124  * @brief Establish network connection
125  * @param[in] context Pointer to the HTTP client context
126  * @param[in] serverIpAddr IP address of the HTTP server to connect to
127  * @param[in] serverPort TCP port number that will be used to establish the
128  * connection
129  * @return Error code
130  **/
131 
133  const IpAddr *serverIpAddr, uint16_t serverPort)
134 {
135  error_t error;
136 
137  //Establish TCP connection
138  error = socketConnect(context->socket, serverIpAddr, serverPort);
139  //Any error to report?
140  if(error)
141  return error;
142 
143 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
144  //TLS-secured connection?
145  if(context->tlsContext != NULL)
146  {
147  //Establish TLS connection
148  error = tlsConnect(context->tlsContext);
149  //Any error to report?
150  if(error)
151  return error;
152  }
153 #endif
154 
155  //Successful processing
156  return NO_ERROR;
157 }
158 
159 
160 /**
161  * @brief Shutdown network connection
162  * @param[in] context Pointer to the HTTP client context
163  * @return Error code
164  **/
165 
167 {
168  error_t error;
169 
170  //Initialize status code
171  error = NO_ERROR;
172 
173 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
174  //Valid TLS context?
175  if(context->tlsContext != NULL)
176  {
177  //Shutdown TLS session
178  error = tlsShutdown(context->tlsContext);
179  }
180 #endif
181 
182  //Check status code
183  if(!error)
184  {
185  //Valid TCP socket?
186  if(context->socket != NULL)
187  {
188  //Shutdown TCP connection
189  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
190  }
191  }
192 
193  //Return status code
194  return error;
195 }
196 
197 
198 /**
199  * @brief Close network connection
200  * @param[in] context Pointer to the HTTP client context
201  **/
202 
204 {
205 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
206  //Release TLS context
207  if(context->tlsContext != NULL)
208  {
209  tlsFree(context->tlsContext);
210  context->tlsContext = NULL;
211  }
212 #endif
213 
214  //Close TCP connection
215  if(context->socket != NULL)
216  {
217  socketClose(context->socket);
218  context->socket = NULL;
219  }
220 }
221 
222 
223 /**
224  * @brief Send data using the relevant transport protocol
225  * @param[in] context Pointer to the HTTP client context
226  * @param[in] data Pointer to a buffer containing the data to be transmitted
227  * @param[in] length Number of bytes to be transmitted
228  * @param[out] written Actual number of bytes written (optional parameter)
229  * @param[in] flags Set of flags that influences the behavior of this function
230  * @return Error code
231  **/
232 
234  size_t length, size_t *written, uint_t flags)
235 {
236  error_t error;
237 
238 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
239  //TLS-secured connection?
240  if(context->tlsContext != NULL)
241  {
242  //Send TLS-encrypted data
243  error = tlsWrite(context->tlsContext, data, length, written, flags);
244  }
245  else
246 #endif
247  {
248  //Transmit data
249  error = socketSend(context->socket, data, length, written, flags);
250  }
251 
252  //Return status code
253  return error;
254 }
255 
256 
257 /**
258  * @brief Receive data using the relevant transport protocol
259  * @param[in] context Pointer to the HTTP client context
260  * @param[out] data Buffer into which received data will be placed
261  * @param[in] size Maximum number of bytes that can be received
262  * @param[out] received Number of bytes that have been received
263  * @param[in] flags Set of flags that influences the behavior of this function
264  * @return Error code
265  **/
266 
268  size_t size, size_t *received, uint_t flags)
269 {
270  error_t error;
271 
272 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
273  //TLS-secured connection?
274  if(context->tlsContext != NULL)
275  {
276  //Receive TLS-encrypted data
277  error = tlsRead(context->tlsContext, data, size, received, flags);
278  }
279  else
280 #endif
281  {
282  //Receive data
283  error = socketReceive(context->socket, data, size, received, flags);
284  }
285 
286  //Return status code
287  return error;
288 }
289 
290 
291 /**
292  * @brief Save TLS session
293  * @param[in] context Pointer to the HTTP client context
294  * @return Error code
295  **/
296 
298 {
299  error_t error;
300 
301  //Initialize status code
302  error = NO_ERROR;
303 
304 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
305  //TLS-secured connection?
306  if(context->tlsContext != NULL)
307  {
308  //Save TLS session
309  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
310  }
311 #endif
312 
313  //Return status code
314  return error;
315 }
316 
317 #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:1514
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:385
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:2094
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:3012
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2603
#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:1724
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:1377
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2052
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2943
uint8_t length
Definition: tcp.h:375
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:2285
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:557
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
@ 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:2148
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2765
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:169
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1819
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define HTTP_CLIENT_TLS_RX_BUFFER_SIZE
Definition: http_client.h:110