Skip to content

Commit 2560589

Browse files
committed
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.
1 parent 545bc5f commit 2560589

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/net.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,16 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10081008
int nInbound = 0;
10091009
int nMaxInbound = nMaxConnections - m_max_outbound;
10101010

1011-
if (hSocket != INVALID_SOCKET) {
1012-
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1013-
LogPrintf("Warning: Unknown socket family\n");
1011+
if (hSocket == INVALID_SOCKET) {
1012+
const int nErr = WSAGetLastError();
1013+
if (nErr != WSAEWOULDBLOCK) {
1014+
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
10141015
}
1016+
return;
1017+
}
1018+
1019+
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1020+
LogPrintf("Warning: Unknown socket family\n");
10151021
}
10161022

10171023
NetPermissionFlags permissionFlags = NetPermissionFlags::PF_NONE;
@@ -1032,14 +1038,6 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10321038
}
10331039
}
10341040

1035-
if (hSocket == INVALID_SOCKET)
1036-
{
1037-
int nErr = WSAGetLastError();
1038-
if (nErr != WSAEWOULDBLOCK)
1039-
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1040-
return;
1041-
}
1042-
10431041
if (!fNetworkActive) {
10441042
LogPrint(BCLog::NET, "connection from %s dropped: not accepting new connections\n", addr.ToString());
10451043
CloseSocket(hSocket);

0 commit comments

Comments
 (0)