| 1072 | } |
| 1073 | |
| 1074 | void CConnman::AcceptConnection(const ListenSocket& hListenSocket) { |
| 1075 | struct sockaddr_storage sockaddr; |
| 1076 | socklen_t len = sizeof(sockaddr); |
| 1077 | SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len); |
| 1078 | CAddress addr; |
| 1079 | int nInbound = 0; |
| 1080 | int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler); |
| 1081 | |
| 1082 | if (hSocket != INVALID_SOCKET) { |
| 1083 | if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) { |
| 1084 | LogPrintf("Warning: Unknown socket family\n"); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr); |
| 1089 | { |
| 1090 | LOCK(cs_vNodes); |
| 1091 | for (const CNode* pnode : vNodes) { |
| 1092 | if (pnode->fInbound) nInbound++; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | if (hSocket == INVALID_SOCKET) |
| 1097 | { |
| 1098 | int nErr = WSAGetLastError(); |
| 1099 | if (nErr != WSAEWOULDBLOCK) |
| 1100 | LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr)); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | if (!fNetworkActive) { |
| 1105 | LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString()); |
| 1106 | CloseSocket(hSocket); |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | if (!IsSelectableSocket(hSocket)) |
| 1111 | { |
| 1112 | LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString()); |
| 1113 | CloseSocket(hSocket); |
| 1114 | return; |
| 1115 | } |
| 1116 | |
| 1117 | // According to the internet TCP_NODELAY is not carried into accepted sockets |
| 1118 | // on all platforms. Set it again here just to be sure. |
| 1119 | SetSocketNoDelay(hSocket); |
| 1120 | |
| 1121 | if (IsBanned(addr) && !whitelisted) |
| 1122 | { |
| 1123 | LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString()); |
| 1124 | CloseSocket(hSocket); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | if (nInbound >= nMaxInbound) |
| 1129 | { |
| 1130 | if (!AttemptToEvictConnection()) { |
| 1131 | // No connection to evict, disconnect the new connection |
nothing calls this directly
no test coverage detected