Merge #11363: net: Split socket create/connect
[bitcoinplatinum.git] / src / net.cpp
blob23bf6404725f1d5e010d3a802d2f9555b1d7d9b6
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #if defined(HAVE_CONFIG_H)
7 #include <config/bitcoin-config.h>
8 #endif
10 #include <net.h>
12 #include <chainparams.h>
13 #include <clientversion.h>
14 #include <consensus/consensus.h>
15 #include <crypto/common.h>
16 #include <crypto/sha256.h>
17 #include <primitives/transaction.h>
18 #include <netbase.h>
19 #include <scheduler.h>
20 #include <ui_interface.h>
21 #include <utilstrencodings.h>
23 #ifdef WIN32
24 #include <string.h>
25 #else
26 #include <fcntl.h>
27 #endif
29 #ifdef USE_UPNP
30 #include <miniupnpc/miniupnpc.h>
31 #include <miniupnpc/miniwget.h>
32 #include <miniupnpc/upnpcommands.h>
33 #include <miniupnpc/upnperrors.h>
34 #endif
37 #include <math.h>
39 // Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
40 #define DUMP_ADDRESSES_INTERVAL 900
42 // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
43 #define FEELER_SLEEP_WINDOW 1
45 #if !defined(HAVE_MSG_NOSIGNAL)
46 #define MSG_NOSIGNAL 0
47 #endif
49 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
50 #if !defined(HAVE_MSG_DONTWAIT)
51 #define MSG_DONTWAIT 0
52 #endif
54 // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
55 // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
56 #ifdef WIN32
57 #ifndef PROTECTION_LEVEL_UNRESTRICTED
58 #define PROTECTION_LEVEL_UNRESTRICTED 10
59 #endif
60 #ifndef IPV6_PROTECTION_LEVEL
61 #define IPV6_PROTECTION_LEVEL 23
62 #endif
63 #endif
65 /** Used to pass flags to the Bind() function */
66 enum BindFlags {
67 BF_NONE = 0,
68 BF_EXPLICIT = (1U << 0),
69 BF_REPORT_ERROR = (1U << 1),
70 BF_WHITELIST = (1U << 2),
73 const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
75 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
76 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // SHA256("localhostnonce")[0:8]
78 // Global state variables
80 bool fDiscover = true;
81 bool fListen = true;
82 bool fRelayTxes = true;
83 CCriticalSection cs_mapLocalHost;
84 std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
85 static bool vfLimited[NET_MAX] = {};
86 std::string strSubVersion;
88 limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
90 void CConnman::AddOneShot(const std::string& strDest)
92 LOCK(cs_vOneShots);
93 vOneShots.push_back(strDest);
96 unsigned short GetListenPort()
98 return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
101 // find 'best' local address for a particular peer
102 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
104 if (!fListen)
105 return false;
107 int nBestScore = -1;
108 int nBestReachability = -1;
110 LOCK(cs_mapLocalHost);
111 for (const auto& entry : mapLocalHost)
113 int nScore = entry.second.nScore;
114 int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
115 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
117 addr = CService(entry.first, entry.second.nPort);
118 nBestReachability = nReachability;
119 nBestScore = nScore;
123 return nBestScore >= 0;
126 //! Convert the pnSeeds6 array into usable address objects.
127 static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
129 // It'll only connect to one or two seed nodes because once it connects,
130 // it'll get a pile of addresses with newer timestamps.
131 // Seed nodes are given a random 'last seen time' of between one and two
132 // weeks ago.
133 const int64_t nOneWeek = 7*24*60*60;
134 std::vector<CAddress> vSeedsOut;
135 vSeedsOut.reserve(vSeedsIn.size());
136 for (const auto& seed_in : vSeedsIn) {
137 struct in6_addr ip;
138 memcpy(&ip, seed_in.addr, sizeof(ip));
139 CAddress addr(CService(ip, seed_in.port), NODE_NETWORK);
140 addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
141 vSeedsOut.push_back(addr);
143 return vSeedsOut;
146 // get best local address for a particular peer as a CAddress
147 // Otherwise, return the unroutable 0.0.0.0 but filled in with
148 // the normal parameters, since the IP may be changed to a useful
149 // one by discovery.
150 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
152 CAddress ret(CService(CNetAddr(),GetListenPort()), nLocalServices);
153 CService addr;
154 if (GetLocal(addr, paddrPeer))
156 ret = CAddress(addr, nLocalServices);
158 ret.nTime = GetAdjustedTime();
159 return ret;
162 int GetnScore(const CService& addr)
164 LOCK(cs_mapLocalHost);
165 if (mapLocalHost.count(addr) == LOCAL_NONE)
166 return 0;
167 return mapLocalHost[addr].nScore;
170 // Is our peer's addrLocal potentially useful as an external IP source?
171 bool IsPeerAddrLocalGood(CNode *pnode)
173 CService addrLocal = pnode->GetAddrLocal();
174 return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
175 !IsLimited(addrLocal.GetNetwork());
178 // pushes our own address to a peer
179 void AdvertiseLocal(CNode *pnode)
181 if (fListen && pnode->fSuccessfullyConnected)
183 CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
184 // If discovery is enabled, sometimes give our peer the address it
185 // tells us that it sees us as in case it has a better idea of our
186 // address than we do.
187 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
188 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
190 addrLocal.SetIP(pnode->GetAddrLocal());
192 if (addrLocal.IsRoutable())
194 LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
195 FastRandomContext insecure_rand;
196 pnode->PushAddress(addrLocal, insecure_rand);
201 // learn a new local address
202 bool AddLocal(const CService& addr, int nScore)
204 if (!addr.IsRoutable())
205 return false;
207 if (!fDiscover && nScore < LOCAL_MANUAL)
208 return false;
210 if (IsLimited(addr))
211 return false;
213 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
216 LOCK(cs_mapLocalHost);
217 bool fAlready = mapLocalHost.count(addr) > 0;
218 LocalServiceInfo &info = mapLocalHost[addr];
219 if (!fAlready || nScore >= info.nScore) {
220 info.nScore = nScore + (fAlready ? 1 : 0);
221 info.nPort = addr.GetPort();
225 return true;
228 bool AddLocal(const CNetAddr &addr, int nScore)
230 return AddLocal(CService(addr, GetListenPort()), nScore);
233 bool RemoveLocal(const CService& addr)
235 LOCK(cs_mapLocalHost);
236 LogPrintf("RemoveLocal(%s)\n", addr.ToString());
237 mapLocalHost.erase(addr);
238 return true;
241 /** Make a particular network entirely off-limits (no automatic connects to it) */
242 void SetLimited(enum Network net, bool fLimited)
244 if (net == NET_UNROUTABLE || net == NET_INTERNAL)
245 return;
246 LOCK(cs_mapLocalHost);
247 vfLimited[net] = fLimited;
250 bool IsLimited(enum Network net)
252 LOCK(cs_mapLocalHost);
253 return vfLimited[net];
256 bool IsLimited(const CNetAddr &addr)
258 return IsLimited(addr.GetNetwork());
261 /** vote for a local address */
262 bool SeenLocal(const CService& addr)
265 LOCK(cs_mapLocalHost);
266 if (mapLocalHost.count(addr) == 0)
267 return false;
268 mapLocalHost[addr].nScore++;
270 return true;
274 /** check whether a given address is potentially local */
275 bool IsLocal(const CService& addr)
277 LOCK(cs_mapLocalHost);
278 return mapLocalHost.count(addr) > 0;
281 /** check whether a given network is one we can probably connect to */
282 bool IsReachable(enum Network net)
284 LOCK(cs_mapLocalHost);
285 return !vfLimited[net];
288 /** check whether a given address is in a network we can probably connect to */
289 bool IsReachable(const CNetAddr& addr)
291 enum Network net = addr.GetNetwork();
292 return IsReachable(net);
296 CNode* CConnman::FindNode(const CNetAddr& ip)
298 LOCK(cs_vNodes);
299 for (CNode* pnode : vNodes) {
300 if ((CNetAddr)pnode->addr == ip) {
301 return pnode;
304 return nullptr;
307 CNode* CConnman::FindNode(const CSubNet& subNet)
309 LOCK(cs_vNodes);
310 for (CNode* pnode : vNodes) {
311 if (subNet.Match((CNetAddr)pnode->addr)) {
312 return pnode;
315 return nullptr;
318 CNode* CConnman::FindNode(const std::string& addrName)
320 LOCK(cs_vNodes);
321 for (CNode* pnode : vNodes) {
322 if (pnode->GetAddrName() == addrName) {
323 return pnode;
326 return nullptr;
329 CNode* CConnman::FindNode(const CService& addr)
331 LOCK(cs_vNodes);
332 for (CNode* pnode : vNodes) {
333 if ((CService)pnode->addr == addr) {
334 return pnode;
337 return nullptr;
340 bool CConnman::CheckIncomingNonce(uint64_t nonce)
342 LOCK(cs_vNodes);
343 for (CNode* pnode : vNodes) {
344 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
345 return false;
347 return true;
350 /** Get the bind address for a socket as CAddress */
351 static CAddress GetBindAddress(SOCKET sock)
353 CAddress addr_bind;
354 struct sockaddr_storage sockaddr_bind;
355 socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
356 if (sock != INVALID_SOCKET) {
357 if (!getsockname(sock, (struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
358 addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind);
359 } else {
360 LogPrint(BCLog::NET, "Warning: getsockname failed\n");
363 return addr_bind;
366 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
368 if (pszDest == nullptr) {
369 if (IsLocal(addrConnect))
370 return nullptr;
372 // Look for an existing connection
373 CNode* pnode = FindNode((CService)addrConnect);
374 if (pnode)
376 LogPrintf("Failed to open new connection, already connected\n");
377 return nullptr;
381 /// debug print
382 LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
383 pszDest ? pszDest : addrConnect.ToString(),
384 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
386 // Resolve
387 const int default_port = Params().GetDefaultPort();
388 if (pszDest) {
389 std::vector<CService> resolved;
390 if (Lookup(pszDest, resolved, default_port, fNameLookup && !HaveNameProxy(), 256) && !resolved.empty()) {
391 addrConnect = CAddress(resolved[GetRand(resolved.size())], NODE_NONE);
392 if (!addrConnect.IsValid()) {
393 LogPrint(BCLog::NET, "Resolver returned invalid address %s for %s", addrConnect.ToString(), pszDest);
394 return nullptr;
396 // It is possible that we already have a connection to the IP/port pszDest resolved to.
397 // In that case, drop the connection that was just created, and return the existing CNode instead.
398 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
399 // name catch this early.
400 LOCK(cs_vNodes);
401 CNode* pnode = FindNode((CService)addrConnect);
402 if (pnode)
404 pnode->MaybeSetAddrName(std::string(pszDest));
405 LogPrintf("Failed to open new connection, already connected\n");
406 return nullptr;
411 // Connect
412 bool connected = false;
413 SOCKET hSocket;
414 proxyType proxy;
415 if (addrConnect.IsValid()) {
416 bool proxyConnectionFailed = false;
418 if (GetProxy(addrConnect.GetNetwork(), proxy)) {
419 hSocket = CreateSocket(proxy.proxy);
420 if (hSocket == INVALID_SOCKET) {
421 return nullptr;
423 connected = ConnectThroughProxy(proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), hSocket, nConnectTimeout, &proxyConnectionFailed);
424 } else {
425 // no proxy needed (none set for target network)
426 hSocket = CreateSocket(addrConnect);
427 if (hSocket == INVALID_SOCKET) {
428 return nullptr;
430 connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout);
432 if (!proxyConnectionFailed) {
433 // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
434 // the proxy, mark this as an attempt.
435 addrman.Attempt(addrConnect, fCountFailure);
437 } else if (pszDest && GetNameProxy(proxy)) {
438 hSocket = CreateSocket(proxy.proxy);
439 if (hSocket == INVALID_SOCKET) {
440 return nullptr;
442 std::string host;
443 int port = default_port;
444 SplitHostPort(std::string(pszDest), port, host);
445 connected = ConnectThroughProxy(proxy, host, port, hSocket, nConnectTimeout, nullptr);
447 if (!connected) {
448 CloseSocket(hSocket);
449 return nullptr;
452 // Add node
453 NodeId id = GetNewNodeId();
454 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
455 CAddress addr_bind = GetBindAddress(hSocket);
456 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", false);
457 pnode->AddRef();
459 return pnode;
462 void CConnman::DumpBanlist()
464 SweepBanned(); // clean unused entries (if bantime has expired)
466 if (!BannedSetIsDirty())
467 return;
469 int64_t nStart = GetTimeMillis();
471 CBanDB bandb;
472 banmap_t banmap;
473 GetBanned(banmap);
474 if (bandb.Write(banmap)) {
475 SetBannedSetDirty(false);
478 LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
479 banmap.size(), GetTimeMillis() - nStart);
482 void CNode::CloseSocketDisconnect()
484 fDisconnect = true;
485 LOCK(cs_hSocket);
486 if (hSocket != INVALID_SOCKET)
488 LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
489 CloseSocket(hSocket);
493 void CConnman::ClearBanned()
496 LOCK(cs_setBanned);
497 setBanned.clear();
498 setBannedIsDirty = true;
500 DumpBanlist(); //store banlist to disk
501 if(clientInterface)
502 clientInterface->BannedListChanged();
505 bool CConnman::IsBanned(CNetAddr ip)
507 LOCK(cs_setBanned);
508 for (const auto& it : setBanned) {
509 CSubNet subNet = it.first;
510 CBanEntry banEntry = it.second;
512 if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
513 return true;
516 return false;
519 bool CConnman::IsBanned(CSubNet subnet)
521 LOCK(cs_setBanned);
522 banmap_t::iterator i = setBanned.find(subnet);
523 if (i != setBanned.end())
525 CBanEntry banEntry = (*i).second;
526 if (GetTime() < banEntry.nBanUntil) {
527 return true;
530 return false;
533 void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
534 CSubNet subNet(addr);
535 Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
538 void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
539 CBanEntry banEntry(GetTime());
540 banEntry.banReason = banReason;
541 if (bantimeoffset <= 0)
543 bantimeoffset = gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
544 sinceUnixEpoch = false;
546 banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
549 LOCK(cs_setBanned);
550 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
551 setBanned[subNet] = banEntry;
552 setBannedIsDirty = true;
554 else
555 return;
557 if(clientInterface)
558 clientInterface->BannedListChanged();
560 LOCK(cs_vNodes);
561 for (CNode* pnode : vNodes) {
562 if (subNet.Match((CNetAddr)pnode->addr))
563 pnode->fDisconnect = true;
566 if(banReason == BanReasonManuallyAdded)
567 DumpBanlist(); //store banlist to disk immediately if user requested ban
570 bool CConnman::Unban(const CNetAddr &addr) {
571 CSubNet subNet(addr);
572 return Unban(subNet);
575 bool CConnman::Unban(const CSubNet &subNet) {
577 LOCK(cs_setBanned);
578 if (!setBanned.erase(subNet))
579 return false;
580 setBannedIsDirty = true;
582 if(clientInterface)
583 clientInterface->BannedListChanged();
584 DumpBanlist(); //store banlist to disk immediately
585 return true;
588 void CConnman::GetBanned(banmap_t &banMap)
590 LOCK(cs_setBanned);
591 // Sweep the banlist so expired bans are not returned
592 SweepBanned();
593 banMap = setBanned; //create a thread safe copy
596 void CConnman::SetBanned(const banmap_t &banMap)
598 LOCK(cs_setBanned);
599 setBanned = banMap;
600 setBannedIsDirty = true;
603 void CConnman::SweepBanned()
605 int64_t now = GetTime();
607 LOCK(cs_setBanned);
608 banmap_t::iterator it = setBanned.begin();
609 while(it != setBanned.end())
611 CSubNet subNet = (*it).first;
612 CBanEntry banEntry = (*it).second;
613 if(now > banEntry.nBanUntil)
615 setBanned.erase(it++);
616 setBannedIsDirty = true;
617 LogPrint(BCLog::NET, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
619 else
620 ++it;
624 bool CConnman::BannedSetIsDirty()
626 LOCK(cs_setBanned);
627 return setBannedIsDirty;
630 void CConnman::SetBannedSetDirty(bool dirty)
632 LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
633 setBannedIsDirty = dirty;
637 bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
638 for (const CSubNet& subnet : vWhitelistedRange) {
639 if (subnet.Match(addr))
640 return true;
642 return false;
645 std::string CNode::GetAddrName() const {
646 LOCK(cs_addrName);
647 return addrName;
650 void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
651 LOCK(cs_addrName);
652 if (addrName.empty()) {
653 addrName = addrNameIn;
657 CService CNode::GetAddrLocal() const {
658 LOCK(cs_addrLocal);
659 return addrLocal;
662 void CNode::SetAddrLocal(const CService& addrLocalIn) {
663 LOCK(cs_addrLocal);
664 if (addrLocal.IsValid()) {
665 error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
666 } else {
667 addrLocal = addrLocalIn;
671 #undef X
672 #define X(name) stats.name = name
673 void CNode::copyStats(CNodeStats &stats)
675 stats.nodeid = this->GetId();
676 X(nServices);
677 X(addr);
678 X(addrBind);
680 LOCK(cs_filter);
681 X(fRelayTxes);
683 X(nLastSend);
684 X(nLastRecv);
685 X(nTimeConnected);
686 X(nTimeOffset);
687 stats.addrName = GetAddrName();
688 X(nVersion);
690 LOCK(cs_SubVer);
691 X(cleanSubVer);
693 X(fInbound);
694 X(m_manual_connection);
695 X(nStartingHeight);
697 LOCK(cs_vSend);
698 X(mapSendBytesPerMsgCmd);
699 X(nSendBytes);
702 LOCK(cs_vRecv);
703 X(mapRecvBytesPerMsgCmd);
704 X(nRecvBytes);
706 X(fWhitelisted);
708 // It is common for nodes with good ping times to suddenly become lagged,
709 // due to a new block arriving or other large transfer.
710 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
711 // since pingtime does not update until the ping is complete, which might take a while.
712 // So, if a ping is taking an unusually long time in flight,
713 // the caller can immediately detect that this is happening.
714 int64_t nPingUsecWait = 0;
715 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
716 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
719 // 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 :)
720 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
721 stats.dMinPing = (((double)nMinPingUsecTime) / 1e6);
722 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
724 // Leave string empty if addrLocal invalid (not filled in yet)
725 CService addrLocalUnlocked = GetAddrLocal();
726 stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
728 #undef X
730 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
732 complete = false;
733 int64_t nTimeMicros = GetTimeMicros();
734 LOCK(cs_vRecv);
735 nLastRecv = nTimeMicros / 1000000;
736 nRecvBytes += nBytes;
737 while (nBytes > 0) {
739 // get current incomplete message, or create a new one
740 if (vRecvMsg.empty() ||
741 vRecvMsg.back().complete())
742 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
744 CNetMessage& msg = vRecvMsg.back();
746 // absorb network data
747 int handled;
748 if (!msg.in_data)
749 handled = msg.readHeader(pch, nBytes);
750 else
751 handled = msg.readData(pch, nBytes);
753 if (handled < 0)
754 return false;
756 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
757 LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
758 return false;
761 pch += handled;
762 nBytes -= handled;
764 if (msg.complete()) {
766 //store received bytes per message command
767 //to prevent a memory DOS, only allow valid commands
768 mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
769 if (i == mapRecvBytesPerMsgCmd.end())
770 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
771 assert(i != mapRecvBytesPerMsgCmd.end());
772 i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
774 msg.nTime = nTimeMicros;
775 complete = true;
779 return true;
782 void CNode::SetSendVersion(int nVersionIn)
784 // Send version may only be changed in the version message, and
785 // only one version message is allowed per session. We can therefore
786 // treat this value as const and even atomic as long as it's only used
787 // once a version message has been successfully processed. Any attempt to
788 // set this twice is an error.
789 if (nSendVersion != 0) {
790 error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
791 } else {
792 nSendVersion = nVersionIn;
796 int CNode::GetSendVersion() const
798 // The send version should always be explicitly set to
799 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
800 // has been called.
801 if (nSendVersion == 0) {
802 error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
803 return INIT_PROTO_VERSION;
805 return nSendVersion;
809 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
811 // copy data to temporary parsing buffer
812 unsigned int nRemaining = 24 - nHdrPos;
813 unsigned int nCopy = std::min(nRemaining, nBytes);
815 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
816 nHdrPos += nCopy;
818 // if header incomplete, exit
819 if (nHdrPos < 24)
820 return nCopy;
822 // deserialize to CMessageHeader
823 try {
824 hdrbuf >> hdr;
826 catch (const std::exception&) {
827 return -1;
830 // reject messages larger than MAX_SIZE
831 if (hdr.nMessageSize > MAX_SIZE)
832 return -1;
834 // switch state to reading message data
835 in_data = true;
837 return nCopy;
840 int CNetMessage::readData(const char *pch, unsigned int nBytes)
842 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
843 unsigned int nCopy = std::min(nRemaining, nBytes);
845 if (vRecv.size() < nDataPos + nCopy) {
846 // Allocate up to 256 KiB ahead, but never more than the total message size.
847 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
850 hasher.Write((const unsigned char*)pch, nCopy);
851 memcpy(&vRecv[nDataPos], pch, nCopy);
852 nDataPos += nCopy;
854 return nCopy;
857 const uint256& CNetMessage::GetMessageHash() const
859 assert(complete());
860 if (data_hash.IsNull())
861 hasher.Finalize(data_hash.begin());
862 return data_hash;
873 // requires LOCK(cs_vSend)
874 size_t CConnman::SocketSendData(CNode *pnode) const
876 auto it = pnode->vSendMsg.begin();
877 size_t nSentSize = 0;
879 while (it != pnode->vSendMsg.end()) {
880 const auto &data = *it;
881 assert(data.size() > pnode->nSendOffset);
882 int nBytes = 0;
884 LOCK(pnode->cs_hSocket);
885 if (pnode->hSocket == INVALID_SOCKET)
886 break;
887 nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
889 if (nBytes > 0) {
890 pnode->nLastSend = GetSystemTimeInSeconds();
891 pnode->nSendBytes += nBytes;
892 pnode->nSendOffset += nBytes;
893 nSentSize += nBytes;
894 if (pnode->nSendOffset == data.size()) {
895 pnode->nSendOffset = 0;
896 pnode->nSendSize -= data.size();
897 pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
898 it++;
899 } else {
900 // could not send full message; stop sending more
901 break;
903 } else {
904 if (nBytes < 0) {
905 // error
906 int nErr = WSAGetLastError();
907 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
909 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
910 pnode->CloseSocketDisconnect();
913 // couldn't send anything at all
914 break;
918 if (it == pnode->vSendMsg.end()) {
919 assert(pnode->nSendOffset == 0);
920 assert(pnode->nSendSize == 0);
922 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
923 return nSentSize;
926 struct NodeEvictionCandidate
928 NodeId id;
929 int64_t nTimeConnected;
930 int64_t nMinPingUsecTime;
931 int64_t nLastBlockTime;
932 int64_t nLastTXTime;
933 bool fRelevantServices;
934 bool fRelayTxes;
935 bool fBloomFilter;
936 CAddress addr;
937 uint64_t nKeyedNetGroup;
940 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
942 return a.nMinPingUsecTime > b.nMinPingUsecTime;
945 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
947 return a.nTimeConnected > b.nTimeConnected;
950 static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
951 return a.nKeyedNetGroup < b.nKeyedNetGroup;
954 static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
956 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
957 if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
958 if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
959 return a.nTimeConnected > b.nTimeConnected;
962 static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
964 // 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.
965 if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
966 if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
967 if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
968 return a.nTimeConnected > b.nTimeConnected;
972 //! Sort an array by the specified comparator, then erase the last K elements.
973 template<typename T, typename Comparator>
974 static void EraseLastKElements(std::vector<T> &elements, Comparator comparator, size_t k)
976 std::sort(elements.begin(), elements.end(), comparator);
977 size_t eraseSize = std::min(k, elements.size());
978 elements.erase(elements.end() - eraseSize, elements.end());
981 /** Try to find a connection to evict when the node is full.
982 * Extreme care must be taken to avoid opening the node to attacker
983 * triggered network partitioning.
984 * The strategy used here is to protect a small number of peers
985 * for each of several distinct characteristics which are difficult
986 * to forge. In order to partition a node the attacker must be
987 * simultaneously better at all of them than honest peers.
989 bool CConnman::AttemptToEvictConnection()
991 std::vector<NodeEvictionCandidate> vEvictionCandidates;
993 LOCK(cs_vNodes);
995 for (const CNode* node : vNodes) {
996 if (node->fWhitelisted)
997 continue;
998 if (!node->fInbound)
999 continue;
1000 if (node->fDisconnect)
1001 continue;
1002 NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
1003 node->nLastBlockTime, node->nLastTXTime,
1004 HasAllDesirableServiceFlags(node->nServices),
1005 node->fRelayTxes, node->pfilter != nullptr, node->addr, node->nKeyedNetGroup};
1006 vEvictionCandidates.push_back(candidate);
1010 // Protect connections with certain characteristics
1012 // Deterministically select 4 peers to protect by netgroup.
1013 // An attacker cannot predict which netgroups will be protected
1014 EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
1015 // Protect the 8 nodes with the lowest minimum ping time.
1016 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
1017 EraseLastKElements(vEvictionCandidates, ReverseCompareNodeMinPingTime, 8);
1018 // Protect 4 nodes that most recently sent us transactions.
1019 // An attacker cannot manipulate this metric without performing useful work.
1020 EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4);
1021 // Protect 4 nodes that most recently sent us blocks.
1022 // An attacker cannot manipulate this metric without performing useful work.
1023 EraseLastKElements(vEvictionCandidates, CompareNodeBlockTime, 4);
1024 // Protect the half of the remaining nodes which have been connected the longest.
1025 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1026 EraseLastKElements(vEvictionCandidates, ReverseCompareNodeTimeConnected, vEvictionCandidates.size() / 2);
1028 if (vEvictionCandidates.empty()) return false;
1030 // Identify the network group with the most connections and youngest member.
1031 // (vEvictionCandidates is already sorted by reverse connect time)
1032 uint64_t naMostConnections;
1033 unsigned int nMostConnections = 0;
1034 int64_t nMostConnectionsTime = 0;
1035 std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
1036 for (const NodeEvictionCandidate &node : vEvictionCandidates) {
1037 std::vector<NodeEvictionCandidate> &group = mapNetGroupNodes[node.nKeyedNetGroup];
1038 group.push_back(node);
1039 int64_t grouptime = group[0].nTimeConnected;
1041 if (group.size() > nMostConnections || (group.size() == nMostConnections && grouptime > nMostConnectionsTime)) {
1042 nMostConnections = group.size();
1043 nMostConnectionsTime = grouptime;
1044 naMostConnections = node.nKeyedNetGroup;
1048 // Reduce to the network group with the most connections
1049 vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1051 // Disconnect from the network group with the most connections
1052 NodeId evicted = vEvictionCandidates.front().id;
1053 LOCK(cs_vNodes);
1054 for (CNode* pnode : vNodes) {
1055 if (pnode->GetId() == evicted) {
1056 pnode->fDisconnect = true;
1057 return true;
1060 return false;
1063 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1064 struct sockaddr_storage sockaddr;
1065 socklen_t len = sizeof(sockaddr);
1066 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
1067 CAddress addr;
1068 int nInbound = 0;
1069 int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler);
1071 if (hSocket != INVALID_SOCKET) {
1072 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1073 LogPrintf("Warning: Unknown socket family\n");
1077 bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
1079 LOCK(cs_vNodes);
1080 for (const CNode* pnode : vNodes) {
1081 if (pnode->fInbound) nInbound++;
1085 if (hSocket == INVALID_SOCKET)
1087 int nErr = WSAGetLastError();
1088 if (nErr != WSAEWOULDBLOCK)
1089 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1090 return;
1093 if (!fNetworkActive) {
1094 LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
1095 CloseSocket(hSocket);
1096 return;
1099 if (!IsSelectableSocket(hSocket))
1101 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
1102 CloseSocket(hSocket);
1103 return;
1106 // According to the internet TCP_NODELAY is not carried into accepted sockets
1107 // on all platforms. Set it again here just to be sure.
1108 SetSocketNoDelay(hSocket);
1110 if (IsBanned(addr) && !whitelisted)
1112 LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString());
1113 CloseSocket(hSocket);
1114 return;
1117 if (nInbound >= nMaxInbound)
1119 if (!AttemptToEvictConnection()) {
1120 // No connection to evict, disconnect the new connection
1121 LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1122 CloseSocket(hSocket);
1123 return;
1127 NodeId id = GetNewNodeId();
1128 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1129 CAddress addr_bind = GetBindAddress(hSocket);
1131 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
1132 pnode->AddRef();
1133 pnode->fWhitelisted = whitelisted;
1134 m_msgproc->InitializeNode(pnode);
1136 LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1139 LOCK(cs_vNodes);
1140 vNodes.push_back(pnode);
1144 void CConnman::ThreadSocketHandler()
1146 unsigned int nPrevNodeCount = 0;
1147 while (!interruptNet)
1150 // Disconnect nodes
1153 LOCK(cs_vNodes);
1154 // Disconnect unused nodes
1155 std::vector<CNode*> vNodesCopy = vNodes;
1156 for (CNode* pnode : vNodesCopy)
1158 if (pnode->fDisconnect)
1160 // remove from vNodes
1161 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1163 // release outbound grant (if any)
1164 pnode->grantOutbound.Release();
1166 // close socket and cleanup
1167 pnode->CloseSocketDisconnect();
1169 // hold in disconnected pool until all refs are released
1170 pnode->Release();
1171 vNodesDisconnected.push_back(pnode);
1176 // Delete disconnected nodes
1177 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1178 for (CNode* pnode : vNodesDisconnectedCopy)
1180 // wait until threads are done using it
1181 if (pnode->GetRefCount() <= 0) {
1182 bool fDelete = false;
1184 TRY_LOCK(pnode->cs_inventory, lockInv);
1185 if (lockInv) {
1186 TRY_LOCK(pnode->cs_vSend, lockSend);
1187 if (lockSend) {
1188 fDelete = true;
1192 if (fDelete) {
1193 vNodesDisconnected.remove(pnode);
1194 DeleteNode(pnode);
1199 size_t vNodesSize;
1201 LOCK(cs_vNodes);
1202 vNodesSize = vNodes.size();
1204 if(vNodesSize != nPrevNodeCount) {
1205 nPrevNodeCount = vNodesSize;
1206 if(clientInterface)
1207 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1211 // Find which sockets have data to receive
1213 struct timeval timeout;
1214 timeout.tv_sec = 0;
1215 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1217 fd_set fdsetRecv;
1218 fd_set fdsetSend;
1219 fd_set fdsetError;
1220 FD_ZERO(&fdsetRecv);
1221 FD_ZERO(&fdsetSend);
1222 FD_ZERO(&fdsetError);
1223 SOCKET hSocketMax = 0;
1224 bool have_fds = false;
1226 for (const ListenSocket& hListenSocket : vhListenSocket) {
1227 FD_SET(hListenSocket.socket, &fdsetRecv);
1228 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1229 have_fds = true;
1233 LOCK(cs_vNodes);
1234 for (CNode* pnode : vNodes)
1236 // Implement the following logic:
1237 // * If there is data to send, select() for sending data. As this only
1238 // happens when optimistic write failed, we choose to first drain the
1239 // write buffer in this case before receiving more. This avoids
1240 // needlessly queueing received data, if the remote peer is not themselves
1241 // receiving data. This means properly utilizing TCP flow control signalling.
1242 // * Otherwise, if there is space left in the receive buffer, select() for
1243 // receiving data.
1244 // * Hand off all complete messages to the processor, to be handled without
1245 // blocking here.
1247 bool select_recv = !pnode->fPauseRecv;
1248 bool select_send;
1250 LOCK(pnode->cs_vSend);
1251 select_send = !pnode->vSendMsg.empty();
1254 LOCK(pnode->cs_hSocket);
1255 if (pnode->hSocket == INVALID_SOCKET)
1256 continue;
1258 FD_SET(pnode->hSocket, &fdsetError);
1259 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1260 have_fds = true;
1262 if (select_send) {
1263 FD_SET(pnode->hSocket, &fdsetSend);
1264 continue;
1266 if (select_recv) {
1267 FD_SET(pnode->hSocket, &fdsetRecv);
1272 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1273 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1274 if (interruptNet)
1275 return;
1277 if (nSelect == SOCKET_ERROR)
1279 if (have_fds)
1281 int nErr = WSAGetLastError();
1282 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1283 for (unsigned int i = 0; i <= hSocketMax; i++)
1284 FD_SET(i, &fdsetRecv);
1286 FD_ZERO(&fdsetSend);
1287 FD_ZERO(&fdsetError);
1288 if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
1289 return;
1293 // Accept new connections
1295 for (const ListenSocket& hListenSocket : vhListenSocket)
1297 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1299 AcceptConnection(hListenSocket);
1304 // Service each socket
1306 std::vector<CNode*> vNodesCopy;
1308 LOCK(cs_vNodes);
1309 vNodesCopy = vNodes;
1310 for (CNode* pnode : vNodesCopy)
1311 pnode->AddRef();
1313 for (CNode* pnode : vNodesCopy)
1315 if (interruptNet)
1316 return;
1319 // Receive
1321 bool recvSet = false;
1322 bool sendSet = false;
1323 bool errorSet = false;
1325 LOCK(pnode->cs_hSocket);
1326 if (pnode->hSocket == INVALID_SOCKET)
1327 continue;
1328 recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
1329 sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
1330 errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
1332 if (recvSet || errorSet)
1334 // typical socket buffer is 8K-64K
1335 char pchBuf[0x10000];
1336 int nBytes = 0;
1338 LOCK(pnode->cs_hSocket);
1339 if (pnode->hSocket == INVALID_SOCKET)
1340 continue;
1341 nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1343 if (nBytes > 0)
1345 bool notify = false;
1346 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1347 pnode->CloseSocketDisconnect();
1348 RecordBytesRecv(nBytes);
1349 if (notify) {
1350 size_t nSizeAdded = 0;
1351 auto it(pnode->vRecvMsg.begin());
1352 for (; it != pnode->vRecvMsg.end(); ++it) {
1353 if (!it->complete())
1354 break;
1355 nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1358 LOCK(pnode->cs_vProcessMsg);
1359 pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1360 pnode->nProcessQueueSize += nSizeAdded;
1361 pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1363 WakeMessageHandler();
1366 else if (nBytes == 0)
1368 // socket closed gracefully
1369 if (!pnode->fDisconnect) {
1370 LogPrint(BCLog::NET, "socket closed\n");
1372 pnode->CloseSocketDisconnect();
1374 else if (nBytes < 0)
1376 // error
1377 int nErr = WSAGetLastError();
1378 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1380 if (!pnode->fDisconnect)
1381 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1382 pnode->CloseSocketDisconnect();
1388 // Send
1390 if (sendSet)
1392 LOCK(pnode->cs_vSend);
1393 size_t nBytes = SocketSendData(pnode);
1394 if (nBytes) {
1395 RecordBytesSent(nBytes);
1400 // Inactivity checking
1402 int64_t nTime = GetSystemTimeInSeconds();
1403 if (nTime - pnode->nTimeConnected > 60)
1405 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1407 LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1408 pnode->fDisconnect = true;
1410 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1412 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1413 pnode->fDisconnect = true;
1415 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1417 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1418 pnode->fDisconnect = true;
1420 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1422 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1423 pnode->fDisconnect = true;
1425 else if (!pnode->fSuccessfullyConnected)
1427 LogPrintf("version handshake timeout from %d\n", pnode->GetId());
1428 pnode->fDisconnect = true;
1433 LOCK(cs_vNodes);
1434 for (CNode* pnode : vNodesCopy)
1435 pnode->Release();
1440 void CConnman::WakeMessageHandler()
1443 std::lock_guard<std::mutex> lock(mutexMsgProc);
1444 fMsgProcWake = true;
1446 condMsgProc.notify_one();
1454 #ifdef USE_UPNP
1455 void ThreadMapPort()
1457 std::string port = strprintf("%u", GetListenPort());
1458 const char * multicastif = nullptr;
1459 const char * minissdpdpath = nullptr;
1460 struct UPNPDev * devlist = nullptr;
1461 char lanaddr[64];
1463 #ifndef UPNPDISCOVER_SUCCESS
1464 /* miniupnpc 1.5 */
1465 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1466 #elif MINIUPNPC_API_VERSION < 14
1467 /* miniupnpc 1.6 */
1468 int error = 0;
1469 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1470 #else
1471 /* miniupnpc 1.9.20150730 */
1472 int error = 0;
1473 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1474 #endif
1476 struct UPNPUrls urls;
1477 struct IGDdatas data;
1478 int r;
1480 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1481 if (r == 1)
1483 if (fDiscover) {
1484 char externalIPAddress[40];
1485 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1486 if(r != UPNPCOMMAND_SUCCESS)
1487 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1488 else
1490 if(externalIPAddress[0])
1492 CNetAddr resolved;
1493 if(LookupHost(externalIPAddress, resolved, false)) {
1494 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1495 AddLocal(resolved, LOCAL_UPNP);
1498 else
1499 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1503 std::string strDesc = "Bitcoin " + FormatFullVersion();
1505 try {
1506 while (true) {
1507 #ifndef UPNPDISCOVER_SUCCESS
1508 /* miniupnpc 1.5 */
1509 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1510 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1511 #else
1512 /* miniupnpc 1.6 */
1513 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1514 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1515 #endif
1517 if(r!=UPNPCOMMAND_SUCCESS)
1518 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1519 port, port, lanaddr, r, strupnperror(r));
1520 else
1521 LogPrintf("UPnP Port Mapping successful.\n");
1523 MilliSleep(20*60*1000); // Refresh every 20 minutes
1526 catch (const boost::thread_interrupted&)
1528 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1529 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1530 freeUPNPDevlist(devlist); devlist = nullptr;
1531 FreeUPNPUrls(&urls);
1532 throw;
1534 } else {
1535 LogPrintf("No valid UPnP IGDs found\n");
1536 freeUPNPDevlist(devlist); devlist = nullptr;
1537 if (r != 0)
1538 FreeUPNPUrls(&urls);
1542 void MapPort(bool fUseUPnP)
1544 static std::unique_ptr<boost::thread> upnp_thread;
1546 if (fUseUPnP)
1548 if (upnp_thread) {
1549 upnp_thread->interrupt();
1550 upnp_thread->join();
1552 upnp_thread.reset(new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
1554 else if (upnp_thread) {
1555 upnp_thread->interrupt();
1556 upnp_thread->join();
1557 upnp_thread.reset();
1561 #else
1562 void MapPort(bool)
1564 // Intentionally left blank.
1566 #endif
1573 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1575 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1576 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1577 *requiredServiceBits = NODE_NETWORK;
1578 return data.host;
1581 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1582 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1586 void CConnman::ThreadDNSAddressSeed()
1588 // goal: only query DNS seeds if address need is acute
1589 // Avoiding DNS seeds when we don't need them improves user privacy by
1590 // creating fewer identifying DNS requests, reduces trust by giving seeds
1591 // less influence on the network topology, and reduces traffic to the seeds.
1592 if ((addrman.size() > 0) &&
1593 (!gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1594 if (!interruptNet.sleep_for(std::chrono::seconds(11)))
1595 return;
1597 LOCK(cs_vNodes);
1598 int nRelevant = 0;
1599 for (auto pnode : vNodes) {
1600 nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound;
1602 if (nRelevant >= 2) {
1603 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1604 return;
1608 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1609 int found = 0;
1611 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1613 for (const CDNSSeedData &seed : vSeeds) {
1614 if (interruptNet) {
1615 return;
1617 if (HaveNameProxy()) {
1618 AddOneShot(seed.host);
1619 } else {
1620 std::vector<CNetAddr> vIPs;
1621 std::vector<CAddress> vAdd;
1622 ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE);
1623 std::string host = GetDNSHost(seed, &requiredServiceBits);
1624 CNetAddr resolveSource;
1625 if (!resolveSource.SetInternal(host)) {
1626 continue;
1628 if (LookupHost(host.c_str(), vIPs, 0, true))
1630 for (const CNetAddr& ip : vIPs)
1632 int nOneDay = 24*3600;
1633 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
1634 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1635 vAdd.push_back(addr);
1636 found++;
1638 addrman.Add(vAdd, resolveSource);
1643 LogPrintf("%d addresses found from DNS seeds\n", found);
1657 void CConnman::DumpAddresses()
1659 int64_t nStart = GetTimeMillis();
1661 CAddrDB adb;
1662 adb.Write(addrman);
1664 LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1665 addrman.size(), GetTimeMillis() - nStart);
1668 void CConnman::DumpData()
1670 DumpAddresses();
1671 DumpBanlist();
1674 void CConnman::ProcessOneShot()
1676 std::string strDest;
1678 LOCK(cs_vOneShots);
1679 if (vOneShots.empty())
1680 return;
1681 strDest = vOneShots.front();
1682 vOneShots.pop_front();
1684 CAddress addr;
1685 CSemaphoreGrant grant(*semOutbound, true);
1686 if (grant) {
1687 if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
1688 AddOneShot(strDest);
1692 bool CConnman::GetTryNewOutboundPeer()
1694 return m_try_another_outbound_peer;
1697 void CConnman::SetTryNewOutboundPeer(bool flag)
1699 m_try_another_outbound_peer = flag;
1700 LogPrint(BCLog::NET, "net: setting try another outbound peer=%s\n", flag ? "true" : "false");
1703 // Return the number of peers we have over our outbound connection limit
1704 // Exclude peers that are marked for disconnect, or are going to be
1705 // disconnected soon (eg one-shots and feelers)
1706 // Also exclude peers that haven't finished initial connection handshake yet
1707 // (so that we don't decide we're over our desired connection limit, and then
1708 // evict some peer that has finished the handshake)
1709 int CConnman::GetExtraOutboundCount()
1711 int nOutbound = 0;
1713 LOCK(cs_vNodes);
1714 for (CNode* pnode : vNodes) {
1715 if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) {
1716 ++nOutbound;
1720 return std::max(nOutbound - nMaxOutbound, 0);
1723 void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1725 // Connect to specific addresses
1726 if (!connect.empty())
1728 for (int64_t nLoop = 0;; nLoop++)
1730 ProcessOneShot();
1731 for (const std::string& strAddr : connect)
1733 CAddress addr(CService(), NODE_NONE);
1734 OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(), false, false, true);
1735 for (int i = 0; i < 10 && i < nLoop; i++)
1737 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1738 return;
1741 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1742 return;
1746 // Initiate network connections
1747 int64_t nStart = GetTime();
1749 // Minimum time before next feeler connection (in microseconds).
1750 int64_t nNextFeeler = PoissonNextSend(nStart*1000*1000, FEELER_INTERVAL);
1751 while (!interruptNet)
1753 ProcessOneShot();
1755 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1756 return;
1758 CSemaphoreGrant grant(*semOutbound);
1759 if (interruptNet)
1760 return;
1762 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1763 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1764 static bool done = false;
1765 if (!done) {
1766 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1767 CNetAddr local;
1768 local.SetInternal("fixedseeds");
1769 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1770 done = true;
1775 // Choose an address to connect to based on most recently seen
1777 CAddress addrConnect;
1779 // Only connect out to one peer per network group (/16 for IPv4).
1780 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1781 int nOutbound = 0;
1782 std::set<std::vector<unsigned char> > setConnected;
1784 LOCK(cs_vNodes);
1785 for (CNode* pnode : vNodes) {
1786 if (!pnode->fInbound && !pnode->m_manual_connection) {
1787 // Netgroups for inbound and addnode peers are not excluded because our goal here
1788 // is to not use multiple of our limited outbound slots on a single netgroup
1789 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1790 // also have the added issue that they're attacker controlled and could be used
1791 // to prevent us from connecting to particular hosts if we used them here.
1792 setConnected.insert(pnode->addr.GetGroup());
1793 nOutbound++;
1798 // Feeler Connections
1800 // Design goals:
1801 // * Increase the number of connectable addresses in the tried table.
1803 // Method:
1804 // * Choose a random address from new and attempt to connect to it if we can connect
1805 // successfully it is added to tried.
1806 // * Start attempting feeler connections only after node finishes making outbound
1807 // connections.
1808 // * Only make a feeler connection once every few minutes.
1810 bool fFeeler = false;
1812 if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer()) {
1813 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1814 if (nTime > nNextFeeler) {
1815 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1816 fFeeler = true;
1817 } else {
1818 continue;
1822 int64_t nANow = GetAdjustedTime();
1823 int nTries = 0;
1824 while (!interruptNet)
1826 CAddrInfo addr = addrman.Select(fFeeler);
1828 // if we selected an invalid address, restart
1829 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1830 break;
1832 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1833 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1834 // already-connected network ranges, ...) before trying new addrman addresses.
1835 nTries++;
1836 if (nTries > 100)
1837 break;
1839 if (IsLimited(addr))
1840 continue;
1842 // only consider very recently tried nodes after 30 failed attempts
1843 if (nANow - addr.nLastTry < 600 && nTries < 30)
1844 continue;
1846 // for non-feelers, require all the services we'll want,
1847 // for feelers, only require they be a full node (only because most
1848 // SPV clients don't have a good address DB available)
1849 if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
1850 continue;
1851 } else if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
1852 continue;
1855 // do not allow non-default ports, unless after 50 invalid addresses selected already
1856 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1857 continue;
1859 addrConnect = addr;
1860 break;
1863 if (addrConnect.IsValid()) {
1865 if (fFeeler) {
1866 // Add small amount of random noise before connection to avoid synchronization.
1867 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1868 if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1869 return;
1870 LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
1873 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler);
1878 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1880 std::vector<AddedNodeInfo> ret;
1882 std::list<std::string> lAddresses(0);
1884 LOCK(cs_vAddedNodes);
1885 ret.reserve(vAddedNodes.size());
1886 std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses));
1890 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1891 std::map<CService, bool> mapConnected;
1892 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
1894 LOCK(cs_vNodes);
1895 for (const CNode* pnode : vNodes) {
1896 if (pnode->addr.IsValid()) {
1897 mapConnected[pnode->addr] = pnode->fInbound;
1899 std::string addrName = pnode->GetAddrName();
1900 if (!addrName.empty()) {
1901 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1906 for (const std::string& strAddNode : lAddresses) {
1907 CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1908 if (service.IsValid()) {
1909 // strAddNode is an IP:port
1910 auto it = mapConnected.find(service);
1911 if (it != mapConnected.end()) {
1912 ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1913 } else {
1914 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1916 } else {
1917 // strAddNode is a name
1918 auto it = mapConnectedByName.find(strAddNode);
1919 if (it != mapConnectedByName.end()) {
1920 ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1921 } else {
1922 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1927 return ret;
1930 void CConnman::ThreadOpenAddedConnections()
1932 while (true)
1934 CSemaphoreGrant grant(*semAddnode);
1935 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1936 bool tried = false;
1937 for (const AddedNodeInfo& info : vInfo) {
1938 if (!info.fConnected) {
1939 if (!grant.TryAcquire()) {
1940 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1941 // the addednodeinfo state might change.
1942 break;
1944 tried = true;
1945 CAddress addr(CService(), NODE_NONE);
1946 OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), false, false, true);
1947 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1948 return;
1951 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1952 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
1953 return;
1957 // if successful, this moves the passed grant to the constructed node
1958 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool manual_connection)
1961 // Initiate outbound network connection
1963 if (interruptNet) {
1964 return false;
1966 if (!fNetworkActive) {
1967 return false;
1969 if (!pszDest) {
1970 if (IsLocal(addrConnect) ||
1971 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1972 FindNode(addrConnect.ToStringIPPort()))
1973 return false;
1974 } else if (FindNode(std::string(pszDest)))
1975 return false;
1977 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1979 if (!pnode)
1980 return false;
1981 if (grantOutbound)
1982 grantOutbound->MoveTo(pnode->grantOutbound);
1983 if (fOneShot)
1984 pnode->fOneShot = true;
1985 if (fFeeler)
1986 pnode->fFeeler = true;
1987 if (manual_connection)
1988 pnode->m_manual_connection = true;
1990 m_msgproc->InitializeNode(pnode);
1992 LOCK(cs_vNodes);
1993 vNodes.push_back(pnode);
1996 return true;
1999 void CConnman::ThreadMessageHandler()
2001 while (!flagInterruptMsgProc)
2003 std::vector<CNode*> vNodesCopy;
2005 LOCK(cs_vNodes);
2006 vNodesCopy = vNodes;
2007 for (CNode* pnode : vNodesCopy) {
2008 pnode->AddRef();
2012 bool fMoreWork = false;
2014 for (CNode* pnode : vNodesCopy)
2016 if (pnode->fDisconnect)
2017 continue;
2019 // Receive messages
2020 bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
2021 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
2022 if (flagInterruptMsgProc)
2023 return;
2024 // Send messages
2026 LOCK(pnode->cs_sendProcessing);
2027 m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
2030 if (flagInterruptMsgProc)
2031 return;
2035 LOCK(cs_vNodes);
2036 for (CNode* pnode : vNodesCopy)
2037 pnode->Release();
2040 std::unique_lock<std::mutex> lock(mutexMsgProc);
2041 if (!fMoreWork) {
2042 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
2044 fMsgProcWake = false;
2053 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
2055 strError = "";
2056 int nOne = 1;
2058 // Create socket for listening for incoming connections
2059 struct sockaddr_storage sockaddr;
2060 socklen_t len = sizeof(sockaddr);
2061 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
2063 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
2064 LogPrintf("%s\n", strError);
2065 return false;
2068 SOCKET hListenSocket = CreateSocket(addrBind);
2069 if (hListenSocket == INVALID_SOCKET)
2071 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2072 LogPrintf("%s\n", strError);
2073 return false;
2075 #ifndef WIN32
2076 // Allow binding if the port is still in TIME_WAIT state after
2077 // the program was closed and restarted.
2078 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2079 #else
2080 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2081 #endif
2083 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2084 // and enable it by default or not. Try to enable it, if possible.
2085 if (addrBind.IsIPv6()) {
2086 #ifdef IPV6_V6ONLY
2087 #ifdef WIN32
2088 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2089 #else
2090 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2091 #endif
2092 #endif
2093 #ifdef WIN32
2094 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2095 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2096 #endif
2099 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2101 int nErr = WSAGetLastError();
2102 if (nErr == WSAEADDRINUSE)
2103 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
2104 else
2105 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2106 LogPrintf("%s\n", strError);
2107 CloseSocket(hListenSocket);
2108 return false;
2110 LogPrintf("Bound to %s\n", addrBind.ToString());
2112 // Listen for incoming connections
2113 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
2115 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2116 LogPrintf("%s\n", strError);
2117 CloseSocket(hListenSocket);
2118 return false;
2121 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
2123 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
2124 AddLocal(addrBind, LOCAL_BIND);
2126 return true;
2129 void Discover(boost::thread_group& threadGroup)
2131 if (!fDiscover)
2132 return;
2134 #ifdef WIN32
2135 // Get local host IP
2136 char pszHostName[256] = "";
2137 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
2139 std::vector<CNetAddr> vaddr;
2140 if (LookupHost(pszHostName, vaddr, 0, true))
2142 for (const CNetAddr &addr : vaddr)
2144 if (AddLocal(addr, LOCAL_IF))
2145 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
2149 #else
2150 // Get local host ip
2151 struct ifaddrs* myaddrs;
2152 if (getifaddrs(&myaddrs) == 0)
2154 for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
2156 if (ifa->ifa_addr == nullptr) continue;
2157 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2158 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2159 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2160 if (ifa->ifa_addr->sa_family == AF_INET)
2162 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2163 CNetAddr addr(s4->sin_addr);
2164 if (AddLocal(addr, LOCAL_IF))
2165 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2167 else if (ifa->ifa_addr->sa_family == AF_INET6)
2169 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2170 CNetAddr addr(s6->sin6_addr);
2171 if (AddLocal(addr, LOCAL_IF))
2172 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2175 freeifaddrs(myaddrs);
2177 #endif
2180 void CConnman::SetNetworkActive(bool active)
2182 LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2184 if (fNetworkActive == active) {
2185 return;
2188 fNetworkActive = active;
2190 if (!fNetworkActive) {
2191 LOCK(cs_vNodes);
2192 // Close sockets to all nodes
2193 for (CNode* pnode : vNodes) {
2194 pnode->CloseSocketDisconnect();
2198 uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2201 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2203 fNetworkActive = true;
2204 setBannedIsDirty = false;
2205 fAddressesInitialized = false;
2206 nLastNodeId = 0;
2207 nSendBufferMaxSize = 0;
2208 nReceiveFloodSize = 0;
2209 flagInterruptMsgProc = false;
2210 SetTryNewOutboundPeer(false);
2212 Options connOptions;
2213 Init(connOptions);
2216 NodeId CConnman::GetNewNodeId()
2218 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2222 bool CConnman::Bind(const CService &addr, unsigned int flags) {
2223 if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2224 return false;
2225 std::string strError;
2226 if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
2227 if ((flags & BF_REPORT_ERROR) && clientInterface) {
2228 clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
2230 return false;
2232 return true;
2235 bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2236 bool fBound = false;
2237 for (const auto& addrBind : binds) {
2238 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2240 for (const auto& addrBind : whiteBinds) {
2241 fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
2243 if (binds.empty() && whiteBinds.empty()) {
2244 struct in_addr inaddr_any;
2245 inaddr_any.s_addr = INADDR_ANY;
2246 fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
2247 fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2249 return fBound;
2252 bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2254 Init(connOptions);
2257 LOCK(cs_totalBytesRecv);
2258 nTotalBytesRecv = 0;
2261 LOCK(cs_totalBytesSent);
2262 nTotalBytesSent = 0;
2263 nMaxOutboundTotalBytesSentInCycle = 0;
2264 nMaxOutboundCycleStartTime = 0;
2267 if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2268 if (clientInterface) {
2269 clientInterface->ThreadSafeMessageBox(
2270 _("Failed to listen on any port. Use -listen=0 if you want this."),
2271 "", CClientUIInterface::MSG_ERROR);
2273 return false;
2276 for (const auto& strDest : connOptions.vSeedNodes) {
2277 AddOneShot(strDest);
2280 if (clientInterface) {
2281 clientInterface->InitMessage(_("Loading P2P addresses..."));
2283 // Load addresses from peers.dat
2284 int64_t nStart = GetTimeMillis();
2286 CAddrDB adb;
2287 if (adb.Read(addrman))
2288 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2289 else {
2290 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2291 LogPrintf("Invalid or missing peers.dat; recreating\n");
2292 DumpAddresses();
2295 if (clientInterface)
2296 clientInterface->InitMessage(_("Loading banlist..."));
2297 // Load addresses from banlist.dat
2298 nStart = GetTimeMillis();
2299 CBanDB bandb;
2300 banmap_t banmap;
2301 if (bandb.Read(banmap)) {
2302 SetBanned(banmap); // thread save setter
2303 SetBannedSetDirty(false); // no need to write down, just read data
2304 SweepBanned(); // sweep out unused entries
2306 LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2307 banmap.size(), GetTimeMillis() - nStart);
2308 } else {
2309 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2310 SetBannedSetDirty(true); // force write
2311 DumpBanlist();
2314 uiInterface.InitMessage(_("Starting network threads..."));
2316 fAddressesInitialized = true;
2318 if (semOutbound == nullptr) {
2319 // initialize semaphore
2320 semOutbound = MakeUnique<CSemaphore>(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
2322 if (semAddnode == nullptr) {
2323 // initialize semaphore
2324 semAddnode = MakeUnique<CSemaphore>(nMaxAddnode);
2328 // Start threads
2330 assert(m_msgproc);
2331 InterruptSocks5(false);
2332 interruptNet.reset();
2333 flagInterruptMsgProc = false;
2336 std::unique_lock<std::mutex> lock(mutexMsgProc);
2337 fMsgProcWake = false;
2340 // Send and receive from sockets, accept connections
2341 threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2343 if (!gArgs.GetBoolArg("-dnsseed", true))
2344 LogPrintf("DNS seeding disabled\n");
2345 else
2346 threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2348 // Initiate outbound connections from -addnode
2349 threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2351 if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
2352 if (clientInterface) {
2353 clientInterface->ThreadSafeMessageBox(
2354 _("Cannot provide specific connections and have addrman find outgoing connections at the same."),
2355 "", CClientUIInterface::MSG_ERROR);
2357 return false;
2359 if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
2360 threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
2362 // Process messages
2363 threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2365 // Dump network addresses
2366 scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
2368 return true;
2371 class CNetCleanup
2373 public:
2374 CNetCleanup() {}
2376 ~CNetCleanup()
2378 #ifdef WIN32
2379 // Shutdown Windows Sockets
2380 WSACleanup();
2381 #endif
2384 instance_of_cnetcleanup;
2386 void CConnman::Interrupt()
2389 std::lock_guard<std::mutex> lock(mutexMsgProc);
2390 flagInterruptMsgProc = true;
2392 condMsgProc.notify_all();
2394 interruptNet();
2395 InterruptSocks5(true);
2397 if (semOutbound) {
2398 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++) {
2399 semOutbound->post();
2403 if (semAddnode) {
2404 for (int i=0; i<nMaxAddnode; i++) {
2405 semAddnode->post();
2410 void CConnman::Stop()
2412 if (threadMessageHandler.joinable())
2413 threadMessageHandler.join();
2414 if (threadOpenConnections.joinable())
2415 threadOpenConnections.join();
2416 if (threadOpenAddedConnections.joinable())
2417 threadOpenAddedConnections.join();
2418 if (threadDNSAddressSeed.joinable())
2419 threadDNSAddressSeed.join();
2420 if (threadSocketHandler.joinable())
2421 threadSocketHandler.join();
2423 if (fAddressesInitialized)
2425 DumpData();
2426 fAddressesInitialized = false;
2429 // Close sockets
2430 for (CNode* pnode : vNodes)
2431 pnode->CloseSocketDisconnect();
2432 for (ListenSocket& hListenSocket : vhListenSocket)
2433 if (hListenSocket.socket != INVALID_SOCKET)
2434 if (!CloseSocket(hListenSocket.socket))
2435 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2437 // clean up some globals (to help leak detection)
2438 for (CNode *pnode : vNodes) {
2439 DeleteNode(pnode);
2441 for (CNode *pnode : vNodesDisconnected) {
2442 DeleteNode(pnode);
2444 vNodes.clear();
2445 vNodesDisconnected.clear();
2446 vhListenSocket.clear();
2447 semOutbound.reset();
2448 semAddnode.reset();
2451 void CConnman::DeleteNode(CNode* pnode)
2453 assert(pnode);
2454 bool fUpdateConnectionTime = false;
2455 m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2456 if(fUpdateConnectionTime) {
2457 addrman.Connected(pnode->addr);
2459 delete pnode;
2462 CConnman::~CConnman()
2464 Interrupt();
2465 Stop();
2468 size_t CConnman::GetAddressCount() const
2470 return addrman.size();
2473 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2475 addrman.SetServices(addr, nServices);
2478 void CConnman::MarkAddressGood(const CAddress& addr)
2480 addrman.Good(addr);
2483 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2485 addrman.Add(vAddr, addrFrom, nTimePenalty);
2488 std::vector<CAddress> CConnman::GetAddresses()
2490 return addrman.GetAddr();
2493 bool CConnman::AddNode(const std::string& strNode)
2495 LOCK(cs_vAddedNodes);
2496 for (const std::string& it : vAddedNodes) {
2497 if (strNode == it) return false;
2500 vAddedNodes.push_back(strNode);
2501 return true;
2504 bool CConnman::RemoveAddedNode(const std::string& strNode)
2506 LOCK(cs_vAddedNodes);
2507 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2508 if (strNode == *it) {
2509 vAddedNodes.erase(it);
2510 return true;
2513 return false;
2516 size_t CConnman::GetNodeCount(NumConnections flags)
2518 LOCK(cs_vNodes);
2519 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2520 return vNodes.size();
2522 int nNum = 0;
2523 for (const auto& pnode : vNodes) {
2524 if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
2525 nNum++;
2529 return nNum;
2532 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2534 vstats.clear();
2535 LOCK(cs_vNodes);
2536 vstats.reserve(vNodes.size());
2537 for (CNode* pnode : vNodes) {
2538 vstats.emplace_back();
2539 pnode->copyStats(vstats.back());
2543 bool CConnman::DisconnectNode(const std::string& strNode)
2545 LOCK(cs_vNodes);
2546 if (CNode* pnode = FindNode(strNode)) {
2547 pnode->fDisconnect = true;
2548 return true;
2550 return false;
2552 bool CConnman::DisconnectNode(NodeId id)
2554 LOCK(cs_vNodes);
2555 for(CNode* pnode : vNodes) {
2556 if (id == pnode->GetId()) {
2557 pnode->fDisconnect = true;
2558 return true;
2561 return false;
2564 void CConnman::RecordBytesRecv(uint64_t bytes)
2566 LOCK(cs_totalBytesRecv);
2567 nTotalBytesRecv += bytes;
2570 void CConnman::RecordBytesSent(uint64_t bytes)
2572 LOCK(cs_totalBytesSent);
2573 nTotalBytesSent += bytes;
2575 uint64_t now = GetTime();
2576 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2578 // timeframe expired, reset cycle
2579 nMaxOutboundCycleStartTime = now;
2580 nMaxOutboundTotalBytesSentInCycle = 0;
2583 // TODO, exclude whitebind peers
2584 nMaxOutboundTotalBytesSentInCycle += bytes;
2587 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2589 LOCK(cs_totalBytesSent);
2590 nMaxOutboundLimit = limit;
2593 uint64_t CConnman::GetMaxOutboundTarget()
2595 LOCK(cs_totalBytesSent);
2596 return nMaxOutboundLimit;
2599 uint64_t CConnman::GetMaxOutboundTimeframe()
2601 LOCK(cs_totalBytesSent);
2602 return nMaxOutboundTimeframe;
2605 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2607 LOCK(cs_totalBytesSent);
2608 if (nMaxOutboundLimit == 0)
2609 return 0;
2611 if (nMaxOutboundCycleStartTime == 0)
2612 return nMaxOutboundTimeframe;
2614 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2615 uint64_t now = GetTime();
2616 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2619 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2621 LOCK(cs_totalBytesSent);
2622 if (nMaxOutboundTimeframe != timeframe)
2624 // reset measure-cycle in case of changing
2625 // the timeframe
2626 nMaxOutboundCycleStartTime = GetTime();
2628 nMaxOutboundTimeframe = timeframe;
2631 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2633 LOCK(cs_totalBytesSent);
2634 if (nMaxOutboundLimit == 0)
2635 return false;
2637 if (historicalBlockServingLimit)
2639 // keep a large enough buffer to at least relay each block once
2640 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2641 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2642 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2643 return true;
2645 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2646 return true;
2648 return false;
2651 uint64_t CConnman::GetOutboundTargetBytesLeft()
2653 LOCK(cs_totalBytesSent);
2654 if (nMaxOutboundLimit == 0)
2655 return 0;
2657 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2660 uint64_t CConnman::GetTotalBytesRecv()
2662 LOCK(cs_totalBytesRecv);
2663 return nTotalBytesRecv;
2666 uint64_t CConnman::GetTotalBytesSent()
2668 LOCK(cs_totalBytesSent);
2669 return nTotalBytesSent;
2672 ServiceFlags CConnman::GetLocalServices() const
2674 return nLocalServices;
2677 void CConnman::SetBestHeight(int height)
2679 nBestHeight.store(height, std::memory_order_release);
2682 int CConnman::GetBestHeight() const
2684 return nBestHeight.load(std::memory_order_acquire);
2687 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2689 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) :
2690 nTimeConnected(GetSystemTimeInSeconds()),
2691 addr(addrIn),
2692 addrBind(addrBindIn),
2693 fInbound(fInboundIn),
2694 nKeyedNetGroup(nKeyedNetGroupIn),
2695 addrKnown(5000, 0.001),
2696 filterInventoryKnown(50000, 0.000001),
2697 id(idIn),
2698 nLocalHostNonce(nLocalHostNonceIn),
2699 nLocalServices(nLocalServicesIn),
2700 nMyStartingHeight(nMyStartingHeightIn),
2701 nSendVersion(0)
2703 nServices = NODE_NONE;
2704 hSocket = hSocketIn;
2705 nRecvVersion = INIT_PROTO_VERSION;
2706 nLastSend = 0;
2707 nLastRecv = 0;
2708 nSendBytes = 0;
2709 nRecvBytes = 0;
2710 nTimeOffset = 0;
2711 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2712 nVersion = 0;
2713 strSubVer = "";
2714 fWhitelisted = false;
2715 fOneShot = false;
2716 m_manual_connection = false;
2717 fClient = false; // set by version message
2718 fFeeler = false;
2719 fSuccessfullyConnected = false;
2720 fDisconnect = false;
2721 nRefCount = 0;
2722 nSendSize = 0;
2723 nSendOffset = 0;
2724 hashContinue = uint256();
2725 nStartingHeight = -1;
2726 filterInventoryKnown.reset();
2727 fSendMempool = false;
2728 fGetAddr = false;
2729 nNextLocalAddrSend = 0;
2730 nNextAddrSend = 0;
2731 nNextInvSend = 0;
2732 fRelayTxes = false;
2733 fSentAddr = false;
2734 pfilter = MakeUnique<CBloomFilter>();
2735 timeLastMempoolReq = 0;
2736 nLastBlockTime = 0;
2737 nLastTXTime = 0;
2738 nPingNonceSent = 0;
2739 nPingUsecStart = 0;
2740 nPingUsecTime = 0;
2741 fPingQueued = false;
2742 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2743 minFeeFilter = 0;
2744 lastSentFeeFilter = 0;
2745 nextSendTimeFeeFilter = 0;
2746 fPauseRecv = false;
2747 fPauseSend = false;
2748 nProcessQueueSize = 0;
2750 for (const std::string &msg : getAllNetMessageTypes())
2751 mapRecvBytesPerMsgCmd[msg] = 0;
2752 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2754 if (fLogIPs) {
2755 LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
2756 } else {
2757 LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
2761 CNode::~CNode()
2763 CloseSocket(hSocket);
2766 void CNode::AskFor(const CInv& inv)
2768 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2769 return;
2770 // a peer may not have multiple non-responded queue positions for a single inv item
2771 if (!setAskFor.insert(inv.hash).second)
2772 return;
2774 // We're using mapAskFor as a priority queue,
2775 // the key is the earliest time the request can be sent
2776 int64_t nRequestTime;
2777 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2778 if (it != mapAlreadyAskedFor.end())
2779 nRequestTime = it->second;
2780 else
2781 nRequestTime = 0;
2782 LogPrint(BCLog::NET, "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2784 // Make sure not to reuse time indexes to keep things in the same order
2785 int64_t nNow = GetTimeMicros() - 1000000;
2786 static int64_t nLastTime;
2787 ++nLastTime;
2788 nNow = std::max(nNow, nLastTime);
2789 nLastTime = nNow;
2791 // Each retry is 2 minutes after the last
2792 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2793 if (it != mapAlreadyAskedFor.end())
2794 mapAlreadyAskedFor.update(it, nRequestTime);
2795 else
2796 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2797 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2800 bool CConnman::NodeFullyConnected(const CNode* pnode)
2802 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
2805 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2807 size_t nMessageSize = msg.data.size();
2808 size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2809 LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2811 std::vector<unsigned char> serializedHeader;
2812 serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
2813 uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
2814 CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize);
2815 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
2817 CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
2819 size_t nBytesSent = 0;
2821 LOCK(pnode->cs_vSend);
2822 bool optimisticSend(pnode->vSendMsg.empty());
2824 //log total amount of bytes per command
2825 pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
2826 pnode->nSendSize += nTotalSize;
2828 if (pnode->nSendSize > nSendBufferMaxSize)
2829 pnode->fPauseSend = true;
2830 pnode->vSendMsg.push_back(std::move(serializedHeader));
2831 if (nMessageSize)
2832 pnode->vSendMsg.push_back(std::move(msg.data));
2834 // If write queue empty, attempt "optimistic write"
2835 if (optimisticSend == true)
2836 nBytesSent = SocketSendData(pnode);
2838 if (nBytesSent)
2839 RecordBytesSent(nBytesSent);
2842 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2844 CNode* found = nullptr;
2845 LOCK(cs_vNodes);
2846 for (auto&& pnode : vNodes) {
2847 if(pnode->GetId() == id) {
2848 found = pnode;
2849 break;
2852 return found != nullptr && NodeFullyConnected(found) && func(found);
2855 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2856 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2859 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
2861 return CSipHasher(nSeed0, nSeed1).Write(id);
2864 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
2866 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2868 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();