[tests] remove redundant univalue_tests.cpp
[bitcoinplatinum.git] / src / net.cpp
blobc0f31e83dd78636e08b20933fc5036f0ea8d9a02
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 <chainparams.h>
13 #include <clientversion.h>
14 #include <consensus/consensus.h>
15 #include <crypto/common.h>
16 #include <crypto/sha256.h>
17 #include <primitives/transaction.h>
18 #include <netbase.h>
19 #include <scheduler.h>
20 #include <ui_interface.h>
21 #include <utilstrencodings.h>
23 #ifdef WIN32
24 #include <string.h>
25 #else
26 #include <fcntl.h>
27 #endif
29 #ifdef USE_UPNP
30 #include <miniupnpc/miniupnpc.h>
31 #include <miniupnpc/miniwget.h>
32 #include <miniupnpc/upnpcommands.h>
33 #include <miniupnpc/upnperrors.h>
34 #endif
37 #include <math.h>
39 // Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
40 #define DUMP_ADDRESSES_INTERVAL 900
42 // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
43 #define FEELER_SLEEP_WINDOW 1
45 #if !defined(HAVE_MSG_NOSIGNAL)
46 #define MSG_NOSIGNAL 0
47 #endif
49 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
50 #if !defined(HAVE_MSG_DONTWAIT)
51 #define MSG_DONTWAIT 0
52 #endif
54 // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
55 // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
56 #ifdef WIN32
57 #ifndef PROTECTION_LEVEL_UNRESTRICTED
58 #define PROTECTION_LEVEL_UNRESTRICTED 10
59 #endif
60 #ifndef IPV6_PROTECTION_LEVEL
61 #define IPV6_PROTECTION_LEVEL 23
62 #endif
63 #endif
65 /** Used to pass flags to the Bind() function */
66 enum BindFlags {
67 BF_NONE = 0,
68 BF_EXPLICIT = (1U << 0),
69 BF_REPORT_ERROR = (1U << 1),
70 BF_WHITELIST = (1U << 2),
73 const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
75 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
76 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // SHA256("localhostnonce")[0:8]
78 // Global state variables
80 bool fDiscover = true;
81 bool fListen = true;
82 bool fRelayTxes = true;
83 CCriticalSection cs_mapLocalHost;
84 std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
85 static bool vfLimited[NET_MAX] = {};
86 std::string strSubVersion;
88 limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
90 void CConnman::AddOneShot(const std::string& strDest)
92 LOCK(cs_vOneShots);
93 vOneShots.push_back(strDest);
96 unsigned short GetListenPort()
98 return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
101 // find 'best' local address for a particular peer
102 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
104 if (!fListen)
105 return false;
107 int nBestScore = -1;
108 int nBestReachability = -1;
110 LOCK(cs_mapLocalHost);
111 for (const auto& entry : mapLocalHost)
113 int nScore = entry.second.nScore;
114 int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
115 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
117 addr = CService(entry.first, entry.second.nPort);
118 nBestReachability = nReachability;
119 nBestScore = nScore;
123 return nBestScore >= 0;
126 //! Convert the pnSeeds6 array into usable address objects.
127 static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
129 // It'll only connect to one or two seed nodes because once it connects,
130 // it'll get a pile of addresses with newer timestamps.
131 // Seed nodes are given a random 'last seen time' of between one and two
132 // weeks ago.
133 const int64_t nOneWeek = 7*24*60*60;
134 std::vector<CAddress> vSeedsOut;
135 vSeedsOut.reserve(vSeedsIn.size());
136 for (const auto& seed_in : vSeedsIn) {
137 struct in6_addr ip;
138 memcpy(&ip, seed_in.addr, sizeof(ip));
139 CAddress addr(CService(ip, seed_in.port), NODE_NETWORK);
140 addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
141 vSeedsOut.push_back(addr);
143 return vSeedsOut;
146 // get best local address for a particular peer as a CAddress
147 // Otherwise, return the unroutable 0.0.0.0 but filled in with
148 // the normal parameters, since the IP may be changed to a useful
149 // one by discovery.
150 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
152 CAddress ret(CService(CNetAddr(),GetListenPort()), nLocalServices);
153 CService addr;
154 if (GetLocal(addr, paddrPeer))
156 ret = CAddress(addr, nLocalServices);
158 ret.nTime = GetAdjustedTime();
159 return ret;
162 int GetnScore(const CService& addr)
164 LOCK(cs_mapLocalHost);
165 if (mapLocalHost.count(addr) == LOCAL_NONE)
166 return 0;
167 return mapLocalHost[addr].nScore;
170 // Is our peer's addrLocal potentially useful as an external IP source?
171 bool IsPeerAddrLocalGood(CNode *pnode)
173 CService addrLocal = pnode->GetAddrLocal();
174 return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
175 !IsLimited(addrLocal.GetNetwork());
178 // pushes our own address to a peer
179 void AdvertiseLocal(CNode *pnode)
181 if (fListen && pnode->fSuccessfullyConnected)
183 CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
184 // If discovery is enabled, sometimes give our peer the address it
185 // tells us that it sees us as in case it has a better idea of our
186 // address than we do.
187 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
188 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
190 addrLocal.SetIP(pnode->GetAddrLocal());
192 if (addrLocal.IsRoutable())
194 LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
195 FastRandomContext insecure_rand;
196 pnode->PushAddress(addrLocal, insecure_rand);
201 // learn a new local address
202 bool AddLocal(const CService& addr, int nScore)
204 if (!addr.IsRoutable())
205 return false;
207 if (!fDiscover && nScore < LOCAL_MANUAL)
208 return false;
210 if (IsLimited(addr))
211 return false;
213 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
216 LOCK(cs_mapLocalHost);
217 bool fAlready = mapLocalHost.count(addr) > 0;
218 LocalServiceInfo &info = mapLocalHost[addr];
219 if (!fAlready || nScore >= info.nScore) {
220 info.nScore = nScore + (fAlready ? 1 : 0);
221 info.nPort = addr.GetPort();
225 return true;
228 bool AddLocal(const CNetAddr &addr, int nScore)
230 return AddLocal(CService(addr, GetListenPort()), nScore);
233 bool RemoveLocal(const CService& addr)
235 LOCK(cs_mapLocalHost);
236 LogPrintf("RemoveLocal(%s)\n", addr.ToString());
237 mapLocalHost.erase(addr);
238 return true;
241 /** Make a particular network entirely off-limits (no automatic connects to it) */
242 void SetLimited(enum Network net, bool fLimited)
244 if (net == NET_UNROUTABLE || net == NET_INTERNAL)
245 return;
246 LOCK(cs_mapLocalHost);
247 vfLimited[net] = fLimited;
250 bool IsLimited(enum Network net)
252 LOCK(cs_mapLocalHost);
253 return vfLimited[net];
256 bool IsLimited(const CNetAddr &addr)
258 return IsLimited(addr.GetNetwork());
261 /** vote for a local address */
262 bool SeenLocal(const CService& addr)
265 LOCK(cs_mapLocalHost);
266 if (mapLocalHost.count(addr) == 0)
267 return false;
268 mapLocalHost[addr].nScore++;
270 return true;
274 /** check whether a given address is potentially local */
275 bool IsLocal(const CService& addr)
277 LOCK(cs_mapLocalHost);
278 return mapLocalHost.count(addr) > 0;
281 /** check whether a given network is one we can probably connect to */
282 bool IsReachable(enum Network net)
284 LOCK(cs_mapLocalHost);
285 return !vfLimited[net];
288 /** check whether a given address is in a network we can probably connect to */
289 bool IsReachable(const CNetAddr& addr)
291 enum Network net = addr.GetNetwork();
292 return IsReachable(net);
296 CNode* CConnman::FindNode(const CNetAddr& ip)
298 LOCK(cs_vNodes);
299 for (CNode* pnode : vNodes) {
300 if ((CNetAddr)pnode->addr == ip) {
301 return pnode;
304 return nullptr;
307 CNode* CConnman::FindNode(const CSubNet& subNet)
309 LOCK(cs_vNodes);
310 for (CNode* pnode : vNodes) {
311 if (subNet.Match((CNetAddr)pnode->addr)) {
312 return pnode;
315 return nullptr;
318 CNode* CConnman::FindNode(const std::string& addrName)
320 LOCK(cs_vNodes);
321 for (CNode* pnode : vNodes) {
322 if (pnode->GetAddrName() == addrName) {
323 return pnode;
326 return nullptr;
329 CNode* CConnman::FindNode(const CService& addr)
331 LOCK(cs_vNodes);
332 for (CNode* pnode : vNodes) {
333 if ((CService)pnode->addr == addr) {
334 return pnode;
337 return nullptr;
340 bool CConnman::CheckIncomingNonce(uint64_t nonce)
342 LOCK(cs_vNodes);
343 for (CNode* pnode : vNodes) {
344 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
345 return false;
347 return true;
350 /** Get the bind address for a socket as CAddress */
351 static CAddress GetBindAddress(SOCKET sock)
353 CAddress addr_bind;
354 struct sockaddr_storage sockaddr_bind;
355 socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
356 if (sock != INVALID_SOCKET) {
357 if (!getsockname(sock, (struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
358 addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind);
359 } else {
360 LogPrint(BCLog::NET, "Warning: getsockname failed\n");
363 return addr_bind;
366 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
368 if (pszDest == nullptr) {
369 if (IsLocal(addrConnect))
370 return nullptr;
372 // Look for an existing connection
373 CNode* pnode = FindNode((CService)addrConnect);
374 if (pnode)
376 LogPrintf("Failed to open new connection, already connected\n");
377 return nullptr;
381 /// debug print
382 LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
383 pszDest ? pszDest : addrConnect.ToString(),
384 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
386 // Resolve
387 const int default_port = Params().GetDefaultPort();
388 if (pszDest) {
389 std::vector<CService> resolved;
390 if (Lookup(pszDest, resolved, default_port, fNameLookup && !HaveNameProxy(), 256) && !resolved.empty()) {
391 addrConnect = CAddress(resolved[GetRand(resolved.size())], NODE_NONE);
392 if (!addrConnect.IsValid()) {
393 LogPrint(BCLog::NET, "Resolver returned invalid address %s for %s", addrConnect.ToString(), pszDest);
394 return nullptr;
396 // It is possible that we already have a connection to the IP/port pszDest resolved to.
397 // In that case, drop the connection that was just created, and return the existing CNode instead.
398 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
399 // name catch this early.
400 LOCK(cs_vNodes);
401 CNode* pnode = FindNode((CService)addrConnect);
402 if (pnode)
404 pnode->MaybeSetAddrName(std::string(pszDest));
405 LogPrintf("Failed to open new connection, already connected\n");
406 return nullptr;
411 // Connect
412 bool connected = false;
413 SOCKET hSocket;
414 proxyType proxy;
415 if (addrConnect.IsValid()) {
416 bool proxyConnectionFailed = false;
418 if (GetProxy(addrConnect.GetNetwork(), proxy))
419 connected = ConnectThroughProxy(proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), hSocket, nConnectTimeout, &proxyConnectionFailed);
420 else // no proxy needed (none set for target network)
421 connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout);
422 if (!proxyConnectionFailed) {
423 // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
424 // the proxy, mark this as an attempt.
425 addrman.Attempt(addrConnect, fCountFailure);
427 } else if (pszDest && GetNameProxy(proxy)) {
428 std::string host;
429 int port = default_port;
430 SplitHostPort(std::string(pszDest), port, host);
431 connected = ConnectThroughProxy(proxy, host, port, hSocket, nConnectTimeout, nullptr);
433 if (connected) {
434 if (!IsSelectableSocket(hSocket)) {
435 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
436 CloseSocket(hSocket);
437 return nullptr;
440 // Add node
441 NodeId id = GetNewNodeId();
442 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
443 CAddress addr_bind = GetBindAddress(hSocket);
444 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", false);
445 pnode->AddRef();
447 return pnode;
450 return nullptr;
453 void CConnman::DumpBanlist()
455 SweepBanned(); // clean unused entries (if bantime has expired)
457 if (!BannedSetIsDirty())
458 return;
460 int64_t nStart = GetTimeMillis();
462 CBanDB bandb;
463 banmap_t banmap;
464 GetBanned(banmap);
465 if (bandb.Write(banmap)) {
466 SetBannedSetDirty(false);
469 LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
470 banmap.size(), GetTimeMillis() - nStart);
473 void CNode::CloseSocketDisconnect()
475 fDisconnect = true;
476 LOCK(cs_hSocket);
477 if (hSocket != INVALID_SOCKET)
479 LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
480 CloseSocket(hSocket);
484 void CConnman::ClearBanned()
487 LOCK(cs_setBanned);
488 setBanned.clear();
489 setBannedIsDirty = true;
491 DumpBanlist(); //store banlist to disk
492 if(clientInterface)
493 clientInterface->BannedListChanged();
496 bool CConnman::IsBanned(CNetAddr ip)
498 LOCK(cs_setBanned);
499 for (const auto& it : setBanned) {
500 CSubNet subNet = it.first;
501 CBanEntry banEntry = it.second;
503 if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
504 return true;
507 return false;
510 bool CConnman::IsBanned(CSubNet subnet)
512 LOCK(cs_setBanned);
513 banmap_t::iterator i = setBanned.find(subnet);
514 if (i != setBanned.end())
516 CBanEntry banEntry = (*i).second;
517 if (GetTime() < banEntry.nBanUntil) {
518 return true;
521 return false;
524 void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
525 CSubNet subNet(addr);
526 Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
529 void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
530 CBanEntry banEntry(GetTime());
531 banEntry.banReason = banReason;
532 if (bantimeoffset <= 0)
534 bantimeoffset = gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
535 sinceUnixEpoch = false;
537 banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
540 LOCK(cs_setBanned);
541 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
542 setBanned[subNet] = banEntry;
543 setBannedIsDirty = true;
545 else
546 return;
548 if(clientInterface)
549 clientInterface->BannedListChanged();
551 LOCK(cs_vNodes);
552 for (CNode* pnode : vNodes) {
553 if (subNet.Match((CNetAddr)pnode->addr))
554 pnode->fDisconnect = true;
557 if(banReason == BanReasonManuallyAdded)
558 DumpBanlist(); //store banlist to disk immediately if user requested ban
561 bool CConnman::Unban(const CNetAddr &addr) {
562 CSubNet subNet(addr);
563 return Unban(subNet);
566 bool CConnman::Unban(const CSubNet &subNet) {
568 LOCK(cs_setBanned);
569 if (!setBanned.erase(subNet))
570 return false;
571 setBannedIsDirty = true;
573 if(clientInterface)
574 clientInterface->BannedListChanged();
575 DumpBanlist(); //store banlist to disk immediately
576 return true;
579 void CConnman::GetBanned(banmap_t &banMap)
581 LOCK(cs_setBanned);
582 // Sweep the banlist so expired bans are not returned
583 SweepBanned();
584 banMap = setBanned; //create a thread safe copy
587 void CConnman::SetBanned(const banmap_t &banMap)
589 LOCK(cs_setBanned);
590 setBanned = banMap;
591 setBannedIsDirty = true;
594 void CConnman::SweepBanned()
596 int64_t now = GetTime();
598 LOCK(cs_setBanned);
599 banmap_t::iterator it = setBanned.begin();
600 while(it != setBanned.end())
602 CSubNet subNet = (*it).first;
603 CBanEntry banEntry = (*it).second;
604 if(now > banEntry.nBanUntil)
606 setBanned.erase(it++);
607 setBannedIsDirty = true;
608 LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
610 else
611 ++it;
615 bool CConnman::BannedSetIsDirty()
617 LOCK(cs_setBanned);
618 return setBannedIsDirty;
621 void CConnman::SetBannedSetDirty(bool dirty)
623 LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
624 setBannedIsDirty = dirty;
628 bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
629 for (const CSubNet& subnet : vWhitelistedRange) {
630 if (subnet.Match(addr))
631 return true;
633 return false;
636 std::string CNode::GetAddrName() const {
637 LOCK(cs_addrName);
638 return addrName;
641 void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
642 LOCK(cs_addrName);
643 if (addrName.empty()) {
644 addrName = addrNameIn;
648 CService CNode::GetAddrLocal() const {
649 LOCK(cs_addrLocal);
650 return addrLocal;
653 void CNode::SetAddrLocal(const CService& addrLocalIn) {
654 LOCK(cs_addrLocal);
655 if (addrLocal.IsValid()) {
656 error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
657 } else {
658 addrLocal = addrLocalIn;
662 #undef X
663 #define X(name) stats.name = name
664 void CNode::copyStats(CNodeStats &stats)
666 stats.nodeid = this->GetId();
667 X(nServices);
668 X(addr);
669 X(addrBind);
671 LOCK(cs_filter);
672 X(fRelayTxes);
674 X(nLastSend);
675 X(nLastRecv);
676 X(nTimeConnected);
677 X(nTimeOffset);
678 stats.addrName = GetAddrName();
679 X(nVersion);
681 LOCK(cs_SubVer);
682 X(cleanSubVer);
684 X(fInbound);
685 X(m_manual_connection);
686 X(nStartingHeight);
688 LOCK(cs_vSend);
689 X(mapSendBytesPerMsgCmd);
690 X(nSendBytes);
693 LOCK(cs_vRecv);
694 X(mapRecvBytesPerMsgCmd);
695 X(nRecvBytes);
697 X(fWhitelisted);
699 // It is common for nodes with good ping times to suddenly become lagged,
700 // due to a new block arriving or other large transfer.
701 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
702 // since pingtime does not update until the ping is complete, which might take a while.
703 // So, if a ping is taking an unusually long time in flight,
704 // the caller can immediately detect that this is happening.
705 int64_t nPingUsecWait = 0;
706 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
707 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
710 // 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 :)
711 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
712 stats.dMinPing = (((double)nMinPingUsecTime) / 1e6);
713 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
715 // Leave string empty if addrLocal invalid (not filled in yet)
716 CService addrLocalUnlocked = GetAddrLocal();
717 stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
719 #undef X
721 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
723 complete = false;
724 int64_t nTimeMicros = GetTimeMicros();
725 LOCK(cs_vRecv);
726 nLastRecv = nTimeMicros / 1000000;
727 nRecvBytes += nBytes;
728 while (nBytes > 0) {
730 // get current incomplete message, or create a new one
731 if (vRecvMsg.empty() ||
732 vRecvMsg.back().complete())
733 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
735 CNetMessage& msg = vRecvMsg.back();
737 // absorb network data
738 int handled;
739 if (!msg.in_data)
740 handled = msg.readHeader(pch, nBytes);
741 else
742 handled = msg.readData(pch, nBytes);
744 if (handled < 0)
745 return false;
747 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
748 LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
749 return false;
752 pch += handled;
753 nBytes -= handled;
755 if (msg.complete()) {
757 //store received bytes per message command
758 //to prevent a memory DOS, only allow valid commands
759 mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
760 if (i == mapRecvBytesPerMsgCmd.end())
761 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
762 assert(i != mapRecvBytesPerMsgCmd.end());
763 i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
765 msg.nTime = nTimeMicros;
766 complete = true;
770 return true;
773 void CNode::SetSendVersion(int nVersionIn)
775 // Send version may only be changed in the version message, and
776 // only one version message is allowed per session. We can therefore
777 // treat this value as const and even atomic as long as it's only used
778 // once a version message has been successfully processed. Any attempt to
779 // set this twice is an error.
780 if (nSendVersion != 0) {
781 error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
782 } else {
783 nSendVersion = nVersionIn;
787 int CNode::GetSendVersion() const
789 // The send version should always be explicitly set to
790 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
791 // has been called.
792 if (nSendVersion == 0) {
793 error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
794 return INIT_PROTO_VERSION;
796 return nSendVersion;
800 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
802 // copy data to temporary parsing buffer
803 unsigned int nRemaining = 24 - nHdrPos;
804 unsigned int nCopy = std::min(nRemaining, nBytes);
806 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
807 nHdrPos += nCopy;
809 // if header incomplete, exit
810 if (nHdrPos < 24)
811 return nCopy;
813 // deserialize to CMessageHeader
814 try {
815 hdrbuf >> hdr;
817 catch (const std::exception&) {
818 return -1;
821 // reject messages larger than MAX_SIZE
822 if (hdr.nMessageSize > MAX_SIZE)
823 return -1;
825 // switch state to reading message data
826 in_data = true;
828 return nCopy;
831 int CNetMessage::readData(const char *pch, unsigned int nBytes)
833 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
834 unsigned int nCopy = std::min(nRemaining, nBytes);
836 if (vRecv.size() < nDataPos + nCopy) {
837 // Allocate up to 256 KiB ahead, but never more than the total message size.
838 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
841 hasher.Write((const unsigned char*)pch, nCopy);
842 memcpy(&vRecv[nDataPos], pch, nCopy);
843 nDataPos += nCopy;
845 return nCopy;
848 const uint256& CNetMessage::GetMessageHash() const
850 assert(complete());
851 if (data_hash.IsNull())
852 hasher.Finalize(data_hash.begin());
853 return data_hash;
864 // requires LOCK(cs_vSend)
865 size_t CConnman::SocketSendData(CNode *pnode) const
867 auto it = pnode->vSendMsg.begin();
868 size_t nSentSize = 0;
870 while (it != pnode->vSendMsg.end()) {
871 const auto &data = *it;
872 assert(data.size() > pnode->nSendOffset);
873 int nBytes = 0;
875 LOCK(pnode->cs_hSocket);
876 if (pnode->hSocket == INVALID_SOCKET)
877 break;
878 nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
880 if (nBytes > 0) {
881 pnode->nLastSend = GetSystemTimeInSeconds();
882 pnode->nSendBytes += nBytes;
883 pnode->nSendOffset += nBytes;
884 nSentSize += nBytes;
885 if (pnode->nSendOffset == data.size()) {
886 pnode->nSendOffset = 0;
887 pnode->nSendSize -= data.size();
888 pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
889 it++;
890 } else {
891 // could not send full message; stop sending more
892 break;
894 } else {
895 if (nBytes < 0) {
896 // error
897 int nErr = WSAGetLastError();
898 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
900 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
901 pnode->CloseSocketDisconnect();
904 // couldn't send anything at all
905 break;
909 if (it == pnode->vSendMsg.end()) {
910 assert(pnode->nSendOffset == 0);
911 assert(pnode->nSendSize == 0);
913 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
914 return nSentSize;
917 struct NodeEvictionCandidate
919 NodeId id;
920 int64_t nTimeConnected;
921 int64_t nMinPingUsecTime;
922 int64_t nLastBlockTime;
923 int64_t nLastTXTime;
924 bool fRelevantServices;
925 bool fRelayTxes;
926 bool fBloomFilter;
927 CAddress addr;
928 uint64_t nKeyedNetGroup;
931 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
933 return a.nMinPingUsecTime > b.nMinPingUsecTime;
936 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
938 return a.nTimeConnected > b.nTimeConnected;
941 static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
942 return a.nKeyedNetGroup < b.nKeyedNetGroup;
945 static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
947 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
948 if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
949 if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
950 return a.nTimeConnected > b.nTimeConnected;
953 static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
955 // 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.
956 if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
957 if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
958 if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
959 return a.nTimeConnected > b.nTimeConnected;
963 //! Sort an array by the specified comparator, then erase the last K elements.
964 template<typename T, typename Comparator>
965 static void EraseLastKElements(std::vector<T> &elements, Comparator comparator, size_t k)
967 std::sort(elements.begin(), elements.end(), comparator);
968 size_t eraseSize = std::min(k, elements.size());
969 elements.erase(elements.end() - eraseSize, elements.end());
972 /** Try to find a connection to evict when the node is full.
973 * Extreme care must be taken to avoid opening the node to attacker
974 * triggered network partitioning.
975 * The strategy used here is to protect a small number of peers
976 * for each of several distinct characteristics which are difficult
977 * to forge. In order to partition a node the attacker must be
978 * simultaneously better at all of them than honest peers.
980 bool CConnman::AttemptToEvictConnection()
982 std::vector<NodeEvictionCandidate> vEvictionCandidates;
984 LOCK(cs_vNodes);
986 for (const CNode* node : vNodes) {
987 if (node->fWhitelisted)
988 continue;
989 if (!node->fInbound)
990 continue;
991 if (node->fDisconnect)
992 continue;
993 NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
994 node->nLastBlockTime, node->nLastTXTime,
995 HasAllDesirableServiceFlags(node->nServices),
996 node->fRelayTxes, node->pfilter != nullptr, node->addr, node->nKeyedNetGroup};
997 vEvictionCandidates.push_back(candidate);
1001 // Protect connections with certain characteristics
1003 // Deterministically select 4 peers to protect by netgroup.
1004 // An attacker cannot predict which netgroups will be protected
1005 EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
1006 // Protect the 8 nodes with the lowest minimum ping time.
1007 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
1008 EraseLastKElements(vEvictionCandidates, ReverseCompareNodeMinPingTime, 8);
1009 // Protect 4 nodes that most recently sent us transactions.
1010 // An attacker cannot manipulate this metric without performing useful work.
1011 EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4);
1012 // Protect 4 nodes that most recently sent us blocks.
1013 // An attacker cannot manipulate this metric without performing useful work.
1014 EraseLastKElements(vEvictionCandidates, CompareNodeBlockTime, 4);
1015 // Protect the half of the remaining nodes which have been connected the longest.
1016 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1017 EraseLastKElements(vEvictionCandidates, ReverseCompareNodeTimeConnected, vEvictionCandidates.size() / 2);
1019 if (vEvictionCandidates.empty()) return false;
1021 // Identify the network group with the most connections and youngest member.
1022 // (vEvictionCandidates is already sorted by reverse connect time)
1023 uint64_t naMostConnections;
1024 unsigned int nMostConnections = 0;
1025 int64_t nMostConnectionsTime = 0;
1026 std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
1027 for (const NodeEvictionCandidate &node : vEvictionCandidates) {
1028 std::vector<NodeEvictionCandidate> &group = mapNetGroupNodes[node.nKeyedNetGroup];
1029 group.push_back(node);
1030 int64_t grouptime = group[0].nTimeConnected;
1032 if (group.size() > nMostConnections || (group.size() == nMostConnections && grouptime > nMostConnectionsTime)) {
1033 nMostConnections = group.size();
1034 nMostConnectionsTime = grouptime;
1035 naMostConnections = node.nKeyedNetGroup;
1039 // Reduce to the network group with the most connections
1040 vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1042 // Disconnect from the network group with the most connections
1043 NodeId evicted = vEvictionCandidates.front().id;
1044 LOCK(cs_vNodes);
1045 for (CNode* pnode : vNodes) {
1046 if (pnode->GetId() == evicted) {
1047 pnode->fDisconnect = true;
1048 return true;
1051 return false;
1054 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1055 struct sockaddr_storage sockaddr;
1056 socklen_t len = sizeof(sockaddr);
1057 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
1058 CAddress addr;
1059 int nInbound = 0;
1060 int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler);
1062 if (hSocket != INVALID_SOCKET) {
1063 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1064 LogPrintf("Warning: Unknown socket family\n");
1068 bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
1070 LOCK(cs_vNodes);
1071 for (const CNode* pnode : vNodes) {
1072 if (pnode->fInbound) nInbound++;
1076 if (hSocket == INVALID_SOCKET)
1078 int nErr = WSAGetLastError();
1079 if (nErr != WSAEWOULDBLOCK)
1080 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1081 return;
1084 if (!fNetworkActive) {
1085 LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
1086 CloseSocket(hSocket);
1087 return;
1090 if (!IsSelectableSocket(hSocket))
1092 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
1093 CloseSocket(hSocket);
1094 return;
1097 // According to the internet TCP_NODELAY is not carried into accepted sockets
1098 // on all platforms. Set it again here just to be sure.
1099 SetSocketNoDelay(hSocket);
1101 if (IsBanned(addr) && !whitelisted)
1103 LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString());
1104 CloseSocket(hSocket);
1105 return;
1108 if (nInbound >= nMaxInbound)
1110 if (!AttemptToEvictConnection()) {
1111 // No connection to evict, disconnect the new connection
1112 LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1113 CloseSocket(hSocket);
1114 return;
1118 NodeId id = GetNewNodeId();
1119 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1120 CAddress addr_bind = GetBindAddress(hSocket);
1122 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
1123 pnode->AddRef();
1124 pnode->fWhitelisted = whitelisted;
1125 m_msgproc->InitializeNode(pnode);
1127 LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1130 LOCK(cs_vNodes);
1131 vNodes.push_back(pnode);
1135 void CConnman::ThreadSocketHandler()
1137 unsigned int nPrevNodeCount = 0;
1138 while (!interruptNet)
1141 // Disconnect nodes
1144 LOCK(cs_vNodes);
1145 // Disconnect unused nodes
1146 std::vector<CNode*> vNodesCopy = vNodes;
1147 for (CNode* pnode : vNodesCopy)
1149 if (pnode->fDisconnect)
1151 // remove from vNodes
1152 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1154 // release outbound grant (if any)
1155 pnode->grantOutbound.Release();
1157 // close socket and cleanup
1158 pnode->CloseSocketDisconnect();
1160 // hold in disconnected pool until all refs are released
1161 pnode->Release();
1162 vNodesDisconnected.push_back(pnode);
1167 // Delete disconnected nodes
1168 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1169 for (CNode* pnode : vNodesDisconnectedCopy)
1171 // wait until threads are done using it
1172 if (pnode->GetRefCount() <= 0) {
1173 bool fDelete = false;
1175 TRY_LOCK(pnode->cs_inventory, lockInv);
1176 if (lockInv) {
1177 TRY_LOCK(pnode->cs_vSend, lockSend);
1178 if (lockSend) {
1179 fDelete = true;
1183 if (fDelete) {
1184 vNodesDisconnected.remove(pnode);
1185 DeleteNode(pnode);
1190 size_t vNodesSize;
1192 LOCK(cs_vNodes);
1193 vNodesSize = vNodes.size();
1195 if(vNodesSize != nPrevNodeCount) {
1196 nPrevNodeCount = vNodesSize;
1197 if(clientInterface)
1198 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1202 // Find which sockets have data to receive
1204 struct timeval timeout;
1205 timeout.tv_sec = 0;
1206 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1208 fd_set fdsetRecv;
1209 fd_set fdsetSend;
1210 fd_set fdsetError;
1211 FD_ZERO(&fdsetRecv);
1212 FD_ZERO(&fdsetSend);
1213 FD_ZERO(&fdsetError);
1214 SOCKET hSocketMax = 0;
1215 bool have_fds = false;
1217 for (const ListenSocket& hListenSocket : vhListenSocket) {
1218 FD_SET(hListenSocket.socket, &fdsetRecv);
1219 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1220 have_fds = true;
1224 LOCK(cs_vNodes);
1225 for (CNode* pnode : vNodes)
1227 // Implement the following logic:
1228 // * If there is data to send, select() for sending data. As this only
1229 // happens when optimistic write failed, we choose to first drain the
1230 // write buffer in this case before receiving more. This avoids
1231 // needlessly queueing received data, if the remote peer is not themselves
1232 // receiving data. This means properly utilizing TCP flow control signalling.
1233 // * Otherwise, if there is space left in the receive buffer, select() for
1234 // receiving data.
1235 // * Hand off all complete messages to the processor, to be handled without
1236 // blocking here.
1238 bool select_recv = !pnode->fPauseRecv;
1239 bool select_send;
1241 LOCK(pnode->cs_vSend);
1242 select_send = !pnode->vSendMsg.empty();
1245 LOCK(pnode->cs_hSocket);
1246 if (pnode->hSocket == INVALID_SOCKET)
1247 continue;
1249 FD_SET(pnode->hSocket, &fdsetError);
1250 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1251 have_fds = true;
1253 if (select_send) {
1254 FD_SET(pnode->hSocket, &fdsetSend);
1255 continue;
1257 if (select_recv) {
1258 FD_SET(pnode->hSocket, &fdsetRecv);
1263 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1264 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1265 if (interruptNet)
1266 return;
1268 if (nSelect == SOCKET_ERROR)
1270 if (have_fds)
1272 int nErr = WSAGetLastError();
1273 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1274 for (unsigned int i = 0; i <= hSocketMax; i++)
1275 FD_SET(i, &fdsetRecv);
1277 FD_ZERO(&fdsetSend);
1278 FD_ZERO(&fdsetError);
1279 if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
1280 return;
1284 // Accept new connections
1286 for (const ListenSocket& hListenSocket : vhListenSocket)
1288 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1290 AcceptConnection(hListenSocket);
1295 // Service each socket
1297 std::vector<CNode*> vNodesCopy;
1299 LOCK(cs_vNodes);
1300 vNodesCopy = vNodes;
1301 for (CNode* pnode : vNodesCopy)
1302 pnode->AddRef();
1304 for (CNode* pnode : vNodesCopy)
1306 if (interruptNet)
1307 return;
1310 // Receive
1312 bool recvSet = false;
1313 bool sendSet = false;
1314 bool errorSet = false;
1316 LOCK(pnode->cs_hSocket);
1317 if (pnode->hSocket == INVALID_SOCKET)
1318 continue;
1319 recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
1320 sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
1321 errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
1323 if (recvSet || errorSet)
1325 // typical socket buffer is 8K-64K
1326 char pchBuf[0x10000];
1327 int nBytes = 0;
1329 LOCK(pnode->cs_hSocket);
1330 if (pnode->hSocket == INVALID_SOCKET)
1331 continue;
1332 nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1334 if (nBytes > 0)
1336 bool notify = false;
1337 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1338 pnode->CloseSocketDisconnect();
1339 RecordBytesRecv(nBytes);
1340 if (notify) {
1341 size_t nSizeAdded = 0;
1342 auto it(pnode->vRecvMsg.begin());
1343 for (; it != pnode->vRecvMsg.end(); ++it) {
1344 if (!it->complete())
1345 break;
1346 nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1349 LOCK(pnode->cs_vProcessMsg);
1350 pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1351 pnode->nProcessQueueSize += nSizeAdded;
1352 pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1354 WakeMessageHandler();
1357 else if (nBytes == 0)
1359 // socket closed gracefully
1360 if (!pnode->fDisconnect) {
1361 LogPrint(BCLog::NET, "socket closed\n");
1363 pnode->CloseSocketDisconnect();
1365 else if (nBytes < 0)
1367 // error
1368 int nErr = WSAGetLastError();
1369 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1371 if (!pnode->fDisconnect)
1372 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1373 pnode->CloseSocketDisconnect();
1379 // Send
1381 if (sendSet)
1383 LOCK(pnode->cs_vSend);
1384 size_t nBytes = SocketSendData(pnode);
1385 if (nBytes) {
1386 RecordBytesSent(nBytes);
1391 // Inactivity checking
1393 int64_t nTime = GetSystemTimeInSeconds();
1394 if (nTime - pnode->nTimeConnected > 60)
1396 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1398 LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1399 pnode->fDisconnect = true;
1401 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1403 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1404 pnode->fDisconnect = true;
1406 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1408 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1409 pnode->fDisconnect = true;
1411 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1413 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1414 pnode->fDisconnect = true;
1416 else if (!pnode->fSuccessfullyConnected)
1418 LogPrintf("version handshake timeout from %d\n", pnode->GetId());
1419 pnode->fDisconnect = true;
1424 LOCK(cs_vNodes);
1425 for (CNode* pnode : vNodesCopy)
1426 pnode->Release();
1431 void CConnman::WakeMessageHandler()
1434 std::lock_guard<std::mutex> lock(mutexMsgProc);
1435 fMsgProcWake = true;
1437 condMsgProc.notify_one();
1445 #ifdef USE_UPNP
1446 void ThreadMapPort()
1448 std::string port = strprintf("%u", GetListenPort());
1449 const char * multicastif = nullptr;
1450 const char * minissdpdpath = nullptr;
1451 struct UPNPDev * devlist = nullptr;
1452 char lanaddr[64];
1454 #ifndef UPNPDISCOVER_SUCCESS
1455 /* miniupnpc 1.5 */
1456 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1457 #elif MINIUPNPC_API_VERSION < 14
1458 /* miniupnpc 1.6 */
1459 int error = 0;
1460 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1461 #else
1462 /* miniupnpc 1.9.20150730 */
1463 int error = 0;
1464 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1465 #endif
1467 struct UPNPUrls urls;
1468 struct IGDdatas data;
1469 int r;
1471 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1472 if (r == 1)
1474 if (fDiscover) {
1475 char externalIPAddress[40];
1476 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1477 if(r != UPNPCOMMAND_SUCCESS)
1478 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1479 else
1481 if(externalIPAddress[0])
1483 CNetAddr resolved;
1484 if(LookupHost(externalIPAddress, resolved, false)) {
1485 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1486 AddLocal(resolved, LOCAL_UPNP);
1489 else
1490 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1494 std::string strDesc = "Bitcoin " + FormatFullVersion();
1496 try {
1497 while (true) {
1498 #ifndef UPNPDISCOVER_SUCCESS
1499 /* miniupnpc 1.5 */
1500 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1501 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1502 #else
1503 /* miniupnpc 1.6 */
1504 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1505 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1506 #endif
1508 if(r!=UPNPCOMMAND_SUCCESS)
1509 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1510 port, port, lanaddr, r, strupnperror(r));
1511 else
1512 LogPrintf("UPnP Port Mapping successful.\n");
1514 MilliSleep(20*60*1000); // Refresh every 20 minutes
1517 catch (const boost::thread_interrupted&)
1519 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1520 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1521 freeUPNPDevlist(devlist); devlist = nullptr;
1522 FreeUPNPUrls(&urls);
1523 throw;
1525 } else {
1526 LogPrintf("No valid UPnP IGDs found\n");
1527 freeUPNPDevlist(devlist); devlist = nullptr;
1528 if (r != 0)
1529 FreeUPNPUrls(&urls);
1533 void MapPort(bool fUseUPnP)
1535 static std::unique_ptr<boost::thread> upnp_thread;
1537 if (fUseUPnP)
1539 if (upnp_thread) {
1540 upnp_thread->interrupt();
1541 upnp_thread->join();
1543 upnp_thread.reset(new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
1545 else if (upnp_thread) {
1546 upnp_thread->interrupt();
1547 upnp_thread->join();
1548 upnp_thread.reset();
1552 #else
1553 void MapPort(bool)
1555 // Intentionally left blank.
1557 #endif
1564 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1566 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1567 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1568 *requiredServiceBits = NODE_NETWORK;
1569 return data.host;
1572 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1573 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1577 void CConnman::ThreadDNSAddressSeed()
1579 // goal: only query DNS seeds if address need is acute
1580 // Avoiding DNS seeds when we don't need them improves user privacy by
1581 // creating fewer identifying DNS requests, reduces trust by giving seeds
1582 // less influence on the network topology, and reduces traffic to the seeds.
1583 if ((addrman.size() > 0) &&
1584 (!gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1585 if (!interruptNet.sleep_for(std::chrono::seconds(11)))
1586 return;
1588 LOCK(cs_vNodes);
1589 int nRelevant = 0;
1590 for (auto pnode : vNodes) {
1591 nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound;
1593 if (nRelevant >= 2) {
1594 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1595 return;
1599 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1600 int found = 0;
1602 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1604 for (const CDNSSeedData &seed : vSeeds) {
1605 if (interruptNet) {
1606 return;
1608 if (HaveNameProxy()) {
1609 AddOneShot(seed.host);
1610 } else {
1611 std::vector<CNetAddr> vIPs;
1612 std::vector<CAddress> vAdd;
1613 ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE);
1614 std::string host = GetDNSHost(seed, &requiredServiceBits);
1615 CNetAddr resolveSource;
1616 if (!resolveSource.SetInternal(host)) {
1617 continue;
1619 if (LookupHost(host.c_str(), vIPs, 0, true))
1621 for (const CNetAddr& ip : vIPs)
1623 int nOneDay = 24*3600;
1624 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
1625 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1626 vAdd.push_back(addr);
1627 found++;
1629 addrman.Add(vAdd, resolveSource);
1634 LogPrintf("%d addresses found from DNS seeds\n", found);
1648 void CConnman::DumpAddresses()
1650 int64_t nStart = GetTimeMillis();
1652 CAddrDB adb;
1653 adb.Write(addrman);
1655 LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1656 addrman.size(), GetTimeMillis() - nStart);
1659 void CConnman::DumpData()
1661 DumpAddresses();
1662 DumpBanlist();
1665 void CConnman::ProcessOneShot()
1667 std::string strDest;
1669 LOCK(cs_vOneShots);
1670 if (vOneShots.empty())
1671 return;
1672 strDest = vOneShots.front();
1673 vOneShots.pop_front();
1675 CAddress addr;
1676 CSemaphoreGrant grant(*semOutbound, true);
1677 if (grant) {
1678 if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
1679 AddOneShot(strDest);
1683 bool CConnman::GetTryNewOutboundPeer()
1685 return m_try_another_outbound_peer;
1688 void CConnman::SetTryNewOutboundPeer(bool flag)
1690 m_try_another_outbound_peer = flag;
1691 LogPrint(BCLog::NET, "net: setting try another outbound peer=%s\n", flag ? "true" : "false");
1694 // Return the number of peers we have over our outbound connection limit
1695 // Exclude peers that are marked for disconnect, or are going to be
1696 // disconnected soon (eg one-shots and feelers)
1697 // Also exclude peers that haven't finished initial connection handshake yet
1698 // (so that we don't decide we're over our desired connection limit, and then
1699 // evict some peer that has finished the handshake)
1700 int CConnman::GetExtraOutboundCount()
1702 int nOutbound = 0;
1704 LOCK(cs_vNodes);
1705 for (CNode* pnode : vNodes) {
1706 if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) {
1707 ++nOutbound;
1711 return std::max(nOutbound - nMaxOutbound, 0);
1714 void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1716 // Connect to specific addresses
1717 if (!connect.empty())
1719 for (int64_t nLoop = 0;; nLoop++)
1721 ProcessOneShot();
1722 for (const std::string& strAddr : connect)
1724 CAddress addr(CService(), NODE_NONE);
1725 OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(), false, false, true);
1726 for (int i = 0; i < 10 && i < nLoop; i++)
1728 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1729 return;
1732 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1733 return;
1737 // Initiate network connections
1738 int64_t nStart = GetTime();
1740 // Minimum time before next feeler connection (in microseconds).
1741 int64_t nNextFeeler = PoissonNextSend(nStart*1000*1000, FEELER_INTERVAL);
1742 while (!interruptNet)
1744 ProcessOneShot();
1746 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1747 return;
1749 CSemaphoreGrant grant(*semOutbound);
1750 if (interruptNet)
1751 return;
1753 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1754 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1755 static bool done = false;
1756 if (!done) {
1757 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1758 CNetAddr local;
1759 local.SetInternal("fixedseeds");
1760 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1761 done = true;
1766 // Choose an address to connect to based on most recently seen
1768 CAddress addrConnect;
1770 // Only connect out to one peer per network group (/16 for IPv4).
1771 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1772 int nOutbound = 0;
1773 std::set<std::vector<unsigned char> > setConnected;
1775 LOCK(cs_vNodes);
1776 for (CNode* pnode : vNodes) {
1777 if (!pnode->fInbound && !pnode->m_manual_connection) {
1778 // Netgroups for inbound and addnode peers are not excluded because our goal here
1779 // is to not use multiple of our limited outbound slots on a single netgroup
1780 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1781 // also have the added issue that they're attacker controlled and could be used
1782 // to prevent us from connecting to particular hosts if we used them here.
1783 setConnected.insert(pnode->addr.GetGroup());
1784 nOutbound++;
1789 // Feeler Connections
1791 // Design goals:
1792 // * Increase the number of connectable addresses in the tried table.
1794 // Method:
1795 // * Choose a random address from new and attempt to connect to it if we can connect
1796 // successfully it is added to tried.
1797 // * Start attempting feeler connections only after node finishes making outbound
1798 // connections.
1799 // * Only make a feeler connection once every few minutes.
1801 bool fFeeler = false;
1803 if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer()) {
1804 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1805 if (nTime > nNextFeeler) {
1806 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1807 fFeeler = true;
1808 } else {
1809 continue;
1813 int64_t nANow = GetAdjustedTime();
1814 int nTries = 0;
1815 while (!interruptNet)
1817 CAddrInfo addr = addrman.Select(fFeeler);
1819 // if we selected an invalid address, restart
1820 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1821 break;
1823 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1824 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1825 // already-connected network ranges, ...) before trying new addrman addresses.
1826 nTries++;
1827 if (nTries > 100)
1828 break;
1830 if (IsLimited(addr))
1831 continue;
1833 // only consider very recently tried nodes after 30 failed attempts
1834 if (nANow - addr.nLastTry < 600 && nTries < 30)
1835 continue;
1837 // for non-feelers, require all the services we'll want,
1838 // for feelers, only require they be a full node (only because most
1839 // SPV clients don't have a good address DB available)
1840 if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
1841 continue;
1842 } else if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
1843 continue;
1846 // do not allow non-default ports, unless after 50 invalid addresses selected already
1847 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1848 continue;
1850 addrConnect = addr;
1851 break;
1854 if (addrConnect.IsValid()) {
1856 if (fFeeler) {
1857 // Add small amount of random noise before connection to avoid synchronization.
1858 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1859 if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1860 return;
1861 LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
1864 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler);
1869 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1871 std::vector<AddedNodeInfo> ret;
1873 std::list<std::string> lAddresses(0);
1875 LOCK(cs_vAddedNodes);
1876 ret.reserve(vAddedNodes.size());
1877 std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses));
1881 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1882 std::map<CService, bool> mapConnected;
1883 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
1885 LOCK(cs_vNodes);
1886 for (const CNode* pnode : vNodes) {
1887 if (pnode->addr.IsValid()) {
1888 mapConnected[pnode->addr] = pnode->fInbound;
1890 std::string addrName = pnode->GetAddrName();
1891 if (!addrName.empty()) {
1892 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1897 for (const std::string& strAddNode : lAddresses) {
1898 CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1899 if (service.IsValid()) {
1900 // strAddNode is an IP:port
1901 auto it = mapConnected.find(service);
1902 if (it != mapConnected.end()) {
1903 ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1904 } else {
1905 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1907 } else {
1908 // strAddNode is a name
1909 auto it = mapConnectedByName.find(strAddNode);
1910 if (it != mapConnectedByName.end()) {
1911 ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1912 } else {
1913 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1918 return ret;
1921 void CConnman::ThreadOpenAddedConnections()
1923 while (true)
1925 CSemaphoreGrant grant(*semAddnode);
1926 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1927 bool tried = false;
1928 for (const AddedNodeInfo& info : vInfo) {
1929 if (!info.fConnected) {
1930 if (!grant.TryAcquire()) {
1931 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1932 // the addednodeinfo state might change.
1933 break;
1935 tried = true;
1936 CAddress addr(CService(), NODE_NONE);
1937 OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), false, false, true);
1938 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1939 return;
1942 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1943 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
1944 return;
1948 // if successful, this moves the passed grant to the constructed node
1949 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool manual_connection)
1952 // Initiate outbound network connection
1954 if (interruptNet) {
1955 return false;
1957 if (!fNetworkActive) {
1958 return false;
1960 if (!pszDest) {
1961 if (IsLocal(addrConnect) ||
1962 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1963 FindNode(addrConnect.ToStringIPPort()))
1964 return false;
1965 } else if (FindNode(std::string(pszDest)))
1966 return false;
1968 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1970 if (!pnode)
1971 return false;
1972 if (grantOutbound)
1973 grantOutbound->MoveTo(pnode->grantOutbound);
1974 if (fOneShot)
1975 pnode->fOneShot = true;
1976 if (fFeeler)
1977 pnode->fFeeler = true;
1978 if (manual_connection)
1979 pnode->m_manual_connection = true;
1981 m_msgproc->InitializeNode(pnode);
1983 LOCK(cs_vNodes);
1984 vNodes.push_back(pnode);
1987 return true;
1990 void CConnman::ThreadMessageHandler()
1992 while (!flagInterruptMsgProc)
1994 std::vector<CNode*> vNodesCopy;
1996 LOCK(cs_vNodes);
1997 vNodesCopy = vNodes;
1998 for (CNode* pnode : vNodesCopy) {
1999 pnode->AddRef();
2003 bool fMoreWork = false;
2005 for (CNode* pnode : vNodesCopy)
2007 if (pnode->fDisconnect)
2008 continue;
2010 // Receive messages
2011 bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
2012 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
2013 if (flagInterruptMsgProc)
2014 return;
2015 // Send messages
2017 LOCK(pnode->cs_sendProcessing);
2018 m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
2021 if (flagInterruptMsgProc)
2022 return;
2026 LOCK(cs_vNodes);
2027 for (CNode* pnode : vNodesCopy)
2028 pnode->Release();
2031 std::unique_lock<std::mutex> lock(mutexMsgProc);
2032 if (!fMoreWork) {
2033 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
2035 fMsgProcWake = false;
2044 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
2046 strError = "";
2047 int nOne = 1;
2049 // Create socket for listening for incoming connections
2050 struct sockaddr_storage sockaddr;
2051 socklen_t len = sizeof(sockaddr);
2052 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
2054 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
2055 LogPrintf("%s\n", strError);
2056 return false;
2059 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2060 if (hListenSocket == INVALID_SOCKET)
2062 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2063 LogPrintf("%s\n", strError);
2064 return false;
2066 if (!IsSelectableSocket(hListenSocket))
2068 strError = "Error: Couldn't create a listenable socket for incoming connections";
2069 LogPrintf("%s\n", strError);
2070 return false;
2074 #ifndef WIN32
2075 #ifdef SO_NOSIGPIPE
2076 // Different way of disabling SIGPIPE on BSD
2077 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
2078 #endif
2079 // Allow binding if the port is still in TIME_WAIT state after
2080 // the program was closed and restarted.
2081 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2082 // Disable Nagle's algorithm
2083 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
2084 #else
2085 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2086 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
2087 #endif
2089 // Set to non-blocking, incoming connections will also inherit this
2090 if (!SetSocketNonBlocking(hListenSocket, true)) {
2091 CloseSocket(hListenSocket);
2092 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2093 LogPrintf("%s\n", strError);
2094 return false;
2097 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2098 // and enable it by default or not. Try to enable it, if possible.
2099 if (addrBind.IsIPv6()) {
2100 #ifdef IPV6_V6ONLY
2101 #ifdef WIN32
2102 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2103 #else
2104 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2105 #endif
2106 #endif
2107 #ifdef WIN32
2108 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2109 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2110 #endif
2113 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2115 int nErr = WSAGetLastError();
2116 if (nErr == WSAEADDRINUSE)
2117 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
2118 else
2119 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2120 LogPrintf("%s\n", strError);
2121 CloseSocket(hListenSocket);
2122 return false;
2124 LogPrintf("Bound to %s\n", addrBind.ToString());
2126 // Listen for incoming connections
2127 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
2129 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2130 LogPrintf("%s\n", strError);
2131 CloseSocket(hListenSocket);
2132 return false;
2135 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
2137 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
2138 AddLocal(addrBind, LOCAL_BIND);
2140 return true;
2143 void Discover(boost::thread_group& threadGroup)
2145 if (!fDiscover)
2146 return;
2148 #ifdef WIN32
2149 // Get local host IP
2150 char pszHostName[256] = "";
2151 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
2153 std::vector<CNetAddr> vaddr;
2154 if (LookupHost(pszHostName, vaddr, 0, true))
2156 for (const CNetAddr &addr : vaddr)
2158 if (AddLocal(addr, LOCAL_IF))
2159 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
2163 #else
2164 // Get local host ip
2165 struct ifaddrs* myaddrs;
2166 if (getifaddrs(&myaddrs) == 0)
2168 for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
2170 if (ifa->ifa_addr == nullptr) continue;
2171 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2172 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2173 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2174 if (ifa->ifa_addr->sa_family == AF_INET)
2176 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2177 CNetAddr addr(s4->sin_addr);
2178 if (AddLocal(addr, LOCAL_IF))
2179 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2181 else if (ifa->ifa_addr->sa_family == AF_INET6)
2183 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2184 CNetAddr addr(s6->sin6_addr);
2185 if (AddLocal(addr, LOCAL_IF))
2186 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2189 freeifaddrs(myaddrs);
2191 #endif
2194 void CConnman::SetNetworkActive(bool active)
2196 LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2198 if (fNetworkActive == active) {
2199 return;
2202 fNetworkActive = active;
2204 if (!fNetworkActive) {
2205 LOCK(cs_vNodes);
2206 // Close sockets to all nodes
2207 for (CNode* pnode : vNodes) {
2208 pnode->CloseSocketDisconnect();
2212 uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2215 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2217 fNetworkActive = true;
2218 setBannedIsDirty = false;
2219 fAddressesInitialized = false;
2220 nLastNodeId = 0;
2221 nSendBufferMaxSize = 0;
2222 nReceiveFloodSize = 0;
2223 flagInterruptMsgProc = false;
2224 SetTryNewOutboundPeer(false);
2226 Options connOptions;
2227 Init(connOptions);
2230 NodeId CConnman::GetNewNodeId()
2232 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2236 bool CConnman::Bind(const CService &addr, unsigned int flags) {
2237 if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2238 return false;
2239 std::string strError;
2240 if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
2241 if ((flags & BF_REPORT_ERROR) && clientInterface) {
2242 clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
2244 return false;
2246 return true;
2249 bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2250 bool fBound = false;
2251 for (const auto& addrBind : binds) {
2252 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2254 for (const auto& addrBind : whiteBinds) {
2255 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
2257 if (binds.empty() && whiteBinds.empty()) {
2258 struct in_addr inaddr_any;
2259 inaddr_any.s_addr = INADDR_ANY;
2260 fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
2261 fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2263 return fBound;
2266 bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2268 Init(connOptions);
2271 LOCK(cs_totalBytesRecv);
2272 nTotalBytesRecv = 0;
2275 LOCK(cs_totalBytesSent);
2276 nTotalBytesSent = 0;
2277 nMaxOutboundTotalBytesSentInCycle = 0;
2278 nMaxOutboundCycleStartTime = 0;
2281 if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2282 if (clientInterface) {
2283 clientInterface->ThreadSafeMessageBox(
2284 _("Failed to listen on any port. Use -listen=0 if you want this."),
2285 "", CClientUIInterface::MSG_ERROR);
2287 return false;
2290 for (const auto& strDest : connOptions.vSeedNodes) {
2291 AddOneShot(strDest);
2294 if (clientInterface) {
2295 clientInterface->InitMessage(_("Loading P2P addresses..."));
2297 // Load addresses from peers.dat
2298 int64_t nStart = GetTimeMillis();
2300 CAddrDB adb;
2301 if (adb.Read(addrman))
2302 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2303 else {
2304 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2305 LogPrintf("Invalid or missing peers.dat; recreating\n");
2306 DumpAddresses();
2309 if (clientInterface)
2310 clientInterface->InitMessage(_("Loading banlist..."));
2311 // Load addresses from banlist.dat
2312 nStart = GetTimeMillis();
2313 CBanDB bandb;
2314 banmap_t banmap;
2315 if (bandb.Read(banmap)) {
2316 SetBanned(banmap); // thread save setter
2317 SetBannedSetDirty(false); // no need to write down, just read data
2318 SweepBanned(); // sweep out unused entries
2320 LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2321 banmap.size(), GetTimeMillis() - nStart);
2322 } else {
2323 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2324 SetBannedSetDirty(true); // force write
2325 DumpBanlist();
2328 uiInterface.InitMessage(_("Starting network threads..."));
2330 fAddressesInitialized = true;
2332 if (semOutbound == nullptr) {
2333 // initialize semaphore
2334 semOutbound = MakeUnique<CSemaphore>(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
2336 if (semAddnode == nullptr) {
2337 // initialize semaphore
2338 semAddnode = MakeUnique<CSemaphore>(nMaxAddnode);
2342 // Start threads
2344 assert(m_msgproc);
2345 InterruptSocks5(false);
2346 interruptNet.reset();
2347 flagInterruptMsgProc = false;
2350 std::unique_lock<std::mutex> lock(mutexMsgProc);
2351 fMsgProcWake = false;
2354 // Send and receive from sockets, accept connections
2355 threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2357 if (!gArgs.GetBoolArg("-dnsseed", true))
2358 LogPrintf("DNS seeding disabled\n");
2359 else
2360 threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2362 // Initiate outbound connections from -addnode
2363 threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2365 if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
2366 if (clientInterface) {
2367 clientInterface->ThreadSafeMessageBox(
2368 _("Cannot provide specific connections and have addrman find outgoing connections at the same."),
2369 "", CClientUIInterface::MSG_ERROR);
2371 return false;
2373 if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
2374 threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
2376 // Process messages
2377 threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2379 // Dump network addresses
2380 scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
2382 return true;
2385 class CNetCleanup
2387 public:
2388 CNetCleanup() {}
2390 ~CNetCleanup()
2392 #ifdef WIN32
2393 // Shutdown Windows Sockets
2394 WSACleanup();
2395 #endif
2398 instance_of_cnetcleanup;
2400 void CConnman::Interrupt()
2403 std::lock_guard<std::mutex> lock(mutexMsgProc);
2404 flagInterruptMsgProc = true;
2406 condMsgProc.notify_all();
2408 interruptNet();
2409 InterruptSocks5(true);
2411 if (semOutbound) {
2412 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++) {
2413 semOutbound->post();
2417 if (semAddnode) {
2418 for (int i=0; i<nMaxAddnode; i++) {
2419 semAddnode->post();
2424 void CConnman::Stop()
2426 if (threadMessageHandler.joinable())
2427 threadMessageHandler.join();
2428 if (threadOpenConnections.joinable())
2429 threadOpenConnections.join();
2430 if (threadOpenAddedConnections.joinable())
2431 threadOpenAddedConnections.join();
2432 if (threadDNSAddressSeed.joinable())
2433 threadDNSAddressSeed.join();
2434 if (threadSocketHandler.joinable())
2435 threadSocketHandler.join();
2437 if (fAddressesInitialized)
2439 DumpData();
2440 fAddressesInitialized = false;
2443 // Close sockets
2444 for (CNode* pnode : vNodes)
2445 pnode->CloseSocketDisconnect();
2446 for (ListenSocket& hListenSocket : vhListenSocket)
2447 if (hListenSocket.socket != INVALID_SOCKET)
2448 if (!CloseSocket(hListenSocket.socket))
2449 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2451 // clean up some globals (to help leak detection)
2452 for (CNode *pnode : vNodes) {
2453 DeleteNode(pnode);
2455 for (CNode *pnode : vNodesDisconnected) {
2456 DeleteNode(pnode);
2458 vNodes.clear();
2459 vNodesDisconnected.clear();
2460 vhListenSocket.clear();
2461 semOutbound.reset();
2462 semAddnode.reset();
2465 void CConnman::DeleteNode(CNode* pnode)
2467 assert(pnode);
2468 bool fUpdateConnectionTime = false;
2469 m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2470 if(fUpdateConnectionTime) {
2471 addrman.Connected(pnode->addr);
2473 delete pnode;
2476 CConnman::~CConnman()
2478 Interrupt();
2479 Stop();
2482 size_t CConnman::GetAddressCount() const
2484 return addrman.size();
2487 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2489 addrman.SetServices(addr, nServices);
2492 void CConnman::MarkAddressGood(const CAddress& addr)
2494 addrman.Good(addr);
2497 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2499 addrman.Add(vAddr, addrFrom, nTimePenalty);
2502 std::vector<CAddress> CConnman::GetAddresses()
2504 return addrman.GetAddr();
2507 bool CConnman::AddNode(const std::string& strNode)
2509 LOCK(cs_vAddedNodes);
2510 for (const std::string& it : vAddedNodes) {
2511 if (strNode == it) return false;
2514 vAddedNodes.push_back(strNode);
2515 return true;
2518 bool CConnman::RemoveAddedNode(const std::string& strNode)
2520 LOCK(cs_vAddedNodes);
2521 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2522 if (strNode == *it) {
2523 vAddedNodes.erase(it);
2524 return true;
2527 return false;
2530 size_t CConnman::GetNodeCount(NumConnections flags)
2532 LOCK(cs_vNodes);
2533 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2534 return vNodes.size();
2536 int nNum = 0;
2537 for (const auto& pnode : vNodes) {
2538 if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
2539 nNum++;
2543 return nNum;
2546 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2548 vstats.clear();
2549 LOCK(cs_vNodes);
2550 vstats.reserve(vNodes.size());
2551 for (CNode* pnode : vNodes) {
2552 vstats.emplace_back();
2553 pnode->copyStats(vstats.back());
2557 bool CConnman::DisconnectNode(const std::string& strNode)
2559 LOCK(cs_vNodes);
2560 if (CNode* pnode = FindNode(strNode)) {
2561 pnode->fDisconnect = true;
2562 return true;
2564 return false;
2566 bool CConnman::DisconnectNode(NodeId id)
2568 LOCK(cs_vNodes);
2569 for(CNode* pnode : vNodes) {
2570 if (id == pnode->GetId()) {
2571 pnode->fDisconnect = true;
2572 return true;
2575 return false;
2578 void CConnman::RecordBytesRecv(uint64_t bytes)
2580 LOCK(cs_totalBytesRecv);
2581 nTotalBytesRecv += bytes;
2584 void CConnman::RecordBytesSent(uint64_t bytes)
2586 LOCK(cs_totalBytesSent);
2587 nTotalBytesSent += bytes;
2589 uint64_t now = GetTime();
2590 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2592 // timeframe expired, reset cycle
2593 nMaxOutboundCycleStartTime = now;
2594 nMaxOutboundTotalBytesSentInCycle = 0;
2597 // TODO, exclude whitebind peers
2598 nMaxOutboundTotalBytesSentInCycle += bytes;
2601 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2603 LOCK(cs_totalBytesSent);
2604 nMaxOutboundLimit = limit;
2607 uint64_t CConnman::GetMaxOutboundTarget()
2609 LOCK(cs_totalBytesSent);
2610 return nMaxOutboundLimit;
2613 uint64_t CConnman::GetMaxOutboundTimeframe()
2615 LOCK(cs_totalBytesSent);
2616 return nMaxOutboundTimeframe;
2619 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2621 LOCK(cs_totalBytesSent);
2622 if (nMaxOutboundLimit == 0)
2623 return 0;
2625 if (nMaxOutboundCycleStartTime == 0)
2626 return nMaxOutboundTimeframe;
2628 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2629 uint64_t now = GetTime();
2630 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2633 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2635 LOCK(cs_totalBytesSent);
2636 if (nMaxOutboundTimeframe != timeframe)
2638 // reset measure-cycle in case of changing
2639 // the timeframe
2640 nMaxOutboundCycleStartTime = GetTime();
2642 nMaxOutboundTimeframe = timeframe;
2645 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2647 LOCK(cs_totalBytesSent);
2648 if (nMaxOutboundLimit == 0)
2649 return false;
2651 if (historicalBlockServingLimit)
2653 // keep a large enough buffer to at least relay each block once
2654 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2655 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2656 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2657 return true;
2659 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2660 return true;
2662 return false;
2665 uint64_t CConnman::GetOutboundTargetBytesLeft()
2667 LOCK(cs_totalBytesSent);
2668 if (nMaxOutboundLimit == 0)
2669 return 0;
2671 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2674 uint64_t CConnman::GetTotalBytesRecv()
2676 LOCK(cs_totalBytesRecv);
2677 return nTotalBytesRecv;
2680 uint64_t CConnman::GetTotalBytesSent()
2682 LOCK(cs_totalBytesSent);
2683 return nTotalBytesSent;
2686 ServiceFlags CConnman::GetLocalServices() const
2688 return nLocalServices;
2691 void CConnman::SetBestHeight(int height)
2693 nBestHeight.store(height, std::memory_order_release);
2696 int CConnman::GetBestHeight() const
2698 return nBestHeight.load(std::memory_order_acquire);
2701 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2703 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) :
2704 nTimeConnected(GetSystemTimeInSeconds()),
2705 addr(addrIn),
2706 addrBind(addrBindIn),
2707 fInbound(fInboundIn),
2708 nKeyedNetGroup(nKeyedNetGroupIn),
2709 addrKnown(5000, 0.001),
2710 filterInventoryKnown(50000, 0.000001),
2711 id(idIn),
2712 nLocalHostNonce(nLocalHostNonceIn),
2713 nLocalServices(nLocalServicesIn),
2714 nMyStartingHeight(nMyStartingHeightIn),
2715 nSendVersion(0)
2717 nServices = NODE_NONE;
2718 hSocket = hSocketIn;
2719 nRecvVersion = INIT_PROTO_VERSION;
2720 nLastSend = 0;
2721 nLastRecv = 0;
2722 nSendBytes = 0;
2723 nRecvBytes = 0;
2724 nTimeOffset = 0;
2725 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2726 nVersion = 0;
2727 strSubVer = "";
2728 fWhitelisted = false;
2729 fOneShot = false;
2730 m_manual_connection = false;
2731 fClient = false; // set by version message
2732 fFeeler = false;
2733 fSuccessfullyConnected = false;
2734 fDisconnect = false;
2735 nRefCount = 0;
2736 nSendSize = 0;
2737 nSendOffset = 0;
2738 hashContinue = uint256();
2739 nStartingHeight = -1;
2740 filterInventoryKnown.reset();
2741 fSendMempool = false;
2742 fGetAddr = false;
2743 nNextLocalAddrSend = 0;
2744 nNextAddrSend = 0;
2745 nNextInvSend = 0;
2746 fRelayTxes = false;
2747 fSentAddr = false;
2748 pfilter = MakeUnique<CBloomFilter>();
2749 timeLastMempoolReq = 0;
2750 nLastBlockTime = 0;
2751 nLastTXTime = 0;
2752 nPingNonceSent = 0;
2753 nPingUsecStart = 0;
2754 nPingUsecTime = 0;
2755 fPingQueued = false;
2756 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2757 minFeeFilter = 0;
2758 lastSentFeeFilter = 0;
2759 nextSendTimeFeeFilter = 0;
2760 fPauseRecv = false;
2761 fPauseSend = false;
2762 nProcessQueueSize = 0;
2764 for (const std::string &msg : getAllNetMessageTypes())
2765 mapRecvBytesPerMsgCmd[msg] = 0;
2766 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2768 if (fLogIPs) {
2769 LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
2770 } else {
2771 LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
2775 CNode::~CNode()
2777 CloseSocket(hSocket);
2780 void CNode::AskFor(const CInv& inv)
2782 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2783 return;
2784 // a peer may not have multiple non-responded queue positions for a single inv item
2785 if (!setAskFor.insert(inv.hash).second)
2786 return;
2788 // We're using mapAskFor as a priority queue,
2789 // the key is the earliest time the request can be sent
2790 int64_t nRequestTime;
2791 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2792 if (it != mapAlreadyAskedFor.end())
2793 nRequestTime = it->second;
2794 else
2795 nRequestTime = 0;
2796 LogPrint(BCLog::NET, "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2798 // Make sure not to reuse time indexes to keep things in the same order
2799 int64_t nNow = GetTimeMicros() - 1000000;
2800 static int64_t nLastTime;
2801 ++nLastTime;
2802 nNow = std::max(nNow, nLastTime);
2803 nLastTime = nNow;
2805 // Each retry is 2 minutes after the last
2806 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2807 if (it != mapAlreadyAskedFor.end())
2808 mapAlreadyAskedFor.update(it, nRequestTime);
2809 else
2810 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2811 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2814 bool CConnman::NodeFullyConnected(const CNode* pnode)
2816 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
2819 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2821 size_t nMessageSize = msg.data.size();
2822 size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2823 LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2825 std::vector<unsigned char> serializedHeader;
2826 serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
2827 uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
2828 CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize);
2829 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
2831 CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
2833 size_t nBytesSent = 0;
2835 LOCK(pnode->cs_vSend);
2836 bool optimisticSend(pnode->vSendMsg.empty());
2838 //log total amount of bytes per command
2839 pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
2840 pnode->nSendSize += nTotalSize;
2842 if (pnode->nSendSize > nSendBufferMaxSize)
2843 pnode->fPauseSend = true;
2844 pnode->vSendMsg.push_back(std::move(serializedHeader));
2845 if (nMessageSize)
2846 pnode->vSendMsg.push_back(std::move(msg.data));
2848 // If write queue empty, attempt "optimistic write"
2849 if (optimisticSend == true)
2850 nBytesSent = SocketSendData(pnode);
2852 if (nBytesSent)
2853 RecordBytesSent(nBytesSent);
2856 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2858 CNode* found = nullptr;
2859 LOCK(cs_vNodes);
2860 for (auto&& pnode : vNodes) {
2861 if(pnode->GetId() == id) {
2862 found = pnode;
2863 break;
2866 return found != nullptr && NodeFullyConnected(found) && func(found);
2869 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2870 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2873 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
2875 return CSipHasher(nSeed0, nSeed1).Write(id);
2878 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
2880 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2882 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();