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-2024 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.4.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, TLS_CONNECTION_END_CLIENT);
84  //Any error to report?
85  if(error)
86  return error;
87 
88  //Bind TLS to the relevant socket
89  error = tlsSetSocket(context->tlsContext, context->socket);
90  //Any error to report?
91  if(error)
92  return error;
93 
94  //Set TX and RX buffer size
95  error = tlsSetBufferSize(context->tlsContext,
97  //Any error to report?
98  if(error)
99  return error;
100 
101  //Restore TLS session
102  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
103  //Any error to report?
104  if(error)
105  return error;
106 
107  //Perform TLS related initialization
108  error = context->tlsInitCallback(context, context->tlsContext);
109  //Any error to report?
110  if(error)
111  return error;
112  }
113 #endif
114 
115  //Successful processing
116  return NO_ERROR;
117 }
118 
119 
120 /**
121  * @brief Establish network connection
122  * @param[in] context Pointer to the HTTP client context
123  * @param[in] serverIpAddr IP address of the HTTP server to connect to
124  * @param[in] serverPort TCP port number that will be used to establish the
125  * connection
126  * @return Error code
127  **/
128 
130  const IpAddr *serverIpAddr, uint16_t serverPort)
131 {
132  error_t error;
133 
134  //Establish TCP connection
135  error = socketConnect(context->socket, serverIpAddr, serverPort);
136  //Any error to report?
137  if(error)
138  return error;
139 
140 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
141  //TLS-secured connection?
142  if(context->tlsContext != NULL)
143  {
144  //Establish TLS connection
145  error = tlsConnect(context->tlsContext);
146  //Any error to report?
147  if(error)
148  return error;
149  }
150 #endif
151 
152  //Successful processing
153  return NO_ERROR;
154 }
155 
156 
157 /**
158  * @brief Shutdown network connection
159  * @param[in] context Pointer to the HTTP client context
160  * @return Error code
161  **/
162 
164 {
165  error_t error;
166 
167  //Initialize status code
168  error = NO_ERROR;
169 
170 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
171  //Valid TLS context?
172  if(context->tlsContext != NULL)
173  {
174  //Shutdown TLS session
175  error = tlsShutdown(context->tlsContext);
176  }
177 #endif
178 
179  //Check status code
180  if(!error)
181  {
182  //Valid TCP socket?
183  if(context->socket != NULL)
184  {
185  //Shutdown TCP connection
186  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
187  }
188  }
189 
190  //Return status code
191  return error;
192 }
193 
194 
195 /**
196  * @brief Close network connection
197  * @param[in] context Pointer to the HTTP client context
198  **/
199 
201 {
202 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
203  //Release TLS context
204  if(context->tlsContext != NULL)
205  {
206  tlsFree(context->tlsContext);
207  context->tlsContext = NULL;
208  }
209 #endif
210 
211  //Close TCP connection
212  if(context->socket != NULL)
213  {
214  socketClose(context->socket);
215  context->socket = NULL;
216  }
217 }
218 
219 
220 /**
221  * @brief Send data using the relevant transport protocol
222  * @param[in] context Pointer to the HTTP client context
223  * @param[in] data Pointer to a buffer containing the data to be transmitted
224  * @param[in] length Number of bytes to be transmitted
225  * @param[out] written Actual number of bytes written (optional parameter)
226  * @param[in] flags Set of flags that influences the behavior of this function
227  * @return Error code
228  **/
229 
231  size_t length, size_t *written, uint_t flags)
232 {
233  error_t error;
234 
235 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
236  //TLS-secured connection?
237  if(context->tlsContext != NULL)
238  {
239  //Send TLS-encrypted data
240  error = tlsWrite(context->tlsContext, data, length, written, flags);
241  }
242  else
243 #endif
244  {
245  //Transmit data
246  error = socketSend(context->socket, data, length, written, flags);
247  }
248 
249  //Return status code
250  return error;
251 }
252 
253 
254 /**
255  * @brief Receive data using the relevant transport protocol
256  * @param[in] context Pointer to the HTTP client context
257  * @param[out] data Buffer into which received data will be placed
258  * @param[in] size Maximum number of bytes that can be received
259  * @param[out] received Number of bytes that have been received
260  * @param[in] flags Set of flags that influences the behavior of this function
261  * @return Error code
262  **/
263 
265  size_t size, size_t *received, uint_t flags)
266 {
267  error_t error;
268 
269 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
270  //TLS-secured connection?
271  if(context->tlsContext != NULL)
272  {
273  //Receive TLS-encrypted data
274  error = tlsRead(context->tlsContext, data, size, received, flags);
275  }
276  else
277 #endif
278  {
279  //Receive data
280  error = socketReceive(context->socket, data, size, received, flags);
281  }
282 
283  //Return status code
284  return error;
285 }
286 
287 
288 /**
289  * @brief Save TLS session
290  * @param[in] context Pointer to the HTTP client context
291  * @return Error code
292  **/
293 
295 {
296  error_t error;
297 
298  //Initialize status code
299  error = NO_ERROR;
300 
301 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
302  //TLS-secured connection?
303  if(context->tlsContext != NULL)
304  {
305  //Save TLS session
306  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
307  }
308 #endif
309 
310  //Return status code
311  return error;
312 }
313 
314 #endif
unsigned int uint_t
Definition: compiler_port.h:50
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
HTTP client (HyperText Transfer Protocol)
#define HTTP_CLIENT_TLS_TX_BUFFER_SIZE
Definition: http_client.h:103
#define HttpClientContext
Definition: http_client.h:198
#define HTTP_CLIENT_TLS_RX_BUFFER_SIZE
Definition: http_client.h:110
error_t httpClientShutdownConnection(HttpClientContext *context)
Shutdown network connection.
error_t httpClientSaveSession(HttpClientContext *context)
Save TLS session.
void httpClientCloseConnection(HttpClientContext *context)
Close network connection.
error_t httpClientEstablishConnection(HttpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t httpClientReceiveData(HttpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
error_t httpClientOpenConnection(HttpClientContext *context)
Open network connection.
error_t httpClientSendData(HttpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
Transport protocol abstraction layer.
TCP/IP stack core.
#define socketBindToInterface
Definition: net_legacy.h:193
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:1152
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
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:946
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:1517
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:1480
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:811
@ SOCKET_SD_BOTH
Definition: socket.h:151
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:100
@ SOCKET_TYPE_STREAM
Definition: socket.h:85
IP network address.
Definition: ip.h:79
uint8_t length
Definition: tcp.h:368
uint8_t flags
Definition: tcp.h:351
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:2690
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1758
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2621
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:1849
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:65
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:1984
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:344
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:516
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2302
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2464
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:953
#define tlsSetSocket(context, socket)
Definition: tls.h:912