From 46581f2bfb6b306800538abc72ffed12b256dcf1 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Wed, 9 Dec 2015 11:45:31 +0100 Subject: [PATCH] Windows: Fix possible crash on connect. Add error message on failed server connect (not perfect yet). Reported by Robben (#4485). --- src/s_dispatch.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/s_dispatch.c b/src/s_dispatch.c index 284a7a508..3b007bd5f 100644 --- a/src/s_dispatch.c +++ b/src/s_dispatch.c @@ -123,7 +123,7 @@ void fd_setselect(int fd, int flags, IOCallbackFunc iocb, void *data) #endif static int highest_fd = -1; -static fd_set read_fds, write_fds, except_fds; +static fd_set read_fds, write_fds; void fd_refresh(int fd) { @@ -177,11 +177,18 @@ void fd_select(time_t delay) { struct timeval to; int num, fd; - fd_set work_read_fds, work_write_fds, work_except_fds; + fd_set work_read_fds; + fd_set work_write_fds; +#ifdef _WIN32 + fd_set work_except_fds; /* only needed on windows as it may indicate a failed connect() */ +#endif - /* optimization: copy the FD sets so that our master sets are untouched */ + /* copy the FD sets so that our master sets are untouched */ memcpy(&work_read_fds, &read_fds, sizeof(fd_set)); memcpy(&work_write_fds, &write_fds, sizeof(fd_set)); +#ifdef _WIN32 + memcpy(&work_except_fds, &write_fds, sizeof(fd_set)); +#endif to.tv_sec = delay / 1000; to.tv_usec = (delay % 1000) * 1000; @@ -190,7 +197,11 @@ void fd_select(time_t delay) ircd_log(LOG_ERROR, "fd_select() on 0-%d...", highest_fd+1); #endif +#ifdef _WIN32 + num = select(highest_fd + 1, &work_read_fds, &work_write_fds, &work_except_fds, &to); +#else num = select(highest_fd + 1, &work_read_fds, &work_write_fds, NULL, &to); +#endif if (num < 0) { extern void report_baderror(char *text, aClient *cptr); @@ -199,7 +210,10 @@ void fd_select(time_t delay) memcpy(&work_read_fds, &read_fds, sizeof(fd_set)); memcpy(&work_write_fds, &write_fds, sizeof(fd_set)); fd_debug(&work_read_fds, highest_fd+1, "read"); - fd_debug(&work_read_fds, highest_fd+1, "write"); + fd_debug(&work_write_fds, highest_fd+1, "write"); +#ifdef _WIN32 + Sleep(500); +#endif } if (num <= 0) @@ -225,9 +239,11 @@ void fd_select(time_t delay) if (FD_ISSET(fd, &work_write_fds)) evflags |= FD_SELECT_WRITE; - /* if exception happens, just dispatch to something so we bail on the FD */ +#ifdef _WIN32 + /* Exception may happen due to failed connect. Translate to write event, like on *NIX. */ if (FD_ISSET(fd, &work_except_fds)) - evflags |= (FD_SELECT_READ | FD_SELECT_WRITE); + evflags |= FD_SELECT_WRITE; +#endif if (!evflags) continue;