Karoly Negyesi 13 years ago
commit efc23393a9

@ -17,7 +17,7 @@ endif
DEPS=$(FORMAT_OBJS) $(HIREDIS_OBJ) $(JANSSON_OBJ) $(HTTP_PARSER_OBJS)
OBJS=webdis.o cmd.o worker.o slog.o server.o libb64/cencode.o acl.o md5/md5.o http.o client.o websocket.o pool.o conf.o $(DEPS)
OBJS=webdis.o cmd.o worker.o slog.o server.o libb64/cencode.o acl.o md5/md5.o sha1/sha1.o http.o client.o websocket.o pool.o conf.o $(DEPS)

@ -304,6 +304,23 @@ http_client_read(struct http_client *c) {
return ret;
}
int
http_client_remove_data(struct http_client *c, size_t sz) {
char *buffer;
if(c->sz < sz)
return -1;
/* replace buffer */
buffer = malloc(c->sz - sz);
memcpy(buffer, c->buffer + sz, c->sz - sz);
free(c->buffer);
c->buffer = buffer;
c->sz -= sz;
return 0;
}
int
http_client_execute(struct http_client *c) {

@ -4,6 +4,7 @@
#include <event.h>
#include <arpa/inet.h>
#include "http_parser.h"
#include "websocket.h"
struct http_header;
struct server;
@ -53,6 +54,8 @@ struct http_client {
char *filename; /* content-disposition */
struct cmd *pub_sub;
struct ws_msg *frame; /* websocket frame */
};
struct http_client *
@ -67,6 +70,9 @@ http_client_free(struct http_client *c);
int
http_client_read(struct http_client *c);
int
http_client_remove_data(struct http_client *c, size_t sz);
int
http_client_execute(struct http_client *c);

@ -41,6 +41,7 @@ raw_ws_extract(struct http_client *c, const char *p, size_t sz) {
struct cmd *cmd = NULL;
void *reader = NULL;
redisReply *reply = NULL;
void **reply_ptr = (void**)&reply;
unsigned int i;
(void)c;
@ -51,7 +52,7 @@ raw_ws_extract(struct http_client *c, const char *p, size_t sz) {
redisReaderFeed(reader, (char*)p, sz);
/* parse data into reply object */
if(redisReaderGetReply(reader, (void**)&reply) == REDIS_ERR) {
if(redisReaderGetReply(reader, reply_ptr) == REDIS_ERR) {
goto end;
}

@ -8,6 +8,7 @@
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/types.h>

@ -0,0 +1,371 @@
/*
* sha1.c
*
* Copyright (C) 1998, 2009
* Paul E. Jones <paulej@packetizer.com>
* All Rights Reserved
*
*****************************************************************************
* $Id: sha1.c 12 2009-06-22 19:34:25Z paulej $
*****************************************************************************
*
* Description:
* This file implements the Secure Hashing Standard as defined
* in FIPS PUB 180-1 published April 17, 1995.
*
* The Secure Hashing Standard, which uses the Secure Hashing
* Algorithm (SHA), produces a 160-bit message digest for a
* given data stream. In theory, it is highly improbable that
* two messages will produce the same message digest. Therefore,
* this algorithm can serve as a means of providing a "fingerprint"
* for a message.
*
* Portability Issues:
* SHA-1 is defined in terms of 32-bit "words". This code was
* written with the expectation that the processor has at least
* a 32-bit machine word size. If the machine word size is larger,
* the code should still function properly. One caveat to that
* is that the input functions taking characters and character
* arrays assume that only 8 bits of information are stored in each
* character.
*
* Caveats:
* SHA-1 is designed to work with messages less than 2^64 bits
* long. Although SHA-1 allows a message digest to be generated for
* messages of any number of bits less than 2^64, this
* implementation only works with messages with a length that is a
* multiple of the size of an 8-bit character.
*
*/
#include "sha1.h"
/*
* Define the circular shift macro
*/
#define SHA1CircularShift(bits,word) \
((((word) << (bits)) & 0xFFFFFFFF) | \
((word) >> (32-(bits))))
/* Function prototypes */
void SHA1ProcessMessageBlock(SHA1Context *);
void SHA1PadMessage(SHA1Context *);
/*
* SHA1Reset
*
* Description:
* This function will initialize the SHA1Context in preparation
* for computing a new message digest.
*
* Parameters:
* context: [in/out]
* The context to reset.
*
* Returns:
* Nothing.
*
* Comments:
*
*/
void SHA1Reset(SHA1Context *context)
{
context->Length_Low = 0;
context->Length_High = 0;
context->Message_Block_Index = 0;
context->Message_Digest[0] = 0x67452301;
context->Message_Digest[1] = 0xEFCDAB89;
context->Message_Digest[2] = 0x98BADCFE;
context->Message_Digest[3] = 0x10325476;
context->Message_Digest[4] = 0xC3D2E1F0;
context->Computed = 0;
context->Corrupted = 0;
}
/*
* SHA1Result
*
* Description:
* This function will return the 160-bit message digest into the
* Message_Digest array within the SHA1Context provided
*
* Parameters:
* context: [in/out]
* The context to use to calculate the SHA-1 hash.
*
* Returns:
* 1 if successful, 0 if it failed.
*
* Comments:
*
*/
int SHA1Result(SHA1Context *context)
{
if (context->Corrupted)
{
return 0;
}
if (!context->Computed)
{
SHA1PadMessage(context);
context->Computed = 1;
}
return 1;
}
/*
* SHA1Input
*
* Description:
* This function accepts an array of octets as the next portion of
* the message.
*
* Parameters:
* context: [in/out]
* The SHA-1 context to update
* message_array: [in]
* An array of characters representing the next portion of the
* message.
* length: [in]
* The length of the message in message_array
*
* Returns:
* Nothing.
*
* Comments:
*
*/
void SHA1Input( SHA1Context *context,
const unsigned char *message_array,
unsigned length)
{
if (!length)
{
return;
}
if (context->Computed || context->Corrupted)
{
context->Corrupted = 1;
return;
}
while(length-- && !context->Corrupted)
{
context->Message_Block[context->Message_Block_Index++] =
(*message_array & 0xFF);
context->Length_Low += 8;
/* Force it to 32 bits */
context->Length_Low &= 0xFFFFFFFF;
if (context->Length_Low == 0)
{
context->Length_High++;
/* Force it to 32 bits */
context->Length_High &= 0xFFFFFFFF;
if (context->Length_High == 0)
{
/* Message is too long */
context->Corrupted = 1;
}
}
if (context->Message_Block_Index == 64)
{
SHA1ProcessMessageBlock(context);
}
message_array++;
}
}
/*
* SHA1ProcessMessageBlock
*
* Description:
* This function will process the next 512 bits of the message
* stored in the Message_Block array.
*
* Parameters:
* None.
*
* Returns:
* Nothing.
*
* Comments:
* Many of the variable names in the SHAContext, especially the
* single character names, were used because those were the names
* used in the publication.
*
*
*/
void SHA1ProcessMessageBlock(SHA1Context *context)
{
const unsigned K[] = /* Constants defined in SHA-1 */
{
0x5A827999,
0x6ED9EBA1,
0x8F1BBCDC,
0xCA62C1D6
};
int t; /* Loop counter */
unsigned temp; /* Temporary word value */
unsigned W[80]; /* Word sequence */
unsigned A, B, C, D, E; /* Word buffers */
/*
* Initialize the first 16 words in the array W
*/
for(t = 0; t < 16; t++)
{
W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
}
for(t = 16; t < 80; t++)
{
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
}
A = context->Message_Digest[0];
B = context->Message_Digest[1];
C = context->Message_Digest[2];
D = context->Message_Digest[3];
E = context->Message_Digest[4];
for(t = 0; t < 20; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | ((~B) & D)) + E + W[t] + K[0];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 20; t < 40; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 40; t < 60; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 60; t < 80; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
context->Message_Digest[0] =
(context->Message_Digest[0] + A) & 0xFFFFFFFF;
context->Message_Digest[1] =
(context->Message_Digest[1] + B) & 0xFFFFFFFF;
context->Message_Digest[2] =
(context->Message_Digest[2] + C) & 0xFFFFFFFF;
context->Message_Digest[3] =
(context->Message_Digest[3] + D) & 0xFFFFFFFF;
context->Message_Digest[4] =
(context->Message_Digest[4] + E) & 0xFFFFFFFF;
context->Message_Block_Index = 0;
}
/*
* SHA1PadMessage
*
* Description:
* According to the standard, the message must be padded to an even
* 512 bits. The first padding bit must be a '1'. The last 64
* bits represent the length of the original message. All bits in
* between should be 0. This function will pad the message
* according to those rules by filling the Message_Block array
* accordingly. It will also call SHA1ProcessMessageBlock()
* appropriately. When it returns, it can be assumed that the
* message digest has been computed.
*
* Parameters:
* context: [in/out]
* The context to pad
*
* Returns:
* Nothing.
*
* Comments:
*
*/
void SHA1PadMessage(SHA1Context *context)
{
/*
* Check to see if the current message block is too small to hold
* the initial padding bits and length. If so, we will pad the
* block, process it, and then continue padding into a second
* block.
*/
if (context->Message_Block_Index > 55)
{
context->Message_Block[context->Message_Block_Index++] = 0x80;
while(context->Message_Block_Index < 64)
{
context->Message_Block[context->Message_Block_Index++] = 0;
}
SHA1ProcessMessageBlock(context);
while(context->Message_Block_Index < 56)
{
context->Message_Block[context->Message_Block_Index++] = 0;
}
}
else
{
context->Message_Block[context->Message_Block_Index++] = 0x80;
while(context->Message_Block_Index < 56)
{
context->Message_Block[context->Message_Block_Index++] = 0;
}
}
/*
* Store the message length as the last 8 octets
*/
context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
context->Message_Block[59] = (context->Length_High) & 0xFF;
context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
context->Message_Block[63] = (context->Length_Low) & 0xFF;
SHA1ProcessMessageBlock(context);
}

@ -0,0 +1,54 @@
/*
* sha1.h
*
* Copyright (C) 1998, 2009
* Paul E. Jones <paulej@packetizer.com>
* All Rights Reserved
*
*****************************************************************************
* $Id: sha1.h 12 2009-06-22 19:34:25Z paulej $
*****************************************************************************
*
* Description:
* This class implements the Secure Hashing Standard as defined
* in FIPS PUB 180-1 published April 17, 1995.
*
* Many of the variable names in the SHA1Context, especially the
* single character names, were used because those were the names
* used in the publication.
*
* Please read the file sha1.c for more information.
*
*/
#ifndef _SHA1_H_
#define _SHA1_H_
/*
* This structure will hold context information for the hashing
* operation
*/
typedef struct SHA1Context
{
unsigned Message_Digest[5]; /* Message Digest (output) */
unsigned Length_Low; /* Message length in bits */
unsigned Length_High; /* Message length in bits */
unsigned char Message_Block[64]; /* 512-bit message blocks */
int Message_Block_Index; /* Index into message block array */
int Computed; /* Is the digest computed? */
int Corrupted; /* Is the message digest corruped? */
} SHA1Context;
/*
* Function Prototypes
*/
void SHA1Reset(SHA1Context *);
int SHA1Result(SHA1Context *);
void SHA1Input( SHA1Context *,
const unsigned char *,
unsigned);
#endif

@ -91,7 +91,13 @@ function log(id, dir, msg) {
}
function testJSON() {
var jsonSocket = new WebSocket("ws://"+host+":"+port+"/.json");
if(typeof(WebSocket) == 'function')
f = WebSocket;
if(typeof(MozWebSocket) == 'function')
f = MozWebSocket;
var jsonSocket = new f("ws://"+host+":"+port+"/.json");
var self = this;
send = function(j) {
@ -112,7 +118,13 @@ function testJSON() {
}
function testRAW() {
var rawSocket = new WebSocket("ws://"+host+":"+port+"/.raw");
if(typeof(WebSocket) == 'function')
f = WebSocket;
if(typeof(MozWebSocket) == 'function')
f = MozWebSocket;
var rawSocket = new f("ws://"+host+":"+port+"/.raw");
var self = this;
sendRaw = function(raw) {

@ -1,4 +1,5 @@
#include "md5/md5.h"
#include "sha1/sha1.h"
#include "libb64/cencode.h"
#include "websocket.h"
#include "client.h"
#include "cmd.h"
@ -14,58 +15,63 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/param.h>
/**
* This code uses the WebSocket specification from May 23, 2010.
* The latest copy is available at http://www.whatwg.org/specs/web-socket-protocol/
* This code uses the WebSocket specification from RFC 6455.
* A copy is available at http://www.rfc-editor.org/rfc/rfc6455.txt
*/
static uint32_t
ws_read_key(const char *s) {
uint32_t ret = 0, spaces = 0;
const char *p;
size_t sz;
if(!s) {
return 0;
}
sz = strlen(s);
for(p = s; p < s+sz; ++p) {
if(*p >= '0' && *p <= '9') {
ret *= 10;
ret += (*p) - '0';
} else if (*p == ' ') {
spaces++;
}
}
return htonl(ret / spaces);
}
/* custom 64-bit encoding functions to avoid portability issues */
#define webdis_ntohl64(p) \
((((uint64_t)((p)[0])) << 0) + (((uint64_t)((p)[1])) << 8) +\
(((uint64_t)((p)[2])) << 16) + (((uint64_t)((p)[3])) << 24) +\
(((uint64_t)((p)[4])) << 32) + (((uint64_t)((p)[5])) << 40) +\
(((uint64_t)((p)[6])) << 48) + (((uint64_t)((p)[7])) << 56))
#define webdis_htonl64(p) {\
(char)(((p & ((uint64_t)0xff << 0)) >> 0) & 0xff), (char)(((p & ((uint64_t)0xff << 8)) >> 8) & 0xff), \
(char)(((p & ((uint64_t)0xff << 16)) >> 16) & 0xff), (char)(((p & ((uint64_t)0xff << 24)) >> 24) & 0xff), \
(char)(((p & ((uint64_t)0xff << 32)) >> 32) & 0xff), (char)(((p & ((uint64_t)0xff << 40)) >> 40) & 0xff), \
(char)(((p & ((uint64_t)0xff << 48)) >> 48) & 0xff), (char)(((p & ((uint64_t)0xff << 56)) >> 56) & 0xff) }
static int
ws_compute_handshake(struct http_client *c, unsigned char *out) {
ws_compute_handshake(struct http_client *c, char *out, size_t *out_sz) {
char buffer[16];
md5_state_t ctx;
unsigned char *buffer, sha1_output[20];
char magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
SHA1Context ctx;
base64_encodestate b64_ctx;
int pos, i;
// websocket handshake
uint32_t number_1 = ws_read_key(client_get_header(c, "Sec-WebSocket-Key1"));
uint32_t number_2 = ws_read_key(client_get_header(c, "Sec-WebSocket-Key2"));
if(c->body_sz < 8) { /* we need at least 8 bytes */
return -1;
const char *key = client_get_header(c, "Sec-WebSocket-Key");
size_t key_sz = key?strlen(key):0, buffer_sz = key_sz + sizeof(magic) - 1;
buffer = calloc(buffer_sz, 1);
// concatenate key and guid in buffer
memcpy(buffer, key, key_sz);
memcpy(buffer+key_sz, magic, sizeof(magic)-1);
// compute sha-1
SHA1Reset(&ctx);
SHA1Input(&ctx, buffer, buffer_sz);
SHA1Result(&ctx);
for(i = 0; i < 5; ++i) { // put in correct byte order before memcpy.
ctx.Message_Digest[i] = ntohl(ctx.Message_Digest[i]);
}
memcpy(sha1_output, (unsigned char*)ctx.Message_Digest, 20);
/* copy number_1, number_2, and last 8 bytes of the body. */
memcpy(buffer, &number_1, 4);
memcpy(buffer + 4, &number_2, 4);
memcpy(buffer + 8, c->body + c->body_sz - 8, 8);
/* hash that buffer, that creates the handshake signature. */
md5_init(&ctx);
md5_append(&ctx, (const md5_byte_t *)buffer, sizeof(buffer));
md5_finish(&ctx, out);
// encode `sha1_output' in base 64, into `out'.
base64_init_encodestate(&b64_ctx);
pos = base64_encode_block((const char*)sha1_output, 20, out, &b64_ctx);
base64_encode_blockend(out + pos, &b64_ctx);
// compute length, without \n
*out_sz = strlen(out);
if(out[*out_sz-1] == '\n')
(*out_sz)--;
free(buffer);
return 0;
}
@ -74,23 +80,28 @@ int
ws_handshake_reply(struct http_client *c) {
int ret;
unsigned char md5_handshake[16];
char sha1_handshake[40];
char *buffer = NULL, *p;
const char *origin = NULL, *host = NULL;
size_t origin_sz = 0, host_sz = 0, sz;
size_t origin_sz = 0, host_sz = 0, handshake_sz = 0, sz;
char template0[] = "HTTP/1.1 101 Websocket Protocol Handshake\r\n"
"Upgrade: WebSocket\r\n"
char template0[] = "HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Protocol: chat\r\n"
"Sec-WebSocket-Origin: "; /* %s */
char template1[] = "\r\n"
"Sec-WebSocket-Location: ws://"; /* %s%s */
char template2[] = "\r\n"
"Origin: http://"; /* %s */
char template3[] = "\r\n\r\n";
char template3[] = "\r\n"
"Sec-WebSocket-Accept: "; /* %s */
char template4[] = "\r\n\r\n";
if((origin = client_get_header(c, "Origin"))) {
origin_sz = strlen(origin);
} else if((origin = client_get_header(c, "Sec-WebSocket-Origin"))) {
origin_sz = strlen(origin);
}
if((host = client_get_header(c, "Host"))) {
host_sz = strlen(host);
@ -101,7 +112,8 @@ ws_handshake_reply(struct http_client *c) {
return -1;
}
if(ws_compute_handshake(c, &md5_handshake[0]) != 0) {
memset(sha1_handshake, 0, sizeof(sha1_handshake));
if(ws_compute_handshake(c, &sha1_handshake[0], &handshake_sz) != 0) {
/* failed to compute handshake. */
return -1;
}
@ -109,20 +121,21 @@ ws_handshake_reply(struct http_client *c) {
sz = sizeof(template0)-1 + origin_sz
+ sizeof(template1)-1 + host_sz + c->path_sz
+ sizeof(template2)-1 + host_sz
+ sizeof(template3)-1 + sizeof(md5_handshake);
+ sizeof(template3)-1 + handshake_sz
+ sizeof(template4)-1;
p = buffer = malloc(sz);
/* Concat all */
/* template0 */
memcpy(p, template0, sizeof(template0)-1);
memcpy(p, template0, sizeof(template0)-1);
p += sizeof(template0)-1;
memcpy(p, origin, origin_sz);
p += origin_sz;
/* template1 */
memcpy(p, template1, sizeof(template1)-1);
memcpy(p, template1, sizeof(template1)-1);
p += sizeof(template1)-1;
memcpy(p, host, host_sz);
p += host_sz;
@ -130,16 +143,22 @@ ws_handshake_reply(struct http_client *c) {
p += c->path_sz;
/* template2 */
memcpy(p, template2, sizeof(template2)-1);
memcpy(p, template2, sizeof(template2)-1);
p += sizeof(template2)-1;
memcpy(p, host, host_sz);
p += host_sz;
/* template3 */
memcpy(p, template3, sizeof(template3)-1);
memcpy(p, template3, sizeof(template3)-1);
p += sizeof(template3)-1;
memcpy(p, &md5_handshake[0], sizeof(md5_handshake));
memcpy(p, &sha1_handshake[0], handshake_sz);
p += handshake_sz;
/* template4 */
memcpy(p, template4, sizeof(template4)-1);
p += sizeof(template4)-1;
/* send data to client */
ret = write(c->fd, buffer, sz);
(void)ret;
free(buffer);
@ -186,66 +205,154 @@ ws_execute(struct http_client *c, const char *frame, size_t frame_len) {
return -1;
}
static struct ws_msg *
ws_msg_new() {
return calloc(1, sizeof(struct ws_msg));
}
static void
ws_msg_add(struct ws_msg *m, const char *p, size_t psz, const unsigned char *mask) {
/* add data to frame */
size_t i;
m->payload = realloc(m->payload, m->payload_sz + psz);
memcpy(m->payload + m->payload_sz, p, psz);
/* apply mask */
for(i = 0; i < psz && mask; ++i) {
m->payload[m->payload_sz + i] = (unsigned char)p[i] ^ mask[i%4];
}
/* save new size */
m->payload_sz += psz;
}
static void
ws_msg_free(struct ws_msg **m) {
free((*m)->payload);
free(*m);
*m = NULL;
}
static enum ws_state
ws_parse_data(const char *frame, size_t sz, struct ws_msg **msg) {
int has_mask;
uint64_t len;
const char *p;
unsigned char mask[4];
/* parse frame and extract contents */
if(sz < 8) {
return WS_READING;
}
has_mask = frame[1] & 0x80 ? 1:0;
/* get payload length */
len = frame[1] & 0x7f; /* remove leftmost bit */
if(len <= 125) { /* data starts right after the mask */
p = frame + 2 + (has_mask ? 4 : 0);
if(has_mask) memcpy(&mask, frame + 2, sizeof(mask));
} else if(len == 126) {
uint16_t sz16;
memcpy(&sz16, frame + 2, sizeof(uint16_t));
len = ntohs(sz16);
p = frame + 4 + (has_mask ? 4 : 0);
if(has_mask) memcpy(&mask, frame + 4, sizeof(mask));
} else if(len == 127) {
len = webdis_ntohl64(frame+2);
p = frame + 10 + (has_mask ? 4 : 0);
if(has_mask) memcpy(&mask, frame + 10, sizeof(mask));
} else {
return WS_ERROR;
}
/* we now have the (possibly masked) data starting in p, and its length. */
if(len > sz - (p - frame)) { /* not enough data */
return WS_READING;
}
if(!*msg)
*msg = ws_msg_new();
ws_msg_add(*msg, p, len, has_mask ? mask : NULL);
(*msg)->total_sz += len + (p - frame);
if(frame[0] & 0x80) { /* FIN bit set */
return WS_MSG_COMPLETE;
} else {
return WS_READING; /* need more data */
}
}
/**
* Process some data just received on the socket.
*/
enum ws_read_action
enum ws_state
ws_add_data(struct http_client *c) {
const char *frame_start, *frame_end;
char *tmp;
while(1) {
/* look for frame start */
if(!c->sz || c->buffer[0] != '\x00') {
/* can't find frame start */
return WS_READ_FAIL;
}
/* look for frame end */
int ret;
size_t frame_len;
frame_start = c->buffer;
frame_end = memchr(frame_start, '\xff', c->sz);
if(frame_end == NULL) {
/* continue reading */
return WS_READ_MORE;
}
enum ws_state state;
state = ws_parse_data(c->buffer, c->sz, &c->frame);
if(state == WS_MSG_COMPLETE) {
int ret = ws_execute(c, c->frame->payload, c->frame->payload_sz);
/* parse and execute frame. */
frame_len = frame_end - frame_start - 1;
/* remove frame from client buffer */
http_client_remove_data(c, c->frame->total_sz);
/* free frame and set back to NULL */
ws_msg_free(&c->frame);
ret = ws_execute(c, frame_start + 1, frame_len);
if(ret != 0) {
/* can't process frame. */
return WS_READ_FAIL;
return WS_ERROR;
}
/* remove frame from buffer */
c->sz -= (2 + frame_len);
tmp = malloc(c->sz);
memcpy(tmp, c->buffer + 2 + frame_len, c->sz);
free(c->buffer);
c->buffer = tmp;
}
return state;
}
int
ws_reply(struct cmd *cmd, const char *p, size_t sz) {
int ret;
char *buffer = malloc(sz + 2);
/* create frame by prepending 0x00 and appending 0xff */
buffer[0] = '\x00';
memcpy(buffer + 1, p, sz);
buffer[sz + 1] = '\xff';
char *frame = malloc(sz + 8); /* create frame by prepending header */
size_t frame_sz = 0;
/*
The length of the "Payload data", in bytes: if 0-125, that is the
payload length. If 126, the following 2 bytes interpreted as a
16-bit unsigned integer are the payload length. If 127, the
following 8 bytes interpreted as a 64-bit unsigned integer (the
most significant bit MUST be 0) are the payload length.
*/
frame[0] = '\x81';
if(sz <= 125) {
frame[1] = sz;
memcpy(frame + 2, p, sz);
frame_sz = sz + 2;
} else if (sz > 125 && sz <= 65536) {
uint16_t sz16 = htons(sz);
frame[1] = 126;
memcpy(frame + 2, &sz16, 2);
memcpy(frame + 4, p, sz);
frame_sz = sz + 4;
} else if (sz > 65536) {
char sz64[8] = webdis_htonl64(sz);
frame[1] = 127;
memcpy(frame + 2, sz64, 8);
memcpy(frame + 10, p, sz);
frame_sz = sz + 10;
}
/* send WS frame */
ret = write(cmd->fd, buffer, sz+2);
free(buffer);
ret = write(cmd->fd, frame, frame_sz);
free(frame);
if(ret == (int)sz + 2) {
/* success */
if(ret == (int)frame_sz) { /* success */
return 0;
}

@ -2,19 +2,26 @@
#define WEBSOCKET_H
#include <stdlib.h>
#include <stdint.h>
struct http_client;
struct cmd;
enum ws_read_action {
WS_READ_FAIL,
WS_READ_MORE,
WS_READ_EXEC};
enum ws_state {
WS_ERROR,
WS_READING,
WS_MSG_COMPLETE};
struct ws_msg {
char *payload;
size_t payload_sz;
size_t total_sz;
};
int
ws_handshake_reply(struct http_client *c);
enum ws_read_action
enum ws_state
ws_add_data(struct http_client *c);
int

Loading…
Cancel
Save