Bitcoin ABC  0.29.9
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 <addrdb.h>
13 #include <addrman.h>
14 #include <avalanche/avalanche.h>
15 #include <banman.h>
16 #include <clientversion.h>
17 #include <common/args.h>
18 #include <compat.h>
19 #include <config.h>
20 #include <consensus/consensus.h>
21 #include <crypto/sha256.h>
22 #include <dnsseeds.h>
23 #include <i2p.h>
24 #include <logging.h>
25 #include <netaddress.h>
26 #include <netbase.h>
27 #include <node/ui_interface.h>
28 #include <protocol.h>
29 #include <random.h>
30 #include <scheduler.h>
31 #include <util/fs.h>
32 #include <util/sock.h>
33 #include <util/strencodings.h>
34 #include <util/thread.h>
35 #include <util/trace.h>
36 #include <util/translation.h>
37 
38 #ifdef WIN32
39 #include <cstring>
40 #else
41 #include <fcntl.h>
42 #endif
43 
44 #ifdef USE_POLL
45 #include <poll.h>
46 #endif
47 
48 #include <algorithm>
49 #include <array>
50 #include <cmath>
51 #include <cstdint>
52 #include <functional>
53 #include <limits>
54 #include <optional>
55 #include <unordered_map>
56 
58 static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2;
59 static_assert(MAX_BLOCK_RELAY_ONLY_ANCHORS <=
60  static_cast<size_t>(MAX_BLOCK_RELAY_ONLY_CONNECTIONS),
61  "MAX_BLOCK_RELAY_ONLY_ANCHORS must not exceed "
62  "MAX_BLOCK_RELAY_ONLY_CONNECTIONS.");
64 const char *const ANCHORS_DATABASE_FILENAME = "anchors.dat";
65 
66 // How often to dump addresses to peers.dat
67 static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15};
68 
72 static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3;
73 
84 static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11};
85 static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
86 // "many" vs "few" peers
87 static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000;
88 
90 static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24};
91 
92 // We add a random period time (0 to 1 seconds) to feeler connections to prevent
93 // synchronization.
94 #define FEELER_SLEEP_WINDOW 1
95 
97 enum BindFlags {
98  BF_NONE = 0,
99  BF_EXPLICIT = (1U << 0),
100  BF_REPORT_ERROR = (1U << 1),
105  BF_DONT_ADVERTISE = (1U << 2),
106 };
107 
108 // The set of sockets cannot be modified while waiting
109 // The sleep time needs to be small to avoid new sockets stalling
110 static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50;
111 
112 const std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
113 
114 // SHA256("netgroup")[0:8]
115 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL;
116 // SHA256("localhostnonce")[0:8]
117 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL;
118 // SHA256("localhostnonce")[8:16]
119 static const uint64_t RANDOMIZER_ID_EXTRAENTROPY = 0x94b05d41679a4ff7ULL;
120 // SHA256("addrcache")[0:8]
121 static const uint64_t RANDOMIZER_ID_ADDRCACHE = 0x1cf2e4ddd306dda9ULL;
122 //
123 // Global state variables
124 //
125 bool fDiscover = true;
126 bool fListen = true;
128 std::map<CNetAddr, LocalServiceInfo>
130 static bool vfLimited[NET_MAX] GUARDED_BY(g_maplocalhost_mutex) = {};
131 
132 void CConnman::AddAddrFetch(const std::string &strDest) {
134  m_addr_fetches.push_back(strDest);
135 }
136 
137 uint16_t GetListenPort() {
138  // If -bind= is provided with ":port" part, use that (first one if multiple
139  // are provided).
140  for (const std::string &bind_arg : gArgs.GetArgs("-bind")) {
141  CService bind_addr;
142  constexpr uint16_t dummy_port = 0;
143 
144  if (Lookup(bind_arg, bind_addr, dummy_port, /*fAllowLookup=*/false)) {
145  if (bind_addr.GetPort() != dummy_port) {
146  return bind_addr.GetPort();
147  }
148  }
149  }
150 
151  // Otherwise, if -whitebind= without NetPermissionFlags::NoBan is provided,
152  // use that
153  // (-whitebind= is required to have ":port").
154  for (const std::string &whitebind_arg : gArgs.GetArgs("-whitebind")) {
155  NetWhitebindPermissions whitebind;
157  if (NetWhitebindPermissions::TryParse(whitebind_arg, whitebind,
158  error)) {
159  if (!NetPermissions::HasFlag(whitebind.m_flags,
161  return whitebind.m_service.GetPort();
162  }
163  }
164  }
165 
166  // Otherwise, if -port= is provided, use that. Otherwise use the default
167  // port.
168  return static_cast<uint16_t>(
169  gArgs.GetIntArg("-port", Params().GetDefaultPort()));
170 }
171 
172 // find 'best' local address for a particular peer
173 bool GetLocal(CService &addr, const CNetAddr *paddrPeer) {
174  if (!fListen) {
175  return false;
176  }
177 
178  int nBestScore = -1;
179  int nBestReachability = -1;
180  {
182  for (const auto &entry : mapLocalHost) {
183  int nScore = entry.second.nScore;
184  int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
185  if (nReachability > nBestReachability ||
186  (nReachability == nBestReachability && nScore > nBestScore)) {
187  addr = CService(entry.first, entry.second.nPort);
188  nBestReachability = nReachability;
189  nBestScore = nScore;
190  }
191  }
192  }
193  return nBestScore >= 0;
194 }
195 
197 static std::vector<CAddress>
198 convertSeed6(const std::vector<SeedSpec6> &vSeedsIn) {
199  // It'll only connect to one or two seed nodes because once it connects,
200  // it'll get a pile of addresses with newer timestamps. Seed nodes are given
201  // a random 'last seen time' of between one and two weeks ago.
202  const auto one_week{7 * 24h};
203  std::vector<CAddress> vSeedsOut;
204  vSeedsOut.reserve(vSeedsIn.size());
205  FastRandomContext rng;
206  for (const auto &seed_in : vSeedsIn) {
207  struct in6_addr ip;
208  memcpy(&ip, seed_in.addr, sizeof(ip));
209  CAddress addr(CService(ip, seed_in.port),
211  addr.nTime =
212  rng.rand_uniform_delay(Now<NodeSeconds>() - one_week, -one_week);
213  vSeedsOut.push_back(addr);
214  }
215  return vSeedsOut;
216 }
217 
218 // Get best local address for a particular peer as a CService. Otherwise, return
219 // the unroutable 0.0.0.0 but filled in with the normal parameters, since the IP
220 // may be changed to a useful one by discovery.
222  CService ret{CNetAddr(), GetListenPort()};
223  CService addr;
224  if (GetLocal(addr, &addrPeer)) {
225  ret = CService{addr};
226  }
227  return ret;
228 }
229 
230 static int GetnScore(const CService &addr) {
232  const auto it = mapLocalHost.find(addr);
233  return (it != mapLocalHost.end()) ? it->second.nScore : 0;
234 }
235 
236 // Is our peer's addrLocal potentially useful as an external IP source?
238  CService addrLocal = pnode->GetAddrLocal();
239  return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
240  IsReachable(addrLocal.GetNetwork());
241 }
242 
243 std::optional<CService> GetLocalAddrForPeer(CNode &node) {
244  CService addrLocal{GetLocalAddress(node.addr)};
245  if (gArgs.GetBoolArg("-addrmantest", false)) {
246  // use IPv4 loopback during addrmantest
247  addrLocal = CService(LookupNumeric("127.0.0.1", GetListenPort()));
248  }
249  // If discovery is enabled, sometimes give our peer the address it
250  // tells us that it sees us as in case it has a better idea of our
251  // address than we do.
252  FastRandomContext rng;
253  if (IsPeerAddrLocalGood(&node) &&
254  (!addrLocal.IsRoutable() ||
255  rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0)) {
256  if (node.IsInboundConn()) {
257  // For inbound connections, assume both the address and the port
258  // as seen from the peer.
259  addrLocal = CService{node.GetAddrLocal()};
260  } else {
261  // For outbound connections, assume just the address as seen from
262  // the peer and leave the port in `addrLocal` as returned by
263  // `GetLocalAddress()` above. The peer has no way to observe our
264  // listening port when we have initiated the connection.
265  addrLocal.SetIP(node.GetAddrLocal());
266  }
267  }
268  if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false)) {
269  LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n",
270  addrLocal.ToString(), node.GetId());
271  return addrLocal;
272  }
273  // Address is unroutable. Don't advertise.
274  return std::nullopt;
275 }
276 
277 // Learn a new local address.
278 bool AddLocal(const CService &addr, int nScore) {
279  if (!addr.IsRoutable()) {
280  return false;
281  }
282 
283  if (!fDiscover && nScore < LOCAL_MANUAL) {
284  return false;
285  }
286 
287  if (!IsReachable(addr)) {
288  return false;
289  }
290 
291  LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
292 
293  {
295  const auto [it, is_newly_added] =
296  mapLocalHost.emplace(addr, LocalServiceInfo());
297  LocalServiceInfo &info = it->second;
298  if (is_newly_added || nScore >= info.nScore) {
299  info.nScore = nScore + !is_newly_added;
300  info.nPort = addr.GetPort();
301  }
302  }
303 
304  return true;
305 }
306 
307 bool AddLocal(const CNetAddr &addr, int nScore) {
308  return AddLocal(CService(addr, GetListenPort()), nScore);
309 }
310 
311 void RemoveLocal(const CService &addr) {
313  LogPrintf("RemoveLocal(%s)\n", addr.ToString());
314  mapLocalHost.erase(addr);
315 }
316 
317 void SetReachable(enum Network net, bool reachable) {
318  if (net == NET_UNROUTABLE || net == NET_INTERNAL) {
319  return;
320  }
322  vfLimited[net] = !reachable;
323 }
324 
325 bool IsReachable(enum Network net) {
327  return !vfLimited[net];
328 }
329 
330 bool IsReachable(const CNetAddr &addr) {
331  return IsReachable(addr.GetNetwork());
332 }
333 
335 bool SeenLocal(const CService &addr) {
337  const auto it = mapLocalHost.find(addr);
338  if (it == mapLocalHost.end()) {
339  return false;
340  }
341  ++it->second.nScore;
342  return true;
343 }
344 
346 bool IsLocal(const CService &addr) {
348  return mapLocalHost.count(addr) > 0;
349 }
350 
353  for (CNode *pnode : m_nodes) {
354  if (static_cast<CNetAddr>(pnode->addr) == ip) {
355  return pnode;
356  }
357  }
358  return nullptr;
359 }
360 
363  for (CNode *pnode : m_nodes) {
364  if (subNet.Match(static_cast<CNetAddr>(pnode->addr))) {
365  return pnode;
366  }
367  }
368  return nullptr;
369 }
370 
371 CNode *CConnman::FindNode(const std::string &addrName) {
373  for (CNode *pnode : m_nodes) {
374  if (pnode->m_addr_name == addrName) {
375  return pnode;
376  }
377  }
378  return nullptr;
379 }
380 
383  for (CNode *pnode : m_nodes) {
384  if (static_cast<CService>(pnode->addr) == addr) {
385  return pnode;
386  }
387  }
388  return nullptr;
389 }
390 
392  return FindNode(static_cast<CNetAddr>(addr)) ||
393  FindNode(addr.ToStringIPPort());
394 }
395 
396 bool CConnman::CheckIncomingNonce(uint64_t nonce) {
398  for (const CNode *pnode : m_nodes) {
399  if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() &&
400  pnode->GetLocalNonce() == nonce) {
401  return false;
402  }
403  }
404  return true;
405 }
406 
409  CAddress addr_bind;
410  struct sockaddr_storage sockaddr_bind;
411  socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
412  if (sock != INVALID_SOCKET) {
413  if (!getsockname(sock, (struct sockaddr *)&sockaddr_bind,
414  &sockaddr_bind_len)) {
415  addr_bind.SetSockAddr((const struct sockaddr *)&sockaddr_bind);
416  } else {
417  LogPrint(BCLog::NET, "Warning: getsockname failed\n");
418  }
419  }
420  return addr_bind;
421 }
422 
423 CNode *CConnman::ConnectNode(CAddress addrConnect, const char *pszDest,
424  bool fCountFailure, ConnectionType conn_type) {
425  assert(conn_type != ConnectionType::INBOUND);
426 
427  if (pszDest == nullptr) {
428  if (IsLocal(addrConnect)) {
429  return nullptr;
430  }
431 
432  // Look for an existing connection
433  CNode *pnode = FindNode(static_cast<CService>(addrConnect));
434  if (pnode) {
435  LogPrintf("Failed to open new connection, already connected\n");
436  return nullptr;
437  }
438  }
439 
440  LogPrint(
441  BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
442  pszDest ? pszDest : addrConnect.ToString(),
443  Ticks<HoursDouble>(pszDest ? 0h : AdjustedTime() - addrConnect.nTime));
444 
445  // Resolve
446  const uint16_t default_port{pszDest != nullptr
447  ? Params().GetDefaultPort(pszDest)
448  : Params().GetDefaultPort()};
449  if (pszDest) {
450  std::vector<CService> resolved;
451  if (Lookup(pszDest, resolved, default_port,
452  fNameLookup && !HaveNameProxy(), 256) &&
453  !resolved.empty()) {
454  addrConnect =
455  CAddress(resolved[GetRand(resolved.size())], NODE_NONE);
456  if (!addrConnect.IsValid()) {
458  "Resolver returned invalid address %s for %s\n",
459  addrConnect.ToString(), pszDest);
460  return nullptr;
461  }
462  // It is possible that we already have a connection to the IP/port
463  // pszDest resolved to. In that case, drop the connection that was
464  // just created.
466  CNode *pnode = FindNode(static_cast<CService>(addrConnect));
467  if (pnode) {
468  LogPrintf("Failed to open new connection, already connected\n");
469  return nullptr;
470  }
471  }
472  }
473 
474  // Connect
475  bool connected = false;
476  std::unique_ptr<Sock> sock;
477  proxyType proxy;
478  CAddress addr_bind;
479  assert(!addr_bind.IsValid());
480 
481  if (addrConnect.IsValid()) {
482  bool proxyConnectionFailed = false;
483 
484  if (addrConnect.GetNetwork() == NET_I2P &&
485  m_i2p_sam_session.get() != nullptr) {
486  i2p::Connection conn;
487  if (m_i2p_sam_session->Connect(addrConnect, conn,
488  proxyConnectionFailed)) {
489  connected = true;
490  sock = std::move(conn.sock);
491  addr_bind = CAddress{conn.me, NODE_NONE};
492  }
493  } else if (GetProxy(addrConnect.GetNetwork(), proxy)) {
494  sock = CreateSock(proxy.proxy);
495  if (!sock) {
496  return nullptr;
497  }
498  connected = ConnectThroughProxy(
499  proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), *sock,
500  nConnectTimeout, proxyConnectionFailed);
501  } else {
502  // no proxy needed (none set for target network)
503  sock = CreateSock(addrConnect);
504  if (!sock) {
505  return nullptr;
506  }
507  connected =
508  ConnectSocketDirectly(addrConnect, *sock, nConnectTimeout,
509  conn_type == ConnectionType::MANUAL);
510  }
511  if (!proxyConnectionFailed) {
512  // If a connection to the node was attempted, and failure (if any)
513  // is not caused by a problem connecting to the proxy, mark this as
514  // an attempt.
515  addrman.Attempt(addrConnect, fCountFailure);
516  }
517  } else if (pszDest && GetNameProxy(proxy)) {
518  sock = CreateSock(proxy.proxy);
519  if (!sock) {
520  return nullptr;
521  }
522  std::string host;
523  uint16_t port{default_port};
524  SplitHostPort(std::string(pszDest), port, host);
525  bool proxyConnectionFailed;
526  connected = ConnectThroughProxy(proxy, host, port, *sock,
527  nConnectTimeout, proxyConnectionFailed);
528  }
529  if (!connected) {
530  return nullptr;
531  }
532 
533  // Add node
534  NodeId id = GetNewNodeId();
536  .Write(id)
537  .Finalize();
538  uint64_t extra_entropy =
540  .Write(id)
541  .Finalize();
542  if (!addr_bind.IsValid()) {
543  addr_bind = GetBindAddress(sock->Get());
544  }
545  CNode *pnode = new CNode(
546  id, sock->Release(), addrConnect, CalculateKeyedNetGroup(addrConnect),
547  nonce, extra_entropy, addr_bind, pszDest ? pszDest : "", conn_type,
548  /* inbound_onion */ false);
549  pnode->AddRef();
550 
551  // We're making a new connection, harvest entropy from the time (and our
552  // peer count)
553  RandAddEvent(uint32_t(id));
554 
555  return pnode;
556 }
557 
559  fDisconnect = true;
560  LOCK(cs_hSocket);
561  if (hSocket != INVALID_SOCKET) {
562  LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
563  CloseSocket(hSocket);
564  }
565 }
566 
568  const CNetAddr &addr) const {
569  for (const auto &subnet : vWhitelistedRange) {
570  if (subnet.m_subnet.Match(addr)) {
571  NetPermissions::AddFlag(flags, subnet.m_flags);
572  }
573  }
574 }
575 
576 std::string ConnectionTypeAsString(ConnectionType conn_type) {
577  switch (conn_type) {
579  return "inbound";
581  return "manual";
583  return "feeler";
585  return "outbound-full-relay";
587  return "block-relay-only";
589  return "addr-fetch";
591  return "avalanche";
592  } // no default case, so the compiler can warn about missing cases
593 
594  assert(false);
595 }
596 
600  return addrLocal;
601 }
602 
603 void CNode::SetAddrLocal(const CService &addrLocalIn) {
606  if (addrLocal.IsValid()) {
607  error("Addr local already set for node: %i. Refusing to change from %s "
608  "to %s",
609  id, addrLocal.ToString(), addrLocalIn.ToString());
610  } else {
611  addrLocal = addrLocalIn;
612  }
613 }
614 
617 }
618 
620  stats.nodeid = this->GetId();
621  stats.addr = addr;
622  stats.addrBind = addrBind;
624  stats.m_last_send = m_last_send;
625  stats.m_last_recv = m_last_recv;
629  stats.m_connected = m_connected;
630  stats.nTimeOffset = nTimeOffset;
631  stats.m_addr_name = m_addr_name;
632  stats.nVersion = nVersion;
633  {
635  stats.cleanSubVer = cleanSubVer;
636  }
637  stats.fInbound = IsInboundConn();
640  {
641  LOCK(cs_vSend);
642  stats.mapSendBytesPerMsgCmd = mapSendBytesPerMsgCmd;
643  stats.nSendBytes = nSendBytes;
644  }
645  {
646  LOCK(cs_vRecv);
647  stats.mapRecvBytesPerMsgCmd = mapRecvBytesPerMsgCmd;
648  stats.nRecvBytes = nRecvBytes;
649  }
651 
654 
655  // Leave string empty if addrLocal invalid (not filled in yet)
656  CService addrLocalUnlocked = GetAddrLocal();
657  stats.addrLocal =
658  addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
659 
660  stats.m_conn_type = m_conn_type;
661 
663  ? std::make_optional(getAvailabilityScore())
664  : std::nullopt;
665 }
666 
667 bool CNode::ReceiveMsgBytes(const Config &config, Span<const uint8_t> msg_bytes,
668  bool &complete) {
669  complete = false;
670  const auto time = GetTime<std::chrono::microseconds>();
671  LOCK(cs_vRecv);
672  m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
673  nRecvBytes += msg_bytes.size();
674  while (msg_bytes.size() > 0) {
675  // Absorb network data.
676  int handled = m_deserializer->Read(config, msg_bytes);
677  if (handled < 0) {
678  return false;
679  }
680 
681  if (m_deserializer->Complete()) {
682  // decompose a transport agnostic CNetMessage from the deserializer
683  CNetMessage msg = m_deserializer->GetMessage(config, time);
684 
685  // Store received bytes per message command to prevent a memory DOS,
686  // only allow valid commands.
687  mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.m_type);
688  if (i == mapRecvBytesPerMsgCmd.end()) {
689  i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
690  }
691 
692  assert(i != mapRecvBytesPerMsgCmd.end());
693  i->second += msg.m_raw_message_size;
694 
695  // push the message to the process queue,
696  vRecvMsg.push_back(std::move(msg));
697 
698  complete = true;
699  }
700  }
701 
702  return true;
703 }
704 
706  Span<const uint8_t> msg_bytes) {
707  // copy data to temporary parsing buffer
708  uint32_t nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
709  uint32_t nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
710 
711  memcpy(&hdrbuf[nHdrPos], msg_bytes.data(), nCopy);
712  nHdrPos += nCopy;
713 
714  // if header incomplete, exit
716  return nCopy;
717  }
718 
719  // deserialize to CMessageHeader
720  try {
721  hdrbuf >> hdr;
722  } catch (const std::exception &) {
723  return -1;
724  }
725 
726  // Reject oversized messages
727  if (hdr.IsOversized(config)) {
728  LogPrint(BCLog::NET, "Oversized header detected\n");
729  return -1;
730  }
731 
732  // switch state to reading message data
733  in_data = true;
734 
735  return nCopy;
736 }
737 
739  unsigned int nRemaining = hdr.nMessageSize - nDataPos;
740  unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
741 
742  if (vRecv.size() < nDataPos + nCopy) {
743  // Allocate up to 256 KiB ahead, but never more than the total message
744  // size.
745  vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
746  }
747 
748  hasher.Write(msg_bytes.first(nCopy));
749  memcpy(&vRecv[nDataPos], msg_bytes.data(), nCopy);
750  nDataPos += nCopy;
751 
752  return nCopy;
753 }
754 
756  assert(Complete());
757  if (data_hash.IsNull()) {
759  }
760  return data_hash;
761 }
762 
765  const std::chrono::microseconds time) {
766  // decompose a single CNetMessage from the TransportDeserializer
767  CNetMessage msg(std::move(vRecv));
768 
769  // store state about valid header, netmagic and checksum
770  msg.m_valid_header = hdr.IsValid(config);
771  // FIXME Split CheckHeaderMagicAndCommand() into CheckHeaderMagic() and
772  // CheckCommand() to prevent the net magic check code duplication.
773  msg.m_valid_netmagic =
774  (memcmp(std::begin(hdr.pchMessageStart),
775  std::begin(config.GetChainParams().NetMagic()),
777  uint256 hash = GetMessageHash();
778 
779  // store command string, payload size
780  msg.m_type = hdr.GetCommand();
783 
784  // We just received a message off the wire, harvest entropy from the time
785  // (and the message checksum)
786  RandAddEvent(ReadLE32(hash.begin()));
787 
788  msg.m_valid_checksum = (memcmp(hash.begin(), hdr.pchChecksum,
790 
791  if (!msg.m_valid_checksum) {
793  "CHECKSUM ERROR (%s, %u bytes), expected %s was %s\n",
797  }
798 
799  // store receive time
800  msg.m_time = time;
801 
802  // reset the network deserializer (prepare for the next message)
803  Reset();
804  return msg;
805 }
806 
808  CSerializedNetMsg &msg,
809  std::vector<uint8_t> &header) {
810  // create dbl-sha256 checksum
811  uint256 hash = Hash(msg.data);
812 
813  // create header
814  CMessageHeader hdr(config.GetChainParams().NetMagic(), msg.m_type.c_str(),
815  msg.data.size());
816  memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
817 
818  // serialize header
819  header.reserve(CMessageHeader::HEADER_SIZE);
820  CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, header, 0, hdr};
821 }
822 
824  size_t nSentSize = 0;
825  size_t nMsgCount = 0;
826 
827  for (const auto &data : node.vSendMsg) {
828  assert(data.size() > node.nSendOffset);
829  int nBytes = 0;
830 
831  {
832  LOCK(node.cs_hSocket);
833  if (node.hSocket == INVALID_SOCKET) {
834  break;
835  }
836 
837  nBytes = send(
838  node.hSocket,
839  reinterpret_cast<const char *>(data.data()) + node.nSendOffset,
840  data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
841  }
842 
843  if (nBytes == 0) {
844  // couldn't send anything at all
845  break;
846  }
847 
848  if (nBytes < 0) {
849  // error
850  int nErr = WSAGetLastError();
851  if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE &&
852  nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
853  LogPrint(BCLog::NET, "socket send error for peer=%d: %s\n",
854  node.GetId(), NetworkErrorString(nErr));
855  node.CloseSocketDisconnect();
856  }
857 
858  break;
859  }
860 
861  assert(nBytes > 0);
862  node.m_last_send = GetTime<std::chrono::seconds>();
863  node.nSendBytes += nBytes;
864  node.nSendOffset += nBytes;
865  nSentSize += nBytes;
866  if (node.nSendOffset != data.size()) {
867  // could not send full message; stop sending more
868  break;
869  }
870 
871  node.nSendOffset = 0;
872  node.nSendSize -= data.size();
873  node.fPauseSend = node.nSendSize > nSendBufferMaxSize;
874  nMsgCount++;
875  }
876 
877  node.vSendMsg.erase(node.vSendMsg.begin(),
878  node.vSendMsg.begin() + nMsgCount);
879 
880  if (node.vSendMsg.empty()) {
881  assert(node.nSendOffset == 0);
882  assert(node.nSendSize == 0);
883  }
884 
885  return nSentSize;
886 }
887 
889  const NodeEvictionCandidate &b) {
890  return a.m_min_ping_time > b.m_min_ping_time;
891 }
892 
894  const NodeEvictionCandidate &b) {
895  return a.m_connected > b.m_connected;
896 }
897 
899  const NodeEvictionCandidate &b) {
900  return a.nKeyedNetGroup < b.nKeyedNetGroup;
901 }
902 
904  const NodeEvictionCandidate &b) {
905  // There is a fall-through here because it is common for a node to have many
906  // peers which have not yet relayed a block.
909  }
910 
912  return b.fRelevantServices;
913  }
914 
915  return a.m_connected > b.m_connected;
916 }
917 
919  const NodeEvictionCandidate &b) {
920  // There is a fall-through here because it is common for a node to have more
921  // than a few peers that have not yet relayed txn.
922  if (a.m_last_tx_time != b.m_last_tx_time) {
923  return a.m_last_tx_time < b.m_last_tx_time;
924  }
925 
926  if (a.m_relay_txs != b.m_relay_txs) {
927  return b.m_relay_txs;
928  }
929 
930  if (a.fBloomFilter != b.fBloomFilter) {
931  return a.fBloomFilter;
932  }
933 
934  return a.m_connected > b.m_connected;
935 }
936 
938  const NodeEvictionCandidate &b) {
939  // There is a fall-through here because it is common for a node to have more
940  // than a few peers that have not yet relayed proofs. This fallback is also
941  // used in the case avalanche is not enabled.
944  }
945 
946  return a.m_connected > b.m_connected;
947 }
948 
949 // Pick out the potential block-relay only peers, and sort them by last block
950 // time.
952  const NodeEvictionCandidate &b) {
953  if (a.m_relay_txs != b.m_relay_txs) {
954  return a.m_relay_txs;
955  }
956 
959  }
960 
962  return b.fRelevantServices;
963  }
964 
965  return a.m_connected > b.m_connected;
966 }
967 
969  const NodeEvictionCandidate &b) {
970  // Equality can happen if the nodes have no score or it has not been
971  // computed yet.
974  }
975 
976  return a.m_connected > b.m_connected;
977 }
978 
990  const bool m_is_local;
992  CompareNodeNetworkTime(bool is_local, Network network)
993  : m_is_local(is_local), m_network(network) {}
995  const NodeEvictionCandidate &b) const {
996  if (m_is_local && a.m_is_local != b.m_is_local) {
997  return b.m_is_local;
998  }
999  if ((a.m_network == m_network) != (b.m_network == m_network)) {
1000  return b.m_network == m_network;
1001  }
1002  return a.m_connected > b.m_connected;
1003  };
1004 };
1005 
1008 template <typename T, typename Comparator>
1010  std::vector<T> &elements, Comparator comparator, size_t k,
1011  std::function<bool(const NodeEvictionCandidate &)> predicate =
1012  [](const NodeEvictionCandidate &n) { return true; }) {
1013  std::sort(elements.begin(), elements.end(), comparator);
1014  size_t eraseSize = std::min(k, elements.size());
1015  elements.erase(
1016  std::remove_if(elements.end() - eraseSize, elements.end(), predicate),
1017  elements.end());
1018 }
1019 
1021  std::vector<NodeEvictionCandidate> &eviction_candidates) {
1022  // Protect the half of the remaining nodes which have been connected the
1023  // longest. This replicates the non-eviction implicit behavior, and
1024  // precludes attacks that start later.
1025  // To promote the diversity of our peer connections, reserve up to half of
1026  // these protected spots for Tor/onion, localhost and I2P peers, even if
1027  // they're not the longest uptime overall. This helps protect these
1028  // higher-latency peers that tend to be otherwise disadvantaged under our
1029  // eviction criteria.
1030  const size_t initial_size = eviction_candidates.size();
1031  const size_t total_protect_size{initial_size / 2};
1032 
1033  // Disadvantaged networks to protect: I2P, localhost and Tor/onion. In case
1034  // of equal counts, earlier array members have first opportunity to recover
1035  // unused slots from the previous iteration.
1036  struct Net {
1037  bool is_local;
1038  Network id;
1039  size_t count;
1040  };
1041  std::array<Net, 3> networks{{{false, NET_I2P, 0},
1042  {/* localhost */ true, NET_MAX, 0},
1043  {false, NET_ONION, 0}}};
1044 
1045  // Count and store the number of eviction candidates per network.
1046  for (Net &n : networks) {
1047  n.count = std::count_if(
1048  eviction_candidates.cbegin(), eviction_candidates.cend(),
1049  [&n](const NodeEvictionCandidate &c) {
1050  return n.is_local ? c.m_is_local : c.m_network == n.id;
1051  });
1052  }
1053  // Sort `networks` by ascending candidate count, to give networks having
1054  // fewer candidates the first opportunity to recover unused protected slots
1055  // from the previous iteration.
1056  std::stable_sort(networks.begin(), networks.end(),
1057  [](Net a, Net b) { return a.count < b.count; });
1058 
1059  // Protect up to 25% of the eviction candidates by disadvantaged network.
1060  const size_t max_protect_by_network{total_protect_size / 2};
1061  size_t num_protected{0};
1062 
1063  while (num_protected < max_protect_by_network) {
1064  // Count the number of disadvantaged networks from which we have peers
1065  // to protect.
1066  auto num_networks = std::count_if(networks.begin(), networks.end(),
1067  [](const Net &n) { return n.count; });
1068  if (num_networks == 0) {
1069  break;
1070  }
1071  const size_t disadvantaged_to_protect{max_protect_by_network -
1072  num_protected};
1073  const size_t protect_per_network{std::max(
1074  disadvantaged_to_protect / num_networks, static_cast<size_t>(1))};
1075 
1076  // Early exit flag if there are no remaining candidates by disadvantaged
1077  // network.
1078  bool protected_at_least_one{false};
1079 
1080  for (Net &n : networks) {
1081  if (n.count == 0) {
1082  continue;
1083  }
1084  const size_t before = eviction_candidates.size();
1086  eviction_candidates, CompareNodeNetworkTime(n.is_local, n.id),
1087  protect_per_network, [&n](const NodeEvictionCandidate &c) {
1088  return n.is_local ? c.m_is_local : c.m_network == n.id;
1089  });
1090  const size_t after = eviction_candidates.size();
1091  if (before > after) {
1092  protected_at_least_one = true;
1093  const size_t delta{before - after};
1094  num_protected += delta;
1095  if (num_protected >= max_protect_by_network) {
1096  break;
1097  }
1098  n.count -= delta;
1099  }
1100  }
1101  if (!protected_at_least_one) {
1102  break;
1103  }
1104  }
1105 
1106  // Calculate how many we removed, and update our total number of peers that
1107  // we want to protect based on uptime accordingly.
1108  assert(num_protected == initial_size - eviction_candidates.size());
1109  const size_t remaining_to_protect{total_protect_size - num_protected};
1111  remaining_to_protect);
1112 }
1113 
1114 [[nodiscard]] std::optional<NodeId>
1115 SelectNodeToEvict(std::vector<NodeEvictionCandidate> &&vEvictionCandidates) {
1116  // Protect connections with certain characteristics
1117 
1118  // Deterministically select 4 peers to protect by netgroup.
1119  // An attacker cannot predict which netgroups will be protected
1120  EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
1121  // Protect the 8 nodes with the lowest minimum ping time.
1122  // An attacker cannot manipulate this metric without physically moving nodes
1123  // closer to the target.
1124  EraseLastKElements(vEvictionCandidates, ReverseCompareNodeMinPingTime, 8);
1125  // Protect 4 nodes that most recently sent us novel transactions accepted
1126  // into our mempool. An attacker cannot manipulate this metric without
1127  // performing useful work.
1128  EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4);
1129  // Protect 4 nodes that most recently sent us novel proofs accepted
1130  // into our proof pool. An attacker cannot manipulate this metric without
1131  // performing useful work.
1132  // TODO this filter must happen before the last tx time once avalanche is
1133  // enabled for pre-consensus.
1134  EraseLastKElements(vEvictionCandidates, CompareNodeProofTime, 4);
1135  // Protect up to 8 non-tx-relay peers that have sent us novel blocks.
1136  EraseLastKElements(vEvictionCandidates, CompareNodeBlockRelayOnlyTime, 8,
1137  [](const NodeEvictionCandidate &n) {
1138  return !n.m_relay_txs && n.fRelevantServices;
1139  });
1140 
1141  // Protect 4 nodes that most recently sent us novel blocks.
1142  // An attacker cannot manipulate this metric without performing useful work.
1143  EraseLastKElements(vEvictionCandidates, CompareNodeBlockTime, 4);
1144 
1145  // Protect up to 128 nodes that have the highest avalanche availability
1146  // score.
1147  EraseLastKElements(vEvictionCandidates, CompareNodeAvailabilityScore, 128,
1148  [](NodeEvictionCandidate const &n) {
1149  return n.availabilityScore > 0.;
1150  });
1151 
1152  // Protect some of the remaining eviction candidates by ratios of desirable
1153  // or disadvantaged characteristics.
1154  ProtectEvictionCandidatesByRatio(vEvictionCandidates);
1155 
1156  if (vEvictionCandidates.empty()) {
1157  return std::nullopt;
1158  }
1159 
1160  // If any remaining peers are preferred for eviction consider only them.
1161  // This happens after the other preferences since if a peer is really the
1162  // best by other criteria (esp relaying blocks)
1163  // then we probably don't want to evict it no matter what.
1164  if (std::any_of(
1165  vEvictionCandidates.begin(), vEvictionCandidates.end(),
1166  [](NodeEvictionCandidate const &n) { return n.prefer_evict; })) {
1167  vEvictionCandidates.erase(
1168  std::remove_if(
1169  vEvictionCandidates.begin(), vEvictionCandidates.end(),
1170  [](NodeEvictionCandidate const &n) { return !n.prefer_evict; }),
1171  vEvictionCandidates.end());
1172  }
1173 
1174  // Identify the network group with the most connections and youngest member.
1175  // (vEvictionCandidates is already sorted by reverse connect time)
1176  uint64_t naMostConnections;
1177  unsigned int nMostConnections = 0;
1178  std::chrono::seconds nMostConnectionsTime{0};
1179  std::map<uint64_t, std::vector<NodeEvictionCandidate>> mapNetGroupNodes;
1180  for (const NodeEvictionCandidate &node : vEvictionCandidates) {
1181  std::vector<NodeEvictionCandidate> &group =
1182  mapNetGroupNodes[node.nKeyedNetGroup];
1183  group.push_back(node);
1184  const auto grouptime{group[0].m_connected};
1185  size_t group_size = group.size();
1186  if (group_size > nMostConnections ||
1187  (group_size == nMostConnections &&
1188  grouptime > nMostConnectionsTime)) {
1189  nMostConnections = group_size;
1190  nMostConnectionsTime = grouptime;
1191  naMostConnections = node.nKeyedNetGroup;
1192  }
1193  }
1194 
1195  // Reduce to the network group with the most connections
1196  vEvictionCandidates = std::move(mapNetGroupNodes[naMostConnections]);
1197 
1198  // Disconnect from the network group with the most connections
1199  return vEvictionCandidates.front().id;
1200 }
1201 
1211  std::vector<NodeEvictionCandidate> vEvictionCandidates;
1212  {
1214  for (const CNode *node : m_nodes) {
1215  if (node->HasPermission(NetPermissionFlags::NoBan)) {
1216  continue;
1217  }
1218  if (!node->IsInboundConn()) {
1219  continue;
1220  }
1221  if (node->fDisconnect) {
1222  continue;
1223  }
1224 
1225  NodeEvictionCandidate candidate = {
1226  node->GetId(),
1227  node->m_connected,
1228  node->m_min_ping_time,
1229  node->m_last_block_time,
1230  node->m_last_proof_time,
1231  node->m_last_tx_time,
1232  node->m_has_all_wanted_services,
1233  node->m_relays_txs.load(),
1234  node->m_bloom_filter_loaded.load(),
1235  node->nKeyedNetGroup,
1236  node->m_prefer_evict,
1237  node->addr.IsLocal(),
1238  node->ConnectedThroughNetwork(),
1239  node->m_avalanche_enabled
1240  ? node->getAvailabilityScore()
1241  : -std::numeric_limits<double>::infinity()};
1242  vEvictionCandidates.push_back(candidate);
1243  }
1244  }
1245  const std::optional<NodeId> node_id_to_evict =
1246  SelectNodeToEvict(std::move(vEvictionCandidates));
1247  if (!node_id_to_evict) {
1248  return false;
1249  }
1251  for (CNode *pnode : m_nodes) {
1252  if (pnode->GetId() == *node_id_to_evict) {
1253  LogPrint(
1254  BCLog::NET,
1255  "selected %s connection for eviction peer=%d; disconnecting\n",
1256  pnode->ConnectionTypeAsString(), pnode->GetId());
1257  pnode->fDisconnect = true;
1258  return true;
1259  }
1260  }
1261  return false;
1262 }
1263 
1264 void CConnman::AcceptConnection(const ListenSocket &hListenSocket) {
1265  struct sockaddr_storage sockaddr;
1266  socklen_t len = sizeof(sockaddr);
1267  SOCKET hSocket =
1268  accept(hListenSocket.socket, (struct sockaddr *)&sockaddr, &len);
1269  CAddress addr;
1270 
1271  if (hSocket == INVALID_SOCKET) {
1272  const int nErr = WSAGetLastError();
1273  if (nErr != WSAEWOULDBLOCK) {
1274  LogPrintf("socket error accept failed: %s\n",
1275  NetworkErrorString(nErr));
1276  }
1277  return;
1278  }
1279 
1280  if (!addr.SetSockAddr((const struct sockaddr *)&sockaddr)) {
1281  LogPrintf("Warning: Unknown socket family\n");
1282  }
1283 
1284  const CAddress addr_bind = GetBindAddress(hSocket);
1285 
1286  NetPermissionFlags permissionFlags = NetPermissionFlags::None;
1287  hListenSocket.AddSocketPermissionFlags(permissionFlags);
1288 
1289  CreateNodeFromAcceptedSocket(hSocket, permissionFlags, addr_bind, addr);
1290 }
1291 
1293  NetPermissionFlags permissionFlags,
1294  const CAddress &addr_bind,
1295  const CAddress &addr) {
1296  int nInbound = 0;
1297  int nMaxInbound = nMaxConnections - m_max_outbound;
1298 
1299  AddWhitelistPermissionFlags(permissionFlags, addr);
1300  if (NetPermissions::HasFlag(permissionFlags,
1302  NetPermissions::ClearFlag(permissionFlags,
1304  if (gArgs.GetBoolArg("-whitelistforcerelay",
1306  NetPermissions::AddFlag(permissionFlags,
1308  }
1309  if (gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)) {
1311  }
1314  }
1315 
1316  {
1318  for (const CNode *pnode : m_nodes) {
1319  if (pnode->IsInboundConn()) {
1320  nInbound++;
1321  }
1322  }
1323  }
1324 
1325  if (!fNetworkActive) {
1327  "connection from %s dropped: not accepting new connections\n",
1328  addr.ToString());
1329  CloseSocket(hSocket);
1330  return;
1331  }
1332 
1333  if (!IsSelectableSocket(hSocket)) {
1334  LogPrintf("connection from %s dropped: non-selectable socket\n",
1335  addr.ToString());
1336  CloseSocket(hSocket);
1337  return;
1338  }
1339 
1340  // According to the internet TCP_NODELAY is not carried into accepted
1341  // sockets on all platforms. Set it again here just to be sure.
1342  SetSocketNoDelay(hSocket);
1343 
1344  // Don't accept connections from banned peers.
1345  bool banned = m_banman && m_banman->IsBanned(addr);
1346  if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::NoBan) &&
1347  banned) {
1348  LogPrint(BCLog::NET, "connection from %s dropped (banned)\n",
1349  addr.ToString());
1350  CloseSocket(hSocket);
1351  return;
1352  }
1353 
1354  // Only accept connections from discouraged peers if our inbound slots
1355  // aren't (almost) full.
1356  bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
1357  if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::NoBan) &&
1358  nInbound + 1 >= nMaxInbound && discouraged) {
1359  LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n",
1360  addr.ToString());
1361  CloseSocket(hSocket);
1362  return;
1363  }
1364 
1365  if (nInbound >= nMaxInbound) {
1366  if (!AttemptToEvictConnection()) {
1367  // No connection to evict, disconnect the new connection
1368  LogPrint(BCLog::NET, "failed to find an eviction candidate - "
1369  "connection dropped (full)\n");
1370  CloseSocket(hSocket);
1371  return;
1372  }
1373  }
1374 
1375  NodeId id = GetNewNodeId();
1377  .Write(id)
1378  .Finalize();
1379  uint64_t extra_entropy =
1381  .Write(id)
1382  .Finalize();
1383 
1384  ServiceFlags nodeServices = nLocalServices;
1385  if (NetPermissions::HasFlag(permissionFlags,
1387  nodeServices = static_cast<ServiceFlags>(nodeServices | NODE_BLOOM);
1388  }
1389 
1390  const bool inbound_onion =
1391  std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) !=
1392  m_onion_binds.end();
1393  CNode *pnode = new CNode(id, hSocket, addr, CalculateKeyedNetGroup(addr),
1394  nonce, extra_entropy, addr_bind, "",
1395  ConnectionType::INBOUND, inbound_onion);
1396  pnode->AddRef();
1397  pnode->m_permissionFlags = permissionFlags;
1398  pnode->m_prefer_evict = discouraged;
1399  for (auto interface : m_msgproc) {
1400  interface->InitializeNode(*config, *pnode, nodeServices);
1401  }
1402 
1403  LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
1404 
1405  {
1407  m_nodes.push_back(pnode);
1408  }
1409 
1410  // We received a new connection, harvest entropy from the time (and our peer
1411  // count)
1412  RandAddEvent(uint32_t(id));
1413 }
1414 
1415 bool CConnman::AddConnection(const std::string &address,
1416  ConnectionType conn_type) {
1417  std::optional<int> max_connections;
1418  switch (conn_type) {
1421  return false;
1423  max_connections = m_max_outbound_full_relay;
1424  break;
1426  max_connections = m_max_outbound_block_relay;
1427  break;
1428  // no limit for ADDR_FETCH because -seednode has no limit either
1430  break;
1431  // no limit for FEELER connections since they're short-lived
1433  break;
1435  max_connections = m_max_avalanche_outbound;
1436  break;
1437  } // no default case, so the compiler can warn about missing cases
1438 
1439  // Count existing connections
1440  int existing_connections =
1442  return std::count_if(
1443  m_nodes.begin(), m_nodes.end(), [conn_type](CNode *node) {
1444  return node->m_conn_type == conn_type;
1445  }););
1446 
1447  // Max connections of specified type already exist
1448  if (max_connections != std::nullopt &&
1449  existing_connections >= max_connections) {
1450  return false;
1451  }
1452 
1453  // Max total outbound connections already exist
1454  CSemaphoreGrant grant(*semOutbound, true);
1455  if (!grant) {
1456  return false;
1457  }
1458 
1459  OpenNetworkConnection(CAddress(), false, &grant, address.c_str(),
1460  conn_type);
1461  return true;
1462 }
1463 
1465  {
1467 
1468  if (!fNetworkActive) {
1469  // Disconnect any connected nodes
1470  for (CNode *pnode : m_nodes) {
1471  if (!pnode->fDisconnect) {
1473  "Network not active, dropping peer=%d\n",
1474  pnode->GetId());
1475  pnode->fDisconnect = true;
1476  }
1477  }
1478  }
1479 
1480  // Disconnect unused nodes
1481  std::vector<CNode *> nodes_copy = m_nodes;
1482  for (CNode *pnode : nodes_copy) {
1483  if (pnode->fDisconnect) {
1484  // remove from m_nodes
1485  m_nodes.erase(remove(m_nodes.begin(), m_nodes.end(), pnode),
1486  m_nodes.end());
1487 
1488  // release outbound grant (if any)
1489  pnode->grantOutbound.Release();
1490 
1491  // close socket and cleanup
1492  pnode->CloseSocketDisconnect();
1493 
1494  // hold in disconnected pool until all refs are released
1495  pnode->Release();
1496  m_nodes_disconnected.push_back(pnode);
1497  }
1498  }
1499  }
1500  {
1501  // Delete disconnected nodes
1502  std::list<CNode *> nodes_disconnected_copy = m_nodes_disconnected;
1503  for (CNode *pnode : nodes_disconnected_copy) {
1504  // Destroy the object only after other threads have stopped using
1505  // it.
1506  if (pnode->GetRefCount() <= 0) {
1507  m_nodes_disconnected.remove(pnode);
1508  DeleteNode(pnode);
1509  }
1510  }
1511  }
1512 }
1513 
1515  size_t nodes_size;
1516  {
1518  nodes_size = m_nodes.size();
1519  }
1520  if (nodes_size != nPrevNodeCount) {
1521  nPrevNodeCount = nodes_size;
1522  if (m_client_interface) {
1523  m_client_interface->NotifyNumConnectionsChanged(nodes_size);
1524  }
1525  }
1526 }
1527 
1529  std::chrono::seconds now) const {
1530  return node.m_connected + m_peer_connect_timeout < now;
1531 }
1532 
1534  // Tests that see disconnects after using mocktime can start nodes with a
1535  // large timeout. For example, -peertimeout=999999999.
1536  const auto now{GetTime<std::chrono::seconds>()};
1537  const auto last_send{node.m_last_send.load()};
1538  const auto last_recv{node.m_last_recv.load()};
1539 
1540  if (!ShouldRunInactivityChecks(node, now)) {
1541  return false;
1542  }
1543 
1544  if (last_recv.count() == 0 || last_send.count() == 0) {
1546  "socket no message in first %i seconds, %d %d peer=%d\n",
1547  count_seconds(m_peer_connect_timeout), last_recv.count() != 0,
1548  last_send.count() != 0, node.GetId());
1549  return true;
1550  }
1551 
1552  if (now > last_send + TIMEOUT_INTERVAL) {
1553  LogPrint(BCLog::NET, "socket sending timeout: %is peer=%d\n",
1554  count_seconds(now - last_send), node.GetId());
1555  return true;
1556  }
1557 
1558  if (now > last_recv + TIMEOUT_INTERVAL) {
1559  LogPrint(BCLog::NET, "socket receive timeout: %is peer=%d\n",
1560  count_seconds(now - last_recv), node.GetId());
1561  return true;
1562  }
1563 
1564  if (!node.fSuccessfullyConnected) {
1565  LogPrint(BCLog::NET, "version handshake timeout peer=%d\n",
1566  node.GetId());
1567  return true;
1568  }
1569 
1570  return false;
1571 }
1572 
1573 bool CConnman::GenerateSelectSet(std::set<SOCKET> &recv_set,
1574  std::set<SOCKET> &send_set,
1575  std::set<SOCKET> &error_set) {
1576  for (const ListenSocket &hListenSocket : vhListenSocket) {
1577  recv_set.insert(hListenSocket.socket);
1578  }
1579 
1580  {
1582  for (CNode *pnode : m_nodes) {
1583  // Implement the following logic:
1584  // * If there is data to send, select() for sending data. As this
1585  // only happens when optimistic write failed, we choose to first
1586  // drain the write buffer in this case before receiving more. This
1587  // avoids needlessly queueing received data, if the remote peer is
1588  // not themselves receiving data. This means properly utilizing
1589  // TCP flow control signalling.
1590  // * Otherwise, if there is space left in the receive buffer,
1591  // select() for receiving data.
1592  // * Hand off all complete messages to the processor, to be handled
1593  // without blocking here.
1594 
1595  bool select_recv = !pnode->fPauseRecv;
1596  bool select_send;
1597  {
1598  LOCK(pnode->cs_vSend);
1599  select_send = !pnode->vSendMsg.empty();
1600  }
1601 
1602  LOCK(pnode->cs_hSocket);
1603  if (pnode->hSocket == INVALID_SOCKET) {
1604  continue;
1605  }
1606 
1607  error_set.insert(pnode->hSocket);
1608  if (select_send) {
1609  send_set.insert(pnode->hSocket);
1610  continue;
1611  }
1612  if (select_recv) {
1613  recv_set.insert(pnode->hSocket);
1614  }
1615  }
1616  }
1617 
1618  return !recv_set.empty() || !send_set.empty() || !error_set.empty();
1619 }
1620 
1621 #ifdef USE_POLL
1622 void CConnman::SocketEvents(std::set<SOCKET> &recv_set,
1623  std::set<SOCKET> &send_set,
1624  std::set<SOCKET> &error_set) {
1625  std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
1626  if (!GenerateSelectSet(recv_select_set, send_select_set,
1627  error_select_set)) {
1629  std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS));
1630  return;
1631  }
1632 
1633  std::unordered_map<SOCKET, struct pollfd> pollfds;
1634  for (SOCKET socket_id : recv_select_set) {
1635  pollfds[socket_id].fd = socket_id;
1636  pollfds[socket_id].events |= POLLIN;
1637  }
1638 
1639  for (SOCKET socket_id : send_select_set) {
1640  pollfds[socket_id].fd = socket_id;
1641  pollfds[socket_id].events |= POLLOUT;
1642  }
1643 
1644  for (SOCKET socket_id : error_select_set) {
1645  pollfds[socket_id].fd = socket_id;
1646  // These flags are ignored, but we set them for clarity
1647  pollfds[socket_id].events |= POLLERR | POLLHUP;
1648  }
1649 
1650  std::vector<struct pollfd> vpollfds;
1651  vpollfds.reserve(pollfds.size());
1652  for (auto it : pollfds) {
1653  vpollfds.push_back(std::move(it.second));
1654  }
1655 
1656  if (poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS) <
1657  0) {
1658  return;
1659  }
1660 
1661  if (interruptNet) {
1662  return;
1663  }
1664 
1665  for (struct pollfd pollfd_entry : vpollfds) {
1666  if (pollfd_entry.revents & POLLIN) {
1667  recv_set.insert(pollfd_entry.fd);
1668  }
1669  if (pollfd_entry.revents & POLLOUT) {
1670  send_set.insert(pollfd_entry.fd);
1671  }
1672  if (pollfd_entry.revents & (POLLERR | POLLHUP)) {
1673  error_set.insert(pollfd_entry.fd);
1674  }
1675  }
1676 }
1677 #else
1678 void CConnman::SocketEvents(std::set<SOCKET> &recv_set,
1679  std::set<SOCKET> &send_set,
1680  std::set<SOCKET> &error_set) {
1681  std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
1682  if (!GenerateSelectSet(recv_select_set, send_select_set,
1683  error_select_set)) {
1685  std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS));
1686  return;
1687  }
1688 
1689  //
1690  // Find which sockets have data to receive
1691  //
1692  struct timeval timeout;
1693  timeout.tv_sec = 0;
1694  // frequency to poll pnode->vSend
1695  timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000;
1696 
1697  fd_set fdsetRecv;
1698  fd_set fdsetSend;
1699  fd_set fdsetError;
1700  FD_ZERO(&fdsetRecv);
1701  FD_ZERO(&fdsetSend);
1702  FD_ZERO(&fdsetError);
1703  SOCKET hSocketMax = 0;
1704 
1705  for (SOCKET hSocket : recv_select_set) {
1706  FD_SET(hSocket, &fdsetRecv);
1707  hSocketMax = std::max(hSocketMax, hSocket);
1708  }
1709 
1710  for (SOCKET hSocket : send_select_set) {
1711  FD_SET(hSocket, &fdsetSend);
1712  hSocketMax = std::max(hSocketMax, hSocket);
1713  }
1714 
1715  for (SOCKET hSocket : error_select_set) {
1716  FD_SET(hSocket, &fdsetError);
1717  hSocketMax = std::max(hSocketMax, hSocket);
1718  }
1719 
1720  int nSelect =
1721  select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1722 
1723  if (interruptNet) {
1724  return;
1725  }
1726 
1727  if (nSelect == SOCKET_ERROR) {
1728  int nErr = WSAGetLastError();
1729  LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
1730  for (unsigned int i = 0; i <= hSocketMax; i++) {
1731  FD_SET(i, &fdsetRecv);
1732  }
1733  FD_ZERO(&fdsetSend);
1734  FD_ZERO(&fdsetError);
1735  if (!interruptNet.sleep_for(
1736  std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS))) {
1737  return;
1738  }
1739  }
1740 
1741  for (SOCKET hSocket : recv_select_set) {
1742  if (FD_ISSET(hSocket, &fdsetRecv)) {
1743  recv_set.insert(hSocket);
1744  }
1745  }
1746 
1747  for (SOCKET hSocket : send_select_set) {
1748  if (FD_ISSET(hSocket, &fdsetSend)) {
1749  send_set.insert(hSocket);
1750  }
1751  }
1752 
1753  for (SOCKET hSocket : error_select_set) {
1754  if (FD_ISSET(hSocket, &fdsetError)) {
1755  error_set.insert(hSocket);
1756  }
1757  }
1758 }
1759 #endif
1760 
1762  std::set<SOCKET> recv_set, send_set, error_set;
1763  SocketEvents(recv_set, send_set, error_set);
1764 
1765  if (interruptNet) {
1766  return;
1767  }
1768 
1769  //
1770  // Accept new connections
1771  //
1772  for (const ListenSocket &hListenSocket : vhListenSocket) {
1773  if (hListenSocket.socket != INVALID_SOCKET &&
1774  recv_set.count(hListenSocket.socket) > 0) {
1775  AcceptConnection(hListenSocket);
1776  }
1777  }
1778 
1779  //
1780  // Service each socket
1781  //
1782  std::vector<CNode *> nodes_copy;
1783  {
1785  nodes_copy = m_nodes;
1786  for (CNode *pnode : nodes_copy) {
1787  pnode->AddRef();
1788  }
1789  }
1790  for (CNode *pnode : nodes_copy) {
1791  if (interruptNet) {
1792  return;
1793  }
1794 
1795  //
1796  // Receive
1797  //
1798  bool recvSet = false;
1799  bool sendSet = false;
1800  bool errorSet = false;
1801  {
1802  LOCK(pnode->cs_hSocket);
1803  if (pnode->hSocket == INVALID_SOCKET) {
1804  continue;
1805  }
1806  recvSet = recv_set.count(pnode->hSocket) > 0;
1807  sendSet = send_set.count(pnode->hSocket) > 0;
1808  errorSet = error_set.count(pnode->hSocket) > 0;
1809  }
1810  if (recvSet || errorSet) {
1811  // typical socket buffer is 8K-64K
1812  uint8_t pchBuf[0x10000];
1813  int32_t nBytes = 0;
1814  {
1815  LOCK(pnode->cs_hSocket);
1816  if (pnode->hSocket == INVALID_SOCKET) {
1817  continue;
1818  }
1819  nBytes = recv(pnode->hSocket, (char *)pchBuf, sizeof(pchBuf),
1820  MSG_DONTWAIT);
1821  }
1822  if (nBytes > 0) {
1823  bool notify = false;
1824  if (!pnode->ReceiveMsgBytes(*config, {pchBuf, (size_t)nBytes},
1825  notify)) {
1826  pnode->CloseSocketDisconnect();
1827  }
1828  RecordBytesRecv(nBytes);
1829  if (notify) {
1830  size_t nSizeAdded = 0;
1831  auto it(pnode->vRecvMsg.begin());
1832  for (; it != pnode->vRecvMsg.end(); ++it) {
1833  // vRecvMsg contains only completed CNetMessage
1834  // the single possible partially deserialized message
1835  // are held by TransportDeserializer
1836  nSizeAdded += it->m_raw_message_size;
1837  }
1838  {
1839  LOCK(pnode->cs_vProcessMsg);
1840  pnode->vProcessMsg.splice(pnode->vProcessMsg.end(),
1841  pnode->vRecvMsg,
1842  pnode->vRecvMsg.begin(), it);
1843  pnode->nProcessQueueSize += nSizeAdded;
1844  pnode->fPauseRecv =
1845  pnode->nProcessQueueSize > nReceiveFloodSize;
1846  }
1848  }
1849  } else if (nBytes == 0) {
1850  // socket closed gracefully
1851  if (!pnode->fDisconnect) {
1852  LogPrint(BCLog::NET, "socket closed for peer=%d\n",
1853  pnode->GetId());
1854  }
1855  pnode->CloseSocketDisconnect();
1856  } else if (nBytes < 0) {
1857  // error
1858  int nErr = WSAGetLastError();
1859  if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE &&
1860  nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
1861  if (!pnode->fDisconnect) {
1863  "socket recv error for peer=%d: %s\n",
1864  pnode->GetId(), NetworkErrorString(nErr));
1865  }
1866  pnode->CloseSocketDisconnect();
1867  }
1868  }
1869  }
1870 
1871  if (sendSet) {
1872  // Send data
1873  size_t bytes_sent =
1874  WITH_LOCK(pnode->cs_vSend, return SocketSendData(*pnode));
1875  if (bytes_sent) {
1876  RecordBytesSent(bytes_sent);
1877  }
1878  }
1879 
1880  if (InactivityCheck(*pnode)) {
1881  pnode->fDisconnect = true;
1882  }
1883  }
1884  {
1886  for (CNode *pnode : nodes_copy) {
1887  pnode->Release();
1888  }
1889  }
1890 }
1891 
1893  while (!interruptNet) {
1894  DisconnectNodes();
1896  SocketHandler();
1897  }
1898 }
1899 
1901  {
1902  LOCK(mutexMsgProc);
1903  fMsgProcWake = true;
1904  }
1905  condMsgProc.notify_one();
1906 }
1907 
1909  FastRandomContext rng;
1910  std::vector<std::string> seeds =
1912  // Number of seeds left before testing if we have enough connections
1913  int seeds_right_now = 0;
1914  int found = 0;
1915 
1916  if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) {
1917  // When -forcednsseed is provided, query all.
1918  seeds_right_now = seeds.size();
1919  } else if (addrman.size() == 0) {
1920  // If we have no known peers, query all.
1921  // This will occur on the first run, or if peers.dat has been
1922  // deleted.
1923  seeds_right_now = seeds.size();
1924  }
1925 
1926  // goal: only query DNS seed if address need is acute
1927  // * If we have a reasonable number of peers in addrman, spend
1928  // some time trying them first. This improves user privacy by
1929  // creating fewer identifying DNS requests, reduces trust by
1930  // giving seeds less influence on the network topology, and
1931  // reduces traffic to the seeds.
1932  // * When querying DNS seeds query a few at once, this ensures
1933  // that we don't give DNS seeds the ability to eclipse nodes
1934  // that query them.
1935  // * If we continue having problems, eventually query all the
1936  // DNS seeds, and if that fails too, also try the fixed seeds.
1937  // (done in ThreadOpenConnections)
1938  const std::chrono::seconds seeds_wait_time =
1942 
1943  for (const std::string &seed : seeds) {
1944  if (seeds_right_now == 0) {
1945  seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
1946 
1947  if (addrman.size() > 0) {
1948  LogPrintf("Waiting %d seconds before querying DNS seeds.\n",
1949  seeds_wait_time.count());
1950  std::chrono::seconds to_wait = seeds_wait_time;
1951  while (to_wait.count() > 0) {
1952  // if sleeping for the MANY_PEERS interval, wake up
1953  // early to see if we have enough peers and can stop
1954  // this thread entirely freeing up its resources
1955  std::chrono::seconds w =
1956  std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait);
1957  if (!interruptNet.sleep_for(w)) {
1958  return;
1959  }
1960  to_wait -= w;
1961 
1962  int nRelevant = 0;
1963  {
1965  for (const CNode *pnode : m_nodes) {
1966  if (pnode->fSuccessfullyConnected &&
1967  pnode->IsFullOutboundConn()) {
1968  ++nRelevant;
1969  }
1970  }
1971  }
1972  if (nRelevant >= 2) {
1973  if (found > 0) {
1974  LogPrintf("%d addresses found from DNS seeds\n",
1975  found);
1976  LogPrintf(
1977  "P2P peers available. Finished DNS seeding.\n");
1978  } else {
1979  LogPrintf(
1980  "P2P peers available. Skipped DNS seeding.\n");
1981  }
1982  return;
1983  }
1984  }
1985  }
1986  }
1987 
1988  if (interruptNet) {
1989  return;
1990  }
1991 
1992  // hold off on querying seeds if P2P network deactivated
1993  if (!fNetworkActive) {
1994  LogPrintf("Waiting for network to be reactivated before querying "
1995  "DNS seeds.\n");
1996  do {
1997  if (!interruptNet.sleep_for(std::chrono::seconds{1})) {
1998  return;
1999  }
2000  } while (!fNetworkActive);
2001  }
2002 
2003  LogPrintf("Loading addresses from DNS seed %s\n", seed);
2004  if (HaveNameProxy()) {
2005  AddAddrFetch(seed);
2006  } else {
2007  std::vector<CNetAddr> vIPs;
2008  std::vector<CAddress> vAdd;
2009  ServiceFlags requiredServiceBits =
2011  std::string host = strprintf("x%x.%s", requiredServiceBits, seed);
2012  CNetAddr resolveSource;
2013  if (!resolveSource.SetInternal(host)) {
2014  continue;
2015  }
2016 
2017  // Limits number of IPs learned from a DNS seed
2018  unsigned int nMaxIPs = 256;
2019  if (LookupHost(host, vIPs, nMaxIPs, true)) {
2020  for (const CNetAddr &ip : vIPs) {
2021  CAddress addr = CAddress(
2023  requiredServiceBits);
2024  // Use a random age between 3 and 7 days old.
2025  addr.nTime = rng.rand_uniform_delay(
2026  Now<NodeSeconds>() - 3 * 24h, -4 * 24h);
2027  vAdd.push_back(addr);
2028  found++;
2029  }
2030  addrman.Add(vAdd, resolveSource);
2031  } else {
2032  // We now avoid directly using results from DNS Seeds which do
2033  // not support service bit filtering, instead using them as a
2034  // addrfetch to get nodes with our desired service bits.
2035  AddAddrFetch(seed);
2036  }
2037  }
2038  --seeds_right_now;
2039  }
2040  LogPrintf("%d addresses found from DNS seeds\n", found);
2041 }
2042 
2044  int64_t nStart = GetTimeMillis();
2045 
2047 
2048  LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
2049  addrman.size(), GetTimeMillis() - nStart);
2050 }
2051 
2053  std::string strDest;
2054  {
2056  if (m_addr_fetches.empty()) {
2057  return;
2058  }
2059  strDest = m_addr_fetches.front();
2060  m_addr_fetches.pop_front();
2061  }
2062  CAddress addr;
2063  CSemaphoreGrant grant(*semOutbound, true);
2064  if (grant) {
2065  OpenNetworkConnection(addr, false, &grant, strDest.c_str(),
2067  }
2068 }
2069 
2072 }
2073 
2076  LogPrint(BCLog::NET, "net: setting try another outbound peer=%s\n",
2077  flag ? "true" : "false");
2078 }
2079 
2080 // Return the number of peers we have over our outbound connection limit.
2081 // Exclude peers that are marked for disconnect, or are going to be disconnected
2082 // soon (eg ADDR_FETCH and FEELER).
2083 // Also exclude peers that haven't finished initial connection handshake yet (so
2084 // that we don't decide we're over our desired connection limit, and then evict
2085 // some peer that has finished the handshake).
2087  int full_outbound_peers = 0;
2088  {
2090  for (const CNode *pnode : m_nodes) {
2091  if (pnode->fSuccessfullyConnected && !pnode->fDisconnect &&
2092  pnode->IsFullOutboundConn()) {
2093  ++full_outbound_peers;
2094  }
2095  }
2096  }
2097  return std::max(full_outbound_peers - m_max_outbound_full_relay -
2099  0);
2100 }
2101 
2103  int block_relay_peers = 0;
2104  {
2106  for (const CNode *pnode : m_nodes) {
2107  if (pnode->fSuccessfullyConnected && !pnode->fDisconnect &&
2108  pnode->IsBlockOnlyConn()) {
2109  ++block_relay_peers;
2110  }
2111  }
2112  }
2113  return std::max(block_relay_peers - m_max_outbound_block_relay, 0);
2114 }
2115 
2117  const std::vector<std::string> connect,
2118  std::function<void(const CAddress &, ConnectionType)> mockOpenConnection) {
2119  // Connect to specific addresses
2120  if (!connect.empty()) {
2121  for (int64_t nLoop = 0;; nLoop++) {
2122  ProcessAddrFetch();
2123  for (const std::string &strAddr : connect) {
2124  CAddress addr(CService(), NODE_NONE);
2125  OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(),
2127  for (int i = 0; i < 10 && i < nLoop; i++) {
2128  if (!interruptNet.sleep_for(
2129  std::chrono::milliseconds(500))) {
2130  return;
2131  }
2132  }
2133  }
2134  if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) {
2135  return;
2136  }
2137  }
2138  }
2139 
2140  // Initiate network connections
2141  auto start = GetTime<std::chrono::microseconds>();
2142 
2143  // Minimum time before next feeler connection (in microseconds).
2144  auto next_feeler = GetExponentialRand(start, FEELER_INTERVAL);
2145  auto next_extra_block_relay =
2147  const bool dnsseed = gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED);
2148  bool add_fixed_seeds = gArgs.GetBoolArg("-fixedseeds", DEFAULT_FIXEDSEEDS);
2149 
2150  if (!add_fixed_seeds) {
2151  LogPrintf("Fixed seeds are disabled\n");
2152  }
2153 
2154  while (!interruptNet) {
2155  ProcessAddrFetch();
2156 
2157  // No need to sleep the thread if we are mocking the network connection
2158  if (!mockOpenConnection &&
2159  !interruptNet.sleep_for(std::chrono::milliseconds(500))) {
2160  return;
2161  }
2162 
2163  CSemaphoreGrant grant(*semOutbound);
2164  if (interruptNet) {
2165  return;
2166  }
2167 
2168  if (add_fixed_seeds && addrman.size() == 0) {
2169  // When the node starts with an empty peers.dat, there are a few
2170  // other sources of peers before we fallback on to fixed seeds:
2171  // -dnsseed, -seednode, -addnode If none of those are available, we
2172  // fallback on to fixed seeds immediately, else we allow 60 seconds
2173  // for any of those sources to populate addrman.
2174  bool add_fixed_seeds_now = false;
2175  // It is cheapest to check if enough time has passed first.
2176  if (GetTime<std::chrono::seconds>() >
2177  start + std::chrono::minutes{1}) {
2178  add_fixed_seeds_now = true;
2179  LogPrintf("Adding fixed seeds as 60 seconds have passed and "
2180  "addrman is empty\n");
2181  }
2182 
2183  // Checking !dnsseed is cheaper before locking 2 mutexes.
2184  if (!add_fixed_seeds_now && !dnsseed) {
2186  if (m_addr_fetches.empty() && m_added_nodes.empty()) {
2187  add_fixed_seeds_now = true;
2188  LogPrintf(
2189  "Adding fixed seeds as -dnsseed=0, -addnode is not "
2190  "provided and all -seednode(s) attempted\n");
2191  }
2192  }
2193 
2194  if (add_fixed_seeds_now) {
2195  CNetAddr local;
2196  local.SetInternal("fixedseeds");
2198  local);
2199  add_fixed_seeds = false;
2200  }
2201  }
2202 
2203  //
2204  // Choose an address to connect to based on most recently seen
2205  //
2206  CAddress addrConnect;
2207 
2208  // Only connect out to one peer per network group (/16 for IPv4).
2209  int nOutboundFullRelay = 0;
2210  int nOutboundBlockRelay = 0;
2211  int nOutboundAvalanche = 0;
2212  std::set<std::vector<uint8_t>> setConnected;
2213 
2214  {
2216  for (const CNode *pnode : m_nodes) {
2217  if (pnode->IsAvalancheOutboundConnection()) {
2218  nOutboundAvalanche++;
2219  } else if (pnode->IsFullOutboundConn()) {
2220  nOutboundFullRelay++;
2221  } else if (pnode->IsBlockOnlyConn()) {
2222  nOutboundBlockRelay++;
2223  }
2224 
2225  // Netgroups for inbound and manual peers are not excluded
2226  // because our goal here is to not use multiple of our
2227  // limited outbound slots on a single netgroup but inbound
2228  // and manual peers do not use our outbound slots. Inbound
2229  // peers also have the added issue that they could be attacker
2230  // controlled and could be used to prevent us from connecting
2231  // to particular hosts if we used them here.
2232  switch (pnode->m_conn_type) {
2235  break;
2241  setConnected.insert(
2242  pnode->addr.GetGroup(addrman.GetAsmap()));
2243  } // no default case, so the compiler can warn about missing
2244  // cases
2245  }
2246  }
2247 
2249  auto now = GetTime<std::chrono::microseconds>();
2250  bool anchor = false;
2251  bool fFeeler = false;
2252 
2253  // Determine what type of connection to open. Opening
2254  // BLOCK_RELAY connections to addresses from anchors.dat gets the
2255  // highest priority. Then we open AVALANCHE_OUTBOUND connection until we
2256  // hit our avalanche outbound peer limit, which is 0 if avalanche is not
2257  // enabled. We fallback after 50 retries to OUTBOUND_FULL_RELAY if the
2258  // peer is not avalanche capable until we meet our full-relay capacity.
2259  // Then we open BLOCK_RELAY connection until we hit our block-relay-only
2260  // peer limit.
2261  // GetTryNewOutboundPeer() gets set when a stale tip is detected, so we
2262  // try opening an additional OUTBOUND_FULL_RELAY connection. If none of
2263  // these conditions are met, check to see if it's time to try an extra
2264  // block-relay-only peer (to confirm our tip is current, see below) or
2265  // the next_feeler timer to decide if we should open a FEELER.
2266 
2267  if (!m_anchors.empty() &&
2268  (nOutboundBlockRelay < m_max_outbound_block_relay)) {
2269  conn_type = ConnectionType::BLOCK_RELAY;
2270  anchor = true;
2271  } else if (nOutboundAvalanche < m_max_avalanche_outbound) {
2273  } else if (nOutboundFullRelay < m_max_outbound_full_relay) {
2274  // OUTBOUND_FULL_RELAY
2275  } else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
2276  conn_type = ConnectionType::BLOCK_RELAY;
2277  } else if (GetTryNewOutboundPeer()) {
2278  // OUTBOUND_FULL_RELAY
2279  } else if (now > next_extra_block_relay &&
2281  // Periodically connect to a peer (using regular outbound selection
2282  // methodology from addrman) and stay connected long enough to sync
2283  // headers, but not much else.
2284  //
2285  // Then disconnect the peer, if we haven't learned anything new.
2286  //
2287  // The idea is to make eclipse attacks very difficult to pull off,
2288  // because every few minutes we're finding a new peer to learn
2289  // headers from.
2290  //
2291  // This is similar to the logic for trying extra outbound
2292  // (full-relay) peers, except:
2293  // - we do this all the time on an exponential timer, rather than
2294  // just when our tip is stale
2295  // - we potentially disconnect our next-youngest block-relay-only
2296  // peer, if our newest block-relay-only peer delivers a block more
2297  // recently.
2298  // See the eviction logic in net_processing.cpp.
2299  //
2300  // Because we can promote these connections to block-relay-only
2301  // connections, they do not get their own ConnectionType enum
2302  // (similar to how we deal with extra outbound peers).
2303  next_extra_block_relay =
2305  conn_type = ConnectionType::BLOCK_RELAY;
2306  } else if (now > next_feeler) {
2307  next_feeler = GetExponentialRand(now, FEELER_INTERVAL);
2308  conn_type = ConnectionType::FEELER;
2309  fFeeler = true;
2310  } else {
2311  // skip to next iteration of while loop
2312  continue;
2313  }
2314 
2316 
2317  const auto nANow{AdjustedTime()};
2318  int nTries = 0;
2319  while (!interruptNet) {
2320  if (anchor && !m_anchors.empty()) {
2321  const CAddress addr = m_anchors.back();
2322  m_anchors.pop_back();
2323  if (!addr.IsValid() || IsLocal(addr) || !IsReachable(addr) ||
2325  setConnected.count(addr.GetGroup(addrman.GetAsmap()))) {
2326  continue;
2327  }
2328  addrConnect = addr;
2330  "Trying to make an anchor connection to %s\n",
2331  addrConnect.ToString());
2332  break;
2333  }
2334  // If we didn't find an appropriate destination after trying 100
2335  // addresses fetched from addrman, stop this loop, and let the outer
2336  // loop run again (which sleeps, adds seed nodes, recalculates
2337  // already-connected network ranges, ...) before trying new addrman
2338  // addresses.
2339  nTries++;
2340  if (nTries > 100) {
2341  break;
2342  }
2343 
2344  CAddress addr;
2345  NodeSeconds addr_last_try{0s};
2346 
2347  if (fFeeler) {
2348  // First, try to get a tried table collision address. This
2349  // returns an empty (invalid) address if there are no collisions
2350  // to try.
2351  std::tie(addr, addr_last_try) = addrman.SelectTriedCollision();
2352 
2353  if (!addr.IsValid()) {
2354  // No tried table collisions. Select a new table address
2355  // for our feeler.
2356  std::tie(addr, addr_last_try) = addrman.Select(true);
2357  } else if (AlreadyConnectedToAddress(addr)) {
2358  // If test-before-evict logic would have us connect to a
2359  // peer that we're already connected to, just mark that
2360  // address as Good(). We won't be able to initiate the
2361  // connection anyway, so this avoids inadvertently evicting
2362  // a currently-connected peer.
2363  addrman.Good(addr);
2364  // Select a new table address for our feeler instead.
2365  std::tie(addr, addr_last_try) = addrman.Select(true);
2366  }
2367  } else {
2368  // Not a feeler
2369  std::tie(addr, addr_last_try) = addrman.Select();
2370  }
2371 
2372  // Require outbound connections, other than feelers and avalanche,
2373  // to be to distinct network groups
2374  if (!fFeeler && conn_type != ConnectionType::AVALANCHE_OUTBOUND &&
2375  setConnected.count(addr.GetGroup(addrman.GetAsmap()))) {
2376  break;
2377  }
2378 
2379  // if we selected an invalid or local address, restart
2380  if (!addr.IsValid() || IsLocal(addr)) {
2381  break;
2382  }
2383 
2384  if (!IsReachable(addr)) {
2385  continue;
2386  }
2387 
2388  // only consider very recently tried nodes after 30 failed attempts
2389  if (nANow - addr_last_try < 10min && nTries < 30) {
2390  continue;
2391  }
2392 
2393  // for non-feelers, require all the services we'll want,
2394  // for feelers, only require they be a full node (only because most
2395  // SPV clients don't have a good address DB available)
2396  if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
2397  continue;
2398  }
2399 
2400  if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
2401  continue;
2402  }
2403 
2404  // Do not connect to bad ports, unless 50 invalid addresses have
2405  // been selected already.
2406  if (nTries < 50 && (addr.IsIPv4() || addr.IsIPv6()) &&
2407  IsBadPort(addr.GetPort())) {
2408  continue;
2409  }
2410 
2411  // For avalanche peers, check they have the avalanche service bit
2412  // set.
2413  if (conn_type == ConnectionType::AVALANCHE_OUTBOUND &&
2414  !(addr.nServices & NODE_AVALANCHE)) {
2415  // If this peer is not suitable as an avalanche one and we tried
2416  // over 20 addresses already, see if we can fallback to a non
2417  // avalanche full outbound.
2418  if (nTries < 20 ||
2419  nOutboundFullRelay >= m_max_outbound_full_relay ||
2420  setConnected.count(addr.GetGroup(addrman.GetAsmap()))) {
2421  // Fallback is not desirable or possible, try another one
2422  continue;
2423  }
2424 
2425  // Fallback is possible, update the connection type accordingly
2427  }
2428 
2429  addrConnect = addr;
2430  break;
2431  }
2432 
2433  if (addrConnect.IsValid()) {
2434  if (fFeeler) {
2435  // Add small amount of random noise before connection to avoid
2436  // synchronization.
2437  int randsleep = GetRand<int>(FEELER_SLEEP_WINDOW * 1000);
2438  if (!interruptNet.sleep_for(
2439  std::chrono::milliseconds(randsleep))) {
2440  return;
2441  }
2442  LogPrint(BCLog::NET, "Making feeler connection to %s\n",
2443  addrConnect.ToString());
2444  }
2445 
2446  // This mock is for testing purpose only. It prevents the thread
2447  // from attempting the connection which is useful for testing.
2448  if (mockOpenConnection) {
2449  mockOpenConnection(addrConnect, conn_type);
2450  } else {
2451  OpenNetworkConnection(addrConnect,
2452  int(setConnected.size()) >=
2453  std::min(nMaxConnections - 1, 2),
2454  &grant, nullptr, conn_type);
2455  }
2456  }
2457  }
2458 }
2459 
2460 std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const {
2461  std::vector<CAddress> ret;
2463  for (const CNode *pnode : m_nodes) {
2464  if (pnode->IsBlockOnlyConn()) {
2465  ret.push_back(pnode->addr);
2466  }
2467  }
2468 
2469  return ret;
2470 }
2471 
2472 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const {
2473  std::vector<AddedNodeInfo> ret;
2474 
2475  std::list<std::string> lAddresses(0);
2476  {
2478  ret.reserve(m_added_nodes.size());
2479  std::copy(m_added_nodes.cbegin(), m_added_nodes.cend(),
2480  std::back_inserter(lAddresses));
2481  }
2482 
2483  // Build a map of all already connected addresses (by IP:port and by name)
2484  // to inbound/outbound and resolved CService
2485  std::map<CService, bool> mapConnected;
2486  std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
2487  {
2489  for (const CNode *pnode : m_nodes) {
2490  if (pnode->addr.IsValid()) {
2491  mapConnected[pnode->addr] = pnode->IsInboundConn();
2492  }
2493  std::string addrName{pnode->m_addr_name};
2494  if (!addrName.empty()) {
2495  mapConnectedByName[std::move(addrName)] =
2496  std::make_pair(pnode->IsInboundConn(),
2497  static_cast<const CService &>(pnode->addr));
2498  }
2499  }
2500  }
2501 
2502  for (const std::string &strAddNode : lAddresses) {
2503  CService service(
2504  LookupNumeric(strAddNode, Params().GetDefaultPort(strAddNode)));
2505  AddedNodeInfo addedNode{strAddNode, CService(), false, false};
2506  if (service.IsValid()) {
2507  // strAddNode is an IP:port
2508  auto it = mapConnected.find(service);
2509  if (it != mapConnected.end()) {
2510  addedNode.resolvedAddress = service;
2511  addedNode.fConnected = true;
2512  addedNode.fInbound = it->second;
2513  }
2514  } else {
2515  // strAddNode is a name
2516  auto it = mapConnectedByName.find(strAddNode);
2517  if (it != mapConnectedByName.end()) {
2518  addedNode.resolvedAddress = it->second.second;
2519  addedNode.fConnected = true;
2520  addedNode.fInbound = it->second.first;
2521  }
2522  }
2523  ret.emplace_back(std::move(addedNode));
2524  }
2525 
2526  return ret;
2527 }
2528 
2530  while (true) {
2531  CSemaphoreGrant grant(*semAddnode);
2532  std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
2533  bool tried = false;
2534  for (const AddedNodeInfo &info : vInfo) {
2535  if (!info.fConnected) {
2536  if (!grant.TryAcquire()) {
2537  // If we've used up our semaphore and need a new one, let's
2538  // not wait here since while we are waiting the
2539  // addednodeinfo state might change.
2540  break;
2541  }
2542  tried = true;
2543  CAddress addr(CService(), NODE_NONE);
2544  OpenNetworkConnection(addr, false, &grant,
2545  info.strAddedNode.c_str(),
2547  if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) {
2548  return;
2549  }
2550  }
2551  }
2552  // Retry every 60 seconds if a connection was attempted, otherwise two
2553  // seconds.
2554  if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2))) {
2555  return;
2556  }
2557  }
2558 }
2559 
2560 // If successful, this moves the passed grant to the constructed node.
2562  bool fCountFailure,
2563  CSemaphoreGrant *grantOutbound,
2564  const char *pszDest,
2565  ConnectionType conn_type) {
2566  assert(conn_type != ConnectionType::INBOUND);
2567 
2568  //
2569  // Initiate outbound network connection
2570  //
2571  if (interruptNet) {
2572  return;
2573  }
2574  if (!fNetworkActive) {
2575  return;
2576  }
2577  if (!pszDest) {
2578  bool banned_or_discouraged =
2579  m_banman && (m_banman->IsDiscouraged(addrConnect) ||
2580  m_banman->IsBanned(addrConnect));
2581  if (IsLocal(addrConnect) || banned_or_discouraged ||
2582  AlreadyConnectedToAddress(addrConnect)) {
2583  return;
2584  }
2585  } else if (FindNode(std::string(pszDest))) {
2586  return;
2587  }
2588 
2589  CNode *pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type);
2590 
2591  if (!pnode) {
2592  return;
2593  }
2594  if (grantOutbound) {
2595  grantOutbound->MoveTo(pnode->grantOutbound);
2596  }
2597 
2598  for (auto interface : m_msgproc) {
2599  interface->InitializeNode(*config, *pnode, nLocalServices);
2600  }
2601 
2602  {
2604  m_nodes.push_back(pnode);
2605  }
2606 }
2607 
2609 
2612 
2613  FastRandomContext rng;
2614  while (!flagInterruptMsgProc) {
2615  std::vector<CNode *> nodes_copy;
2616  {
2618  nodes_copy = m_nodes;
2619  for (CNode *pnode : nodes_copy) {
2620  pnode->AddRef();
2621  }
2622  }
2623 
2624  bool fMoreWork = false;
2625 
2626  // Randomize the order in which we process messages from/to our peers.
2627  // This prevents attacks in which an attacker exploits having multiple
2628  // consecutive connections in the m_nodes list.
2629  Shuffle(nodes_copy.begin(), nodes_copy.end(), rng);
2630 
2631  for (CNode *pnode : nodes_copy) {
2632  if (pnode->fDisconnect) {
2633  continue;
2634  }
2635 
2636  bool fMoreNodeWork = false;
2637  // Receive messages
2638  for (auto interface : m_msgproc) {
2639  fMoreNodeWork |= interface->ProcessMessages(
2640  *config, pnode, flagInterruptMsgProc);
2641  }
2642  fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
2643  if (flagInterruptMsgProc) {
2644  return;
2645  }
2646 
2647  // Send messages
2648  for (auto interface : m_msgproc) {
2649  interface->SendMessages(*config, pnode);
2650  }
2651 
2652  if (flagInterruptMsgProc) {
2653  return;
2654  }
2655  }
2656 
2657  {
2659  for (CNode *pnode : nodes_copy) {
2660  pnode->Release();
2661  }
2662  }
2663 
2664  WAIT_LOCK(mutexMsgProc, lock);
2665  if (!fMoreWork) {
2666  condMsgProc.wait_until(lock,
2667  std::chrono::steady_clock::now() +
2668  std::chrono::milliseconds(100),
2669  [this]() EXCLUSIVE_LOCKS_REQUIRED(
2670  mutexMsgProc) { return fMsgProcWake; });
2671  }
2672  fMsgProcWake = false;
2673  }
2674 }
2675 
2677  static constexpr auto err_wait_begin = 1s;
2678  static constexpr auto err_wait_cap = 5min;
2679  auto err_wait = err_wait_begin;
2680 
2681  bool advertising_listen_addr = false;
2682  i2p::Connection conn;
2683 
2684  while (!interruptNet) {
2685  if (!m_i2p_sam_session->Listen(conn)) {
2686  if (advertising_listen_addr && conn.me.IsValid()) {
2687  RemoveLocal(conn.me);
2688  advertising_listen_addr = false;
2689  }
2690 
2691  interruptNet.sleep_for(err_wait);
2692  if (err_wait < err_wait_cap) {
2693  err_wait *= 2;
2694  }
2695 
2696  continue;
2697  }
2698 
2699  if (!advertising_listen_addr) {
2700  AddLocal(conn.me, LOCAL_MANUAL);
2701  advertising_listen_addr = true;
2702  }
2703 
2704  if (!m_i2p_sam_session->Accept(conn)) {
2705  continue;
2706  }
2707 
2709  conn.sock->Release(), NetPermissionFlags::None,
2710  CAddress{conn.me, NODE_NONE}, CAddress{conn.peer, NODE_NONE});
2711  }
2712 }
2713 
2714 bool CConnman::BindListenPort(const CService &addrBind, bilingual_str &strError,
2715  NetPermissionFlags permissions) {
2716  int nOne = 1;
2717 
2718  // Create socket for listening for incoming connections
2719  struct sockaddr_storage sockaddr;
2720  socklen_t len = sizeof(sockaddr);
2721  if (!addrBind.GetSockAddr((struct sockaddr *)&sockaddr, &len)) {
2722  strError = strprintf(
2723  Untranslated("Error: Bind address family for %s not supported"),
2724  addrBind.ToString());
2725  LogPrintf("%s\n", strError.original);
2726  return false;
2727  }
2728 
2729  std::unique_ptr<Sock> sock = CreateSock(addrBind);
2730  if (!sock) {
2731  strError =
2732  strprintf(Untranslated("Error: Couldn't open socket for incoming "
2733  "connections (socket returned error %s)"),
2735  LogPrintf("%s\n", strError.original);
2736  return false;
2737  }
2738 
2739  // Allow binding if the port is still in TIME_WAIT state after
2740  // the program was closed and restarted.
2741  setsockopt(sock->Get(), SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne,
2742  sizeof(int));
2743 
2744  // Some systems don't have IPV6_V6ONLY but are always v6only; others do have
2745  // the option and enable it by default or not. Try to enable it, if
2746  // possible.
2747  if (addrBind.IsIPv6()) {
2748 #ifdef IPV6_V6ONLY
2749  setsockopt(sock->Get(), IPPROTO_IPV6, IPV6_V6ONLY,
2750  (sockopt_arg_type)&nOne, sizeof(int));
2751 #endif
2752 #ifdef WIN32
2753  int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2754  setsockopt(sock->Get(), IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
2755  (sockopt_arg_type)&nProtLevel, sizeof(int));
2756 #endif
2757  }
2758 
2759  if (::bind(sock->Get(), (struct sockaddr *)&sockaddr, len) ==
2760  SOCKET_ERROR) {
2761  int nErr = WSAGetLastError();
2762  if (nErr == WSAEADDRINUSE) {
2763  strError = strprintf(_("Unable to bind to %s on this computer. %s "
2764  "is probably already running."),
2765  addrBind.ToString(), PACKAGE_NAME);
2766  } else {
2767  strError = strprintf(_("Unable to bind to %s on this computer "
2768  "(bind returned error %s)"),
2769  addrBind.ToString(), NetworkErrorString(nErr));
2770  }
2771  LogPrintf("%s\n", strError.original);
2772  return false;
2773  }
2774  LogPrintf("Bound to %s\n", addrBind.ToString());
2775 
2776  // Listen for incoming connections
2777  if (listen(sock->Get(), SOMAXCONN) == SOCKET_ERROR) {
2778  strError = strprintf(_("Error: Listening for incoming connections "
2779  "failed (listen returned error %s)"),
2781  LogPrintf("%s\n", strError.original);
2782  return false;
2783  }
2784 
2785  vhListenSocket.push_back(ListenSocket(sock->Release(), permissions));
2786  return true;
2787 }
2788 
2789 void Discover() {
2790  if (!fDiscover) {
2791  return;
2792  }
2793 
2794 #ifdef WIN32
2795  // Get local host IP
2796  char pszHostName[256] = "";
2797  if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) {
2798  std::vector<CNetAddr> vaddr;
2799  if (LookupHost(pszHostName, vaddr, 0, true)) {
2800  for (const CNetAddr &addr : vaddr) {
2801  if (AddLocal(addr, LOCAL_IF)) {
2802  LogPrintf("%s: %s - %s\n", __func__, pszHostName,
2803  addr.ToString());
2804  }
2805  }
2806  }
2807  }
2808 #elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)
2809  // Get local host ip
2810  struct ifaddrs *myaddrs;
2811  if (getifaddrs(&myaddrs) == 0) {
2812  for (struct ifaddrs *ifa = myaddrs; ifa != nullptr;
2813  ifa = ifa->ifa_next) {
2814  if (ifa->ifa_addr == nullptr || (ifa->ifa_flags & IFF_UP) == 0 ||
2815  strcmp(ifa->ifa_name, "lo") == 0 ||
2816  strcmp(ifa->ifa_name, "lo0") == 0) {
2817  continue;
2818  }
2819  if (ifa->ifa_addr->sa_family == AF_INET) {
2820  struct sockaddr_in *s4 =
2821  reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
2822  CNetAddr addr(s4->sin_addr);
2823  if (AddLocal(addr, LOCAL_IF)) {
2824  LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name,
2825  addr.ToString());
2826  }
2827  } else if (ifa->ifa_addr->sa_family == AF_INET6) {
2828  struct sockaddr_in6 *s6 =
2829  reinterpret_cast<struct sockaddr_in6 *>(ifa->ifa_addr);
2830  CNetAddr addr(s6->sin6_addr);
2831  if (AddLocal(addr, LOCAL_IF)) {
2832  LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name,
2833  addr.ToString());
2834  }
2835  }
2836  }
2837  freeifaddrs(myaddrs);
2838  }
2839 #endif
2840 }
2841 
2842 void CConnman::SetNetworkActive(bool active) {
2843  LogPrintf("%s: %s\n", __func__, active);
2844 
2845  if (fNetworkActive == active) {
2846  return;
2847  }
2848 
2849  fNetworkActive = active;
2850 
2851  if (m_client_interface) {
2852  m_client_interface->NotifyNetworkActiveChanged(fNetworkActive);
2853  }
2854 }
2855 
2856 CConnman::CConnman(const Config &configIn, uint64_t nSeed0In, uint64_t nSeed1In,
2857  AddrMan &addrmanIn, bool network_active)
2858  : config(&configIn), addrman(addrmanIn), nSeed0(nSeed0In),
2859  nSeed1(nSeed1In) {
2860  SetTryNewOutboundPeer(false);
2861 
2862  Options connOptions;
2863  Init(connOptions);
2864  SetNetworkActive(network_active);
2865 }
2866 
2868  return nLastNodeId.fetch_add(1);
2869 }
2870 
2871 bool CConnman::Bind(const CService &addr, unsigned int flags,
2872  NetPermissionFlags permissions) {
2873  if (!(flags & BF_EXPLICIT) && !IsReachable(addr)) {
2874  return false;
2875  }
2876  bilingual_str strError;
2877  if (!BindListenPort(addr, strError, permissions)) {
2879  m_client_interface->ThreadSafeMessageBox(
2880  strError, "", CClientUIInterface::MSG_ERROR);
2881  }
2882  return false;
2883  }
2884 
2885  if (addr.IsRoutable() && fDiscover && !(flags & BF_DONT_ADVERTISE) &&
2887  AddLocal(addr, LOCAL_BIND);
2888  }
2889 
2890  return true;
2891 }
2892 
2893 bool CConnman::InitBinds(const Options &options) {
2894  bool fBound = false;
2895  for (const auto &addrBind : options.vBinds) {
2896  fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR),
2898  }
2899  for (const auto &addrBind : options.vWhiteBinds) {
2900  fBound |= Bind(addrBind.m_service, (BF_EXPLICIT | BF_REPORT_ERROR),
2901  addrBind.m_flags);
2902  }
2903  for (const auto &addr_bind : options.onion_binds) {
2904  fBound |= Bind(addr_bind, BF_EXPLICIT | BF_DONT_ADVERTISE,
2906  }
2907  if (options.bind_on_any) {
2908  struct in_addr inaddr_any;
2909  inaddr_any.s_addr = htonl(INADDR_ANY);
2910  struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
2911  fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE,
2913  fBound |=
2914  Bind(CService(inaddr_any, GetListenPort()),
2916  }
2917  return fBound;
2918 }
2919 
2920 bool CConnman::Start(CScheduler &scheduler, const Options &connOptions) {
2921  Init(connOptions);
2922 
2923  if (fListen && !InitBinds(connOptions)) {
2924  if (m_client_interface) {
2925  m_client_interface->ThreadSafeMessageBox(
2926  _("Failed to listen on any port. Use -listen=0 if you want "
2927  "this."),
2929  }
2930  return false;
2931  }
2932 
2933  proxyType i2p_sam;
2934  if (GetProxy(NET_I2P, i2p_sam)) {
2935  m_i2p_sam_session = std::make_unique<i2p::sam::Session>(
2936  gArgs.GetDataDirNet() / "i2p_private_key", i2p_sam.proxy,
2937  &interruptNet);
2938  }
2939 
2940  for (const auto &strDest : connOptions.vSeedNodes) {
2941  AddAddrFetch(strDest);
2942  }
2943 
2944  if (m_use_addrman_outgoing) {
2945  // Load addresses from anchors.dat
2946  m_anchors =
2949  if (m_anchors.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) {
2951  }
2952  LogPrintf(
2953  "%i block-relay-only anchors will be tried for connections.\n",
2954  m_anchors.size());
2955  }
2956 
2957  if (m_client_interface) {
2958  m_client_interface->InitMessage(
2959  _("Starting network threads...").translated);
2960  }
2961 
2962  fAddressesInitialized = true;
2963 
2964  if (semOutbound == nullptr) {
2965  // initialize semaphore
2966  semOutbound = std::make_unique<CSemaphore>(
2967  std::min(m_max_outbound, nMaxConnections));
2968  }
2969  if (semAddnode == nullptr) {
2970  // initialize semaphore
2971  semAddnode = std::make_unique<CSemaphore>(nMaxAddnode);
2972  }
2973 
2974  //
2975  // Start threads
2976  //
2977  assert(m_msgproc.size() > 0);
2978  InterruptSocks5(false);
2979  interruptNet.reset();
2980  flagInterruptMsgProc = false;
2981 
2982  {
2983  LOCK(mutexMsgProc);
2984  fMsgProcWake = false;
2985  }
2986 
2987  // Send and receive from sockets, accept connections
2988  threadSocketHandler = std::thread(&util::TraceThread, "net",
2989  [this] { ThreadSocketHandler(); });
2990 
2991  if (!gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED)) {
2992  LogPrintf("DNS seeding disabled\n");
2993  } else {
2994  threadDNSAddressSeed = std::thread(&util::TraceThread, "dnsseed",
2995  [this] { ThreadDNSAddressSeed(); });
2996  }
2997 
2998  // Initiate manual connections
2999  threadOpenAddedConnections = std::thread(
3000  &util::TraceThread, "addcon", [this] { ThreadOpenAddedConnections(); });
3001 
3002  if (connOptions.m_use_addrman_outgoing &&
3003  !connOptions.m_specified_outgoing.empty()) {
3004  if (m_client_interface) {
3005  m_client_interface->ThreadSafeMessageBox(
3006  _("Cannot provide specific connections and have addrman find "
3007  "outgoing connections at the same."),
3009  }
3010  return false;
3011  }
3012  if (connOptions.m_use_addrman_outgoing ||
3013  !connOptions.m_specified_outgoing.empty()) {
3015  std::thread(&util::TraceThread, "opencon",
3016  [this, connect = connOptions.m_specified_outgoing] {
3017  ThreadOpenConnections(connect, nullptr);
3018  });
3019  }
3020 
3021  // Process messages
3022  threadMessageHandler = std::thread(&util::TraceThread, "msghand",
3023  [this] { ThreadMessageHandler(); });
3024 
3025  if (connOptions.m_i2p_accept_incoming &&
3026  m_i2p_sam_session.get() != nullptr) {
3028  std::thread(&util::TraceThread, "i2paccept",
3029  [this] { ThreadI2PAcceptIncoming(); });
3030  }
3031 
3032  // Dump network addresses
3033  scheduler.scheduleEvery(
3034  [this]() {
3035  this->DumpAddresses();
3036  return true;
3037  },
3039 
3040  return true;
3041 }
3042 
3044 public:
3046 
3048 #ifdef WIN32
3049  // Shutdown Windows Sockets
3050  WSACleanup();
3051 #endif
3052  }
3053 };
3055 
3057  {
3058  LOCK(mutexMsgProc);
3059  flagInterruptMsgProc = true;
3060  }
3061  condMsgProc.notify_all();
3062 
3063  interruptNet();
3064  InterruptSocks5(true);
3065 
3066  if (semOutbound) {
3067  for (int i = 0; i < m_max_outbound; i++) {
3068  semOutbound->post();
3069  }
3070  }
3071 
3072  if (semAddnode) {
3073  for (int i = 0; i < nMaxAddnode; i++) {
3074  semAddnode->post();
3075  }
3076  }
3077 }
3078 
3080  if (threadI2PAcceptIncoming.joinable()) {
3081  threadI2PAcceptIncoming.join();
3082  }
3083  if (threadMessageHandler.joinable()) {
3084  threadMessageHandler.join();
3085  }
3086  if (threadOpenConnections.joinable()) {
3087  threadOpenConnections.join();
3088  }
3089  if (threadOpenAddedConnections.joinable()) {
3091  }
3092  if (threadDNSAddressSeed.joinable()) {
3093  threadDNSAddressSeed.join();
3094  }
3095  if (threadSocketHandler.joinable()) {
3096  threadSocketHandler.join();
3097  }
3098 }
3099 
3101  if (fAddressesInitialized) {
3102  DumpAddresses();
3103  fAddressesInitialized = false;
3104 
3105  if (m_use_addrman_outgoing) {
3106  // Anchor connections are only dumped during clean shutdown.
3107  std::vector<CAddress> anchors_to_dump =
3109  if (anchors_to_dump.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) {
3110  anchors_to_dump.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS);
3111  }
3114  anchors_to_dump);
3115  }
3116  }
3117 
3118  // Delete peer connections.
3119  std::vector<CNode *> nodes;
3120  WITH_LOCK(m_nodes_mutex, nodes.swap(m_nodes));
3121  for (CNode *pnode : nodes) {
3122  pnode->CloseSocketDisconnect();
3123  DeleteNode(pnode);
3124  }
3125 
3126  // Close listening sockets.
3127  for (ListenSocket &hListenSocket : vhListenSocket) {
3128  if (hListenSocket.socket != INVALID_SOCKET) {
3129  if (!CloseSocket(hListenSocket.socket)) {
3130  LogPrintf("CloseSocket(hListenSocket) failed with error %s\n",
3132  }
3133  }
3134  }
3135 
3136  for (CNode *pnode : m_nodes_disconnected) {
3137  DeleteNode(pnode);
3138  }
3139  m_nodes_disconnected.clear();
3140  vhListenSocket.clear();
3141  semOutbound.reset();
3142  semAddnode.reset();
3143 }
3144 
3146  assert(pnode);
3147  for (auto interface : m_msgproc) {
3148  interface->FinalizeNode(*config, *pnode);
3149  }
3150  delete pnode;
3151 }
3152 
3154  Interrupt();
3155  Stop();
3156 }
3157 
3158 std::vector<CAddress>
3159 CConnman::GetAddresses(size_t max_addresses, size_t max_pct,
3160  std::optional<Network> network) const {
3161  std::vector<CAddress> addresses =
3162  addrman.GetAddr(max_addresses, max_pct, network);
3163  if (m_banman) {
3164  addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
3165  [this](const CAddress &addr) {
3166  return m_banman->IsDiscouraged(
3167  addr) ||
3168  m_banman->IsBanned(addr);
3169  }),
3170  addresses.end());
3171  }
3172  return addresses;
3173 }
3174 
3175 std::vector<CAddress>
3176 CConnman::GetAddresses(CNode &requestor, size_t max_addresses, size_t max_pct) {
3177  auto local_socket_bytes = requestor.addrBind.GetAddrBytes();
3178  uint64_t cache_id =
3180  .Write(requestor.addr.GetNetwork())
3181  .Write(local_socket_bytes.data(), local_socket_bytes.size())
3182  .Finalize();
3183  const auto current_time = GetTime<std::chrono::microseconds>();
3184  auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{});
3185  CachedAddrResponse &cache_entry = r.first->second;
3186  // New CachedAddrResponse have expiration 0.
3187  if (cache_entry.m_cache_entry_expiration < current_time) {
3188  cache_entry.m_addrs_response_cache =
3189  GetAddresses(max_addresses, max_pct, /* network */ std::nullopt);
3190  // Choosing a proper cache lifetime is a trade-off between the privacy
3191  // leak minimization and the usefulness of ADDR responses to honest
3192  // users.
3193  //
3194  // Longer cache lifetime makes it more difficult for an attacker to
3195  // scrape enough AddrMan data to maliciously infer something useful. By
3196  // the time an attacker scraped enough AddrMan records, most of the
3197  // records should be old enough to not leak topology info by e.g.
3198  // analyzing real-time changes in timestamps.
3199  //
3200  // It takes only several hundred requests to scrape everything from an
3201  // AddrMan containing 100,000 nodes, so ~24 hours of cache lifetime
3202  // indeed makes the data less inferable by the time most of it could be
3203  // scraped (considering that timestamps are updated via ADDR
3204  // self-announcements and when nodes communicate). We also should be
3205  // robust to those attacks which may not require scraping *full*
3206  // victim's AddrMan (because even several timestamps of the same handful
3207  // of nodes may leak privacy).
3208  //
3209  // On the other hand, longer cache lifetime makes ADDR responses
3210  // outdated and less useful for an honest requestor, e.g. if most nodes
3211  // in the ADDR response are no longer active.
3212  //
3213  // However, the churn in the network is known to be rather low. Since we
3214  // consider nodes to be "terrible" (see IsTerrible()) if the timestamps
3215  // are older than 30 days, max. 24 hours of "penalty" due to cache
3216  // shouldn't make any meaningful difference in terms of the freshness of
3217  // the response.
3218  cache_entry.m_cache_entry_expiration =
3219  current_time + std::chrono::hours(21) +
3220  GetRandMillis(std::chrono::hours(6));
3221  }
3222  return cache_entry.m_addrs_response_cache;
3223 }
3224 
3225 bool CConnman::AddNode(const std::string &strNode) {
3227  for (const std::string &it : m_added_nodes) {
3228  if (strNode == it) {
3229  return false;
3230  }
3231  }
3232 
3233  m_added_nodes.push_back(strNode);
3234  return true;
3235 }
3236 
3237 bool CConnman::RemoveAddedNode(const std::string &strNode) {
3239  for (std::vector<std::string>::iterator it = m_added_nodes.begin();
3240  it != m_added_nodes.end(); ++it) {
3241  if (strNode == *it) {
3242  m_added_nodes.erase(it);
3243  return true;
3244  }
3245  }
3246  return false;
3247 }
3248 
3251  // Shortcut if we want total
3253  return m_nodes.size();
3254  }
3255 
3256  int nNum = 0;
3257  for (const auto &pnode : m_nodes) {
3258  if (flags &
3259  (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
3260  nNum++;
3261  }
3262  }
3263 
3264  return nNum;
3265 }
3266 
3267 void CConnman::GetNodeStats(std::vector<CNodeStats> &vstats) const {
3268  vstats.clear();
3270  vstats.reserve(m_nodes.size());
3271  for (CNode *pnode : m_nodes) {
3272  vstats.emplace_back();
3273  pnode->copyStats(vstats.back());
3274  vstats.back().m_mapped_as = pnode->addr.GetMappedAS(addrman.GetAsmap());
3275  }
3276 }
3277 
3278 bool CConnman::DisconnectNode(const std::string &strNode) {
3280  if (CNode *pnode = FindNode(strNode)) {
3282  "disconnect by address%s matched peer=%d; disconnecting\n",
3283  (fLogIPs ? strprintf("=%s", strNode) : ""), pnode->GetId());
3284  pnode->fDisconnect = true;
3285  return true;
3286  }
3287  return false;
3288 }
3289 
3290 bool CConnman::DisconnectNode(const CSubNet &subnet) {
3291  bool disconnected = false;
3293  for (CNode *pnode : m_nodes) {
3294  if (subnet.Match(pnode->addr)) {
3296  "disconnect by subnet%s matched peer=%d; disconnecting\n",
3297  (fLogIPs ? strprintf("=%s", subnet.ToString()) : ""),
3298  pnode->GetId());
3299  pnode->fDisconnect = true;
3300  disconnected = true;
3301  }
3302  }
3303  return disconnected;
3304 }
3305 
3307  return DisconnectNode(CSubNet(addr));
3308 }
3309 
3312  for (CNode *pnode : m_nodes) {
3313  if (id == pnode->GetId()) {
3314  LogPrint(BCLog::NET, "disconnect by id peer=%d; disconnecting\n",
3315  pnode->GetId());
3316  pnode->fDisconnect = true;
3317  return true;
3318  }
3319  }
3320  return false;
3321 }
3322 
3323 void CConnman::RecordBytesRecv(uint64_t bytes) {
3324  nTotalBytesRecv += bytes;
3325 }
3326 
3327 void CConnman::RecordBytesSent(uint64_t bytes) {
3329  nTotalBytesSent += bytes;
3330 
3331  const auto now = GetTime<std::chrono::seconds>();
3332  if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now) {
3333  // timeframe expired, reset cycle
3334  nMaxOutboundCycleStartTime = now;
3335  nMaxOutboundTotalBytesSentInCycle = 0;
3336  }
3337 
3338  // TODO, exclude peers with download permission
3339  nMaxOutboundTotalBytesSentInCycle += bytes;
3340 }
3341 
3344  return nMaxOutboundLimit;
3345 }
3346 
3347 std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const {
3348  return MAX_UPLOAD_TIMEFRAME;
3349 }
3350 
3351 std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const {
3353  if (nMaxOutboundLimit == 0) {
3354  return 0s;
3355  }
3356 
3357  if (nMaxOutboundCycleStartTime.count() == 0) {
3358  return MAX_UPLOAD_TIMEFRAME;
3359  }
3360 
3361  const std::chrono::seconds cycleEndTime =
3362  nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME;
3363  const auto now = GetTime<std::chrono::seconds>();
3364  return (cycleEndTime < now) ? 0s : cycleEndTime - now;
3365 }
3366 
3367 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const {
3369  if (nMaxOutboundLimit == 0) {
3370  return false;
3371  }
3372 
3373  if (historicalBlockServingLimit) {
3374  // keep a large enough buffer to at least relay each block once.
3375  const std::chrono::seconds timeLeftInCycle =
3377  const uint64_t buffer =
3378  timeLeftInCycle / std::chrono::minutes{10} * ONE_MEGABYTE;
3379  if (buffer >= nMaxOutboundLimit ||
3380  nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer) {
3381  return true;
3382  }
3383  } else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) {
3384  return true;
3385  }
3386 
3387  return false;
3388 }
3389 
3392  if (nMaxOutboundLimit == 0) {
3393  return 0;
3394  }
3395 
3396  return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
3397  ? 0
3398  : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
3399 }
3400 
3402  return nTotalBytesRecv;
3403 }
3404 
3407  return nTotalBytesSent;
3408 }
3409 
3411  return nLocalServices;
3412 }
3413 
3414 unsigned int CConnman::GetReceiveFloodSize() const {
3415  return nReceiveFloodSize;
3416 }
3417 
3418 void CNode::invsPolled(uint32_t count) {
3419  invCounters += count;
3420 }
3421 
3422 void CNode::invsVoted(uint32_t count) {
3423  invCounters += uint64_t(count) << 32;
3424 }
3425 
3426 void CNode::updateAvailabilityScore(double decayFactor) {
3427  if (!m_avalanche_enabled) {
3428  return;
3429  }
3430 
3431  uint64_t windowInvCounters = invCounters.exchange(0);
3432  double previousScore = availabilityScore;
3433 
3434  int64_t polls = windowInvCounters & std::numeric_limits<uint32_t>::max();
3435  int64_t votes = windowInvCounters >> 32;
3436 
3438  decayFactor * (2 * votes - polls) + (1. - decayFactor) * previousScore;
3439 }
3440 
3442  // The score is set atomically so there is no need to lock the statistics
3443  // when reading.
3444  return availabilityScore;
3445 }
3446 
3447 CNode::CNode(NodeId idIn, SOCKET hSocketIn, const CAddress &addrIn,
3448  uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
3449  uint64_t nLocalExtraEntropyIn, const CAddress &addrBindIn,
3450  const std::string &addrNameIn, ConnectionType conn_type_in,
3451  bool inbound_onion)
3452  : m_connected(GetTime<std::chrono::seconds>()), addr(addrIn),
3453  addrBind(addrBindIn), m_addr_name{addrNameIn.empty()
3454  ? addr.ToStringIPPort()
3455  : addrNameIn},
3456  m_inbound_onion(inbound_onion), nKeyedNetGroup(nKeyedNetGroupIn),
3457  // Don't relay addr messages to peers that we connect to as
3458  // block-relay-only peers (to prevent adversaries from inferring these
3459  // links from addr traffic).
3460  id(idIn), nLocalHostNonce(nLocalHostNonceIn),
3461  nLocalExtraEntropy(nLocalExtraEntropyIn), m_conn_type(conn_type_in) {
3462  if (inbound_onion) {
3463  assert(conn_type_in == ConnectionType::INBOUND);
3464  }
3465  hSocket = hSocketIn;
3466 
3467  for (const std::string &msg : getAllNetMessageTypes()) {
3468  mapRecvBytesPerMsgCmd[msg] = 0;
3469  }
3470  mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
3471 
3472  if (fLogIPs) {
3473  LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", m_addr_name,
3474  id);
3475  } else {
3476  LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
3477  }
3478 
3479  m_deserializer = std::make_unique<V1TransportDeserializer>(
3480  V1TransportDeserializer(GetConfig().GetChainParams().NetMagic(),
3482  m_serializer =
3483  std::make_unique<V1TransportSerializer>(V1TransportSerializer());
3484 }
3485 
3487  CloseSocket(hSocket);
3488 }
3489 
3491  return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
3492 }
3493 
3495  size_t nMessageSize = msg.data.size();
3496  LogPrint(BCLog::NETDEBUG, "sending %s (%d bytes) peer=%d\n", msg.m_type,
3497  nMessageSize, pnode->GetId());
3498  if (gArgs.GetBoolArg("-capturemessages", false)) {
3499  CaptureMessage(pnode->addr, msg.m_type, msg.data,
3500  /*is_incoming=*/false);
3501  }
3502 
3503  TRACE6(net, outbound_message, pnode->GetId(), pnode->m_addr_name.c_str(),
3504  pnode->ConnectionTypeAsString().c_str(), msg.m_type.c_str(),
3505  msg.data.size(), msg.data.data());
3506 
3507  // make sure we use the appropriate network transport format
3508  std::vector<uint8_t> serializedHeader;
3509  pnode->m_serializer->prepareForTransport(*config, msg, serializedHeader);
3510  size_t nTotalSize = nMessageSize + serializedHeader.size();
3511 
3512  size_t nBytesSent = 0;
3513  {
3514  LOCK(pnode->cs_vSend);
3515  bool optimisticSend(pnode->vSendMsg.empty());
3516 
3517  // log total amount of bytes per message type
3518  pnode->mapSendBytesPerMsgCmd[msg.m_type] += nTotalSize;
3519  pnode->nSendSize += nTotalSize;
3520 
3521  if (pnode->nSendSize > nSendBufferMaxSize) {
3522  pnode->fPauseSend = true;
3523  }
3524  pnode->vSendMsg.push_back(std::move(serializedHeader));
3525  if (nMessageSize) {
3526  pnode->vSendMsg.push_back(std::move(msg.data));
3527  }
3528 
3529  // If write queue empty, attempt "optimistic write"
3530  if (optimisticSend == true) {
3531  nBytesSent = SocketSendData(*pnode);
3532  }
3533  }
3534  if (nBytesSent) {
3535  RecordBytesSent(nBytesSent);
3536  }
3537 }
3538 
3539 bool CConnman::ForNode(NodeId id, std::function<bool(CNode *pnode)> func) {
3540  CNode *found = nullptr;
3542  for (auto &&pnode : m_nodes) {
3543  if (pnode->GetId() == id) {
3544  found = pnode;
3545  break;
3546  }
3547  }
3548  return found != nullptr && NodeFullyConnected(found) && func(found);
3549 }
3550 
3552  return CSipHasher(nSeed0, nSeed1).Write(id);
3553 }
3554 
3555 uint64_t CConnman::CalculateKeyedNetGroup(const CAddress &ad) const {
3556  std::vector<uint8_t> vchNetGroup(ad.GetGroup(addrman.GetAsmap()));
3557 
3559  .Write(vchNetGroup.data(), vchNetGroup.size())
3560  .Finalize();
3561 }
3562 
3577 std::string getSubVersionEB(uint64_t MaxBlockSize) {
3578  // Prepare EB string we are going to add to SubVer:
3579  // 1) translate from byte to MB and convert to string
3580  // 2) limit the EB string to the first decimal digit (floored)
3581  std::stringstream ebMBs;
3582  ebMBs << (MaxBlockSize / (ONE_MEGABYTE / 10));
3583  std::string eb = ebMBs.str();
3584  eb.insert(eb.size() - 1, ".", 1);
3585  if (eb.substr(0, 1) == ".") {
3586  eb = "0" + eb;
3587  }
3588  return eb;
3589 }
3590 
3591 std::string userAgent(const Config &config) {
3592  // format excessive blocksize value
3593  std::string eb = getSubVersionEB(config.GetMaxBlockSize());
3594  std::vector<std::string> uacomments;
3595  uacomments.push_back("EB" + eb);
3596 
3597  // Comments are checked for char compliance at startup, it is safe to add
3598  // them to the user agent string
3599  for (const std::string &cmt : gArgs.GetArgs("-uacomment")) {
3600  uacomments.push_back(cmt);
3601  }
3602 
3603  const std::string client_name = gArgs.GetArg("-uaclientname", CLIENT_NAME);
3604  const std::string client_version =
3605  gArgs.GetArg("-uaclientversion", FormatVersion(CLIENT_VERSION));
3606 
3607  // Size compliance is checked at startup, it is safe to not check it again
3608  return FormatUserAgent(client_name, client_version, uacomments);
3609 }
3610 
3611 void CaptureMessageToFile(const CAddress &addr, const std::string &msg_type,
3612  Span<const uint8_t> data, bool is_incoming) {
3613  // Note: This function captures the message at the time of processing,
3614  // not at socket receive/send time.
3615  // This ensures that the messages are always in order from an application
3616  // layer (processing) perspective.
3617  auto now = GetTime<std::chrono::microseconds>();
3618 
3619  // Windows folder names can not include a colon
3620  std::string clean_addr = addr.ToString();
3621  std::replace(clean_addr.begin(), clean_addr.end(), ':', '_');
3622 
3623  fs::path base_path = gArgs.GetDataDirNet() / "message_capture" / clean_addr;
3624  fs::create_directories(base_path);
3625 
3626  fs::path path =
3627  base_path / (is_incoming ? "msgs_recv.dat" : "msgs_sent.dat");
3628  AutoFile f{fsbridge::fopen(path, "ab")};
3629 
3630  ser_writedata64(f, now.count());
3631  f.write(MakeByteSpan(msg_type));
3632  for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) {
3633  f << uint8_t{'\0'};
3634  }
3635  uint32_t size = data.size();
3636  ser_writedata32(f, size);
3637  f.write(AsBytes(data));
3638 }
3639 
3640 std::function<void(const CAddress &addr, const std::string &msg_type,
3641  Span<const uint8_t> data, bool is_incoming)>
bool DumpPeerAddresses(const CChainParams &chainParams, const ArgsManager &args, const AddrMan &addr)
Definition: addrdb.cpp:152
std::vector< CAddress > ReadAnchors(const CChainParams &chainParams, const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:225
void DumpAnchors(const CChainParams &chainParams, const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:215
ArgsManager gArgs
Definition: args.cpp:38
int flags
Definition: bitcoin-tx.cpp:543
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:19
Stochastic address manager.
Definition: addrman.h:69
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.cpp:1346
void Good(const CService &addr, bool test_before_evict=true, NodeSeconds time=AdjustedTime())
Mark an entry as accessible, possibly moving it from "new" to "tried".
Definition: addrman.cpp:1324
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1359
std::pair< CAddress, NodeSeconds > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1342
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1334
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1315
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1338
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty=0s)
Attempt to add one or more addresses to addrman's new table.
Definition: addrman.cpp:1319
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=AdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1329
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:371
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:215
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:526
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:494
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:556
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:528
bool IsBanned(const CNetAddr &net_addr)
Return whether net_addr is banned.
Definition: banman.cpp:89
bool IsDiscouraged(const CNetAddr &net_addr)
Return whether net_addr is discouraged.
Definition: banman.cpp:84
A CService with information about it as peer.
Definition: protocol.h:442
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
Definition: protocol.h:546
NodeSeconds nTime
Always included in serialization, except in the network format on INIT_PROTO_VERSION.
Definition: protocol.h:544
const CMessageHeader::MessageMagic & NetMagic() const
Definition: chainparams.h:94
uint16_t GetDefaultPort() const
Definition: chainparams.h:95
const std::vector< SeedSpec6 > & FixedSeeds() const
Definition: chainparams.h:133
void CreateNodeFromAcceptedSocket(SOCKET hSocket, NetPermissionFlags permissionFlags, const CAddress &addr_bind, const CAddress &addr)
Create a CNode object from a socket that has just been accepted and add the node to the m_nodes membe...
Definition: net.cpp:1292
std::condition_variable condMsgProc
Definition: net.h:1298
size_t GetNodeCount(NumConnections num) const
Definition: net.cpp:3249
std::thread threadMessageHandler
Definition: net.h:1320
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const
returns the time in second left in the current max outbound cycle in case of no limit,...
Definition: net.cpp:3351
bool OutboundTargetReached(bool historicalBlockServingLimit) const
check if the outbound target is reached.
Definition: net.cpp:3367
CClientUIInterface * m_client_interface
Definition: net.h:1277
size_t SocketSendData(CNode &node) const EXCLUSIVE_LOCKS_REQUIRED(node.cs_vSend)
Definition: net.cpp:823
void ThreadMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
Definition: net.cpp:2610
bool ForNode(NodeId id, std::function< bool(CNode *pnode)> func)
Definition: net.cpp:3539
bool AddConnection(const std::string &address, ConnectionType conn_type)
Attempts to open a connection.
Definition: net.cpp:1415
CNode * ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type)
Definition: net.cpp:423
void DeleteNode(CNode *pnode)
Definition: net.cpp:3145
bool RemoveAddedNode(const std::string &node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Definition: net.cpp:3237
bool AttemptToEvictConnection()
Try to find a connection to evict when the node is full.
Definition: net.cpp:1210
bool AlreadyConnectedToAddress(const CAddress &addr)
Determine whether we're already connected to a given address, in order to avoid initiating duplicate ...
Definition: net.cpp:391
int m_max_outbound
Definition: net.h:1275
ServiceFlags nLocalServices
Services this node offers.
Definition: net.h:1257
bool GetTryNewOutboundPeer() const
Definition: net.cpp:2070
void Stop()
Definition: net.h:932
int m_max_outbound_block_relay
Definition: net.h:1268
std::thread threadI2PAcceptIncoming
Definition: net.h:1321
void SetTryNewOutboundPeer(bool flag)
Definition: net.cpp:2074
std::atomic< bool > flagInterruptMsgProc
Definition: net.h:1300
unsigned int GetReceiveFloodSize() const
Definition: net.cpp:3414
void ThreadOpenAddedConnections() EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Definition: net.cpp:2529
void Interrupt() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
Definition: net.cpp:3056
void ThreadDNSAddressSeed() EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex
Definition: net.cpp:1908
void SocketEvents(std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set)
Definition: net.cpp:1678
NodeId GetNewNodeId()
Definition: net.cpp:2867
CThreadInterrupt interruptNet
This is signaled when network activity should cease.
Definition: net.h:1308
std::unique_ptr< CSemaphore > semAddnode
Definition: net.h:1260
bool Start(CScheduler &scheduler, const Options &options) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex
Definition: net.cpp:2920
std::atomic< NodeId > nLastNodeId
Definition: net.h:1217
void RecordBytesSent(uint64_t bytes)
Definition: net.cpp:3327
int GetExtraBlockRelayCount() const
Definition: net.cpp:2102
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
Definition: net.cpp:1900
BanMan * m_banman
Pointer to this node's banman.
Definition: net.h:1284
uint64_t GetOutboundTargetBytesLeft() const
response the bytes left in the current max outbound cycle in case of no limit, it will always respons...
Definition: net.cpp:3390
std::thread threadDNSAddressSeed
Definition: net.h:1316
void ThreadI2PAcceptIncoming()
Definition: net.cpp:2676
const uint64_t nSeed1
Definition: net.h:1293
std::vector< CAddress > m_anchors
Addresses that were saved during the previous clean shutdown.
Definition: net.h:1290
std::chrono::seconds GetMaxOutboundTimeframe() const
Definition: net.cpp:3347
unsigned int nPrevNodeCount
Definition: net.h:1218
void NotifyNumConnectionsChanged()
Definition: net.cpp:1514
ServiceFlags GetLocalServices() const
Used to convey which local services we are offering peers during node connection.
Definition: net.cpp:3410
bool DisconnectNode(const std::string &node)
Definition: net.cpp:3278
std::chrono::seconds m_peer_connect_timeout
Definition: net.h:1197
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:1327
bool InitBinds(const Options &options)
Definition: net.cpp:2893
void AddAddrFetch(const std::string &strDest) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex)
Definition: net.cpp:132
std::vector< ListenSocket > vhListenSocket
Definition: net.h:1206
std::vector< CAddress > GetCurrentBlockRelayOnlyConns() const
Return vector of current BLOCK_RELAY peers.
Definition: net.cpp:2460
CSipHasher GetDeterministicRandomizer(uint64_t id) const
Get a unique deterministic randomizer.
Definition: net.cpp:3551
uint64_t GetMaxOutboundTarget() const
Definition: net.cpp:3342
std::unique_ptr< CSemaphore > semOutbound
Definition: net.h:1259
std::vector< NetWhitelistPermissions > vWhitelistedRange
Definition: net.h:1201
RecursiveMutex cs_totalBytesSent
Definition: net.h:1186
bool Bind(const CService &addr, unsigned int flags, NetPermissionFlags permissions)
Definition: net.cpp:2871
std::thread threadOpenConnections
Definition: net.h:1319
NumConnections
Definition: net.h:847
@ CONNECTIONS_IN
Definition: net.h:849
@ CONNECTIONS_ALL
Definition: net.h:851
@ CONNECTIONS_OUT
Definition: net.h:850
Mutex m_addr_fetches_mutex
Definition: net.h:1211
bool InactivityCheck(const CNode &node) const
Return true if the peer is inactive and should be disconnected.
Definition: net.cpp:1533
CNode * FindNode(const CNetAddr &ip)
Definition: net.cpp:351
void GetNodeStats(std::vector< CNodeStats > &vstats) const
Definition: net.cpp:3267
std::vector< AddedNodeInfo > GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Definition: net.cpp:2472
const uint64_t nSeed0
SipHasher seeds for deterministic randomness.
Definition: net.h:1293
unsigned int nReceiveFloodSize
Definition: net.h:1204
int GetExtraFullOutboundCount() const
Definition: net.cpp:2086
uint64_t GetTotalBytesRecv() const
Definition: net.cpp:3401
RecursiveMutex m_nodes_mutex
Definition: net.h:1216
static bool NodeFullyConnected(const CNode *pnode)
Definition: net.cpp:3490
void ProcessAddrFetch() EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex)
Definition: net.cpp:2052
int nMaxConnections
Definition: net.h:1261
CConnman(const Config &configIn, uint64_t seed0, uint64_t seed1, AddrMan &addrmanIn, bool network_active=true)
Definition: net.cpp:2856
std::vector< CAddress > GetAddresses(size_t max_addresses, size_t max_pct, std::optional< Network > network) const
Return all or many randomly selected addresses, optionally by network.
Definition: net.cpp:3159
void SetNetworkActive(bool active)
Definition: net.cpp:2842
std::list< CNode * > m_nodes_disconnected
Definition: net.h:1215
void OpenNetworkConnection(const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *strDest, ConnectionType conn_type)
Definition: net.cpp:2561
void AddWhitelistPermissionFlags(NetPermissionFlags &flags, const CNetAddr &addr) const
Definition: net.cpp:567
bool GenerateSelectSet(std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set)
Definition: net.cpp:1573
AddrMan & addrman
Definition: net.h:1209
void SocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
Definition: net.cpp:1761
uint64_t CalculateKeyedNetGroup(const CAddress &ad) const
Definition: net.cpp:3555
Mutex mutexMsgProc
Definition: net.h:1299
bool fAddressesInitialized
Definition: net.h:1208
~CConnman()
Definition: net.cpp:3153
void StopThreads()
Definition: net.cpp:3079
bool AddNode(const std::string &node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Definition: net.cpp:3225
std::thread threadOpenAddedConnections
Definition: net.h:1318
Mutex m_added_nodes_mutex
Definition: net.h:1213
const Config * config
Definition: net.h:1183
void Init(const Options &connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex)
Definition: net.h:883
bool CheckIncomingNonce(uint64_t nonce)
Definition: net.cpp:396
int m_max_outbound_full_relay
Definition: net.h:1264
int nMaxAddnode
Definition: net.h:1273
void RecordBytesRecv(uint64_t bytes)
Definition: net.cpp:3323
bool ShouldRunInactivityChecks(const CNode &node, std::chrono::seconds now) const
Return true if we should disconnect the peer for failing an inactivity check.
Definition: net.cpp:1528
void ThreadSocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc)
Definition: net.cpp:1892
void PushMessage(CNode *pnode, CSerializedNetMsg &&msg)
Definition: net.cpp:3494
void StopNodes()
Definition: net.cpp:3100
unsigned int nSendBufferMaxSize
Definition: net.h:1203
std::unique_ptr< i2p::sam::Session > m_i2p_sam_session
I2P SAM session.
Definition: net.h:1314
bool m_use_addrman_outgoing
Definition: net.h:1276
std::map< uint64_t, CachedAddrResponse > m_addr_response_caches
Addr responses stored in different caches per (network, local socket) prevent cross-network node iden...
Definition: net.h:1245
std::atomic< uint64_t > nTotalBytesRecv
Definition: net.h:1187
std::atomic< bool > fNetworkActive
Definition: net.h:1207
std::atomic_bool m_start_extra_block_relay_peers
flag for initiating extra block-relay-only peer connections.
Definition: net.h:1334
void DisconnectNodes()
Definition: net.cpp:1464
void DumpAddresses()
Definition: net.cpp:2043
std::vector< CService > m_onion_binds
A vector of -bind=<address>:<port>=onion arguments each of which is an address and port that are desi...
Definition: net.h:1340
std::vector< NetEventsInterface * > m_msgproc
Definition: net.h:1279
std::thread threadSocketHandler
Definition: net.h:1317
uint64_t GetTotalBytesSent() const
Definition: net.cpp:3405
void ThreadOpenConnections(std::vector< std::string > connect, std::function< void(const CAddress &, ConnectionType)> mockOpenConnection) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex
Definition: net.cpp:2116
void AcceptConnection(const ListenSocket &hListenSocket)
Definition: net.cpp:1264
bool BindListenPort(const CService &bindAddr, bilingual_str &strError, NetPermissionFlags permissions)
Definition: net.cpp:2714
int m_max_avalanche_outbound
Definition: net.h:1271
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:225
size_type size() const
Definition: streams.h:223
void Finalize(Span< uint8_t > output)
Definition: hash.h:29
CHash256 & Write(Span< const uint8_t > input)
Definition: hash.h:36
Message header.
Definition: protocol.h:34
bool IsValid(const Config &config) const
Definition: protocol.cpp:153
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:39
MessageMagic pchMessageStart
Definition: protocol.h:69
bool IsOversized(const Config &config) const
Definition: protocol.cpp:194
static constexpr size_t HEADER_SIZE
Definition: protocol.h:44
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:72
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:36
std::string GetCommand() const
Definition: protocol.cpp:119
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:37
uint32_t nMessageSize
Definition: protocol.h:71
Network address.
Definition: netaddress.h:121
Network GetNetClass() const
Definition: netaddress.cpp:746
std::string ToStringIP() const
Definition: netaddress.cpp:628
std::string ToString() const
Definition: netaddress.cpp:673
bool IsRoutable() const
Definition: netaddress.cpp:514
bool IsValid() const
Definition: netaddress.cpp:479
bool IsIPv4() const
Definition: netaddress.cpp:342
bool IsIPv6() const
Definition: netaddress.cpp:346
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:808
std::vector< uint8_t > GetAddrBytes() const
Definition: netaddress.cpp:863
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:188
enum Network GetNetwork() const
Definition: netaddress.cpp:551
~CNetCleanup()
Definition: net.cpp:3047
CNetCleanup()
Definition: net.cpp:3045
Transport protocol agnostic message container.
Definition: net.h:331
uint32_t m_message_size
size of the payload
Definition: net.h:341
std::chrono::microseconds m_time
time of message receipt
Definition: net.h:336
uint32_t m_raw_message_size
used wire size of the message (including header/checksum)
Definition: net.h:343
std::string m_type
Definition: net.h:344
bool m_valid_checksum
Definition: net.h:339
bool m_valid_header
Definition: net.h:338
bool m_valid_netmagic
Definition: net.h:337
Information about a peer.
Definition: net.h:456
const CAddress addrBind
Definition: net.h:490
const std::chrono::seconds m_connected
Unix epoch time at peer connection.
Definition: net.h:485
bool m_prefer_evict
Definition: net.h:507
std::atomic< int > nVersion
Definition: net.h:495
std::atomic< double > availabilityScore
The last computed score.
Definition: net.h:785
bool IsInboundConn() const
Definition: net.h:555
NodeId GetId() const
Definition: net.h:712
std::atomic< int64_t > nTimeOffset
Definition: net.h:486
const std::string m_addr_name
Definition: net.h:491
std::string ConnectionTypeAsString() const
Definition: net.h:758
std::atomic< bool > m_bip152_highbandwidth_to
Definition: net.h:593
std::list< CNetMessage > vRecvMsg
Definition: net.h:771
std::atomic< bool > m_bip152_highbandwidth_from
Definition: net.h:595
std::atomic_bool fSuccessfullyConnected
Definition: net.h:511
const CAddress addr
Definition: net.h:488
void SetAddrLocal(const CService &addrLocalIn) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex)
May not be called more than once.
Definition: net.cpp:603
CSemaphoreGrant grantOutbound
Definition: net.h:515
Mutex cs_hSocket
Definition: net.h:473
std::unique_ptr< TransportSerializer > m_serializer
Definition: net.h:462
Mutex m_subver_mutex
cleanSubVer is a sanitized string of the user agent byte array we read from the wire.
Definition: net.h:504
Mutex cs_vSend
Definition: net.h:472
std::atomic_bool fPauseSend
Definition: net.h:520
void CloseSocketDisconnect() EXCLUSIVE_LOCKS_REQUIRED(!cs_hSocket)
Definition: net.cpp:558
std::unique_ptr< TransportDeserializer > m_deserializer
Definition: net.h:461
double getAvailabilityScore() const
Definition: net.cpp:3441
NetPermissionFlags m_permissionFlags
Definition: net.h:769
const ConnectionType m_conn_type
Definition: net.h:766
Network ConnectedThroughNetwork() const
Get network the peer connected through.
Definition: net.cpp:615
void copyStats(CNodeStats &stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex
Definition: net.cpp:619
std::atomic< std::chrono::microseconds > m_last_ping_time
Last measured round-trip time.
Definition: net.h:685
void updateAvailabilityScore(double decayFactor)
The availability score is calculated using an exponentially weighted average.
Definition: net.cpp:3426
bool ReceiveMsgBytes(const Config &config, Span< const uint8_t > msg_bytes, bool &complete) EXCLUSIVE_LOCKS_REQUIRED(!cs_vRecv)
Receive bytes from the buffer and deserialize them into messages.
Definition: net.cpp:667
void invsPolled(uint32_t count)
The node was polled for count invs.
Definition: net.cpp:3418
Mutex m_addr_local_mutex
Definition: net.h:774
const bool m_inbound_onion
Whether this peer is an inbound onion, i.e.
Definition: net.h:494
std::atomic< std::chrono::microseconds > m_min_ping_time
Lowest measured round-trip time.
Definition: net.h:691
std::atomic< std::chrono::seconds > m_last_proof_time
UNIX epoch time of the last proof received from this peer that we had not yet seen (e....
Definition: net.h:682
Mutex cs_vRecv
Definition: net.h:474
std::atomic< bool > m_avalanche_enabled
Definition: net.h:618
std::atomic< std::chrono::seconds > m_last_block_time
UNIX epoch time of the last block received from this peer that we had not yet seen (e....
Definition: net.h:666
~CNode()
Definition: net.cpp:3486
std::atomic< uint64_t > invCounters
The inventories polled and voted counters since last score computation, stored as a pair of uint32_t ...
Definition: net.h:782
std::atomic_bool fDisconnect
Definition: net.h:514
std::atomic< std::chrono::seconds > m_last_recv
Definition: net.h:483
CNode(NodeId id, 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, bool inbound_onion)
Definition: net.cpp:3447
std::atomic< std::chrono::seconds > m_last_tx_time
UNIX epoch time of the last transaction received from this peer that we had not yet seen (e....
Definition: net.h:674
CService GetAddrLocal() const EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex)
Definition: net.cpp:597
void invsVoted(uint32_t count)
The node voted for count invs.
Definition: net.cpp:3422
std::atomic< std::chrono::seconds > m_last_send
Definition: net.h:482
CNode * AddRef()
Definition: net.h:745
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:41
void scheduleEvery(Predicate p, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Repeat p until it return false.
Definition: scheduler.cpp:114
RAII-style semaphore lock.
Definition: sync.h:397
bool TryAcquire()
Definition: sync.h:419
void MoveTo(CSemaphoreGrant &grant)
Definition: sync.h:426
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
std::string ToStringIPPort() const
std::string ToString() const
uint16_t GetPort() const
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:995
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
SipHash-2-4.
Definition: siphash.h:13
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:82
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
Definition: siphash.cpp:36
std::string ToString() const
bool Match(const CNetAddr &addr) const
bool sleep_for(std::chrono::milliseconds rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
Minimal stream for overwriting and/or appending to an existing byte vector.
Definition: streams.h:65
Definition: config.h:19
virtual uint64_t GetMaxBlockSize() const =0
virtual const CChainParams & GetChainParams() const =0
Fast randomness source.
Definition: random.h:156
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range)
Return the time point advanced by a uniform random duration.
Definition: random.h:260
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:211
Different type to mark Mutex at global scope.
Definition: sync.h:144
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:799
NetPermissionFlags m_flags
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
static void ClearFlag(NetPermissionFlags &flags, NetPermissionFlags f)
ClearFlag is only called with f == NetPermissionFlags::Implicit.
static bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
static bool TryParse(const std::string &str, NetWhitebindPermissions &output, bilingual_str &error)
constexpr std::size_t size() const noexcept
Definition: span.h:209
constexpr C * data() const noexcept
Definition: span.h:198
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:227
CNetMessage GetMessage(const Config &config, std::chrono::microseconds time) override
Definition: net.cpp:764
CDataStream vRecv
Definition: net.h:382
CMessageHeader hdr
Definition: net.h:380
const uint256 & GetMessageHash() const
Definition: net.cpp:755
uint32_t nDataPos
Definition: net.h:384
uint32_t nHdrPos
Definition: net.h:383
int readData(Span< const uint8_t > msg_bytes)
Definition: net.cpp:738
bool Complete() const override
Definition: net.h:410
int readHeader(const Config &config, Span< const uint8_t > msg_bytes)
Definition: net.cpp:705
CHash256 hasher
Definition: net.h:372
CDataStream hdrbuf
Definition: net.h:378
uint256 data_hash
Definition: net.h:373
void prepareForTransport(const Config &config, CSerializedNetMsg &msg, std::vector< uint8_t > &header) override
Definition: net.cpp:807
uint8_t * begin()
Definition: uint256.h:85
bool IsNull() const
Definition: uint256.h:32
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
CService proxy
Definition: netbase.h:40
256-bit opaque blob.
Definition: uint256.h:129
std::string FormatVersion(int nVersion)
std::string FormatUserAgent(const std::string &name, const std::string &version, const std::vector< std::string > &comments)
Format the subversion field according to BIP 14 spec.
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
const std::string CLIENT_NAME
#define INVALID_SOCKET
Definition: compat.h:52
#define WSAEWOULDBLOCK
Definition: compat.h:45
#define SOCKET_ERROR
Definition: compat.h:53
#define WSAGetLastError()
Definition: compat.h:42
static bool IsSelectableSocket(const SOCKET &s)
Definition: compat.h:102
#define WSAEMSGSIZE
Definition: compat.h:47
#define MSG_NOSIGNAL
Definition: compat.h:113
#define MSG_DONTWAIT
Definition: compat.h:119
unsigned int SOCKET
Definition: compat.h:40
void * sockopt_arg_type
Definition: compat.h:87
#define WSAEINPROGRESS
Definition: compat.h:49
#define WSAEADDRINUSE
Definition: compat.h:50
#define WSAEINTR
Definition: compat.h:48
const Config & GetConfig()
Definition: config.cpp:40
static const uint64_t ONE_MEGABYTE
1MB
Definition: consensus.h:12
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
const std::vector< std::string > GetRandomizedDNSSeeds(const CChainParams &params)
Return the list of hostnames to look up for DNS seeds.
Definition: dnsseeds.cpp:11
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
bool fLogIPs
Definition: logging.cpp:17
bool error(const char *fmt, const Args &...args)
Definition: logging.h:226
#define LogPrint(category,...)
Definition: logging.h:211
#define LogPrintf(...)
Definition: logging.h:207
static unsigned char elements[DATACOUNT][DATALEN]
Definition: tests_impl.h:36
@ NETDEBUG
Definition: logging.h:69
@ NET
Definition: logging.h:40
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:179
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
Definition: init.h:28
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
void TraceThread(const char *thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Definition: thread.cpp:13
static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:903
bool IsPeerAddrLocalGood(CNode *pnode)
Definition: net.cpp:237
uint16_t GetListenPort()
Definition: net.cpp:137
static void EraseLastKElements(std::vector< T > &elements, Comparator comparator, size_t k, std::function< bool(const NodeEvictionCandidate &)> predicate=[](const NodeEvictionCandidate &n) { return true;})
Sort an array by the specified comparator, then erase the last K elements where predicate is true.
Definition: net.cpp:1009
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE
Number of DNS seeds to query when the number of connections is low.
Definition: net.cpp:72
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:346
static const uint64_t RANDOMIZER_ID_NETGROUP
Definition: net.cpp:115
CService GetLocalAddress(const CNetAddr &addrPeer)
Definition: net.cpp:221
static const uint64_t SELECT_TIMEOUT_MILLISECONDS
Definition: net.cpp:110
static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:951
void RemoveLocal(const CService &addr)
Definition: net.cpp:311
std::optional< CService > GetLocalAddrForPeer(CNode &node)
Returns a local address that we should advertise to this peer.
Definition: net.cpp:243
BindFlags
Used to pass flags to the Bind() function.
Definition: net.cpp:97
@ BF_REPORT_ERROR
Definition: net.cpp:100
@ BF_NONE
Definition: net.cpp:98
@ BF_EXPLICIT
Definition: net.cpp:99
@ BF_DONT_ADVERTISE
Do not call AddLocal() for our special addresses, e.g., for incoming Tor connections,...
Definition: net.cpp:105
bool fDiscover
Definition: net.cpp:125
static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE
Definition: net.cpp:117
static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL
Definition: net.cpp:67
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD
Definition: net.cpp:87
bool fListen
Definition: net.cpp:126
static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS
Maximum number of block-relay-only anchor connections.
Definition: net.cpp:58
bool GetLocal(CService &addr, const CNetAddr *paddrPeer)
Definition: net.cpp:173
static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:893
std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY(g_maplocalhost_mutex)
static bool CompareNodeAvailabilityScore(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:968
static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:898
static CAddress GetBindAddress(SOCKET sock)
Get the bind address for a socket as CAddress.
Definition: net.cpp:408
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS
How long to delay before querying DNS seeds.
Definition: net.cpp:84
static const uint64_t RANDOMIZER_ID_ADDRCACHE
Definition: net.cpp:121
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
Definition: net.cpp:1020
const std::string NET_MESSAGE_COMMAND_OTHER
Definition: net.cpp:112
void SetReachable(enum Network net, bool reachable)
Mark a network as reachable or unreachable (no automatic connects to it)
Definition: net.cpp:317
static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:918
std::function< void(const CAddress &addr, const std::string &msg_type, Span< const uint8_t > data, bool is_incoming)> CaptureMessage
Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
Definition: net.cpp:3642
const char *const ANCHORS_DATABASE_FILENAME
Anchor IP address database file name.
Definition: net.cpp:64
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:3577
GlobalMutex g_maplocalhost_mutex
Definition: net.cpp:127
bool AddLocal(const CService &addr, int nScore)
Definition: net.cpp:278
std::optional< NodeId > SelectNodeToEvict(std::vector< NodeEvictionCandidate > &&vEvictionCandidates)
Select an inbound peer to evict after filtering out (protecting) peers having distinct,...
Definition: net.cpp:1115
static bool CompareNodeProofTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:937
#define FEELER_SLEEP_WINDOW
Definition: net.cpp:94
void CaptureMessageToFile(const CAddress &addr, const std::string &msg_type, Span< const uint8_t > data, bool is_incoming)
Dump binary message to file, with timestamp.
Definition: net.cpp:3611
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS
Definition: net.cpp:85
static int GetnScore(const CService &addr)
Definition: net.cpp:230
std::string ConnectionTypeAsString(ConnectionType conn_type)
Convert ConnectionType enum to a string value.
Definition: net.cpp:576
static std::vector< CAddress > convertSeed6(const std::vector< SeedSpec6 > &vSeedsIn)
Convert the pnSeed6 array into usable address objects.
Definition: net.cpp:198
static const uint64_t RANDOMIZER_ID_EXTRAENTROPY
Definition: net.cpp:119
static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
Definition: net.cpp:888
static CNetCleanup instance_of_cnetcleanup
Definition: net.cpp:3054
std::string userAgent(const Config &config)
Definition: net.cpp:3591
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME
The default timeframe for -maxuploadtarget.
Definition: net.cpp:90
void Discover()
Look up IP addresses from all interfaces on the machine and add them to the list of local addresses t...
Definition: net.cpp:2789
bool IsReachable(enum Network net)
Definition: net.cpp:325
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:335
static constexpr std::chrono::minutes TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Definition: net.h:63
static const bool DEFAULT_WHITELISTFORCERELAY
Default for -whitelistforcerelay.
Definition: net.h:57
static const bool DEFAULT_FORCEDNSSEED
Definition: net.h:103
static const bool DEFAULT_WHITELISTRELAY
Default for -whitelistrelay.
Definition: net.h:55
static constexpr auto EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL
Run the extra block-relay-only connection loop once every 5 minutes.
Definition: net.h:67
static const bool DEFAULT_FIXEDSEEDS
Definition: net.h:105
ConnectionType
Different types of connections to a peer.
Definition: net.h:151
@ BLOCK_RELAY
We use block-relay-only connections to help prevent against partition attacks.
@ MANUAL
We open manual connections to addresses that users explicitly inputted via the addnode RPC,...
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ FEELER
Feeler connections are short-lived connections made to check that a node is alive.
@ INBOUND
Inbound connections are those initiated by a peer.
@ AVALANCHE_OUTBOUND
Special case of connection to a full relay outbound with avalanche service enabled.
@ ADDR_FETCH
AddrFetch connections are short lived connections used to solicit addresses from peers.
static constexpr auto FEELER_INTERVAL
Run the feeler connection loop once every 2 minutes.
Definition: net.h:65
static const bool DEFAULT_DNSSEED
Definition: net.h:104
@ LOCAL_MANUAL
Definition: net.h:242
@ LOCAL_BIND
Definition: net.h:238
@ LOCAL_IF
Definition: net.h:236
static const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS
Maximum number of block-relay-only outgoing connections.
Definition: net.h:78
NetPermissionFlags
Network
A network type.
Definition: netaddress.h:44
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:69
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
bool GetNameProxy(proxyType &nameProxyOut)
Definition: netbase.cpp:739
bool HaveNameProxy()
Definition: netbase.cpp:748
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:720
bool SetSocketNoDelay(const SOCKET &hSocket)
Set the TCP_NODELAY flag on a socket.
Definition: netbase.cpp:848
bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, uint16_t port, const Sock &sock, int nTimeout, bool &outProxyConnectionFailed)
Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 p...
Definition: netbase.cpp:763
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:855
bool ConnectSocketDirectly(const CService &addrConnect, const Sock &sock, int nTimeout, bool manual_connection)
Try to connect to the specified service on the specified socket.
Definition: netbase.cpp:634
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:620
bool Lookup(const std::string &name, std::vector< CService > &vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:222
bool fNameLookup
Definition: netbase.cpp:38
int nConnectTimeout
Definition: netbase.cpp:37
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:260
bool IsBadPort(uint16_t port)
Determine if a port is "bad" from the perspective of attempting to connect to a node on that port.
Definition: netbase.cpp:859
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:190
int64_t NodeId
Definition: nodeid.h:10
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:204
const std::vector< std::string > & getAllNetMessageTypes()
Get a vector of all valid message types (see above)
Definition: protocol.cpp:243
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services),...
Definition: protocol.h:427
ServiceFlags
nServices flags.
Definition: protocol.h:335
@ NODE_NONE
Definition: protocol.h:338
@ NODE_BLOOM
Definition: protocol.h:352
@ NODE_AVALANCHE
Definition: protocol.h:380
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:435
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval)
Return a timestamp in the future sampled from an exponential distribution (https://en....
Definition: random.cpp:794
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
Definition: random.cpp:649
constexpr auto GetRandMillis
Definition: random.h:107
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:291
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition: random.h:85
static RPCHelpMan send()
Definition: rpcwallet.cpp:4219
static uint16_t GetDefaultPort()
Definition: bitcoin.h:18
void ser_writedata32(Stream &s, uint32_t obj)
Definition: serialize.h:69
@ SER_NETWORK
Definition: serialize.h:152
void ser_writedata64(Stream &s, uint64_t obj)
Definition: serialize.h:79
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:332
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:339
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:301
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:294
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
Cache responses to addr requests to minimize privacy leak.
Definition: net.h:1226
std::chrono::microseconds m_cache_entry_expiration
Definition: net.h:1228
std::vector< CAddress > m_addrs_response_cache
Definition: net.h:1227
void AddSocketPermissionFlags(NetPermissionFlags &flags) const
Definition: net.h:1086
std::vector< NetWhitebindPermissions > vWhiteBinds
Definition: net.h:871
std::vector< CService > onion_binds
Definition: net.h:873
std::vector< std::string > m_specified_outgoing
Definition: net.h:878
std::vector< CService > vBinds
Definition: net.h:872
bool m_i2p_accept_incoming
Definition: net.h:880
std::vector< std::string > vSeedNodes
Definition: net.h:869
bool m_use_addrman_outgoing
Definition: net.h:877
bool bind_on_any
True if the user did not specify -bind= or -whitebind= and thus we should bind on 0....
Definition: net.h:876
POD that contains various stats about a node.
Definition: net.h:290
std::string addrLocal
Definition: net.h:314
CAddress addrBind
Definition: net.h:318
uint64_t nRecvBytes
Definition: net.h:308
mapMsgCmdSize mapSendBytesPerMsgCmd
Definition: net.h:307
std::chrono::microseconds m_last_ping_time
Definition: net.h:311
bool fInbound
Definition: net.h:302
uint64_t nSendBytes
Definition: net.h:306
std::chrono::seconds m_last_recv
Definition: net.h:293
std::optional< double > m_availabilityScore
Definition: net.h:323
std::chrono::seconds m_last_proof_time
Definition: net.h:295
ConnectionType m_conn_type
Definition: net.h:322
std::chrono::seconds m_last_send
Definition: net.h:292
std::chrono::seconds m_last_tx_time
Definition: net.h:294
CAddress addr
Definition: net.h:316
std::chrono::microseconds m_min_ping_time
Definition: net.h:312
int64_t nTimeOffset
Definition: net.h:298
std::chrono::seconds m_connected
Definition: net.h:297
bool m_bip152_highbandwidth_from
Definition: net.h:304
bool m_bip152_highbandwidth_to
Definition: net.h:303
std::string m_addr_name
Definition: net.h:299
mapMsgCmdSize mapRecvBytesPerMsgCmd
Definition: net.h:309
int nVersion
Definition: net.h:300
std::chrono::seconds m_last_block_time
Definition: net.h:296
Network m_network
Definition: net.h:320
NodeId nodeid
Definition: net.h:291
NetPermissionFlags m_permissionFlags
Definition: net.h:310
std::string cleanSubVer
Definition: net.h:301
std::vector< uint8_t > data
Definition: net.h:134
std::string m_type
Definition: net.h:135
Sort eviction candidates by network/localhost and connection uptime.
Definition: net.cpp:989
CompareNodeNetworkTime(bool is_local, Network network)
Definition: net.cpp:992
const Network m_network
Definition: net.cpp:991
bool operator()(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) const
Definition: net.cpp:994
const bool m_is_local
Definition: net.cpp:990
uint16_t nPort
Definition: net.h:274
int nScore
Definition: net.h:273
std::chrono::seconds m_last_tx_time
Definition: net.h:1366
Network m_network
Definition: net.h:1373
double availabilityScore
Definition: net.h:1374
std::chrono::seconds m_connected
Definition: net.h:1362
std::chrono::seconds m_last_block_time
Definition: net.h:1364
bool fRelevantServices
Definition: net.h:1367
std::chrono::microseconds m_min_ping_time
Definition: net.h:1363
std::chrono::seconds m_last_proof_time
Definition: net.h:1365
uint64_t nKeyedNetGroup
Definition: net.h:1370
Bilingual messages:
Definition: translation.h:17
std::string original
Definition: translation.h:18
An established connection with another peer.
Definition: i2p.h:31
std::unique_ptr< Sock > sock
Connected socket.
Definition: i2p.h:33
CService me
Our I2P address.
Definition: i2p.h:36
#define WAIT_LOCK(cs, name)
Definition: sync.h:317
#define AssertLockNotHeld(cs)
Definition: sync.h:163
#define LOCK2(cs1, cs2)
Definition: sync.h:309
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
static int count
Definition: tests.c:31
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:101
int64_t GetTime()
Definition: time.cpp:109
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:55
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
NodeSeconds AdjustedTime()
Definition: timedata.h:70
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
#define TRACE6(context, event, a, b, c, d, e, f)
Definition: trace.h:45
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:14