Add required space to [[ -n "$1" ]] (previously [[ -n"$1" ]])
[bitcoinplatinum.git] / src / net.cpp
blob258599747a6a0ed184287c54bf7fb0c1e2b0f147
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
10 #include "net.h"
12 #include "addrman.h"
13 #include "chainparams.h"
14 #include "clientversion.h"
15 #include "consensus/consensus.h"
16 #include "crypto/common.h"
17 #include "crypto/sha256.h"
18 #include "hash.h"
19 #include "primitives/transaction.h"
20 #include "netbase.h"
21 #include "scheduler.h"
22 #include "ui_interface.h"
23 #include "utilstrencodings.h"
25 #ifdef WIN32
26 #include <string.h>
27 #else
28 #include <fcntl.h>
29 #endif
31 #ifdef USE_UPNP
32 #include <miniupnpc/miniupnpc.h>
33 #include <miniupnpc/miniwget.h>
34 #include <miniupnpc/upnpcommands.h>
35 #include <miniupnpc/upnperrors.h>
36 #endif
39 #include <math.h>
41 // Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
42 #define DUMP_ADDRESSES_INTERVAL 900
44 // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
45 #define FEELER_SLEEP_WINDOW 1
47 #if !defined(HAVE_MSG_NOSIGNAL)
48 #define MSG_NOSIGNAL 0
49 #endif
51 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
52 #if !defined(HAVE_MSG_DONTWAIT)
53 #define MSG_DONTWAIT 0
54 #endif
56 // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
57 // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
58 #ifdef WIN32
59 #ifndef PROTECTION_LEVEL_UNRESTRICTED
60 #define PROTECTION_LEVEL_UNRESTRICTED 10
61 #endif
62 #ifndef IPV6_PROTECTION_LEVEL
63 #define IPV6_PROTECTION_LEVEL 23
64 #endif
65 #endif
67 /** Used to pass flags to the Bind() function */
68 enum BindFlags {
69 BF_NONE = 0,
70 BF_EXPLICIT = (1U << 0),
71 BF_REPORT_ERROR = (1U << 1),
72 BF_WHITELIST = (1U << 2),
75 const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
77 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
78 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // SHA256("localhostnonce")[0:8]
80 // Global state variables
82 bool fDiscover = true;
83 bool fListen = true;
84 bool fRelayTxes = true;
85 CCriticalSection cs_mapLocalHost;
86 std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
87 static bool vfLimited[NET_MAX] = {};
88 std::string strSubVersion;
90 limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
92 void CConnman::AddOneShot(const std::string& strDest)
94 LOCK(cs_vOneShots);
95 vOneShots.push_back(strDest);
98 unsigned short GetListenPort()
100 return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
103 // find 'best' local address for a particular peer
104 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
106 if (!fListen)
107 return false;
109 int nBestScore = -1;
110 int nBestReachability = -1;
112 LOCK(cs_mapLocalHost);
113 for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
115 int nScore = (*it).second.nScore;
116 int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
117 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
119 addr = CService((*it).first, (*it).second.nPort);
120 nBestReachability = nReachability;
121 nBestScore = nScore;
125 return nBestScore >= 0;
128 //! Convert the pnSeeds6 array into usable address objects.
129 static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
131 // It'll only connect to one or two seed nodes because once it connects,
132 // it'll get a pile of addresses with newer timestamps.
133 // Seed nodes are given a random 'last seen time' of between one and two
134 // weeks ago.
135 const int64_t nOneWeek = 7*24*60*60;
136 std::vector<CAddress> vSeedsOut;
137 vSeedsOut.reserve(vSeedsIn.size());
138 for (const auto& seed_in : vSeedsIn) {
139 struct in6_addr ip;
140 memcpy(&ip, seed_in.addr, sizeof(ip));
141 CAddress addr(CService(ip, seed_in.port), NODE_NETWORK);
142 addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
143 vSeedsOut.push_back(addr);
145 return vSeedsOut;
148 // get best local address for a particular peer as a CAddress
149 // Otherwise, return the unroutable 0.0.0.0 but filled in with
150 // the normal parameters, since the IP may be changed to a useful
151 // one by discovery.
152 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
154 CAddress ret(CService(CNetAddr(),GetListenPort()), nLocalServices);
155 CService addr;
156 if (GetLocal(addr, paddrPeer))
158 ret = CAddress(addr, nLocalServices);
160 ret.nTime = GetAdjustedTime();
161 return ret;
164 int GetnScore(const CService& addr)
166 LOCK(cs_mapLocalHost);
167 if (mapLocalHost.count(addr) == LOCAL_NONE)
168 return 0;
169 return mapLocalHost[addr].nScore;
172 // Is our peer's addrLocal potentially useful as an external IP source?
173 bool IsPeerAddrLocalGood(CNode *pnode)
175 CService addrLocal = pnode->GetAddrLocal();
176 return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
177 !IsLimited(addrLocal.GetNetwork());
180 // pushes our own address to a peer
181 void AdvertiseLocal(CNode *pnode)
183 if (fListen && pnode->fSuccessfullyConnected)
185 CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
186 // If discovery is enabled, sometimes give our peer the address it
187 // tells us that it sees us as in case it has a better idea of our
188 // address than we do.
189 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
190 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
192 addrLocal.SetIP(pnode->GetAddrLocal());
194 if (addrLocal.IsRoutable())
196 LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
197 FastRandomContext insecure_rand;
198 pnode->PushAddress(addrLocal, insecure_rand);
203 // learn a new local address
204 bool AddLocal(const CService& addr, int nScore)
206 if (!addr.IsRoutable())
207 return false;
209 if (!fDiscover && nScore < LOCAL_MANUAL)
210 return false;
212 if (IsLimited(addr))
213 return false;
215 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
218 LOCK(cs_mapLocalHost);
219 bool fAlready = mapLocalHost.count(addr) > 0;
220 LocalServiceInfo &info = mapLocalHost[addr];
221 if (!fAlready || nScore >= info.nScore) {
222 info.nScore = nScore + (fAlready ? 1 : 0);
223 info.nPort = addr.GetPort();
227 return true;
230 bool AddLocal(const CNetAddr &addr, int nScore)
232 return AddLocal(CService(addr, GetListenPort()), nScore);
235 bool RemoveLocal(const CService& addr)
237 LOCK(cs_mapLocalHost);
238 LogPrintf("RemoveLocal(%s)\n", addr.ToString());
239 mapLocalHost.erase(addr);
240 return true;
243 /** Make a particular network entirely off-limits (no automatic connects to it) */
244 void SetLimited(enum Network net, bool fLimited)
246 if (net == NET_UNROUTABLE || net == NET_INTERNAL)
247 return;
248 LOCK(cs_mapLocalHost);
249 vfLimited[net] = fLimited;
252 bool IsLimited(enum Network net)
254 LOCK(cs_mapLocalHost);
255 return vfLimited[net];
258 bool IsLimited(const CNetAddr &addr)
260 return IsLimited(addr.GetNetwork());
263 /** vote for a local address */
264 bool SeenLocal(const CService& addr)
267 LOCK(cs_mapLocalHost);
268 if (mapLocalHost.count(addr) == 0)
269 return false;
270 mapLocalHost[addr].nScore++;
272 return true;
276 /** check whether a given address is potentially local */
277 bool IsLocal(const CService& addr)
279 LOCK(cs_mapLocalHost);
280 return mapLocalHost.count(addr) > 0;
283 /** check whether a given network is one we can probably connect to */
284 bool IsReachable(enum Network net)
286 LOCK(cs_mapLocalHost);
287 return !vfLimited[net];
290 /** check whether a given address is in a network we can probably connect to */
291 bool IsReachable(const CNetAddr& addr)
293 enum Network net = addr.GetNetwork();
294 return IsReachable(net);
298 CNode* CConnman::FindNode(const CNetAddr& ip)
300 LOCK(cs_vNodes);
301 for (CNode* pnode : vNodes) {
302 if ((CNetAddr)pnode->addr == ip) {
303 return pnode;
306 return nullptr;
309 CNode* CConnman::FindNode(const CSubNet& subNet)
311 LOCK(cs_vNodes);
312 for (CNode* pnode : vNodes) {
313 if (subNet.Match((CNetAddr)pnode->addr)) {
314 return pnode;
317 return nullptr;
320 CNode* CConnman::FindNode(const std::string& addrName)
322 LOCK(cs_vNodes);
323 for (CNode* pnode : vNodes) {
324 if (pnode->GetAddrName() == addrName) {
325 return pnode;
328 return nullptr;
331 CNode* CConnman::FindNode(const CService& addr)
333 LOCK(cs_vNodes);
334 for (CNode* pnode : vNodes) {
335 if ((CService)pnode->addr == addr) {
336 return pnode;
339 return nullptr;
342 bool CConnman::CheckIncomingNonce(uint64_t nonce)
344 LOCK(cs_vNodes);
345 for (CNode* pnode : vNodes) {
346 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
347 return false;
349 return true;
352 /** Get the bind address for a socket as CAddress */
353 static CAddress GetBindAddress(SOCKET sock)
355 CAddress addr_bind;
356 struct sockaddr_storage sockaddr_bind;
357 socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
358 if (sock != INVALID_SOCKET) {
359 if (!getsockname(sock, (struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
360 addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind);
361 } else {
362 LogPrint(BCLog::NET, "Warning: getsockname failed\n");
365 return addr_bind;
368 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
370 if (pszDest == nullptr) {
371 if (IsLocal(addrConnect))
372 return nullptr;
374 // Look for an existing connection
375 CNode* pnode = FindNode((CService)addrConnect);
376 if (pnode)
378 LogPrintf("Failed to open new connection, already connected\n");
379 return nullptr;
383 /// debug print
384 LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
385 pszDest ? pszDest : addrConnect.ToString(),
386 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
388 // Resolve
389 const int default_port = Params().GetDefaultPort();
390 if (pszDest) {
391 std::vector<CService> resolved;
392 if (Lookup(pszDest, resolved, default_port, fNameLookup && !HaveNameProxy(), 256) && !resolved.empty()) {
393 addrConnect = CAddress(resolved[GetRand(resolved.size())], NODE_NONE);
394 if (!addrConnect.IsValid()) {
395 LogPrint(BCLog::NET, "Resolver returned invalid address %s for %s", addrConnect.ToString(), pszDest);
396 return nullptr;
398 // It is possible that we already have a connection to the IP/port pszDest resolved to.
399 // In that case, drop the connection that was just created, and return the existing CNode instead.
400 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
401 // name catch this early.
402 LOCK(cs_vNodes);
403 CNode* pnode = FindNode((CService)addrConnect);
404 if (pnode)
406 pnode->MaybeSetAddrName(std::string(pszDest));
407 LogPrintf("Failed to open new connection, already connected\n");
408 return nullptr;
413 // Connect
414 bool connected = false;
415 SOCKET hSocket;
416 proxyType proxy;
417 if (addrConnect.IsValid()) {
418 bool proxyConnectionFailed = false;
420 if (GetProxy(addrConnect.GetNetwork(), proxy))
421 connected = ConnectThroughProxy(proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), hSocket, nConnectTimeout, &proxyConnectionFailed);
422 else // no proxy needed (none set for target network)
423 connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout);
424 if (!proxyConnectionFailed) {
425 // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
426 // the proxy, mark this as an attempt.
427 addrman.Attempt(addrConnect, fCountFailure);
429 } else if (pszDest && GetNameProxy(proxy)) {
430 std::string host;
431 int port = default_port;
432 SplitHostPort(std::string(pszDest), port, host);
433 connected = ConnectThroughProxy(proxy, host, port, hSocket, nConnectTimeout, nullptr);
435 if (connected) {
436 if (!IsSelectableSocket(hSocket)) {
437 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
438 CloseSocket(hSocket);
439 return nullptr;
442 // Add node
443 NodeId id = GetNewNodeId();
444 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
445 CAddress addr_bind = GetBindAddress(hSocket);
446 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", false);
447 pnode->AddRef();
449 return pnode;
452 return nullptr;
455 void CConnman::DumpBanlist()
457 SweepBanned(); // clean unused entries (if bantime has expired)
459 if (!BannedSetIsDirty())
460 return;
462 int64_t nStart = GetTimeMillis();
464 CBanDB bandb;
465 banmap_t banmap;
466 GetBanned(banmap);
467 if (bandb.Write(banmap)) {
468 SetBannedSetDirty(false);
471 LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
472 banmap.size(), GetTimeMillis() - nStart);
475 void CNode::CloseSocketDisconnect()
477 fDisconnect = true;
478 LOCK(cs_hSocket);
479 if (hSocket != INVALID_SOCKET)
481 LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
482 CloseSocket(hSocket);
486 void CConnman::ClearBanned()
489 LOCK(cs_setBanned);
490 setBanned.clear();
491 setBannedIsDirty = true;
493 DumpBanlist(); //store banlist to disk
494 if(clientInterface)
495 clientInterface->BannedListChanged();
498 bool CConnman::IsBanned(CNetAddr ip)
500 LOCK(cs_setBanned);
501 for (const auto& it : setBanned) {
502 CSubNet subNet = it.first;
503 CBanEntry banEntry = it.second;
505 if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
506 return true;
509 return false;
512 bool CConnman::IsBanned(CSubNet subnet)
514 LOCK(cs_setBanned);
515 banmap_t::iterator i = setBanned.find(subnet);
516 if (i != setBanned.end())
518 CBanEntry banEntry = (*i).second;
519 if (GetTime() < banEntry.nBanUntil) {
520 return true;
523 return false;
526 void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
527 CSubNet subNet(addr);
528 Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
531 void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
532 CBanEntry banEntry(GetTime());
533 banEntry.banReason = banReason;
534 if (bantimeoffset <= 0)
536 bantimeoffset = gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
537 sinceUnixEpoch = false;
539 banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
542 LOCK(cs_setBanned);
543 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
544 setBanned[subNet] = banEntry;
545 setBannedIsDirty = true;
547 else
548 return;
550 if(clientInterface)
551 clientInterface->BannedListChanged();
553 LOCK(cs_vNodes);
554 for (CNode* pnode : vNodes) {
555 if (subNet.Match((CNetAddr)pnode->addr))
556 pnode->fDisconnect = true;
559 if(banReason == BanReasonManuallyAdded)
560 DumpBanlist(); //store banlist to disk immediately if user requested ban
563 bool CConnman::Unban(const CNetAddr &addr) {
564 CSubNet subNet(addr);
565 return Unban(subNet);
568 bool CConnman::Unban(const CSubNet &subNet) {
570 LOCK(cs_setBanned);
571 if (!setBanned.erase(subNet))
572 return false;
573 setBannedIsDirty = true;
575 if(clientInterface)
576 clientInterface->BannedListChanged();
577 DumpBanlist(); //store banlist to disk immediately
578 return true;
581 void CConnman::GetBanned(banmap_t &banMap)
583 LOCK(cs_setBanned);
584 // Sweep the banlist so expired bans are not returned
585 SweepBanned();
586 banMap = setBanned; //create a thread safe copy
589 void CConnman::SetBanned(const banmap_t &banMap)
591 LOCK(cs_setBanned);
592 setBanned = banMap;
593 setBannedIsDirty = true;
596 void CConnman::SweepBanned()
598 int64_t now = GetTime();
600 LOCK(cs_setBanned);
601 banmap_t::iterator it = setBanned.begin();
602 while(it != setBanned.end())
604 CSubNet subNet = (*it).first;
605 CBanEntry banEntry = (*it).second;
606 if(now > banEntry.nBanUntil)
608 setBanned.erase(it++);
609 setBannedIsDirty = true;
610 LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
612 else
613 ++it;
617 bool CConnman::BannedSetIsDirty()
619 LOCK(cs_setBanned);
620 return setBannedIsDirty;
623 void CConnman::SetBannedSetDirty(bool dirty)
625 LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
626 setBannedIsDirty = dirty;
630 bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
631 for (const CSubNet& subnet : vWhitelistedRange) {
632 if (subnet.Match(addr))
633 return true;
635 return false;
638 std::string CNode::GetAddrName() const {
639 LOCK(cs_addrName);
640 return addrName;
643 void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
644 LOCK(cs_addrName);
645 if (addrName.empty()) {
646 addrName = addrNameIn;
650 CService CNode::GetAddrLocal() const {
651 LOCK(cs_addrLocal);
652 return addrLocal;
655 void CNode::SetAddrLocal(const CService& addrLocalIn) {
656 LOCK(cs_addrLocal);
657 if (addrLocal.IsValid()) {
658 error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
659 } else {
660 addrLocal = addrLocalIn;
664 #undef X
665 #define X(name) stats.name = name
666 void CNode::copyStats(CNodeStats &stats)
668 stats.nodeid = this->GetId();
669 X(nServices);
670 X(addr);
671 X(addrBind);
673 LOCK(cs_filter);
674 X(fRelayTxes);
676 X(nLastSend);
677 X(nLastRecv);
678 X(nTimeConnected);
679 X(nTimeOffset);
680 stats.addrName = GetAddrName();
681 X(nVersion);
683 LOCK(cs_SubVer);
684 X(cleanSubVer);
686 X(fInbound);
687 X(m_manual_connection);
688 X(nStartingHeight);
690 LOCK(cs_vSend);
691 X(mapSendBytesPerMsgCmd);
692 X(nSendBytes);
695 LOCK(cs_vRecv);
696 X(mapRecvBytesPerMsgCmd);
697 X(nRecvBytes);
699 X(fWhitelisted);
701 // It is common for nodes with good ping times to suddenly become lagged,
702 // due to a new block arriving or other large transfer.
703 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
704 // since pingtime does not update until the ping is complete, which might take a while.
705 // So, if a ping is taking an unusually long time in flight,
706 // the caller can immediately detect that this is happening.
707 int64_t nPingUsecWait = 0;
708 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
709 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
712 // Raw ping time is in microseconds, but show it to user as whole seconds (Bitcoin users should be well used to small numbers with many decimal places by now :)
713 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
714 stats.dMinPing = (((double)nMinPingUsecTime) / 1e6);
715 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
717 // Leave string empty if addrLocal invalid (not filled in yet)
718 CService addrLocalUnlocked = GetAddrLocal();
719 stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
721 #undef X
723 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
725 complete = false;
726 int64_t nTimeMicros = GetTimeMicros();
727 LOCK(cs_vRecv);
728 nLastRecv = nTimeMicros / 1000000;
729 nRecvBytes += nBytes;
730 while (nBytes > 0) {
732 // get current incomplete message, or create a new one
733 if (vRecvMsg.empty() ||
734 vRecvMsg.back().complete())
735 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
737 CNetMessage& msg = vRecvMsg.back();
739 // absorb network data
740 int handled;
741 if (!msg.in_data)
742 handled = msg.readHeader(pch, nBytes);
743 else
744 handled = msg.readData(pch, nBytes);
746 if (handled < 0)
747 return false;
749 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
750 LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
751 return false;
754 pch += handled;
755 nBytes -= handled;
757 if (msg.complete()) {
759 //store received bytes per message command
760 //to prevent a memory DOS, only allow valid commands
761 mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
762 if (i == mapRecvBytesPerMsgCmd.end())
763 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
764 assert(i != mapRecvBytesPerMsgCmd.end());
765 i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
767 msg.nTime = nTimeMicros;
768 complete = true;
772 return true;
775 void CNode::SetSendVersion(int nVersionIn)
777 // Send version may only be changed in the version message, and
778 // only one version message is allowed per session. We can therefore
779 // treat this value as const and even atomic as long as it's only used
780 // once a version message has been successfully processed. Any attempt to
781 // set this twice is an error.
782 if (nSendVersion != 0) {
783 error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
784 } else {
785 nSendVersion = nVersionIn;
789 int CNode::GetSendVersion() const
791 // The send version should always be explicitly set to
792 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
793 // has been called.
794 if (nSendVersion == 0) {
795 error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
796 return INIT_PROTO_VERSION;
798 return nSendVersion;
802 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
804 // copy data to temporary parsing buffer
805 unsigned int nRemaining = 24 - nHdrPos;
806 unsigned int nCopy = std::min(nRemaining, nBytes);
808 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
809 nHdrPos += nCopy;
811 // if header incomplete, exit
812 if (nHdrPos < 24)
813 return nCopy;
815 // deserialize to CMessageHeader
816 try {
817 hdrbuf >> hdr;
819 catch (const std::exception&) {
820 return -1;
823 // reject messages larger than MAX_SIZE
824 if (hdr.nMessageSize > MAX_SIZE)
825 return -1;
827 // switch state to reading message data
828 in_data = true;
830 return nCopy;
833 int CNetMessage::readData(const char *pch, unsigned int nBytes)
835 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
836 unsigned int nCopy = std::min(nRemaining, nBytes);
838 if (vRecv.size() < nDataPos + nCopy) {
839 // Allocate up to 256 KiB ahead, but never more than the total message size.
840 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
843 hasher.Write((const unsigned char*)pch, nCopy);
844 memcpy(&vRecv[nDataPos], pch, nCopy);
845 nDataPos += nCopy;
847 return nCopy;
850 const uint256& CNetMessage::GetMessageHash() const
852 assert(complete());
853 if (data_hash.IsNull())
854 hasher.Finalize(data_hash.begin());
855 return data_hash;
866 // requires LOCK(cs_vSend)
867 size_t CConnman::SocketSendData(CNode *pnode) const
869 auto it = pnode->vSendMsg.begin();
870 size_t nSentSize = 0;
872 while (it != pnode->vSendMsg.end()) {
873 const auto &data = *it;
874 assert(data.size() > pnode->nSendOffset);
875 int nBytes = 0;
877 LOCK(pnode->cs_hSocket);
878 if (pnode->hSocket == INVALID_SOCKET)
879 break;
880 nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
882 if (nBytes > 0) {
883 pnode->nLastSend = GetSystemTimeInSeconds();
884 pnode->nSendBytes += nBytes;
885 pnode->nSendOffset += nBytes;
886 nSentSize += nBytes;
887 if (pnode->nSendOffset == data.size()) {
888 pnode->nSendOffset = 0;
889 pnode->nSendSize -= data.size();
890 pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
891 it++;
892 } else {
893 // could not send full message; stop sending more
894 break;
896 } else {
897 if (nBytes < 0) {
898 // error
899 int nErr = WSAGetLastError();
900 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
902 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
903 pnode->CloseSocketDisconnect();
906 // couldn't send anything at all
907 break;
911 if (it == pnode->vSendMsg.end()) {
912 assert(pnode->nSendOffset == 0);
913 assert(pnode->nSendSize == 0);
915 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
916 return nSentSize;
919 struct NodeEvictionCandidate
921 NodeId id;
922 int64_t nTimeConnected;
923 int64_t nMinPingUsecTime;
924 int64_t nLastBlockTime;
925 int64_t nLastTXTime;
926 bool fRelevantServices;
927 bool fRelayTxes;
928 bool fBloomFilter;
929 CAddress addr;
930 uint64_t nKeyedNetGroup;
933 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
935 return a.nMinPingUsecTime > b.nMinPingUsecTime;
938 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
940 return a.nTimeConnected > b.nTimeConnected;
943 static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
944 return a.nKeyedNetGroup < b.nKeyedNetGroup;
947 static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
949 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
950 if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
951 if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
952 return a.nTimeConnected > b.nTimeConnected;
955 static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
957 // There is a fall-through here because it is common for a node to have more than a few peers that have not yet relayed txn.
958 if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
959 if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
960 if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
961 return a.nTimeConnected > b.nTimeConnected;
964 /** Try to find a connection to evict when the node is full.
965 * Extreme care must be taken to avoid opening the node to attacker
966 * triggered network partitioning.
967 * The strategy used here is to protect a small number of peers
968 * for each of several distinct characteristics which are difficult
969 * to forge. In order to partition a node the attacker must be
970 * simultaneously better at all of them than honest peers.
972 bool CConnman::AttemptToEvictConnection()
974 std::vector<NodeEvictionCandidate> vEvictionCandidates;
976 LOCK(cs_vNodes);
978 for (const CNode* node : vNodes) {
979 if (node->fWhitelisted)
980 continue;
981 if (!node->fInbound)
982 continue;
983 if (node->fDisconnect)
984 continue;
985 NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
986 node->nLastBlockTime, node->nLastTXTime,
987 HasAllDesirableServiceFlags(node->nServices),
988 node->fRelayTxes, node->pfilter != nullptr, node->addr, node->nKeyedNetGroup};
989 vEvictionCandidates.push_back(candidate);
993 if (vEvictionCandidates.empty()) return false;
995 // Protect connections with certain characteristics
997 // Deterministically select 4 peers to protect by netgroup.
998 // An attacker cannot predict which netgroups will be protected
999 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNetGroupKeyed);
1000 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
1002 if (vEvictionCandidates.empty()) return false;
1004 // Protect the 8 nodes with the lowest minimum ping time.
1005 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
1006 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);
1007 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
1009 if (vEvictionCandidates.empty()) return false;
1011 // Protect 4 nodes that most recently sent us transactions.
1012 // An attacker cannot manipulate this metric without performing useful work.
1013 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeTXTime);
1014 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
1016 if (vEvictionCandidates.empty()) return false;
1018 // Protect 4 nodes that most recently sent us blocks.
1019 // An attacker cannot manipulate this metric without performing useful work.
1020 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeBlockTime);
1021 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
1023 if (vEvictionCandidates.empty()) return false;
1025 // Protect the half of the remaining nodes which have been connected the longest.
1026 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1027 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
1028 vEvictionCandidates.erase(vEvictionCandidates.end() - static_cast<int>(vEvictionCandidates.size() / 2), vEvictionCandidates.end());
1030 if (vEvictionCandidates.empty()) return false;
1032 // Identify the network group with the most connections and youngest member.
1033 // (vEvictionCandidates is already sorted by reverse connect time)
1034 uint64_t naMostConnections;
1035 unsigned int nMostConnections = 0;
1036 int64_t nMostConnectionsTime = 0;
1037 std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
1038 for (const NodeEvictionCandidate &node : vEvictionCandidates) {
1039 mapNetGroupNodes[node.nKeyedNetGroup].push_back(node);
1040 int64_t grouptime = mapNetGroupNodes[node.nKeyedNetGroup][0].nTimeConnected;
1041 size_t groupsize = mapNetGroupNodes[node.nKeyedNetGroup].size();
1043 if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
1044 nMostConnections = groupsize;
1045 nMostConnectionsTime = grouptime;
1046 naMostConnections = node.nKeyedNetGroup;
1050 // Reduce to the network group with the most connections
1051 vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1053 // Disconnect from the network group with the most connections
1054 NodeId evicted = vEvictionCandidates.front().id;
1055 LOCK(cs_vNodes);
1056 for (CNode* pnode : vNodes) {
1057 if (pnode->GetId() == evicted) {
1058 pnode->fDisconnect = true;
1059 return true;
1062 return false;
1065 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1066 struct sockaddr_storage sockaddr;
1067 socklen_t len = sizeof(sockaddr);
1068 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
1069 CAddress addr;
1070 int nInbound = 0;
1071 int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler);
1073 if (hSocket != INVALID_SOCKET) {
1074 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1075 LogPrintf("Warning: Unknown socket family\n");
1079 bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
1081 LOCK(cs_vNodes);
1082 for (const CNode* pnode : vNodes) {
1083 if (pnode->fInbound) nInbound++;
1087 if (hSocket == INVALID_SOCKET)
1089 int nErr = WSAGetLastError();
1090 if (nErr != WSAEWOULDBLOCK)
1091 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1092 return;
1095 if (!fNetworkActive) {
1096 LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
1097 CloseSocket(hSocket);
1098 return;
1101 if (!IsSelectableSocket(hSocket))
1103 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
1104 CloseSocket(hSocket);
1105 return;
1108 // According to the internet TCP_NODELAY is not carried into accepted sockets
1109 // on all platforms. Set it again here just to be sure.
1110 SetSocketNoDelay(hSocket);
1112 if (IsBanned(addr) && !whitelisted)
1114 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
1115 CloseSocket(hSocket);
1116 return;
1119 if (nInbound >= nMaxInbound)
1121 if (!AttemptToEvictConnection()) {
1122 // No connection to evict, disconnect the new connection
1123 LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1124 CloseSocket(hSocket);
1125 return;
1129 NodeId id = GetNewNodeId();
1130 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1131 CAddress addr_bind = GetBindAddress(hSocket);
1133 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
1134 pnode->AddRef();
1135 pnode->fWhitelisted = whitelisted;
1136 m_msgproc->InitializeNode(pnode);
1138 LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1141 LOCK(cs_vNodes);
1142 vNodes.push_back(pnode);
1146 void CConnman::ThreadSocketHandler()
1148 unsigned int nPrevNodeCount = 0;
1149 while (!interruptNet)
1152 // Disconnect nodes
1155 LOCK(cs_vNodes);
1156 // Disconnect unused nodes
1157 std::vector<CNode*> vNodesCopy = vNodes;
1158 for (CNode* pnode : vNodesCopy)
1160 if (pnode->fDisconnect)
1162 // remove from vNodes
1163 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1165 // release outbound grant (if any)
1166 pnode->grantOutbound.Release();
1168 // close socket and cleanup
1169 pnode->CloseSocketDisconnect();
1171 // hold in disconnected pool until all refs are released
1172 pnode->Release();
1173 vNodesDisconnected.push_back(pnode);
1178 // Delete disconnected nodes
1179 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1180 for (CNode* pnode : vNodesDisconnectedCopy)
1182 // wait until threads are done using it
1183 if (pnode->GetRefCount() <= 0) {
1184 bool fDelete = false;
1186 TRY_LOCK(pnode->cs_inventory, lockInv);
1187 if (lockInv) {
1188 TRY_LOCK(pnode->cs_vSend, lockSend);
1189 if (lockSend) {
1190 fDelete = true;
1194 if (fDelete) {
1195 vNodesDisconnected.remove(pnode);
1196 DeleteNode(pnode);
1201 size_t vNodesSize;
1203 LOCK(cs_vNodes);
1204 vNodesSize = vNodes.size();
1206 if(vNodesSize != nPrevNodeCount) {
1207 nPrevNodeCount = vNodesSize;
1208 if(clientInterface)
1209 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1213 // Find which sockets have data to receive
1215 struct timeval timeout;
1216 timeout.tv_sec = 0;
1217 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1219 fd_set fdsetRecv;
1220 fd_set fdsetSend;
1221 fd_set fdsetError;
1222 FD_ZERO(&fdsetRecv);
1223 FD_ZERO(&fdsetSend);
1224 FD_ZERO(&fdsetError);
1225 SOCKET hSocketMax = 0;
1226 bool have_fds = false;
1228 for (const ListenSocket& hListenSocket : vhListenSocket) {
1229 FD_SET(hListenSocket.socket, &fdsetRecv);
1230 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1231 have_fds = true;
1235 LOCK(cs_vNodes);
1236 for (CNode* pnode : vNodes)
1238 // Implement the following logic:
1239 // * If there is data to send, select() for sending data. As this only
1240 // happens when optimistic write failed, we choose to first drain the
1241 // write buffer in this case before receiving more. This avoids
1242 // needlessly queueing received data, if the remote peer is not themselves
1243 // receiving data. This means properly utilizing TCP flow control signalling.
1244 // * Otherwise, if there is space left in the receive buffer, select() for
1245 // receiving data.
1246 // * Hand off all complete messages to the processor, to be handled without
1247 // blocking here.
1249 bool select_recv = !pnode->fPauseRecv;
1250 bool select_send;
1252 LOCK(pnode->cs_vSend);
1253 select_send = !pnode->vSendMsg.empty();
1256 LOCK(pnode->cs_hSocket);
1257 if (pnode->hSocket == INVALID_SOCKET)
1258 continue;
1260 FD_SET(pnode->hSocket, &fdsetError);
1261 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1262 have_fds = true;
1264 if (select_send) {
1265 FD_SET(pnode->hSocket, &fdsetSend);
1266 continue;
1268 if (select_recv) {
1269 FD_SET(pnode->hSocket, &fdsetRecv);
1274 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1275 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1276 if (interruptNet)
1277 return;
1279 if (nSelect == SOCKET_ERROR)
1281 if (have_fds)
1283 int nErr = WSAGetLastError();
1284 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1285 for (unsigned int i = 0; i <= hSocketMax; i++)
1286 FD_SET(i, &fdsetRecv);
1288 FD_ZERO(&fdsetSend);
1289 FD_ZERO(&fdsetError);
1290 if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
1291 return;
1295 // Accept new connections
1297 for (const ListenSocket& hListenSocket : vhListenSocket)
1299 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1301 AcceptConnection(hListenSocket);
1306 // Service each socket
1308 std::vector<CNode*> vNodesCopy;
1310 LOCK(cs_vNodes);
1311 vNodesCopy = vNodes;
1312 for (CNode* pnode : vNodesCopy)
1313 pnode->AddRef();
1315 for (CNode* pnode : vNodesCopy)
1317 if (interruptNet)
1318 return;
1321 // Receive
1323 bool recvSet = false;
1324 bool sendSet = false;
1325 bool errorSet = false;
1327 LOCK(pnode->cs_hSocket);
1328 if (pnode->hSocket == INVALID_SOCKET)
1329 continue;
1330 recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
1331 sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
1332 errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
1334 if (recvSet || errorSet)
1336 // typical socket buffer is 8K-64K
1337 char pchBuf[0x10000];
1338 int nBytes = 0;
1340 LOCK(pnode->cs_hSocket);
1341 if (pnode->hSocket == INVALID_SOCKET)
1342 continue;
1343 nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1345 if (nBytes > 0)
1347 bool notify = false;
1348 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1349 pnode->CloseSocketDisconnect();
1350 RecordBytesRecv(nBytes);
1351 if (notify) {
1352 size_t nSizeAdded = 0;
1353 auto it(pnode->vRecvMsg.begin());
1354 for (; it != pnode->vRecvMsg.end(); ++it) {
1355 if (!it->complete())
1356 break;
1357 nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1360 LOCK(pnode->cs_vProcessMsg);
1361 pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1362 pnode->nProcessQueueSize += nSizeAdded;
1363 pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1365 WakeMessageHandler();
1368 else if (nBytes == 0)
1370 // socket closed gracefully
1371 if (!pnode->fDisconnect) {
1372 LogPrint(BCLog::NET, "socket closed\n");
1374 pnode->CloseSocketDisconnect();
1376 else if (nBytes < 0)
1378 // error
1379 int nErr = WSAGetLastError();
1380 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1382 if (!pnode->fDisconnect)
1383 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1384 pnode->CloseSocketDisconnect();
1390 // Send
1392 if (sendSet)
1394 LOCK(pnode->cs_vSend);
1395 size_t nBytes = SocketSendData(pnode);
1396 if (nBytes) {
1397 RecordBytesSent(nBytes);
1402 // Inactivity checking
1404 int64_t nTime = GetSystemTimeInSeconds();
1405 if (nTime - pnode->nTimeConnected > 60)
1407 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1409 LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1410 pnode->fDisconnect = true;
1412 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1414 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1415 pnode->fDisconnect = true;
1417 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1419 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1420 pnode->fDisconnect = true;
1422 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1424 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1425 pnode->fDisconnect = true;
1427 else if (!pnode->fSuccessfullyConnected)
1429 LogPrintf("version handshake timeout from %d\n", pnode->GetId());
1430 pnode->fDisconnect = true;
1435 LOCK(cs_vNodes);
1436 for (CNode* pnode : vNodesCopy)
1437 pnode->Release();
1442 void CConnman::WakeMessageHandler()
1445 std::lock_guard<std::mutex> lock(mutexMsgProc);
1446 fMsgProcWake = true;
1448 condMsgProc.notify_one();
1456 #ifdef USE_UPNP
1457 void ThreadMapPort()
1459 std::string port = strprintf("%u", GetListenPort());
1460 const char * multicastif = nullptr;
1461 const char * minissdpdpath = nullptr;
1462 struct UPNPDev * devlist = nullptr;
1463 char lanaddr[64];
1465 #ifndef UPNPDISCOVER_SUCCESS
1466 /* miniupnpc 1.5 */
1467 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1468 #elif MINIUPNPC_API_VERSION < 14
1469 /* miniupnpc 1.6 */
1470 int error = 0;
1471 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1472 #else
1473 /* miniupnpc 1.9.20150730 */
1474 int error = 0;
1475 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1476 #endif
1478 struct UPNPUrls urls;
1479 struct IGDdatas data;
1480 int r;
1482 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1483 if (r == 1)
1485 if (fDiscover) {
1486 char externalIPAddress[40];
1487 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1488 if(r != UPNPCOMMAND_SUCCESS)
1489 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1490 else
1492 if(externalIPAddress[0])
1494 CNetAddr resolved;
1495 if(LookupHost(externalIPAddress, resolved, false)) {
1496 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1497 AddLocal(resolved, LOCAL_UPNP);
1500 else
1501 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1505 std::string strDesc = "Bitcoin " + FormatFullVersion();
1507 try {
1508 while (true) {
1509 #ifndef UPNPDISCOVER_SUCCESS
1510 /* miniupnpc 1.5 */
1511 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1512 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1513 #else
1514 /* miniupnpc 1.6 */
1515 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1516 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1517 #endif
1519 if(r!=UPNPCOMMAND_SUCCESS)
1520 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1521 port, port, lanaddr, r, strupnperror(r));
1522 else
1523 LogPrintf("UPnP Port Mapping successful.\n");
1525 MilliSleep(20*60*1000); // Refresh every 20 minutes
1528 catch (const boost::thread_interrupted&)
1530 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1531 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1532 freeUPNPDevlist(devlist); devlist = nullptr;
1533 FreeUPNPUrls(&urls);
1534 throw;
1536 } else {
1537 LogPrintf("No valid UPnP IGDs found\n");
1538 freeUPNPDevlist(devlist); devlist = nullptr;
1539 if (r != 0)
1540 FreeUPNPUrls(&urls);
1544 void MapPort(bool fUseUPnP)
1546 static boost::thread* upnp_thread = nullptr;
1548 if (fUseUPnP)
1550 if (upnp_thread) {
1551 upnp_thread->interrupt();
1552 upnp_thread->join();
1553 delete upnp_thread;
1555 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1557 else if (upnp_thread) {
1558 upnp_thread->interrupt();
1559 upnp_thread->join();
1560 delete upnp_thread;
1561 upnp_thread = nullptr;
1565 #else
1566 void MapPort(bool)
1568 // Intentionally left blank.
1570 #endif
1577 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1579 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1580 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1581 *requiredServiceBits = NODE_NETWORK;
1582 return data.host;
1585 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1586 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1590 void CConnman::ThreadDNSAddressSeed()
1592 // goal: only query DNS seeds if address need is acute
1593 // Avoiding DNS seeds when we don't need them improves user privacy by
1594 // creating fewer identifying DNS requests, reduces trust by giving seeds
1595 // less influence on the network topology, and reduces traffic to the seeds.
1596 if ((addrman.size() > 0) &&
1597 (!gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1598 if (!interruptNet.sleep_for(std::chrono::seconds(11)))
1599 return;
1601 LOCK(cs_vNodes);
1602 int nRelevant = 0;
1603 for (auto pnode : vNodes) {
1604 nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound;
1606 if (nRelevant >= 2) {
1607 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1608 return;
1612 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1613 int found = 0;
1615 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1617 for (const CDNSSeedData &seed : vSeeds) {
1618 if (interruptNet) {
1619 return;
1621 if (HaveNameProxy()) {
1622 AddOneShot(seed.host);
1623 } else {
1624 std::vector<CNetAddr> vIPs;
1625 std::vector<CAddress> vAdd;
1626 ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE);
1627 std::string host = GetDNSHost(seed, &requiredServiceBits);
1628 CNetAddr resolveSource;
1629 if (!resolveSource.SetInternal(host)) {
1630 continue;
1632 if (LookupHost(host.c_str(), vIPs, 0, true))
1634 for (const CNetAddr& ip : vIPs)
1636 int nOneDay = 24*3600;
1637 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
1638 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1639 vAdd.push_back(addr);
1640 found++;
1642 addrman.Add(vAdd, resolveSource);
1647 LogPrintf("%d addresses found from DNS seeds\n", found);
1661 void CConnman::DumpAddresses()
1663 int64_t nStart = GetTimeMillis();
1665 CAddrDB adb;
1666 adb.Write(addrman);
1668 LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1669 addrman.size(), GetTimeMillis() - nStart);
1672 void CConnman::DumpData()
1674 DumpAddresses();
1675 DumpBanlist();
1678 void CConnman::ProcessOneShot()
1680 std::string strDest;
1682 LOCK(cs_vOneShots);
1683 if (vOneShots.empty())
1684 return;
1685 strDest = vOneShots.front();
1686 vOneShots.pop_front();
1688 CAddress addr;
1689 CSemaphoreGrant grant(*semOutbound, true);
1690 if (grant) {
1691 if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
1692 AddOneShot(strDest);
1696 void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1698 // Connect to specific addresses
1699 if (!connect.empty())
1701 for (int64_t nLoop = 0;; nLoop++)
1703 ProcessOneShot();
1704 for (const std::string& strAddr : connect)
1706 CAddress addr(CService(), NODE_NONE);
1707 OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(), false, false, true);
1708 for (int i = 0; i < 10 && i < nLoop; i++)
1710 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1711 return;
1714 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1715 return;
1719 // Initiate network connections
1720 int64_t nStart = GetTime();
1722 // Minimum time before next feeler connection (in microseconds).
1723 int64_t nNextFeeler = PoissonNextSend(nStart*1000*1000, FEELER_INTERVAL);
1724 while (!interruptNet)
1726 ProcessOneShot();
1728 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1729 return;
1731 CSemaphoreGrant grant(*semOutbound);
1732 if (interruptNet)
1733 return;
1735 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1736 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1737 static bool done = false;
1738 if (!done) {
1739 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1740 CNetAddr local;
1741 local.SetInternal("fixedseeds");
1742 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1743 done = true;
1748 // Choose an address to connect to based on most recently seen
1750 CAddress addrConnect;
1752 // Only connect out to one peer per network group (/16 for IPv4).
1753 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1754 int nOutbound = 0;
1755 std::set<std::vector<unsigned char> > setConnected;
1757 LOCK(cs_vNodes);
1758 for (CNode* pnode : vNodes) {
1759 if (!pnode->fInbound && !pnode->m_manual_connection) {
1760 // Netgroups for inbound and addnode peers are not excluded because our goal here
1761 // is to not use multiple of our limited outbound slots on a single netgroup
1762 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1763 // also have the added issue that they're attacker controlled and could be used
1764 // to prevent us from connecting to particular hosts if we used them here.
1765 setConnected.insert(pnode->addr.GetGroup());
1766 nOutbound++;
1771 // Feeler Connections
1773 // Design goals:
1774 // * Increase the number of connectable addresses in the tried table.
1776 // Method:
1777 // * Choose a random address from new and attempt to connect to it if we can connect
1778 // successfully it is added to tried.
1779 // * Start attempting feeler connections only after node finishes making outbound
1780 // connections.
1781 // * Only make a feeler connection once every few minutes.
1783 bool fFeeler = false;
1784 if (nOutbound >= nMaxOutbound) {
1785 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1786 if (nTime > nNextFeeler) {
1787 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1788 fFeeler = true;
1789 } else {
1790 continue;
1794 int64_t nANow = GetAdjustedTime();
1795 int nTries = 0;
1796 while (!interruptNet)
1798 CAddrInfo addr = addrman.Select(fFeeler);
1800 // if we selected an invalid address, restart
1801 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1802 break;
1804 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1805 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1806 // already-connected network ranges, ...) before trying new addrman addresses.
1807 nTries++;
1808 if (nTries > 100)
1809 break;
1811 if (IsLimited(addr))
1812 continue;
1814 // only consider very recently tried nodes after 30 failed attempts
1815 if (nANow - addr.nLastTry < 600 && nTries < 30)
1816 continue;
1818 // for non-feelers, require all the services we'll want,
1819 // for feelers, only require they be a full node (only because most
1820 // SPV clients don't have a good address DB available)
1821 if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
1822 continue;
1823 } else if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
1824 continue;
1827 // do not allow non-default ports, unless after 50 invalid addresses selected already
1828 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1829 continue;
1831 addrConnect = addr;
1832 break;
1835 if (addrConnect.IsValid()) {
1837 if (fFeeler) {
1838 // Add small amount of random noise before connection to avoid synchronization.
1839 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1840 if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1841 return;
1842 LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
1845 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler);
1850 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1852 std::vector<AddedNodeInfo> ret;
1854 std::list<std::string> lAddresses(0);
1856 LOCK(cs_vAddedNodes);
1857 ret.reserve(vAddedNodes.size());
1858 std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses));
1862 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1863 std::map<CService, bool> mapConnected;
1864 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
1866 LOCK(cs_vNodes);
1867 for (const CNode* pnode : vNodes) {
1868 if (pnode->addr.IsValid()) {
1869 mapConnected[pnode->addr] = pnode->fInbound;
1871 std::string addrName = pnode->GetAddrName();
1872 if (!addrName.empty()) {
1873 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1878 for (const std::string& strAddNode : lAddresses) {
1879 CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1880 if (service.IsValid()) {
1881 // strAddNode is an IP:port
1882 auto it = mapConnected.find(service);
1883 if (it != mapConnected.end()) {
1884 ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1885 } else {
1886 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1888 } else {
1889 // strAddNode is a name
1890 auto it = mapConnectedByName.find(strAddNode);
1891 if (it != mapConnectedByName.end()) {
1892 ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1893 } else {
1894 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1899 return ret;
1902 void CConnman::ThreadOpenAddedConnections()
1904 while (true)
1906 CSemaphoreGrant grant(*semAddnode);
1907 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1908 bool tried = false;
1909 for (const AddedNodeInfo& info : vInfo) {
1910 if (!info.fConnected) {
1911 if (!grant.TryAcquire()) {
1912 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1913 // the addednodeinfo state might change.
1914 break;
1916 tried = true;
1917 CAddress addr(CService(), NODE_NONE);
1918 OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), false, false, true);
1919 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1920 return;
1923 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1924 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
1925 return;
1929 // if successful, this moves the passed grant to the constructed node
1930 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool manual_connection)
1933 // Initiate outbound network connection
1935 if (interruptNet) {
1936 return false;
1938 if (!fNetworkActive) {
1939 return false;
1941 if (!pszDest) {
1942 if (IsLocal(addrConnect) ||
1943 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1944 FindNode(addrConnect.ToStringIPPort()))
1945 return false;
1946 } else if (FindNode(std::string(pszDest)))
1947 return false;
1949 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1951 if (!pnode)
1952 return false;
1953 if (grantOutbound)
1954 grantOutbound->MoveTo(pnode->grantOutbound);
1955 if (fOneShot)
1956 pnode->fOneShot = true;
1957 if (fFeeler)
1958 pnode->fFeeler = true;
1959 if (manual_connection)
1960 pnode->m_manual_connection = true;
1962 m_msgproc->InitializeNode(pnode);
1964 LOCK(cs_vNodes);
1965 vNodes.push_back(pnode);
1968 return true;
1971 void CConnman::ThreadMessageHandler()
1973 while (!flagInterruptMsgProc)
1975 std::vector<CNode*> vNodesCopy;
1977 LOCK(cs_vNodes);
1978 vNodesCopy = vNodes;
1979 for (CNode* pnode : vNodesCopy) {
1980 pnode->AddRef();
1984 bool fMoreWork = false;
1986 for (CNode* pnode : vNodesCopy)
1988 if (pnode->fDisconnect)
1989 continue;
1991 // Receive messages
1992 bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
1993 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
1994 if (flagInterruptMsgProc)
1995 return;
1996 // Send messages
1998 LOCK(pnode->cs_sendProcessing);
1999 m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
2002 if (flagInterruptMsgProc)
2003 return;
2007 LOCK(cs_vNodes);
2008 for (CNode* pnode : vNodesCopy)
2009 pnode->Release();
2012 std::unique_lock<std::mutex> lock(mutexMsgProc);
2013 if (!fMoreWork) {
2014 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
2016 fMsgProcWake = false;
2025 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
2027 strError = "";
2028 int nOne = 1;
2030 // Create socket for listening for incoming connections
2031 struct sockaddr_storage sockaddr;
2032 socklen_t len = sizeof(sockaddr);
2033 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
2035 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
2036 LogPrintf("%s\n", strError);
2037 return false;
2040 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2041 if (hListenSocket == INVALID_SOCKET)
2043 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2044 LogPrintf("%s\n", strError);
2045 return false;
2047 if (!IsSelectableSocket(hListenSocket))
2049 strError = "Error: Couldn't create a listenable socket for incoming connections";
2050 LogPrintf("%s\n", strError);
2051 return false;
2055 #ifndef WIN32
2056 #ifdef SO_NOSIGPIPE
2057 // Different way of disabling SIGPIPE on BSD
2058 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
2059 #endif
2060 // Allow binding if the port is still in TIME_WAIT state after
2061 // the program was closed and restarted.
2062 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2063 // Disable Nagle's algorithm
2064 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
2065 #else
2066 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2067 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
2068 #endif
2070 // Set to non-blocking, incoming connections will also inherit this
2071 if (!SetSocketNonBlocking(hListenSocket, true)) {
2072 CloseSocket(hListenSocket);
2073 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2074 LogPrintf("%s\n", strError);
2075 return false;
2078 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2079 // and enable it by default or not. Try to enable it, if possible.
2080 if (addrBind.IsIPv6()) {
2081 #ifdef IPV6_V6ONLY
2082 #ifdef WIN32
2083 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2084 #else
2085 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2086 #endif
2087 #endif
2088 #ifdef WIN32
2089 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2090 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2091 #endif
2094 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2096 int nErr = WSAGetLastError();
2097 if (nErr == WSAEADDRINUSE)
2098 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
2099 else
2100 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2101 LogPrintf("%s\n", strError);
2102 CloseSocket(hListenSocket);
2103 return false;
2105 LogPrintf("Bound to %s\n", addrBind.ToString());
2107 // Listen for incoming connections
2108 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
2110 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2111 LogPrintf("%s\n", strError);
2112 CloseSocket(hListenSocket);
2113 return false;
2116 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
2118 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
2119 AddLocal(addrBind, LOCAL_BIND);
2121 return true;
2124 void Discover(boost::thread_group& threadGroup)
2126 if (!fDiscover)
2127 return;
2129 #ifdef WIN32
2130 // Get local host IP
2131 char pszHostName[256] = "";
2132 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
2134 std::vector<CNetAddr> vaddr;
2135 if (LookupHost(pszHostName, vaddr, 0, true))
2137 for (const CNetAddr &addr : vaddr)
2139 if (AddLocal(addr, LOCAL_IF))
2140 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
2144 #else
2145 // Get local host ip
2146 struct ifaddrs* myaddrs;
2147 if (getifaddrs(&myaddrs) == 0)
2149 for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
2151 if (ifa->ifa_addr == nullptr) continue;
2152 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2153 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2154 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2155 if (ifa->ifa_addr->sa_family == AF_INET)
2157 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2158 CNetAddr addr(s4->sin_addr);
2159 if (AddLocal(addr, LOCAL_IF))
2160 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2162 else if (ifa->ifa_addr->sa_family == AF_INET6)
2164 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2165 CNetAddr addr(s6->sin6_addr);
2166 if (AddLocal(addr, LOCAL_IF))
2167 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2170 freeifaddrs(myaddrs);
2172 #endif
2175 void CConnman::SetNetworkActive(bool active)
2177 LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2179 if (fNetworkActive == active) {
2180 return;
2183 fNetworkActive = active;
2185 if (!fNetworkActive) {
2186 LOCK(cs_vNodes);
2187 // Close sockets to all nodes
2188 for (CNode* pnode : vNodes) {
2189 pnode->CloseSocketDisconnect();
2193 uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2196 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2198 fNetworkActive = true;
2199 setBannedIsDirty = false;
2200 fAddressesInitialized = false;
2201 nLastNodeId = 0;
2202 nSendBufferMaxSize = 0;
2203 nReceiveFloodSize = 0;
2204 semOutbound = nullptr;
2205 semAddnode = nullptr;
2206 flagInterruptMsgProc = false;
2208 Options connOptions;
2209 Init(connOptions);
2212 NodeId CConnman::GetNewNodeId()
2214 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2218 bool CConnman::Bind(const CService &addr, unsigned int flags) {
2219 if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2220 return false;
2221 std::string strError;
2222 if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
2223 if ((flags & BF_REPORT_ERROR) && clientInterface) {
2224 clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
2226 return false;
2228 return true;
2231 bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2232 bool fBound = false;
2233 for (const auto& addrBind : binds) {
2234 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2236 for (const auto& addrBind : whiteBinds) {
2237 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
2239 if (binds.empty() && whiteBinds.empty()) {
2240 struct in_addr inaddr_any;
2241 inaddr_any.s_addr = INADDR_ANY;
2242 fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
2243 fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2245 return fBound;
2248 bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2250 Init(connOptions);
2252 nTotalBytesRecv = 0;
2253 nTotalBytesSent = 0;
2254 nMaxOutboundTotalBytesSentInCycle = 0;
2255 nMaxOutboundCycleStartTime = 0;
2257 if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2258 if (clientInterface) {
2259 clientInterface->ThreadSafeMessageBox(
2260 _("Failed to listen on any port. Use -listen=0 if you want this."),
2261 "", CClientUIInterface::MSG_ERROR);
2263 return false;
2266 for (const auto& strDest : connOptions.vSeedNodes) {
2267 AddOneShot(strDest);
2270 if (clientInterface) {
2271 clientInterface->InitMessage(_("Loading P2P addresses..."));
2273 // Load addresses from peers.dat
2274 int64_t nStart = GetTimeMillis();
2276 CAddrDB adb;
2277 if (adb.Read(addrman))
2278 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2279 else {
2280 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2281 LogPrintf("Invalid or missing peers.dat; recreating\n");
2282 DumpAddresses();
2285 if (clientInterface)
2286 clientInterface->InitMessage(_("Loading banlist..."));
2287 // Load addresses from banlist.dat
2288 nStart = GetTimeMillis();
2289 CBanDB bandb;
2290 banmap_t banmap;
2291 if (bandb.Read(banmap)) {
2292 SetBanned(banmap); // thread save setter
2293 SetBannedSetDirty(false); // no need to write down, just read data
2294 SweepBanned(); // sweep out unused entries
2296 LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2297 banmap.size(), GetTimeMillis() - nStart);
2298 } else {
2299 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2300 SetBannedSetDirty(true); // force write
2301 DumpBanlist();
2304 uiInterface.InitMessage(_("Starting network threads..."));
2306 fAddressesInitialized = true;
2308 if (semOutbound == nullptr) {
2309 // initialize semaphore
2310 semOutbound = new CSemaphore(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
2312 if (semAddnode == nullptr) {
2313 // initialize semaphore
2314 semAddnode = new CSemaphore(nMaxAddnode);
2318 // Start threads
2320 assert(m_msgproc);
2321 InterruptSocks5(false);
2322 interruptNet.reset();
2323 flagInterruptMsgProc = false;
2326 std::unique_lock<std::mutex> lock(mutexMsgProc);
2327 fMsgProcWake = false;
2330 // Send and receive from sockets, accept connections
2331 threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2333 if (!gArgs.GetBoolArg("-dnsseed", true))
2334 LogPrintf("DNS seeding disabled\n");
2335 else
2336 threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2338 // Initiate outbound connections from -addnode
2339 threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2341 if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
2342 if (clientInterface) {
2343 clientInterface->ThreadSafeMessageBox(
2344 _("Cannot provide specific connections and have addrman find outgoing connections at the same."),
2345 "", CClientUIInterface::MSG_ERROR);
2347 return false;
2349 if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
2350 threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
2352 // Process messages
2353 threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2355 // Dump network addresses
2356 scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
2358 return true;
2361 class CNetCleanup
2363 public:
2364 CNetCleanup() {}
2366 ~CNetCleanup()
2368 #ifdef WIN32
2369 // Shutdown Windows Sockets
2370 WSACleanup();
2371 #endif
2374 instance_of_cnetcleanup;
2376 void CConnman::Interrupt()
2379 std::lock_guard<std::mutex> lock(mutexMsgProc);
2380 flagInterruptMsgProc = true;
2382 condMsgProc.notify_all();
2384 interruptNet();
2385 InterruptSocks5(true);
2387 if (semOutbound) {
2388 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++) {
2389 semOutbound->post();
2393 if (semAddnode) {
2394 for (int i=0; i<nMaxAddnode; i++) {
2395 semAddnode->post();
2400 void CConnman::Stop()
2402 if (threadMessageHandler.joinable())
2403 threadMessageHandler.join();
2404 if (threadOpenConnections.joinable())
2405 threadOpenConnections.join();
2406 if (threadOpenAddedConnections.joinable())
2407 threadOpenAddedConnections.join();
2408 if (threadDNSAddressSeed.joinable())
2409 threadDNSAddressSeed.join();
2410 if (threadSocketHandler.joinable())
2411 threadSocketHandler.join();
2413 if (fAddressesInitialized)
2415 DumpData();
2416 fAddressesInitialized = false;
2419 // Close sockets
2420 for (CNode* pnode : vNodes)
2421 pnode->CloseSocketDisconnect();
2422 for (ListenSocket& hListenSocket : vhListenSocket)
2423 if (hListenSocket.socket != INVALID_SOCKET)
2424 if (!CloseSocket(hListenSocket.socket))
2425 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2427 // clean up some globals (to help leak detection)
2428 for (CNode *pnode : vNodes) {
2429 DeleteNode(pnode);
2431 for (CNode *pnode : vNodesDisconnected) {
2432 DeleteNode(pnode);
2434 vNodes.clear();
2435 vNodesDisconnected.clear();
2436 vhListenSocket.clear();
2437 delete semOutbound;
2438 semOutbound = nullptr;
2439 delete semAddnode;
2440 semAddnode = nullptr;
2443 void CConnman::DeleteNode(CNode* pnode)
2445 assert(pnode);
2446 bool fUpdateConnectionTime = false;
2447 m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2448 if(fUpdateConnectionTime) {
2449 addrman.Connected(pnode->addr);
2451 delete pnode;
2454 CConnman::~CConnman()
2456 Interrupt();
2457 Stop();
2460 size_t CConnman::GetAddressCount() const
2462 return addrman.size();
2465 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2467 addrman.SetServices(addr, nServices);
2470 void CConnman::MarkAddressGood(const CAddress& addr)
2472 addrman.Good(addr);
2475 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2477 addrman.Add(vAddr, addrFrom, nTimePenalty);
2480 std::vector<CAddress> CConnman::GetAddresses()
2482 return addrman.GetAddr();
2485 bool CConnman::AddNode(const std::string& strNode)
2487 LOCK(cs_vAddedNodes);
2488 for (const std::string& it : vAddedNodes) {
2489 if (strNode == it) return false;
2492 vAddedNodes.push_back(strNode);
2493 return true;
2496 bool CConnman::RemoveAddedNode(const std::string& strNode)
2498 LOCK(cs_vAddedNodes);
2499 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2500 if (strNode == *it) {
2501 vAddedNodes.erase(it);
2502 return true;
2505 return false;
2508 size_t CConnman::GetNodeCount(NumConnections flags)
2510 LOCK(cs_vNodes);
2511 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2512 return vNodes.size();
2514 int nNum = 0;
2515 for (const auto& pnode : vNodes) {
2516 if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
2517 nNum++;
2521 return nNum;
2524 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2526 vstats.clear();
2527 LOCK(cs_vNodes);
2528 vstats.reserve(vNodes.size());
2529 for (CNode* pnode : vNodes) {
2530 vstats.emplace_back();
2531 pnode->copyStats(vstats.back());
2535 bool CConnman::DisconnectNode(const std::string& strNode)
2537 LOCK(cs_vNodes);
2538 if (CNode* pnode = FindNode(strNode)) {
2539 pnode->fDisconnect = true;
2540 return true;
2542 return false;
2544 bool CConnman::DisconnectNode(NodeId id)
2546 LOCK(cs_vNodes);
2547 for(CNode* pnode : vNodes) {
2548 if (id == pnode->GetId()) {
2549 pnode->fDisconnect = true;
2550 return true;
2553 return false;
2556 void CConnman::RecordBytesRecv(uint64_t bytes)
2558 LOCK(cs_totalBytesRecv);
2559 nTotalBytesRecv += bytes;
2562 void CConnman::RecordBytesSent(uint64_t bytes)
2564 LOCK(cs_totalBytesSent);
2565 nTotalBytesSent += bytes;
2567 uint64_t now = GetTime();
2568 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2570 // timeframe expired, reset cycle
2571 nMaxOutboundCycleStartTime = now;
2572 nMaxOutboundTotalBytesSentInCycle = 0;
2575 // TODO, exclude whitebind peers
2576 nMaxOutboundTotalBytesSentInCycle += bytes;
2579 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2581 LOCK(cs_totalBytesSent);
2582 nMaxOutboundLimit = limit;
2585 uint64_t CConnman::GetMaxOutboundTarget()
2587 LOCK(cs_totalBytesSent);
2588 return nMaxOutboundLimit;
2591 uint64_t CConnman::GetMaxOutboundTimeframe()
2593 LOCK(cs_totalBytesSent);
2594 return nMaxOutboundTimeframe;
2597 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2599 LOCK(cs_totalBytesSent);
2600 if (nMaxOutboundLimit == 0)
2601 return 0;
2603 if (nMaxOutboundCycleStartTime == 0)
2604 return nMaxOutboundTimeframe;
2606 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2607 uint64_t now = GetTime();
2608 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2611 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2613 LOCK(cs_totalBytesSent);
2614 if (nMaxOutboundTimeframe != timeframe)
2616 // reset measure-cycle in case of changing
2617 // the timeframe
2618 nMaxOutboundCycleStartTime = GetTime();
2620 nMaxOutboundTimeframe = timeframe;
2623 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2625 LOCK(cs_totalBytesSent);
2626 if (nMaxOutboundLimit == 0)
2627 return false;
2629 if (historicalBlockServingLimit)
2631 // keep a large enough buffer to at least relay each block once
2632 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2633 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2634 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2635 return true;
2637 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2638 return true;
2640 return false;
2643 uint64_t CConnman::GetOutboundTargetBytesLeft()
2645 LOCK(cs_totalBytesSent);
2646 if (nMaxOutboundLimit == 0)
2647 return 0;
2649 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2652 uint64_t CConnman::GetTotalBytesRecv()
2654 LOCK(cs_totalBytesRecv);
2655 return nTotalBytesRecv;
2658 uint64_t CConnman::GetTotalBytesSent()
2660 LOCK(cs_totalBytesSent);
2661 return nTotalBytesSent;
2664 ServiceFlags CConnman::GetLocalServices() const
2666 return nLocalServices;
2669 void CConnman::SetBestHeight(int height)
2671 nBestHeight.store(height, std::memory_order_release);
2674 int CConnman::GetBestHeight() const
2676 return nBestHeight.load(std::memory_order_acquire);
2679 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2681 CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string& addrNameIn, bool fInboundIn) :
2682 nTimeConnected(GetSystemTimeInSeconds()),
2683 addr(addrIn),
2684 addrBind(addrBindIn),
2685 fInbound(fInboundIn),
2686 nKeyedNetGroup(nKeyedNetGroupIn),
2687 addrKnown(5000, 0.001),
2688 filterInventoryKnown(50000, 0.000001),
2689 id(idIn),
2690 nLocalHostNonce(nLocalHostNonceIn),
2691 nLocalServices(nLocalServicesIn),
2692 nMyStartingHeight(nMyStartingHeightIn),
2693 nSendVersion(0)
2695 nServices = NODE_NONE;
2696 hSocket = hSocketIn;
2697 nRecvVersion = INIT_PROTO_VERSION;
2698 nLastSend = 0;
2699 nLastRecv = 0;
2700 nSendBytes = 0;
2701 nRecvBytes = 0;
2702 nTimeOffset = 0;
2703 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2704 nVersion = 0;
2705 strSubVer = "";
2706 fWhitelisted = false;
2707 fOneShot = false;
2708 m_manual_connection = false;
2709 fClient = false; // set by version message
2710 fFeeler = false;
2711 fSuccessfullyConnected = false;
2712 fDisconnect = false;
2713 nRefCount = 0;
2714 nSendSize = 0;
2715 nSendOffset = 0;
2716 hashContinue = uint256();
2717 nStartingHeight = -1;
2718 filterInventoryKnown.reset();
2719 fSendMempool = false;
2720 fGetAddr = false;
2721 nNextLocalAddrSend = 0;
2722 nNextAddrSend = 0;
2723 nNextInvSend = 0;
2724 fRelayTxes = false;
2725 fSentAddr = false;
2726 pfilter = new CBloomFilter();
2727 timeLastMempoolReq = 0;
2728 nLastBlockTime = 0;
2729 nLastTXTime = 0;
2730 nPingNonceSent = 0;
2731 nPingUsecStart = 0;
2732 nPingUsecTime = 0;
2733 fPingQueued = false;
2734 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2735 minFeeFilter = 0;
2736 lastSentFeeFilter = 0;
2737 nextSendTimeFeeFilter = 0;
2738 fPauseRecv = false;
2739 fPauseSend = false;
2740 nProcessQueueSize = 0;
2742 for (const std::string &msg : getAllNetMessageTypes())
2743 mapRecvBytesPerMsgCmd[msg] = 0;
2744 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2746 if (fLogIPs) {
2747 LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
2748 } else {
2749 LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
2753 CNode::~CNode()
2755 CloseSocket(hSocket);
2757 if (pfilter)
2758 delete pfilter;
2761 void CNode::AskFor(const CInv& inv)
2763 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2764 return;
2765 // a peer may not have multiple non-responded queue positions for a single inv item
2766 if (!setAskFor.insert(inv.hash).second)
2767 return;
2769 // We're using mapAskFor as a priority queue,
2770 // the key is the earliest time the request can be sent
2771 int64_t nRequestTime;
2772 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2773 if (it != mapAlreadyAskedFor.end())
2774 nRequestTime = it->second;
2775 else
2776 nRequestTime = 0;
2777 LogPrint(BCLog::NET, "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2779 // Make sure not to reuse time indexes to keep things in the same order
2780 int64_t nNow = GetTimeMicros() - 1000000;
2781 static int64_t nLastTime;
2782 ++nLastTime;
2783 nNow = std::max(nNow, nLastTime);
2784 nLastTime = nNow;
2786 // Each retry is 2 minutes after the last
2787 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2788 if (it != mapAlreadyAskedFor.end())
2789 mapAlreadyAskedFor.update(it, nRequestTime);
2790 else
2791 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2792 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2795 bool CConnman::NodeFullyConnected(const CNode* pnode)
2797 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
2800 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2802 size_t nMessageSize = msg.data.size();
2803 size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2804 LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2806 std::vector<unsigned char> serializedHeader;
2807 serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
2808 uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
2809 CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize);
2810 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
2812 CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
2814 size_t nBytesSent = 0;
2816 LOCK(pnode->cs_vSend);
2817 bool optimisticSend(pnode->vSendMsg.empty());
2819 //log total amount of bytes per command
2820 pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
2821 pnode->nSendSize += nTotalSize;
2823 if (pnode->nSendSize > nSendBufferMaxSize)
2824 pnode->fPauseSend = true;
2825 pnode->vSendMsg.push_back(std::move(serializedHeader));
2826 if (nMessageSize)
2827 pnode->vSendMsg.push_back(std::move(msg.data));
2829 // If write queue empty, attempt "optimistic write"
2830 if (optimisticSend == true)
2831 nBytesSent = SocketSendData(pnode);
2833 if (nBytesSent)
2834 RecordBytesSent(nBytesSent);
2837 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2839 CNode* found = nullptr;
2840 LOCK(cs_vNodes);
2841 for (auto&& pnode : vNodes) {
2842 if(pnode->GetId() == id) {
2843 found = pnode;
2844 break;
2847 return found != nullptr && NodeFullyConnected(found) && func(found);
2850 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2851 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2854 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
2856 return CSipHasher(nSeed0, nSeed1).Write(id);
2859 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
2861 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2863 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();