Go to the documentation of this file.
39 #ifndef WEB_SOCKET_SUPPORT
40 #define WEB_SOCKET_SUPPORT DISABLED
41 #elif (WEB_SOCKET_SUPPORT != ENABLED && WEB_SOCKET_SUPPORT != DISABLED)
42 #error WEB_SOCKET_SUPPORT parameter is not valid
46 #ifndef WEB_SOCKET_MAX_COUNT
47 #define WEB_SOCKET_MAX_COUNT 4
48 #elif (WEB_SOCKET_MAX_COUNT < 1)
49 #error WEB_SOCKET_MAX_COUNT parameter is not valid
53 #ifndef WEB_SOCKET_TLS_SUPPORT
54 #define WEB_SOCKET_TLS_SUPPORT DISABLED
55 #elif (WEB_SOCKET_TLS_SUPPORT != ENABLED && WEB_SOCKET_TLS_SUPPORT != DISABLED)
56 #error WEB_SOCKET_TLS_SUPPORT parameter is not valid
60 #ifndef WEB_SOCKET_BASIC_AUTH_SUPPORT
61 #define WEB_SOCKET_BASIC_AUTH_SUPPORT DISABLED
62 #elif (WEB_SOCKET_BASIC_AUTH_SUPPORT != ENABLED && WEB_SOCKET_BASIC_AUTH_SUPPORT != DISABLED)
63 #error WEB_SOCKET_BASIC_AUTH_SUPPORT parameter is not valid
67 #ifndef WEB_SOCKET_DIGEST_AUTH_SUPPORT
68 #define WEB_SOCKET_DIGEST_AUTH_SUPPORT DISABLED
69 #elif (WEB_SOCKET_DIGEST_AUTH_SUPPORT != ENABLED && WEB_SOCKET_DIGEST_AUTH_SUPPORT != DISABLED)
70 #error WEB_SOCKET_DIGEST_AUTH_SUPPORT parameter is not valid
74 #ifndef WEB_SOCKET_MAX_CONN_RETRIES
75 #define WEB_SOCKET_MAX_CONN_RETRIES 3
76 #elif (WEB_SOCKET_MAX_CONN_RETRIES < 1)
77 #error WEB_SOCKET_MAX_CONN_RETRIES parameter is not valid
81 #ifndef WEB_SOCKET_BUFFER_SIZE
82 #define WEB_SOCKET_BUFFER_SIZE 1024
83 #elif (WEB_SOCKET_BUFFER_SIZE < 128)
84 #error WEB_SOCKET_BUFFER_SIZE parameter is not valid
88 #ifndef WEB_SOCKET_HOST_MAX_LEN
89 #define WEB_SOCKET_HOST_MAX_LEN 32
90 #elif (WEB_SOCKET_HOST_MAX_LEN < 1)
91 #error WEB_SOCKET_HOST_MAX_LEN parameter is not valid
95 #ifndef WEB_SOCKET_ORIGIN_MAX_LEN
96 #define WEB_SOCKET_ORIGIN_MAX_LEN 16
97 #elif (WEB_SOCKET_ORIGIN_MAX_LEN < 1)
98 #error WEB_SOCKET_ORIGIN_MAX_LEN parameter is not valid
102 #ifndef WEB_SOCKET_SUB_PROTOCOL_MAX_LEN
103 #define WEB_SOCKET_SUB_PROTOCOL_MAX_LEN 8
104 #elif (WEB_SOCKET_SUB_PROTOCOL_MAX_LEN < 1)
105 #error WEB_SOCKET_SUB_PROTOCOL_MAX_LEN parameter is not valid
109 #ifndef WEB_SOCKET_URI_MAX_LEN
110 #define WEB_SOCKET_URI_MAX_LEN 32
111 #elif (WEB_SOCKET_URI_MAX_LEN < 1)
112 #error WEB_SOCKET_URI_MAX_LEN parameter is not valid
116 #ifndef WEB_SOCKET_QUERY_STRING_MAX_LEN
117 #define WEB_SOCKET_QUERY_STRING_MAX_LEN 32
118 #elif (WEB_SOCKET_QUERY_STRING_MAX_LEN < 1)
119 #error WEB_SOCKET_QUERY_STRING_MAX_LEN parameter is not valid
123 #ifndef WEB_SOCKET_REALM_MAX_LEN
124 #define WEB_SOCKET_REALM_MAX_LEN 32
125 #elif (WEB_SOCKET_REALM_MAX_LEN < 1)
126 #error WEB_SOCKET_REALM_MAX_LEN parameter is not valid
130 #ifndef WEB_SOCKET_USERNAME_MAX_LEN
131 #define WEB_SOCKET_USERNAME_MAX_LEN 16
132 #elif (WEB_SOCKET_USERNAME_MAX_LEN < 1)
133 #error WEB_SOCKET_USERNAME_MAX_LEN parameter is not valid
137 #ifndef WEB_SOCKET_PASSWORD_MAX_LEN
138 #define WEB_SOCKET_PASSWORD_MAX_LEN 16
139 #elif (WEB_SOCKET_PASSWORD_MAX_LEN < 1)
140 #error WEB_SOCKET_PASSWORD_MAX_LEN parameter is not valid
144 #ifndef WEB_SOCKET_NONCE_MAX_LEN
145 #define WEB_SOCKET_NONCE_MAX_LEN 32
146 #elif (WEB_SOCKET_NONCE_MAX_LEN < 1)
147 #error WEB_SOCKET_NONCE_MAX_LEN parameter is not valid
151 #ifndef WEB_SOCKET_OPAQUE_MAX_LEN
152 #define WEB_SOCKET_OPAQUE_MAX_LEN 32
153 #elif (WEB_SOCKET_OPAQUE_MAX_LEN < 1)
154 #error WEB_SOCKET_OPAQUE_MAX_LEN parameter is not valid
158 #ifndef WEB_SOCKET_CNONCE_SIZE
159 #define WEB_SOCKET_CNONCE_SIZE 16
160 #elif (WEB_SOCKET_CNONCE_SIZE < 1)
161 #error WEB_SOCKET_CNONCE_SIZE parameter is not valid
165 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
171 #define WEB_SOCKET_CLIENT_KEY_SIZE 24
173 #define WEB_SOCKET_SERVER_KEY_SIZE 28
177 #define WebSocket struct _WebSocket
295 #if defined(__CCRX__)
297 #elif defined(__CWCC__) || defined(_WIN32)
298 #pragma pack(push, 1)
308 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
326 #if defined(__CCRX__)
328 #elif defined(__CWCC__) || defined(_WIN32)
341 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
365 #if (WEB_SOCKET_DIGEST_AUTH_SUPPORT == ENABLED)
405 uint8_t maskingKey[4];
445 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
451 #if (WEB_SOCKET_BASIC_AUTH_SUPPORT == ENABLED || WEB_SOCKET_DIGEST_AUTH_SUPPORT == ENABLED)
472 #if (WEB_SOCKET_TLS_SUPPORT == ENABLED)
493 uint16_t serverPort,
const char_t *uri);
#define WEB_SOCKET_BUFFER_SIZE
@ WS_STATUS_CODE_GOING_AWAY
@ WS_STATUS_CODE_UNSUPPORTED_DATA
error_t webSocketSetAuthInfo(WebSocket *webSocket, const char_t *username, const char_t *password, uint_t allowedAuthModes)
Set authentication information.
@ WS_STATUS_CODE_TLS_HANDSHAKE
NetInterface * interface
Underlying network interface.
void * tlsInitParam
Opaque pointer passed to the TLS initialization callback function.
WebSocket * webSocketUpgradeSocket(Socket *socket)
Upgrade a socket to a WebSocket.
WebSocketUtf8Context utf8Context
bool_t mask
Defines whether the payload data is masked.
#define WEB_SOCKET_CLIENT_KEY_SIZE
@ WS_SUB_STATE_FRAME_PAYLOAD
error_t webSocketSetSubProtocol(WebSocket *webSocket, const char_t *subProtocol)
Set the sub-protocol header field.
WebSocket * webSocketUpgradeSecureSocket(Socket *socket, TlsContext *tlsContext)
Upgrade a secure socket to a secure WebSocket.
#define WEB_SOCKET_HOST_MAX_LEN
WebSocketHttpVersion
HTTP version numbers.
error_t webSocketReceiveEx(WebSocket *webSocket, void *data, size_t size, WebSocketFrameType *type, size_t *received, bool_t *firstFrag, bool_t *lastFrag)
Receive data from a WebSocket connection.
error_t webSocketSendServerHandshake(WebSocket *webSocket)
Send server's handshake.
@ WS_STATUS_CODE_NO_STATUS_RCVD
WebSocketTlsInitCallback tlsInitCallback
TLS initialization callback function.
error_t(* WebSocketRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
#define WEB_SOCKET_SUB_PROTOCOL_MAX_LEN
#define WEB_SOCKET_ORIGIN_MAX_LEN
error_t webSocketSendEx(WebSocket *webSocket, const void *data, size_t length, WebSocketFrameType type, size_t *written, bool_t firstFrag, bool_t lastFrag)
Transmit data over the WebSocket connection.
error_t webSocketShutdown(WebSocket *webSocket)
Gracefully close a WebSocket connection.
#define WEB_SOCKET_CNONCE_SIZE
char_t host[WEB_SOCKET_HOST_MAX_LEN+1]
Domain name of the server (for virtual hosting)
WebSocketEndpoint endpoint
Endpoint type (client or server)
error_t webSocketSetHost(WebSocket *webSocket, const char_t *host)
Set the domain name of the server (for virtual hosting)
WebSocketEndpoint
WebSocket endpoint types.
error_t webSocketRegisterTlsInitCallback(WebSocket *webSocket, WebSocketTlsInitCallback callback)
Register TLS initialization callback function.
error_t webSocketParseClientHandshake(WebSocket *webSocket)
Parse client's handshake.
WebSocketFrameType controlFrameType
Control frame type.
WebSocketFrameContext txContext
bool_t webSocketIsRxReady(WebSocket *webSocket)
Check whether some data is available in the receive buffer.
@ WS_SUB_STATE_HANDSHAKE_HEADER_FIELD
WebSocketAuthMode selectedAuthMode
@ WS_SUB_STATE_FRAME_HEADER
#define WEB_SOCKET_NONCE_MAX_LEN
TlsContext * tlsContext
TLS context.
size_t bufferLen
Length of the data buffer.
size_t payloadPos
Current position.
Frame encoding/decoding context.
error_t(* WebSocketTlsInitCallback)(WebSocket *webSocket, TlsContext *tlsContext)
TLS initialization callback function.
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
WebSocketFrameType
WebSocket frame types.
WebSocketState state
WebSocket connection state.
error_t webSocketReceive(WebSocket *webSocket, void *data, size_t size, WebSocketFrameType *type, size_t *received)
Receive data from a WebSocket connection.
char_t subProtocol[WEB_SOCKET_SUB_PROTOCOL_MAX_LEN+1]
systime_t timeout
timeout value for blocking operations
Socket * socket
Underlying TCP socket.
General definitions for cryptographic algorithms.
WebSocketFrameType dataFrameType
FSM state.
error_t webSocketConnect(WebSocket *webSocket, const IpAddr *serverIpAddr, uint16_t serverPort, const char_t *uri)
Establish a WebSocket connection.
size_t payloadLen
Payload length.
@ WS_STATUS_CODE_MESSAGE_TOO_BIG
@ WS_STATUS_CODE_ABNORMAL_CLOSURE
typedef __packed_struct
WebSocket frame.
bool_t closingFrameReceived
WebSocketAuthMode requiredAuthMode
@ WS_STATUS_CODE_INTERNAL_ERROR
WebSocketState
WebSocket states.
@ WS_SUB_STATE_HANDSHAKE_LEADING_LINE
error_t webSocketBindToInterface(WebSocket *webSocket, NetInterface *interface)
Bind the WebSocket to a particular network interface.
TlsSessionState tlsSession
TLS session state.
char_t queryString[WEB_SOCKET_QUERY_STRING_MAX_LEN+1]
@ WS_STATUS_CODE_POLICY_VIOLATION
char_t uri[WEB_SOCKET_URI_MAX_LEN+1]
WebSocket * webSocketOpen(void)
Create a WebSocket.
uint32_t systime_t
System time.
error_t webSocketSetClientKey(WebSocket *webSocket, const char_t *clientKey)
Set client's key.
error_t webSocketSendErrorResponse(WebSocket *webSocket, uint_t statusCode, const char_t *message)
Send HTTP error response to the client.
Structure describing a WebSocket.
WebSocketStatusCode
WebSocket status codes.
#define WEB_SOCKET_PASSWORD_MAX_LEN
#define WEB_SOCKET_OPAQUE_MAX_LEN
@ WS_STATUS_CODE_INVALID_PAYLOAD_DATA
error_t webSocketInit(void)
WebSocket related initialization.
WebSocketAuthMode
Authentication schemes.
@ WS_FRAME_TYPE_CONTINUATION
@ WS_STATE_SERVER_RESP_BODY
bool_t fin
Final fragment in a message.
WebSocketAuthContext authContext
@ WS_STATE_CLIENT_HANDSHAKE
TLS (Transport Layer Security)
@ WS_SUB_STATE_FRAME_EXT_HEADER
@ WS_STATUS_CODE_MANDATORY_EXT
@ WS_SUB_STATE_HANDSHAKE_LWSP
#define WEB_SOCKET_QUERY_STRING_MAX_LEN
@ WS_STATE_SERVER_HANDSHAKE
error_t webSocketSend(WebSocket *webSocket, const void *data, size_t length, WebSocketFrameType type, size_t *written)
Transmit data over the WebSocket connection.
#define WEB_SOCKET_SERVER_KEY_SIZE
@ WS_STATUS_CODE_PROTOCOL_ERROR
size_t bufferPos
Current position.
error_t webSocketSetOrigin(WebSocket *webSocket, const char_t *origin)
Set the origin header field.
char_t origin[WEB_SOCKET_ORIGIN_MAX_LEN+1]
WebSocketSubState
WebSocket sub-states.
WebSocketRandCallback webSockRandCallback
#define WEB_SOCKET_URI_MAX_LEN
WebSocketFrameContext rxContext
@ WS_STATUS_CODE_NORMAL_CLOSURE
#define WEB_SOCKET_REALM_MAX_LEN
WebSocketHandshakeContext handshakeContext
#define WEB_SOCKET_USERNAME_MAX_LEN
error_t webSocketRegisterRandCallback(WebSocketRandCallback callback)
Register RNG callback function.
void webSocketClose(WebSocket *webSocket)
Close a WebSocket connection.
error_t webSocketSetTimeout(WebSocket *webSocket, systime_t timeout)
Set timeout value for blocking operations.