Classes | Macros | Typedefs | Functions | Variables
WebSocket.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "WebSocket.h"
#include "Base64.h"
#include "Log.h"
#include "SHA1.h"
#include "LinkedList.h"
#include "MQTTProtocolOut.h"
#include "SocketBuffer.h"
#include "StackTrace.h"
#include "Socket.h"
#include <limits.h>
#include <stdlib.h>
#include "Heap.h"
Include dependency graph for WebSocket.c:

Go to the source code of this file.

Classes

struct  frameData
 builds a websocket frame for data transmission More...
 
struct  ws_frame
 

Macros

#define HTTP_PROTOCOL(x)   x ? "https" : "http"
 

Typedefs

typedef unsigned char uuid_t[16]
 raw uuid type More...
 

Functions

void uuid_generate (uuid_t out)
 generates a uuid, compatible with RFC 4122, version 4 (random) More...
 
void uuid_unparse (uuid_t uu, char *out)
 converts a uuid to a string More...
 
static struct frameData WebSocket_buildFrame (networkHandles *net, int opcode, int mask_data, char **pbuf0, size_t *pbuf0len, PacketBuffers *bufs)
 
size_t WebSocket_calculateFrameHeaderSize (networkHandles *net, int mask_data, size_t data_len)
 
void WebSocket_close (networkHandles *net, int status_code, const char *reason)
 
int WebSocket_connect (networkHandles *net, const char *uri)
 
size_t WebSocket_framePos ()
 
void WebSocket_framePosSeekTo (size_t pos)
 
int WebSocket_getch (networkHandles *net, char *c)
 receives 1 byte from a socket More...
 
char * WebSocket_getdata (networkHandles *net, size_t bytes, size_t *actual_len)
 receives data from a socket. It should receive all data from the socket that is immediately available. Because it is encapsulated in websocket frames which cannot be More...
 
static char * WebSocket_getRawSocketData (networkHandles *net, size_t bytes, size_t *actual_len, int *rc)
 
static void WebSocket_pong (networkHandles *net, char *app_data, size_t app_data_len)
 
int WebSocket_proxy_connect (networkHandles *net, int ssl, const char *hostname)
 
int WebSocket_putdatas (networkHandles *net, char **buf0, size_t *buf0len, PacketBuffers *bufs)
 
static int WebSocket_receiveFrame (networkHandles *net, size_t *actual_len)
 
static void WebSocket_rewindData (void)
 
static const char * WebSocket_strcasefind (const char *buf, const char *str, size_t len)
 
void WebSocket_terminate (void)
 
static void WebSocket_unmaskData (size_t idx, PacketBuffers *bufs)
 
int WebSocket_upgrade (networkHandles *net)
 

Variables

static char * frame_buffer = NULL
 
static size_t frame_buffer_data_len = 0
 
static size_t frame_buffer_index = 0
 
static size_t frame_buffer_len = 0
 
static Listin_frames = NULL
 
struct ws_framelast_frame = NULL
 

Macro Definition Documentation

#define HTTP_PROTOCOL (   x)    x ? "https" : "http"

Definition at line 93 of file WebSocket.c.

Typedef Documentation

typedef unsigned char uuid_t[16]

raw uuid type

Definition at line 104 of file WebSocket.c.

Function Documentation

void uuid_generate ( uuid_t  out)

generates a uuid, compatible with RFC 4122, version 4 (random)

Note
Uses a very insecure algorithm but no external dependencies

Definition at line 110 of file WebSocket.c.

void uuid_unparse ( uuid_t  uu,
char *  out 
)

converts a uuid to a string

Definition at line 128 of file WebSocket.c.

static struct frameData WebSocket_buildFrame ( networkHandles net,
int  opcode,
int  mask_data,
char **  pbuf0,
size_t *  pbuf0len,
PacketBuffers bufs 
)
static

Definition at line 236 of file WebSocket.c.

size_t WebSocket_calculateFrameHeaderSize ( networkHandles net,
int  mask_data,
size_t  data_len 
)

calculates the amount of data required for the websocket header

this function is used to calculate how much offset is required before calling WebSocket_putdatas, as that function will write data before the passed in buffer

Parameters
[in,out]netnetwork connection
[in]mask_datawhether to mask the data
[in]data_lenamount of data in the payload
Returns
the size in bytes of the websocket header required
See also
WebSocket_putdatas

Definition at line 195 of file WebSocket.c.

void WebSocket_close ( networkHandles net,
int  status_code,
const char *  reason 
)

closes a websocket connection

Parameters
[in,out]netstructure containing network connection
[in]status_codewebsocket close status code
[in]reasonreason for closing connection (optional)

Definition at line 521 of file WebSocket.c.

int WebSocket_connect ( networkHandles net,
const char *  uri 
)

sends out a websocket request on the given uri

Parameters
[in]netnetwork connection
[in]uriuri to connect to
Return values
SOCKET_ERRORon failure
1on success
See also
WebSocket_upgrade

Definition at line 383 of file WebSocket.c.

size_t WebSocket_framePos ( )

Definition at line 634 of file WebSocket.c.

void WebSocket_framePosSeekTo ( size_t  pos)

