1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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>
38 #include <boost/filesystem.hpp>
39 #include <boost/thread.hpp>
43 // Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
44 #define DUMP_ADDRESSES_INTERVAL 900
46 // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
47 #define FEELER_SLEEP_WINDOW 1
49 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
50 #define MSG_NOSIGNAL 0
53 // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
54 // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
56 #ifndef PROTECTION_LEVEL_UNRESTRICTED
57 #define PROTECTION_LEVEL_UNRESTRICTED 10
59 #ifndef IPV6_PROTECTION_LEVEL
60 #define IPV6_PROTECTION_LEVEL 23
64 const static std::string NET_MESSAGE_COMMAND_OTHER
= "*other*";
66 static const uint64_t RANDOMIZER_ID_NETGROUP
= 0x6c0edd8036ef4036ULL
; // SHA256("netgroup")[0:8]
67 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE
= 0xd93e69e2bbfa5735ULL
; // SHA256("localhostnonce")[0:8]
69 // Global state variables
71 bool fDiscover
= true;
73 bool fRelayTxes
= true;
74 CCriticalSection cs_mapLocalHost
;
75 std::map
<CNetAddr
, LocalServiceInfo
> mapLocalHost
;
76 static bool vfLimited
[NET_MAX
] = {};
77 static CNode
* pnodeLocalHost
= NULL
;
78 std::string strSubVersion
;
80 limitedmap
<uint256
, int64_t> mapAlreadyAskedFor(MAX_INV_SZ
);
82 // Signals for message handling
83 static CNodeSignals g_signals
;
84 CNodeSignals
& GetNodeSignals() { return g_signals
; }
86 void CConnman::AddOneShot(const std::string
& strDest
)
89 vOneShots
.push_back(strDest
);
92 unsigned short GetListenPort()
94 return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
97 // find 'best' local address for a particular peer
98 bool GetLocal(CService
& addr
, const CNetAddr
*paddrPeer
)
104 int nBestReachability
= -1;
106 LOCK(cs_mapLocalHost
);
107 for (std::map
<CNetAddr
, LocalServiceInfo
>::iterator it
= mapLocalHost
.begin(); it
!= mapLocalHost
.end(); it
++)
109 int nScore
= (*it
).second
.nScore
;
110 int nReachability
= (*it
).first
.GetReachabilityFrom(paddrPeer
);
111 if (nReachability
> nBestReachability
|| (nReachability
== nBestReachability
&& nScore
> nBestScore
))
113 addr
= CService((*it
).first
, (*it
).second
.nPort
);
114 nBestReachability
= nReachability
;
119 return nBestScore
>= 0;
122 //! Convert the pnSeeds6 array into usable address objects.
123 static std::vector
<CAddress
> convertSeed6(const std::vector
<SeedSpec6
> &vSeedsIn
)
125 // It'll only connect to one or two seed nodes because once it connects,
126 // it'll get a pile of addresses with newer timestamps.
127 // Seed nodes are given a random 'last seen time' of between one and two
129 const int64_t nOneWeek
= 7*24*60*60;
130 std::vector
<CAddress
> vSeedsOut
;
131 vSeedsOut
.reserve(vSeedsIn
.size());
132 for (std::vector
<SeedSpec6
>::const_iterator
i(vSeedsIn
.begin()); i
!= vSeedsIn
.end(); ++i
)
135 memcpy(&ip
, i
->addr
, sizeof(ip
));
136 CAddress
addr(CService(ip
, i
->port
), NODE_NETWORK
);
137 addr
.nTime
= GetTime() - GetRand(nOneWeek
) - nOneWeek
;
138 vSeedsOut
.push_back(addr
);
143 // get best local address for a particular peer as a CAddress
144 // Otherwise, return the unroutable 0.0.0.0 but filled in with
145 // the normal parameters, since the IP may be changed to a useful
147 CAddress
GetLocalAddress(const CNetAddr
*paddrPeer
, ServiceFlags nLocalServices
)
149 CAddress
ret(CService(CNetAddr(),GetListenPort()), NODE_NONE
);
151 if (GetLocal(addr
, paddrPeer
))
153 ret
= CAddress(addr
, nLocalServices
);
155 ret
.nTime
= GetAdjustedTime();
159 int GetnScore(const CService
& addr
)
161 LOCK(cs_mapLocalHost
);
162 if (mapLocalHost
.count(addr
) == LOCAL_NONE
)
164 return mapLocalHost
[addr
].nScore
;
167 // Is our peer's addrLocal potentially useful as an external IP source?
168 bool IsPeerAddrLocalGood(CNode
*pnode
)
170 return fDiscover
&& pnode
->addr
.IsRoutable() && pnode
->addrLocal
.IsRoutable() &&
171 !IsLimited(pnode
->addrLocal
.GetNetwork());
174 // pushes our own address to a peer
175 void AdvertiseLocal(CNode
*pnode
)
177 if (fListen
&& pnode
->fSuccessfullyConnected
)
179 CAddress addrLocal
= GetLocalAddress(&pnode
->addr
, pnode
->GetLocalServices());
180 // If discovery is enabled, sometimes give our peer the address it
181 // tells us that it sees us as in case it has a better idea of our
182 // address than we do.
183 if (IsPeerAddrLocalGood(pnode
) && (!addrLocal
.IsRoutable() ||
184 GetRand((GetnScore(addrLocal
) > LOCAL_MANUAL
) ? 8:2) == 0))
186 addrLocal
.SetIP(pnode
->addrLocal
);
188 if (addrLocal
.IsRoutable())
190 LogPrint("net", "AdvertiseLocal: advertising address %s\n", addrLocal
.ToString());
191 FastRandomContext insecure_rand
;
192 pnode
->PushAddress(addrLocal
, insecure_rand
);
197 // learn a new local address
198 bool AddLocal(const CService
& addr
, int nScore
)
200 if (!addr
.IsRoutable())
203 if (!fDiscover
&& nScore
< LOCAL_MANUAL
)
209 LogPrintf("AddLocal(%s,%i)\n", addr
.ToString(), nScore
);
212 LOCK(cs_mapLocalHost
);
213 bool fAlready
= mapLocalHost
.count(addr
) > 0;
214 LocalServiceInfo
&info
= mapLocalHost
[addr
];
215 if (!fAlready
|| nScore
>= info
.nScore
) {
216 info
.nScore
= nScore
+ (fAlready
? 1 : 0);
217 info
.nPort
= addr
.GetPort();
224 bool AddLocal(const CNetAddr
&addr
, int nScore
)
226 return AddLocal(CService(addr
, GetListenPort()), nScore
);
229 bool RemoveLocal(const CService
& addr
)
231 LOCK(cs_mapLocalHost
);
232 LogPrintf("RemoveLocal(%s)\n", addr
.ToString());
233 mapLocalHost
.erase(addr
);
237 /** Make a particular network entirely off-limits (no automatic connects to it) */
238 void SetLimited(enum Network net
, bool fLimited
)
240 if (net
== NET_UNROUTABLE
)
242 LOCK(cs_mapLocalHost
);
243 vfLimited
[net
] = fLimited
;
246 bool IsLimited(enum Network net
)
248 LOCK(cs_mapLocalHost
);
249 return vfLimited
[net
];
252 bool IsLimited(const CNetAddr
&addr
)
254 return IsLimited(addr
.GetNetwork());
257 /** vote for a local address */
258 bool SeenLocal(const CService
& addr
)
261 LOCK(cs_mapLocalHost
);
262 if (mapLocalHost
.count(addr
) == 0)
264 mapLocalHost
[addr
].nScore
++;
270 /** check whether a given address is potentially local */
271 bool IsLocal(const CService
& addr
)
273 LOCK(cs_mapLocalHost
);
274 return mapLocalHost
.count(addr
) > 0;
277 /** check whether a given network is one we can probably connect to */
278 bool IsReachable(enum Network net
)
280 LOCK(cs_mapLocalHost
);
281 return !vfLimited
[net
];
284 /** check whether a given address is in a network we can probably connect to */
285 bool IsReachable(const CNetAddr
& addr
)
287 enum Network net
= addr
.GetNetwork();
288 return IsReachable(net
);
292 CNode
* CConnman::FindNode(const CNetAddr
& ip
)
295 BOOST_FOREACH(CNode
* pnode
, vNodes
)
296 if ((CNetAddr
)pnode
->addr
== ip
)
301 CNode
* CConnman::FindNode(const CSubNet
& subNet
)
304 BOOST_FOREACH(CNode
* pnode
, vNodes
)
305 if (subNet
.Match((CNetAddr
)pnode
->addr
))
310 CNode
* CConnman::FindNode(const std::string
& addrName
)
313 BOOST_FOREACH(CNode
* pnode
, vNodes
)
314 if (pnode
->addrName
== addrName
)
319 CNode
* CConnman::FindNode(const CService
& addr
)
322 BOOST_FOREACH(CNode
* pnode
, vNodes
)
323 if ((CService
)pnode
->addr
== addr
)
328 bool CConnman::CheckIncomingNonce(uint64_t nonce
)
331 BOOST_FOREACH(CNode
* pnode
, vNodes
) {
332 if (!pnode
->fSuccessfullyConnected
&& !pnode
->fInbound
&& pnode
->GetLocalNonce() == nonce
)
338 CNode
* CConnman::ConnectNode(CAddress addrConnect
, const char *pszDest
, bool fCountFailure
)
340 if (pszDest
== NULL
) {
341 if (IsLocal(addrConnect
))
344 // Look for an existing connection
345 CNode
* pnode
= FindNode((CService
)addrConnect
);
354 LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
355 pszDest
? pszDest
: addrConnect
.ToString(),
356 pszDest
? 0.0 : (double)(GetAdjustedTime() - addrConnect
.nTime
)/3600.0);
360 bool proxyConnectionFailed
= false;
361 if (pszDest
? ConnectSocketByName(addrConnect
, hSocket
, pszDest
, Params().GetDefaultPort(), nConnectTimeout
, &proxyConnectionFailed
) :
362 ConnectSocket(addrConnect
, hSocket
, nConnectTimeout
, &proxyConnectionFailed
))
364 if (!IsSelectableSocket(hSocket
)) {
365 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
366 CloseSocket(hSocket
);
370 if (pszDest
&& addrConnect
.IsValid()) {
371 // It is possible that we already have a connection to the IP/port pszDest resolved to.
372 // In that case, drop the connection that was just created, and return the existing CNode instead.
373 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
374 // name catch this early.
375 CNode
* pnode
= FindNode((CService
)addrConnect
);
381 if (pnode
->addrName
.empty()) {
382 pnode
->addrName
= std::string(pszDest
);
385 CloseSocket(hSocket
);
390 addrman
.Attempt(addrConnect
, fCountFailure
);
393 NodeId id
= GetNewNodeId();
394 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
395 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addrConnect
, CalculateKeyedNetGroup(addrConnect
), nonce
, pszDest
? pszDest
: "", false);
396 pnode
->nServicesExpected
= ServiceFlags(addrConnect
.nServices
& nRelevantServices
);
397 pnode
->nTimeConnected
= GetTime();
399 GetNodeSignals().InitializeNode(pnode
, *this);
402 vNodes
.push_back(pnode
);
406 } else if (!proxyConnectionFailed
) {
407 // If connecting to the node failed, and failure is not caused by a problem connecting to
408 // the proxy, mark this as an attempt.
409 addrman
.Attempt(addrConnect
, fCountFailure
);
415 void CConnman::DumpBanlist()
417 SweepBanned(); // clean unused entries (if bantime has expired)
419 if (!BannedSetIsDirty())
422 int64_t nStart
= GetTimeMillis();
426 SetBannedSetDirty(false);
428 if (!bandb
.Write(banmap
))
429 SetBannedSetDirty(true);
431 LogPrint("net", "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
432 banmap
.size(), GetTimeMillis() - nStart
);
435 void CNode::CloseSocketDisconnect()
438 if (hSocket
!= INVALID_SOCKET
)
440 LogPrint("net", "disconnecting peer=%d\n", id
);
441 CloseSocket(hSocket
);
444 // in case this fails, we'll empty the recv buffer when the CNode is deleted
445 TRY_LOCK(cs_vRecvMsg
, lockRecv
);
450 void CConnman::ClearBanned()
455 setBannedIsDirty
= true;
457 DumpBanlist(); //store banlist to disk
459 clientInterface
->BannedListChanged();
462 bool CConnman::IsBanned(CNetAddr ip
)
464 bool fResult
= false;
467 for (banmap_t::iterator it
= setBanned
.begin(); it
!= setBanned
.end(); it
++)
469 CSubNet subNet
= (*it
).first
;
470 CBanEntry banEntry
= (*it
).second
;
472 if(subNet
.Match(ip
) && GetTime() < banEntry
.nBanUntil
)
479 bool CConnman::IsBanned(CSubNet subnet
)
481 bool fResult
= false;
484 banmap_t::iterator i
= setBanned
.find(subnet
);
485 if (i
!= setBanned
.end())
487 CBanEntry banEntry
= (*i
).second
;
488 if (GetTime() < banEntry
.nBanUntil
)
495 void CConnman::Ban(const CNetAddr
& addr
, const BanReason
&banReason
, int64_t bantimeoffset
, bool sinceUnixEpoch
) {
496 CSubNet
subNet(addr
);
497 Ban(subNet
, banReason
, bantimeoffset
, sinceUnixEpoch
);
500 void CConnman::Ban(const CSubNet
& subNet
, const BanReason
&banReason
, int64_t bantimeoffset
, bool sinceUnixEpoch
) {
501 CBanEntry
banEntry(GetTime());
502 banEntry
.banReason
= banReason
;
503 if (bantimeoffset
<= 0)
505 bantimeoffset
= GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME
);
506 sinceUnixEpoch
= false;
508 banEntry
.nBanUntil
= (sinceUnixEpoch
? 0 : GetTime() )+bantimeoffset
;
512 if (setBanned
[subNet
].nBanUntil
< banEntry
.nBanUntil
) {
513 setBanned
[subNet
] = banEntry
;
514 setBannedIsDirty
= true;
520 clientInterface
->BannedListChanged();
523 BOOST_FOREACH(CNode
* pnode
, vNodes
) {
524 if (subNet
.Match((CNetAddr
)pnode
->addr
))
525 pnode
->fDisconnect
= true;
528 if(banReason
== BanReasonManuallyAdded
)
529 DumpBanlist(); //store banlist to disk immediately if user requested ban
532 bool CConnman::Unban(const CNetAddr
&addr
) {
533 CSubNet
subNet(addr
);
534 return Unban(subNet
);
537 bool CConnman::Unban(const CSubNet
&subNet
) {
540 if (!setBanned
.erase(subNet
))
542 setBannedIsDirty
= true;
545 clientInterface
->BannedListChanged();
546 DumpBanlist(); //store banlist to disk immediately
550 void CConnman::GetBanned(banmap_t
&banMap
)
553 banMap
= setBanned
; //create a thread safe copy
556 void CConnman::SetBanned(const banmap_t
&banMap
)
560 setBannedIsDirty
= true;
563 void CConnman::SweepBanned()
565 int64_t now
= GetTime();
568 banmap_t::iterator it
= setBanned
.begin();
569 while(it
!= setBanned
.end())
571 CSubNet subNet
= (*it
).first
;
572 CBanEntry banEntry
= (*it
).second
;
573 if(now
> banEntry
.nBanUntil
)
575 setBanned
.erase(it
++);
576 setBannedIsDirty
= true;
577 LogPrint("net", "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__
, subNet
.ToString());
584 bool CConnman::BannedSetIsDirty()
587 return setBannedIsDirty
;
590 void CConnman::SetBannedSetDirty(bool dirty
)
592 LOCK(cs_setBanned
); //reuse setBanned lock for the isDirty flag
593 setBannedIsDirty
= dirty
;
597 bool CConnman::IsWhitelistedRange(const CNetAddr
&addr
) {
598 LOCK(cs_vWhitelistedRange
);
599 BOOST_FOREACH(const CSubNet
& subnet
, vWhitelistedRange
) {
600 if (subnet
.Match(addr
))
606 void CConnman::AddWhitelistedRange(const CSubNet
&subnet
) {
607 LOCK(cs_vWhitelistedRange
);
608 vWhitelistedRange
.push_back(subnet
);
612 #define X(name) stats.name = name
613 void CNode::copyStats(CNodeStats
&stats
)
615 stats
.nodeid
= this->GetId();
629 X(mapSendBytesPerMsgCmd
);
631 X(mapRecvBytesPerMsgCmd
);
634 // It is common for nodes with good ping times to suddenly become lagged,
635 // due to a new block arriving or other large transfer.
636 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
637 // since pingtime does not update until the ping is complete, which might take a while.
638 // So, if a ping is taking an unusually long time in flight,
639 // the caller can immediately detect that this is happening.
640 int64_t nPingUsecWait
= 0;
641 if ((0 != nPingNonceSent
) && (0 != nPingUsecStart
)) {
642 nPingUsecWait
= GetTimeMicros() - nPingUsecStart
;
645 // 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 :)
646 stats
.dPingTime
= (((double)nPingUsecTime
) / 1e6
);
647 stats
.dMinPing
= (((double)nMinPingUsecTime
) / 1e6
);
648 stats
.dPingWait
= (((double)nPingUsecWait
) / 1e6
);
650 // Leave string empty if addrLocal invalid (not filled in yet)
651 stats
.addrLocal
= addrLocal
.IsValid() ? addrLocal
.ToString() : "";
655 // requires LOCK(cs_vRecvMsg)
656 bool CNode::ReceiveMsgBytes(const char *pch
, unsigned int nBytes
, bool& complete
)
661 // get current incomplete message, or create a new one
662 if (vRecvMsg
.empty() ||
663 vRecvMsg
.back().complete())
664 vRecvMsg
.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK
, nRecvVersion
));
666 CNetMessage
& msg
= vRecvMsg
.back();
668 // absorb network data
671 handled
= msg
.readHeader(pch
, nBytes
);
673 handled
= msg
.readData(pch
, nBytes
);
678 if (msg
.in_data
&& msg
.hdr
.nMessageSize
> MAX_PROTOCOL_MESSAGE_LENGTH
) {
679 LogPrint("net", "Oversized message from peer=%i, disconnecting\n", GetId());
686 if (msg
.complete()) {
688 //store received bytes per message command
689 //to prevent a memory DOS, only allow valid commands
690 mapMsgCmdSize::iterator i
= mapRecvBytesPerMsgCmd
.find(msg
.hdr
.pchCommand
);
691 if (i
== mapRecvBytesPerMsgCmd
.end())
692 i
= mapRecvBytesPerMsgCmd
.find(NET_MESSAGE_COMMAND_OTHER
);
693 assert(i
!= mapRecvBytesPerMsgCmd
.end());
694 i
->second
+= msg
.hdr
.nMessageSize
+ CMessageHeader::HEADER_SIZE
;
696 msg
.nTime
= GetTimeMicros();
704 int CNetMessage::readHeader(const char *pch
, unsigned int nBytes
)
706 // copy data to temporary parsing buffer
707 unsigned int nRemaining
= 24 - nHdrPos
;
708 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
710 memcpy(&hdrbuf
[nHdrPos
], pch
, nCopy
);
713 // if header incomplete, exit
717 // deserialize to CMessageHeader
721 catch (const std::exception
&) {
725 // reject messages larger than MAX_SIZE
726 if (hdr
.nMessageSize
> MAX_SIZE
)
729 // switch state to reading message data
735 int CNetMessage::readData(const char *pch
, unsigned int nBytes
)
737 unsigned int nRemaining
= hdr
.nMessageSize
- nDataPos
;
738 unsigned int nCopy
= std::min(nRemaining
, nBytes
);
740 if (vRecv
.size() < nDataPos
+ nCopy
) {
741 // Allocate up to 256 KiB ahead, but never more than the total message size.
742 vRecv
.resize(std::min(hdr
.nMessageSize
, nDataPos
+ nCopy
+ 256 * 1024));
745 hasher
.Write((const unsigned char*)pch
, nCopy
);
746 memcpy(&vRecv
[nDataPos
], pch
, nCopy
);
752 const uint256
& CNetMessage::GetMessageHash() const
755 if (data_hash
.IsNull())
756 hasher
.Finalize(data_hash
.begin());
768 // requires LOCK(cs_vSend)
769 size_t SocketSendData(CNode
*pnode
)
771 std::deque
<CSerializeData
>::iterator it
= pnode
->vSendMsg
.begin();
772 size_t nSentSize
= 0;
774 while (it
!= pnode
->vSendMsg
.end()) {
775 const CSerializeData
&data
= *it
;
776 assert(data
.size() > pnode
->nSendOffset
);
777 int nBytes
= send(pnode
->hSocket
, &data
[pnode
->nSendOffset
], data
.size() - pnode
->nSendOffset
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
779 pnode
->nLastSend
= GetTime();
780 pnode
->nSendBytes
+= nBytes
;
781 pnode
->nSendOffset
+= nBytes
;
783 if (pnode
->nSendOffset
== data
.size()) {
784 pnode
->nSendOffset
= 0;
785 pnode
->nSendSize
-= data
.size();
788 // could not send full message; stop sending more
794 int nErr
= WSAGetLastError();
795 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
797 LogPrintf("socket send error %s\n", NetworkErrorString(nErr
));
798 pnode
->CloseSocketDisconnect();
801 // couldn't send anything at all
806 if (it
== pnode
->vSendMsg
.end()) {
807 assert(pnode
->nSendOffset
== 0);
808 assert(pnode
->nSendSize
== 0);
810 pnode
->vSendMsg
.erase(pnode
->vSendMsg
.begin(), it
);
814 struct NodeEvictionCandidate
817 int64_t nTimeConnected
;
818 int64_t nMinPingUsecTime
;
819 int64_t nLastBlockTime
;
821 bool fRelevantServices
;
825 uint64_t nKeyedNetGroup
;
828 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
830 return a
.nMinPingUsecTime
> b
.nMinPingUsecTime
;
833 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
835 return a
.nTimeConnected
> b
.nTimeConnected
;
838 static bool CompareNetGroupKeyed(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
) {
839 return a
.nKeyedNetGroup
< b
.nKeyedNetGroup
;
842 static bool CompareNodeBlockTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
844 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
845 if (a
.nLastBlockTime
!= b
.nLastBlockTime
) return a
.nLastBlockTime
< b
.nLastBlockTime
;
846 if (a
.fRelevantServices
!= b
.fRelevantServices
) return b
.fRelevantServices
;
847 return a
.nTimeConnected
> b
.nTimeConnected
;
850 static bool CompareNodeTXTime(const NodeEvictionCandidate
&a
, const NodeEvictionCandidate
&b
)
852 // 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.
853 if (a
.nLastTXTime
!= b
.nLastTXTime
) return a
.nLastTXTime
< b
.nLastTXTime
;
854 if (a
.fRelayTxes
!= b
.fRelayTxes
) return b
.fRelayTxes
;
855 if (a
.fBloomFilter
!= b
.fBloomFilter
) return a
.fBloomFilter
;
856 return a
.nTimeConnected
> b
.nTimeConnected
;
859 /** Try to find a connection to evict when the node is full.
860 * Extreme care must be taken to avoid opening the node to attacker
861 * triggered network partitioning.
862 * The strategy used here is to protect a small number of peers
863 * for each of several distinct characteristics which are difficult
864 * to forge. In order to partition a node the attacker must be
865 * simultaneously better at all of them than honest peers.
867 bool CConnman::AttemptToEvictConnection()
869 std::vector
<NodeEvictionCandidate
> vEvictionCandidates
;
873 BOOST_FOREACH(CNode
*node
, vNodes
) {
874 if (node
->fWhitelisted
)
878 if (node
->fDisconnect
)
880 NodeEvictionCandidate candidate
= {node
->id
, node
->nTimeConnected
, node
->nMinPingUsecTime
,
881 node
->nLastBlockTime
, node
->nLastTXTime
,
882 (node
->nServices
& nRelevantServices
) == nRelevantServices
,
883 node
->fRelayTxes
, node
->pfilter
!= NULL
, node
->addr
, node
->nKeyedNetGroup
};
884 vEvictionCandidates
.push_back(candidate
);
888 if (vEvictionCandidates
.empty()) return false;
890 // Protect connections with certain characteristics
892 // Deterministically select 4 peers to protect by netgroup.
893 // An attacker cannot predict which netgroups will be protected
894 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNetGroupKeyed
);
895 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
897 if (vEvictionCandidates
.empty()) return false;
899 // Protect the 8 nodes with the lowest minimum ping time.
900 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
901 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), ReverseCompareNodeMinPingTime
);
902 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(8, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
904 if (vEvictionCandidates
.empty()) return false;
906 // Protect 4 nodes that most recently sent us transactions.
907 // An attacker cannot manipulate this metric without performing useful work.
908 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNodeTXTime
);
909 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
911 if (vEvictionCandidates
.empty()) return false;
913 // Protect 4 nodes that most recently sent us blocks.
914 // An attacker cannot manipulate this metric without performing useful work.
915 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), CompareNodeBlockTime
);
916 vEvictionCandidates
.erase(vEvictionCandidates
.end() - std::min(4, static_cast<int>(vEvictionCandidates
.size())), vEvictionCandidates
.end());
918 if (vEvictionCandidates
.empty()) return false;
920 // Protect the half of the remaining nodes which have been connected the longest.
921 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
922 std::sort(vEvictionCandidates
.begin(), vEvictionCandidates
.end(), ReverseCompareNodeTimeConnected
);
923 vEvictionCandidates
.erase(vEvictionCandidates
.end() - static_cast<int>(vEvictionCandidates
.size() / 2), vEvictionCandidates
.end());
925 if (vEvictionCandidates
.empty()) return false;
927 // Identify the network group with the most connections and youngest member.
928 // (vEvictionCandidates is already sorted by reverse connect time)
929 uint64_t naMostConnections
;
930 unsigned int nMostConnections
= 0;
931 int64_t nMostConnectionsTime
= 0;
932 std::map
<uint64_t, std::vector
<NodeEvictionCandidate
> > mapNetGroupNodes
;
933 BOOST_FOREACH(const NodeEvictionCandidate
&node
, vEvictionCandidates
) {
934 mapNetGroupNodes
[node
.nKeyedNetGroup
].push_back(node
);
935 int64_t grouptime
= mapNetGroupNodes
[node
.nKeyedNetGroup
][0].nTimeConnected
;
936 size_t groupsize
= mapNetGroupNodes
[node
.nKeyedNetGroup
].size();
938 if (groupsize
> nMostConnections
|| (groupsize
== nMostConnections
&& grouptime
> nMostConnectionsTime
)) {
939 nMostConnections
= groupsize
;
940 nMostConnectionsTime
= grouptime
;
941 naMostConnections
= node
.nKeyedNetGroup
;
945 // Reduce to the network group with the most connections
946 vEvictionCandidates
= std::move(mapNetGroupNodes
[naMostConnections
]);
948 // Disconnect from the network group with the most connections
949 NodeId evicted
= vEvictionCandidates
.front().id
;
951 for(std::vector
<CNode
*>::const_iterator
it(vNodes
.begin()); it
!= vNodes
.end(); ++it
) {
952 if ((*it
)->GetId() == evicted
) {
953 (*it
)->fDisconnect
= true;
960 void CConnman::AcceptConnection(const ListenSocket
& hListenSocket
) {
961 struct sockaddr_storage sockaddr
;
962 socklen_t len
= sizeof(sockaddr
);
963 SOCKET hSocket
= accept(hListenSocket
.socket
, (struct sockaddr
*)&sockaddr
, &len
);
966 int nMaxInbound
= nMaxConnections
- (nMaxOutbound
+ nMaxFeeler
);
968 if (hSocket
!= INVALID_SOCKET
)
969 if (!addr
.SetSockAddr((const struct sockaddr
*)&sockaddr
))
970 LogPrintf("Warning: Unknown socket family\n");
972 bool whitelisted
= hListenSocket
.whitelisted
|| IsWhitelistedRange(addr
);
975 BOOST_FOREACH(CNode
* pnode
, vNodes
)
980 if (hSocket
== INVALID_SOCKET
)
982 int nErr
= WSAGetLastError();
983 if (nErr
!= WSAEWOULDBLOCK
)
984 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr
));
988 if (!IsSelectableSocket(hSocket
))
990 LogPrintf("connection from %s dropped: non-selectable socket\n", addr
.ToString());
991 CloseSocket(hSocket
);
995 // According to the internet TCP_NODELAY is not carried into accepted sockets
996 // on all platforms. Set it again here just to be sure.
999 setsockopt(hSocket
, IPPROTO_TCP
, TCP_NODELAY
, (const char*)&set
, sizeof(int));
1001 setsockopt(hSocket
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&set
, sizeof(int));
1004 if (IsBanned(addr
) && !whitelisted
)
1006 LogPrintf("connection from %s dropped (banned)\n", addr
.ToString());
1007 CloseSocket(hSocket
);
1011 if (nInbound
>= nMaxInbound
)
1013 if (!AttemptToEvictConnection()) {
1014 // No connection to evict, disconnect the new connection
1015 LogPrint("net", "failed to find an eviction candidate - connection dropped (full)\n");
1016 CloseSocket(hSocket
);
1021 NodeId id
= GetNewNodeId();
1022 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
1024 CNode
* pnode
= new CNode(id
, nLocalServices
, GetBestHeight(), hSocket
, addr
, CalculateKeyedNetGroup(addr
), nonce
, "", true);
1026 pnode
->fWhitelisted
= whitelisted
;
1027 GetNodeSignals().InitializeNode(pnode
, *this);
1029 LogPrint("net", "connection from %s accepted\n", addr
.ToString());
1033 vNodes
.push_back(pnode
);
1037 void CConnman::ThreadSocketHandler()
1039 unsigned int nPrevNodeCount
= 0;
1047 // Disconnect unused nodes
1048 std::vector
<CNode
*> vNodesCopy
= vNodes
;
1049 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
)
1051 if (pnode
->fDisconnect
||
1052 (pnode
->GetRefCount() <= 0 && pnode
->vRecvMsg
.empty() && pnode
->nSendSize
== 0))
1054 // remove from vNodes
1055 vNodes
.erase(remove(vNodes
.begin(), vNodes
.end(), pnode
), vNodes
.end());
1057 // release outbound grant (if any)
1058 pnode
->grantOutbound
.Release();
1060 // close socket and cleanup
1061 pnode
->CloseSocketDisconnect();
1063 // hold in disconnected pool until all refs are released
1064 if (pnode
->fNetworkNode
|| pnode
->fInbound
)
1066 vNodesDisconnected
.push_back(pnode
);
1071 // Delete disconnected nodes
1072 std::list
<CNode
*> vNodesDisconnectedCopy
= vNodesDisconnected
;
1073 BOOST_FOREACH(CNode
* pnode
, vNodesDisconnectedCopy
)
1075 // wait until threads are done using it
1076 if (pnode
->GetRefCount() <= 0)
1078 bool fDelete
= false;
1080 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1083 TRY_LOCK(pnode
->cs_vRecvMsg
, lockRecv
);
1086 TRY_LOCK(pnode
->cs_inventory
, lockInv
);
1094 vNodesDisconnected
.remove(pnode
);
1100 if(vNodes
.size() != nPrevNodeCount
) {
1101 nPrevNodeCount
= vNodes
.size();
1103 clientInterface
->NotifyNumConnectionsChanged(nPrevNodeCount
);
1107 // Find which sockets have data to receive
1109 struct timeval timeout
;
1111 timeout
.tv_usec
= 50000; // frequency to poll pnode->vSend
1116 FD_ZERO(&fdsetRecv
);
1117 FD_ZERO(&fdsetSend
);
1118 FD_ZERO(&fdsetError
);
1119 SOCKET hSocketMax
= 0;
1120 bool have_fds
= false;
1122 BOOST_FOREACH(const ListenSocket
& hListenSocket
, vhListenSocket
) {
1123 FD_SET(hListenSocket
.socket
, &fdsetRecv
);
1124 hSocketMax
= std::max(hSocketMax
, hListenSocket
.socket
);
1130 BOOST_FOREACH(CNode
* pnode
, vNodes
)
1132 if (pnode
->hSocket
== INVALID_SOCKET
)
1134 FD_SET(pnode
->hSocket
, &fdsetError
);
1135 hSocketMax
= std::max(hSocketMax
, pnode
->hSocket
);
1138 // Implement the following logic:
1139 // * If there is data to send, select() for sending data. As this only
1140 // happens when optimistic write failed, we choose to first drain the
1141 // write buffer in this case before receiving more. This avoids
1142 // needlessly queueing received data, if the remote peer is not themselves
1143 // receiving data. This means properly utilizing TCP flow control signalling.
1144 // * Otherwise, if there is no (complete) message in the receive buffer,
1145 // or there is space left in the buffer, select() for receiving data.
1146 // * (if neither of the above applies, there is certainly one message
1147 // in the receiver buffer ready to be processed).
1148 // Together, that means that at least one of the following is always possible,
1149 // so we don't deadlock:
1150 // * We send some data.
1151 // * We wait for data to be received (and disconnect after timeout).
1152 // * We process a message in the buffer (message handler thread).
1154 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1156 if (!pnode
->vSendMsg
.empty()) {
1157 FD_SET(pnode
->hSocket
, &fdsetSend
);
1163 TRY_LOCK(pnode
->cs_vRecvMsg
, lockRecv
);
1165 pnode
->vRecvMsg
.empty() || !pnode
->vRecvMsg
.front().complete() ||
1166 pnode
->GetTotalRecvSize() <= GetReceiveFloodSize()))
1167 FD_SET(pnode
->hSocket
, &fdsetRecv
);
1172 int nSelect
= select(have_fds
? hSocketMax
+ 1 : 0,
1173 &fdsetRecv
, &fdsetSend
, &fdsetError
, &timeout
);
1174 boost::this_thread::interruption_point();
1176 if (nSelect
== SOCKET_ERROR
)
1180 int nErr
= WSAGetLastError();
1181 LogPrintf("socket select error %s\n", NetworkErrorString(nErr
));
1182 for (unsigned int i
= 0; i
<= hSocketMax
; i
++)
1183 FD_SET(i
, &fdsetRecv
);
1185 FD_ZERO(&fdsetSend
);
1186 FD_ZERO(&fdsetError
);
1187 MilliSleep(timeout
.tv_usec
/1000);
1191 // Accept new connections
1193 BOOST_FOREACH(const ListenSocket
& hListenSocket
, vhListenSocket
)
1195 if (hListenSocket
.socket
!= INVALID_SOCKET
&& FD_ISSET(hListenSocket
.socket
, &fdsetRecv
))
1197 AcceptConnection(hListenSocket
);
1202 // Service each socket
1204 std::vector
<CNode
*> vNodesCopy
;
1207 vNodesCopy
= vNodes
;
1208 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
)
1211 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
)
1213 boost::this_thread::interruption_point();
1218 if (pnode
->hSocket
== INVALID_SOCKET
)
1220 if (FD_ISSET(pnode
->hSocket
, &fdsetRecv
) || FD_ISSET(pnode
->hSocket
, &fdsetError
))
1222 TRY_LOCK(pnode
->cs_vRecvMsg
, lockRecv
);
1226 // typical socket buffer is 8K-64K
1227 char pchBuf
[0x10000];
1228 int nBytes
= recv(pnode
->hSocket
, pchBuf
, sizeof(pchBuf
), MSG_DONTWAIT
);
1231 bool notify
= false;
1232 if (!pnode
->ReceiveMsgBytes(pchBuf
, nBytes
, notify
))
1233 pnode
->CloseSocketDisconnect();
1235 messageHandlerCondition
.notify_one();
1236 pnode
->nLastRecv
= GetTime();
1237 pnode
->nRecvBytes
+= nBytes
;
1238 RecordBytesRecv(nBytes
);
1240 else if (nBytes
== 0)
1242 // socket closed gracefully
1243 if (!pnode
->fDisconnect
)
1244 LogPrint("net", "socket closed\n");
1245 pnode
->CloseSocketDisconnect();
1247 else if (nBytes
< 0)
1250 int nErr
= WSAGetLastError();
1251 if (nErr
!= WSAEWOULDBLOCK
&& nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
1253 if (!pnode
->fDisconnect
)
1254 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr
));
1255 pnode
->CloseSocketDisconnect();
1265 if (pnode
->hSocket
== INVALID_SOCKET
)
1267 if (FD_ISSET(pnode
->hSocket
, &fdsetSend
))
1269 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1271 size_t nBytes
= SocketSendData(pnode
);
1273 RecordBytesSent(nBytes
);
1278 // Inactivity checking
1280 int64_t nTime
= GetTime();
1281 if (nTime
- pnode
->nTimeConnected
> 60)
1283 if (pnode
->nLastRecv
== 0 || pnode
->nLastSend
== 0)
1285 LogPrint("net", "socket no message in first 60 seconds, %d %d from %d\n", pnode
->nLastRecv
!= 0, pnode
->nLastSend
!= 0, pnode
->id
);
1286 pnode
->fDisconnect
= true;
1288 else if (nTime
- pnode
->nLastSend
> TIMEOUT_INTERVAL
)
1290 LogPrintf("socket sending timeout: %is\n", nTime
- pnode
->nLastSend
);
1291 pnode
->fDisconnect
= true;
1293 else if (nTime
- pnode
->nLastRecv
> (pnode
->nVersion
> BIP0031_VERSION
? TIMEOUT_INTERVAL
: 90*60))
1295 LogPrintf("socket receive timeout: %is\n", nTime
- pnode
->nLastRecv
);
1296 pnode
->fDisconnect
= true;
1298 else if (pnode
->nPingNonceSent
&& pnode
->nPingUsecStart
+ TIMEOUT_INTERVAL
* 1000000 < GetTimeMicros())
1300 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode
->nPingUsecStart
));
1301 pnode
->fDisconnect
= true;
1307 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
)
1322 void ThreadMapPort()
1324 std::string port
= strprintf("%u", GetListenPort());
1325 const char * multicastif
= 0;
1326 const char * minissdpdpath
= 0;
1327 struct UPNPDev
* devlist
= 0;
1330 #ifndef UPNPDISCOVER_SUCCESS
1332 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0);
1333 #elif MINIUPNPC_API_VERSION < 14
1336 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, &error
);
1338 /* miniupnpc 1.9.20150730 */
1340 devlist
= upnpDiscover(2000, multicastif
, minissdpdpath
, 0, 0, 2, &error
);
1343 struct UPNPUrls urls
;
1344 struct IGDdatas data
;
1347 r
= UPNP_GetValidIGD(devlist
, &urls
, &data
, lanaddr
, sizeof(lanaddr
));
1351 char externalIPAddress
[40];
1352 r
= UPNP_GetExternalIPAddress(urls
.controlURL
, data
.first
.servicetype
, externalIPAddress
);
1353 if(r
!= UPNPCOMMAND_SUCCESS
)
1354 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r
);
1357 if(externalIPAddress
[0])
1360 if(LookupHost(externalIPAddress
, resolved
, false)) {
1361 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved
.ToString().c_str());
1362 AddLocal(resolved
, LOCAL_UPNP
);
1366 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1370 std::string strDesc
= "Bitcoin " + FormatFullVersion();
1374 #ifndef UPNPDISCOVER_SUCCESS
1376 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1377 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0);
1380 r
= UPNP_AddPortMapping(urls
.controlURL
, data
.first
.servicetype
,
1381 port
.c_str(), port
.c_str(), lanaddr
, strDesc
.c_str(), "TCP", 0, "0");
1384 if(r
!=UPNPCOMMAND_SUCCESS
)
1385 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1386 port
, port
, lanaddr
, r
, strupnperror(r
));
1388 LogPrintf("UPnP Port Mapping successful.\n");
1390 MilliSleep(20*60*1000); // Refresh every 20 minutes
1393 catch (const boost::thread_interrupted
&)
1395 r
= UPNP_DeletePortMapping(urls
.controlURL
, data
.first
.servicetype
, port
.c_str(), "TCP", 0);
1396 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r
);
1397 freeUPNPDevlist(devlist
); devlist
= 0;
1398 FreeUPNPUrls(&urls
);
1402 LogPrintf("No valid UPnP IGDs found\n");
1403 freeUPNPDevlist(devlist
); devlist
= 0;
1405 FreeUPNPUrls(&urls
);
1409 void MapPort(bool fUseUPnP
)
1411 static boost::thread
* upnp_thread
= NULL
;
1416 upnp_thread
->interrupt();
1417 upnp_thread
->join();
1420 upnp_thread
= new boost::thread(boost::bind(&TraceThread
<void (*)()>, "upnp", &ThreadMapPort
));
1422 else if (upnp_thread
) {
1423 upnp_thread
->interrupt();
1424 upnp_thread
->join();
1433 // Intentionally left blank.
1442 static std::string
GetDNSHost(const CDNSSeedData
& data
, ServiceFlags
* requiredServiceBits
)
1444 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1445 if (!data
.supportsServiceBitsFiltering
|| *requiredServiceBits
== NODE_NETWORK
) {
1446 *requiredServiceBits
= NODE_NETWORK
;
1450 // See chainparams.cpp, most dnsseeds only support one or two possible servicebits hostnames
1451 return strprintf("x%x.%s", *requiredServiceBits
, data
.host
);
1455 void CConnman::ThreadDNSAddressSeed()
1457 // goal: only query DNS seeds if address need is acute
1458 // Avoiding DNS seeds when we don't need them improves user privacy by
1459 // creating fewer identifying DNS requests, reduces trust by giving seeds
1460 // less influence on the network topology, and reduces traffic to the seeds.
1461 if ((addrman
.size() > 0) &&
1462 (!GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED
))) {
1463 MilliSleep(11 * 1000);
1467 for (auto pnode
: vNodes
) {
1468 nRelevant
+= pnode
->fSuccessfullyConnected
&& ((pnode
->nServices
& nRelevantServices
) == nRelevantServices
);
1470 if (nRelevant
>= 2) {
1471 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1476 const std::vector
<CDNSSeedData
> &vSeeds
= Params().DNSSeeds();
1479 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1481 BOOST_FOREACH(const CDNSSeedData
&seed
, vSeeds
) {
1482 if (HaveNameProxy()) {
1483 AddOneShot(seed
.host
);
1485 std::vector
<CNetAddr
> vIPs
;
1486 std::vector
<CAddress
> vAdd
;
1487 ServiceFlags requiredServiceBits
= nRelevantServices
;
1488 if (LookupHost(GetDNSHost(seed
, &requiredServiceBits
).c_str(), vIPs
, 0, true))
1490 BOOST_FOREACH(const CNetAddr
& ip
, vIPs
)
1492 int nOneDay
= 24*3600;
1493 CAddress addr
= CAddress(CService(ip
, Params().GetDefaultPort()), requiredServiceBits
);
1494 addr
.nTime
= GetTime() - 3*nOneDay
- GetRand(4*nOneDay
); // use a random age between 3 and 7 days old
1495 vAdd
.push_back(addr
);
1499 // TODO: The seed name resolve may fail, yielding an IP of [::], which results in
1500 // addrman assigning the same source to results from different seeds.
1501 // This should switch to a hard-coded stable dummy IP for each seed name, so that the
1502 // resolve is not required at all.
1503 if (!vIPs
.empty()) {
1504 CService seedSource
;
1505 Lookup(seed
.name
.c_str(), seedSource
, 0, true);
1506 addrman
.Add(vAdd
, seedSource
);
1511 LogPrintf("%d addresses found from DNS seeds\n", found
);
1525 void CConnman::DumpAddresses()
1527 int64_t nStart
= GetTimeMillis();
1532 LogPrint("net", "Flushed %d addresses to peers.dat %dms\n",
1533 addrman
.size(), GetTimeMillis() - nStart
);
1536 void CConnman::DumpData()
1542 void CConnman::ProcessOneShot()
1544 std::string strDest
;
1547 if (vOneShots
.empty())
1549 strDest
= vOneShots
.front();
1550 vOneShots
.pop_front();
1553 CSemaphoreGrant
grant(*semOutbound
, true);
1555 if (!OpenNetworkConnection(addr
, false, &grant
, strDest
.c_str(), true))
1556 AddOneShot(strDest
);
1560 void CConnman::ThreadOpenConnections()
1562 // Connect to specific addresses
1563 if (mapArgs
.count("-connect") && mapMultiArgs
["-connect"].size() > 0)
1565 for (int64_t nLoop
= 0;; nLoop
++)
1568 BOOST_FOREACH(const std::string
& strAddr
, mapMultiArgs
["-connect"])
1570 CAddress
addr(CService(), NODE_NONE
);
1571 OpenNetworkConnection(addr
, false, NULL
, strAddr
.c_str());
1572 for (int i
= 0; i
< 10 && i
< nLoop
; i
++)
1581 // Initiate network connections
1582 int64_t nStart
= GetTime();
1584 // Minimum time before next feeler connection (in microseconds).
1585 int64_t nNextFeeler
= PoissonNextSend(nStart
*1000*1000, FEELER_INTERVAL
);
1592 CSemaphoreGrant
grant(*semOutbound
);
1593 boost::this_thread::interruption_point();
1595 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1596 if (addrman
.size() == 0 && (GetTime() - nStart
> 60)) {
1597 static bool done
= false;
1599 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1601 LookupHost("127.0.0.1", local
, false);
1602 addrman
.Add(convertSeed6(Params().FixedSeeds()), local
);
1608 // Choose an address to connect to based on most recently seen
1610 CAddress addrConnect
;
1612 // Only connect out to one peer per network group (/16 for IPv4).
1613 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1615 std::set
<std::vector
<unsigned char> > setConnected
;
1618 BOOST_FOREACH(CNode
* pnode
, vNodes
) {
1619 if (!pnode
->fInbound
) {
1620 setConnected
.insert(pnode
->addr
.GetGroup());
1626 // Feeler Connections
1629 // * Increase the number of connectable addresses in the tried table.
1632 // * Choose a random address from new and attempt to connect to it if we can connect
1633 // successfully it is added to tried.
1634 // * Start attempting feeler connections only after node finishes making outbound
1636 // * Only make a feeler connection once every few minutes.
1638 bool fFeeler
= false;
1639 if (nOutbound
>= nMaxOutbound
) {
1640 int64_t nTime
= GetTimeMicros(); // The current time right now (in microseconds).
1641 if (nTime
> nNextFeeler
) {
1642 nNextFeeler
= PoissonNextSend(nTime
, FEELER_INTERVAL
);
1649 int64_t nANow
= GetAdjustedTime();
1653 CAddrInfo addr
= addrman
.Select(fFeeler
);
1655 // if we selected an invalid address, restart
1656 if (!addr
.IsValid() || setConnected
.count(addr
.GetGroup()) || IsLocal(addr
))
1659 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1660 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1661 // already-connected network ranges, ...) before trying new addrman addresses.
1666 if (IsLimited(addr
))
1669 // only connect to full nodes
1670 if ((addr
.nServices
& REQUIRED_SERVICES
) != REQUIRED_SERVICES
)
1673 // only consider very recently tried nodes after 30 failed attempts
1674 if (nANow
- addr
.nLastTry
< 600 && nTries
< 30)
1677 // only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1678 if ((addr
.nServices
& nRelevantServices
) != nRelevantServices
&& (nTries
< 40 || nOutbound
>= (nMaxOutbound
>> 1)))
1681 // do not allow non-default ports, unless after 50 invalid addresses selected already
1682 if (addr
.GetPort() != Params().GetDefaultPort() && nTries
< 50)
1689 if (addrConnect
.IsValid()) {
1692 // Add small amount of random noise before connection to avoid synchronization.
1693 int randsleep
= GetRandInt(FEELER_SLEEP_WINDOW
* 1000);
1694 MilliSleep(randsleep
);
1695 LogPrint("net", "Making feeler connection to %s\n", addrConnect
.ToString());
1698 OpenNetworkConnection(addrConnect
, (int)setConnected
.size() >= std::min(nMaxConnections
- 1, 2), &grant
, NULL
, false, fFeeler
);
1703 std::vector
<AddedNodeInfo
> CConnman::GetAddedNodeInfo()
1705 std::vector
<AddedNodeInfo
> ret
;
1707 std::list
<std::string
> lAddresses(0);
1709 LOCK(cs_vAddedNodes
);
1710 ret
.reserve(vAddedNodes
.size());
1711 BOOST_FOREACH(const std::string
& strAddNode
, vAddedNodes
)
1712 lAddresses
.push_back(strAddNode
);
1716 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1717 std::map
<CService
, bool> mapConnected
;
1718 std::map
<std::string
, std::pair
<bool, CService
>> mapConnectedByName
;
1721 for (const CNode
* pnode
: vNodes
) {
1722 if (pnode
->addr
.IsValid()) {
1723 mapConnected
[pnode
->addr
] = pnode
->fInbound
;
1725 if (!pnode
->addrName
.empty()) {
1726 mapConnectedByName
[pnode
->addrName
] = std::make_pair(pnode
->fInbound
, static_cast<const CService
&>(pnode
->addr
));
1731 BOOST_FOREACH(const std::string
& strAddNode
, lAddresses
) {
1732 CService
service(LookupNumeric(strAddNode
.c_str(), Params().GetDefaultPort()));
1733 if (service
.IsValid()) {
1734 // strAddNode is an IP:port
1735 auto it
= mapConnected
.find(service
);
1736 if (it
!= mapConnected
.end()) {
1737 ret
.push_back(AddedNodeInfo
{strAddNode
, service
, true, it
->second
});
1739 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1742 // strAddNode is a name
1743 auto it
= mapConnectedByName
.find(strAddNode
);
1744 if (it
!= mapConnectedByName
.end()) {
1745 ret
.push_back(AddedNodeInfo
{strAddNode
, it
->second
.second
, true, it
->second
.first
});
1747 ret
.push_back(AddedNodeInfo
{strAddNode
, CService(), false, false});
1755 void CConnman::ThreadOpenAddedConnections()
1758 LOCK(cs_vAddedNodes
);
1759 vAddedNodes
= mapMultiArgs
["-addnode"];
1762 for (unsigned int i
= 0; true; i
++)
1764 std::vector
<AddedNodeInfo
> vInfo
= GetAddedNodeInfo();
1765 for (const AddedNodeInfo
& info
: vInfo
) {
1766 if (!info
.fConnected
) {
1767 CSemaphoreGrant
grant(*semOutbound
);
1768 // If strAddedNode is an IP/port, decode it immediately, so
1769 // OpenNetworkConnection can detect existing connections to that IP/port.
1770 CService
service(LookupNumeric(info
.strAddedNode
.c_str(), Params().GetDefaultPort()));
1771 OpenNetworkConnection(CAddress(service
, NODE_NONE
), false, &grant
, info
.strAddedNode
.c_str(), false);
1776 MilliSleep(120000); // Retry every 2 minutes
1780 // if successful, this moves the passed grant to the constructed node
1781 bool CConnman::OpenNetworkConnection(const CAddress
& addrConnect
, bool fCountFailure
, CSemaphoreGrant
*grantOutbound
, const char *pszDest
, bool fOneShot
, bool fFeeler
)
1784 // Initiate outbound network connection
1786 boost::this_thread::interruption_point();
1788 if (IsLocal(addrConnect
) ||
1789 FindNode((CNetAddr
)addrConnect
) || IsBanned(addrConnect
) ||
1790 FindNode(addrConnect
.ToStringIPPort()))
1792 } else if (FindNode(std::string(pszDest
)))
1795 CNode
* pnode
= ConnectNode(addrConnect
, pszDest
, fCountFailure
);
1796 boost::this_thread::interruption_point();
1801 grantOutbound
->MoveTo(pnode
->grantOutbound
);
1802 pnode
->fNetworkNode
= true;
1804 pnode
->fOneShot
= true;
1806 pnode
->fFeeler
= true;
1812 void CConnman::ThreadMessageHandler()
1814 boost::mutex condition_mutex
;
1815 boost::unique_lock
<boost::mutex
> lock(condition_mutex
);
1819 std::vector
<CNode
*> vNodesCopy
;
1822 vNodesCopy
= vNodes
;
1823 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
) {
1830 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
)
1832 if (pnode
->fDisconnect
)
1837 TRY_LOCK(pnode
->cs_vRecvMsg
, lockRecv
);
1840 if (!GetNodeSignals().ProcessMessages(pnode
, *this))
1841 pnode
->CloseSocketDisconnect();
1843 if (pnode
->nSendSize
< GetSendBufferSize())
1845 if (!pnode
->vRecvGetData
.empty() || (!pnode
->vRecvMsg
.empty() && pnode
->vRecvMsg
[0].complete()))
1852 boost::this_thread::interruption_point();
1856 TRY_LOCK(pnode
->cs_vSend
, lockSend
);
1858 GetNodeSignals().SendMessages(pnode
, *this);
1860 boost::this_thread::interruption_point();
1865 BOOST_FOREACH(CNode
* pnode
, vNodesCopy
)
1870 messageHandlerCondition
.timed_wait(lock
, boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(100));
1879 bool CConnman::BindListenPort(const CService
&addrBind
, std::string
& strError
, bool fWhitelisted
)
1884 // Create socket for listening for incoming connections
1885 struct sockaddr_storage sockaddr
;
1886 socklen_t len
= sizeof(sockaddr
);
1887 if (!addrBind
.GetSockAddr((struct sockaddr
*)&sockaddr
, &len
))
1889 strError
= strprintf("Error: Bind address family for %s not supported", addrBind
.ToString());
1890 LogPrintf("%s\n", strError
);
1894 SOCKET hListenSocket
= socket(((struct sockaddr
*)&sockaddr
)->sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
1895 if (hListenSocket
== INVALID_SOCKET
)
1897 strError
= strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
1898 LogPrintf("%s\n", strError
);
1901 if (!IsSelectableSocket(hListenSocket
))
1903 strError
= "Error: Couldn't create a listenable socket for incoming connections";
1904 LogPrintf("%s\n", strError
);
1911 // Different way of disabling SIGPIPE on BSD
1912 setsockopt(hListenSocket
, SOL_SOCKET
, SO_NOSIGPIPE
, (void*)&nOne
, sizeof(int));
1914 // Allow binding if the port is still in TIME_WAIT state after
1915 // the program was closed and restarted.
1916 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&nOne
, sizeof(int));
1917 // Disable Nagle's algorithm
1918 setsockopt(hListenSocket
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&nOne
, sizeof(int));
1920 setsockopt(hListenSocket
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&nOne
, sizeof(int));
1921 setsockopt(hListenSocket
, IPPROTO_TCP
, TCP_NODELAY
, (const char*)&nOne
, sizeof(int));
1924 // Set to non-blocking, incoming connections will also inherit this
1925 if (!SetSocketNonBlocking(hListenSocket
, true)) {
1926 strError
= strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
1927 LogPrintf("%s\n", strError
);
1931 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
1932 // and enable it by default or not. Try to enable it, if possible.
1933 if (addrBind
.IsIPv6()) {
1936 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_V6ONLY
, (const char*)&nOne
, sizeof(int));
1938 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void*)&nOne
, sizeof(int));
1942 int nProtLevel
= PROTECTION_LEVEL_UNRESTRICTED
;
1943 setsockopt(hListenSocket
, IPPROTO_IPV6
, IPV6_PROTECTION_LEVEL
, (const char*)&nProtLevel
, sizeof(int));
1947 if (::bind(hListenSocket
, (struct sockaddr
*)&sockaddr
, len
) == SOCKET_ERROR
)
1949 int nErr
= WSAGetLastError();
1950 if (nErr
== WSAEADDRINUSE
)
1951 strError
= strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind
.ToString(), _(PACKAGE_NAME
));
1953 strError
= strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind
.ToString(), NetworkErrorString(nErr
));
1954 LogPrintf("%s\n", strError
);
1955 CloseSocket(hListenSocket
);
1958 LogPrintf("Bound to %s\n", addrBind
.ToString());
1960 // Listen for incoming connections
1961 if (listen(hListenSocket
, SOMAXCONN
) == SOCKET_ERROR
)
1963 strError
= strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
1964 LogPrintf("%s\n", strError
);
1965 CloseSocket(hListenSocket
);
1969 vhListenSocket
.push_back(ListenSocket(hListenSocket
, fWhitelisted
));
1971 if (addrBind
.IsRoutable() && fDiscover
&& !fWhitelisted
)
1972 AddLocal(addrBind
, LOCAL_BIND
);
1977 void Discover(boost::thread_group
& threadGroup
)
1983 // Get local host IP
1984 char pszHostName
[256] = "";
1985 if (gethostname(pszHostName
, sizeof(pszHostName
)) != SOCKET_ERROR
)
1987 std::vector
<CNetAddr
> vaddr
;
1988 if (LookupHost(pszHostName
, vaddr
, 0, true))
1990 BOOST_FOREACH (const CNetAddr
&addr
, vaddr
)
1992 if (AddLocal(addr
, LOCAL_IF
))
1993 LogPrintf("%s: %s - %s\n", __func__
, pszHostName
, addr
.ToString());
1998 // Get local host ip
1999 struct ifaddrs
* myaddrs
;
2000 if (getifaddrs(&myaddrs
) == 0)
2002 for (struct ifaddrs
* ifa
= myaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
2004 if (ifa
->ifa_addr
== NULL
) continue;
2005 if ((ifa
->ifa_flags
& IFF_UP
) == 0) continue;
2006 if (strcmp(ifa
->ifa_name
, "lo") == 0) continue;
2007 if (strcmp(ifa
->ifa_name
, "lo0") == 0) continue;
2008 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
2010 struct sockaddr_in
* s4
= (struct sockaddr_in
*)(ifa
->ifa_addr
);
2011 CNetAddr
addr(s4
->sin_addr
);
2012 if (AddLocal(addr
, LOCAL_IF
))
2013 LogPrintf("%s: IPv4 %s: %s\n", __func__
, ifa
->ifa_name
, addr
.ToString());
2015 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
2017 struct sockaddr_in6
* s6
= (struct sockaddr_in6
*)(ifa
->ifa_addr
);
2018 CNetAddr
addr(s6
->sin6_addr
);
2019 if (AddLocal(addr
, LOCAL_IF
))
2020 LogPrintf("%s: IPv6 %s: %s\n", __func__
, ifa
->ifa_name
, addr
.ToString());
2023 freeifaddrs(myaddrs
);
2028 CConnman::CConnman(uint64_t nSeed0In
, uint64_t nSeed1In
) : nSeed0(nSeed0In
), nSeed1(nSeed1In
)
2030 setBannedIsDirty
= false;
2031 fAddressesInitialized
= false;
2033 nSendBufferMaxSize
= 0;
2034 nReceiveFloodSize
= 0;
2036 nMaxConnections
= 0;
2039 clientInterface
= NULL
;
2042 NodeId
CConnman::GetNewNodeId()
2044 return nLastNodeId
.fetch_add(1, std::memory_order_relaxed
);
2047 bool CConnman::Start(boost::thread_group
& threadGroup
, CScheduler
& scheduler
, std::string
& strNodeError
, Options connOptions
)
2049 nTotalBytesRecv
= 0;
2050 nTotalBytesSent
= 0;
2051 nMaxOutboundTotalBytesSentInCycle
= 0;
2052 nMaxOutboundCycleStartTime
= 0;
2054 nRelevantServices
= connOptions
.nRelevantServices
;
2055 nLocalServices
= connOptions
.nLocalServices
;
2056 nMaxConnections
= connOptions
.nMaxConnections
;
2057 nMaxOutbound
= std::min((connOptions
.nMaxOutbound
), nMaxConnections
);
2058 nMaxFeeler
= connOptions
.nMaxFeeler
;
2060 nSendBufferMaxSize
= connOptions
.nSendBufferMaxSize
;
2061 nReceiveFloodSize
= connOptions
.nSendBufferMaxSize
;
2063 nMaxOutboundLimit
= connOptions
.nMaxOutboundLimit
;
2064 nMaxOutboundTimeframe
= connOptions
.nMaxOutboundTimeframe
;
2066 SetBestHeight(connOptions
.nBestHeight
);
2068 clientInterface
= connOptions
.uiInterface
;
2069 if (clientInterface
)
2070 clientInterface
->InitMessage(_("Loading addresses..."));
2071 // Load addresses from peers.dat
2072 int64_t nStart
= GetTimeMillis();
2075 if (adb
.Read(addrman
))
2076 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman
.size(), GetTimeMillis() - nStart
);
2078 addrman
.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2079 LogPrintf("Invalid or missing peers.dat; recreating\n");
2083 if (clientInterface
)
2084 clientInterface
->InitMessage(_("Loading banlist..."));
2085 // Load addresses from banlist.dat
2086 nStart
= GetTimeMillis();
2089 if (bandb
.Read(banmap
)) {
2090 SetBanned(banmap
); // thread save setter
2091 SetBannedSetDirty(false); // no need to write down, just read data
2092 SweepBanned(); // sweep out unused entries
2094 LogPrint("net", "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2095 banmap
.size(), GetTimeMillis() - nStart
);
2097 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2098 SetBannedSetDirty(true); // force write
2102 uiInterface
.InitMessage(_("Starting network threads..."));
2104 fAddressesInitialized
= true;
2106 if (semOutbound
== NULL
) {
2107 // initialize semaphore
2108 semOutbound
= new CSemaphore(std::min((nMaxOutbound
+ nMaxFeeler
), nMaxConnections
));
2111 if (pnodeLocalHost
== NULL
) {
2113 LookupHost("127.0.0.1", local
, false);
2115 NodeId id
= GetNewNodeId();
2116 uint64_t nonce
= GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE
).Write(id
).Finalize();
2118 pnodeLocalHost
= new CNode(id
, nLocalServices
, GetBestHeight(), INVALID_SOCKET
, CAddress(CService(local
, 0), nLocalServices
), 0, nonce
);
2119 GetNodeSignals().InitializeNode(pnodeLocalHost
, *this);
2126 if (!GetBoolArg("-dnsseed", true))
2127 LogPrintf("DNS seeding disabled\n");
2129 threadGroup
.create_thread(boost::bind(&TraceThread
<boost::function
<void()> >, "dnsseed", boost::function
<void()>(boost::bind(&CConnman::ThreadDNSAddressSeed
, this))));
2131 // Send and receive from sockets, accept connections
2132 threadGroup
.create_thread(boost::bind(&TraceThread
<boost::function
<void()> >, "net", boost::function
<void()>(boost::bind(&CConnman::ThreadSocketHandler
, this))));
2134 // Initiate outbound connections from -addnode
2135 threadGroup
.create_thread(boost::bind(&TraceThread
<boost::function
<void()> >, "addcon", boost::function
<void()>(boost::bind(&CConnman::ThreadOpenAddedConnections
, this))));
2137 // Initiate outbound connections unless connect=0
2138 if (!mapArgs
.count("-connect") || mapMultiArgs
["-connect"].size() != 1 || mapMultiArgs
["-connect"][0] != "0")
2139 threadGroup
.create_thread(boost::bind(&TraceThread
<boost::function
<void()> >, "opencon", boost::function
<void()>(boost::bind(&CConnman::ThreadOpenConnections
, this))));
2142 threadGroup
.create_thread(boost::bind(&TraceThread
<boost::function
<void()> >, "msghand", boost::function
<void()>(boost::bind(&CConnman::ThreadMessageHandler
, this))));
2144 // Dump network addresses
2145 scheduler
.scheduleEvery(boost::bind(&CConnman::DumpData
, this), DUMP_ADDRESSES_INTERVAL
);
2158 // Shutdown Windows Sockets
2163 instance_of_cnetcleanup
;
2165 void CConnman::Stop()
2167 LogPrintf("%s\n",__func__
);
2169 for (int i
=0; i
<(nMaxOutbound
+ nMaxFeeler
); i
++)
2170 semOutbound
->post();
2172 if (fAddressesInitialized
)
2175 fAddressesInitialized
= false;
2179 BOOST_FOREACH(CNode
* pnode
, vNodes
)
2180 if (pnode
->hSocket
!= INVALID_SOCKET
)
2181 CloseSocket(pnode
->hSocket
);
2182 BOOST_FOREACH(ListenSocket
& hListenSocket
, vhListenSocket
)
2183 if (hListenSocket
.socket
!= INVALID_SOCKET
)
2184 if (!CloseSocket(hListenSocket
.socket
))
2185 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2187 // clean up some globals (to help leak detection)
2188 BOOST_FOREACH(CNode
*pnode
, vNodes
) {
2191 BOOST_FOREACH(CNode
*pnode
, vNodesDisconnected
) {
2195 vNodesDisconnected
.clear();
2196 vhListenSocket
.clear();
2200 DeleteNode(pnodeLocalHost
);
2201 pnodeLocalHost
= NULL
;
2204 void CConnman::DeleteNode(CNode
* pnode
)
2207 bool fUpdateConnectionTime
= false;
2208 GetNodeSignals().FinalizeNode(pnode
->GetId(), fUpdateConnectionTime
);
2209 if(fUpdateConnectionTime
)
2210 addrman
.Connected(pnode
->addr
);
2214 CConnman::~CConnman()
2219 size_t CConnman::GetAddressCount() const
2221 return addrman
.size();
2224 void CConnman::SetServices(const CService
&addr
, ServiceFlags nServices
)
2226 addrman
.SetServices(addr
, nServices
);
2229 void CConnman::MarkAddressGood(const CAddress
& addr
)
2234 void CConnman::AddNewAddress(const CAddress
& addr
, const CAddress
& addrFrom
, int64_t nTimePenalty
)
2236 addrman
.Add(addr
, addrFrom
, nTimePenalty
);
2239 void CConnman::AddNewAddresses(const std::vector
<CAddress
>& vAddr
, const CAddress
& addrFrom
, int64_t nTimePenalty
)
2241 addrman
.Add(vAddr
, addrFrom
, nTimePenalty
);
2244 std::vector
<CAddress
> CConnman::GetAddresses()
2246 return addrman
.GetAddr();
2249 bool CConnman::AddNode(const std::string
& strNode
)
2251 LOCK(cs_vAddedNodes
);
2252 for(std::vector
<std::string
>::const_iterator it
= vAddedNodes
.begin(); it
!= vAddedNodes
.end(); ++it
) {
2257 vAddedNodes
.push_back(strNode
);
2261 bool CConnman::RemoveAddedNode(const std::string
& strNode
)
2263 LOCK(cs_vAddedNodes
);
2264 for(std::vector
<std::string
>::iterator it
= vAddedNodes
.begin(); it
!= vAddedNodes
.end(); ++it
) {
2265 if (strNode
== *it
) {
2266 vAddedNodes
.erase(it
);
2273 size_t CConnman::GetNodeCount(NumConnections flags
)
2276 if (flags
== CConnman::CONNECTIONS_ALL
) // Shortcut if we want total
2277 return vNodes
.size();
2280 for(std::vector
<CNode
*>::const_iterator it
= vNodes
.begin(); it
!= vNodes
.end(); ++it
)
2281 if (flags
& ((*it
)->fInbound
? CONNECTIONS_IN
: CONNECTIONS_OUT
))
2287 void CConnman::GetNodeStats(std::vector
<CNodeStats
>& vstats
)
2291 vstats
.reserve(vNodes
.size());
2292 for(std::vector
<CNode
*>::iterator it
= vNodes
.begin(); it
!= vNodes
.end(); ++it
) {
2295 pnode
->copyStats(stats
);
2296 vstats
.push_back(stats
);
2300 bool CConnman::DisconnectAddress(const CNetAddr
& netAddr
)
2302 if (CNode
* pnode
= FindNode(netAddr
)) {
2303 pnode
->fDisconnect
= true;
2309 bool CConnman::DisconnectSubnet(const CSubNet
& subNet
)
2311 if (CNode
* pnode
= FindNode(subNet
)) {
2312 pnode
->fDisconnect
= true;
2318 bool CConnman::DisconnectNode(const std::string
& strNode
)
2320 if (CNode
* pnode
= FindNode(strNode
)) {
2321 pnode
->fDisconnect
= true;
2326 bool CConnman::DisconnectNode(NodeId id
)
2329 for(CNode
* pnode
: vNodes
) {
2330 if (id
== pnode
->id
) {
2331 pnode
->fDisconnect
= true;
2338 void CConnman::RelayTransaction(const CTransaction
& tx
)
2340 CInv
inv(MSG_TX
, tx
.GetHash());
2342 BOOST_FOREACH(CNode
* pnode
, vNodes
)
2344 pnode
->PushInventory(inv
);
2348 void CConnman::RecordBytesRecv(uint64_t bytes
)
2350 LOCK(cs_totalBytesRecv
);
2351 nTotalBytesRecv
+= bytes
;
2354 void CConnman::RecordBytesSent(uint64_t bytes
)
2356 LOCK(cs_totalBytesSent
);
2357 nTotalBytesSent
+= bytes
;
2359 uint64_t now
= GetTime();
2360 if (nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
< now
)
2362 // timeframe expired, reset cycle
2363 nMaxOutboundCycleStartTime
= now
;
2364 nMaxOutboundTotalBytesSentInCycle
= 0;
2367 // TODO, exclude whitebind peers
2368 nMaxOutboundTotalBytesSentInCycle
+= bytes
;
2371 void CConnman::SetMaxOutboundTarget(uint64_t limit
)
2373 LOCK(cs_totalBytesSent
);
2374 nMaxOutboundLimit
= limit
;
2377 uint64_t CConnman::GetMaxOutboundTarget()
2379 LOCK(cs_totalBytesSent
);
2380 return nMaxOutboundLimit
;
2383 uint64_t CConnman::GetMaxOutboundTimeframe()
2385 LOCK(cs_totalBytesSent
);
2386 return nMaxOutboundTimeframe
;
2389 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2391 LOCK(cs_totalBytesSent
);
2392 if (nMaxOutboundLimit
== 0)
2395 if (nMaxOutboundCycleStartTime
== 0)
2396 return nMaxOutboundTimeframe
;
2398 uint64_t cycleEndTime
= nMaxOutboundCycleStartTime
+ nMaxOutboundTimeframe
;
2399 uint64_t now
= GetTime();
2400 return (cycleEndTime
< now
) ? 0 : cycleEndTime
- GetTime();
2403 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe
)
2405 LOCK(cs_totalBytesSent
);
2406 if (nMaxOutboundTimeframe
!= timeframe
)
2408 // reset measure-cycle in case of changing
2410 nMaxOutboundCycleStartTime
= GetTime();
2412 nMaxOutboundTimeframe
= timeframe
;
2415 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit
)
2417 LOCK(cs_totalBytesSent
);
2418 if (nMaxOutboundLimit
== 0)
2421 if (historicalBlockServingLimit
)
2423 // keep a large enough buffer to at least relay each block once
2424 uint64_t timeLeftInCycle
= GetMaxOutboundTimeLeftInCycle();
2425 uint64_t buffer
= timeLeftInCycle
/ 600 * MAX_BLOCK_SERIALIZED_SIZE
;
2426 if (buffer
>= nMaxOutboundLimit
|| nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
- buffer
)
2429 else if (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
)
2435 uint64_t CConnman::GetOutboundTargetBytesLeft()
2437 LOCK(cs_totalBytesSent
);
2438 if (nMaxOutboundLimit
== 0)
2441 return (nMaxOutboundTotalBytesSentInCycle
>= nMaxOutboundLimit
) ? 0 : nMaxOutboundLimit
- nMaxOutboundTotalBytesSentInCycle
;
2444 uint64_t CConnman::GetTotalBytesRecv()
2446 LOCK(cs_totalBytesRecv
);
2447 return nTotalBytesRecv
;
2450 uint64_t CConnman::GetTotalBytesSent()
2452 LOCK(cs_totalBytesSent
);
2453 return nTotalBytesSent
;
2456 ServiceFlags
CConnman::GetLocalServices() const
2458 return nLocalServices
;
2461 void CConnman::SetBestHeight(int height
)
2463 nBestHeight
.store(height
, std::memory_order_release
);
2466 int CConnman::GetBestHeight() const
2468 return nBestHeight
.load(std::memory_order_acquire
);
2471 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize
; }
2472 unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize
; }
2474 CNode::CNode(NodeId idIn
, ServiceFlags nLocalServicesIn
, int nMyStartingHeightIn
, SOCKET hSocketIn
, const CAddress
& addrIn
, uint64_t nKeyedNetGroupIn
, uint64_t nLocalHostNonceIn
, const std::string
& addrNameIn
, bool fInboundIn
) :
2476 fInbound(fInboundIn
),
2478 nKeyedNetGroup(nKeyedNetGroupIn
),
2479 addrKnown(5000, 0.001),
2480 filterInventoryKnown(50000, 0.000001),
2481 nLocalHostNonce(nLocalHostNonceIn
),
2482 nLocalServices(nLocalServicesIn
),
2483 nMyStartingHeight(nMyStartingHeightIn
),
2486 nServices
= NODE_NONE
;
2487 nServicesExpected
= NODE_NONE
;
2488 hSocket
= hSocketIn
;
2489 nRecvVersion
= INIT_PROTO_VERSION
;
2494 nTimeConnected
= GetTime();
2496 addrName
= addrNameIn
== "" ? addr
.ToStringIPPort() : addrNameIn
;
2499 fWhitelisted
= false;
2501 fClient
= false; // set by version message
2503 fNetworkNode
= false;
2504 fSuccessfullyConnected
= false;
2505 fDisconnect
= false;
2509 hashContinue
= uint256();
2510 nStartingHeight
= -1;
2511 filterInventoryKnown
.reset();
2512 fSendMempool
= false;
2514 nNextLocalAddrSend
= 0;
2519 pfilter
= new CBloomFilter();
2520 timeLastMempoolReq
= 0;
2526 fPingQueued
= false;
2527 nMinPingUsecTime
= std::numeric_limits
<int64_t>::max();
2529 lastSentFeeFilter
= 0;
2530 nextSendTimeFeeFilter
= 0;
2532 BOOST_FOREACH(const std::string
&msg
, getAllNetMessageTypes())
2533 mapRecvBytesPerMsgCmd
[msg
] = 0;
2534 mapRecvBytesPerMsgCmd
[NET_MESSAGE_COMMAND_OTHER
] = 0;
2537 LogPrint("net", "Added connection to %s peer=%d\n", addrName
, id
);
2539 LogPrint("net", "Added connection peer=%d\n", id
);
2544 CloseSocket(hSocket
);
2550 void CNode::AskFor(const CInv
& inv
)
2552 if (mapAskFor
.size() > MAPASKFOR_MAX_SZ
|| setAskFor
.size() > SETASKFOR_MAX_SZ
)
2554 // a peer may not have multiple non-responded queue positions for a single inv item
2555 if (!setAskFor
.insert(inv
.hash
).second
)
2558 // We're using mapAskFor as a priority queue,
2559 // the key is the earliest time the request can be sent
2560 int64_t nRequestTime
;
2561 limitedmap
<uint256
, int64_t>::const_iterator it
= mapAlreadyAskedFor
.find(inv
.hash
);
2562 if (it
!= mapAlreadyAskedFor
.end())
2563 nRequestTime
= it
->second
;
2566 LogPrint("net", "askfor %s %d (%s) peer=%d\n", inv
.ToString(), nRequestTime
, DateTimeStrFormat("%H:%M:%S", nRequestTime
/1000000), id
);
2568 // Make sure not to reuse time indexes to keep things in the same order
2569 int64_t nNow
= GetTimeMicros() - 1000000;
2570 static int64_t nLastTime
;
2572 nNow
= std::max(nNow
, nLastTime
);
2575 // Each retry is 2 minutes after the last
2576 nRequestTime
= std::max(nRequestTime
+ 2 * 60 * 1000000, nNow
);
2577 if (it
!= mapAlreadyAskedFor
.end())
2578 mapAlreadyAskedFor
.update(it
, nRequestTime
);
2580 mapAlreadyAskedFor
.insert(std::make_pair(inv
.hash
, nRequestTime
));
2581 mapAskFor
.insert(std::make_pair(nRequestTime
, inv
));
2584 CDataStream
CConnman::BeginMessage(CNode
* pnode
, int nVersion
, int flags
, const std::string
& sCommand
)
2586 return {SER_NETWORK
, (nVersion
? nVersion
: pnode
->GetSendVersion()) | flags
, CMessageHeader(Params().MessageStart(), sCommand
.c_str(), 0) };
2589 void CConnman::EndMessage(CDataStream
& strm
)
2592 assert(strm
.size () >= CMessageHeader::HEADER_SIZE
);
2593 unsigned int nSize
= strm
.size() - CMessageHeader::HEADER_SIZE
;
2594 WriteLE32((uint8_t*)&strm
[CMessageHeader::MESSAGE_SIZE_OFFSET
], nSize
);
2596 uint256 hash
= Hash(strm
.begin() + CMessageHeader::HEADER_SIZE
, strm
.end());
2597 memcpy((char*)&strm
[CMessageHeader::CHECKSUM_OFFSET
], hash
.begin(), CMessageHeader::CHECKSUM_SIZE
);
2601 void CConnman::PushMessage(CNode
* pnode
, CDataStream
& strm
, const std::string
& sCommand
)
2606 unsigned int nSize
= strm
.size() - CMessageHeader::HEADER_SIZE
;
2607 LogPrint("net", "sending %s (%d bytes) peer=%d\n", SanitizeString(sCommand
.c_str()), nSize
, pnode
->id
);
2609 size_t nBytesSent
= 0;
2611 LOCK(pnode
->cs_vSend
);
2612 if(pnode
->hSocket
== INVALID_SOCKET
) {
2615 bool optimisticSend(pnode
->vSendMsg
.empty());
2616 pnode
->vSendMsg
.emplace_back(strm
.begin(), strm
.end());
2618 //log total amount of bytes per command
2619 pnode
->mapSendBytesPerMsgCmd
[sCommand
] += strm
.size();
2620 pnode
->nSendSize
+= strm
.size();
2622 // If write queue empty, attempt "optimistic write"
2623 if (optimisticSend
== true)
2624 nBytesSent
= SocketSendData(pnode
);
2627 RecordBytesSent(nBytesSent
);
2630 bool CConnman::ForNode(NodeId id
, std::function
<bool(CNode
* pnode
)> func
)
2632 CNode
* found
= nullptr;
2634 for (auto&& pnode
: vNodes
) {
2635 if(pnode
->id
== id
) {
2640 return found
!= nullptr && func(found
);
2643 int64_t PoissonNextSend(int64_t nNow
, int average_interval_seconds
) {
2644 return nNow
+ (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds
* -1000000.0 + 0.5);
2647 CSipHasher
CConnman::GetDeterministicRandomizer(uint64_t id
)
2649 return CSipHasher(nSeed0
, nSeed1
).Write(id
);
2652 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress
& ad
)
2654 std::vector
<unsigned char> vchNetGroup(ad
.GetGroup());
2656 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP
).Write(&vchNetGroup
[0], vchNetGroup
.size()).Finalize();