Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 2,019 Lines • ▼ Show 20 Lines | std::map<std::string, std::pair<bool, CService>> mapConnectedByName; | ||||
mapConnectedByName[std::move(addrName)] = | mapConnectedByName[std::move(addrName)] = | ||||
std::make_pair(pnode->fInbound, | std::make_pair(pnode->fInbound, | ||||
static_cast<const CService &>(pnode->addr)); | static_cast<const CService &>(pnode->addr)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
for (const std::string &strAddNode : lAddresses) { | for (const std::string &strAddNode : lAddresses) { | ||||
CService service(LookupNumeric( | CService service( | ||||
strAddNode.c_str(), config->GetChainParams().GetDefaultPort())); | LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort())); | ||||
AddedNodeInfo addedNode{strAddNode, CService(), false, false}; | |||||
if (service.IsValid()) { | if (service.IsValid()) { | ||||
// strAddNode is an IP:port | // strAddNode is an IP:port | ||||
auto it = mapConnected.find(service); | auto it = mapConnected.find(service); | ||||
if (it != mapConnected.end()) { | if (it != mapConnected.end()) { | ||||
ret.push_back( | addedNode.resolvedAddress = service; | ||||
AddedNodeInfo{strAddNode, service, true, it->second}); | addedNode.fConnected = true; | ||||
} else { | addedNode.fInbound = it->second; | ||||
ret.push_back( | |||||
AddedNodeInfo{strAddNode, CService(), false, false}); | |||||
} | } | ||||
} else { | } else { | ||||
// strAddNode is a name | // strAddNode is a name | ||||
auto it = mapConnectedByName.find(strAddNode); | auto it = mapConnectedByName.find(strAddNode); | ||||
if (it != mapConnectedByName.end()) { | if (it != mapConnectedByName.end()) { | ||||
ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, | addedNode.resolvedAddress = it->second.second; | ||||
it->second.first}); | addedNode.fConnected = true; | ||||
} else { | addedNode.fInbound = it->second.first; | ||||
ret.push_back( | |||||
AddedNodeInfo{strAddNode, CService(), false, false}); | |||||
} | } | ||||
} | } | ||||
ret.emplace_back(std::move(addedNode)); | |||||
} | } | ||||
return ret; | return ret; | ||||
} | } | ||||
void CConnman::ThreadOpenAddedConnections() { | void CConnman::ThreadOpenAddedConnections() { | ||||
while (true) { | while (true) { | ||||
CSemaphoreGrant grant(*semAddnode); | CSemaphoreGrant grant(*semAddnode); | ||||
▲ Show 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | bool CConnman::BindListenPort(const CService &addrBind, std::string &strError, | ||||
SOCKET hListenSocket = CreateSocket(addrBind); | SOCKET hListenSocket = CreateSocket(addrBind); | ||||
if (hListenSocket == INVALID_SOCKET) { | if (hListenSocket == INVALID_SOCKET) { | ||||
strError = strprintf("Error: Couldn't open socket for incoming " | strError = strprintf("Error: Couldn't open socket for incoming " | ||||
"connections (socket returned error %s)", | "connections (socket returned error %s)", | ||||
NetworkErrorString(WSAGetLastError())); | NetworkErrorString(WSAGetLastError())); | ||||
LogPrintf("%s\n", strError); | LogPrintf("%s\n", strError); | ||||
return false; | return false; | ||||
} | } | ||||
#ifndef WIN32 | |||||
// Allow binding if the port is still in TIME_WAIT state after | // Allow binding if the port is still in TIME_WAIT state after | ||||
// the program was closed and restarted. | // the program was closed and restarted. | ||||
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&nOne, | setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, | ||||
sizeof(int)); | sizeof(int)); | ||||
#else | |||||
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&nOne, | |||||
sizeof(int)); | |||||
#endif | |||||
// Some systems don't have IPV6_V6ONLY but are always v6only; others do have | // Some systems don't have IPV6_V6ONLY but are always v6only; others do have | ||||
// the option and enable it by default or not. Try to enable it, if | // the option and enable it by default or not. Try to enable it, if | ||||
// possible. | // possible. | ||||
if (addrBind.IsIPv6()) { | if (addrBind.IsIPv6()) { | ||||
#ifdef IPV6_V6ONLY | #ifdef IPV6_V6ONLY | ||||
#ifdef WIN32 | |||||
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, | setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, | ||||
(const char *)&nOne, sizeof(int)); | (sockopt_arg_type)&nOne, sizeof(int)); | ||||
#else | |||||
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&nOne, | |||||
sizeof(int)); | |||||
#endif | |||||
#endif | #endif | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED; | int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED; | ||||
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, | setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, | ||||
(const char *)&nProtLevel, sizeof(int)); | (sockopt_arg_type)&nProtLevel, sizeof(int)); | ||||
#endif | #endif | ||||
} | } | ||||
if (::bind(hListenSocket, (struct sockaddr *)&sockaddr, len) == | if (::bind(hListenSocket, (struct sockaddr *)&sockaddr, len) == | ||||
SOCKET_ERROR) { | SOCKET_ERROR) { | ||||
int nErr = WSAGetLastError(); | int nErr = WSAGetLastError(); | ||||
if (nErr == WSAEADDRINUSE) { | if (nErr == WSAEADDRINUSE) { | ||||
strError = strprintf(_("Unable to bind to %s on this computer. %s " | strError = strprintf(_("Unable to bind to %s on this computer. %s " | ||||
▲ Show 20 Lines • Show All 863 Lines • Show Last 20 Lines |