Skip to content

Commit

Permalink
Merge pull request #195 from freeswitch/ws_payload_size
Browse files Browse the repository at this point in the history
Add ws_set_global_payload_size_max() API.
  • Loading branch information
andywolk committed Jan 17, 2023
2 parents 4398da8 + 544989b commit 983e35b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libsofia-sip-ua/tport/ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#define SHA1_HASH_SIZE 20
static struct ws_globals_s ws_globals;
ssize_t ws_global_payload_size_max = 0;

#ifndef WSS_STANDALONE

Expand Down Expand Up @@ -712,11 +713,16 @@ int establish_logical_layer(wsh_t *wsh)
return 0;
}

void ws_set_global_payload_size_max(ssize_t bytes)
{
ws_global_payload_size_max = bytes;
}

int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block, int stay_open)
{
memset(wsh, 0, sizeof(*wsh));

wsh->payload_size_max = ws_global_payload_size_max;
wsh->sock = sock;
wsh->block = block;
wsh->sanity = WS_INIT_SANITY;
Expand Down Expand Up @@ -1007,6 +1013,12 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)

wsh->bbuflen = need + blen + wsh->rplen;

if (wsh->payload_size_max && wsh->bbuflen > wsh->payload_size_max) {
/* size limit */
*oc = WSOC_CLOSE;
return ws_close(wsh, WS_NONE);
}

if ((tmp = realloc(wsh->bbuffer, wsh->bbuflen))) {
wsh->bbuffer = tmp;
} else {
Expand Down
3 changes: 3 additions & 0 deletions libsofia-sip-ua/tport/ws.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ typedef struct wsh_s {
int x;
void *write_buffer;
size_t write_buffer_len;

ssize_t payload_size_max;
} wsh_t;

ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
Expand All @@ -127,6 +129,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block);
ssize_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);
ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
void ws_set_global_payload_size_max(ssize_t bytes);
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block, int stay_open);
ssize_t ws_close(wsh_t *wsh, int16_t reason);
void ws_destroy(wsh_t *wsh);
Expand Down

0 comments on commit 983e35b

Please sign in to comment.