Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 346 Lines • ▼ Show 20 Lines | LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n", | ||||
pszDest | pszDest | ||||
? 0.0 | ? 0.0 | ||||
: (double)(GetAdjustedTime() - addrConnect.nTime) / 3600.0); | : (double)(GetAdjustedTime() - addrConnect.nTime) / 3600.0); | ||||
// Connect | // Connect | ||||
SOCKET hSocket; | SOCKET hSocket; | ||||
bool proxyConnectionFailed = false; | bool proxyConnectionFailed = false; | ||||
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, | if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, | ||||
Params().GetDefaultPort(), | config->GetChainParams().GetDefaultPort(), | ||||
nConnectTimeout, &proxyConnectionFailed) | nConnectTimeout, &proxyConnectionFailed) | ||||
: ConnectSocket(addrConnect, hSocket, nConnectTimeout, | : ConnectSocket(addrConnect, hSocket, nConnectTimeout, | ||||
&proxyConnectionFailed)) { | &proxyConnectionFailed)) { | ||||
if (!IsSelectableSocket(hSocket)) { | if (!IsSelectableSocket(hSocket)) { | ||||
LogPrintf("Cannot create connection: non-selectable socket created " | LogPrintf("Cannot create connection: non-selectable socket created " | ||||
"(fd >= FD_SETSIZE ?)\n"); | "(fd >= FD_SETSIZE ?)\n"); | ||||
CloseSocket(hSocket); | CloseSocket(hSocket); | ||||
return nullptr; | return nullptr; | ||||
▲ Show 20 Lines • Show All 1,302 Lines • ▼ Show 20 Lines | if ((addrman.size() > 0) && | ||||
((pnode->nServices & nRelevantServices) == nRelevantServices); | ((pnode->nServices & nRelevantServices) == nRelevantServices); | ||||
} | } | ||||
if (nRelevant >= 2) { | if (nRelevant >= 2) { | ||||
LogPrintf("P2P peers available. Skipped DNS seeding.\n"); | LogPrintf("P2P peers available. Skipped DNS seeding.\n"); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds(); | const std::vector<CDNSSeedData> &vSeeds = | ||||
config->GetChainParams().DNSSeeds(); | |||||
int found = 0; | int found = 0; | ||||
LogPrintf("Loading addresses from DNS seeds (could take a while)\n"); | LogPrintf("Loading addresses from DNS seeds (could take a while)\n"); | ||||
for (const CDNSSeedData &seed : vSeeds) { | for (const CDNSSeedData &seed : vSeeds) { | ||||
if (HaveNameProxy()) { | if (HaveNameProxy()) { | ||||
AddOneShot(seed.host); | AddOneShot(seed.host); | ||||
} else { | } else { | ||||
std::vector<CNetAddr> vIPs; | std::vector<CNetAddr> vIPs; | ||||
std::vector<CAddress> vAdd; | std::vector<CAddress> vAdd; | ||||
ServiceFlags requiredServiceBits = nRelevantServices; | ServiceFlags requiredServiceBits = nRelevantServices; | ||||
if (LookupHost(GetDNSHost(seed, &requiredServiceBits).c_str(), vIPs, | if (LookupHost(GetDNSHost(seed, &requiredServiceBits).c_str(), vIPs, | ||||
0, true)) { | 0, true)) { | ||||
for (const CNetAddr &ip : vIPs) { | for (const CNetAddr &ip : vIPs) { | ||||
int nOneDay = 24 * 3600; | int nOneDay = 24 * 3600; | ||||
CAddress addr = | CAddress addr = CAddress( | ||||
CAddress(CService(ip, Params().GetDefaultPort()), | CService(ip, config->GetChainParams().GetDefaultPort()), | ||||
requiredServiceBits); | requiredServiceBits); | ||||
// Use a random age between 3 and 7 days old. | // Use a random age between 3 and 7 days old. | ||||
addr.nTime = GetTime() - 3 * nOneDay - GetRand(4 * nOneDay); | addr.nTime = GetTime() - 3 * nOneDay - GetRand(4 * nOneDay); | ||||
vAdd.push_back(addr); | vAdd.push_back(addr); | ||||
found++; | found++; | ||||
} | } | ||||
} | } | ||||
// TODO: The seed name resolve may fail, yielding an IP of [::], | // TODO: The seed name resolve may fail, yielding an IP of [::], | ||||
// which results in addrman assigning the same source to results | // which results in addrman assigning the same source to results | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | while (!interruptNet) { | ||||
// Add seed nodes if DNS seeds are all down (an infrastructure attack?). | // Add seed nodes if DNS seeds are all down (an infrastructure attack?). | ||||
if (addrman.size() == 0 && (GetTime() - nStart > 60)) { | if (addrman.size() == 0 && (GetTime() - nStart > 60)) { | ||||
static bool done = false; | static bool done = false; | ||||
if (!done) { | if (!done) { | ||||
LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be " | LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be " | ||||
"available.\n"); | "available.\n"); | ||||
CNetAddr local; | CNetAddr local; | ||||
LookupHost("127.0.0.1", local, false); | LookupHost("127.0.0.1", local, false); | ||||
addrman.Add(convertSeed6(Params().FixedSeeds()), local); | addrman.Add(convertSeed6(config->GetChainParams().FixedSeeds()), | ||||
local); | |||||
done = true; | done = true; | ||||
} | } | ||||
} | } | ||||
// | // | ||||
// Choose an address to connect to based on most recently seen | // Choose an address to connect to based on most recently seen | ||||
// | // | ||||
CAddress addrConnect; | CAddress addrConnect; | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | while (!interruptNet) { | ||||
// attempts and only if less than half the outbound are up. | // attempts and only if less than half the outbound are up. | ||||
if ((addr.nServices & nRelevantServices) != nRelevantServices && | if ((addr.nServices & nRelevantServices) != nRelevantServices && | ||||
(nTries < 40 || nOutbound >= (nMaxOutbound >> 1))) { | (nTries < 40 || nOutbound >= (nMaxOutbound >> 1))) { | ||||
continue; | continue; | ||||
} | } | ||||
// do not allow non-default ports, unless after 50 invalid addresses | // do not allow non-default ports, unless after 50 invalid addresses | ||||
// selected already. | // selected already. | ||||
if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50) { | if (addr.GetPort() != config->GetChainParams().GetDefaultPort() && | ||||
nTries < 50) { | |||||
continue; | continue; | ||||
} | } | ||||
addrConnect = addr; | addrConnect = addr; | ||||
break; | break; | ||||
} | } | ||||
if (addrConnect.IsValid()) { | if (addrConnect.IsValid()) { | ||||
▲ Show 20 Lines • Show All 45 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( | CService service(LookupNumeric( | ||||
LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort())); | strAddNode.c_str(), config->GetChainParams().GetDefaultPort())); | ||||
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( | ret.push_back( | ||||
AddedNodeInfo{strAddNode, service, true, it->second}); | AddedNodeInfo{strAddNode, service, true, it->second}); | ||||
} else { | } else { | ||||
ret.push_back( | ret.push_back( | ||||
Show All 34 Lines | while (true) { | ||||
// not wait here since while we are waiting the | // not wait here since while we are waiting the | ||||
// addednodeinfo state might change. | // addednodeinfo state might change. | ||||
break; | break; | ||||
} | } | ||||
// If strAddedNode is an IP/port, decode it immediately, so | // If strAddedNode is an IP/port, decode it immediately, so | ||||
// OpenNetworkConnection can detect existing connections to that | // OpenNetworkConnection can detect existing connections to that | ||||
// IP/port. | // IP/port. | ||||
tried = true; | tried = true; | ||||
CService service(LookupNumeric(info.strAddedNode.c_str(), | CService service( | ||||
Params().GetDefaultPort())); | LookupNumeric(info.strAddedNode.c_str(), | ||||
config->GetChainParams().GetDefaultPort())); | |||||
OpenNetworkConnection(CAddress(service, NODE_NONE), false, | OpenNetworkConnection(CAddress(service, NODE_NONE), false, | ||||
&grant, info.strAddedNode.c_str(), false, | &grant, info.strAddedNode.c_str(), false, | ||||
false, true); | false, true); | ||||
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) { | if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) { | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 901 Lines • ▼ Show 20 Lines | void CConnman::PushMessage(CNode *pnode, CSerializedNetMsg &&msg) { | ||||
size_t nMessageSize = msg.data.size(); | size_t nMessageSize = msg.data.size(); | ||||
size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE; | size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE; | ||||
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", | LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", | ||||
SanitizeString(msg.command.c_str()), nMessageSize, pnode->id); | SanitizeString(msg.command.c_str()), nMessageSize, pnode->id); | ||||
std::vector<uint8_t> serializedHeader; | std::vector<uint8_t> serializedHeader; | ||||
serializedHeader.reserve(CMessageHeader::HEADER_SIZE); | serializedHeader.reserve(CMessageHeader::HEADER_SIZE); | ||||
uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize); | uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize); | ||||
CMessageHeader hdr(Params().NetMagic(), msg.command.c_str(), nMessageSize); | CMessageHeader hdr(config->GetChainParams().NetMagic(), msg.command.c_str(), | ||||
nMessageSize); | |||||
memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); | memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); | ||||
CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr}; | CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr}; | ||||
size_t nBytesSent = 0; | size_t nBytesSent = 0; | ||||
{ | { | ||||
LOCK(pnode->cs_vSend); | LOCK(pnode->cs_vSend); | ||||
bool optimisticSend(pnode->vSendMsg.empty()); | bool optimisticSend(pnode->vSendMsg.empty()); | ||||
▲ Show 20 Lines • Show All 115 Lines • Show Last 20 Lines |