aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2015-07-20 16:54:18 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2015-07-20 16:54:28 +0200
commit1a2de3237fc06a97dd83ab12759c57d83db370ef (patch)
tree41e95768db9f2aa7314fa3f898239c5c1d3e5e74
parentdc51608c9c279a3eb33e9a071ded27f613282ac7 (diff)
parentd422f9b1fdb42a51aadaa1bbc157542dca2feb17 (diff)
Merge pull request #6412
d422f9b Test whether created sockets are select()able (Pieter Wuille)
-rw-r--r--src/compat.h8
-rw-r--r--src/net.cpp18
-rw-r--r--src/netbase.cpp3
3 files changed, 29 insertions, 0 deletions
diff --git a/src/compat.h b/src/compat.h
index 7a5438a11e..f88b6ce36e 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -92,4 +92,12 @@ typedef u_int SOCKET;
size_t strnlen( const char *start, size_t max_len);
#endif // HAVE_DECL_STRNLEN
+bool static inline IsSelectableSocket(SOCKET s) {
+#ifdef WIN32
+ return true;
+#else
+ return (s >= 0 && s < FD_SETSIZE);
+#endif
+}
+
#endif // BITCOIN_COMPAT_H
diff --git a/src/net.cpp b/src/net.cpp
index 2c7ba0ca79..3d369c7dd1 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -386,6 +386,12 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
{
+ if (!IsSelectableSocket(hSocket)) {
+ LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
+ CloseSocket(hSocket);
+ return NULL;
+ }
+
addrman.Attempt(addrConnect);
// Add node
@@ -949,6 +955,11 @@ void ThreadSocketHandler()
if (nErr != WSAEWOULDBLOCK)
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
}
+ else if (!IsSelectableSocket(hSocket))
+ {
+ LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
+ CloseSocket(hSocket);
+ }
else if (nInbound >= nMaxInbound)
{
LogPrint("net", "connection from %s dropped (full)\n", addr.ToString());
@@ -1597,6 +1608,13 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste
LogPrintf("%s\n", strError);
return false;
}
+ if (!IsSelectableSocket(hListenSocket))
+ {
+ strError = "Error: Couldn't create a listenable socket for incoming connections";
+ LogPrintf("%s\n", strError);
+ return false;
+ }
+
#ifndef WIN32
#ifdef SO_NOSIGPIPE
diff --git a/src/netbase.cpp b/src/netbase.cpp
index c9fc7d67f7..b7e2e57917 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -266,6 +266,9 @@ bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSock
} else { // Other error or blocking
int nErr = WSAGetLastError();
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
+ if (!IsSelectableSocket(hSocket)) {
+ return false;
+ }
struct timeval tval = MillisToTimeval(std::min(endTime - curTime, maxWait));
fd_set fdset;
FD_ZERO(&fdset);