1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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>
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>
19 #include <scheduler.h>
20 #include <ui_interface.h>
21 #include <utilstrencodings.h>
30 #include <miniupnpc/miniupnpc.h>
31 #include <miniupnpc/miniwget.h>
32 #include <miniupnpc/upnpcommands.h>
33 #include <miniupnpc/upnperrors.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
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
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.
57 #ifndef PROTECTION_LEVEL_UNRESTRICTED
58 #define PROTECTION_LEVEL_UNRESTRICTED 10
60 #ifndef IPV6_PROTECTION_LEVEL
61 #define IPV6_PROTECTION_LEVEL 23
65 /** Used to pass flags to the Bind() function */
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;
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
)
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
)
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
;
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
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
) {
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
);
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
150 CAddress
GetLocalAddress(const CNetAddr
*paddrPeer
, ServiceFlags nLocalServices
)
152 CAddress
ret(CService(CNetAddr(),GetListenPort()), nLocalServices
);
154 if (GetLocal(addr
, paddrPeer
))
156 ret
= CAddress(addr
, nLocalServices
);
158 ret
.nTime
= GetAdjustedTime();
162 int GetnScore(const CService
& addr
)
164 LOCK(cs_mapLocalHost
);
165 if (mapLocalHost
.count(addr
) == LOCAL_NONE
)
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())
207 if (!fDiscover
&& nScore
< LOCAL_MANUAL
)
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();
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
);
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
)
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)
268 mapLocalHost
[addr
].nScore
++;
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
)
299 for (CNode
* pnode
: vNodes
) {
300 if ((CNetAddr
)pnode
->addr
== ip
) {
307 CNode
* CConnman::FindNode(const CSubNet
& subNet
)
310 for (CNode
* pnode
: vNodes
) {
311 if (subNet
.Match((CNetAddr
)pnode
->addr
)) {
318 CNode
* CConnman::FindNode(const std::string
& addrName
)
321 for (CNode
* pnode
: vNodes
) {
322 if (pnode
->GetAddrName() == addrName
) {
329 CNode
* CConnman::FindNode(const CService
& addr
)
332 for (CNode
* pnode
: vNodes
) {
333 if ((CService
)pnode
->addr
== addr
) {
340 bool CConnman::CheckIncomingNonce(uint64_t nonce
)
343 for (CNode
* pnode
: vNodes
) {
344 if (!pnode
->fSuccessfullyConnected
&& !pnode
->fInbound
&& pnode
->GetLocalNonce() == nonce
)
350 /** Get the bind address for a socket as CAddress */
351 static CAddress
GetBindAddress(SOCKET sock
)
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
);
360 LogPrint(BCLog::NET
, "Warning: getsockname failed\n");
366 CNode
* CConnman::ConnectNode(CAddress addrConnect
, const char *pszDest
, bool fCountFailure
)
368 if (pszDest
== nullptr) {
369 if (IsLocal(addrConnect
))
372 // Look for an existing connection
373 CNode
* pnode
= FindNode((CService
)addrConnect
);
376 LogPrintf("Failed to open new connection, already connected\n");
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);
387 const int default_port
= Params().GetDefaultPort();
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
);
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.
401 CNode
* pnode
= FindNode((CService
)addrConnect
);
404 pnode
->MaybeSetAddrName(std::string(pszDest
));
405 LogPrintf("Failed to open new connection, already connected\n");
412 bool connected
= false;
415 if (addrConnect
.IsValid()) {
416 bool proxyConnectionFailed
= false;
418 if (GetProxy(addrConnect
.GetNetwork(), proxy
)) {
419 hSocket
= CreateSocket(proxy
.proxy
);
420 if (hSocket
== INVALID_SOCKET
) {
423 connected
= ConnectThroughProxy(proxy
, addrConnect
.ToStringIP(), addrConnect
.GetPort(), hSocket
, nConnectTimeout
, &proxyConnectionFailed
);
425 // no proxy needed (none set for target network)
426 hSocket
= CreateSocket(addrConnect
);
427 if (hSocket
== INVALID_SOCKET
) {
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
) {
443 int port
= default_port
;
444 SplitHostPort(std::string(pszDest
), port
, host
);
445 connected
= ConnectThroughProxy(proxy
, host
, port
, hSocket
, nConnectTimeout
, nullptr);
448 CloseSocket(hSocket
);
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);
462 void CConnman::DumpBanlist()
464 SweepBanned(); // clean unused entries (if bantime has expired)
466 if (!BannedSetIsDirty())
469 int64_t nStart
= GetTimeMillis();
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()
486 if (hSocket
!= INVALID_SOCKET
)
488 LogPrint(BCLog::NET
, "disconnecting peer=%d\n", id
);
489 CloseSocket(hSocket
);
493 void CConnman::ClearBanned()
498 setBannedIsDirty
= true;
500 DumpBanlist(); //store banlist to disk
502 clientInterface
->BannedListChanged();
505 bool CConnman::IsBanned(CNetAddr ip
)
508 for (const auto& it
: setBanned
) {
509 CSubNet subNet
= it
.first
;
510 CBanEntry banEntry
= it
.second
;
512 if (subNet
.Match(ip
) && GetTime() < banEntry
.nBanUntil
) {
519 bool CConnman::IsBanned(CSubNet subnet
)
522 banmap_t::iterator i
= setBanned
.find(subnet
);
523 if (i
!= setBanned
.end())
525 CBanEntry banEntry
= (*i
).second
;
526 if (GetTime() < banEntry
.nBanUntil
) {
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
;
550 if (setBanned
[subNet
].nBanUntil
< banEntry
.nBanUntil
) {
551 setBanned
[subNet
] = banEntry
;
552 setBannedIsDirty
= true;
558 clientInterface
->BannedListChanged();
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
) {
578 if (!setBanned
.erase(subNet
))
580 setBannedIsDirty
= true;
583 clientInterface
->BannedListChanged();
584 DumpBanlist(); //store banlist to disk immediately
588 void CConnman::GetBanned(banmap_t
&banMap
)
591 // Sweep the banlist so expired bans are not returned
593 banMap
= setBanned
; //create a thread safe copy
596 void CConnman::SetBanned(const banmap_t
&banMap
)
600 setBannedIsDirty
= true;
603 void CConnman::SweepBanned()
605 int64_t now
= GetTime();
606 bool notifyUI
= false;
609 banmap_t::iterator it
= setBanned
.begin();
610 while(it
!= setBanned
.end())
612 CSubNet subNet
= (*it
).first
;
613 CBanEntry banEntry
= (*it
).second
;
614 if(now
> banEntry
.nBanUntil
)
616 setBanned
.erase(it
++);
617 setBannedIsDirty
= true;
619 LogPrint(BCLog::NET
, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__
, subNet
.ToString());
626 if(notifyUI
&& clientInterface
) {
627 clientInterface
->BannedListChanged();
631 bool CConnman::BannedSetIsDirty()
634 return setBannedIsDirty
;
637 void CConnman::SetBannedSetDirty(bool dirty
)
639 LOCK(cs_setBanned
); //reuse setBanned lock for the isDirty flag
640 setBannedIsDirty
= dirty
;
644 bool CConnman::IsWhitelistedRange(const CNetAddr
&addr
) {
645 for (const CSubNet
& subnet
: vWhitelistedRange
) {
646 if (subnet
.Match(addr
))
652 std::string
CNode::GetAddrName() const {
657 void CNode::MaybeSetAddrName(const std::string
& addrNameIn
) {
659 if (addrName
.empty()) {
660 addrName
= addrNameIn
;
664 CService
CNode::GetAddrLocal() const {
669 void CNode::SetAddrLocal(const CService
& addrLocalIn
) {
671 if (addrLocal
.IsValid()) {
672 error("Addr local already set for node: %i. Refusing to change from %s to %s", id
, addrLocal
.ToString(), addrLocalIn
.ToString());
674 addrLocal
= addrLocalIn
;
679 #define X(name) stats.name = name
680 void CNode::copyStats(CNodeStats
&stats
)
682 stats
.nodeid
= this->GetId();
694 stats
.addrName
= GetAddrName();
701 X(m_manual_connection
);
705 X(mapSendBytesPerMsgCmd
);
710 X(mapRecvBytesPerMsgCmd
);
715 // It is common for nodes with good ping times to suddenly become lagged,
716 // due to a new block arriving or other large transfer.
717 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
718 // since pingtime does not update until the ping is complete, which might take a while.
719 // So, if a ping is taking an unusually long time in flight,
720 // the caller can immediately detect that this is happening.
721 int64_t nPingUsecWait
= 0;
722 if ((0 != nPingNonceSent
) && (0 != nPingUsecStart
)) {
723 nPingUsecWait
= GetTimeMicros() - nPingUsecStart
;
726 // 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 :)
727 stats
.dPingTime
= (((double)nPingUsecTime
) / 1e6
);
728 stats
.dMinPing
= (((double)nMinPingUsecTime
) / 1e6
);
729 stats
.dPingWait
= (((double)nPingUsecWait
) / 1e6
);
731 // Leave string empty if addrLocal invalid (not filled in yet)
732 CService addrLocalUnlocked
= GetAddrLocal();
733 stats
.addrLocal
= addrLocalUnlocked
.IsValid() ? addrLocalUnlocked
.ToString() : "";
737 bool CNode::ReceiveMsgBytes(const char *pch
, unsigned int nBytes
, bool& complete
)
740 int64_t nTimeMicros
= GetTimeMicros();
742 nLastRecv
= nTimeMicros
/ 1000000;
743 nRecvBytes
+= nBytes
;
746 // get current incomplete message, or create a new one
747 if (vRecvMsg
.empty() ||
748 vRecvMsg
.back().complete())
749 vRecvMsg
.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK
, INIT_PROTO_VERSION
));
751 CNetMessage
& msg
= vRecvMsg
.back();
753 // absorb network data
756 handled
= msg
.readHeader(pch
, nBytes
);
758 handled
= msg
.readData(pch
, nBytes
);
763 if (msg
.in_data
&& msg
.hdr
.nMessageSize
> MAX_PROTOCOL_MESSAGE_LENGTH
) {
764 LogPrint(BCLog::NET
, "Oversized message from peer=%i, disconnecting\n", GetId());
771 if (msg
.complete()) {
773 //store received bytes per message command
774 //to prevent a memory DOS, only allow valid commands
775 mapMsgCmdSize::iterator i
= mapRecvBytesPerMsgCmd
.find(msg
.hdr
.pchCommand
);
776 if (i
== mapRecvBytesPerMsgCmd
.end())
777 i
= mapRecvBytesPerMsgCmd
.find(NET_MESSAGE_COMMAND_OTHER
);
778 assert(i
!= mapRecvBytesPerMsgCmd
.end());
779 i
->second
+= msg
.hdr
.nMessageSize
+ CMessageHeader::HEADER_SIZE
;
781 msg
.nTime
= nTimeMicros
;
789 void CNode::SetSendVersion(int nVersionIn
)
791 // Send version may only be changed in the version message, and
792 // only one version message is allowed per session. We can therefore
793 // treat this value as const and even atomic as long as it's only used
794 // once a version message has been successfully processed. Any attempt to
795 // set this twice is an error.
796 if (nSendVersion
!= 0) {
797 error("Send version already set for node: %i. Refusing to change from %i to %i", id
, nSendVersion
, nVersionIn
);
799 nSendVersion
= nVersionIn
;
803 int CNode::GetSendVersion() const
805 // The send version should always be explicitly set to
806 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
808 if (nSendVersion
== 0) {
809 error("Requesting unset send version for node: %i. Using %i", id
, INIT_PROTO_VERSION
);
810 return INIT_PROTO_VERSION
;
816 int CNetMessage::readHeader(const char *pch
, unsigned int nBytes
)
818 // copy data to temporary parsing buffer
819 unsigned int nRemaining
= 24 - nHdrPos
;
820 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
822 memcpy(&hdrbuf
[nHdrPos
], pch
, nCopy
);
825 // if header incomplete, exit
829 // deserialize to CMessageHeader
833 catch (const std::exception
&) {
837 // reject messages larger than MAX_SIZE
838 if (hdr
.nMessageSize
> MAX_SIZE
)
841 // switch state to reading message data
847 int CNetMessage::readData(const char *pch
, unsigned int nBytes
)
849 unsigned int nRemaining
= hdr
.nMessageSize
- nDataPos
;
850 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
852 if (vRecv
.size() < nDataPos
+ nCopy
) {
853 // Allocate up to 256 KiB ahead, but never more than the total message size.
854 vRecv
.resize(std::min(hdr
.nMessageSize
, nDataPos
+ nCopy
+ 256 * 1024));
857 hasher
.Write((const unsigned char*)pch
, nCopy
);
858 memcpy(&vRecv
[nDataPos
], pch
, nCopy
);
864 const uint256
& CNetMessage::GetMessageHash() const
867 if (data_hash
.IsNull())
868 hasher
.Finalize(data_hash
.begin());
880 // requires LOCK(cs_vSend)
881 size_t CConnman::SocketSendData(CNode
*pnode
) const
883 auto it
= pnode
->vSendMsg
.begin();
884 size_t nSentSize
= 0;
886 while (it
!= pnode
->vSendMsg
.end()) {
887 const auto &data
= *it
;
888 assert(data
.size() > pnode
->nSendOffset
);
891 LOCK(pnode
->cs_hSocket
);
892 if (pnode
->hSocket
== INVALID_SOCKET
)
894 nBytes
= send(pnode
->hSocket
, reinterpret_cast<const char*>(data
.data()) + pnode
->nSendOffset
, data
.size() - pnode
->nSendOffset
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
897 pnode
->nLastSend
= GetSystemTimeInSeconds();
898 pnode
->nSendBytes
+= nBytes
;
899 pnode
->nSendOffset
+= nBytes
;
901 if (pnode
->nSendOffset
== data
.size()) {
902 pnode
->nSendOffset
= 0;
903 pnode
->nSendSize
-= data
.size();
904 pnode
->fPauseSend
= pnode
->nSendSize
> nSendBufferMaxSize
;
907 // could not send full message; stop sending more
913 int nErr
= WSAGetLastError();
914 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
916 LogPrintf("socket send error %s\n", NetworkErrorString(nErr
));
917 pnode
->CloseSocketDisconnect();
920 // couldn't send anything at all
925 if (it
== pnode
->vSendMsg
.end()) {
926 assert(pnode
->nSendOffset
== 0);
927 assert(pnode
->nSendSize
== 0);
929 pnode
->vSendMsg
.erase(pnode
->vSendMsg
.begin(), it
);
933 struct NodeEvictionCandidate
936 int64_t nTimeConnected
;
937 int64_t nMinPingUsecTime
;
938 int64_t nLastBlockTime
;
940 bool fRelevantServices
;
944 uint64_t nKeyedNetGroup
;
947 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
949 return a
.nMinPingUsecTime
> b
.nMinPingUsecTime
;
952 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
954 return a
.nTimeConnected
> b
.nTimeConnected
;
957 static bool CompareNetGroupKeyed(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
) {
958 return a
.nKeyedNetGroup
< b
.nKeyedNetGroup
;
961 static bool CompareNodeBlockTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
963 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
964 if (a
.nLastBlockTime
!= b
.nLastBlockTime
) return a
.nLastBlockTime
< b
.nLastBlockTime
;
965 if (a
.fRelevantServices
!= b
.fRelevantServices
) return b
.fRelevantServices
;
966 return a
.nTimeConnected
> b
.nTimeConnected
;
969 static bool CompareNodeTXTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
971 // 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.
972 if (a
.nLastTXTime
!= b
.nLastTXTime
) return a
.nLastTXTime
< b
.nLastTXTime
;
973 if (a
.fRelayTxes
!= b
.fRelayTxes
) return b
.fRelayTxes
;
974 if (a
.fBloomFilter
!= b
.fBloomFilter
) return a
.fBloomFilter
;
975 return a
.nTimeConnected
> b
.nTimeConnected
;
979 //! Sort an array by the specified comparator, then erase the last K elements.
980 template<typename T
, typename Comparator
>
981 static void EraseLastKElements(std::vector
<T
> &elements
, Comparator comparator
, size_t k
)
983 std::sort(elements
.begin(), elements
.end(), comparator
);
984 size_t eraseSize
= std::min(k
, elements
.size());
985 elements
.erase(elements
.end() - eraseSize
, elements
.end());
988 /** Try to find a connection to evict when the node is full.
989 * Extreme care must be taken to avoid opening the node to attacker
990 * triggered network partitioning.
991 * The strategy used here is to protect a small number of peers
992 * for each of several distinct characteristics which are difficult
993 * to forge. In order to partition a node the attacker must be
994 * simultaneously better at all of them than honest peers.
996 bool CConnman::AttemptToEvictConnection()
998 std::vector
<NodeEvictionCandidate
> vEvictionCandidates
;
1002 for (const CNode
* node
: vNodes
) {
1003 if (node
->fWhitelisted
)
1005 if (!node
->fInbound
)
1007 if (node
->fDisconnect
)
1009 NodeEvictionCandidate candidate
= {node
->GetId(), node
->nTimeConnected
, node
->nMinPingUsecTime
,
1010 node
->nLastBlockTime
, node
->nLastTXTime
,
1011 HasAllDesirableServiceFlags(node
->nServices
),
1012 node
->fRelayTxes
, node
->pfilter
!= nullptr, node
->addr
, node
->nKeyedNetGroup
};
1013 vEvictionCandidates
.push_back(candidate
);
1017 // Protect connections with certain characteristics
1019 // Deterministically select 4 peers to protect by netgroup.
1020 // An attacker cannot predict which netgroups will be protected
1021 EraseLastKElements(vEvictionCandidates
, CompareNetGroupKeyed
, 4);
1022 // Protect the 8 nodes with the lowest minimum ping time.
1023 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
1024 EraseLastKElements(vEvictionCandidates
, ReverseCompareNodeMinPingTime
, 8);
1025 // Protect 4 nodes that most recently sent us transactions.
1026 // An attacker cannot manipulate this metric without performing useful work.
1027 EraseLastKElements(vEvictionCandidates
, CompareNodeTXTime
, 4);
1028 // Protect 4 nodes that most recently sent us blocks.
1029 // An attacker cannot manipulate this metric without performing useful work.
1030 EraseLastKElements(vEvictionCandidates
, CompareNodeBlockTime
, 4);
1031 // Protect the half of the remaining nodes which have been connected the longest.
1032 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1033 EraseLastKElements(vEvictionCandidates
, ReverseCompareNodeTimeConnected
, vEvictionCandidates
.size() / 2);
1035 if (vEvictionCandidates
.empty()) return false;
1037 // Identify the network group with the most connections and youngest member.
1038 // (vEvictionCandidates is already sorted by reverse connect time)
1039 uint64_t naMostConnections
;
1040 unsigned int nMostConnections
= 0;
1041 int64_t nMostConnectionsTime
= 0;
1042 std::map
<uint64_t, std::vector
<NodeEvictionCandidate
> > mapNetGroupNodes
;
1043 for (const NodeEvictionCandidate
&node
: vEvictionCandidates
) {
1044 std::vector
<NodeEvictionCandidate
> &group
= mapNetGroupNodes
[node
.nKeyedNetGroup
];
1045 group
.push_back(node
);
1046 int64_t grouptime
= group
[0].nTimeConnected
;
1048 if (group
.size() > nMostConnections
|| (group
.size() == nMostConnections
&& grouptime
> nMostConnectionsTime
)) {
1049 nMostConnections
= group
.size();
1050 nMostConnectionsTime
= grouptime
;
1051 naMostConnections
= node
.nKeyedNetGroup
;
1055 // Reduce to the network group with the most connections
1056 vEvictionCandidates
= std::move(mapNetGroupNodes
[naMostConnections
]);
1058 // Disconnect from the network group with the most connections
1059 NodeId evicted
= vEvictionCandidates
.front().id
;
1061 for (CNode
* pnode
: vNodes
) {
1062 if (pnode
->GetId() == evicted
) {
1063 pnode
->fDisconnect
= true;
1070 void CConnman::AcceptConnection(const ListenSocket
& hListenSocket
) {
1071 struct sockaddr_storage sockaddr
;
1072 socklen_t len
= sizeof(sockaddr
);
1073 SOCKET hSocket
= accept(hListenSocket
.socket
, (struct sockaddr
*)&sockaddr
, &len
);
1076 int nMaxInbound
= nMaxConnections
- (nMaxOutbound
+ nMaxFeeler
);
1078 if (hSocket
!= INVALID_SOCKET
) {
1079 if (!addr
.SetSockAddr((const struct sockaddr
*)&sockaddr
)) {
1080 LogPrintf("Warning: Unknown socket family\n");
1084 bool whitelisted
= hListenSocket
.whitelisted
|| IsWhitelistedRange(addr
);
1087 for (const CNode
* pnode
: vNodes
) {
1088 if (pnode
->fInbound
) nInbound
++;
1092 if (hSocket
== INVALID_SOCKET
)
1094 int nErr
= WSAGetLastError();
1095 if (nErr
!= WSAEWOULDBLOCK
)
1096 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr
));
1100 if (!fNetworkActive
) {
1101 LogPrintf("connection from %s dropped: not accepting new connections\n", addr
.ToString());
1102 CloseSocket(hSocket
);
1106 if (!IsSelectableSocket(hSocket
))
1108 LogPrintf("connection from %s dropped: non-selectable socket\n", addr
.ToString());
1109 CloseSocket(hSocket
);
1113 // According to the internet TCP_NODELAY is not carried into accepted sockets
1114 // on all platforms. Set it again here just to be sure.
1115 SetSocketNoDelay(hSocket
);
1117 if (IsBanned(addr
) && !whitelisted
)
1119 LogPrint(BCLog::NET
, "connection from %s dropped (banned)\n", addr
.ToString());
1120 CloseSocket(hSocket
);
1124 if (nInbound
>= nMaxInbound
)
1126 if (!AttemptToEvictConnection()) {
1127 // No connection to evict, disconnect the new connection
1128 LogPrint(BCLog::NET
, "failed to find an eviction candidate - connection dropped (full)\n");
1129 CloseSocket(hSocket
);
1134 NodeId id
= GetNewNodeId();
1135 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
1136 CAddress addr_bind
= GetBindAddress(hSocket
);
1138 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addr
, CalculateKeyedNetGroup(addr
), nonce
, addr_bind
, "", true);
1140 pnode
->fWhitelisted
= whitelisted
;
1141 m_msgproc
->InitializeNode(pnode
);
1143 LogPrint(BCLog::NET
, "connection from %s accepted\n", addr
.ToString());
1147 vNodes
.push_back(pnode
);
1151 void CConnman::ThreadSocketHandler()
1153 unsigned int nPrevNodeCount
= 0;
1154 while (!interruptNet
)
1161 // Disconnect unused nodes
1162 std::vector
<CNode
*> vNodesCopy
= vNodes
;
1163 for (CNode
* pnode
: vNodesCopy
)
1165 if (pnode
->fDisconnect
)
1167 // remove from vNodes
1168 vNodes
.erase(remove(vNodes
.begin(), vNodes
.end(), pnode
), vNodes
.end());
1170 // release outbound grant (if any)
1171 pnode
->grantOutbound
.Release();
1173 // close socket and cleanup
1174 pnode
->CloseSocketDisconnect();
1176 // hold in disconnected pool until all refs are released
1178 vNodesDisconnected
.push_back(pnode
);
1183 // Delete disconnected nodes
1184 std::list
<CNode
*> vNodesDisconnectedCopy
= vNodesDisconnected
;
1185 for (CNode
* pnode
: vNodesDisconnectedCopy
)
1187 // wait until threads are done using it
1188 if (pnode
->GetRefCount() <= 0) {
1189 bool fDelete
= false;
1191 TRY_LOCK(pnode
->cs_inventory
, lockInv
);
1193 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1200 vNodesDisconnected
.remove(pnode
);
1209 vNodesSize
= vNodes
.size();
1211 if(vNodesSize
!= nPrevNodeCount
) {
1212 nPrevNodeCount
= vNodesSize
;
1214 clientInterface
->NotifyNumConnectionsChanged(nPrevNodeCount
);
1218 // Find which sockets have data to receive
1220 struct timeval timeout
;
1222 timeout
.tv_usec
= 50000; // frequency to poll pnode->vSend
1227 FD_ZERO(&fdsetRecv
);
1228 FD_ZERO(&fdsetSend
);
1229 FD_ZERO(&fdsetError
);
1230 SOCKET hSocketMax
= 0;
1231 bool have_fds
= false;
1233 for (const ListenSocket
& hListenSocket
: vhListenSocket
) {
1234 FD_SET(hListenSocket
.socket
, &fdsetRecv
);
1235 hSocketMax
= std::max(hSocketMax
, hListenSocket
.socket
);
1241 for (CNode
* pnode
: vNodes
)
1243 // Implement the following logic:
1244 // * If there is data to send, select() for sending data. As this only
1245 // happens when optimistic write failed, we choose to first drain the
1246 // write buffer in this case before receiving more. This avoids
1247 // needlessly queueing received data, if the remote peer is not themselves
1248 // receiving data. This means properly utilizing TCP flow control signalling.
1249 // * Otherwise, if there is space left in the receive buffer, select() for
1251 // * Hand off all complete messages to the processor, to be handled without
1254 bool select_recv
= !pnode
->fPauseRecv
;
1257 LOCK(pnode
->cs_vSend
);
1258 select_send
= !pnode
->vSendMsg
.empty();
1261 LOCK(pnode
->cs_hSocket
);
1262 if (pnode
->hSocket
== INVALID_SOCKET
)
1265 FD_SET(pnode
->hSocket
, &fdsetError
);
1266 hSocketMax
= std::max(hSocketMax
, pnode
->hSocket
);
1270 FD_SET(pnode
->hSocket
, &fdsetSend
);
1274 FD_SET(pnode
->hSocket
, &fdsetRecv
);
1279 int nSelect
= select(have_fds
? hSocketMax
+ 1 : 0,
1280 &fdsetRecv
, &fdsetSend
, &fdsetError
, &timeout
);
1284 if (nSelect
== SOCKET_ERROR
)
1288 int nErr
= WSAGetLastError();
1289 LogPrintf("socket select error %s\n", NetworkErrorString(nErr
));
1290 for (unsigned int i
= 0; i
<= hSocketMax
; i
++)
1291 FD_SET(i
, &fdsetRecv
);
1293 FD_ZERO(&fdsetSend
);
1294 FD_ZERO(&fdsetError
);
1295 if (!interruptNet
.sleep_for(std::chrono::milliseconds(timeout
.tv_usec
/1000)))
1300 // Accept new connections
1302 for (const ListenSocket
& hListenSocket
: vhListenSocket
)
1304 if (hListenSocket
.socket
!= INVALID_SOCKET
&& FD_ISSET(hListenSocket
.socket
, &fdsetRecv
))
1306 AcceptConnection(hListenSocket
);
1311 // Service each socket
1313 std::vector
<CNode
*> vNodesCopy
;
1316 vNodesCopy
= vNodes
;
1317 for (CNode
* pnode
: vNodesCopy
)
1320 for (CNode
* pnode
: vNodesCopy
)
1328 bool recvSet
= false;
1329 bool sendSet
= false;
1330 bool errorSet
= false;
1332 LOCK(pnode
->cs_hSocket
);
1333 if (pnode
->hSocket
== INVALID_SOCKET
)
1335 recvSet
= FD_ISSET(pnode
->hSocket
, &fdsetRecv
);
1336 sendSet
= FD_ISSET(pnode
->hSocket
, &fdsetSend
);
1337 errorSet
= FD_ISSET(pnode
->hSocket
, &fdsetError
);
1339 if (recvSet
|| errorSet
)
1341 // typical socket buffer is 8K-64K
1342 char pchBuf
[0x10000];
1345 LOCK(pnode
->cs_hSocket
);
1346 if (pnode
->hSocket
== INVALID_SOCKET
)
1348 nBytes
= recv(pnode
->hSocket
, pchBuf
, sizeof(pchBuf
), MSG_DONTWAIT
);
1352 bool notify
= false;
1353 if (!pnode
->ReceiveMsgBytes(pchBuf
, nBytes
, notify
))
1354 pnode
->CloseSocketDisconnect();
1355 RecordBytesRecv(nBytes
);
1357 size_t nSizeAdded
= 0;
1358 auto it(pnode
->vRecvMsg
.begin());
1359 for (; it
!= pnode
->vRecvMsg
.end(); ++it
) {
1360 if (!it
->complete())
1362 nSizeAdded
+= it
->vRecv
.size() + CMessageHeader::HEADER_SIZE
;
1365 LOCK(pnode
->cs_vProcessMsg
);
1366 pnode
->vProcessMsg
.splice(pnode
->vProcessMsg
.end(), pnode
->vRecvMsg
, pnode
->vRecvMsg
.begin(), it
);
1367 pnode
->nProcessQueueSize
+= nSizeAdded
;
1368 pnode
->fPauseRecv
= pnode
->nProcessQueueSize
> nReceiveFloodSize
;
1370 WakeMessageHandler();
1373 else if (nBytes
== 0)
1375 // socket closed gracefully
1376 if (!pnode
->fDisconnect
) {
1377 LogPrint(BCLog::NET
, "socket closed\n");
1379 pnode
->CloseSocketDisconnect();
1381 else if (nBytes
< 0)
1384 int nErr
= WSAGetLastError();
1385 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
1387 if (!pnode
->fDisconnect
)
1388 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr
));
1389 pnode
->CloseSocketDisconnect();
1399 LOCK(pnode
->cs_vSend
);
1400 size_t nBytes
= SocketSendData(pnode
);
1402 RecordBytesSent(nBytes
);
1407 // Inactivity checking
1409 int64_t nTime
= GetSystemTimeInSeconds();
1410 if (nTime
- pnode
->nTimeConnected
> 60)
1412 if (pnode
->nLastRecv
== 0 || pnode
->nLastSend
== 0)
1414 LogPrint(BCLog::NET
, "socket no message in first 60 seconds, %d %d from %d\n", pnode
->nLastRecv
!= 0, pnode
->nLastSend
!= 0, pnode
->GetId());
1415 pnode
->fDisconnect
= true;
1417 else if (nTime
- pnode
->nLastSend
> TIMEOUT_INTERVAL
)
1419 LogPrintf("socket sending timeout: %is\n", nTime
- pnode
->nLastSend
);
1420 pnode
->fDisconnect
= true;
1422 else if (nTime
- pnode
->nLastRecv
> (pnode
->nVersion
> BIP0031_VERSION
? TIMEOUT_INTERVAL
: 90*60))
1424 LogPrintf("socket receive timeout: %is\n", nTime
- pnode
->nLastRecv
);
1425 pnode
->fDisconnect
= true;
1427 else if (pnode
->nPingNonceSent
&& pnode
->nPingUsecStart
+ TIMEOUT_INTERVAL
* 1000000 < GetTimeMicros())
1429 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode
->nPingUsecStart
));
1430 pnode
->fDisconnect
= true;
1432 else if (!pnode
->fSuccessfullyConnected
)
1434 LogPrintf("version handshake timeout from %d\n", pnode
->GetId());
1435 pnode
->fDisconnect
= true;
1441 for (CNode
* pnode
: vNodesCopy
)
1447 void CConnman::WakeMessageHandler()
1450 std::lock_guard
<std::mutex
> lock(mutexMsgProc
);
1451 fMsgProcWake
= true;
1453 condMsgProc
.notify_one();
1462 void ThreadMapPort()
1464 std::string port
= strprintf("%u", GetListenPort());
1465 const char * multicastif
= nullptr;
1466 const char * minissdpdpath
= nullptr;
1467 struct UPNPDev
* devlist
= nullptr;
1470 #ifndef UPNPDISCOVER_SUCCESS
1472 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0);
1473 #elif MINIUPNPC_API_VERSION < 14
1476 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, &error
);
1478 /* miniupnpc 1.9.20150730 */
1480 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, 2, &error
);
1483 struct UPNPUrls urls
;
1484 struct IGDdatas data
;
1487 r
= UPNP_GetValidIGD(devlist
, &urls
, &data
, lanaddr
, sizeof(lanaddr
));
1491 char externalIPAddress
[40];
1492 r
= UPNP_GetExternalIPAddress(urls
.controlURL
, data
.first
.servicetype
, externalIPAddress
);
1493 if(r
!= UPNPCOMMAND_SUCCESS
)
1494 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r
);
1497 if(externalIPAddress
[0])
1500 if(LookupHost(externalIPAddress
, resolved
, false)) {
1501 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved
.ToString().c_str());
1502 AddLocal(resolved
, LOCAL_UPNP
);
1506 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1510 std::string strDesc
= "Bitcoin " + FormatFullVersion();
1514 #ifndef UPNPDISCOVER_SUCCESS
1516 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1517 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0);
1520 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1521 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0, "0");
1524 if(r
!=UPNPCOMMAND_SUCCESS
)
1525 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1526 port
, port
, lanaddr
, r
, strupnperror(r
));
1528 LogPrintf("UPnP Port Mapping successful.\n");
1530 MilliSleep(20*60*1000); // Refresh every 20 minutes
1533 catch (const boost::thread_interrupted
&)
1535 r
= UPNP_DeletePortMapping(urls
.controlURL
, data
.first
.servicetype
, port
.c_str(), "TCP", 0);
1536 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r
);
1537 freeUPNPDevlist(devlist
); devlist
= nullptr;
1538 FreeUPNPUrls(&urls
);
1542 LogPrintf("No valid UPnP IGDs found\n");
1543 freeUPNPDevlist(devlist
); devlist
= nullptr;
1545 FreeUPNPUrls(&urls
);
1549 void MapPort(bool fUseUPnP
)
1551 static std::unique_ptr
<boost::thread
> upnp_thread
;
1556 upnp_thread
->interrupt();
1557 upnp_thread
->join();
1559 upnp_thread
.reset(new boost::thread(boost::bind(&TraceThread
<void (*)()>, "upnp", &ThreadMapPort
)));
1561 else if (upnp_thread
) {
1562 upnp_thread
->interrupt();
1563 upnp_thread
->join();
1564 upnp_thread
.reset();
1571 // Intentionally left blank.
1580 static std::string
GetDNSHost(const CDNSSeedData
& data
, ServiceFlags
* requiredServiceBits
)
1582 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1583 if (!data
.supportsServiceBitsFiltering
|| *requiredServiceBits
== NODE_NETWORK
) {
1584 *requiredServiceBits
= NODE_NETWORK
;
1588 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1589 return strprintf("x%x.%s", *requiredServiceBits
, data
.host
);
1593 void CConnman::ThreadDNSAddressSeed()
1595 // goal: only query DNS seeds if address need is acute
1596 // Avoiding DNS seeds when we don't need them improves user privacy by
1597 // creating fewer identifying DNS requests, reduces trust by giving seeds
1598 // less influence on the network topology, and reduces traffic to the seeds.
1599 if ((addrman
.size() > 0) &&
1600 (!gArgs
.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED
))) {
1601 if (!interruptNet
.sleep_for(std::chrono::seconds(11)))
1606 for (auto pnode
: vNodes
) {
1607 nRelevant
+= pnode
->fSuccessfullyConnected
&& !pnode
->fFeeler
&& !pnode
->fOneShot
&& !pnode
->m_manual_connection
&& !pnode
->fInbound
;
1609 if (nRelevant
>= 2) {
1610 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1615 const std::vector
<CDNSSeedData
> &vSeeds
= Params().DNSSeeds();
1618 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1620 for (const CDNSSeedData
&seed
: vSeeds
) {
1624 if (HaveNameProxy()) {
1625 AddOneShot(seed
.host
);
1627 std::vector
<CNetAddr
> vIPs
;
1628 std::vector
<CAddress
> vAdd
;
1629 ServiceFlags requiredServiceBits
= GetDesirableServiceFlags(NODE_NONE
);
1630 std::string host
= GetDNSHost(seed
, &requiredServiceBits
);
1631 CNetAddr resolveSource
;
1632 if (!resolveSource
.SetInternal(host
)) {
1635 if (LookupHost(host
.c_str(), vIPs
, 0, true))
1637 for (const CNetAddr
& ip
: vIPs
)
1639 int nOneDay
= 24*3600;
1640 CAddress addr
= CAddress(CService(ip
, Params().GetDefaultPort()), requiredServiceBits
);
1641 addr
.nTime
= GetTime() - 3*nOneDay
- GetRand(4*nOneDay
); // use a random age between 3 and 7 days old
1642 vAdd
.push_back(addr
);
1645 addrman
.Add(vAdd
, resolveSource
);
1650 LogPrintf("%d addresses found from DNS seeds\n", found
);
1664 void CConnman::DumpAddresses()
1666 int64_t nStart
= GetTimeMillis();
1671 LogPrint(BCLog::NET
, "Flushed %d addresses to peers.dat %dms\n",
1672 addrman
.size(), GetTimeMillis() - nStart
);
1675 void CConnman::DumpData()
1681 void CConnman::ProcessOneShot()
1683 std::string strDest
;
1686 if (vOneShots
.empty())
1688 strDest
= vOneShots
.front();
1689 vOneShots
.pop_front();
1692 CSemaphoreGrant
grant(*semOutbound
, true);
1694 if (!OpenNetworkConnection(addr
, false, &grant
, strDest
.c_str(), true))
1695 AddOneShot(strDest
);
1699 bool CConnman::GetTryNewOutboundPeer()
1701 return m_try_another_outbound_peer
;
1704 void CConnman::SetTryNewOutboundPeer(bool flag
)
1706 m_try_another_outbound_peer
= flag
;
1707 LogPrint(BCLog::NET
, "net: setting try another outbound peer=%s\n", flag
? "true" : "false");
1710 // Return the number of peers we have over our outbound connection limit
1711 // Exclude peers that are marked for disconnect, or are going to be
1712 // disconnected soon (eg one-shots and feelers)
1713 // Also exclude peers that haven't finished initial connection handshake yet
1714 // (so that we don't decide we're over our desired connection limit, and then
1715 // evict some peer that has finished the handshake)
1716 int CConnman::GetExtraOutboundCount()
1721 for (CNode
* pnode
: vNodes
) {
1722 if (!pnode
->fInbound
&& !pnode
->m_manual_connection
&& !pnode
->fFeeler
&& !pnode
->fDisconnect
&& !pnode
->fOneShot
&& pnode
->fSuccessfullyConnected
) {
1727 return std::max(nOutbound
- nMaxOutbound
, 0);
1730 void CConnman::ThreadOpenConnections(const std::vector
<std::string
> connect
)
1732 // Connect to specific addresses
1733 if (!connect
.empty())
1735 for (int64_t nLoop
= 0;; nLoop
++)
1738 for (const std::string
& strAddr
: connect
)
1740 CAddress
addr(CService(), NODE_NONE
);
1741 OpenNetworkConnection(addr
, false, nullptr, strAddr
.c_str(), false, false, true);
1742 for (int i
= 0; i
< 10 && i
< nLoop
; i
++)
1744 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1748 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1753 // Initiate network connections
1754 int64_t nStart
= GetTime();
1756 // Minimum time before next feeler connection (in microseconds).
1757 int64_t nNextFeeler
= PoissonNextSend(nStart
*1000*1000, FEELER_INTERVAL
);
1758 while (!interruptNet
)
1762 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1765 CSemaphoreGrant
grant(*semOutbound
);
1769 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1770 if (addrman
.size() == 0 && (GetTime() - nStart
> 60)) {
1771 static bool done
= false;
1773 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1775 local
.SetInternal("fixedseeds");
1776 addrman
.Add(convertSeed6(Params().FixedSeeds()), local
);
1782 // Choose an address to connect to based on most recently seen
1784 CAddress addrConnect
;
1786 // Only connect out to one peer per network group (/16 for IPv4).
1787 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1789 std::set
<std::vector
<unsigned char> > setConnected
;
1792 for (CNode
* pnode
: vNodes
) {
1793 if (!pnode
->fInbound
&& !pnode
->m_manual_connection
) {
1794 // Netgroups for inbound and addnode peers are not excluded because our goal here
1795 // is to not use multiple of our limited outbound slots on a single netgroup
1796 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1797 // also have the added issue that they're attacker controlled and could be used
1798 // to prevent us from connecting to particular hosts if we used them here.
1799 setConnected
.insert(pnode
->addr
.GetGroup());
1805 // Feeler Connections
1808 // * Increase the number of connectable addresses in the tried table.
1811 // * Choose a random address from new and attempt to connect to it if we can connect
1812 // successfully it is added to tried.
1813 // * Start attempting feeler connections only after node finishes making outbound
1815 // * Only make a feeler connection once every few minutes.
1817 bool fFeeler
= false;
1819 if (nOutbound
>= nMaxOutbound
&& !GetTryNewOutboundPeer()) {
1820 int64_t nTime
= GetTimeMicros(); // The current time right now (in microseconds).
1821 if (nTime
> nNextFeeler
) {
1822 nNextFeeler
= PoissonNextSend(nTime
, FEELER_INTERVAL
);
1829 int64_t nANow
= GetAdjustedTime();
1831 while (!interruptNet
)
1833 CAddrInfo addr
= addrman
.Select(fFeeler
);
1835 // if we selected an invalid address, restart
1836 if (!addr
.IsValid() || setConnected
.count(addr
.GetGroup()) || IsLocal(addr
))
1839 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1840 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1841 // already-connected network ranges, ...) before trying new addrman addresses.
1846 if (IsLimited(addr
))
1849 // only consider very recently tried nodes after 30 failed attempts
1850 if (nANow
- addr
.nLastTry
< 600 && nTries
< 30)
1853 // for non-feelers, require all the services we'll want,
1854 // for feelers, only require they be a full node (only because most
1855 // SPV clients don't have a good address DB available)
1856 if (!fFeeler
&& !HasAllDesirableServiceFlags(addr
.nServices
)) {
1858 } else if (fFeeler
&& !MayHaveUsefulAddressDB(addr
.nServices
)) {
1862 // do not allow non-default ports, unless after 50 invalid addresses selected already
1863 if (addr
.GetPort() != Params().GetDefaultPort() && nTries
< 50)
1870 if (addrConnect
.IsValid()) {
1873 // Add small amount of random noise before connection to avoid synchronization.
1874 int randsleep
= GetRandInt(FEELER_SLEEP_WINDOW
* 1000);
1875 if (!interruptNet
.sleep_for(std::chrono::milliseconds(randsleep
)))
1877 LogPrint(BCLog::NET
, "Making feeler connection to %s\n", addrConnect
.ToString());
1880 OpenNetworkConnection(addrConnect
, (int)setConnected
.size() >= std::min(nMaxConnections
- 1, 2), &grant
, nullptr, false, fFeeler
);
1885 std::vector
<AddedNodeInfo
> CConnman::GetAddedNodeInfo()
1887 std::vector
<AddedNodeInfo
> ret
;
1889 std::list
<std::string
> lAddresses(0);
1891 LOCK(cs_vAddedNodes
);
1892 ret
.reserve(vAddedNodes
.size());
1893 std::copy(vAddedNodes
.cbegin(), vAddedNodes
.cend(), std::back_inserter(lAddresses
));
1897 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1898 std::map
<CService
, bool> mapConnected
;
1899 std::map
<std::string
, std::pair
<bool, CService
>> mapConnectedByName
;
1902 for (const CNode
* pnode
: vNodes
) {
1903 if (pnode
->addr
.IsValid()) {
1904 mapConnected
[pnode
->addr
] = pnode
->fInbound
;
1906 std::string addrName
= pnode
->GetAddrName();
1907 if (!addrName
.empty()) {
1908 mapConnectedByName
[std::move(addrName
)] = std::make_pair(pnode
->fInbound
, static_cast<const CService
&>(pnode
->addr
));
1913 for (const std::string
& strAddNode
: lAddresses
) {
1914 CService
service(LookupNumeric(strAddNode
.c_str(), Params().GetDefaultPort()));
1915 if (service
.IsValid()) {
1916 // strAddNode is an IP:port
1917 auto it
= mapConnected
.find(service
);
1918 if (it
!= mapConnected
.end()) {
1919 ret
.push_back(AddedNodeInfo
{strAddNode
, service
, true, it
->second
});
1921 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1924 // strAddNode is a name
1925 auto it
= mapConnectedByName
.find(strAddNode
);
1926 if (it
!= mapConnectedByName
.end()) {
1927 ret
.push_back(AddedNodeInfo
{strAddNode
, it
->second
.second
, true, it
->second
.first
});
1929 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1937 void CConnman::ThreadOpenAddedConnections()
1941 CSemaphoreGrant
grant(*semAddnode
);
1942 std::vector
<AddedNodeInfo
> vInfo
= GetAddedNodeInfo();
1944 for (const AddedNodeInfo
& info
: vInfo
) {
1945 if (!info
.fConnected
) {
1946 if (!grant
.TryAcquire()) {
1947 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1948 // the addednodeinfo state might change.
1952 CAddress
addr(CService(), NODE_NONE
);
1953 OpenNetworkConnection(addr
, false, &grant
, info
.strAddedNode
.c_str(), false, false, true);
1954 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1958 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1959 if (!interruptNet
.sleep_for(std::chrono::seconds(tried
? 60 : 2)))
1964 // if successful, this moves the passed grant to the constructed node
1965 bool CConnman::OpenNetworkConnection(const CAddress
& addrConnect
, bool fCountFailure
, CSemaphoreGrant
*grantOutbound
, const char *pszDest
, bool fOneShot
, bool fFeeler
, bool manual_connection
)
1968 // Initiate outbound network connection
1973 if (!fNetworkActive
) {
1977 if (IsLocal(addrConnect
) ||
1978 FindNode((CNetAddr
)addrConnect
) || IsBanned(addrConnect
) ||
1979 FindNode(addrConnect
.ToStringIPPort()))
1981 } else if (FindNode(std::string(pszDest
)))
1984 CNode
* pnode
= ConnectNode(addrConnect
, pszDest
, fCountFailure
);
1989 grantOutbound
->MoveTo(pnode
->grantOutbound
);
1991 pnode
->fOneShot
= true;
1993 pnode
->fFeeler
= true;
1994 if (manual_connection
)
1995 pnode
->m_manual_connection
= true;
1997 m_msgproc
->InitializeNode(pnode
);
2000 vNodes
.push_back(pnode
);
2006 void CConnman::ThreadMessageHandler()
2008 while (!flagInterruptMsgProc
)
2010 std::vector
<CNode
*> vNodesCopy
;
2013 vNodesCopy
= vNodes
;
2014 for (CNode
* pnode
: vNodesCopy
) {
2019 bool fMoreWork
= false;
2021 for (CNode
* pnode
: vNodesCopy
)
2023 if (pnode
->fDisconnect
)
2027 bool fMoreNodeWork
= m_msgproc
->ProcessMessages(pnode
, flagInterruptMsgProc
);
2028 fMoreWork
|= (fMoreNodeWork
&& !pnode
->fPauseSend
);
2029 if (flagInterruptMsgProc
)
2033 LOCK(pnode
->cs_sendProcessing
);
2034 m_msgproc
->SendMessages(pnode
, flagInterruptMsgProc
);
2037 if (flagInterruptMsgProc
)
2043 for (CNode
* pnode
: vNodesCopy
)
2047 std::unique_lock
<std::mutex
> lock(mutexMsgProc
);
2049 condMsgProc
.wait_until(lock
, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake
; });
2051 fMsgProcWake
= false;
2060 bool CConnman::BindListenPort(const CService
&addrBind
, std::string
& strError
, bool fWhitelisted
)
2065 // Create socket for listening for incoming connections
2066 struct sockaddr_storage sockaddr
;
2067 socklen_t len
= sizeof(sockaddr
);
2068 if (!addrBind
.GetSockAddr((struct sockaddr
*)&sockaddr
, &len
))
2070 strError
= strprintf("Error: Bind address family for %s not supported", addrBind
.ToString());
2071 LogPrintf("%s\n", strError
);
2075 SOCKET hListenSocket
= CreateSocket(addrBind
);
2076 if (hListenSocket
== INVALID_SOCKET
)
2078 strError
= strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2079 LogPrintf("%s\n", strError
);
2083 // Allow binding if the port is still in TIME_WAIT state after
2084 // the program was closed and restarted.
2085 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&nOne
, sizeof(int));
2087 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&nOne
, sizeof(int));
2090 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2091 // and enable it by default or not. Try to enable it, if possible.
2092 if (addrBind
.IsIPv6()) {
2095 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_V6ONLY
, (const char*)&nOne
, sizeof(int));
2097 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void*)&nOne
, sizeof(int));
2101 int nProtLevel
= PROTECTION_LEVEL_UNRESTRICTED
;
2102 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_PROTECTION_LEVEL
, (const char*)&nProtLevel
, sizeof(int));
2106 if (::bind(hListenSocket
, (struct sockaddr
*)&sockaddr
, len
) == SOCKET_ERROR
)
2108 int nErr
= WSAGetLastError();
2109 if (nErr
== WSAEADDRINUSE
)
2110 strError
= strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind
.ToString(), _(PACKAGE_NAME
));
2112 strError
= strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind
.ToString(), NetworkErrorString(nErr
));
2113 LogPrintf("%s\n", strError
);
2114 CloseSocket(hListenSocket
);
2117 LogPrintf("Bound to %s\n", addrBind
.ToString());
2119 // Listen for incoming connections
2120 if (listen(hListenSocket
, SOMAXCONN
) == SOCKET_ERROR
)
2122 strError
= strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2123 LogPrintf("%s\n", strError
);
2124 CloseSocket(hListenSocket
);
2128 vhListenSocket
.push_back(ListenSocket(hListenSocket
, fWhitelisted
));
2130 if (addrBind
.IsRoutable() && fDiscover
&& !fWhitelisted
)
2131 AddLocal(addrBind
, LOCAL_BIND
);
2136 void Discover(boost::thread_group
& threadGroup
)
2142 // Get local host IP
2143 char pszHostName
[256] = "";
2144 if (gethostname(pszHostName
, sizeof(pszHostName
)) != SOCKET_ERROR
)
2146 std::vector
<CNetAddr
> vaddr
;
2147 if (LookupHost(pszHostName
, vaddr
, 0, true))
2149 for (const CNetAddr
&addr
: vaddr
)
2151 if (AddLocal(addr
, LOCAL_IF
))
2152 LogPrintf("%s: %s - %s\n", __func__
, pszHostName
, addr
.ToString());
2157 // Get local host ip
2158 struct ifaddrs
* myaddrs
;
2159 if (getifaddrs(&myaddrs
) == 0)
2161 for (struct ifaddrs
* ifa
= myaddrs
; ifa
!= nullptr; ifa
= ifa
->ifa_next
)
2163 if (ifa
->ifa_addr
== nullptr) continue;
2164 if ((ifa
->ifa_flags
& IFF_UP
) == 0) continue;
2165 if (strcmp(ifa
->ifa_name
, "lo") == 0) continue;
2166 if (strcmp(ifa
->ifa_name
, "lo0") == 0) continue;
2167 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
2169 struct sockaddr_in
* s4
= (struct sockaddr_in
*)(ifa
->ifa_addr
);
2170 CNetAddr
addr(s4
->sin_addr
);
2171 if (AddLocal(addr
, LOCAL_IF
))
2172 LogPrintf("%s: IPv4 %s: %s\n", __func__
, ifa
->ifa_name
, addr
.ToString());
2174 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
2176 struct sockaddr_in6
* s6
= (struct sockaddr_in6
*)(ifa
->ifa_addr
);
2177 CNetAddr
addr(s6
->sin6_addr
);
2178 if (AddLocal(addr
, LOCAL_IF
))
2179 LogPrintf("%s: IPv6 %s: %s\n", __func__
, ifa
->ifa_name
, addr
.ToString());
2182 freeifaddrs(myaddrs
);
2187 void CConnman::SetNetworkActive(bool active
)
2189 LogPrint(BCLog::NET
, "SetNetworkActive: %s\n", active
);
2191 if (fNetworkActive
== active
) {
2195 fNetworkActive
= active
;
2197 if (!fNetworkActive
) {
2199 // Close sockets to all nodes
2200 for (CNode
* pnode
: vNodes
) {
2201 pnode
->CloseSocketDisconnect();
2205 uiInterface
.NotifyNetworkActiveChanged(fNetworkActive
);
2208 CConnman::CConnman(uint64_t nSeed0In
, uint64_t nSeed1In
) : nSeed0(nSeed0In
), nSeed1(nSeed1In
)
2210 fNetworkActive
= true;
2211 setBannedIsDirty
= false;
2212 fAddressesInitialized
= false;
2214 nSendBufferMaxSize
= 0;
2215 nReceiveFloodSize
= 0;
2216 flagInterruptMsgProc
= false;
2217 SetTryNewOutboundPeer(false);
2219 Options connOptions
;
2223 NodeId
CConnman::GetNewNodeId()
2225 return nLastNodeId
.fetch_add(1, std::memory_order_relaxed
);
2229 bool CConnman::Bind(const CService
&addr
, unsigned int flags
) {
2230 if (!(flags
& BF_EXPLICIT
) && IsLimited(addr
))
2232 std::string strError
;
2233 if (!BindListenPort(addr
, strError
, (flags
& BF_WHITELIST
) != 0)) {
2234 if ((flags
& BF_REPORT_ERROR
) && clientInterface
) {
2235 clientInterface
->ThreadSafeMessageBox(strError
, "", CClientUIInterface::MSG_ERROR
);
2242 bool CConnman::InitBinds(const std::vector
<CService
>& binds
, const std::vector
<CService
>& whiteBinds
) {
2243 bool fBound
= false;
2244 for (const auto& addrBind
: binds
) {
2245 fBound
|= Bind(addrBind
, (BF_EXPLICIT
| BF_REPORT_ERROR
));
2247 for (const auto& addrBind
: whiteBinds
) {
2248 fBound
|= Bind(addrBind
, (BF_EXPLICIT
| BF_REPORT_ERROR
| BF_WHITELIST
));
2250 if (binds
.empty() && whiteBinds
.empty()) {
2251 struct in_addr inaddr_any
;
2252 inaddr_any
.s_addr
= INADDR_ANY
;
2253 fBound
|= Bind(CService(in6addr_any
, GetListenPort()), BF_NONE
);
2254 fBound
|= Bind(CService(inaddr_any
, GetListenPort()), !fBound
? BF_REPORT_ERROR
: BF_NONE
);
2259 bool CConnman::Start(CScheduler
& scheduler
, const Options
& connOptions
)
2264 LOCK(cs_totalBytesRecv
);
2265 nTotalBytesRecv
= 0;
2268 LOCK(cs_totalBytesSent
);
2269 nTotalBytesSent
= 0;
2270 nMaxOutboundTotalBytesSentInCycle
= 0;
2271 nMaxOutboundCycleStartTime
= 0;
2274 if (fListen
&& !InitBinds(connOptions
.vBinds
, connOptions
.vWhiteBinds
)) {
2275 if (clientInterface
) {
2276 clientInterface
->ThreadSafeMessageBox(
2277 _("Failed to listen on any port. Use -listen=0 if you want this."),
2278 "", CClientUIInterface::MSG_ERROR
);
2283 for (const auto& strDest
: connOptions
.vSeedNodes
) {
2284 AddOneShot(strDest
);
2287 if (clientInterface
) {
2288 clientInterface
->InitMessage(_("Loading P2P addresses..."));
2290 // Load addresses from peers.dat
2291 int64_t nStart
= GetTimeMillis();
2294 if (adb
.Read(addrman
))
2295 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman
.size(), GetTimeMillis() - nStart
);
2297 addrman
.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2298 LogPrintf("Invalid or missing peers.dat; recreating\n");
2302 if (clientInterface
)
2303 clientInterface
->InitMessage(_("Loading banlist..."));
2304 // Load addresses from banlist.dat
2305 nStart
= GetTimeMillis();
2308 if (bandb
.Read(banmap
)) {
2309 SetBanned(banmap
); // thread save setter
2310 SetBannedSetDirty(false); // no need to write down, just read data
2311 SweepBanned(); // sweep out unused entries
2313 LogPrint(BCLog::NET
, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2314 banmap
.size(), GetTimeMillis() - nStart
);
2316 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2317 SetBannedSetDirty(true); // force write
2321 uiInterface
.InitMessage(_("Starting network threads..."));
2323 fAddressesInitialized
= true;
2325 if (semOutbound
== nullptr) {
2326 // initialize semaphore
2327 semOutbound
= MakeUnique
<CSemaphore
>(std::min((nMaxOutbound
+ nMaxFeeler
), nMaxConnections
));
2329 if (semAddnode
== nullptr) {
2330 // initialize semaphore
2331 semAddnode
= MakeUnique
<CSemaphore
>(nMaxAddnode
);
2338 InterruptSocks5(false);
2339 interruptNet
.reset();
2340 flagInterruptMsgProc
= false;
2343 std::unique_lock
<std::mutex
> lock(mutexMsgProc
);
2344 fMsgProcWake
= false;
2347 // Send and receive from sockets, accept connections
2348 threadSocketHandler
= std::thread(&TraceThread
<std::function
<void()> >, "net", std::function
<void()>(std::bind(&CConnman::ThreadSocketHandler
, this)));
2350 if (!gArgs
.GetBoolArg("-dnsseed", true))
2351 LogPrintf("DNS seeding disabled\n");
2353 threadDNSAddressSeed
= std::thread(&TraceThread
<std::function
<void()> >, "dnsseed", std::function
<void()>(std::bind(&CConnman::ThreadDNSAddressSeed
, this)));
2355 // Initiate outbound connections from -addnode
2356 threadOpenAddedConnections
= std::thread(&TraceThread
<std::function
<void()> >, "addcon", std::function
<void()>(std::bind(&CConnman::ThreadOpenAddedConnections
, this)));
2358 if (connOptions
.m_use_addrman_outgoing
&& !connOptions
.m_specified_outgoing
.empty()) {
2359 if (clientInterface
) {
2360 clientInterface
->ThreadSafeMessageBox(
2361 _("Cannot provide specific connections and have addrman find outgoing connections at the same."),
2362 "", CClientUIInterface::MSG_ERROR
);
2366 if (connOptions
.m_use_addrman_outgoing
|| !connOptions
.m_specified_outgoing
.empty())
2367 threadOpenConnections
= std::thread(&TraceThread
<std::function
<void()> >, "opencon", std::function
<void()>(std::bind(&CConnman::ThreadOpenConnections
, this, connOptions
.m_specified_outgoing
)));
2370 threadMessageHandler
= std::thread(&TraceThread
<std::function
<void()> >, "msghand", std::function
<void()>(std::bind(&CConnman::ThreadMessageHandler
, this)));
2372 // Dump network addresses
2373 scheduler
.scheduleEvery(std::bind(&CConnman::DumpData
, this), DUMP_ADDRESSES_INTERVAL
* 1000);
2386 // Shutdown Windows Sockets
2391 instance_of_cnetcleanup
;
2393 void CConnman::Interrupt()
2396 std::lock_guard
<std::mutex
> lock(mutexMsgProc
);
2397 flagInterruptMsgProc
= true;
2399 condMsgProc
.notify_all();
2402 InterruptSocks5(true);
2405 for (int i
=0; i
<(nMaxOutbound
+ nMaxFeeler
); i
++) {
2406 semOutbound
->post();
2411 for (int i
=0; i
<nMaxAddnode
; i
++) {
2417 void CConnman::Stop()
2419 if (threadMessageHandler
.joinable())
2420 threadMessageHandler
.join();
2421 if (threadOpenConnections
.joinable())
2422 threadOpenConnections
.join();
2423 if (threadOpenAddedConnections
.joinable())
2424 threadOpenAddedConnections
.join();
2425 if (threadDNSAddressSeed
.joinable())
2426 threadDNSAddressSeed
.join();
2427 if (threadSocketHandler
.joinable())
2428 threadSocketHandler
.join();
2430 if (fAddressesInitialized
)
2433 fAddressesInitialized
= false;
2437 for (CNode
* pnode
: vNodes
)
2438 pnode
->CloseSocketDisconnect();
2439 for (ListenSocket
& hListenSocket
: vhListenSocket
)
2440 if (hListenSocket
.socket
!= INVALID_SOCKET
)
2441 if (!CloseSocket(hListenSocket
.socket
))
2442 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2444 // clean up some globals (to help leak detection)
2445 for (CNode
*pnode
: vNodes
) {
2448 for (CNode
*pnode
: vNodesDisconnected
) {
2452 vNodesDisconnected
.clear();
2453 vhListenSocket
.clear();
2454 semOutbound
.reset();
2458 void CConnman::DeleteNode(CNode
* pnode
)
2461 bool fUpdateConnectionTime
= false;
2462 m_msgproc
->FinalizeNode(pnode
->GetId(), fUpdateConnectionTime
);
2463 if(fUpdateConnectionTime
) {
2464 addrman
.Connected(pnode
->addr
);
2469 CConnman::~CConnman()
2475 size_t CConnman::GetAddressCount() const
2477 return addrman
.size();
2480 void CConnman::SetServices(const CService
&addr
, ServiceFlags nServices
)
2482 addrman
.SetServices(addr
, nServices
);
2485 void CConnman::MarkAddressGood(const CAddress
& addr
)
2490 void CConnman::AddNewAddresses(const std::vector
<CAddress
>& vAddr
, const CAddress
& addrFrom
, int64_t nTimePenalty
)
2492 addrman
.Add(vAddr
, addrFrom
, nTimePenalty
);
2495 std::vector
<CAddress
> CConnman::GetAddresses()
2497 return addrman
.GetAddr();
2500 bool CConnman::AddNode(const std::string
& strNode
)
2502 LOCK(cs_vAddedNodes
);
2503 for (const std::string
& it
: vAddedNodes
) {
2504 if (strNode
== it
) return false;
2507 vAddedNodes
.push_back(strNode
);
2511 bool CConnman::RemoveAddedNode(const std::string
& strNode
)
2513 LOCK(cs_vAddedNodes
);
2514 for(std::vector
<std::string
>::iterator it
= vAddedNodes
.begin(); it
!= vAddedNodes
.end(); ++it
) {
2515 if (strNode
== *it
) {
2516 vAddedNodes
.erase(it
);
2523 size_t CConnman::GetNodeCount(NumConnections flags
)
2526 if (flags
== CConnman::CONNECTIONS_ALL
) // Shortcut if we want total
2527 return vNodes
.size();
2530 for (const auto& pnode
: vNodes
) {
2531 if (flags
& (pnode
->fInbound
? CONNECTIONS_IN
: CONNECTIONS_OUT
)) {
2539 void CConnman::GetNodeStats(std::vector
<CNodeStats
>& vstats
)
2543 vstats
.reserve(vNodes
.size());
2544 for (CNode
* pnode
: vNodes
) {
2545 vstats
.emplace_back();
2546 pnode
->copyStats(vstats
.back());
2550 bool CConnman::DisconnectNode(const std::string
& strNode
)
2553 if (CNode
* pnode
= FindNode(strNode
)) {
2554 pnode
->fDisconnect
= true;
2559 bool CConnman::DisconnectNode(NodeId id
)
2562 for(CNode
* pnode
: vNodes
) {
2563 if (id
== pnode
->GetId()) {
2564 pnode
->fDisconnect
= true;
2571 void CConnman::RecordBytesRecv(uint64_t bytes
)
2573 LOCK(cs_totalBytesRecv
);
2574 nTotalBytesRecv
+= bytes
;
2577 void CConnman::RecordBytesSent(uint64_t bytes
)
2579 LOCK(cs_totalBytesSent
);
2580 nTotalBytesSent
+= bytes
;
2582 uint64_t now
= GetTime();
2583 if (nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
< now
)
2585 // timeframe expired, reset cycle
2586 nMaxOutboundCycleStartTime
= now
;
2587 nMaxOutboundTotalBytesSentInCycle
= 0;
2590 // TODO, exclude whitebind peers
2591 nMaxOutboundTotalBytesSentInCycle
+= bytes
;
2594 void CConnman::SetMaxOutboundTarget(uint64_t limit
)
2596 LOCK(cs_totalBytesSent
);
2597 nMaxOutboundLimit
= limit
;
2600 uint64_t CConnman::GetMaxOutboundTarget()
2602 LOCK(cs_totalBytesSent
);
2603 return nMaxOutboundLimit
;
2606 uint64_t CConnman::GetMaxOutboundTimeframe()
2608 LOCK(cs_totalBytesSent
);
2609 return nMaxOutboundTimeframe
;
2612 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2614 LOCK(cs_totalBytesSent
);
2615 if (nMaxOutboundLimit
== 0)
2618 if (nMaxOutboundCycleStartTime
== 0)
2619 return nMaxOutboundTimeframe
;
2621 uint64_t cycleEndTime
= nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
;
2622 uint64_t now
= GetTime();
2623 return (cycleEndTime
< now
) ? 0 : cycleEndTime
- GetTime();
2626 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe
)
2628 LOCK(cs_totalBytesSent
);
2629 if (nMaxOutboundTimeframe
!= timeframe
)
2631 // reset measure-cycle in case of changing
2633 nMaxOutboundCycleStartTime
= GetTime();
2635 nMaxOutboundTimeframe
= timeframe
;
2638 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit
)
2640 LOCK(cs_totalBytesSent
);
2641 if (nMaxOutboundLimit
== 0)
2644 if (historicalBlockServingLimit
)
2646 // keep a large enough buffer to at least relay each block once
2647 uint64_t timeLeftInCycle
= GetMaxOutboundTimeLeftInCycle();
2648 uint64_t buffer
= timeLeftInCycle
/ 600 * MAX_BLOCK_SERIALIZED_SIZE
;
2649 if (buffer
>= nMaxOutboundLimit
|| nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
- buffer
)
2652 else if (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
)
2658 uint64_t CConnman::GetOutboundTargetBytesLeft()
2660 LOCK(cs_totalBytesSent
);
2661 if (nMaxOutboundLimit
== 0)
2664 return (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
) ? 0 : nMaxOutboundLimit
- nMaxOutboundTotalBytesSentInCycle
;
2667 uint64_t CConnman::GetTotalBytesRecv()
2669 LOCK(cs_totalBytesRecv
);
2670 return nTotalBytesRecv
;
2673 uint64_t CConnman::GetTotalBytesSent()
2675 LOCK(cs_totalBytesSent
);
2676 return nTotalBytesSent
;
2679 ServiceFlags
CConnman::GetLocalServices() const
2681 return nLocalServices
;
2684 void CConnman::SetBestHeight(int height
)
2686 nBestHeight
.store(height
, std::memory_order_release
);
2689 int CConnman::GetBestHeight() const
2691 return nBestHeight
.load(std::memory_order_acquire
);
2694 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize
; }
2696 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
) :
2697 nTimeConnected(GetSystemTimeInSeconds()),
2699 addrBind(addrBindIn
),
2700 fInbound(fInboundIn
),
2701 nKeyedNetGroup(nKeyedNetGroupIn
),
2702 addrKnown(5000, 0.001),
2703 filterInventoryKnown(50000, 0.000001),
2705 nLocalHostNonce(nLocalHostNonceIn
),
2706 nLocalServices(nLocalServicesIn
),
2707 nMyStartingHeight(nMyStartingHeightIn
),
2710 nServices
= NODE_NONE
;
2711 hSocket
= hSocketIn
;
2712 nRecvVersion
= INIT_PROTO_VERSION
;
2718 addrName
= addrNameIn
== "" ? addr
.ToStringIPPort() : addrNameIn
;
2721 fWhitelisted
= false;
2723 m_manual_connection
= false;
2724 fClient
= false; // set by version message
2726 fSuccessfullyConnected
= false;
2727 fDisconnect
= false;
2731 hashContinue
= uint256();
2732 nStartingHeight
= -1;
2733 filterInventoryKnown
.reset();
2734 fSendMempool
= false;
2736 nNextLocalAddrSend
= 0;
2741 pfilter
= MakeUnique
<CBloomFilter
>();
2742 timeLastMempoolReq
= 0;
2748 fPingQueued
= false;
2749 nMinPingUsecTime
= std::numeric_limits
<int64_t>::max();
2751 lastSentFeeFilter
= 0;
2752 nextSendTimeFeeFilter
= 0;
2755 nProcessQueueSize
= 0;
2757 for (const std::string
&msg
: getAllNetMessageTypes())
2758 mapRecvBytesPerMsgCmd
[msg
] = 0;
2759 mapRecvBytesPerMsgCmd
[NET_MESSAGE_COMMAND_OTHER
] = 0;
2762 LogPrint(BCLog::NET
, "Added connection to %s peer=%d\n", addrName
, id
);
2764 LogPrint(BCLog::NET
, "Added connection peer=%d\n", id
);
2770 CloseSocket(hSocket
);
2773 void CNode::AskFor(const CInv
& inv
)
2775 if (mapAskFor
.size() > MAPASKFOR_MAX_SZ
|| setAskFor
.size() > SETASKFOR_MAX_SZ
)
2777 // a peer may not have multiple non-responded queue positions for a single inv item
2778 if (!setAskFor
.insert(inv
.hash
).second
)
2781 // We're using mapAskFor as a priority queue,
2782 // the key is the earliest time the request can be sent
2783 int64_t nRequestTime
;
2784 limitedmap
<uint256
, int64_t>::const_iterator it
= mapAlreadyAskedFor
.find(inv
.hash
);
2785 if (it
!= mapAlreadyAskedFor
.end())
2786 nRequestTime
= it
->second
;
2789 LogPrint(BCLog::NET
, "askfor %s %d (%s) peer=%d\n", inv
.ToString(), nRequestTime
, DateTimeStrFormat("%H:%M:%S", nRequestTime
/1000000), id
);
2791 // Make sure not to reuse time indexes to keep things in the same order
2792 int64_t nNow
= GetTimeMicros() - 1000000;
2793 static int64_t nLastTime
;
2795 nNow
= std::max(nNow
, nLastTime
);
2798 // Each retry is 2 minutes after the last
2799 nRequestTime
= std::max(nRequestTime
+ 2 * 60 * 1000000, nNow
);
2800 if (it
!= mapAlreadyAskedFor
.end())
2801 mapAlreadyAskedFor
.update(it
, nRequestTime
);
2803 mapAlreadyAskedFor
.insert(std::make_pair(inv
.hash
, nRequestTime
));
2804 mapAskFor
.insert(std::make_pair(nRequestTime
, inv
));
2807 bool CConnman::NodeFullyConnected(const CNode
* pnode
)
2809 return pnode
&& pnode
->fSuccessfullyConnected
&& !pnode
->fDisconnect
;
2812 void CConnman::PushMessage(CNode
* pnode
, CSerializedNetMsg
&& msg
)
2814 size_t nMessageSize
= msg
.data
.size();
2815 size_t nTotalSize
= nMessageSize
+ CMessageHeader::HEADER_SIZE
;
2816 LogPrint(BCLog::NET
, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg
.command
.c_str()), nMessageSize
, pnode
->GetId());
2818 std::vector
<unsigned char> serializedHeader
;
2819 serializedHeader
.reserve(CMessageHeader::HEADER_SIZE
);
2820 uint256 hash
= Hash(msg
.data
.data(), msg
.data
.data() + nMessageSize
);
2821 CMessageHeader
hdr(Params().MessageStart(), msg
.command
.c_str(), nMessageSize
);
2822 memcpy(hdr
.pchChecksum
, hash
.begin(), CMessageHeader::CHECKSUM_SIZE
);
2824 CVectorWriter
{SER_NETWORK
, INIT_PROTO_VERSION
, serializedHeader
, 0, hdr
};
2826 size_t nBytesSent
= 0;
2828 LOCK(pnode
->cs_vSend
);
2829 bool optimisticSend(pnode
->vSendMsg
.empty());
2831 //log total amount of bytes per command
2832 pnode
->mapSendBytesPerMsgCmd
[msg
.command
] += nTotalSize
;
2833 pnode
->nSendSize
+= nTotalSize
;
2835 if (pnode
->nSendSize
> nSendBufferMaxSize
)
2836 pnode
->fPauseSend
= true;
2837 pnode
->vSendMsg
.push_back(std::move(serializedHeader
));
2839 pnode
->vSendMsg
.push_back(std::move(msg
.data
));
2841 // If write queue empty, attempt "optimistic write"
2842 if (optimisticSend
== true)
2843 nBytesSent
= SocketSendData(pnode
);
2846 RecordBytesSent(nBytesSent
);
2849 bool CConnman::ForNode(NodeId id
, std::function
<bool(CNode
* pnode
)> func
)
2851 CNode
* found
= nullptr;
2853 for (auto&& pnode
: vNodes
) {
2854 if(pnode
->GetId() == id
) {
2859 return found
!= nullptr && NodeFullyConnected(found
) && func(found
);
2862 int64_t PoissonNextSend(int64_t nNow
, int average_interval_seconds
) {
2863 return nNow
+ (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds
* -1000000.0 + 0.5);
2866 CSipHasher
CConnman::GetDeterministicRandomizer(uint64_t id
) const
2868 return CSipHasher(nSeed0
, nSeed1
).Write(id
);
2871 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress
& ad
) const
2873 std::vector
<unsigned char> vchNetGroup(ad
.GetGroup());
2875 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP
).Write(vchNetGroup
.data(), vchNetGroup
.size()).Finalize();