Definition at line 647 of file WebSocket.c.

int WebSocket_getch ( networkHandles net,
char *  c 
)

receives 1 byte from a socket

Parameters
[in,out]netnetwork connection
[out]cbyte that was read
Return values
SOCKET_ERRORon error
TCPSOCKET_INTERRUPTEDno data available
TCPSOCKET_COMPLETEon success
See also
WebSocket_getdata

Definition at line 589 of file WebSocket.c.

char* WebSocket_getdata ( networkHandles net,
size_t  bytes,
size_t *  actual_len 
)

receives data from a socket. It should receive all data from the socket that is immediately available. Because it is encapsulated in websocket frames which cannot be

Parameters
[in,out]netnetwork connection
[in]bytesamount of data to get (0 if last packet)
[out]actual_lenamount of data read
Returns
a pointer to the read data
See also
WebSocket_getch

Definition at line 669 of file WebSocket.c.

char * WebSocket_getRawSocketData ( networkHandles net,
size_t  bytes,
size_t *  actual_len,
int *  rc 
)
static

reads raw socket data for underlying layers

Parameters
[in]netnetwork connection
[in]bytesnumber of bytes to read, 0 to complete packet
[in]actual_lenamount of data read
Returns
a buffer containing raw data

Definition at line 771 of file WebSocket.c.

void WebSocket_pong ( networkHandles net,
char *  app_data,
size_t  app_data_len 
)
static

sends a "websocket pong" message

Parameters
[in]netnetwork connection
[in]app_dataapplication data to put in payload
[in]app_data_lenapplication data length

Definition at line 889 of file WebSocket.c.

int WebSocket_proxy_connect ( networkHandles net,
int  ssl,
const char *  hostname 
)

Notify the IP address and port of the endpoint to proxy, and wait connection to endpoint.

Parameters
[in]netnetwork connection to proxy.
[in]sslenable ssl.
[in]hostnamehostname of endpoint.
Return values
SOCKET_ERRORfailed to network connection
0connection to endpoint

Definition at line 1445 of file WebSocket.c.

int WebSocket_putdatas ( networkHandles net,
char **  buf0,
size_t *  buf0len,
PacketBuffers bufs 
)

writes data to a socket (websocket header will be prepended if required)

Warning
buf0 will be expanded (backwords before buf0 buffer, to add a websocket frame header to the data if required). So use WebSocket_calculateFrameHeader, to determine if extra space is needed before the buf0 pointer.
Parameters
[in,out]netnetwork connection
[in,out]buf0first buffer
[in]buf0lensize of first buffer
[in]countnumber of payload buffers
[in,out]buffersarray of paylaod buffers
[in]buflensarray of payload buffer sizes
[in]freeDataarray indicating to free payload buffers
Returns
amount of data wrote to socket
See also
WebSocket_calculateFrameHeaderSize

Definition at line 938 of file WebSocket.c.

int WebSocket_receiveFrame ( networkHandles net,
size_t *  actual_len 
)
static

receives incoming socket data and parses websocket frames Copes with socket reads returning partial websocket frames by using the SocketBuffer mechanism.

Parameters
[in]netnetwork connection
[out]actual_lenamount of data actually read
Return values
TCPSOCKET_COMPLETEpacket received
TCPSOCKET_INTERRUPTEDincomplete packet received
SOCKET_ERRORan error was encountered

Definition at line 990 of file WebSocket.c.

void WebSocket_rewindData ( void  )
static

Definition at line 757 of file WebSocket.c.

const char * WebSocket_strcasefind ( const char *  buf,
const char *  str,
size_t  len 
)
static

case-insensitive string search

similar to strcase, but takes a maximum length

Parameters
[in]bufbuffer to search
[in]strstring to find
[in]lenlength of the buffer
Return values
!NULLlocation of string found
NULLstring not found

Definition at line 1237 of file WebSocket.c.

void WebSocket_terminate ( void  )

releases resources used by the websocket sub-system

Definition at line 1257 of file WebSocket.c.

static void WebSocket_unmaskData ( size_t  idx,
PacketBuffers bufs 
)
static

Definition at line 355 of file WebSocket.c.

int WebSocket_upgrade ( networkHandles net)

handles the websocket upgrade response

Parameters
[in,out]netnetwork connection to upgrade
Return values
SOCKET_ERRORfailed to upgrade network connection
TCPSOCKET_INTERRUPTEDupgrade not complete, but not failed. Try again
1socket upgraded to use websockets
See also
WebSocket_connect

Definition at line 1306 of file WebSocket.c.

Variable Documentation

char* frame_buffer = NULL
static

Definition at line 160 of file WebSocket.c.

size_t frame_buffer_data_len = 0
static

Definition at line 163 of file WebSocket.c.

size_t frame_buffer_index = 0
static

Definition at line 162 of file WebSocket.c.

size_t frame_buffer_len = 0
static

Definition at line 161 of file WebSocket.c.

List* in_frames = NULL
static

Holds any received websocket frames, to be process

Definition at line 158 of file WebSocket.c.

struct ws_frame* last_frame = NULL

Current frame being processed

Definition at line 155 of file WebSocket.c.



plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:49