factor out hybi decode part to make it testable

remove direct dependency on rfbClientPtr structure in hybi decode
function(s)
pull/3/head
Andreas Weigel 8 years ago committed by Christian Beier
parent aac95a9dcf
commit bcefa591cd
No known key found for this signature in database
GPG Key ID: 421BB3B45C6067F8

@ -34,10 +34,6 @@
/* errno */
#include <errno.h>
#ifndef _MSC_VER
#include <resolv.h> /* __b64_ntop */
#endif
#ifdef LIBVNCSERVER_HAVE_ENDIAN_H
#include <endian.h>
#elif LIBVNCSERVER_HAVE_SYS_ENDIAN_H
@ -55,36 +51,8 @@
#include "rfb/rfbconfig.h"
#include "rfbssl.h"
#include "rfbcrypto.h"
#include "ws_decode.h"
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define WS_NTOH64(n) OSSwapBigToHostInt64(n)
#define WS_NTOH32(n) OSSwapBigToHostInt32(n)
#define WS_NTOH16(n) OSSwapBigToHostInt16(n)
#define WS_HTON64(n) OSSwapHostToBigInt64(n)
#define WS_HTON16(n) OSSwapHostToBigInt16(n)
#else
#define WS_NTOH64(n) htobe64(n)
#define WS_NTOH32(n) htobe32(n)
#define WS_NTOH16(n) htobe16(n)
#define WS_HTON64(n) htobe64(n)
#define WS_HTON16(n) htobe16(n)
#endif
#define B64LEN(__x) (((__x + 2) / 3) * 12 / 3)
#define WSHLENMAX 14 /* 2 + sizeof(uint64_t) + sizeof(uint32_t) */
#define WS_HYBI_MASK_LEN 4
#define ARRAYSIZE(a) ((sizeof(a) / sizeof((a[0]))) / (size_t)(!(sizeof(a) % sizeof((a[0])))))
enum {
WEBSOCKETS_VERSION_HIXIE,
WEBSOCKETS_VERSION_HYBI
};
#if 0
#include <sys/syscall.h>
@ -93,104 +61,6 @@ static int gettid() {
}
#endif
typedef int (*wsEncodeFunc)(rfbClientPtr cl, const char *src, int len, char **dst);
typedef int (*wsDecodeFunc)(rfbClientPtr cl, char *dst, int len);
enum {
/* header not yet received completely */
WS_HYBI_STATE_HEADER_PENDING,
/* data available */
WS_HYBI_STATE_DATA_AVAILABLE,
WS_HYBI_STATE_DATA_NEEDED,
/* received a complete frame */
WS_HYBI_STATE_FRAME_COMPLETE,
/* received part of a 'close' frame */
WS_HYBI_STATE_CLOSE_REASON_PENDING,
/* */
WS_HYBI_STATE_ERR
};
typedef union ws_mask_s {
char c[4];
uint32_t u;
} ws_mask_t;
/* XXX: The union and the structs do not need to be named.
* We are working around a bug present in GCC < 4.6 which prevented
* it from recognizing anonymous structs and unions.
* See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=4784
*/
typedef struct
#if __GNUC__
__attribute__ ((__packed__))
#endif
ws_header_s {
unsigned char b0;
unsigned char b1;
union {
struct
#if __GNUC__
__attribute__ ((__packed__))
#endif
{
uint16_t l16;
ws_mask_t m16;
} s16;
struct
#if __GNUC__
__attribute__ ((__packed__))
#endif
{
uint64_t l64;
ws_mask_t m64;
} s64;
ws_mask_t m;
} u;
} ws_header_t;
typedef struct ws_header_data_s {
ws_header_t *data;
/** bytes read */
int nRead;
/** mask value */
ws_mask_t mask;
/** length of frame header including payload len, but without mask */
int headerLen;
/** length of the payload data */
int payloadLen;
/** opcode */
unsigned char opcode;
} ws_header_data_t;
typedef struct ws_ctx_s {
char codeBufDecode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
char codeBufEncode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
char *writePos;
unsigned char *readPos;
int readlen;
int hybiDecodeState;
char carryBuf[3]; /* For base64 carry-over */
int carrylen;
int version;
int base64;
ws_header_data_t header;
int nReadRaw;
int nToRead;
wsEncodeFunc encode;
wsDecodeFunc decode;
} ws_ctx_t;
enum
{
WS_OPCODE_CONTINUATION = 0x0,
WS_OPCODE_TEXT_FRAME,
WS_OPCODE_BINARY_FRAME,
WS_OPCODE_CLOSE = 0x8,
WS_OPCODE_PING,
WS_OPCODE_PONG
};
#define FLASH_POLICY_RESPONSE "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\n"
#define SZ_FLASH_POLICY_RESPONSE 93
@ -238,10 +108,11 @@ void webSocketsGenMd5(char * target, char *key1, char *key2, char *key3);
static int webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst);
static int webSocketsEncodeHixie(rfbClientPtr cl, const char *src, int len, char **dst);
static int webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len);
static int webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len);
static int webSocketsDecodeHixie(ws_ctx_t *wsctx, char *dst, int len);
static int ws_read(void *cl, char *buf, int len);
static int ws_peek(void *cl, char *buf, int len);
static void hybiDecodeCleanup(ws_ctx_t *wsctx);
static int
min (int a, int b) {
@ -503,6 +374,8 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
wsctx->encode = webSocketsEncodeHixie;
wsctx->decode = webSocketsDecodeHixie;
}
wsctx->ctxInfo.readFunc = ws_read;
wsctx->ctxInfo.peekFunc = ws_peek;
wsctx->base64 = base64;
hybiDecodeCleanup(wsctx);
cl->wsctx = (wsCtx *)wsctx;
@ -578,9 +451,10 @@ webSocketsEncodeHixie(rfbClientPtr cl, const char *src, int len, char **dst)
}
static int
ws_read(rfbClientPtr cl, char *buf, int len)
ws_read(void *ctxPtr, char *buf, int len)
{
int n;
rfbClientPtr cl = ctxPtr;
if (cl->sslctx) {
n = rfbssl_read(cl, buf, len);
} else {
@ -590,9 +464,10 @@ ws_read(rfbClientPtr cl, char *buf, int len)
}
static int
ws_peek(rfbClientPtr cl, char *buf, int len)
ws_peek(void *ctxPtr, char *buf, int len)
{
int n;
rfbClientPtr cl = ctxPtr;
if (cl->sslctx) {
n = rfbssl_peek(cl, buf, len);
} else {
@ -605,15 +480,15 @@ ws_peek(rfbClientPtr cl, char *buf, int len)
}
static int
webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len)
webSocketsDecodeHixie(ws_ctx_t *wsctx, char *dst, int len)
{
int retlen = 0, n, i, avail, modlen, needlen;
char *buf, *end = NULL;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
buf = wsctx->codeBufDecode;
n = ws_peek(cl, buf, len*2+2);
//n = ws_peek(cl, buf, len*2+2);
n = wsctx->ctxInfo.peekFunc(wsctx->ctxInfo.ctxPtr, buf, len*2+2);
if (n <= 0) {
/* save errno because rfbErr() will tamper it */
@ -627,7 +502,8 @@ webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len)
/* Base64 encoded WebSockets stream */
if (buf[0] == '\xff') {
i = ws_read(cl, buf, 1); /* Consume marker */
//i = ws_read(cl, buf, 1); /* Consume marker */
i = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, buf, 1);
buf++;
n--;
}
@ -636,7 +512,8 @@ webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len)
return -1;
}
if (buf[0] == '\x00') {
i = ws_read(cl, buf, 1); /* Consume marker */
//i = ws_read(cl, buf, 1); /* Consume marker */
i = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, buf, 1);
buf++;
n--;
}
@ -686,7 +563,8 @@ webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len)
retlen += n;
/* Consume the data from socket */
i = ws_read(cl, buf, needlen);
//i = ws_read(cl, buf, needlen);
i = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, buf, needlen);
wsctx->carrylen = n - len;
retlen -= wsctx->carrylen;
@ -699,443 +577,6 @@ webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len)
return retlen;
}
static int
hybiRemaining(ws_ctx_t *wsctx)
{
return wsctx->nToRead - wsctx->nReadRaw;
}
static void
hybiDecodeCleanup(ws_ctx_t *wsctx)
{
wsctx->header.payloadLen = 0;
wsctx->header.mask.u = 0;
wsctx->nReadRaw = 0;
wsctx->nToRead= 0;
wsctx->carrylen = 0;
wsctx->readPos = (unsigned char *)wsctx->codeBufDecode;
wsctx->readlen = 0;
wsctx->hybiDecodeState = WS_HYBI_STATE_HEADER_PENDING;
wsctx->writePos = NULL;
rfbLog("cleaned up wsctx\n");
}
/**
* Return payload data that has been decoded/unmasked from
* a websocket frame.
*
* @param[out] dst destination buffer
* @param[in] len bytes to copy to destination buffer
* @param[in,out] wsctx internal state of decoding procedure
* @param[out] number of bytes actually written to dst buffer
* @return next hybi decoding state
*/
static int
hybiReturnData(char *dst, int len, ws_ctx_t *wsctx, int *nWritten)
{
int nextState = WS_HYBI_STATE_ERR;
/* if we have something already decoded copy and return */
if (wsctx->readlen > 0) {
/* simply return what we have */
if (wsctx->readlen > len) {
rfbLog("copy to %d bytes to dst buffer; readPos=%p, readLen=%d\n", len, wsctx->readPos, wsctx->readlen);
memcpy(dst, wsctx->readPos, len);
*nWritten = len;
wsctx->readlen -= len;
wsctx->readPos += len;
nextState = WS_HYBI_STATE_DATA_AVAILABLE;
} else {
rfbLog("copy to %d bytes to dst buffer; readPos=%p, readLen=%d\n", wsctx->readlen, wsctx->readPos, wsctx->readlen);
memcpy(dst, wsctx->readPos, wsctx->readlen);
*nWritten = wsctx->readlen;
wsctx->readlen = 0;
wsctx->readPos = NULL;
if (hybiRemaining(wsctx) == 0) {
nextState = WS_HYBI_STATE_FRAME_COMPLETE;
} else {
nextState = WS_HYBI_STATE_DATA_NEEDED;
}
}
rfbLog("after copy: readPos=%p, readLen=%d\n", wsctx->readPos, wsctx->readlen);
} else if (wsctx->hybiDecodeState == WS_HYBI_STATE_CLOSE_REASON_PENDING) {
nextState = WS_HYBI_STATE_CLOSE_REASON_PENDING;
}
return nextState;
}
/**
* Read an RFC 6455 websocket frame (IETF hybi working group).
*
* Internal state is updated according to bytes received and the
* decoding of header information.
*
* @param[in] cl client ptr with ptr to raw socket and ws_ctx_t ptr
* @param[out] sockRet emulated recv return value
* @return next hybi decoding state; WS_HYBI_STATE_HEADER_PENDING indicates
* that the header was not received completely.
*/
static int
hybiReadHeader(rfbClientPtr cl, int *sockRet)
{
int ret;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
char *headerDst = wsctx->codeBufDecode + wsctx->nReadRaw;
int n = WSHLENMAX - wsctx->nReadRaw;
rfbLog("header_read to %p with len=%d\n", headerDst, n);
ret = ws_read(cl, headerDst, n);
rfbLog("read %d bytes from socket\n", ret);
if (ret <= 0) {
if (-1 == ret) {
/* save errno because rfbErr() will tamper it */
int olderrno = errno;
rfbErr("%s: peek; %m\n", __func__);
errno = olderrno;
*sockRet = -1;
} else {
*sockRet = 0;
}
return WS_HYBI_STATE_ERR;
}
wsctx->nReadRaw += ret;
if (wsctx->nReadRaw < 2) {
/* cannot decode header with less than two bytes */
errno = EAGAIN;
*sockRet = -1;
return WS_HYBI_STATE_HEADER_PENDING;
}
/* first two header bytes received; interpret header data and get rest */
wsctx->header.data = (ws_header_t *)wsctx->codeBufDecode;
wsctx->header.opcode = wsctx->header.data->b0 & 0x0f;
/* fin = (header->b0 & 0x80) >> 7; */ /* not used atm */
wsctx->header.payloadLen = wsctx->header.data->b1 & 0x7f;
rfbLog("first header bytes received; opcode=%d lenbyte=%d\n", wsctx->header.opcode, wsctx->header.payloadLen);
/*
* 4.3. Client-to-Server Masking
*
* The client MUST mask all frames sent to the server. A server MUST
* close the connection upon receiving a frame with the MASK bit set to 0.
**/
if (!(wsctx->header.data->b1 & 0x80)) {
rfbErr("%s: got frame without mask ret=%d\n", __func__, ret);
errno = EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
if (wsctx->header.payloadLen < 126 && wsctx->nReadRaw >= 6) {
wsctx->header.headerLen = 2 + WS_HYBI_MASK_LEN;
wsctx->header.mask = wsctx->header.data->u.m;
} else if (wsctx->header.payloadLen == 126 && 8 <= wsctx->nReadRaw) {
wsctx->header.headerLen = 4 + WS_HYBI_MASK_LEN;
wsctx->header.payloadLen = WS_NTOH16(wsctx->header.data->u.s16.l16);
wsctx->header.mask = wsctx->header.data->u.s16.m16;
} else if (wsctx->header.payloadLen == 127 && 14 <= wsctx->nReadRaw) {
wsctx->header.headerLen = 10 + WS_HYBI_MASK_LEN;
wsctx->header.payloadLen = WS_NTOH64(wsctx->header.data->u.s64.l64);
wsctx->header.mask = wsctx->header.data->u.s64.m64;
} else {
/* Incomplete frame header, try again */
rfbErr("%s: incomplete frame header; ret=%d\n", __func__, ret);
errno = EAGAIN;
*sockRet = -1;
return WS_HYBI_STATE_HEADER_PENDING;
}
/* absolute length of frame */
wsctx->nToRead = wsctx->header.headerLen + wsctx->header.payloadLen;
/* set payload pointer just after header */
wsctx->writePos = wsctx->codeBufDecode + wsctx->nReadRaw;
wsctx->readPos = (unsigned char *)(wsctx->codeBufDecode + wsctx->header.headerLen);
rfbLog("header complete: state=%d flen=%d writeTo=%p\n", wsctx->hybiDecodeState, wsctx->nToRead, wsctx->writePos);
return WS_HYBI_STATE_DATA_NEEDED;
}
static int
hybiWsFrameComplete(ws_ctx_t *wsctx)
{
return wsctx != NULL && hybiRemaining(wsctx) == 0;
}
static char *
hybiPayloadStart(ws_ctx_t *wsctx)
{
return wsctx->codeBufDecode + wsctx->header.headerLen;
}
/**
* Read the remaining payload bytes from associated raw socket.
*
* - try to read remaining bytes from socket
* - unmask all multiples of 4
* - if frame incomplete but some bytes are left, these are copied to
* the carry buffer
* - if opcode is TEXT: Base64-decode all unmasked received bytes
* - set state for reading decoded data
* - reset write position to begin of buffer (+ header)
* --> before we retrieve more data we let the caller clear all bytes
* from the reception buffer
* - execute return data routine
*
* Sets errno corresponding to what it gets from the underlying
* socket or EIO if some internal sanity check fails.
*
* @param[in] cl client ptr with raw socket reference
* @param[out] dst destination buffer
* @param[in] len size of destination buffer
* @param[out] sockRet emulated recv return value
* @return next hybi decode state
*/
static int
hybiReadAndDecode(rfbClientPtr cl, char *dst, int len, int *sockRet)
{
int n;
int i;
int toReturn;
int toDecode;
int bufsize;
int nextRead;
unsigned char *data;
uint32_t *data32;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
/* if data was carried over, copy to start of buffer */
memcpy(wsctx->writePos, wsctx->carryBuf, wsctx->carrylen);
wsctx->writePos += wsctx->carrylen;
/* -1 accounts for potential '\0' terminator for base64 decoding */
bufsize = wsctx->codeBufDecode + ARRAYSIZE(wsctx->codeBufDecode) - wsctx->writePos - 1;
if (hybiRemaining(wsctx) > bufsize) {
nextRead = bufsize;
} else {
nextRead = hybiRemaining(wsctx);
}
rfbLog("calling read with buf=%p and len=%d (decodebuf=%p headerLen=%d\n)", wsctx->writePos, nextRead, wsctx->codeBufDecode, wsctx->header.headerLen);
if (wsctx->nReadRaw < wsctx->nToRead) {
/* decode more data */
if (-1 == (n = ws_read(cl, wsctx->writePos, nextRead))) {
int olderrno = errno;
rfbErr("%s: read; %m", __func__);
errno = olderrno;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
} else if (n == 0) {
*sockRet = 0;
return WS_HYBI_STATE_ERR;
}
wsctx->nReadRaw += n;
rfbLog("read %d bytes from socket; nRead=%d\n", n, wsctx->nReadRaw);
} else {
n = 0;
}
wsctx->writePos += n;
if (wsctx->nReadRaw >= wsctx->nToRead) {
if (wsctx->nReadRaw > wsctx->nToRead) {
rfbErr("%s: internal error, read past websocket frame", __func__);
errno=EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
}
toDecode = wsctx->writePos - hybiPayloadStart(wsctx);
rfbLog("toDecode=%d from n=%d carrylen=%d headerLen=%d\n", toDecode, n, wsctx->carrylen, wsctx->header.headerLen);
if (toDecode < 0) {
rfbErr("%s: internal error; negative number of bytes to decode: %d", __func__, toDecode);
errno=EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
/* for a possible base64 decoding, we decode multiples of 4 bytes until
* the whole frame is received and carry over any remaining bytes in the carry buf*/
data = (unsigned char *)hybiPayloadStart(wsctx);
data32= (uint32_t *)data;
for (i = 0; i < (toDecode >> 2); i++) {
data32[i] ^= wsctx->header.mask.u;
}
rfbLog("mask decoding; i=%d toDecode=%d\n", i, toDecode);
if (wsctx->hybiDecodeState == WS_HYBI_STATE_FRAME_COMPLETE) {
/* process the remaining bytes (if any) */
for (i*=4; i < toDecode; i++) {
data[i] ^= wsctx->header.mask.c[i % 4];
}
/* all data is here, no carrying */
wsctx->carrylen = 0;
} else {
/* carry over remaining, non-multiple-of-four bytes */
wsctx->carrylen = toDecode - (i * 4);
if (wsctx->carrylen < 0 || wsctx->carrylen > ARRAYSIZE(wsctx->carryBuf)) {
rfbErr("%s: internal error, invalid carry over size: carrylen=%d, toDecode=%d, i=%d", __func__, wsctx->carrylen, toDecode, i);
*sockRet = -1;
errno = EIO;
return WS_HYBI_STATE_ERR;
}
rfbLog("carrying over %d bytes from %p to %p\n", wsctx->carrylen, wsctx->writePos + (i * 4), wsctx->carryBuf);
memcpy(wsctx->carryBuf, data + (i * 4), wsctx->carrylen);
}
toReturn = toDecode - wsctx->carrylen;
switch (wsctx->header.opcode) {
case WS_OPCODE_CLOSE:
/* this data is not returned as payload data */
if (hybiWsFrameComplete(wsctx)) {
rfbLog("got closure, reason %d\n", WS_NTOH16(((uint16_t *)data)[0]));
errno = ECONNRESET;
*sockRet = -1;
return WS_HYBI_STATE_FRAME_COMPLETE;
} else {
rfbErr("%s: close reason with long frame not supported", __func__);
errno = EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
break;
case WS_OPCODE_TEXT_FRAME:
data[toReturn] = '\0';
rfbLog("Initiate Base64 decoding in %p with max size %d and '\\0' at %p\n", data, bufsize, data + toReturn);
if (-1 == (wsctx->readlen = b64_pton((char *)data, data, bufsize))) {
rfbErr("Base64 decode error in %s; data=%p bufsize=%d", __func__, data, bufsize);
rfbErr("%s: Base64 decode error; %m\n", __func__);
}
wsctx->writePos = hybiPayloadStart(wsctx);
break;
case WS_OPCODE_BINARY_FRAME:
wsctx->readlen = toReturn;
wsctx->writePos = hybiPayloadStart(wsctx);
break;
default:
rfbErr("%s: unhandled opcode %d, b0: %02x, b1: %02x\n", __func__, (int)wsctx->header.opcode, wsctx->header.data->b0, wsctx->header.data->b1);
}
wsctx->readPos = data;
return hybiReturnData(dst, len, wsctx, sockRet);
}
/**
* Read function for websocket-socket emulation.
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-------+-+-------------+-------------------------------+
* |F|R|R|R| opcode|M| Payload len | Extended payload length |
* |I|S|S|S| (4) |A| (7) | (16/64) |
* |N|V|V|V| |S| | (if payload len==126/127) |
* | |1|2|3| |K| | |
* +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
* | Extended payload length continued, if payload len == 127 |
* + - - - - - - - - - - - - - - - +-------------------------------+
* | |Masking-key, if MASK set to 1 |
* +-------------------------------+-------------------------------+
* | Masking-key (continued) | Payload Data |
* +-------------------------------- - - - - - - - - - - - - - - - +
* : Payload Data continued ... :
* + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
* | Payload Data continued ... |
* +---------------------------------------------------------------+
*
* Using the decode buffer, this function:
* - reads the complete header from the underlying socket
* - reads any remaining data bytes
* - unmasks the payload data using the provided mask
* - decodes Base64 encoded text data
* - copies len bytes of decoded payload data into dst
*
* Emulates a read call on a socket.
*/
static int
webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len)
{
int result = -1;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
/* int fin; */ /* not used atm */
/* rfbLog(" <== %s[%d]: %d cl: %p, wsctx: %p-%p (%d)\n", __func__, gettid(), len, cl, wsctx, (char *)wsctx + sizeof(ws_ctx_t), sizeof(ws_ctx_t)); */
rfbLog("%s_enter: len=%d; "
"CTX: readlen=%d readPos=%p "
"writeTo=%p "
"state=%d toRead=%d remaining=%d "
" nReadRaw=%d carrylen=%d carryBuf=%p\n",
__func__, len,
wsctx->readlen, wsctx->readPos,
wsctx->writePos,
wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf);
switch (wsctx->hybiDecodeState){
case WS_HYBI_STATE_HEADER_PENDING:
wsctx->hybiDecodeState = hybiReadHeader(cl, &result);
if (wsctx->hybiDecodeState == WS_HYBI_STATE_ERR) {
goto spor;
}
if (wsctx->hybiDecodeState != WS_HYBI_STATE_HEADER_PENDING) {
/* when header is complete, try to read some more data */
wsctx->hybiDecodeState = hybiReadAndDecode(cl, dst, len, &result);
}
break;
case WS_HYBI_STATE_DATA_AVAILABLE:
wsctx->hybiDecodeState = hybiReturnData(dst, len, wsctx, &result);
break;
case WS_HYBI_STATE_DATA_NEEDED:
wsctx->hybiDecodeState = hybiReadAndDecode(cl, dst, len, &result);
break;
case WS_HYBI_STATE_CLOSE_REASON_PENDING:
wsctx->hybiDecodeState = hybiReadAndDecode(cl, dst, len, &result);
break;
default:
/* invalid state */
rfbErr("%s: called with invalid state %d\n", wsctx->hybiDecodeState);
result = -1;
errno = EIO;
wsctx->hybiDecodeState = WS_HYBI_STATE_ERR;
}
/* single point of return, if someone has questions :-) */
spor:
/* rfbLog("%s: ret: %d/%d\n", __func__, result, len); */
if (wsctx->hybiDecodeState == WS_HYBI_STATE_FRAME_COMPLETE) {
rfbLog("frame received successfully, cleaning up: read=%d hlen=%d plen=%d\n", wsctx->header.nRead, wsctx->header.headerLen, wsctx->header.payloadLen);
/* frame finished, cleanup state */
hybiDecodeCleanup(wsctx);
} else if (wsctx->hybiDecodeState == WS_HYBI_STATE_ERR) {
hybiDecodeCleanup(wsctx);
}
rfbLog("%s_exit: len=%d; "
"CTX: readlen=%d readPos=%p "
"writePos=%p "
"state=%d toRead=%d remaining=%d "
"nRead=%d carrylen=%d carryBuf=%p "
"result=%d\n",
__func__, len,
wsctx->readlen, wsctx->readPos,
wsctx->writePos,
wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf,
result);
return result;
}
static int
webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
{
@ -1210,7 +651,9 @@ webSocketsEncode(rfbClientPtr cl, const char *src, int len, char **dst)
int
webSocketsDecode(rfbClientPtr cl, char *dst, int len)
{
return ((ws_ctx_t *)cl->wsctx)->decode(cl, dst, len);
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
wsctx->ctxInfo.ctxPtr = cl;
return wsctx->decode(wsctx, dst, len);
}

@ -0,0 +1,448 @@
#include "ws_decode.h"
#include <syslog.h>
#include <string.h>
#include <errno.h>
#define WS_HYBI_MASK_LEN 4
static int
hybiRemaining(ws_ctx_t *wsctx)
{
return wsctx->nToRead - wsctx->nReadRaw;
}
void
hybiDecodeCleanup(ws_ctx_t *wsctx)
{
wsctx->header.payloadLen = 0;
wsctx->header.mask.u = 0;
wsctx->nReadRaw = 0;
wsctx->nToRead= 0;
wsctx->carrylen = 0;
wsctx->readPos = (unsigned char *)wsctx->codeBufDecode;
wsctx->readlen = 0;
wsctx->hybiDecodeState = WS_HYBI_STATE_HEADER_PENDING;
wsctx->writePos = NULL;
rfbLog("cleaned up wsctx\n");
}
/**
* Return payload data that has been decoded/unmasked from
* a websocket frame.
*
* @param[out] dst destination buffer
* @param[in] len bytes to copy to destination buffer
* @param[in,out] wsctx internal state of decoding procedure
* @param[out] number of bytes actually written to dst buffer
* @return next hybi decoding state
*/
static int
hybiReturnData(char *dst, int len, ws_ctx_t *wsctx, int *nWritten)
{
int nextState = WS_HYBI_STATE_ERR;
/* if we have something already decoded copy and return */
if (wsctx->readlen > 0) {
/* simply return what we have */
if (wsctx->readlen > len) {
rfbLog("copy to %d bytes to dst buffer; readPos=%p, readLen=%d\n", len, wsctx->readPos, wsctx->readlen);
memcpy(dst, wsctx->readPos, len);
*nWritten = len;
wsctx->readlen -= len;
wsctx->readPos += len;
nextState = WS_HYBI_STATE_DATA_AVAILABLE;
} else {
rfbLog("copy to %d bytes to dst buffer; readPos=%p, readLen=%d\n", wsctx->readlen, wsctx->readPos, wsctx->readlen);
memcpy(dst, wsctx->readPos, wsctx->readlen);
*nWritten = wsctx->readlen;
wsctx->readlen = 0;
wsctx->readPos = NULL;
if (hybiRemaining(wsctx) == 0) {
nextState = WS_HYBI_STATE_FRAME_COMPLETE;
} else {
nextState = WS_HYBI_STATE_DATA_NEEDED;
}
}
rfbLog("after copy: readPos=%p, readLen=%d\n", wsctx->readPos, wsctx->readlen);
} else if (wsctx->hybiDecodeState == WS_HYBI_STATE_CLOSE_REASON_PENDING) {
nextState = WS_HYBI_STATE_CLOSE_REASON_PENDING;
}
return nextState;
}
/**
* Read an RFC 6455 websocket frame (IETF hybi working group).
*
* Internal state is updated according to bytes received and the
* decoding of header information.
*
* @param[in] cl client ptr with ptr to raw socket and ws_ctx_t ptr
* @param[out] sockRet emulated recv return value
* @return next hybi decoding state; WS_HYBI_STATE_HEADER_PENDING indicates
* that the header was not received completely.
*/
static int
hybiReadHeader(ws_ctx_t *wsctx, int *sockRet)
{
int ret;
char *headerDst = wsctx->codeBufDecode + wsctx->nReadRaw;
int n = WSHLENMAX - wsctx->nReadRaw;
rfbLog("header_read to %p with len=%d\n", headerDst, n);
//ret = ws_read(cl, headerDst, n);
ret = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, headerDst, n);
rfbLog("read %d bytes from socket\n", ret);
if (ret <= 0) {
if (-1 == ret) {
/* save errno because rfbErr() will tamper it */
int olderrno = errno;
rfbErr("%s: peek; %m\n", __func__);
errno = olderrno;
*sockRet = -1;
} else {
*sockRet = 0;
}
return WS_HYBI_STATE_ERR;
}
wsctx->nReadRaw += ret;
if (wsctx->nReadRaw < 2) {
/* cannot decode header with less than two bytes */
errno = EAGAIN;
*sockRet = -1;
return WS_HYBI_STATE_HEADER_PENDING;
}
/* first two header bytes received; interpret header data and get rest */
wsctx->header.data = (ws_header_t *)wsctx->codeBufDecode;
wsctx->header.opcode = wsctx->header.data->b0 & 0x0f;
/* fin = (header->b0 & 0x80) >> 7; */ /* not used atm */
wsctx->header.payloadLen = wsctx->header.data->b1 & 0x7f;
rfbLog("first header bytes received; opcode=%d lenbyte=%d\n", wsctx->header.opcode, wsctx->header.payloadLen);
/*
* 4.3. Client-to-Server Masking
*
* The client MUST mask all frames sent to the server. A server MUST
* close the connection upon receiving a frame with the MASK bit set to 0.
**/
if (!(wsctx->header.data->b1 & 0x80)) {
rfbErr("%s: got frame without mask ret=%d\n", __func__, ret);
syslog(LOG_ERR, "%s: got frame without mask; ret=%d\n", __func__, ret);
errno = EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
if (wsctx->header.payloadLen < 126 && wsctx->nReadRaw >= 6) {
wsctx->header.headerLen = 2 + WS_HYBI_MASK_LEN;
wsctx->header.mask = wsctx->header.data->u.m;
} else if (wsctx->header.payloadLen == 126 && 8 <= wsctx->nReadRaw) {
wsctx->header.headerLen = 4 + WS_HYBI_MASK_LEN;
wsctx->header.payloadLen = WS_NTOH16(wsctx->header.data->u.s16.l16);
wsctx->header.mask = wsctx->header.data->u.s16.m16;
} else if (wsctx->header.payloadLen == 127 && 14 <= wsctx->nReadRaw) {
wsctx->header.headerLen = 10 + WS_HYBI_MASK_LEN;
wsctx->header.payloadLen = WS_NTOH64(wsctx->header.data->u.s64.l64);
wsctx->header.mask = wsctx->header.data->u.s64.m64;
} else {
/* Incomplete frame header, try again */
rfbErr("%s: incomplete frame header; ret=%d\n", __func__, ret);
errno = EAGAIN;
*sockRet = -1;
return WS_HYBI_STATE_HEADER_PENDING;
}
/* absolute length of frame */
wsctx->nToRead = wsctx->header.headerLen + wsctx->header.payloadLen;
/* update write position for next bytes */
wsctx->writePos = wsctx->codeBufDecode + wsctx->nReadRaw;
/* set payload pointer just after header */
wsctx->readPos = (unsigned char *)(wsctx->codeBufDecode + wsctx->header.headerLen);
rfbLog("header complete: state=%d flen=%d writeTo=%p\n", wsctx->hybiDecodeState, wsctx->nToRead, wsctx->writePos);
return WS_HYBI_STATE_DATA_NEEDED;
}
static int
hybiWsFrameComplete(ws_ctx_t *wsctx)
{
return wsctx != NULL && hybiRemaining(wsctx) == 0;
}
static char *
hybiPayloadStart(ws_ctx_t *wsctx)
{
return wsctx->codeBufDecode + wsctx->header.headerLen;
}
/**
* Read the remaining payload bytes from associated raw socket.
*
* - try to read remaining bytes from socket
* - unmask all multiples of 4
* - if frame incomplete but some bytes are left, these are copied to
* the carry buffer
* - if opcode is TEXT: Base64-decode all unmasked received bytes
* - set state for reading decoded data
* - reset write position to begin of buffer (+ header)
* --> before we retrieve more data we let the caller clear all bytes
* from the reception buffer
* - execute return data routine
*
* Sets errno corresponding to what it gets from the underlying
* socket or EIO if some internal sanity check fails.
*
* @param[in] cl client ptr with raw socket reference
* @param[out] dst destination buffer
* @param[in] len size of destination buffer
* @param[out] sockRet emulated recv return value
* @return next hybi decode state
*/
static int
hybiReadAndDecode(ws_ctx_t *wsctx, char *dst, int len, int *sockRet)
{
int n;
int i;
int toReturn;
int toDecode;
int bufsize;
int nextRead;
unsigned char *data;
uint32_t *data32;
/* if data was carried over, copy to start of buffer */
memcpy(wsctx->writePos, wsctx->carryBuf, wsctx->carrylen);
wsctx->writePos += wsctx->carrylen;
/* -1 accounts for potential '\0' terminator for base64 decoding */
bufsize = wsctx->codeBufDecode + ARRAYSIZE(wsctx->codeBufDecode) - wsctx->writePos - 1;
if (hybiRemaining(wsctx) > bufsize) {
nextRead = bufsize;
} else {
nextRead = hybiRemaining(wsctx);
}
rfbLog("calling read with buf=%p and len=%d (decodebuf=%p headerLen=%d)\n", wsctx->writePos, nextRead, wsctx->codeBufDecode, wsctx->header.headerLen);
if (wsctx->nReadRaw < wsctx->nToRead) {
/* decode more data */
//if (-1 == (n = ws_read(cl, wsctx->writePos, nextRead))) {
if (-1 == (n = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, wsctx->writePos, nextRead))) {
int olderrno = errno;
rfbErr("%s: read; %m", __func__);
errno = olderrno;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
} else if (n == 0) {
*sockRet = 0;
return WS_HYBI_STATE_ERR;
}
wsctx->nReadRaw += n;
rfbLog("read %d bytes from socket; nRead=%d\n", n, wsctx->nReadRaw);
} else {
n = 0;
}
wsctx->writePos += n;
if (wsctx->nReadRaw >= wsctx->nToRead) {
if (wsctx->nReadRaw > wsctx->nToRead) {
rfbErr("%s: internal error, read past websocket frame", __func__);
errno=EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
}
toDecode = wsctx->writePos - hybiPayloadStart(wsctx);
rfbLog("toDecode=%d from n=%d carrylen=%d headerLen=%d\n", toDecode, n, wsctx->carrylen, wsctx->header.headerLen);
if (toDecode < 0) {
rfbErr("%s: internal error; negative number of bytes to decode: %d", __func__, toDecode);
errno=EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
/* for a possible base64 decoding, we decode multiples of 4 bytes until
* the whole frame is received and carry over any remaining bytes in the carry buf*/
data = (unsigned char *)hybiPayloadStart(wsctx);
data32= (uint32_t *)data;
for (i = 0; i < (toDecode >> 2); i++) {
data32[i] ^= wsctx->header.mask.u;
}
rfbLog("mask decoding; i=%d toDecode=%d\n", i, toDecode);
if (wsctx->hybiDecodeState == WS_HYBI_STATE_FRAME_COMPLETE) {
/* process the remaining bytes (if any) */
for (i*=4; i < toDecode; i++) {
data[i] ^= wsctx->header.mask.c[i % 4];
}
/* all data is here, no carrying */
wsctx->carrylen = 0;
} else {
/* carry over remaining, non-multiple-of-four bytes */
wsctx->carrylen = toDecode - (i * 4);
if (wsctx->carrylen < 0 || wsctx->carrylen > ARRAYSIZE(wsctx->carryBuf)) {
syslog(LOG_ERR, "%s: internal error, invalid carry over size: carrylen=%d, toDecode=%d, i=%d", __func__, wsctx->carrylen, toDecode, i);
*sockRet = -1;
errno = EIO;
return WS_HYBI_STATE_ERR;
}
rfbLog("carrying over %d bytes from %p to %p\n", wsctx->carrylen, wsctx->writePos + (i * 4), wsctx->carryBuf);
memcpy(wsctx->carryBuf, data + (i * 4), wsctx->carrylen);
}
toReturn = toDecode - wsctx->carrylen;
switch (wsctx->header.opcode) {
case WS_OPCODE_CLOSE:
/* this data is not returned as payload data */
if (hybiWsFrameComplete(wsctx)) {
rfbLog("got closure, reason %d\n", WS_NTOH16(((uint16_t *)data)[0]));
rfbLog("got close cmd, reason %d\n", WS_NTOH16(((uint16_t *)data)[0]));
errno = ECONNRESET;
*sockRet = -1;
return WS_HYBI_STATE_FRAME_COMPLETE;
} else {
rfbErr("%s: close reason with long frame not supported", __func__);
errno = EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
break;
case WS_OPCODE_TEXT_FRAME:
data[toReturn] = '\0';
rfbLog("Initiate Base64 decoding in %p with max size %d and '\\0' at %p\n", data, bufsize, data + toReturn);
if (-1 == (wsctx->readlen = b64_pton((char *)data, data, bufsize))) {
syslog(LOG_ERR, "Base64 decode error in %s; data=%p bufsize=%d", __func__, data, bufsize);
rfbErr("%s: Base64 decode error; %m\n", __func__);
}
wsctx->writePos = hybiPayloadStart(wsctx);
break;
case WS_OPCODE_BINARY_FRAME:
wsctx->readlen = toReturn;
wsctx->writePos = hybiPayloadStart(wsctx);
rfbLog("set readlen=%d writePos=%p\n", wsctx->readlen, wsctx->writePos);
break;
default:
rfbErr("%s: unhandled opcode %d, b0: %02x, b1: %02x\n", __func__, (int)wsctx->header.opcode, wsctx->header.data->b0, wsctx->header.data->b1);
}
wsctx->readPos = data;
return hybiReturnData(dst, len, wsctx, sockRet);
}
/**
* Read function for websocket-socket emulation.
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-------+-+-------------+-------------------------------+
* |F|R|R|R| opcode|M| Payload len | Extended payload length |
* |I|S|S|S| (4) |A| (7) | (16/64) |
* |N|V|V|V| |S| | (if payload len==126/127) |
* | |1|2|3| |K| | |
* +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
* | Extended payload length continued, if payload len == 127 |
* + - - - - - - - - - - - - - - - +-------------------------------+
* | |Masking-key, if MASK set to 1 |
* +-------------------------------+-------------------------------+
* | Masking-key (continued) | Payload Data |
* +-------------------------------- - - - - - - - - - - - - - - - +
* : Payload Data continued ... :
* + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
* | Payload Data continued ... |
* +---------------------------------------------------------------+
*
* Using the decode buffer, this function:
* - reads the complete header from the underlying socket
* - reads any remaining data bytes
* - unmasks the payload data using the provided mask
* - decodes Base64 encoded text data
* - copies len bytes of decoded payload data into dst
*
* Emulates a read call on a socket.
*/
int
webSocketsDecodeHybi(ws_ctx_t *wsctx, char *dst, int len)
{
int result = -1;
/* int fin; */ /* not used atm */
/* rfbLog(" <== %s[%d]: %d cl: %p, wsctx: %p-%p (%d)\n", __func__, gettid(), len, cl, wsctx, (char *)wsctx + sizeof(ws_ctx_t), sizeof(ws_ctx_t)); */
rfbLog("%s_enter: len=%d; "
"CTX: readlen=%d readPos=%p "
"writeTo=%p "
"state=%d toRead=%d remaining=%d "
" nReadRaw=%d carrylen=%d carryBuf=%p\n",
__func__, len,
wsctx->readlen, wsctx->readPos,
wsctx->writePos,
wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf);
switch (wsctx->hybiDecodeState){
case WS_HYBI_STATE_HEADER_PENDING:
wsctx->hybiDecodeState = hybiReadHeader(wsctx, &result);
if (wsctx->hybiDecodeState == WS_HYBI_STATE_ERR) {
goto spor;
}
if (wsctx->hybiDecodeState != WS_HYBI_STATE_HEADER_PENDING) {
/* when header is complete, try to read some more data */
wsctx->hybiDecodeState = hybiReadAndDecode(wsctx, dst, len, &result);
}
break;
case WS_HYBI_STATE_DATA_AVAILABLE:
wsctx->hybiDecodeState = hybiReturnData(dst, len, wsctx, &result);
break;
case WS_HYBI_STATE_DATA_NEEDED:
wsctx->hybiDecodeState = hybiReadAndDecode(wsctx, dst, len, &result);
break;
case WS_HYBI_STATE_CLOSE_REASON_PENDING:
wsctx->hybiDecodeState = hybiReadAndDecode(wsctx, dst, len, &result);
break;
default:
/* invalid state */
rfbErr("%s: called with invalid state %d\n", wsctx->hybiDecodeState);
result = -1;
errno = EIO;
wsctx->hybiDecodeState = WS_HYBI_STATE_ERR;
}
/* single point of return, if someone has questions :-) */
spor:
/* rfbLog("%s: ret: %d/%d\n", __func__, result, len); */
if (wsctx->hybiDecodeState == WS_HYBI_STATE_FRAME_COMPLETE) {
rfbLog("frame received successfully, cleaning up: read=%d hlen=%d plen=%d\n", wsctx->header.nRead, wsctx->header.headerLen, wsctx->header.payloadLen);
/* frame finished, cleanup state */
hybiDecodeCleanup(wsctx);
} else if (wsctx->hybiDecodeState == WS_HYBI_STATE_ERR) {
hybiDecodeCleanup(wsctx);
}
rfbLog("%s_exit: len=%d; "
"CTX: readlen=%d readPos=%p "
"writePos=%p "
"state=%d toRead=%d remaining=%d "
"nRead=%d carrylen=%d carryBuf=%p "
"result=%d\n",
__func__, len,
wsctx->readlen, wsctx->readPos,
wsctx->writePos,
wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf,
result);
return result;
}

@ -0,0 +1,160 @@
#ifndef _WS_DECODE_H_
#define _WS_DECODE_H_
#include <stdint.h>
#include <rfb/rfb.h>
#ifndef _MSC_VER
#include <resolv.h> /* __b64_ntop */
#endif
enum {
WEBSOCKETS_VERSION_HIXIE,
WEBSOCKETS_VERSION_HYBI
};
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define WS_NTOH64(n) OSSwapBigToHostInt64(n)
#define WS_NTOH32(n) OSSwapBigToHostInt32(n)
#define WS_NTOH16(n) OSSwapBigToHostInt16(n)
#define WS_HTON64(n) OSSwapHostToBigInt64(n)
#define WS_HTON16(n) OSSwapHostToBigInt16(n)
#else
#define WS_NTOH64(n) htobe64(n)
#define WS_NTOH32(n) htobe32(n)
#define WS_NTOH16(n) htobe16(n)
#define WS_HTON64(n) htobe64(n)
#define WS_HTON16(n) htobe16(n)
#endif
#define B64LEN(__x) (((__x + 2) / 3) * 12 / 3)
#define WSHLENMAX 14 /* 2 + sizeof(uint64_t) + sizeof(uint32_t) */
#define WS_HYBI_MASK_LEN 4
#define ARRAYSIZE(a) ((sizeof(a) / sizeof((a[0]))) / (size_t)(!(sizeof(a) % sizeof((a[0])))))
enum {
WEBSOCKETS_VERSION_HIXIE,
WEBSOCKETS_VERSION_HYBI
};
struct ws_ctx_s;
typedef struct ws_ctx_s ws_ctx_t;
typedef int (*wsEncodeFunc)(rfbClientPtr cl, const char *src, int len, char **dst);
typedef int (*wsDecodeFunc)(ws_ctx_t *wsctx, char *dst, int len);
typedef int (*wsReadFunc)(void *ctx, char *dst, int len);
typedef int (*wsPeekFunc)(void *ctx, char *dst, int len);
typedef struct ctxInfo_s{
void *ctxPtr;
wsReadFunc readFunc;
wsPeekFunc peekFunc;
} ctxInfo_t;
enum {
/* header not yet received completely */
WS_HYBI_STATE_HEADER_PENDING,
/* data available */
WS_HYBI_STATE_DATA_AVAILABLE,
WS_HYBI_STATE_DATA_NEEDED,
/* received a complete frame */
WS_HYBI_STATE_FRAME_COMPLETE,
/* received part of a 'close' frame */
WS_HYBI_STATE_CLOSE_REASON_PENDING,
/* */
WS_HYBI_STATE_ERR
};
typedef union ws_mask_s {
char c[4];
uint32_t u;
} ws_mask_t;
/* XXX: The union and the structs do not need to be named.
* We are working around a bug present in GCC < 4.6 which prevented
* it from recognizing anonymous structs and unions.
* See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=4784
*/
typedef struct
#if __GNUC__
__attribute__ ((__packed__))
#endif
ws_header_s {
unsigned char b0;
unsigned char b1;
union {
struct
#if __GNUC__
__attribute__ ((__packed__))
#endif
{
uint16_t l16;
ws_mask_t m16;
} s16;
struct
#if __GNUC__
__attribute__ ((__packed__))
#endif
{
uint64_t l64;
ws_mask_t m64;
} s64;
ws_mask_t m;
} u;
} ws_header_t;
typedef struct ws_header_data_s {
ws_header_t *data;
/** bytes read */
int nRead;
/** mask value */
ws_mask_t mask;
/** length of frame header including payload len, but without mask */
int headerLen;
/** length of the payload data */
int payloadLen;
/** opcode */
unsigned char opcode;
} ws_header_data_t;
typedef struct ws_ctx_s {
char codeBufDecode[2048 + WSHLENMAX]; /* base64 + maximum frame header length */
char codeBufEncode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
char *writePos;
unsigned char *readPos;
int readlen;
int hybiDecodeState;
char carryBuf[3]; /* For base64 carry-over */
int carrylen;
int version;
int base64;
ws_header_data_t header;
int nReadRaw;
int nToRead;
wsEncodeFunc encode;
wsDecodeFunc decode;
ctxInfo_t ctxInfo;
} ws_ctx_t;
enum
{
WS_OPCODE_CONTINUATION = 0x0,
WS_OPCODE_TEXT_FRAME,
WS_OPCODE_BINARY_FRAME,
WS_OPCODE_CLOSE = 0x8,
WS_OPCODE_PING,
WS_OPCODE_PONG
};
int webSocketsDecodeHybi(ws_ctx_t *wsctx, char *dst, int len);
void hybiDecodeCleanup(ws_ctx_t *wsctx);
#endif
Loading…
Cancel
Save