revert
This commit is contained in:
parent
2e0cd74e07
commit
4def48b881
|
@ -1,18 +1,11 @@
|
||||||
#include "ws.h"
|
#include "ws.h"
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef _MSC_VER
|
|
||||||
#define ms_sleep(x) usleep( x * 1000);
|
|
||||||
#else
|
|
||||||
#define ms_sleep(x) Sleep( x );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define SHA1_HASH_SIZE 20
|
#define SHA1_HASH_SIZE 20
|
||||||
struct ws_globals_s ws_globals;
|
struct globals_s globals;
|
||||||
|
|
||||||
#ifndef WSS_STANDALONE
|
#ifndef WSS_STANDALONE
|
||||||
|
|
||||||
|
@ -89,20 +82,20 @@ void init_ssl(void) {
|
||||||
|
|
||||||
OpenSSL_add_all_algorithms(); /* load & register cryptos */
|
OpenSSL_add_all_algorithms(); /* load & register cryptos */
|
||||||
SSL_load_error_strings(); /* load all error messages */
|
SSL_load_error_strings(); /* load all error messages */
|
||||||
ws_globals.ssl_method = TLSv1_server_method(); /* create server instance */
|
globals.ssl_method = TLSv1_server_method(); /* create server instance */
|
||||||
ws_globals.ssl_ctx = SSL_CTX_new(ws_globals.ssl_method); /* create context */
|
globals.ssl_ctx = SSL_CTX_new(globals.ssl_method); /* create context */
|
||||||
assert(ws_globals.ssl_ctx);
|
assert(globals.ssl_ctx);
|
||||||
|
|
||||||
/* set the local certificate from CertFile */
|
/* set the local certificate from CertFile */
|
||||||
SSL_CTX_use_certificate_file(ws_globals.ssl_ctx, ws_globals.cert, SSL_FILETYPE_PEM);
|
SSL_CTX_use_certificate_file(globals.ssl_ctx, globals.cert, SSL_FILETYPE_PEM);
|
||||||
/* set the private key from KeyFile */
|
/* set the private key from KeyFile */
|
||||||
SSL_CTX_use_PrivateKey_file(ws_globals.ssl_ctx, ws_globals.key, SSL_FILETYPE_PEM);
|
SSL_CTX_use_PrivateKey_file(globals.ssl_ctx, globals.key, SSL_FILETYPE_PEM);
|
||||||
/* verify private key */
|
/* verify private key */
|
||||||
if ( !SSL_CTX_check_private_key(ws_globals.ssl_ctx) ) {
|
if ( !SSL_CTX_check_private_key(globals.ssl_ctx) ) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
SSL_CTX_set_cipher_list(ws_globals.ssl_ctx, "HIGH:!DSS:!aNULL@STRENGTH");
|
SSL_CTX_set_cipher_list(globals.ssl_ctx, "HIGH:!DSS:!aNULL@STRENGTH");
|
||||||
|
|
||||||
thread_setup();
|
thread_setup();
|
||||||
}
|
}
|
||||||
|
@ -147,7 +140,7 @@ static int cheezy_get_var(char *data, char *name, char *buf, size_t buflen)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (v && e) {
|
if (v && e) {
|
||||||
int cplen;
|
size_t cplen;
|
||||||
size_t len = e - v;
|
size_t len = e - v;
|
||||||
|
|
||||||
if (len > buflen - 1) {
|
if (len > buflen - 1) {
|
||||||
|
@ -224,55 +217,18 @@ static void sha1_digest(unsigned char *digest, char *in)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int ws_handshake(wsh_t *wsh)
|
int ws_handshake_kvp(wsh_t *wsh, char *key, char *version, char *proto)
|
||||||
{
|
{
|
||||||
char key[256] = "";
|
|
||||||
char version[5] = "";
|
|
||||||
char proto[256] = "";
|
|
||||||
char proto_buf[384] = "";
|
|
||||||
char uri[256] = "";
|
|
||||||
char input[256] = "";
|
char input[256] = "";
|
||||||
unsigned char output[SHA1_HASH_SIZE] = "";
|
unsigned char output[SHA1_HASH_SIZE] = "";
|
||||||
char b64[256] = "";
|
char b64[256] = "";
|
||||||
char respond[512] = "";
|
char respond[512] = "";
|
||||||
ssize_t bytes;
|
|
||||||
char *p, *e = 0;
|
|
||||||
|
|
||||||
if (wsh->sock == ws_sock_invalid) {
|
if (!wsh->tsession) {
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
while((bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, wsh->buflen - wsh->datalen)) > 0) {
|
if (!*key || !*version || !*proto) {
|
||||||
wsh->datalen += bytes;
|
|
||||||
if (strstr(wsh->buffer, "\r\n\r\n") || strstr(wsh->buffer, "\n\n")) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bytes > sizeof(wsh->buffer) -1) {
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
*(wsh->buffer+bytes) = '\0';
|
|
||||||
|
|
||||||
if (strncasecmp(wsh->buffer, "GET ", 4)) {
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
p = wsh->buffer + 4;
|
|
||||||
|
|
||||||
e = strchr(p, ' ');
|
|
||||||
if (!e) {
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(uri, p, e-p);
|
|
||||||
|
|
||||||
cheezy_get_var(wsh->buffer, "Sec-WebSocket-Key", key, sizeof(key));
|
|
||||||
cheezy_get_var(wsh->buffer, "Sec-WebSocket-Version", version, sizeof(version));
|
|
||||||
cheezy_get_var(wsh->buffer, "Sec-WebSocket-Protocol", proto, sizeof(proto));
|
|
||||||
|
|
||||||
if (!*key) {
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,33 +236,25 @@ int ws_handshake(wsh_t *wsh)
|
||||||
sha1_digest(output, input);
|
sha1_digest(output, input);
|
||||||
b64encode((unsigned char *)output, SHA1_HASH_SIZE, (unsigned char *)b64, sizeof(b64));
|
b64encode((unsigned char *)output, SHA1_HASH_SIZE, (unsigned char *)b64, sizeof(b64));
|
||||||
|
|
||||||
if (*proto) {
|
|
||||||
snprintf(proto_buf, sizeof(proto_buf), "Sec-WebSocket-Protocol: %s\r\n", proto);
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(respond, sizeof(respond),
|
snprintf(respond, sizeof(respond),
|
||||||
"HTTP/1.1 101 Switching Protocols\r\n"
|
"HTTP/1.1 101 Switching Protocols\r\n"
|
||||||
"Upgrade: websocket\r\n"
|
"Upgrade: websocket\r\n"
|
||||||
"Connection: Upgrade\r\n"
|
"Connection: Upgrade\r\n"
|
||||||
"Sec-WebSocket-Accept: %s\r\n"
|
"Sec-WebSocket-Accept: %s\r\n"
|
||||||
"%s\r\n",
|
"Sec-WebSocket-Protocol: %s\r\n\r\n",
|
||||||
b64,
|
b64,
|
||||||
proto_buf);
|
proto);
|
||||||
|
|
||||||
|
if (ws_raw_write(wsh, respond, strlen(respond))) {
|
||||||
ws_raw_write(wsh, respond, strlen(respond));
|
|
||||||
wsh->handshake = 1;
|
wsh->handshake = 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
err:
|
err:
|
||||||
|
|
||||||
snprintf(respond, sizeof(respond), "HTTP/1.1 400 Bad Request\r\n"
|
snprintf(respond, sizeof(respond), "HTTP/1.1 400 Bad Request\r\n"
|
||||||
"Sec-WebSocket-Version: 13\r\n\r\n");
|
"Sec-WebSocket-Version: 13\r\n\r\n");
|
||||||
|
|
||||||
//printf("ERR:\n%s\n", respond);
|
|
||||||
|
|
||||||
|
|
||||||
ws_raw_write(wsh, respond, strlen(respond));
|
ws_raw_write(wsh, respond, strlen(respond));
|
||||||
|
|
||||||
ws_close(wsh, WS_NONE);
|
ws_close(wsh, WS_NONE);
|
||||||
|
@ -315,238 +263,85 @@ int ws_handshake(wsh_t *wsh)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes)
|
issize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes)
|
||||||
{
|
{
|
||||||
ssize_t r;
|
issize_t r;
|
||||||
int err = 0;
|
TConn *conn = wsh->tsession->connP;
|
||||||
|
|
||||||
if (wsh->ssl) {
|
|
||||||
do {
|
|
||||||
r = SSL_read(wsh->ssl, data, bytes);
|
|
||||||
#ifndef _MSC_VER
|
|
||||||
if (wsh->x++) usleep(10000);
|
|
||||||
#else
|
|
||||||
if (wsh->x++) Sleep(10);
|
|
||||||
#endif
|
|
||||||
if (r == -1) {
|
|
||||||
err = SSL_get_error(wsh->ssl, r);
|
|
||||||
|
|
||||||
if (wsh->handshake && err == SSL_ERROR_WANT_READ) {
|
|
||||||
r = -2;
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (r == -1 && err == SSL_ERROR_WANT_READ && wsh->x < 100);
|
|
||||||
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
r = recv(wsh->sock, data, bytes, 0);
|
|
||||||
#ifndef _MSC_VER
|
|
||||||
if (wsh->x++) usleep(10000);
|
|
||||||
#else
|
|
||||||
if (wsh->x++) Sleep(10);
|
|
||||||
#endif
|
|
||||||
} while (r == -1 && xp_is_blocking(xp_errno()) && wsh->x < 100);
|
|
||||||
|
|
||||||
if (wsh->x >= 100) {
|
|
||||||
r = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
end:
|
|
||||||
|
|
||||||
if (r > 0) {
|
|
||||||
*((char *)data + r) = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r >= 0) {
|
|
||||||
wsh->x = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!wsh->handshake) {
|
||||||
|
r = wsh->tsession->connP->buffersize;
|
||||||
|
memcpy(data, conn->buffer.b, r);
|
||||||
|
printf("%s\n", conn->buffer.t);
|
||||||
|
ConnReadInit(conn);
|
||||||
return r;
|
return r;
|
||||||
|
} else {
|
||||||
|
const char *readError = NULL;
|
||||||
|
|
||||||
|
// printf(" pos=%d size=%d need=%d\n", conn->bufferpos, conn->buffersize, bytes);
|
||||||
|
|
||||||
|
r = conn->buffersize - conn->bufferpos;
|
||||||
|
|
||||||
|
if (r < 0) {
|
||||||
|
printf("286 Read Error %d!\n", r);
|
||||||
|
return 0;
|
||||||
|
} else if (r == 0) {
|
||||||
|
ConnRead(conn, 2, NULL, NULL, &readError);
|
||||||
|
|
||||||
|
if (readError) {
|
||||||
|
// printf("292 Read Error %s\n", readError);
|
||||||
|
free((void *)readError);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = conn->buffersize - conn->bufferpos;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r <= (issize_t)bytes) {
|
||||||
|
memcpy(data, conn->buffer.b + conn->bufferpos, r);
|
||||||
|
// ConnReadInit(conn);
|
||||||
|
conn->bufferpos = conn->buffersize;
|
||||||
|
ConnReadInit(conn);
|
||||||
|
return r;
|
||||||
|
} else {
|
||||||
|
memcpy(data, conn->buffer.b + conn->bufferpos, bytes);
|
||||||
|
conn->bufferpos += (uint32_t)bytes;
|
||||||
|
return (issize_t)bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
|
issize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
|
||||||
{
|
{
|
||||||
size_t r;
|
size_t r;
|
||||||
|
|
||||||
if (wsh->ssl) {
|
if (wsh->ssl) {
|
||||||
do {
|
do {
|
||||||
r = SSL_write(wsh->ssl, data, bytes);
|
r = SSL_write(wsh->ssl, data, (int)bytes);
|
||||||
} while (r == -1 && SSL_get_error(wsh->ssl, r) == SSL_ERROR_WANT_WRITE);
|
} while (r == -1 && SSL_get_error(wsh->ssl, (int)r) == SSL_ERROR_WANT_WRITE);
|
||||||
|
|
||||||
return r;
|
return (issize_t)r;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
if (ConnWrite(wsh->tsession->connP, data, (uint32_t)bytes)) {
|
||||||
r = send(wsh->sock, data, bytes, 0);
|
return (issize_t)bytes;
|
||||||
} while (r == -1 && xp_is_blocking(xp_errno()));
|
|
||||||
|
|
||||||
//if (r<0) {
|
|
||||||
//printf("wRITE FAIL: %s\n", strerror(errno));
|
|
||||||
//}
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
static int setup_socket(ws_socket_t sock)
|
|
||||||
{
|
|
||||||
unsigned long v = 1;
|
|
||||||
|
|
||||||
if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int restore_socket(ws_socket_t sock)
|
|
||||||
{
|
|
||||||
unsigned long v = 0;
|
|
||||||
|
|
||||||
if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
static int setup_socket(ws_socket_t sock)
|
|
||||||
{
|
|
||||||
int flags = fcntl(sock, F_GETFL, 0);
|
|
||||||
return fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int restore_socket(ws_socket_t sock)
|
|
||||||
{
|
|
||||||
int flags = fcntl(sock, F_GETFL, 0);
|
|
||||||
|
|
||||||
flags &= ~O_NONBLOCK;
|
|
||||||
|
|
||||||
return fcntl(sock, F_SETFL, flags);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static int establish_logical_layer(wsh_t *wsh)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (!wsh->sanity) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->logical_established) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->secure && !wsh->secure_established) {
|
|
||||||
int code;
|
|
||||||
|
|
||||||
if (!wsh->ssl) {
|
|
||||||
wsh->ssl = SSL_new(wsh->ssl_ctx);
|
|
||||||
assert(wsh->ssl);
|
|
||||||
|
|
||||||
SSL_set_fd(wsh->ssl, wsh->sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
code = SSL_accept(wsh->ssl);
|
|
||||||
|
|
||||||
if (code == 1) {
|
|
||||||
wsh->secure_established = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (code == 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (code < 0) {
|
|
||||||
if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->block) {
|
|
||||||
ms_sleep(10);
|
|
||||||
} else {
|
} else {
|
||||||
ms_sleep(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
wsh->sanity--;
|
|
||||||
|
|
||||||
if (!wsh->block) {
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (wsh->sanity > 0);
|
|
||||||
|
|
||||||
if (!wsh->sanity) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!wsh->down && !wsh->handshake) {
|
|
||||||
int r = ws_handshake(wsh);
|
|
||||||
|
|
||||||
if (r < 0) {
|
|
||||||
wsh->down = 1;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!wsh->handshake && !wsh->block) {
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
wsh->logical_established = 1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wsh_t * ws_init(ws_tsession_t *tsession)
|
||||||
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block)
|
|
||||||
{
|
{
|
||||||
|
wsh_t *wsh = (wsh_t *)malloc(sizeof(*wsh));
|
||||||
|
|
||||||
|
if (!wsh) return NULL;
|
||||||
|
|
||||||
memset(wsh, 0, sizeof(*wsh));
|
memset(wsh, 0, sizeof(*wsh));
|
||||||
|
wsh->tsession = tsession;
|
||||||
wsh->sock = sock;
|
|
||||||
wsh->block = block;
|
|
||||||
wsh->sanity = 5000;
|
|
||||||
wsh->ssl_ctx = ssl_ctx;
|
|
||||||
|
|
||||||
if (!ssl_ctx) {
|
|
||||||
ssl_ctx = ws_globals.ssl_ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (close_sock) {
|
|
||||||
wsh->close_sock = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
wsh->buflen = sizeof(wsh->buffer);
|
wsh->buflen = sizeof(wsh->buffer);
|
||||||
wsh->secure = ssl_ctx ? 1 : 0;
|
|
||||||
|
|
||||||
setup_socket(sock);
|
return wsh;
|
||||||
|
|
||||||
if (establish_logical_layer(wsh) == -1) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->down) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ws_destroy(wsh_t *wsh)
|
void ws_destroy(wsh_t *wsh)
|
||||||
|
@ -577,7 +372,7 @@ void ws_destroy(wsh_t *wsh)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_close(wsh_t *wsh, int16_t reason)
|
issize_t ws_close(wsh_t *wsh, int16_t reason)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (wsh->down) {
|
if (wsh->down) {
|
||||||
|
@ -586,45 +381,20 @@ ssize_t ws_close(wsh_t *wsh, int16_t reason)
|
||||||
|
|
||||||
wsh->down = 1;
|
wsh->down = 1;
|
||||||
|
|
||||||
if (reason && wsh->sock != ws_sock_invalid) {
|
|
||||||
uint16_t *u16;
|
|
||||||
uint8_t fr[4] = {WSOC_CLOSE | 0x80, 2, 0};
|
|
||||||
|
|
||||||
u16 = (uint16_t *) &fr[2];
|
|
||||||
*u16 = htons((int16_t)reason);
|
|
||||||
ws_raw_write(wsh, fr, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
restore_socket(wsh->sock);
|
|
||||||
|
|
||||||
if (wsh->close_sock && wsh->sock != ws_sock_invalid) {
|
|
||||||
close(wsh->sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
wsh->sock = ws_sock_invalid;
|
|
||||||
|
|
||||||
return reason * -1;
|
return reason * -1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
issize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
{
|
{
|
||||||
|
|
||||||
ssize_t need = 2;
|
issize_t need = 2;
|
||||||
char *maskp;
|
char *maskp;
|
||||||
int ll = 0;
|
|
||||||
|
|
||||||
again:
|
again:
|
||||||
need = 2;
|
need = 2;
|
||||||
maskp = NULL;
|
maskp = NULL;
|
||||||
*data = NULL;
|
*data = NULL;
|
||||||
|
|
||||||
ll = establish_logical_layer(wsh);
|
|
||||||
|
|
||||||
if (ll < 0) {
|
|
||||||
return ll;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->down) {
|
if (wsh->down) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -633,15 +403,10 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_PROTO_ERR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9)) < 0) {
|
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 14)) < need) {
|
||||||
if (wsh->datalen == -2) {
|
while (!wsh->down && (wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 14 - wsh->datalen)) < need) ;
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wsh->datalen < need) {
|
if (0 && (wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 14 - wsh->datalen)) < need) {
|
||||||
if ((wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen)) < need) {
|
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_PROTO_ERR);
|
||||||
}
|
}
|
||||||
|
@ -718,13 +483,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
|
|
||||||
need = (wsh->plen - (wsh->datalen - need));
|
need = (wsh->plen - (wsh->datalen - need));
|
||||||
|
|
||||||
if (need < 0) {
|
if ((need + wsh->datalen) > (issize_t)wsh->buflen) {
|
||||||
/* invalid read - protocol err .. */
|
|
||||||
*oc = WSOC_CLOSE;
|
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((need + wsh->datalen) > (ssize_t)wsh->buflen) {
|
|
||||||
/* too big - Ain't nobody got time fo' dat */
|
/* too big - Ain't nobody got time fo' dat */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_DATA_TOO_BIG);
|
return ws_close(wsh, WS_DATA_TOO_BIG);
|
||||||
|
@ -733,7 +492,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
wsh->rplen = wsh->plen - need;
|
wsh->rplen = wsh->plen - need;
|
||||||
|
|
||||||
while(need) {
|
while(need) {
|
||||||
ssize_t r = ws_raw_read(wsh, wsh->payload + wsh->rplen, need);
|
issize_t r = ws_raw_read(wsh, wsh->payload + wsh->rplen, need);
|
||||||
|
|
||||||
if (r < 1) {
|
if (r < 1) {
|
||||||
/* invalid read - protocol err .. */
|
/* invalid read - protocol err .. */
|
||||||
|
@ -747,7 +506,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mask && maskp) {
|
if (mask && maskp) {
|
||||||
ssize_t i;
|
issize_t i;
|
||||||
|
|
||||||
for (i = 0; i < wsh->datalen; i++) {
|
for (i = 0; i < wsh->datalen; i++) {
|
||||||
wsh->payload[i] ^= maskp[i % 4];
|
wsh->payload[i] ^= maskp[i % 4];
|
||||||
|
@ -780,7 +539,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes)
|
issize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (bytes + wsh->wdatalen > wsh->buflen) {
|
if (bytes + wsh->wdatalen > wsh->buflen) {
|
||||||
|
@ -789,14 +548,14 @@ ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes)
|
||||||
|
|
||||||
memcpy(wsh->wbuffer + wsh->wdatalen, data, bytes);
|
memcpy(wsh->wbuffer + wsh->wdatalen, data, bytes);
|
||||||
|
|
||||||
wsh->wdatalen += bytes;
|
wsh->wdatalen += (issize_t)bytes;
|
||||||
|
|
||||||
return bytes;
|
return (issize_t)bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc)
|
issize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc)
|
||||||
{
|
{
|
||||||
ssize_t r = 0;
|
issize_t r = 0;
|
||||||
|
|
||||||
if (!wsh->wdatalen) {
|
if (!wsh->wdatalen) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -810,7 +569,7 @@ ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes)
|
issize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes)
|
||||||
{
|
{
|
||||||
uint8_t hdr[14] = { 0 };
|
uint8_t hdr[14] = { 0 };
|
||||||
size_t hlen = 2;
|
size_t hlen = 2;
|
||||||
|
@ -844,39 +603,15 @@ ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes)
|
||||||
*u64 = htonl(bytes);
|
*u64 = htonl(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ws_raw_write(wsh, (void *) &hdr[0], hlen) != (ssize_t)hlen) {
|
if (ws_raw_write(wsh, (void *) &hdr[0], hlen) != (issize_t)hlen) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ws_raw_write(wsh, data, bytes) != (ssize_t)bytes) {
|
if (ws_raw_write(wsh, data, bytes) != (issize_t)bytes) {
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return (issize_t)bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
|
|
||||||
int xp_errno(void)
|
|
||||||
{
|
|
||||||
return WSAGetLastError();
|
|
||||||
}
|
|
||||||
|
|
||||||
int xp_is_blocking(int errcode)
|
|
||||||
{
|
|
||||||
return errcode == WSAEWOULDBLOCK || errcode == WSAEINPROGRESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
int xp_errno(void)
|
|
||||||
{
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
int xp_is_blocking(int errcode)
|
|
||||||
{
|
|
||||||
return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS || errcode == EINTR || errcode == ETIMEDOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -13,40 +13,30 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#else
|
#else
|
||||||
#pragma warning(disable:4996)
|
#pragma warning(disable:4996)
|
||||||
|
#define snprintf _snprintf
|
||||||
#endif
|
#endif
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
//#include "sha1.h"
|
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
|
#include <../lib/abyss/src/session.h>
|
||||||
|
#include <../lib/abyss/src/conn.h>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
typedef TSession ws_tsession_t;
|
||||||
#define strncasecmp _strnicmp
|
typedef int issize_t;
|
||||||
#define snprintf _snprintf
|
|
||||||
#ifdef _WIN64
|
|
||||||
#define WS_SSIZE_T __int64
|
|
||||||
#elif _MSC_VER >= 1400
|
|
||||||
#define WS_SSIZE_T __int32 __w64
|
|
||||||
#else
|
|
||||||
#define WS_SSIZE_T __int32
|
|
||||||
#endif
|
|
||||||
typedef WS_SSIZE_T ssize_t;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
struct globals_s {
|
||||||
struct ws_globals_s {
|
|
||||||
const SSL_METHOD *ssl_method;
|
const SSL_METHOD *ssl_method;
|
||||||
SSL_CTX *ssl_ctx;
|
SSL_CTX *ssl_ctx;
|
||||||
char cert[512];
|
char cert[512];
|
||||||
char key[512];
|
char key[512];
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct ws_globals_s ws_globals;
|
// extern struct globals_s globals;
|
||||||
|
|
||||||
typedef int ws_socket_t;
|
typedef int ws_socket_t;
|
||||||
#define ws_sock_invalid -1
|
#define ws_sock_invalid -1
|
||||||
|
@ -69,44 +59,36 @@ typedef enum {
|
||||||
} ws_opcode_t;
|
} ws_opcode_t;
|
||||||
|
|
||||||
typedef struct wsh_s {
|
typedef struct wsh_s {
|
||||||
ws_socket_t sock;
|
ws_tsession_t *tsession;
|
||||||
char buffer[65536];
|
char buffer[65536];
|
||||||
char wbuffer[65536];
|
char wbuffer[65536];
|
||||||
size_t buflen;
|
size_t buflen;
|
||||||
ssize_t datalen;
|
issize_t datalen;
|
||||||
ssize_t wdatalen;
|
issize_t wdatalen;
|
||||||
char *payload;
|
char *payload;
|
||||||
ssize_t plen;
|
issize_t plen;
|
||||||
ssize_t rplen;
|
issize_t rplen;
|
||||||
SSL *ssl;
|
SSL *ssl;
|
||||||
int handshake;
|
int handshake;
|
||||||
uint8_t down;
|
uint8_t down;
|
||||||
int secure;
|
int secure;
|
||||||
uint8_t close_sock;
|
uint8_t close_sock;
|
||||||
SSL_CTX *ssl_ctx;
|
|
||||||
int block;
|
|
||||||
int sanity;
|
|
||||||
int secure_established;
|
|
||||||
int logical_established;
|
|
||||||
int x;
|
|
||||||
} wsh_t;
|
} wsh_t;
|
||||||
|
|
||||||
ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
|
issize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
|
||||||
ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes);
|
issize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes);
|
||||||
|
|
||||||
|
|
||||||
ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes);
|
issize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes);
|
||||||
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
|
issize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
|
||||||
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
|
issize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
|
||||||
ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
|
issize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
|
||||||
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block);
|
wsh_t * ws_init(ws_tsession_t *tsession);
|
||||||
ssize_t ws_close(wsh_t *wsh, int16_t reason);
|
issize_t ws_close(wsh_t *wsh, int16_t reason);
|
||||||
void ws_destroy(wsh_t *wsh);
|
void ws_destroy(wsh_t *wsh);
|
||||||
void init_ssl(void);
|
void init_ssl(void);
|
||||||
void deinit_ssl(void);
|
void deinit_ssl(void);
|
||||||
int xp_errno(void);
|
int ws_handshake_kvp(wsh_t *wsh, char *key, char *version, char *proto);
|
||||||
int xp_is_blocking(int errcode);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
|
|
Loading…
Reference in New Issue