From 2fd6e6fb55f2e1165578e4cc3be7c292b1a93b26 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Tue, 24 Mar 2015 21:07:01 +0800 Subject: [PATCH] ping allow zero length PING Part of https://github.com/warmcat/libwebsockets/issues/256 Signed-off-by: Andy Green --- lib/libwebsockets.c | 1 + lib/parsers.c | 3 ++- lib/private-libwebsockets.h | 3 ++- lib/service.c | 5 +++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 3f406c2..317a738 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -235,6 +235,7 @@ just_kill_connection: lws_free2(wsi->u.ws.ping_payload_buf); wsi->u.ws.ping_payload_alloc = 0; wsi->u.ws.ping_payload_len = 0; + wsi->u.ws.ping_pending_flag = 0; } } diff --git a/lib/parsers.c b/lib/parsers.c index 6a0fe09..2ee4e7b 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -876,7 +876,7 @@ spill: lwsl_info("received %d byte ping, sending pong\n", wsi->u.ws.rx_user_buffer_head); - if (wsi->u.ws.ping_payload_len) { + if (wsi->u.ws.ping_pending_flag) { /* * there is already a pending ping payload * we should just log and drop @@ -910,6 +910,7 @@ spill: wsi->u.ws.rx_user_buffer_head); wsi->u.ws.ping_payload_len = wsi->u.ws.rx_user_buffer_head; + wsi->u.ws.ping_pending_flag = 1; /* get it sent as soon as possible */ libwebsocket_callback_on_writable(wsi->protocol->owning_server, wsi); diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 5e0a6a3..5e73dff 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -794,7 +794,8 @@ struct _lws_websocket_related { unsigned char *ping_payload_buf; /* non-NULL if malloc'd */ unsigned int ping_payload_alloc; /* length malloc'd */ - unsigned int ping_payload_len; /* nonzero if PONG pending */ + unsigned int ping_payload_len; + unsigned char ping_pending_flag; }; struct libwebsocket { diff --git a/lib/service.c b/lib/service.c index bf968b7..a94e679 100644 --- a/lib/service.c +++ b/lib/service.c @@ -92,7 +92,8 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context, #endif /* pending control packets have next priority */ - if (wsi->state == WSI_STATE_ESTABLISHED && wsi->u.ws.ping_payload_len) { + if (wsi->state == WSI_STATE_ESTABLISHED && + wsi->u.ws.ping_pending_flag) { n = libwebsocket_write(wsi, &wsi->u.ws.ping_payload_buf[ LWS_SEND_BUFFER_PRE_PADDING], @@ -101,7 +102,7 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context, if (n < 0) return -1; /* well he is sent, mark him done */ - wsi->u.ws.ping_payload_len = 0; + wsi->u.ws.ping_pending_flag = 0; /* leave POLLOUT active either way */ return 0; } -- 2.7.4