Skip to content

Commit 1a2de32

Browse files
committed
Merge pull request #6412
d422f9b Test whether created sockets are select()able (Pieter Wuille)
2 parents dc51608 + d422f9b commit 1a2de32

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/compat.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,12 @@ typedef u_int SOCKET;
9292
size_t strnlen( const char *start, size_t max_len);
9393
#endif // HAVE_DECL_STRNLEN
9494

95+
bool static inline IsSelectableSocket(SOCKET s) {
96+
#ifdef WIN32
97+
return true;
98+
#else
99+
return (s >= 0 && s < FD_SETSIZE);
100+
#endif
101+
}
102+
95103
#endif // BITCOIN_COMPAT_H

src/net.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
386386
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
387387
ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
388388
{
389+
if (!IsSelectableSocket(hSocket)) {
390+
LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
391+
CloseSocket(hSocket);
392+
return NULL;
393+
}
394+
389395
addrman.Attempt(addrConnect);
390396

391397
// Add node
@@ -949,6 +955,11 @@ void ThreadSocketHandler()
949955
if (nErr != WSAEWOULDBLOCK)
950956
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
951957
}
958+
else if (!IsSelectableSocket(hSocket))
959+
{
960+
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
961+
CloseSocket(hSocket);
962+
}
952963
else if (nInbound >= nMaxInbound)
953964
{
954965
LogPrint("net", "connection from %s dropped (full)\n", addr.ToString());
@@ -1597,6 +1608,13 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste
15971608
LogPrintf("%s\n", strError);
15981609
return false;
15991610
}
1611+
if (!IsSelectableSocket(hListenSocket))
1612+
{
1613+
strError = "Error: Couldn't create a listenable socket for incoming connections";
1614+
LogPrintf("%s\n", strError);
1615+
return false;
1616+
}
1617+
16001618

16011619
#ifndef WIN32
16021620
#ifdef SO_NOSIGPIPE

src/netbase.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSock
266266
} else { // Other error or blocking
267267
int nErr = WSAGetLastError();
268268
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
269+
if (!IsSelectableSocket(hSocket)) {
270+
return false;
271+
}
269272
struct timeval tval = MillisToTimeval(std::min(endTime - curTime, maxWait));
270273
fd_set fdset;
271274
FD_ZERO(&fdset);

0 commit comments

Comments
 (0)