From c28b3db5272cb5d1dcbd8a5d66f9cb1f799d782b Mon Sep 17 00:00:00 2001 From: mcskaf Date: Sun, 1 Dec 2002 19:10:26 +0000 Subject: [PATCH] Changed connect() success/fail checking in scanners for all OSs. --- Changes | 1 + src/modules/scan_http.c | 37 ++++++++++++++++------- src/modules/scan_socks.c | 64 ++++++++++++++++++++++++++++++---------- 3 files changed, 77 insertions(+), 25 deletions(-) diff --git a/Changes b/Changes index e972414d2..8f71f04f2 100644 --- a/Changes +++ b/Changes @@ -1710,3 +1710,4 @@ seen. gmtime warning still there - Updated docs some more - Fixed a makefile.win32.ssl problem reported by WeeD - Changed version to beta13, updated docs +- Check for connect() success/failure in scanners on all OSs. diff --git a/src/modules/scan_http.c b/src/modules/scan_http.c index f5a3feefe..7f67842c6 100644 --- a/src/modules/scan_http.c +++ b/src/modules/scan_http.c @@ -193,9 +193,9 @@ void scan_http_scan_port(HSStruct *z) struct SOCKADDR_IN bin; SOCKET fd; unsigned char httpbuf[160]; - fd_set rfds; + fd_set rfds, efds; + int err, len = sizeof(err); struct timeval tv; - int len; IRCMutexLock((h->lock)); #ifndef INET6 @@ -250,26 +250,43 @@ void scan_http_scan_port(HSStruct *z) * -Zogg */ set_non_blocking(fd, NULL); - if ((retval = connect(fd, (struct sockaddr *)&sin, - sizeof(sin))) == -1 && !(ERRNO == P_EINPROGRESS)) + if ((retval = connect(fd, (struct sockaddr *)&sin, sizeof(sin))) == -1 && (ERRNO != P_EWORKING)) { /* we have no socks server! */ CLOSE_SOCK(fd); goto exituniverse; - return; } - /* We wait for write-ready */ + /* We wait for connection to complete */ tv.tv_sec = *xScan_TimeOut; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(fd, &rfds); - if (!select(fd + 1, NULL, &rfds, NULL, &tv)) + FD_ZERO(&efds); + FD_SET(fd, &efds); + if (select(fd + 1, NULL, &rfds, &efds, &tv) <= 0) { + /* timeout or error */ CLOSE_SOCK(fd); goto exituniverse; } - + /* did connection fail on windows? */ + if (FD_ISSET(fd, &efds)) + { + err = ERRNO; + CLOSE_SOCK(fd); + goto exituniverse; + } +#ifdef SO_ERROR + /* did connection fail on unix? */ + if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, (OPT_TYPE *)&err, &len)) + if (err) + { + /* connection failed */ + CLOSE_SOCK(fd); + goto exituniverse; + } +#endif bzero(httpbuf, sizeof(httpbuf)); snprintf(httpbuf, sizeof httpbuf, "CONNECT %s:%i HTTP/1.1\n\n", Inet_ia2p(&xScan_endpoint->SIN_ADDR), ntohs(xScan_endpoint->SIN_PORT)); @@ -278,7 +295,7 @@ void scan_http_scan_port(HSStruct *z) CLOSE_SOCK(fd); goto exituniverse; } - /* Now we wait for data. 10 secs ought to be enough */ + /* Now we wait for data. Duration is set::scan::timeout */ tv.tv_sec = *xScan_TimeOut; tv.tv_usec = 0; FD_ZERO(&rfds); @@ -290,7 +307,7 @@ void scan_http_scan_port(HSStruct *z) CLOSE_SOCK(fd); if (len < 4) goto exituniverse; - if (!strncmp(httpbuf, "HTTP/1.0 200", 12)) + if (strncmp(httpbuf, "HTTP/1.0 200", 12)) { /* Gotcha */ IRCMutexLock((h->lock)); diff --git a/src/modules/scan_socks.c b/src/modules/scan_socks.c index 7731b1239..f52e31965 100644 --- a/src/modules/scan_socks.c +++ b/src/modules/scan_socks.c @@ -174,9 +174,9 @@ void scan_socks4_scan(Scan_AddrStruct *h) SOCKET fd; unsigned char socksbuf[10]; unsigned long theip; - fd_set rfds; + fd_set rfds, efds; struct timeval tv; - int len; + int err, len = sizeof(err); /* Get host */ IRCMutexLock((h->lock)); @@ -231,8 +231,7 @@ void scan_socks4_scan(Scan_AddrStruct *h) * -Zogg */ set_non_blocking(fd, NULL); - if ((retval = connect(fd, (struct sockaddr *)&sin, - sizeof(sin))) == -1 && !(ERRNO == P_EINPROGRESS)) + if ((retval = connect(fd, (struct sockaddr *)&sin, sizeof(sin))) == -1 && !(ERRNO == P_EWORKING)) { /* we have no socks server! */ CLOSE_SOCK(fd); @@ -240,16 +239,35 @@ void scan_socks4_scan(Scan_AddrStruct *h) return; } - /* We wait for write-ready */ + /* We wait for connection to complete */ tv.tv_sec = *xScan_TimeOut; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(fd, &rfds); - if (!select(fd + 1, NULL, &rfds, NULL, &tv)) + FD_ZERO(&efds); + FD_SET(fd, &efds); + if (select(fd + 1, NULL, &rfds, &efds, &tv) <= 0) { CLOSE_SOCK(fd); goto exituniverse; } + /* did connection fail on windows? */ + if (FD_ISSET(fd, &efds)) + { + err = ERRNO; + CLOSE_SOCK(fd); + goto exituniverse; + } +#ifdef SO_ERROR + /* did connection fail on unix? */ + if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, (OPT_TYPE *)&err, &len)) + if (err) + { + /* connection failed */ + CLOSE_SOCK(fd); + goto exituniverse; + } +#endif #ifdef INET6 ia4.s_addr = inet_aton((char *)Inet_ia2p(&xScan_endpoint->SIN_ADDR)); #else @@ -315,10 +333,10 @@ void scan_socks5_scan(Scan_AddrStruct *h) struct SOCKADDR_IN sin; struct in_addr ia4; SOCKET fd; - unsigned long theip; - fd_set rfds; + unsigned long theip; + fd_set rfds, efds; struct timeval tv; - int len; + int err, len = sizeof(err); unsigned char socksbuf[10]; /* Get host */ IRCMutexLock((h->lock)); @@ -366,26 +384,42 @@ void scan_socks5_scan(Scan_AddrStruct *h) * -Zogg */ set_non_blocking(fd, NULL); - if ((retval = connect(fd, (struct sockaddr *)&sin, - sizeof(sin))) == -1 && - !(ERRNO == P_EINPROGRESS)) + if ((retval = connect(fd, (struct sockaddr *)&sin, sizeof(sin))) == -1 && !(ERRNO == P_EWORKING)) { /* we have no socks server! */ CLOSE_SOCK(fd); goto exituniverse; - return; } - /* We wait for write-ready */ + /* We wait for connection to complete */ tv.tv_sec = *xScan_TimeOut; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(fd, &rfds); - if (!select(fd + 1, NULL, &rfds, NULL, &tv)) + FD_ZERO(&efds); + FD_SET(fd, &efds); + if (select(fd + 1, NULL, &rfds, &efds, &tv) <= 0) { CLOSE_SOCK(fd); goto exituniverse; } + /* did connection fail on windows? */ + if (FD_ISSET(fd, &efds)) + { + err = ERRNO; + CLOSE_SOCK(fd); + goto exituniverse; + } +#ifdef SO_ERROR + /* did connection fail on unix? */ + if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, (OPT_TYPE *)&err, &len)) + if (err) + { + /* connection failed */ + CLOSE_SOCK(fd); + goto exituniverse; + } +#endif #ifdef INET6 ia4.s_addr = inet_aton((char *)Inet_ia2p(&xScan_endpoint->SIN_ADDR)); #else