Compare commits
No commits in common. "b6cf73165849a9bcd7c830e19d0184dd6c61642b" and "6cf4ef1bef2e265204da074b88aa4bdba21eac66" have entirely different histories.
b6cf731658
...
6cf4ef1bef
2 changed files with 32 additions and 149 deletions
137
src/wsock.c
137
src/wsock.c
|
@ -1,30 +1,5 @@
|
|||
#include "wsock.h"
|
||||
|
||||
/** UTILITIES **/
|
||||
/*****************/
|
||||
|
||||
#define WS_REORDER_BUFLEN (sizeof(long double))
|
||||
|
||||
inline int _sys_is_net_order() {
|
||||
return ((char*)(0xB00B))[0] == 0xB0;
|
||||
}
|
||||
|
||||
void swap_order(char* in, int length) {
|
||||
if(_sys_is_net_order() || length > WS_REORDER_BUFLEN)
|
||||
return;
|
||||
|
||||
char buf[WS_REORDER_BUFLEN];
|
||||
for(int i = 0; i < length; ++i)
|
||||
buf[i] = in[length - i - 1];
|
||||
memcpy(in, buf, length);
|
||||
}
|
||||
|
||||
void swap_order_copy(char* in, char* out, int length) {
|
||||
memcpy(out, in, length);
|
||||
swap_order(out, length);
|
||||
}
|
||||
|
||||
|
||||
/** DATA BUFFER **/
|
||||
/*******************/
|
||||
|
||||
|
@ -52,67 +27,48 @@ void buffer_append
|
|||
(buffer_t* buf, const char* data, int length)
|
||||
{
|
||||
if(buf->data != NULL) {
|
||||
if(buf->next == NULL)
|
||||
buf->next = buffer_create();
|
||||
buffer_append(buf->next, data, length);
|
||||
} else {
|
||||
}
|
||||
|
||||
buf->data = malloc(length);
|
||||
strncpy(buf->data, data, length);
|
||||
buf->length = length;
|
||||
}
|
||||
}
|
||||
|
||||
void buffer_append_str(buffer_t* buf, const char* data) {
|
||||
buffer_append(buf, data, strlen(data));
|
||||
}
|
||||
|
||||
int _buffer_read(buffer_t* buf, char* out, int n) {
|
||||
void _buffer_read(buffer_t* buf, char* out) {
|
||||
int i = 0;
|
||||
while(buf != NULL && (!n || (n && i < n))) {
|
||||
if(!n || buf->length <= n - i) {
|
||||
while(buf != NULL) {
|
||||
strncpy(out + i, buf->data, buf->length);
|
||||
i += buf->length;
|
||||
buf = buf->next;
|
||||
} else if(n && buf->length > n - i) {
|
||||
strncpy(out + i, buf->data, n - i);
|
||||
i += n - i;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int buffer_peek(buffer_t* buf, char* out, int length) {
|
||||
return _buffer_read(buf, out, length);
|
||||
}
|
||||
|
||||
int buffer_peek_str(buffer_t* buf, char* out, int length) {
|
||||
int read = _buffer_read(buf, out, length);
|
||||
out[read] = '\0';
|
||||
return read;
|
||||
}
|
||||
|
||||
int buffer_read(buffer_t* buf, char** out) {
|
||||
int length = buffer_length(buf);
|
||||
*out = malloc(length);
|
||||
|
||||
_buffer_read(buf, *out, 0);
|
||||
_buffer_read(buf, *out);
|
||||
return length;
|
||||
}
|
||||
|
||||
int buffer_read_str(buffer_t* buf, char** out) {
|
||||
int length = buffer_length(buf);
|
||||
*out = malloc(length + 1);
|
||||
(*out)[length] = '\0';
|
||||
out[length] = '\0';
|
||||
|
||||
_buffer_read(buf, *out, 0);
|
||||
_buffer_read(buf, *out);
|
||||
return length;
|
||||
}
|
||||
|
||||
int buffer_flush(buffer_t* buf, char** out) {
|
||||
int length = buffer_read(buf, out);
|
||||
buffer_clear(buf);
|
||||
return length;
|
||||
return out;
|
||||
}
|
||||
|
||||
int buffer_flush_str(buffer_t* buf, char** out) {
|
||||
|
@ -149,56 +105,6 @@ void buffer_free(buffer_t* buf) {
|
|||
/** WEB SOCKET **/
|
||||
/******************/
|
||||
|
||||
typedef struct {
|
||||
uint8_t fin, opcode, masked, head_length;
|
||||
uint8_t mask[4];
|
||||
uint64_t body_length;
|
||||
} _ws_head_t;
|
||||
|
||||
typedef enum {
|
||||
WS_CONT = 0x0,
|
||||
WS_TEXT = 0x1,
|
||||
WS_BIN = 0x2,
|
||||
WS_CLOSE = 0x8,
|
||||
WS_PING = 0x9,
|
||||
WS_PONG = 0xA
|
||||
} _ws_opcode_t;
|
||||
|
||||
int _wsock_header_length(char* head) {
|
||||
return 2
|
||||
+ ((head[1] & 0x80) == 0 ? 0 : 4)
|
||||
+ ((head[1] & 0x7F) < 0x7E ? 0
|
||||
: ((head[1] & 0x7F) == 0x7E ? 2 : 8));
|
||||
}
|
||||
|
||||
int _wsock_read_header(char* head, _ws_head_t* out) {
|
||||
out->head_length = _wsock_header_length(head);
|
||||
out->fin = 0x80 & head[0];
|
||||
out->opcode = 0x0F & head[0];
|
||||
out->masked = 0x80 & head[1];
|
||||
|
||||
if((head[0] & 0x70) != 0)
|
||||
return -1;
|
||||
if((head[0] & 0x0F) > 0x2 && (head[0] & 0x0F) < 0x8)
|
||||
return -1;
|
||||
if((head[0] & 0x0F) > 0xA && (head[0] & 0x0F) <= 0xF)
|
||||
return -1;
|
||||
|
||||
int mask_start;
|
||||
if((head[1] & 0x7F) < 0x7E) {
|
||||
out->body_length = head[1] & 0x7F;
|
||||
mask_start = 2;
|
||||
} else if((head[1] & 0x7F) == 0x7E) {
|
||||
swap_order_copy(head[2], (char*)out->body_length, 2);
|
||||
mask_start = 4;
|
||||
} else {
|
||||
swap_order_copy(head[2], (char*)out->body_length, 8);
|
||||
mask_start = 10;
|
||||
}
|
||||
if(out->masked)
|
||||
memcpy(out->mask, head + mask_start, 4);
|
||||
}
|
||||
|
||||
wsock_t* wsock_open
|
||||
(const char* host, const char* path, uint16_t port)
|
||||
{
|
||||
|
@ -247,8 +153,8 @@ wsock_t* wsock_open
|
|||
pfd.fd = sock;
|
||||
pfd.events = POLLIN;
|
||||
|
||||
int total = 0;
|
||||
char* end;
|
||||
int total = 0;
|
||||
while((end = strstr(s_shake, "\r\n\r\n")) == NULL) {
|
||||
if(total + 1 >= sizeof(s_shake)
|
||||
|| poll(&pfd, 1, 5000) <= 0)
|
||||
|
@ -257,14 +163,11 @@ wsock_t* wsock_open
|
|||
return NULL;
|
||||
}
|
||||
|
||||
total = recv(sock, s_shake, sizeof(s_shake) - 1, MSG_PEEK);
|
||||
total += recv(sock, s_shake + total,
|
||||
sizeof(s_shake) - total - 1, 0);
|
||||
s_shake[total] = '\0';
|
||||
}
|
||||
|
||||
total = end - s_shake + 3;
|
||||
recv(sock, s_shake, total, 0);
|
||||
s_shake[total + 1] = '\0';
|
||||
|
||||
const char resp_check[] = "HTTP/1.1 101 Switching Protocols";
|
||||
if(strncmp(s_shake, resp_check, sizeof(resp_check) - 1) != 0)
|
||||
{
|
||||
|
@ -275,8 +178,11 @@ wsock_t* wsock_open
|
|||
wsock_t* wsock = malloc(sizeof(wsock_t));
|
||||
wsock->sock = sock;
|
||||
wsock->mode = WS_BLOCK;
|
||||
wsock->recv_buf = buffer_create();
|
||||
wsock->send_buf = buffer_create();
|
||||
wsock->buffer = buffer_create();
|
||||
if(total - 1 != (end - s_shake) + 3)
|
||||
buffer_append(wsock->buffer, end + 4,
|
||||
total - 1 - ((end - s_shake) + 3));
|
||||
|
||||
return wsock;
|
||||
}
|
||||
|
||||
|
@ -288,15 +194,7 @@ int wsock_mode_get(wsock_t* ws) {
|
|||
return ws->mode;
|
||||
}
|
||||
|
||||
int wsock_recv(wsock_t* ws, char** out) {
|
||||
char buffer[4096];
|
||||
int got = 0;
|
||||
do {
|
||||
//if((ws->mode & WS_NOBLOCK) != 0)
|
||||
|
||||
} while((ws->mode & WS_FULL_RECV) != 0);
|
||||
return got;
|
||||
}
|
||||
|
||||
int wsock_is_open(wsock_t* ws) {
|
||||
return ws->sock >= 0;
|
||||
|
@ -305,8 +203,7 @@ int wsock_is_open(wsock_t* ws) {
|
|||
void wsock_close(wsock_t* ws) {
|
||||
// todo: send close frame
|
||||
shutdown(ws->sock, SHUT_RDWR);
|
||||
buffer_free(ws->recv_buf);
|
||||
buffer_free(ws->send_buf);
|
||||
buffer_free(ws->buffer);
|
||||
free(ws);
|
||||
}
|
||||
|
||||
|
|
28
src/wsock.h
28
src/wsock.h
|
@ -1,20 +1,12 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/poll.h>
|
||||
#include <netdb.h>
|
||||
|
||||
/** UTILITIES **/
|
||||
/*****************/
|
||||
|
||||
void swap_order(char*, int);
|
||||
void swap_order_copy(char*, char*, int);
|
||||
|
||||
|
||||
/** DATA BUFFER **/
|
||||
/*******************/
|
||||
|
||||
|
@ -32,12 +24,10 @@ int buffer_length(buffer_t*);
|
|||
void buffer_append(buffer_t*, const char*, int);
|
||||
void buffer_append_str(buffer_t*, const char*);
|
||||
|
||||
int buffer_peek(buffer_t*, char*, int);
|
||||
int buffer_peek_str(buffer_t*, char*, int);
|
||||
int buffer_read(buffer_t*, char**);
|
||||
int buffer_read_str(buffer_t*, char**);
|
||||
int buffer_flush(buffer_t*, char**);
|
||||
int buffer_flush_str(buffer_t*, char**);
|
||||
char* buffer_read(buffer_t*, int*);
|
||||
char* buffer_read_str(buffer_t*, int*);
|
||||
char* buffer_flush(buffer_t*, int*);
|
||||
char* buffer_flush_str(buffer_t*, int*);
|
||||
|
||||
void buffer_clear(buffer_t*);
|
||||
void buffer_free(buffer_t*);
|
||||
|
@ -54,9 +44,7 @@ typedef enum {
|
|||
// block until a full packet is received
|
||||
WS_FULL_RECV = 2,
|
||||
// block until a full packet is sent
|
||||
WS_FULL_SEND = 4,
|
||||
// receive text frames as binary
|
||||
WS_RECV_ALL_BIN = 8
|
||||
WS_FULL_SEND = 4
|
||||
} ws_mode_t;
|
||||
// mode options that are not mutually exclusive can be
|
||||
// concatenated using the OR (|) operator. note that
|
||||
|
@ -85,16 +73,14 @@ typedef enum {
|
|||
|
||||
typedef struct {
|
||||
int sock, mode;
|
||||
buffer_t *recv_buf, *send_buf;
|
||||
buffer_t* buffer;
|
||||
} wsock_t;
|
||||
|
||||
wsock_t* wsock_open(const char*, const char*, uint16_t);
|
||||
void wsock_mode_set(wsock_t*, int);
|
||||
int wsock_mode_get(wsock_t*);
|
||||
|
||||
int wsock_recv(wsock_t*, char**);
|
||||
int wsock_send(wsock_t*, char*, int);
|
||||
int wsock_send_str(wsock_t*, char*);
|
||||
int wsock_recv(wsock_t*,
|
||||
|
||||
int wsock_is_open(wsock_t*);
|
||||
void wsock_close(wsock_t*);
|
||||
|
|
Loading…
Reference in a new issue