From 25605895afe84b1765dd9da9240af22f99489df7 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Tue, 24 Nov 2020 12:15:07 +0100 Subject: [PATCH] net: check for invalid socket earlier in CConnman::AcceptConnection() This check is related to an `accept()` failure. So do the check earlier, closer to the `accept()` call. This will allow to isolate the `accept()`-specific code at the beginning of `CConnman::AcceptConnection()` and reuse the code that follows it. --- src/net.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index d9571e7036..f90131f1c9 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1008,10 +1008,16 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) { int nInbound = 0; int nMaxInbound = nMaxConnections - m_max_outbound; - if (hSocket != INVALID_SOCKET) { - if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) { - LogPrintf("Warning: Unknown socket family\n"); + if (hSocket == INVALID_SOCKET) { + const int nErr = WSAGetLastError(); + if (nErr != WSAEWOULDBLOCK) { + LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr)); } + return; + } + + if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) { + LogPrintf("Warning: Unknown socket family\n"); } NetPermissionFlags permissionFlags = NetPermissionFlags::PF_NONE; @@ -1032,14 +1038,6 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) { } } - if (hSocket == INVALID_SOCKET) - { - int nErr = WSAGetLastError(); - if (nErr != WSAEWOULDBLOCK) - LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr)); - return; - } - if (!fNetworkActive) { LogPrint(BCLog::NET, "connection from %s dropped: not accepting new connections\n", addr.ToString()); CloseSocket(hSocket);