Merge #10189: devtools/net: add a verifier for scriptable changes. Use it to make...
[bitcoinplatinum.git] / src / net.cpp
blob1d9080462730ba2605d81e343e833168721b994f
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 const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
69 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
70 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // SHA256("localhostnonce")[0:8]
72 // Global state variables
74 bool fDiscover = true;
75 bool fListen = true;
76 bool fRelayTxes = true;
77 CCriticalSection cs_mapLocalHost;
78 std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
79 static bool vfLimited[NET_MAX] = {};
80 std::string strSubVersion;
82 limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
84 // Signals for message handling
85 static CNodeSignals g_signals;
86 CNodeSignals& GetNodeSignals() { return g_signals; }
88 void CConnman::AddOneShot(const std::string& strDest)
90 LOCK(cs_vOneShots);
91 vOneShots.push_back(strDest);
94 unsigned short GetListenPort()
96 return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
99 // find 'best' local address for a particular peer
100 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
102 if (!fListen)
103 return false;
105 int nBestScore = -1;
106 int nBestReachability = -1;
108 LOCK(cs_mapLocalHost);
109 for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
111 int nScore = (*it).second.nScore;
112 int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
113 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
115 addr = CService((*it).first, (*it).second.nPort);
116 nBestReachability = nReachability;
117 nBestScore = nScore;
121 return nBestScore >= 0;
124 //! Convert the pnSeeds6 array into usable address objects.
125 static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
127 // It'll only connect to one or two seed nodes because once it connects,
128 // it'll get a pile of addresses with newer timestamps.
129 // Seed nodes are given a random 'last seen time' of between one and two
130 // weeks ago.
131 const int64_t nOneWeek = 7*24*60*60;
132 std::vector<CAddress> vSeedsOut;
133 vSeedsOut.reserve(vSeedsIn.size());
134 for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i)
136 struct in6_addr ip;
137 memcpy(&ip, i->addr, sizeof(ip));
138 CAddress addr(CService(ip, i->port), NODE_NETWORK);
139 addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
140 vSeedsOut.push_back(addr);
142 return vSeedsOut;
145 // get best local address for a particular peer as a CAddress
146 // Otherwise, return the unroutable 0.0.0.0 but filled in with
147 // the normal parameters, since the IP may be changed to a useful
148 // one by discovery.
149 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
151 CAddress ret(CService(CNetAddr(),GetListenPort()), NODE_NONE);
152 CService addr;
153 if (GetLocal(addr, paddrPeer))
155 ret = CAddress(addr, nLocalServices);
157 ret.nTime = GetAdjustedTime();
158 return ret;
161 int GetnScore(const CService& addr)
163 LOCK(cs_mapLocalHost);
164 if (mapLocalHost.count(addr) == LOCAL_NONE)
165 return 0;
166 return mapLocalHost[addr].nScore;
169 // Is our peer's addrLocal potentially useful as an external IP source?
170 bool IsPeerAddrLocalGood(CNode *pnode)
172 CService addrLocal = pnode->GetAddrLocal();
173 return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
174 !IsLimited(addrLocal.GetNetwork());
177 // pushes our own address to a peer
178 void AdvertiseLocal(CNode *pnode)
180 if (fListen && pnode->fSuccessfullyConnected)
182 CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
183 // If discovery is enabled, sometimes give our peer the address it
184 // tells us that it sees us as in case it has a better idea of our
185 // address than we do.
186 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
187 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
189 addrLocal.SetIP(pnode->GetAddrLocal());
191 if (addrLocal.IsRoutable())
193 LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
194 FastRandomContext insecure_rand;
195 pnode->PushAddress(addrLocal, insecure_rand);
200 // learn a new local address
201 bool AddLocal(const CService& addr, int nScore)
203 if (!addr.IsRoutable())
204 return false;
206 if (!fDiscover && nScore < LOCAL_MANUAL)
207 return false;
209 if (IsLimited(addr))
210 return false;
212 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
215 LOCK(cs_mapLocalHost);
216 bool fAlready = mapLocalHost.count(addr) > 0;
217 LocalServiceInfo &info = mapLocalHost[addr];
218 if (!fAlready || nScore >= info.nScore) {
219 info.nScore = nScore + (fAlready ? 1 : 0);
220 info.nPort = addr.GetPort();
224 return true;
227 bool AddLocal(const CNetAddr &addr, int nScore)
229 return AddLocal(CService(addr, GetListenPort()), nScore);
232 bool RemoveLocal(const CService& addr)
234 LOCK(cs_mapLocalHost);
235 LogPrintf("RemoveLocal(%s)\n", addr.ToString());
236 mapLocalHost.erase(addr);
237 return true;
240 /** Make a particular network entirely off-limits (no automatic connects to it) */
241 void SetLimited(enum Network net, bool fLimited)
243 if (net == NET_UNROUTABLE)
244 return;
245 LOCK(cs_mapLocalHost);
246 vfLimited[net] = fLimited;
249 bool IsLimited(enum Network net)
251 LOCK(cs_mapLocalHost);
252 return vfLimited[net];
255 bool IsLimited(const CNetAddr &addr)
257 return IsLimited(addr.GetNetwork());
260 /** vote for a local address */
261 bool SeenLocal(const CService& addr)
264 LOCK(cs_mapLocalHost);
265 if (mapLocalHost.count(addr) == 0)
266 return false;
267 mapLocalHost[addr].nScore++;
269 return true;
273 /** check whether a given address is potentially local */
274 bool IsLocal(const CService& addr)
276 LOCK(cs_mapLocalHost);
277 return mapLocalHost.count(addr) > 0;
280 /** check whether a given network is one we can probably connect to */
281 bool IsReachable(enum Network net)
283 LOCK(cs_mapLocalHost);
284 return !vfLimited[net];
287 /** check whether a given address is in a network we can probably connect to */
288 bool IsReachable(const CNetAddr& addr)
290 enum Network net = addr.GetNetwork();
291 return IsReachable(net);
295 CNode* CConnman::FindNode(const CNetAddr& ip)
297 LOCK(cs_vNodes);
298 BOOST_FOREACH(CNode* pnode, vNodes)
299 if ((CNetAddr)pnode->addr == ip)
300 return (pnode);
301 return NULL;
304 CNode* CConnman::FindNode(const CSubNet& subNet)
306 LOCK(cs_vNodes);
307 BOOST_FOREACH(CNode* pnode, vNodes)
308 if (subNet.Match((CNetAddr)pnode->addr))
309 return (pnode);
310 return NULL;
313 CNode* CConnman::FindNode(const std::string& addrName)
315 LOCK(cs_vNodes);
316 BOOST_FOREACH(CNode* pnode, vNodes) {
317 if (pnode->GetAddrName() == addrName) {
318 return (pnode);
321 return NULL;
324 CNode* CConnman::FindNode(const CService& addr)
326 LOCK(cs_vNodes);
327 BOOST_FOREACH(CNode* pnode, vNodes)
328 if ((CService)pnode->addr == addr)
329 return (pnode);
330 return NULL;
333 bool CConnman::CheckIncomingNonce(uint64_t nonce)
335 LOCK(cs_vNodes);
336 BOOST_FOREACH(CNode* pnode, vNodes) {
337 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
338 return false;
340 return true;
343 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
345 if (pszDest == NULL) {
346 if (IsLocal(addrConnect))
347 return NULL;
349 // Look for an existing connection
350 CNode* pnode = FindNode((CService)addrConnect);
351 if (pnode)
353 LogPrintf("Failed to open new connection, already connected\n");
354 return NULL;
358 /// debug print
359 LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
360 pszDest ? pszDest : addrConnect.ToString(),
361 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
363 // Connect
364 SOCKET hSocket;
365 bool proxyConnectionFailed = false;
366 if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
367 ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
369 if (!IsSelectableSocket(hSocket)) {
370 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
371 CloseSocket(hSocket);
372 return NULL;
375 if (pszDest && addrConnect.IsValid()) {
376 // It is possible that we already have a connection to the IP/port pszDest resolved to.
377 // In that case, drop the connection that was just created, and return the existing CNode instead.
378 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
379 // name catch this early.
380 LOCK(cs_vNodes);
381 CNode* pnode = FindNode((CService)addrConnect);
382 if (pnode)
384 pnode->MaybeSetAddrName(std::string(pszDest));
385 CloseSocket(hSocket);
386 LogPrintf("Failed to open new connection, already connected\n");
387 return NULL;
391 addrman.Attempt(addrConnect, fCountFailure);
393 // Add node
394 NodeId id = GetNewNodeId();
395 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
396 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, pszDest ? pszDest : "", false);
397 pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
398 pnode->AddRef();
400 return pnode;
401 } else if (!proxyConnectionFailed) {
402 // If connecting to the node failed, and failure is not caused by a problem connecting to
403 // the proxy, mark this as an attempt.
404 addrman.Attempt(addrConnect, fCountFailure);
407 return NULL;
410 void CConnman::DumpBanlist()
412 SweepBanned(); // clean unused entries (if bantime has expired)
414 if (!BannedSetIsDirty())
415 return;
417 int64_t nStart = GetTimeMillis();
419 CBanDB bandb;
420 banmap_t banmap;
421 GetBanned(banmap);
422 if (bandb.Write(banmap)) {
423 SetBannedSetDirty(false);
426 LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
427 banmap.size(), GetTimeMillis() - nStart);
430 void CNode::CloseSocketDisconnect()
432 fDisconnect = true;
433 LOCK(cs_hSocket);
434 if (hSocket != INVALID_SOCKET)
436 LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
437 CloseSocket(hSocket);
441 void CConnman::ClearBanned()
444 LOCK(cs_setBanned);
445 setBanned.clear();
446 setBannedIsDirty = true;
448 DumpBanlist(); //store banlist to disk
449 if(clientInterface)
450 clientInterface->BannedListChanged();
453 bool CConnman::IsBanned(CNetAddr ip)
455 bool fResult = false;
457 LOCK(cs_setBanned);
458 for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++)
460 CSubNet subNet = (*it).first;
461 CBanEntry banEntry = (*it).second;
463 if(subNet.Match(ip) && GetTime() < banEntry.nBanUntil)
464 fResult = true;
467 return fResult;
470 bool CConnman::IsBanned(CSubNet subnet)
472 bool fResult = false;
474 LOCK(cs_setBanned);
475 banmap_t::iterator i = setBanned.find(subnet);
476 if (i != setBanned.end())
478 CBanEntry banEntry = (*i).second;
479 if (GetTime() < banEntry.nBanUntil)
480 fResult = true;
483 return fResult;
486 void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
487 CSubNet subNet(addr);
488 Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
491 void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
492 CBanEntry banEntry(GetTime());
493 banEntry.banReason = banReason;
494 if (bantimeoffset <= 0)
496 bantimeoffset = GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
497 sinceUnixEpoch = false;
499 banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
502 LOCK(cs_setBanned);
503 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
504 setBanned[subNet] = banEntry;
505 setBannedIsDirty = true;
507 else
508 return;
510 if(clientInterface)
511 clientInterface->BannedListChanged();
513 LOCK(cs_vNodes);
514 BOOST_FOREACH(CNode* pnode, vNodes) {
515 if (subNet.Match((CNetAddr)pnode->addr))
516 pnode->fDisconnect = true;
519 if(banReason == BanReasonManuallyAdded)
520 DumpBanlist(); //store banlist to disk immediately if user requested ban
523 bool CConnman::Unban(const CNetAddr &addr) {
524 CSubNet subNet(addr);
525 return Unban(subNet);
528 bool CConnman::Unban(const CSubNet &subNet) {
530 LOCK(cs_setBanned);
531 if (!setBanned.erase(subNet))
532 return false;
533 setBannedIsDirty = true;
535 if(clientInterface)
536 clientInterface->BannedListChanged();
537 DumpBanlist(); //store banlist to disk immediately
538 return true;
541 void CConnman::GetBanned(banmap_t &banMap)
543 LOCK(cs_setBanned);
544 // Sweep the banlist so expired bans are not returned
545 SweepBanned();
546 banMap = setBanned; //create a thread safe copy
549 void CConnman::SetBanned(const banmap_t &banMap)
551 LOCK(cs_setBanned);
552 setBanned = banMap;
553 setBannedIsDirty = true;
556 void CConnman::SweepBanned()
558 int64_t now = GetTime();
560 LOCK(cs_setBanned);
561 banmap_t::iterator it = setBanned.begin();
562 while(it != setBanned.end())
564 CSubNet subNet = (*it).first;
565 CBanEntry banEntry = (*it).second;
566 if(now > banEntry.nBanUntil)
568 setBanned.erase(it++);
569 setBannedIsDirty = true;
570 LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
572 else
573 ++it;
577 bool CConnman::BannedSetIsDirty()
579 LOCK(cs_setBanned);
580 return setBannedIsDirty;
583 void CConnman::SetBannedSetDirty(bool dirty)
585 LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
586 setBannedIsDirty = dirty;
590 bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
591 LOCK(cs_vWhitelistedRange);
592 BOOST_FOREACH(const CSubNet& subnet, vWhitelistedRange) {
593 if (subnet.Match(addr))
594 return true;
596 return false;
599 void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
600 LOCK(cs_vWhitelistedRange);
601 vWhitelistedRange.push_back(subnet);
605 std::string CNode::GetAddrName() const {
606 LOCK(cs_addrName);
607 return addrName;
610 void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
611 LOCK(cs_addrName);
612 if (addrName.empty()) {
613 addrName = addrNameIn;
617 CService CNode::GetAddrLocal() const {
618 LOCK(cs_addrLocal);
619 return addrLocal;
622 void CNode::SetAddrLocal(const CService& addrLocalIn) {
623 LOCK(cs_addrLocal);
624 if (addrLocal.IsValid()) {
625 error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
626 } else {
627 addrLocal = addrLocalIn;
631 #undef X
632 #define X(name) stats.name = name
633 void CNode::copyStats(CNodeStats &stats)
635 stats.nodeid = this->GetId();
636 X(nServices);
637 X(addr);
639 LOCK(cs_filter);
640 X(fRelayTxes);
642 X(nLastSend);
643 X(nLastRecv);
644 X(nTimeConnected);
645 X(nTimeOffset);
646 stats.addrName = GetAddrName();
647 X(nVersion);
649 LOCK(cs_SubVer);
650 X(cleanSubVer);
652 X(fInbound);
653 X(fAddnode);
654 X(nStartingHeight);
656 LOCK(cs_vSend);
657 X(mapSendBytesPerMsgCmd);
658 X(nSendBytes);
661 LOCK(cs_vRecv);
662 X(mapRecvBytesPerMsgCmd);
663 X(nRecvBytes);
665 X(fWhitelisted);
667 // It is common for nodes with good ping times to suddenly become lagged,
668 // due to a new block arriving or other large transfer.
669 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
670 // since pingtime does not update until the ping is complete, which might take a while.
671 // So, if a ping is taking an unusually long time in flight,
672 // the caller can immediately detect that this is happening.
673 int64_t nPingUsecWait = 0;
674 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
675 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
678 // 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 :)
679 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
680 stats.dMinPing = (((double)nMinPingUsecTime) / 1e6);
681 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
683 // Leave string empty if addrLocal invalid (not filled in yet)
684 CService addrLocalUnlocked = GetAddrLocal();
685 stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
687 #undef X
689 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
691 complete = false;
692 int64_t nTimeMicros = GetTimeMicros();
693 LOCK(cs_vRecv);
694 nLastRecv = nTimeMicros / 1000000;
695 nRecvBytes += nBytes;
696 while (nBytes > 0) {
698 // get current incomplete message, or create a new one
699 if (vRecvMsg.empty() ||
700 vRecvMsg.back().complete())
701 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
703 CNetMessage& msg = vRecvMsg.back();
705 // absorb network data
706 int handled;
707 if (!msg.in_data)
708 handled = msg.readHeader(pch, nBytes);
709 else
710 handled = msg.readData(pch, nBytes);
712 if (handled < 0)
713 return false;
715 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
716 LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
717 return false;
720 pch += handled;
721 nBytes -= handled;
723 if (msg.complete()) {
725 //store received bytes per message command
726 //to prevent a memory DOS, only allow valid commands
727 mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
728 if (i == mapRecvBytesPerMsgCmd.end())
729 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
730 assert(i != mapRecvBytesPerMsgCmd.end());
731 i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
733 msg.nTime = nTimeMicros;
734 complete = true;
738 return true;
741 void CNode::SetSendVersion(int nVersionIn)
743 // Send version may only be changed in the version message, and
744 // only one version message is allowed per session. We can therefore
745 // treat this value as const and even atomic as long as it's only used
746 // once a version message has been successfully processed. Any attempt to
747 // set this twice is an error.
748 if (nSendVersion != 0) {
749 error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
750 } else {
751 nSendVersion = nVersionIn;
755 int CNode::GetSendVersion() const
757 // The send version should always be explicitly set to
758 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
759 // has been called.
760 if (nSendVersion == 0) {
761 error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
762 return INIT_PROTO_VERSION;
764 return nSendVersion;
768 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
770 // copy data to temporary parsing buffer
771 unsigned int nRemaining = 24 - nHdrPos;
772 unsigned int nCopy = std::min(nRemaining, nBytes);
774 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
775 nHdrPos += nCopy;
777 // if header incomplete, exit
778 if (nHdrPos < 24)
779 return nCopy;
781 // deserialize to CMessageHeader
782 try {
783 hdrbuf >> hdr;
785 catch (const std::exception&) {
786 return -1;
789 // reject messages larger than MAX_SIZE
790 if (hdr.nMessageSize > MAX_SIZE)
791 return -1;
793 // switch state to reading message data
794 in_data = true;
796 return nCopy;
799 int CNetMessage::readData(const char *pch, unsigned int nBytes)
801 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
802 unsigned int nCopy = std::min(nRemaining, nBytes);
804 if (vRecv.size() < nDataPos + nCopy) {
805 // Allocate up to 256 KiB ahead, but never more than the total message size.
806 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
809 hasher.Write((const unsigned char*)pch, nCopy);
810 memcpy(&vRecv[nDataPos], pch, nCopy);
811 nDataPos += nCopy;
813 return nCopy;
816 const uint256& CNetMessage::GetMessageHash() const
818 assert(complete());
819 if (data_hash.IsNull())
820 hasher.Finalize(data_hash.begin());
821 return data_hash;
832 // requires LOCK(cs_vSend)
833 size_t CConnman::SocketSendData(CNode *pnode) const
835 auto it = pnode->vSendMsg.begin();
836 size_t nSentSize = 0;
838 while (it != pnode->vSendMsg.end()) {
839 const auto &data = *it;
840 assert(data.size() > pnode->nSendOffset);
841 int nBytes = 0;
843 LOCK(pnode->cs_hSocket);
844 if (pnode->hSocket == INVALID_SOCKET)
845 break;
846 nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
848 if (nBytes > 0) {
849 pnode->nLastSend = GetSystemTimeInSeconds();
850 pnode->nSendBytes += nBytes;
851 pnode->nSendOffset += nBytes;
852 nSentSize += nBytes;
853 if (pnode->nSendOffset == data.size()) {
854 pnode->nSendOffset = 0;
855 pnode->nSendSize -= data.size();
856 pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
857 it++;
858 } else {
859 // could not send full message; stop sending more
860 break;
862 } else {
863 if (nBytes < 0) {
864 // error
865 int nErr = WSAGetLastError();
866 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
868 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
869 pnode->CloseSocketDisconnect();
872 // couldn't send anything at all
873 break;
877 if (it == pnode->vSendMsg.end()) {
878 assert(pnode->nSendOffset == 0);
879 assert(pnode->nSendSize == 0);
881 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
882 return nSentSize;
885 struct NodeEvictionCandidate
887 NodeId id;
888 int64_t nTimeConnected;
889 int64_t nMinPingUsecTime;
890 int64_t nLastBlockTime;
891 int64_t nLastTXTime;
892 bool fRelevantServices;
893 bool fRelayTxes;
894 bool fBloomFilter;
895 CAddress addr;
896 uint64_t nKeyedNetGroup;
899 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
901 return a.nMinPingUsecTime > b.nMinPingUsecTime;
904 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
906 return a.nTimeConnected > b.nTimeConnected;
909 static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
910 return a.nKeyedNetGroup < b.nKeyedNetGroup;
913 static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
915 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
916 if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
917 if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
918 return a.nTimeConnected > b.nTimeConnected;
921 static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
923 // 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.
924 if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
925 if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
926 if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
927 return a.nTimeConnected > b.nTimeConnected;
930 /** Try to find a connection to evict when the node is full.
931 * Extreme care must be taken to avoid opening the node to attacker
932 * triggered network partitioning.
933 * The strategy used here is to protect a small number of peers
934 * for each of several distinct characteristics which are difficult
935 * to forge. In order to partition a node the attacker must be
936 * simultaneously better at all of them than honest peers.
938 bool CConnman::AttemptToEvictConnection()
940 std::vector<NodeEvictionCandidate> vEvictionCandidates;
942 LOCK(cs_vNodes);
944 BOOST_FOREACH(CNode *node, vNodes) {
945 if (node->fWhitelisted)
946 continue;
947 if (!node->fInbound)
948 continue;
949 if (node->fDisconnect)
950 continue;
951 NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
952 node->nLastBlockTime, node->nLastTXTime,
953 (node->nServices & nRelevantServices) == nRelevantServices,
954 node->fRelayTxes, node->pfilter != NULL, node->addr, node->nKeyedNetGroup};
955 vEvictionCandidates.push_back(candidate);
959 if (vEvictionCandidates.empty()) return false;
961 // Protect connections with certain characteristics
963 // Deterministically select 4 peers to protect by netgroup.
964 // An attacker cannot predict which netgroups will be protected
965 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNetGroupKeyed);
966 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
968 if (vEvictionCandidates.empty()) return false;
970 // Protect the 8 nodes with the lowest minimum ping time.
971 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
972 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);
973 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
975 if (vEvictionCandidates.empty()) return false;
977 // Protect 4 nodes that most recently sent us transactions.
978 // An attacker cannot manipulate this metric without performing useful work.
979 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeTXTime);
980 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
982 if (vEvictionCandidates.empty()) return false;
984 // Protect 4 nodes that most recently sent us blocks.
985 // An attacker cannot manipulate this metric without performing useful work.
986 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeBlockTime);
987 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
989 if (vEvictionCandidates.empty()) return false;
991 // Protect the half of the remaining nodes which have been connected the longest.
992 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
993 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
994 vEvictionCandidates.erase(vEvictionCandidates.end() - static_cast<int>(vEvictionCandidates.size() / 2), vEvictionCandidates.end());
996 if (vEvictionCandidates.empty()) return false;
998 // Identify the network group with the most connections and youngest member.
999 // (vEvictionCandidates is already sorted by reverse connect time)
1000 uint64_t naMostConnections;
1001 unsigned int nMostConnections = 0;
1002 int64_t nMostConnectionsTime = 0;
1003 std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
1004 BOOST_FOREACH(const NodeEvictionCandidate &node, vEvictionCandidates) {
1005 mapNetGroupNodes[node.nKeyedNetGroup].push_back(node);
1006 int64_t grouptime = mapNetGroupNodes[node.nKeyedNetGroup][0].nTimeConnected;
1007 size_t groupsize = mapNetGroupNodes[node.nKeyedNetGroup].size();
1009 if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
1010 nMostConnections = groupsize;
1011 nMostConnectionsTime = grouptime;
1012 naMostConnections = node.nKeyedNetGroup;
1016 // Reduce to the network group with the most connections
1017 vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1019 // Disconnect from the network group with the most connections
1020 NodeId evicted = vEvictionCandidates.front().id;
1021 LOCK(cs_vNodes);
1022 for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
1023 if ((*it)->GetId() == evicted) {
1024 (*it)->fDisconnect = true;
1025 return true;
1028 return false;
1031 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1032 struct sockaddr_storage sockaddr;
1033 socklen_t len = sizeof(sockaddr);
1034 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
1035 CAddress addr;
1036 int nInbound = 0;
1037 int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler);
1039 if (hSocket != INVALID_SOCKET)
1040 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
1041 LogPrintf("Warning: Unknown socket family\n");
1043 bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
1045 LOCK(cs_vNodes);
1046 BOOST_FOREACH(CNode* pnode, vNodes)
1047 if (pnode->fInbound)
1048 nInbound++;
1051 if (hSocket == INVALID_SOCKET)
1053 int nErr = WSAGetLastError();
1054 if (nErr != WSAEWOULDBLOCK)
1055 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1056 return;
1059 if (!fNetworkActive) {
1060 LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
1061 CloseSocket(hSocket);
1062 return;
1065 if (!IsSelectableSocket(hSocket))
1067 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
1068 CloseSocket(hSocket);
1069 return;
1072 // According to the internet TCP_NODELAY is not carried into accepted sockets
1073 // on all platforms. Set it again here just to be sure.
1074 int set = 1;
1075 #ifdef WIN32
1076 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
1077 #else
1078 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
1079 #endif
1081 if (IsBanned(addr) && !whitelisted)
1083 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
1084 CloseSocket(hSocket);
1085 return;
1088 if (nInbound >= nMaxInbound)
1090 if (!AttemptToEvictConnection()) {
1091 // No connection to evict, disconnect the new connection
1092 LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1093 CloseSocket(hSocket);
1094 return;
1098 NodeId id = GetNewNodeId();
1099 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1101 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, "", true);
1102 pnode->AddRef();
1103 pnode->fWhitelisted = whitelisted;
1104 GetNodeSignals().InitializeNode(pnode, *this);
1106 LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1109 LOCK(cs_vNodes);
1110 vNodes.push_back(pnode);
1114 void CConnman::ThreadSocketHandler()
1116 unsigned int nPrevNodeCount = 0;
1117 while (!interruptNet)
1120 // Disconnect nodes
1123 LOCK(cs_vNodes);
1124 // Disconnect unused nodes
1125 std::vector<CNode*> vNodesCopy = vNodes;
1126 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1128 if (pnode->fDisconnect)
1130 // remove from vNodes
1131 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1133 // release outbound grant (if any)
1134 pnode->grantOutbound.Release();
1136 // close socket and cleanup
1137 pnode->CloseSocketDisconnect();
1139 // hold in disconnected pool until all refs are released
1140 pnode->Release();
1141 vNodesDisconnected.push_back(pnode);
1146 // Delete disconnected nodes
1147 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1148 BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
1150 // wait until threads are done using it
1151 if (pnode->GetRefCount() <= 0) {
1152 bool fDelete = false;
1154 TRY_LOCK(pnode->cs_inventory, lockInv);
1155 if (lockInv) {
1156 TRY_LOCK(pnode->cs_vSend, lockSend);
1157 if (lockSend) {
1158 fDelete = true;
1162 if (fDelete) {
1163 vNodesDisconnected.remove(pnode);
1164 DeleteNode(pnode);
1169 size_t vNodesSize;
1171 LOCK(cs_vNodes);
1172 vNodesSize = vNodes.size();
1174 if(vNodesSize != nPrevNodeCount) {
1175 nPrevNodeCount = vNodesSize;
1176 if(clientInterface)
1177 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1181 // Find which sockets have data to receive
1183 struct timeval timeout;
1184 timeout.tv_sec = 0;
1185 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1187 fd_set fdsetRecv;
1188 fd_set fdsetSend;
1189 fd_set fdsetError;
1190 FD_ZERO(&fdsetRecv);
1191 FD_ZERO(&fdsetSend);
1192 FD_ZERO(&fdsetError);
1193 SOCKET hSocketMax = 0;
1194 bool have_fds = false;
1196 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket) {
1197 FD_SET(hListenSocket.socket, &fdsetRecv);
1198 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1199 have_fds = true;
1203 LOCK(cs_vNodes);
1204 BOOST_FOREACH(CNode* pnode, vNodes)
1206 // Implement the following logic:
1207 // * If there is data to send, select() for sending data. As this only
1208 // happens when optimistic write failed, we choose to first drain the
1209 // write buffer in this case before receiving more. This avoids
1210 // needlessly queueing received data, if the remote peer is not themselves
1211 // receiving data. This means properly utilizing TCP flow control signalling.
1212 // * Otherwise, if there is space left in the receive buffer, select() for
1213 // receiving data.
1214 // * Hand off all complete messages to the processor, to be handled without
1215 // blocking here.
1217 bool select_recv = !pnode->fPauseRecv;
1218 bool select_send;
1220 LOCK(pnode->cs_vSend);
1221 select_send = !pnode->vSendMsg.empty();
1224 LOCK(pnode->cs_hSocket);
1225 if (pnode->hSocket == INVALID_SOCKET)
1226 continue;
1228 FD_SET(pnode->hSocket, &fdsetError);
1229 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1230 have_fds = true;
1232 if (select_send) {
1233 FD_SET(pnode->hSocket, &fdsetSend);
1234 continue;
1236 if (select_recv) {
1237 FD_SET(pnode->hSocket, &fdsetRecv);
1242 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1243 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1244 if (interruptNet)
1245 return;
1247 if (nSelect == SOCKET_ERROR)
1249 if (have_fds)
1251 int nErr = WSAGetLastError();
1252 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1253 for (unsigned int i = 0; i <= hSocketMax; i++)
1254 FD_SET(i, &fdsetRecv);
1256 FD_ZERO(&fdsetSend);
1257 FD_ZERO(&fdsetError);
1258 if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
1259 return;
1263 // Accept new connections
1265 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket)
1267 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1269 AcceptConnection(hListenSocket);
1274 // Service each socket
1276 std::vector<CNode*> vNodesCopy;
1278 LOCK(cs_vNodes);
1279 vNodesCopy = vNodes;
1280 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1281 pnode->AddRef();
1283 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1285 if (interruptNet)
1286 return;
1289 // Receive
1291 bool recvSet = false;
1292 bool sendSet = false;
1293 bool errorSet = false;
1295 LOCK(pnode->cs_hSocket);
1296 if (pnode->hSocket == INVALID_SOCKET)
1297 continue;
1298 recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
1299 sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
1300 errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
1302 if (recvSet || errorSet)
1304 // typical socket buffer is 8K-64K
1305 char pchBuf[0x10000];
1306 int nBytes = 0;
1308 LOCK(pnode->cs_hSocket);
1309 if (pnode->hSocket == INVALID_SOCKET)
1310 continue;
1311 nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1313 if (nBytes > 0)
1315 bool notify = false;
1316 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1317 pnode->CloseSocketDisconnect();
1318 RecordBytesRecv(nBytes);
1319 if (notify) {
1320 size_t nSizeAdded = 0;
1321 auto it(pnode->vRecvMsg.begin());
1322 for (; it != pnode->vRecvMsg.end(); ++it) {
1323 if (!it->complete())
1324 break;
1325 nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1328 LOCK(pnode->cs_vProcessMsg);
1329 pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1330 pnode->nProcessQueueSize += nSizeAdded;
1331 pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1333 WakeMessageHandler();
1336 else if (nBytes == 0)
1338 // socket closed gracefully
1339 if (!pnode->fDisconnect) {
1340 LogPrint(BCLog::NET, "socket closed\n");
1342 pnode->CloseSocketDisconnect();
1344 else if (nBytes < 0)
1346 // error
1347 int nErr = WSAGetLastError();
1348 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1350 if (!pnode->fDisconnect)
1351 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1352 pnode->CloseSocketDisconnect();
1358 // Send
1360 if (sendSet)
1362 LOCK(pnode->cs_vSend);
1363 size_t nBytes = SocketSendData(pnode);
1364 if (nBytes) {
1365 RecordBytesSent(nBytes);
1370 // Inactivity checking
1372 int64_t nTime = GetSystemTimeInSeconds();
1373 if (nTime - pnode->nTimeConnected > 60)
1375 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1377 LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1378 pnode->fDisconnect = true;
1380 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1382 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1383 pnode->fDisconnect = true;
1385 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1387 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1388 pnode->fDisconnect = true;
1390 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1392 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1393 pnode->fDisconnect = true;
1395 else if (!pnode->fSuccessfullyConnected)
1397 LogPrintf("version handshake timeout from %d\n", pnode->GetId());
1398 pnode->fDisconnect = true;
1403 LOCK(cs_vNodes);
1404 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1405 pnode->Release();
1410 void CConnman::WakeMessageHandler()
1413 std::lock_guard<std::mutex> lock(mutexMsgProc);
1414 fMsgProcWake = true;
1416 condMsgProc.notify_one();
1424 #ifdef USE_UPNP
1425 void ThreadMapPort()
1427 std::string port = strprintf("%u", GetListenPort());
1428 const char * multicastif = 0;
1429 const char * minissdpdpath = 0;
1430 struct UPNPDev * devlist = 0;
1431 char lanaddr[64];
1433 #ifndef UPNPDISCOVER_SUCCESS
1434 /* miniupnpc 1.5 */
1435 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1436 #elif MINIUPNPC_API_VERSION < 14
1437 /* miniupnpc 1.6 */
1438 int error = 0;
1439 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1440 #else
1441 /* miniupnpc 1.9.20150730 */
1442 int error = 0;
1443 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1444 #endif
1446 struct UPNPUrls urls;
1447 struct IGDdatas data;
1448 int r;
1450 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1451 if (r == 1)
1453 if (fDiscover) {
1454 char externalIPAddress[40];
1455 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1456 if(r != UPNPCOMMAND_SUCCESS)
1457 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1458 else
1460 if(externalIPAddress[0])
1462 CNetAddr resolved;
1463 if(LookupHost(externalIPAddress, resolved, false)) {
1464 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1465 AddLocal(resolved, LOCAL_UPNP);
1468 else
1469 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1473 std::string strDesc = "Bitcoin " + FormatFullVersion();
1475 try {
1476 while (true) {
1477 #ifndef UPNPDISCOVER_SUCCESS
1478 /* miniupnpc 1.5 */
1479 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1480 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1481 #else
1482 /* miniupnpc 1.6 */
1483 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1484 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1485 #endif
1487 if(r!=UPNPCOMMAND_SUCCESS)
1488 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1489 port, port, lanaddr, r, strupnperror(r));
1490 else
1491 LogPrintf("UPnP Port Mapping successful.\n");
1493 MilliSleep(20*60*1000); // Refresh every 20 minutes
1496 catch (const boost::thread_interrupted&)
1498 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1499 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1500 freeUPNPDevlist(devlist); devlist = 0;
1501 FreeUPNPUrls(&urls);
1502 throw;
1504 } else {
1505 LogPrintf("No valid UPnP IGDs found\n");
1506 freeUPNPDevlist(devlist); devlist = 0;
1507 if (r != 0)
1508 FreeUPNPUrls(&urls);
1512 void MapPort(bool fUseUPnP)
1514 static boost::thread* upnp_thread = NULL;
1516 if (fUseUPnP)
1518 if (upnp_thread) {
1519 upnp_thread->interrupt();
1520 upnp_thread->join();
1521 delete upnp_thread;
1523 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1525 else if (upnp_thread) {
1526 upnp_thread->interrupt();
1527 upnp_thread->join();
1528 delete upnp_thread;
1529 upnp_thread = NULL;
1533 #else
1534 void MapPort(bool)
1536 // Intentionally left blank.
1538 #endif
1545 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1547 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1548 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1549 *requiredServiceBits = NODE_NETWORK;
1550 return data.host;
1553 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1554 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1558 void CConnman::ThreadDNSAddressSeed()
1560 // goal: only query DNS seeds if address need is acute
1561 // Avoiding DNS seeds when we don't need them improves user privacy by
1562 // creating fewer identifying DNS requests, reduces trust by giving seeds
1563 // less influence on the network topology, and reduces traffic to the seeds.
1564 if ((addrman.size() > 0) &&
1565 (!GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1566 if (!interruptNet.sleep_for(std::chrono::seconds(11)))
1567 return;
1569 LOCK(cs_vNodes);
1570 int nRelevant = 0;
1571 for (auto pnode : vNodes) {
1572 nRelevant += pnode->fSuccessfullyConnected && ((pnode->nServices & nRelevantServices) == nRelevantServices);
1574 if (nRelevant >= 2) {
1575 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1576 return;
1580 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1581 int found = 0;
1583 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1585 BOOST_FOREACH(const CDNSSeedData &seed, vSeeds) {
1586 if (interruptNet) {
1587 return;
1589 if (HaveNameProxy()) {
1590 AddOneShot(seed.host);
1591 } else {
1592 std::vector<CNetAddr> vIPs;
1593 std::vector<CAddress> vAdd;
1594 ServiceFlags requiredServiceBits = nRelevantServices;
1595 if (LookupHost(GetDNSHost(seed, &requiredServiceBits).c_str(), vIPs, 0, true))
1597 BOOST_FOREACH(const CNetAddr& ip, vIPs)
1599 int nOneDay = 24*3600;
1600 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
1601 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1602 vAdd.push_back(addr);
1603 found++;
1606 if (interruptNet) {
1607 return;
1609 // TODO: The seed name resolve may fail, yielding an IP of [::], which results in
1610 // addrman assigning the same source to results from different seeds.
1611 // This should switch to a hard-coded stable dummy IP for each seed name, so that the
1612 // resolve is not required at all.
1613 if (!vIPs.empty()) {
1614 CService seedSource;
1615 Lookup(seed.name.c_str(), seedSource, 0, true);
1616 addrman.Add(vAdd, seedSource);
1621 LogPrintf("%d addresses found from DNS seeds\n", found);
1635 void CConnman::DumpAddresses()
1637 int64_t nStart = GetTimeMillis();
1639 CAddrDB adb;
1640 adb.Write(addrman);
1642 LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1643 addrman.size(), GetTimeMillis() - nStart);
1646 void CConnman::DumpData()
1648 DumpAddresses();
1649 DumpBanlist();
1652 void CConnman::ProcessOneShot()
1654 std::string strDest;
1656 LOCK(cs_vOneShots);
1657 if (vOneShots.empty())
1658 return;
1659 strDest = vOneShots.front();
1660 vOneShots.pop_front();
1662 CAddress addr;
1663 CSemaphoreGrant grant(*semOutbound, true);
1664 if (grant) {
1665 if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
1666 AddOneShot(strDest);
1670 void CConnman::ThreadOpenConnections()
1672 // Connect to specific addresses
1673 if (mapMultiArgs.count("-connect") && mapMultiArgs.at("-connect").size() > 0)
1675 for (int64_t nLoop = 0;; nLoop++)
1677 ProcessOneShot();
1678 BOOST_FOREACH(const std::string& strAddr, mapMultiArgs.at("-connect"))
1680 CAddress addr(CService(), NODE_NONE);
1681 OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
1682 for (int i = 0; i < 10 && i < nLoop; i++)
1684 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1685 return;
1688 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1689 return;
1693 // Initiate network connections
1694 int64_t nStart = GetTime();
1696 // Minimum time before next feeler connection (in microseconds).
1697 int64_t nNextFeeler = PoissonNextSend(nStart*1000*1000, FEELER_INTERVAL);
1698 while (!interruptNet)
1700 ProcessOneShot();
1702 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1703 return;
1705 CSemaphoreGrant grant(*semOutbound);
1706 if (interruptNet)
1707 return;
1709 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1710 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1711 static bool done = false;
1712 if (!done) {
1713 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1714 CNetAddr local;
1715 LookupHost("127.0.0.1", local, false);
1716 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1717 done = true;
1722 // Choose an address to connect to based on most recently seen
1724 CAddress addrConnect;
1726 // Only connect out to one peer per network group (/16 for IPv4).
1727 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1728 int nOutbound = 0;
1729 std::set<std::vector<unsigned char> > setConnected;
1731 LOCK(cs_vNodes);
1732 BOOST_FOREACH(CNode* pnode, vNodes) {
1733 if (!pnode->fInbound && !pnode->fAddnode) {
1734 // Netgroups for inbound and addnode peers are not excluded because our goal here
1735 // is to not use multiple of our limited outbound slots on a single netgroup
1736 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1737 // also have the added issue that they're attacker controlled and could be used
1738 // to prevent us from connecting to particular hosts if we used them here.
1739 setConnected.insert(pnode->addr.GetGroup());
1740 nOutbound++;
1745 // Feeler Connections
1747 // Design goals:
1748 // * Increase the number of connectable addresses in the tried table.
1750 // Method:
1751 // * Choose a random address from new and attempt to connect to it if we can connect
1752 // successfully it is added to tried.
1753 // * Start attempting feeler connections only after node finishes making outbound
1754 // connections.
1755 // * Only make a feeler connection once every few minutes.
1757 bool fFeeler = false;
1758 if (nOutbound >= nMaxOutbound) {
1759 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1760 if (nTime > nNextFeeler) {
1761 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1762 fFeeler = true;
1763 } else {
1764 continue;
1768 int64_t nANow = GetAdjustedTime();
1769 int nTries = 0;
1770 while (!interruptNet)
1772 CAddrInfo addr = addrman.Select(fFeeler);
1774 // if we selected an invalid address, restart
1775 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1776 break;
1778 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1779 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1780 // already-connected network ranges, ...) before trying new addrman addresses.
1781 nTries++;
1782 if (nTries > 100)
1783 break;
1785 if (IsLimited(addr))
1786 continue;
1788 // only connect to full nodes
1789 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1790 continue;
1792 // only consider very recently tried nodes after 30 failed attempts
1793 if (nANow - addr.nLastTry < 600 && nTries < 30)
1794 continue;
1796 // only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1797 if ((addr.nServices & nRelevantServices) != nRelevantServices && (nTries < 40 || nOutbound >= (nMaxOutbound >> 1)))
1798 continue;
1800 // do not allow non-default ports, unless after 50 invalid addresses selected already
1801 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1802 continue;
1804 addrConnect = addr;
1805 break;
1808 if (addrConnect.IsValid()) {
1810 if (fFeeler) {
1811 // Add small amount of random noise before connection to avoid synchronization.
1812 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1813 if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1814 return;
1815 LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
1818 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, NULL, false, fFeeler);
1823 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1825 std::vector<AddedNodeInfo> ret;
1827 std::list<std::string> lAddresses(0);
1829 LOCK(cs_vAddedNodes);
1830 ret.reserve(vAddedNodes.size());
1831 BOOST_FOREACH(const std::string& strAddNode, vAddedNodes)
1832 lAddresses.push_back(strAddNode);
1836 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1837 std::map<CService, bool> mapConnected;
1838 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
1840 LOCK(cs_vNodes);
1841 for (const CNode* pnode : vNodes) {
1842 if (pnode->addr.IsValid()) {
1843 mapConnected[pnode->addr] = pnode->fInbound;
1845 std::string addrName = pnode->GetAddrName();
1846 if (!addrName.empty()) {
1847 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1852 BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
1853 CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1854 if (service.IsValid()) {
1855 // strAddNode is an IP:port
1856 auto it = mapConnected.find(service);
1857 if (it != mapConnected.end()) {
1858 ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1859 } else {
1860 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1862 } else {
1863 // strAddNode is a name
1864 auto it = mapConnectedByName.find(strAddNode);
1865 if (it != mapConnectedByName.end()) {
1866 ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1867 } else {
1868 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1873 return ret;
1876 void CConnman::ThreadOpenAddedConnections()
1879 LOCK(cs_vAddedNodes);
1880 if (mapMultiArgs.count("-addnode"))
1881 vAddedNodes = mapMultiArgs.at("-addnode");
1884 while (true)
1886 CSemaphoreGrant grant(*semAddnode);
1887 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1888 bool tried = false;
1889 for (const AddedNodeInfo& info : vInfo) {
1890 if (!info.fConnected) {
1891 if (!grant.TryAcquire()) {
1892 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1893 // the addednodeinfo state might change.
1894 break;
1896 // If strAddedNode is an IP/port, decode it immediately, so
1897 // OpenNetworkConnection can detect existing connections to that IP/port.
1898 tried = true;
1899 CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
1900 OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false, false, true);
1901 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1902 return;
1905 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1906 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
1907 return;
1911 // if successful, this moves the passed grant to the constructed node
1912 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool fAddnode)
1915 // Initiate outbound network connection
1917 if (interruptNet) {
1918 return false;
1920 if (!fNetworkActive) {
1921 return false;
1923 if (!pszDest) {
1924 if (IsLocal(addrConnect) ||
1925 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1926 FindNode(addrConnect.ToStringIPPort()))
1927 return false;
1928 } else if (FindNode(std::string(pszDest)))
1929 return false;
1931 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1933 if (!pnode)
1934 return false;
1935 if (grantOutbound)
1936 grantOutbound->MoveTo(pnode->grantOutbound);
1937 if (fOneShot)
1938 pnode->fOneShot = true;
1939 if (fFeeler)
1940 pnode->fFeeler = true;
1941 if (fAddnode)
1942 pnode->fAddnode = true;
1944 GetNodeSignals().InitializeNode(pnode, *this);
1946 LOCK(cs_vNodes);
1947 vNodes.push_back(pnode);
1950 return true;
1953 void CConnman::ThreadMessageHandler()
1955 while (!flagInterruptMsgProc)
1957 std::vector<CNode*> vNodesCopy;
1959 LOCK(cs_vNodes);
1960 vNodesCopy = vNodes;
1961 BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1962 pnode->AddRef();
1966 bool fMoreWork = false;
1968 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1970 if (pnode->fDisconnect)
1971 continue;
1973 // Receive messages
1974 bool fMoreNodeWork = GetNodeSignals().ProcessMessages(pnode, *this, flagInterruptMsgProc);
1975 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
1976 if (flagInterruptMsgProc)
1977 return;
1979 // Send messages
1981 LOCK(pnode->cs_sendProcessing);
1982 GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
1984 if (flagInterruptMsgProc)
1985 return;
1989 LOCK(cs_vNodes);
1990 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1991 pnode->Release();
1994 std::unique_lock<std::mutex> lock(mutexMsgProc);
1995 if (!fMoreWork) {
1996 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
1998 fMsgProcWake = false;
2007 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
2009 strError = "";
2010 int nOne = 1;
2012 // Create socket for listening for incoming connections
2013 struct sockaddr_storage sockaddr;
2014 socklen_t len = sizeof(sockaddr);
2015 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
2017 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
2018 LogPrintf("%s\n", strError);
2019 return false;
2022 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2023 if (hListenSocket == INVALID_SOCKET)
2025 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2026 LogPrintf("%s\n", strError);
2027 return false;
2029 if (!IsSelectableSocket(hListenSocket))
2031 strError = "Error: Couldn't create a listenable socket for incoming connections";
2032 LogPrintf("%s\n", strError);
2033 return false;
2037 #ifndef WIN32
2038 #ifdef SO_NOSIGPIPE
2039 // Different way of disabling SIGPIPE on BSD
2040 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
2041 #endif
2042 // Allow binding if the port is still in TIME_WAIT state after
2043 // the program was closed and restarted.
2044 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2045 // Disable Nagle's algorithm
2046 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
2047 #else
2048 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2049 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
2050 #endif
2052 // Set to non-blocking, incoming connections will also inherit this
2053 if (!SetSocketNonBlocking(hListenSocket, true)) {
2054 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2055 LogPrintf("%s\n", strError);
2056 return false;
2059 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2060 // and enable it by default or not. Try to enable it, if possible.
2061 if (addrBind.IsIPv6()) {
2062 #ifdef IPV6_V6ONLY
2063 #ifdef WIN32
2064 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2065 #else
2066 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2067 #endif
2068 #endif
2069 #ifdef WIN32
2070 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2071 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2072 #endif
2075 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2077 int nErr = WSAGetLastError();
2078 if (nErr == WSAEADDRINUSE)
2079 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
2080 else
2081 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2082 LogPrintf("%s\n", strError);
2083 CloseSocket(hListenSocket);
2084 return false;
2086 LogPrintf("Bound to %s\n", addrBind.ToString());
2088 // Listen for incoming connections
2089 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
2091 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2092 LogPrintf("%s\n", strError);
2093 CloseSocket(hListenSocket);
2094 return false;
2097 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
2099 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
2100 AddLocal(addrBind, LOCAL_BIND);
2102 return true;
2105 void Discover(boost::thread_group& threadGroup)
2107 if (!fDiscover)
2108 return;
2110 #ifdef WIN32
2111 // Get local host IP
2112 char pszHostName[256] = "";
2113 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
2115 std::vector<CNetAddr> vaddr;
2116 if (LookupHost(pszHostName, vaddr, 0, true))
2118 BOOST_FOREACH (const CNetAddr &addr, vaddr)
2120 if (AddLocal(addr, LOCAL_IF))
2121 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
2125 #else
2126 // Get local host ip
2127 struct ifaddrs* myaddrs;
2128 if (getifaddrs(&myaddrs) == 0)
2130 for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
2132 if (ifa->ifa_addr == NULL) continue;
2133 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2134 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2135 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2136 if (ifa->ifa_addr->sa_family == AF_INET)
2138 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2139 CNetAddr addr(s4->sin_addr);
2140 if (AddLocal(addr, LOCAL_IF))
2141 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2143 else if (ifa->ifa_addr->sa_family == AF_INET6)
2145 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2146 CNetAddr addr(s6->sin6_addr);
2147 if (AddLocal(addr, LOCAL_IF))
2148 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2151 freeifaddrs(myaddrs);
2153 #endif
2156 void CConnman::SetNetworkActive(bool active)
2158 LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2160 if (!active) {
2161 fNetworkActive = false;
2163 LOCK(cs_vNodes);
2164 // Close sockets to all nodes
2165 BOOST_FOREACH(CNode* pnode, vNodes) {
2166 pnode->CloseSocketDisconnect();
2168 } else {
2169 fNetworkActive = true;
2172 uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2175 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2177 fNetworkActive = true;
2178 setBannedIsDirty = false;
2179 fAddressesInitialized = false;
2180 nLastNodeId = 0;
2181 nSendBufferMaxSize = 0;
2182 nReceiveFloodSize = 0;
2183 semOutbound = NULL;
2184 semAddnode = NULL;
2185 nMaxConnections = 0;
2186 nMaxOutbound = 0;
2187 nMaxAddnode = 0;
2188 nBestHeight = 0;
2189 clientInterface = NULL;
2190 flagInterruptMsgProc = false;
2193 NodeId CConnman::GetNewNodeId()
2195 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2198 bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options connOptions)
2200 nTotalBytesRecv = 0;
2201 nTotalBytesSent = 0;
2202 nMaxOutboundTotalBytesSentInCycle = 0;
2203 nMaxOutboundCycleStartTime = 0;
2205 nRelevantServices = connOptions.nRelevantServices;
2206 nLocalServices = connOptions.nLocalServices;
2207 nMaxConnections = connOptions.nMaxConnections;
2208 nMaxOutbound = std::min((connOptions.nMaxOutbound), nMaxConnections);
2209 nMaxAddnode = connOptions.nMaxAddnode;
2210 nMaxFeeler = connOptions.nMaxFeeler;
2212 nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
2213 nReceiveFloodSize = connOptions.nReceiveFloodSize;
2215 nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
2216 nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
2218 SetBestHeight(connOptions.nBestHeight);
2220 clientInterface = connOptions.uiInterface;
2221 if (clientInterface) {
2222 clientInterface->InitMessage(_("Loading P2P addresses..."));
2224 // Load addresses from peers.dat
2225 int64_t nStart = GetTimeMillis();
2227 CAddrDB adb;
2228 if (adb.Read(addrman))
2229 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2230 else {
2231 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2232 LogPrintf("Invalid or missing peers.dat; recreating\n");
2233 DumpAddresses();
2236 if (clientInterface)
2237 clientInterface->InitMessage(_("Loading banlist..."));
2238 // Load addresses from banlist.dat
2239 nStart = GetTimeMillis();
2240 CBanDB bandb;
2241 banmap_t banmap;
2242 if (bandb.Read(banmap)) {
2243 SetBanned(banmap); // thread save setter
2244 SetBannedSetDirty(false); // no need to write down, just read data
2245 SweepBanned(); // sweep out unused entries
2247 LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2248 banmap.size(), GetTimeMillis() - nStart);
2249 } else {
2250 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2251 SetBannedSetDirty(true); // force write
2252 DumpBanlist();
2255 uiInterface.InitMessage(_("Starting network threads..."));
2257 fAddressesInitialized = true;
2259 if (semOutbound == NULL) {
2260 // initialize semaphore
2261 semOutbound = new CSemaphore(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
2263 if (semAddnode == NULL) {
2264 // initialize semaphore
2265 semAddnode = new CSemaphore(nMaxAddnode);
2269 // Start threads
2271 InterruptSocks5(false);
2272 interruptNet.reset();
2273 flagInterruptMsgProc = false;
2276 std::unique_lock<std::mutex> lock(mutexMsgProc);
2277 fMsgProcWake = false;
2280 // Send and receive from sockets, accept connections
2281 threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2283 if (!GetBoolArg("-dnsseed", true))
2284 LogPrintf("DNS seeding disabled\n");
2285 else
2286 threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2288 // Initiate outbound connections from -addnode
2289 threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2291 // Initiate outbound connections unless connect=0
2292 if (!mapMultiArgs.count("-connect") || mapMultiArgs.at("-connect").size() != 1 || mapMultiArgs.at("-connect")[0] != "0")
2293 threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this)));
2295 // Process messages
2296 threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2298 // Dump network addresses
2299 scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
2301 return true;
2304 class CNetCleanup
2306 public:
2307 CNetCleanup() {}
2309 ~CNetCleanup()
2311 #ifdef WIN32
2312 // Shutdown Windows Sockets
2313 WSACleanup();
2314 #endif
2317 instance_of_cnetcleanup;
2319 void CConnman::Interrupt()
2322 std::lock_guard<std::mutex> lock(mutexMsgProc);
2323 flagInterruptMsgProc = true;
2325 condMsgProc.notify_all();
2327 interruptNet();
2328 InterruptSocks5(true);
2330 if (semOutbound) {
2331 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++) {
2332 semOutbound->post();
2336 if (semAddnode) {
2337 for (int i=0; i<nMaxAddnode; i++) {
2338 semAddnode->post();
2343 void CConnman::Stop()
2345 if (threadMessageHandler.joinable())
2346 threadMessageHandler.join();
2347 if (threadOpenConnections.joinable())
2348 threadOpenConnections.join();
2349 if (threadOpenAddedConnections.joinable())
2350 threadOpenAddedConnections.join();
2351 if (threadDNSAddressSeed.joinable())
2352 threadDNSAddressSeed.join();
2353 if (threadSocketHandler.joinable())
2354 threadSocketHandler.join();
2356 if (fAddressesInitialized)
2358 DumpData();
2359 fAddressesInitialized = false;
2362 // Close sockets
2363 BOOST_FOREACH(CNode* pnode, vNodes)
2364 pnode->CloseSocketDisconnect();
2365 BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
2366 if (hListenSocket.socket != INVALID_SOCKET)
2367 if (!CloseSocket(hListenSocket.socket))
2368 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2370 // clean up some globals (to help leak detection)
2371 BOOST_FOREACH(CNode *pnode, vNodes) {
2372 DeleteNode(pnode);
2374 BOOST_FOREACH(CNode *pnode, vNodesDisconnected) {
2375 DeleteNode(pnode);
2377 vNodes.clear();
2378 vNodesDisconnected.clear();
2379 vhListenSocket.clear();
2380 delete semOutbound;
2381 semOutbound = NULL;
2382 delete semAddnode;
2383 semAddnode = NULL;
2386 void CConnman::DeleteNode(CNode* pnode)
2388 assert(pnode);
2389 bool fUpdateConnectionTime = false;
2390 GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2391 if(fUpdateConnectionTime)
2392 addrman.Connected(pnode->addr);
2393 delete pnode;
2396 CConnman::~CConnman()
2398 Interrupt();
2399 Stop();
2402 size_t CConnman::GetAddressCount() const
2404 return addrman.size();
2407 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2409 addrman.SetServices(addr, nServices);
2412 void CConnman::MarkAddressGood(const CAddress& addr)
2414 addrman.Good(addr);
2417 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2419 addrman.Add(vAddr, addrFrom, nTimePenalty);
2422 std::vector<CAddress> CConnman::GetAddresses()
2424 return addrman.GetAddr();
2427 bool CConnman::AddNode(const std::string& strNode)
2429 LOCK(cs_vAddedNodes);
2430 for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2431 if (strNode == *it)
2432 return false;
2435 vAddedNodes.push_back(strNode);
2436 return true;
2439 bool CConnman::RemoveAddedNode(const std::string& strNode)
2441 LOCK(cs_vAddedNodes);
2442 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2443 if (strNode == *it) {
2444 vAddedNodes.erase(it);
2445 return true;
2448 return false;
2451 size_t CConnman::GetNodeCount(NumConnections flags)
2453 LOCK(cs_vNodes);
2454 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2455 return vNodes.size();
2457 int nNum = 0;
2458 for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
2459 if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
2460 nNum++;
2462 return nNum;
2465 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2467 vstats.clear();
2468 LOCK(cs_vNodes);
2469 vstats.reserve(vNodes.size());
2470 for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
2471 CNode* pnode = *it;
2472 vstats.emplace_back();
2473 pnode->copyStats(vstats.back());
2477 bool CConnman::DisconnectNode(const std::string& strNode)
2479 LOCK(cs_vNodes);
2480 if (CNode* pnode = FindNode(strNode)) {
2481 pnode->fDisconnect = true;
2482 return true;
2484 return false;
2486 bool CConnman::DisconnectNode(NodeId id)
2488 LOCK(cs_vNodes);
2489 for(CNode* pnode : vNodes) {
2490 if (id == pnode->GetId()) {
2491 pnode->fDisconnect = true;
2492 return true;
2495 return false;
2498 void CConnman::RecordBytesRecv(uint64_t bytes)
2500 LOCK(cs_totalBytesRecv);
2501 nTotalBytesRecv += bytes;
2504 void CConnman::RecordBytesSent(uint64_t bytes)
2506 LOCK(cs_totalBytesSent);
2507 nTotalBytesSent += bytes;
2509 uint64_t now = GetTime();
2510 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2512 // timeframe expired, reset cycle
2513 nMaxOutboundCycleStartTime = now;
2514 nMaxOutboundTotalBytesSentInCycle = 0;
2517 // TODO, exclude whitebind peers
2518 nMaxOutboundTotalBytesSentInCycle += bytes;
2521 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2523 LOCK(cs_totalBytesSent);
2524 nMaxOutboundLimit = limit;
2527 uint64_t CConnman::GetMaxOutboundTarget()
2529 LOCK(cs_totalBytesSent);
2530 return nMaxOutboundLimit;
2533 uint64_t CConnman::GetMaxOutboundTimeframe()
2535 LOCK(cs_totalBytesSent);
2536 return nMaxOutboundTimeframe;
2539 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2541 LOCK(cs_totalBytesSent);
2542 if (nMaxOutboundLimit == 0)
2543 return 0;
2545 if (nMaxOutboundCycleStartTime == 0)
2546 return nMaxOutboundTimeframe;
2548 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2549 uint64_t now = GetTime();
2550 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2553 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2555 LOCK(cs_totalBytesSent);
2556 if (nMaxOutboundTimeframe != timeframe)
2558 // reset measure-cycle in case of changing
2559 // the timeframe
2560 nMaxOutboundCycleStartTime = GetTime();
2562 nMaxOutboundTimeframe = timeframe;
2565 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2567 LOCK(cs_totalBytesSent);
2568 if (nMaxOutboundLimit == 0)
2569 return false;
2571 if (historicalBlockServingLimit)
2573 // keep a large enough buffer to at least relay each block once
2574 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2575 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2576 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2577 return true;
2579 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2580 return true;
2582 return false;
2585 uint64_t CConnman::GetOutboundTargetBytesLeft()
2587 LOCK(cs_totalBytesSent);
2588 if (nMaxOutboundLimit == 0)
2589 return 0;
2591 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2594 uint64_t CConnman::GetTotalBytesRecv()
2596 LOCK(cs_totalBytesRecv);
2597 return nTotalBytesRecv;
2600 uint64_t CConnman::GetTotalBytesSent()
2602 LOCK(cs_totalBytesSent);
2603 return nTotalBytesSent;
2606 ServiceFlags CConnman::GetLocalServices() const
2608 return nLocalServices;
2611 void CConnman::SetBestHeight(int height)
2613 nBestHeight.store(height, std::memory_order_release);
2616 int CConnman::GetBestHeight() const
2618 return nBestHeight.load(std::memory_order_acquire);
2621 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2622 unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize; }
2624 CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string& addrNameIn, bool fInboundIn) :
2625 nTimeConnected(GetSystemTimeInSeconds()),
2626 addr(addrIn),
2627 fInbound(fInboundIn),
2628 nKeyedNetGroup(nKeyedNetGroupIn),
2629 addrKnown(5000, 0.001),
2630 filterInventoryKnown(50000, 0.000001),
2631 id(idIn),
2632 nLocalHostNonce(nLocalHostNonceIn),
2633 nLocalServices(nLocalServicesIn),
2634 nMyStartingHeight(nMyStartingHeightIn),
2635 nSendVersion(0)
2637 nServices = NODE_NONE;
2638 nServicesExpected = NODE_NONE;
2639 hSocket = hSocketIn;
2640 nRecvVersion = INIT_PROTO_VERSION;
2641 nLastSend = 0;
2642 nLastRecv = 0;
2643 nSendBytes = 0;
2644 nRecvBytes = 0;
2645 nTimeOffset = 0;
2646 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2647 nVersion = 0;
2648 strSubVer = "";
2649 fWhitelisted = false;
2650 fOneShot = false;
2651 fAddnode = false;
2652 fClient = false; // set by version message
2653 fFeeler = false;
2654 fSuccessfullyConnected = false;
2655 fDisconnect = false;
2656 nRefCount = 0;
2657 nSendSize = 0;
2658 nSendOffset = 0;
2659 hashContinue = uint256();
2660 nStartingHeight = -1;
2661 filterInventoryKnown.reset();
2662 fSendMempool = false;
2663 fGetAddr = false;
2664 nNextLocalAddrSend = 0;
2665 nNextAddrSend = 0;
2666 nNextInvSend = 0;
2667 fRelayTxes = false;
2668 fSentAddr = false;
2669 pfilter = new CBloomFilter();
2670 timeLastMempoolReq = 0;
2671 nLastBlockTime = 0;
2672 nLastTXTime = 0;
2673 nPingNonceSent = 0;
2674 nPingUsecStart = 0;
2675 nPingUsecTime = 0;
2676 fPingQueued = false;
2677 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2678 minFeeFilter = 0;
2679 lastSentFeeFilter = 0;
2680 nextSendTimeFeeFilter = 0;
2681 fPauseRecv = false;
2682 fPauseSend = false;
2683 nProcessQueueSize = 0;
2685 BOOST_FOREACH(const std::string &msg, getAllNetMessageTypes())
2686 mapRecvBytesPerMsgCmd[msg] = 0;
2687 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2689 if (fLogIPs) {
2690 LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
2691 } else {
2692 LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
2696 CNode::~CNode()
2698 CloseSocket(hSocket);
2700 if (pfilter)
2701 delete pfilter;
2704 void CNode::AskFor(const CInv& inv)
2706 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2707 return;
2708 // a peer may not have multiple non-responded queue positions for a single inv item
2709 if (!setAskFor.insert(inv.hash).second)
2710 return;
2712 // We're using mapAskFor as a priority queue,
2713 // the key is the earliest time the request can be sent
2714 int64_t nRequestTime;
2715 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2716 if (it != mapAlreadyAskedFor.end())
2717 nRequestTime = it->second;
2718 else
2719 nRequestTime = 0;
2720 LogPrint(BCLog::NET, "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2722 // Make sure not to reuse time indexes to keep things in the same order
2723 int64_t nNow = GetTimeMicros() - 1000000;
2724 static int64_t nLastTime;
2725 ++nLastTime;
2726 nNow = std::max(nNow, nLastTime);
2727 nLastTime = nNow;
2729 // Each retry is 2 minutes after the last
2730 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2731 if (it != mapAlreadyAskedFor.end())
2732 mapAlreadyAskedFor.update(it, nRequestTime);
2733 else
2734 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2735 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2738 bool CConnman::NodeFullyConnected(const CNode* pnode)
2740 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
2743 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2745 size_t nMessageSize = msg.data.size();
2746 size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2747 LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2749 std::vector<unsigned char> serializedHeader;
2750 serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
2751 uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
2752 CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize);
2753 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
2755 CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
2757 size_t nBytesSent = 0;
2759 LOCK(pnode->cs_vSend);
2760 bool optimisticSend(pnode->vSendMsg.empty());
2762 //log total amount of bytes per command
2763 pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
2764 pnode->nSendSize += nTotalSize;
2766 if (pnode->nSendSize > nSendBufferMaxSize)
2767 pnode->fPauseSend = true;
2768 pnode->vSendMsg.push_back(std::move(serializedHeader));
2769 if (nMessageSize)
2770 pnode->vSendMsg.push_back(std::move(msg.data));
2772 // If write queue empty, attempt "optimistic write"
2773 if (optimisticSend == true)
2774 nBytesSent = SocketSendData(pnode);
2776 if (nBytesSent)
2777 RecordBytesSent(nBytesSent);
2780 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2782 CNode* found = nullptr;
2783 LOCK(cs_vNodes);
2784 for (auto&& pnode : vNodes) {
2785 if(pnode->GetId() == id) {
2786 found = pnode;
2787 break;
2790 return found != nullptr && NodeFullyConnected(found) && func(found);
2793 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2794 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2797 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
2799 return CSipHasher(nSeed0, nSeed1).Write(id);
2802 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
2804 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2806 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(&vchNetGroup[0], vchNetGroup.size()).Finalize();