Skip to content

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) &lt;&lt; (bits)) \| ((value) &gt;&gt; (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:

  • ws WebSocket session context.
  • opcode Frame type opcode (e.g., text, binary, ping).
  • data Payload data to send.
  • len Length of payload in bytes.

Returns:

SYN_OK on success, or socket error code.


function syn_websocket_task

Background task for polling active WebSockets.

SYN_PT_Status syn_websocket_task (
    SYN_PT * pt,
    SYN_Task * task
) 

Parameters:

  • pt Cooperative protothread state tracker.
  • task Scheduler 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:

  • req Incoming HTTP upgrade request.
  • resp HTTP response interface to write upgrade headers to.
  • ws WebSocket session context to initialize.
  • on_message Handled frame callback function.
  • ctx User 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.

static void base64_encode (
    const uint8_t * src,
    size_t len,
    char * dst
) 

Parameters:

  • src Source bytes.
  • len Number of bytes.
  • dst [out] Null-terminated base64 string.

function prefix_icase

static inline bool prefix_icase (
    const char * str,
    const char * prefix
) 

function sha1_final

Finalize SHA-1 and write the 20-byte digest.

static void sha1_final (
    SYN_SHA1_Ctx * ctx,
    uint8_t digest
) 

Parameters:

  • ctx SHA-1 context.
  • digest [out] 20-byte digest.

function sha1_init

Initialize SHA-1 context.

static void sha1_init (
    SYN_SHA1_Ctx * ctx
) 

Parameters:

  • ctx SHA-1 context to initialize.

function sha1_transform

Process a 64-byte SHA-1 block.

static void sha1_transform (
    uint32_t state,
    const uint8_t buffer
) 

Parameters:

  • state Running hash state.
  • buffer 64-byte input block.

function sha1_update

Feed data into the SHA-1 context.

static void sha1_update (
    SYN_SHA1_Ctx * ctx,
    const uint8_t * data,
    uint32_t len
) 

Parameters:

  • ctx SHA-1 context.
  • data Input data.
  • len Input length.

Macro Definition Documentation

define SHA1_ROL

SHA-1 rotate-left helper.

#define SHA1_ROL (
    value,
    bits
) `(((value) << (bits)) | ((value) >> (32 - (bits))))`



The documentation for this class was generated from the following file src/syntropic/net/syn_websocket.c