Remove unused var UNLIKELY_PCT from fees.h
[bitcoinplatinum.git] / src / net.cpp
blobe47a8bb1682f7301caefa4d799cc9f4696bf34be
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"
8 #endif
10 #include "net.h"
12 #include "addrman.h"
13 #include "chainparams.h"
14 #include "clientversion.h"
15 #include "consensus/consensus.h"
16 #include "crypto/common.h"
17 #include "crypto/sha256.h"
18 #include "hash.h"
19 #include "primitives/transaction.h"
20 #include "netbase.h"
21 #include "scheduler.h"
22 #include "ui_interface.h"
23 #include "utilstrencodings.h"
25 #ifdef WIN32
26 #include <string.h>
27 #else
28 #include <fcntl.h>
29 #endif
31 #ifdef USE_UPNP
32 #include <miniupnpc/miniupnpc.h>
33 #include <miniupnpc/miniwget.h>
34 #include <miniupnpc/upnpcommands.h>
35 #include <miniupnpc/upnperrors.h>
36 #endif
38 #include <boost/filesystem.hpp>
39 #include <boost/thread.hpp>
41 #include <math.h>
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
51 #endif
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.
55 #ifdef WIN32
56 #ifndef PROTECTION_LEVEL_UNRESTRICTED
57 #define PROTECTION_LEVEL_UNRESTRICTED 10
58 #endif
59 #ifndef IPV6_PROTECTION_LEVEL
60 #define IPV6_PROTECTION_LEVEL 23
61 #endif
62 #endif
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;
72 bool fListen = 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)
88 LOCK(cs_vOneShots);
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)
100 if (!fListen)
101 return false;
103 int nBestScore = -1;
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;
115 nBestScore = nScore;
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
128 // weeks ago.
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)
134 struct in6_addr ip;
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);
140 return vSeedsOut;
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
146 // one by discovery.
147 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
149 CAddress ret(CService(CNetAddr(),GetListenPort()), NODE_NONE);
150 CService addr;
151 if (GetLocal(addr, paddrPeer))
153 ret = CAddress(addr, nLocalServices);
155 ret.nTime = GetAdjustedTime();
156 return ret;
159 int GetnScore(const CService& addr)
161 LOCK(cs_mapLocalHost);
162 if (mapLocalHost.count(addr) == LOCAL_NONE)
163 return 0;
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())
201 return false;
203 if (!fDiscover && nScore < LOCAL_MANUAL)
204 return false;
206 if (IsLimited(addr))
207 return false;
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();
221 return true;
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);
234 return true;
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)
241 return;
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)
263 return false;
264 mapLocalHost[addr].nScore++;
266 return true;
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)
294 LOCK(cs_vNodes);
295 BOOST_FOREACH(CNode* pnode, vNodes)
296 if ((CNetAddr)pnode->addr == ip)
297 return (pnode);
298 return NULL;
301 CNode* CConnman::FindNode(const CSubNet& subNet)
303 LOCK(cs_vNodes);
304 BOOST_FOREACH(CNode* pnode, vNodes)
305 if (subNet.Match((CNetAddr)pnode->addr))
306 return (pnode);
307 return NULL;
310 CNode* CConnman::FindNode(const std::string& addrName)
312 LOCK(cs_vNodes);
313 BOOST_FOREACH(CNode* pnode, vNodes)
314 if (pnode->addrName == addrName)
315 return (pnode);
316 return NULL;
319 CNode* CConnman::FindNode(const CService& addr)
321 LOCK(cs_vNodes);
322 BOOST_FOREACH(CNode* pnode, vNodes)
323 if ((CService)pnode->addr == addr)
324 return (pnode);
325 return NULL;
328 bool CConnman::CheckIncomingNonce(uint64_t nonce)
330 LOCK(cs_vNodes);
331 BOOST_FOREACH(CNode* pnode, vNodes) {
332 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
333 return false;
335 return true;
338 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
340 if (pszDest == NULL) {
341 if (IsLocal(addrConnect))
342 return NULL;
344 // Look for an existing connection
345 CNode* pnode = FindNode((CService)addrConnect);
346 if (pnode)
348 pnode->AddRef();
349 return pnode;
353 /// debug print
354 LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
355 pszDest ? pszDest : addrConnect.ToString(),
356 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
358 // Connect
359 SOCKET hSocket;
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);
367 return NULL;
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);
376 if (pnode)
378 pnode->AddRef();
380 LOCK(cs_vNodes);
381 if (pnode->addrName.empty()) {
382 pnode->addrName = std::string(pszDest);
385 CloseSocket(hSocket);
386 return pnode;
390 addrman.Attempt(addrConnect, fCountFailure);
392 // Add node
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();
398 pnode->AddRef();
399 GetNodeSignals().InitializeNode(pnode, *this);
401 LOCK(cs_vNodes);
402 vNodes.push_back(pnode);
405 return 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);
412 return NULL;
415 void CConnman::DumpBanlist()
417 SweepBanned(); // clean unused entries (if bantime has expired)
419 if (!BannedSetIsDirty())
420 return;
422 int64_t nStart = GetTimeMillis();
424 CBanDB bandb;
425 banmap_t banmap;
426 SetBannedSetDirty(false);
427 GetBanned(banmap);
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()
437 fDisconnect = true;
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);
446 if (lockRecv)
447 vRecvMsg.clear();
450 void CConnman::ClearBanned()
453 LOCK(cs_setBanned);
454 setBanned.clear();
455 setBannedIsDirty = true;
457 DumpBanlist(); //store banlist to disk
458 if(clientInterface)
459 clientInterface->BannedListChanged();
462 bool CConnman::IsBanned(CNetAddr ip)
464 bool fResult = false;
466 LOCK(cs_setBanned);
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)
473 fResult = true;
476 return fResult;
479 bool CConnman::IsBanned(CSubNet subnet)
481 bool fResult = false;
483 LOCK(cs_setBanned);
484 banmap_t::iterator i = setBanned.find(subnet);
485 if (i != setBanned.end())
487 CBanEntry banEntry = (*i).second;
488 if (GetTime() < banEntry.nBanUntil)
489 fResult = true;
492 return fResult;
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;
511 LOCK(cs_setBanned);
512 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
513 setBanned[subNet] = banEntry;
514 setBannedIsDirty = true;
516 else
517 return;
519 if(clientInterface)
520 clientInterface->BannedListChanged();
522 LOCK(cs_vNodes);
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) {
539 LOCK(cs_setBanned);
540 if (!setBanned.erase(subNet))
541 return false;
542 setBannedIsDirty = true;
544 if(clientInterface)
545 clientInterface->BannedListChanged();
546 DumpBanlist(); //store banlist to disk immediately
547 return true;
550 void CConnman::GetBanned(banmap_t &banMap)
552 LOCK(cs_setBanned);
553 banMap = setBanned; //create a thread safe copy
556 void CConnman::SetBanned(const banmap_t &banMap)
558 LOCK(cs_setBanned);
559 setBanned = banMap;
560 setBannedIsDirty = true;
563 void CConnman::SweepBanned()
565 int64_t now = GetTime();
567 LOCK(cs_setBanned);
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());
579 else
580 ++it;
584 bool CConnman::BannedSetIsDirty()
586 LOCK(cs_setBanned);
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))
601 return true;
603 return false;
606 void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
607 LOCK(cs_vWhitelistedRange);
608 vWhitelistedRange.push_back(subnet);
611 #undef X
612 #define X(name) stats.name = name
613 void CNode::copyStats(CNodeStats &stats)
615 stats.nodeid = this->GetId();
616 X(nServices);
617 X(addr);
618 X(fRelayTxes);
619 X(nLastSend);
620 X(nLastRecv);
621 X(nTimeConnected);
622 X(nTimeOffset);
623 X(addrName);
624 X(nVersion);
625 X(cleanSubVer);
626 X(fInbound);
627 X(nStartingHeight);
628 X(nSendBytes);
629 X(mapSendBytesPerMsgCmd);
630 X(nRecvBytes);
631 X(mapRecvBytesPerMsgCmd);
632 X(fWhitelisted);
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() : "";
653 #undef X
655 // requires LOCK(cs_vRecvMsg)
656 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
658 complete = false;
659 while (nBytes > 0) {
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
669 int handled;
670 if (!msg.in_data)
671 handled = msg.readHeader(pch, nBytes);
672 else
673 handled = msg.readData(pch, nBytes);
675 if (handled < 0)
676 return false;
678 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
679 LogPrint("net", "Oversized message from peer=%i, disconnecting\n", GetId());
680 return false;
683 pch += handled;
684 nBytes -= handled;
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();
697 complete = true;
701 return true;
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);
711 nHdrPos += nCopy;
713 // if header incomplete, exit
714 if (nHdrPos < 24)
715 return nCopy;
717 // deserialize to CMessageHeader
718 try {
719 hdrbuf >> hdr;
721 catch (const std::exception&) {
722 return -1;
725 // reject messages larger than MAX_SIZE
726 if (hdr.nMessageSize > MAX_SIZE)
727 return -1;
729 // switch state to reading message data
730 in_data = true;
732 return nCopy;
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);
747 nDataPos += nCopy;
749 return nCopy;
752 const uint256& CNetMessage::GetMessageHash() const
754 assert(complete());
755 if (data_hash.IsNull())
756 hasher.Finalize(data_hash.begin());
757 return data_hash;
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);
778 if (nBytes > 0) {
779 pnode->nLastSend = GetTime();
780 pnode->nSendBytes += nBytes;
781 pnode->nSendOffset += nBytes;
782 nSentSize += nBytes;
783 if (pnode->nSendOffset == data.size()) {
784 pnode->nSendOffset = 0;
785 pnode->nSendSize -= data.size();
786 it++;
787 } else {
788 // could not send full message; stop sending more
789 break;
791 } else {
792 if (nBytes < 0) {
793 // error
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
802 break;
806 if (it == pnode->vSendMsg.end()) {
807 assert(pnode->nSendOffset == 0);
808 assert(pnode->nSendSize == 0);
810 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
811 return nSentSize;
814 struct NodeEvictionCandidate
816 NodeId id;
817 int64_t nTimeConnected;
818 int64_t nMinPingUsecTime;
819 int64_t nLastBlockTime;
820 int64_t nLastTXTime;
821 bool fRelevantServices;
822 bool fRelayTxes;
823 bool fBloomFilter;
824 CAddress addr;
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;
871 LOCK(cs_vNodes);
873 BOOST_FOREACH(CNode *node, vNodes) {
874 if (node->fWhitelisted)
875 continue;
876 if (!node->fInbound)
877 continue;
878 if (node->fDisconnect)
879 continue;
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;
950 LOCK(cs_vNodes);
951 for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
952 if ((*it)->GetId() == evicted) {
953 (*it)->fDisconnect = true;
954 return true;
957 return false;
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);
964 CAddress addr;
965 int nInbound = 0;
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);
974 LOCK(cs_vNodes);
975 BOOST_FOREACH(CNode* pnode, vNodes)
976 if (pnode->fInbound)
977 nInbound++;
980 if (hSocket == INVALID_SOCKET)
982 int nErr = WSAGetLastError();
983 if (nErr != WSAEWOULDBLOCK)
984 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
985 return;
988 if (!IsSelectableSocket(hSocket))
990 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
991 CloseSocket(hSocket);
992 return;
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.
997 int set = 1;
998 #ifdef WIN32
999 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
1000 #else
1001 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
1002 #endif
1004 if (IsBanned(addr) && !whitelisted)
1006 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
1007 CloseSocket(hSocket);
1008 return;
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);
1017 return;
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);
1025 pnode->AddRef();
1026 pnode->fWhitelisted = whitelisted;
1027 GetNodeSignals().InitializeNode(pnode, *this);
1029 LogPrint("net", "connection from %s accepted\n", addr.ToString());
1032 LOCK(cs_vNodes);
1033 vNodes.push_back(pnode);
1037 void CConnman::ThreadSocketHandler()
1039 unsigned int nPrevNodeCount = 0;
1040 while (true)
1043 // Disconnect nodes
1046 LOCK(cs_vNodes);
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)
1065 pnode->Release();
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);
1081 if (lockSend)
1083 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1084 if (lockRecv)
1086 TRY_LOCK(pnode->cs_inventory, lockInv);
1087 if (lockInv)
1088 fDelete = true;
1092 if (fDelete)
1094 vNodesDisconnected.remove(pnode);
1095 DeleteNode(pnode);
1100 if(vNodes.size() != nPrevNodeCount) {
1101 nPrevNodeCount = vNodes.size();
1102 if(clientInterface)
1103 clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
1107 // Find which sockets have data to receive
1109 struct timeval timeout;
1110 timeout.tv_sec = 0;
1111 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1113 fd_set fdsetRecv;
1114 fd_set fdsetSend;
1115 fd_set fdsetError;
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);
1125 have_fds = true;
1129 LOCK(cs_vNodes);
1130 BOOST_FOREACH(CNode* pnode, vNodes)
1132 if (pnode->hSocket == INVALID_SOCKET)
1133 continue;
1134 FD_SET(pnode->hSocket, &fdsetError);
1135 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1136 have_fds = true;
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);
1155 if (lockSend) {
1156 if (!pnode->vSendMsg.empty()) {
1157 FD_SET(pnode->hSocket, &fdsetSend);
1158 continue;
1163 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1164 if (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)
1178 if (have_fds)
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;
1206 LOCK(cs_vNodes);
1207 vNodesCopy = vNodes;
1208 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1209 pnode->AddRef();
1211 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1213 boost::this_thread::interruption_point();
1216 // Receive
1218 if (pnode->hSocket == INVALID_SOCKET)
1219 continue;
1220 if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
1222 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1223 if (lockRecv)
1226 // typical socket buffer is 8K-64K
1227 char pchBuf[0x10000];
1228 int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1229 if (nBytes > 0)
1231 bool notify = false;
1232 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1233 pnode->CloseSocketDisconnect();
1234 if(notify)
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)
1249 // error
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();
1263 // Send
1265 if (pnode->hSocket == INVALID_SOCKET)
1266 continue;
1267 if (FD_ISSET(pnode->hSocket, &fdsetSend))
1269 TRY_LOCK(pnode->cs_vSend, lockSend);
1270 if (lockSend) {
1271 size_t nBytes = SocketSendData(pnode);
1272 if (nBytes)
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;
1306 LOCK(cs_vNodes);
1307 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1308 pnode->Release();
1321 #ifdef USE_UPNP
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;
1328 char lanaddr[64];
1330 #ifndef UPNPDISCOVER_SUCCESS
1331 /* miniupnpc 1.5 */
1332 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1333 #elif MINIUPNPC_API_VERSION < 14
1334 /* miniupnpc 1.6 */
1335 int error = 0;
1336 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1337 #else
1338 /* miniupnpc 1.9.20150730 */
1339 int error = 0;
1340 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1341 #endif
1343 struct UPNPUrls urls;
1344 struct IGDdatas data;
1345 int r;
1347 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1348 if (r == 1)
1350 if (fDiscover) {
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);
1355 else
1357 if(externalIPAddress[0])
1359 CNetAddr resolved;
1360 if(LookupHost(externalIPAddress, resolved, false)) {
1361 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1362 AddLocal(resolved, LOCAL_UPNP);
1365 else
1366 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1370 std::string strDesc = "Bitcoin " + FormatFullVersion();
1372 try {
1373 while (true) {
1374 #ifndef UPNPDISCOVER_SUCCESS
1375 /* miniupnpc 1.5 */
1376 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1377 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1378 #else
1379 /* miniupnpc 1.6 */
1380 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1381 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1382 #endif
1384 if(r!=UPNPCOMMAND_SUCCESS)
1385 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1386 port, port, lanaddr, r, strupnperror(r));
1387 else
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);
1399 throw;
1401 } else {
1402 LogPrintf("No valid UPnP IGDs found\n");
1403 freeUPNPDevlist(devlist); devlist = 0;
1404 if (r != 0)
1405 FreeUPNPUrls(&urls);
1409 void MapPort(bool fUseUPnP)
1411 static boost::thread* upnp_thread = NULL;
1413 if (fUseUPnP)
1415 if (upnp_thread) {
1416 upnp_thread->interrupt();
1417 upnp_thread->join();
1418 delete upnp_thread;
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();
1425 delete upnp_thread;
1426 upnp_thread = NULL;
1430 #else
1431 void MapPort(bool)
1433 // Intentionally left blank.
1435 #endif
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;
1447 return data.host;
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);
1465 LOCK(cs_vNodes);
1466 int nRelevant = 0;
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");
1472 return;
1476 const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1477 int found = 0;
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);
1484 } else {
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);
1496 found++;
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();
1529 CAddrDB adb;
1530 adb.Write(addrman);
1532 LogPrint("net", "Flushed %d addresses to peers.dat %dms\n",
1533 addrman.size(), GetTimeMillis() - nStart);
1536 void CConnman::DumpData()
1538 DumpAddresses();
1539 DumpBanlist();
1542 void CConnman::ProcessOneShot()
1544 std::string strDest;
1546 LOCK(cs_vOneShots);
1547 if (vOneShots.empty())
1548 return;
1549 strDest = vOneShots.front();
1550 vOneShots.pop_front();
1552 CAddress addr;
1553 CSemaphoreGrant grant(*semOutbound, true);
1554 if (grant) {
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++)
1567 ProcessOneShot();
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++)
1574 MilliSleep(500);
1577 MilliSleep(500);
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);
1586 while (true)
1588 ProcessOneShot();
1590 MilliSleep(500);
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;
1598 if (!done) {
1599 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1600 CNetAddr local;
1601 LookupHost("127.0.0.1", local, false);
1602 addrman.Add(convertSeed6(Params().FixedSeeds()), local);
1603 done = true;
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.
1614 int nOutbound = 0;
1615 std::set<std::vector<unsigned char> > setConnected;
1617 LOCK(cs_vNodes);
1618 BOOST_FOREACH(CNode* pnode, vNodes) {
1619 if (!pnode->fInbound) {
1620 setConnected.insert(pnode->addr.GetGroup());
1621 nOutbound++;
1626 // Feeler Connections
1628 // Design goals:
1629 // * Increase the number of connectable addresses in the tried table.
1631 // Method:
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
1635 // connections.
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);
1643 fFeeler = true;
1644 } else {
1645 continue;
1649 int64_t nANow = GetAdjustedTime();
1650 int nTries = 0;
1651 while (true)
1653 CAddrInfo addr = addrman.Select(fFeeler);
1655 // if we selected an invalid address, restart
1656 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1657 break;
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.
1662 nTries++;
1663 if (nTries > 100)
1664 break;
1666 if (IsLimited(addr))
1667 continue;
1669 // only connect to full nodes
1670 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1671 continue;
1673 // only consider very recently tried nodes after 30 failed attempts
1674 if (nANow - addr.nLastTry < 600 && nTries < 30)
1675 continue;
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)))
1679 continue;
1681 // do not allow non-default ports, unless after 50 invalid addresses selected already
1682 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1683 continue;
1685 addrConnect = addr;
1686 break;
1689 if (addrConnect.IsValid()) {
1691 if (fFeeler) {
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;
1720 LOCK(cs_vNodes);
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});
1738 } else {
1739 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1741 } else {
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});
1746 } else {
1747 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1752 return ret;
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);
1772 MilliSleep(500);
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();
1787 if (!pszDest) {
1788 if (IsLocal(addrConnect) ||
1789 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1790 FindNode(addrConnect.ToStringIPPort()))
1791 return false;
1792 } else if (FindNode(std::string(pszDest)))
1793 return false;
1795 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1796 boost::this_thread::interruption_point();
1798 if (!pnode)
1799 return false;
1800 if (grantOutbound)
1801 grantOutbound->MoveTo(pnode->grantOutbound);
1802 pnode->fNetworkNode = true;
1803 if (fOneShot)
1804 pnode->fOneShot = true;
1805 if (fFeeler)
1806 pnode->fFeeler = true;
1808 return true;
1812 void CConnman::ThreadMessageHandler()
1814 boost::mutex condition_mutex;
1815 boost::unique_lock<boost::mutex> lock(condition_mutex);
1817 while (true)
1819 std::vector<CNode*> vNodesCopy;
1821 LOCK(cs_vNodes);
1822 vNodesCopy = vNodes;
1823 BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1824 pnode->AddRef();
1828 bool fSleep = true;
1830 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1832 if (pnode->fDisconnect)
1833 continue;
1835 // Receive messages
1837 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1838 if (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()))
1847 fSleep = false;
1852 boost::this_thread::interruption_point();
1854 // Send messages
1856 TRY_LOCK(pnode->cs_vSend, lockSend);
1857 if (lockSend)
1858 GetNodeSignals().SendMessages(pnode, *this);
1860 boost::this_thread::interruption_point();
1864 LOCK(cs_vNodes);
1865 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1866 pnode->Release();
1869 if (fSleep)
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)
1881 strError = "";
1882 int nOne = 1;
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);
1891 return false;
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);
1899 return false;
1901 if (!IsSelectableSocket(hListenSocket))
1903 strError = "Error: Couldn't create a listenable socket for incoming connections";
1904 LogPrintf("%s\n", strError);
1905 return false;
1909 #ifndef WIN32
1910 #ifdef SO_NOSIGPIPE
1911 // Different way of disabling SIGPIPE on BSD
1912 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
1913 #endif
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));
1919 #else
1920 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
1921 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
1922 #endif
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);
1928 return false;
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()) {
1934 #ifdef IPV6_V6ONLY
1935 #ifdef WIN32
1936 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
1937 #else
1938 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
1939 #endif
1940 #endif
1941 #ifdef WIN32
1942 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
1943 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
1944 #endif
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));
1952 else
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);
1956 return false;
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);
1966 return false;
1969 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
1971 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
1972 AddLocal(addrBind, LOCAL_BIND);
1974 return true;
1977 void Discover(boost::thread_group& threadGroup)
1979 if (!fDiscover)
1980 return;
1982 #ifdef WIN32
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());
1997 #else
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);
2025 #endif
2028 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2030 setBannedIsDirty = false;
2031 fAddressesInitialized = false;
2032 nLastNodeId = 0;
2033 nSendBufferMaxSize = 0;
2034 nReceiveFloodSize = 0;
2035 semOutbound = NULL;
2036 nMaxConnections = 0;
2037 nMaxOutbound = 0;
2038 nBestHeight = 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();
2074 CAddrDB adb;
2075 if (adb.Read(addrman))
2076 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2077 else {
2078 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2079 LogPrintf("Invalid or missing peers.dat; recreating\n");
2080 DumpAddresses();
2083 if (clientInterface)
2084 clientInterface->InitMessage(_("Loading banlist..."));
2085 // Load addresses from banlist.dat
2086 nStart = GetTimeMillis();
2087 CBanDB bandb;
2088 banmap_t banmap;
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);
2096 } else {
2097 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2098 SetBannedSetDirty(true); // force write
2099 DumpBanlist();
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) {
2112 CNetAddr local;
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);
2123 // Start threads
2126 if (!GetBoolArg("-dnsseed", true))
2127 LogPrintf("DNS seeding disabled\n");
2128 else
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))));
2141 // Process messages
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);
2147 return true;
2150 class CNetCleanup
2152 public:
2153 CNetCleanup() {}
2155 ~CNetCleanup()
2157 #ifdef WIN32
2158 // Shutdown Windows Sockets
2159 WSACleanup();
2160 #endif
2163 instance_of_cnetcleanup;
2165 void CConnman::Stop()
2167 LogPrintf("%s\n",__func__);
2168 if (semOutbound)
2169 for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++)
2170 semOutbound->post();
2172 if (fAddressesInitialized)
2174 DumpData();
2175 fAddressesInitialized = false;
2178 // Close sockets
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) {
2189 DeleteNode(pnode);
2191 BOOST_FOREACH(CNode *pnode, vNodesDisconnected) {
2192 DeleteNode(pnode);
2194 vNodes.clear();
2195 vNodesDisconnected.clear();
2196 vhListenSocket.clear();
2197 delete semOutbound;
2198 semOutbound = NULL;
2199 if(pnodeLocalHost)
2200 DeleteNode(pnodeLocalHost);
2201 pnodeLocalHost = NULL;
2204 void CConnman::DeleteNode(CNode* pnode)
2206 assert(pnode);
2207 bool fUpdateConnectionTime = false;
2208 GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2209 if(fUpdateConnectionTime)
2210 addrman.Connected(pnode->addr);
2211 delete pnode;
2214 CConnman::~CConnman()
2216 Stop();
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)
2231 addrman.Good(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) {
2253 if (strNode == *it)
2254 return false;
2257 vAddedNodes.push_back(strNode);
2258 return true;
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);
2267 return true;
2270 return false;
2273 size_t CConnman::GetNodeCount(NumConnections flags)
2275 LOCK(cs_vNodes);
2276 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2277 return vNodes.size();
2279 int nNum = 0;
2280 for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
2281 if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
2282 nNum++;
2284 return nNum;
2287 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2289 vstats.clear();
2290 LOCK(cs_vNodes);
2291 vstats.reserve(vNodes.size());
2292 for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
2293 CNode* pnode = *it;
2294 CNodeStats stats;
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;
2304 return true;
2306 return false;
2309 bool CConnman::DisconnectSubnet(const CSubNet& subNet)
2311 if (CNode* pnode = FindNode(subNet)) {
2312 pnode->fDisconnect = true;
2313 return true;
2315 return false;
2318 bool CConnman::DisconnectNode(const std::string& strNode)
2320 if (CNode* pnode = FindNode(strNode)) {
2321 pnode->fDisconnect = true;
2322 return true;
2324 return false;
2326 bool CConnman::DisconnectNode(NodeId id)
2328 LOCK(cs_vNodes);
2329 for(CNode* pnode : vNodes) {
2330 if (id == pnode->id) {
2331 pnode->fDisconnect = true;
2332 return true;
2335 return false;
2338 void CConnman::RelayTransaction(const CTransaction& tx)
2340 CInv inv(MSG_TX, tx.GetHash());
2341 LOCK(cs_vNodes);
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)
2393 return 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
2409 // the timeframe
2410 nMaxOutboundCycleStartTime = GetTime();
2412 nMaxOutboundTimeframe = timeframe;
2415 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2417 LOCK(cs_totalBytesSent);
2418 if (nMaxOutboundLimit == 0)
2419 return false;
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)
2427 return true;
2429 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2430 return true;
2432 return false;
2435 uint64_t CConnman::GetOutboundTargetBytesLeft()
2437 LOCK(cs_totalBytesSent);
2438 if (nMaxOutboundLimit == 0)
2439 return 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) :
2475 addr(addrIn),
2476 fInbound(fInboundIn),
2477 id(idIn),
2478 nKeyedNetGroup(nKeyedNetGroupIn),
2479 addrKnown(5000, 0.001),
2480 filterInventoryKnown(50000, 0.000001),
2481 nLocalHostNonce(nLocalHostNonceIn),
2482 nLocalServices(nLocalServicesIn),
2483 nMyStartingHeight(nMyStartingHeightIn),
2484 nSendVersion(0)
2486 nServices = NODE_NONE;
2487 nServicesExpected = NODE_NONE;
2488 hSocket = hSocketIn;
2489 nRecvVersion = INIT_PROTO_VERSION;
2490 nLastSend = 0;
2491 nLastRecv = 0;
2492 nSendBytes = 0;
2493 nRecvBytes = 0;
2494 nTimeConnected = GetTime();
2495 nTimeOffset = 0;
2496 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2497 nVersion = 0;
2498 strSubVer = "";
2499 fWhitelisted = false;
2500 fOneShot = false;
2501 fClient = false; // set by version message
2502 fFeeler = false;
2503 fNetworkNode = false;
2504 fSuccessfullyConnected = false;
2505 fDisconnect = false;
2506 nRefCount = 0;
2507 nSendSize = 0;
2508 nSendOffset = 0;
2509 hashContinue = uint256();
2510 nStartingHeight = -1;
2511 filterInventoryKnown.reset();
2512 fSendMempool = false;
2513 fGetAddr = false;
2514 nNextLocalAddrSend = 0;
2515 nNextAddrSend = 0;
2516 nNextInvSend = 0;
2517 fRelayTxes = false;
2518 fSentAddr = false;
2519 pfilter = new CBloomFilter();
2520 timeLastMempoolReq = 0;
2521 nLastBlockTime = 0;
2522 nLastTXTime = 0;
2523 nPingNonceSent = 0;
2524 nPingUsecStart = 0;
2525 nPingUsecTime = 0;
2526 fPingQueued = false;
2527 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2528 minFeeFilter = 0;
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;
2536 if (fLogIPs)
2537 LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
2538 else
2539 LogPrint("net", "Added connection peer=%d\n", id);
2542 CNode::~CNode()
2544 CloseSocket(hSocket);
2546 if (pfilter)
2547 delete pfilter;
2550 void CNode::AskFor(const CInv& inv)
2552 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2553 return;
2554 // a peer may not have multiple non-responded queue positions for a single inv item
2555 if (!setAskFor.insert(inv.hash).second)
2556 return;
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;
2564 else
2565 nRequestTime = 0;
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;
2571 ++nLastTime;
2572 nNow = std::max(nNow, nLastTime);
2573 nLastTime = nNow;
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);
2579 else
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)
2591 // Set the size
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);
2595 // Set the checksum
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)
2603 if(strm.empty())
2604 return;
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) {
2613 return;
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);
2626 if (nBytesSent)
2627 RecordBytesSent(nBytesSent);
2630 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2632 CNode* found = nullptr;
2633 LOCK(cs_vNodes);
2634 for (auto&& pnode : vNodes) {
2635 if(pnode->id == id) {
2636 found = pnode;
2637 break;
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();