Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
Show First 20 Lines • Show All 1,477 Lines • ▼ Show 20 Lines | const CSipHasher hasher = | ||||
.Write((GetTime() + hashAddr) / (24 * 60 * 60)); | .Write((GetTime() + hashAddr) / (24 * 60 * 60)); | ||||
FastRandomContext insecure_rand; | FastRandomContext insecure_rand; | ||||
std::array<std::pair<uint64_t, CNode *>, 2> best{ | std::array<std::pair<uint64_t, CNode *>, 2> best{ | ||||
{{0, nullptr}, {0, nullptr}}}; | {{0, nullptr}, {0, nullptr}}}; | ||||
assert(nRelayNodes <= best.size()); | assert(nRelayNodes <= best.size()); | ||||
auto sortfunc = [&best, &hasher, nRelayNodes](CNode *pnode) { | auto sortfunc = [&best, &hasher, nRelayNodes](CNode *pnode) { | ||||
if (pnode->nVersion >= CADDR_TIME_VERSION) { | if (pnode->nVersion >= CADDR_TIME_VERSION && pnode->IsAddrRelayPeer()) { | ||||
uint64_t hashKey = | uint64_t hashKey = | ||||
CSipHasher(hasher).Write(pnode->GetId()).Finalize(); | CSipHasher(hasher).Write(pnode->GetId()).Finalize(); | ||||
for (unsigned int i = 0; i < nRelayNodes; i++) { | for (unsigned int i = 0; i < nRelayNodes; i++) { | ||||
if (hashKey > best[i].first) { | if (hashKey > best[i].first) { | ||||
std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, | std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, | ||||
best.begin() + i + 1); | best.begin() + i + 1); | ||||
best[i] = std::make_pair(hashKey, pnode); | best[i] = std::make_pair(hashKey, pnode); | ||||
break; | break; | ||||
▲ Show 20 Lines • Show All 836 Lines • ▼ Show 20 Lines | if (strCommand == NetMsgType::VERSION) { | ||||
pfrom->nVersion = nVersion; | pfrom->nVersion = nVersion; | ||||
// Potentially mark this peer as a preferred download peer. | // Potentially mark this peer as a preferred download peer. | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
UpdatePreferredDownload(pfrom, State(pfrom->GetId())); | UpdatePreferredDownload(pfrom, State(pfrom->GetId())); | ||||
} | } | ||||
if (!pfrom->fInbound) { | if (!pfrom->fInbound && pfrom->IsAddrRelayPeer()) { | ||||
// Advertise our address | // Advertise our address | ||||
if (fListen && !::ChainstateActive().IsInitialBlockDownload()) { | if (fListen && !::ChainstateActive().IsInitialBlockDownload()) { | ||||
CAddress addr = | CAddress addr = | ||||
GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices()); | GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices()); | ||||
FastRandomContext insecure_rand; | FastRandomContext insecure_rand; | ||||
if (addr.IsRoutable()) { | if (addr.IsRoutable()) { | ||||
LogPrint(BCLog::NET, | LogPrint(BCLog::NET, | ||||
"ProcessMessages: advertising address %s\n", | "ProcessMessages: advertising address %s\n", | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | if (strCommand == NetMsgType::ADDR) { | ||||
std::vector<CAddress> vAddr; | std::vector<CAddress> vAddr; | ||||
vRecv >> vAddr; | vRecv >> vAddr; | ||||
// Don't want addr from older versions unless seeding | // Don't want addr from older versions unless seeding | ||||
if (pfrom->nVersion < CADDR_TIME_VERSION && | if (pfrom->nVersion < CADDR_TIME_VERSION && | ||||
connman->GetAddressCount() > 1000) { | connman->GetAddressCount() > 1000) { | ||||
return true; | return true; | ||||
} | } | ||||
if (!pfrom->IsAddrRelayPeer()) { | |||||
return true; | |||||
} | |||||
if (vAddr.size() > 1000) { | if (vAddr.size() > 1000) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
Misbehaving(pfrom, 20, "oversized-addr"); | Misbehaving(pfrom, 20, "oversized-addr"); | ||||
return error("message addr size() = %u", vAddr.size()); | return error("message addr size() = %u", vAddr.size()); | ||||
} | } | ||||
// Store the new addresses | // Store the new addresses | ||||
std::vector<CAddress> vAddrOk; | std::vector<CAddress> vAddrOk; | ||||
▲ Show 20 Lines • Show All 1,082 Lines • ▼ Show 20 Lines | if (strCommand == NetMsgType::GETADDR) { | ||||
// only make outgoing connections ignore the getaddr message mitigates | // only make outgoing connections ignore the getaddr message mitigates | ||||
// the attack. | // the attack. | ||||
if (!pfrom->fInbound) { | if (!pfrom->fInbound) { | ||||
LogPrint(BCLog::NET, | LogPrint(BCLog::NET, | ||||
"Ignoring \"getaddr\" from outbound connection. peer=%d\n", | "Ignoring \"getaddr\" from outbound connection. peer=%d\n", | ||||
pfrom->GetId()); | pfrom->GetId()); | ||||
return true; | return true; | ||||
} | } | ||||
if (!pfrom->IsAddrRelayPeer()) { | |||||
LogPrint(BCLog::NET, | |||||
"Ignoring \"getaddr\" from block-relay-only connection. " | |||||
"peer=%d\n", | |||||
pfrom->GetId()); | |||||
return true; | |||||
} | |||||
// Only send one GetAddr response per connection to reduce resource | // Only send one GetAddr response per connection to reduce resource | ||||
// waste and discourage addr stamping of INV announcements. | // waste and discourage addr stamping of INV announcements. | ||||
if (pfrom->fSentAddr) { | if (pfrom->fSentAddr) { | ||||
LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", | LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", | ||||
pfrom->GetId()); | pfrom->GetId()); | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 707 Lines • ▼ Show 20 Lines | bool PeerLogicValidation::SendMessages(const Config &config, CNode *pto, | ||||
if (SendRejectsAndCheckIfBanned(pto, m_enable_bip61)) { | if (SendRejectsAndCheckIfBanned(pto, m_enable_bip61)) { | ||||
return true; | return true; | ||||
} | } | ||||
CNodeState &state = *State(pto->GetId()); | CNodeState &state = *State(pto->GetId()); | ||||
// Address refresh broadcast | // Address refresh broadcast | ||||
int64_t nNow = GetTimeMicros(); | int64_t nNow = GetTimeMicros(); | ||||
if (!::ChainstateActive().IsInitialBlockDownload() && | if (pto->IsAddrRelayPeer() && | ||||
!::ChainstateActive().IsInitialBlockDownload() && | |||||
pto->nNextLocalAddrSend < nNow) { | pto->nNextLocalAddrSend < nNow) { | ||||
AdvertiseLocal(pto); | AdvertiseLocal(pto); | ||||
pto->nNextLocalAddrSend = | pto->nNextLocalAddrSend = | ||||
PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL); | PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL); | ||||
} | } | ||||
// | // | ||||
// Message: addr | // Message: addr | ||||
// | // | ||||
if (pto->nNextAddrSend < nNow) { | if (pto->IsAddrRelayPeer() && pto->nNextAddrSend < nNow) { | ||||
pto->nNextAddrSend = | pto->nNextAddrSend = | ||||
PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL); | PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL); | ||||
std::vector<CAddress> vAddr; | std::vector<CAddress> vAddr; | ||||
vAddr.reserve(pto->vAddrToSend.size()); | vAddr.reserve(pto->vAddrToSend.size()); | ||||
for (const CAddress &addr : pto->vAddrToSend) { | for (const CAddress &addr : pto->vAddrToSend) { | ||||
if (!pto->addrKnown.contains(addr.GetKey())) { | if (!pto->addrKnown.contains(addr.GetKey())) { | ||||
pto->addrKnown.insert(addr.GetKey()); | pto->addrKnown.insert(addr.GetKey()); | ||||
vAddr.push_back(addr); | vAddr.push_back(addr); | ||||
▲ Show 20 Lines • Show All 629 Lines • Show Last 20 Lines |