From 7685c9d78c4abd88c5d389f38c63c6e3903af221 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 12 Jan 2019 21:36:36 +0000
Subject: [PATCH] Bug-fix: Socket.connect() would return true (success) even
 though a TCP connection actually failed. This bug only appeared to affect
 *nix systems. This bug appears to be very old, introduced in rev 1.74 of this
 file (Mar-2003) by yours truly. From the Linux 'connect' man page:
 EINPROGRESS               The  socket  is  nonblocking  and the connection
 cannot be i               completed immediately.  It is possible to select(2)
 or poll(2)               for completion by selecting the socket for writing. 
 After               select(2) indicates writability, use getsockopt(2) to
 read the               SO_ERROR option at level SOL_SOCKET to determine
 whether               connect() completed successfully (SO_ERROR is zero) or 
              unsuccessfully (SO_ERROR is one of the usual error codes listed 
              here, explaining the reason for the failure).

We weren't doing the 'getsockopt(SO_ERROR)' part.
---
 src/sbbs3/js_socket.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/sbbs3/js_socket.c b/src/sbbs3/js_socket.c
index c14256699c..908c535dea 100644
--- a/src/sbbs3/js_socket.c
+++ b/src/sbbs3/js_socket.c
@@ -1,4 +1,5 @@
 /* Synchronet JavaScript "Socket" Object */
+// vi: tabstop=4
 
 /* $Id$ */
 
@@ -789,8 +790,12 @@ js_connect(JSContext *cx, uintN argc, jsval *arglist)
 				&& (ERROR_VALUE==EWOULDBLOCK || ERROR_VALUE==EINPROGRESS)) {
 			FD_ZERO(&socket_set);
 			FD_SET(p->sock,&socket_set);
-			if(select(p->sock+1,NULL,&socket_set,NULL,&tv)==1)
-				result=0;	/* success */
+			if(select(p->sock+1,NULL,&socket_set,NULL,&tv)==1) {
+				int so_error = -1;
+				socklen_t optlen = sizeof(so_error);
+				if(getsockopt(p->sock, SOL_SOCKET, SO_ERROR, (void*)&so_error, &optlen) == 0 && so_error == 0)
+					result=0;	/* success */
+			}
 		}
 		if(result==0)
 			break;
-- 
GitLab