Skip to content

Commit 9aaa1ca

Browse files
committed
Merge pull request #4397
5d59921 add missing BOOST_FOREACH indentation in ThreadSocketHandler() (Philip Kaufmann) 9e9ca2b small cleanup of #ifdefs in BindListenPort() (Philip Kaufmann)
2 parents 3faf1f8 + 5d59921 commit 9aaa1ca

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

src/net.cpp

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
#define MSG_NOSIGNAL 0
3737
#endif
3838

39+
// Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
40+
// Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
41+
#ifdef WIN32
42+
#ifndef PROTECTION_LEVEL_UNRESTRICTED
43+
#define PROTECTION_LEVEL_UNRESTRICTED 10
44+
#endif
45+
#ifndef IPV6_PROTECTION_LEVEL
46+
#define IPV6_PROTECTION_LEVEL 23
47+
#endif
48+
#endif
49+
3950
using namespace std;
4051
using namespace boost;
4152

@@ -816,7 +827,6 @@ void ThreadSocketHandler()
816827
uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
817828
}
818829

819-
820830
//
821831
// Find which sockets have data to receive
822832
//
@@ -838,6 +848,7 @@ void ThreadSocketHandler()
838848
hSocketMax = max(hSocketMax, hListenSocket);
839849
have_fds = true;
840850
}
851+
841852
{
842853
LOCK(cs_vNodes);
843854
BOOST_FOREACH(CNode* pnode, vNodes)
@@ -898,58 +909,59 @@ void ThreadSocketHandler()
898909
MilliSleep(timeout.tv_usec/1000);
899910
}
900911

901-
902912
//
903913
// Accept new connections
904914
//
905915
BOOST_FOREACH(SOCKET hListenSocket, vhListenSocket)
906-
if (hListenSocket != INVALID_SOCKET && FD_ISSET(hListenSocket, &fdsetRecv))
907916
{
908-
struct sockaddr_storage sockaddr;
909-
socklen_t len = sizeof(sockaddr);
910-
SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len);
911-
CAddress addr;
912-
int nInbound = 0;
913-
914-
if (hSocket != INVALID_SOCKET)
915-
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
916-
LogPrintf("Warning: Unknown socket family\n");
917-
917+
if (hListenSocket != INVALID_SOCKET && FD_ISSET(hListenSocket, &fdsetRecv))
918918
{
919-
LOCK(cs_vNodes);
920-
BOOST_FOREACH(CNode* pnode, vNodes)
921-
if (pnode->fInbound)
922-
nInbound++;
923-
}
919+
struct sockaddr_storage sockaddr;
920+
socklen_t len = sizeof(sockaddr);
921+
SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len);
922+
CAddress addr;
923+
int nInbound = 0;
924+
925+
if (hSocket != INVALID_SOCKET)
926+
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
927+
LogPrintf("Warning: Unknown socket family\n");
924928

925-
if (hSocket == INVALID_SOCKET)
926-
{
927-
int nErr = WSAGetLastError();
928-
if (nErr != WSAEWOULDBLOCK)
929-
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
930-
}
931-
else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
932-
{
933-
closesocket(hSocket);
934-
}
935-
else if (CNode::IsBanned(addr))
936-
{
937-
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
938-
closesocket(hSocket);
939-
}
940-
else
941-
{
942-
LogPrint("net", "accepted connection %s\n", addr.ToString());
943-
CNode* pnode = new CNode(hSocket, addr, "", true);
944-
pnode->AddRef();
945929
{
946930
LOCK(cs_vNodes);
947-
vNodes.push_back(pnode);
931+
BOOST_FOREACH(CNode* pnode, vNodes)
932+
if (pnode->fInbound)
933+
nInbound++;
934+
}
935+
936+
if (hSocket == INVALID_SOCKET)
937+
{
938+
int nErr = WSAGetLastError();
939+
if (nErr != WSAEWOULDBLOCK)
940+
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
941+
}
942+
else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
943+
{
944+
closesocket(hSocket);
945+
}
946+
else if (CNode::IsBanned(addr))
947+
{
948+
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
949+
closesocket(hSocket);
950+
}
951+
else
952+
{
953+
LogPrint("net", "accepted connection %s\n", addr.ToString());
954+
CNode* pnode = new CNode(hSocket, addr, "", true);
955+
pnode->AddRef();
956+
957+
{
958+
LOCK(cs_vNodes);
959+
vNodes.push_back(pnode);
960+
}
948961
}
949962
}
950963
}
951964

952-
953965
//
954966
// Service each socket
955967
//
@@ -1587,18 +1599,16 @@ bool BindListenPort(const CService &addrBind, string& strError)
15871599
return false;
15881600
}
15891601

1602+
#ifndef WIN32
15901603
#ifdef SO_NOSIGPIPE
15911604
// Different way of disabling SIGPIPE on BSD
15921605
setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
15931606
#endif
1594-
1595-
#ifndef WIN32
15961607
// Allow binding if the port is still in TIME_WAIT state after
1597-
// the program was closed and restarted. Not an issue on windows.
1608+
// the program was closed and restarted. Not an issue on windows!
15981609
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
15991610
#endif
16001611

1601-
16021612
#ifdef WIN32
16031613
// Set to non-blocking, incoming connections will also inherit this
16041614
if (ioctlsocket(hListenSocket, FIONBIO, (u_long*)&nOne) == SOCKET_ERROR)
@@ -1622,10 +1632,8 @@ bool BindListenPort(const CService &addrBind, string& strError)
16221632
#endif
16231633
#endif
16241634
#ifdef WIN32
1625-
int nProtLevel = 10 /* PROTECTION_LEVEL_UNRESTRICTED */;
1626-
int nParameterId = 23 /* IPV6_PROTECTION_LEVEl */;
1627-
// this call is allowed to fail
1628-
setsockopt(hListenSocket, IPPROTO_IPV6, nParameterId, (const char*)&nProtLevel, sizeof(int));
1635+
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
1636+
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
16291637
#endif
16301638
}
16311639

0 commit comments

Comments
 (0)