net: Pass best block known height into CConnman
[bitcoinplatinum.git] / src / net.cpp
blobd510952551a33e315c7985792301db8b1fbb67b3
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
65 namespace {
66 const int MAX_FEELER_CONNECTIONS = 1;
69 const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
72 // Global state variables
74 bool fDiscover = true;
75 bool fListen = true;
76 bool fRelayTxes = true;
77 CCriticalSection cs_mapLocalHost;
78 std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
79 static bool vfLimited[NET_MAX] = {};
80 static CNode* pnodeLocalHost = NULL;
81 std::string strSubVersion;
83 limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
85 // Signals for message handling
86 static CNodeSignals g_signals;
87 CNodeSignals& GetNodeSignals() { return g_signals; }
89 void CConnman::AddOneShot(const std::string& strDest)
91 LOCK(cs_vOneShots);
92 vOneShots.push_back(strDest);
95 unsigned short GetListenPort()
97 return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
100 // find 'best' local address for a particular peer
101 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
103 if (!fListen)
104 return false;
106 int nBestScore = -1;
107 int nBestReachability = -1;
109 LOCK(cs_mapLocalHost);
110 for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
112 int nScore = (*it).second.nScore;
113 int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
114 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
116 addr = CService((*it).first, (*it).second.nPort);
117 nBestReachability = nReachability;
118 nBestScore = nScore;
122 return nBestScore >= 0;
125 //! Convert the pnSeeds6 array into usable address objects.
126 static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
128 // It'll only connect to one or two seed nodes because once it connects,
129 // it'll get a pile of addresses with newer timestamps.
130 // Seed nodes are given a random 'last seen time' of between one and two
131 // weeks ago.
132 const int64_t nOneWeek = 7*24*60*60;
133 std::vector<CAddress> vSeedsOut;
134 vSeedsOut.reserve(vSeedsIn.size());
135 for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i)
137 struct in6_addr ip;
138 memcpy(&ip, i->addr, sizeof(ip));
139 CAddress addr(CService(ip, i->port), NODE_NETWORK);
140 addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
141 vSeedsOut.push_back(addr);
143 return vSeedsOut;
146 // get best local address for a particular peer as a CAddress
147 // Otherwise, return the unroutable 0.0.0.0 but filled in with
148 // the normal parameters, since the IP may be changed to a useful
149 // one by discovery.
150 CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
152 CAddress ret(CService(CNetAddr(),GetListenPort()), NODE_NONE);
153 CService addr;
154 if (GetLocal(addr, paddrPeer))
156 ret = CAddress(addr, nLocalServices);
158 ret.nTime = GetAdjustedTime();
159 return ret;
162 int GetnScore(const CService& addr)
164 LOCK(cs_mapLocalHost);
165 if (mapLocalHost.count(addr) == LOCAL_NONE)
166 return 0;
167 return mapLocalHost[addr].nScore;
170 // Is our peer's addrLocal potentially useful as an external IP source?
171 bool IsPeerAddrLocalGood(CNode *pnode)
173 return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() &&
174 !IsLimited(pnode->addrLocal.GetNetwork());
177 // pushes our own address to a peer
178 void AdvertiseLocal(CNode *pnode)
180 if (fListen && pnode->fSuccessfullyConnected)
182 CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
183 // If discovery is enabled, sometimes give our peer the address it
184 // tells us that it sees us as in case it has a better idea of our
185 // address than we do.
186 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
187 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
189 addrLocal.SetIP(pnode->addrLocal);
191 if (addrLocal.IsRoutable())
193 LogPrint("net", "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
194 pnode->PushAddress(addrLocal);
199 // learn a new local address
200 bool AddLocal(const CService& addr, int nScore)
202 if (!addr.IsRoutable())
203 return false;
205 if (!fDiscover && nScore < LOCAL_MANUAL)
206 return false;
208 if (IsLimited(addr))
209 return false;
211 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
214 LOCK(cs_mapLocalHost);
215 bool fAlready = mapLocalHost.count(addr) > 0;
216 LocalServiceInfo &info = mapLocalHost[addr];
217 if (!fAlready || nScore >= info.nScore) {
218 info.nScore = nScore + (fAlready ? 1 : 0);
219 info.nPort = addr.GetPort();
223 return true;
226 bool AddLocal(const CNetAddr &addr, int nScore)
228 return AddLocal(CService(addr, GetListenPort()), nScore);
231 bool RemoveLocal(const CService& addr)
233 LOCK(cs_mapLocalHost);
234 LogPrintf("RemoveLocal(%s)\n", addr.ToString());
235 mapLocalHost.erase(addr);
236 return true;
239 /** Make a particular network entirely off-limits (no automatic connects to it) */
240 void SetLimited(enum Network net, bool fLimited)
242 if (net == NET_UNROUTABLE)
243 return;
244 LOCK(cs_mapLocalHost);
245 vfLimited[net] = fLimited;
248 bool IsLimited(enum Network net)
250 LOCK(cs_mapLocalHost);
251 return vfLimited[net];
254 bool IsLimited(const CNetAddr &addr)
256 return IsLimited(addr.GetNetwork());
259 /** vote for a local address */
260 bool SeenLocal(const CService& addr)
263 LOCK(cs_mapLocalHost);
264 if (mapLocalHost.count(addr) == 0)
265 return false;
266 mapLocalHost[addr].nScore++;
268 return true;
272 /** check whether a given address is potentially local */
273 bool IsLocal(const CService& addr)
275 LOCK(cs_mapLocalHost);
276 return mapLocalHost.count(addr) > 0;
279 /** check whether a given network is one we can probably connect to */
280 bool IsReachable(enum Network net)
282 LOCK(cs_mapLocalHost);
283 return !vfLimited[net];
286 /** check whether a given address is in a network we can probably connect to */
287 bool IsReachable(const CNetAddr& addr)
289 enum Network net = addr.GetNetwork();
290 return IsReachable(net);
294 CNode* CConnman::FindNode(const CNetAddr& ip)
296 LOCK(cs_vNodes);
297 BOOST_FOREACH(CNode* pnode, vNodes)
298 if ((CNetAddr)pnode->addr == ip)
299 return (pnode);
300 return NULL;
303 CNode* CConnman::FindNode(const CSubNet& subNet)
305 LOCK(cs_vNodes);
306 BOOST_FOREACH(CNode* pnode, vNodes)
307 if (subNet.Match((CNetAddr)pnode->addr))
308 return (pnode);
309 return NULL;
312 CNode* CConnman::FindNode(const std::string& addrName)
314 LOCK(cs_vNodes);
315 BOOST_FOREACH(CNode* pnode, vNodes)
316 if (pnode->addrName == addrName)
317 return (pnode);
318 return NULL;
321 CNode* CConnman::FindNode(const CService& addr)
323 LOCK(cs_vNodes);
324 BOOST_FOREACH(CNode* pnode, vNodes)
325 if ((CService)pnode->addr == addr)
326 return (pnode);
327 return NULL;
330 bool CConnman::CheckIncomingNonce(uint64_t nonce)
332 LOCK(cs_vNodes);
333 BOOST_FOREACH(CNode* pnode, vNodes) {
334 if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
335 return false;
337 return true;
340 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
342 if (pszDest == NULL) {
343 if (IsLocal(addrConnect))
344 return NULL;
346 // Look for an existing connection
347 CNode* pnode = FindNode((CService)addrConnect);
348 if (pnode)
350 pnode->AddRef();
351 return pnode;
355 /// debug print
356 LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
357 pszDest ? pszDest : addrConnect.ToString(),
358 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
360 // Connect
361 SOCKET hSocket;
362 bool proxyConnectionFailed = false;
363 if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
364 ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
366 if (!IsSelectableSocket(hSocket)) {
367 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
368 CloseSocket(hSocket);
369 return NULL;
372 if (pszDest && addrConnect.IsValid()) {
373 // It is possible that we already have a connection to the IP/port pszDest resolved to.
374 // In that case, drop the connection that was just created, and return the existing CNode instead.
375 // Also store the name we used to connect in that CNode, so that future FindNode() calls to that
376 // name catch this early.
377 CNode* pnode = FindNode((CService)addrConnect);
378 if (pnode)
380 pnode->AddRef();
382 LOCK(cs_vNodes);
383 if (pnode->addrName.empty()) {
384 pnode->addrName = std::string(pszDest);
387 CloseSocket(hSocket);
388 return pnode;
392 addrman.Attempt(addrConnect, fCountFailure);
394 // Add node
395 CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addrConnect, pszDest ? pszDest : "", false);
396 GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
397 pnode->AddRef();
400 LOCK(cs_vNodes);
401 vNodes.push_back(pnode);
404 pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
405 pnode->nTimeConnected = GetTime();
407 return pnode;
408 } else if (!proxyConnectionFailed) {
409 // If connecting to the node failed, and failure is not caused by a problem connecting to
410 // the proxy, mark this as an attempt.
411 addrman.Attempt(addrConnect, fCountFailure);
414 return NULL;
417 void CConnman::DumpBanlist()
419 SweepBanned(); // clean unused entries (if bantime has expired)
421 if (!BannedSetIsDirty())
422 return;
424 int64_t nStart = GetTimeMillis();
426 CBanDB bandb;
427 banmap_t banmap;
428 SetBannedSetDirty(false);
429 GetBanned(banmap);
430 if (!bandb.Write(banmap))
431 SetBannedSetDirty(true);
433 LogPrint("net", "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
434 banmap.size(), GetTimeMillis() - nStart);
437 void CNode::CloseSocketDisconnect()
439 fDisconnect = true;
440 if (hSocket != INVALID_SOCKET)
442 LogPrint("net", "disconnecting peer=%d\n", id);
443 CloseSocket(hSocket);
446 // in case this fails, we'll empty the recv buffer when the CNode is deleted
447 TRY_LOCK(cs_vRecvMsg, lockRecv);
448 if (lockRecv)
449 vRecvMsg.clear();
452 void CNode::PushVersion()
454 int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
455 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
456 CAddress addrMe = GetLocalAddress(&addr, nLocalServices);
457 if (fLogIPs)
458 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nMyStartingHeight, addrMe.ToString(), addrYou.ToString(), id);
459 else
460 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nMyStartingHeight, addrMe.ToString(), id);
461 PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalServices, nTime, addrYou, addrMe,
462 nLocalHostNonce, strSubVersion, nMyStartingHeight, ::fRelayTxes);
469 void CConnman::ClearBanned()
472 LOCK(cs_setBanned);
473 setBanned.clear();
474 setBannedIsDirty = true;
476 DumpBanlist(); //store banlist to disk
477 uiInterface.BannedListChanged();
480 bool CConnman::IsBanned(CNetAddr ip)
482 bool fResult = false;
484 LOCK(cs_setBanned);
485 for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++)
487 CSubNet subNet = (*it).first;
488 CBanEntry banEntry = (*it).second;
490 if(subNet.Match(ip) && GetTime() < banEntry.nBanUntil)
491 fResult = true;
494 return fResult;
497 bool CConnman::IsBanned(CSubNet subnet)
499 bool fResult = false;
501 LOCK(cs_setBanned);
502 banmap_t::iterator i = setBanned.find(subnet);
503 if (i != setBanned.end())
505 CBanEntry banEntry = (*i).second;
506 if (GetTime() < banEntry.nBanUntil)
507 fResult = true;
510 return fResult;
513 void CConnman::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
514 CSubNet subNet(addr);
515 Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
518 void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
519 CBanEntry banEntry(GetTime());
520 banEntry.banReason = banReason;
521 if (bantimeoffset <= 0)
523 bantimeoffset = GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
524 sinceUnixEpoch = false;
526 banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
529 LOCK(cs_setBanned);
530 if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
531 setBanned[subNet] = banEntry;
532 setBannedIsDirty = true;
534 else
535 return;
537 uiInterface.BannedListChanged();
539 LOCK(cs_vNodes);
540 BOOST_FOREACH(CNode* pnode, vNodes) {
541 if (subNet.Match((CNetAddr)pnode->addr))
542 pnode->fDisconnect = true;
545 if(banReason == BanReasonManuallyAdded)
546 DumpBanlist(); //store banlist to disk immediately if user requested ban
549 bool CConnman::Unban(const CNetAddr &addr) {
550 CSubNet subNet(addr);
551 return Unban(subNet);
554 bool CConnman::Unban(const CSubNet &subNet) {
556 LOCK(cs_setBanned);
557 if (!setBanned.erase(subNet))
558 return false;
559 setBannedIsDirty = true;
561 uiInterface.BannedListChanged();
562 DumpBanlist(); //store banlist to disk immediately
563 return true;
566 void CConnman::GetBanned(banmap_t &banMap)
568 LOCK(cs_setBanned);
569 banMap = setBanned; //create a thread safe copy
572 void CConnman::SetBanned(const banmap_t &banMap)
574 LOCK(cs_setBanned);
575 setBanned = banMap;
576 setBannedIsDirty = true;
579 void CConnman::SweepBanned()
581 int64_t now = GetTime();
583 LOCK(cs_setBanned);
584 banmap_t::iterator it = setBanned.begin();
585 while(it != setBanned.end())
587 CSubNet subNet = (*it).first;
588 CBanEntry banEntry = (*it).second;
589 if(now > banEntry.nBanUntil)
591 setBanned.erase(it++);
592 setBannedIsDirty = true;
593 LogPrint("net", "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
595 else
596 ++it;
600 bool CConnman::BannedSetIsDirty()
602 LOCK(cs_setBanned);
603 return setBannedIsDirty;
606 void CConnman::SetBannedSetDirty(bool dirty)
608 LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
609 setBannedIsDirty = dirty;
613 bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
614 LOCK(cs_vWhitelistedRange);
615 BOOST_FOREACH(const CSubNet& subnet, vWhitelistedRange) {
616 if (subnet.Match(addr))
617 return true;
619 return false;
622 void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
623 LOCK(cs_vWhitelistedRange);
624 vWhitelistedRange.push_back(subnet);
627 #undef X
628 #define X(name) stats.name = name
629 void CNode::copyStats(CNodeStats &stats)
631 stats.nodeid = this->GetId();
632 X(nServices);
633 X(fRelayTxes);
634 X(nLastSend);
635 X(nLastRecv);
636 X(nTimeConnected);
637 X(nTimeOffset);
638 X(addrName);
639 X(nVersion);
640 X(cleanSubVer);
641 X(fInbound);
642 X(nStartingHeight);
643 X(nSendBytes);
644 X(mapSendBytesPerMsgCmd);
645 X(nRecvBytes);
646 X(mapRecvBytesPerMsgCmd);
647 X(fWhitelisted);
649 // It is common for nodes with good ping times to suddenly become lagged,
650 // due to a new block arriving or other large transfer.
651 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
652 // since pingtime does not update until the ping is complete, which might take a while.
653 // So, if a ping is taking an unusually long time in flight,
654 // the caller can immediately detect that this is happening.
655 int64_t nPingUsecWait = 0;
656 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
657 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
660 // 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 :)
661 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
662 stats.dPingMin = (((double)nMinPingUsecTime) / 1e6);
663 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
665 // Leave string empty if addrLocal invalid (not filled in yet)
666 stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
668 #undef X
670 // requires LOCK(cs_vRecvMsg)
671 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete)
673 complete = false;
674 while (nBytes > 0) {
676 // get current incomplete message, or create a new one
677 if (vRecvMsg.empty() ||
678 vRecvMsg.back().complete())
679 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, nRecvVersion));
681 CNetMessage& msg = vRecvMsg.back();
683 // absorb network data
684 int handled;
685 if (!msg.in_data)
686 handled = msg.readHeader(pch, nBytes);
687 else
688 handled = msg.readData(pch, nBytes);
690 if (handled < 0)
691 return false;
693 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
694 LogPrint("net", "Oversized message from peer=%i, disconnecting\n", GetId());
695 return false;
698 pch += handled;
699 nBytes -= handled;
701 if (msg.complete()) {
703 //store received bytes per message command
704 //to prevent a memory DOS, only allow valid commands
705 mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
706 if (i == mapRecvBytesPerMsgCmd.end())
707 i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
708 assert(i != mapRecvBytesPerMsgCmd.end());
709 i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
711 msg.nTime = GetTimeMicros();
712 complete = true;
716 return true;
719 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
721 // copy data to temporary parsing buffer
722 unsigned int nRemaining = 24 - nHdrPos;
723 unsigned int nCopy = std::min(nRemaining, nBytes);
725 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
726 nHdrPos += nCopy;
728 // if header incomplete, exit
729 if (nHdrPos < 24)
730 return nCopy;
732 // deserialize to CMessageHeader
733 try {
734 hdrbuf >> hdr;
736 catch (const std::exception&) {
737 return -1;
740 // reject messages larger than MAX_SIZE
741 if (hdr.nMessageSize > MAX_SIZE)
742 return -1;
744 // switch state to reading message data
745 in_data = true;
747 return nCopy;
750 int CNetMessage::readData(const char *pch, unsigned int nBytes)
752 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
753 unsigned int nCopy = std::min(nRemaining, nBytes);
755 if (vRecv.size() < nDataPos + nCopy) {
756 // Allocate up to 256 KiB ahead, but never more than the total message size.
757 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
760 memcpy(&vRecv[nDataPos], pch, nCopy);
761 nDataPos += nCopy;
763 return nCopy;
774 // requires LOCK(cs_vSend)
775 size_t SocketSendData(CNode *pnode)
777 std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
778 size_t nSentSize = 0;
780 while (it != pnode->vSendMsg.end()) {
781 const CSerializeData &data = *it;
782 assert(data.size() > pnode->nSendOffset);
783 int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
784 if (nBytes > 0) {
785 pnode->nLastSend = GetTime();
786 pnode->nSendBytes += nBytes;
787 pnode->nSendOffset += nBytes;
788 nSentSize += nBytes;
789 if (pnode->nSendOffset == data.size()) {
790 pnode->nSendOffset = 0;
791 pnode->nSendSize -= data.size();
792 it++;
793 } else {
794 // could not send full message; stop sending more
795 break;
797 } else {
798 if (nBytes < 0) {
799 // error
800 int nErr = WSAGetLastError();
801 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
803 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
804 pnode->CloseSocketDisconnect();
807 // couldn't send anything at all
808 break;
812 if (it == pnode->vSendMsg.end()) {
813 assert(pnode->nSendOffset == 0);
814 assert(pnode->nSendSize == 0);
816 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
817 return nSentSize;
820 static std::list<CNode*> vNodesDisconnected;
822 struct NodeEvictionCandidate
824 NodeId id;
825 int64_t nTimeConnected;
826 int64_t nMinPingUsecTime;
827 int64_t nLastBlockTime;
828 int64_t nLastTXTime;
829 bool fNetworkNode;
830 bool fRelayTxes;
831 bool fBloomFilter;
832 CAddress addr;
833 uint64_t nKeyedNetGroup;
836 static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
838 return a.nMinPingUsecTime > b.nMinPingUsecTime;
841 static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
843 return a.nTimeConnected > b.nTimeConnected;
846 static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
847 return a.nKeyedNetGroup < b.nKeyedNetGroup;
850 static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
852 // There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
853 if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
854 if (a.fNetworkNode != b.fNetworkNode) return b.fNetworkNode;
855 return a.nTimeConnected > b.nTimeConnected;
858 static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
860 // 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.
861 if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
862 if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
863 if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
864 return a.nTimeConnected > b.nTimeConnected;
867 /** Try to find a connection to evict when the node is full.
868 * Extreme care must be taken to avoid opening the node to attacker
869 * triggered network partitioning.
870 * The strategy used here is to protect a small number of peers
871 * for each of several distinct characteristics which are difficult
872 * to forge. In order to partition a node the attacker must be
873 * simultaneously better at all of them than honest peers.
875 bool CConnman::AttemptToEvictConnection()
877 std::vector<NodeEvictionCandidate> vEvictionCandidates;
879 LOCK(cs_vNodes);
881 BOOST_FOREACH(CNode *node, vNodes) {
882 if (node->fWhitelisted)
883 continue;
884 if (!node->fInbound)
885 continue;
886 if (node->fDisconnect)
887 continue;
888 NodeEvictionCandidate candidate = {node->id, node->nTimeConnected, node->nMinPingUsecTime,
889 node->nLastBlockTime, node->nLastTXTime, node->fNetworkNode,
890 node->fRelayTxes, node->pfilter != NULL, node->addr, node->nKeyedNetGroup};
891 vEvictionCandidates.push_back(candidate);
895 if (vEvictionCandidates.empty()) return false;
897 // Protect connections with certain characteristics
899 // Deterministically select 4 peers to protect by netgroup.
900 // An attacker cannot predict which netgroups will be protected
901 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNetGroupKeyed);
902 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
904 if (vEvictionCandidates.empty()) return false;
906 // Protect the 8 nodes with the lowest minimum ping time.
907 // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
908 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);
909 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
911 if (vEvictionCandidates.empty()) return false;
913 // Protect 4 nodes that most recently sent us transactions.
914 // An attacker cannot manipulate this metric without performing useful work.
915 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeTXTime);
916 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
918 if (vEvictionCandidates.empty()) return false;
920 // Protect 4 nodes that most recently sent us blocks.
921 // An attacker cannot manipulate this metric without performing useful work.
922 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), CompareNodeBlockTime);
923 vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
925 if (vEvictionCandidates.empty()) return false;
927 // Protect the half of the remaining nodes which have been connected the longest.
928 // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
929 std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
930 vEvictionCandidates.erase(vEvictionCandidates.end() - static_cast<int>(vEvictionCandidates.size() / 2), vEvictionCandidates.end());
932 if (vEvictionCandidates.empty()) return false;
934 // Identify the network group with the most connections and youngest member.
935 // (vEvictionCandidates is already sorted by reverse connect time)
936 uint64_t naMostConnections;
937 unsigned int nMostConnections = 0;
938 int64_t nMostConnectionsTime = 0;
939 std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
940 BOOST_FOREACH(const NodeEvictionCandidate &node, vEvictionCandidates) {
941 mapNetGroupNodes[node.nKeyedNetGroup].push_back(node);
942 int64_t grouptime = mapNetGroupNodes[node.nKeyedNetGroup][0].nTimeConnected;
943 size_t groupsize = mapNetGroupNodes[node.nKeyedNetGroup].size();
945 if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
946 nMostConnections = groupsize;
947 nMostConnectionsTime = grouptime;
948 naMostConnections = node.nKeyedNetGroup;
952 // Reduce to the network group with the most connections
953 vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
955 // Disconnect from the network group with the most connections
956 NodeId evicted = vEvictionCandidates.front().id;
957 LOCK(cs_vNodes);
958 for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
959 if ((*it)->GetId() == evicted) {
960 (*it)->fDisconnect = true;
961 return true;
964 return false;
967 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
968 struct sockaddr_storage sockaddr;
969 socklen_t len = sizeof(sockaddr);
970 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
971 CAddress addr;
972 int nInbound = 0;
973 int nMaxInbound = nMaxConnections - (nMaxOutbound + MAX_FEELER_CONNECTIONS);
974 assert(nMaxInbound > 0);
976 if (hSocket != INVALID_SOCKET)
977 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
978 LogPrintf("Warning: Unknown socket family\n");
980 bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
982 LOCK(cs_vNodes);
983 BOOST_FOREACH(CNode* pnode, vNodes)
984 if (pnode->fInbound)
985 nInbound++;
988 if (hSocket == INVALID_SOCKET)
990 int nErr = WSAGetLastError();
991 if (nErr != WSAEWOULDBLOCK)
992 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
993 return;
996 if (!IsSelectableSocket(hSocket))
998 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
999 CloseSocket(hSocket);
1000 return;
1003 // According to the internet TCP_NODELAY is not carried into accepted sockets
1004 // on all platforms. Set it again here just to be sure.
1005 int set = 1;
1006 #ifdef WIN32
1007 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
1008 #else
1009 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
1010 #endif
1012 if (IsBanned(addr) && !whitelisted)
1014 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
1015 CloseSocket(hSocket);
1016 return;
1019 if (nInbound >= nMaxInbound)
1021 if (!AttemptToEvictConnection()) {
1022 // No connection to evict, disconnect the new connection
1023 LogPrint("net", "failed to find an eviction candidate - connection dropped (full)\n");
1024 CloseSocket(hSocket);
1025 return;
1029 CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addr, "", true);
1030 GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
1031 pnode->AddRef();
1032 pnode->fWhitelisted = whitelisted;
1034 LogPrint("net", "connection from %s accepted\n", addr.ToString());
1037 LOCK(cs_vNodes);
1038 vNodes.push_back(pnode);
1042 void CConnman::ThreadSocketHandler()
1044 unsigned int nPrevNodeCount = 0;
1045 while (true)
1048 // Disconnect nodes
1051 LOCK(cs_vNodes);
1052 // Disconnect unused nodes
1053 std::vector<CNode*> vNodesCopy = vNodes;
1054 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1056 if (pnode->fDisconnect ||
1057 (pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
1059 // remove from vNodes
1060 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
1062 // release outbound grant (if any)
1063 pnode->grantOutbound.Release();
1065 // close socket and cleanup
1066 pnode->CloseSocketDisconnect();
1068 // hold in disconnected pool until all refs are released
1069 if (pnode->fNetworkNode || pnode->fInbound)
1070 pnode->Release();
1071 vNodesDisconnected.push_back(pnode);
1076 // Delete disconnected nodes
1077 std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
1078 BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
1080 // wait until threads are done using it
1081 if (pnode->GetRefCount() <= 0)
1083 bool fDelete = false;
1085 TRY_LOCK(pnode->cs_vSend, lockSend);
1086 if (lockSend)
1088 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1089 if (lockRecv)
1091 TRY_LOCK(pnode->cs_inventory, lockInv);
1092 if (lockInv)
1093 fDelete = true;
1097 if (fDelete)
1099 vNodesDisconnected.remove(pnode);
1100 DeleteNode(pnode);
1105 if(vNodes.size() != nPrevNodeCount) {
1106 nPrevNodeCount = vNodes.size();
1107 uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
1111 // Find which sockets have data to receive
1113 struct timeval timeout;
1114 timeout.tv_sec = 0;
1115 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
1117 fd_set fdsetRecv;
1118 fd_set fdsetSend;
1119 fd_set fdsetError;
1120 FD_ZERO(&fdsetRecv);
1121 FD_ZERO(&fdsetSend);
1122 FD_ZERO(&fdsetError);
1123 SOCKET hSocketMax = 0;
1124 bool have_fds = false;
1126 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket) {
1127 FD_SET(hListenSocket.socket, &fdsetRecv);
1128 hSocketMax = std::max(hSocketMax, hListenSocket.socket);
1129 have_fds = true;
1133 LOCK(cs_vNodes);
1134 BOOST_FOREACH(CNode* pnode, vNodes)
1136 if (pnode->hSocket == INVALID_SOCKET)
1137 continue;
1138 FD_SET(pnode->hSocket, &fdsetError);
1139 hSocketMax = std::max(hSocketMax, pnode->hSocket);
1140 have_fds = true;
1142 // Implement the following logic:
1143 // * If there is data to send, select() for sending data. As this only
1144 // happens when optimistic write failed, we choose to first drain the
1145 // write buffer in this case before receiving more. This avoids
1146 // needlessly queueing received data, if the remote peer is not themselves
1147 // receiving data. This means properly utilizing TCP flow control signalling.
1148 // * Otherwise, if there is no (complete) message in the receive buffer,
1149 // or there is space left in the buffer, select() for receiving data.
1150 // * (if neither of the above applies, there is certainly one message
1151 // in the receiver buffer ready to be processed).
1152 // Together, that means that at least one of the following is always possible,
1153 // so we don't deadlock:
1154 // * We send some data.
1155 // * We wait for data to be received (and disconnect after timeout).
1156 // * We process a message in the buffer (message handler thread).
1158 TRY_LOCK(pnode->cs_vSend, lockSend);
1159 if (lockSend) {
1160 if (pnode->nOptimisticBytesWritten) {
1161 RecordBytesSent(pnode->nOptimisticBytesWritten);
1162 pnode->nOptimisticBytesWritten = 0;
1164 if (!pnode->vSendMsg.empty()) {
1165 FD_SET(pnode->hSocket, &fdsetSend);
1166 continue;
1171 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1172 if (lockRecv && (
1173 pnode->vRecvMsg.empty() || !pnode->vRecvMsg.front().complete() ||
1174 pnode->GetTotalRecvSize() <= GetReceiveFloodSize()))
1175 FD_SET(pnode->hSocket, &fdsetRecv);
1180 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
1181 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1182 boost::this_thread::interruption_point();
1184 if (nSelect == SOCKET_ERROR)
1186 if (have_fds)
1188 int nErr = WSAGetLastError();
1189 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1190 for (unsigned int i = 0; i <= hSocketMax; i++)
1191 FD_SET(i, &fdsetRecv);
1193 FD_ZERO(&fdsetSend);
1194 FD_ZERO(&fdsetError);
1195 MilliSleep(timeout.tv_usec/1000);
1199 // Accept new connections
1201 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket)
1203 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
1205 AcceptConnection(hListenSocket);
1210 // Service each socket
1212 std::vector<CNode*> vNodesCopy;
1214 LOCK(cs_vNodes);
1215 vNodesCopy = vNodes;
1216 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1217 pnode->AddRef();
1219 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1221 boost::this_thread::interruption_point();
1224 // Receive
1226 if (pnode->hSocket == INVALID_SOCKET)
1227 continue;
1228 if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
1230 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1231 if (lockRecv)
1234 // typical socket buffer is 8K-64K
1235 char pchBuf[0x10000];
1236 int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1237 if (nBytes > 0)
1239 bool notify = false;
1240 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1241 pnode->CloseSocketDisconnect();
1242 if(notify)
1243 messageHandlerCondition.notify_one();
1244 pnode->nLastRecv = GetTime();
1245 pnode->nRecvBytes += nBytes;
1246 RecordBytesRecv(nBytes);
1248 else if (nBytes == 0)
1250 // socket closed gracefully
1251 if (!pnode->fDisconnect)
1252 LogPrint("net", "socket closed\n");
1253 pnode->CloseSocketDisconnect();
1255 else if (nBytes < 0)
1257 // error
1258 int nErr = WSAGetLastError();
1259 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1261 if (!pnode->fDisconnect)
1262 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1263 pnode->CloseSocketDisconnect();
1271 // Send
1273 if (pnode->hSocket == INVALID_SOCKET)
1274 continue;
1275 if (FD_ISSET(pnode->hSocket, &fdsetSend))
1277 TRY_LOCK(pnode->cs_vSend, lockSend);
1278 if (lockSend) {
1279 size_t nBytes = SocketSendData(pnode);
1280 if (nBytes)
1281 RecordBytesSent(nBytes);
1286 // Inactivity checking
1288 int64_t nTime = GetTime();
1289 if (nTime - pnode->nTimeConnected > 60)
1291 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1293 LogPrint("net", "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->id);
1294 pnode->fDisconnect = true;
1296 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
1298 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
1299 pnode->fDisconnect = true;
1301 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
1303 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
1304 pnode->fDisconnect = true;
1306 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
1308 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1309 pnode->fDisconnect = true;
1314 LOCK(cs_vNodes);
1315 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1316 pnode->Release();
1329 #ifdef USE_UPNP
1330 void ThreadMapPort()
1332 std::string port = strprintf("%u", GetListenPort());
1333 const char * multicastif = 0;
1334 const char * minissdpdpath = 0;
1335 struct UPNPDev * devlist = 0;
1336 char lanaddr[64];
1338 #ifndef UPNPDISCOVER_SUCCESS
1339 /* miniupnpc 1.5 */
1340 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1341 #elif MINIUPNPC_API_VERSION < 14
1342 /* miniupnpc 1.6 */
1343 int error = 0;
1344 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1345 #else
1346 /* miniupnpc 1.9.20150730 */
1347 int error = 0;
1348 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1349 #endif
1351 struct UPNPUrls urls;
1352 struct IGDdatas data;
1353 int r;
1355 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1356 if (r == 1)
1358 if (fDiscover) {
1359 char externalIPAddress[40];
1360 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1361 if(r != UPNPCOMMAND_SUCCESS)
1362 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1363 else
1365 if(externalIPAddress[0])
1367 CNetAddr resolved;
1368 if(LookupHost(externalIPAddress, resolved, false)) {
1369 LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1370 AddLocal(resolved, LOCAL_UPNP);
1373 else
1374 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1378 std::string strDesc = "Bitcoin " + FormatFullVersion();
1380 try {
1381 while (true) {
1382 #ifndef UPNPDISCOVER_SUCCESS
1383 /* miniupnpc 1.5 */
1384 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1385 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1386 #else
1387 /* miniupnpc 1.6 */
1388 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1389 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1390 #endif
1392 if(r!=UPNPCOMMAND_SUCCESS)
1393 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1394 port, port, lanaddr, r, strupnperror(r));
1395 else
1396 LogPrintf("UPnP Port Mapping successful.\n");
1398 MilliSleep(20*60*1000); // Refresh every 20 minutes
1401 catch (const boost::thread_interrupted&)
1403 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1404 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1405 freeUPNPDevlist(devlist); devlist = 0;
1406 FreeUPNPUrls(&urls);
1407 throw;
1409 } else {
1410 LogPrintf("No valid UPnP IGDs found\n");
1411 freeUPNPDevlist(devlist); devlist = 0;
1412 if (r != 0)
1413 FreeUPNPUrls(&urls);
1417 void MapPort(bool fUseUPnP)
1419 static boost::thread* upnp_thread = NULL;
1421 if (fUseUPnP)
1423 if (upnp_thread) {
1424 upnp_thread->interrupt();
1425 upnp_thread->join();
1426 delete upnp_thread;
1428 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1430 else if (upnp_thread) {
1431 upnp_thread->interrupt();
1432 upnp_thread->join();
1433 delete upnp_thread;
1434 upnp_thread = NULL;
1438 #else
1439 void MapPort(bool)
1441 // Intentionally left blank.
1443 #endif
1450 static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
1452 //use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)
1453 if (!data.supportsServiceBitsFiltering || *requiredServiceBits == NODE_NETWORK) {
1454 *requiredServiceBits = NODE_NETWORK;
1455 return data.host;
1458 return strprintf("x%x.%s", *requiredServiceBits, data.host);
1462 void CConnman::ThreadDNSAddressSeed()
1464 // goal: only query DNS seeds if address need is acute
1465 if ((addrman.size() > 0) &&
1466 (!GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
1467 MilliSleep(11 * 1000);
1469 LOCK(cs_vNodes);
1470 if (vNodes.size() >= 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++;
1625 assert(nOutbound <= (nMaxOutbound + MAX_FEELER_CONNECTIONS));
1627 // Feeler Connections
1629 // Design goals:
1630 // * Increase the number of connectable addresses in the tried table.
1632 // Method:
1633 // * Choose a random address from new and attempt to connect to it if we can connect
1634 // successfully it is added to tried.
1635 // * Start attempting feeler connections only after node finishes making outbound
1636 // connections.
1637 // * Only make a feeler connection once every few minutes.
1639 bool fFeeler = false;
1640 if (nOutbound >= nMaxOutbound) {
1641 int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1642 if (nTime > nNextFeeler) {
1643 nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1644 fFeeler = true;
1645 } else {
1646 continue;
1650 int64_t nANow = GetAdjustedTime();
1651 int nTries = 0;
1652 while (true)
1654 CAddrInfo addr = addrman.Select(fFeeler);
1656 // if we selected an invalid address, restart
1657 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1658 break;
1660 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1661 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1662 // already-connected network ranges, ...) before trying new addrman addresses.
1663 nTries++;
1664 if (nTries > 100)
1665 break;
1667 if (IsLimited(addr))
1668 continue;
1670 // only connect to full nodes
1671 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1672 continue;
1674 // only consider very recently tried nodes after 30 failed attempts
1675 if (nANow - addr.nLastTry < 600 && nTries < 30)
1676 continue;
1678 // only consider nodes missing relevant services after 40 failed attempts
1679 if ((addr.nServices & nRelevantServices) != nRelevantServices && nTries < 40)
1680 continue;
1682 // do not allow non-default ports, unless after 50 invalid addresses selected already
1683 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1684 continue;
1686 addrConnect = addr;
1687 break;
1690 if (addrConnect.IsValid()) {
1692 if (fFeeler) {
1693 // Add small amount of random noise before connection to avoid synchronization.
1694 int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
1695 MilliSleep(randsleep);
1696 LogPrint("net", "Making feeler connection to %s\n", addrConnect.ToString());
1699 OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, NULL, false, fFeeler);
1704 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1706 std::vector<AddedNodeInfo> ret;
1708 std::list<std::string> lAddresses(0);
1710 LOCK(cs_vAddedNodes);
1711 ret.reserve(vAddedNodes.size());
1712 BOOST_FOREACH(const std::string& strAddNode, vAddedNodes)
1713 lAddresses.push_back(strAddNode);
1717 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
1718 std::map<CService, bool> mapConnected;
1719 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
1721 LOCK(cs_vNodes);
1722 for (const CNode* pnode : vNodes) {
1723 if (pnode->addr.IsValid()) {
1724 mapConnected[pnode->addr] = pnode->fInbound;
1726 if (!pnode->addrName.empty()) {
1727 mapConnectedByName[pnode->addrName] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1732 BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
1733 CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1734 if (service.IsValid()) {
1735 // strAddNode is an IP:port
1736 auto it = mapConnected.find(service);
1737 if (it != mapConnected.end()) {
1738 ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1739 } else {
1740 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1742 } else {
1743 // strAddNode is a name
1744 auto it = mapConnectedByName.find(strAddNode);
1745 if (it != mapConnectedByName.end()) {
1746 ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1747 } else {
1748 ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1753 return ret;
1756 void CConnman::ThreadOpenAddedConnections()
1759 LOCK(cs_vAddedNodes);
1760 vAddedNodes = mapMultiArgs["-addnode"];
1763 for (unsigned int i = 0; true; i++)
1765 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
1766 for (const AddedNodeInfo& info : vInfo) {
1767 if (!info.fConnected) {
1768 CSemaphoreGrant grant(*semOutbound);
1769 // If strAddedNode is an IP/port, decode it immediately, so
1770 // OpenNetworkConnection can detect existing connections to that IP/port.
1771 CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
1772 OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
1773 MilliSleep(500);
1777 MilliSleep(120000); // Retry every 2 minutes
1781 // if successful, this moves the passed grant to the constructed node
1782 bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler)
1785 // Initiate outbound network connection
1787 boost::this_thread::interruption_point();
1788 if (!pszDest) {
1789 if (IsLocal(addrConnect) ||
1790 FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1791 FindNode(addrConnect.ToStringIPPort()))
1792 return false;
1793 } else if (FindNode(std::string(pszDest)))
1794 return false;
1796 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1797 boost::this_thread::interruption_point();
1799 if (!pnode)
1800 return false;
1801 if (grantOutbound)
1802 grantOutbound->MoveTo(pnode->grantOutbound);
1803 pnode->fNetworkNode = true;
1804 if (fOneShot)
1805 pnode->fOneShot = true;
1806 if (fFeeler)
1807 pnode->fFeeler = true;
1809 return true;
1813 void CConnman::ThreadMessageHandler()
1815 boost::mutex condition_mutex;
1816 boost::unique_lock<boost::mutex> lock(condition_mutex);
1818 while (true)
1820 std::vector<CNode*> vNodesCopy;
1822 LOCK(cs_vNodes);
1823 vNodesCopy = vNodes;
1824 BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1825 pnode->AddRef();
1829 bool fSleep = true;
1831 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1833 if (pnode->fDisconnect)
1834 continue;
1836 // Receive messages
1838 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1839 if (lockRecv)
1841 if (!GetNodeSignals().ProcessMessages(pnode, *this))
1842 pnode->CloseSocketDisconnect();
1844 if (pnode->nSendSize < GetSendBufferSize())
1846 if (!pnode->vRecvGetData.empty() || (!pnode->vRecvMsg.empty() && pnode->vRecvMsg[0].complete()))
1848 fSleep = false;
1853 boost::this_thread::interruption_point();
1855 // Send messages
1857 TRY_LOCK(pnode->cs_vSend, lockSend);
1858 if (lockSend)
1859 GetNodeSignals().SendMessages(pnode, *this);
1861 boost::this_thread::interruption_point();
1865 LOCK(cs_vNodes);
1866 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1867 pnode->Release();
1870 if (fSleep)
1871 messageHandlerCondition.timed_wait(lock, boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(100));
1880 bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
1882 strError = "";
1883 int nOne = 1;
1885 // Create socket for listening for incoming connections
1886 struct sockaddr_storage sockaddr;
1887 socklen_t len = sizeof(sockaddr);
1888 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
1890 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
1891 LogPrintf("%s\n", strError);
1892 return false;
1895 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
1896 if (hListenSocket == INVALID_SOCKET)
1898 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
1899 LogPrintf("%s\n", strError);
1900 return false;
1902 if (!IsSelectableSocket(hListenSocket))
1904 strError = "Error: Couldn't create a listenable socket for incoming connections";
1905 LogPrintf("%s\n", strError);
1906 return false;
1910 #ifndef WIN32
1911 #ifdef SO_NOSIGPIPE
1912 // Different way of disabling SIGPIPE on BSD
1913 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
1914 #endif
1915 // Allow binding if the port is still in TIME_WAIT state after
1916 // the program was closed and restarted.
1917 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
1918 // Disable Nagle's algorithm
1919 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
1920 #else
1921 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
1922 setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
1923 #endif
1925 // Set to non-blocking, incoming connections will also inherit this
1926 if (!SetSocketNonBlocking(hListenSocket, true)) {
1927 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
1928 LogPrintf("%s\n", strError);
1929 return false;
1932 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
1933 // and enable it by default or not. Try to enable it, if possible.
1934 if (addrBind.IsIPv6()) {
1935 #ifdef IPV6_V6ONLY
1936 #ifdef WIN32
1937 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
1938 #else
1939 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
1940 #endif
1941 #endif
1942 #ifdef WIN32
1943 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
1944 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
1945 #endif
1948 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
1950 int nErr = WSAGetLastError();
1951 if (nErr == WSAEADDRINUSE)
1952 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
1953 else
1954 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
1955 LogPrintf("%s\n", strError);
1956 CloseSocket(hListenSocket);
1957 return false;
1959 LogPrintf("Bound to %s\n", addrBind.ToString());
1961 // Listen for incoming connections
1962 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
1964 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
1965 LogPrintf("%s\n", strError);
1966 CloseSocket(hListenSocket);
1967 return false;
1970 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
1972 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
1973 AddLocal(addrBind, LOCAL_BIND);
1975 return true;
1978 void static Discover(boost::thread_group& threadGroup)
1980 if (!fDiscover)
1981 return;
1983 #ifdef WIN32
1984 // Get local host IP
1985 char pszHostName[256] = "";
1986 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
1988 std::vector<CNetAddr> vaddr;
1989 if (LookupHost(pszHostName, vaddr, 0, true))
1991 BOOST_FOREACH (const CNetAddr &addr, vaddr)
1993 if (AddLocal(addr, LOCAL_IF))
1994 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
1998 #else
1999 // Get local host ip
2000 struct ifaddrs* myaddrs;
2001 if (getifaddrs(&myaddrs) == 0)
2003 for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
2005 if (ifa->ifa_addr == NULL) continue;
2006 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
2007 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
2008 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
2009 if (ifa->ifa_addr->sa_family == AF_INET)
2011 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
2012 CNetAddr addr(s4->sin_addr);
2013 if (AddLocal(addr, LOCAL_IF))
2014 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2016 else if (ifa->ifa_addr->sa_family == AF_INET6)
2018 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
2019 CNetAddr addr(s6->sin6_addr);
2020 if (AddLocal(addr, LOCAL_IF))
2021 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
2024 freeifaddrs(myaddrs);
2026 #endif
2029 CConnman::CConnman()
2031 setBannedIsDirty = false;
2032 fAddressesInitialized = false;
2033 nLastNodeId = 0;
2034 nSendBufferMaxSize = 0;
2035 nReceiveFloodSize = 0;
2036 semOutbound = NULL;
2037 nMaxConnections = 0;
2038 nMaxOutbound = 0;
2039 nBestHeight = 0;
2042 bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServices, ServiceFlags nRelevantServices, int nMaxConnectionsIn, int nMaxOutboundIn, int nBestHeightIn, std::string& strNodeError)
2044 Discover(threadGroup);
2046 bool ret = connman.Start(threadGroup, scheduler, nLocalServices, nRelevantServices, nMaxConnectionsIn, nMaxOutboundIn, nBestHeightIn, strNodeError);
2048 return ret;
2051 NodeId CConnman::GetNewNodeId()
2053 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
2056 bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServicesIn, ServiceFlags nRelevantServicesIn, int nMaxConnectionsIn, int nMaxOutboundIn, int nBestHeightIn, std::string& strNodeError)
2058 nTotalBytesRecv = 0;
2059 nTotalBytesSent = 0;
2060 nMaxOutboundLimit = 0;
2061 nMaxOutboundTotalBytesSentInCycle = 0;
2062 nMaxOutboundTimeframe = 60*60*24; //1 day
2063 nLocalServices = nLocalServicesIn;
2064 nRelevantServices = nRelevantServicesIn;
2065 nMaxOutboundCycleStartTime = 0;
2067 nMaxConnections = nMaxConnectionsIn;
2068 nMaxOutbound = std::min((nMaxOutboundIn), nMaxConnections);
2070 nSendBufferMaxSize = 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
2071 nReceiveFloodSize = 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
2073 SetBestHeight(nBestHeightIn);
2075 uiInterface.InitMessage(_("Loading addresses..."));
2076 // Load addresses from peers.dat
2077 int64_t nStart = GetTimeMillis();
2079 CAddrDB adb;
2080 if (adb.Read(addrman))
2081 LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
2082 else {
2083 addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
2084 LogPrintf("Invalid or missing peers.dat; recreating\n");
2085 DumpAddresses();
2089 uiInterface.InitMessage(_("Loading banlist..."));
2090 // Load addresses from banlist.dat
2091 nStart = GetTimeMillis();
2092 CBanDB bandb;
2093 banmap_t banmap;
2094 if (bandb.Read(banmap)) {
2095 SetBanned(banmap); // thread save setter
2096 SetBannedSetDirty(false); // no need to write down, just read data
2097 SweepBanned(); // sweep out unused entries
2099 LogPrint("net", "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
2100 banmap.size(), GetTimeMillis() - nStart);
2101 } else {
2102 LogPrintf("Invalid or missing banlist.dat; recreating\n");
2103 SetBannedSetDirty(true); // force write
2104 DumpBanlist();
2107 uiInterface.InitMessage(_("Starting network threads..."));
2109 fAddressesInitialized = true;
2111 if (semOutbound == NULL) {
2112 // initialize semaphore
2113 semOutbound = new CSemaphore(std::min((nMaxOutbound + MAX_FEELER_CONNECTIONS), nMaxConnections));
2116 if (pnodeLocalHost == NULL) {
2117 CNetAddr local;
2118 LookupHost("127.0.0.1", local, false);
2119 pnodeLocalHost = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices));
2120 GetNodeSignals().InitializeNode(pnodeLocalHost->GetId(), pnodeLocalHost);
2124 // Start threads
2127 if (!GetBoolArg("-dnsseed", true))
2128 LogPrintf("DNS seeding disabled\n");
2129 else
2130 threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "dnsseed", boost::function<void()>(boost::bind(&CConnman::ThreadDNSAddressSeed, this))));
2132 // Map ports with UPnP
2133 MapPort(GetBoolArg("-upnp", DEFAULT_UPNP));
2135 // Send and receive from sockets, accept connections
2136 threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "net", boost::function<void()>(boost::bind(&CConnman::ThreadSocketHandler, this))));
2138 // Initiate outbound connections from -addnode
2139 threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "addcon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenAddedConnections, this))));
2141 // Initiate outbound connections
2142 threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "opencon", boost::function<void()>(boost::bind(&CConnman::ThreadOpenConnections, this))));
2144 // Process messages
2145 threadGroup.create_thread(boost::bind(&TraceThread<boost::function<void()> >, "msghand", boost::function<void()>(boost::bind(&CConnman::ThreadMessageHandler, this))));
2147 // Dump network addresses
2148 scheduler.scheduleEvery(boost::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL);
2150 return true;
2153 bool StopNode(CConnman& connman)
2155 LogPrintf("StopNode()\n");
2156 MapPort(false);
2158 connman.Stop();
2159 return true;
2162 class CNetCleanup
2164 public:
2165 CNetCleanup() {}
2167 ~CNetCleanup()
2169 #ifdef WIN32
2170 // Shutdown Windows Sockets
2171 WSACleanup();
2172 #endif
2175 instance_of_cnetcleanup;
2177 void CConnman::Stop()
2179 if (semOutbound)
2180 for (int i=0; i<(nMaxOutbound + MAX_FEELER_CONNECTIONS); i++)
2181 semOutbound->post();
2183 if (fAddressesInitialized)
2185 DumpData();
2186 fAddressesInitialized = false;
2189 // Close sockets
2190 BOOST_FOREACH(CNode* pnode, vNodes)
2191 if (pnode->hSocket != INVALID_SOCKET)
2192 CloseSocket(pnode->hSocket);
2193 BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
2194 if (hListenSocket.socket != INVALID_SOCKET)
2195 if (!CloseSocket(hListenSocket.socket))
2196 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2198 // clean up some globals (to help leak detection)
2199 BOOST_FOREACH(CNode *pnode, vNodes) {
2200 DeleteNode(pnode);
2202 BOOST_FOREACH(CNode *pnode, vNodesDisconnected) {
2203 DeleteNode(pnode);
2205 vNodes.clear();
2206 vNodesDisconnected.clear();
2207 vhListenSocket.clear();
2208 delete semOutbound;
2209 semOutbound = NULL;
2210 if(pnodeLocalHost)
2211 DeleteNode(pnodeLocalHost);
2212 pnodeLocalHost = NULL;
2215 void CConnman::DeleteNode(CNode* pnode)
2217 assert(pnode);
2218 bool fUpdateConnectionTime = false;
2219 GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2220 if(fUpdateConnectionTime)
2221 addrman.Connected(pnode->addr);
2222 delete pnode;
2225 CConnman::~CConnman()
2229 size_t CConnman::GetAddressCount() const
2231 return addrman.size();
2234 void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2236 addrman.SetServices(addr, nServices);
2239 void CConnman::MarkAddressGood(const CAddress& addr)
2241 addrman.Good(addr);
2244 void CConnman::AddNewAddress(const CAddress& addr, const CAddress& addrFrom, int64_t nTimePenalty)
2246 addrman.Add(addr, addrFrom, nTimePenalty);
2249 void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2251 addrman.Add(vAddr, addrFrom, nTimePenalty);
2254 std::vector<CAddress> CConnman::GetAddresses()
2256 return addrman.GetAddr();
2259 bool CConnman::AddNode(const std::string& strNode)
2261 LOCK(cs_vAddedNodes);
2262 for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2263 if (strNode == *it)
2264 return false;
2267 vAddedNodes.push_back(strNode);
2268 return true;
2271 bool CConnman::RemoveAddedNode(const std::string& strNode)
2273 LOCK(cs_vAddedNodes);
2274 for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2275 if (strNode == *it) {
2276 vAddedNodes.erase(it);
2277 return true;
2280 return false;
2283 size_t CConnman::GetNodeCount(NumConnections flags)
2285 LOCK(cs_vNodes);
2286 if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2287 return vNodes.size();
2289 int nNum = 0;
2290 for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
2291 if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
2292 nNum++;
2294 return nNum;
2297 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2299 vstats.clear();
2300 LOCK(cs_vNodes);
2301 vstats.reserve(vNodes.size());
2302 for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
2303 CNode* pnode = *it;
2304 CNodeStats stats;
2305 pnode->copyStats(stats);
2306 vstats.push_back(stats);
2310 bool CConnman::DisconnectAddress(const CNetAddr& netAddr)
2312 if (CNode* pnode = FindNode(netAddr)) {
2313 pnode->fDisconnect = true;
2314 return true;
2316 return false;
2319 bool CConnman::DisconnectSubnet(const CSubNet& subNet)
2321 if (CNode* pnode = FindNode(subNet)) {
2322 pnode->fDisconnect = true;
2323 return true;
2325 return false;
2328 bool CConnman::DisconnectNode(const std::string& strNode)
2330 if (CNode* pnode = FindNode(strNode)) {
2331 pnode->fDisconnect = true;
2332 return true;
2334 return false;
2336 bool CConnman::DisconnectNode(NodeId id)
2338 LOCK(cs_vNodes);
2339 for(CNode* pnode : vNodes) {
2340 if (id == pnode->id) {
2341 pnode->fDisconnect = true;
2342 return true;
2345 return false;
2348 void CConnman::RelayTransaction(const CTransaction& tx)
2350 CInv inv(MSG_TX, tx.GetHash());
2351 LOCK(cs_vNodes);
2352 BOOST_FOREACH(CNode* pnode, vNodes)
2354 pnode->PushInventory(inv);
2358 void CConnman::RecordBytesRecv(uint64_t bytes)
2360 LOCK(cs_totalBytesRecv);
2361 nTotalBytesRecv += bytes;
2364 void CConnman::RecordBytesSent(uint64_t bytes)
2366 LOCK(cs_totalBytesSent);
2367 nTotalBytesSent += bytes;
2369 uint64_t now = GetTime();
2370 if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2372 // timeframe expired, reset cycle
2373 nMaxOutboundCycleStartTime = now;
2374 nMaxOutboundTotalBytesSentInCycle = 0;
2377 // TODO, exclude whitebind peers
2378 nMaxOutboundTotalBytesSentInCycle += bytes;
2381 void CConnman::SetMaxOutboundTarget(uint64_t limit)
2383 LOCK(cs_totalBytesSent);
2384 uint64_t recommendedMinimum = (nMaxOutboundTimeframe / 600) * MAX_BLOCK_SERIALIZED_SIZE;
2385 nMaxOutboundLimit = limit;
2387 if (limit > 0 && limit < recommendedMinimum)
2388 LogPrintf("Max outbound target is very small (%s bytes) and will be overshot. Recommended minimum is %s bytes.\n", nMaxOutboundLimit, recommendedMinimum);
2391 uint64_t CConnman::GetMaxOutboundTarget()
2393 LOCK(cs_totalBytesSent);
2394 return nMaxOutboundLimit;
2397 uint64_t CConnman::GetMaxOutboundTimeframe()
2399 LOCK(cs_totalBytesSent);
2400 return nMaxOutboundTimeframe;
2403 uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2405 LOCK(cs_totalBytesSent);
2406 if (nMaxOutboundLimit == 0)
2407 return 0;
2409 if (nMaxOutboundCycleStartTime == 0)
2410 return nMaxOutboundTimeframe;
2412 uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2413 uint64_t now = GetTime();
2414 return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2417 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2419 LOCK(cs_totalBytesSent);
2420 if (nMaxOutboundTimeframe != timeframe)
2422 // reset measure-cycle in case of changing
2423 // the timeframe
2424 nMaxOutboundCycleStartTime = GetTime();
2426 nMaxOutboundTimeframe = timeframe;
2429 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2431 LOCK(cs_totalBytesSent);
2432 if (nMaxOutboundLimit == 0)
2433 return false;
2435 if (historicalBlockServingLimit)
2437 // keep a large enough buffer to at least relay each block once
2438 uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2439 uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2440 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2441 return true;
2443 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2444 return true;
2446 return false;
2449 uint64_t CConnman::GetOutboundTargetBytesLeft()
2451 LOCK(cs_totalBytesSent);
2452 if (nMaxOutboundLimit == 0)
2453 return 0;
2455 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2458 uint64_t CConnman::GetTotalBytesRecv()
2460 LOCK(cs_totalBytesRecv);
2461 return nTotalBytesRecv;
2464 uint64_t CConnman::GetTotalBytesSent()
2466 LOCK(cs_totalBytesSent);
2467 return nTotalBytesSent;
2470 ServiceFlags CConnman::GetLocalServices() const
2472 return nLocalServices;
2475 void CConnman::SetBestHeight(int height)
2477 nBestHeight.store(height, std::memory_order_release);
2480 int CConnman::GetBestHeight() const
2482 return nBestHeight.load(std::memory_order_acquire);
2485 void CNode::Fuzz(int nChance)
2487 if (!fSuccessfullyConnected) return; // Don't fuzz initial handshake
2488 if (GetRand(nChance) != 0) return; // Fuzz 1 of every nChance messages
2490 switch (GetRand(3))
2492 case 0:
2493 // xor a random byte with a random value:
2494 if (!ssSend.empty()) {
2495 CDataStream::size_type pos = GetRand(ssSend.size());
2496 ssSend[pos] ^= (unsigned char)(GetRand(256));
2498 break;
2499 case 1:
2500 // delete a random byte:
2501 if (!ssSend.empty()) {
2502 CDataStream::size_type pos = GetRand(ssSend.size());
2503 ssSend.erase(ssSend.begin()+pos);
2505 break;
2506 case 2:
2507 // insert a random byte at a random position
2509 CDataStream::size_type pos = GetRand(ssSend.size());
2510 char ch = (char)GetRand(256);
2511 ssSend.insert(ssSend.begin()+pos, ch);
2513 break;
2515 // Chance of more than one change half the time:
2516 // (more changes exponentially less likely):
2517 Fuzz(2);
2520 unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2521 unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize; }
2523 CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNameIn, bool fInboundIn) :
2524 ssSend(SER_NETWORK, INIT_PROTO_VERSION),
2525 addr(addrIn),
2526 nKeyedNetGroup(CalculateKeyedNetGroup(addrIn)),
2527 addrKnown(5000, 0.001),
2528 filterInventoryKnown(50000, 0.000001)
2530 nServices = NODE_NONE;
2531 nServicesExpected = NODE_NONE;
2532 hSocket = hSocketIn;
2533 nRecvVersion = INIT_PROTO_VERSION;
2534 nLastSend = 0;
2535 nLastRecv = 0;
2536 nSendBytes = 0;
2537 nRecvBytes = 0;
2538 nTimeConnected = GetTime();
2539 nTimeOffset = 0;
2540 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2541 nVersion = 0;
2542 strSubVer = "";
2543 fWhitelisted = false;
2544 fOneShot = false;
2545 fClient = false; // set by version message
2546 fFeeler = false;
2547 fInbound = fInboundIn;
2548 fNetworkNode = false;
2549 fSuccessfullyConnected = false;
2550 fDisconnect = false;
2551 nRefCount = 0;
2552 nSendSize = 0;
2553 nSendOffset = 0;
2554 hashContinue = uint256();
2555 nStartingHeight = -1;
2556 filterInventoryKnown.reset();
2557 fSendMempool = false;
2558 fGetAddr = false;
2559 nNextLocalAddrSend = 0;
2560 nNextAddrSend = 0;
2561 nNextInvSend = 0;
2562 fRelayTxes = false;
2563 fSentAddr = false;
2564 pfilter = new CBloomFilter();
2565 timeLastMempoolReq = 0;
2566 nLastBlockTime = 0;
2567 nLastTXTime = 0;
2568 nPingNonceSent = 0;
2569 nPingUsecStart = 0;
2570 nPingUsecTime = 0;
2571 fPingQueued = false;
2572 nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2573 minFeeFilter = 0;
2574 lastSentFeeFilter = 0;
2575 nextSendTimeFeeFilter = 0;
2576 id = idIn;
2577 nOptimisticBytesWritten = 0;
2578 nLocalServices = nLocalServicesIn;
2580 GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
2581 nMyStartingHeight = nMyStartingHeightIn;
2583 BOOST_FOREACH(const std::string &msg, getAllNetMessageTypes())
2584 mapRecvBytesPerMsgCmd[msg] = 0;
2585 mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2587 if (fLogIPs)
2588 LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
2589 else
2590 LogPrint("net", "Added connection peer=%d\n", id);
2592 // Be shy and don't send version until we hear
2593 if (hSocket != INVALID_SOCKET && !fInbound)
2594 PushVersion();
2597 CNode::~CNode()
2599 CloseSocket(hSocket);
2601 if (pfilter)
2602 delete pfilter;
2605 void CNode::AskFor(const CInv& inv)
2607 if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
2608 return;
2609 // a peer may not have multiple non-responded queue positions for a single inv item
2610 if (!setAskFor.insert(inv.hash).second)
2611 return;
2613 // We're using mapAskFor as a priority queue,
2614 // the key is the earliest time the request can be sent
2615 int64_t nRequestTime;
2616 limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
2617 if (it != mapAlreadyAskedFor.end())
2618 nRequestTime = it->second;
2619 else
2620 nRequestTime = 0;
2621 LogPrint("net", "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2623 // Make sure not to reuse time indexes to keep things in the same order
2624 int64_t nNow = GetTimeMicros() - 1000000;
2625 static int64_t nLastTime;
2626 ++nLastTime;
2627 nNow = std::max(nNow, nLastTime);
2628 nLastTime = nNow;
2630 // Each retry is 2 minutes after the last
2631 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2632 if (it != mapAlreadyAskedFor.end())
2633 mapAlreadyAskedFor.update(it, nRequestTime);
2634 else
2635 mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
2636 mapAskFor.insert(std::make_pair(nRequestTime, inv));
2639 void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
2641 ENTER_CRITICAL_SECTION(cs_vSend);
2642 assert(ssSend.size() == 0);
2643 ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0);
2644 LogPrint("net", "sending: %s ", SanitizeString(pszCommand));
2647 void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
2649 ssSend.clear();
2651 LEAVE_CRITICAL_SECTION(cs_vSend);
2653 LogPrint("net", "(aborted)\n");
2656 void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
2658 // The -*messagestest options are intentionally not documented in the help message,
2659 // since they are only used during development to debug the networking code and are
2660 // not intended for end-users.
2661 if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
2663 LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
2664 AbortMessage();
2665 return;
2667 if (mapArgs.count("-fuzzmessagestest"))
2668 Fuzz(GetArg("-fuzzmessagestest", 10));
2670 if (ssSend.size() == 0)
2672 LEAVE_CRITICAL_SECTION(cs_vSend);
2673 return;
2675 // Set the size
2676 unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
2677 WriteLE32((uint8_t*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
2679 //log total amount of bytes per command
2680 mapSendBytesPerMsgCmd[std::string(pszCommand)] += nSize + CMessageHeader::HEADER_SIZE;
2682 // Set the checksum
2683 uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
2684 unsigned int nChecksum = 0;
2685 memcpy(&nChecksum, &hash, sizeof(nChecksum));
2686 assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
2687 memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
2689 LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
2691 std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
2692 ssSend.GetAndClear(*it);
2693 nSendSize += (*it).size();
2695 // If write queue empty, attempt "optimistic write"
2696 if (it == vSendMsg.begin())
2697 nOptimisticBytesWritten += SocketSendData(this);
2699 LEAVE_CRITICAL_SECTION(cs_vSend);
2702 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
2704 CNode* found = nullptr;
2705 LOCK(cs_vNodes);
2706 for (auto&& pnode : vNodes) {
2707 if(pnode->id == id) {
2708 found = pnode;
2709 break;
2712 return found != nullptr && func(found);
2715 bool CConnman::ForEachNode(std::function<bool(CNode* pnode)> func)
2717 LOCK(cs_vNodes);
2718 for (auto&& node : vNodes)
2719 if(!func(node))
2720 return false;
2721 return true;
2724 bool CConnman::ForEachNode(std::function<bool(const CNode* pnode)> func) const
2726 LOCK(cs_vNodes);
2727 for (const auto& node : vNodes)
2728 if(!func(node))
2729 return false;
2730 return true;
2733 bool CConnman::ForEachNodeThen(std::function<bool(CNode* pnode)> pre, std::function<void()> post)
2735 bool ret = true;
2736 LOCK(cs_vNodes);
2737 for (auto&& node : vNodes)
2738 if(!pre(node)) {
2739 ret = false;
2740 break;
2742 post();
2743 return ret;
2746 bool CConnman::ForEachNodeThen(std::function<bool(const CNode* pnode)> pre, std::function<void()> post) const
2748 bool ret = true;
2749 LOCK(cs_vNodes);
2750 for (const auto& node : vNodes)
2751 if(!pre(node)) {
2752 ret = false;
2753 break;
2755 post();
2756 return ret;
2759 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
2760 return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2763 /* static */ uint64_t CNode::CalculateKeyedNetGroup(const CAddress& ad)
2765 static const uint64_t k0 = GetRand(std::numeric_limits<uint64_t>::max());
2766 static const uint64_t k1 = GetRand(std::numeric_limits<uint64_t>::max());
2768 std::vector<unsigned char> vchNetGroup(ad.GetGroup());
2770 return CSipHasher(k0, k1).Write(&vchNetGroup[0], vchNetGroup.size()).Finalize();