diff options
Diffstat (limited to 'src/net.cpp')
-rw-r--r-- | src/net.cpp | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/src/net.cpp b/src/net.cpp index d806059ece..5be91fd4f3 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -235,12 +235,11 @@ bool AddLocal(const CNetAddr &addr, int nScore) return AddLocal(CService(addr, GetListenPort()), nScore); } -bool RemoveLocal(const CService& addr) +void RemoveLocal(const CService& addr) { LOCK(cs_mapLocalHost); LogPrintf("RemoveLocal(%s)\n", addr.ToString()); mapLocalHost.erase(addr); - return true; } /** Make a particular network entirely off-limits (no automatic connects to it) */ @@ -1163,6 +1162,17 @@ void CConnman::ThreadSocketHandler() // { LOCK(cs_vNodes); + + if (!fNetworkActive) { + // Disconnect any connected nodes + for (CNode* pnode : vNodes) { + if (!pnode->fDisconnect) { + LogPrint(BCLog::NET, "Network not active, dropping peer=%d\n", pnode->GetId()); + pnode->fDisconnect = true; + } + } + } + // Disconnect unused nodes std::vector<CNode*> vNodesCopy = vNodes; for (CNode* pnode : vNodesCopy) @@ -1788,7 +1798,6 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect) CAddress addrConnect; // Only connect out to one peer per network group (/16 for IPv4). - // Do this here so we don't have to critsect vNodes inside mapAddresses critsect. int nOutbound = 0; std::set<std::vector<unsigned char> > setConnected; { @@ -2198,14 +2207,6 @@ void CConnman::SetNetworkActive(bool active) fNetworkActive = active; - if (!fNetworkActive) { - LOCK(cs_vNodes); - // Close sockets to all nodes - for (CNode* pnode : vNodes) { - pnode->CloseSocketDisconnect(); - } - } - uiInterface.NotifyNetworkActiveChanged(fNetworkActive); } @@ -2254,7 +2255,8 @@ bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<C if (binds.empty() && whiteBinds.empty()) { struct in_addr inaddr_any; inaddr_any.s_addr = INADDR_ANY; - fBound |= Bind(CService((in6_addr)IN6ADDR_ANY_INIT, GetListenPort()), BF_NONE); + struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT; + fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE); fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE); } return fBound; @@ -2864,8 +2866,20 @@ bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func) return found != nullptr && NodeFullyConnected(found) && func(found); } -int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) { - return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5); +int64_t CConnman::PoissonNextSendInbound(int64_t now, int average_interval_seconds) +{ + if (m_next_send_inv_to_incoming < now) { + // If this function were called from multiple threads simultaneously + // it would possible that both update the next send variable, and return a different result to their caller. + // This is not possible in practice as only the net processing thread invokes this function. + m_next_send_inv_to_incoming = PoissonNextSend(now, average_interval_seconds); + } + return m_next_send_inv_to_incoming; +} + +int64_t PoissonNextSend(int64_t now, int average_interval_seconds) +{ + return now + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5); } CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const |