File syn_websocket.c¶
FileList > net > syn_websocket.c
Go to the source code of this file
WebSocket protocol implementation.
#include "../util/syn_assert.h"#include "../util/syn_pack.h"#include "syn_websocket.h"#include <stdio.h>#include <string.h>#include "../util/syn_fmt.h"
Classes¶
| Type | Name |
|---|---|
| struct | SYN_SHA1_Ctx SHA-1 hashing algorithm context. |
Public Functions¶
| Type | Name |
|---|---|
| SYN_Status | syn_websocket_send (SYN_WebsocketSession * ws, uint8_t opcode, const void * data, size_t len) Send a frame over WebSocket. |
| SYN_PT_Status | syn_websocket_task (SYN_PT * pt, SYN_Task * task) Background task for polling active WebSockets. |
| SYN_Status | syn_websocket_upgrade (const SYN_HttpdRequest * req, SYN_HttpdResponse * resp, SYN_WebsocketSession * ws, void(*)(const uint8_t *payload, size_t len, uint8_t opcode, void *ctx) on_message, void * ctx) Handle upgrading a connection in an httpd route handler to WebSocket. |
Public Static Functions¶
| Type | Name |
|---|---|
| void | base64_encode (const uint8_t * src, size_t len, char * dst) Base64 encode a byte array. |
| bool | prefix_icase (const char * str, const char * prefix) |
| void | sha1_final (SYN_SHA1_Ctx * ctx, uint8_t digest) Finalize SHA-1 and write the 20-byte digest. |
| void | sha1_init (SYN_SHA1_Ctx * ctx) Initialize SHA-1 context. |
| void | sha1_transform (uint32_t state, const uint8_t buffer) Process a 64-byte SHA-1 block. |
| void | sha1_update (SYN_SHA1_Ctx * ctx, const uint8_t * data, uint32_t len) Feed data into the SHA-1 context. |
Macros¶
| Type | Name |
|---|---|
| define | SHA1_ROL (value, bits) (((value) << (bits)) \| ((value) >> (32 - (bits))))SHA-1 rotate-left helper. |
Public Functions Documentation¶
function syn_websocket_send¶
Send a frame over WebSocket.
SYN_Status syn_websocket_send (
SYN_WebsocketSession * ws,
uint8_t opcode,
const void * data,
size_t len
)
Formats a WebSocket packet header and writes the payload to the socket.
Parameters:
wsWebSocket session context.opcodeFrame type opcode (e.g., text, binary, ping).dataPayload data to send.lenLength of payload in bytes.
Returns:
SYN_OK on success, or socket error code.
function syn_websocket_task¶
Background task for polling active WebSockets.
Parameters:
ptCooperative protothread state tracker.taskScheduler task control block.
Returns:
PT_WAITING or PT_EXITED status.
function syn_websocket_upgrade¶
Handle upgrading a connection in an httpd route handler to WebSocket.
SYN_Status syn_websocket_upgrade (
const SYN_HttpdRequest * req,
SYN_HttpdResponse * resp,
SYN_WebsocketSession * ws,
void(*)(const uint8_t *payload, size_t len, uint8_t opcode, void *ctx) on_message,
void * ctx
)
Performs handshake, sends 101 status, sets upgraded = true on the response.
Parameters:
reqIncoming HTTP upgrade request.respHTTP response interface to write upgrade headers to.wsWebSocket session context to initialize.on_messageHandled frame callback function.ctxUser context pointer passed through to callback.
Returns:
SYN_OK on successful handshake, SYN_ERROR on negotiation failure.
Public Static Functions Documentation¶
function base64_encode¶
Base64 encode a byte array.
Parameters:
srcSource bytes.lenNumber of bytes.dst[out] Null-terminated base64 string.
function prefix_icase¶
function sha1_final¶
Finalize SHA-1 and write the 20-byte digest.
Parameters:
ctxSHA-1 context.digest[out] 20-byte digest.
function sha1_init¶
Initialize SHA-1 context.
Parameters:
ctxSHA-1 context to initialize.
function sha1_transform¶
Process a 64-byte SHA-1 block.
Parameters:
stateRunning hash state.buffer64-byte input block.
function sha1_update¶
Feed data into the SHA-1 context.
Parameters:
ctxSHA-1 context.dataInput data.lenInput length.
Macro Definition Documentation¶
define SHA1_ROL¶
SHA-1 rotate-left helper.
The documentation for this class was generated from the following file src/syntropic/net/syn_websocket.c