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"
13 #include "chainparams.h"
14 #include "clientversion.h"
15 #include "consensus/consensus.h"
16 #include "crypto/common.h"
17 #include "crypto/sha256.h"
19 #include "primitives/transaction.h"
21 #include "scheduler.h"
22 #include "ui_interface.h"
23 #include "utilstrencodings.h"
32 #include <miniupnpc/miniupnpc.h>
33 #include <miniupnpc/miniwget.h>
34 #include <miniupnpc/upnpcommands.h>
35 #include <miniupnpc/upnperrors.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
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
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.
59 #ifndef PROTECTION_LEVEL_UNRESTRICTED
60 #define PROTECTION_LEVEL_UNRESTRICTED 10
62 #ifndef IPV6_PROTECTION_LEVEL
63 #define IPV6_PROTECTION_LEVEL 23
67 /** Used to pass flags to the Bind() function */
70 BF_EXPLICIT
= (1U << 0),
71 BF_REPORT_ERROR
= (1U << 1),
72 BF_WHITELIST
= (1U << 2),
75 const static std::string NET_MESSAGE_COMMAND_OTHER
= "*other*";
77 static const uint64_t RANDOMIZER_ID_NETGROUP
= 0x6c0edd8036ef4036ULL
; // SHA256("netgroup")[0:8]
78 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE
= 0xd93e69e2bbfa5735ULL
; // SHA256("localhostnonce")[0:8]
80 // Global state variables
82 bool fDiscover
= true;
84 bool fRelayTxes
= true;
85 CCriticalSection cs_mapLocalHost
;
86 std::map
<CNetAddr
, LocalServiceInfo
> mapLocalHost
;
87 static bool vfLimited
[NET_MAX
] = {};
88 std::string strSubVersion
;
90 limitedmap
<uint256
, int64_t> mapAlreadyAskedFor(MAX_INV_SZ
);
92 void CConnman::AddOneShot(const std::string
& strDest
)
95 vOneShots
.push_back(strDest
);
98 unsigned short GetListenPort()
100 return (unsigned short)(gArgs
.GetArg("-port", Params().GetDefaultPort()));
103 // find 'best' local address for a particular peer
104 bool GetLocal(CService
& addr
, const CNetAddr
*paddrPeer
)
110 int nBestReachability
= -1;
112 LOCK(cs_mapLocalHost
);
113 for (std::map
<CNetAddr
, LocalServiceInfo
>::iterator it
= mapLocalHost
.begin(); it
!= mapLocalHost
.end(); it
++)
115 int nScore
= (*it
).second
.nScore
;
116 int nReachability
= (*it
).first
.GetReachabilityFrom(paddrPeer
);
117 if (nReachability
> nBestReachability
|| (nReachability
== nBestReachability
&& nScore
> nBestScore
))
119 addr
= CService((*it
).first
, (*it
).second
.nPort
);
120 nBestReachability
= nReachability
;
125 return nBestScore
>= 0;
128 //! Convert the pnSeeds6 array into usable address objects.
129 static std::vector
<CAddress
> convertSeed6(const std::vector
<SeedSpec6
> &vSeedsIn
)
131 // It'll only connect to one or two seed nodes because once it connects,
132 // it'll get a pile of addresses with newer timestamps.
133 // Seed nodes are given a random 'last seen time' of between one and two
135 const int64_t nOneWeek
= 7*24*60*60;
136 std::vector
<CAddress
> vSeedsOut
;
137 vSeedsOut
.reserve(vSeedsIn
.size());
138 for (const auto& seed_in
: vSeedsIn
) {
140 memcpy(&ip
, seed_in
.addr
, sizeof(ip
));
141 CAddress
addr(CService(ip
, seed_in
.port
), NODE_NETWORK
);
142 addr
.nTime
= GetTime() - GetRand(nOneWeek
) - nOneWeek
;
143 vSeedsOut
.push_back(addr
);
148 // get best local address for a particular peer as a CAddress
149 // Otherwise, return the unroutable 0.0.0.0 but filled in with
150 // the normal parameters, since the IP may be changed to a useful
152 CAddress
GetLocalAddress(const CNetAddr
*paddrPeer
, ServiceFlags nLocalServices
)
154 CAddress
ret(CService(CNetAddr(),GetListenPort()), nLocalServices
);
156 if (GetLocal(addr
, paddrPeer
))
158 ret
= CAddress(addr
, nLocalServices
);
160 ret
.nTime
= GetAdjustedTime();
164 int GetnScore(const CService
& addr
)
166 LOCK(cs_mapLocalHost
);
167 if (mapLocalHost
.count(addr
) == LOCAL_NONE
)
169 return mapLocalHost
[addr
].nScore
;
172 // Is our peer's addrLocal potentially useful as an external IP source?
173 bool IsPeerAddrLocalGood(CNode
*pnode
)
175 CService addrLocal
= pnode
->GetAddrLocal();
176 return fDiscover
&& pnode
->addr
.IsRoutable() && addrLocal
.IsRoutable() &&
177 !IsLimited(addrLocal
.GetNetwork());
180 // pushes our own address to a peer
181 void AdvertiseLocal(CNode
*pnode
)
183 if (fListen
&& pnode
->fSuccessfullyConnected
)
185 CAddress addrLocal
= GetLocalAddress(&pnode
->addr
, pnode
->GetLocalServices());
186 // If discovery is enabled, sometimes give our peer the address it
187 // tells us that it sees us as in case it has a better idea of our
188 // address than we do.
189 if (IsPeerAddrLocalGood(pnode
) && (!addrLocal
.IsRoutable() ||
190 GetRand((GetnScore(addrLocal
) > LOCAL_MANUAL
) ? 8:2) == 0))
192 addrLocal
.SetIP(pnode
->GetAddrLocal());
194 if (addrLocal
.IsRoutable())
196 LogPrint(BCLog::NET
, "AdvertiseLocal: advertising address %s\n", addrLocal
.ToString());
197 FastRandomContext insecure_rand
;
198 pnode
->PushAddress(addrLocal
, insecure_rand
);
203 // learn a new local address
204 bool AddLocal(const CService
& addr
, int nScore
)
206 if (!addr
.IsRoutable())
209 if (!fDiscover
&& nScore
< LOCAL_MANUAL
)
215 LogPrintf("AddLocal(%s,%i)\n", addr
.ToString(), nScore
);
218 LOCK(cs_mapLocalHost
);
219 bool fAlready
= mapLocalHost
.count(addr
) > 0;
220 LocalServiceInfo
&info
= mapLocalHost
[addr
];
221 if (!fAlready
|| nScore
>= info
.nScore
) {
222 info
.nScore
= nScore
+ (fAlready
? 1 : 0);
223 info
.nPort
= addr
.GetPort();
230 bool AddLocal(const CNetAddr
&addr
, int nScore
)
232 return AddLocal(CService(addr
, GetListenPort()), nScore
);
235 bool RemoveLocal(const CService
& addr
)
237 LOCK(cs_mapLocalHost
);
238 LogPrintf("RemoveLocal(%s)\n", addr
.ToString());
239 mapLocalHost
.erase(addr
);
243 /** Make a particular network entirely off-limits (no automatic connects to it) */
244 void SetLimited(enum Network net
, bool fLimited
)
246 if (net
== NET_UNROUTABLE
|| net
== NET_INTERNAL
)
248 LOCK(cs_mapLocalHost
);
249 vfLimited
[net
] = fLimited
;
252 bool IsLimited(enum Network net
)
254 LOCK(cs_mapLocalHost
);
255 return vfLimited
[net
];
258 bool IsLimited(const CNetAddr
&addr
)
260 return IsLimited(addr
.GetNetwork());
263 /** vote for a local address */
264 bool SeenLocal(const CService
& addr
)
267 LOCK(cs_mapLocalHost
);
268 if (mapLocalHost
.count(addr
) == 0)
270 mapLocalHost
[addr
].nScore
++;
276 /** check whether a given address is potentially local */
277 bool IsLocal(const CService
& addr
)
279 LOCK(cs_mapLocalHost
);
280 return mapLocalHost
.count(addr
) > 0;
283 /** check whether a given network is one we can probably connect to */
284 bool IsReachable(enum Network net
)
286 LOCK(cs_mapLocalHost
);
287 return !vfLimited
[net
];
290 /** check whether a given address is in a network we can probably connect to */
291 bool IsReachable(const CNetAddr
& addr
)
293 enum Network net
= addr
.GetNetwork();
294 return IsReachable(net
);
298 CNode
* CConnman::FindNode(const CNetAddr
& ip
)
301 for (CNode
* pnode
: vNodes
) {
302 if ((CNetAddr
)pnode
->addr
== ip
) {
309 CNode
* CConnman::FindNode(const CSubNet
& subNet
)
312 for (CNode
* pnode
: vNodes
) {
313 if (subNet
.Match((CNetAddr
)pnode
->addr
)) {
320 CNode
* CConnman::FindNode(const std::string
& addrName
)
323 for (CNode
* pnode
: vNodes
) {
324 if (pnode
->GetAddrName() == addrName
) {
331 CNode
* CConnman::FindNode(const CService
& addr
)
334 for (CNode
* pnode
: vNodes
) {
335 if ((CService
)pnode
->addr
== addr
) {
342 bool CConnman::CheckIncomingNonce(uint64_t nonce
)
345 for (CNode
* pnode
: vNodes
) {
346 if (!pnode
->fSuccessfullyConnected
&& !pnode
->fInbound
&& pnode
->GetLocalNonce() == nonce
)
352 /** Get the bind address for a socket as CAddress */
353 static CAddress
GetBindAddress(SOCKET sock
)
356 struct sockaddr_storage sockaddr_bind
;
357 socklen_t sockaddr_bind_len
= sizeof(sockaddr_bind
);
358 if (sock
!= INVALID_SOCKET
) {
359 if (!getsockname(sock
, (struct sockaddr
*)&sockaddr_bind
, &sockaddr_bind_len
)) {
360 addr_bind
.SetSockAddr((const struct sockaddr
*)&sockaddr_bind
);
362 LogPrint(BCLog::NET
, "Warning: getsockname failed\n");
368 CNode
* CConnman::ConnectNode(CAddress addrConnect
, const char *pszDest
, bool fCountFailure
)
370 if (pszDest
== nullptr) {
371 if (IsLocal(addrConnect
))
374 // Look for an existing connection
375 CNode
* pnode
= FindNode((CService
)addrConnect
);
378 LogPrintf("Failed to open new connection, already connected\n");
384 LogPrint(BCLog::NET
, "trying connection %s lastseen=%.1fhrs\n",
385 pszDest
? pszDest
: addrConnect
.ToString(),
386 pszDest
? 0.0 : (double)(GetAdjustedTime() - addrConnect
.nTime
)/3600.0);
389 const int default_port
= Params().GetDefaultPort();
391 std::vector
<CService
> resolved
;
392 if (Lookup(pszDest
, resolved
, default_port
, fNameLookup
&& !HaveNameProxy(), 256) && !resolved
.empty()) {
393 addrConnect
= CAddress(resolved
[GetRand(resolved
.size())], NODE_NONE
);
394 if (!addrConnect
.IsValid()) {
395 LogPrint(BCLog::NET
, "Resolver returned invalid address %s for %s", addrConnect
.ToString(), pszDest
);
398 // It is possible that we already have a connection to the IP/port pszDest resolved to.
399 // In that case, drop the connection that was just created, and return the existing CNode instead.
400 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
401 // name catch this early.
403 CNode
* pnode
= FindNode((CService
)addrConnect
);
406 pnode
->MaybeSetAddrName(std::string(pszDest
));
407 LogPrintf("Failed to open new connection, already connected\n");
414 bool connected
= false;
417 if (addrConnect
.IsValid()) {
418 bool proxyConnectionFailed
= false;
420 if (GetProxy(addrConnect
.GetNetwork(), proxy
))
421 connected
= ConnectThroughProxy(proxy
, addrConnect
.ToStringIP(), addrConnect
.GetPort(), hSocket
, nConnectTimeout
, &proxyConnectionFailed
);
422 else // no proxy needed (none set for target network)
423 connected
= ConnectSocketDirectly(addrConnect
, hSocket
, nConnectTimeout
);
424 if (!proxyConnectionFailed
) {
425 // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
426 // the proxy, mark this as an attempt.
427 addrman
.Attempt(addrConnect
, fCountFailure
);
429 } else if (pszDest
&& GetNameProxy(proxy
)) {
431 int port
= default_port
;
432 SplitHostPort(std::string(pszDest
), port
, host
);
433 connected
= ConnectThroughProxy(proxy
, host
, port
, hSocket
, nConnectTimeout
, nullptr);
436 if (!IsSelectableSocket(hSocket
)) {
437 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
438 CloseSocket(hSocket
);
443 NodeId id
= GetNewNodeId();
444 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
445 CAddress addr_bind
= GetBindAddress(hSocket
);
446 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addrConnect
, CalculateKeyedNetGroup(addrConnect
), nonce
, addr_bind
, pszDest
? pszDest
: "", false);
455 void CConnman::DumpBanlist()
457 SweepBanned(); // clean unused entries (if bantime has expired)
459 if (!BannedSetIsDirty())
462 int64_t nStart
= GetTimeMillis();
467 if (bandb
.Write(banmap
)) {
468 SetBannedSetDirty(false);
471 LogPrint(BCLog::NET
, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
472 banmap
.size(), GetTimeMillis() - nStart
);
475 void CNode::CloseSocketDisconnect()
479 if (hSocket
!= INVALID_SOCKET
)
481 LogPrint(BCLog::NET
, "disconnecting peer=%d\n", id
);
482 CloseSocket(hSocket
);
486 void CConnman::ClearBanned()
491 setBannedIsDirty
= true;
493 DumpBanlist(); //store banlist to disk
495 clientInterface
->BannedListChanged();
498 bool CConnman::IsBanned(CNetAddr ip
)
501 for (const auto& it
: setBanned
) {
502 CSubNet subNet
= it
.first
;
503 CBanEntry banEntry
= it
.second
;
505 if (subNet
.Match(ip
) && GetTime() < banEntry
.nBanUntil
) {
512 bool CConnman::IsBanned(CSubNet subnet
)
515 banmap_t::iterator i
= setBanned
.find(subnet
);
516 if (i
!= setBanned
.end())
518 CBanEntry banEntry
= (*i
).second
;
519 if (GetTime() < banEntry
.nBanUntil
) {
526 void CConnman::Ban(const CNetAddr
& addr
, const BanReason
&banReason
, int64_t bantimeoffset
, bool sinceUnixEpoch
) {
527 CSubNet
subNet(addr
);
528 Ban(subNet
, banReason
, bantimeoffset
, sinceUnixEpoch
);
531 void CConnman::Ban(const CSubNet
& subNet
, const BanReason
&banReason
, int64_t bantimeoffset
, bool sinceUnixEpoch
) {
532 CBanEntry
banEntry(GetTime());
533 banEntry
.banReason
= banReason
;
534 if (bantimeoffset
<= 0)
536 bantimeoffset
= gArgs
.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME
);
537 sinceUnixEpoch
= false;
539 banEntry
.nBanUntil
= (sinceUnixEpoch
? 0 : GetTime() )+bantimeoffset
;
543 if (setBanned
[subNet
].nBanUntil
< banEntry
.nBanUntil
) {
544 setBanned
[subNet
] = banEntry
;
545 setBannedIsDirty
= true;
551 clientInterface
->BannedListChanged();
554 for (CNode
* pnode
: vNodes
) {
555 if (subNet
.Match((CNetAddr
)pnode
->addr
))
556 pnode
->fDisconnect
= true;
559 if(banReason
== BanReasonManuallyAdded
)
560 DumpBanlist(); //store banlist to disk immediately if user requested ban
563 bool CConnman::Unban(const CNetAddr
&addr
) {
564 CSubNet
subNet(addr
);
565 return Unban(subNet
);
568 bool CConnman::Unban(const CSubNet
&subNet
) {
571 if (!setBanned
.erase(subNet
))
573 setBannedIsDirty
= true;
576 clientInterface
->BannedListChanged();
577 DumpBanlist(); //store banlist to disk immediately
581 void CConnman::GetBanned(banmap_t
&banMap
)
584 // Sweep the banlist so expired bans are not returned
586 banMap
= setBanned
; //create a thread safe copy
589 void CConnman::SetBanned(const banmap_t
&banMap
)
593 setBannedIsDirty
= true;
596 void CConnman::SweepBanned()
598 int64_t now
= GetTime();
601 banmap_t::iterator it
= setBanned
.begin();
602 while(it
!= setBanned
.end())
604 CSubNet subNet
= (*it
).first
;
605 CBanEntry banEntry
= (*it
).second
;
606 if(now
> banEntry
.nBanUntil
)
608 setBanned
.erase(it
++);
609 setBannedIsDirty
= true;
610 LogPrint(BCLog::NET
, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__
, subNet
.ToString());
617 bool CConnman::BannedSetIsDirty()
620 return setBannedIsDirty
;
623 void CConnman::SetBannedSetDirty(bool dirty
)
625 LOCK(cs_setBanned
); //reuse setBanned lock for the isDirty flag
626 setBannedIsDirty
= dirty
;
630 bool CConnman::IsWhitelistedRange(const CNetAddr
&addr
) {
631 for (const CSubNet
& subnet
: vWhitelistedRange
) {
632 if (subnet
.Match(addr
))
638 std::string
CNode::GetAddrName() const {
643 void CNode::MaybeSetAddrName(const std::string
& addrNameIn
) {
645 if (addrName
.empty()) {
646 addrName
= addrNameIn
;
650 CService
CNode::GetAddrLocal() const {
655 void CNode::SetAddrLocal(const CService
& addrLocalIn
) {
657 if (addrLocal
.IsValid()) {
658 error("Addr local already set for node: %i. Refusing to change from %s to %s", id
, addrLocal
.ToString(), addrLocalIn
.ToString());
660 addrLocal
= addrLocalIn
;
665 #define X(name) stats.name = name
666 void CNode::copyStats(CNodeStats
&stats
)
668 stats
.nodeid
= this->GetId();
680 stats
.addrName
= GetAddrName();
687 X(m_manual_connection
);
691 X(mapSendBytesPerMsgCmd
);
696 X(mapRecvBytesPerMsgCmd
);
701 // It is common for nodes with good ping times to suddenly become lagged,
702 // due to a new block arriving or other large transfer.
703 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
704 // since pingtime does not update until the ping is complete, which might take a while.
705 // So, if a ping is taking an unusually long time in flight,
706 // the caller can immediately detect that this is happening.
707 int64_t nPingUsecWait
= 0;
708 if ((0 != nPingNonceSent
) && (0 != nPingUsecStart
)) {
709 nPingUsecWait
= GetTimeMicros() - nPingUsecStart
;
712 // 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 :)
713 stats
.dPingTime
= (((double)nPingUsecTime
) / 1e6
);
714 stats
.dMinPing
= (((double)nMinPingUsecTime
) / 1e6
);
715 stats
.dPingWait
= (((double)nPingUsecWait
) / 1e6
);
717 // Leave string empty if addrLocal invalid (not filled in yet)
718 CService addrLocalUnlocked
= GetAddrLocal();
719 stats
.addrLocal
= addrLocalUnlocked
.IsValid() ? addrLocalUnlocked
.ToString() : "";
723 bool CNode::ReceiveMsgBytes(const char *pch
, unsigned int nBytes
, bool& complete
)
726 int64_t nTimeMicros
= GetTimeMicros();
728 nLastRecv
= nTimeMicros
/ 1000000;
729 nRecvBytes
+= nBytes
;
732 // get current incomplete message, or create a new one
733 if (vRecvMsg
.empty() ||
734 vRecvMsg
.back().complete())
735 vRecvMsg
.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK
, INIT_PROTO_VERSION
));
737 CNetMessage
& msg
= vRecvMsg
.back();
739 // absorb network data
742 handled
= msg
.readHeader(pch
, nBytes
);
744 handled
= msg
.readData(pch
, nBytes
);
749 if (msg
.in_data
&& msg
.hdr
.nMessageSize
> MAX_PROTOCOL_MESSAGE_LENGTH
) {
750 LogPrint(BCLog::NET
, "Oversized message from peer=%i, disconnecting\n", GetId());
757 if (msg
.complete()) {
759 //store received bytes per message command
760 //to prevent a memory DOS, only allow valid commands
761 mapMsgCmdSize::iterator i
= mapRecvBytesPerMsgCmd
.find(msg
.hdr
.pchCommand
);
762 if (i
== mapRecvBytesPerMsgCmd
.end())
763 i
= mapRecvBytesPerMsgCmd
.find(NET_MESSAGE_COMMAND_OTHER
);
764 assert(i
!= mapRecvBytesPerMsgCmd
.end());
765 i
->second
+= msg
.hdr
.nMessageSize
+ CMessageHeader::HEADER_SIZE
;
767 msg
.nTime
= nTimeMicros
;
775 void CNode::SetSendVersion(int nVersionIn
)
777 // Send version may only be changed in the version message, and
778 // only one version message is allowed per session. We can therefore
779 // treat this value as const and even atomic as long as it's only used
780 // once a version message has been successfully processed. Any attempt to
781 // set this twice is an error.
782 if (nSendVersion
!= 0) {
783 error("Send version already set for node: %i. Refusing to change from %i to %i", id
, nSendVersion
, nVersionIn
);
785 nSendVersion
= nVersionIn
;
789 int CNode::GetSendVersion() const
791 // The send version should always be explicitly set to
792 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
794 if (nSendVersion
== 0) {
795 error("Requesting unset send version for node: %i. Using %i", id
, INIT_PROTO_VERSION
);
796 return INIT_PROTO_VERSION
;
802 int CNetMessage::readHeader(const char *pch
, unsigned int nBytes
)
804 // copy data to temporary parsing buffer
805 unsigned int nRemaining
= 24 - nHdrPos
;
806 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
808 memcpy(&hdrbuf
[nHdrPos
], pch
, nCopy
);
811 // if header incomplete, exit
815 // deserialize to CMessageHeader
819 catch (const std::exception
&) {
823 // reject messages larger than MAX_SIZE
824 if (hdr
.nMessageSize
> MAX_SIZE
)
827 // switch state to reading message data
833 int CNetMessage::readData(const char *pch
, unsigned int nBytes
)
835 unsigned int nRemaining
= hdr
.nMessageSize
- nDataPos
;
836 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
838 if (vRecv
.size() < nDataPos
+ nCopy
) {
839 // Allocate up to 256 KiB ahead, but never more than the total message size.
840 vRecv
.resize(std::min(hdr
.nMessageSize
, nDataPos
+ nCopy
+ 256 * 1024));
843 hasher
.Write((const unsigned char*)pch
, nCopy
);
844 memcpy(&vRecv
[nDataPos
], pch
, nCopy
);
850 const uint256
& CNetMessage::GetMessageHash() const
853 if (data_hash
.IsNull())
854 hasher
.Finalize(data_hash
.begin());
866 // requires LOCK(cs_vSend)
867 size_t CConnman::SocketSendData(CNode
*pnode
) const
869 auto it
= pnode
->vSendMsg
.begin();
870 size_t nSentSize
= 0;
872 while (it
!= pnode
->vSendMsg
.end()) {
873 const auto &data
= *it
;
874 assert(data
.size() > pnode
->nSendOffset
);
877 LOCK(pnode
->cs_hSocket
);
878 if (pnode
->hSocket
== INVALID_SOCKET
)
880 nBytes
= send(pnode
->hSocket
, reinterpret_cast<const char*>(data
.data()) + pnode
->nSendOffset
, data
.size() - pnode
->nSendOffset
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
883 pnode
->nLastSend
= GetSystemTimeInSeconds();
884 pnode
->nSendBytes
+= nBytes
;
885 pnode
->nSendOffset
+= nBytes
;
887 if (pnode
->nSendOffset
== data
.size()) {
888 pnode
->nSendOffset
= 0;
889 pnode
->nSendSize
-= data
.size();
890 pnode
->fPauseSend
= pnode
->nSendSize
> nSendBufferMaxSize
;
893 // could not send full message; stop sending more
899 int nErr
= WSAGetLastError();
900 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
902 LogPrintf("socket send error %s\n", NetworkErrorString(nErr
));
903 pnode
->CloseSocketDisconnect();
906 // couldn't send anything at all
911 if (it
== pnode
->vSendMsg
.end()) {
912 assert(pnode
->nSendOffset
== 0);
913 assert(pnode
->nSendSize
== 0);
915 pnode
->vSendMsg
.erase(pnode
->vSendMsg
.begin(), it
);
919 struct NodeEvictionCandidate
922 int64_t nTimeConnected
;
923 int64_t nMinPingUsecTime
;
924 int64_t nLastBlockTime
;
926 bool fRelevantServices
;
930 uint64_t nKeyedNetGroup
;
933 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
935 return a
.nMinPingUsecTime
> b
.nMinPingUsecTime
;
938 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
940 return a
.nTimeConnected
> b
.nTimeConnected
;
943 static bool CompareNetGroupKeyed(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
) {
944 return a
.nKeyedNetGroup
< b
.nKeyedNetGroup
;
947 static bool CompareNodeBlockTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
949 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
950 if (a
.nLastBlockTime
!= b
.nLastBlockTime
) return a
.nLastBlockTime
< b
.nLastBlockTime
;
951 if (a
.fRelevantServices
!= b
.fRelevantServices
) return b
.fRelevantServices
;
952 return a
.nTimeConnected
> b
.nTimeConnected
;
955 static bool CompareNodeTXTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
957 // 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.
958 if (a
.nLastTXTime
!= b
.nLastTXTime
) return a
.nLastTXTime
< b
.nLastTXTime
;
959 if (a
.fRelayTxes
!= b
.fRelayTxes
) return b
.fRelayTxes
;
960 if (a
.fBloomFilter
!= b
.fBloomFilter
) return a
.fBloomFilter
;
961 return a
.nTimeConnected
> b
.nTimeConnected
;
964 /** Try to find a connection to evict when the node is full.
965 * Extreme care must be taken to avoid opening the node to attacker
966 * triggered network partitioning.
967 * The strategy used here is to protect a small number of peers
968 * for each of several distinct characteristics which are difficult
969 * to forge. In order to partition a node the attacker must be
970 * simultaneously better at all of them than honest peers.
972 bool CConnman::AttemptToEvictConnection()
974 std::vector
<NodeEvictionCandidate
> vEvictionCandidates
;
978 for (const CNode
* node
: vNodes
) {
979 if (node
->fWhitelisted
)
983 if (node
->fDisconnect
)
985 NodeEvictionCandidate candidate
= {node
->GetId(), node
->nTimeConnected
, node
->nMinPingUsecTime
,
986 node
->nLastBlockTime
, node
->nLastTXTime
,
987 HasAllDesirableServiceFlags(node
->nServices
),
988 node
->fRelayTxes
, node
->pfilter
!= nullptr, node
->addr
, node
->nKeyedNetGroup
};
989 vEvictionCandidates
.push_back(candidate
);
993 if (vEvictionCandidates
.empty()) return false;
995 // Protect connections with certain characteristics
997 // Deterministically select 4 peers to protect by netgroup.
998 // An attacker cannot predict which netgroups will be protected
999 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNetGroupKeyed
);
1000 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
1002 if (vEvictionCandidates
.empty()) return false;
1004 // Protect the 8 nodes with the lowest minimum ping time.
1005 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
1006 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), ReverseCompareNodeMinPingTime
);
1007 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(8, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
1009 if (vEvictionCandidates
.empty()) return false;
1011 // Protect 4 nodes that most recently sent us transactions.
1012 // An attacker cannot manipulate this metric without performing useful work.
1013 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNodeTXTime
);
1014 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
1016 if (vEvictionCandidates
.empty()) return false;
1018 // Protect 4 nodes that most recently sent us blocks.
1019 // An attacker cannot manipulate this metric without performing useful work.
1020 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNodeBlockTime
);
1021 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
1023 if (vEvictionCandidates
.empty()) return false;
1025 // Protect the half of the remaining nodes which have been connected the longest.
1026 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1027 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), ReverseCompareNodeTimeConnected
);
1028 vEvictionCandidates
.erase(vEvictionCandidates
.end() - static_cast<int>(vEvictionCandidates
.size() / 2), vEvictionCandidates
.end());
1030 if (vEvictionCandidates
.empty()) return false;
1032 // Identify the network group with the most connections and youngest member.
1033 // (vEvictionCandidates is already sorted by reverse connect time)
1034 uint64_t naMostConnections
;
1035 unsigned int nMostConnections
= 0;
1036 int64_t nMostConnectionsTime
= 0;
1037 std::map
<uint64_t, std::vector
<NodeEvictionCandidate
> > mapNetGroupNodes
;
1038 for (const NodeEvictionCandidate
&node
: vEvictionCandidates
) {
1039 mapNetGroupNodes
[node
.nKeyedNetGroup
].push_back(node
);
1040 int64_t grouptime
= mapNetGroupNodes
[node
.nKeyedNetGroup
][0].nTimeConnected
;
1041 size_t groupsize
= mapNetGroupNodes
[node
.nKeyedNetGroup
].size();
1043 if (groupsize
> nMostConnections
|| (groupsize
== nMostConnections
&& grouptime
> nMostConnectionsTime
)) {
1044 nMostConnections
= groupsize
;
1045 nMostConnectionsTime
= grouptime
;
1046 naMostConnections
= node
.nKeyedNetGroup
;
1050 // Reduce to the network group with the most connections
1051 vEvictionCandidates
= std::move(mapNetGroupNodes
[naMostConnections
]);
1053 // Disconnect from the network group with the most connections
1054 NodeId evicted
= vEvictionCandidates
.front().id
;
1056 for (CNode
* pnode
: vNodes
) {
1057 if (pnode
->GetId() == evicted
) {
1058 pnode
->fDisconnect
= true;
1065 void CConnman::AcceptConnection(const ListenSocket
& hListenSocket
) {
1066 struct sockaddr_storage sockaddr
;
1067 socklen_t len
= sizeof(sockaddr
);
1068 SOCKET hSocket
= accept(hListenSocket
.socket
, (struct sockaddr
*)&sockaddr
, &len
);
1071 int nMaxInbound
= nMaxConnections
- (nMaxOutbound
+ nMaxFeeler
);
1073 if (hSocket
!= INVALID_SOCKET
) {
1074 if (!addr
.SetSockAddr((const struct sockaddr
*)&sockaddr
)) {
1075 LogPrintf("Warning: Unknown socket family\n");
1079 bool whitelisted
= hListenSocket
.whitelisted
|| IsWhitelistedRange(addr
);
1082 for (const CNode
* pnode
: vNodes
) {
1083 if (pnode
->fInbound
) nInbound
++;
1087 if (hSocket
== INVALID_SOCKET
)
1089 int nErr
= WSAGetLastError();
1090 if (nErr
!= WSAEWOULDBLOCK
)
1091 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr
));
1095 if (!fNetworkActive
) {
1096 LogPrintf("connection from %s dropped: not accepting new connections\n", addr
.ToString());
1097 CloseSocket(hSocket
);
1101 if (!IsSelectableSocket(hSocket
))
1103 LogPrintf("connection from %s dropped: non-selectable socket\n", addr
.ToString());
1104 CloseSocket(hSocket
);
1108 // According to the internet TCP_NODELAY is not carried into accepted sockets
1109 // on all platforms. Set it again here just to be sure.
1110 SetSocketNoDelay(hSocket
);
1112 if (IsBanned(addr
) && !whitelisted
)
1114 LogPrintf("connection from %s dropped (banned)\n", addr
.ToString());
1115 CloseSocket(hSocket
);
1119 if (nInbound
>= nMaxInbound
)
1121 if (!AttemptToEvictConnection()) {
1122 // No connection to evict, disconnect the new connection
1123 LogPrint(BCLog::NET
, "failed to find an eviction candidate - connection dropped (full)\n");
1124 CloseSocket(hSocket
);
1129 NodeId id
= GetNewNodeId();
1130 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
1131 CAddress addr_bind
= GetBindAddress(hSocket
);
1133 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addr
, CalculateKeyedNetGroup(addr
), nonce
, addr_bind
, "", true);
1135 pnode
->fWhitelisted
= whitelisted
;
1136 m_msgproc
->InitializeNode(pnode
);
1138 LogPrint(BCLog::NET
, "connection from %s accepted\n", addr
.ToString());
1142 vNodes
.push_back(pnode
);
1146 void CConnman::ThreadSocketHandler()
1148 unsigned int nPrevNodeCount
= 0;
1149 while (!interruptNet
)
1156 // Disconnect unused nodes
1157 std::vector
<CNode
*> vNodesCopy
= vNodes
;
1158 for (CNode
* pnode
: vNodesCopy
)
1160 if (pnode
->fDisconnect
)
1162 // remove from vNodes
1163 vNodes
.erase(remove(vNodes
.begin(), vNodes
.end(), pnode
), vNodes
.end());
1165 // release outbound grant (if any)
1166 pnode
->grantOutbound
.Release();
1168 // close socket and cleanup
1169 pnode
->CloseSocketDisconnect();
1171 // hold in disconnected pool until all refs are released
1173 vNodesDisconnected
.push_back(pnode
);
1178 // Delete disconnected nodes
1179 std::list
<CNode
*> vNodesDisconnectedCopy
= vNodesDisconnected
;
1180 for (CNode
* pnode
: vNodesDisconnectedCopy
)
1182 // wait until threads are done using it
1183 if (pnode
->GetRefCount() <= 0) {
1184 bool fDelete
= false;
1186 TRY_LOCK(pnode
->cs_inventory
, lockInv
);
1188 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1195 vNodesDisconnected
.remove(pnode
);
1204 vNodesSize
= vNodes
.size();
1206 if(vNodesSize
!= nPrevNodeCount
) {
1207 nPrevNodeCount
= vNodesSize
;
1209 clientInterface
->NotifyNumConnectionsChanged(nPrevNodeCount
);
1213 // Find which sockets have data to receive
1215 struct timeval timeout
;
1217 timeout
.tv_usec
= 50000; // frequency to poll pnode->vSend
1222 FD_ZERO(&fdsetRecv
);
1223 FD_ZERO(&fdsetSend
);
1224 FD_ZERO(&fdsetError
);
1225 SOCKET hSocketMax
= 0;
1226 bool have_fds
= false;
1228 for (const ListenSocket
& hListenSocket
: vhListenSocket
) {
1229 FD_SET(hListenSocket
.socket
, &fdsetRecv
);
1230 hSocketMax
= std::max(hSocketMax
, hListenSocket
.socket
);
1236 for (CNode
* pnode
: vNodes
)
1238 // Implement the following logic:
1239 // * If there is data to send, select() for sending data. As this only
1240 // happens when optimistic write failed, we choose to first drain the
1241 // write buffer in this case before receiving more. This avoids
1242 // needlessly queueing received data, if the remote peer is not themselves
1243 // receiving data. This means properly utilizing TCP flow control signalling.
1244 // * Otherwise, if there is space left in the receive buffer, select() for
1246 // * Hand off all complete messages to the processor, to be handled without
1249 bool select_recv
= !pnode
->fPauseRecv
;
1252 LOCK(pnode
->cs_vSend
);
1253 select_send
= !pnode
->vSendMsg
.empty();
1256 LOCK(pnode
->cs_hSocket
);
1257 if (pnode
->hSocket
== INVALID_SOCKET
)
1260 FD_SET(pnode
->hSocket
, &fdsetError
);
1261 hSocketMax
= std::max(hSocketMax
, pnode
->hSocket
);
1265 FD_SET(pnode
->hSocket
, &fdsetSend
);
1269 FD_SET(pnode
->hSocket
, &fdsetRecv
);
1274 int nSelect
= select(have_fds
? hSocketMax
+ 1 : 0,
1275 &fdsetRecv
, &fdsetSend
, &fdsetError
, &timeout
);
1279 if (nSelect
== SOCKET_ERROR
)
1283 int nErr
= WSAGetLastError();
1284 LogPrintf("socket select error %s\n", NetworkErrorString(nErr
));
1285 for (unsigned int i
= 0; i
<= hSocketMax
; i
++)
1286 FD_SET(i
, &fdsetRecv
);
1288 FD_ZERO(&fdsetSend
);
1289 FD_ZERO(&fdsetError
);
1290 if (!interruptNet
.sleep_for(std::chrono::milliseconds(timeout
.tv_usec
/1000)))
1295 // Accept new connections
1297 for (const ListenSocket
& hListenSocket
: vhListenSocket
)
1299 if (hListenSocket
.socket
!= INVALID_SOCKET
&& FD_ISSET(hListenSocket
.socket
, &fdsetRecv
))
1301 AcceptConnection(hListenSocket
);
1306 // Service each socket
1308 std::vector
<CNode
*> vNodesCopy
;
1311 vNodesCopy
= vNodes
;
1312 for (CNode
* pnode
: vNodesCopy
)
1315 for (CNode
* pnode
: vNodesCopy
)
1323 bool recvSet
= false;
1324 bool sendSet
= false;
1325 bool errorSet
= false;
1327 LOCK(pnode
->cs_hSocket
);
1328 if (pnode
->hSocket
== INVALID_SOCKET
)
1330 recvSet
= FD_ISSET(pnode
->hSocket
, &fdsetRecv
);
1331 sendSet
= FD_ISSET(pnode
->hSocket
, &fdsetSend
);
1332 errorSet
= FD_ISSET(pnode
->hSocket
, &fdsetError
);
1334 if (recvSet
|| errorSet
)
1336 // typical socket buffer is 8K-64K
1337 char pchBuf
[0x10000];
1340 LOCK(pnode
->cs_hSocket
);
1341 if (pnode
->hSocket
== INVALID_SOCKET
)
1343 nBytes
= recv(pnode
->hSocket
, pchBuf
, sizeof(pchBuf
), MSG_DONTWAIT
);
1347 bool notify
= false;
1348 if (!pnode
->ReceiveMsgBytes(pchBuf
, nBytes
, notify
))
1349 pnode
->CloseSocketDisconnect();
1350 RecordBytesRecv(nBytes
);
1352 size_t nSizeAdded
= 0;
1353 auto it(pnode
->vRecvMsg
.begin());
1354 for (; it
!= pnode
->vRecvMsg
.end(); ++it
) {
1355 if (!it
->complete())
1357 nSizeAdded
+= it
->vRecv
.size() + CMessageHeader::HEADER_SIZE
;
1360 LOCK(pnode
->cs_vProcessMsg
);
1361 pnode
->vProcessMsg
.splice(pnode
->vProcessMsg
.end(), pnode
->vRecvMsg
, pnode
->vRecvMsg
.begin(), it
);
1362 pnode
->nProcessQueueSize
+= nSizeAdded
;
1363 pnode
->fPauseRecv
= pnode
->nProcessQueueSize
> nReceiveFloodSize
;
1365 WakeMessageHandler();
1368 else if (nBytes
== 0)
1370 // socket closed gracefully
1371 if (!pnode
->fDisconnect
) {
1372 LogPrint(BCLog::NET
, "socket closed\n");
1374 pnode
->CloseSocketDisconnect();
1376 else if (nBytes
< 0)
1379 int nErr
= WSAGetLastError();
1380 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
1382 if (!pnode
->fDisconnect
)
1383 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr
));
1384 pnode
->CloseSocketDisconnect();
1394 LOCK(pnode
->cs_vSend
);
1395 size_t nBytes
= SocketSendData(pnode
);
1397 RecordBytesSent(nBytes
);
1402 // Inactivity checking
1404 int64_t nTime
= GetSystemTimeInSeconds();
1405 if (nTime
- pnode
->nTimeConnected
> 60)
1407 if (pnode
->nLastRecv
== 0 || pnode
->nLastSend
== 0)
1409 LogPrint(BCLog::NET
, "socket no message in first 60 seconds, %d %d from %d\n", pnode
->nLastRecv
!= 0, pnode
->nLastSend
!= 0, pnode
->GetId());
1410 pnode
->fDisconnect
= true;
1412 else if (nTime
- pnode
->nLastSend
> TIMEOUT_INTERVAL
)
1414 LogPrintf("socket sending timeout: %is\n", nTime
- pnode
->nLastSend
);
1415 pnode
->fDisconnect
= true;
1417 else if (nTime
- pnode
->nLastRecv
> (pnode
->nVersion
> BIP0031_VERSION
? TIMEOUT_INTERVAL
: 90*60))
1419 LogPrintf("socket receive timeout: %is\n", nTime
- pnode
->nLastRecv
);
1420 pnode
->fDisconnect
= true;
1422 else if (pnode
->nPingNonceSent
&& pnode
->nPingUsecStart
+ TIMEOUT_INTERVAL
* 1000000 < GetTimeMicros())
1424 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode
->nPingUsecStart
));
1425 pnode
->fDisconnect
= true;
1427 else if (!pnode
->fSuccessfullyConnected
)
1429 LogPrintf("version handshake timeout from %d\n", pnode
->GetId());
1430 pnode
->fDisconnect
= true;
1436 for (CNode
* pnode
: vNodesCopy
)
1442 void CConnman::WakeMessageHandler()
1445 std::lock_guard
<std::mutex
> lock(mutexMsgProc
);
1446 fMsgProcWake
= true;
1448 condMsgProc
.notify_one();
1457 void ThreadMapPort()
1459 std::string port
= strprintf("%u", GetListenPort());
1460 const char * multicastif
= nullptr;
1461 const char * minissdpdpath
= nullptr;
1462 struct UPNPDev
* devlist
= nullptr;
1465 #ifndef UPNPDISCOVER_SUCCESS
1467 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0);
1468 #elif MINIUPNPC_API_VERSION < 14
1471 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, &error
);
1473 /* miniupnpc 1.9.20150730 */
1475 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, 2, &error
);
1478 struct UPNPUrls urls
;
1479 struct IGDdatas data
;
1482 r
= UPNP_GetValidIGD(devlist
, &urls
, &data
, lanaddr
, sizeof(lanaddr
));
1486 char externalIPAddress
[40];
1487 r
= UPNP_GetExternalIPAddress(urls
.controlURL
, data
.first
.servicetype
, externalIPAddress
);
1488 if(r
!= UPNPCOMMAND_SUCCESS
)
1489 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r
);
1492 if(externalIPAddress
[0])
1495 if(LookupHost(externalIPAddress
, resolved
, false)) {
1496 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved
.ToString().c_str());
1497 AddLocal(resolved
, LOCAL_UPNP
);
1501 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1505 std::string strDesc
= "Bitcoin " + FormatFullVersion();
1509 #ifndef UPNPDISCOVER_SUCCESS
1511 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1512 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0);
1515 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1516 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0, "0");
1519 if(r
!=UPNPCOMMAND_SUCCESS
)
1520 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1521 port
, port
, lanaddr
, r
, strupnperror(r
));
1523 LogPrintf("UPnP Port Mapping successful.\n");
1525 MilliSleep(20*60*1000); // Refresh every 20 minutes
1528 catch (const boost::thread_interrupted
&)
1530 r
= UPNP_DeletePortMapping(urls
.controlURL
, data
.first
.servicetype
, port
.c_str(), "TCP", 0);
1531 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r
);
1532 freeUPNPDevlist(devlist
); devlist
= nullptr;
1533 FreeUPNPUrls(&urls
);
1537 LogPrintf("No valid UPnP IGDs found\n");
1538 freeUPNPDevlist(devlist
); devlist
= nullptr;
1540 FreeUPNPUrls(&urls
);
1544 void MapPort(bool fUseUPnP
)
1546 static boost::thread
* upnp_thread
= nullptr;
1551 upnp_thread
->interrupt();
1552 upnp_thread
->join();
1555 upnp_thread
= new boost::thread(boost::bind(&TraceThread
<void (*)()>, "upnp", &ThreadMapPort
));
1557 else if (upnp_thread
) {
1558 upnp_thread
->interrupt();
1559 upnp_thread
->join();
1561 upnp_thread
= nullptr;
1568 // Intentionally left blank.
1577 static std::string
GetDNSHost(const CDNSSeedData
& data
, ServiceFlags
* requiredServiceBits
)
1579 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1580 if (!data
.supportsServiceBitsFiltering
|| *requiredServiceBits
== NODE_NETWORK
) {
1581 *requiredServiceBits
= NODE_NETWORK
;
1585 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1586 return strprintf("x%x.%s", *requiredServiceBits
, data
.host
);
1590 void CConnman::ThreadDNSAddressSeed()
1592 // goal: only query DNS seeds if address need is acute
1593 // Avoiding DNS seeds when we don't need them improves user privacy by
1594 // creating fewer identifying DNS requests, reduces trust by giving seeds
1595 // less influence on the network topology, and reduces traffic to the seeds.
1596 if ((addrman
.size() > 0) &&
1597 (!gArgs
.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED
))) {
1598 if (!interruptNet
.sleep_for(std::chrono::seconds(11)))
1603 for (auto pnode
: vNodes
) {
1604 nRelevant
+= pnode
->fSuccessfullyConnected
&& !pnode
->fFeeler
&& !pnode
->fOneShot
&& !pnode
->m_manual_connection
&& !pnode
->fInbound
;
1606 if (nRelevant
>= 2) {
1607 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1612 const std::vector
<CDNSSeedData
> &vSeeds
= Params().DNSSeeds();
1615 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1617 for (const CDNSSeedData
&seed
: vSeeds
) {
1621 if (HaveNameProxy()) {
1622 AddOneShot(seed
.host
);
1624 std::vector
<CNetAddr
> vIPs
;
1625 std::vector
<CAddress
> vAdd
;
1626 ServiceFlags requiredServiceBits
= GetDesirableServiceFlags(NODE_NONE
);
1627 std::string host
= GetDNSHost(seed
, &requiredServiceBits
);
1628 CNetAddr resolveSource
;
1629 if (!resolveSource
.SetInternal(host
)) {
1632 if (LookupHost(host
.c_str(), vIPs
, 0, true))
1634 for (const CNetAddr
& ip
: vIPs
)
1636 int nOneDay
= 24*3600;
1637 CAddress addr
= CAddress(CService(ip
, Params().GetDefaultPort()), requiredServiceBits
);
1638 addr
.nTime
= GetTime() - 3*nOneDay
- GetRand(4*nOneDay
); // use a random age between 3 and 7 days old
1639 vAdd
.push_back(addr
);
1642 addrman
.Add(vAdd
, resolveSource
);
1647 LogPrintf("%d addresses found from DNS seeds\n", found
);
1661 void CConnman::DumpAddresses()
1663 int64_t nStart
= GetTimeMillis();
1668 LogPrint(BCLog::NET
, "Flushed %d addresses to peers.dat %dms\n",
1669 addrman
.size(), GetTimeMillis() - nStart
);
1672 void CConnman::DumpData()
1678 void CConnman::ProcessOneShot()
1680 std::string strDest
;
1683 if (vOneShots
.empty())
1685 strDest
= vOneShots
.front();
1686 vOneShots
.pop_front();
1689 CSemaphoreGrant
grant(*semOutbound
, true);
1691 if (!OpenNetworkConnection(addr
, false, &grant
, strDest
.c_str(), true))
1692 AddOneShot(strDest
);
1696 void CConnman::ThreadOpenConnections(const std::vector
<std::string
> connect
)
1698 // Connect to specific addresses
1699 if (!connect
.empty())
1701 for (int64_t nLoop
= 0;; nLoop
++)
1704 for (const std::string
& strAddr
: connect
)
1706 CAddress
addr(CService(), NODE_NONE
);
1707 OpenNetworkConnection(addr
, false, nullptr, strAddr
.c_str(), false, false, true);
1708 for (int i
= 0; i
< 10 && i
< nLoop
; i
++)
1710 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1714 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1719 // Initiate network connections
1720 int64_t nStart
= GetTime();
1722 // Minimum time before next feeler connection (in microseconds).
1723 int64_t nNextFeeler
= PoissonNextSend(nStart
*1000*1000, FEELER_INTERVAL
);
1724 while (!interruptNet
)
1728 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1731 CSemaphoreGrant
grant(*semOutbound
);
1735 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1736 if (addrman
.size() == 0 && (GetTime() - nStart
> 60)) {
1737 static bool done
= false;
1739 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1741 local
.SetInternal("fixedseeds");
1742 addrman
.Add(convertSeed6(Params().FixedSeeds()), local
);
1748 // Choose an address to connect to based on most recently seen
1750 CAddress addrConnect
;
1752 // Only connect out to one peer per network group (/16 for IPv4).
1753 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1755 std::set
<std::vector
<unsigned char> > setConnected
;
1758 for (CNode
* pnode
: vNodes
) {
1759 if (!pnode
->fInbound
&& !pnode
->m_manual_connection
) {
1760 // Netgroups for inbound and addnode peers are not excluded because our goal here
1761 // is to not use multiple of our limited outbound slots on a single netgroup
1762 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1763 // also have the added issue that they're attacker controlled and could be used
1764 // to prevent us from connecting to particular hosts if we used them here.
1765 setConnected
.insert(pnode
->addr
.GetGroup());
1771 // Feeler Connections
1774 // * Increase the number of connectable addresses in the tried table.
1777 // * Choose a random address from new and attempt to connect to it if we can connect
1778 // successfully it is added to tried.
1779 // * Start attempting feeler connections only after node finishes making outbound
1781 // * Only make a feeler connection once every few minutes.
1783 bool fFeeler
= false;
1784 if (nOutbound
>= nMaxOutbound
) {
1785 int64_t nTime
= GetTimeMicros(); // The current time right now (in microseconds).
1786 if (nTime
> nNextFeeler
) {
1787 nNextFeeler
= PoissonNextSend(nTime
, FEELER_INTERVAL
);
1794 int64_t nANow
= GetAdjustedTime();
1796 while (!interruptNet
)
1798 CAddrInfo addr
= addrman
.Select(fFeeler
);
1800 // if we selected an invalid address, restart
1801 if (!addr
.IsValid() || setConnected
.count(addr
.GetGroup()) || IsLocal(addr
))
1804 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1805 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1806 // already-connected network ranges, ...) before trying new addrman addresses.
1811 if (IsLimited(addr
))
1814 // only consider very recently tried nodes after 30 failed attempts
1815 if (nANow
- addr
.nLastTry
< 600 && nTries
< 30)
1818 // for non-feelers, require all the services we'll want,
1819 // for feelers, only require they be a full node (only because most
1820 // SPV clients don't have a good address DB available)
1821 if (!fFeeler
&& !HasAllDesirableServiceFlags(addr
.nServices
)) {
1823 } else if (fFeeler
&& !MayHaveUsefulAddressDB(addr
.nServices
)) {
1827 // do not allow non-default ports, unless after 50 invalid addresses selected already
1828 if (addr
.GetPort() != Params().GetDefaultPort() && nTries
< 50)
1835 if (addrConnect
.IsValid()) {
1838 // Add small amount of random noise before connection to avoid synchronization.
1839 int randsleep
= GetRandInt(FEELER_SLEEP_WINDOW
* 1000);
1840 if (!interruptNet
.sleep_for(std::chrono::milliseconds(randsleep
)))
1842 LogPrint(BCLog::NET
, "Making feeler connection to %s\n", addrConnect
.ToString());
1845 OpenNetworkConnection(addrConnect
, (int)setConnected
.size() >= std::min(nMaxConnections
- 1, 2), &grant
, nullptr, false, fFeeler
);
1850 std::vector
<AddedNodeInfo
> CConnman::GetAddedNodeInfo()
1852 std::vector
<AddedNodeInfo
> ret
;
1854 std::list
<std::string
> lAddresses(0);
1856 LOCK(cs_vAddedNodes
);
1857 ret
.reserve(vAddedNodes
.size());
1858 std::copy(vAddedNodes
.cbegin(), vAddedNodes
.cend(), std::back_inserter(lAddresses
));
1862 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1863 std::map
<CService
, bool> mapConnected
;
1864 std::map
<std::string
, std::pair
<bool, CService
>> mapConnectedByName
;
1867 for (const CNode
* pnode
: vNodes
) {
1868 if (pnode
->addr
.IsValid()) {
1869 mapConnected
[pnode
->addr
] = pnode
->fInbound
;
1871 std::string addrName
= pnode
->GetAddrName();
1872 if (!addrName
.empty()) {
1873 mapConnectedByName
[std::move(addrName
)] = std::make_pair(pnode
->fInbound
, static_cast<const CService
&>(pnode
->addr
));
1878 for (const std::string
& strAddNode
: lAddresses
) {
1879 CService
service(LookupNumeric(strAddNode
.c_str(), Params().GetDefaultPort()));
1880 if (service
.IsValid()) {
1881 // strAddNode is an IP:port
1882 auto it
= mapConnected
.find(service
);
1883 if (it
!= mapConnected
.end()) {
1884 ret
.push_back(AddedNodeInfo
{strAddNode
, service
, true, it
->second
});
1886 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1889 // strAddNode is a name
1890 auto it
= mapConnectedByName
.find(strAddNode
);
1891 if (it
!= mapConnectedByName
.end()) {
1892 ret
.push_back(AddedNodeInfo
{strAddNode
, it
->second
.second
, true, it
->second
.first
});
1894 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1902 void CConnman::ThreadOpenAddedConnections()
1906 CSemaphoreGrant
grant(*semAddnode
);
1907 std::vector
<AddedNodeInfo
> vInfo
= GetAddedNodeInfo();
1909 for (const AddedNodeInfo
& info
: vInfo
) {
1910 if (!info
.fConnected
) {
1911 if (!grant
.TryAcquire()) {
1912 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1913 // the addednodeinfo state might change.
1917 CAddress
addr(CService(), NODE_NONE
);
1918 OpenNetworkConnection(addr
, false, &grant
, info
.strAddedNode
.c_str(), false, false, true);
1919 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1923 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1924 if (!interruptNet
.sleep_for(std::chrono::seconds(tried
? 60 : 2)))
1929 // if successful, this moves the passed grant to the constructed node
1930 bool CConnman::OpenNetworkConnection(const CAddress
& addrConnect
, bool fCountFailure
, CSemaphoreGrant
*grantOutbound
, const char *pszDest
, bool fOneShot
, bool fFeeler
, bool manual_connection
)
1933 // Initiate outbound network connection
1938 if (!fNetworkActive
) {
1942 if (IsLocal(addrConnect
) ||
1943 FindNode((CNetAddr
)addrConnect
) || IsBanned(addrConnect
) ||
1944 FindNode(addrConnect
.ToStringIPPort()))
1946 } else if (FindNode(std::string(pszDest
)))
1949 CNode
* pnode
= ConnectNode(addrConnect
, pszDest
, fCountFailure
);
1954 grantOutbound
->MoveTo(pnode
->grantOutbound
);
1956 pnode
->fOneShot
= true;
1958 pnode
->fFeeler
= true;
1959 if (manual_connection
)
1960 pnode
->m_manual_connection
= true;
1962 m_msgproc
->InitializeNode(pnode
);
1965 vNodes
.push_back(pnode
);
1971 void CConnman::ThreadMessageHandler()
1973 while (!flagInterruptMsgProc
)
1975 std::vector
<CNode
*> vNodesCopy
;
1978 vNodesCopy
= vNodes
;
1979 for (CNode
* pnode
: vNodesCopy
) {
1984 bool fMoreWork
= false;
1986 for (CNode
* pnode
: vNodesCopy
)
1988 if (pnode
->fDisconnect
)
1992 bool fMoreNodeWork
= m_msgproc
->ProcessMessages(pnode
, flagInterruptMsgProc
);
1993 fMoreWork
|= (fMoreNodeWork
&& !pnode
->fPauseSend
);
1994 if (flagInterruptMsgProc
)
1998 LOCK(pnode
->cs_sendProcessing
);
1999 m_msgproc
->SendMessages(pnode
, flagInterruptMsgProc
);
2002 if (flagInterruptMsgProc
)
2008 for (CNode
* pnode
: vNodesCopy
)
2012 std::unique_lock
<std::mutex
> lock(mutexMsgProc
);
2014 condMsgProc
.wait_until(lock
, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake
; });
2016 fMsgProcWake
= false;
2025 bool CConnman::BindListenPort(const CService
&addrBind
, std::string
& strError
, bool fWhitelisted
)
2030 // Create socket for listening for incoming connections
2031 struct sockaddr_storage sockaddr
;
2032 socklen_t len
= sizeof(sockaddr
);
2033 if (!addrBind
.GetSockAddr((struct sockaddr
*)&sockaddr
, &len
))
2035 strError
= strprintf("Error: Bind address family for %s not supported", addrBind
.ToString());
2036 LogPrintf("%s\n", strError
);
2040 SOCKET hListenSocket
= socket(((struct sockaddr
*)&sockaddr
)->sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
2041 if (hListenSocket
== INVALID_SOCKET
)
2043 strError
= strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2044 LogPrintf("%s\n", strError
);
2047 if (!IsSelectableSocket(hListenSocket
))
2049 strError
= "Error: Couldn't create a listenable socket for incoming connections";
2050 LogPrintf("%s\n", strError
);
2057 // Different way of disabling SIGPIPE on BSD
2058 setsockopt(hListenSocket
, SOL_SOCKET
, SO_NOSIGPIPE
, (void*)&nOne
, sizeof(int));
2060 // Allow binding if the port is still in TIME_WAIT state after
2061 // the program was closed and restarted.
2062 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&nOne
, sizeof(int));
2063 // Disable Nagle's algorithm
2064 setsockopt(hListenSocket
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&nOne
, sizeof(int));
2066 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&nOne
, sizeof(int));
2067 setsockopt(hListenSocket
, IPPROTO_TCP
, TCP_NODELAY
, (const char*)&nOne
, sizeof(int));
2070 // Set to non-blocking, incoming connections will also inherit this
2071 if (!SetSocketNonBlocking(hListenSocket
, true)) {
2072 CloseSocket(hListenSocket
);
2073 strError
= strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2074 LogPrintf("%s\n", strError
);
2078 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2079 // and enable it by default or not. Try to enable it, if possible.
2080 if (addrBind
.IsIPv6()) {
2083 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_V6ONLY
, (const char*)&nOne
, sizeof(int));
2085 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void*)&nOne
, sizeof(int));
2089 int nProtLevel
= PROTECTION_LEVEL_UNRESTRICTED
;
2090 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_PROTECTION_LEVEL
, (const char*)&nProtLevel
, sizeof(int));
2094 if (::bind(hListenSocket
, (struct sockaddr
*)&sockaddr
, len
) == SOCKET_ERROR
)
2096 int nErr
= WSAGetLastError();
2097 if (nErr
== WSAEADDRINUSE
)
2098 strError
= strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind
.ToString(), _(PACKAGE_NAME
));
2100 strError
= strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind
.ToString(), NetworkErrorString(nErr
));
2101 LogPrintf("%s\n", strError
);
2102 CloseSocket(hListenSocket
);
2105 LogPrintf("Bound to %s\n", addrBind
.ToString());
2107 // Listen for incoming connections
2108 if (listen(hListenSocket
, SOMAXCONN
) == SOCKET_ERROR
)
2110 strError
= strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2111 LogPrintf("%s\n", strError
);
2112 CloseSocket(hListenSocket
);
2116 vhListenSocket
.push_back(ListenSocket(hListenSocket
, fWhitelisted
));
2118 if (addrBind
.IsRoutable() && fDiscover
&& !fWhitelisted
)
2119 AddLocal(addrBind
, LOCAL_BIND
);
2124 void Discover(boost::thread_group
& threadGroup
)
2130 // Get local host IP
2131 char pszHostName
[256] = "";
2132 if (gethostname(pszHostName
, sizeof(pszHostName
)) != SOCKET_ERROR
)
2134 std::vector
<CNetAddr
> vaddr
;
2135 if (LookupHost(pszHostName
, vaddr
, 0, true))
2137 for (const CNetAddr
&addr
: vaddr
)
2139 if (AddLocal(addr
, LOCAL_IF
))
2140 LogPrintf("%s: %s - %s\n", __func__
, pszHostName
, addr
.ToString());
2145 // Get local host ip
2146 struct ifaddrs
* myaddrs
;
2147 if (getifaddrs(&myaddrs
) == 0)
2149 for (struct ifaddrs
* ifa
= myaddrs
; ifa
!= nullptr; ifa
= ifa
->ifa_next
)
2151 if (ifa
->ifa_addr
== nullptr) continue;
2152 if ((ifa
->ifa_flags
& IFF_UP
) == 0) continue;
2153 if (strcmp(ifa
->ifa_name
, "lo") == 0) continue;
2154 if (strcmp(ifa
->ifa_name
, "lo0") == 0) continue;
2155 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
2157 struct sockaddr_in
* s4
= (struct sockaddr_in
*)(ifa
->ifa_addr
);
2158 CNetAddr
addr(s4
->sin_addr
);
2159 if (AddLocal(addr
, LOCAL_IF
))
2160 LogPrintf("%s: IPv4 %s: %s\n", __func__
, ifa
->ifa_name
, addr
.ToString());
2162 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
2164 struct sockaddr_in6
* s6
= (struct sockaddr_in6
*)(ifa
->ifa_addr
);
2165 CNetAddr
addr(s6
->sin6_addr
);
2166 if (AddLocal(addr
, LOCAL_IF
))
2167 LogPrintf("%s: IPv6 %s: %s\n", __func__
, ifa
->ifa_name
, addr
.ToString());
2170 freeifaddrs(myaddrs
);
2175 void CConnman::SetNetworkActive(bool active
)
2177 LogPrint(BCLog::NET
, "SetNetworkActive: %s\n", active
);
2179 if (fNetworkActive
== active
) {
2183 fNetworkActive
= active
;
2185 if (!fNetworkActive
) {
2187 // Close sockets to all nodes
2188 for (CNode
* pnode
: vNodes
) {
2189 pnode
->CloseSocketDisconnect();
2193 uiInterface
.NotifyNetworkActiveChanged(fNetworkActive
);
2196 CConnman::CConnman(uint64_t nSeed0In
, uint64_t nSeed1In
) : nSeed0(nSeed0In
), nSeed1(nSeed1In
)
2198 fNetworkActive
= true;
2199 setBannedIsDirty
= false;
2200 fAddressesInitialized
= false;
2202 nSendBufferMaxSize
= 0;
2203 nReceiveFloodSize
= 0;
2204 semOutbound
= nullptr;
2205 semAddnode
= nullptr;
2206 flagInterruptMsgProc
= false;
2208 Options connOptions
;
2212 NodeId
CConnman::GetNewNodeId()
2214 return nLastNodeId
.fetch_add(1, std::memory_order_relaxed
);
2218 bool CConnman::Bind(const CService
&addr
, unsigned int flags
) {
2219 if (!(flags
& BF_EXPLICIT
) && IsLimited(addr
))
2221 std::string strError
;
2222 if (!BindListenPort(addr
, strError
, (flags
& BF_WHITELIST
) != 0)) {
2223 if ((flags
& BF_REPORT_ERROR
) && clientInterface
) {
2224 clientInterface
->ThreadSafeMessageBox(strError
, "", CClientUIInterface::MSG_ERROR
);
2231 bool CConnman::InitBinds(const std::vector
<CService
>& binds
, const std::vector
<CService
>& whiteBinds
) {
2232 bool fBound
= false;
2233 for (const auto& addrBind
: binds
) {
2234 fBound
|= Bind(addrBind
, (BF_EXPLICIT
| BF_REPORT_ERROR
));
2236 for (const auto& addrBind
: whiteBinds
) {
2237 fBound
|= Bind(addrBind
, (BF_EXPLICIT
| BF_REPORT_ERROR
| BF_WHITELIST
));
2239 if (binds
.empty() && whiteBinds
.empty()) {
2240 struct in_addr inaddr_any
;
2241 inaddr_any
.s_addr
= INADDR_ANY
;
2242 fBound
|= Bind(CService(in6addr_any
, GetListenPort()), BF_NONE
);
2243 fBound
|= Bind(CService(inaddr_any
, GetListenPort()), !fBound
? BF_REPORT_ERROR
: BF_NONE
);
2248 bool CConnman::Start(CScheduler
& scheduler
, const Options
& connOptions
)
2252 nTotalBytesRecv
= 0;
2253 nTotalBytesSent
= 0;
2254 nMaxOutboundTotalBytesSentInCycle
= 0;
2255 nMaxOutboundCycleStartTime
= 0;
2257 if (fListen
&& !InitBinds(connOptions
.vBinds
, connOptions
.vWhiteBinds
)) {
2258 if (clientInterface
) {
2259 clientInterface
->ThreadSafeMessageBox(
2260 _("Failed to listen on any port. Use -listen=0 if you want this."),
2261 "", CClientUIInterface::MSG_ERROR
);
2266 for (const auto& strDest
: connOptions
.vSeedNodes
) {
2267 AddOneShot(strDest
);
2270 if (clientInterface
) {
2271 clientInterface
->InitMessage(_("Loading P2P addresses..."));
2273 // Load addresses from peers.dat
2274 int64_t nStart
= GetTimeMillis();
2277 if (adb
.Read(addrman
))
2278 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman
.size(), GetTimeMillis() - nStart
);
2280 addrman
.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2281 LogPrintf("Invalid or missing peers.dat; recreating\n");
2285 if (clientInterface
)
2286 clientInterface
->InitMessage(_("Loading banlist..."));
2287 // Load addresses from banlist.dat
2288 nStart
= GetTimeMillis();
2291 if (bandb
.Read(banmap
)) {
2292 SetBanned(banmap
); // thread save setter
2293 SetBannedSetDirty(false); // no need to write down, just read data
2294 SweepBanned(); // sweep out unused entries
2296 LogPrint(BCLog::NET
, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2297 banmap
.size(), GetTimeMillis() - nStart
);
2299 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2300 SetBannedSetDirty(true); // force write
2304 uiInterface
.InitMessage(_("Starting network threads..."));
2306 fAddressesInitialized
= true;
2308 if (semOutbound
== nullptr) {
2309 // initialize semaphore
2310 semOutbound
= new CSemaphore(std::min((nMaxOutbound
+ nMaxFeeler
), nMaxConnections
));
2312 if (semAddnode
== nullptr) {
2313 // initialize semaphore
2314 semAddnode
= new CSemaphore(nMaxAddnode
);
2321 InterruptSocks5(false);
2322 interruptNet
.reset();
2323 flagInterruptMsgProc
= false;
2326 std::unique_lock
<std::mutex
> lock(mutexMsgProc
);
2327 fMsgProcWake
= false;
2330 // Send and receive from sockets, accept connections
2331 threadSocketHandler
= std::thread(&TraceThread
<std::function
<void()> >, "net", std::function
<void()>(std::bind(&CConnman::ThreadSocketHandler
, this)));
2333 if (!gArgs
.GetBoolArg("-dnsseed", true))
2334 LogPrintf("DNS seeding disabled\n");
2336 threadDNSAddressSeed
= std::thread(&TraceThread
<std::function
<void()> >, "dnsseed", std::function
<void()>(std::bind(&CConnman::ThreadDNSAddressSeed
, this)));
2338 // Initiate outbound connections from -addnode
2339 threadOpenAddedConnections
= std::thread(&TraceThread
<std::function
<void()> >, "addcon", std::function
<void()>(std::bind(&CConnman::ThreadOpenAddedConnections
, this)));
2341 if (connOptions
.m_use_addrman_outgoing
&& !connOptions
.m_specified_outgoing
.empty()) {
2342 if (clientInterface
) {
2343 clientInterface
->ThreadSafeMessageBox(
2344 _("Cannot provide specific connections and have addrman find outgoing connections at the same."),
2345 "", CClientUIInterface::MSG_ERROR
);
2349 if (connOptions
.m_use_addrman_outgoing
|| !connOptions
.m_specified_outgoing
.empty())
2350 threadOpenConnections
= std::thread(&TraceThread
<std::function
<void()> >, "opencon", std::function
<void()>(std::bind(&CConnman::ThreadOpenConnections
, this, connOptions
.m_specified_outgoing
)));
2353 threadMessageHandler
= std::thread(&TraceThread
<std::function
<void()> >, "msghand", std::function
<void()>(std::bind(&CConnman::ThreadMessageHandler
, this)));
2355 // Dump network addresses
2356 scheduler
.scheduleEvery(std::bind(&CConnman::DumpData
, this), DUMP_ADDRESSES_INTERVAL
* 1000);
2369 // Shutdown Windows Sockets
2374 instance_of_cnetcleanup
;
2376 void CConnman::Interrupt()
2379 std::lock_guard
<std::mutex
> lock(mutexMsgProc
);
2380 flagInterruptMsgProc
= true;
2382 condMsgProc
.notify_all();
2385 InterruptSocks5(true);
2388 for (int i
=0; i
<(nMaxOutbound
+ nMaxFeeler
); i
++) {
2389 semOutbound
->post();
2394 for (int i
=0; i
<nMaxAddnode
; i
++) {
2400 void CConnman::Stop()
2402 if (threadMessageHandler
.joinable())
2403 threadMessageHandler
.join();
2404 if (threadOpenConnections
.joinable())
2405 threadOpenConnections
.join();
2406 if (threadOpenAddedConnections
.joinable())
2407 threadOpenAddedConnections
.join();
2408 if (threadDNSAddressSeed
.joinable())
2409 threadDNSAddressSeed
.join();
2410 if (threadSocketHandler
.joinable())
2411 threadSocketHandler
.join();
2413 if (fAddressesInitialized
)
2416 fAddressesInitialized
= false;
2420 for (CNode
* pnode
: vNodes
)
2421 pnode
->CloseSocketDisconnect();
2422 for (ListenSocket
& hListenSocket
: vhListenSocket
)
2423 if (hListenSocket
.socket
!= INVALID_SOCKET
)
2424 if (!CloseSocket(hListenSocket
.socket
))
2425 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2427 // clean up some globals (to help leak detection)
2428 for (CNode
*pnode
: vNodes
) {
2431 for (CNode
*pnode
: vNodesDisconnected
) {
2435 vNodesDisconnected
.clear();
2436 vhListenSocket
.clear();
2438 semOutbound
= nullptr;
2440 semAddnode
= nullptr;
2443 void CConnman::DeleteNode(CNode
* pnode
)
2446 bool fUpdateConnectionTime
= false;
2447 m_msgproc
->FinalizeNode(pnode
->GetId(), fUpdateConnectionTime
);
2448 if(fUpdateConnectionTime
) {
2449 addrman
.Connected(pnode
->addr
);
2454 CConnman::~CConnman()
2460 size_t CConnman::GetAddressCount() const
2462 return addrman
.size();
2465 void CConnman::SetServices(const CService
&addr
, ServiceFlags nServices
)
2467 addrman
.SetServices(addr
, nServices
);
2470 void CConnman::MarkAddressGood(const CAddress
& addr
)
2475 void CConnman::AddNewAddresses(const std::vector
<CAddress
>& vAddr
, const CAddress
& addrFrom
, int64_t nTimePenalty
)
2477 addrman
.Add(vAddr
, addrFrom
, nTimePenalty
);
2480 std::vector
<CAddress
> CConnman::GetAddresses()
2482 return addrman
.GetAddr();
2485 bool CConnman::AddNode(const std::string
& strNode
)
2487 LOCK(cs_vAddedNodes
);
2488 for (const std::string
& it
: vAddedNodes
) {
2489 if (strNode
== it
) return false;
2492 vAddedNodes
.push_back(strNode
);
2496 bool CConnman::RemoveAddedNode(const std::string
& strNode
)
2498 LOCK(cs_vAddedNodes
);
2499 for(std::vector
<std::string
>::iterator it
= vAddedNodes
.begin(); it
!= vAddedNodes
.end(); ++it
) {
2500 if (strNode
== *it
) {
2501 vAddedNodes
.erase(it
);
2508 size_t CConnman::GetNodeCount(NumConnections flags
)
2511 if (flags
== CConnman::CONNECTIONS_ALL
) // Shortcut if we want total
2512 return vNodes
.size();
2515 for (const auto& pnode
: vNodes
) {
2516 if (flags
& (pnode
->fInbound
? CONNECTIONS_IN
: CONNECTIONS_OUT
)) {
2524 void CConnman::GetNodeStats(std::vector
<CNodeStats
>& vstats
)
2528 vstats
.reserve(vNodes
.size());
2529 for (CNode
* pnode
: vNodes
) {
2530 vstats
.emplace_back();
2531 pnode
->copyStats(vstats
.back());
2535 bool CConnman::DisconnectNode(const std::string
& strNode
)
2538 if (CNode
* pnode
= FindNode(strNode
)) {
2539 pnode
->fDisconnect
= true;
2544 bool CConnman::DisconnectNode(NodeId id
)
2547 for(CNode
* pnode
: vNodes
) {
2548 if (id
== pnode
->GetId()) {
2549 pnode
->fDisconnect
= true;
2556 void CConnman::RecordBytesRecv(uint64_t bytes
)
2558 LOCK(cs_totalBytesRecv
);
2559 nTotalBytesRecv
+= bytes
;
2562 void CConnman::RecordBytesSent(uint64_t bytes
)
2564 LOCK(cs_totalBytesSent
);
2565 nTotalBytesSent
+= bytes
;
2567 uint64_t now
= GetTime();
2568 if (nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
< now
)
2570 // timeframe expired, reset cycle
2571 nMaxOutboundCycleStartTime
= now
;
2572 nMaxOutboundTotalBytesSentInCycle
= 0;
2575 // TODO, exclude whitebind peers
2576 nMaxOutboundTotalBytesSentInCycle
+= bytes
;
2579 void CConnman::SetMaxOutboundTarget(uint64_t limit
)
2581 LOCK(cs_totalBytesSent
);
2582 nMaxOutboundLimit
= limit
;
2585 uint64_t CConnman::GetMaxOutboundTarget()
2587 LOCK(cs_totalBytesSent
);
2588 return nMaxOutboundLimit
;
2591 uint64_t CConnman::GetMaxOutboundTimeframe()
2593 LOCK(cs_totalBytesSent
);
2594 return nMaxOutboundTimeframe
;
2597 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2599 LOCK(cs_totalBytesSent
);
2600 if (nMaxOutboundLimit
== 0)
2603 if (nMaxOutboundCycleStartTime
== 0)
2604 return nMaxOutboundTimeframe
;
2606 uint64_t cycleEndTime
= nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
;
2607 uint64_t now
= GetTime();
2608 return (cycleEndTime
< now
) ? 0 : cycleEndTime
- GetTime();
2611 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe
)
2613 LOCK(cs_totalBytesSent
);
2614 if (nMaxOutboundTimeframe
!= timeframe
)
2616 // reset measure-cycle in case of changing
2618 nMaxOutboundCycleStartTime
= GetTime();
2620 nMaxOutboundTimeframe
= timeframe
;
2623 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit
)
2625 LOCK(cs_totalBytesSent
);
2626 if (nMaxOutboundLimit
== 0)
2629 if (historicalBlockServingLimit
)
2631 // keep a large enough buffer to at least relay each block once
2632 uint64_t timeLeftInCycle
= GetMaxOutboundTimeLeftInCycle();
2633 uint64_t buffer
= timeLeftInCycle
/ 600 * MAX_BLOCK_SERIALIZED_SIZE
;
2634 if (buffer
>= nMaxOutboundLimit
|| nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
- buffer
)
2637 else if (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
)
2643 uint64_t CConnman::GetOutboundTargetBytesLeft()
2645 LOCK(cs_totalBytesSent
);
2646 if (nMaxOutboundLimit
== 0)
2649 return (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
) ? 0 : nMaxOutboundLimit
- nMaxOutboundTotalBytesSentInCycle
;
2652 uint64_t CConnman::GetTotalBytesRecv()
2654 LOCK(cs_totalBytesRecv
);
2655 return nTotalBytesRecv
;
2658 uint64_t CConnman::GetTotalBytesSent()
2660 LOCK(cs_totalBytesSent
);
2661 return nTotalBytesSent
;
2664 ServiceFlags
CConnman::GetLocalServices() const
2666 return nLocalServices
;
2669 void CConnman::SetBestHeight(int height
)
2671 nBestHeight
.store(height
, std::memory_order_release
);
2674 int CConnman::GetBestHeight() const
2676 return nBestHeight
.load(std::memory_order_acquire
);
2679 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize
; }
2681 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
) :
2682 nTimeConnected(GetSystemTimeInSeconds()),
2684 addrBind(addrBindIn
),
2685 fInbound(fInboundIn
),
2686 nKeyedNetGroup(nKeyedNetGroupIn
),
2687 addrKnown(5000, 0.001),
2688 filterInventoryKnown(50000, 0.000001),
2690 nLocalHostNonce(nLocalHostNonceIn
),
2691 nLocalServices(nLocalServicesIn
),
2692 nMyStartingHeight(nMyStartingHeightIn
),
2695 nServices
= NODE_NONE
;
2696 hSocket
= hSocketIn
;
2697 nRecvVersion
= INIT_PROTO_VERSION
;
2703 addrName
= addrNameIn
== "" ? addr
.ToStringIPPort() : addrNameIn
;
2706 fWhitelisted
= false;
2708 m_manual_connection
= false;
2709 fClient
= false; // set by version message
2711 fSuccessfullyConnected
= false;
2712 fDisconnect
= false;
2716 hashContinue
= uint256();
2717 nStartingHeight
= -1;
2718 filterInventoryKnown
.reset();
2719 fSendMempool
= false;
2721 nNextLocalAddrSend
= 0;
2726 pfilter
= new CBloomFilter();
2727 timeLastMempoolReq
= 0;
2733 fPingQueued
= false;
2734 nMinPingUsecTime
= std::numeric_limits
<int64_t>::max();
2736 lastSentFeeFilter
= 0;
2737 nextSendTimeFeeFilter
= 0;
2740 nProcessQueueSize
= 0;
2742 for (const std::string
&msg
: getAllNetMessageTypes())
2743 mapRecvBytesPerMsgCmd
[msg
] = 0;
2744 mapRecvBytesPerMsgCmd
[NET_MESSAGE_COMMAND_OTHER
] = 0;
2747 LogPrint(BCLog::NET
, "Added connection to %s peer=%d\n", addrName
, id
);
2749 LogPrint(BCLog::NET
, "Added connection peer=%d\n", id
);
2755 CloseSocket(hSocket
);
2761 void CNode::AskFor(const CInv
& inv
)
2763 if (mapAskFor
.size() > MAPASKFOR_MAX_SZ
|| setAskFor
.size() > SETASKFOR_MAX_SZ
)
2765 // a peer may not have multiple non-responded queue positions for a single inv item
2766 if (!setAskFor
.insert(inv
.hash
).second
)
2769 // We're using mapAskFor as a priority queue,
2770 // the key is the earliest time the request can be sent
2771 int64_t nRequestTime
;
2772 limitedmap
<uint256
, int64_t>::const_iterator it
= mapAlreadyAskedFor
.find(inv
.hash
);
2773 if (it
!= mapAlreadyAskedFor
.end())
2774 nRequestTime
= it
->second
;
2777 LogPrint(BCLog::NET
, "askfor %s %d (%s) peer=%d\n", inv
.ToString(), nRequestTime
, DateTimeStrFormat("%H:%M:%S", nRequestTime
/1000000), id
);
2779 // Make sure not to reuse time indexes to keep things in the same order
2780 int64_t nNow
= GetTimeMicros() - 1000000;
2781 static int64_t nLastTime
;
2783 nNow
= std::max(nNow
, nLastTime
);
2786 // Each retry is 2 minutes after the last
2787 nRequestTime
= std::max(nRequestTime
+ 2 * 60 * 1000000, nNow
);
2788 if (it
!= mapAlreadyAskedFor
.end())
2789 mapAlreadyAskedFor
.update(it
, nRequestTime
);
2791 mapAlreadyAskedFor
.insert(std::make_pair(inv
.hash
, nRequestTime
));
2792 mapAskFor
.insert(std::make_pair(nRequestTime
, inv
));
2795 bool CConnman::NodeFullyConnected(const CNode
* pnode
)
2797 return pnode
&& pnode
->fSuccessfullyConnected
&& !pnode
->fDisconnect
;
2800 void CConnman::PushMessage(CNode
* pnode
, CSerializedNetMsg
&& msg
)
2802 size_t nMessageSize
= msg
.data
.size();
2803 size_t nTotalSize
= nMessageSize
+ CMessageHeader::HEADER_SIZE
;
2804 LogPrint(BCLog::NET
, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg
.command
.c_str()), nMessageSize
, pnode
->GetId());
2806 std::vector
<unsigned char> serializedHeader
;
2807 serializedHeader
.reserve(CMessageHeader::HEADER_SIZE
);
2808 uint256 hash
= Hash(msg
.data
.data(), msg
.data
.data() + nMessageSize
);
2809 CMessageHeader
hdr(Params().MessageStart(), msg
.command
.c_str(), nMessageSize
);
2810 memcpy(hdr
.pchChecksum
, hash
.begin(), CMessageHeader::CHECKSUM_SIZE
);
2812 CVectorWriter
{SER_NETWORK
, INIT_PROTO_VERSION
, serializedHeader
, 0, hdr
};
2814 size_t nBytesSent
= 0;
2816 LOCK(pnode
->cs_vSend
);
2817 bool optimisticSend(pnode
->vSendMsg
.empty());
2819 //log total amount of bytes per command
2820 pnode
->mapSendBytesPerMsgCmd
[msg
.command
] += nTotalSize
;
2821 pnode
->nSendSize
+= nTotalSize
;
2823 if (pnode
->nSendSize
> nSendBufferMaxSize
)
2824 pnode
->fPauseSend
= true;
2825 pnode
->vSendMsg
.push_back(std::move(serializedHeader
));
2827 pnode
->vSendMsg
.push_back(std::move(msg
.data
));
2829 // If write queue empty, attempt "optimistic write"
2830 if (optimisticSend
== true)
2831 nBytesSent
= SocketSendData(pnode
);
2834 RecordBytesSent(nBytesSent
);
2837 bool CConnman::ForNode(NodeId id
, std::function
<bool(CNode
* pnode
)> func
)
2839 CNode
* found
= nullptr;
2841 for (auto&& pnode
: vNodes
) {
2842 if(pnode
->GetId() == id
) {
2847 return found
!= nullptr && NodeFullyConnected(found
) && func(found
);
2850 int64_t PoissonNextSend(int64_t nNow
, int average_interval_seconds
) {
2851 return nNow
+ (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds
* -1000000.0 + 0.5);
2854 CSipHasher
CConnman::GetDeterministicRandomizer(uint64_t id
) const
2856 return CSipHasher(nSeed0
, nSeed1
).Write(id
);
2859 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress
& ad
) const
2861 std::vector
<unsigned char> vchNetGroup(ad
.GetGroup());
2863 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP
).Write(vchNetGroup
.data(), vchNetGroup
.size()).Finalize();