Bitcoin ABC  0.22.13
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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.
5 
6 #if defined(HAVE_CONFIG_H)
7 #include <config/bitcoin-config.h>
8 #endif
9 
10 #include <net.h>
11 
12 #include <banman.h>
13 #include <clientversion.h>
14 #include <config.h>
15 #include <consensus/consensus.h>
16 #include <crypto/sha256.h>
17 #include <netbase.h>
18 #include <node/ui_interface.h>
19 #include <random.h>
20 #include <scheduler.h>
21 #include <util/strencodings.h>
22 #include <util/translation.h>
23 
24 #ifdef WIN32
25 #include <cstring>
26 #else
27 #include <fcntl.h>
28 #endif
29 
30 #ifdef USE_POLL
31 #include <poll.h>
32 #endif
33 
34 #ifdef USE_UPNP
35 #include <miniupnpc/miniupnpc.h>
36 #include <miniupnpc/upnpcommands.h>
37 #include <miniupnpc/upnperrors.h>
38 // The minimum supported miniUPnPc API version is set to 10. This keeps
39 // compatibility with Ubuntu 16.04 LTS and Debian 8 libminiupnpc-dev packages.
40 static_assert(MINIUPNPC_API_VERSION >= 10,
41  "miniUPnPc API version >= 10 assumed");
42 #endif
43 
44 #include <unordered_map>
45 
46 #include <cmath>
47 
48 // How often to dump addresses to peers.dat
49 static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15};
50 
54 static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3;
55 
66 static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11};
67 static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
68 // "many" vs "few" peers
69 static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000;
70 
71 // We add a random period time (0 to 1 seconds) to feeler connections to prevent
72 // synchronization.
73 #define FEELER_SLEEP_WINDOW 1
74 
75 // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define
76 // it as 0
77 #if !defined(MSG_NOSIGNAL)
78 #define MSG_NOSIGNAL 0
79 #endif
80 
81 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define
82 // it as 0
83 #if !defined(MSG_DONTWAIT)
84 #define MSG_DONTWAIT 0
85 #endif
86 
88 enum BindFlags {
89  BF_NONE = 0,
90  BF_EXPLICIT = (1U << 0),
91  BF_REPORT_ERROR = (1U << 1),
92 };
93 
94 // The set of sockets cannot be modified while waiting
95 // The sleep time needs to be small to avoid new sockets stalling
96 static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50;
97 
98 const std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
99 
100 // SHA256("netgroup")[0:8]
101 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL;
102 // SHA256("localhostnonce")[0:8]
103 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL;
104 // SHA256("localhostnonce")[8:16]
105 static const uint64_t RANDOMIZER_ID_EXTRAENTROPY = 0x94b05d41679a4ff7ULL;
106 //
107 // Global state variables
108 //
109 bool fDiscover = true;
110 bool fListen = true;
113 std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(cs_mapLocalHost);
114 static bool vfLimited[NET_MAX] GUARDED_BY(cs_mapLocalHost) = {};
115 
116 void CConnman::AddAddrFetch(const std::string &strDest) {
118  m_addr_fetches.push_back(strDest);
119 }
120 
121 unsigned short GetListenPort() {
122  return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
123 }
124 
125 // find 'best' local address for a particular peer
126 bool GetLocal(CService &addr, const CNetAddr *paddrPeer) {
127  if (!fListen) {
128  return false;
129  }
130 
131  int nBestScore = -1;
132  int nBestReachability = -1;
133  {
134  LOCK(cs_mapLocalHost);
135  for (const auto &entry : mapLocalHost) {
136  int nScore = entry.second.nScore;
137  int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
138  if (nReachability > nBestReachability ||
139  (nReachability == nBestReachability && nScore > nBestScore)) {
140  addr = CService(entry.first, entry.second.nPort);
141  nBestReachability = nReachability;
142  nBestScore = nScore;
143  }
144  }
145  }
146  return nBestScore >= 0;
147 }
148 
150 static std::vector<CAddress>
151 convertSeed6(const std::vector<SeedSpec6> &vSeedsIn) {
152  // It'll only connect to one or two seed nodes because once it connects,
153  // it'll get a pile of addresses with newer timestamps. Seed nodes are given
154  // a random 'last seen time' of between one and two weeks ago.
155  const int64_t nOneWeek = 7 * 24 * 60 * 60;
156  std::vector<CAddress> vSeedsOut;
157  vSeedsOut.reserve(vSeedsIn.size());
158  FastRandomContext rng;
159  for (const auto &seed_in : vSeedsIn) {
160  struct in6_addr ip;
161  memcpy(&ip, seed_in.addr, sizeof(ip));
162  CAddress addr(CService(ip, seed_in.port),
164  addr.nTime = GetTime() - rng.randrange(nOneWeek) - nOneWeek;
165  vSeedsOut.push_back(addr);
166  }
167  return vSeedsOut;
168 }
169 
170 // Get best local address for a particular peer as a CAddress. Otherwise, return
171 // the unroutable 0.0.0.0 but filled in with the normal parameters, since the IP
172 // may be changed to a useful one by discovery.
175  CAddress ret(CService(CNetAddr(), GetListenPort()), nLocalServices);
176  CService addr;
177  if (GetLocal(addr, paddrPeer)) {
178  ret = CAddress(addr, nLocalServices);
179  }
180  ret.nTime = GetAdjustedTime();
181  return ret;
182 }
183 
184 static int GetnScore(const CService &addr) {
185  LOCK(cs_mapLocalHost);
186  if (mapLocalHost.count(addr) == 0) {
187  return 0;
188  }
189  return mapLocalHost[addr].nScore;
190 }
191 
192 // Is our peer's addrLocal potentially useful as an external IP source?
194  CService addrLocal = pnode->GetAddrLocal();
195  return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
196  IsReachable(addrLocal.GetNetwork());
197 }
198 
199 // Pushes our own address to a peer.
200 void AdvertiseLocal(CNode *pnode) {
201  if (fListen && pnode->fSuccessfullyConnected) {
202  CAddress addrLocal =
203  GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
204  if (gArgs.GetBoolArg("-addrmantest", false)) {
205  // use IPv4 loopback during addrmantest
206  addrLocal =
207  CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())),
208  pnode->GetLocalServices());
209  }
210  // If discovery is enabled, sometimes give our peer the address it
211  // tells us that it sees us as in case it has a better idea of our
212  // address than we do.
213  FastRandomContext rng;
214  if (IsPeerAddrLocalGood(pnode) &&
215  (!addrLocal.IsRoutable() ||
216  rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) ==
217  0)) {
218  addrLocal.SetIP(pnode->GetAddrLocal());
219  }
220  if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false)) {
221  LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n",
222  addrLocal.ToString());
223  pnode->PushAddress(addrLocal, rng);
224  }
225  }
226 }
227 
228 // Learn a new local address.
229 bool AddLocal(const CService &addr, int nScore) {
230  if (!addr.IsRoutable()) {
231  return false;
232  }
233 
234  if (!fDiscover && nScore < LOCAL_MANUAL) {
235  return false;
236  }
237 
238  if (!IsReachable(addr)) {
239  return false;
240  }
241 
242  LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
243 
244  {
245  LOCK(cs_mapLocalHost);
246  bool fAlready = mapLocalHost.count(addr) > 0;
247  LocalServiceInfo &info = mapLocalHost[addr];
248  if (!fAlready || nScore >= info.nScore) {
249  info.nScore = nScore + (fAlready ? 1 : 0);
250  info.nPort = addr.GetPort();
251  }
252  }
253 
254  return true;
255 }
256 
257 bool AddLocal(const CNetAddr &addr, int nScore) {
258  return AddLocal(CService(addr, GetListenPort()), nScore);
259 }
260 
261 void RemoveLocal(const CService &addr) {
262  LOCK(cs_mapLocalHost);
263  LogPrintf("RemoveLocal(%s)\n", addr.ToString());
264  mapLocalHost.erase(addr);
265 }
266 
267 void SetReachable(enum Network net, bool reachable) {
268  if (net == NET_UNROUTABLE || net == NET_INTERNAL) {
269  return;
270  }
271  LOCK(cs_mapLocalHost);
272  vfLimited[net] = !reachable;
273 }
274 
275 bool IsReachable(enum Network net) {
276  LOCK(cs_mapLocalHost);
277  return !vfLimited[net];
278 }
279 
280 bool IsReachable(const CNetAddr &addr) {
281  return IsReachable(addr.GetNetwork());
282 }
283 
285 bool SeenLocal(const CService &addr) {
286  LOCK(cs_mapLocalHost);
287  if (mapLocalHost.count(addr) == 0) {
288  return false;
289  }
290  mapLocalHost[addr].nScore++;
291  return true;
292 }
293 
295 bool IsLocal(const CService &addr) {
296  LOCK(cs_mapLocalHost);
297  return mapLocalHost.count(addr) > 0;
298 }
299 
301  LOCK(cs_vNodes);
302  for (CNode *pnode : vNodes) {
303  if (static_cast<CNetAddr>(pnode->addr) == ip) {
304  return pnode;
305  }
306  }
307  return nullptr;
308 }
309 
311  LOCK(cs_vNodes);
312  for (CNode *pnode : vNodes) {
313  if (subNet.Match(static_cast<CNetAddr>(pnode->addr))) {
314  return pnode;
315  }
316  }
317  return nullptr;
318 }
319 
320 CNode *CConnman::FindNode(const std::string &addrName) {
321  LOCK(cs_vNodes);
322  for (CNode *pnode : vNodes) {
323  if (pnode->GetAddrName() == addrName) {
324  return pnode;
325  }
326  }
327  return nullptr;
328 }
329 
331  LOCK(cs_vNodes);
332  for (CNode *pnode : vNodes) {
333  if (static_cast<CService>(pnode->addr) == addr) {
334  return pnode;
335  }
336  }
337  return nullptr;
338 }
339 
340 bool CConnman::CheckIncomingNonce(uint64_t nonce) {
341  LOCK(cs_vNodes);
342  for (const CNode *pnode : vNodes) {
343  if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() &&
344  pnode->GetLocalNonce() == nonce) {
345  return false;
346  }
347  }
348  return true;
349 }
350 
353  CAddress addr_bind;
354  struct sockaddr_storage sockaddr_bind;
355  socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
356  if (sock != INVALID_SOCKET) {
357  if (!getsockname(sock, (struct sockaddr *)&sockaddr_bind,
358  &sockaddr_bind_len)) {
359  addr_bind.SetSockAddr((const struct sockaddr *)&sockaddr_bind);
360  } else {
361  LogPrint(BCLog::NET, "Warning: getsockname failed\n");
362  }
363  }
364  return addr_bind;
365 }
366 
367 CNode *CConnman::ConnectNode(CAddress addrConnect, const char *pszDest,
368  bool fCountFailure, ConnectionType conn_type) {
369  assert(conn_type != ConnectionType::INBOUND);
370 
371  if (pszDest == nullptr) {
372  if (IsLocal(addrConnect)) {
373  return nullptr;
374  }
375 
376  // Look for an existing connection
377  CNode *pnode = FindNode(static_cast<CService>(addrConnect));
378  if (pnode) {
379  LogPrintf("Failed to open new connection, already connected\n");
380  return nullptr;
381  }
382  }
383 
385  LogPrint(BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
386  pszDest ? pszDest : addrConnect.ToString(),
387  pszDest
388  ? 0.0
389  : (double)(GetAdjustedTime() - addrConnect.nTime) / 3600.0);
390 
391  // Resolve
392  const int default_port = Params().GetDefaultPort();
393  if (pszDest) {
394  std::vector<CService> resolved;
395  if (Lookup(pszDest, resolved, default_port,
396  fNameLookup && !HaveNameProxy(), 256) &&
397  !resolved.empty()) {
398  addrConnect =
399  CAddress(resolved[GetRand(resolved.size())], NODE_NONE);
400  if (!addrConnect.IsValid()) {
402  "Resolver returned invalid address %s for %s\n",
403  addrConnect.ToString(), pszDest);
404  return nullptr;
405  }
406  // It is possible that we already have a connection to the IP/port
407  // pszDest resolved to. In that case, drop the connection that was
408  // just created, and return the existing CNode instead. Also store
409  // the name we used to connect in that CNode, so that future
410  // FindNode() calls to that name catch this early.
411  LOCK(cs_vNodes);
412  CNode *pnode = FindNode(static_cast<CService>(addrConnect));
413  if (pnode) {
414  pnode->MaybeSetAddrName(std::string(pszDest));
415  LogPrintf("Failed to open new connection, already connected\n");
416  return nullptr;
417  }
418  }
419  }
420 
421  // Connect
422  bool connected = false;
423  SOCKET hSocket = INVALID_SOCKET;
424  proxyType proxy;
425  if (addrConnect.IsValid()) {
426  bool proxyConnectionFailed = false;
427 
428  if (GetProxy(addrConnect.GetNetwork(), proxy)) {
429  hSocket = CreateSocket(proxy.proxy);
430  if (hSocket == INVALID_SOCKET) {
431  return nullptr;
432  }
433  connected = ConnectThroughProxy(
434  proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), hSocket,
435  nConnectTimeout, proxyConnectionFailed);
436  } else {
437  // no proxy needed (none set for target network)
438  hSocket = CreateSocket(addrConnect);
439  if (hSocket == INVALID_SOCKET) {
440  return nullptr;
441  }
442  connected =
443  ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout,
444  conn_type == ConnectionType::MANUAL);
445  }
446  if (!proxyConnectionFailed) {
447  // If a connection to the node was attempted, and failure (if any)
448  // is not caused by a problem connecting to the proxy, mark this as
449  // an attempt.
450  addrman.Attempt(addrConnect, fCountFailure);
451  }
452  } else if (pszDest && GetNameProxy(proxy)) {
453  hSocket = CreateSocket(proxy.proxy);
454  if (hSocket == INVALID_SOCKET) {
455  return nullptr;
456  }
457  std::string host;
458  int port = default_port;
459  SplitHostPort(std::string(pszDest), port, host);
460  bool proxyConnectionFailed;
461  connected = ConnectThroughProxy(proxy, host, port, hSocket,
462  nConnectTimeout, proxyConnectionFailed);
463  }
464  if (!connected) {
465  CloseSocket(hSocket);
466  return nullptr;
467  }
468 
469  // Add node
470  NodeId id = GetNewNodeId();
471  uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE)
472  .Write(id)
473  .Finalize();
474  uint64_t extra_entropy =
475  GetDeterministicRandomizer(RANDOMIZER_ID_EXTRAENTROPY)
476  .Write(id)
477  .Finalize();
478  CAddress addr_bind = GetBindAddress(hSocket);
479  CNode *pnode =
480  new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect,
481  CalculateKeyedNetGroup(addrConnect), nonce, extra_entropy,
482  addr_bind, pszDest ? pszDest : "", conn_type);
483  pnode->AddRef();
484 
485  // We're making a new connection, harvest entropy from the time (and our
486  // peer count)
487  RandAddEvent(uint32_t(id));
488 
489  return pnode;
490 }
491 
493  fDisconnect = true;
494  LOCK(cs_hSocket);
495  if (hSocket != INVALID_SOCKET) {
496  LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
497  CloseSocket(hSocket);
498  }
499 }
500 
502  const CNetAddr &addr) const {
503  for (const auto &subnet : vWhitelistedRange) {
504  if (subnet.m_subnet.Match(addr)) {
505  NetPermissions::AddFlag(flags, subnet.m_flags);
506  }
507  }
508 }
509 
510 std::string CNode::GetAddrName() const {
511  LOCK(cs_addrName);
512  return addrName;
513 }
514 
515 void CNode::MaybeSetAddrName(const std::string &addrNameIn) {
516  LOCK(cs_addrName);
517  if (addrName.empty()) {
518  addrName = addrNameIn;
519  }
520 }
521 
523  LOCK(cs_addrLocal);
524  return addrLocal;
525 }
526 
527 void CNode::SetAddrLocal(const CService &addrLocalIn) {
528  LOCK(cs_addrLocal);
529  if (addrLocal.IsValid()) {
530  error("Addr local already set for node: %i. Refusing to change from %s "
531  "to %s",
532  id, addrLocal.ToString(), addrLocalIn.ToString());
533  } else {
534  addrLocal = addrLocalIn;
535  }
536 }
537 
538 void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap) {
539  stats.nodeid = this->GetId();
540  stats.nServices = nServices;
541  stats.addr = addr;
542  stats.addrBind = addrBind;
543  stats.m_mapped_as = addr.GetMappedAS(m_asmap);
544  if (m_tx_relay != nullptr) {
545  LOCK(m_tx_relay->cs_filter);
546  stats.fRelayTxes = m_tx_relay->fRelayTxes;
547  } else {
548  stats.fRelayTxes = false;
549  }
550  stats.nLastSend = nLastSend;
551  stats.nLastRecv = nLastRecv;
552  stats.nTimeConnected = nTimeConnected;
553  stats.nTimeOffset = nTimeOffset;
554  stats.addrName = GetAddrName();
555  stats.nVersion = nVersion;
556  {
557  LOCK(cs_SubVer);
558  stats.cleanSubVer = cleanSubVer;
559  }
560  stats.fInbound = IsInboundConn();
561  stats.m_manual_connection = IsManualConn();
562  stats.nStartingHeight = nStartingHeight;
563  {
564  LOCK(cs_vSend);
565  stats.mapSendBytesPerMsgCmd = mapSendBytesPerMsgCmd;
566  stats.nSendBytes = nSendBytes;
567  }
568  {
569  LOCK(cs_vRecv);
570  stats.mapRecvBytesPerMsgCmd = mapRecvBytesPerMsgCmd;
571  stats.nRecvBytes = nRecvBytes;
572  }
573  stats.m_legacyWhitelisted = m_legacyWhitelisted;
574  stats.m_permissionFlags = m_permissionFlags;
575  if (m_tx_relay != nullptr) {
576  LOCK(m_tx_relay->cs_feeFilter);
577  stats.minFeeFilter = m_tx_relay->minFeeFilter;
578  } else {
579  stats.minFeeFilter = Amount::zero();
580  }
581 
582  // It is common for nodes with good ping times to suddenly become lagged,
583  // due to a new block arriving or other large transfer. Merely reporting
584  // pingtime might fool the caller into thinking the node was still
585  // responsive, since pingtime does not update until the ping is complete,
586  // which might take a while. So, if a ping is taking an unusually long time
587  // in flight, the caller can immediately detect that this is happening.
588  int64_t nPingUsecWait = 0;
589  if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
590  nPingUsecWait = GetTimeMicros() - nPingUsecStart;
591  }
592 
593  // Raw ping time is in microseconds, but show it to user as whole seconds
594  // (Bitcoin users should be well used to small numbers with many decimal
595  // places by now :)
596  stats.m_ping_usec = nPingUsecTime;
597  stats.m_min_ping_usec = nMinPingUsecTime;
598  stats.m_ping_wait_usec = nPingUsecWait;
599 
600  // Leave string empty if addrLocal invalid (not filled in yet)
601  CService addrLocalUnlocked = GetAddrLocal();
602  stats.addrLocal =
603  addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
604 }
605 
606 bool CNode::ReceiveMsgBytes(const Config &config, const char *pch,
607  uint32_t nBytes, bool &complete) {
608  complete = false;
609  int64_t nTimeMicros = GetTimeMicros();
610  LOCK(cs_vRecv);
611  nLastRecv = nTimeMicros / 1000000;
612  nRecvBytes += nBytes;
613  while (nBytes > 0) {
614  // Absorb network data.
615  int handled = m_deserializer->Read(config, pch, nBytes);
616 
617  if (handled < 0) {
618  return false;
619  }
620 
621  pch += handled;
622  nBytes -= handled;
623 
624  if (m_deserializer->Complete()) {
625  // decompose a transport agnostic CNetMessage from the deserializer
626  CNetMessage msg = m_deserializer->GetMessage(config, nTimeMicros);
627 
628  // Store received bytes per message command to prevent a memory DOS,
629  // only allow valid commands.
630  mapMsgCmdSize::iterator i =
631  mapRecvBytesPerMsgCmd.find(msg.m_command);
632  if (i == mapRecvBytesPerMsgCmd.end()) {
633  i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
634  }
635 
636  assert(i != mapRecvBytesPerMsgCmd.end());
637  i->second += msg.m_raw_message_size;
638 
639  // push the message to the process queue,
640  vRecvMsg.push_back(std::move(msg));
641 
642  complete = true;
643  }
644  }
645 
646  return true;
647 }
648 
649 void CNode::SetSendVersion(int nVersionIn) {
650  // Send version may only be changed in the version message, and only one
651  // version message is allowed per session. We can therefore treat this value
652  // as const and even atomic as long as it's only used once a version message
653  // has been successfully processed. Any attempt to set this twice is an
654  // error.
655  if (nSendVersion != 0) {
656  error("Send version already set for node: %i. Refusing to change from "
657  "%i to %i",
658  id, nSendVersion, nVersionIn);
659  } else {
660  nSendVersion = nVersionIn;
661  }
662 }
663 
665  // The send version should always be explicitly set to INIT_PROTO_VERSION
666  // rather than using this value until SetSendVersion has been called.
667  if (nSendVersion == 0) {
668  error("Requesting unset send version for node: %i. Using %i", id,
670  return INIT_PROTO_VERSION;
671  }
672  return nSendVersion;
673 }
674 
676  uint32_t nBytes) {
677  // copy data to temporary parsing buffer
678  uint32_t nRemaining = 24 - nHdrPos;
679  uint32_t nCopy = std::min(nRemaining, nBytes);
680 
681  memcpy(&hdrbuf[nHdrPos], pch, nCopy);
682  nHdrPos += nCopy;
683 
684  // if header incomplete, exit
685  if (nHdrPos < 24) {
686  return nCopy;
687  }
688 
689  // deserialize to CMessageHeader
690  try {
691  hdrbuf >> hdr;
692  } catch (const std::exception &) {
693  return -1;
694  }
695 
696  // Reject oversized messages
697  if (hdr.IsOversized(config)) {
698  LogPrint(BCLog::NET, "Oversized header detected\n");
699  return -1;
700  }
701 
702  // switch state to reading message data
703  in_data = true;
704 
705  return nCopy;
706 }
707 
708 int V1TransportDeserializer::readData(const char *pch, uint32_t nBytes) {
709  unsigned int nRemaining = hdr.nMessageSize - nDataPos;
710  unsigned int nCopy = std::min(nRemaining, nBytes);
711 
712  if (vRecv.size() < nDataPos + nCopy) {
713  // Allocate up to 256 KiB ahead, but never more than the total message
714  // size.
715  vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
716  }
717 
718  hasher.Write((const uint8_t *)pch, nCopy);
719  memcpy(&vRecv[nDataPos], pch, nCopy);
720  nDataPos += nCopy;
721 
722  return nCopy;
723 }
724 
726  assert(Complete());
727  if (data_hash.IsNull()) {
728  hasher.Finalize(data_hash.begin());
729  }
730  return data_hash;
731 }
732 
734  int64_t time) {
735  // decompose a single CNetMessage from the TransportDeserializer
736  CNetMessage msg(std::move(vRecv));
737 
738  // store state about valid header, netmagic and checksum
739  msg.m_valid_header = hdr.IsValid(config);
740  // FIXME Split CheckHeaderMagicAndCommand() into CheckHeaderMagic() and
741  // CheckCommand() to prevent the net magic check code duplication.
742  msg.m_valid_netmagic =
743  (memcmp(std::begin(hdr.pchMessageStart),
744  std::begin(config.GetChainParams().NetMagic()),
746  uint256 hash = GetMessageHash();
747 
748  // store command string, payload size
749  msg.m_command = hdr.GetCommand();
750  msg.m_message_size = hdr.nMessageSize;
751  msg.m_raw_message_size = hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
752 
753  // We just received a message off the wire, harvest entropy from the time
754  // (and the message checksum)
755  RandAddEvent(ReadLE32(hash.begin()));
756 
757  msg.m_valid_checksum = (memcmp(hash.begin(), hdr.pchChecksum,
759 
760  if (!msg.m_valid_checksum) {
761  LogPrint(
762  BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s\n",
765  HexStr(hdr.pchChecksum,
766  hdr.pchChecksum + CMessageHeader::CHECKSUM_SIZE));
767  }
768 
769  // store receive time
770  msg.m_time = time;
771 
772  // reset the network deserializer (prepare for the next message)
773  Reset();
774  return msg;
775 }
776 
778  CSerializedNetMsg &msg,
779  std::vector<uint8_t> &header) {
780  // create dbl-sha256 checksum
781  uint256 hash = Hash(msg.data.begin(), msg.data.end());
782 
783  // create header
784  CMessageHeader hdr(config.GetChainParams().NetMagic(), msg.m_type.c_str(),
785  msg.data.size());
786  memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
787 
788  // serialize header
789  header.reserve(CMessageHeader::HEADER_SIZE);
790  CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, header, 0, hdr};
791 }
792 
793 size_t CConnman::SocketSendData(CNode *pnode) const
794  EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_vSend) {
795  size_t nSentSize = 0;
796  size_t nMsgCount = 0;
797 
798  for (const auto &data : pnode->vSendMsg) {
799  assert(data.size() > pnode->nSendOffset);
800  int nBytes = 0;
801 
802  {
803  LOCK(pnode->cs_hSocket);
804  if (pnode->hSocket == INVALID_SOCKET) {
805  break;
806  }
807 
808  nBytes = send(pnode->hSocket,
809  reinterpret_cast<const char *>(data.data()) +
810  pnode->nSendOffset,
811  data.size() - pnode->nSendOffset,
813  }
814 
815  if (nBytes == 0) {
816  // couldn't send anything at all
817  break;
818  }
819 
820  if (nBytes < 0) {
821  // error
822  int nErr = WSAGetLastError();
823  if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE &&
824  nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
825  LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
826  pnode->CloseSocketDisconnect();
827  }
828 
829  break;
830  }
831 
832  assert(nBytes > 0);
833  pnode->nLastSend = GetSystemTimeInSeconds();
834  pnode->nSendBytes += nBytes;
835  pnode->nSendOffset += nBytes;
836  nSentSize += nBytes;
837  if (pnode->nSendOffset != data.size()) {
838  // could not send full message; stop sending more
839  break;
840  }
841 
842  pnode->nSendOffset = 0;
843  pnode->nSendSize -= data.size();
844  pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
845  nMsgCount++;
846  }
847 
848  pnode->vSendMsg.erase(pnode->vSendMsg.begin(),
849  pnode->vSendMsg.begin() + nMsgCount);
850 
851  if (pnode->vSendMsg.empty()) {
852  assert(pnode->nSendOffset == 0);
853  assert(pnode->nSendSize == 0);
854  }
855 
856  return nSentSize;
857 }
858 
861  int64_t nTimeConnected;
863  int64_t nLastBlockTime;
864  int64_t nLastTXTime;
869  uint64_t nKeyedNetGroup;
871 };
872 
874  const NodeEvictionCandidate &b) {
875  return a.nMinPingUsecTime > b.nMinPingUsecTime;
876 }
877 
879  const NodeEvictionCandidate &b) {
880  return a.nTimeConnected > b.nTimeConnected;
881 }
882 
884  const NodeEvictionCandidate &b) {
885  return a.nKeyedNetGroup < b.nKeyedNetGroup;
886 }
887 
889  const NodeEvictionCandidate &b) {
890  // There is a fall-through here because it is common for a node to have many
891  // peers which have not yet relayed a block.
892  if (a.nLastBlockTime != b.nLastBlockTime) {
893  return a.nLastBlockTime < b.nLastBlockTime;
894  }
895 
897  return b.fRelevantServices;
898  }
899 
900  return a.nTimeConnected > b.nTimeConnected;
901 }
902 
904  const NodeEvictionCandidate &b) {
905  // There is a fall-through here because it is common for a node to have more
906  // than a few peers that have not yet relayed txn.
907  if (a.nLastTXTime != b.nLastTXTime) {
908  return a.nLastTXTime < b.nLastTXTime;
909  }
910 
911  if (a.fRelayTxes != b.fRelayTxes) {
912  return b.fRelayTxes;
913  }
914 
915  if (a.fBloomFilter != b.fBloomFilter) {
916  return a.fBloomFilter;
917  }
918 
919  return a.nTimeConnected > b.nTimeConnected;
920 }
921 
923 template <typename T, typename Comparator>
924 static void EraseLastKElements(std::vector<T> &elements, Comparator comparator,
925  size_t k) {
926  std::sort(elements.begin(), elements.end(), comparator);
927  size_t eraseSize = std::min(k, elements.size());
928  elements.erase(elements.end() - eraseSize, elements.end());
929 }
930 
941  std::vector<NodeEvictionCandidate> vEvictionCandidates;
942  {
943  LOCK(cs_vNodes);
944 
945  for (const CNode *node : vNodes) {
946  if (node->HasPermission(PF_NOBAN)) {
947  continue;
948  }
949  if (!node->IsInboundConn()) {
950  continue;
951  }
952  if (node->fDisconnect) {
953  continue;
954  }
955  bool peer_relay_txes = false;
956  bool peer_filter_not_null = false;
957  if (node->m_tx_relay != nullptr) {
958  LOCK(node->m_tx_relay->cs_filter);
959  peer_relay_txes = node->m_tx_relay->fRelayTxes;
960  peer_filter_not_null = node->m_tx_relay->pfilter != nullptr;
961  }
962  NodeEvictionCandidate candidate = {
963  node->GetId(),
964  node->nTimeConnected,
965  node->nMinPingUsecTime,
966  node->nLastBlockTime,
967  node->nLastTXTime,
968  HasAllDesirableServiceFlags(node->nServices),
969  peer_relay_txes,
970  peer_filter_not_null,
971  node->addr,
972  node->nKeyedNetGroup,
973  node->m_prefer_evict};
974  vEvictionCandidates.push_back(candidate);
975  }
976  }
977 
978  // Protect connections with certain characteristics
979 
980  // Deterministically select 4 peers to protect by netgroup.
981  // An attacker cannot predict which netgroups will be protected
982  EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
983  // Protect the 8 nodes with the lowest minimum ping time.
984  // An attacker cannot manipulate this metric without physically moving nodes
985  // closer to the target.
986  EraseLastKElements(vEvictionCandidates, ReverseCompareNodeMinPingTime, 8);
987  // Protect 4 nodes that most recently sent us transactions.
988  // An attacker cannot manipulate this metric without performing useful work.
989  EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4);
990  // Protect 4 nodes that most recently sent us blocks.
991  // An attacker cannot manipulate this metric without performing useful work.
992  EraseLastKElements(vEvictionCandidates, CompareNodeBlockTime, 4);
993  // Protect the half of the remaining nodes which have been connected the
994  // longest. This replicates the non-eviction implicit behavior, and
995  // precludes attacks that start later.
997  vEvictionCandidates.size() / 2);
998 
999  if (vEvictionCandidates.empty()) {
1000  return false;
1001  }
1002 
1003  // If any remaining peers are preferred for eviction consider only them.
1004  // This happens after the other preferences since if a peer is really the
1005  // best by other criteria (esp relaying blocks)
1006  // then we probably don't want to evict it no matter what.
1007  if (std::any_of(
1008  vEvictionCandidates.begin(), vEvictionCandidates.end(),
1009  [](NodeEvictionCandidate const &n) { return n.prefer_evict; })) {
1010  vEvictionCandidates.erase(
1011  std::remove_if(
1012  vEvictionCandidates.begin(), vEvictionCandidates.end(),
1013  [](NodeEvictionCandidate const &n) { return !n.prefer_evict; }),
1014  vEvictionCandidates.end());
1015  }
1016 
1017  // Identify the network group with the most connections and youngest member.
1018  // (vEvictionCandidates is already sorted by reverse connect time)
1019  uint64_t naMostConnections;
1020  unsigned int nMostConnections = 0;
1021  int64_t nMostConnectionsTime = 0;
1022  std::map<uint64_t, std::vector<NodeEvictionCandidate>> mapNetGroupNodes;
1023  for (const NodeEvictionCandidate &node : vEvictionCandidates) {
1024  std::vector<NodeEvictionCandidate> &group =
1025  mapNetGroupNodes[node.nKeyedNetGroup];
1026  group.push_back(node);
1027  int64_t grouptime = group[0].nTimeConnected;
1028  size_t group_size = group.size();
1029  if (group_size > nMostConnections ||
1030  (group_size == nMostConnections &&
1031  grouptime > nMostConnectionsTime)) {
1032  nMostConnections = group_size;
1033  nMostConnectionsTime = grouptime;
1034  naMostConnections = node.nKeyedNetGroup;
1035  }
1036  }
1037 
1038  // Reduce to the network group with the most connections
1039  vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1040 
1041  // Disconnect from the network group with the most connections
1042  NodeId evicted = vEvictionCandidates.front().id;
1043  LOCK(cs_vNodes);
1044  for (CNode *pnode : vNodes) {
1045  if (pnode->GetId() == evicted) {
1046  pnode->fDisconnect = true;
1047  return true;
1048  }
1049  }
1050  return false;
1051 }
1052 
1053 void CConnman::AcceptConnection(const ListenSocket &hListenSocket) {
1054  struct sockaddr_storage sockaddr;
1055  socklen_t len = sizeof(sockaddr);
1056  SOCKET hSocket =
1057  accept(hListenSocket.socket, (struct sockaddr *)&sockaddr, &len);
1058  CAddress addr;
1059  int nInbound = 0;
1060  int nMaxInbound = nMaxConnections - m_max_outbound;
1061 
1062  if (hSocket != INVALID_SOCKET) {
1063  if (!addr.SetSockAddr((const struct sockaddr *)&sockaddr)) {
1064  LogPrintf("Warning: Unknown socket family\n");
1065  }
1066  }
1067 
1069  hListenSocket.AddSocketPermissionFlags(permissionFlags);
1070  AddWhitelistPermissionFlags(permissionFlags, addr);
1071  bool legacyWhitelisted = false;
1072  if (NetPermissions::HasFlag(permissionFlags,
1074  NetPermissions::ClearFlag(permissionFlags, PF_ISIMPLICIT);
1075  if (gArgs.GetBoolArg("-whitelistforcerelay",
1077  NetPermissions::AddFlag(permissionFlags, PF_FORCERELAY);
1078  }
1079  if (gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)) {
1080  NetPermissions::AddFlag(permissionFlags, PF_RELAY);
1081  }
1082  NetPermissions::AddFlag(permissionFlags, PF_MEMPOOL);
1083  NetPermissions::AddFlag(permissionFlags, PF_NOBAN);
1084  legacyWhitelisted = true;
1085  }
1086 
1087  {
1088  LOCK(cs_vNodes);
1089  for (const CNode *pnode : vNodes) {
1090  if (pnode->IsInboundConn()) {
1091  nInbound++;
1092  }
1093  }
1094  }
1095 
1096  if (hSocket == INVALID_SOCKET) {
1097  int nErr = WSAGetLastError();
1098  if (nErr != WSAEWOULDBLOCK) {
1099  LogPrintf("socket error accept failed: %s\n",
1100  NetworkErrorString(nErr));
1101  }
1102  return;
1103  }
1104 
1105  if (!fNetworkActive) {
1106  LogPrintf("connection from %s dropped: not accepting new connections\n",
1107  addr.ToString());
1108  CloseSocket(hSocket);
1109  return;
1110  }
1111 
1112  if (!IsSelectableSocket(hSocket)) {
1113  LogPrintf("connection from %s dropped: non-selectable socket\n",
1114  addr.ToString());
1115  CloseSocket(hSocket);
1116  return;
1117  }
1118 
1119  // According to the internet TCP_NODELAY is not carried into accepted
1120  // sockets on all platforms. Set it again here just to be sure.
1121  SetSocketNoDelay(hSocket);
1122 
1123  // Don't accept connections from banned peers.
1124  bool banned = m_banman && m_banman->IsBanned(addr);
1125  if (!NetPermissions::HasFlag(permissionFlags,
1127  banned) {
1128  LogPrint(BCLog::NET, "connection from %s dropped (banned)\n",
1129  addr.ToString());
1130  CloseSocket(hSocket);
1131  return;
1132  }
1133 
1134  // Only accept connections from discouraged peers if our inbound slots
1135  // aren't (almost) full.
1136  bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
1137  if (!NetPermissions::HasFlag(permissionFlags,
1139  nInbound + 1 >= nMaxInbound && discouraged) {
1140  LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n",
1141  addr.ToString());
1142  CloseSocket(hSocket);
1143  return;
1144  }
1145 
1146  if (nInbound >= nMaxInbound) {
1147  if (!AttemptToEvictConnection()) {
1148  // No connection to evict, disconnect the new connection
1149  LogPrint(BCLog::NET, "failed to find an eviction candidate - "
1150  "connection dropped (full)\n");
1151  CloseSocket(hSocket);
1152  return;
1153  }
1154  }
1155 
1156  NodeId id = GetNewNodeId();
1157  uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE)
1158  .Write(id)
1159  .Finalize();
1160  uint64_t extra_entropy =
1161  GetDeterministicRandomizer(RANDOMIZER_ID_EXTRAENTROPY)
1162  .Write(id)
1163  .Finalize();
1164  CAddress addr_bind = GetBindAddress(hSocket);
1165 
1166  ServiceFlags nodeServices = nLocalServices;
1167  if (NetPermissions::HasFlag(permissionFlags, PF_BLOOMFILTER)) {
1168  nodeServices = static_cast<ServiceFlags>(nodeServices | NODE_BLOOM);
1169  }
1170  CNode *pnode = new CNode(id, nodeServices, GetBestHeight(), hSocket, addr,
1171  CalculateKeyedNetGroup(addr), nonce, extra_entropy,
1172  addr_bind, "", ConnectionType::INBOUND);
1173  pnode->AddRef();
1174  pnode->m_permissionFlags = permissionFlags;
1175  // If this flag is present, the user probably expect that RPC and QT report
1176  // it as whitelisted (backward compatibility)
1177  pnode->m_legacyWhitelisted = legacyWhitelisted;
1178  pnode->m_prefer_evict = discouraged;
1179  m_msgproc->InitializeNode(*config, pnode);
1180 
1181  LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1182 
1183  {
1184  LOCK(cs_vNodes);
1185  vNodes.push_back(pnode);
1186  }
1187 
1188  // We received a new connection, harvest entropy from the time (and our peer
1189  // count)
1190  RandAddEvent(uint32_t(id));
1191 }
1192 
1194  {
1195  LOCK(cs_vNodes);
1196 
1197  if (!fNetworkActive) {
1198  // Disconnect any connected nodes
1199  for (CNode *pnode : vNodes) {
1200  if (!pnode->fDisconnect) {
1202  "Network not active, dropping peer=%d\n",
1203  pnode->GetId());
1204  pnode->fDisconnect = true;
1205  }
1206  }
1207  }
1208 
1209  // Disconnect unused nodes
1210  std::vector<CNode *> vNodesCopy = vNodes;
1211  for (CNode *pnode : vNodesCopy) {
1212  if (pnode->fDisconnect) {
1213  // remove from vNodes
1214  vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode),
1215  vNodes.end());
1216 
1217  // release outbound grant (if any)
1218  pnode->grantOutbound.Release();
1219 
1220  // close socket and cleanup
1221  pnode->CloseSocketDisconnect();
1222 
1223  // hold in disconnected pool until all refs are released
1224  pnode->Release();
1225  vNodesDisconnected.push_back(pnode);
1226  }
1227  }
1228  }
1229  {
1230  // Delete disconnected nodes
1231  std::list<CNode *> vNodesDisconnectedCopy = vNodesDisconnected;
1232  for (CNode *pnode : vNodesDisconnectedCopy) {
1233  // wait until threads are done using it
1234  if (pnode->GetRefCount() <= 0) {
1235  bool fDelete = false;
1236  {
1237  TRY_LOCK(pnode->cs_inventory, lockInv);
1238  if (lockInv) {
1239  TRY_LOCK(pnode->cs_vSend, lockSend);
1240  if (lockSend) {
1241  fDelete = true;
1242  }
1243  }
1244  }
1245  if (fDelete) {
1246  vNodesDisconnected.remove(pnode);
1247  DeleteNode(pnode);
1248  }
1249  }
1250  }
1251  }
1252 }
1253 
1255  size_t vNodesSize;
1256  {
1257  LOCK(cs_vNodes);
1258  vNodesSize = vNodes.size();
1259  }
1260  if (vNodesSize != nPrevNodeCount) {
1261  nPrevNodeCount = vNodesSize;
1262  if (clientInterface) {
1263  clientInterface->NotifyNumConnectionsChanged(vNodesSize);
1264  }
1265  }
1266 }
1267 
1269  int64_t nTime = GetSystemTimeInSeconds();
1270  if (nTime - pnode->nTimeConnected > m_peer_connect_timeout) {
1271  if (pnode->nLastRecv == 0 || pnode->nLastSend == 0) {
1273  "socket no message in first %i seconds, %d %d from %d\n",
1274  m_peer_connect_timeout, pnode->nLastRecv != 0,
1275  pnode->nLastSend != 0, pnode->GetId());
1276  pnode->fDisconnect = true;
1277  } else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL) {
1278  LogPrintf("socket sending timeout: %is\n",
1279  nTime - pnode->nLastSend);
1280  pnode->fDisconnect = true;
1281  } else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION
1283  : 90 * 60)) {
1284  LogPrintf("socket receive timeout: %is\n",
1285  nTime - pnode->nLastRecv);
1286  pnode->fDisconnect = true;
1287  } else if (pnode->nPingNonceSent &&
1288  pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 <
1289  GetTimeMicros()) {
1290  LogPrintf("ping timeout: %fs\n",
1291  0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
1292  pnode->fDisconnect = true;
1293  } else if (!pnode->fSuccessfullyConnected) {
1294  LogPrint(BCLog::NET, "version handshake timeout from %d\n",
1295  pnode->GetId());
1296  pnode->fDisconnect = true;
1297  }
1298  }
1299 }
1300 
1301 bool CConnman::GenerateSelectSet(std::set<SOCKET> &recv_set,
1302  std::set<SOCKET> &send_set,
1303  std::set<SOCKET> &error_set) {
1304  for (const ListenSocket &hListenSocket : vhListenSocket) {
1305  recv_set.insert(hListenSocket.socket);
1306  }
1307 
1308  {
1309  LOCK(cs_vNodes);
1310  for (CNode *pnode : vNodes) {
1311  // Implement the following logic:
1312  // * If there is data to send, select() for sending data. As this
1313  // only happens when optimistic write failed, we choose to first
1314  // drain the write buffer in this case before receiving more. This
1315  // avoids needlessly queueing received data, if the remote peer is
1316  // not themselves receiving data. This means properly utilizing
1317  // TCP flow control signalling.
1318  // * Otherwise, if there is space left in the receive buffer,
1319  // select() for receiving data.
1320  // * Hand off all complete messages to the processor, to be handled
1321  // without blocking here.
1322 
1323  bool select_recv = !pnode->fPauseRecv;
1324  bool select_send;
1325  {
1326  LOCK(pnode->cs_vSend);
1327  select_send = !pnode->vSendMsg.empty();
1328  }
1329 
1330  LOCK(pnode->cs_hSocket);
1331  if (pnode->hSocket == INVALID_SOCKET) {
1332  continue;
1333  }
1334 
1335  error_set.insert(pnode->hSocket);
1336  if (select_send) {
1337  send_set.insert(pnode->hSocket);
1338  continue;
1339  }
1340  if (select_recv) {
1341  recv_set.insert(pnode->hSocket);
1342  }
1343  }
1344  }
1345 
1346  return !recv_set.empty() || !send_set.empty() || !error_set.empty();
1347 }
1348 
1349 #ifdef USE_POLL
1350 void CConnman::SocketEvents(std::set<SOCKET> &recv_set,
1351  std::set<SOCKET> &send_set,
1352  std::set<SOCKET> &error_set) {
1353  std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
1354  if (!GenerateSelectSet(recv_select_set, send_select_set,
1355  error_select_set)) {
1357  std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS));
1358  return;
1359  }
1360 
1361  std::unordered_map<SOCKET, struct pollfd> pollfds;
1362  for (SOCKET socket_id : recv_select_set) {
1363  pollfds[socket_id].fd = socket_id;
1364  pollfds[socket_id].events |= POLLIN;
1365  }
1366 
1367  for (SOCKET socket_id : send_select_set) {
1368  pollfds[socket_id].fd = socket_id;
1369  pollfds[socket_id].events |= POLLOUT;
1370  }
1371 
1372  for (SOCKET socket_id : error_select_set) {
1373  pollfds[socket_id].fd = socket_id;
1374  // These flags are ignored, but we set them for clarity
1375  pollfds[socket_id].events |= POLLERR | POLLHUP;
1376  }
1377 
1378  std::vector<struct pollfd> vpollfds;
1379  vpollfds.reserve(pollfds.size());
1380  for (auto it : pollfds) {
1381  vpollfds.push_back(std::move(it.second));
1382  }
1383 
1384  if (poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS) <
1385  0) {
1386  return;
1387  }
1388 
1389  if (interruptNet) {
1390  return;
1391  }
1392 
1393  for (struct pollfd pollfd_entry : vpollfds) {
1394  if (pollfd_entry.revents & POLLIN) {
1395  recv_set.insert(pollfd_entry.fd);
1396  }
1397  if (pollfd_entry.revents & POLLOUT) {
1398  send_set.insert(pollfd_entry.fd);
1399  }
1400  if (pollfd_entry.revents & (POLLERR | POLLHUP)) {
1401  error_set.insert(pollfd_entry.fd);
1402  }
1403  }
1404 }
1405 #else
1406 void CConnman::SocketEvents(std::set<SOCKET> &recv_set,
1407  std::set<SOCKET> &send_set,
1408  std::set<SOCKET> &error_set) {
1409  std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
1410  if (!GenerateSelectSet(recv_select_set, send_select_set,
1411  error_select_set)) {
1413  std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS));
1414  return;
1415  }
1416 
1417  //
1418  // Find which sockets have data to receive
1419  //
1420  struct timeval timeout;
1421  timeout.tv_sec = 0;
1422  // frequency to poll pnode->vSend
1423  timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000;
1424 
1425  fd_set fdsetRecv;
1426  fd_set fdsetSend;
1427  fd_set fdsetError;
1428  FD_ZERO(&fdsetRecv);
1429  FD_ZERO(&fdsetSend);
1430  FD_ZERO(&fdsetError);
1431  SOCKET hSocketMax = 0;
1432 
1433  for (SOCKET hSocket : recv_select_set) {
1434  FD_SET(hSocket, &fdsetRecv);
1435  hSocketMax = std::max(hSocketMax, hSocket);
1436  }
1437 
1438  for (SOCKET hSocket : send_select_set) {
1439  FD_SET(hSocket, &fdsetSend);
1440  hSocketMax = std::max(hSocketMax, hSocket);
1441  }
1442 
1443  for (SOCKET hSocket : error_select_set) {
1444  FD_SET(hSocket, &fdsetError);
1445  hSocketMax = std::max(hSocketMax, hSocket);
1446  }
1447 
1448  int nSelect =
1449  select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1450 
1451  if (interruptNet) {
1452  return;
1453  }
1454 
1455  if (nSelect == SOCKET_ERROR) {
1456  int nErr = WSAGetLastError();
1457  LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1458  for (unsigned int i = 0; i <= hSocketMax; i++) {
1459  FD_SET(i, &fdsetRecv);
1460  }
1461  FD_ZERO(&fdsetSend);
1462  FD_ZERO(&fdsetError);
1463  if (!interruptNet.sleep_for(
1464  std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS))) {
1465  return;
1466  }
1467  }
1468 
1469  for (SOCKET hSocket : recv_select_set) {
1470  if (FD_ISSET(hSocket, &fdsetRecv)) {
1471  recv_set.insert(hSocket);
1472  }
1473  }
1474 
1475  for (SOCKET hSocket : send_select_set) {
1476  if (FD_ISSET(hSocket, &fdsetSend)) {
1477  send_set.insert(hSocket);
1478  }
1479  }
1480 
1481  for (SOCKET hSocket : error_select_set) {
1482  if (FD_ISSET(hSocket, &fdsetError)) {
1483  error_set.insert(hSocket);
1484  }
1485  }
1486 }
1487 #endif
1488 
1490  std::set<SOCKET> recv_set, send_set, error_set;
1491  SocketEvents(recv_set, send_set, error_set);
1492 
1493  if (interruptNet) {
1494  return;
1495  }
1496 
1497  //
1498  // Accept new connections
1499  //
1500  for (const ListenSocket &hListenSocket : vhListenSocket) {
1501  if (hListenSocket.socket != INVALID_SOCKET &&
1502  recv_set.count(hListenSocket.socket) > 0) {
1503  AcceptConnection(hListenSocket);
1504  }
1505  }
1506 
1507  //
1508  // Service each socket
1509  //
1510  std::vector<CNode *> vNodesCopy;
1511  {
1512  LOCK(cs_vNodes);
1513  vNodesCopy = vNodes;
1514  for (CNode *pnode : vNodesCopy) {
1515  pnode->AddRef();
1516  }
1517  }
1518  for (CNode *pnode : vNodesCopy) {
1519  if (interruptNet) {
1520  return;
1521  }
1522 
1523  //
1524  // Receive
1525  //
1526  bool recvSet = false;
1527  bool sendSet = false;
1528  bool errorSet = false;
1529  {
1530  LOCK(pnode->cs_hSocket);
1531  if (pnode->hSocket == INVALID_SOCKET) {
1532  continue;
1533  }
1534  recvSet = recv_set.count(pnode->hSocket) > 0;
1535  sendSet = send_set.count(pnode->hSocket) > 0;
1536  errorSet = error_set.count(pnode->hSocket) > 0;
1537  }
1538  if (recvSet || errorSet) {
1539  // typical socket buffer is 8K-64K
1540  char pchBuf[0x10000];
1541  int32_t nBytes = 0;
1542  {
1543  LOCK(pnode->cs_hSocket);
1544  if (pnode->hSocket == INVALID_SOCKET) {
1545  continue;
1546  }
1547  nBytes =
1548  recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1549  }
1550  if (nBytes > 0) {
1551  bool notify = false;
1552  if (!pnode->ReceiveMsgBytes(*config, pchBuf, nBytes, notify)) {
1553  pnode->CloseSocketDisconnect();
1554  }
1555  RecordBytesRecv(nBytes);
1556  if (notify) {
1557  size_t nSizeAdded = 0;
1558  auto it(pnode->vRecvMsg.begin());
1559  for (; it != pnode->vRecvMsg.end(); ++it) {
1560  // vRecvMsg contains only completed CNetMessage
1561  // the single possible partially deserialized message
1562  // are held by TransportDeserializer
1563  nSizeAdded += it->m_raw_message_size;
1564  }
1565  {
1566  LOCK(pnode->cs_vProcessMsg);
1567  pnode->vProcessMsg.splice(pnode->vProcessMsg.end(),
1568  pnode->vRecvMsg,
1569  pnode->vRecvMsg.begin(), it);
1570  pnode->nProcessQueueSize += nSizeAdded;
1571  pnode->fPauseRecv =
1572  pnode->nProcessQueueSize > nReceiveFloodSize;
1573  }
1575  }
1576  } else if (nBytes == 0) {
1577  // socket closed gracefully
1578  if (!pnode->fDisconnect) {
1579  LogPrint(BCLog::NET, "socket closed for peer=%d\n",
1580  pnode->GetId());
1581  }
1582  pnode->CloseSocketDisconnect();
1583  } else if (nBytes < 0) {
1584  // error
1585  int nErr = WSAGetLastError();
1586  if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE &&
1587  nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
1588  if (!pnode->fDisconnect) {
1590  "socket recv error for peer=%d: %s\n",
1591  pnode->GetId(), NetworkErrorString(nErr));
1592  }
1593  pnode->CloseSocketDisconnect();
1594  }
1595  }
1596  }
1597 
1598  //
1599  // Send
1600  //
1601  if (sendSet) {
1602  LOCK(pnode->cs_vSend);
1603  size_t nBytes = SocketSendData(pnode);
1604  if (nBytes) {
1605  RecordBytesSent(nBytes);
1606  }
1607  }
1608 
1609  InactivityCheck(pnode);
1610  }
1611  {
1612  LOCK(cs_vNodes);
1613  for (CNode *pnode : vNodesCopy) {
1614  pnode->Release();
1615  }
1616  }
1617 }
1618 
1620  while (!interruptNet) {
1621  DisconnectNodes();
1623  SocketHandler();
1624  }
1625 }
1626 
1628  {
1629  LOCK(mutexMsgProc);
1630  fMsgProcWake = true;
1631  }
1632  condMsgProc.notify_one();
1633 }
1634 
1635 #ifdef USE_UPNP
1636 static CThreadInterrupt g_upnp_interrupt;
1637 static std::thread g_upnp_thread;
1638 static void ThreadMapPort() {
1639  std::string port = strprintf("%u", GetListenPort());
1640  const char *multicastif = nullptr;
1641  const char *minissdpdpath = nullptr;
1642  struct UPNPDev *devlist = nullptr;
1643  char lanaddr[64];
1644 
1645  int error = 0;
1646 #if MINIUPNPC_API_VERSION < 14
1647  devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1648 #else
1649  devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1650 #endif
1651 
1652  struct UPNPUrls urls;
1653  struct IGDdatas data;
1654  int r;
1655 
1656  r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1657  if (r == 1) {
1658  if (fDiscover) {
1659  char externalIPAddress[40];
1660  r = UPNP_GetExternalIPAddress(
1661  urls.controlURL, data.first.servicetype, externalIPAddress);
1662  if (r != UPNPCOMMAND_SUCCESS) {
1663  LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1664  } else {
1665  if (externalIPAddress[0]) {
1666  CNetAddr resolved;
1667  if (LookupHost(externalIPAddress, resolved, false)) {
1668  LogPrintf("UPnP: ExternalIPAddress = %s\n",
1669  resolved.ToString());
1670  AddLocal(resolved, LOCAL_UPNP);
1671  }
1672  } else {
1673  LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1674  }
1675  }
1676  }
1677 
1678  std::string strDesc = PACKAGE_NAME " " + FormatFullVersion();
1679 
1680  do {
1681  r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1682  port.c_str(), port.c_str(), lanaddr,
1683  strDesc.c_str(), "TCP", 0, "0");
1684 
1685  if (r != UPNPCOMMAND_SUCCESS) {
1686  LogPrintf(
1687  "AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1688  port, port, lanaddr, r, strupnperror(r));
1689  } else {
1690  LogPrintf("UPnP Port Mapping successful.\n");
1691  }
1692  } while (g_upnp_interrupt.sleep_for(std::chrono::minutes(20)));
1693 
1694  r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype,
1695  port.c_str(), "TCP", 0);
1696  LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1697  freeUPNPDevlist(devlist);
1698  devlist = nullptr;
1699  FreeUPNPUrls(&urls);
1700  } else {
1701  LogPrintf("No valid UPnP IGDs found\n");
1702  freeUPNPDevlist(devlist);
1703  devlist = nullptr;
1704  if (r != 0) {
1705  FreeUPNPUrls(&urls);
1706  }
1707  }
1708 }
1709 
1710 void StartMapPort() {
1711  if (!g_upnp_thread.joinable()) {
1712  assert(!g_upnp_interrupt);
1713  g_upnp_thread = std::thread(
1714  (std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
1715  }
1716 }
1717 
1718 void InterruptMapPort() {
1719  if (g_upnp_thread.joinable()) {
1720  g_upnp_interrupt();
1721  }
1722 }
1723 
1724 void StopMapPort() {
1725  if (g_upnp_thread.joinable()) {
1726  g_upnp_thread.join();
1727  g_upnp_interrupt.reset();
1728  }
1729 }
1730 
1731 #else
1733  // Intentionally left blank.
1734 }
1736  // Intentionally left blank.
1737 }
1738 void StopMapPort() {
1739  // Intentionally left blank.
1740 }
1741 #endif
1742 
1744  FastRandomContext rng;
1745  std::vector<std::string> seeds = config->GetChainParams().DNSSeeds();
1746  Shuffle(seeds.begin(), seeds.end(), rng);
1747  // Number of seeds left before testing if we have enough connections
1748  int seeds_right_now = 0;
1749  int found = 0;
1750 
1751  if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) {
1752  // When -forcednsseed is provided, query all.
1753  seeds_right_now = seeds.size();
1754  } else if (addrman.size() == 0) {
1755  // If we have no known peers, query all.
1756  // This will occur on the first run, or if peers.dat has been
1757  // deleted.
1758  seeds_right_now = seeds.size();
1759  }
1760 
1761  // goal: only query DNS seed if address need is acute
1762  // * If we have a reasonable number of peers in addrman, spend
1763  // some time trying them first. This improves user privacy by
1764  // creating fewer identifying DNS requests, reduces trust by
1765  // giving seeds less influence on the network topology, and
1766  // reduces traffic to the seeds.
1767  // * When querying DNS seeds query a few at once, this ensures
1768  // that we don't give DNS seeds the ability to eclipse nodes
1769  // that query them.
1770  // * If we continue having problems, eventually query all the
1771  // DNS seeds, and if that fails too, also try the fixed seeds.
1772  // (done in ThreadOpenConnections)
1773  const std::chrono::seconds seeds_wait_time =
1777 
1778  for (const std::string &seed : seeds) {
1779  if (seeds_right_now == 0) {
1780  seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
1781 
1782  if (addrman.size() > 0) {
1783  LogPrintf("Waiting %d seconds before querying DNS seeds.\n",
1784  seeds_wait_time.count());
1785  std::chrono::seconds to_wait = seeds_wait_time;
1786  while (to_wait.count() > 0) {
1787  // if sleeping for the MANY_PEERS interval, wake up
1788  // early to see if we have enough peers and can stop
1789  // this thread entirely freeing up its resources
1790  std::chrono::seconds w =
1791  std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait);
1792  if (!interruptNet.sleep_for(w)) {
1793  return;
1794  }
1795  to_wait -= w;
1796 
1797  int nRelevant = 0;
1798  {
1799  LOCK(cs_vNodes);
1800  for (const CNode *pnode : vNodes) {
1801  if (pnode->fSuccessfullyConnected &&
1802  pnode->IsOutboundOrBlockRelayConn()) {
1803  ++nRelevant;
1804  }
1805  }
1806  }
1807  if (nRelevant >= 2) {
1808  if (found > 0) {
1809  LogPrintf("%d addresses found from DNS seeds\n",
1810  found);
1811  LogPrintf(
1812  "P2P peers available. Finished DNS seeding.\n");
1813  } else {
1814  LogPrintf(
1815  "P2P peers available. Skipped DNS seeding.\n");
1816  }
1817  return;
1818  }
1819  }
1820  }
1821  }
1822 
1823  if (interruptNet) {
1824  return;
1825  }
1826 
1827  // hold off on querying seeds if P2P network deactivated
1828  if (!fNetworkActive) {
1829  LogPrintf("Waiting for network to be reactivated before querying "
1830  "DNS seeds.\n");
1831  do {
1832  if (!interruptNet.sleep_for(std::chrono::seconds{1})) {
1833  return;
1834  }
1835  } while (!fNetworkActive);
1836  }
1837 
1838  LogPrintf("Loading addresses from DNS seed %s\n", seed);
1839  if (HaveNameProxy()) {
1840  AddAddrFetch(seed);
1841  } else {
1842  std::vector<CNetAddr> vIPs;
1843  std::vector<CAddress> vAdd;
1844  ServiceFlags requiredServiceBits =
1846  std::string host = strprintf("x%x.%s", requiredServiceBits, seed);
1847  CNetAddr resolveSource;
1848  if (!resolveSource.SetInternal(host)) {
1849  continue;
1850  }
1851 
1852  // Limits number of IPs learned from a DNS seed
1853  unsigned int nMaxIPs = 256;
1854  if (LookupHost(host, vIPs, nMaxIPs, true)) {
1855  for (const CNetAddr &ip : vIPs) {
1856  int nOneDay = 24 * 3600;
1857  CAddress addr = CAddress(
1859  requiredServiceBits);
1860  // Use a random age between 3 and 7 days old.
1861  addr.nTime =
1862  GetTime() - 3 * nOneDay - rng.randrange(4 * nOneDay);
1863  vAdd.push_back(addr);
1864  found++;
1865  }
1866  addrman.Add(vAdd, resolveSource);
1867  } else {
1868  // We now avoid directly using results from DNS Seeds which do
1869  // not support service bit filtering, instead using them as a
1870  // addrfetch to get nodes with our desired service bits.
1871  AddAddrFetch(seed);
1872  }
1873  }
1874  --seeds_right_now;
1875  }
1876  LogPrintf("%d addresses found from DNS seeds\n", found);
1877 }
1878 
1880  int64_t nStart = GetTimeMillis();
1881 
1882  CAddrDB adb(config->GetChainParams());
1883  adb.Write(addrman);
1884 
1885  LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
1886  addrman.size(), GetTimeMillis() - nStart);
1887 }
1888 
1890  std::string strDest;
1891  {
1893  if (m_addr_fetches.empty()) {
1894  return;
1895  }
1896  strDest = m_addr_fetches.front();
1897  m_addr_fetches.pop_front();
1898  }
1899  CAddress addr;
1900  CSemaphoreGrant grant(*semOutbound, true);
1901  if (grant) {
1902  OpenNetworkConnection(addr, false, &grant, strDest.c_str(),
1904  }
1905 }
1906 
1909 }
1910 
1913  LogPrint(BCLog::NET, "net: setting try another outbound peer=%s\n",
1914  flag ? "true" : "false");
1915 }
1916 
1917 // Return the number of peers we have over our outbound connection limit.
1918 // Exclude peers that are marked for disconnect, or are going to be disconnected
1919 // soon (eg one-shots and feelers).
1920 // Also exclude peers that haven't finished initial connection handshake yet (so
1921 // that we don't decide we're over our desired connection limit, and then evict
1922 // some peer that has finished the handshake).
1924  int nOutbound = 0;
1925  {
1926  LOCK(cs_vNodes);
1927  for (const CNode *pnode : vNodes) {
1928  if (pnode->fSuccessfullyConnected && !pnode->fDisconnect &&
1929  pnode->IsOutboundOrBlockRelayConn()) {
1930  ++nOutbound;
1931  }
1932  }
1933  }
1934  return std::max(
1936 }
1937 
1938 void CConnman::ThreadOpenConnections(const std::vector<std::string> connect) {
1939  // Connect to specific addresses
1940  if (!connect.empty()) {
1941  for (int64_t nLoop = 0;; nLoop++) {
1942  ProcessAddrFetch();
1943  for (const std::string &strAddr : connect) {
1944  CAddress addr(CService(), NODE_NONE);
1945  OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(),
1947  for (int i = 0; i < 10 && i < nLoop; i++) {
1948  if (!interruptNet.sleep_for(
1949  std::chrono::milliseconds(500))) {
1950  return;
1951  }
1952  }
1953  }
1954  if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) {
1955  return;
1956  }
1957  }
1958  }
1959 
1960  // Initiate network connections
1961  int64_t nStart = GetTime();
1962 
1963  // Minimum time before next feeler connection (in microseconds).
1964  int64_t nNextFeeler =
1965  PoissonNextSend(nStart * 1000 * 1000, FEELER_INTERVAL);
1966  while (!interruptNet) {
1967  ProcessAddrFetch();
1968 
1969  if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) {
1970  return;
1971  }
1972 
1973  CSemaphoreGrant grant(*semOutbound);
1974  if (interruptNet) {
1975  return;
1976  }
1977 
1978  // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1979  // Note that we only do this if we started with an empty peers.dat,
1980  // (in which case we will query DNS seeds immediately) *and* the DNS
1981  // seeds have not returned any results.
1982  if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1983  static bool done = false;
1984  if (!done) {
1985  LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be "
1986  "available.\n");
1987  CNetAddr local;
1988  local.SetInternal("fixedseeds");
1990  local);
1991  done = true;
1992  }
1993  }
1994 
1995  //
1996  // Choose an address to connect to based on most recently seen
1997  //
1998  CAddress addrConnect;
1999 
2000  // Only connect out to one peer per network group (/16 for IPv4).
2001  int nOutboundFullRelay = 0;
2002  int nOutboundBlockRelay = 0;
2003  std::set<std::vector<uint8_t>> setConnected;
2004 
2005  {
2006  LOCK(cs_vNodes);
2007  for (const CNode *pnode : vNodes) {
2008  if (pnode->IsFullOutboundConn()) {
2009  nOutboundFullRelay++;
2010  }
2011  if (pnode->IsBlockOnlyConn()) {
2012  nOutboundBlockRelay++;
2013  }
2014 
2015  // Netgroups for inbound and manual peers are not excluded
2016  // because our goal here is to not use multiple of our
2017  // limited outbound slots on a single netgroup but inbound
2018  // and manual peers do not use our outbound slots. Inbound
2019  // peers also have the added issue that they could be attacker
2020  // controlled and could be used to prevent us from connecting
2021  // to particular hosts if we used them here.
2022  switch (pnode->m_conn_type) {
2025  break;
2030  setConnected.insert(
2031  pnode->addr.GetGroup(addrman.m_asmap));
2032  }
2033  }
2034  }
2035 
2036  // Feeler Connections
2037  //
2038  // Design goals:
2039  // * Increase the number of connectable addresses in the tried table.
2040  //
2041  // Method:
2042  // * Choose a random address from new and attempt to connect to it if
2043  // we can connect successfully it is added to tried.
2044  // * Start attempting feeler connections only after node finishes
2045  // making outbound connections.
2046  // * Only make a feeler connection once every few minutes.
2047  //
2048  bool fFeeler = false;
2049 
2050  if (nOutboundFullRelay >= m_max_outbound_full_relay &&
2051  nOutboundBlockRelay >= m_max_outbound_block_relay &&
2052  !GetTryNewOutboundPeer()) {
2053  // The current time right now (in microseconds).
2054  int64_t nTime = GetTimeMicros();
2055  if (nTime > nNextFeeler) {
2056  nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
2057  fFeeler = true;
2058  } else {
2059  continue;
2060  }
2061  }
2062 
2064 
2065  int64_t nANow = GetAdjustedTime();
2066  int nTries = 0;
2067  while (!interruptNet) {
2069 
2070  // SelectTriedCollision returns an invalid address if it is empty.
2071  if (!fFeeler || !addr.IsValid()) {
2072  addr = addrman.Select(fFeeler);
2073  }
2074 
2075  // Require outbound connections, other than feelers, to be to
2076  // distinct network groups
2077  if (!fFeeler &&
2078  setConnected.count(addr.GetGroup(addrman.m_asmap))) {
2079  break;
2080  }
2081 
2082  // if we selected an invalid or local address, restart
2083  if (!addr.IsValid() || IsLocal(addr)) {
2084  break;
2085  }
2086 
2087  // If we didn't find an appropriate destination after trying 100
2088  // addresses fetched from addrman, stop this loop, and let the outer
2089  // loop run again (which sleeps, adds seed nodes, recalculates
2090  // already-connected network ranges, ...) before trying new addrman
2091  // addresses.
2092  nTries++;
2093  if (nTries > 100) {
2094  break;
2095  }
2096 
2097  if (!IsReachable(addr)) {
2098  continue;
2099  }
2100 
2101  // only consider very recently tried nodes after 30 failed attempts
2102  if (nANow - addr.nLastTry < 600 && nTries < 30) {
2103  continue;
2104  }
2105 
2106  // for non-feelers, require all the services we'll want,
2107  // for feelers, only require they be a full node (only because most
2108  // SPV clients don't have a good address DB available)
2109  if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
2110  continue;
2111  }
2112 
2113  if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
2114  continue;
2115  }
2116 
2117  // do not allow non-default ports, unless after 50 invalid addresses
2118  // selected already.
2119  if (addr.GetPort() != config->GetChainParams().GetDefaultPort() &&
2120  nTries < 50) {
2121  continue;
2122  }
2123 
2124  addrConnect = addr;
2125  break;
2126  }
2127 
2128  if (addrConnect.IsValid()) {
2129  if (fFeeler) {
2130  // Add small amount of random noise before connection to avoid
2131  // synchronization.
2132  int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
2133  if (!interruptNet.sleep_for(
2134  std::chrono::milliseconds(randsleep))) {
2135  return;
2136  }
2137  LogPrint(BCLog::NET, "Making feeler connection to %s\n",
2138  addrConnect.ToString());
2139  }
2140 
2141  ConnectionType conn_type;
2142  // Determine what type of connection to open. If fFeeler is not
2143  // set, open OUTBOUND connections until we meet our full-relay
2144  // capacity. Then open BLOCK_RELAY connections until we hit our
2145  // block-relay peer limit. Otherwise, default to opening an
2146  // OUTBOUND connection.
2147  if (fFeeler) {
2148  conn_type = ConnectionType::FEELER;
2149  } else if (nOutboundFullRelay < m_max_outbound_full_relay) {
2150  conn_type = ConnectionType::OUTBOUND;
2151  } else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
2152  conn_type = ConnectionType::BLOCK_RELAY;
2153  } else {
2154  // GetTryNewOutboundPeer() is true
2155  conn_type = ConnectionType::OUTBOUND;
2156  }
2157 
2158  OpenNetworkConnection(addrConnect,
2159  int(setConnected.size()) >=
2160  std::min(nMaxConnections - 1, 2),
2161  &grant, nullptr, conn_type);
2162  }
2163  }
2164 }
2165 
2166 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() {
2167  std::vector<AddedNodeInfo> ret;
2168 
2169  std::list<std::string> lAddresses(0);
2170  {
2172  ret.reserve(vAddedNodes.size());
2173  std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(),
2174  std::back_inserter(lAddresses));
2175  }
2176 
2177  // Build a map of all already connected addresses (by IP:port and by name)
2178  // to inbound/outbound and resolved CService
2179  std::map<CService, bool> mapConnected;
2180  std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
2181  {
2182  LOCK(cs_vNodes);
2183  for (const CNode *pnode : vNodes) {
2184  if (pnode->addr.IsValid()) {
2185  mapConnected[pnode->addr] = pnode->IsInboundConn();
2186  }
2187  std::string addrName = pnode->GetAddrName();
2188  if (!addrName.empty()) {
2189  mapConnectedByName[std::move(addrName)] =
2190  std::make_pair(pnode->IsInboundConn(),
2191  static_cast<const CService &>(pnode->addr));
2192  }
2193  }
2194  }
2195 
2196  for (const std::string &strAddNode : lAddresses) {
2197  CService service(LookupNumeric(strAddNode, Params().GetDefaultPort()));
2198  AddedNodeInfo addedNode{strAddNode, CService(), false, false};
2199  if (service.IsValid()) {
2200  // strAddNode is an IP:port
2201  auto it = mapConnected.find(service);
2202  if (it != mapConnected.end()) {
2203  addedNode.resolvedAddress = service;
2204  addedNode.fConnected = true;
2205  addedNode.fInbound = it->second;
2206  }
2207  } else {
2208  // strAddNode is a name
2209  auto it = mapConnectedByName.find(strAddNode);
2210  if (it != mapConnectedByName.end()) {
2211  addedNode.resolvedAddress = it->second.second;
2212  addedNode.fConnected = true;
2213  addedNode.fInbound = it->second.first;
2214  }
2215  }
2216  ret.emplace_back(std::move(addedNode));
2217  }
2218 
2219  return ret;
2220 }
2221 
2223  while (true) {
2224  CSemaphoreGrant grant(*semAddnode);
2225  std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
2226  bool tried = false;
2227  for (const AddedNodeInfo &info : vInfo) {
2228  if (!info.fConnected) {
2229  if (!grant.TryAcquire()) {
2230  // If we've used up our semaphore and need a new one, let's
2231  // not wait here since while we are waiting the
2232  // addednodeinfo state might change.
2233  break;
2234  }
2235  tried = true;
2236  CAddress addr(CService(), NODE_NONE);
2237  OpenNetworkConnection(addr, false, &grant,
2238  info.strAddedNode.c_str(),
2240  if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) {
2241  return;
2242  }
2243  }
2244  }
2245  // Retry every 60 seconds if a connection was attempted, otherwise two
2246  // seconds.
2247  if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2))) {
2248  return;
2249  }
2250  }
2251 }
2252 
2253 // If successful, this moves the passed grant to the constructed node.
2255  bool fCountFailure,
2256  CSemaphoreGrant *grantOutbound,
2257  const char *pszDest,
2258  ConnectionType conn_type) {
2259  assert(conn_type != ConnectionType::INBOUND);
2260 
2261  //
2262  // Initiate outbound network connection
2263  //
2264  if (interruptNet) {
2265  return;
2266  }
2267  if (!fNetworkActive) {
2268  return;
2269  }
2270  if (!pszDest) {
2271  bool banned_or_discouraged =
2272  m_banman && (m_banman->IsDiscouraged(addrConnect) ||
2273  m_banman->IsBanned(addrConnect));
2274  if (IsLocal(addrConnect) ||
2275  FindNode(static_cast<CNetAddr>(addrConnect)) ||
2276  banned_or_discouraged || FindNode(addrConnect.ToStringIPPort())) {
2277  return;
2278  }
2279  } else if (FindNode(std::string(pszDest))) {
2280  return;
2281  }
2282 
2283  CNode *pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type);
2284 
2285  if (!pnode) {
2286  return;
2287  }
2288  if (grantOutbound) {
2289  grantOutbound->MoveTo(pnode->grantOutbound);
2290  }
2291 
2292  m_msgproc->InitializeNode(*config, pnode);
2293  {
2294  LOCK(cs_vNodes);
2295  vNodes.push_back(pnode);
2296  }
2297 }
2298 
2300  while (!flagInterruptMsgProc) {
2301  std::vector<CNode *> vNodesCopy;
2302  {
2303  LOCK(cs_vNodes);
2304  vNodesCopy = vNodes;
2305  for (CNode *pnode : vNodesCopy) {
2306  pnode->AddRef();
2307  }
2308  }
2309 
2310  bool fMoreWork = false;
2311 
2312  for (CNode *pnode : vNodesCopy) {
2313  if (pnode->fDisconnect) {
2314  continue;
2315  }
2316 
2317  // Receive messages
2318  bool fMoreNodeWork = m_msgproc->ProcessMessages(
2319  *config, pnode, flagInterruptMsgProc);
2320  fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
2321  if (flagInterruptMsgProc) {
2322  return;
2323  }
2324 
2325  // Send messages
2326  {
2327  LOCK(pnode->cs_sendProcessing);
2329  }
2330 
2331  if (flagInterruptMsgProc) {
2332  return;
2333  }
2334  }
2335 
2336  {
2337  LOCK(cs_vNodes);
2338  for (CNode *pnode : vNodesCopy) {
2339  pnode->Release();
2340  }
2341  }
2342 
2343  WAIT_LOCK(mutexMsgProc, lock);
2344  if (!fMoreWork) {
2345  condMsgProc.wait_until(lock,
2346  std::chrono::steady_clock::now() +
2347  std::chrono::milliseconds(100),
2348  [this]() EXCLUSIVE_LOCKS_REQUIRED(
2349  mutexMsgProc) { return fMsgProcWake; });
2350  }
2351  fMsgProcWake = false;
2352  }
2353 }
2354 
2355 bool CConnman::BindListenPort(const CService &addrBind, bilingual_str &strError,
2356  NetPermissionFlags permissions) {
2357  int nOne = 1;
2358 
2359  // Create socket for listening for incoming connections
2360  struct sockaddr_storage sockaddr;
2361  socklen_t len = sizeof(sockaddr);
2362  if (!addrBind.GetSockAddr((struct sockaddr *)&sockaddr, &len)) {
2363  strError = strprintf(
2364  Untranslated("Error: Bind address family for %s not supported"),
2365  addrBind.ToString());
2366  LogPrintf("%s\n", strError.original);
2367  return false;
2368  }
2369 
2370  SOCKET hListenSocket = CreateSocket(addrBind);
2371  if (hListenSocket == INVALID_SOCKET) {
2372  strError =
2373  strprintf(Untranslated("Error: Couldn't open socket for incoming "
2374  "connections (socket returned error %s)"),
2376  LogPrintf("%s\n", strError.original);
2377  return false;
2378  }
2379 
2380  // Allow binding if the port is still in TIME_WAIT state after
2381  // the program was closed and restarted.
2382  setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne,
2383  sizeof(int));
2384 
2385  // Some systems don't have IPV6_V6ONLY but are always v6only; others do have
2386  // the option and enable it by default or not. Try to enable it, if
2387  // possible.
2388  if (addrBind.IsIPv6()) {
2389 #ifdef IPV6_V6ONLY
2390  setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY,
2391  (sockopt_arg_type)&nOne, sizeof(int));
2392 #endif
2393 #ifdef WIN32
2394  int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2395  setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
2396  (sockopt_arg_type)&nProtLevel, sizeof(int));
2397 #endif
2398  }
2399 
2400  if (::bind(hListenSocket, (struct sockaddr *)&sockaddr, len) ==
2401  SOCKET_ERROR) {
2402  int nErr = WSAGetLastError();
2403  if (nErr == WSAEADDRINUSE) {
2404  strError = strprintf(_("Unable to bind to %s on this computer. %s "
2405  "is probably already running."),
2406  addrBind.ToString(), PACKAGE_NAME);
2407  } else {
2408  strError = strprintf(_("Unable to bind to %s on this computer "
2409  "(bind returned error %s)"),
2410  addrBind.ToString(), NetworkErrorString(nErr));
2411  }
2412  LogPrintf("%s\n", strError.original);
2413  CloseSocket(hListenSocket);
2414  return false;
2415  }
2416  LogPrintf("Bound to %s\n", addrBind.ToString());
2417 
2418  // Listen for incoming connections
2419  if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR) {
2420  strError = strprintf(_("Error: Listening for incoming connections "
2421  "failed (listen returned error %s)"),
2423  LogPrintf("%s\n", strError.original);
2424  CloseSocket(hListenSocket);
2425  return false;
2426  }
2427 
2428  vhListenSocket.push_back(ListenSocket(hListenSocket, permissions));
2429 
2430  if (addrBind.IsRoutable() && fDiscover && (permissions & PF_NOBAN) == 0) {
2431  AddLocal(addrBind, LOCAL_BIND);
2432  }
2433 
2434  return true;
2435 }
2436 
2437 void Discover() {
2438  if (!fDiscover) {
2439  return;
2440  }
2441 
2442 #ifdef WIN32
2443  // Get local host IP
2444  char pszHostName[256] = "";
2445  if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) {
2446  std::vector<CNetAddr> vaddr;
2447  if (LookupHost(pszHostName, vaddr, 0, true)) {
2448  for (const CNetAddr &addr : vaddr) {
2449  if (AddLocal(addr, LOCAL_IF)) {
2450  LogPrintf("%s: %s - %s\n", __func__, pszHostName,
2451  addr.ToString());
2452  }
2453  }
2454  }
2455  }
2456 #elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)
2457  // Get local host ip
2458  struct ifaddrs *myaddrs;
2459  if (getifaddrs(&myaddrs) == 0) {
2460  for (struct ifaddrs *ifa = myaddrs; ifa != nullptr;
2461  ifa = ifa->ifa_next) {
2462  if (ifa->ifa_addr == nullptr || (ifa->ifa_flags & IFF_UP) == 0 ||
2463  strcmp(ifa->ifa_name, "lo") == 0 ||
2464  strcmp(ifa->ifa_name, "lo0") == 0) {
2465  continue;
2466  }
2467  if (ifa->ifa_addr->sa_family == AF_INET) {
2468  struct sockaddr_in *s4 =
2469  reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
2470  CNetAddr addr(s4->sin_addr);
2471  if (AddLocal(addr, LOCAL_IF)) {
2472  LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name,
2473  addr.ToString());
2474  }
2475  } else if (ifa->ifa_addr->sa_family == AF_INET6) {
2476  struct sockaddr_in6 *s6 =
2477  reinterpret_cast<struct sockaddr_in6 *>(ifa->ifa_addr);
2478  CNetAddr addr(s6->sin6_addr);
2479  if (AddLocal(addr, LOCAL_IF)) {
2480  LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name,
2481  addr.ToString());
2482  }
2483  }
2484  }
2485  freeifaddrs(myaddrs);
2486  }
2487 #endif
2488 }
2489 
2490 void CConnman::SetNetworkActive(bool active) {
2491  LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2492 
2493  if (fNetworkActive == active) {
2494  return;
2495  }
2496 
2497  fNetworkActive = active;
2498  uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2499 }
2500 
2501 CConnman::CConnman(const Config &configIn, uint64_t nSeed0In, uint64_t nSeed1In)
2502  : config(&configIn), nSeed0(nSeed0In), nSeed1(nSeed1In) {
2503  SetTryNewOutboundPeer(false);
2504 
2505  Options connOptions;
2506  Init(connOptions);
2507 }
2508 
2510  return nLastNodeId.fetch_add(1);
2511 }
2512 
2513 bool CConnman::Bind(const CService &addr, unsigned int flags,
2514  NetPermissionFlags permissions) {
2515  if (!(flags & BF_EXPLICIT) && !IsReachable(addr)) {
2516  return false;
2517  }
2518  bilingual_str strError;
2519  if (!BindListenPort(addr, strError, permissions)) {
2520  if ((flags & BF_REPORT_ERROR) && clientInterface) {
2521  clientInterface->ThreadSafeMessageBox(
2522  strError, "", CClientUIInterface::MSG_ERROR);
2523  }
2524  return false;
2525  }
2526  return true;
2527 }
2528 
2530  const std::vector<CService> &binds,
2531  const std::vector<NetWhitebindPermissions> &whiteBinds) {
2532  bool fBound = false;
2533  for (const auto &addrBind : binds) {
2534  fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR),
2536  }
2537  for (const auto &addrBind : whiteBinds) {
2538  fBound |= Bind(addrBind.m_service, (BF_EXPLICIT | BF_REPORT_ERROR),
2539  addrBind.m_flags);
2540  }
2541  if (binds.empty() && whiteBinds.empty()) {
2542  struct in_addr inaddr_any;
2543  inaddr_any.s_addr = INADDR_ANY;
2544  struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
2545  fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE,
2547  fBound |= Bind(CService(inaddr_any, GetListenPort()),
2548  !fBound ? BF_REPORT_ERROR : BF_NONE,
2550  }
2551  return fBound;
2552 }
2553 
2554 bool CConnman::Start(CScheduler &scheduler, const Options &connOptions) {
2555  Init(connOptions);
2556 
2557  {
2559  nTotalBytesRecv = 0;
2560  }
2561  {
2563  nTotalBytesSent = 0;
2564  nMaxOutboundTotalBytesSentInCycle = 0;
2565  nMaxOutboundCycleStartTime = 0;
2566  }
2567 
2568  if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2569  if (clientInterface) {
2570  clientInterface->ThreadSafeMessageBox(
2571  _("Failed to listen on any port. Use -listen=0 if you want "
2572  "this."),
2574  }
2575  return false;
2576  }
2577 
2578  for (const auto &strDest : connOptions.vSeedNodes) {
2579  AddAddrFetch(strDest);
2580  }
2581 
2582  if (clientInterface) {
2583  clientInterface->InitMessage(_("Loading P2P addresses...").translated);
2584  }
2585  // Load addresses from peers.dat
2586  int64_t nStart = GetTimeMillis();
2587  {
2588  CAddrDB adb(config->GetChainParams());
2589  if (adb.Read(addrman)) {
2590  LogPrintf("Loaded %i addresses from peers.dat %dms\n",
2591  addrman.size(), GetTimeMillis() - nStart);
2592  } else {
2593  // Addrman can be in an inconsistent state after failure, reset it
2594  addrman.Clear();
2595  LogPrintf("Invalid or missing peers.dat; recreating\n");
2596  DumpAddresses();
2597  }
2598  }
2599 
2600  uiInterface.InitMessage(_("Starting network threads...").translated);
2601 
2602  fAddressesInitialized = true;
2603 
2604  if (semOutbound == nullptr) {
2605  // initialize semaphore
2606  semOutbound = std::make_unique<CSemaphore>(
2607  std::min(m_max_outbound, nMaxConnections));
2608  }
2609  if (semAddnode == nullptr) {
2610  // initialize semaphore
2611  semAddnode = std::make_unique<CSemaphore>(nMaxAddnode);
2612  }
2613 
2614  //
2615  // Start threads
2616  //
2617  assert(m_msgproc);
2618  InterruptSocks5(false);
2619  interruptNet.reset();
2620  flagInterruptMsgProc = false;
2621 
2622  {
2623  LOCK(mutexMsgProc);
2624  fMsgProcWake = false;
2625  }
2626 
2627  // Send and receive from sockets, accept connections
2628  threadSocketHandler = std::thread(
2629  &TraceThread<std::function<void()>>, "net",
2630  std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2631 
2632  if (!gArgs.GetBoolArg("-dnsseed", true)) {
2633  LogPrintf("DNS seeding disabled\n");
2634  } else {
2636  std::thread(&TraceThread<std::function<void()>>, "dnsseed",
2637  std::function<void()>(
2638  std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2639  }
2640 
2641  // Initiate manual connections
2643  std::thread(&TraceThread<std::function<void()>>, "addcon",
2644  std::function<void()>(std::bind(
2646 
2647  if (connOptions.m_use_addrman_outgoing &&
2648  !connOptions.m_specified_outgoing.empty()) {
2649  if (clientInterface) {
2650  clientInterface->ThreadSafeMessageBox(
2651  _("Cannot provide specific connections and have addrman find "
2652  "outgoing connections at the same."),
2654  }
2655  return false;
2656  }
2657  if (connOptions.m_use_addrman_outgoing ||
2658  !connOptions.m_specified_outgoing.empty()) {
2660  std::thread(&TraceThread<std::function<void()>>, "opencon",
2661  std::function<void()>(
2662  std::bind(&CConnman::ThreadOpenConnections, this,
2663  connOptions.m_specified_outgoing)));
2664  }
2665 
2666  // Process messages
2668  std::thread(&TraceThread<std::function<void()>>, "msghand",
2669  std::function<void()>(
2670  std::bind(&CConnman::ThreadMessageHandler, this)));
2671 
2672  // Dump network addresses
2673  scheduler.scheduleEvery(
2674  [this]() {
2675  this->DumpAddresses();
2676  return true;
2677  },
2679 
2680  return true;
2681 }
2682 
2684 public:
2686 
2688 #ifdef WIN32
2689  // Shutdown Windows Sockets
2690  WSACleanup();
2691 #endif
2692  }
2693 };
2695 
2697  {
2698  LOCK(mutexMsgProc);
2699  flagInterruptMsgProc = true;
2700  }
2701  condMsgProc.notify_all();
2702 
2703  interruptNet();
2704  InterruptSocks5(true);
2705 
2706  if (semOutbound) {
2707  for (int i = 0; i < m_max_outbound; i++) {
2708  semOutbound->post();
2709  }
2710  }
2711 
2712  if (semAddnode) {
2713  for (int i = 0; i < nMaxAddnode; i++) {
2714  semAddnode->post();
2715  }
2716  }
2717 }
2718 
2720  if (threadMessageHandler.joinable()) {
2721  threadMessageHandler.join();
2722  }
2723  if (threadOpenConnections.joinable()) {
2724  threadOpenConnections.join();
2725  }
2726  if (threadOpenAddedConnections.joinable()) {
2728  }
2729  if (threadDNSAddressSeed.joinable()) {
2730  threadDNSAddressSeed.join();
2731  }
2732  if (threadSocketHandler.joinable()) {
2733  threadSocketHandler.join();
2734  }
2735 }
2736 
2738  if (fAddressesInitialized) {
2739  DumpAddresses();
2740  fAddressesInitialized = false;
2741  }
2742 
2743  // Close sockets
2744  LOCK(cs_vNodes);
2745  for (CNode *pnode : vNodes) {
2746  pnode->CloseSocketDisconnect();
2747  }
2748  for (ListenSocket &hListenSocket : vhListenSocket) {
2749  if (hListenSocket.socket != INVALID_SOCKET) {
2750  if (!CloseSocket(hListenSocket.socket)) {
2751  LogPrintf("CloseSocket(hListenSocket) failed with error %s\n",
2753  }
2754  }
2755  }
2756 
2757  // clean up some globals (to help leak detection)
2758  for (CNode *pnode : vNodes) {
2759  DeleteNode(pnode);
2760  }
2761  for (CNode *pnode : vNodesDisconnected) {
2762  DeleteNode(pnode);
2763  }
2764  vNodes.clear();
2765  vNodesDisconnected.clear();
2766  vhListenSocket.clear();
2767  semOutbound.reset();
2768  semAddnode.reset();
2769 }
2770 
2772  assert(pnode);
2773  bool fUpdateConnectionTime = false;
2774  m_msgproc->FinalizeNode(*config, pnode->GetId(), fUpdateConnectionTime);
2775  if (fUpdateConnectionTime) {
2776  addrman.Connected(pnode->addr);
2777  }
2778  delete pnode;
2779 }
2780 
2782  Interrupt();
2783  Stop();
2784 }
2785 
2786 void CConnman::SetServices(const CService &addr, ServiceFlags nServices) {
2787  addrman.SetServices(addr, nServices);
2788 }
2789 
2791  addrman.Good(addr);
2792 }
2793 
2794 void CConnman::AddNewAddresses(const std::vector<CAddress> &vAddr,
2795  const CAddress &addrFrom, int64_t nTimePenalty) {
2796  addrman.Add(vAddr, addrFrom, nTimePenalty);
2797 }
2798 
2799 std::vector<CAddress> CConnman::GetAddresses() {
2800  return addrman.GetAddr();
2801 }
2802 
2803 bool CConnman::AddNode(const std::string &strNode) {
2805  for (const std::string &it : vAddedNodes) {
2806  if (strNode == it) {
2807  return false;
2808  }
2809  }
2810 
2811  vAddedNodes.push_back(strNode);
2812  return true;
2813 }
2814 
2815 bool CConnman::RemoveAddedNode(const std::string &strNode) {
2817  for (std::vector<std::string>::iterator it = vAddedNodes.begin();
2818  it != vAddedNodes.end(); ++it) {
2819  if (strNode == *it) {
2820  vAddedNodes.erase(it);
2821  return true;
2822  }
2823  }
2824  return false;
2825 }
2826 
2828  LOCK(cs_vNodes);
2829  // Shortcut if we want total
2830  if (flags == CConnman::CONNECTIONS_ALL) {
2831  return vNodes.size();
2832  }
2833 
2834  int nNum = 0;
2835  for (const auto &pnode : vNodes) {
2836  if (flags &
2837  (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
2838  nNum++;
2839  }
2840  }
2841 
2842  return nNum;
2843 }
2844 
2845 void CConnman::GetNodeStats(std::vector<CNodeStats> &vstats) {
2846  vstats.clear();
2847  LOCK(cs_vNodes);
2848  vstats.reserve(vNodes.size());
2849  for (CNode *pnode : vNodes) {
2850  vstats.emplace_back();
2851  pnode->copyStats(vstats.back(), addrman.m_asmap);
2852  }
2853 }
2854 
2855 bool CConnman::DisconnectNode(const std::string &strNode) {
2856  LOCK(cs_vNodes);
2857  if (CNode *pnode = FindNode(strNode)) {
2858  pnode->fDisconnect = true;
2859  return true;
2860  }
2861  return false;
2862 }
2863 
2864 bool CConnman::DisconnectNode(const CSubNet &subnet) {
2865  bool disconnected = false;
2866  LOCK(cs_vNodes);
2867  for (CNode *pnode : vNodes) {
2868  if (subnet.Match(pnode->addr)) {
2869  pnode->fDisconnect = true;
2870  disconnected = true;
2871  }
2872  }
2873  return disconnected;
2874 }
2875 
2877  return DisconnectNode(CSubNet(addr));
2878 }
2879 
2881  LOCK(cs_vNodes);
2882  for (CNode *pnode : vNodes) {
2883  if (id == pnode->GetId()) {
2884  pnode->fDisconnect = true;
2885  return true;
2886  }
2887  }
2888  return false;
2889 }
2890 
2891 void CConnman::RecordBytesRecv(uint64_t bytes) {
2893  nTotalBytesRecv += bytes;
2894 }
2895 
2896 void CConnman::RecordBytesSent(uint64_t bytes) {
2898  nTotalBytesSent += bytes;
2899 
2900  uint64_t now = GetTime();
2901  if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now) {
2902  // timeframe expired, reset cycle
2903  nMaxOutboundCycleStartTime = now;
2904  nMaxOutboundTotalBytesSentInCycle = 0;
2905  }
2906 
2907  // TODO, exclude whitebind peers
2908  nMaxOutboundTotalBytesSentInCycle += bytes;
2909 }
2910 
2911 void CConnman::SetMaxOutboundTarget(uint64_t limit) {
2913  nMaxOutboundLimit = limit;
2914 }
2915 
2918  return nMaxOutboundLimit;
2919 }
2920 
2923  return nMaxOutboundTimeframe;
2924 }
2925 
2928  if (nMaxOutboundLimit == 0) {
2929  return 0;
2930  }
2931 
2932  if (nMaxOutboundCycleStartTime == 0) {
2933  return nMaxOutboundTimeframe;
2934  }
2935 
2936  uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2937  uint64_t now = GetTime();
2938  return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2939 }
2940 
2941 void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe) {
2943  if (nMaxOutboundTimeframe != timeframe) {
2944  // reset measure-cycle in case of changing the timeframe.
2945  nMaxOutboundCycleStartTime = GetTime();
2946  }
2947  nMaxOutboundTimeframe = timeframe;
2948 }
2949 
2950 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) {
2952  if (nMaxOutboundLimit == 0) {
2953  return false;
2954  }
2955 
2956  if (historicalBlockServingLimit) {
2957  // keep a large enough buffer to at least relay each block once.
2958  uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2959  uint64_t buffer = timeLeftInCycle / 600 * ONE_MEGABYTE;
2960  if (buffer >= nMaxOutboundLimit ||
2961  nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer) {
2962  return true;
2963  }
2964  } else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) {
2965  return true;
2966  }
2967 
2968  return false;
2969 }
2970 
2973  if (nMaxOutboundLimit == 0) {
2974  return 0;
2975  }
2976 
2977  return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2978  ? 0
2979  : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
2980 }
2981 
2984  return nTotalBytesRecv;
2985 }
2986 
2989  return nTotalBytesSent;
2990 }
2991 
2993  return nLocalServices;
2994 }
2995 
2996 void CConnman::SetBestHeight(int height) {
2997  nBestHeight.store(height, std::memory_order_release);
2998 }
2999 
3001  return nBestHeight.load(std::memory_order_acquire);
3002 }
3003 
3004 unsigned int CConnman::GetReceiveFloodSize() const {
3005  return nReceiveFloodSize;
3006 }
3007 
3008 CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn,
3009  int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn,
3010  uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
3011  uint64_t nLocalExtraEntropyIn, const CAddress &addrBindIn,
3012  const std::string &addrNameIn, ConnectionType conn_type_in)
3013  : nTimeConnected(GetSystemTimeInSeconds()), addr(addrIn),
3014  addrBind(addrBindIn), nKeyedNetGroup(nKeyedNetGroupIn),
3015  // Don't relay addr messages to peers that we connect to as
3016  // block-relay-only peers (to prevent adversaries from inferring these
3017  // links from addr traffic).
3018  id(idIn), nLocalHostNonce(nLocalHostNonceIn),
3019  nLocalExtraEntropy(nLocalExtraEntropyIn), m_conn_type(conn_type_in),
3020  nLocalServices(nLocalServicesIn), nMyStartingHeight(nMyStartingHeightIn) {
3021  hSocket = hSocketIn;
3022  addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
3023  hashContinue = BlockHash();
3024  if (conn_type_in != ConnectionType::BLOCK_RELAY) {
3025  m_tx_relay = std::make_unique<TxRelay>();
3026  m_addr_known = std::make_unique<CRollingBloomFilter>(5000, 0.001);
3027  }
3028 
3029  for (const std::string &msg : getAllNetMessageTypes()) {
3030  mapRecvBytesPerMsgCmd[msg] = 0;
3031  }
3032  mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
3033 
3034  if (fLogIPs) {
3035  LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id);
3036  } else {
3037  LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
3038  }
3039 
3040  m_deserializer = std::make_unique<V1TransportDeserializer>(
3041  V1TransportDeserializer(GetConfig().GetChainParams().NetMagic(),
3043  m_serializer =
3044  std::make_unique<V1TransportSerializer>(V1TransportSerializer());
3045 }
3046 
3048  CloseSocket(hSocket);
3049 }
3050 
3052  return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
3053 }
3054 
3056  size_t nMessageSize = msg.data.size();
3057  LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n",
3058  SanitizeString(msg.m_type), nMessageSize, pnode->GetId());
3059 
3060  // make sure we use the appropriate network transport format
3061  std::vector<uint8_t> serializedHeader;
3062  pnode->m_serializer->prepareForTransport(*config, msg, serializedHeader);
3063  size_t nTotalSize = nMessageSize + serializedHeader.size();
3064 
3065  size_t nBytesSent = 0;
3066  {
3067  LOCK(pnode->cs_vSend);
3068  bool optimisticSend(pnode->vSendMsg.empty());
3069 
3070  // log total amount of bytes per message type
3071  pnode->mapSendBytesPerMsgCmd[msg.m_type] += nTotalSize;
3072  pnode->nSendSize += nTotalSize;
3073 
3074  if (pnode->nSendSize > nSendBufferMaxSize) {
3075  pnode->fPauseSend = true;
3076  }
3077  pnode->vSendMsg.push_back(std::move(serializedHeader));
3078  if (nMessageSize) {
3079  pnode->vSendMsg.push_back(std::move(msg.data));
3080  }
3081 
3082  // If write queue empty, attempt "optimistic write"
3083  if (optimisticSend == true) {
3084  nBytesSent = SocketSendData(pnode);
3085  }
3086  }
3087  if (nBytesSent) {
3088  RecordBytesSent(nBytesSent);
3089  }
3090 }
3091 
3092 bool CConnman::ForNode(NodeId id, std::function<bool(CNode *pnode)> func) {
3093  CNode *found = nullptr;
3094  LOCK(cs_vNodes);
3095  for (auto &&pnode : vNodes) {
3096  if (pnode->GetId() == id) {
3097  found = pnode;
3098  break;
3099  }
3100  }
3101  return found != nullptr && NodeFullyConnected(found) && func(found);
3102 }
3103 
3105  int average_interval_seconds) {
3106  if (m_next_send_inv_to_incoming < now) {
3107  // If this function were called from multiple threads simultaneously
3108  // it would be possible that both update the next send variable, and
3109  // return a different result to their caller. This is not possible in
3110  // practice as only the net processing thread invokes this function.
3111  m_next_send_inv_to_incoming =
3112  PoissonNextSend(now, average_interval_seconds);
3113  }
3114  return m_next_send_inv_to_incoming;
3115 }
3116 
3117 int64_t PoissonNextSend(int64_t now, int average_interval_seconds) {
3118  return now + int64_t(log1p(GetRand(1ULL << 48) *
3119  -0.0000000000000035527136788 /* -1/2^48 */) *
3120  average_interval_seconds * -1000000.0 +
3121  0.5);
3122 }
3123 
3125  return CSipHasher(nSeed0, nSeed1).Write(id);
3126 }
3127 
3128 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress &ad) const {
3129  std::vector<uint8_t> vchNetGroup(ad.GetGroup(addrman.m_asmap));
3130 
3131  return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP)
3132  .Write(vchNetGroup.data(), vchNetGroup.size())
3133  .Finalize();
3134 }
3135 
3150 std::string getSubVersionEB(uint64_t MaxBlockSize) {
3151  // Prepare EB string we are going to add to SubVer:
3152  // 1) translate from byte to MB and convert to string
3153  // 2) limit the EB string to the first decimal digit (floored)
3154  std::stringstream ebMBs;
3155  ebMBs << (MaxBlockSize / (ONE_MEGABYTE / 10));
3156  std::string eb = ebMBs.str();
3157  eb.insert(eb.size() - 1, ".", 1);
3158  if (eb.substr(0, 1) == ".") {
3159  eb = "0" + eb;
3160  }
3161  return eb;
3162 }
3163 
3164 std::string userAgent(const Config &config) {
3165  // format excessive blocksize value
3166  std::string eb = getSubVersionEB(config.GetMaxBlockSize());
3167  std::vector<std::string> uacomments;
3168  uacomments.push_back("EB" + eb);
3169 
3170  // Comments are checked for char compliance at startup, it is safe to add
3171  // them to the user agent string
3172  for (const std::string &cmt : gArgs.GetArgs("-uacomment")) {
3173  uacomments.push_back(cmt);
3174  }
3175 
3176  // Size compliance is checked at startup, it is safe to not check it again
3177  std::string subversion =
3179 
3180  return subversion;
3181 }
const std::vector< std::string > & DNSSeeds() const
Return the list of hostnames to look up for DNS seeds.
Definition: chainparams.h:88
std::vector< CService > vBinds
Definition: net.h:193
std::atomic< bool > flagInterruptMsgProc
Definition: net.h:536
std::string getSubVersionEB(uint64_t MaxBlockSize)
This function convert MaxBlockSize from byte to MB with a decimal precision one digit rounded down E...
Definition: net.cpp:3150
void copyStats(CNodeStats &stats, const std::vector< bool > &m_asmap)
Definition: net.cpp:538
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
uint64_t CalculateKeyedNetGroup(const CAddress &ad) const
Definition: net.cpp:3128
#define WSAEINPROGRESS
Definition: compat.h:52
std::string m_type
Definition: net.h:125
unsigned short GetPort() const
Definition: netaddress.cpp:692
std::atomic< uint64_t > nPingNonceSent
Definition: net.h:1007
void SocketHandler()
Definition: net.cpp:1489
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
Definition: random.cpp:635
static unsigned short GetDefaultPort()
Definition: bitcoin.h:15
void MoveTo(CSemaphoreGrant &grant)
Definition: sync.h:330
std::atomic_bool fPauseSend
Definition: net.h:878
Access to the (IP) address database (peers.dat)
Definition: addrdb.h:49
uint64_t GetRand(uint64_t nMax) noexcept
Definition: random.cpp:641
#define WSAEINTR
Definition: compat.h:51
void ThreadOpenAddedConnections()
Definition: net.cpp:2222
bool GetLocal(CService &addr, const CNetAddr *paddrPeer)
Definition: net.cpp:126
bool sleep_for(std::chrono::milliseconds rel_time)
virtual bool ProcessMessages(const Config &config, CNode *pnode, std::atomic< bool > &interrupt)=0
#define MSG_DONTWAIT
Definition: net.cpp:84
int GetSendVersion() const
Definition: net.cpp:664
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET &hSocket, int nTimeout, bool manual_connection)
Try to connect to the specified service on the specified socket.
Definition: netbase.cpp:683
int m_max_outbound
Definition: net.h:517
static constexpr Amount zero()
Definition: amount.h:35
CService LookupNumeric(const std::string &name, int portDefault)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:270
std::atomic< bool > fNetworkActive
Definition: net.h:477
Amount minFeeFilter
Definition: net.h:668
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
ServiceFlags
nServices flags.
Definition: protocol.h:320
CAddrInfo Select(bool newOnly=false)
Choose an address to connect to.
Definition: addrman.h:672
std::unique_ptr< TransportSerializer > m_serializer
Definition: net.h:812
RecursiveMutex cs_vNodes
Definition: net.h:486
#define LogPrint(category,...)
Definition: logging.h:192
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.h:639
bool AddLocal(const CService &addr, int nScore)
Definition: net.cpp:229
Connections to addresses added via addnode or the connect command line argument.
std::string addrLocal
Definition: net.h:670
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
Definition: siphash.cpp:36
#define TRY_LOCK(cs, name)
Definition: sync.h:236
static bool IsSelectableSocket(const SOCKET &s)
Definition: compat.h:97
std::atomic< int > nBestHeight
Definition: net.h:519
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:28
void WakeMessageHandler()
Definition: net.cpp:1627
bool Bind(const CService &addr, unsigned int flags, NetPermissionFlags permissions)
Definition: net.cpp:2513
virtual void FinalizeNode(const Config &config, NodeId id, bool &update_connection_time)=0
void SetServices(const CService &addr, ServiceFlags nServices)
Definition: net.cpp:2786
void Interrupt()
Definition: net.cpp:2696
int64_t nLastTXTime
Definition: net.cpp:864
Bilingual messages:
Definition: translation.h:17
void SocketEvents(std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set)
Definition: net.cpp:1406
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:59
CAddress addrBind
Definition: net.h:674
Mutex mutexMsgProc
Definition: net.h:535
bool m_legacyWhitelisted
Definition: net.h:664
static const bool DEFAULT_FORCEDNSSEED
Definition: net.h:95
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
mapMsgCmdSize mapSendBytesPerMsgCmd
Definition: net.h:931
bool m_valid_checksum
Definition: net.h:691
#define WSAEADDRINUSE
Definition: compat.h:53
static void ClearFlag(NetPermissionFlags &flags, NetPermissionFlags f)
bool IsIPv6() const
Definition: netaddress.cpp:127
uint64_t nRecvBytes
Definition: net.h:661
std::list< CNode * > vNodesDisconnected
Definition: net.h:485
NodeId nodeid
Definition: net.h:646
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:493
Full relay connections (blocks, addrs, txns) made automatically.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
std::vector< NetWhitelistPermissions > vWhitelistedRange
Definition: net.h:471
void * sockopt_arg_type
Definition: compat.h:82
int m_max_outbound_full_relay
Definition: net.h:509
bool GetNameProxy(proxyType &nameProxyOut)
Definition: netbase.cpp:817
bool m_legacyWhitelisted
Definition: net.h:863
void ProcessAddrFetch()
Definition: net.cpp:1889
void AddWhitelistPermissionFlags(NetPermissionFlags &flags, const CNetAddr &addr) const
Definition: net.cpp:501
bool GetTryNewOutboundPeer()
Definition: net.cpp:1907
#define FEELER_SLEEP_WINDOW
Definition: net.cpp:73
int nMaxAddnode
Definition: net.h:515
RAII-style semaphore lock.
Definition: sync.h:301
uint64_t GetMaxOutboundTarget()
Definition: net.cpp:2916
static const int BIP0031_VERSION
BIP 0031, pong message, is enabled for all versions AFTER this one.
Definition: version.h:20
void PushMessage(CNode *pnode, CSerializedNetMsg &&msg)
Definition: net.cpp:3055
bool BindListenPort(const CService &bindAddr, bilingual_str &strError, NetPermissionFlags permissions)
Definition: net.cpp:2355
uint32_t m_message_size
Definition: net.h:693
#define MSG_NOSIGNAL
Definition: net.cpp:78
bool m_prefer_evict
Definition: net.h:857
void SetMaxOutboundTimeframe(uint64_t timeframe)
set the timeframe for the max outbound target.
Definition: net.cpp:2941
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:174
bool fDiscover
Definition: net.cpp:109
NetEventsInterface * m_msgproc
Definition: net.h:521
std::atomic< int64_t > nPingUsecStart
Definition: net.h:1009
void Discover()
Definition: net.cpp:2437
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
Definition: net.cpp:173
int nStartingHeight
Definition: net.h:658
int m_max_outbound_block_relay
Definition: net.h:513
static void EraseLastKElements(std::vector< T > &elements, Comparator comparator, size_t k)
Sort an array by the specified comparator, then erase the last K elements.
Definition: net.cpp:924
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:498
void SetTryNewOutboundPeer(bool flag)
Definition: net.cpp:1911
unsigned short GetListenPort()
Definition: net.cpp:121
bool InitBinds(const std::vector< CService > &binds, const std::vector< NetWhitebindPermissions > &whiteBinds)
Definition: net.cpp:2529
std::string ToString() const
Definition: netaddress.cpp:364
std::unique_ptr< TransportDeserializer > m_deserializer
Definition: net.h:811
static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE
Definition: net.cpp:103
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:285
void ThreadSocketHandler()
Definition: net.cpp:1619
static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:888
#define INVALID_SOCKET
Definition: compat.h:55
size_t GetNodeCount(NumConnections num)
Definition: net.cpp:2827
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
Add a single address.
Definition: addrman.h:597
void PushAddress(const CAddress &_addr, FastRandomContext &insecure_rand)
Definition: net.h:1101
NetPermissionFlags m_permissionFlags
Definition: net.h:663
static const int FEELER_INTERVAL
Run the feeler connection loop once every 2 minutes or 120 seconds.
Definition: net.h:58
static const int TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Definition: net.h:56
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: time.cpp:75
#define WSAGetLastError()
Definition: compat.h:46
void StopThreads()
Definition: net.cpp:2719
static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:903
void SetReachable(enum Network net, bool reachable)
Mark a network as reachable or unreachable (no automatic connects to it)
Definition: net.cpp:267
void NotifyNumConnectionsChanged()
Definition: net.cpp:1254
enum Network GetNetwork() const
Definition: netaddress.cpp:312
virtual const CChainParams & GetChainParams() const =0
void SetMaxOutboundTarget(uint64_t limit)
set the max outbound target in bytes.
Definition: net.cpp:2911
mapMsgCmdSize mapSendBytesPerMsgCmd
Definition: net.h:660
void RecordBytesSent(uint64_t bytes)
Definition: net.cpp:2896
CAddrMan addrman
Definition: net.h:479
void SetAddrLocal(const CService &addrLocalIn)
May not be called more than once.
Definition: net.cpp:527
uint64_t GetMaxOutboundTimeframe()
Definition: net.cpp:2921
void AddSocketPermissionFlags(NetPermissionFlags &flags) const
Definition: net.h:391
bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, int port, const SOCKET &hSocket, int nTimeout, bool &outProxyConnectionFailed)
Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 p...
Definition: netbase.cpp:856
bool ForNode(NodeId id, std::function< bool(CNode *pnode)> func)
Definition: net.cpp:3092
ServiceFlags nLocalServices
Services this instance offers.
Definition: net.h:502
mapMsgCmdSize mapRecvBytesPerMsgCmd
Definition: net.h:662
RecursiveMutex cs_mapLocalHost
Definition: net.cpp:112
bool IsValid() const
Definition: netaddress.cpp:244
bool DisconnectNode(const std::string &node)
Definition: net.cpp:2855
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD
Definition: net.cpp:69
int readHeader(const Config &config, const char *pch, uint32_t nBytes)
Definition: net.cpp:675
bool Lookup(const std::string &name, std::vector< CService > &vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:219
Definition: net.cpp:89
std::atomic< int64_t > nLastSend
Definition: net.h:837
Definition: net.h:590
void RecordBytesRecv(uint64_t bytes)
Definition: net.cpp:2891
bool HaveNameProxy()
Definition: netbase.cpp:826
void DumpAddresses()
Definition: net.cpp:1879
bool done
bool IsDiscouraged(const CNetAddr &net_addr)
Return whether net_addr is discouraged.
Definition: banman.cpp:84
std::vector< CAddress > GetAddr()
Return a bunch of addresses, selected at random.
Definition: addrman.h:684
void StopMapPort()
Definition: net.cpp:1738
Extended statistics about a CAddress.
Definition: addrman.h:29
ServiceFlags GetLocalServices() const
Definition: net.h:1146
#define SOCKET_ERROR
Definition: compat.h:56
static CAddress GetBindAddress(SOCKET sock)
Get the bind address for a socket as CAddress.
Definition: net.cpp:352
void CloseSocketDisconnect()
Definition: net.cpp:492
BlockHash hashContinue
Definition: net.h:935
uint64_t GetOutboundTargetBytesLeft()
response the bytes left in the current max outbound cycle in case of no limit, it will always respons...
Definition: net.cpp:2971
int64_t nTimeConnected
Definition: net.h:651
virtual uint64_t GetMaxBlockSize() const =0
int readData(const char *pch, uint32_t nBytes)
Definition: net.cpp:708
Definition: config.h:19
bool Write(const CAddrMan &addr)
Definition: addrdb.cpp:138
void AddAddrFetch(const std::string &strDest)
Definition: net.cpp:116
static bool NodeFullyConnected(const CNode *pnode)
Definition: net.cpp:3051
unsigned int nPrevNodeCount
Definition: net.h:488
std::unique_ptr< CRollingBloomFilter > m_addr_known
Definition: net.h:940
CConnman(const Config &configIn, uint64_t seed0, uint64_t seed1)
Definition: net.cpp:2501
static const uint64_t RANDOMIZER_ID_NETGROUP
Definition: net.cpp:101
bool AddNode(const std::string &node)
Definition: net.cpp:2803
std::condition_variable condMsgProc
Definition: net.h:534
RecursiveMutex cs_vAddedNodes
Definition: net.h:483
CService GetAddrLocal() const
Definition: net.cpp:522
uint64_t GetMaxOutboundTimeLeftInCycle()
response the time in second left in the current max outbound cycle in case of no limit, it will always response 0
Definition: net.cpp:2926
void InactivityCheck(CNode *pnode)
Definition: net.cpp:1268
std::atomic< int64_t > nLastRecv
Definition: net.h:838
const uint64_t nSeed0
SipHasher seeds for deterministic randomness.
Definition: net.h:529
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
Definition: protocol.h:412
std::thread threadOpenAddedConnections
Definition: net.h:542
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
Definition: netaddress.cpp:719
std::string ToStringIP() const
Definition: netaddress.cpp:332
#define LOCK(cs)
Definition: sync.h:230
CNode * FindNode(const CNetAddr &ip)
Definition: net.cpp:300
ServiceFlags GetLocalServices() const
Used to convey which local services we are offering peers during node connection. ...
Definition: net.cpp:2992
RecursiveMutex m_addr_fetches_mutex
Definition: net.h:481
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
bool IsPeerAddrLocalGood(CNode *pnode)
Definition: net.cpp:193
bool fInbound
Definition: net.h:656
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:14
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE
Number of DNS seeds to query when the number of connections is low.
Definition: net.cpp:54
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:179
Fast randomness source.
Definition: random.h:113
Transport protocol agnostic message container.
Definition: net.h:683
bool g_relay_txes
Definition: net.cpp:111
std::vector< std::string > vSeedNodes
Definition: net.h:190
int64_t PoissonNextSendInbound(int64_t now, int average_interval_seconds)
Attempts to obfuscate tx time through exponentially distributed emitting.
Definition: net.cpp:3104
void scheduleEvery(Predicate p, std::chrono::milliseconds delta)
Repeat p until it return false.
Definition: scheduler.cpp:127
bool OutboundTargetReached(bool historicalBlockServingLimit)
check if the outbound target is reached.
Definition: net.cpp:2950
std::vector< std::string > m_specified_outgoing
Definition: net.h:195
std::vector< CAddress > GetAddresses()
Definition: net.cpp:2799
int64_t m_peer_connect_timeout
Definition: net.h:467
void DisconnectNodes()
Definition: net.cpp:1193
std::unique_ptr< CSemaphore > semOutbound
Definition: net.h:504
void ThreadOpenConnections(std::vector< std::string > connect)
Definition: net.cpp:1938
std::thread threadMessageHandler
Definition: net.h:544
static bool HasFlag(const NetPermissionFlags &flags, NetPermissionFlags f)
const uint256 & GetMessageHash() const
Definition: net.cpp:725
A CService with information about it as peer.
Definition: protocol.h:427
bool Start(CScheduler &scheduler, const Options &options)
Definition: net.cpp:2554
static int GetnScore(const CService &addr)
Definition: net.cpp:184
void MaybeSetAddrName(const std::string &addrNameIn)
Sets the addrName only if it was not previously set.
Definition: net.cpp:515
const std::vector< std::string > & getAllNetMessageTypes()
Get a vector of all valid message types (see above)
Definition: protocol.cpp:255
uint64_t GetTotalBytesRecv()
Definition: net.cpp:2982
const Config * config
Definition: net.h:452
static unsigned char elements[DATACOUNT][DATALEN]
Definition: tests_impl.h:36
int nVersion
Definition: net.h:654
Network
Definition: netaddress.h:19
const Config & GetConfig()
Definition: config.cpp:34
std::string addrName
Definition: net.h:653
void OpenNetworkConnection(const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound=nullptr, const char *strDest=nullptr, ConnectionType conn_type=ConnectionType::OUTBOUND)
Definition: net.cpp:2254
std::atomic_bool m_try_another_outbound_peer
flag for deciding to connect to an extra outbound peer, in excess of m_max_outbound_full_relay.
Definition: net.h:550
int64_t NodeId
Definition: net.h:99
static const uint64_t ONE_MEGABYTE
1MB
Definition: consensus.h:12
CNetCleanup()
Definition: net.cpp:2685
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:72
void SetNetworkActive(bool active)
Definition: net.cpp:2490
void AddNewAddresses(const std::vector< CAddress > &vAddr, const CAddress &addrFrom, int64_t nTimePenalty=0)
Definition: net.cpp:2794
int GetDefaultPort() const
Definition: chainparams.h:62
#define WAIT_LOCK(cs, name)
Definition: sync.h:238
NumConnections
Definition: net.h:167
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:82
uint8_t * begin()
Definition: uint256.h:76
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:42
CClientUIInterface * clientInterface
Definition: net.h:520
NodeId GetId() const
Definition: net.h:1065
void GetNodeStats(std::vector< CNodeStats > &vstats)
Definition: net.cpp:2845
CSipHasher GetDeterministicRandomizer(uint64_t id) const
Get a unique deterministic randomizer.
Definition: net.cpp:3124
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS
Definition: net.cpp:67
const std::string CLIENT_NAME
std::atomic_bool fDisconnect
Definition: net.h:871
int nMaxConnections
Definition: net.h:506
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
A helper class for interruptible sleeps.
void TraceThread(const char *name, Callable func)
Definition: system.h:445
NetPermissionFlags
size_t nSendSize
Definition: net.h:818
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: netbase.cpp:962
int nConnectTimeout
Definition: netbase.cpp:32
virtual void InitializeNode(const Config &config, CNode *pnode)=0
bool IsRoutable() const
Definition: netaddress.cpp:296
#define WSAEWOULDBLOCK
Definition: compat.h:49
std::string FormatFullVersion()
const std::string NET_MESSAGE_COMMAND_OTHER
Definition: net.cpp:98
static bool MayHaveUsefulAddressDB(ServiceFlags services)
Checks if a peer with the given service flags may be capable of having a robust address-storage DB...
Definition: protocol.h:420
unsigned int GetReceiveFloodSize() const
Definition: net.cpp:3004
Peer initiated connections.
~CNode()
Definition: net.cpp:3047
void DeleteNode(CNode *pnode)
Definition: net.cpp:2771
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS
How long to delay before querying DNS seeds.
Definition: net.cpp:66
unsigned int SOCKET
Definition: compat.h:44
bool CheckIncomingNonce(uint64_t nonce)
Definition: net.cpp:340
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:39
const CAddress addr
Definition: net.h:842
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:845
const int64_t nTimeConnected
Definition: net.h:839
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:67
int flags
Definition: bitcoin-tx.cpp:529
std::atomic< NodeId > nLastNodeId
Definition: net.h:487
bool RemoveAddedNode(const std::string &node)
Definition: net.cpp:2815
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:30
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:164
CNode * ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type)
Definition: net.cpp:367
int64_t m_ping_wait_usec
Definition: net.h:666
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.h:577
256-bit opaque blob.
Definition: uint256.h:120
int GetBestHeight() const
Definition: net.cpp:3000
~CNetCleanup()
Definition: net.cpp:2687
unsigned int nTime
Definition: protocol.h:466
bool IsReachable(enum Network net)
Definition: net.cpp:275
bool m_valid_netmagic
Definition: net.h:689
uint32_t m_raw_message_size
Definition: net.h:695
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:235
int nScore
Definition: net.h:628
ServiceFlags nServices
Definition: protocol.h:463
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
static const bool DEFAULT_WHITELISTFORCERELAY
Default for -whitelistforcerelay.
Definition: net.h:50
CService proxy
Definition: netbase.h:37
BanMan * m_banman
Pointer to this node&#39;s banman.
Definition: net.h:526
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:44
void Clear()
Definition: addrman.h:548
static const uint64_t SELECT_TIMEOUT_MILLISECONDS
Definition: net.cpp:96
~CConnman()
Definition: net.cpp:2781
std::thread threadOpenConnections
Definition: net.h:543
bool AttemptToEvictConnection()
Try to find a connection to evict when the node is full.
Definition: net.cpp:940
std::string original
Definition: translation.h:18
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
ConnectionType
Different types of connections to a peer.
Definition: net.h:133
static std::vector< CAddress > convertSeed6(const std::vector< SeedSpec6 > &vSeedsIn)
Convert the pnSeed6 array into usable address objects.
Definition: net.cpp:151
const CChainParams & Params()
Return the currently selected parameters.
CSemaphoreGrant grantOutbound
Definition: net.h:873
ServiceFlags nServices
Definition: net.h:647
bool SetSocketNoDelay(const SOCKET &hSocket)
Set the TCP_NODELAY flag on a socket.
Definition: netbase.cpp:1005
int64_t nLastSend
Definition: net.h:649
const NodeId id
Definition: net.h:1029
NodeId GetNewNodeId()
Definition: net.cpp:2509
Short lived connections used to test address validity.
NetPermissionFlags m_permissionFlags
Definition: net.h:1053
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:479
bool fAddressesInitialized
Definition: net.h:478
std::thread threadDNSAddressSeed
Definition: net.h:540
int64_t nTimeConnected
Definition: net.cpp:861
virtual bool SendMessages(const Config &config, CNode *pnode, std::atomic< bool > &interrupt)=0
bool fLogIPs
Definition: logging.cpp:12
CNetMessage GetMessage(const Config &config, int64_t time) override
Definition: net.cpp:733
int64_t GetAdjustedTime()
Definition: timedata.cpp:34
CAddrInfo SelectTriedCollision()
Randomly select an address in tried that another address is attempting to evict.
Definition: addrman.h:658
std::string ToStringIPPort() const
Definition: netaddress.cpp:772
size_t SocketSendData(CNode *pnode) const
Definition: net.cpp:793
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:204
void SetSendVersion(int nVersionIn)
Definition: net.cpp:649
RecursiveMutex cs_vSend
Definition: net.h:823
std::vector< uint8_t > data
Definition: net.h:124
std::vector< ListenSocket > vhListenSocket
Definition: net.h:476
uint64_t nSendBytes
Definition: net.h:659
int64_t m_min_ping_usec
Definition: net.h:667
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
Format the subversion field according to BIP 14 spec (https://github.com/bitcoin/bips/blob/master/bip...
void SetBestHeight(int height)
Definition: net.cpp:2996
SOCKET CreateSocket(const CService &addrConnect)
Try to create a socket file descriptor with specific properties in the communications domain (address...
Definition: netbase.cpp:613
bool ReceiveMsgBytes(const Config &config, const char *pch, uint32_t nBytes, bool &complete)
Definition: net.cpp:606
std::string cleanSubVer
Definition: net.h:655
int64_t PoissonNextSend(int64_t now, int average_interval_seconds)
Return a timestamp in the future (in microseconds) for exponentially distributed events.
Definition: net.cpp:3117
const std::vector< SeedSpec6 > & FixedSeeds() const
Definition: chainparams.h:93
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:132
ArgsManager gArgs
Definition: system.cpp:76
int64_t m_ping_usec
Definition: net.h:665
bool fListen
Definition: net.cpp:110
std::atomic_bool fSuccessfullyConnected
Definition: net.h:868
SipHash-2-4.
Definition: siphash.h:13
void ThreadMessageHandler()
Definition: net.cpp:2299
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions...
Definition: addrman.h:649
void StopNodes()
Definition: net.cpp:2737
std::atomic< int > nVersion
Definition: net.h:845
POD that contains various stats about a node.
Definition: net.h:645
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:1012
unsigned int nSendBufferMaxSize
Definition: net.h:473
static const bool DEFAULT_BLOCKSONLY
Default for blocks only.
Definition: net.h:91
int64_t nMinPingUsecTime
Definition: net.cpp:862
static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL
Definition: net.cpp:49
std::vector< bool > m_asmap
Definition: addrman.h:316
static constexpr size_t HEADER_SIZE
Definition: protocol.h:47
std::string ToString() const
Definition: netaddress.cpp:780
std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY(cs_mapLocalHost)
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:782
uint32_t m_mapped_as
Definition: net.h:675
std::string m_command
Definition: net.h:696
int GetExtraOutboundCount()
Definition: net.cpp:1923
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: netbase.cpp:940
#define WSAEMSGSIZE
Definition: compat.h:50
Minimal stream for overwriting and/or appending to an existing byte vector.
Definition: streams.h:62
static const bool DEFAULT_WHITELISTRELAY
Default for -whitelistrelay.
Definition: net.h:48
void Good(const CService &addr, bool test_before_evict=true, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible.
Definition: addrman.h:630
bool IsBanned(const CNetAddr &net_addr)
Return whether net_addr is banned.
Definition: banman.cpp:89
BindFlags
Used to pass flags to the Bind() function.
Definition: net.cpp:88
void SetServices(const CService &addr, ServiceFlags nServices)
Definition: addrman.h:703
uint64_t GetTotalBytesSent()
Definition: net.cpp:2987
bool GenerateSelectSet(std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set)
Definition: net.cpp:1301
void AcceptConnection(const ListenSocket &hListenSocket)
Definition: net.cpp:1053
bool m_valid_header
Definition: net.h:690
CClientUIInterface uiInterface
void MarkAddressGood(const CAddress &addr)
Definition: net.cpp:2790
static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:883
Information about a peer.
Definition: net.h:806
static CNetCleanup instance_of_cnetcleanup
Definition: net.cpp:2694
void prepareForTransport(const Config &config, CSerializedNetMsg &msg, std::vector< uint8_t > &header) override
Definition: net.cpp:777
int64_t nLastRecv
Definition: net.h:650
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:677
std::string userAgent(const Config &config)
Definition: net.cpp:3164
std::thread threadSocketHandler
Definition: net.h:541
Only relay blocks to these automatic outbound connections.
RecursiveMutex cs_totalBytesRecv
Definition: net.h:455
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: system.cpp:381
bool SetInternal(const std::string &name)
Try to make this a dummy address that maps the specified name into IPv6 like so: (0xFD + sha256("bitc...
Definition: netaddress.cpp:60
uint64_t nKeyedNetGroup
Definition: net.cpp:869
std::string GetAddrName() const
Definition: net.cpp:510
int64_t m_time
Definition: net.h:688
bool TryAcquire()
Definition: sync.h:323
Short lived connections used to solicit addrs when starting the node without a populated AddrMan...
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as currently-connected-to.
Definition: addrman.h:696
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:27
CNode * AddRef()
Definition: net.h:1089
std::unique_ptr< CSemaphore > semAddnode
Definition: net.h:505
void Init(const Options &connOptions)
Definition: net.h:200
CThreadInterrupt interruptNet
Definition: net.h:538
int GetRandInt(int nMax) noexcept
Definition: random.cpp:655
static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:873
const uint64_t nSeed1
Definition: net.h:529
void Stop()
Definition: net.h:243
bool m_use_addrman_outgoing
Definition: net.h:194
std::vector< NetWhitebindPermissions > vWhiteBinds
Definition: net.h:192
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
CAddress addr
Definition: net.h:672
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:188
void AdvertiseLocal(CNode *pnode)
Definition: net.cpp:200
std::vector< AddedNodeInfo > GetAddedNodeInfo()
Definition: net.cpp:2166
bool fNameLookup
Definition: netbase.cpp:33
void StartMapPort()
Definition: net.cpp:1732
const CMessageHeader::MessageMagic & NetMagic() const
Definition: chainparams.h:61
void InterruptMapPort()
Definition: net.cpp:1735
int64_t nLastBlockTime
Definition: net.cpp:863
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman.h:32
void RemoveLocal(const CService &addr)
Definition: net.cpp:261
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:295
RecursiveMutex cs_totalBytesSent
Definition: net.h:456
bool fRelayTxes
Definition: net.h:648
unsigned int nReceiveFloodSize
Definition: net.h:474
bool fRelevantServices
Definition: net.cpp:865
bool m_manual_connection
Definition: net.h:657
static const uint64_t RANDOMIZER_ID_EXTRAENTROPY
Definition: net.cpp:105
std::unique_ptr< TxRelay > m_tx_relay
Definition: net.h:987
Message header.
Definition: protocol.h:37
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
void ThreadDNSAddressSeed()
Definition: net.cpp:1743
static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:878
int64_t nTimeOffset
Definition: net.h:652
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, uint64_t nLocalExtraEntropyIn, const CAddress &addrBindIn, const std::string &addrNameIn, ConnectionType conn_type_in)
Definition: net.cpp:3008