mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-06-27 03:37:50 +00:00
FS-10150: [freeswitch-core] Reduce writes to closed ssl sockets #resolve
Conflicts: libs/libks/src/kws.c libs/sofia-sip/.update
This commit is contained in:
parent
46b518d57a
commit
9844d1887b
@ -1 +1 @@
|
|||||||
Thu Feb 9 17:36:33 CST 2017
|
Mon Mar 20 17:03:26 CDT 2017
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
#define ms_sleep(x) usleep( x * 1000);
|
#define ms_sleep(x) usleep( x * 1000);
|
||||||
#else
|
#else
|
||||||
#define ms_sleep(x) Sleep( x );
|
#define ms_sleep(x) Sleep( x );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
/* warning C4706: assignment within conditional expression*/
|
/* warning C4706: assignment within conditional expression*/
|
||||||
@ -29,11 +29,11 @@ static struct ws_globals_s ws_globals;
|
|||||||
|
|
||||||
#ifndef WSS_STANDALONE
|
#ifndef WSS_STANDALONE
|
||||||
|
|
||||||
void init_ssl(void)
|
void init_ssl(void)
|
||||||
{
|
{
|
||||||
SSL_library_init();
|
SSL_library_init();
|
||||||
}
|
}
|
||||||
void deinit_ssl(void)
|
void deinit_ssl(void)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -107,13 +107,13 @@ void init_ssl(void) {
|
|||||||
assert(ws_globals.ssl_ctx);
|
assert(ws_globals.ssl_ctx);
|
||||||
|
|
||||||
/* Disable SSLv2 */
|
/* Disable SSLv2 */
|
||||||
SSL_CTX_set_options(globals.ssl_ctx, SSL_OP_NO_SSLv2);
|
SSL_CTX_set_options(ws_globals.ssl_ctx, SSL_OP_NO_SSLv2);
|
||||||
/* Disable SSLv3 */
|
/* Disable SSLv3 */
|
||||||
SSL_CTX_set_options(globals.ssl_ctx, SSL_OP_NO_SSLv3);
|
SSL_CTX_set_options(ws_globals.ssl_ctx, SSL_OP_NO_SSLv3);
|
||||||
/* Disable TLSv1 */
|
/* Disable TLSv1 */
|
||||||
SSL_CTX_set_options(globals.ssl_ctx, SSL_OP_NO_TLSv1);
|
SSL_CTX_set_options(ws_globals.ssl_ctx, SSL_OP_NO_TLSv1);
|
||||||
/* Disable Compression CRIME (Compression Ratio Info-leak Made Easy) */
|
/* Disable Compression CRIME (Compression Ratio Info-leak Made Easy) */
|
||||||
SSL_CTX_set_options(globals.ssl_ctx, SSL_OP_NO_COMPRESSION);
|
SSL_CTX_set_options(ws_globals.ssl_ctx, SSL_OP_NO_COMPRESSION);
|
||||||
/* 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(ws_globals.ssl_ctx, ws_globals.cert, SSL_FILETYPE_PEM);
|
||||||
/* set the private key from KeyFile */
|
/* set the private key from KeyFile */
|
||||||
@ -166,28 +166,28 @@ static int cheezy_get_var(char *data, char *name, char *buf, size_t buflen)
|
|||||||
e = strchr(v, '\n');
|
e = strchr(v, '\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (v && e) {
|
if (v && e) {
|
||||||
int cplen;
|
int cplen;
|
||||||
size_t len = e - v;
|
size_t len = e - v;
|
||||||
|
|
||||||
if (len > buflen - 1) {
|
if (len > buflen - 1) {
|
||||||
cplen = buflen -1;
|
cplen = buflen -1;
|
||||||
} else {
|
} else {
|
||||||
cplen = len;
|
cplen = len;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(buf, v, cplen);
|
strncpy(buf, v, cplen);
|
||||||
*(buf+cplen) = '\0';
|
*(buf+cplen) = '\0';
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int b64encode(unsigned char *in, size_t ilen, unsigned char *out, size_t olen)
|
static int b64encode(unsigned char *in, size_t ilen, unsigned char *out, size_t olen)
|
||||||
{
|
{
|
||||||
int y=0,bytes=0;
|
int y=0,bytes=0;
|
||||||
size_t x=0;
|
size_t x=0;
|
||||||
@ -231,7 +231,7 @@ static void sha1_digest(char *digest, unsigned char *in)
|
|||||||
SHA1Update(&sha, in, strlen(in));
|
SHA1Update(&sha, in, strlen(in));
|
||||||
SHA1Final(&sha, digest);
|
SHA1Final(&sha, digest);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
||||||
static void sha1_digest(unsigned char *digest, char *in)
|
static void sha1_digest(unsigned char *digest, char *in)
|
||||||
{
|
{
|
||||||
@ -269,18 +269,18 @@ int ws_handshake(wsh_t *wsh)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bytes > wsh->buflen -1) {
|
if (bytes < 0 || bytes > wsh->buflen -1) {
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
*(wsh->buffer + wsh->datalen) = '\0';
|
*(wsh->buffer + wsh->datalen) = '\0';
|
||||||
|
|
||||||
if (strncasecmp(wsh->buffer, "GET ", 4)) {
|
if (strncasecmp(wsh->buffer, "GET ", 4)) {
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = wsh->buffer + 4;
|
p = wsh->buffer + 4;
|
||||||
|
|
||||||
e = strchr(p, ' ');
|
e = strchr(p, ' ');
|
||||||
if (!e) {
|
if (!e) {
|
||||||
goto err;
|
goto err;
|
||||||
@ -293,11 +293,11 @@ int ws_handshake(wsh_t *wsh)
|
|||||||
cheezy_get_var(wsh->buffer, "Sec-WebSocket-Key", key, sizeof(key));
|
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-Version", version, sizeof(version));
|
||||||
cheezy_get_var(wsh->buffer, "Sec-WebSocket-Protocol", proto, sizeof(proto));
|
cheezy_get_var(wsh->buffer, "Sec-WebSocket-Protocol", proto, sizeof(proto));
|
||||||
|
|
||||||
if (!*key) {
|
if (!*key) {
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(input, sizeof(input), "%s%s", key, WEBSOCKET_GUID);
|
snprintf(input, sizeof(input), "%s%s", key, WEBSOCKET_GUID);
|
||||||
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));
|
||||||
@ -306,7 +306,7 @@ int ws_handshake(wsh_t *wsh)
|
|||||||
snprintf(proto_buf, sizeof(proto_buf), "Sec-WebSocket-Protocol: %s\r\n", 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"
|
||||||
@ -328,11 +328,13 @@ int ws_handshake(wsh_t *wsh)
|
|||||||
|
|
||||||
if (!wsh->stay_open) {
|
if (!wsh->stay_open) {
|
||||||
|
|
||||||
snprintf(respond, sizeof(respond), "HTTP/1.1 400 Bad Request\r\n"
|
if (bytes > 0) {
|
||||||
"Sec-WebSocket-Version: 13\r\n\r\n");
|
snprintf(respond, sizeof(respond), "HTTP/1.1 400 Bad Request\r\n"
|
||||||
respond[511] = 0;
|
"Sec-WebSocket-Version: 13\r\n\r\n");
|
||||||
|
respond[511] = 0;
|
||||||
|
|
||||||
ws_raw_write(wsh, respond, strlen(respond));
|
ws_raw_write(wsh, respond, strlen(respond));
|
||||||
|
}
|
||||||
|
|
||||||
ws_close(wsh, WS_NONE);
|
ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
@ -355,7 +357,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block)
|
|||||||
|
|
||||||
if (r == -1) {
|
if (r == -1) {
|
||||||
err = SSL_get_error(wsh->ssl, r);
|
err = SSL_get_error(wsh->ssl, r);
|
||||||
|
|
||||||
if (err == SSL_ERROR_WANT_READ) {
|
if (err == SSL_ERROR_WANT_READ) {
|
||||||
if (!block) {
|
if (!block) {
|
||||||
r = -2;
|
r = -2;
|
||||||
@ -390,7 +392,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (r == -1 && xp_is_blocking(xp_errno()) && wsh->x < 1000);
|
} while (r == -1 && xp_is_blocking(xp_errno()) && wsh->x < 1000);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
|
||||||
if (wsh->x >= 10000 || (block && wsh->x >= 1000)) {
|
if (wsh->x >= 10000 || (block && wsh->x >= 1000)) {
|
||||||
@ -404,7 +406,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block)
|
|||||||
if (r >= 0) {
|
if (r >= 0) {
|
||||||
wsh->x = 0;
|
wsh->x = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -436,13 +438,13 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
|
|||||||
if (ssl_err) {
|
if (ssl_err) {
|
||||||
r = ssl_err * -1;
|
r = ssl_err * -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
r = send(wsh->sock, (void *)((unsigned char *)data + wrote), bytes - wrote, 0);
|
r = send(wsh->sock, (void *)((unsigned char *)data + wrote), bytes - wrote, 0);
|
||||||
|
|
||||||
if (r > 0) {
|
if (r > 0) {
|
||||||
wrote += r;
|
wrote += r;
|
||||||
}
|
}
|
||||||
@ -450,9 +452,9 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
|
|||||||
if (sanity < 2000) {
|
if (sanity < 2000) {
|
||||||
ms_sleep(1);
|
ms_sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes)));
|
} while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes)));
|
||||||
|
|
||||||
//if (r<0) {
|
//if (r<0) {
|
||||||
//printf("wRITE FAIL: %s\n", strerror(errno));
|
//printf("wRITE FAIL: %s\n", strerror(errno));
|
||||||
//}
|
//}
|
||||||
@ -538,7 +540,7 @@ int establish_logical_layer(wsh_t *wsh)
|
|||||||
if (code == 0) {
|
if (code == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (code < 0) {
|
if (code < 0) {
|
||||||
if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
|
if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -558,11 +560,11 @@ int establish_logical_layer(wsh_t *wsh)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} while (wsh->sanity > 0);
|
} while (wsh->sanity > 0);
|
||||||
|
|
||||||
if (!wsh->sanity) {
|
if (!wsh->sanity) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!wsh->down && !wsh->handshake) {
|
while (!wsh->down && !wsh->handshake) {
|
||||||
@ -580,7 +582,7 @@ int establish_logical_layer(wsh_t *wsh)
|
|||||||
}
|
}
|
||||||
|
|
||||||
wsh->logical_established = 1;
|
wsh->logical_established = 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -641,7 +643,7 @@ void ws_destroy(wsh_t *wsh)
|
|||||||
if (wsh->down > 1) {
|
if (wsh->down > 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
wsh->down = 2;
|
wsh->down = 2;
|
||||||
|
|
||||||
if (wsh->write_buffer) {
|
if (wsh->write_buffer) {
|
||||||
@ -667,15 +669,15 @@ void ws_destroy(wsh_t *wsh)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t ws_close(wsh_t *wsh, int16_t reason)
|
ssize_t ws_close(wsh_t *wsh, int16_t reason)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (wsh->down) {
|
if (wsh->down) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
wsh->down = 1;
|
wsh->down = 1;
|
||||||
|
|
||||||
if (wsh->uri) {
|
if (wsh->uri) {
|
||||||
free(wsh->uri);
|
free(wsh->uri);
|
||||||
wsh->uri = NULL;
|
wsh->uri = NULL;
|
||||||
@ -703,7 +705,7 @@ ssize_t ws_close(wsh_t *wsh, int16_t reason)
|
|||||||
wsh->sock = ws_sock_invalid;
|
wsh->sock = ws_sock_invalid;
|
||||||
|
|
||||||
return reason * -1;
|
return reason * -1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -722,7 +724,7 @@ uint64_t ntoh64(uint64_t val)
|
|||||||
|
|
||||||
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
||||||
{
|
{
|
||||||
|
|
||||||
ssize_t need = 2;
|
ssize_t need = 2;
|
||||||
char *maskp;
|
char *maskp;
|
||||||
int ll = 0;
|
int ll = 0;
|
||||||
@ -748,20 +750,22 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!wsh->handshake) {
|
if (!wsh->handshake) {
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9, wsh->block)) < 0) {
|
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9, wsh->block)) < 0) {
|
||||||
if (wsh->datalen == -2) {
|
if (wsh->datalen == -2) {
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wsh->datalen < need) {
|
if (wsh->datalen < need) {
|
||||||
if ((wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen, WS_BLOCK)) < need) {
|
ssize_t bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen, WS_BLOCK);
|
||||||
|
|
||||||
|
if (bytes < 0 || (wsh->datalen += bytes) < need) {
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -783,7 +787,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
{
|
{
|
||||||
int fin = (wsh->buffer[0] >> 7) & 1;
|
int fin = (wsh->buffer[0] >> 7) & 1;
|
||||||
int mask = (wsh->buffer[1] >> 7) & 1;
|
int mask = (wsh->buffer[1] >> 7) & 1;
|
||||||
|
|
||||||
|
|
||||||
if (!fin && *oc != WSOC_CONTINUATION) {
|
if (!fin && *oc != WSOC_CONTINUATION) {
|
||||||
frag = 1;
|
frag = 1;
|
||||||
@ -793,17 +797,17 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
|
|
||||||
if (mask) {
|
if (mask) {
|
||||||
need += 4;
|
need += 4;
|
||||||
|
|
||||||
if (need > wsh->datalen) {
|
if (need > wsh->datalen) {
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wsh->plen = wsh->buffer[1] & 0x7f;
|
wsh->plen = wsh->buffer[1] & 0x7f;
|
||||||
wsh->payload = &wsh->buffer[2];
|
wsh->payload = &wsh->buffer[2];
|
||||||
|
|
||||||
if (wsh->plen == 127) {
|
if (wsh->plen == 127) {
|
||||||
uint64_t *u64;
|
uint64_t *u64;
|
||||||
int more = 0;
|
int more = 0;
|
||||||
@ -817,16 +821,16 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
|
|
||||||
more = ws_raw_read(wsh, wsh->buffer + wsh->datalen, need - wsh->datalen, WS_BLOCK);
|
more = ws_raw_read(wsh, wsh->buffer + wsh->datalen, need - wsh->datalen, WS_BLOCK);
|
||||||
|
|
||||||
if (more < need - wsh->datalen) {
|
if (more < 0 || more < need - wsh->datalen) {
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
} else {
|
} else {
|
||||||
wsh->datalen += more;
|
wsh->datalen += more;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 = (uint64_t *) wsh->payload;
|
u64 = (uint64_t *) wsh->payload;
|
||||||
wsh->payload += 8;
|
wsh->payload += 8;
|
||||||
wsh->plen = ntoh64(*u64);
|
wsh->plen = ntoh64(*u64);
|
||||||
@ -838,7 +842,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
if (need > wsh->datalen) {
|
if (need > wsh->datalen) {
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
u16 = (uint16_t *) wsh->payload;
|
u16 = (uint16_t *) wsh->payload;
|
||||||
@ -856,14 +860,14 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
if (need < 0) {
|
if (need < 0) {
|
||||||
/* invalid read - protocol err .. */
|
/* invalid read - protocol err .. */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
blen = wsh->body - wsh->bbuffer;
|
blen = wsh->body - wsh->bbuffer;
|
||||||
|
|
||||||
if (need + blen > (ssize_t)wsh->bbuflen) {
|
if (need + blen > (ssize_t)wsh->bbuflen) {
|
||||||
void *tmp;
|
void *tmp;
|
||||||
|
|
||||||
wsh->bbuflen = need + blen + wsh->rplen;
|
wsh->bbuflen = need + blen + wsh->rplen;
|
||||||
|
|
||||||
if ((tmp = realloc(wsh->bbuffer, wsh->bbuflen))) {
|
if ((tmp = realloc(wsh->bbuffer, wsh->bbuflen))) {
|
||||||
@ -876,25 +880,25 @@ 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;
|
||||||
|
|
||||||
if (wsh->rplen) {
|
if (wsh->rplen) {
|
||||||
memcpy(wsh->body, wsh->payload, wsh->rplen);
|
memcpy(wsh->body, wsh->payload, wsh->rplen);
|
||||||
}
|
}
|
||||||
|
|
||||||
while(need) {
|
while(need) {
|
||||||
ssize_t r = ws_raw_read(wsh, wsh->body + wsh->rplen, need, WS_BLOCK);
|
ssize_t r = ws_raw_read(wsh, wsh->body + wsh->rplen, need, WS_BLOCK);
|
||||||
|
|
||||||
if (r < 1) {
|
if (r < 1) {
|
||||||
/* invalid read - protocol err .. */
|
/* invalid read - protocol err .. */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
wsh->datalen += r;
|
wsh->datalen += r;
|
||||||
wsh->rplen += r;
|
wsh->rplen += r;
|
||||||
need -= r;
|
need -= r;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mask && maskp) {
|
if (mask && maskp) {
|
||||||
ssize_t i;
|
ssize_t i;
|
||||||
|
|
||||||
@ -902,7 +906,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
wsh->body[i] ^= maskp[i % 4];
|
wsh->body[i] ^= maskp[i % 4];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (*oc == WSOC_PING) {
|
if (*oc == WSOC_PING) {
|
||||||
ws_write_frame(wsh, WSOC_PONG, wsh->body, wsh->rplen);
|
ws_write_frame(wsh, WSOC_PONG, wsh->body, wsh->rplen);
|
||||||
@ -918,7 +922,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
*data = (uint8_t *)wsh->bbuffer;
|
*data = (uint8_t *)wsh->bbuffer;
|
||||||
|
|
||||||
//printf("READ[%ld][%d]-----------------------------:\n[%s]\n-------------------------------\n", wsh->packetlen, *oc, (char *)*data);
|
//printf("READ[%ld][%d]-----------------------------:\n[%s]\n-------------------------------\n", wsh->packetlen, *oc, (char *)*data);
|
||||||
|
|
||||||
|
|
||||||
@ -966,7 +970,7 @@ ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes)
|
|||||||
|
|
||||||
hdr[1] = 127;
|
hdr[1] = 127;
|
||||||
hlen += 8;
|
hlen += 8;
|
||||||
|
|
||||||
u64 = (uint64_t *) &hdr[2];
|
u64 = (uint64_t *) &hdr[2];
|
||||||
*u64 = hton64(bytes);
|
*u64 = hton64(bytes);
|
||||||
}
|
}
|
||||||
@ -981,17 +985,17 @@ ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes)
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bp = (uint8_t *) wsh->write_buffer;
|
bp = (uint8_t *) wsh->write_buffer;
|
||||||
memcpy(bp, (void *) &hdr[0], hlen);
|
memcpy(bp, (void *) &hdr[0], hlen);
|
||||||
memcpy(bp + hlen, data, bytes);
|
memcpy(bp + hlen, data, bytes);
|
||||||
|
|
||||||
raw_ret = ws_raw_write(wsh, bp, (hlen + bytes));
|
raw_ret = ws_raw_write(wsh, bp, (hlen + bytes));
|
||||||
|
|
||||||
if (raw_ret != (ssize_t) (hlen + bytes)) {
|
if (raw_ret != (ssize_t) (hlen + bytes)) {
|
||||||
return raw_ret;
|
return raw_ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,7 +269,7 @@ int ws_handshake(wsh_t *wsh)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bytes > wsh->buflen -1) {
|
if (bytes < 0 || bytes > wsh->buflen -1) {
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,11 +328,13 @@ int ws_handshake(wsh_t *wsh)
|
|||||||
|
|
||||||
if (!wsh->stay_open) {
|
if (!wsh->stay_open) {
|
||||||
|
|
||||||
snprintf(respond, sizeof(respond), "HTTP/1.1 400 Bad Request\r\n"
|
if (bytes > 0) {
|
||||||
"Sec-WebSocket-Version: 13\r\n\r\n");
|
snprintf(respond, sizeof(respond), "HTTP/1.1 400 Bad Request\r\n"
|
||||||
respond[511] = 0;
|
"Sec-WebSocket-Version: 13\r\n\r\n");
|
||||||
|
respond[511] = 0;
|
||||||
|
|
||||||
ws_raw_write(wsh, respond, strlen(respond));
|
ws_raw_write(wsh, respond, strlen(respond));
|
||||||
|
}
|
||||||
|
|
||||||
ws_close(wsh, WS_NONE);
|
ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
@ -748,20 +750,22 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!wsh->handshake) {
|
if (!wsh->handshake) {
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9, wsh->block)) < 0) {
|
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9, wsh->block)) < 0) {
|
||||||
if (wsh->datalen == -2) {
|
if (wsh->datalen == -2) {
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wsh->datalen < need) {
|
if (wsh->datalen < need) {
|
||||||
if ((wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen, WS_BLOCK)) < need) {
|
ssize_t bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen, WS_BLOCK);
|
||||||
|
|
||||||
|
if (bytes < 0 || (wsh->datalen += bytes) < need) {
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -797,7 +801,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
if (need > wsh->datalen) {
|
if (need > wsh->datalen) {
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -817,9 +821,9 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
|
|
||||||
more = ws_raw_read(wsh, wsh->buffer + wsh->datalen, need - wsh->datalen, WS_BLOCK);
|
more = ws_raw_read(wsh, wsh->buffer + wsh->datalen, need - wsh->datalen, WS_BLOCK);
|
||||||
|
|
||||||
if (more < need - wsh->datalen) {
|
if (more < 0 || more < need - wsh->datalen) {
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
} else {
|
} else {
|
||||||
wsh->datalen += more;
|
wsh->datalen += more;
|
||||||
}
|
}
|
||||||
@ -838,7 +842,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
if (need > wsh->datalen) {
|
if (need > wsh->datalen) {
|
||||||
/* too small - protocol err */
|
/* too small - protocol err */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
u16 = (uint16_t *) wsh->payload;
|
u16 = (uint16_t *) wsh->payload;
|
||||||
@ -856,7 +860,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
if (need < 0) {
|
if (need < 0) {
|
||||||
/* invalid read - protocol err .. */
|
/* invalid read - protocol err .. */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
blen = wsh->body - wsh->bbuffer;
|
blen = wsh->body - wsh->bbuffer;
|
||||||
@ -887,7 +891,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
if (r < 1) {
|
if (r < 1) {
|
||||||
/* invalid read - protocol err .. */
|
/* invalid read - protocol err .. */
|
||||||
*oc = WSOC_CLOSE;
|
*oc = WSOC_CLOSE;
|
||||||
return ws_close(wsh, WS_PROTO_ERR);
|
return ws_close(wsh, WS_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
wsh->datalen += r;
|
wsh->datalen += r;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user