web_socket_transport.c
Go to the documentation of this file.
1 /**
2  * @file web_socket_transport.c
3  * @brief WebSocket transport 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.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL WEB_SOCKET_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "web_socket/web_socket.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (WEB_SOCKET_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open network connection
46  * @param[in] webSocket Handle to a WebSocket
47  * @return Error code
48  **/
49 
51 {
52  error_t error;
53 
54  //Invalid socket handle?
55  if(webSocket->socket == NULL)
56  {
57  //Open a TCP socket
59  //Failed to open socket?
60  if(webSocket->socket == NULL)
61  {
62  return ERROR_OPEN_FAILED;
63  }
64 
65  //Associate the WebSocket with the relevant interface
66  error = socketBindToInterface(webSocket->socket, webSocket->interface);
67  //Any error to report?
68  if(error)
69  {
70  return error;
71  }
72  }
73 
74  //Set timeout
75  error = socketSetTimeout(webSocket->socket, webSocket->timeout);
76  //Any error to report?
77  if(error)
78  {
79  return error;
80  }
81 
82 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
83  //TLS-secured connection?
84  if(webSocket->tlsInitCallback != NULL)
85  {
86  TlsConnectionEnd connectionEnd;
87 
88  //Allocate TLS context
89  webSocket->tlsContext = tlsInit();
90  //Failed to allocate TLS context?
91  if(webSocket->tlsContext == NULL)
92  {
93  return ERROR_OUT_OF_MEMORY;
94  }
95 
96  //Client or server operation?
97  if(webSocket->endpoint == WS_ENDPOINT_CLIENT)
98  {
99  connectionEnd = TLS_CONNECTION_END_CLIENT;
100  }
101  else
102  {
103  connectionEnd = TLS_CONNECTION_END_SERVER;
104  }
105 
106  //Select the relevant operation mode
107  error = tlsSetConnectionEnd(webSocket->tlsContext, connectionEnd);
108  //Any error to report?
109  if(error)
110  {
111  return error;
112  }
113 
114  //Bind TLS to the relevant socket
115  error = tlsSetSocket(webSocket->tlsContext, webSocket->socket);
116  //Any error to report?
117  if(error)
118  {
119  return error;
120  }
121 
122  //Restore TLS session, if any
123  error = tlsRestoreSessionState(webSocket->tlsContext, &webSocket->tlsSession);
124  //Any error to report?
125  if(error)
126  {
127  return error;
128  }
129 
130  //Invoke user-defined callback, if any
131  if(webSocket->tlsInitCallback != NULL)
132  {
133  //Perform TLS related initialization
134  error = webSocket->tlsInitCallback(webSocket, webSocket->tlsContext);
135  //Any error to report?
136  if(error)
137  {
138  return error;
139  }
140  }
141  }
142 #endif
143 
144  //Successful initialization
145  return NO_ERROR;
146 }
147 
148 
149 /**
150  * @brief Establish network connection
151  * @param[in] webSocket Handle to a WebSocket
152  * @param[in] serverIpAddr IP address of the WebSocket server to connect to
153  * @param[in] serverPort TCP port number that will be used to establish the
154  * connection
155  * @return Error code
156  **/
157 
159  const IpAddr *serverIpAddr, uint16_t serverPort)
160 {
161  error_t error;
162 
163  //Connect to WebSocket server
164  error = socketConnect(webSocket->socket, serverIpAddr, serverPort);
165 
166 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
167  //TLS-secured connection?
168  if(webSocket->tlsInitCallback != NULL)
169  {
170  //Check status code
171  if(!error)
172  {
173  //Establish TLS connection
174  error = tlsConnect(webSocket->tlsContext);
175  }
176  }
177 #endif
178 
179  //Return status code
180  return error;
181 }
182 
183 
184 /**
185  * @brief Shutdown network connection
186  * @param[in] webSocket Handle to a WebSocket
187  * @return Error code
188  **/
189 
191 {
192  error_t error;
193  size_t n;
194 
195  //Initialize status code
196  error = NO_ERROR;
197 
198 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
199  //Check whether a secure connection is being used
200  if(webSocket->tlsContext != NULL)
201  {
202  //Shutdown TLS connection
203  error = tlsShutdown(webSocket->tlsContext);
204  }
205 #endif
206 
207  //Check status code
208  if(!error)
209  {
210  //Further transmissions are disallowed
211  error = socketShutdown(webSocket->socket, SOCKET_SD_SEND);
212  }
213 
214  //Receive data until until the peer has also performed an orderly shutdown
215  while(!error)
216  {
217  //Discard data
218  error = socketReceive(webSocket->socket, webSocket->rxContext.buffer,
220  }
221 
222  //Check whether the connection has been properly closed
223  if(error == ERROR_END_OF_STREAM)
224  {
225  error = NO_ERROR;
226  }
227 
228  //Return status code
229  return error;
230 }
231 
232 
233 /**
234  * @brief Close network connection
235  * @param[in] webSocket Handle to a WebSocket
236  **/
237 
239 {
240 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
241  //Check whether a secure connection is being used
242  if(webSocket->tlsContext != NULL)
243  {
244  //Save TLS session
245  tlsSaveSessionState(webSocket->tlsContext, &webSocket->tlsSession);
246 
247  //Release TLS context
248  tlsFree(webSocket->tlsContext);
249  webSocket->tlsContext = NULL;
250  }
251 #endif
252 
253  //Close TCP connection
254  if(webSocket->socket != NULL)
255  {
256  socketClose(webSocket->socket);
257  webSocket->socket = NULL;
258  }
259 }
260 
261 
262 /**
263  * @brief Send data using the relevant transport protocol
264  * @param[in] webSocket Handle to a WebSocket
265  * @param[in] data Pointer to a buffer containing the data to be transmitted
266  * @param[in] length Number of bytes to be transmitted
267  * @param[out] written Actual number of bytes written (optional parameter)
268  * @param[in] flags Set of flags that influences the behavior of this function
269  * @return Error code
270  **/
271 
272 error_t webSocketSendData(WebSocket *webSocket, const void *data,
273  size_t length, size_t *written, uint_t flags)
274 {
275  error_t error;
276 
277 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
278  //Check whether a secure connection is being used
279  if(webSocket->tlsContext != NULL)
280  {
281  //Use TLS to transmit data
282  error = tlsWrite(webSocket->tlsContext, data, length, written, flags);
283  }
284  else
285 #endif
286  {
287  //Transmit data
288  error = socketSend(webSocket->socket, data, length, written, flags);
289  }
290 
291  //Return status code
292  return error;
293 }
294 
295 
296 /**
297  * @brief Receive data using the relevant transport protocol
298  * @param[in] webSocket Handle to a WebSocket
299  * @param[out] data Buffer into which received data will be placed
300  * @param[in] size Maximum number of bytes that can be received
301  * @param[out] received Number of bytes that have been received
302  * @param[in] flags Set of flags that influences the behavior of this function
303  * @return Error code
304  **/
305 
307  size_t size, size_t *received, uint_t flags)
308 {
309  error_t error;
310 
311 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
312  //Check whether a secure connection is being used
313  if(webSocket->tlsContext != NULL)
314  {
315  //Use TLS to receive data
316  error = tlsRead(webSocket->tlsContext, data, size, received, flags);
317  }
318  else
319 #endif
320  {
321  //Receive data
322  error = socketReceive(webSocket->socket, data, size, received, flags);
323  }
324 
325  //Return status code
326  return error;
327 }
328 
329 #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:1605
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:68
#define WEB_SOCKET_BUFFER_SIZE
Definition: web_socket.h:82
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:392
error_t webSocketOpenConnection(WebSocket *webSocket)
Open network connection.
IP network address.
Definition: ip.h:97
WebSocket API (client and server)
TlsConnectionEnd
TLS connection end.
Definition: tls.h:1075
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2169
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_END_OF_STREAM
Definition: error.h:212
void webSocketCloseConnection(WebSocket *webSocket)
Close network connection.
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:3326
error_t webSocketSendData(WebSocket *webSocket, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ SOCKET_SD_SEND
Definition: socket.h:160
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2864
#define tlsSetSocket(context, socket)
Definition: tls.h:1034
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:1820
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:1077
error_t webSocketEstablishConnection(WebSocket *webSocket, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t webSocketReceiveData(WebSocket *webSocket, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1458
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2123
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:3255
uint8_t length
Definition: tcp.h:375
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:123
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:2517
@ WS_ENDPOINT_CLIENT
Definition: web_socket.h:191
#define WebSocket
Definition: web_socket.h:177
#define socketBindToInterface
Definition: net_legacy.h:193
uint8_t flags
Definition: tcp.h:358
uint8_t n
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1076
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:2368
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:3065
error_t webSocketShutdownConnection(WebSocket *webSocket)
Shutdown network connection.
WebSocket transport layer.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ 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:166
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:2008
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.