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 const static std::string NET_MESSAGE_COMMAND_OTHER
= "*other*";
69 static const uint64_t RANDOMIZER_ID_NETGROUP
= 0x6c0edd8036ef4036ULL
; // SHA256("netgroup")[0:8]
70 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE
= 0xd93e69e2bbfa5735ULL
; // SHA256("localhostnonce")[0:8]
72 // Global state variables
74 bool fDiscover
= true;
76 bool fRelayTxes
= true;
77 CCriticalSection cs_mapLocalHost
;
78 std::map
<CNetAddr
, LocalServiceInfo
> mapLocalHost
;
79 static bool vfLimited
[NET_MAX
] = {};
80 std::string strSubVersion
;
82 limitedmap
<uint256
, int64_t> mapAlreadyAskedFor(MAX_INV_SZ
);
84 // Signals for message handling
85 static CNodeSignals g_signals
;
86 CNodeSignals
& GetNodeSignals() { return g_signals
; }
88 void CConnman::AddOneShot(const std::string
& strDest
)
91 vOneShots
.push_back(strDest
);
94 unsigned short GetListenPort()
96 return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
99 // find 'best' local address for a particular peer
100 bool GetLocal(CService
& addr
, const CNetAddr
*paddrPeer
)
106 int nBestReachability
= -1;
108 LOCK(cs_mapLocalHost
);
109 for (std::map
<CNetAddr
, LocalServiceInfo
>::iterator it
= mapLocalHost
.begin(); it
!= mapLocalHost
.end(); it
++)
111 int nScore
= (*it
).second
.nScore
;
112 int nReachability
= (*it
).first
.GetReachabilityFrom(paddrPeer
);
113 if (nReachability
> nBestReachability
|| (nReachability
== nBestReachability
&& nScore
> nBestScore
))
115 addr
= CService((*it
).first
, (*it
).second
.nPort
);
116 nBestReachability
= nReachability
;
121 return nBestScore
>= 0;
124 //! Convert the pnSeeds6 array into usable address objects.
125 static std::vector
<CAddress
> convertSeed6(const std::vector
<SeedSpec6
> &vSeedsIn
)
127 // It'll only connect to one or two seed nodes because once it connects,
128 // it'll get a pile of addresses with newer timestamps.
129 // Seed nodes are given a random 'last seen time' of between one and two
131 const int64_t nOneWeek
= 7*24*60*60;
132 std::vector
<CAddress
> vSeedsOut
;
133 vSeedsOut
.reserve(vSeedsIn
.size());
134 for (std::vector
<SeedSpec6
>::const_iterator
i(vSeedsIn
.begin()); i
!= vSeedsIn
.end(); ++i
)
137 memcpy(&ip
, i
->addr
, sizeof(ip
));
138 CAddress
addr(CService(ip
, i
->port
), NODE_NETWORK
);
139 addr
.nTime
= GetTime() - GetRand(nOneWeek
) - nOneWeek
;
140 vSeedsOut
.push_back(addr
);
145 // get best local address for a particular peer as a CAddress
146 // Otherwise, return the unroutable 0.0.0.0 but filled in with
147 // the normal parameters, since the IP may be changed to a useful
149 CAddress
GetLocalAddress(const CNetAddr
*paddrPeer
, ServiceFlags nLocalServices
)
151 CAddress
ret(CService(CNetAddr(),GetListenPort()), nLocalServices
);
153 if (GetLocal(addr
, paddrPeer
))
155 ret
= CAddress(addr
, nLocalServices
);
157 ret
.nTime
= GetAdjustedTime();
161 int GetnScore(const CService
& addr
)
163 LOCK(cs_mapLocalHost
);
164 if (mapLocalHost
.count(addr
) == LOCAL_NONE
)
166 return mapLocalHost
[addr
].nScore
;
169 // Is our peer's addrLocal potentially useful as an external IP source?
170 bool IsPeerAddrLocalGood(CNode
*pnode
)
172 CService addrLocal
= pnode
->GetAddrLocal();
173 return fDiscover
&& pnode
->addr
.IsRoutable() && addrLocal
.IsRoutable() &&
174 !IsLimited(addrLocal
.GetNetwork());
177 // pushes our own address to a peer
178 void AdvertiseLocal(CNode
*pnode
)
180 if (fListen
&& pnode
->fSuccessfullyConnected
)
182 CAddress addrLocal
= GetLocalAddress(&pnode
->addr
, pnode
->GetLocalServices());
183 // If discovery is enabled, sometimes give our peer the address it
184 // tells us that it sees us as in case it has a better idea of our
185 // address than we do.
186 if (IsPeerAddrLocalGood(pnode
) && (!addrLocal
.IsRoutable() ||
187 GetRand((GetnScore(addrLocal
) > LOCAL_MANUAL
) ? 8:2) == 0))
189 addrLocal
.SetIP(pnode
->GetAddrLocal());
191 if (addrLocal
.IsRoutable())
193 LogPrint(BCLog::NET
, "AdvertiseLocal: advertising address %s\n", addrLocal
.ToString());
194 FastRandomContext insecure_rand
;
195 pnode
->PushAddress(addrLocal
, insecure_rand
);
200 // learn a new local address
201 bool AddLocal(const CService
& addr
, int nScore
)
203 if (!addr
.IsRoutable())
206 if (!fDiscover
&& nScore
< LOCAL_MANUAL
)
212 LogPrintf("AddLocal(%s,%i)\n", addr
.ToString(), nScore
);
215 LOCK(cs_mapLocalHost
);
216 bool fAlready
= mapLocalHost
.count(addr
) > 0;
217 LocalServiceInfo
&info
= mapLocalHost
[addr
];
218 if (!fAlready
|| nScore
>= info
.nScore
) {
219 info
.nScore
= nScore
+ (fAlready
? 1 : 0);
220 info
.nPort
= addr
.GetPort();
227 bool AddLocal(const CNetAddr
&addr
, int nScore
)
229 return AddLocal(CService(addr
, GetListenPort()), nScore
);
232 bool RemoveLocal(const CService
& addr
)
234 LOCK(cs_mapLocalHost
);
235 LogPrintf("RemoveLocal(%s)\n", addr
.ToString());
236 mapLocalHost
.erase(addr
);
240 /** Make a particular network entirely off-limits (no automatic connects to it) */
241 void SetLimited(enum Network net
, bool fLimited
)
243 if (net
== NET_UNROUTABLE
)
245 LOCK(cs_mapLocalHost
);
246 vfLimited
[net
] = fLimited
;
249 bool IsLimited(enum Network net
)
251 LOCK(cs_mapLocalHost
);
252 return vfLimited
[net
];
255 bool IsLimited(const CNetAddr
&addr
)
257 return IsLimited(addr
.GetNetwork());
260 /** vote for a local address */
261 bool SeenLocal(const CService
& addr
)
264 LOCK(cs_mapLocalHost
);
265 if (mapLocalHost
.count(addr
) == 0)
267 mapLocalHost
[addr
].nScore
++;
273 /** check whether a given address is potentially local */
274 bool IsLocal(const CService
& addr
)
276 LOCK(cs_mapLocalHost
);
277 return mapLocalHost
.count(addr
) > 0;
280 /** check whether a given network is one we can probably connect to */
281 bool IsReachable(enum Network net
)
283 LOCK(cs_mapLocalHost
);
284 return !vfLimited
[net
];
287 /** check whether a given address is in a network we can probably connect to */
288 bool IsReachable(const CNetAddr
& addr
)
290 enum Network net
= addr
.GetNetwork();
291 return IsReachable(net
);
295 CNode
* CConnman::FindNode(const CNetAddr
& ip
)
298 for (CNode
* pnode
: vNodes
)
299 if ((CNetAddr
)pnode
->addr
== ip
)
304 CNode
* CConnman::FindNode(const CSubNet
& subNet
)
307 for (CNode
* pnode
: vNodes
)
308 if (subNet
.Match((CNetAddr
)pnode
->addr
))
313 CNode
* CConnman::FindNode(const std::string
& addrName
)
316 for (CNode
* pnode
: vNodes
) {
317 if (pnode
->GetAddrName() == addrName
) {
324 CNode
* CConnman::FindNode(const CService
& addr
)
327 for (CNode
* pnode
: vNodes
)
328 if ((CService
)pnode
->addr
== addr
)
333 bool CConnman::CheckIncomingNonce(uint64_t nonce
)
336 for (CNode
* pnode
: vNodes
) {
337 if (!pnode
->fSuccessfullyConnected
&& !pnode
->fInbound
&& pnode
->GetLocalNonce() == nonce
)
343 /** Get the bind address for a socket as CAddress */
344 static CAddress
GetBindAddress(SOCKET sock
)
347 struct sockaddr_storage sockaddr_bind
;
348 socklen_t sockaddr_bind_len
= sizeof(sockaddr_bind
);
349 if (sock
!= INVALID_SOCKET
) {
350 if (!getsockname(sock
, (struct sockaddr
*)&sockaddr_bind
, &sockaddr_bind_len
)) {
351 addr_bind
.SetSockAddr((const struct sockaddr
*)&sockaddr_bind
);
353 LogPrint(BCLog::NET
, "Warning: getsockname failed\n");
359 CNode
* CConnman::ConnectNode(CAddress addrConnect
, const char *pszDest
, bool fCountFailure
)
361 if (pszDest
== NULL
) {
362 if (IsLocal(addrConnect
))
365 // Look for an existing connection
366 CNode
* pnode
= FindNode((CService
)addrConnect
);
369 LogPrintf("Failed to open new connection, already connected\n");
375 LogPrint(BCLog::NET
, "trying connection %s lastseen=%.1fhrs\n",
376 pszDest
? pszDest
: addrConnect
.ToString(),
377 pszDest
? 0.0 : (double)(GetAdjustedTime() - addrConnect
.nTime
)/3600.0);
381 bool proxyConnectionFailed
= false;
382 if (pszDest
? ConnectSocketByName(addrConnect
, hSocket
, pszDest
, Params().GetDefaultPort(), nConnectTimeout
, &proxyConnectionFailed
) :
383 ConnectSocket(addrConnect
, hSocket
, nConnectTimeout
, &proxyConnectionFailed
))
385 if (!IsSelectableSocket(hSocket
)) {
386 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
387 CloseSocket(hSocket
);
391 if (pszDest
&& addrConnect
.IsValid()) {
392 // It is possible that we already have a connection to the IP/port pszDest resolved to.
393 // In that case, drop the connection that was just created, and return the existing CNode instead.
394 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
395 // name catch this early.
397 CNode
* pnode
= FindNode((CService
)addrConnect
);
400 pnode
->MaybeSetAddrName(std::string(pszDest
));
401 CloseSocket(hSocket
);
402 LogPrintf("Failed to open new connection, already connected\n");
407 addrman
.Attempt(addrConnect
, fCountFailure
);
410 NodeId id
= GetNewNodeId();
411 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
412 CAddress addr_bind
= GetBindAddress(hSocket
);
413 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addrConnect
, CalculateKeyedNetGroup(addrConnect
), nonce
, addr_bind
, pszDest
? pszDest
: "", false);
414 pnode
->nServicesExpected
= ServiceFlags(addrConnect
.nServices
& nRelevantServices
);
418 } else if (!proxyConnectionFailed
) {
419 // If connecting to the node failed, and failure is not caused by a problem connecting to
420 // the proxy, mark this as an attempt.
421 addrman
.Attempt(addrConnect
, fCountFailure
);
427 void CConnman::DumpBanlist()
429 SweepBanned(); // clean unused entries (if bantime has expired)
431 if (!BannedSetIsDirty())
434 int64_t nStart
= GetTimeMillis();
439 if (bandb
.Write(banmap
)) {
440 SetBannedSetDirty(false);
443 LogPrint(BCLog::NET
, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
444 banmap
.size(), GetTimeMillis() - nStart
);
447 void CNode::CloseSocketDisconnect()
451 if (hSocket
!= INVALID_SOCKET
)
453 LogPrint(BCLog::NET
, "disconnecting peer=%d\n", id
);
454 CloseSocket(hSocket
);
458 void CConnman::ClearBanned()
463 setBannedIsDirty
= true;
465 DumpBanlist(); //store banlist to disk
467 clientInterface
->BannedListChanged();
470 bool CConnman::IsBanned(CNetAddr ip
)
473 for (banmap_t::iterator it
= setBanned
.begin(); it
!= setBanned
.end(); it
++)
475 CSubNet subNet
= (*it
).first
;
476 CBanEntry banEntry
= (*it
).second
;
478 if (subNet
.Match(ip
) && GetTime() < banEntry
.nBanUntil
) {
485 bool CConnman::IsBanned(CSubNet subnet
)
488 banmap_t::iterator i
= setBanned
.find(subnet
);
489 if (i
!= setBanned
.end())
491 CBanEntry banEntry
= (*i
).second
;
492 if (GetTime() < banEntry
.nBanUntil
) {
499 void CConnman::Ban(const CNetAddr
& addr
, const BanReason
&banReason
, int64_t bantimeoffset
, bool sinceUnixEpoch
) {
500 CSubNet
subNet(addr
);
501 Ban(subNet
, banReason
, bantimeoffset
, sinceUnixEpoch
);
504 void CConnman::Ban(const CSubNet
& subNet
, const BanReason
&banReason
, int64_t bantimeoffset
, bool sinceUnixEpoch
) {
505 CBanEntry
banEntry(GetTime());
506 banEntry
.banReason
= banReason
;
507 if (bantimeoffset
<= 0)
509 bantimeoffset
= GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME
);
510 sinceUnixEpoch
= false;
512 banEntry
.nBanUntil
= (sinceUnixEpoch
? 0 : GetTime() )+bantimeoffset
;
516 if (setBanned
[subNet
].nBanUntil
< banEntry
.nBanUntil
) {
517 setBanned
[subNet
] = banEntry
;
518 setBannedIsDirty
= true;
524 clientInterface
->BannedListChanged();
527 for (CNode
* pnode
: vNodes
) {
528 if (subNet
.Match((CNetAddr
)pnode
->addr
))
529 pnode
->fDisconnect
= true;
532 if(banReason
== BanReasonManuallyAdded
)
533 DumpBanlist(); //store banlist to disk immediately if user requested ban
536 bool CConnman::Unban(const CNetAddr
&addr
) {
537 CSubNet
subNet(addr
);
538 return Unban(subNet
);
541 bool CConnman::Unban(const CSubNet
&subNet
) {
544 if (!setBanned
.erase(subNet
))
546 setBannedIsDirty
= true;
549 clientInterface
->BannedListChanged();
550 DumpBanlist(); //store banlist to disk immediately
554 void CConnman::GetBanned(banmap_t
&banMap
)
557 // Sweep the banlist so expired bans are not returned
559 banMap
= setBanned
; //create a thread safe copy
562 void CConnman::SetBanned(const banmap_t
&banMap
)
566 setBannedIsDirty
= true;
569 void CConnman::SweepBanned()
571 int64_t now
= GetTime();
574 banmap_t::iterator it
= setBanned
.begin();
575 while(it
!= setBanned
.end())
577 CSubNet subNet
= (*it
).first
;
578 CBanEntry banEntry
= (*it
).second
;
579 if(now
> banEntry
.nBanUntil
)
581 setBanned
.erase(it
++);
582 setBannedIsDirty
= true;
583 LogPrint(BCLog::NET
, "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__
, subNet
.ToString());
590 bool CConnman::BannedSetIsDirty()
593 return setBannedIsDirty
;
596 void CConnman::SetBannedSetDirty(bool dirty
)
598 LOCK(cs_setBanned
); //reuse setBanned lock for the isDirty flag
599 setBannedIsDirty
= dirty
;
603 bool CConnman::IsWhitelistedRange(const CNetAddr
&addr
) {
604 LOCK(cs_vWhitelistedRange
);
605 for (const CSubNet
& subnet
: vWhitelistedRange
) {
606 if (subnet
.Match(addr
))
612 void CConnman::AddWhitelistedRange(const CSubNet
&subnet
) {
613 LOCK(cs_vWhitelistedRange
);
614 vWhitelistedRange
.push_back(subnet
);
618 std::string
CNode::GetAddrName() const {
623 void CNode::MaybeSetAddrName(const std::string
& addrNameIn
) {
625 if (addrName
.empty()) {
626 addrName
= addrNameIn
;
630 CService
CNode::GetAddrLocal() const {
635 void CNode::SetAddrLocal(const CService
& addrLocalIn
) {
637 if (addrLocal
.IsValid()) {
638 error("Addr local already set for node: %i. Refusing to change from %s to %s", id
, addrLocal
.ToString(), addrLocalIn
.ToString());
640 addrLocal
= addrLocalIn
;
645 #define X(name) stats.name = name
646 void CNode::copyStats(CNodeStats
&stats
)
648 stats
.nodeid
= this->GetId();
660 stats
.addrName
= GetAddrName();
671 X(mapSendBytesPerMsgCmd
);
676 X(mapRecvBytesPerMsgCmd
);
681 // It is common for nodes with good ping times to suddenly become lagged,
682 // due to a new block arriving or other large transfer.
683 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
684 // since pingtime does not update until the ping is complete, which might take a while.
685 // So, if a ping is taking an unusually long time in flight,
686 // the caller can immediately detect that this is happening.
687 int64_t nPingUsecWait
= 0;
688 if ((0 != nPingNonceSent
) && (0 != nPingUsecStart
)) {
689 nPingUsecWait
= GetTimeMicros() - nPingUsecStart
;
692 // 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 :)
693 stats
.dPingTime
= (((double)nPingUsecTime
) / 1e6
);
694 stats
.dMinPing
= (((double)nMinPingUsecTime
) / 1e6
);
695 stats
.dPingWait
= (((double)nPingUsecWait
) / 1e6
);
697 // Leave string empty if addrLocal invalid (not filled in yet)
698 CService addrLocalUnlocked
= GetAddrLocal();
699 stats
.addrLocal
= addrLocalUnlocked
.IsValid() ? addrLocalUnlocked
.ToString() : "";
703 bool CNode::ReceiveMsgBytes(const char *pch
, unsigned int nBytes
, bool& complete
)
706 int64_t nTimeMicros
= GetTimeMicros();
708 nLastRecv
= nTimeMicros
/ 1000000;
709 nRecvBytes
+= nBytes
;
712 // get current incomplete message, or create a new one
713 if (vRecvMsg
.empty() ||
714 vRecvMsg
.back().complete())
715 vRecvMsg
.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK
, INIT_PROTO_VERSION
));
717 CNetMessage
& msg
= vRecvMsg
.back();
719 // absorb network data
722 handled
= msg
.readHeader(pch
, nBytes
);
724 handled
= msg
.readData(pch
, nBytes
);
729 if (msg
.in_data
&& msg
.hdr
.nMessageSize
> MAX_PROTOCOL_MESSAGE_LENGTH
) {
730 LogPrint(BCLog::NET
, "Oversized message from peer=%i, disconnecting\n", GetId());
737 if (msg
.complete()) {
739 //store received bytes per message command
740 //to prevent a memory DOS, only allow valid commands
741 mapMsgCmdSize::iterator i
= mapRecvBytesPerMsgCmd
.find(msg
.hdr
.pchCommand
);
742 if (i
== mapRecvBytesPerMsgCmd
.end())
743 i
= mapRecvBytesPerMsgCmd
.find(NET_MESSAGE_COMMAND_OTHER
);
744 assert(i
!= mapRecvBytesPerMsgCmd
.end());
745 i
->second
+= msg
.hdr
.nMessageSize
+ CMessageHeader::HEADER_SIZE
;
747 msg
.nTime
= nTimeMicros
;
755 void CNode::SetSendVersion(int nVersionIn
)
757 // Send version may only be changed in the version message, and
758 // only one version message is allowed per session. We can therefore
759 // treat this value as const and even atomic as long as it's only used
760 // once a version message has been successfully processed. Any attempt to
761 // set this twice is an error.
762 if (nSendVersion
!= 0) {
763 error("Send version already set for node: %i. Refusing to change from %i to %i", id
, nSendVersion
, nVersionIn
);
765 nSendVersion
= nVersionIn
;
769 int CNode::GetSendVersion() const
771 // The send version should always be explicitly set to
772 // INIT_PROTO_VERSION rather than using this value until SetSendVersion
774 if (nSendVersion
== 0) {
775 error("Requesting unset send version for node: %i. Using %i", id
, INIT_PROTO_VERSION
);
776 return INIT_PROTO_VERSION
;
782 int CNetMessage::readHeader(const char *pch
, unsigned int nBytes
)
784 // copy data to temporary parsing buffer
785 unsigned int nRemaining
= 24 - nHdrPos
;
786 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
788 memcpy(&hdrbuf
[nHdrPos
], pch
, nCopy
);
791 // if header incomplete, exit
795 // deserialize to CMessageHeader
799 catch (const std::exception
&) {
803 // reject messages larger than MAX_SIZE
804 if (hdr
.nMessageSize
> MAX_SIZE
)
807 // switch state to reading message data
813 int CNetMessage::readData(const char *pch
, unsigned int nBytes
)
815 unsigned int nRemaining
= hdr
.nMessageSize
- nDataPos
;
816 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
818 if (vRecv
.size() < nDataPos
+ nCopy
) {
819 // Allocate up to 256 KiB ahead, but never more than the total message size.
820 vRecv
.resize(std::min(hdr
.nMessageSize
, nDataPos
+ nCopy
+ 256 * 1024));
823 hasher
.Write((const unsigned char*)pch
, nCopy
);
824 memcpy(&vRecv
[nDataPos
], pch
, nCopy
);
830 const uint256
& CNetMessage::GetMessageHash() const
833 if (data_hash
.IsNull())
834 hasher
.Finalize(data_hash
.begin());
846 // requires LOCK(cs_vSend)
847 size_t CConnman::SocketSendData(CNode
*pnode
) const
849 auto it
= pnode
->vSendMsg
.begin();
850 size_t nSentSize
= 0;
852 while (it
!= pnode
->vSendMsg
.end()) {
853 const auto &data
= *it
;
854 assert(data
.size() > pnode
->nSendOffset
);
857 LOCK(pnode
->cs_hSocket
);
858 if (pnode
->hSocket
== INVALID_SOCKET
)
860 nBytes
= send(pnode
->hSocket
, reinterpret_cast<const char*>(data
.data()) + pnode
->nSendOffset
, data
.size() - pnode
->nSendOffset
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
863 pnode
->nLastSend
= GetSystemTimeInSeconds();
864 pnode
->nSendBytes
+= nBytes
;
865 pnode
->nSendOffset
+= nBytes
;
867 if (pnode
->nSendOffset
== data
.size()) {
868 pnode
->nSendOffset
= 0;
869 pnode
->nSendSize
-= data
.size();
870 pnode
->fPauseSend
= pnode
->nSendSize
> nSendBufferMaxSize
;
873 // could not send full message; stop sending more
879 int nErr
= WSAGetLastError();
880 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
882 LogPrintf("socket send error %s\n", NetworkErrorString(nErr
));
883 pnode
->CloseSocketDisconnect();
886 // couldn't send anything at all
891 if (it
== pnode
->vSendMsg
.end()) {
892 assert(pnode
->nSendOffset
== 0);
893 assert(pnode
->nSendSize
== 0);
895 pnode
->vSendMsg
.erase(pnode
->vSendMsg
.begin(), it
);
899 struct NodeEvictionCandidate
902 int64_t nTimeConnected
;
903 int64_t nMinPingUsecTime
;
904 int64_t nLastBlockTime
;
906 bool fRelevantServices
;
910 uint64_t nKeyedNetGroup
;
913 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
915 return a
.nMinPingUsecTime
> b
.nMinPingUsecTime
;
918 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
920 return a
.nTimeConnected
> b
.nTimeConnected
;
923 static bool CompareNetGroupKeyed(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
) {
924 return a
.nKeyedNetGroup
< b
.nKeyedNetGroup
;
927 static bool CompareNodeBlockTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
929 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
930 if (a
.nLastBlockTime
!= b
.nLastBlockTime
) return a
.nLastBlockTime
< b
.nLastBlockTime
;
931 if (a
.fRelevantServices
!= b
.fRelevantServices
) return b
.fRelevantServices
;
932 return a
.nTimeConnected
> b
.nTimeConnected
;
935 static bool CompareNodeTXTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
937 // 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.
938 if (a
.nLastTXTime
!= b
.nLastTXTime
) return a
.nLastTXTime
< b
.nLastTXTime
;
939 if (a
.fRelayTxes
!= b
.fRelayTxes
) return b
.fRelayTxes
;
940 if (a
.fBloomFilter
!= b
.fBloomFilter
) return a
.fBloomFilter
;
941 return a
.nTimeConnected
> b
.nTimeConnected
;
944 /** Try to find a connection to evict when the node is full.
945 * Extreme care must be taken to avoid opening the node to attacker
946 * triggered network partitioning.
947 * The strategy used here is to protect a small number of peers
948 * for each of several distinct characteristics which are difficult
949 * to forge. In order to partition a node the attacker must be
950 * simultaneously better at all of them than honest peers.
952 bool CConnman::AttemptToEvictConnection()
954 std::vector
<NodeEvictionCandidate
> vEvictionCandidates
;
958 for (CNode
*node
: vNodes
) {
959 if (node
->fWhitelisted
)
963 if (node
->fDisconnect
)
965 NodeEvictionCandidate candidate
= {node
->GetId(), node
->nTimeConnected
, node
->nMinPingUsecTime
,
966 node
->nLastBlockTime
, node
->nLastTXTime
,
967 (node
->nServices
& nRelevantServices
) == nRelevantServices
,
968 node
->fRelayTxes
, node
->pfilter
!= NULL
, node
->addr
, node
->nKeyedNetGroup
};
969 vEvictionCandidates
.push_back(candidate
);
973 if (vEvictionCandidates
.empty()) return false;
975 // Protect connections with certain characteristics
977 // Deterministically select 4 peers to protect by netgroup.
978 // An attacker cannot predict which netgroups will be protected
979 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNetGroupKeyed
);
980 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
982 if (vEvictionCandidates
.empty()) return false;
984 // Protect the 8 nodes with the lowest minimum ping time.
985 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
986 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), ReverseCompareNodeMinPingTime
);
987 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(8, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
989 if (vEvictionCandidates
.empty()) return false;
991 // Protect 4 nodes that most recently sent us transactions.
992 // An attacker cannot manipulate this metric without performing useful work.
993 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNodeTXTime
);
994 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
996 if (vEvictionCandidates
.empty()) return false;
998 // Protect 4 nodes that most recently sent us blocks.
999 // An attacker cannot manipulate this metric without performing useful work.
1000 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNodeBlockTime
);
1001 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
1003 if (vEvictionCandidates
.empty()) return false;
1005 // Protect the half of the remaining nodes which have been connected the longest.
1006 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
1007 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), ReverseCompareNodeTimeConnected
);
1008 vEvictionCandidates
.erase(vEvictionCandidates
.end() - static_cast<int>(vEvictionCandidates
.size() / 2), vEvictionCandidates
.end());
1010 if (vEvictionCandidates
.empty()) return false;
1012 // Identify the network group with the most connections and youngest member.
1013 // (vEvictionCandidates is already sorted by reverse connect time)
1014 uint64_t naMostConnections
;
1015 unsigned int nMostConnections
= 0;
1016 int64_t nMostConnectionsTime
= 0;
1017 std::map
<uint64_t, std::vector
<NodeEvictionCandidate
> > mapNetGroupNodes
;
1018 for (const NodeEvictionCandidate
&node
: vEvictionCandidates
) {
1019 mapNetGroupNodes
[node
.nKeyedNetGroup
].push_back(node
);
1020 int64_t grouptime
= mapNetGroupNodes
[node
.nKeyedNetGroup
][0].nTimeConnected
;
1021 size_t groupsize
= mapNetGroupNodes
[node
.nKeyedNetGroup
].size();
1023 if (groupsize
> nMostConnections
|| (groupsize
== nMostConnections
&& grouptime
> nMostConnectionsTime
)) {
1024 nMostConnections
= groupsize
;
1025 nMostConnectionsTime
= grouptime
;
1026 naMostConnections
= node
.nKeyedNetGroup
;
1030 // Reduce to the network group with the most connections
1031 vEvictionCandidates
= std::move(mapNetGroupNodes
[naMostConnections
]);
1033 // Disconnect from the network group with the most connections
1034 NodeId evicted
= vEvictionCandidates
.front().id
;
1036 for(std::vector
<CNode
*>::const_iterator
it(vNodes
.begin()); it
!= vNodes
.end(); ++it
) {
1037 if ((*it
)->GetId() == evicted
) {
1038 (*it
)->fDisconnect
= true;
1045 void CConnman::AcceptConnection(const ListenSocket
& hListenSocket
) {
1046 struct sockaddr_storage sockaddr
;
1047 socklen_t len
= sizeof(sockaddr
);
1048 SOCKET hSocket
= accept(hListenSocket
.socket
, (struct sockaddr
*)&sockaddr
, &len
);
1051 int nMaxInbound
= nMaxConnections
- (nMaxOutbound
+ nMaxFeeler
);
1053 if (hSocket
!= INVALID_SOCKET
) {
1054 if (!addr
.SetSockAddr((const struct sockaddr
*)&sockaddr
)) {
1055 LogPrintf("Warning: Unknown socket family\n");
1059 bool whitelisted
= hListenSocket
.whitelisted
|| IsWhitelistedRange(addr
);
1062 for (CNode
* pnode
: vNodes
)
1063 if (pnode
->fInbound
)
1067 if (hSocket
== INVALID_SOCKET
)
1069 int nErr
= WSAGetLastError();
1070 if (nErr
!= WSAEWOULDBLOCK
)
1071 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr
));
1075 if (!fNetworkActive
) {
1076 LogPrintf("connection from %s dropped: not accepting new connections\n", addr
.ToString());
1077 CloseSocket(hSocket
);
1081 if (!IsSelectableSocket(hSocket
))
1083 LogPrintf("connection from %s dropped: non-selectable socket\n", addr
.ToString());
1084 CloseSocket(hSocket
);
1088 // According to the internet TCP_NODELAY is not carried into accepted sockets
1089 // on all platforms. Set it again here just to be sure.
1090 SetSocketNoDelay(hSocket
);
1092 if (IsBanned(addr
) && !whitelisted
)
1094 LogPrintf("connection from %s dropped (banned)\n", addr
.ToString());
1095 CloseSocket(hSocket
);
1099 if (nInbound
>= nMaxInbound
)
1101 if (!AttemptToEvictConnection()) {
1102 // No connection to evict, disconnect the new connection
1103 LogPrint(BCLog::NET
, "failed to find an eviction candidate - connection dropped (full)\n");
1104 CloseSocket(hSocket
);
1109 NodeId id
= GetNewNodeId();
1110 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
1111 CAddress addr_bind
= GetBindAddress(hSocket
);
1113 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addr
, CalculateKeyedNetGroup(addr
), nonce
, addr_bind
, "", true);
1115 pnode
->fWhitelisted
= whitelisted
;
1116 GetNodeSignals().InitializeNode(pnode
, *this);
1118 LogPrint(BCLog::NET
, "connection from %s accepted\n", addr
.ToString());
1122 vNodes
.push_back(pnode
);
1126 void CConnman::ThreadSocketHandler()
1128 unsigned int nPrevNodeCount
= 0;
1129 while (!interruptNet
)
1136 // Disconnect unused nodes
1137 std::vector
<CNode
*> vNodesCopy
= vNodes
;
1138 for (CNode
* pnode
: vNodesCopy
)
1140 if (pnode
->fDisconnect
)
1142 // remove from vNodes
1143 vNodes
.erase(remove(vNodes
.begin(), vNodes
.end(), pnode
), vNodes
.end());
1145 // release outbound grant (if any)
1146 pnode
->grantOutbound
.Release();
1148 // close socket and cleanup
1149 pnode
->CloseSocketDisconnect();
1151 // hold in disconnected pool until all refs are released
1153 vNodesDisconnected
.push_back(pnode
);
1158 // Delete disconnected nodes
1159 std::list
<CNode
*> vNodesDisconnectedCopy
= vNodesDisconnected
;
1160 for (CNode
* pnode
: vNodesDisconnectedCopy
)
1162 // wait until threads are done using it
1163 if (pnode
->GetRefCount() <= 0) {
1164 bool fDelete
= false;
1166 TRY_LOCK(pnode
->cs_inventory
, lockInv
);
1168 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1175 vNodesDisconnected
.remove(pnode
);
1184 vNodesSize
= vNodes
.size();
1186 if(vNodesSize
!= nPrevNodeCount
) {
1187 nPrevNodeCount
= vNodesSize
;
1189 clientInterface
->NotifyNumConnectionsChanged(nPrevNodeCount
);
1193 // Find which sockets have data to receive
1195 struct timeval timeout
;
1197 timeout
.tv_usec
= 50000; // frequency to poll pnode->vSend
1202 FD_ZERO(&fdsetRecv
);
1203 FD_ZERO(&fdsetSend
);
1204 FD_ZERO(&fdsetError
);
1205 SOCKET hSocketMax
= 0;
1206 bool have_fds
= false;
1208 for (const ListenSocket
& hListenSocket
: vhListenSocket
) {
1209 FD_SET(hListenSocket
.socket
, &fdsetRecv
);
1210 hSocketMax
= std::max(hSocketMax
, hListenSocket
.socket
);
1216 for (CNode
* pnode
: vNodes
)
1218 // Implement the following logic:
1219 // * If there is data to send, select() for sending data. As this only
1220 // happens when optimistic write failed, we choose to first drain the
1221 // write buffer in this case before receiving more. This avoids
1222 // needlessly queueing received data, if the remote peer is not themselves
1223 // receiving data. This means properly utilizing TCP flow control signalling.
1224 // * Otherwise, if there is space left in the receive buffer, select() for
1226 // * Hand off all complete messages to the processor, to be handled without
1229 bool select_recv
= !pnode
->fPauseRecv
;
1232 LOCK(pnode
->cs_vSend
);
1233 select_send
= !pnode
->vSendMsg
.empty();
1236 LOCK(pnode
->cs_hSocket
);
1237 if (pnode
->hSocket
== INVALID_SOCKET
)
1240 FD_SET(pnode
->hSocket
, &fdsetError
);
1241 hSocketMax
= std::max(hSocketMax
, pnode
->hSocket
);
1245 FD_SET(pnode
->hSocket
, &fdsetSend
);
1249 FD_SET(pnode
->hSocket
, &fdsetRecv
);
1254 int nSelect
= select(have_fds
? hSocketMax
+ 1 : 0,
1255 &fdsetRecv
, &fdsetSend
, &fdsetError
, &timeout
);
1259 if (nSelect
== SOCKET_ERROR
)
1263 int nErr
= WSAGetLastError();
1264 LogPrintf("socket select error %s\n", NetworkErrorString(nErr
));
1265 for (unsigned int i
= 0; i
<= hSocketMax
; i
++)
1266 FD_SET(i
, &fdsetRecv
);
1268 FD_ZERO(&fdsetSend
);
1269 FD_ZERO(&fdsetError
);
1270 if (!interruptNet
.sleep_for(std::chrono::milliseconds(timeout
.tv_usec
/1000)))
1275 // Accept new connections
1277 for (const ListenSocket
& hListenSocket
: vhListenSocket
)
1279 if (hListenSocket
.socket
!= INVALID_SOCKET
&& FD_ISSET(hListenSocket
.socket
, &fdsetRecv
))
1281 AcceptConnection(hListenSocket
);
1286 // Service each socket
1288 std::vector
<CNode
*> vNodesCopy
;
1291 vNodesCopy
= vNodes
;
1292 for (CNode
* pnode
: vNodesCopy
)
1295 for (CNode
* pnode
: vNodesCopy
)
1303 bool recvSet
= false;
1304 bool sendSet
= false;
1305 bool errorSet
= false;
1307 LOCK(pnode
->cs_hSocket
);
1308 if (pnode
->hSocket
== INVALID_SOCKET
)
1310 recvSet
= FD_ISSET(pnode
->hSocket
, &fdsetRecv
);
1311 sendSet
= FD_ISSET(pnode
->hSocket
, &fdsetSend
);
1312 errorSet
= FD_ISSET(pnode
->hSocket
, &fdsetError
);
1314 if (recvSet
|| errorSet
)
1316 // typical socket buffer is 8K-64K
1317 char pchBuf
[0x10000];
1320 LOCK(pnode
->cs_hSocket
);
1321 if (pnode
->hSocket
== INVALID_SOCKET
)
1323 nBytes
= recv(pnode
->hSocket
, pchBuf
, sizeof(pchBuf
), MSG_DONTWAIT
);
1327 bool notify
= false;
1328 if (!pnode
->ReceiveMsgBytes(pchBuf
, nBytes
, notify
))
1329 pnode
->CloseSocketDisconnect();
1330 RecordBytesRecv(nBytes
);
1332 size_t nSizeAdded
= 0;
1333 auto it(pnode
->vRecvMsg
.begin());
1334 for (; it
!= pnode
->vRecvMsg
.end(); ++it
) {
1335 if (!it
->complete())
1337 nSizeAdded
+= it
->vRecv
.size() + CMessageHeader::HEADER_SIZE
;
1340 LOCK(pnode
->cs_vProcessMsg
);
1341 pnode
->vProcessMsg
.splice(pnode
->vProcessMsg
.end(), pnode
->vRecvMsg
, pnode
->vRecvMsg
.begin(), it
);
1342 pnode
->nProcessQueueSize
+= nSizeAdded
;
1343 pnode
->fPauseRecv
= pnode
->nProcessQueueSize
> nReceiveFloodSize
;
1345 WakeMessageHandler();
1348 else if (nBytes
== 0)
1350 // socket closed gracefully
1351 if (!pnode
->fDisconnect
) {
1352 LogPrint(BCLog::NET
, "socket closed\n");
1354 pnode
->CloseSocketDisconnect();
1356 else if (nBytes
< 0)
1359 int nErr
= WSAGetLastError();
1360 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
1362 if (!pnode
->fDisconnect
)
1363 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr
));
1364 pnode
->CloseSocketDisconnect();
1374 LOCK(pnode
->cs_vSend
);
1375 size_t nBytes
= SocketSendData(pnode
);
1377 RecordBytesSent(nBytes
);
1382 // Inactivity checking
1384 int64_t nTime
= GetSystemTimeInSeconds();
1385 if (nTime
- pnode
->nTimeConnected
> 60)
1387 if (pnode
->nLastRecv
== 0 || pnode
->nLastSend
== 0)
1389 LogPrint(BCLog::NET
, "socket no message in first 60 seconds, %d %d from %d\n", pnode
->nLastRecv
!= 0, pnode
->nLastSend
!= 0, pnode
->GetId());
1390 pnode
->fDisconnect
= true;
1392 else if (nTime
- pnode
->nLastSend
> TIMEOUT_INTERVAL
)
1394 LogPrintf("socket sending timeout: %is\n", nTime
- pnode
->nLastSend
);
1395 pnode
->fDisconnect
= true;
1397 else if (nTime
- pnode
->nLastRecv
> (pnode
->nVersion
> BIP0031_VERSION
? TIMEOUT_INTERVAL
: 90*60))
1399 LogPrintf("socket receive timeout: %is\n", nTime
- pnode
->nLastRecv
);
1400 pnode
->fDisconnect
= true;
1402 else if (pnode
->nPingNonceSent
&& pnode
->nPingUsecStart
+ TIMEOUT_INTERVAL
* 1000000 < GetTimeMicros())
1404 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode
->nPingUsecStart
));
1405 pnode
->fDisconnect
= true;
1407 else if (!pnode
->fSuccessfullyConnected
)
1409 LogPrintf("version handshake timeout from %d\n", pnode
->GetId());
1410 pnode
->fDisconnect
= true;
1416 for (CNode
* pnode
: vNodesCopy
)
1422 void CConnman::WakeMessageHandler()
1425 std::lock_guard
<std::mutex
> lock(mutexMsgProc
);
1426 fMsgProcWake
= true;
1428 condMsgProc
.notify_one();
1437 void ThreadMapPort()
1439 std::string port
= strprintf("%u", GetListenPort());
1440 const char * multicastif
= 0;
1441 const char * minissdpdpath
= 0;
1442 struct UPNPDev
* devlist
= 0;
1445 #ifndef UPNPDISCOVER_SUCCESS
1447 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0);
1448 #elif MINIUPNPC_API_VERSION < 14
1451 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, &error
);
1453 /* miniupnpc 1.9.20150730 */
1455 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, 2, &error
);
1458 struct UPNPUrls urls
;
1459 struct IGDdatas data
;
1462 r
= UPNP_GetValidIGD(devlist
, &urls
, &data
, lanaddr
, sizeof(lanaddr
));
1466 char externalIPAddress
[40];
1467 r
= UPNP_GetExternalIPAddress(urls
.controlURL
, data
.first
.servicetype
, externalIPAddress
);
1468 if(r
!= UPNPCOMMAND_SUCCESS
)
1469 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r
);
1472 if(externalIPAddress
[0])
1475 if(LookupHost(externalIPAddress
, resolved
, false)) {
1476 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved
.ToString().c_str());
1477 AddLocal(resolved
, LOCAL_UPNP
);
1481 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1485 std::string strDesc
= "Bitcoin " + FormatFullVersion();
1489 #ifndef UPNPDISCOVER_SUCCESS
1491 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1492 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0);
1495 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1496 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0, "0");
1499 if(r
!=UPNPCOMMAND_SUCCESS
)
1500 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1501 port
, port
, lanaddr
, r
, strupnperror(r
));
1503 LogPrintf("UPnP Port Mapping successful.\n");
1505 MilliSleep(20*60*1000); // Refresh every 20 minutes
1508 catch (const boost::thread_interrupted
&)
1510 r
= UPNP_DeletePortMapping(urls
.controlURL
, data
.first
.servicetype
, port
.c_str(), "TCP", 0);
1511 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r
);
1512 freeUPNPDevlist(devlist
); devlist
= 0;
1513 FreeUPNPUrls(&urls
);
1517 LogPrintf("No valid UPnP IGDs found\n");
1518 freeUPNPDevlist(devlist
); devlist
= 0;
1520 FreeUPNPUrls(&urls
);
1524 void MapPort(bool fUseUPnP
)
1526 static boost::thread
* upnp_thread
= NULL
;
1531 upnp_thread
->interrupt();
1532 upnp_thread
->join();
1535 upnp_thread
= new boost::thread(boost::bind(&TraceThread
<void (*)()>, "upnp", &ThreadMapPort
));
1537 else if (upnp_thread
) {
1538 upnp_thread
->interrupt();
1539 upnp_thread
->join();
1548 // Intentionally left blank.
1557 static std::string
GetDNSHost(const CDNSSeedData
& data
, ServiceFlags
* requiredServiceBits
)
1559 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1560 if (!data
.supportsServiceBitsFiltering
|| *requiredServiceBits
== NODE_NETWORK
) {
1561 *requiredServiceBits
= NODE_NETWORK
;
1565 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1566 return strprintf("x%x.%s", *requiredServiceBits
, data
.host
);
1570 void CConnman::ThreadDNSAddressSeed()
1572 // goal: only query DNS seeds if address need is acute
1573 // Avoiding DNS seeds when we don't need them improves user privacy by
1574 // creating fewer identifying DNS requests, reduces trust by giving seeds
1575 // less influence on the network topology, and reduces traffic to the seeds.
1576 if ((addrman
.size() > 0) &&
1577 (!GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED
))) {
1578 if (!interruptNet
.sleep_for(std::chrono::seconds(11)))
1583 for (auto pnode
: vNodes
) {
1584 nRelevant
+= pnode
->fSuccessfullyConnected
&& ((pnode
->nServices
& nRelevantServices
) == nRelevantServices
);
1586 if (nRelevant
>= 2) {
1587 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1592 const std::vector
<CDNSSeedData
> &vSeeds
= Params().DNSSeeds();
1595 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1597 for (const CDNSSeedData
&seed
: vSeeds
) {
1601 if (HaveNameProxy()) {
1602 AddOneShot(seed
.host
);
1604 std::vector
<CNetAddr
> vIPs
;
1605 std::vector
<CAddress
> vAdd
;
1606 ServiceFlags requiredServiceBits
= nRelevantServices
;
1607 if (LookupHost(GetDNSHost(seed
, &requiredServiceBits
).c_str(), vIPs
, 0, true))
1609 for (const CNetAddr
& ip
: vIPs
)
1611 int nOneDay
= 24*3600;
1612 CAddress addr
= CAddress(CService(ip
, Params().GetDefaultPort()), requiredServiceBits
);
1613 addr
.nTime
= GetTime() - 3*nOneDay
- GetRand(4*nOneDay
); // use a random age between 3 and 7 days old
1614 vAdd
.push_back(addr
);
1621 // TODO: The seed name resolve may fail, yielding an IP of [::], which results in
1622 // addrman assigning the same source to results from different seeds.
1623 // This should switch to a hard-coded stable dummy IP for each seed name, so that the
1624 // resolve is not required at all.
1625 if (!vIPs
.empty()) {
1626 CService seedSource
;
1627 Lookup(seed
.name
.c_str(), seedSource
, 0, true);
1628 addrman
.Add(vAdd
, seedSource
);
1633 LogPrintf("%d addresses found from DNS seeds\n", found
);
1647 void CConnman::DumpAddresses()
1649 int64_t nStart
= GetTimeMillis();
1654 LogPrint(BCLog::NET
, "Flushed %d addresses to peers.dat %dms\n",
1655 addrman
.size(), GetTimeMillis() - nStart
);
1658 void CConnman::DumpData()
1664 void CConnman::ProcessOneShot()
1666 std::string strDest
;
1669 if (vOneShots
.empty())
1671 strDest
= vOneShots
.front();
1672 vOneShots
.pop_front();
1675 CSemaphoreGrant
grant(*semOutbound
, true);
1677 if (!OpenNetworkConnection(addr
, false, &grant
, strDest
.c_str(), true))
1678 AddOneShot(strDest
);
1682 void CConnman::ThreadOpenConnections()
1684 // Connect to specific addresses
1685 if (gArgs
.IsArgSet("-connect") && gArgs
.GetArgs("-connect").size() > 0)
1687 for (int64_t nLoop
= 0;; nLoop
++)
1690 for (const std::string
& strAddr
: gArgs
.GetArgs("-connect"))
1692 CAddress
addr(CService(), NODE_NONE
);
1693 OpenNetworkConnection(addr
, false, NULL
, strAddr
.c_str());
1694 for (int i
= 0; i
< 10 && i
< nLoop
; i
++)
1696 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1700 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1705 // Initiate network connections
1706 int64_t nStart
= GetTime();
1708 // Minimum time before next feeler connection (in microseconds).
1709 int64_t nNextFeeler
= PoissonNextSend(nStart
*1000*1000, FEELER_INTERVAL
);
1710 while (!interruptNet
)
1714 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1717 CSemaphoreGrant
grant(*semOutbound
);
1721 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1722 if (addrman
.size() == 0 && (GetTime() - nStart
> 60)) {
1723 static bool done
= false;
1725 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1727 LookupHost("127.0.0.1", local
, false);
1728 addrman
.Add(convertSeed6(Params().FixedSeeds()), local
);
1734 // Choose an address to connect to based on most recently seen
1736 CAddress addrConnect
;
1738 // Only connect out to one peer per network group (/16 for IPv4).
1739 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1741 int nOutboundRelevant
= 0;
1742 std::set
<std::vector
<unsigned char> > setConnected
;
1745 for (CNode
* pnode
: vNodes
) {
1746 if (!pnode
->fInbound
&& !pnode
->fAddnode
) {
1748 // Count the peers that have all relevant services
1749 if (pnode
->fSuccessfullyConnected
&& !pnode
->fFeeler
&& ((pnode
->nServices
& nRelevantServices
) == nRelevantServices
)) {
1750 nOutboundRelevant
++;
1752 // Netgroups for inbound and addnode peers are not excluded because our goal here
1753 // is to not use multiple of our limited outbound slots on a single netgroup
1754 // but inbound and addnode peers do not use our outbound slots. Inbound peers
1755 // also have the added issue that they're attacker controlled and could be used
1756 // to prevent us from connecting to particular hosts if we used them here.
1757 setConnected
.insert(pnode
->addr
.GetGroup());
1763 // Feeler Connections
1766 // * Increase the number of connectable addresses in the tried table.
1769 // * Choose a random address from new and attempt to connect to it if we can connect
1770 // successfully it is added to tried.
1771 // * Start attempting feeler connections only after node finishes making outbound
1773 // * Only make a feeler connection once every few minutes.
1775 bool fFeeler
= false;
1776 if (nOutbound
>= nMaxOutbound
) {
1777 int64_t nTime
= GetTimeMicros(); // The current time right now (in microseconds).
1778 if (nTime
> nNextFeeler
) {
1779 nNextFeeler
= PoissonNextSend(nTime
, FEELER_INTERVAL
);
1786 int64_t nANow
= GetAdjustedTime();
1788 while (!interruptNet
)
1790 CAddrInfo addr
= addrman
.Select(fFeeler
);
1792 // if we selected an invalid address, restart
1793 if (!addr
.IsValid() || setConnected
.count(addr
.GetGroup()) || IsLocal(addr
))
1796 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1797 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1798 // already-connected network ranges, ...) before trying new addrman addresses.
1803 if (IsLimited(addr
))
1806 // only connect to full nodes
1807 if ((addr
.nServices
& REQUIRED_SERVICES
) != REQUIRED_SERVICES
)
1810 // only consider very recently tried nodes after 30 failed attempts
1811 if (nANow
- addr
.nLastTry
< 600 && nTries
< 30)
1814 // only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1815 ServiceFlags nRequiredServices
= nRelevantServices
;
1816 if (nTries
>= 40 && nOutbound
< (nMaxOutbound
>> 1)) {
1817 nRequiredServices
= REQUIRED_SERVICES
;
1820 if ((addr
.nServices
& nRequiredServices
) != nRequiredServices
) {
1824 // do not allow non-default ports, unless after 50 invalid addresses selected already
1825 if (addr
.GetPort() != Params().GetDefaultPort() && nTries
< 50)
1830 // regardless of the services assumed to be available, only require the minimum if half or more outbound have relevant services
1831 if (nOutboundRelevant
>= (nMaxOutbound
>> 1)) {
1832 addrConnect
.nServices
= REQUIRED_SERVICES
;
1834 addrConnect
.nServices
= nRequiredServices
;
1839 if (addrConnect
.IsValid()) {
1842 // Add small amount of random noise before connection to avoid synchronization.
1843 int randsleep
= GetRandInt(FEELER_SLEEP_WINDOW
* 1000);
1844 if (!interruptNet
.sleep_for(std::chrono::milliseconds(randsleep
)))
1846 LogPrint(BCLog::NET
, "Making feeler connection to %s\n", addrConnect
.ToString());
1849 OpenNetworkConnection(addrConnect
, (int)setConnected
.size() >= std::min(nMaxConnections
- 1, 2), &grant
, NULL
, false, fFeeler
);
1854 std::vector
<AddedNodeInfo
> CConnman::GetAddedNodeInfo()
1856 std::vector
<AddedNodeInfo
> ret
;
1858 std::list
<std::string
> lAddresses(0);
1860 LOCK(cs_vAddedNodes
);
1861 ret
.reserve(vAddedNodes
.size());
1862 for (const std::string
& strAddNode
: vAddedNodes
)
1863 lAddresses
.push_back(strAddNode
);
1867 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1868 std::map
<CService
, bool> mapConnected
;
1869 std::map
<std::string
, std::pair
<bool, CService
>> mapConnectedByName
;
1872 for (const CNode
* pnode
: vNodes
) {
1873 if (pnode
->addr
.IsValid()) {
1874 mapConnected
[pnode
->addr
] = pnode
->fInbound
;
1876 std::string addrName
= pnode
->GetAddrName();
1877 if (!addrName
.empty()) {
1878 mapConnectedByName
[std::move(addrName
)] = std::make_pair(pnode
->fInbound
, static_cast<const CService
&>(pnode
->addr
));
1883 for (const std::string
& strAddNode
: lAddresses
) {
1884 CService
service(LookupNumeric(strAddNode
.c_str(), Params().GetDefaultPort()));
1885 if (service
.IsValid()) {
1886 // strAddNode is an IP:port
1887 auto it
= mapConnected
.find(service
);
1888 if (it
!= mapConnected
.end()) {
1889 ret
.push_back(AddedNodeInfo
{strAddNode
, service
, true, it
->second
});
1891 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1894 // strAddNode is a name
1895 auto it
= mapConnectedByName
.find(strAddNode
);
1896 if (it
!= mapConnectedByName
.end()) {
1897 ret
.push_back(AddedNodeInfo
{strAddNode
, it
->second
.second
, true, it
->second
.first
});
1899 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1907 void CConnman::ThreadOpenAddedConnections()
1910 LOCK(cs_vAddedNodes
);
1911 if (gArgs
.IsArgSet("-addnode"))
1912 vAddedNodes
= gArgs
.GetArgs("-addnode");
1917 CSemaphoreGrant
grant(*semAddnode
);
1918 std::vector
<AddedNodeInfo
> vInfo
= GetAddedNodeInfo();
1920 for (const AddedNodeInfo
& info
: vInfo
) {
1921 if (!info
.fConnected
) {
1922 if (!grant
.TryAcquire()) {
1923 // If we've used up our semaphore and need a new one, lets not wait here since while we are waiting
1924 // the addednodeinfo state might change.
1927 // If strAddedNode is an IP/port, decode it immediately, so
1928 // OpenNetworkConnection can detect existing connections to that IP/port.
1930 CService
service(LookupNumeric(info
.strAddedNode
.c_str(), Params().GetDefaultPort()));
1931 OpenNetworkConnection(CAddress(service
, NODE_NONE
), false, &grant
, info
.strAddedNode
.c_str(), false, false, true);
1932 if (!interruptNet
.sleep_for(std::chrono::milliseconds(500)))
1936 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
1937 if (!interruptNet
.sleep_for(std::chrono::seconds(tried
? 60 : 2)))
1942 // if successful, this moves the passed grant to the constructed node
1943 bool CConnman::OpenNetworkConnection(const CAddress
& addrConnect
, bool fCountFailure
, CSemaphoreGrant
*grantOutbound
, const char *pszDest
, bool fOneShot
, bool fFeeler
, bool fAddnode
)
1946 // Initiate outbound network connection
1951 if (!fNetworkActive
) {
1955 if (IsLocal(addrConnect
) ||
1956 FindNode((CNetAddr
)addrConnect
) || IsBanned(addrConnect
) ||
1957 FindNode(addrConnect
.ToStringIPPort()))
1959 } else if (FindNode(std::string(pszDest
)))
1962 CNode
* pnode
= ConnectNode(addrConnect
, pszDest
, fCountFailure
);
1967 grantOutbound
->MoveTo(pnode
->grantOutbound
);
1969 pnode
->fOneShot
= true;
1971 pnode
->fFeeler
= true;
1973 pnode
->fAddnode
= true;
1975 GetNodeSignals().InitializeNode(pnode
, *this);
1978 vNodes
.push_back(pnode
);
1984 void CConnman::ThreadMessageHandler()
1986 while (!flagInterruptMsgProc
)
1988 std::vector
<CNode
*> vNodesCopy
;
1991 vNodesCopy
= vNodes
;
1992 for (CNode
* pnode
: vNodesCopy
) {
1997 bool fMoreWork
= false;
1999 for (CNode
* pnode
: vNodesCopy
)
2001 if (pnode
->fDisconnect
)
2005 bool fMoreNodeWork
= GetNodeSignals().ProcessMessages(pnode
, *this, flagInterruptMsgProc
);
2006 fMoreWork
|= (fMoreNodeWork
&& !pnode
->fPauseSend
);
2007 if (flagInterruptMsgProc
)
2012 LOCK(pnode
->cs_sendProcessing
);
2013 GetNodeSignals().SendMessages(pnode
, *this, flagInterruptMsgProc
);
2015 if (flagInterruptMsgProc
)
2021 for (CNode
* pnode
: vNodesCopy
)
2025 std::unique_lock
<std::mutex
> lock(mutexMsgProc
);
2027 condMsgProc
.wait_until(lock
, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake
; });
2029 fMsgProcWake
= false;
2038 bool CConnman::BindListenPort(const CService
&addrBind
, std::string
& strError
, bool fWhitelisted
)
2043 // Create socket for listening for incoming connections
2044 struct sockaddr_storage sockaddr
;
2045 socklen_t len
= sizeof(sockaddr
);
2046 if (!addrBind
.GetSockAddr((struct sockaddr
*)&sockaddr
, &len
))
2048 strError
= strprintf("Error: Bind address family for %s not supported", addrBind
.ToString());
2049 LogPrintf("%s\n", strError
);
2053 SOCKET hListenSocket
= socket(((struct sockaddr
*)&sockaddr
)->sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
2054 if (hListenSocket
== INVALID_SOCKET
)
2056 strError
= strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
2057 LogPrintf("%s\n", strError
);
2060 if (!IsSelectableSocket(hListenSocket
))
2062 strError
= "Error: Couldn't create a listenable socket for incoming connections";
2063 LogPrintf("%s\n", strError
);
2070 // Different way of disabling SIGPIPE on BSD
2071 setsockopt(hListenSocket
, SOL_SOCKET
, SO_NOSIGPIPE
, (void*)&nOne
, sizeof(int));
2073 // Allow binding if the port is still in TIME_WAIT state after
2074 // the program was closed and restarted.
2075 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&nOne
, sizeof(int));
2076 // Disable Nagle's algorithm
2077 setsockopt(hListenSocket
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&nOne
, sizeof(int));
2079 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&nOne
, sizeof(int));
2080 setsockopt(hListenSocket
, IPPROTO_TCP
, TCP_NODELAY
, (const char*)&nOne
, sizeof(int));
2083 // Set to non-blocking, incoming connections will also inherit this
2084 if (!SetSocketNonBlocking(hListenSocket
, true)) {
2085 strError
= strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
2086 LogPrintf("%s\n", strError
);
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
!= NULL
; ifa
= ifa
->ifa_next
)
2163 if (ifa
->ifa_addr
== NULL
) 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
);
2192 fNetworkActive
= false;
2195 // Close sockets to all nodes
2196 for (CNode
* pnode
: vNodes
) {
2197 pnode
->CloseSocketDisconnect();
2200 fNetworkActive
= true;
2203 uiInterface
.NotifyNetworkActiveChanged(fNetworkActive
);
2206 CConnman::CConnman(uint64_t nSeed0In
, uint64_t nSeed1In
) : nSeed0(nSeed0In
), nSeed1(nSeed1In
)
2208 fNetworkActive
= true;
2209 setBannedIsDirty
= false;
2210 fAddressesInitialized
= false;
2212 nSendBufferMaxSize
= 0;
2213 nReceiveFloodSize
= 0;
2216 nMaxConnections
= 0;
2220 clientInterface
= NULL
;
2221 flagInterruptMsgProc
= false;
2224 NodeId
CConnman::GetNewNodeId()
2226 return nLastNodeId
.fetch_add(1, std::memory_order_relaxed
);
2229 bool CConnman::Start(CScheduler
& scheduler
, std::string
& strNodeError
, Options connOptions
)
2231 nTotalBytesRecv
= 0;
2232 nTotalBytesSent
= 0;
2233 nMaxOutboundTotalBytesSentInCycle
= 0;
2234 nMaxOutboundCycleStartTime
= 0;
2236 nRelevantServices
= connOptions
.nRelevantServices
;
2237 nLocalServices
= connOptions
.nLocalServices
;
2238 nMaxConnections
= connOptions
.nMaxConnections
;
2239 nMaxOutbound
= std::min((connOptions
.nMaxOutbound
), nMaxConnections
);
2240 nMaxAddnode
= connOptions
.nMaxAddnode
;
2241 nMaxFeeler
= connOptions
.nMaxFeeler
;
2243 nSendBufferMaxSize
= connOptions
.nSendBufferMaxSize
;
2244 nReceiveFloodSize
= connOptions
.nReceiveFloodSize
;
2246 nMaxOutboundLimit
= connOptions
.nMaxOutboundLimit
;
2247 nMaxOutboundTimeframe
= connOptions
.nMaxOutboundTimeframe
;
2249 SetBestHeight(connOptions
.nBestHeight
);
2251 for (const auto& strDest
: connOptions
.vSeedNodes
) {
2252 AddOneShot(strDest
);
2255 clientInterface
= connOptions
.uiInterface
;
2256 if (clientInterface
) {
2257 clientInterface
->InitMessage(_("Loading P2P addresses..."));
2259 // Load addresses from peers.dat
2260 int64_t nStart
= GetTimeMillis();
2263 if (adb
.Read(addrman
))
2264 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman
.size(), GetTimeMillis() - nStart
);
2266 addrman
.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2267 LogPrintf("Invalid or missing peers.dat; recreating\n");
2271 if (clientInterface
)
2272 clientInterface
->InitMessage(_("Loading banlist..."));
2273 // Load addresses from banlist.dat
2274 nStart
= GetTimeMillis();
2277 if (bandb
.Read(banmap
)) {
2278 SetBanned(banmap
); // thread save setter
2279 SetBannedSetDirty(false); // no need to write down, just read data
2280 SweepBanned(); // sweep out unused entries
2282 LogPrint(BCLog::NET
, "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2283 banmap
.size(), GetTimeMillis() - nStart
);
2285 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2286 SetBannedSetDirty(true); // force write
2290 uiInterface
.InitMessage(_("Starting network threads..."));
2292 fAddressesInitialized
= true;
2294 if (semOutbound
== NULL
) {
2295 // initialize semaphore
2296 semOutbound
= new CSemaphore(std::min((nMaxOutbound
+ nMaxFeeler
), nMaxConnections
));
2298 if (semAddnode
== NULL
) {
2299 // initialize semaphore
2300 semAddnode
= new CSemaphore(nMaxAddnode
);
2306 InterruptSocks5(false);
2307 interruptNet
.reset();
2308 flagInterruptMsgProc
= false;
2311 std::unique_lock
<std::mutex
> lock(mutexMsgProc
);
2312 fMsgProcWake
= false;
2315 // Send and receive from sockets, accept connections
2316 threadSocketHandler
= std::thread(&TraceThread
<std::function
<void()> >, "net", std::function
<void()>(std::bind(&CConnman::ThreadSocketHandler
, this)));
2318 if (!GetBoolArg("-dnsseed", true))
2319 LogPrintf("DNS seeding disabled\n");
2321 threadDNSAddressSeed
= std::thread(&TraceThread
<std::function
<void()> >, "dnsseed", std::function
<void()>(std::bind(&CConnman::ThreadDNSAddressSeed
, this)));
2323 // Initiate outbound connections from -addnode
2324 threadOpenAddedConnections
= std::thread(&TraceThread
<std::function
<void()> >, "addcon", std::function
<void()>(std::bind(&CConnman::ThreadOpenAddedConnections
, this)));
2326 // Initiate outbound connections unless connect=0
2327 if (!gArgs
.IsArgSet("-connect") || gArgs
.GetArgs("-connect").size() != 1 || gArgs
.GetArgs("-connect")[0] != "0")
2328 threadOpenConnections
= std::thread(&TraceThread
<std::function
<void()> >, "opencon", std::function
<void()>(std::bind(&CConnman::ThreadOpenConnections
, this)));
2331 threadMessageHandler
= std::thread(&TraceThread
<std::function
<void()> >, "msghand", std::function
<void()>(std::bind(&CConnman::ThreadMessageHandler
, this)));
2333 // Dump network addresses
2334 scheduler
.scheduleEvery(std::bind(&CConnman::DumpData
, this), DUMP_ADDRESSES_INTERVAL
* 1000);
2347 // Shutdown Windows Sockets
2352 instance_of_cnetcleanup
;
2354 void CConnman::Interrupt()
2357 std::lock_guard
<std::mutex
> lock(mutexMsgProc
);
2358 flagInterruptMsgProc
= true;
2360 condMsgProc
.notify_all();
2363 InterruptSocks5(true);
2366 for (int i
=0; i
<(nMaxOutbound
+ nMaxFeeler
); i
++) {
2367 semOutbound
->post();
2372 for (int i
=0; i
<nMaxAddnode
; i
++) {
2378 void CConnman::Stop()
2380 if (threadMessageHandler
.joinable())
2381 threadMessageHandler
.join();
2382 if (threadOpenConnections
.joinable())
2383 threadOpenConnections
.join();
2384 if (threadOpenAddedConnections
.joinable())
2385 threadOpenAddedConnections
.join();
2386 if (threadDNSAddressSeed
.joinable())
2387 threadDNSAddressSeed
.join();
2388 if (threadSocketHandler
.joinable())
2389 threadSocketHandler
.join();
2391 if (fAddressesInitialized
)
2394 fAddressesInitialized
= false;
2398 for (CNode
* pnode
: vNodes
)
2399 pnode
->CloseSocketDisconnect();
2400 for (ListenSocket
& hListenSocket
: vhListenSocket
)
2401 if (hListenSocket
.socket
!= INVALID_SOCKET
)
2402 if (!CloseSocket(hListenSocket
.socket
))
2403 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2405 // clean up some globals (to help leak detection)
2406 for (CNode
*pnode
: vNodes
) {
2409 for (CNode
*pnode
: vNodesDisconnected
) {
2413 vNodesDisconnected
.clear();
2414 vhListenSocket
.clear();
2421 void CConnman::DeleteNode(CNode
* pnode
)
2424 bool fUpdateConnectionTime
= false;
2425 GetNodeSignals().FinalizeNode(pnode
->GetId(), fUpdateConnectionTime
);
2426 if(fUpdateConnectionTime
)
2427 addrman
.Connected(pnode
->addr
);
2431 CConnman::~CConnman()
2437 size_t CConnman::GetAddressCount() const
2439 return addrman
.size();
2442 void CConnman::SetServices(const CService
&addr
, ServiceFlags nServices
)
2444 addrman
.SetServices(addr
, nServices
);
2447 void CConnman::MarkAddressGood(const CAddress
& addr
)
2452 void CConnman::AddNewAddresses(const std::vector
<CAddress
>& vAddr
, const CAddress
& addrFrom
, int64_t nTimePenalty
)
2454 addrman
.Add(vAddr
, addrFrom
, nTimePenalty
);
2457 std::vector
<CAddress
> CConnman::GetAddresses()
2459 return addrman
.GetAddr();
2462 bool CConnman::AddNode(const std::string
& strNode
)
2464 LOCK(cs_vAddedNodes
);
2465 for(std::vector
<std::string
>::const_iterator it
= vAddedNodes
.begin(); it
!= vAddedNodes
.end(); ++it
) {
2470 vAddedNodes
.push_back(strNode
);
2474 bool CConnman::RemoveAddedNode(const std::string
& strNode
)
2476 LOCK(cs_vAddedNodes
);
2477 for(std::vector
<std::string
>::iterator it
= vAddedNodes
.begin(); it
!= vAddedNodes
.end(); ++it
) {
2478 if (strNode
== *it
) {
2479 vAddedNodes
.erase(it
);
2486 size_t CConnman::GetNodeCount(NumConnections flags
)
2489 if (flags
== CConnman::CONNECTIONS_ALL
) // Shortcut if we want total
2490 return vNodes
.size();
2493 for(std::vector
<CNode
*>::const_iterator it
= vNodes
.begin(); it
!= vNodes
.end(); ++it
)
2494 if (flags
& ((*it
)->fInbound
? CONNECTIONS_IN
: CONNECTIONS_OUT
))
2500 void CConnman::GetNodeStats(std::vector
<CNodeStats
>& vstats
)
2504 vstats
.reserve(vNodes
.size());
2505 for(std::vector
<CNode
*>::iterator it
= vNodes
.begin(); it
!= vNodes
.end(); ++it
) {
2507 vstats
.emplace_back();
2508 pnode
->copyStats(vstats
.back());
2512 bool CConnman::DisconnectNode(const std::string
& strNode
)
2515 if (CNode
* pnode
= FindNode(strNode
)) {
2516 pnode
->fDisconnect
= true;
2521 bool CConnman::DisconnectNode(NodeId id
)
2524 for(CNode
* pnode
: vNodes
) {
2525 if (id
== pnode
->GetId()) {
2526 pnode
->fDisconnect
= true;
2533 void CConnman::RecordBytesRecv(uint64_t bytes
)
2535 LOCK(cs_totalBytesRecv
);
2536 nTotalBytesRecv
+= bytes
;
2539 void CConnman::RecordBytesSent(uint64_t bytes
)
2541 LOCK(cs_totalBytesSent
);
2542 nTotalBytesSent
+= bytes
;
2544 uint64_t now
= GetTime();
2545 if (nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
< now
)
2547 // timeframe expired, reset cycle
2548 nMaxOutboundCycleStartTime
= now
;
2549 nMaxOutboundTotalBytesSentInCycle
= 0;
2552 // TODO, exclude whitebind peers
2553 nMaxOutboundTotalBytesSentInCycle
+= bytes
;
2556 void CConnman::SetMaxOutboundTarget(uint64_t limit
)
2558 LOCK(cs_totalBytesSent
);
2559 nMaxOutboundLimit
= limit
;
2562 uint64_t CConnman::GetMaxOutboundTarget()
2564 LOCK(cs_totalBytesSent
);
2565 return nMaxOutboundLimit
;
2568 uint64_t CConnman::GetMaxOutboundTimeframe()
2570 LOCK(cs_totalBytesSent
);
2571 return nMaxOutboundTimeframe
;
2574 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2576 LOCK(cs_totalBytesSent
);
2577 if (nMaxOutboundLimit
== 0)
2580 if (nMaxOutboundCycleStartTime
== 0)
2581 return nMaxOutboundTimeframe
;
2583 uint64_t cycleEndTime
= nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
;
2584 uint64_t now
= GetTime();
2585 return (cycleEndTime
< now
) ? 0 : cycleEndTime
- GetTime();
2588 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe
)
2590 LOCK(cs_totalBytesSent
);
2591 if (nMaxOutboundTimeframe
!= timeframe
)
2593 // reset measure-cycle in case of changing
2595 nMaxOutboundCycleStartTime
= GetTime();
2597 nMaxOutboundTimeframe
= timeframe
;
2600 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit
)
2602 LOCK(cs_totalBytesSent
);
2603 if (nMaxOutboundLimit
== 0)
2606 if (historicalBlockServingLimit
)
2608 // keep a large enough buffer to at least relay each block once
2609 uint64_t timeLeftInCycle
= GetMaxOutboundTimeLeftInCycle();
2610 uint64_t buffer
= timeLeftInCycle
/ 600 * MAX_BLOCK_SERIALIZED_SIZE
;
2611 if (buffer
>= nMaxOutboundLimit
|| nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
- buffer
)
2614 else if (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
)
2620 uint64_t CConnman::GetOutboundTargetBytesLeft()
2622 LOCK(cs_totalBytesSent
);
2623 if (nMaxOutboundLimit
== 0)
2626 return (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
) ? 0 : nMaxOutboundLimit
- nMaxOutboundTotalBytesSentInCycle
;
2629 uint64_t CConnman::GetTotalBytesRecv()
2631 LOCK(cs_totalBytesRecv
);
2632 return nTotalBytesRecv
;
2635 uint64_t CConnman::GetTotalBytesSent()
2637 LOCK(cs_totalBytesSent
);
2638 return nTotalBytesSent
;
2641 ServiceFlags
CConnman::GetLocalServices() const
2643 return nLocalServices
;
2646 void CConnman::SetBestHeight(int height
)
2648 nBestHeight
.store(height
, std::memory_order_release
);
2651 int CConnman::GetBestHeight() const
2653 return nBestHeight
.load(std::memory_order_acquire
);
2656 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize
; }
2657 unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize
; }
2659 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
) :
2660 nTimeConnected(GetSystemTimeInSeconds()),
2662 addrBind(addrBindIn
),
2663 fInbound(fInboundIn
),
2664 nKeyedNetGroup(nKeyedNetGroupIn
),
2665 addrKnown(5000, 0.001),
2666 filterInventoryKnown(50000, 0.000001),
2668 nLocalHostNonce(nLocalHostNonceIn
),
2669 nLocalServices(nLocalServicesIn
),
2670 nMyStartingHeight(nMyStartingHeightIn
),
2673 nServices
= NODE_NONE
;
2674 nServicesExpected
= NODE_NONE
;
2675 hSocket
= hSocketIn
;
2676 nRecvVersion
= INIT_PROTO_VERSION
;
2682 addrName
= addrNameIn
== "" ? addr
.ToStringIPPort() : addrNameIn
;
2685 fWhitelisted
= false;
2688 fClient
= false; // set by version message
2690 fSuccessfullyConnected
= false;
2691 fDisconnect
= false;
2695 hashContinue
= uint256();
2696 nStartingHeight
= -1;
2697 filterInventoryKnown
.reset();
2698 fSendMempool
= false;
2700 nNextLocalAddrSend
= 0;
2705 pfilter
= new CBloomFilter();
2706 timeLastMempoolReq
= 0;
2712 fPingQueued
= false;
2713 nMinPingUsecTime
= std::numeric_limits
<int64_t>::max();
2715 lastSentFeeFilter
= 0;
2716 nextSendTimeFeeFilter
= 0;
2719 nProcessQueueSize
= 0;
2721 for (const std::string
&msg
: getAllNetMessageTypes())
2722 mapRecvBytesPerMsgCmd
[msg
] = 0;
2723 mapRecvBytesPerMsgCmd
[NET_MESSAGE_COMMAND_OTHER
] = 0;
2726 LogPrint(BCLog::NET
, "Added connection to %s peer=%d\n", addrName
, id
);
2728 LogPrint(BCLog::NET
, "Added connection peer=%d\n", id
);
2734 CloseSocket(hSocket
);
2740 void CNode::AskFor(const CInv
& inv
)
2742 if (mapAskFor
.size() > MAPASKFOR_MAX_SZ
|| setAskFor
.size() > SETASKFOR_MAX_SZ
)
2744 // a peer may not have multiple non-responded queue positions for a single inv item
2745 if (!setAskFor
.insert(inv
.hash
).second
)
2748 // We're using mapAskFor as a priority queue,
2749 // the key is the earliest time the request can be sent
2750 int64_t nRequestTime
;
2751 limitedmap
<uint256
, int64_t>::const_iterator it
= mapAlreadyAskedFor
.find(inv
.hash
);
2752 if (it
!= mapAlreadyAskedFor
.end())
2753 nRequestTime
= it
->second
;
2756 LogPrint(BCLog::NET
, "askfor %s %d (%s) peer=%d\n", inv
.ToString(), nRequestTime
, DateTimeStrFormat("%H:%M:%S", nRequestTime
/1000000), id
);
2758 // Make sure not to reuse time indexes to keep things in the same order
2759 int64_t nNow
= GetTimeMicros() - 1000000;
2760 static int64_t nLastTime
;
2762 nNow
= std::max(nNow
, nLastTime
);
2765 // Each retry is 2 minutes after the last
2766 nRequestTime
= std::max(nRequestTime
+ 2 * 60 * 1000000, nNow
);
2767 if (it
!= mapAlreadyAskedFor
.end())
2768 mapAlreadyAskedFor
.update(it
, nRequestTime
);
2770 mapAlreadyAskedFor
.insert(std::make_pair(inv
.hash
, nRequestTime
));
2771 mapAskFor
.insert(std::make_pair(nRequestTime
, inv
));
2774 bool CConnman::NodeFullyConnected(const CNode
* pnode
)
2776 return pnode
&& pnode
->fSuccessfullyConnected
&& !pnode
->fDisconnect
;
2779 void CConnman::PushMessage(CNode
* pnode
, CSerializedNetMsg
&& msg
)
2781 size_t nMessageSize
= msg
.data
.size();
2782 size_t nTotalSize
= nMessageSize
+ CMessageHeader::HEADER_SIZE
;
2783 LogPrint(BCLog::NET
, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg
.command
.c_str()), nMessageSize
, pnode
->GetId());
2785 std::vector
<unsigned char> serializedHeader
;
2786 serializedHeader
.reserve(CMessageHeader::HEADER_SIZE
);
2787 uint256 hash
= Hash(msg
.data
.data(), msg
.data
.data() + nMessageSize
);
2788 CMessageHeader
hdr(Params().MessageStart(), msg
.command
.c_str(), nMessageSize
);
2789 memcpy(hdr
.pchChecksum
, hash
.begin(), CMessageHeader::CHECKSUM_SIZE
);
2791 CVectorWriter
{SER_NETWORK
, INIT_PROTO_VERSION
, serializedHeader
, 0, hdr
};
2793 size_t nBytesSent
= 0;
2795 LOCK(pnode
->cs_vSend
);
2796 bool optimisticSend(pnode
->vSendMsg
.empty());
2798 //log total amount of bytes per command
2799 pnode
->mapSendBytesPerMsgCmd
[msg
.command
] += nTotalSize
;
2800 pnode
->nSendSize
+= nTotalSize
;
2802 if (pnode
->nSendSize
> nSendBufferMaxSize
)
2803 pnode
->fPauseSend
= true;
2804 pnode
->vSendMsg
.push_back(std::move(serializedHeader
));
2806 pnode
->vSendMsg
.push_back(std::move(msg
.data
));
2808 // If write queue empty, attempt "optimistic write"
2809 if (optimisticSend
== true)
2810 nBytesSent
= SocketSendData(pnode
);
2813 RecordBytesSent(nBytesSent
);
2816 bool CConnman::ForNode(NodeId id
, std::function
<bool(CNode
* pnode
)> func
)
2818 CNode
* found
= nullptr;
2820 for (auto&& pnode
: vNodes
) {
2821 if(pnode
->GetId() == id
) {
2826 return found
!= nullptr && NodeFullyConnected(found
) && func(found
);
2829 int64_t PoissonNextSend(int64_t nNow
, int average_interval_seconds
) {
2830 return nNow
+ (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds
* -1000000.0 + 0.5);
2833 CSipHasher
CConnman::GetDeterministicRandomizer(uint64_t id
) const
2835 return CSipHasher(nSeed0
, nSeed1
).Write(id
);
2838 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress
& ad
) const
2840 std::vector
<unsigned char> vchNetGroup(ad
.GetGroup());
2842 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP
).Write(&vchNetGroup
[0], vchNetGroup
.size()).Finalize();