From 0303db482e6b6390c6c5686368baeee2fcf70910 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Thu, 17 Jan 2013 14:46:43 +0800 Subject: [PATCH] check errors on shutdown close Also make sure CLOSE doesn't go through extension munging Reduce wait for close ack to 1s Signed-off-by: Andy Green --- lib/libwebsockets.c | 23 ++++++++++++++++------- lib/output.c | 27 ++++++++++++++++++--------- lib/parsers.c | 6 +++--- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 6b2f2db..add3bd7 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -262,8 +262,10 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, if (eff_buf.token_len) if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, - eff_buf.token_len)) + eff_buf.token_len)) { + lwsl_debug("close: sending final extension spill had problems\n"); goto just_kill_connection; + } } /* @@ -293,16 +295,18 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, wsi->state = WSI_STATE_AWAITING_CLOSE_ACK; - /* and we should wait for a reply for a bit */ + /* and we should wait for a reply for a bit out of politeness */ libwebsocket_set_timeout(wsi, - PENDING_TIMEOUT_CLOSE_ACK, AWAITING_TIMEOUT); + PENDING_TIMEOUT_CLOSE_ACK, 1); lwsl_debug("sent close indication, awaiting ack\n"); return; } + lwsl_info("close: sending the close packet failed, hanging up\n"); + /* else, the send failed and we should just hang up */ } @@ -377,10 +381,15 @@ just_kill_connection: SSL_free(wsi->ssl); } else { #endif - shutdown(wsi->sock, SHUT_RDWR); - - if (wsi->sock) - compatible_close(wsi->sock); + if (wsi->sock) { + n = shutdown(wsi->sock, SHUT_RDWR); + if (n) + lwsl_debug("closing: shutdown returned %d\n", errno); + + n = compatible_close(wsi->sock); + if (n) + lwsl_debug("closing: close returned %d\n", errno); + } #ifdef LWS_OPENSSL_SUPPORT } #endif diff --git a/lib/output.c b/lib/output.c index 671e78c..68fd312 100644 --- a/lib/output.c +++ b/lib/output.c @@ -147,7 +147,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) #if 0 lwsl_debug(" TX: "); - lws_stderr_hexdump(buf, len); + lws_hexdump(buf, len); #endif #ifdef LWS_OPENSSL_SUPPORT @@ -160,8 +160,8 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) } else { #endif n = send(wsi->sock, buf, len, MSG_NOSIGNAL); - if (n < 0) { - lwsl_debug("ERROR writing to socket\n"); + if (n != len) { + lwsl_debug("ERROR writing len %d to socket %d\n", len, n); return -1; } #ifdef LWS_OPENSSL_SUPPORT @@ -312,7 +312,12 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, eff_buf.token = (char *)buf; eff_buf.token_len = len; - if (protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) { + switch (protocol) { + case LWS_WRITE_PING: + case LWS_WRITE_PONG: + case LWS_WRITE_CLOSE: + break; + default: for (n = 0; n < wsi->count_active_extensions; n++) { m = wsi->active_extensions[n]->callback( @@ -583,18 +588,22 @@ send_raw: #if 0 lwsl_debug("send %ld: ", len + post); - for (n = -pre; n < ((int)len + post); n++) - lwsl_debug("%02X ", buf[n]); - - lwsl_debug("\n"); + lwsl_hexdump(&buf[-pre], len + post); #endif - if (protocol == LWS_WRITE_HTTP || protocol == LWS_WRITE_PONG || protocol == LWS_WRITE_PING) { + switch (protocol) { + case LWS_WRITE_CLOSE: +// lwsl_hexdump(&buf[-pre], len + post); + case LWS_WRITE_HTTP: + case LWS_WRITE_PONG: + case LWS_WRITE_PING: if (lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre + post)) return -1; return 0; + default: + break; } /* diff --git a/lib/parsers.c b/lib/parsers.c index 6ef26dd..6d87907 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -774,6 +774,8 @@ spill: n = libwebsocket_write(wsi, (unsigned char *) &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head, LWS_WRITE_CLOSE); + if (n) + lwsl_info("write of close ack failed %d\n", n); wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY; /* close the connection */ return -1; @@ -896,9 +898,7 @@ int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, #ifdef DEBUG lwsl_parser("received %d byte packet\n", (int)len); - for (n = 0; n < len; n++) - lwsl_parser("%02X ", buf[n]); - lwsl_parser("\n"); + lwsl_hexdump(buf, len); #endif /* let the rx protocol state machine have as much as it needs */ -- 2.7.4