[tests] Add libFuzzer support.
[bitcoinplatinum.git] / src / net.cpp
blob198d8f5fff6ebc05eb3c3e9c899044fbaf0cc9d1
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()), nLocalServices);
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 SetSocketNoDelay(hSocket);
1076 if (IsBanned(addr) && !whitelisted)
1078 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
1079 CloseSocket(hSocket);
1080 return;
1083 if (nInbound >= nMaxInbound)
1085 if (!AttemptToEvictConnection()) {
1086 // No connection to evict, disconnect the new connection
1087 LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1088 CloseSocket(hSocket);
1089 return;
1093 NodeId id = GetNewNodeId();
1094 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1096 CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, "", true);
1097 pnode->AddRef();
1098 pnode->fWhitelisted = whitelisted;
1099 GetNodeSignals().InitializeNode(pnode, *this);
1101 LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1104 LOCK(cs_vNodes);
1105 vNodes.push_back(pnode);
1109 void CConnman::ThreadSocketHandler()
1111 unsigned int nPrevNodeCount = 0;
1112 while (!interruptNet)
1115 // Disconnect nodes
1118 LOCK(cs_vNodes);
1119 // Disconnect unused nodes
1120 std::vector<CNode*> vNodesCopy = vNodes;
1121 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1123 if (pnode->fDisconnect)
1125 // remove from vNodes
1126 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1128 // release outbound grant (if any)
1129 pnode->grantOutbound.Release();
1131 // close socket and cleanup
1132 pnode->CloseSocketDisconnect();
1134 // hold in disconnected pool until all refs are released
1135 pnode->Release();
1136 vNodesDisconnected.push_back(pnode);
1141 // Delete disconnected nodes
1142 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1143 BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
1145 // wait until threads are done using it
1146 if (pnode->GetRefCount() <= 0) {
1147 bool fDelete = false;
1149 TRY_LOCK(pnode->cs_inventory, lockInv);
1150 if (lockInv) {
1151 TRY_LOCK(pnode->cs_vSend, lockSend);
1152 if (lockSend) {
1153 fDelete = true;
1157 if (fDelete) {
1158 vNodesDisconnected.remove(pnode);
1159 DeleteNode(pnode);
1164 size_t vNodesSize;
1166 LOCK(cs_vNodes);
1167 vNodesSize = vNodes.size();
1169 if(vNodesSize != nPrevNodeCount) {
1170 nPrevNodeCount = vNodesSize;
1171 if(clientInterface)
1172 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1176 // Find which sockets have data to receive
1178 struct timeval timeout;
1179 timeout.tv_sec = 0;
1180 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1182 fd_set fdsetRecv;
1183 fd_set fdsetSend;
1184 fd_set fdsetError;
1185 FD_ZERO(&fdsetRecv);
1186 FD_ZERO(&fdsetSend);
1187 FD_ZERO(&fdsetError);
1188 SOCKET hSocketMax = 0;
1189 bool have_fds = false;
1191 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket) {
1192 FD_SET(hListenSocket.socket, &fdsetRecv);
1193 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1194 have_fds = true;
1198 LOCK(cs_vNodes);
1199 BOOST_FOREACH(CNode* pnode, vNodes)
1201 // Implement the following logic:
1202 // * If there is data to send, select() for sending data. As this only
1203 // happens when optimistic write failed, we choose to first drain the
1204 // write buffer in this case before receiving more. This avoids
1205 // needlessly queueing received data, if the remote peer is not themselves
1206 // receiving data. This means properly utilizing TCP flow control signalling.
1207 // * Otherwise, if there is space left in the receive buffer, select() for
1208 // receiving data.
1209 // * Hand off all complete messages to the processor, to be handled without
1210 // blocking here.
1212 bool select_recv = !pnode->fPauseRecv;
1213 bool select_send;
1215 LOCK(pnode->cs_vSend);
1216 select_send = !pnode->vSendMsg.empty();
1219 LOCK(pnode->cs_hSocket);
1220 if (pnode->hSocket == INVALID_SOCKET)
1221 continue;
1223 FD_SET(pnode->hSocket, &fdsetError);
1224 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1225 have_fds = true;
1227 if (select_send) {
1228 FD_SET(pnode->hSocket, &fdsetSend);
1229 continue;
1231 if (select_recv) {
1232 FD_SET(pnode->hSocket, &fdsetRecv);
1237 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1238 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1239 if (interruptNet)
1240 return;
1242 if (nSelect == SOCKET_ERROR)
1244 if (have_fds)
1246 int nErr = WSAGetLastError();
1247 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1248 for (unsigned int i = 0; i <= hSocketMax; i++)
1249 FD_SET(i, &fdsetRecv);
1251 FD_ZERO(&fdsetSend);
1252 FD_ZERO(&fdsetError);
1253 if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
1254 return;
1258 // Accept new connections
1260 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket)
1262 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1264 AcceptConnection(hListenSocket);
1269 // Service each socket
1271 std::vector<CNode*> vNodesCopy;
1273 LOCK(cs_vNodes);
1274 vNodesCopy = vNodes;
1275 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1276 pnode->AddRef();
1278 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1280 if (interruptNet)
1281 return;
1284 // Receive
1286 bool recvSet = false;
1287 bool sendSet = false;
1288 bool errorSet = false;
1290 LOCK(pnode->cs_hSocket);
1291 if (pnode->hSocket == INVALID_SOCKET)
1292 continue;
1293 recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
1294 sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
1295 errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
1297 if (recvSet || errorSet)
1299 // typical socket buffer is 8K-64K
1300 char pchBuf[0x10000];
1301 int nBytes = 0;
1303 LOCK(pnode->cs_hSocket);
1304 if (pnode->hSocket == INVALID_SOCKET)
1305 continue;
1306 nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1308 if (nBytes > 0)
1310 bool notify = false;
1311 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1312 pnode->CloseSocketDisconnect();
1313 RecordBytesRecv(nBytes);
1314 if (notify) {
1315 size_t nSizeAdded = 0;
1316 auto it(pnode->vRecvMsg.begin());
1317 for (; it != pnode->vRecvMsg.end(); ++it) {
1318 if (!it->complete())
1319 break;
1320 nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1323 LOCK(pnode->cs_vProcessMsg);
1324 pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1325 pnode->nProcessQueueSize += nSizeAdded;
1326 pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1328 WakeMessageHandler();
1331 else if (nBytes == 0)
1333 // socket closed gracefully
1334 if (!pnode->fDisconnect) {
1335 LogPrint(BCLog::NET, "socket closed\n");
1337 pnode->CloseSocketDisconnect();
1339 else if (nBytes < 0)
1341 // error
1342 int nErr = WSAGetLastError();
1343 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1345 if (!pnode->fDisconnect)
1346 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1347 pnode->CloseSocketDisconnect();
1353 // Send
1355 if (sendSet)
1357 LOCK(pnode->cs_vSend);
1358 size_t nBytes = SocketSendData(pnode);
1359 if (nBytes) {
1360 RecordBytesSent(nBytes);
1365 // Inactivity checking
1367 int64_t nTime = GetSystemTimeInSeconds();
1368 if (nTime - pnode->nTimeConnected > 60)
1370 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1372 LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1373 pnode->fDisconnect = true;
1375 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1377 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1378 pnode->fDisconnect = true;
1380 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1382 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1383 pnode->fDisconnect = true;
1385 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1387 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1388 pnode->fDisconnect = true;
1390 else if (!pnode->fSuccessfullyConnected)
1392 LogPrintf("version handshake timeout from %d\n", pnode->GetId());
1393 pnode->fDisconnect = true;
1398 LOCK(cs_vNodes);
1399 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1400 pnode->Release();
1405 void CConnman::WakeMessageHandler()
1408 std::lock_guard<std::mutex> lock(mutexMsgProc);
1409 fMsgProcWake = true;
1411 condMsgProc.notify_one();
1419 #ifdef USE_UPNP
1420 void ThreadMapPort()
1422 std::string port = strprintf("%u", GetListenPort());
1423 const char * multicastif = 0;
1424 const char * minissdpdpath = 0;
1425 struct UPNPDev * devlist = 0;
1426 char lanaddr[64];
1428 #ifndef UPNPDISCOVER_SUCCESS
1429 /* miniupnpc 1.5 */
1430 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1431 #elif MINIUPNPC_API_VERSION < 14
1432 /* miniupnpc 1.6 */
1433 int error = 0;
1434 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1435 #else
1436 /* miniupnpc 1.9.20150730 */
1437 int error = 0;
1438 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1439 #endif
1441 struct UPNPUrls urls;
1442 struct IGDdatas data;
1443 int r;
1445 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1446 if (r == 1)
1448 if (fDiscover) {
1449 char externalIPAddress[40];
1450 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1451 if(r != UPNPCOMMAND_SUCCESS)
1452 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1453 else
1455 if(externalIPAddress[0])
1457 CNetAddr resolved;
1458 if(LookupHost(externalIPAddress, resolved, false)) {
1459 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1460 AddLocal(resolved, LOCAL_UPNP);
1463 else
1464 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1468 std::string strDesc = "Bitcoin " + FormatFullVersion();
1470 try {
1471 while (true) {
1472 #ifndef UPNPDISCOVER_SUCCESS
1473 /* miniupnpc 1.5 */
1474 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1475 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1476 #else
1477 /* miniupnpc 1.6 */
1478 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1479 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1480 #endif
1482 if(r!=UPNPCOMMAND_SUCCESS)
1483 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1484 port, port, lanaddr, r, strupnperror(r));
1485 else
1486 LogPrintf("UPnP Port Mapping successful.\n");
1488 MilliSleep(20*60*1000); // Refresh every 20 minutes
1491 catch (const boost::thread_interrupted&)
1493 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1494 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1495 freeUPNPDevlist(devlist); devlist = 0;
1496 FreeUPNPUrls(&urls);
1497 throw;
1499 } else {
1500 LogPrintf("No valid UPnP IGDs found\n");
1501 freeUPNPDevlist(devlist); devlist = 0;
1502 if (r != 0)
1503 FreeUPNPUrls(&urls);
1507 void MapPort(bool fUseUPnP)
1509 static boost::thread* upnp_thread = NULL;
1511 if (fUseUPnP)
1513 if (upnp_thread) {
1514 upnp_thread->interrupt();
1515 upnp_thread->join();
1516 delete upnp_thread;
1518 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1520 else if (upnp_thread) {
1521 upnp_thread->interrupt();
1522 upnp_thread->join();
1523 delete upnp_thread;
1524 upnp_thread = NULL;
1528 #else
1529 void MapPort(bool)
1531 // Intentionally left blank.
1533 #endif
1540 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1542 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1543 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1544 *requiredServiceBits = NODE_NETWORK;
1545 return data.host;
1548 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1549 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1553 void CConnman::ThreadDNSAddressSeed()
1555 // goal: only query DNS seeds if address need is acute
1556 // Avoiding DNS seeds when we don't need them improves user privacy by
1557 // creating fewer identifying DNS requests, reduces trust by giving seeds
1558 // less influence on the network topology, and reduces traffic to the seeds.
1559 if ((addrman.size() > 0) &&
1560 (!GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1561 if (!interruptNet.sleep_for(std::chrono::seconds(11)))
1562 return;
1564 LOCK(cs_vNodes);
1565 int nRelevant = 0;
1566 for (auto pnode : vNodes) {
1567 nRelevant += pnode->fSuccessfullyConnected && ((pnode->nServices & nRelevantServices) == nRelevantServices);
1569 if (nRelevant >= 2) {
1570 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1571 return;
1575 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1576 int found = 0;
1578 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1580 BOOST_FOREACH(const CDNSSeedData &seed, vSeeds) {
1581 if (interruptNet) {
1582 return;
1584 if (HaveNameProxy()) {
1585 AddOneShot(seed.host);
1586 } else {
1587 std::vector<CNetAddr> vIPs;
1588 std::vector<CAddress> vAdd;
1589 ServiceFlags requiredServiceBits = nRelevantServices;
1590 if (LookupHost(GetDNSHost(seed, &requiredServiceBits).c_str(), vIPs, 0, true))
1592 BOOST_FOREACH(const CNetAddr& ip, vIPs)
1594 int nOneDay = 24*3600;
1595 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
1596 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1597 vAdd.push_back(addr);
1598 found++;
1601 if (interruptNet) {
1602 return;
1604 // TODO: The seed name resolve may fail, yielding an IP of [::], which results in
1605 // addrman assigning the same source to results from different seeds.
1606 // This should switch to a hard-coded stable dummy IP for each seed name, so that the
1607 // resolve is not required at all.
1608 if (!vIPs.empty()) {
1609 CService seedSource;
1610 Lookup(seed.name.c_str(), seedSource, 0, true);
1611 addrman.Add(vAdd, seedSource);
1616 LogPrintf("%d addresses found from DNS seeds\n", found);
1630 void CConnman::DumpAddresses()
1632 int64_t nStart = GetTimeMillis();
1634 CAddrDB adb;
1635 adb.Write(addrman);
1637 LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1638 addrman.size(), GetTimeMillis() - nStart);
1641 void CConnman::DumpData()
1643 DumpAddresses();
1644 DumpBanlist();
1647 void CConnman::ProcessOneShot()
1649 std::string strDest;
1651 LOCK(cs_vOneShots);
1652 if (vOneShots.empty())
1653 return;
1654 strDest = vOneShots.front();
1655 vOneShots.pop_front();
1657 CAddress addr;
1658 CSemaphoreGrant grant(*semOutbound, true);
1659 if (grant) {
1660 if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
1661 AddOneShot(strDest);
1665 void CConnman::ThreadOpenConnections()
1667 // Connect to specific addresses
1668 if (gArgs.IsArgSet("-connect") && gArgs.GetArgs("-connect").size() > 0)
1670 for (int64_t nLoop = 0;; nLoop++)
1672 ProcessOneShot();
1673 BOOST_FOREACH(const std::string& strAddr, gArgs.GetArgs("-connect"))
1675 CAddress addr(CService(), NODE_NONE);
1676 OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
1677 for (int i = 0; i < 10 && i < nLoop; i++)
1679 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1680 return;
1683 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1684 return;
1688 // Initiate network connections
1689 int64_t nStart = GetTime();
1691 // Minimum time before next feeler connection (in microseconds).
1692 int64_t nNextFeeler = PoissonNextSend(nStart*1000*1000, FEELER_INTERVAL);
1693 while (!interruptNet)
1695 ProcessOneShot();
1697 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1698 return;
1700 CSemaphoreGrant grant(*semOutbound);
1701 if (interruptNet)
1702 return;
1704 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1705 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1706 static bool done = false;
1707 if (!done) {
1708 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1709 CNetAddr local;
1710 LookupHost("127.0.0.1", local, false);
1711 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1712 done = true;
1717 // Choose an address to connect to based on most recently seen
1719 CAddress addrConnect;
1721 // Only connect out to one peer per network group (/16 for IPv4).
1722 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1723 int nOutbound = 0;
1724 std::set<std::vector<unsigned char> > setConnected;
1726 LOCK(cs_vNodes);
1727 BOOST_FOREACH(CNode* pnode, vNodes) {
1728 if (!pnode->fInbound && !pnode->fAddnode) {
1729 // Netgroups for inbound and addnode peers are not excluded because our goal here
1730 // is to not use multiple of our limited outbound slots on a single netgroup
1731 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1732 // also have the added issue that they're attacker controlled and could be used
1733 // to prevent us from connecting to particular hosts if we used them here.
1734 setConnected.insert(pnode->addr.GetGroup());
1735 nOutbound++;
1740 // Feeler Connections
1742 // Design goals:
1743 // * Increase the number of connectable addresses in the tried table.
1745 // Method:
1746 // * Choose a random address from new and attempt to connect to it if we can connect
1747 // successfully it is added to tried.
1748 // * Start attempting feeler connections only after node finishes making outbound
1749 // connections.
1750 // * Only make a feeler connection once every few minutes.
1752 bool fFeeler = false;
1753 if (nOutbound >= nMaxOutbound) {
1754 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1755 if (nTime > nNextFeeler) {
1756 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1757 fFeeler = true;
1758 } else {
1759 continue;
1763 int64_t nANow = GetAdjustedTime();
1764 int nTries = 0;
1765 while (!interruptNet)
1767 CAddrInfo addr = addrman.Select(fFeeler);
1769 // if we selected an invalid address, restart
1770 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1771 break;
1773 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1774 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1775 // already-connected network ranges, ...) before trying new addrman addresses.
1776 nTries++;
1777 if (nTries > 100)
1778 break;
1780 if (IsLimited(addr))
1781 continue;
1783 // only connect to full nodes
1784 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1785 continue;
1787 // only consider very recently tried nodes after 30 failed attempts
1788 if (nANow - addr.nLastTry < 600 && nTries < 30)
1789 continue;
1791 // only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1792 if ((addr.nServices & nRelevantServices) != nRelevantServices && (nTries < 40 || nOutbound >= (nMaxOutbound >> 1)))
1793 continue;
1795 // do not allow non-default ports, unless after 50 invalid addresses selected already
1796 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1797 continue;
1799 addrConnect = addr;
1800 break;
1803 if (addrConnect.IsValid()) {
1805 if (fFeeler) {
1806 // Add small amount of random noise before connection to avoid synchronization.
1807 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1808 if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1809 return;
1810 LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
1813 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, NULL, false, fFeeler);
1818 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1820 std::vector<AddedNodeInfo> ret;
1822 std::list<std::string> lAddresses(0);
1824 LOCK(cs_vAddedNodes);
1825 ret.reserve(vAddedNodes.size());
1826 BOOST_FOREACH(const std::string& strAddNode, vAddedNodes)
1827 lAddresses.push_back(strAddNode);
1831 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1832 std::map<CService, bool> mapConnected;
1833 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
1835 LOCK(cs_vNodes);
1836 for (const CNode* pnode : vNodes) {
1837 if (pnode->addr.IsValid()) {
1838 mapConnected[pnode->addr] = pnode->fInbound;
1840 std::string addrName = pnode->GetAddrName();
1841 if (!addrName.empty()) {
1842 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1847 BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
1848 CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1849 if (service.IsValid()) {
1850 // strAddNode is an IP:port
1851 auto it = mapConnected.find(service);
1852 if (it != mapConnected.end()) {
1853 ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1854 } else {
1855 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1857 } else {
1858 // strAddNode is a name
1859 auto it = mapConnectedByName.find(strAddNode);
1860 if (it != mapConnectedByName.end()) {
1861 ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1862 } else {
1863 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1868 return ret;
1871 void CConnman::ThreadOpenAddedConnections()
1874 LOCK(cs_vAddedNodes);
1875 if (gArgs.IsArgSet("-addnode"))
1876 vAddedNodes = gArgs.GetArgs("-addnode");
1879 while (true)
1881 CSemaphoreGrant grant(*semAddnode);
1882 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1883 bool tried = false;
1884 for (const AddedNodeInfo& info : vInfo) {
1885 if (!info.fConnected) {
1886 if (!grant.TryAcquire()) {
1887 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1888 // the addednodeinfo state might change.
1889 break;
1891 // If strAddedNode is an IP/port, decode it immediately, so
1892 // OpenNetworkConnection can detect existing connections to that IP/port.
1893 tried = true;
1894 CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
1895 OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false, false, true);
1896 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
1897 return;
1900 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1901 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
1902 return;
1906 // if successful, this moves the passed grant to the constructed node
1907 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool fAddnode)
1910 // Initiate outbound network connection
1912 if (interruptNet) {
1913 return false;
1915 if (!fNetworkActive) {
1916 return false;
1918 if (!pszDest) {
1919 if (IsLocal(addrConnect) ||
1920 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1921 FindNode(addrConnect.ToStringIPPort()))
1922 return false;
1923 } else if (FindNode(std::string(pszDest)))
1924 return false;
1926 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1928 if (!pnode)
1929 return false;
1930 if (grantOutbound)
1931 grantOutbound->MoveTo(pnode->grantOutbound);
1932 if (fOneShot)
1933 pnode->fOneShot = true;
1934 if (fFeeler)
1935 pnode->fFeeler = true;
1936 if (fAddnode)
1937 pnode->fAddnode = true;
1939 GetNodeSignals().InitializeNode(pnode, *this);
1941 LOCK(cs_vNodes);
1942 vNodes.push_back(pnode);
1945 return true;
1948 void CConnman::ThreadMessageHandler()
1950 while (!flagInterruptMsgProc)
1952 std::vector<CNode*> vNodesCopy;
1954 LOCK(cs_vNodes);
1955 vNodesCopy = vNodes;
1956 BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1957 pnode->AddRef();
1961 bool fMoreWork = false;
1963 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1965 if (pnode->fDisconnect)
1966 continue;
1968 // Receive messages
1969 bool fMoreNodeWork = GetNodeSignals().ProcessMessages(pnode, *this, flagInterruptMsgProc);
1970 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
1971 if (flagInterruptMsgProc)
1972 return;
1974 // Send messages
1976 LOCK(pnode->cs_sendProcessing);
1977 GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
1979 if (flagInterruptMsgProc)
1980 return;
1984 LOCK(cs_vNodes);
1985 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1986 pnode->Release();
1989 std::unique_lock<std::mutex> lock(mutexMsgProc);
1990 if (!fMoreWork) {
1991 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
1993 fMsgProcWake = false;
2002 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
2004 strError = "";
2005 int nOne = 1;
2007 // Create socket for listening for incoming connections
2008 struct sockaddr_storage sockaddr;
2009 socklen_t len = sizeof(sockaddr);
2010 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
2012 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
2013 LogPrintf("%s\n", strError);
2014 return false;
2017 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2018 if (hListenSocket == INVALID_SOCKET)
2020 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2021 LogPrintf("%s\n", strError);
2022 return false;
2024 if (!IsSelectableSocket(hListenSocket))
2026 strError = "Error: Couldn't create a listenable socket for incoming connections";
2027 LogPrintf("%s\n", strError);
2028 return false;
2032 #ifndef WIN32
2033 #ifdef SO_NOSIGPIPE
2034 // Different way of disabling SIGPIPE on BSD
2035 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
2036 #endif
2037 // Allow binding if the port is still in TIME_WAIT state after
2038 // the program was closed and restarted.
2039 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2040 // Disable Nagle's algorithm
2041 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
2042 #else
2043 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2044 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
2045 #endif
2047 // Set to non-blocking, incoming connections will also inherit this
2048 if (!SetSocketNonBlocking(hListenSocket, true)) {
2049 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2050 LogPrintf("%s\n", strError);
2051 return false;
2054 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2055 // and enable it by default or not. Try to enable it, if possible.
2056 if (addrBind.IsIPv6()) {
2057 #ifdef IPV6_V6ONLY
2058 #ifdef WIN32
2059 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2060 #else
2061 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2062 #endif
2063 #endif
2064 #ifdef WIN32
2065 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2066 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2067 #endif
2070 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2072 int nErr = WSAGetLastError();
2073 if (nErr == WSAEADDRINUSE)
2074 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
2075 else
2076 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2077 LogPrintf("%s\n", strError);
2078 CloseSocket(hListenSocket);
2079 return false;
2081 LogPrintf("Bound to %s\n", addrBind.ToString());
2083 // Listen for incoming connections
2084 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
2086 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2087 LogPrintf("%s\n", strError);
2088 CloseSocket(hListenSocket);
2089 return false;
2092 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
2094 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
2095 AddLocal(addrBind, LOCAL_BIND);
2097 return true;
2100 void Discover(boost::thread_group& threadGroup)
2102 if (!fDiscover)
2103 return;
2105 #ifdef WIN32
2106 // Get local host IP
2107 char pszHostName[256] = "";
2108 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
2110 std::vector<CNetAddr> vaddr;
2111 if (LookupHost(pszHostName, vaddr, 0, true))
2113 BOOST_FOREACH (const CNetAddr &addr, vaddr)
2115 if (AddLocal(addr, LOCAL_IF))
2116 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
2120 #else
2121 // Get local host ip
2122 struct ifaddrs* myaddrs;
2123 if (getifaddrs(&myaddrs) == 0)
2125 for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
2127 if (ifa->ifa_addr == NULL) continue;
2128 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2129 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2130 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2131 if (ifa->ifa_addr->sa_family == AF_INET)
2133 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2134 CNetAddr addr(s4->sin_addr);
2135 if (AddLocal(addr, LOCAL_IF))
2136 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2138 else if (ifa->ifa_addr->sa_family == AF_INET6)
2140 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2141 CNetAddr addr(s6->sin6_addr);
2142 if (AddLocal(addr, LOCAL_IF))
2143 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2146 freeifaddrs(myaddrs);
2148 #endif
2151 void CConnman::SetNetworkActive(bool active)
2153 LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2155 if (!active) {
2156 fNetworkActive = false;
2158 LOCK(cs_vNodes);
2159 // Close sockets to all nodes
2160 BOOST_FOREACH(CNode* pnode, vNodes) {
2161 pnode->CloseSocketDisconnect();
2163 } else {
2164 fNetworkActive = true;
2167 uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2170 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2172 fNetworkActive = true;
2173 setBannedIsDirty = false;
2174 fAddressesInitialized = false;
2175 nLastNodeId = 0;
2176 nSendBufferMaxSize = 0;
2177 nReceiveFloodSize = 0;
2178 semOutbound = NULL;
2179 semAddnode = NULL;
2180 nMaxConnections = 0;
2181 nMaxOutbound = 0;
2182 nMaxAddnode = 0;
2183 nBestHeight = 0;
2184 clientInterface = NULL;
2185 flagInterruptMsgProc = false;
2188 NodeId CConnman::GetNewNodeId()
2190 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2193 bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options connOptions)
2195 nTotalBytesRecv = 0;
2196 nTotalBytesSent = 0;
2197 nMaxOutboundTotalBytesSentInCycle = 0;
2198 nMaxOutboundCycleStartTime = 0;
2200 nRelevantServices = connOptions.nRelevantServices;
2201 nLocalServices = connOptions.nLocalServices;
2202 nMaxConnections = connOptions.nMaxConnections;
2203 nMaxOutbound = std::min((connOptions.nMaxOutbound), nMaxConnections);
2204 nMaxAddnode = connOptions.nMaxAddnode;
2205 nMaxFeeler = connOptions.nMaxFeeler;
2207 nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
2208 nReceiveFloodSize = connOptions.nReceiveFloodSize;
2210 nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
2211 nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
2213 SetBestHeight(connOptions.nBestHeight);
2215 clientInterface = connOptions.uiInterface;
2216 if (clientInterface) {
2217 clientInterface->InitMessage(_("Loading P2P addresses..."));
2219 // Load addresses from peers.dat
2220 int64_t nStart = GetTimeMillis();
2222 CAddrDB adb;
2223 if (adb.Read(addrman))
2224 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2225 else {
2226 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2227 LogPrintf("Invalid or missing peers.dat; recreating\n");
2228 DumpAddresses();
2231 if (clientInterface)
2232 clientInterface->InitMessage(_("Loading banlist..."));
2233 // Load addresses from banlist.dat
2234 nStart = GetTimeMillis();
2235 CBanDB bandb;
2236 banmap_t banmap;
2237 if (bandb.Read(banmap)) {
2238 SetBanned(banmap); // thread save setter
2239 SetBannedSetDirty(false); // no need to write down, just read data
2240 SweepBanned(); // sweep out unused entries
2242 LogPrint(BCLog::NET, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2243 banmap.size(), GetTimeMillis() - nStart);
2244 } else {
2245 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2246 SetBannedSetDirty(true); // force write
2247 DumpBanlist();
2250 uiInterface.InitMessage(_("Starting network threads..."));
2252 fAddressesInitialized = true;
2254 if (semOutbound == NULL) {
2255 // initialize semaphore
2256 semOutbound = new CSemaphore(std::min((nMaxOutbound + nMaxFeeler), nMaxConnections));
2258 if (semAddnode == NULL) {
2259 // initialize semaphore
2260 semAddnode = new CSemaphore(nMaxAddnode);
2264 // Start threads
2266 InterruptSocks5(false);
2267 interruptNet.reset();
2268 flagInterruptMsgProc = false;
2271 std::unique_lock<std::mutex> lock(mutexMsgProc);
2272 fMsgProcWake = false;
2275 // Send and receive from sockets, accept connections
2276 threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2278 if (!GetBoolArg("-dnsseed", true))
2279 LogPrintf("DNS seeding disabled\n");
2280 else
2281 threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2283 // Initiate outbound connections from -addnode
2284 threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2286 // Initiate outbound connections unless connect=0
2287 if (!gArgs.IsArgSet("-connect") || gArgs.GetArgs("-connect").size() != 1 || gArgs.GetArgs("-connect")[0] != "0")
2288 threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this)));
2290 // Process messages
2291 threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2293 // Dump network addresses
2294 scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
2296 return true;
2299 class CNetCleanup
2301 public:
2302 CNetCleanup() {}
2304 ~CNetCleanup()
2306 #ifdef WIN32
2307 // Shutdown Windows Sockets
2308 WSACleanup();
2309 #endif
2312 instance_of_cnetcleanup;
2314 void CConnman::Interrupt()
2317 std::lock_guard<std::mutex> lock(mutexMsgProc);
2318 flagInterruptMsgProc = true;
2320 condMsgProc.notify_all();
2322 interruptNet();
2323 InterruptSocks5(true);
2325 if (semOutbound) {
2326 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++) {
2327 semOutbound->post();
2331 if (semAddnode) {
2332 for (int i=0; i<nMaxAddnode; i++) {
2333 semAddnode->post();
2338 void CConnman::Stop()
2340 if (threadMessageHandler.joinable())
2341 threadMessageHandler.join();
2342 if (threadOpenConnections.joinable())
2343 threadOpenConnections.join();
2344 if (threadOpenAddedConnections.joinable())
2345 threadOpenAddedConnections.join();
2346 if (threadDNSAddressSeed.joinable())
2347 threadDNSAddressSeed.join();
2348 if (threadSocketHandler.joinable())
2349 threadSocketHandler.join();
2351 if (fAddressesInitialized)
2353 DumpData();
2354 fAddressesInitialized = false;
2357 // Close sockets
2358 BOOST_FOREACH(CNode* pnode, vNodes)
2359 pnode->CloseSocketDisconnect();
2360 BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
2361 if (hListenSocket.socket != INVALID_SOCKET)
2362 if (!CloseSocket(hListenSocket.socket))
2363 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2365 // clean up some globals (to help leak detection)
2366 BOOST_FOREACH(CNode *pnode, vNodes) {
2367 DeleteNode(pnode);
2369 BOOST_FOREACH(CNode *pnode, vNodesDisconnected) {
2370 DeleteNode(pnode);
2372 vNodes.clear();
2373 vNodesDisconnected.clear();
2374 vhListenSocket.clear();
2375 delete semOutbound;
2376 semOutbound = NULL;
2377 delete semAddnode;
2378 semAddnode = NULL;
2381 void CConnman::DeleteNode(CNode* pnode)
2383 assert(pnode);
2384 bool fUpdateConnectionTime = false;
2385 GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2386 if(fUpdateConnectionTime)
2387 addrman.Connected(pnode->addr);
2388 delete pnode;
2391 CConnman::~CConnman()
2393 Interrupt();
2394 Stop();
2397 size_t CConnman::GetAddressCount() const
2399 return addrman.size();
2402 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2404 addrman.SetServices(addr, nServices);
2407 void CConnman::MarkAddressGood(const CAddress& addr)
2409 addrman.Good(addr);
2412 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2414 addrman.Add(vAddr, addrFrom, nTimePenalty);
2417 std::vector<CAddress> CConnman::GetAddresses()
2419 return addrman.GetAddr();
2422 bool CConnman::AddNode(const std::string& strNode)
2424 LOCK(cs_vAddedNodes);
2425 for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2426 if (strNode == *it)
2427 return false;
2430 vAddedNodes.push_back(strNode);
2431 return true;
2434 bool CConnman::RemoveAddedNode(const std::string& strNode)
2436 LOCK(cs_vAddedNodes);
2437 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2438 if (strNode == *it) {
2439 vAddedNodes.erase(it);
2440 return true;
2443 return false;
2446 size_t CConnman::GetNodeCount(NumConnections flags)
2448 LOCK(cs_vNodes);
2449 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2450 return vNodes.size();
2452 int nNum = 0;
2453 for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
2454 if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
2455 nNum++;
2457 return nNum;
2460 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2462 vstats.clear();
2463 LOCK(cs_vNodes);
2464 vstats.reserve(vNodes.size());
2465 for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
2466 CNode* pnode = *it;
2467 vstats.emplace_back();
2468 pnode->copyStats(vstats.back());
2472 bool CConnman::DisconnectNode(const std::string& strNode)
2474 LOCK(cs_vNodes);
2475 if (CNode* pnode = FindNode(strNode)) {
2476 pnode->fDisconnect = true;
2477 return true;
2479 return false;
2481 bool CConnman::DisconnectNode(NodeId id)
2483 LOCK(cs_vNodes);
2484 for(CNode* pnode : vNodes) {
2485 if (id == pnode->GetId()) {
2486 pnode->fDisconnect = true;
2487 return true;
2490 return false;
2493 void CConnman::RecordBytesRecv(uint64_t bytes)
2495 LOCK(cs_totalBytesRecv);
2496 nTotalBytesRecv += bytes;
2499 void CConnman::RecordBytesSent(uint64_t bytes)
2501 LOCK(cs_totalBytesSent);
2502 nTotalBytesSent += bytes;
2504 uint64_t now = GetTime();
2505 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2507 // timeframe expired, reset cycle
2508 nMaxOutboundCycleStartTime = now;
2509 nMaxOutboundTotalBytesSentInCycle = 0;
2512 // TODO, exclude whitebind peers
2513 nMaxOutboundTotalBytesSentInCycle += bytes;
2516 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2518 LOCK(cs_totalBytesSent);
2519 nMaxOutboundLimit = limit;
2522 uint64_t CConnman::GetMaxOutboundTarget()
2524 LOCK(cs_totalBytesSent);
2525 return nMaxOutboundLimit;
2528 uint64_t CConnman::GetMaxOutboundTimeframe()
2530 LOCK(cs_totalBytesSent);
2531 return nMaxOutboundTimeframe;
2534 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2536 LOCK(cs_totalBytesSent);
2537 if (nMaxOutboundLimit == 0)
2538 return 0;
2540 if (nMaxOutboundCycleStartTime == 0)
2541 return nMaxOutboundTimeframe;
2543 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2544 uint64_t now = GetTime();
2545 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2548 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2550 LOCK(cs_totalBytesSent);
2551 if (nMaxOutboundTimeframe != timeframe)
2553 // reset measure-cycle in case of changing
2554 // the timeframe
2555 nMaxOutboundCycleStartTime = GetTime();
2557 nMaxOutboundTimeframe = timeframe;
2560 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2562 LOCK(cs_totalBytesSent);
2563 if (nMaxOutboundLimit == 0)
2564 return false;
2566 if (historicalBlockServingLimit)
2568 // keep a large enough buffer to at least relay each block once
2569 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2570 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2571 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2572 return true;
2574 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2575 return true;
2577 return false;
2580 uint64_t CConnman::GetOutboundTargetBytesLeft()
2582 LOCK(cs_totalBytesSent);
2583 if (nMaxOutboundLimit == 0)
2584 return 0;
2586 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2589 uint64_t CConnman::GetTotalBytesRecv()
2591 LOCK(cs_totalBytesRecv);
2592 return nTotalBytesRecv;
2595 uint64_t CConnman::GetTotalBytesSent()
2597 LOCK(cs_totalBytesSent);
2598 return nTotalBytesSent;
2601 ServiceFlags CConnman::GetLocalServices() const
2603 return nLocalServices;
2606 void CConnman::SetBestHeight(int height)
2608 nBestHeight.store(height, std::memory_order_release);
2611 int CConnman::GetBestHeight() const
2613 return nBestHeight.load(std::memory_order_acquire);
2616 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2617 unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize; }
2619 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) :
2620 nTimeConnected(GetSystemTimeInSeconds()),
2621 addr(addrIn),
2622 fInbound(fInboundIn),
2623 nKeyedNetGroup(nKeyedNetGroupIn),
2624 addrKnown(5000, 0.001),
2625 filterInventoryKnown(50000, 0.000001),
2626 id(idIn),
2627 nLocalHostNonce(nLocalHostNonceIn),
2628 nLocalServices(nLocalServicesIn),
2629 nMyStartingHeight(nMyStartingHeightIn),
2630 nSendVersion(0)
2632 nServices = NODE_NONE;
2633 nServicesExpected = NODE_NONE;
2634 hSocket = hSocketIn;
2635 nRecvVersion = INIT_PROTO_VERSION;
2636 nLastSend = 0;
2637 nLastRecv = 0;
2638 nSendBytes = 0;
2639 nRecvBytes = 0;
2640 nTimeOffset = 0;
2641 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2642 nVersion = 0;
2643 strSubVer = "";
2644 fWhitelisted = false;
2645 fOneShot = false;
2646 fAddnode = false;
2647 fClient = false; // set by version message
2648 fFeeler = false;
2649 fSuccessfullyConnected = false;
2650 fDisconnect = false;
2651 nRefCount = 0;
2652 nSendSize = 0;
2653 nSendOffset = 0;
2654 hashContinue = uint256();
2655 nStartingHeight = -1;
2656 filterInventoryKnown.reset();
2657 fSendMempool = false;
2658 fGetAddr = false;
2659 nNextLocalAddrSend = 0;
2660 nNextAddrSend = 0;
2661 nNextInvSend = 0;
2662 fRelayTxes = false;
2663 fSentAddr = false;
2664 pfilter = new CBloomFilter();
2665 timeLastMempoolReq = 0;
2666 nLastBlockTime = 0;
2667 nLastTXTime = 0;
2668 nPingNonceSent = 0;
2669 nPingUsecStart = 0;
2670 nPingUsecTime = 0;
2671 fPingQueued = false;
2672 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2673 minFeeFilter = 0;
2674 lastSentFeeFilter = 0;
2675 nextSendTimeFeeFilter = 0;
2676 fPauseRecv = false;
2677 fPauseSend = false;
2678 nProcessQueueSize = 0;
2680 BOOST_FOREACH(const std::string &msg, getAllNetMessageTypes())
2681 mapRecvBytesPerMsgCmd[msg] = 0;
2682 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2684 if (fLogIPs) {
2685 LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
2686 } else {
2687 LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
2691 CNode::~CNode()
2693 CloseSocket(hSocket);
2695 if (pfilter)
2696 delete pfilter;
2699 void CNode::AskFor(const CInv& inv)
2701 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2702 return;
2703 // a peer may not have multiple non-responded queue positions for a single inv item
2704 if (!setAskFor.insert(inv.hash).second)
2705 return;
2707 // We're using mapAskFor as a priority queue,
2708 // the key is the earliest time the request can be sent
2709 int64_t nRequestTime;
2710 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2711 if (it != mapAlreadyAskedFor.end())
2712 nRequestTime = it->second;
2713 else
2714 nRequestTime = 0;
2715 LogPrint(BCLog::NET, "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2717 // Make sure not to reuse time indexes to keep things in the same order
2718 int64_t nNow = GetTimeMicros() - 1000000;
2719 static int64_t nLastTime;
2720 ++nLastTime;
2721 nNow = std::max(nNow, nLastTime);
2722 nLastTime = nNow;
2724 // Each retry is 2 minutes after the last
2725 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2726 if (it != mapAlreadyAskedFor.end())
2727 mapAlreadyAskedFor.update(it, nRequestTime);
2728 else
2729 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2730 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2733 bool CConnman::NodeFullyConnected(const CNode* pnode)
2735 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
2738 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
2740 size_t nMessageSize = msg.data.size();
2741 size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
2742 LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->GetId());
2744 std::vector<unsigned char> serializedHeader;
2745 serializedHeader.reserve(CMessageHeader::HEADER_SIZE);
2746 uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize);
2747 CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize);
2748 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
2750 CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
2752 size_t nBytesSent = 0;
2754 LOCK(pnode->cs_vSend);
2755 bool optimisticSend(pnode->vSendMsg.empty());
2757 //log total amount of bytes per command
2758 pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
2759 pnode->nSendSize += nTotalSize;
2761 if (pnode->nSendSize > nSendBufferMaxSize)
2762 pnode->fPauseSend = true;
2763 pnode->vSendMsg.push_back(std::move(serializedHeader));
2764 if (nMessageSize)
2765 pnode->vSendMsg.push_back(std::move(msg.data));
2767 // If write queue empty, attempt "optimistic write"
2768 if (optimisticSend == true)
2769 nBytesSent = SocketSendData(pnode);
2771 if (nBytesSent)
2772 RecordBytesSent(nBytesSent);
2775 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2777 CNode* found = nullptr;
2778 LOCK(cs_vNodes);
2779 for (auto&& pnode : vNodes) {
2780 if(pnode->GetId() == id) {
2781 found = pnode;
2782 break;
2785 return found != nullptr && NodeFullyConnected(found) && func(found);
2788 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2789 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2792 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
2794 return CSipHasher(nSeed0, nSeed1).Write(id);
2797 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
2799 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2801 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(&vchNetGroup[0], vchNetGroup.size()).Finalize();