diff --git a/src/compat.h b/src/compat.h --- a/src/compat.h +++ b/src/compat.h @@ -86,7 +86,7 @@ size_t strnlen(const char *start, size_t max_len); #endif // HAVE_DECL_STRNLEN -static bool inline IsSelectableSocket(SOCKET s) { +static bool inline IsSelectableSocket(const SOCKET &s) { #ifdef WIN32 return true; #else diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -2277,6 +2277,7 @@ // Set to non-blocking, incoming connections will also inherit this if (!SetSocketNonBlocking(hListenSocket, true)) { + CloseSocket(hListenSocket); strError = strprintf("BindListenPort: Setting listening socket to " "non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError())); diff --git a/src/netbase.h b/src/netbase.h --- a/src/netbase.h +++ b/src/netbase.h @@ -64,9 +64,9 @@ /** Close socket and set hSocket to INVALID_SOCKET */ bool CloseSocket(SOCKET &hSocket); /** Disable or enable blocking-mode for a socket */ -bool SetSocketNonBlocking(SOCKET &hSocket, bool fNonBlocking); +bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking); /** Set the TCP_NODELAY flag on a socket */ -bool SetSocketNoDelay(SOCKET &hSocket); +bool SetSocketNoDelay(const SOCKET &hSocket); /** * Convert milliseconds to a struct timeval for e.g. select. */ diff --git a/src/netbase.cpp b/src/netbase.cpp --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -200,7 +200,7 @@ * @note This function requires that hSocket is in non-blocking mode. */ static IntrRecvError InterruptibleRecv(char *data, size_t len, int timeout, - SOCKET &hSocket) { + const SOCKET &hSocket) { int64_t curTime = GetTimeMillis(); int64_t endTime = curTime + timeout; // Maximum time to wait in one select call. It will take up until this time @@ -464,10 +464,12 @@ SetSocketNoDelay(hSocket); // Set to non-blocking - if (!SetSocketNonBlocking(hSocket, true)) + if (!SetSocketNonBlocking(hSocket, true)) { + CloseSocket(hSocket); return error("ConnectSocketDirectly: Setting socket to non-blocking " "failed, error %s\n", NetworkErrorString(WSAGetLastError())); + } if (connect(hSocket, (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR) { int nErr = WSAGetLastError(); @@ -675,7 +677,7 @@ return ret != SOCKET_ERROR; } -bool SetSocketNonBlocking(SOCKET &hSocket, bool fNonBlocking) { +bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking) { if (fNonBlocking) { #ifdef WIN32 u_long nOne = 1; @@ -684,7 +686,6 @@ int fFlags = fcntl(hSocket, F_GETFL, 0); if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) { #endif - CloseSocket(hSocket); return false; } } else { @@ -695,7 +696,6 @@ int fFlags = fcntl(hSocket, F_GETFL, 0); if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) { #endif - CloseSocket(hSocket); return false; } } @@ -703,7 +703,7 @@ return true; } -bool SetSocketNoDelay(SOCKET &hSocket) { +bool SetSocketNoDelay(const SOCKET &hSocket) { int set = 1; int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&set, sizeof(int));