Skip to content

Commit

Permalink
websocket: handle CRLF received inside a frame (as SIP layer ping)
Browse files Browse the repository at this point in the history
- reported by André Freitas, GH #810

(cherry picked from commit 585c60f)
  • Loading branch information
miconda committed Oct 10, 2016
1 parent 28e622c commit a45e9f6
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions modules/websocket/ws_frame.c
Expand Up @@ -136,6 +136,8 @@ static str str_status_error_closing = str_init("Error closing connection");
static str str_status_error_sending = str_init("Error sending frame");
static str str_status_string_error = str_init("Error converting string to int");

static int ws_send_crlf(ws_connection_t *wsc, int opcode);

static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
{
int pos = 0, extended_length;
Expand Down Expand Up @@ -680,10 +682,17 @@ int ws_frame_receive(void *data)
case OPCODE_BINARY_FRAME:
if (likely(frame.wsc->sub_protocol == SUB_PROTOCOL_SIP))
{
LM_DBG("Rx SIP message:\n%.*s\n", frame.payload_len,
LM_DBG("Rx SIP (or text) message:\n%.*s\n", frame.payload_len,
frame.payload_data);
update_stat(ws_sip_received_frames, 1);

if(frame.payload_len==CRLF_LEN
&& strncmp(frame.payload_data, CRLF, CRLF_LEN)==0)
{
ws_send_crlf(frame.wsc, opcode);
wsconn_put(frame.wsc);
return 0;
}
if (frame.fin)
{

Expand Down Expand Up @@ -788,7 +797,7 @@ int ws_frame_transmit(void *data)
frame.payload_data);

if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0)
{
{
LM_ERR("sending message\n");

wsconn_put(frame.wsc);
Expand All @@ -813,7 +822,7 @@ static int ping_pong(ws_connection_t *wsc, int opcode)
frame.wsc = wsc;

if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0)
{
{
LM_ERR("sending keepalive\n");
return -1;
}
Expand All @@ -824,6 +833,25 @@ static int ping_pong(ws_connection_t *wsc, int opcode)
return 0;
}

static int ws_send_crlf(ws_connection_t *wsc, int opcode)
{
ws_frame_t frame;

memset(&frame, 0, sizeof(frame));
frame.fin = 1;
frame.opcode = opcode;
frame.payload_len = CRLF_LEN;
frame.payload_data = CRLF;
frame.wsc = wsc;

if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0) {
LM_ERR("failed sending CRLF\n");
return -1;
}

return 0;
}

struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
{
unsigned int id;
Expand Down

0 comments on commit a45e9f6

Please sign in to comment.