From 9e4c917c274f8049d2b13d52a3de7df629d28255 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sun, 10 Feb 2013 09:06:38 +0800 Subject: [PATCH] fix broken listen socket piggybacking As reported here http://libwebsockets.org/trac/ticket/11 the code for connection storm handling had rotted, fds[0] is no longer always related to the listen socket when serving. This patch updates it to determine the listen socket correctly to stop infinite recursion here. Reported-by: amn Signed-off-by: Andy Green --- lib/libwebsockets.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 55fcf00f..dd58ff12 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -777,6 +777,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, struct libwebsocket *wsi; int n; int m; + int listen_socket_fds_index = context->lws_lookup[context->listen_service_fd]->position_in_fds_table; struct timeval tv; #ifdef LWS_OPENSSL_SUPPORT char ssl_err_buf[512]; @@ -836,7 +837,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, * pending connection here, it causes us to check again next time. */ - if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) { + if (context->listen_service_fd && pollfd != &context->fds[listen_socket_fds_index]) { context->listen_service_count++; if (context->listen_service_extraseen || context->listen_service_count == context->listen_service_modulo) { @@ -846,9 +847,9 @@ libwebsocket_service_fd(struct libwebsocket_context *context, m = 2; while (m--) { /* even with extpoll, we prepared this internal fds for listen */ - n = poll(&context->fds[0], 1, 0); + n = poll(&context->fds[listen_socket_fds_index], 1, 0); if (n > 0) { /* there's a connection waiting for us */ - libwebsocket_service_fd(context, &context->fds[0]); + libwebsocket_service_fd(context, &context->fds[listen_socket_fds_index]); context->listen_service_extraseen++; } else { if (context->listen_service_extraseen)