#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"
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 List * | in_frames = NULL |
| struct ws_frame * | last_frame = NULL |
| #define HTTP_PROTOCOL | ( | x | ) | x ? "https" : "http" |
Definition at line 93 of file WebSocket.c.
| typedef unsigned char uuid_t[16] |
raw uuid type
Definition at line 104 of file WebSocket.c.
| void uuid_generate | ( | uuid_t | out | ) |
generates a uuid, compatible with RFC 4122, version 4 (random)
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 |
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
| [in,out] | net | network connection |
| [in] | mask_data | whether to mask the data |
| [in] | data_len | amount of data in the payload |
Definition at line 195 of file WebSocket.c.
| void WebSocket_close | ( | networkHandles * | net, |
| int | status_code, | ||
| const char * | reason | ||
| ) |
closes a websocket connection
| [in,out] | net | structure containing network connection |
| [in] | status_code | websocket close status code |
| [in] | reason | reason 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
| [in] | net | network connection |
| [in] | uri | uri to connect to |
| SOCKET_ERROR | on failure |
| 1 | on success |
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
| [in,out] | net | network connection |
| [out] | c | byte that was read |
| SOCKET_ERROR | on error |
| TCPSOCKET_INTERRUPTED | no data available |
| TCPSOCKET_COMPLETE | on success |
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
| [in,out] | net | network connection |
| [in] | bytes | amount of data to get (0 if last packet) |
| [out] | actual_len | amount of data read |
Definition at line 669 of file WebSocket.c.
|
static |
reads raw socket data for underlying layers
| [in] | net | network connection |
| [in] | bytes | number of bytes to read, 0 to complete packet |
| [in] | actual_len | amount of data read |
Definition at line 771 of file WebSocket.c.
|
static |
sends a "websocket pong" message
| [in] | net | network connection |
| [in] | app_data | application data to put in payload |
| [in] | app_data_len | application 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.
| [in] | net | network connection to proxy. |
| [in] | ssl | enable ssl. |
| [in] | hostname | hostname of endpoint. |
| SOCKET_ERROR | failed to network connection |
| 0 | connection 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)
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.| [in,out] | net | network connection |
| [in,out] | buf0 | first buffer |
| [in] | buf0len | size of first buffer |
| [in] | count | number of payload buffers |
| [in,out] | buffers | array of paylaod buffers |
| [in] | buflens | array of payload buffer sizes |
| [in] | freeData | array indicating to free payload buffers |
Definition at line 938 of file WebSocket.c.
|
static |
receives incoming socket data and parses websocket frames Copes with socket reads returning partial websocket frames by using the SocketBuffer mechanism.
| [in] | net | network connection |
| [out] | actual_len | amount of data actually read |
| TCPSOCKET_COMPLETE | packet received |
| TCPSOCKET_INTERRUPTED | incomplete packet received |
| SOCKET_ERROR | an error was encountered |
Definition at line 990 of file WebSocket.c.
|
static |
Definition at line 757 of file WebSocket.c.
|
static |
case-insensitive string search
similar to strcase, but takes a maximum length
| [in] | buf | buffer to search |
| [in] | str | string to find |
| [in] | len | length of the buffer |
| !NULL | location of string found |
| NULL | string 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 |
Definition at line 355 of file WebSocket.c.
| int WebSocket_upgrade | ( | networkHandles * | net | ) |
handles the websocket upgrade response
| [in,out] | net | network connection to upgrade |
| SOCKET_ERROR | failed to upgrade network connection |
| TCPSOCKET_INTERRUPTED | upgrade not complete, but not failed. Try again |
| 1 | socket upgraded to use websockets |
Definition at line 1306 of file WebSocket.c.
|
static |
Definition at line 160 of file WebSocket.c.
|
static |
Definition at line 163 of file WebSocket.c.
|
static |
Definition at line 162 of file WebSocket.c.
|
static |
Definition at line 161 of file WebSocket.c.
|
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.