Improve readability of DecodeBase58Check(...)
[bitcoinplatinum.git] / src / net.cpp
blob1af317726a5ec3eedb07afdf2c6b3be225b9e85d
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 // Signals for message handling
93 static CNodeSignals g_signals;
94 CNodeSignals& GetNodeSignals() { return g_signals; }
96 void CConnman::AddOneShot(const std::string& strDest)
98 LOCK(cs_vOneShots);
99 vOneShots.push_back(strDest);
102 unsigned short GetListenPort()
104 return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
107 // find 'best' local address for a particular peer
108 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
110 if (!fListen)
111 return false;
113 int nBestScore = -1;
114 int nBestReachability = -1;
116 LOCK(cs_mapLocalHost);
117 for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
119 int nScore = (*it).second.nScore;
120 int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
121 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
123 addr = CService((*it).first, (*it).second.nPort);
124 nBestReachability = nReachability;
125 nBestScore = nScore;
129 return nBestScore >= 0;
132 //! Convert the pnSeeds6 array into usable address objects.
133 static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
135 // It'll only connect to one or two seed nodes because once it connects,
136 // it'll get a pile of addresses with newer timestamps.
137 // Seed nodes are given a random 'last seen time' of between one and two
138 // weeks ago.
139 const int64_t nOneWeek = 7*24*60*60;
140 std::vector<CAddress> vSeedsOut;
141 vSeedsOut.reserve(vSeedsIn.size());
142 for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i)
144 struct in6_addr ip;
145 memcpy(&ip, i->addr, sizeof(ip));
146 CAddress addr(CService(ip, i->port), NODE_NETWORK);
147 addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
148 vSeedsOut.push_back(addr);
150 return vSeedsOut;
153 // get best local address for a particular peer as a CAddress
154 // Otherwise, return the unroutable 0.0.0.0 but filled in with
155 // the normal parameters, since the IP may be changed to a useful
156 // one by discovery.
157 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
159 CAddress ret(CService(CNetAddr(),GetListenPort()), nLocalServices);
160 CService addr;
161 if (GetLocal(addr, paddrPeer))
163 ret = CAddress(addr, nLocalServices);
165 ret.nTime = GetAdjustedTime();
166 return ret;
169 int GetnScore(const CService& addr)
171 LOCK(cs_mapLocalHost);
172 if (mapLocalHost.count(addr) == LOCAL_NONE)
173 return 0;
174 return mapLocalHost[addr].nScore;
177 // Is our peer's addrLocal potentially useful as an external IP source?
178 bool IsPeerAddrLocalGood(CNode *pnode)
180 CService addrLocal = pnode->GetAddrLocal();
181 return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
182 !IsLimited(addrLocal.GetNetwork());
185 // pushes our own address to a peer
186 void AdvertiseLocal(CNode *pnode)
188 if (fListen && pnode->fSuccessfullyConnected)
190 CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
191 // If discovery is enabled, sometimes give our peer the address it
192 // tells us that it sees us as in case it has a better idea of our
193 // address than we do.
194 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
195 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
197 addrLocal.SetIP(pnode->GetAddrLocal());
199 if (addrLocal.IsRoutable())
201 LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
202 FastRandomContext insecure_rand;
203 pnode->PushAddress(addrLocal, insecure_rand);
208 // learn a new local address
209 bool AddLocal(const CService& addr, int nScore)
211 if (!addr.IsRoutable())
212 return false;
214 if (!fDiscover && nScore < LOCAL_MANUAL)
215 return false;
217 if (IsLimited(addr))
218 return false;
220 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
223 LOCK(cs_mapLocalHost);
224 bool fAlready = mapLocalHost.count(addr) > 0;
225 LocalServiceInfo &info = mapLocalHost[addr];
226 if (!fAlready || nScore >= info.nScore) {
227 info.nScore = nScore + (fAlready ? 1 : 0);
228 info.nPort = addr.GetPort();
232 return true;
235 bool AddLocal(const CNetAddr &addr, int nScore)
237 return AddLocal(CService(addr, GetListenPort()), nScore);
240 bool RemoveLocal(const CService& addr)
242 LOCK(cs_mapLocalHost);
243 LogPrintf("RemoveLocal(%s)\n", addr.ToString());
244 mapLocalHost.erase(addr);
245 return true;
248 /** Make a particular network entirely off-limits (no automatic connects to it) */
249 void SetLimited(enum Network net, bool fLimited)
251 if (net == NET_UNROUTABLE || net == NET_INTERNAL)
252 return;
253 LOCK(cs_mapLocalHost);
254 vfLimited[net] = fLimited;
257 bool IsLimited(enum Network net)
259 LOCK(cs_mapLocalHost);
260 return vfLimited[net];
263 bool IsLimited(const CNetAddr &addr)
265 return IsLimited(addr.GetNetwork());
268 /** vote for a local address */
269 bool SeenLocal(const CService& addr)
272 LOCK(cs_mapLocalHost);
273 if (mapLocalHost.count(addr) == 0)
274 return false;
275 mapLocalHost[addr].nScore++;
277 return true;
281 /** check whether a given address is potentially local */
282 bool IsLocal(const CService& addr)
284 LOCK(cs_mapLocalHost);
285 return mapLocalHost.count(addr) > 0;
288 /** check whether a given network is one we can probably connect to */
289 bool IsReachable(enum Network net)
291 LOCK(cs_mapLocalHost);
292 return !vfLimited[net];
295 /** check whether a given address is in a network we can probably connect to */
296 bool IsReachable(const CNetAddr& addr)
298 enum Network net = addr.GetNetwork();
299 return IsReachable(net);
303 CNode* CConnman::FindNode(const CNetAddr& ip)
305 LOCK(cs_vNodes);
306 for (CNode* pnode : vNodes)
307 if ((CNetAddr)pnode->addr == ip)
308 return (pnode);
309 return nullptr;
312 CNode* CConnman::FindNode(const CSubNet& subNet)
314 LOCK(cs_vNodes);
315 for (CNode* pnode : vNodes)
316 if (subNet.Match((CNetAddr)pnode->addr))
317 return (pnode);
318 return nullptr;
321 CNode* CConnman::FindNode(const std::string& addrName)
323 LOCK(cs_vNodes);
324 for (CNode* pnode : vNodes) {
325 if (pnode->GetAddrName() == addrName) {
326 return (pnode);
329 return nullptr;
332 CNode* CConnman::FindNode(const CService& addr)
334 LOCK(cs_vNodes);
335 for (CNode* pnode : vNodes)
336 if ((CService)pnode->addr == addr)
337 return (pnode);
338 return nullptr;
341 bool CConnman::CheckIncomingNonce(uint64_t nonce)
343 LOCK(cs_vNodes);
344 for (CNode* pnode : vNodes) {
345 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
346 return false;
348 return true;
351 /** Get the bind address for a socket as CAddress */
352 static CAddress GetBindAddress(SOCKET sock)
354 CAddress addr_bind;
355 struct sockaddr_storage sockaddr_bind;
356 socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
357 if (sock != INVALID_SOCKET) {
358 if (!getsockname(sock, (struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
359 addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind);
360 } else {
361 LogPrint(BCLog::NET, "Warning: getsockname failed\n");
364 return addr_bind;
367 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
369 if (pszDest == nullptr) {
370 if (IsLocal(addrConnect))
371 return nullptr;
373 // Look for an existing connection
374 CNode* pnode = FindNode((CService)addrConnect);
375 if (pnode)
377 LogPrintf("Failed to open new connection, already connected\n");
378 return nullptr;
382 /// debug print
383 LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
384 pszDest ? pszDest : addrConnect.ToString(),
385 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
387 // Connect
388 SOCKET hSocket;
389 bool proxyConnectionFailed = false;
390 if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
391 ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
393 if (!IsSelectableSocket(hSocket)) {
394 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
395 CloseSocket(hSocket);
396 return nullptr;
399 if (pszDest && addrConnect.IsValid()) {
400 // It is possible that we already have a connection to the IP/port pszDest resolved to.
401 // In that case, drop the connection that was just created, and return the existing CNode instead.
402 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
403 // name catch this early.
404 LOCK(cs_vNodes);
405 CNode* pnode = FindNode((CService)addrConnect);
406 if (pnode)
408 pnode->MaybeSetAddrName(std::string(pszDest));
409 CloseSocket(hSocket);
410 LogPrintf("Failed to open new connection, already connected\n");
411 return nullptr;
415 addrman.Attempt(addrConnect, fCountFailure);
417 // Add node
418 NodeId id = GetNewNodeId();
419 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
420 CAddress addr_bind = GetBindAddress(hSocket);
421 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", false);
422 pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
423 pnode->AddRef();
425 return pnode;
426 } else if (!proxyConnectionFailed) {
427 // If connecting to the node failed, and failure is not caused by a problem connecting to
428 // the proxy, mark this as an attempt.
429 addrman.Attempt(addrConnect, fCountFailure);
432 return nullptr;
435 void CConnman::DumpBanlist()
437 SweepBanned(); // clean unused entries (if bantime has expired)
439 if (!BannedSetIsDirty())
440 return;
442 int64_t nStart = GetTimeMillis();
444 CBanDB bandb;
445 banmap_t banmap;
446 GetBanned(banmap);
447 if (bandb.Write(banmap)) {
448 SetBannedSetDirty(false);
451 LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
452 banmap.size(), GetTimeMillis() - nStart);
455 void CNode::CloseSocketDisconnect()
457 fDisconnect = true;
458 LOCK(cs_hSocket);
459 if (hSocket != INVALID_SOCKET)
461 LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
462 CloseSocket(hSocket);
466 void CConnman::ClearBanned()
469 LOCK(cs_setBanned);
470 setBanned.clear();
471 setBannedIsDirty = true;
473 DumpBanlist(); //store banlist to disk
474 if(clientInterface)
475 clientInterface->BannedListChanged();
478 bool CConnman::IsBanned(CNetAddr ip)
480 LOCK(cs_setBanned);
481 for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++)
483 CSubNet subNet = (*it).first;
484 CBanEntry banEntry = (*it).second;
486 if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
487 return true;
490 return false;
493 bool CConnman::IsBanned(CSubNet subnet)
495 LOCK(cs_setBanned);
496 banmap_t::iterator i = setBanned.find(subnet);
497 if (i != setBanned.end())
499 CBanEntry banEntry = (*i).second;
500 if (GetTime() < banEntry.nBanUntil) {
501 return true;
504 return false;
507 void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
508 CSubNet subNet(addr);
509 Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
512 void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
513 CBanEntry banEntry(GetTime());
514 banEntry.banReason = banReason;
515 if (bantimeoffset <= 0)
517 bantimeoffset = gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
518 sinceUnixEpoch = false;
520 banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
523 LOCK(cs_setBanned);
524 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
525 setBanned[subNet] = banEntry;
526 setBannedIsDirty = true;
528 else
529 return;
531 if(clientInterface)
532 clientInterface->BannedListChanged();
534 LOCK(cs_vNodes);
535 for (CNode* pnode : vNodes) {
536 if (subNet.Match((CNetAddr)pnode->addr))
537 pnode->fDisconnect = true;
540 if(banReason == BanReasonManuallyAdded)
541 DumpBanlist(); //store banlist to disk immediately if user requested ban
544 bool CConnman::Unban(const CNetAddr &addr) {
545 CSubNet subNet(addr);
546 return Unban(subNet);
549 bool CConnman::Unban(const CSubNet &subNet) {
551 LOCK(cs_setBanned);
552 if (!setBanned.erase(subNet))
553 return false;
554 setBannedIsDirty = true;
556 if(clientInterface)
557 clientInterface->BannedListChanged();
558 DumpBanlist(); //store banlist to disk immediately
559 return true;
562 void CConnman::GetBanned(banmap_t &banMap)
564 LOCK(cs_setBanned);
565 // Sweep the banlist so expired bans are not returned
566 SweepBanned();
567 banMap = setBanned; //create a thread safe copy
570 void CConnman::SetBanned(const banmap_t &banMap)
572 LOCK(cs_setBanned);
573 setBanned = banMap;
574 setBannedIsDirty = true;
577 void CConnman::SweepBanned()
579 int64_t now = GetTime();
581 LOCK(cs_setBanned);
582 banmap_t::iterator it = setBanned.begin();
583 while(it != setBanned.end())
585 CSubNet subNet = (*it).first;
586 CBanEntry banEntry = (*it).second;
587 if(now > banEntry.nBanUntil)
589 setBanned.erase(it++);
590 setBannedIsDirty = true;
591 LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
593 else
594 ++it;
598 bool CConnman::BannedSetIsDirty()
600 LOCK(cs_setBanned);
601 return setBannedIsDirty;
604 void CConnman::SetBannedSetDirty(bool dirty)
606 LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
607 setBannedIsDirty = dirty;
611 bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
612 for (const CSubNet& subnet : vWhitelistedRange) {
613 if (subnet.Match(addr))
614 return true;
616 return false;
619 std::string CNode::GetAddrName() const {
620 LOCK(cs_addrName);
621 return addrName;
624 void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
625 LOCK(cs_addrName);
626 if (addrName.empty()) {
627 addrName = addrNameIn;
631 CService CNode::GetAddrLocal() const {
632 LOCK(cs_addrLocal);
633 return addrLocal;
636 void CNode::SetAddrLocal(const CService& addrLocalIn) {
637 LOCK(cs_addrLocal);
638 if (addrLocal.IsValid()) {
639 error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
640 } else {
641 addrLocal = addrLocalIn;
645 #undef X
646 #define X(name) stats.name = name
647 void CNode::copyStats(CNodeStats &stats)
649 stats.nodeid = this->GetId();
650 X(nServices);
651 X(addr);
652 X(addrBind);
654 LOCK(cs_filter);
655 X(fRelayTxes);
657 X(nLastSend);
658 X(nLastRecv);
659 X(nTimeConnected);
660 X(nTimeOffset);
661 stats.addrName = GetAddrName();
662 X(nVersion);
664 LOCK(cs_SubVer);
665 X(cleanSubVer);
667 X(fInbound);
668 X(fAddnode);
669 X(nStartingHeight);
671 LOCK(cs_vSend);
672 X(mapSendBytesPerMsgCmd);
673 X(nSendBytes);
676 LOCK(cs_vRecv);
677 X(mapRecvBytesPerMsgCmd);
678 X(nRecvBytes);
680 X(fWhitelisted);
682 // It is common for nodes with good ping times to suddenly become lagged,
683 // due to a new block arriving or other large transfer.
684 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
685 // since pingtime does not update until the ping is complete, which might take a while.
686 // So, if a ping is taking an unusually long time in flight,
687 // the caller can immediately detect that this is happening.
688 int64_t nPingUsecWait = 0;
689 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
690 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
693 // 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 :)
694 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
695 stats.dMinPing = (((double)nMinPingUsecTime) / 1e6);
696 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
698 // Leave string empty if addrLocal invalid (not filled in yet)
699 CService addrLocalUnlocked = GetAddrLocal();
700 stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
702 #undef X
704 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
706 complete = false;
707 int64_t nTimeMicros = GetTimeMicros();
708 LOCK(cs_vRecv);
709 nLastRecv = nTimeMicros / 1000000;
710 nRecvBytes += nBytes;
711 while (nBytes > 0) {
713 // get current incomplete message, or create a new one
714 if (vRecvMsg.empty() ||
715 vRecvMsg.back().complete())
716 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
718 CNetMessage& msg = vRecvMsg.back();
720 // absorb network data
721 int handled;
722 if (!msg.in_data)
723 handled = msg.readHeader(pch, nBytes);
724 else
725 handled = msg.readData(pch, nBytes);
727 if (handled < 0)
728 return false;
730 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
731 LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
732 return false;
735 pch += handled;
736 nBytes -= handled;
738 if (msg.complete()) {
740 //store received bytes per message command
741 //to prevent a memory DOS, only allow valid commands
742 mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
743 if (i == mapRecvBytesPerMsgCmd.end())
744 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
745 assert(i != mapRecvBytesPerMsgCmd.end());
746 i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
748 msg.nTime = nTimeMicros;
749 complete = true;
753 return true;
756 void CNode::SetSendVersion(int nVersionIn)
758 // Send version may only be changed in the version message, and
759 // only one version message is allowed per session. We can therefore
760 // treat this value as const and even atomic as long as it's only used
761 // once a version message has been successfully processed. Any attempt to
762 // set this twice is an error.
763 if (nSendVersion != 0) {
764 error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
765 } else {
766 nSendVersion = nVersionIn;
770 int CNode::GetSendVersion() const
772 // The send version should always be explicitly set to
773 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
774 // has been called.
775 if (nSendVersion == 0) {
776 error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
777 return INIT_PROTO_VERSION;
779 return nSendVersion;
783 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
785 // copy data to temporary parsing buffer
786 unsigned int nRemaining = 24 - nHdrPos;
787 unsigned int nCopy = std::min(nRemaining, nBytes);
789 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
790 nHdrPos += nCopy;
792 // if header incomplete, exit
793 if (nHdrPos < 24)
794 return nCopy;
796 // deserialize to CMessageHeader
797 try {
798 hdrbuf >> hdr;
800 catch (const std::exception&) {
801 return -1;
804 // reject messages larger than MAX_SIZE
805 if (hdr.nMessageSize > MAX_SIZE)
806 return -1;
808 // switch state to reading message data
809 in_data = true;
811 return nCopy;
814 int CNetMessage::readData(const char *pch, unsigned int nBytes)
816 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
817 unsigned int nCopy = std::min(nRemaining, nBytes);
819 if (vRecv.size() < nDataPos + nCopy) {
820 // Allocate up to 256 KiB ahead, but never more than the total message size.
821 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
824 hasher.Write((const unsigned char*)pch, nCopy);
825 memcpy(&vRecv[nDataPos], pch, nCopy);
826 nDataPos += nCopy;
828 return nCopy;
831 const uint256& CNetMessage::GetMessageHash() const
833 assert(complete());
834 if (data_hash.IsNull())
835 hasher.Finalize(data_hash.begin());
836 return data_hash;
847 // requires LOCK(cs_vSend)
848 size_t CConnman::SocketSendData(CNode *pnode) const
850 auto it = pnode->vSendMsg.begin();
851 size_t nSentSize = 0;
853 while (it != pnode->vSendMsg.end()) {
854 const auto &data = *it;
855 assert(data.size() > pnode->nSendOffset);
856 int nBytes = 0;
858 LOCK(pnode->cs_hSocket);
859 if (pnode->hSocket == INVALID_SOCKET)
860 break;
861 nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
863 if (nBytes > 0) {
864 pnode->nLastSend = GetSystemTimeInSeconds();
865 pnode->nSendBytes += nBytes;
866 pnode->nSendOffset += nBytes;
867 nSentSize += nBytes;
868 if (pnode->nSendOffset == data.size()) {
869 pnode->nSendOffset = 0;
870 pnode->nSendSize -= data.size();
871 pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
872 it++;
873 } else {
874 // could not send full message; stop sending more
875 break;
877 } else {
878 if (nBytes < 0) {
879 // error
880 int nErr = WSAGetLastError();
881 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
883 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
884 pnode->CloseSocketDisconnect();
887 // couldn't send anything at all
888 break;
892 if (it == pnode->vSendMsg.end()) {
893 assert(pnode->nSendOffset == 0);
894 assert(pnode->nSendSize == 0);
896 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
897 return nSentSize;
900 struct NodeEvictionCandidate
902 NodeId id;
903 int64_t nTimeConnected;
904 int64_t nMinPingUsecTime;
905 int64_t nLastBlockTime;
906 int64_t nLastTXTime;
907 bool fRelevantServices;
908 bool fRelayTxes;
909 bool fBloomFilter;
910 CAddress addr;
911 uint64_t nKeyedNetGroup;
914 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
916 return a.nMinPingUsecTime > b.nMinPingUsecTime;
919 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
921 return a.nTimeConnected > b.nTimeConnected;
924 static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
925 return a.nKeyedNetGroup < b.nKeyedNetGroup;
928 static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
930 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
931 if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
932 if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
933 return a.nTimeConnected > b.nTimeConnected;
936 static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
938 // 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.
939 if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
940 if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
941 if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
942 return a.nTimeConnected > b.nTimeConnected;
945 /** Try to find a connection to evict when the node is full.
946 * Extreme care must be taken to avoid opening the node to attacker
947 * triggered network partitioning.
948 * The strategy used here is to protect a small number of peers
949 * for each of several distinct characteristics which are difficult
950 * to forge. In order to partition a node the attacker must be
951 * simultaneously better at all of them than honest peers.
953 bool CConnman::AttemptToEvictConnection()
955 std::vector<NodeEvictionCandidate> vEvictionCandidates;
957 LOCK(cs_vNodes);
959 for (CNode *node : vNodes) {
960 if (node->fWhitelisted)
961 continue;
962 if (!node->fInbound)
963 continue;
964 if (node->fDisconnect)
965 continue;
966 NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
967 node->nLastBlockTime, node->nLastTXTime,
968 (node->nServices & nRelevantServices) == nRelevantServices,
969 node->fRelayTxes, node->pfilter != nullptr, node->addr, node->nKeyedNetGroup};
970 vEvictionCandidates.push_back(candidate);
974 if (vEvictionCandidates.empty()) return false;
976 // Protect connections with certain characteristics
978 // Deterministically select 4 peers to protect by netgroup.
979 // An attacker cannot predict which netgroups will be protected
980 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNetGroupKeyed);
981 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
983 if (vEvictionCandidates.empty()) return false;
985 // Protect the 8 nodes with the lowest minimum ping time.
986 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
987 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);
988 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
990 if (vEvictionCandidates.empty()) return false;
992 // Protect 4 nodes that most recently sent us transactions.
993 // An attacker cannot manipulate this metric without performing useful work.
994 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeTXTime);
995 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
997 if (vEvictionCandidates.empty()) return false;
999 // Protect 4 nodes that most recently sent us blocks.
1000 // An attacker cannot manipulate this metric without performing useful work.
1001 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeBlockTime);
1002 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
1004 if (vEvictionCandidates.empty()) return false;
1006 // Protect the half of the remaining nodes which have been connected the longest.
1007 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1008 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
1009 vEvictionCandidates.erase(vEvictionCandidates.end() - static_cast<int>(vEvictionCandidates.size() / 2), vEvictionCandidates.end());
1011 if (vEvictionCandidates.empty()) return false;
1013 // Identify the network group with the most connections and youngest member.
1014 // (vEvictionCandidates is already sorted by reverse connect time)
1015 uint64_t naMostConnections;
1016 unsigned int nMostConnections = 0;
1017 int64_t nMostConnectionsTime = 0;
1018 std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
1019 for (const NodeEvictionCandidate &node : vEvictionCandidates) {
1020 mapNetGroupNodes[node.nKeyedNetGroup].push_back(node);
1021 int64_t grouptime = mapNetGroupNodes[node.nKeyedNetGroup][0].nTimeConnected;
1022 size_t groupsize = mapNetGroupNodes[node.nKeyedNetGroup].size();
1024 if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
1025 nMostConnections = groupsize;
1026 nMostConnectionsTime = grouptime;
1027 naMostConnections = node.nKeyedNetGroup;
1031 // Reduce to the network group with the most connections
1032 vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1034 // Disconnect from the network group with the most connections
1035 NodeId evicted = vEvictionCandidates.front().id;
1036 LOCK(cs_vNodes);
1037 for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
1038 if ((*it)->GetId() == evicted) {
1039 (*it)->fDisconnect = true;
1040 return true;
1043 return false;
1046 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1047 struct sockaddr_storage sockaddr;
1048 socklen_t len = sizeof(sockaddr);
1049 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
1050 CAddress addr;
1051 int nInbound = 0;
1052 int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler);
1054 if (hSocket != INVALID_SOCKET) {
1055 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1056 LogPrintf("Warning: Unknown socket family\n");
1060 bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
1062 LOCK(cs_vNodes);
1063 for (CNode* pnode : vNodes)
1064 if (pnode->fInbound)
1065 nInbound++;
1068 if (hSocket == INVALID_SOCKET)
1070 int nErr = WSAGetLastError();
1071 if (nErr != WSAEWOULDBLOCK)
1072 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1073 return;
1076 if (!fNetworkActive) {
1077 LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
1078 CloseSocket(hSocket);
1079 return;
1082 if (!IsSelectableSocket(hSocket))
1084 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
1085 CloseSocket(hSocket);
1086 return;
1089 // According to the internet TCP_NODELAY is not carried into accepted sockets
1090 // on all platforms. Set it again here just to be sure.
1091 SetSocketNoDelay(hSocket);
1093 if (IsBanned(addr) && !whitelisted)
1095 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
1096 CloseSocket(hSocket);
1097 return;
1100 if (nInbound >= nMaxInbound)
1102 if (!AttemptToEvictConnection()) {
1103 // No connection to evict, disconnect the new connection
1104 LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1105 CloseSocket(hSocket);
1106 return;
1110 NodeId id = GetNewNodeId();
1111 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1112 CAddress addr_bind = GetBindAddress(hSocket);
1114 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
1115 pnode->AddRef();
1116 pnode->fWhitelisted = whitelisted;
1117 GetNodeSignals().InitializeNode(pnode, *this);
1119 LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1122 LOCK(cs_vNodes);
1123 vNodes.push_back(pnode);
1127 void CConnman::ThreadSocketHandler()
1129 unsigned int nPrevNodeCount = 0;
1130 while (!interruptNet)
1133 // Disconnect nodes
1136 LOCK(cs_vNodes);
1137 // Disconnect unused nodes
1138 std::vector<CNode*> vNodesCopy = vNodes;
1139 for (CNode* pnode : vNodesCopy)
1141 if (pnode->fDisconnect)
1143 // remove from vNodes
1144 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1146 // release outbound grant (if any)
1147 pnode->grantOutbound.Release();
1149 // close socket and cleanup
1150 pnode->CloseSocketDisconnect();
1152 // hold in disconnected pool until all refs are released
1153 pnode->Release();
1154 vNodesDisconnected.push_back(pnode);
1159 // Delete disconnected nodes
1160 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1161 for (CNode* pnode : vNodesDisconnectedCopy)
1163 // wait until threads are done using it
1164 if (pnode->GetRefCount() <= 0) {
1165 bool fDelete = false;
1167 TRY_LOCK(pnode->cs_inventory, lockInv);
1168 if (lockInv) {
1169 TRY_LOCK(pnode->cs_vSend, lockSend);
1170 if (lockSend) {
1171 fDelete = true;
1175 if (fDelete) {
1176 vNodesDisconnected.remove(pnode);
1177 DeleteNode(pnode);
1182 size_t vNodesSize;
1184 LOCK(cs_vNodes);
1185 vNodesSize = vNodes.size();
1187 if(vNodesSize != nPrevNodeCount) {
1188 nPrevNodeCount = vNodesSize;
1189 if(clientInterface)
1190 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1194 // Find which sockets have data to receive
1196 struct timeval timeout;
1197 timeout.tv_sec = 0;
1198 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1200 fd_set fdsetRecv;
1201 fd_set fdsetSend;
1202 fd_set fdsetError;
1203 FD_ZERO(&fdsetRecv);
1204 FD_ZERO(&fdsetSend);
1205 FD_ZERO(&fdsetError);
1206 SOCKET hSocketMax = 0;
1207 bool have_fds = false;
1209 for (const ListenSocket& hListenSocket : vhListenSocket) {
1210 FD_SET(hListenSocket.socket, &fdsetRecv);
1211 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1212 have_fds = true;
1216 LOCK(cs_vNodes);
1217 for (CNode* pnode : vNodes)
1219 // Implement the following logic:
1220 // * If there is data to send, select() for sending data. As this only
1221 // happens when optimistic write failed, we choose to first drain the
1222 // write buffer in this case before receiving more. This avoids
1223 // needlessly queueing received data, if the remote peer is not themselves
1224 // receiving data. This means properly utilizing TCP flow control signalling.
1225 // * Otherwise, if there is space left in the receive buffer, select() for
1226 // receiving data.
1227 // * Hand off all complete messages to the processor, to be handled without
1228 // blocking here.
1230 bool select_recv = !pnode->fPauseRecv;
1231 bool select_send;
1233 LOCK(pnode->cs_vSend);
1234 select_send = !pnode->vSendMsg.empty();
1237 LOCK(pnode->cs_hSocket);
1238 if (pnode->hSocket == INVALID_SOCKET)
1239 continue;
1241 FD_SET(pnode->hSocket, &fdsetError);
1242 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1243 have_fds = true;
1245 if (select_send) {
1246 FD_SET(pnode->hSocket, &fdsetSend);
1247 continue;
1249 if (select_recv) {
1250 FD_SET(pnode->hSocket, &fdsetRecv);
1255 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1256 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1257 if (interruptNet)
1258 return;
1260 if (nSelect == SOCKET_ERROR)
1262 if (have_fds)
1264 int nErr = WSAGetLastError();
1265 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1266 for (unsigned int i = 0; i <= hSocketMax; i++)
1267 FD_SET(i, &fdsetRecv);
1269 FD_ZERO(&fdsetSend);
1270 FD_ZERO(&fdsetError);
1271 if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
1272 return;
1276 // Accept new connections
1278 for (const ListenSocket& hListenSocket : vhListenSocket)
1280 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1282 AcceptConnection(hListenSocket);
1287 // Service each socket
1289 std::vector<CNode*> vNodesCopy;
1291 LOCK(cs_vNodes);
1292 vNodesCopy = vNodes;
1293 for (CNode* pnode : vNodesCopy)
1294 pnode->AddRef();
1296 for (CNode* pnode : vNodesCopy)
1298 if (interruptNet)
1299 return;
1302 // Receive
1304 bool recvSet = false;
1305 bool sendSet = false;
1306 bool errorSet = false;
1308 LOCK(pnode->cs_hSocket);
1309 if (pnode->hSocket == INVALID_SOCKET)
1310 continue;
1311 recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
1312 sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
1313 errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
1315 if (recvSet || errorSet)
1317 // typical socket buffer is 8K-64K
1318 char pchBuf[0x10000];
1319 int nBytes = 0;
1321 LOCK(pnode->cs_hSocket);
1322 if (pnode->hSocket == INVALID_SOCKET)
1323 continue;
1324 nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1326 if (nBytes > 0)
1328 bool notify = false;
1329 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1330 pnode->CloseSocketDisconnect();
1331 RecordBytesRecv(nBytes);
1332 if (notify) {
1333 size_t nSizeAdded = 0;
1334 auto it(pnode->vRecvMsg.begin());
1335 for (; it != pnode->vRecvMsg.end(); ++it) {
1336 if (!it->complete())
1337 break;
1338 nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1341 LOCK(pnode->cs_vProcessMsg);
1342 pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1343 pnode->nProcessQueueSize += nSizeAdded;
1344 pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1346 WakeMessageHandler();
1349 else if (nBytes == 0)
1351 // socket closed gracefully
1352 if (!pnode->fDisconnect) {
1353 LogPrint(BCLog::NET, "socket closed\n");
1355 pnode->CloseSocketDisconnect();
1357 else if (nBytes < 0)
1359 // error
1360 int nErr = WSAGetLastError();
1361 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1363 if (!pnode->fDisconnect)
1364 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1365 pnode->CloseSocketDisconnect();
1371 // Send
1373 if (sendSet)
1375 LOCK(pnode->cs_vSend);
1376 size_t nBytes = SocketSendData(pnode);
1377 if (nBytes) {
1378 RecordBytesSent(nBytes);
1383 // Inactivity checking
1385 int64_t nTime = GetSystemTimeInSeconds();
1386 if (nTime - pnode->nTimeConnected > 60)
1388 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1390 LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1391 pnode->fDisconnect = true;
1393 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1395 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1396 pnode->fDisconnect = true;
1398 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1400 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1401 pnode->fDisconnect = true;
1403 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1405 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1406 pnode->fDisconnect = true;
1408 else if (!pnode->fSuccessfullyConnected)
1410 LogPrintf("version handshake timeout from %d\n", pnode->GetId());
1411 pnode->fDisconnect = true;
1416 LOCK(cs_vNodes);
1417 for (CNode* pnode : vNodesCopy)
1418 pnode->Release();
1423 void CConnman::WakeMessageHandler()
1426 std::lock_guard<std::mutex> lock(mutexMsgProc);
1427 fMsgProcWake = true;
1429 condMsgProc.notify_one();
1437 #ifdef USE_UPNP
1438 void ThreadMapPort()
1440 std::string port = strprintf("%u", GetListenPort());
1441 const char * multicastif = nullptr;
1442 const char * minissdpdpath = nullptr;
1443 struct UPNPDev * devlist = nullptr;
1444 char lanaddr[64];
1446 #ifndef UPNPDISCOVER_SUCCESS
1447 /* miniupnpc 1.5 */
1448 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1449 #elif MINIUPNPC_API_VERSION < 14
1450 /* miniupnpc 1.6 */
1451 int error = 0;
1452 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1453 #else
1454 /* miniupnpc 1.9.20150730 */
1455 int error = 0;
1456 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1457 #endif
1459 struct UPNPUrls urls;
1460 struct IGDdatas data;
1461 int r;
1463 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1464 if (r == 1)
1466 if (fDiscover) {
1467 char externalIPAddress[40];
1468 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1469 if(r != UPNPCOMMAND_SUCCESS)
1470 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1471 else
1473 if(externalIPAddress[0])
1475 CNetAddr resolved;
1476 if(LookupHost(externalIPAddress, resolved, false)) {
1477 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1478 AddLocal(resolved, LOCAL_UPNP);
1481 else
1482 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1486 std::string strDesc = "Bitcoin " + FormatFullVersion();
1488 try {
1489 while (true) {
1490 #ifndef UPNPDISCOVER_SUCCESS
1491 /* miniupnpc 1.5 */
1492 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1493 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1494 #else
1495 /* miniupnpc 1.6 */
1496 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1497 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1498 #endif
1500 if(r!=UPNPCOMMAND_SUCCESS)
1501 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1502 port, port, lanaddr, r, strupnperror(r));
1503 else
1504 LogPrintf("UPnP Port Mapping successful.\n");
1506 MilliSleep(20*60*1000); // Refresh every 20 minutes
1509 catch (const boost::thread_interrupted&)
1511 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1512 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1513 freeUPNPDevlist(devlist); devlist = nullptr;
1514 FreeUPNPUrls(&urls);
1515 throw;
1517 } else {
1518 LogPrintf("No valid UPnP IGDs found\n");
1519 freeUPNPDevlist(devlist); devlist = nullptr;
1520 if (r != 0)
1521 FreeUPNPUrls(&urls);
1525 void MapPort(bool fUseUPnP)
1527 static boost::thread* upnp_thread = nullptr;
1529 if (fUseUPnP)
1531 if (upnp_thread) {
1532 upnp_thread->interrupt();
1533 upnp_thread->join();
1534 delete upnp_thread;
1536 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1538 else if (upnp_thread) {
1539 upnp_thread->interrupt();
1540 upnp_thread->join();
1541 delete upnp_thread;
1542 upnp_thread = nullptr;
1546 #else
1547 void MapPort(bool)
1549 // Intentionally left blank.
1551 #endif
1558 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1560 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1561 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1562 *requiredServiceBits = NODE_NETWORK;
1563 return data.host;
1566 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1567 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1571 void CConnman::ThreadDNSAddressSeed()
1573 // goal: only query DNS seeds if address need is acute
1574 // Avoiding DNS seeds when we don't need them improves user privacy by
1575 // creating fewer identifying DNS requests, reduces trust by giving seeds
1576 // less influence on the network topology, and reduces traffic to the seeds.
1577 if ((addrman.size() > 0) &&
1578 (!gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1579 if (!interruptNet.sleep_for(std::chrono::seconds(11)))
1580 return;
1582 LOCK(cs_vNodes);
1583 int nRelevant = 0;
1584 for (auto pnode : vNodes) {
1585 nRelevant += pnode->fSuccessfullyConnected && ((pnode->nServices & nRelevantServices) == nRelevantServices);
1587 if (nRelevant >= 2) {
1588 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1589 return;
1593 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1594 int found = 0;
1596 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1598 for (const CDNSSeedData &seed : vSeeds) {
1599 if (interruptNet) {
1600 return;
1602 if (HaveNameProxy()) {
1603 AddOneShot(seed.host);
1604 } else {
1605 std::vector<CNetAddr> vIPs;
1606 std::vector<CAddress> vAdd;
1607 ServiceFlags requiredServiceBits = nRelevantServices;
1608 std::string host = GetDNSHost(seed, &requiredServiceBits);
1609 CNetAddr resolveSource;
1610 if (!resolveSource.SetInternal(host)) {
1611 continue;
1613 if (LookupHost(host.c_str(), vIPs, 0, true))
1615 for (const CNetAddr& ip : vIPs)
1617 int nOneDay = 24*3600;
1618 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
1619 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1620 vAdd.push_back(addr);
1621 found++;
1623 addrman.Add(vAdd, resolveSource);
1628 LogPrintf("%d addresses found from DNS seeds\n", found);
1642 void CConnman::DumpAddresses()
1644 int64_t nStart = GetTimeMillis();
1646 CAddrDB adb;
1647 adb.Write(addrman);
1649 LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1650 addrman.size(), GetTimeMillis() - nStart);
1653 void CConnman::DumpData()
1655 DumpAddresses();
1656 DumpBanlist();
1659 void CConnman::ProcessOneShot()
1661 std::string strDest;
1663 LOCK(cs_vOneShots);
1664 if (vOneShots.empty())
1665 return;
1666 strDest = vOneShots.front();
1667 vOneShots.pop_front();
1669 CAddress addr;
1670 CSemaphoreGrant grant(*semOutbound, true);
1671 if (grant) {
1672 if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
1673 AddOneShot(strDest);
1677 void CConnman::ThreadOpenConnections()
1679 // Connect to specific addresses
1680 if (gArgs.IsArgSet("-connect"))
1682 for (int64_t nLoop = 0;; nLoop++)
1684 ProcessOneShot();
1685 for (const std::string& strAddr : gArgs.GetArgs("-connect"))
1687 CAddress addr(CService(), NODE_NONE);
1688 OpenNetworkConnection(addr, false, nullptr, strAddr.c_str());
1689 for (int i = 0; i < 10 && i < nLoop; i++)
1691 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1692 return;
1695 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1696 return;
1700 // Initiate network connections
1701 int64_t nStart = GetTime();
1703 // Minimum time before next feeler connection (in microseconds).
1704 int64_t nNextFeeler = PoissonNextSend(nStart*1000*1000, FEELER_INTERVAL);
1705 while (!interruptNet)
1707 ProcessOneShot();
1709 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1710 return;
1712 CSemaphoreGrant grant(*semOutbound);
1713 if (interruptNet)
1714 return;
1716 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1717 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1718 static bool done = false;
1719 if (!done) {
1720 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1721 CNetAddr local;
1722 local.SetInternal("fixedseeds");
1723 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1724 done = true;
1729 // Choose an address to connect to based on most recently seen
1731 CAddress addrConnect;
1733 // Only connect out to one peer per network group (/16 for IPv4).
1734 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1735 int nOutbound = 0;
1736 int nOutboundRelevant = 0;
1737 std::set<std::vector<unsigned char> > setConnected;
1739 LOCK(cs_vNodes);
1740 for (CNode* pnode : vNodes) {
1741 if (!pnode->fInbound && !pnode->fAddnode) {
1743 // Count the peers that have all relevant services
1744 if (pnode->fSuccessfullyConnected && !pnode->fFeeler && ((pnode->nServices & nRelevantServices) == nRelevantServices)) {
1745 nOutboundRelevant++;
1747 // Netgroups for inbound and addnode peers are not excluded because our goal here
1748 // is to not use multiple of our limited outbound slots on a single netgroup
1749 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1750 // also have the added issue that they're attacker controlled and could be used
1751 // to prevent us from connecting to particular hosts if we used them here.
1752 setConnected.insert(pnode->addr.GetGroup());
1753 nOutbound++;
1758 // Feeler Connections
1760 // Design goals:
1761 // * Increase the number of connectable addresses in the tried table.
1763 // Method:
1764 // * Choose a random address from new and attempt to connect to it if we can connect
1765 // successfully it is added to tried.
1766 // * Start attempting feeler connections only after node finishes making outbound
1767 // connections.
1768 // * Only make a feeler connection once every few minutes.
1770 bool fFeeler = false;
1771 if (nOutbound >= nMaxOutbound) {
1772 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1773 if (nTime > nNextFeeler) {
1774 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1775 fFeeler = true;
1776 } else {
1777 continue;
1781 int64_t nANow = GetAdjustedTime();
1782 int nTries = 0;
1783 while (!interruptNet)
1785 CAddrInfo addr = addrman.Select(fFeeler);
1787 // if we selected an invalid address, restart
1788 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1789 break;
1791 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1792 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1793 // already-connected network ranges, ...) before trying new addrman addresses.
1794 nTries++;
1795 if (nTries > 100)
1796 break;
1798 if (IsLimited(addr))
1799 continue;
1801 // only connect to full nodes
1802 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1803 continue;
1805 // only consider very recently tried nodes after 30 failed attempts
1806 if (nANow - addr.nLastTry < 600 && nTries < 30)
1807 continue;
1809 // only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1810 ServiceFlags nRequiredServices = nRelevantServices;
1811 if (nTries >= 40 && nOutbound < (nMaxOutbound >> 1)) {
1812 nRequiredServices = REQUIRED_SERVICES;
1815 if ((addr.nServices & nRequiredServices) != nRequiredServices) {
1816 continue;
1819 // do not allow non-default ports, unless after 50 invalid addresses selected already
1820 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1821 continue;
1823 addrConnect = addr;
1825 // regardless of the services assumed to be available, only require the minimum if half or more outbound have relevant services
1826 if (nOutboundRelevant >= (nMaxOutbound >> 1)) {
1827 addrConnect.nServices = REQUIRED_SERVICES;
1828 } else {
1829 addrConnect.nServices = nRequiredServices;
1831 break;
1834 if (addrConnect.IsValid()) {
1836 if (fFeeler) {
1837 // Add small amount of random noise before connection to avoid synchronization.
1838 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1839 if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1840 return;
1841 LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
1844 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler);
1849 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1851 std::vector<AddedNodeInfo> ret;
1853 std::list<std::string> lAddresses(0);
1855 LOCK(cs_vAddedNodes);
1856 ret.reserve(vAddedNodes.size());
1857 for (const std::string& strAddNode : vAddedNodes)
1858 lAddresses.push_back(strAddNode);
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()
1905 LOCK(cs_vAddedNodes);
1906 vAddedNodes = gArgs.GetArgs("-addnode");
1909 while (true)
1911 CSemaphoreGrant grant(*semAddnode);
1912 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1913 bool tried = false;
1914 for (const AddedNodeInfo& info : vInfo) {
1915 if (!info.fConnected) {
1916 if (!grant.TryAcquire()) {
1917 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1918 // the addednodeinfo state might change.
1919 break;
1921 // If strAddedNode is an IP/port, decode it immediately, so
1922 // OpenNetworkConnection can detect existing connections to that IP/port.
1923 tried = true;
1924 CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
1925 OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false, false, true);
1926 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1927 return;
1930 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1931 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
1932 return;
1936 // if successful, this moves the passed grant to the constructed node
1937 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool fAddnode)
1940 // Initiate outbound network connection
1942 if (interruptNet) {
1943 return false;
1945 if (!fNetworkActive) {
1946 return false;
1948 if (!pszDest) {
1949 if (IsLocal(addrConnect) ||
1950 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1951 FindNode(addrConnect.ToStringIPPort()))
1952 return false;
1953 } else if (FindNode(std::string(pszDest)))
1954 return false;
1956 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1958 if (!pnode)
1959 return false;
1960 if (grantOutbound)
1961 grantOutbound->MoveTo(pnode->grantOutbound);
1962 if (fOneShot)
1963 pnode->fOneShot = true;
1964 if (fFeeler)
1965 pnode->fFeeler = true;
1966 if (fAddnode)
1967 pnode->fAddnode = true;
1969 GetNodeSignals().InitializeNode(pnode, *this);
1971 LOCK(cs_vNodes);
1972 vNodes.push_back(pnode);
1975 return true;
1978 void CConnman::ThreadMessageHandler()
1980 while (!flagInterruptMsgProc)
1982 std::vector<CNode*> vNodesCopy;
1984 LOCK(cs_vNodes);
1985 vNodesCopy = vNodes;
1986 for (CNode* pnode : vNodesCopy) {
1987 pnode->AddRef();
1991 bool fMoreWork = false;
1993 for (CNode* pnode : vNodesCopy)
1995 if (pnode->fDisconnect)
1996 continue;
1998 // Receive messages
1999 bool fMoreNodeWork = GetNodeSignals().ProcessMessages(pnode, *this, flagInterruptMsgProc);
2000 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
2001 if (flagInterruptMsgProc)
2002 return;
2004 // Send messages
2006 LOCK(pnode->cs_sendProcessing);
2007 GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
2009 if (flagInterruptMsgProc)
2010 return;
2014 LOCK(cs_vNodes);
2015 for (CNode* pnode : vNodesCopy)
2016 pnode->Release();
2019 std::unique_lock<std::mutex> lock(mutexMsgProc);
2020 if (!fMoreWork) {
2021 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
2023 fMsgProcWake = false;
2032 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
2034 strError = "";
2035 int nOne = 1;
2037 // Create socket for listening for incoming connections
2038 struct sockaddr_storage sockaddr;
2039 socklen_t len = sizeof(sockaddr);
2040 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
2042 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
2043 LogPrintf("%s\n", strError);
2044 return false;
2047 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2048 if (hListenSocket == INVALID_SOCKET)
2050 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2051 LogPrintf("%s\n", strError);
2052 return false;
2054 if (!IsSelectableSocket(hListenSocket))
2056 strError = "Error: Couldn't create a listenable socket for incoming connections";
2057 LogPrintf("%s\n", strError);
2058 return false;
2062 #ifndef WIN32
2063 #ifdef SO_NOSIGPIPE
2064 // Different way of disabling SIGPIPE on BSD
2065 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
2066 #endif
2067 // Allow binding if the port is still in TIME_WAIT state after
2068 // the program was closed and restarted.
2069 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2070 // Disable Nagle's algorithm
2071 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
2072 #else
2073 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2074 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
2075 #endif
2077 // Set to non-blocking, incoming connections will also inherit this
2078 if (!SetSocketNonBlocking(hListenSocket, true)) {
2079 CloseSocket(hListenSocket);
2080 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2081 LogPrintf("%s\n", strError);
2082 return false;
2085 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2086 // and enable it by default or not. Try to enable it, if possible.
2087 if (addrBind.IsIPv6()) {
2088 #ifdef IPV6_V6ONLY
2089 #ifdef WIN32
2090 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2091 #else
2092 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2093 #endif
2094 #endif
2095 #ifdef WIN32
2096 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2097 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2098 #endif
2101 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2103 int nErr = WSAGetLastError();
2104 if (nErr == WSAEADDRINUSE)
2105 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
2106 else
2107 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2108 LogPrintf("%s\n", strError);
2109 CloseSocket(hListenSocket);
2110 return false;
2112 LogPrintf("Bound to %s\n", addrBind.ToString());
2114 // Listen for incoming connections
2115 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
2117 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2118 LogPrintf("%s\n", strError);
2119 CloseSocket(hListenSocket);
2120 return false;
2123 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
2125 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
2126 AddLocal(addrBind, LOCAL_BIND);
2128 return true;
2131 void Discover(boost::thread_group& threadGroup)
2133 if (!fDiscover)
2134 return;
2136 #ifdef WIN32
2137 // Get local host IP
2138 char pszHostName[256] = "";
2139 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
2141 std::vector<CNetAddr> vaddr;
2142 if (LookupHost(pszHostName, vaddr, 0, true))
2144 for (const CNetAddr &addr : vaddr)
2146 if (AddLocal(addr, LOCAL_IF))
2147 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
2151 #else
2152 // Get local host ip
2153 struct ifaddrs* myaddrs;
2154 if (getifaddrs(&myaddrs) == 0)
2156 for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
2158 if (ifa->ifa_addr == nullptr) continue;
2159 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2160 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2161 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2162 if (ifa->ifa_addr->sa_family == AF_INET)
2164 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2165 CNetAddr addr(s4->sin_addr);
2166 if (AddLocal(addr, LOCAL_IF))
2167 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2169 else if (ifa->ifa_addr->sa_family == AF_INET6)
2171 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2172 CNetAddr addr(s6->sin6_addr);
2173 if (AddLocal(addr, LOCAL_IF))
2174 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2177 freeifaddrs(myaddrs);
2179 #endif
2182 void CConnman::SetNetworkActive(bool active)
2184 LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2186 if (fNetworkActive == active) {
2187 return;
2190 fNetworkActive = active;
2192 if (!fNetworkActive) {
2193 LOCK(cs_vNodes);
2194 // Close sockets to all nodes
2195 for (CNode* pnode : vNodes) {
2196 pnode->CloseSocketDisconnect();
2200 uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2203 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2205 fNetworkActive = true;
2206 setBannedIsDirty = false;
2207 fAddressesInitialized = false;
2208 nLastNodeId = 0;
2209 nSendBufferMaxSize = 0;
2210 nReceiveFloodSize = 0;
2211 semOutbound = nullptr;
2212 semAddnode = nullptr;
2213 flagInterruptMsgProc = false;
2215 Options connOptions;
2216 Init(connOptions);
2219 NodeId CConnman::GetNewNodeId()
2221 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2225 bool CConnman::Bind(const CService &addr, unsigned int flags) {
2226 if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2227 return false;
2228 std::string strError;
2229 if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
2230 if ((flags & BF_REPORT_ERROR) && clientInterface) {
2231 clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
2233 return false;
2235 return true;
2238 bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2239 bool fBound = false;
2240 for (const auto& addrBind : binds) {
2241 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2243 for (const auto& addrBind : whiteBinds) {
2244 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
2246 if (binds.empty() && whiteBinds.empty()) {
2247 struct in_addr inaddr_any;
2248 inaddr_any.s_addr = INADDR_ANY;
2249 fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
2250 fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2252 return fBound;
2255 bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2257 Init(connOptions);
2259 nTotalBytesRecv = 0;
2260 nTotalBytesSent = 0;
2261 nMaxOutboundTotalBytesSentInCycle = 0;
2262 nMaxOutboundCycleStartTime = 0;
2264 if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2265 if (clientInterface) {
2266 clientInterface->ThreadSafeMessageBox(
2267 _("Failed to listen on any port. Use -listen=0 if you want this."),
2268 "", CClientUIInterface::MSG_ERROR);
2270 return false;
2273 for (const auto& strDest : connOptions.vSeedNodes) {
2274 AddOneShot(strDest);
2277 if (clientInterface) {
2278 clientInterface->InitMessage(_("Loading P2P addresses..."));
2280 // Load addresses from peers.dat
2281 int64_t nStart = GetTimeMillis();
2283 CAddrDB adb;
2284 if (adb.Read(addrman))
2285 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2286 else {
2287 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2288 LogPrintf("Invalid or missing peers.dat; recreating\n");
2289 DumpAddresses();
2292 if (clientInterface)
2293 clientInterface->InitMessage(_("Loading banlist..."));
2294 // Load addresses from banlist.dat
2295 nStart = GetTimeMillis();
2296 CBanDB bandb;
2297 banmap_t banmap;
2298 if (bandb.Read(banmap)) {
2299 SetBanned(banmap); // thread save setter
2300 SetBannedSetDirty(false); // no need to write down, just read data
2301 SweepBanned(); // sweep out unused entries
2303 LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2304 banmap.size(), GetTimeMillis() - nStart);
2305 } else {
2306 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2307 SetBannedSetDirty(true); // force write
2308 DumpBanlist();
2311 uiInterface.InitMessage(_("Starting network threads..."));
2313 fAddressesInitialized = true;
2315 if (semOutbound == nullptr) {
2316 // initialize semaphore
2317 semOutbound = new CSemaphore(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
2319 if (semAddnode == nullptr) {
2320 // initialize semaphore
2321 semAddnode = new CSemaphore(nMaxAddnode);
2325 // Start threads
2327 InterruptSocks5(false);
2328 interruptNet.reset();
2329 flagInterruptMsgProc = false;
2332 std::unique_lock<std::mutex> lock(mutexMsgProc);
2333 fMsgProcWake = false;
2336 // Send and receive from sockets, accept connections
2337 threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2339 if (!gArgs.GetBoolArg("-dnsseed", true))
2340 LogPrintf("DNS seeding disabled\n");
2341 else
2342 threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2344 // Initiate outbound connections from -addnode
2345 threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2347 // Initiate outbound connections unless connect=0
2348 if (!gArgs.IsArgSet("-connect") || gArgs.GetArgs("-connect").size() != 1 || gArgs.GetArgs("-connect")[0] != "0")
2349 threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this)));
2351 // Process messages
2352 threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2354 // Dump network addresses
2355 scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
2357 return true;
2360 class CNetCleanup
2362 public:
2363 CNetCleanup() {}
2365 ~CNetCleanup()
2367 #ifdef WIN32
2368 // Shutdown Windows Sockets
2369 WSACleanup();
2370 #endif
2373 instance_of_cnetcleanup;
2375 void CConnman::Interrupt()
2378 std::lock_guard<std::mutex> lock(mutexMsgProc);
2379 flagInterruptMsgProc = true;
2381 condMsgProc.notify_all();
2383 interruptNet();
2384 InterruptSocks5(true);
2386 if (semOutbound) {
2387 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++) {
2388 semOutbound->post();
2392 if (semAddnode) {
2393 for (int i=0; i<nMaxAddnode; i++) {
2394 semAddnode->post();
2399 void CConnman::Stop()
2401 if (threadMessageHandler.joinable())
2402 threadMessageHandler.join();
2403 if (threadOpenConnections.joinable())
2404 threadOpenConnections.join();
2405 if (threadOpenAddedConnections.joinable())
2406 threadOpenAddedConnections.join();
2407 if (threadDNSAddressSeed.joinable())
2408 threadDNSAddressSeed.join();
2409 if (threadSocketHandler.joinable())
2410 threadSocketHandler.join();
2412 if (fAddressesInitialized)
2414 DumpData();
2415 fAddressesInitialized = false;
2418 // Close sockets
2419 for (CNode* pnode : vNodes)
2420 pnode->CloseSocketDisconnect();
2421 for (ListenSocket& hListenSocket : vhListenSocket)
2422 if (hListenSocket.socket != INVALID_SOCKET)
2423 if (!CloseSocket(hListenSocket.socket))
2424 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2426 // clean up some globals (to help leak detection)
2427 for (CNode *pnode : vNodes) {
2428 DeleteNode(pnode);
2430 for (CNode *pnode : vNodesDisconnected) {
2431 DeleteNode(pnode);
2433 vNodes.clear();
2434 vNodesDisconnected.clear();
2435 vhListenSocket.clear();
2436 delete semOutbound;
2437 semOutbound = nullptr;
2438 delete semAddnode;
2439 semAddnode = nullptr;
2442 void CConnman::DeleteNode(CNode* pnode)
2444 assert(pnode);
2445 bool fUpdateConnectionTime = false;
2446 GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2447 if(fUpdateConnectionTime)
2448 addrman.Connected(pnode->addr);
2449 delete pnode;
2452 CConnman::~CConnman()
2454 Interrupt();
2455 Stop();
2458 size_t CConnman::GetAddressCount() const
2460 return addrman.size();
2463 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2465 addrman.SetServices(addr, nServices);
2468 void CConnman::MarkAddressGood(const CAddress& addr)
2470 addrman.Good(addr);
2473 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2475 addrman.Add(vAddr, addrFrom, nTimePenalty);
2478 std::vector<CAddress> CConnman::GetAddresses()
2480 return addrman.GetAddr();
2483 bool CConnman::AddNode(const std::string& strNode)
2485 LOCK(cs_vAddedNodes);
2486 for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2487 if (strNode == *it)
2488 return false;
2491 vAddedNodes.push_back(strNode);
2492 return true;
2495 bool CConnman::RemoveAddedNode(const std::string& strNode)
2497 LOCK(cs_vAddedNodes);
2498 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2499 if (strNode == *it) {
2500 vAddedNodes.erase(it);
2501 return true;
2504 return false;
2507 size_t CConnman::GetNodeCount(NumConnections flags)
2509 LOCK(cs_vNodes);
2510 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2511 return vNodes.size();
2513 int nNum = 0;
2514 for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
2515 if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
2516 nNum++;
2518 return nNum;
2521 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2523 vstats.clear();
2524 LOCK(cs_vNodes);
2525 vstats.reserve(vNodes.size());
2526 for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
2527 CNode* pnode = *it;
2528 vstats.emplace_back();
2529 pnode->copyStats(vstats.back());
2533 bool CConnman::DisconnectNode(const std::string& strNode)
2535 LOCK(cs_vNodes);
2536 if (CNode* pnode = FindNode(strNode)) {
2537 pnode->fDisconnect = true;
2538 return true;
2540 return false;
2542 bool CConnman::DisconnectNode(NodeId id)
2544 LOCK(cs_vNodes);
2545 for(CNode* pnode : vNodes) {
2546 if (id == pnode->GetId()) {
2547 pnode->fDisconnect = true;
2548 return true;
2551 return false;
2554 void CConnman::RecordBytesRecv(uint64_t bytes)
2556 LOCK(cs_totalBytesRecv);
2557 nTotalBytesRecv += bytes;
2560 void CConnman::RecordBytesSent(uint64_t bytes)
2562 LOCK(cs_totalBytesSent);
2563 nTotalBytesSent += bytes;
2565 uint64_t now = GetTime();
2566 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2568 // timeframe expired, reset cycle
2569 nMaxOutboundCycleStartTime = now;
2570 nMaxOutboundTotalBytesSentInCycle = 0;
2573 // TODO, exclude whitebind peers
2574 nMaxOutboundTotalBytesSentInCycle += bytes;
2577 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2579 LOCK(cs_totalBytesSent);
2580 nMaxOutboundLimit = limit;
2583 uint64_t CConnman::GetMaxOutboundTarget()
2585 LOCK(cs_totalBytesSent);
2586 return nMaxOutboundLimit;
2589 uint64_t CConnman::GetMaxOutboundTimeframe()
2591 LOCK(cs_totalBytesSent);
2592 return nMaxOutboundTimeframe;
2595 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2597 LOCK(cs_totalBytesSent);
2598 if (nMaxOutboundLimit == 0)
2599 return 0;
2601 if (nMaxOutboundCycleStartTime == 0)
2602 return nMaxOutboundTimeframe;
2604 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2605 uint64_t now = GetTime();
2606 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2609 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2611 LOCK(cs_totalBytesSent);
2612 if (nMaxOutboundTimeframe != timeframe)
2614 // reset measure-cycle in case of changing
2615 // the timeframe
2616 nMaxOutboundCycleStartTime = GetTime();
2618 nMaxOutboundTimeframe = timeframe;
2621 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2623 LOCK(cs_totalBytesSent);
2624 if (nMaxOutboundLimit == 0)
2625 return false;
2627 if (historicalBlockServingLimit)
2629 // keep a large enough buffer to at least relay each block once
2630 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2631 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2632 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2633 return true;
2635 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2636 return true;
2638 return false;
2641 uint64_t CConnman::GetOutboundTargetBytesLeft()
2643 LOCK(cs_totalBytesSent);
2644 if (nMaxOutboundLimit == 0)
2645 return 0;
2647 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2650 uint64_t CConnman::GetTotalBytesRecv()
2652 LOCK(cs_totalBytesRecv);
2653 return nTotalBytesRecv;
2656 uint64_t CConnman::GetTotalBytesSent()
2658 LOCK(cs_totalBytesSent);
2659 return nTotalBytesSent;
2662 ServiceFlags CConnman::GetLocalServices() const
2664 return nLocalServices;
2667 void CConnman::SetBestHeight(int height)
2669 nBestHeight.store(height, std::memory_order_release);
2672 int CConnman::GetBestHeight() const
2674 return nBestHeight.load(std::memory_order_acquire);
2677 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2679 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) :
2680 nTimeConnected(GetSystemTimeInSeconds()),
2681 addr(addrIn),
2682 addrBind(addrBindIn),
2683 fInbound(fInboundIn),
2684 nKeyedNetGroup(nKeyedNetGroupIn),
2685 addrKnown(5000, 0.001),
2686 filterInventoryKnown(50000, 0.000001),
2687 id(idIn),
2688 nLocalHostNonce(nLocalHostNonceIn),
2689 nLocalServices(nLocalServicesIn),
2690 nMyStartingHeight(nMyStartingHeightIn),
2691 nSendVersion(0)
2693 nServices = NODE_NONE;
2694 nServicesExpected = NODE_NONE;
2695 hSocket = hSocketIn;
2696 nRecvVersion = INIT_PROTO_VERSION;
2697 nLastSend = 0;
2698 nLastRecv = 0;
2699 nSendBytes = 0;
2700 nRecvBytes = 0;
2701 nTimeOffset = 0;
2702 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2703 nVersion = 0;
2704 strSubVer = "";
2705 fWhitelisted = false;
2706 fOneShot = false;
2707 fAddnode = false;
2708 fClient = false; // set by version message
2709 fFeeler = false;
2710 fSuccessfullyConnected = false;
2711 fDisconnect = false;
2712 nRefCount = 0;
2713 nSendSize = 0;
2714 nSendOffset = 0;
2715 hashContinue = uint256();
2716 nStartingHeight = -1;
2717 filterInventoryKnown.reset();
2718 fSendMempool = false;
2719 fGetAddr = false;
2720 nNextLocalAddrSend = 0;
2721 nNextAddrSend = 0;
2722 nNextInvSend = 0;
2723 fRelayTxes = false;
2724 fSentAddr = false;
2725 pfilter = new CBloomFilter();
2726 timeLastMempoolReq = 0;
2727 nLastBlockTime = 0;
2728 nLastTXTime = 0;
2729 nPingNonceSent = 0;
2730 nPingUsecStart = 0;
2731 nPingUsecTime = 0;
2732 fPingQueued = false;
2733 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2734 minFeeFilter = 0;
2735 lastSentFeeFilter = 0;
2736 nextSendTimeFeeFilter = 0;
2737 fPauseRecv = false;
2738 fPauseSend = false;
2739 nProcessQueueSize = 0;
2741 for (const std::string &msg : getAllNetMessageTypes())
2742 mapRecvBytesPerMsgCmd[msg] = 0;
2743 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2745 if (fLogIPs) {
2746 LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
2747 } else {
2748 LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
2752 CNode::~CNode()
2754 CloseSocket(hSocket);
2756 if (pfilter)
2757 delete pfilter;
2760 void CNode::AskFor(const CInv& inv)
2762 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2763 return;
2764 // a peer may not have multiple non-responded queue positions for a single inv item
2765 if (!setAskFor.insert(inv.hash).second)
2766 return;
2768 // We're using mapAskFor as a priority queue,
2769 // the key is the earliest time the request can be sent
2770 int64_t nRequestTime;
2771 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2772 if (it != mapAlreadyAskedFor.end())
2773 nRequestTime = it->second;
2774 else
2775 nRequestTime = 0;
2776 LogPrint(BCLog::NET, "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2778 // Make sure not to reuse time indexes to keep things in the same order
2779 int64_t nNow = GetTimeMicros() - 1000000;
2780 static int64_t nLastTime;
2781 ++nLastTime;
2782 nNow = std::max(nNow, nLastTime);
2783 nLastTime = nNow;
2785 // Each retry is 2 minutes after the last
2786 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2787 if (it != mapAlreadyAskedFor.end())
2788 mapAlreadyAskedFor.update(it, nRequestTime);
2789 else
2790 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2791 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2794 bool CConnman::NodeFullyConnected(const CNode* pnode)
2796 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
2799 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2801 size_t nMessageSize = msg.data.size();
2802 size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2803 LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2805 std::vector<unsigned char> serializedHeader;
2806 serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
2807 uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
2808 CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize);
2809 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
2811 CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
2813 size_t nBytesSent = 0;
2815 LOCK(pnode->cs_vSend);
2816 bool optimisticSend(pnode->vSendMsg.empty());
2818 //log total amount of bytes per command
2819 pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
2820 pnode->nSendSize += nTotalSize;
2822 if (pnode->nSendSize > nSendBufferMaxSize)
2823 pnode->fPauseSend = true;
2824 pnode->vSendMsg.push_back(std::move(serializedHeader));
2825 if (nMessageSize)
2826 pnode->vSendMsg.push_back(std::move(msg.data));
2828 // If write queue empty, attempt "optimistic write"
2829 if (optimisticSend == true)
2830 nBytesSent = SocketSendData(pnode);
2832 if (nBytesSent)
2833 RecordBytesSent(nBytesSent);
2836 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2838 CNode* found = nullptr;
2839 LOCK(cs_vNodes);
2840 for (auto&& pnode : vNodes) {
2841 if(pnode->GetId() == id) {
2842 found = pnode;
2843 break;
2846 return found != nullptr && NodeFullyConnected(found) && func(found);
2849 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2850 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2853 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
2855 return CSipHasher(nSeed0, nSeed1).Write(id);
2858 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
2860 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2862 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();