Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Number of DNS seeds to query when the number of connections is low. | * Number of DNS seeds to query when the number of connections is low. | ||||
*/ | */ | ||||
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3; | static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3; | ||||
/** | /** | ||||
* How long to delay before querying DNS seeds | * How long to delay before querying DNS seeds | ||||
* | |||||
* If we have more than THRESHOLD entries in addrman, then it's likely | |||||
* that we got those addresses from having previously connected to the P2P | |||||
* network, and that we'll be able to successfully reconnect to the P2P | |||||
* network via contacting one of them. So if that's the case, spend a | |||||
* little longer trying to connect to known peers before querying the | |||||
* DNS seeds. | |||||
*/ | */ | ||||
// 11sec | |||||
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11}; | static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11}; | ||||
// 5min | static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5}; | ||||
static constexpr std::chrono::seconds DNSSEEDS_DELAY_MANY_PEERS{300}; | // "many" vs "few" peers | ||||
// "many" vs "few" peers -- you should only get this many if you've been on the | |||||
// live network | |||||
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; | static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; | ||||
// We add a random period time (0 to 1 seconds) to feeler connections to prevent | // We add a random period time (0 to 1 seconds) to feeler connections to prevent | ||||
// synchronization. | // synchronization. | ||||
#define FEELER_SLEEP_WINDOW 1 | #define FEELER_SLEEP_WINDOW 1 | ||||
// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define | // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define | ||||
// it as 0 | // it as 0 | ||||
▲ Show 20 Lines • Show All 1,658 Lines • ▼ Show 20 Lines | void CConnman::ThreadDNSAddressSeed() { | ||||
int seeds_right_now = 0; | int seeds_right_now = 0; | ||||
int found = 0; | int found = 0; | ||||
if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) { | if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) { | ||||
// When -forcednsseed is provided, query all. | // When -forcednsseed is provided, query all. | ||||
seeds_right_now = seeds.size(); | seeds_right_now = seeds.size(); | ||||
} else if (addrman.size() == 0) { | } else if (addrman.size() == 0) { | ||||
// If we have no known peers, query all. | // If we have no known peers, query all. | ||||
// This will occur on the first run, or if peers.dat has been | |||||
// deleted. | |||||
seeds_right_now = seeds.size(); | seeds_right_now = seeds.size(); | ||||
} | } | ||||
// goal: only query DNS seed if address need is acute | // goal: only query DNS seed if address need is acute | ||||
// * If we have a reasonable number of peers in addrman, spend | // * If we have a reasonable number of peers in addrman, spend | ||||
// some time trying them first. This improves user privacy by | // some time trying them first. This improves user privacy by | ||||
// creating fewer identifying DNS requests, reduces trust by | // creating fewer identifying DNS requests, reduces trust by | ||||
// giving seeds less influence on the network topology, and | // giving seeds less influence on the network topology, and | ||||
Show All 13 Lines | for (const std::string &seed : seeds) { | ||||
if (seeds_right_now == 0) { | if (seeds_right_now == 0) { | ||||
seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE; | seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE; | ||||
if (addrman.size() > 0) { | if (addrman.size() > 0) { | ||||
LogPrintf("Waiting %d seconds before querying DNS seeds.\n", | LogPrintf("Waiting %d seconds before querying DNS seeds.\n", | ||||
seeds_wait_time.count()); | seeds_wait_time.count()); | ||||
std::chrono::seconds to_wait = seeds_wait_time; | std::chrono::seconds to_wait = seeds_wait_time; | ||||
while (to_wait.count() > 0) { | while (to_wait.count() > 0) { | ||||
// if sleeping for the MANY_PEERS interval, wake up | |||||
// early to see if we have enough peers and can stop | |||||
// this thread entirely freeing up its resources | |||||
std::chrono::seconds w = | std::chrono::seconds w = | ||||
std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait); | std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait); | ||||
if (!interruptNet.sleep_for(w)) { | if (!interruptNet.sleep_for(w)) { | ||||
return; | return; | ||||
} | } | ||||
to_wait -= w; | to_wait -= w; | ||||
int nRelevant = 0; | int nRelevant = 0; | ||||
Show All 21 Lines | for (const std::string &seed : seeds) { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (interruptNet) { | if (interruptNet) { | ||||
return; | return; | ||||
} | } | ||||
// hold off on querying seeds if p2p network deactivated | // hold off on querying seeds if P2P network deactivated | ||||
if (!fNetworkActive) { | if (!fNetworkActive) { | ||||
LogPrintf("Waiting for network to be reactivated before querying " | LogPrintf("Waiting for network to be reactivated before querying " | ||||
"DNS seeds.\n"); | "DNS seeds.\n"); | ||||
do { | do { | ||||
if (!interruptNet.sleep_for(std::chrono::seconds{1})) { | if (!interruptNet.sleep_for(std::chrono::seconds{1})) { | ||||
return; | return; | ||||
} | } | ||||
} while (!fNetworkActive); | } while (!fNetworkActive); | ||||
▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | while (!interruptNet) { | ||||
} | } | ||||
CSemaphoreGrant grant(*semOutbound); | CSemaphoreGrant grant(*semOutbound); | ||||
if (interruptNet) { | if (interruptNet) { | ||||
return; | return; | ||||
} | } | ||||
// Add seed nodes if DNS seeds are all down (an infrastructure attack?). | // Add seed nodes if DNS seeds are all down (an infrastructure attack?). | ||||
// Note that we only do this if we started with an empty peers.dat, | |||||
// (in which case we will query DNS seeds immediately) *and* the DNS | |||||
// seeds have not returned any results. | |||||
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; | ||||
local.SetInternal("fixedseeds"); | local.SetInternal("fixedseeds"); | ||||
addrman.Add(convertSeed6(config->GetChainParams().FixedSeeds()), | addrman.Add(convertSeed6(config->GetChainParams().FixedSeeds()), | ||||
▲ Show 20 Lines • Show All 1,188 Lines • Show Last 20 Lines |