net.cpp raw
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka 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 #include <limenka-build-config.h> // IWYU pragma: keep
7
8 #include <net.h>
9
10 #include <addrdb.h>
11 #include <addrman.h>
12 #include <banman.h>
13 #include <clientversion.h>
14 #include <common/args.h>
15 #include <common/netif.h>
16 #include <compat/compat.h>
17 #include <consensus/consensus.h>
18 #include <crypto/sha256.h>
19 #include <i2p.h>
20 #include <key.h>
21 #include <logging.h>
22 #include <memusage.h>
23 #include <net_permissions.h>
24 #include <netaddress.h>
25 #include <netbase.h>
26 #include <node/eviction.h>
27 #include <node/interface_ui.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/threadinterrupt.h>
36 #include <util/trace.h>
37 #include <util/translation.h>
38 #include <util/vector.h>
39
40 #ifdef WIN32
41 #include <string.h>
42 #endif
43
44 #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
45 #include <ifaddrs.h>
46 #endif
47
48 #include <algorithm>
49 #include <array>
50 #include <cmath>
51 #include <cstdint>
52 #include <functional>
53 #include <optional>
54 #include <unordered_map>
55
56 TRACEPOINT_SEMAPHORE(net, closed_connection);
57 TRACEPOINT_SEMAPHORE(net, evicted_inbound_connection);
58 TRACEPOINT_SEMAPHORE(net, inbound_connection);
59 TRACEPOINT_SEMAPHORE(net, outbound_connection);
60 TRACEPOINT_SEMAPHORE(net, outbound_message);
61
62 /** Maximum number of block-relay-only anchor connections */
63 static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2;
64 static_assert (MAX_BLOCK_RELAY_ONLY_ANCHORS <= static_cast<size_t>(MAX_BLOCK_RELAY_ONLY_CONNECTIONS), "MAX_BLOCK_RELAY_ONLY_ANCHORS must not exceed MAX_BLOCK_RELAY_ONLY_CONNECTIONS.");
65 /** Anchor IP address database file name */
66 const char* const ANCHORS_DATABASE_FILENAME = "anchors.dat";
67
68 // How often to dump addresses to peers.dat
69 static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15};
70
71 /** Number of DNS seeds to query when the number of connections is low. */
72 static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3;
73
74 /** Minimum number of outbound connections under which we will keep fetching our address seeds. */
75 static constexpr int SEED_OUTBOUND_CONNECTION_THRESHOLD = 2;
76
77 /** How long to delay before querying DNS seeds
78 *
79 * If we have more than THRESHOLD entries in addrman, then it's likely
80 * that we got those addresses from having previously connected to the P2P
81 * network, and that we'll be able to successfully reconnect to the P2P
82 * network via contacting one of them. So if that's the case, spend a
83 * little longer trying to connect to known peers before querying the
84 * DNS seeds.
85 */
86 static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11};
87 static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
88 static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers
89
90 /** The default timeframe for -maxuploadtarget. 1 day. */
91 static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24};
92
93 // A random time period (0 to 1 seconds) is added to feeler connections to prevent synchronization.
94 static constexpr auto FEELER_SLEEP_WINDOW{1s};
95
96 /** Frequency to attempt extra connections to reachable networks we're not connected to yet **/
97 static constexpr auto EXTRA_NETWORK_PEER_INTERVAL{5min};
98
99 /** Used to pass flags to the Bind() function */
100 enum BindFlags {
101 BF_NONE = 0,
102 BF_REPORT_ERROR = (1U << 0),
103 /**
104 * Do not call AddLocal() for our special addresses, e.g., for incoming
105 * Tor connections, to prevent gossiping them over the network.
106 */
107 BF_DONT_ADVERTISE = (1U << 1),
108 };
109
110 // The set of sockets cannot be modified while waiting
111 // The sleep time needs to be small to avoid new sockets stalling
112 static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50;
113
114 const std::string NET_MESSAGE_TYPE_OTHER = "*other*";
115
116 static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
117 static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // SHA256("localhostnonce")[0:8]
118 static const uint64_t RANDOMIZER_ID_NETWORKKEY = 0x0e8a2b136c592a7dULL; // SHA256("networkkey")[0:8]
119 //
120 // Global state variables
121 //
122 bool fDiscover = true;
123 bool fListen = true;
124 GlobalMutex g_maplocalhost_mutex;
125 std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(g_maplocalhost_mutex);
126 std::string strSubVersion;
127
128 size_t CSerializedNetMsg::GetMemoryUsage() const noexcept
129 {
130 return sizeof(*this) + memusage::DynamicUsage(m_type) + memusage::DynamicUsage(data);
131 }
132
133 size_t CNetMessage::GetMemoryUsage() const noexcept
134 {
135 return sizeof(*this) + memusage::DynamicUsage(m_type) + m_recv.GetMemoryUsage();
136 }
137
138 void CConnman::AddAddrFetch(const std::string& strDest)
139 {
140 LOCK(m_addr_fetches_mutex);
141 m_addr_fetches.push_back(strDest);
142 }
143
144 uint16_t GetListenPort()
145 {
146 // If -bind= is provided with ":port" part, use that (first one if multiple are provided).
147 for (const std::string& bind_arg : gArgs.GetArgs("-bind")) {
148 constexpr uint16_t dummy_port = 0;
149
150 const std::optional<CService> bind_addr{Lookup(bind_arg, dummy_port, /*fAllowLookup=*/false)};
151 if (bind_addr.has_value() && bind_addr->GetPort() != dummy_port) return bind_addr->GetPort();
152 }
153
154 // Otherwise, if -whitebind= without NetPermissionFlags::NoBan is provided, use that
155 // (-whitebind= is required to have ":port").
156 for (const std::string& whitebind_arg : gArgs.GetArgs("-whitebind")) {
157 NetWhitebindPermissions whitebind;
158 bilingual_str error;
159 if (NetWhitebindPermissions::TryParse(whitebind_arg, whitebind, error)) {
160 if (!NetPermissions::HasFlag(whitebind.m_flags, NetPermissionFlags::NoBan)) {
161 return whitebind.m_service.GetPort();
162 }
163 }
164 }
165
166 // Otherwise, if -port= is provided, use that. Otherwise use the default port.
167 return static_cast<uint16_t>(gArgs.GetIntArg("-port", Params().GetDefaultPort()));
168 }
169
170 // Determine the "best" local address for a particular peer.
171 [[nodiscard]] static std::optional<CService> GetLocal(const CNode& peer)
172 {
173 if (!fListen) return std::nullopt;
174
175 std::optional<CService> addr;
176 int nBestScore = -1;
177 int nBestReachability = -1;
178 {
179 LOCK(g_maplocalhost_mutex);
180 for (const auto& [local_addr, local_service_info] : mapLocalHost) {
181 // For privacy reasons, don't advertise our privacy-network address
182 // to other networks and don't advertise our other-network address
183 // to privacy networks.
184 if (local_addr.GetNetwork() != peer.ConnectedThroughNetwork()
185 && (local_addr.IsPrivacyNet() || peer.IsConnectedThroughPrivacyNet())) {
186 continue;
187 }
188 const int nScore{local_service_info.nScore};
189 const int nReachability{local_addr.GetReachabilityFrom(peer.addr)};
190 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
191 addr.emplace(CService{local_addr, local_service_info.nPort});
192 nBestReachability = nReachability;
193 nBestScore = nScore;
194 }
195 }
196 }
197 return addr;
198 }
199
200 //! Convert the serialized seeds into usable address objects.
201 static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
202 {
203 // It'll only connect to one or two seed nodes because once it connects,
204 // it'll get a pile of addresses with newer timestamps.
205 // Seed nodes are given a random 'last seen time' of between one and two
206 // weeks ago.
207 const auto one_week{7 * 24h};
208 std::vector<CAddress> vSeedsOut;
209 FastRandomContext rng;
210 ParamsStream s{DataStream{vSeedsIn}, CAddress::V2_NETWORK};
211 while (!s.eof()) {
212 CService endpoint;
213 s >> endpoint;
214 CAddress addr{endpoint, SeedsServiceFlags()};
215 addr.nTime = rng.rand_uniform_delay(Now<NodeSeconds>() - one_week, -one_week);
216 LogDebug(BCLog::NET, "Added hardcoded seed: %s\n", addr.ToStringAddrPort());
217 vSeedsOut.push_back(addr);
218 }
219 return vSeedsOut;
220 }
221
222 // Determine the "best" local address for a particular peer.
223 // If none, return the unroutable 0.0.0.0 but filled in with
224 // the normal parameters, since the IP may be changed to a useful
225 // one by discovery.
226 CService GetLocalAddress(const CNode& peer)
227 {
228 return GetLocal(peer).value_or(CService{CNetAddr(), GetListenPort()});
229 }
230
231 int GetnScore(const CService& addr)
232 {
233 LOCK(g_maplocalhost_mutex);
234 const auto it = mapLocalHost.find(addr);
235 return (it != mapLocalHost.end()) ? it->second.nScore : 0;
236 }
237
238 // Is our peer's addrLocal potentially useful as an external IP source?
239 [[nodiscard]] static bool IsPeerAddrLocalGood(CNode *pnode)
240 {
241 CService addrLocal = pnode->GetAddrLocal();
242 return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
243 g_reachable_nets.Contains(addrLocal);
244 }
245
246 std::optional<CService> GetLocalAddrForPeer(CNode& node)
247 {
248 CService addrLocal{GetLocalAddress(node)};
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) && (!addrLocal.IsRoutable() ||
254 rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
255 {
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()) {
269 LogDebug(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToStringAddrPort(), node.GetId());
270 return addrLocal;
271 }
272 // Address is unroutable. Don't advertise.
273 return std::nullopt;
274 }
275
276 // learn a new local address
277 bool AddLocal(const CService& addr_, int nScore)
278 {
279 CService addr{MaybeFlipIPv6toCJDNS(addr_)};
280
281 if (!addr.IsRoutable())
282 return false;
283
284 if (!fDiscover && nScore < LOCAL_MANUAL)
285 return false;
286
287 // IPv4 and IPv6 cannot be connected to unless their networks are reachable, but Tor is not necessarily bidirectional
288 if (!(g_reachable_nets.Contains(addr) || addr.IsTor()))
289 return false;
290
291 LogPrintf("AddLocal(%s,%i)\n", addr.ToStringAddrPort(), nScore);
292
293 bool fAlready;
294 {
295 LOCK(g_maplocalhost_mutex);
296 const auto [it, is_newly_added] = mapLocalHost.emplace(addr, LocalServiceInfo());
297 fAlready = !is_newly_added;
298 LocalServiceInfo &info = it->second;
299 if (is_newly_added || nScore >= info.nScore) {
300 info.nScore = nScore + (is_newly_added ? 0 : 1);
301 info.nPort = addr.GetPort();
302 }
303 }
304
305 if (!fAlready) {
306 uiInterface.NotifyNetworkLocalChanged();
307 }
308
309 return true;
310 }
311
312 bool AddLocal(const CNetAddr &addr, int nScore)
313 {
314 return AddLocal(CService(addr, GetListenPort()), nScore);
315 }
316
317 void RemoveLocal(const CService& addr)
318 {
319 {
320 LOCK(g_maplocalhost_mutex);
321 LogPrintf("RemoveLocal(%s)\n", addr.ToStringAddrPort());
322 mapLocalHost.erase(addr);
323 }
324 uiInterface.NotifyNetworkLocalChanged();
325 }
326
327 /** vote for a local address */
328 bool SeenLocal(const CService& addr)
329 {
330 LOCK(g_maplocalhost_mutex);
331 const auto it = mapLocalHost.find(addr);
332 if (it == mapLocalHost.end()) return false;
333 if (it->second.nScore < std::numeric_limits<int>::max()) {
334 ++it->second.nScore;
335 }
336 return true;
337 }
338
339
340 /** check whether a given address is potentially local */
341 bool IsLocal(const CService& addr)
342 {
343 LOCK(g_maplocalhost_mutex);
344 return mapLocalHost.count(addr) > 0;
345 }
346
347 CNode* CConnman::FindNode(const CNetAddr& ip)
348 {
349 LOCK(m_nodes_mutex);
350 for (CNode* pnode : m_nodes) {
351 if (static_cast<CNetAddr>(pnode->addr) == ip) {
352 return pnode;
353 }
354 }
355 return nullptr;
356 }
357
358 CNode* CConnman::FindNode(const std::string& addrName)
359 {
360 LOCK(m_nodes_mutex);
361 for (CNode* pnode : m_nodes) {
362 if (pnode->m_addr_name == addrName) {
363 return pnode;
364 }
365 }
366 return nullptr;
367 }
368
369 CNode* CConnman::FindNode(const CService& addr)
370 {
371 LOCK(m_nodes_mutex);
372 for (CNode* pnode : m_nodes) {
373 if (static_cast<CService>(pnode->addr) == addr) {
374 return pnode;
375 }
376 }
377 return nullptr;
378 }
379
380 bool CConnman::AlreadyConnectedToAddress(const CAddress& addr)
381 {
382 return FindNode(static_cast<CNetAddr>(addr)) || FindNode(addr.ToStringAddrPort());
383 }
384
385 bool CConnman::CheckIncomingNonce(uint64_t nonce)
386 {
387 LOCK(m_nodes_mutex);
388 for (const CNode* pnode : m_nodes) {
389 if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() && pnode->GetLocalNonce() == nonce)
390 return false;
391 }
392 return true;
393 }
394
395 /** Get the bind address for a socket as CService. */
396 static CService GetBindAddress(const Sock& sock)
397 {
398 CService addr_bind;
399 struct sockaddr_storage sockaddr_bind;
400 socklen_t sockaddr_bind_len = sizeof(sockaddr_bind);
401 if (!sock.GetSockName((struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
402 addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind, sockaddr_bind_len);
403 } else {
404 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "getsockname failed\n");
405 }
406 return addr_bind;
407 }
408
409 CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport)
410 {
411 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
412 assert(conn_type != ConnectionType::INBOUND);
413
414 if (pszDest == nullptr) {
415 if (IsLocal(addrConnect))
416 return nullptr;
417
418 // Look for an existing connection
419 CNode* pnode = FindNode(static_cast<CService>(addrConnect));
420 if (pnode)
421 {
422 LogPrintf("Failed to open new connection, already connected\n");
423 return nullptr;
424 }
425 }
426
427 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying %s connection %s lastseen=%.1fhrs\n",
428 use_v2transport ? "v2" : "v1",
429 pszDest ? pszDest : addrConnect.ToStringAddrPort(),
430 Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.nTime));
431
432 // Resolve
433 const uint16_t default_port{pszDest != nullptr ? GetDefaultPort(pszDest) :
434 m_params.GetDefaultPort()};
435
436 // Collection of addresses to try to connect to: either all dns resolved addresses if a domain name (pszDest) is provided, or addrConnect otherwise.
437 std::vector<CAddress> connect_to{};
438 if (pszDest) {
439 std::vector<CService> resolved{Lookup(pszDest, default_port, fNameLookup && !HaveNameProxy(), 256)};
440 if (!resolved.empty()) {
441 std::shuffle(resolved.begin(), resolved.end(), FastRandomContext());
442 // If the connection is made by name, it can be the case that the name resolves to more than one address.
443 // We don't want to connect any more of them if we are already connected to one
444 for (const auto& r : resolved) {
445 addrConnect = CAddress{MaybeFlipIPv6toCJDNS(r), NODE_NONE};
446 if (!addrConnect.IsValid()) {
447 LogDebug(BCLog::NET, "Resolver returned invalid address %s for %s\n", addrConnect.ToStringAddrPort(), pszDest);
448 return nullptr;
449 }
450 // It is possible that we already have a connection to the IP/port pszDest resolved to.
451 // In that case, drop the connection that was just created.
452 LOCK(m_nodes_mutex);
453 CNode* pnode = FindNode(static_cast<CService>(addrConnect));
454 if (pnode) {
455 LogPrintf("Not opening a connection to %s, already connected to %s\n", pszDest, addrConnect.ToStringAddrPort());
456 return nullptr;
457 }
458 // Add the address to the resolved addresses vector so we can try to connect to it later on
459 connect_to.push_back(addrConnect);
460 }
461 } else {
462 // For resolution via proxy
463 connect_to.push_back(addrConnect);
464 }
465 } else {
466 // Connect via addrConnect directly
467 connect_to.push_back(addrConnect);
468 }
469
470 // Connect
471 std::unique_ptr<Sock> sock;
472 Proxy proxy;
473 CService addr_bind;
474 assert(!addr_bind.IsValid());
475 std::unique_ptr<i2p::sam::Session> i2p_transient_session;
476
477 for (auto& target_addr: connect_to) {
478 if (DisableV1OnClearnet(target_addr.GetNetClass()) && !use_v2transport) {
479 continue;
480 }
481 if (target_addr.IsValid()) {
482 const bool use_proxy{GetProxy(target_addr.GetNetwork(), proxy)};
483 bool proxyConnectionFailed = false;
484
485 if (target_addr.IsI2P() && use_proxy) {
486 i2p::Connection conn;
487 bool connected{false};
488
489 if (m_i2p_sam_session) {
490 connected = m_i2p_sam_session->Connect(target_addr, conn, proxyConnectionFailed);
491 } else {
492 {
493 LOCK(m_unused_i2p_sessions_mutex);
494 if (m_unused_i2p_sessions.empty()) {
495 i2p_transient_session =
496 std::make_unique<i2p::sam::Session>(proxy, &interruptNet);
497 } else {
498 i2p_transient_session.swap(m_unused_i2p_sessions.front());
499 m_unused_i2p_sessions.pop();
500 }
501 }
502 connected = i2p_transient_session->Connect(target_addr, conn, proxyConnectionFailed);
503 if (!connected) {
504 LOCK(m_unused_i2p_sessions_mutex);
505 if (m_unused_i2p_sessions.size() < MAX_UNUSED_I2P_SESSIONS_SIZE) {
506 m_unused_i2p_sessions.emplace(i2p_transient_session.release());
507 }
508 }
509 }
510
511 if (connected) {
512 sock = std::move(conn.sock);
513 addr_bind = conn.me;
514 }
515 } else if (use_proxy) {
516 LogPrintLevel(BCLog::PROXY, BCLog::Level::Debug, "Using proxy: %s to connect to %s\n", proxy.ToString(), target_addr.ToStringAddrPort());
517 sock = ConnectThroughProxy(proxy, target_addr.ToStringAddr(), target_addr.GetPort(), proxyConnectionFailed);
518 } else {
519 // no proxy needed (none set for target network)
520 sock = ConnectDirectly(target_addr, conn_type == ConnectionType::MANUAL);
521 }
522 if (!proxyConnectionFailed) {
523 // If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
524 // the proxy, mark this as an attempt.
525 addrman.Attempt(target_addr, fCountFailure);
526 }
527 } else if (pszDest && GetNameProxy(proxy)) {
528 std::string host;
529 uint16_t port{default_port};
530 SplitHostPort(std::string(pszDest), port, host);
531 bool proxyConnectionFailed;
532 sock = ConnectThroughProxy(proxy, host, port, proxyConnectionFailed);
533 }
534 // Check any other resolved address (if any) if we fail to connect
535 if (!sock) {
536 continue;
537 }
538
539 NetPermissionFlags permission_flags = NetPermissionFlags::None;
540 AddWhitelistPermissionFlags(permission_flags, target_addr, vWhitelistedRangeOutgoing);
541
542 // Add node
543 NodeId id = GetNewNodeId();
544 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
545 if (!addr_bind.IsValid()) {
546 addr_bind = GetBindAddress(*sock);
547 }
548 uint64_t network_id = GetDeterministicRandomizer(RANDOMIZER_ID_NETWORKKEY)
549 .Write(target_addr.GetNetClass())
550 .Write(addr_bind.GetAddrBytes())
551 // For outbound connections, the port of the bound address is randomly
552 // assigned by the OS and would therefore not be useful for seeding.
553 .Write(0)
554 .Finalize();
555 CNode* pnode = new CNode(id,
556 std::move(sock),
557 target_addr,
558 CalculateKeyedNetGroup(target_addr),
559 nonce,
560 addr_bind,
561 pszDest ? pszDest : "",
562 conn_type,
563 /*inbound_onion=*/false,
564 network_id,
565 CNodeOptions{
566 .permission_flags = permission_flags,
567 .i2p_sam_session = std::move(i2p_transient_session),
568 .recv_flood_size = nReceiveFloodSize,
569 .use_v2transport = use_v2transport,
570 });
571 pnode->AddRef();
572
573 // We're making a new connection, harvest entropy from the time (and our peer count)
574 RandAddEvent((uint32_t)id);
575
576 return pnode;
577 }
578
579 return nullptr;
580 }
581
582 void CNode::CloseSocketDisconnect()
583 {
584 fDisconnect = true;
585 LOCK(m_sock_mutex);
586 if (m_sock) {
587 LogDebug(BCLog::NET, "Resetting socket for peer=%d%s", GetId(), LogIP(fLogIPs));
588 m_sock.reset();
589
590 TRACEPOINT(net, closed_connection,
591 GetId(),
592 m_addr_name.c_str(),
593 ConnectionTypeAsString().c_str(),
594 ConnectedThroughNetwork(),
595 Ticks<std::chrono::seconds>(m_connected));
596 }
597 m_i2p_sam_session.reset();
598 }
599
600 void CConnman::AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const {
601 for (const auto& subnet : ranges) {
602 if (addr.has_value() && subnet.m_subnet.Match(addr.value())) {
603 NetPermissions::AddFlag(flags, subnet.m_flags);
604 }
605 }
606 if (NetPermissions::HasFlag(flags, NetPermissionFlags::Implicit)) {
607 NetPermissions::ClearFlag(flags, NetPermissionFlags::Implicit);
608 if (whitelist_forcerelay) NetPermissions::AddFlag(flags, NetPermissionFlags::ForceRelay);
609 if (whitelist_relay) NetPermissions::AddFlag(flags, NetPermissionFlags::Relay);
610 NetPermissions::AddFlag(flags, NetPermissionFlags::Mempool);
611 NetPermissions::AddFlag(flags, NetPermissionFlags::NoBan);
612 NetPermissions::AddFlag(flags, NetPermissionFlags::Addr);
613 }
614 }
615
616 CService CNode::GetAddrLocal() const
617 {
618 AssertLockNotHeld(m_addr_local_mutex);
619 LOCK(m_addr_local_mutex);
620 return m_addr_local;
621 }
622
623 void CNode::SetAddrLocal(const CService& addrLocalIn) {
624 AssertLockNotHeld(m_addr_local_mutex);
625 LOCK(m_addr_local_mutex);
626 if (Assume(!m_addr_local.IsValid())) { // Addr local can only be set once during version msg processing
627 m_addr_local = addrLocalIn;
628 }
629 }
630
631 Network CNode::ConnectedThroughNetwork() const
632 {
633 return m_inbound_onion ? NET_ONION : addr.GetNetClass();
634 }
635
636 bool CNode::IsConnectedThroughPrivacyNet() const
637 {
638 return m_inbound_onion || addr.IsPrivacyNet();
639 }
640
641 #undef X
642 #define X(name) stats.name = name
643 void CNode::CopyStats(CNodeStats& stats)
644 {
645 stats.nodeid = this->GetId();
646 X(addr);
647 X(addrBind);
648 stats.m_network = ConnectedThroughNetwork();
649 X(m_last_send);
650 X(m_last_recv);
651 X(m_last_tx_time);
652 X(m_last_block_time);
653 X(m_connected);
654 X(m_addr_name);
655 X(nVersion);
656 {
657 LOCK(m_subver_mutex);
658 X(cleanSubVer);
659 }
660 stats.fInbound = IsInboundConn();
661 X(m_bip152_highbandwidth_to);
662 X(m_bip152_highbandwidth_from);
663 {
664 LOCK(cs_vSend);
665 X(mapSendBytesPerMsgType);
666 X(nSendBytes);
667 }
668 {
669 LOCK(cs_vRecv);
670 X(mapRecvBytesPerMsgType);
671 X(nRecvBytes);
672 Transport::Info info = m_transport->GetInfo();
673 stats.m_transport_type = info.transport_type;
674 if (info.session_id) stats.m_session_id = HexStr(*info.session_id);
675 }
676 X(m_permission_flags);
677 X(m_forced_inbound);
678
679 X(m_last_ping_time);
680 X(m_min_ping_time);
681
682 // Leave string empty if addrLocal invalid (not filled in yet)
683 CService addrLocalUnlocked = GetAddrLocal();
684 stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToStringAddrPort() : "";
685
686 X(m_conn_type);
687
688 X(m_cpu_time);
689 }
690 #undef X
691
692 bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
693 {
694 complete = false;
695 const auto time = GetTime<std::chrono::microseconds>();
696 LOCK(cs_vRecv);
697 m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
698 nRecvBytes += msg_bytes.size();
699 while (msg_bytes.size() > 0) {
700 // absorb network data
701 if (!m_transport->ReceivedBytes(msg_bytes)) {
702 // Serious transport problem, disconnect from the peer.
703 return false;
704 }
705
706 if (m_transport->ReceivedMessageComplete()) {
707 // decompose a transport agnostic CNetMessage from the deserializer
708 bool reject_message{false};
709 CNetMessage msg = m_transport->GetReceivedMessage(time, reject_message);
710 if (reject_message) {
711 // Message deserialization failed. Drop the message but don't disconnect the peer.
712 // store the size of the corrupt message
713 mapRecvBytesPerMsgType.at(NET_MESSAGE_TYPE_OTHER) += msg.m_raw_message_size;
714 continue;
715 }
716
717 // Store received bytes per message type.
718 // To prevent a memory DOS, only allow known message types.
719 auto i = mapRecvBytesPerMsgType.find(msg.m_type);
720 if (i == mapRecvBytesPerMsgType.end()) {
721 i = mapRecvBytesPerMsgType.find(NET_MESSAGE_TYPE_OTHER);
722 }
723 assert(i != mapRecvBytesPerMsgType.end());
724 i->second += msg.m_raw_message_size;
725
726 // push the message to the process queue,
727 vRecvMsg.push_back(std::move(msg));
728
729 complete = true;
730 }
731 }
732
733 return true;
734 }
735
736 std::string CNode::LogIP(bool log_ip) const
737 {
738 return log_ip ? strprintf(" peeraddr=%s", addr.ToStringAddrPort()) : "";
739 }
740
741 std::string CNode::DisconnectMsg(bool log_ip) const
742 {
743 return strprintf("disconnecting peer=%d%s",
744 GetId(),
745 LogIP(log_ip));
746 }
747
748 V1Transport::V1Transport(const NodeId node_id) noexcept
749 : m_magic_bytes{Params().MessageStart()}, m_node_id{node_id}
750 {
751 LOCK(m_recv_mutex);
752 Reset();
753 }
754
755 Transport::Info V1Transport::GetInfo() const noexcept
756 {
757 return {.transport_type = TransportProtocolType::V1, .session_id = {}};
758 }
759
760 int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
761 {
762 AssertLockHeld(m_recv_mutex);
763 // copy data to temporary parsing buffer
764 unsigned int nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
765 unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
766
767 memcpy(&hdrbuf[nHdrPos], msg_bytes.data(), nCopy);
768 nHdrPos += nCopy;
769
770 // if header incomplete, exit
771 if (nHdrPos < CMessageHeader::HEADER_SIZE)
772 return nCopy;
773
774 // deserialize to CMessageHeader
775 try {
776 hdrbuf >> hdr;
777 }
778 catch (const std::exception&) {
779 LogDebug(BCLog::NET, "Header error: Unable to deserialize, peer=%d\n", m_node_id);
780 return -1;
781 }
782
783 // Check start string, network magic
784 if (hdr.pchMessageStart != m_magic_bytes) {
785 LogDebug(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
786 return -1;
787 }
788
789 // reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
790 if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
791 LogDebug(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetMessageType()), hdr.nMessageSize, m_node_id);
792 return -1;
793 }
794
795 // switch state to reading message data
796 in_data = true;
797
798 return nCopy;
799 }
800
801 int V1Transport::readData(Span<const uint8_t> msg_bytes)
802 {
803 AssertLockHeld(m_recv_mutex);
804 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
805 unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
806
807 if (vRecv.size() < nDataPos + nCopy) {
808 // Allocate up to 256 KiB ahead, but never more than the total message size.
809 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
810 }
811
812 hasher.Write(msg_bytes.first(nCopy));
813 memcpy(&vRecv[nDataPos], msg_bytes.data(), nCopy);
814 nDataPos += nCopy;
815
816 return nCopy;
817 }
818
819 const uint256& V1Transport::GetMessageHash() const
820 {
821 AssertLockHeld(m_recv_mutex);
822 assert(CompleteInternal());
823 if (data_hash.IsNull())
824 hasher.Finalize(data_hash);
825 return data_hash;
826 }
827
828 CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time, bool& reject_message)
829 {
830 AssertLockNotHeld(m_recv_mutex);
831 // Initialize out parameter
832 reject_message = false;
833 // decompose a single CNetMessage from the TransportDeserializer
834 LOCK(m_recv_mutex);
835 CNetMessage msg(std::move(vRecv));
836
837 // store message type string, time, and sizes
838 msg.m_type = hdr.GetMessageType();
839 msg.m_time = time;
840 msg.m_message_size = hdr.nMessageSize;
841 msg.m_raw_message_size = hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
842
843 uint256 hash = GetMessageHash();
844
845 // We just received a message off the wire, harvest entropy from the time (and the message checksum)
846 RandAddEvent(ReadLE32(hash.begin()));
847
848 // Check checksum and header message type string
849 if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
850 LogDebug(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n",
851 SanitizeString(msg.m_type), msg.m_message_size,
852 HexStr(Span{hash}.first(CMessageHeader::CHECKSUM_SIZE)),
853 HexStr(hdr.pchChecksum),
854 m_node_id);
855 reject_message = true;
856 } else if (!hdr.IsMessageTypeValid()) {
857 LogDebug(BCLog::NET, "Header error: Invalid message type (%s, %u bytes), peer=%d\n",
858 SanitizeString(hdr.GetMessageType()), msg.m_message_size, m_node_id);
859 reject_message = true;
860 }
861
862 // Always reset the network deserializer (prepare for the next message)
863 Reset();
864 return msg;
865 }
866
867 bool V1Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept
868 {
869 AssertLockNotHeld(m_send_mutex);
870 // Determine whether a new message can be set.
871 LOCK(m_send_mutex);
872 if (m_sending_header || m_bytes_sent < m_message_to_send.data.size()) return false;
873
874 // create dbl-sha256 checksum
875 uint256 hash = Hash(msg.data);
876
877 // create header
878 CMessageHeader hdr(m_magic_bytes, msg.m_type.c_str(), msg.data.size());
879 memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
880
881 // serialize header
882 m_header_to_send.clear();
883 VectorWriter{m_header_to_send, 0, hdr};
884
885 // update state
886 m_message_to_send = std::move(msg);
887 m_sending_header = true;
888 m_bytes_sent = 0;
889 return true;
890 }
891
892 Transport::BytesToSend V1Transport::GetBytesToSend(bool have_next_message) const noexcept
893 {
894 AssertLockNotHeld(m_send_mutex);
895 LOCK(m_send_mutex);
896 if (m_sending_header) {
897 return {Span{m_header_to_send}.subspan(m_bytes_sent),
898 // We have more to send after the header if the message has payload, or if there
899 // is a next message after that.
900 have_next_message || !m_message_to_send.data.empty(),
901 m_message_to_send.m_type
902 };
903 } else {
904 return {Span{m_message_to_send.data}.subspan(m_bytes_sent),
905 // We only have more to send after this message's payload if there is another
906 // message.
907 have_next_message,
908 m_message_to_send.m_type
909 };
910 }
911 }
912
913 void V1Transport::MarkBytesSent(size_t bytes_sent) noexcept
914 {
915 AssertLockNotHeld(m_send_mutex);
916 LOCK(m_send_mutex);
917 m_bytes_sent += bytes_sent;
918 if (m_sending_header && m_bytes_sent == m_header_to_send.size()) {
919 // We're done sending a message's header. Switch to sending its data bytes.
920 m_sending_header = false;
921 m_bytes_sent = 0;
922 } else if (!m_sending_header && m_bytes_sent == m_message_to_send.data.size()) {
923 // We're done sending a message's data. Wipe the data vector to reduce memory consumption.
924 ClearShrink(m_message_to_send.data);
925 m_bytes_sent = 0;
926 }
927 }
928
929 size_t V1Transport::GetSendMemoryUsage() const noexcept
930 {
931 AssertLockNotHeld(m_send_mutex);
932 LOCK(m_send_mutex);
933 // Don't count sending-side fields besides m_message_to_send, as they're all small and bounded.
934 return m_message_to_send.GetMemoryUsage();
935 }
936
937 namespace {
938
939 /** List of short messages as defined in BIP324, in order.
940 *
941 * Only message types that are actually implemented in this codebase need to be listed, as other
942 * messages get ignored anyway - whether we know how to decode them or not.
943 */
944 const std::array<std::string, 33> V2_MESSAGE_IDS = {
945 "", // 12 bytes follow encoding the message type like in V1
946 NetMsgType::ADDR,
947 NetMsgType::BLOCK,
948 NetMsgType::BLOCKTXN,
949 NetMsgType::CMPCTBLOCK,
950 NetMsgType::FEEFILTER,
951 NetMsgType::FILTERADD,
952 NetMsgType::FILTERCLEAR,
953 NetMsgType::FILTERLOAD,
954 NetMsgType::GETBLOCKS,
955 NetMsgType::GETBLOCKTXN,
956 NetMsgType::GETDATA,
957 NetMsgType::GETHEADERS,
958 NetMsgType::HEADERS,
959 NetMsgType::INV,
960 NetMsgType::MEMPOOL,
961 NetMsgType::MERKLEBLOCK,
962 NetMsgType::NOTFOUND,
963 NetMsgType::PING,
964 NetMsgType::PONG,
965 NetMsgType::SENDCMPCT,
966 NetMsgType::TX,
967 NetMsgType::GETCFILTERS,
968 NetMsgType::CFILTER,
969 NetMsgType::GETCFHEADERS,
970 NetMsgType::CFHEADERS,
971 NetMsgType::GETCFCHECKPT,
972 NetMsgType::CFCHECKPT,
973 NetMsgType::ADDRV2,
974 // Unimplemented message types that are assigned in BIP324:
975 "",
976 "",
977 "",
978 ""
979 };
980
981 class V2MessageMap
982 {
983 std::unordered_map<std::string, uint8_t> m_map;
984
985 public:
986 V2MessageMap() noexcept
987 {
988 for (size_t i = 1; i < std::size(V2_MESSAGE_IDS); ++i) {
989 m_map.emplace(V2_MESSAGE_IDS[i], i);
990 }
991 }
992
993 std::optional<uint8_t> operator()(const std::string& message_name) const noexcept
994 {
995 auto it = m_map.find(message_name);
996 if (it == m_map.end()) return std::nullopt;
997 return it->second;
998 }
999 };
1000
1001 const V2MessageMap V2_MESSAGE_MAP;
1002
1003 std::vector<uint8_t> GenerateRandomGarbage() noexcept
1004 {
1005 std::vector<uint8_t> ret;
1006 FastRandomContext rng;
1007 ret.resize(rng.randrange(V2Transport::MAX_GARBAGE_LEN + 1));
1008 rng.fillrand(MakeWritableByteSpan(ret));
1009 return ret;
1010 }
1011
1012 } // namespace
1013
1014 void V2Transport::StartSendingHandshake() noexcept
1015 {
1016 AssertLockHeld(m_send_mutex);
1017 Assume(m_send_state == SendState::AWAITING_KEY);
1018 Assume(m_send_buffer.empty());
1019 // Initialize the send buffer with ellswift pubkey + provided garbage.
1020 m_send_buffer.resize(EllSwiftPubKey::size() + m_send_garbage.size());
1021 std::copy(std::begin(m_cipher.GetOurPubKey()), std::end(m_cipher.GetOurPubKey()), MakeWritableByteSpan(m_send_buffer).begin());
1022 std::copy(m_send_garbage.begin(), m_send_garbage.end(), m_send_buffer.begin() + EllSwiftPubKey::size());
1023 // We cannot wipe m_send_garbage as it will still be used as AAD later in the handshake.
1024 }
1025
1026 V2Transport::V2Transport(NodeId nodeid, bool initiating, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept
1027 : m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
1028 m_v1_fallback{nodeid},
1029 m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
1030 m_send_garbage{std::move(garbage)},
1031 m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
1032 {
1033 Assume(m_send_garbage.size() <= MAX_GARBAGE_LEN);
1034 // Start sending immediately if we're the initiator of the connection.
1035 if (initiating) {
1036 LOCK(m_send_mutex);
1037 StartSendingHandshake();
1038 }
1039 }
1040
1041 V2Transport::V2Transport(NodeId nodeid, bool initiating) noexcept
1042 : V2Transport{nodeid, initiating, GenerateRandomKey(),
1043 MakeByteSpan(GetRandHash()), GenerateRandomGarbage()} {}
1044
1045 void V2Transport::SetReceiveState(RecvState recv_state) noexcept
1046 {
1047 AssertLockHeld(m_recv_mutex);
1048 // Enforce allowed state transitions.
1049 switch (m_recv_state) {
1050 case RecvState::KEY_MAYBE_V1:
1051 Assume(recv_state == RecvState::KEY || recv_state == RecvState::V1);
1052 break;
1053 case RecvState::KEY:
1054 Assume(recv_state == RecvState::GARB_GARBTERM);
1055 break;
1056 case RecvState::GARB_GARBTERM:
1057 Assume(recv_state == RecvState::VERSION);
1058 break;
1059 case RecvState::VERSION:
1060 Assume(recv_state == RecvState::APP);
1061 break;
1062 case RecvState::APP:
1063 Assume(recv_state == RecvState::APP_READY);
1064 break;
1065 case RecvState::APP_READY:
1066 Assume(recv_state == RecvState::APP);
1067 break;
1068 case RecvState::V1:
1069 Assume(false); // V1 state cannot be left
1070 break;
1071 }
1072 // Change state.
1073 m_recv_state = recv_state;
1074 }
1075
1076 void V2Transport::SetSendState(SendState send_state) noexcept
1077 {
1078 AssertLockHeld(m_send_mutex);
1079 // Enforce allowed state transitions.
1080 switch (m_send_state) {
1081 case SendState::MAYBE_V1:
1082 Assume(send_state == SendState::V1 || send_state == SendState::AWAITING_KEY);
1083 break;
1084 case SendState::AWAITING_KEY:
1085 Assume(send_state == SendState::READY);
1086 break;
1087 case SendState::READY:
1088 case SendState::V1:
1089 Assume(false); // Final states
1090 break;
1091 }
1092 // Change state.
1093 m_send_state = send_state;
1094 }
1095
1096 bool V2Transport::ReceivedMessageComplete() const noexcept
1097 {
1098 AssertLockNotHeld(m_recv_mutex);
1099 LOCK(m_recv_mutex);
1100 if (m_recv_state == RecvState::V1) return m_v1_fallback.ReceivedMessageComplete();
1101
1102 return m_recv_state == RecvState::APP_READY;
1103 }
1104
1105 void V2Transport::ProcessReceivedMaybeV1Bytes() noexcept
1106 {
1107 AssertLockHeld(m_recv_mutex);
1108 AssertLockNotHeld(m_send_mutex);
1109 Assume(m_recv_state == RecvState::KEY_MAYBE_V1);
1110 // We still have to determine if this is a v1 or v2 connection. The bytes being received could
1111 // be the beginning of either a v1 packet (network magic + "version\x00\x00\x00\x00\x00"), or
1112 // of a v2 public key. BIP324 specifies that a mismatch with this 16-byte string should trigger
1113 // sending of the key.
1114 std::array<uint8_t, V1_PREFIX_LEN> v1_prefix = {0, 0, 0, 0, 'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0};
1115 std::copy(std::begin(Params().MessageStart()), std::end(Params().MessageStart()), v1_prefix.begin());
1116 Assume(m_recv_buffer.size() <= v1_prefix.size());
1117 if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.end(), v1_prefix.begin())) {
1118 // Mismatch with v1 prefix, so we can assume a v2 connection.
1119 SetReceiveState(RecvState::KEY); // Convert to KEY state, leaving received bytes around.
1120 // Transition the sender to AWAITING_KEY state and start sending.
1121 LOCK(m_send_mutex);
1122 SetSendState(SendState::AWAITING_KEY);
1123 StartSendingHandshake();
1124 } else if (m_recv_buffer.size() == v1_prefix.size()) {
1125 // Full match with the v1 prefix, so fall back to v1 behavior.
1126 LOCK(m_send_mutex);
1127 Span<const uint8_t> feedback{m_recv_buffer};
1128 // Feed already received bytes to v1 transport. It should always accept these, because it's
1129 // less than the size of a v1 header, and these are the first bytes fed to m_v1_fallback.
1130 bool ret = m_v1_fallback.ReceivedBytes(feedback);
1131 Assume(feedback.empty());
1132 Assume(ret);
1133 SetReceiveState(RecvState::V1);
1134 SetSendState(SendState::V1);
1135 // Reset v2 transport buffers to save memory.
1136 ClearShrink(m_recv_buffer);
1137 ClearShrink(m_send_buffer);
1138 } else {
1139 // We have not received enough to distinguish v1 from v2 yet. Wait until more bytes come.
1140 }
1141 }
1142
1143 bool V2Transport::ProcessReceivedKeyBytes() noexcept
1144 {
1145 AssertLockHeld(m_recv_mutex);
1146 AssertLockNotHeld(m_send_mutex);
1147 Assume(m_recv_state == RecvState::KEY);
1148 Assume(m_recv_buffer.size() <= EllSwiftPubKey::size());
1149
1150 // As a special exception, if bytes 4-16 of the key on a responder connection match the
1151 // corresponding bytes of a V1 version message, but bytes 0-4 don't match the network magic
1152 // (if they did, we'd have switched to V1 state already), assume this is a peer from
1153 // another network, and disconnect them. They will almost certainly disconnect us too when
1154 // they receive our uniformly random key and garbage, but detecting this case specially
1155 // means we can log it.
1156 static constexpr std::array<uint8_t, 12> MATCH = {'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0};
1157 static constexpr size_t OFFSET = std::tuple_size_v<MessageStartChars>;
1158 if (!m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) {
1159 if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) {
1160 LogDebug(BCLog::NET, "V2 transport error: V1 peer with wrong MessageStart %s\n",
1161 HexStr(Span(m_recv_buffer).first(OFFSET)));
1162 return false;
1163 }
1164 }
1165
1166 if (m_recv_buffer.size() == EllSwiftPubKey::size()) {
1167 // Other side's key has been fully received, and can now be Diffie-Hellman combined with
1168 // our key to initialize the encryption ciphers.
1169
1170 // Initialize the ciphers.
1171 EllSwiftPubKey ellswift(MakeByteSpan(m_recv_buffer));
1172 LOCK(m_send_mutex);
1173 m_cipher.Initialize(ellswift, m_initiating);
1174
1175 // Switch receiver state to GARB_GARBTERM.
1176 SetReceiveState(RecvState::GARB_GARBTERM);
1177 m_recv_buffer.clear();
1178
1179 // Switch sender state to READY.
1180 SetSendState(SendState::READY);
1181
1182 // Append the garbage terminator to the send buffer.
1183 m_send_buffer.resize(m_send_buffer.size() + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
1184 std::copy(m_cipher.GetSendGarbageTerminator().begin(),
1185 m_cipher.GetSendGarbageTerminator().end(),
1186 MakeWritableByteSpan(m_send_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN).begin());
1187
1188 // Construct version packet in the send buffer, with the sent garbage data as AAD.
1189 m_send_buffer.resize(m_send_buffer.size() + BIP324Cipher::EXPANSION + VERSION_CONTENTS.size());
1190 m_cipher.Encrypt(
1191 /*contents=*/VERSION_CONTENTS,
1192 /*aad=*/MakeByteSpan(m_send_garbage),
1193 /*ignore=*/false,
1194 /*output=*/MakeWritableByteSpan(m_send_buffer).last(BIP324Cipher::EXPANSION + VERSION_CONTENTS.size()));
1195 // We no longer need the garbage.
1196 ClearShrink(m_send_garbage);
1197 } else {
1198 // We still have to receive more key bytes.
1199 }
1200 return true;
1201 }
1202
1203 bool V2Transport::ProcessReceivedGarbageBytes() noexcept
1204 {
1205 AssertLockHeld(m_recv_mutex);
1206 Assume(m_recv_state == RecvState::GARB_GARBTERM);
1207 Assume(m_recv_buffer.size() <= MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
1208 if (m_recv_buffer.size() >= BIP324Cipher::GARBAGE_TERMINATOR_LEN) {
1209 if (std::ranges::equal(MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN), m_cipher.GetReceiveGarbageTerminator())) {
1210 // Garbage terminator received. Store garbage to authenticate it as AAD later.
1211 m_recv_aad = std::move(m_recv_buffer);
1212 m_recv_aad.resize(m_recv_aad.size() - BIP324Cipher::GARBAGE_TERMINATOR_LEN);
1213 m_recv_buffer.clear();
1214 SetReceiveState(RecvState::VERSION);
1215 } else if (m_recv_buffer.size() == MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN) {
1216 // We've reached the maximum length for garbage + garbage terminator, and the
1217 // terminator still does not match. Abort.
1218 LogDebug(BCLog::NET, "V2 transport error: missing garbage terminator, peer=%d\n", m_nodeid);
1219 return false;
1220 } else {
1221 // We still need to receive more garbage and/or garbage terminator bytes.
1222 }
1223 } else {
1224 // We have less than GARBAGE_TERMINATOR_LEN (16) bytes, so we certainly need to receive
1225 // more first.
1226 }
1227 return true;
1228 }
1229
1230 bool V2Transport::ProcessReceivedPacketBytes() noexcept
1231 {
1232 AssertLockHeld(m_recv_mutex);
1233 Assume(m_recv_state == RecvState::VERSION || m_recv_state == RecvState::APP);
1234
1235 // The maximum permitted contents length for a packet, consisting of:
1236 // - 0x00 byte: indicating long message type encoding
1237 // - 12 bytes of message type
1238 // - payload
1239 static constexpr size_t MAX_CONTENTS_LEN =
1240 1 + CMessageHeader::MESSAGE_TYPE_SIZE +
1241 std::min<size_t>(MAX_SIZE, MAX_PROTOCOL_MESSAGE_LENGTH);
1242
1243 if (m_recv_buffer.size() == BIP324Cipher::LENGTH_LEN) {
1244 // Length descriptor received.
1245 m_recv_len = m_cipher.DecryptLength(MakeByteSpan(m_recv_buffer));
1246 if (m_recv_len > MAX_CONTENTS_LEN) {
1247 LogDebug(BCLog::NET, "V2 transport error: packet too large (%u bytes), peer=%d\n", m_recv_len, m_nodeid);
1248 return false;
1249 }
1250 } else if (m_recv_buffer.size() > BIP324Cipher::LENGTH_LEN && m_recv_buffer.size() == m_recv_len + BIP324Cipher::EXPANSION) {
1251 // Ciphertext received, decrypt it into m_recv_decode_buffer.
1252 // Note that it is impossible to reach this branch without hitting the branch above first,
1253 // as GetMaxBytesToProcess only allows up to LENGTH_LEN into the buffer before that point.
1254 m_recv_decode_buffer.resize(m_recv_len);
1255 bool ignore{false};
1256 bool ret = m_cipher.Decrypt(
1257 /*input=*/MakeByteSpan(m_recv_buffer).subspan(BIP324Cipher::LENGTH_LEN),
1258 /*aad=*/MakeByteSpan(m_recv_aad),
1259 /*ignore=*/ignore,
1260 /*contents=*/MakeWritableByteSpan(m_recv_decode_buffer));
1261 if (!ret) {
1262 LogDebug(BCLog::NET, "V2 transport error: packet decryption failure (%u bytes), peer=%d\n", m_recv_len, m_nodeid);
1263 return false;
1264 }
1265 // We have decrypted a valid packet with the AAD we expected, so clear the expected AAD.
1266 ClearShrink(m_recv_aad);
1267 // Feed the last 4 bytes of the Poly1305 authentication tag (and its timing) into our RNG.
1268 RandAddEvent(ReadLE32(m_recv_buffer.data() + m_recv_buffer.size() - 4));
1269
1270 // At this point we have a valid packet decrypted into m_recv_decode_buffer. If it's not a
1271 // decoy, which we simply ignore, use the current state to decide what to do with it.
1272 if (!ignore) {
1273 switch (m_recv_state) {
1274 case RecvState::VERSION:
1275 // Version message received; transition to application phase. The contents is
1276 // ignored, but can be used for future extensions.
1277 SetReceiveState(RecvState::APP);
1278 break;
1279 case RecvState::APP:
1280 // Application message decrypted correctly. It can be extracted using GetMessage().
1281 SetReceiveState(RecvState::APP_READY);
1282 break;
1283 default:
1284 // Any other state is invalid (this function should not have been called).
1285 Assume(false);
1286 }
1287 }
1288 // Wipe the receive buffer where the next packet will be received into.
1289 ClearShrink(m_recv_buffer);
1290 // In all but APP_READY state, we can wipe the decoded contents.
1291 if (m_recv_state != RecvState::APP_READY) ClearShrink(m_recv_decode_buffer);
1292 } else {
1293 // We either have less than 3 bytes, so we don't know the packet's length yet, or more
1294 // than 3 bytes but less than the packet's full ciphertext. Wait until those arrive.
1295 }
1296 return true;
1297 }
1298
1299 size_t V2Transport::GetMaxBytesToProcess() noexcept
1300 {
1301 AssertLockHeld(m_recv_mutex);
1302 switch (m_recv_state) {
1303 case RecvState::KEY_MAYBE_V1:
1304 // During the KEY_MAYBE_V1 state we do not allow more than the length of v1 prefix into the
1305 // receive buffer.
1306 Assume(m_recv_buffer.size() <= V1_PREFIX_LEN);
1307 // As long as we're not sure if this is a v1 or v2 connection, don't receive more than what
1308 // is strictly necessary to distinguish the two (16 bytes). If we permitted more than
1309 // the v1 header size (24 bytes), we may not be able to feed the already-received bytes
1310 // back into the m_v1_fallback V1 transport.
1311 return V1_PREFIX_LEN - m_recv_buffer.size();
1312 case RecvState::KEY:
1313 // During the KEY state, we only allow the 64-byte key into the receive buffer.
1314 Assume(m_recv_buffer.size() <= EllSwiftPubKey::size());
1315 // As long as we have not received the other side's public key, don't receive more than
1316 // that (64 bytes), as garbage follows, and locating the garbage terminator requires the
1317 // key exchange first.
1318 return EllSwiftPubKey::size() - m_recv_buffer.size();
1319 case RecvState::GARB_GARBTERM:
1320 // Process garbage bytes one by one (because terminator may appear anywhere).
1321 return 1;
1322 case RecvState::VERSION:
1323 case RecvState::APP:
1324 // These three states all involve decoding a packet. Process the length descriptor first,
1325 // so that we know where the current packet ends (and we don't process bytes from the next
1326 // packet or decoy yet). Then, process the ciphertext bytes of the current packet.
1327 if (m_recv_buffer.size() < BIP324Cipher::LENGTH_LEN) {
1328 return BIP324Cipher::LENGTH_LEN - m_recv_buffer.size();
1329 } else {
1330 // Note that BIP324Cipher::EXPANSION is the total difference between contents size
1331 // and encoded packet size, which includes the 3 bytes due to the packet length.
1332 // When transitioning from receiving the packet length to receiving its ciphertext,
1333 // the encrypted packet length is left in the receive buffer.
1334 return BIP324Cipher::EXPANSION + m_recv_len - m_recv_buffer.size();
1335 }
1336 case RecvState::APP_READY:
1337 // No bytes can be processed until GetMessage() is called.
1338 return 0;
1339 case RecvState::V1:
1340 // Not allowed (must be dealt with by the caller).
1341 Assume(false);
1342 return 0;
1343 }
1344 Assume(false); // unreachable
1345 return 0;
1346 }
1347
1348 bool V2Transport::ReceivedBytes(Span<const uint8_t>& msg_bytes) noexcept
1349 {
1350 AssertLockNotHeld(m_recv_mutex);
1351 /** How many bytes to allocate in the receive buffer at most above what is received so far. */
1352 static constexpr size_t MAX_RESERVE_AHEAD = 256 * 1024;
1353
1354 LOCK(m_recv_mutex);
1355 if (m_recv_state == RecvState::V1) return m_v1_fallback.ReceivedBytes(msg_bytes);
1356
1357 // Process the provided bytes in msg_bytes in a loop. In each iteration a nonzero number of
1358 // bytes (decided by GetMaxBytesToProcess) are taken from the beginning om msg_bytes, and
1359 // appended to m_recv_buffer. Then, depending on the receiver state, one of the
1360 // ProcessReceived*Bytes functions is called to process the bytes in that buffer.
1361 while (!msg_bytes.empty()) {
1362 // Decide how many bytes to copy from msg_bytes to m_recv_buffer.
1363 size_t max_read = GetMaxBytesToProcess();
1364
1365 // Reserve space in the buffer if there is not enough.
1366 if (m_recv_buffer.size() + std::min(msg_bytes.size(), max_read) > m_recv_buffer.capacity()) {
1367 switch (m_recv_state) {
1368 case RecvState::KEY_MAYBE_V1:
1369 case RecvState::KEY:
1370 case RecvState::GARB_GARBTERM:
1371 // During the initial states (key/garbage), allocate once to fit the maximum (4111
1372 // bytes).
1373 m_recv_buffer.reserve(MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
1374 break;
1375 case RecvState::VERSION:
1376 case RecvState::APP: {
1377 // During states where a packet is being received, as much as is expected but never
1378 // more than MAX_RESERVE_AHEAD bytes in addition to what is received so far.
1379 // This means attackers that want to cause us to waste allocated memory are limited
1380 // to MAX_RESERVE_AHEAD above the largest allowed message contents size, and to
1381 // MAX_RESERVE_AHEAD more than they've actually sent us.
1382 size_t alloc_add = std::min(max_read, msg_bytes.size() + MAX_RESERVE_AHEAD);
1383 m_recv_buffer.reserve(m_recv_buffer.size() + alloc_add);
1384 break;
1385 }
1386 case RecvState::APP_READY:
1387 // The buffer is empty in this state.
1388 Assume(m_recv_buffer.empty());
1389 break;
1390 case RecvState::V1:
1391 // Should have bailed out above.
1392 Assume(false);
1393 break;
1394 }
1395 }
1396
1397 // Can't read more than provided input.
1398 max_read = std::min(msg_bytes.size(), max_read);
1399 // Copy data to buffer.
1400 m_recv_buffer.insert(m_recv_buffer.end(), UCharCast(msg_bytes.data()), UCharCast(msg_bytes.data() + max_read));
1401 msg_bytes = msg_bytes.subspan(max_read);
1402
1403 // Process data in the buffer.
1404 switch (m_recv_state) {
1405 case RecvState::KEY_MAYBE_V1:
1406 ProcessReceivedMaybeV1Bytes();
1407 if (m_recv_state == RecvState::V1) return true;
1408 break;
1409
1410 case RecvState::KEY:
1411 if (!ProcessReceivedKeyBytes()) return false;
1412 break;
1413
1414 case RecvState::GARB_GARBTERM:
1415 if (!ProcessReceivedGarbageBytes()) return false;
1416 break;
1417
1418 case RecvState::VERSION:
1419 case RecvState::APP:
1420 if (!ProcessReceivedPacketBytes()) return false;
1421 break;
1422
1423 case RecvState::APP_READY:
1424 return true;
1425
1426 case RecvState::V1:
1427 // We should have bailed out before.
1428 Assume(false);
1429 break;
1430 }
1431 // Make sure we have made progress before continuing.
1432 Assume(max_read > 0);
1433 }
1434
1435 return true;
1436 }
1437
1438 std::optional<std::string> V2Transport::GetMessageType(Span<const uint8_t>& contents) noexcept
1439 {
1440 if (contents.size() == 0) return std::nullopt; // Empty contents
1441 uint8_t first_byte = contents[0];
1442 contents = contents.subspan(1); // Strip first byte.
1443
1444 if (first_byte != 0) {
1445 // Short (1 byte) encoding.
1446 if (first_byte < std::size(V2_MESSAGE_IDS)) {
1447 // Valid short message id.
1448 return V2_MESSAGE_IDS[first_byte];
1449 } else {
1450 // Unknown short message id.
1451 return std::nullopt;
1452 }
1453 }
1454
1455 if (contents.size() < CMessageHeader::MESSAGE_TYPE_SIZE) {
1456 return std::nullopt; // Long encoding needs 12 message type bytes.
1457 }
1458
1459 size_t msg_type_len{0};
1460 while (msg_type_len < CMessageHeader::MESSAGE_TYPE_SIZE && contents[msg_type_len] != 0) {
1461 // Verify that message type bytes before the first 0x00 are in range.
1462 if (contents[msg_type_len] < ' ' || contents[msg_type_len] > 0x7F) {
1463 return {};
1464 }
1465 ++msg_type_len;
1466 }
1467 std::string ret{reinterpret_cast<const char*>(contents.data()), msg_type_len};
1468 while (msg_type_len < CMessageHeader::MESSAGE_TYPE_SIZE) {
1469 // Verify that message type bytes after the first 0x00 are also 0x00.
1470 if (contents[msg_type_len] != 0) return {};
1471 ++msg_type_len;
1472 }
1473 // Strip message type bytes of contents.
1474 contents = contents.subspan(CMessageHeader::MESSAGE_TYPE_SIZE);
1475 return ret;
1476 }
1477
1478 CNetMessage V2Transport::GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) noexcept
1479 {
1480 AssertLockNotHeld(m_recv_mutex);
1481 LOCK(m_recv_mutex);
1482 if (m_recv_state == RecvState::V1) return m_v1_fallback.GetReceivedMessage(time, reject_message);
1483
1484 Assume(m_recv_state == RecvState::APP_READY);
1485 Span<const uint8_t> contents{m_recv_decode_buffer};
1486 auto msg_type = GetMessageType(contents);
1487 CNetMessage msg{DataStream{}};
1488 // Note that BIP324Cipher::EXPANSION also includes the length descriptor size.
1489 msg.m_raw_message_size = m_recv_decode_buffer.size() + BIP324Cipher::EXPANSION;
1490 if (msg_type) {
1491 reject_message = false;
1492 msg.m_type = std::move(*msg_type);
1493 msg.m_time = time;
1494 msg.m_message_size = contents.size();
1495 msg.m_recv.resize(contents.size());
1496 std::copy(contents.begin(), contents.end(), UCharCast(msg.m_recv.data()));
1497 } else {
1498 LogDebug(BCLog::NET, "V2 transport error: invalid message type (%u bytes contents), peer=%d\n", m_recv_decode_buffer.size(), m_nodeid);
1499 reject_message = true;
1500 }
1501 ClearShrink(m_recv_decode_buffer);
1502 SetReceiveState(RecvState::APP);
1503
1504 return msg;
1505 }
1506
1507 bool V2Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept
1508 {
1509 AssertLockNotHeld(m_send_mutex);
1510 LOCK(m_send_mutex);
1511 if (m_send_state == SendState::V1) return m_v1_fallback.SetMessageToSend(msg);
1512 // We only allow adding a new message to be sent when in the READY state (so the packet cipher
1513 // is available) and the send buffer is empty. This limits the number of messages in the send
1514 // buffer to just one, and leaves the responsibility for queueing them up to the caller.
1515 if (!(m_send_state == SendState::READY && m_send_buffer.empty())) return false;
1516 // Construct contents (encoding message type + payload).
1517 std::vector<uint8_t> contents;
1518 auto short_message_id = V2_MESSAGE_MAP(msg.m_type);
1519 if (short_message_id) {
1520 contents.resize(1 + msg.data.size());
1521 contents[0] = *short_message_id;
1522 std::copy(msg.data.begin(), msg.data.end(), contents.begin() + 1);
1523 } else {
1524 // Initialize with zeroes, and then write the message type string starting at offset 1.
1525 // This means contents[0] and the unused positions in contents[1..13] remain 0x00.
1526 contents.resize(1 + CMessageHeader::MESSAGE_TYPE_SIZE + msg.data.size(), 0);
1527 std::copy(msg.m_type.begin(), msg.m_type.end(), contents.data() + 1);
1528 std::copy(msg.data.begin(), msg.data.end(), contents.begin() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
1529 }
1530 // Construct ciphertext in send buffer.
1531 m_send_buffer.resize(contents.size() + BIP324Cipher::EXPANSION);
1532 m_cipher.Encrypt(MakeByteSpan(contents), {}, false, MakeWritableByteSpan(m_send_buffer));
1533 m_send_type = msg.m_type;
1534 // Release memory
1535 ClearShrink(msg.data);
1536 return true;
1537 }
1538
1539 Transport::BytesToSend V2Transport::GetBytesToSend(bool have_next_message) const noexcept
1540 {
1541 AssertLockNotHeld(m_send_mutex);
1542 LOCK(m_send_mutex);
1543 if (m_send_state == SendState::V1) return m_v1_fallback.GetBytesToSend(have_next_message);
1544
1545 if (m_send_state == SendState::MAYBE_V1) Assume(m_send_buffer.empty());
1546 Assume(m_send_pos <= m_send_buffer.size());
1547 return {
1548 Span{m_send_buffer}.subspan(m_send_pos),
1549 // We only have more to send after the current m_send_buffer if there is a (next)
1550 // message to be sent, and we're capable of sending packets. */
1551 have_next_message && m_send_state == SendState::READY,
1552 m_send_type
1553 };
1554 }
1555
1556 void V2Transport::MarkBytesSent(size_t bytes_sent) noexcept
1557 {
1558 AssertLockNotHeld(m_send_mutex);
1559 LOCK(m_send_mutex);
1560 if (m_send_state == SendState::V1) return m_v1_fallback.MarkBytesSent(bytes_sent);
1561
1562 if (m_send_state == SendState::AWAITING_KEY && m_send_pos == 0 && bytes_sent > 0) {
1563 LogDebug(BCLog::NET, "start sending v2 handshake to peer=%d\n", m_nodeid);
1564 }
1565
1566 m_send_pos += bytes_sent;
1567 Assume(m_send_pos <= m_send_buffer.size());
1568 if (m_send_pos >= CMessageHeader::HEADER_SIZE) {
1569 m_sent_v1_header_worth = true;
1570 }
1571 // Wipe the buffer when everything is sent.
1572 if (m_send_pos == m_send_buffer.size()) {
1573 m_send_pos = 0;
1574 ClearShrink(m_send_buffer);
1575 }
1576 }
1577
1578 bool V2Transport::ShouldReconnectV1() const noexcept
1579 {
1580 AssertLockNotHeld(m_send_mutex);
1581 AssertLockNotHeld(m_recv_mutex);
1582 // Only outgoing connections need reconnection.
1583 if (!m_initiating) return false;
1584
1585 LOCK(m_recv_mutex);
1586 // We only reconnect in the very first state and when the receive buffer is empty. Together
1587 // these conditions imply nothing has been received so far.
1588 if (m_recv_state != RecvState::KEY) return false;
1589 if (!m_recv_buffer.empty()) return false;
1590 // Check if we've sent enough for the other side to disconnect us (if it was V1).
1591 LOCK(m_send_mutex);
1592 return m_sent_v1_header_worth;
1593 }
1594
1595 size_t V2Transport::GetSendMemoryUsage() const noexcept
1596 {
1597 AssertLockNotHeld(m_send_mutex);
1598 LOCK(m_send_mutex);
1599 if (m_send_state == SendState::V1) return m_v1_fallback.GetSendMemoryUsage();
1600
1601 return sizeof(m_send_buffer) + memusage::DynamicUsage(m_send_buffer);
1602 }
1603
1604 Transport::Info V2Transport::GetInfo() const noexcept
1605 {
1606 AssertLockNotHeld(m_recv_mutex);
1607 LOCK(m_recv_mutex);
1608 if (m_recv_state == RecvState::V1) return m_v1_fallback.GetInfo();
1609
1610 Transport::Info info;
1611
1612 // Do not report v2 and session ID until the version packet has been received
1613 // and verified (confirming that the other side very likely has the same keys as us).
1614 if (m_recv_state != RecvState::KEY_MAYBE_V1 && m_recv_state != RecvState::KEY &&
1615 m_recv_state != RecvState::GARB_GARBTERM && m_recv_state != RecvState::VERSION) {
1616 info.transport_type = TransportProtocolType::V2;
1617 info.session_id = uint256(MakeUCharSpan(m_cipher.GetSessionID()));
1618 } else {
1619 info.transport_type = TransportProtocolType::DETECTING;
1620 }
1621
1622 return info;
1623 }
1624
1625 std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
1626 {
1627 auto it = node.vSendMsg.begin();
1628 size_t nSentSize = 0;
1629 bool data_left{false}; //!< second return value (whether unsent data remains)
1630 std::optional<bool> expected_more;
1631
1632 while (true) {
1633 if (it != node.vSendMsg.end()) {
1634 // If possible, move one message from the send queue to the transport. This fails when
1635 // there is an existing message still being sent, or (for v2 transports) when the
1636 // handshake has not yet completed.
1637 size_t memusage = it->GetMemoryUsage();
1638 if (node.m_transport->SetMessageToSend(*it)) {
1639 // Update memory usage of send buffer (as *it will be deleted).
1640 node.m_send_memusage -= memusage;
1641 ++it;
1642 }
1643 }
1644 const auto& [data, more, msg_type] = node.m_transport->GetBytesToSend(it != node.vSendMsg.end());
1645 // We rely on the 'more' value returned by GetBytesToSend to correctly predict whether more
1646 // bytes are still to be sent, to correctly set the MSG_MORE flag. As a sanity check,
1647 // verify that the previously returned 'more' was correct.
1648 if (expected_more.has_value()) Assume(!data.empty() == *expected_more);
1649 expected_more = more;
1650 data_left = !data.empty(); // will be overwritten on next loop if all of data gets sent
1651 int nBytes = 0;
1652 if (!data.empty()) {
1653 LOCK(node.m_sock_mutex);
1654 // There is no socket in case we've already disconnected, or in test cases without
1655 // real connections. In these cases, we bail out immediately and just leave things
1656 // in the send queue and transport.
1657 if (!node.m_sock) {
1658 break;
1659 }
1660 int flags = MSG_NOSIGNAL | MSG_DONTWAIT;
1661 #ifdef MSG_MORE
1662 if (more) {
1663 flags |= MSG_MORE;
1664 }
1665 #endif
1666 nBytes = node.m_sock->Send(reinterpret_cast<const char*>(data.data()), data.size(), flags);
1667 }
1668 if (nBytes > 0) {
1669 node.m_last_send = GetTime<std::chrono::seconds>();
1670 node.nSendBytes += nBytes;
1671 // Notify transport that bytes have been processed.
1672 node.m_transport->MarkBytesSent(nBytes);
1673 // Update statistics per message type.
1674 if (!msg_type.empty()) { // don't report v2 handshake bytes for now
1675 node.AccountForSentBytes(msg_type, nBytes);
1676 }
1677 nSentSize += nBytes;
1678 if ((size_t)nBytes != data.size()) {
1679 // could not send full message; stop sending more
1680 break;
1681 }
1682 } else {
1683 if (nBytes < 0) {
1684 // error
1685 int nErr = WSAGetLastError();
1686 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
1687 LogDebug(BCLog::NET, "socket send error, %s: %s\n", node.DisconnectMsg(fLogIPs), NetworkErrorString(nErr));
1688 node.CloseSocketDisconnect();
1689 }
1690 }
1691 break;
1692 }
1693 }
1694
1695 node.fPauseSend = node.m_send_memusage + node.m_transport->GetSendMemoryUsage() > nSendBufferMaxSize;
1696
1697 if (it == node.vSendMsg.end()) {
1698 assert(node.m_send_memusage == 0);
1699 }
1700 node.vSendMsg.erase(node.vSendMsg.begin(), it);
1701 return {nSentSize, data_left};
1702 }
1703
1704 /** Try to find a connection to evict when the node is full.
1705 * Extreme care must be taken to avoid opening the node to attacker
1706 * triggered network partitioning.
1707 * The strategy used here is to protect a small number of peers
1708 * for each of several distinct characteristics which are difficult
1709 * to forge. In order to partition a node the attacker must be
1710 * simultaneously better at all of them than honest peers.
1711 */
1712 bool CConnman::AttemptToEvictConnection(bool force)
1713 {
1714 std::vector<NodeEvictionCandidate> vEvictionCandidates;
1715 {
1716
1717 LOCK(m_nodes_mutex);
1718 for (const CNode* node : m_nodes) {
1719 if (node->fDisconnect)
1720 continue;
1721 NodeEvictionCandidate candidate{
1722 .id = node->GetId(),
1723 .m_connected = node->m_connected,
1724 .m_min_ping_time = node->m_min_ping_time,
1725 .m_last_block_time = node->m_last_block_time,
1726 .m_last_tx_time = node->m_last_tx_time,
1727 .fRelevantServices = node->m_has_all_wanted_services,
1728 .m_relay_txs = node->m_relays_txs.load(),
1729 .fBloomFilter = node->m_bloom_filter_loaded.load(),
1730 .nKeyedNetGroup = node->nKeyedNetGroup,
1731 .prefer_evict = node->m_prefer_evict,
1732 .m_is_local = node->addr.IsLocal(),
1733 .m_network = node->ConnectedThroughNetwork(),
1734 .m_noban = node->HasPermission(NetPermissionFlags::NoBan),
1735 .m_conn_type = node->m_conn_type,
1736 };
1737 vEvictionCandidates.push_back(candidate);
1738 }
1739 }
1740 const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates), force);
1741 if (!node_id_to_evict) {
1742 return false;
1743 }
1744 LOCK(m_nodes_mutex);
1745 for (CNode* pnode : m_nodes) {
1746 if (pnode->GetId() == *node_id_to_evict) {
1747 LogDebug(BCLog::NET, "selected %s connection for eviction, %s", pnode->ConnectionTypeAsString(), pnode->DisconnectMsg(fLogIPs));
1748 TRACEPOINT(net, evicted_inbound_connection,
1749 pnode->GetId(),
1750 pnode->m_addr_name.c_str(),
1751 pnode->ConnectionTypeAsString().c_str(),
1752 pnode->ConnectedThroughNetwork(),
1753 Ticks<std::chrono::seconds>(pnode->m_connected));
1754 pnode->fDisconnect = true;
1755 return true;
1756 }
1757 }
1758 return false;
1759 }
1760
1761 void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1762 struct sockaddr_storage sockaddr;
1763 socklen_t len = sizeof(sockaddr);
1764 auto sock = hListenSocket.sock->Accept((struct sockaddr*)&sockaddr, &len);
1765
1766 if (!sock) {
1767 const int nErr = WSAGetLastError();
1768 if (nErr != WSAEWOULDBLOCK) {
1769 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
1770 }
1771 return;
1772 }
1773
1774 CService addr;
1775 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr, len)) {
1776 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "Unknown socket family\n");
1777 } else {
1778 addr = MaybeFlipIPv6toCJDNS(addr);
1779 }
1780
1781 const CService addr_bind{MaybeFlipIPv6toCJDNS(GetBindAddress(*sock))};
1782
1783 NetPermissionFlags permission_flags = NetPermissionFlags::None;
1784 hListenSocket.AddSocketPermissionFlags(permission_flags);
1785
1786 CreateNodeFromAcceptedSocket(std::move(sock), permission_flags, addr_bind, addr);
1787 }
1788
1789 void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
1790 NetPermissionFlags permission_flags,
1791 const CService& addr_bind,
1792 const CService& addr)
1793 {
1794 int nInbound = 0;
1795
1796 const bool inbound_onion = [this, &addr, &addr_bind]{
1797 if (m_onion_binds.empty()) {
1798 if (!m_listenonion) {
1799 // If -listenonion=0, assume we do not have inbound Tor connections on non-onion listeners
1800 return false;
1801 }
1802 // Tor connections are coming in on the first -bind
1803 if ((!m_normal_binds.empty()) && addr_bind == m_normal_binds.front()) {
1804 if (addr_bind.IsBindAny()) {
1805 // Tor connections should have a source IP that is local
1806 return addr.IsLocal();
1807 }
1808 // Otherwise, the source IP is unpredictable, so assume anything could be onion
1809 return true;
1810 }
1811 return false;
1812 } else {
1813 return std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) != m_onion_binds.end();
1814 }
1815 }();
1816
1817 // Tor inbound connections do not reveal the peer's actual network address.
1818 // Therefore do not apply address-based whitelist permissions to them.
1819 AddWhitelistPermissionFlags(permission_flags, inbound_onion ? std::optional<CNetAddr>{} : addr, vWhitelistedRangeIncoming);
1820
1821 {
1822 LOCK(m_nodes_mutex);
1823 for (const CNode* pnode : m_nodes) {
1824 if (pnode->IsInboundConn()) nInbound++;
1825 }
1826 }
1827
1828 if (!fNetworkActive) {
1829 LogDebug(BCLog::NET, "connection from %s dropped: not accepting new connections\n", addr.ToStringAddrPort());
1830 return;
1831 }
1832
1833 if (!sock->IsSelectable()) {
1834 LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToStringAddrPort());
1835 return;
1836 }
1837
1838 // According to the internet TCP_NODELAY is not carried into accepted sockets
1839 // on all platforms. Set it again here just to be sure.
1840 const int on{1};
1841 if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
1842 LogDebug(BCLog::NET, "connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
1843 addr.ToStringAddrPort());
1844 }
1845
1846 // Don't accept connections from banned peers.
1847 bool banned = m_banman && m_banman->IsBanned(addr);
1848 if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && banned)
1849 {
1850 LogDebug(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToStringAddrPort());
1851 return;
1852 }
1853
1854 // Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
1855 bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
1856 if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= m_max_inbound && discouraged)
1857 {
1858 LogDebug(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort());
1859 return;
1860 }
1861
1862 bool forced{false};
1863 if (nInbound >= m_max_inbound)
1864 {
1865 // If the inbound connection attempt is granted ForceInbound permission, try a little harder
1866 // to make room by evicting a peer we may not have otherwise evicted.
1867 if (!AttemptToEvictConnection(NetPermissions::HasFlag(permission_flags, NetPermissionFlags::ForceInbound))) {
1868 // No connection to evict, disconnect the new connection
1869 LogDebug(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1870 return;
1871 }
1872
1873 // We kicked someone out
1874 forced = true;
1875 }
1876
1877 NodeId id = GetNewNodeId();
1878 uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1879
1880 // The V2Transport transparently falls back to V1 behavior when an incoming V1 connection is
1881 // detected, so use it whenever we signal NODE_P2P_V2.
1882 ServiceFlags local_services = GetLocalServices();
1883 const bool use_v2transport(local_services & NODE_P2P_V2);
1884
1885 uint64_t network_id = GetDeterministicRandomizer(RANDOMIZER_ID_NETWORKKEY)
1886 .Write(inbound_onion ? NET_ONION : addr.GetNetClass())
1887 .Write(addr_bind.GetAddrBytes())
1888 .Write(addr_bind.GetPort()) // inbound connections use bind port
1889 .Finalize();
1890 CNode* pnode = new CNode(id,
1891 std::move(sock),
1892 CAddress{addr, NODE_NONE},
1893 CalculateKeyedNetGroup(addr),
1894 nonce,
1895 addr_bind,
1896 /*addrNameIn=*/"",
1897 ConnectionType::INBOUND,
1898 inbound_onion,
1899 network_id,
1900 CNodeOptions{
1901 .permission_flags = permission_flags,
1902 .prefer_evict = discouraged,
1903 .forced_inbound = forced,
1904 .recv_flood_size = nReceiveFloodSize,
1905 .use_v2transport = use_v2transport,
1906 });
1907 pnode->AddRef();
1908 m_msgproc->InitializeNode(*pnode, local_services);
1909 {
1910 LOCK(m_nodes_mutex);
1911 m_nodes.push_back(pnode);
1912 }
1913 LogDebug(BCLog::NET, "connection from %s accepted\n", addr.ToStringAddrPort());
1914 TRACEPOINT(net, inbound_connection,
1915 pnode->GetId(),
1916 pnode->m_addr_name.c_str(),
1917 pnode->ConnectionTypeAsString().c_str(),
1918 pnode->ConnectedThroughNetwork(),
1919 GetNodeCount(ConnectionDirection::In));
1920
1921 // We received a new connection, harvest entropy from the time (and our peer count)
1922 RandAddEvent((uint32_t)id);
1923 }
1924
1925 bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type, bool use_v2transport = false)
1926 {
1927 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
1928 std::optional<int> max_connections;
1929 switch (conn_type) {
1930 case ConnectionType::INBOUND:
1931 case ConnectionType::MANUAL:
1932 return false;
1933 case ConnectionType::OUTBOUND_FULL_RELAY:
1934 max_connections = m_max_outbound_full_relay;
1935 break;
1936 case ConnectionType::BLOCK_RELAY:
1937 max_connections = m_max_outbound_block_relay;
1938 break;
1939 // no limit for ADDR_FETCH because -seednode has no limit either
1940 case ConnectionType::ADDR_FETCH:
1941 break;
1942 // no limit for FEELER connections since they're short-lived
1943 case ConnectionType::FEELER:
1944 break;
1945 } // no default case, so the compiler can warn about missing cases
1946
1947 // Count existing connections
1948 int existing_connections = WITH_LOCK(m_nodes_mutex,
1949 return std::count_if(m_nodes.begin(), m_nodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; }););
1950
1951 // Max connections of specified type already exist
1952 if (max_connections != std::nullopt && existing_connections >= max_connections) return false;
1953
1954 // Max total outbound connections already exist
1955 CSemaphoreGrant grant(*semOutbound, true);
1956 if (!grant) return false;
1957
1958 OpenNetworkConnection(CAddress(), false, std::move(grant), address.c_str(), conn_type, /*use_v2transport=*/use_v2transport);
1959 return true;
1960 }
1961
1962 void CConnman::DisconnectNodes()
1963 {
1964 AssertLockNotHeld(m_nodes_mutex);
1965 AssertLockNotHeld(m_reconnections_mutex);
1966
1967 // Use a temporary variable to accumulate desired reconnections, so we don't need
1968 // m_reconnections_mutex while holding m_nodes_mutex.
1969 decltype(m_reconnections) reconnections_to_add;
1970
1971 {
1972 LOCK(m_nodes_mutex);
1973
1974 const bool network_active{fNetworkActive};
1975 if (!network_active) {
1976 // Disconnect any connected nodes
1977 for (CNode* pnode : m_nodes) {
1978 if (!pnode->fDisconnect) {
1979 LogDebug(BCLog::NET, "Network not active, %s\n", pnode->DisconnectMsg(fLogIPs));
1980 pnode->fDisconnect = true;
1981 }
1982 }
1983 }
1984
1985 // Disconnect unused nodes
1986 std::vector<CNode*> nodes_copy = m_nodes;
1987 for (CNode* pnode : nodes_copy)
1988 {
1989 if (pnode->fDisconnect)
1990 {
1991 // remove from m_nodes
1992 m_nodes.erase(remove(m_nodes.begin(), m_nodes.end(), pnode), m_nodes.end());
1993
1994 // Add to reconnection list if appropriate. We don't reconnect right here, because
1995 // the creation of a connection is a blocking operation (up to several seconds),
1996 // and we don't want to hold up the socket handler thread for that long.
1997 if (network_active && pnode->m_transport->ShouldReconnectV1() && !DisableV1OnClearnet(pnode->addr.GetNetClass())) {
1998 reconnections_to_add.push_back({
1999 .addr_connect = pnode->addr,
2000 .grant = std::move(pnode->grantOutbound),
2001 .destination = pnode->m_dest,
2002 .conn_type = pnode->m_conn_type,
2003 .use_v2transport = false});
2004 LogDebug(BCLog::NET, "retrying with v1 transport protocol for peer=%d\n", pnode->GetId());
2005 }
2006
2007 // release outbound grant (if any)
2008 pnode->grantOutbound.Release();
2009
2010 // close socket and cleanup
2011 pnode->CloseSocketDisconnect();
2012
2013 // update connection count by network
2014 if (pnode->IsManualOrFullOutboundConn()) --m_network_conn_counts[pnode->addr.GetNetwork()];
2015
2016 // hold in disconnected pool until all refs are released
2017 pnode->Release();
2018 m_nodes_disconnected.push_back(pnode);
2019 }
2020 }
2021 }
2022 {
2023 // Delete disconnected nodes
2024 std::list<CNode*> nodes_disconnected_copy = m_nodes_disconnected;
2025 for (CNode* pnode : nodes_disconnected_copy)
2026 {
2027 // Destroy the object only after other threads have stopped using it.
2028 if (pnode->GetRefCount() <= 0) {
2029 m_nodes_disconnected.remove(pnode);
2030 DeleteNode(pnode);
2031 }
2032 }
2033 }
2034 {
2035 // Move entries from reconnections_to_add to m_reconnections.
2036 LOCK(m_reconnections_mutex);
2037 m_reconnections.splice(m_reconnections.end(), std::move(reconnections_to_add));
2038 }
2039 }
2040
2041 void CConnman::NotifyNumConnectionsChanged()
2042 {
2043 size_t nodes_size;
2044 {
2045 LOCK(m_nodes_mutex);
2046 nodes_size = m_nodes.size();
2047 }
2048 if(nodes_size != nPrevNodeCount) {
2049 nPrevNodeCount = nodes_size;
2050 if (m_client_interface) {
2051 m_client_interface->NotifyNumConnectionsChanged(nodes_size);
2052 }
2053 }
2054 }
2055
2056 bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const
2057 {
2058 return node.m_connected + m_peer_connect_timeout < now;
2059 }
2060
2061 bool CConnman::InactivityCheck(const CNode& node, std::chrono::microseconds now) const
2062 {
2063 // Tests that see disconnects after using mocktime can start nodes with a
2064 // large timeout. For example, -peertimeout=999999999.
2065 const auto last_send{node.m_last_send.load()};
2066 const auto last_recv{node.m_last_recv.load()};
2067
2068 if (!ShouldRunInactivityChecks(node, now)) return false;
2069
2070 bool has_received{last_recv.count() != 0};
2071 bool has_sent{last_send.count() != 0};
2072
2073 if (!has_received || !has_sent) {
2074 std::string has_never;
2075 if (!has_received) has_never += ", never received from peer";
2076 if (!has_sent) has_never += ", never sent to peer";
2077 LogDebug(BCLog::NET,
2078 "socket no message in first %i seconds%s, %s\n",
2079 count_seconds(m_peer_connect_timeout),
2080 has_never,
2081 node.DisconnectMsg(fLogIPs)
2082 );
2083 return true;
2084 }
2085
2086 if (now > last_send + TIMEOUT_INTERVAL) {
2087 LogDebug(BCLog::NET,
2088 "socket sending timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_send),
2089 node.DisconnectMsg(fLogIPs)
2090 );
2091 return true;
2092 }
2093
2094 if (now > last_recv + TIMEOUT_INTERVAL) {
2095 LogDebug(BCLog::NET,
2096 "socket receive timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_recv),
2097 node.DisconnectMsg(fLogIPs)
2098 );
2099 return true;
2100 }
2101
2102 if (!node.fSuccessfullyConnected) {
2103 if (node.m_transport->GetInfo().transport_type == TransportProtocolType::DETECTING) {
2104 LogDebug(BCLog::NET, "V2 handshake timeout, %s\n", node.DisconnectMsg(fLogIPs));
2105 } else {
2106 LogDebug(BCLog::NET, "version handshake timeout, %s\n", node.DisconnectMsg(fLogIPs));
2107 }
2108 return true;
2109 }
2110
2111 return false;
2112 }
2113
2114 Sock::EventsPerSock CConnman::GenerateWaitSockets(Span<CNode* const> nodes)
2115 {
2116 Sock::EventsPerSock events_per_sock;
2117
2118 for (const ListenSocket& hListenSocket : vhListenSocket) {
2119 events_per_sock.emplace(hListenSocket.sock, Sock::Events{Sock::RECV});
2120 }
2121
2122 for (CNode* pnode : nodes) {
2123 bool select_recv = !pnode->fPauseRecv;
2124 bool select_send;
2125 {
2126 LOCK(pnode->cs_vSend);
2127 // Sending is possible if either there are bytes to send right now, or if there will be
2128 // once a potential message from vSendMsg is handed to the transport. GetBytesToSend
2129 // determines both of these in a single call.
2130 const auto& [to_send, more, _msg_type] = pnode->m_transport->GetBytesToSend(!pnode->vSendMsg.empty());
2131 select_send = !to_send.empty() || more;
2132 }
2133 if (!select_recv && !select_send) continue;
2134
2135 LOCK(pnode->m_sock_mutex);
2136 if (pnode->m_sock) {
2137 Sock::Event event = (select_send ? Sock::SEND : 0) | (select_recv ? Sock::RECV : 0);
2138 events_per_sock.emplace(pnode->m_sock, Sock::Events{event});
2139 }
2140 }
2141
2142 return events_per_sock;
2143 }
2144
2145 void CConnman::SocketHandler()
2146 {
2147 AssertLockNotHeld(m_total_bytes_sent_mutex);
2148
2149 Sock::EventsPerSock events_per_sock;
2150
2151 {
2152 const NodesSnapshot snap{*this, /*shuffle=*/false};
2153
2154 const auto timeout = std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS);
2155
2156 // Check for the readiness of the already connected sockets and the
2157 // listening sockets in one call ("readiness" as in poll(2) or
2158 // select(2)). If none are ready, wait for a short while and return
2159 // empty sets.
2160 events_per_sock = GenerateWaitSockets(snap.Nodes());
2161 if (events_per_sock.empty() || !events_per_sock.begin()->first->WaitMany(timeout, events_per_sock)) {
2162 interruptNet.sleep_for(timeout);
2163 }
2164
2165 // Service (send/receive) each of the already connected nodes.
2166 SocketHandlerConnected(snap.Nodes(), events_per_sock);
2167 }
2168
2169 // Accept new connections from listening sockets.
2170 SocketHandlerListening(events_per_sock);
2171 }
2172
2173 void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
2174 const Sock::EventsPerSock& events_per_sock)
2175 {
2176 AssertLockNotHeld(m_total_bytes_sent_mutex);
2177
2178 auto now = GetTime<std::chrono::microseconds>();
2179
2180 for (CNode* pnode : nodes) {
2181 if (interruptNet)
2182 return;
2183
2184 //
2185 // Receive
2186 //
2187 bool recvSet = false;
2188 bool sendSet = false;
2189 bool errorSet = false;
2190 {
2191 LOCK(pnode->m_sock_mutex);
2192 if (!pnode->m_sock) {
2193 continue;
2194 }
2195 const auto it = events_per_sock.find(pnode->m_sock);
2196 if (it != events_per_sock.end()) {
2197 recvSet = it->second.occurred & Sock::RECV;
2198 sendSet = it->second.occurred & Sock::SEND;
2199 errorSet = it->second.occurred & Sock::ERR;
2200 }
2201 }
2202
2203 if (sendSet) {
2204 // Send data
2205 auto [bytes_sent, data_left] = WITH_LOCK(pnode->cs_vSend, return SocketSendData(*pnode));
2206 if (bytes_sent) {
2207 RecordBytesSent(bytes_sent);
2208
2209 // If both receiving and (non-optimistic) sending were possible, we first attempt
2210 // sending. If that succeeds, but does not fully drain the send queue, do not
2211 // attempt to receive. This avoids needlessly queueing data if the remote peer
2212 // is slow at receiving data, by means of TCP flow control. We only do this when
2213 // sending actually succeeded to make sure progress is always made; otherwise a
2214 // deadlock would be possible when both sides have data to send, but neither is
2215 // receiving.
2216 if (data_left) recvSet = false;
2217 }
2218 }
2219
2220 if (recvSet || errorSet)
2221 {
2222 // typical socket buffer is 8K-64K
2223 uint8_t pchBuf[0x10000];
2224 int nBytes = 0;
2225 {
2226 LOCK(pnode->m_sock_mutex);
2227 if (!pnode->m_sock) {
2228 continue;
2229 }
2230 nBytes = pnode->m_sock->Recv(pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
2231 }
2232 if (nBytes > 0)
2233 {
2234 bool notify = false;
2235 if (!pnode->ReceiveMsgBytes({pchBuf, (size_t)nBytes}, notify)) {
2236 LogDebug(BCLog::NET,
2237 "receiving message bytes failed, %s\n",
2238 pnode->DisconnectMsg(fLogIPs)
2239 );
2240 pnode->CloseSocketDisconnect();
2241 }
2242 RecordBytesRecv(nBytes);
2243 if (notify) {
2244 pnode->MarkReceivedMsgsForProcessing();
2245 WakeMessageHandler();
2246 }
2247 }
2248 else if (nBytes == 0)
2249 {
2250 // socket closed gracefully
2251 if (!pnode->fDisconnect) {
2252 LogDebug(BCLog::NET, "socket closed, %s\n", pnode->DisconnectMsg(fLogIPs));
2253 }
2254 pnode->CloseSocketDisconnect();
2255 }
2256 else if (nBytes < 0)
2257 {
2258 // error
2259 int nErr = WSAGetLastError();
2260 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
2261 {
2262 if (!pnode->fDisconnect) {
2263 LogDebug(BCLog::NET, "socket recv error, %s: %s\n", pnode->DisconnectMsg(fLogIPs), NetworkErrorString(nErr));
2264 }
2265 pnode->CloseSocketDisconnect();
2266 }
2267 }
2268 }
2269
2270 if (InactivityCheck(*pnode, now)) pnode->fDisconnect = true;
2271 }
2272 }
2273
2274 void CConnman::SocketHandlerListening(const Sock::EventsPerSock& events_per_sock)
2275 {
2276 for (const ListenSocket& listen_socket : vhListenSocket) {
2277 if (interruptNet) {
2278 return;
2279 }
2280 const auto it = events_per_sock.find(listen_socket.sock);
2281 if (it != events_per_sock.end() && it->second.occurred & Sock::RECV) {
2282 AcceptConnection(listen_socket);
2283 }
2284 }
2285 }
2286
2287 void CConnman::ThreadSocketHandler()
2288 {
2289 AssertLockNotHeld(m_total_bytes_sent_mutex);
2290
2291 while (!interruptNet)
2292 {
2293 DisconnectNodes();
2294 NotifyNumConnectionsChanged();
2295 SocketHandler();
2296 }
2297 }
2298
2299 void CConnman::WakeMessageHandler()
2300 {
2301 {
2302 LOCK(mutexMsgProc);
2303 fMsgProcWake = true;
2304 }
2305 condMsgProc.notify_one();
2306 }
2307
2308 void CConnman::ThreadDNSAddressSeed()
2309 {
2310 int outbound_connection_count = 0;
2311
2312 if (!gArgs.GetArgs("-seednode").empty()) {
2313 auto start = NodeClock::now();
2314 constexpr std::chrono::seconds SEEDNODE_TIMEOUT = 30s;
2315 LogPrintf("-seednode enabled. Trying the provided seeds for %d seconds before defaulting to the dnsseeds.\n", SEEDNODE_TIMEOUT.count());
2316 while (!interruptNet) {
2317 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
2318 return;
2319
2320 // Abort if we have spent enough time without reaching our target.
2321 // Giving seed nodes 30 seconds so this does not become a race against fixedseeds (which triggers after 1 min)
2322 if (NodeClock::now() > start + SEEDNODE_TIMEOUT) {
2323 LogPrintf("Couldn't connect to enough peers via seed nodes. Handing fetch logic to the DNS seeds.\n");
2324 break;
2325 }
2326
2327 outbound_connection_count = GetBIP110FullOutboundConnCount();
2328 if (outbound_connection_count >= SEED_OUTBOUND_CONNECTION_THRESHOLD) {
2329 LogPrintf("P2P peers available. Finished fetching data from seed nodes.\n");
2330 break;
2331 }
2332 }
2333 }
2334
2335 FastRandomContext rng;
2336 std::vector<std::string> seeds = m_params.DNSSeeds();
2337 std::shuffle(seeds.begin(), seeds.end(), rng);
2338 int seeds_right_now = 0; // Number of seeds left before testing if we have enough connections
2339
2340 if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) {
2341 // When -forcednsseed is provided, query all.
2342 seeds_right_now = seeds.size();
2343 } else if (addrman.Size() == 0) {
2344 // If we have no known peers, query all.
2345 // This will occur on the first run, or if peers.dat has been
2346 // deleted.
2347 seeds_right_now = seeds.size();
2348 }
2349
2350 // Proceed with dnsseeds if seednodes hasn't reached the target or if forcednsseed is set
2351 if (outbound_connection_count < SEED_OUTBOUND_CONNECTION_THRESHOLD || seeds_right_now) {
2352 // goal: only query DNS seed if address need is acute
2353 // * If we have a reasonable number of peers in addrman, spend
2354 // some time trying them first. This improves user privacy by
2355 // creating fewer identifying DNS requests, reduces trust by
2356 // giving seeds less influence on the network topology, and
2357 // reduces traffic to the seeds.
2358 // * When querying DNS seeds query a few at once, this ensures
2359 // that we don't give DNS seeds the ability to eclipse nodes
2360 // that query them.
2361 // * If we continue having problems, eventually query all the
2362 // DNS seeds, and if that fails too, also try the fixed seeds.
2363 // (done in ThreadOpenConnections)
2364 int found = 0;
2365 const std::chrono::seconds seeds_wait_time = (addrman.Size() >= DNSSEEDS_DELAY_PEER_THRESHOLD ? DNSSEEDS_DELAY_MANY_PEERS : DNSSEEDS_DELAY_FEW_PEERS);
2366
2367 for (const std::string& seed : seeds) {
2368 if (seeds_right_now == 0) {
2369 seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
2370
2371 if (addrman.Size() > 0) {
2372 LogPrintf("Waiting %d seconds before querying DNS seeds.\n", seeds_wait_time.count());
2373 std::chrono::seconds to_wait = seeds_wait_time;
2374 while (to_wait.count() > 0) {
2375 // if sleeping for the MANY_PEERS interval, wake up
2376 // early to see if we have enough peers and can stop
2377 // this thread entirely freeing up its resources
2378 std::chrono::seconds w = std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait);
2379 if (!interruptNet.sleep_for(w)) return;
2380 to_wait -= w;
2381
2382 if (GetBIP110FullOutboundConnCount() >= SEED_OUTBOUND_CONNECTION_THRESHOLD) {
2383 if (found > 0) {
2384 LogPrintf("%d addresses found from DNS seeds\n", found);
2385 LogPrintf("P2P peers available. Finished DNS seeding.\n");
2386 } else {
2387 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
2388 }
2389 return;
2390 }
2391 }
2392 }
2393 }
2394
2395 if (interruptNet) return;
2396
2397 // hold off on querying seeds if P2P network deactivated
2398 if (!fNetworkActive) {
2399 LogPrintf("Waiting for network to be reactivated before querying DNS seeds.\n");
2400 do {
2401 if (!interruptNet.sleep_for(std::chrono::seconds{1})) return;
2402 } while (!fNetworkActive);
2403 }
2404
2405 LogPrintf("Loading addresses from DNS seed %s\n", seed);
2406 // If -proxy is in use, we make an ADDR_FETCH connection to the DNS resolved peer address
2407 // for the base dns seed domain in chainparams
2408 if (HaveNameProxy()) {
2409 AddAddrFetch(seed);
2410 } else {
2411 std::vector<CAddress> vAdd;
2412 constexpr ServiceFlags requiredServiceBits{SeedsServiceFlags()};
2413 std::string host = strprintf("x%x.%s", requiredServiceBits, seed);
2414 CNetAddr resolveSource;
2415 if (!resolveSource.SetInternal(host)) {
2416 continue;
2417 }
2418 // Limit number of IPs learned from a single DNS seed. This limit exists to prevent the results from
2419 // one DNS seed from dominating AddrMan. Note that the number of results from a UDP DNS query is
2420 // bounded to 33 already, but it is possible for it to use TCP where a larger number of results can be
2421 // returned.
2422 unsigned int nMaxIPs = 32;
2423 const auto addresses{LookupHost(host, nMaxIPs, true)};
2424 if (!addresses.empty()) {
2425 for (const CNetAddr& ip : addresses) {
2426 CAddress addr = CAddress(CService(ip, m_params.GetDefaultPort()), requiredServiceBits);
2427 addr.nTime = rng.rand_uniform_delay(Now<NodeSeconds>() - 3 * 24h, -4 * 24h); // use a random age between 3 and 7 days old
2428 vAdd.push_back(addr);
2429 found++;
2430 }
2431 addrman.Add(vAdd, resolveSource);
2432 } else {
2433 // If the seed does not support a subdomain with our desired service bits,
2434 // we make an ADDR_FETCH connection to the DNS resolved peer address for the
2435 // base dns seed domain in chainparams
2436 AddAddrFetch(seed);
2437 }
2438 }
2439 --seeds_right_now;
2440 }
2441 LogPrintf("%d addresses found from DNS seeds\n", found);
2442 } else {
2443 LogPrintf("Skipping DNS seeds. Enough peers have been found\n");
2444 }
2445 }
2446
2447 void CConnman::DumpAddresses()
2448 {
2449 const auto start{SteadyClock::now()};
2450
2451 DumpPeerAddresses(::gArgs, addrman);
2452
2453 LogDebug(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
2454 addrman.Size(), Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
2455 }
2456
2457 void CConnman::ProcessAddrFetch()
2458 {
2459 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
2460 std::string strDest;
2461 {
2462 LOCK(m_addr_fetches_mutex);
2463 if (m_addr_fetches.empty())
2464 return;
2465 strDest = m_addr_fetches.front();
2466 m_addr_fetches.pop_front();
2467 }
2468 // Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
2469 // peer doesn't support it or immediately disconnects us for another reason.
2470 const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
2471 CAddress addr;
2472 CSemaphoreGrant grant(*semOutbound, /*fTry=*/true);
2473 if (grant) {
2474 OpenNetworkConnection(addr, false, std::move(grant), strDest.c_str(), ConnectionType::ADDR_FETCH, use_v2transport);
2475 }
2476 }
2477
2478 bool CConnman::GetTryNewOutboundPeer() const
2479 {
2480 return m_try_another_outbound_peer;
2481 }
2482
2483 void CConnman::SetTryNewOutboundPeer(bool flag)
2484 {
2485 m_try_another_outbound_peer = flag;
2486 LogDebug(BCLog::NET, "setting try another outbound peer=%s\n", flag ? "true" : "false");
2487 }
2488
2489 void CConnman::StartExtraBlockRelayPeers()
2490 {
2491 LogDebug(BCLog::NET, "enabling extra block-relay-only peers\n");
2492 m_start_extra_block_relay_peers = true;
2493 }
2494
2495 // Return the number of BIP110 outbound connections that are full relay (not blocks only).
2496 // Non-BIP110 outbound peers are excluded as they are "additional" and don't count toward limits.
2497 int CConnman::GetBIP110FullOutboundConnCount() const
2498 {
2499 int nRelevant = 0;
2500 {
2501 LOCK(m_nodes_mutex);
2502 for (const CNode* pnode : m_nodes) {
2503 if (pnode->fSuccessfullyConnected && pnode->IsFullOutboundConn() && !pnode->m_is_non_bip110_outbound) ++nRelevant;
2504 }
2505 }
2506 return nRelevant;
2507 }
2508
2509 // Return the number of peers we have over our outbound connection limit
2510 // Exclude peers that are marked for disconnect, or are going to be
2511 // disconnected soon (eg ADDR_FETCH and FEELER)
2512 // Also exclude peers that haven't finished initial connection handshake yet
2513 // (so that we don't decide we're over our desired connection limit, and then
2514 // evict some peer that has finished the handshake)
2515 int CConnman::GetExtraFullOutboundCount() const
2516 {
2517 int full_outbound_peers = 0;
2518 {
2519 LOCK(m_nodes_mutex);
2520 for (const CNode* pnode : m_nodes) {
2521 if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsFullOutboundConn()) {
2522 ++full_outbound_peers;
2523 }
2524 }
2525 }
2526 return std::max(full_outbound_peers - m_max_outbound_full_relay, 0);
2527 }
2528
2529 int CConnman::GetExtraBlockRelayCount() const
2530 {
2531 int block_relay_peers = 0;
2532 {
2533 LOCK(m_nodes_mutex);
2534 for (const CNode* pnode : m_nodes) {
2535 if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsBlockOnlyConn()) {
2536 ++block_relay_peers;
2537 }
2538 }
2539 }
2540 return std::max(block_relay_peers - m_max_outbound_block_relay, 0);
2541 }
2542
2543 std::unordered_set<Network> CConnman::GetReachableEmptyNetworks() const
2544 {
2545 std::unordered_set<Network> networks{};
2546 for (int n = 0; n < NET_MAX; n++) {
2547 enum Network net = (enum Network)n;
2548 if (net == NET_UNROUTABLE || net == NET_INTERNAL) continue;
2549 if (g_reachable_nets.Contains(net) && addrman.Size(net, std::nullopt) == 0) {
2550 networks.insert(net);
2551 }
2552 }
2553 return networks;
2554 }
2555
2556 bool CConnman::MultipleManualOrFullOutboundConns(Network net) const
2557 {
2558 AssertLockHeld(m_nodes_mutex);
2559 return m_network_conn_counts[net] > 1;
2560 }
2561
2562 bool CConnman::DisableV1OnClearnet(Network net) const
2563 {
2564 return disable_v1conn_clearnet && (net == NET_IPV4 || net == NET_IPV6);
2565 }
2566
2567 bool CConnman::MaybePickPreferredNetwork(std::optional<Network>& network)
2568 {
2569 std::array<Network, 5> nets{NET_IPV4, NET_IPV6, NET_ONION, NET_I2P, NET_CJDNS};
2570 std::shuffle(nets.begin(), nets.end(), FastRandomContext());
2571
2572 LOCK(m_nodes_mutex);
2573 for (const auto net : nets) {
2574 if (g_reachable_nets.Contains(net) && m_network_conn_counts[net] == 0 && addrman.Size(net) != 0) {
2575 network = net;
2576 return true;
2577 }
2578 }
2579
2580 return false;
2581 }
2582
2583 void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, Span<const std::string> seed_nodes)
2584 {
2585 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
2586 AssertLockNotHeld(m_reconnections_mutex);
2587 FastRandomContext rng;
2588 // Connect to specific addresses
2589 if (!connect.empty())
2590 {
2591 // Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
2592 // peer doesn't support it or immediately disconnects us for another reason.
2593 const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
2594 for (int64_t nLoop = 0;; nLoop++)
2595 {
2596 for (const std::string& strAddr : connect)
2597 {
2598 CAddress addr(CService(), NODE_NONE);
2599 OpenNetworkConnection(addr, false, {}, strAddr.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/use_v2transport);
2600 for (int i = 0; i < 10 && i < nLoop; i++)
2601 {
2602 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
2603 return;
2604 }
2605 }
2606 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
2607 return;
2608 PerformReconnections();
2609 }
2610 }
2611
2612 // Initiate network connections
2613 auto start = GetTime<std::chrono::microseconds>();
2614
2615 // Minimum time before next feeler connection (in microseconds).
2616 auto next_feeler = start + rng.rand_exp_duration(FEELER_INTERVAL);
2617 auto next_extra_block_relay = start + rng.rand_exp_duration(EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
2618 auto next_extra_network_peer{start + rng.rand_exp_duration(EXTRA_NETWORK_PEER_INTERVAL)};
2619 const bool dnsseed = gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED);
2620 bool add_fixed_seeds = gArgs.GetBoolArg("-fixedseeds", DEFAULT_FIXEDSEEDS);
2621 const bool use_seednodes{!gArgs.GetArgs("-seednode").empty()};
2622
2623 auto seed_node_timer = NodeClock::now();
2624 bool add_addr_fetch{addrman.Size() == 0 && !seed_nodes.empty()};
2625 constexpr std::chrono::seconds ADD_NEXT_SEEDNODE = 10s;
2626
2627 if (!add_fixed_seeds) {
2628 LogPrintf("Fixed seeds are disabled\n");
2629 }
2630
2631 while (!interruptNet)
2632 {
2633 if (add_addr_fetch) {
2634 add_addr_fetch = false;
2635 const auto& seed{SpanPopBack(seed_nodes)};
2636 AddAddrFetch(seed);
2637
2638 if (addrman.Size() == 0) {
2639 LogInfo("Empty addrman, adding seednode (%s) to addrfetch\n", seed);
2640 } else {
2641 LogInfo("Couldn't connect to peers from addrman after %d seconds. Adding seednode (%s) to addrfetch\n", ADD_NEXT_SEEDNODE.count(), seed);
2642 }
2643 }
2644
2645 ProcessAddrFetch();
2646
2647 if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
2648 return;
2649
2650 PerformReconnections();
2651
2652 CSemaphoreGrant grant(*semOutbound);
2653 if (interruptNet)
2654 return;
2655
2656 const std::unordered_set<Network> fixed_seed_networks{GetReachableEmptyNetworks()};
2657 if (add_fixed_seeds && !fixed_seed_networks.empty()) {
2658 // When the node starts with an empty peers.dat, there are a few other sources of peers before
2659 // we fallback on to fixed seeds: -dnsseed, -seednode, -addnode
2660 // If none of those are available, we fallback on to fixed seeds immediately, else we allow
2661 // 60 seconds for any of those sources to populate addrman.
2662 bool add_fixed_seeds_now = false;
2663 // It is cheapest to check if enough time has passed first.
2664 if (GetTime<std::chrono::seconds>() > start + std::chrono::minutes{1}) {
2665 add_fixed_seeds_now = true;
2666 LogPrintf("Adding fixed seeds as 60 seconds have passed and addrman is empty for at least one reachable network\n");
2667 }
2668
2669 // Perform cheap checks before locking a mutex.
2670 else if (!dnsseed && !use_seednodes) {
2671 LOCK(m_added_nodes_mutex);
2672 if (m_added_node_params.empty()) {
2673 add_fixed_seeds_now = true;
2674 LogPrintf("Adding fixed seeds as -dnsseed=0 (or IPv4/IPv6 connections are disabled via -onlynet) and neither -addnode nor -seednode are provided\n");
2675 }
2676 }
2677
2678 if (add_fixed_seeds_now) {
2679 std::vector<CAddress> seed_addrs{ConvertSeeds(m_params.FixedSeeds())};
2680 // We will not make outgoing connections to peers that are unreachable
2681 // (e.g. because of -onlynet configuration).
2682 // Therefore, we do not add them to addrman in the first place.
2683 // In case previously unreachable networks become reachable
2684 // (e.g. in case of -onlynet changes by the user), fixed seeds will
2685 // be loaded only for networks for which we have no addresses.
2686 seed_addrs.erase(std::remove_if(seed_addrs.begin(), seed_addrs.end(),
2687 [&fixed_seed_networks](const CAddress& addr) { return fixed_seed_networks.count(addr.GetNetwork()) == 0; }),
2688 seed_addrs.end());
2689 CNetAddr local;
2690 local.SetInternal("fixedseeds");
2691 addrman.Add(seed_addrs, local);
2692 add_fixed_seeds = false;
2693 LogPrintf("Added %d fixed seeds from reachable networks.\n", seed_addrs.size());
2694 }
2695 }
2696
2697 //
2698 // Choose an address to connect to based on most recently seen
2699 //
2700 CAddress addrConnect;
2701
2702 // Only connect out to one peer per ipv4/ipv6 network group (/16 for IPv4).
2703 int nOutboundFullRelay = 0;
2704 int nOutboundBlockRelay = 0;
2705 int outbound_privacy_network_peers = 0;
2706 std::set<std::vector<unsigned char>> outbound_ipv46_peer_netgroups;
2707
2708 {
2709 LOCK(m_nodes_mutex);
2710 for (const CNode* pnode : m_nodes) {
2711 // Non-BIP110 outbound peers are "additional" - don't count toward limits
2712 if (pnode->IsFullOutboundConn() && !pnode->m_is_non_bip110_outbound) nOutboundFullRelay++;
2713 if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++;
2714
2715 // Make sure our persistent outbound slots to ipv4/ipv6 peers belong to different netgroups.
2716 switch (pnode->m_conn_type) {
2717 // We currently don't take inbound connections into account. Since they are
2718 // free to make, an attacker could make them to prevent us from connecting to
2719 // certain peers.
2720 case ConnectionType::INBOUND:
2721 // Short-lived outbound connections should not affect how we select outbound
2722 // peers from addrman.
2723 case ConnectionType::ADDR_FETCH:
2724 case ConnectionType::FEELER:
2725 break;
2726 case ConnectionType::MANUAL:
2727 case ConnectionType::OUTBOUND_FULL_RELAY:
2728 case ConnectionType::BLOCK_RELAY:
2729 const CAddress address{pnode->addr};
2730 if (address.IsTor() || address.IsI2P() || address.IsCJDNS()) {
2731 // Since our addrman-groups for these networks are
2732 // random, without relation to the route we
2733 // take to connect to these peers or to the
2734 // difficulty in obtaining addresses with diverse
2735 // groups, we don't worry about diversity with
2736 // respect to our addrman groups when connecting to
2737 // these networks.
2738 ++outbound_privacy_network_peers;
2739 } else {
2740 outbound_ipv46_peer_netgroups.insert(m_netgroupman.GetGroup(address));
2741 }
2742 } // no default case, so the compiler can warn about missing cases
2743 }
2744 }
2745
2746 if (!seed_nodes.empty() && nOutboundFullRelay < SEED_OUTBOUND_CONNECTION_THRESHOLD) {
2747 if (NodeClock::now() > seed_node_timer + ADD_NEXT_SEEDNODE) {
2748 seed_node_timer = NodeClock::now();
2749 add_addr_fetch = true;
2750 }
2751 }
2752
2753 ConnectionType conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
2754 auto now = GetTime<std::chrono::microseconds>();
2755 bool anchor = false;
2756 bool fFeeler = false;
2757 std::optional<Network> preferred_net;
2758
2759 // Determine what type of connection to open. Opening
2760 // BLOCK_RELAY connections to addresses from anchors.dat gets the highest
2761 // priority. Then we open OUTBOUND_FULL_RELAY priority until we
2762 // meet our full-relay capacity. Then we open BLOCK_RELAY connection
2763 // until we hit our block-relay-only peer limit.
2764 // GetTryNewOutboundPeer() gets set when a stale tip is detected, so we
2765 // try opening an additional OUTBOUND_FULL_RELAY connection. If none of
2766 // these conditions are met, check to see if it's time to try an extra
2767 // block-relay-only peer (to confirm our tip is current, see below) or the next_feeler
2768 // timer to decide if we should open a FEELER.
2769
2770 if (!m_anchors.empty() && (nOutboundBlockRelay < m_max_outbound_block_relay)) {
2771 conn_type = ConnectionType::BLOCK_RELAY;
2772 anchor = true;
2773 } else if (nOutboundFullRelay < m_max_outbound_full_relay) {
2774 // OUTBOUND_FULL_RELAY
2775 } else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
2776 conn_type = ConnectionType::BLOCK_RELAY;
2777 } else if (GetTryNewOutboundPeer()) {
2778 // OUTBOUND_FULL_RELAY
2779 } else if (now > next_extra_block_relay && m_start_extra_block_relay_peers) {
2780 // Periodically connect to a peer (using regular outbound selection
2781 // methodology from addrman) and stay connected long enough to sync
2782 // headers, but not much else.
2783 //
2784 // Then disconnect the peer, if we haven't learned anything new.
2785 //
2786 // The idea is to make eclipse attacks very difficult to pull off,
2787 // because every few minutes we're finding a new peer to learn headers
2788 // from.
2789 //
2790 // This is similar to the logic for trying extra outbound (full-relay)
2791 // peers, except:
2792 // - we do this all the time on an exponential timer, rather than just when
2793 // our tip is stale
2794 // - we potentially disconnect our next-youngest block-relay-only peer, if our
2795 // newest block-relay-only peer delivers a block more recently.
2796 // See the eviction logic in net_processing.cpp.
2797 //
2798 // Because we can promote these connections to block-relay-only
2799 // connections, they do not get their own ConnectionType enum
2800 // (similar to how we deal with extra outbound peers).
2801 next_extra_block_relay = now + rng.rand_exp_duration(EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
2802 conn_type = ConnectionType::BLOCK_RELAY;
2803 } else if (now > next_feeler) {
2804 next_feeler = now + rng.rand_exp_duration(FEELER_INTERVAL);
2805 conn_type = ConnectionType::FEELER;
2806 fFeeler = true;
2807 } else if (nOutboundFullRelay == m_max_outbound_full_relay &&
2808 m_max_outbound_full_relay == MAX_OUTBOUND_FULL_RELAY_CONNECTIONS &&
2809 now > next_extra_network_peer &&
2810 MaybePickPreferredNetwork(preferred_net)) {
2811 // Full outbound connection management: Attempt to get at least one
2812 // outbound peer from each reachable network by making extra connections
2813 // and then protecting "only" peers from a network during outbound eviction.
2814 // This is not attempted if the user changed -maxconnections to a value
2815 // so low that less than MAX_OUTBOUND_FULL_RELAY_CONNECTIONS are made,
2816 // to prevent interactions with otherwise protected outbound peers.
2817 next_extra_network_peer = now + rng.rand_exp_duration(EXTRA_NETWORK_PEER_INTERVAL);
2818 } else {
2819 // skip to next iteration of while loop
2820 continue;
2821 }
2822
2823 addrman.ResolveCollisions();
2824
2825 const auto current_time{NodeClock::now()};
2826 int nTries = 0;
2827 const auto reachable_nets{g_reachable_nets.All()};
2828
2829 while (!interruptNet)
2830 {
2831 if (anchor && !m_anchors.empty()) {
2832 const CAddress addr = m_anchors.back();
2833 m_anchors.pop_back();
2834 if (!addr.IsValid() || IsLocal(addr) || !g_reachable_nets.Contains(addr) ||
2835 !m_msgproc->HasAllDesirableServiceFlags(addr.nServices) ||
2836 outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr))) continue;
2837 addrConnect = addr;
2838 LogDebug(BCLog::NET, "Trying to make an anchor connection to %s\n", addrConnect.ToStringAddrPort());
2839 break;
2840 }
2841
2842 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
2843 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
2844 // already-connected network ranges, ...) before trying new addrman addresses.
2845 nTries++;
2846 if (nTries > 100)
2847 break;
2848
2849 CAddress addr;
2850 NodeSeconds addr_last_try{0s};
2851
2852 if (fFeeler) {
2853 // First, try to get a tried table collision address. This returns
2854 // an empty (invalid) address if there are no collisions to try.
2855 std::tie(addr, addr_last_try) = addrman.SelectTriedCollision();
2856
2857 if (!addr.IsValid()) {
2858 // No tried table collisions. Select a new table address
2859 // for our feeler.
2860 std::tie(addr, addr_last_try) = addrman.Select(true, reachable_nets);
2861 } else if (AlreadyConnectedToAddress(addr)) {
2862 // If test-before-evict logic would have us connect to a
2863 // peer that we're already connected to, just mark that
2864 // address as Good(). We won't be able to initiate the
2865 // connection anyway, so this avoids inadvertently evicting
2866 // a currently-connected peer.
2867 addrman.Good(addr);
2868 // Select a new table address for our feeler instead.
2869 std::tie(addr, addr_last_try) = addrman.Select(true, reachable_nets);
2870 }
2871 } else {
2872 // Not a feeler
2873 // If preferred_net has a value set, pick an extra outbound
2874 // peer from that network. The eviction logic in net_processing
2875 // ensures that a peer from another network will be evicted.
2876 std::tie(addr, addr_last_try) = preferred_net.has_value()
2877 ? addrman.Select(false, {*preferred_net})
2878 : addrman.Select(false, reachable_nets);
2879 }
2880
2881 // Require outbound IPv4/IPv6 connections, other than feelers, to be to distinct network groups
2882 if (!fFeeler && outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr))) {
2883 continue;
2884 }
2885
2886 // if we selected an invalid or local address, restart
2887 if (!addr.IsValid() || IsLocal(addr)) {
2888 break;
2889 }
2890
2891 if (!g_reachable_nets.Contains(addr)) {
2892 continue;
2893 }
2894
2895 // only consider very recently tried nodes after 30 failed attempts
2896 if (current_time - addr_last_try < 10min && nTries < 30) {
2897 continue;
2898 }
2899
2900 // for non-feelers, require all the services we'll want,
2901 // for feelers, only require they be a full node (only because most
2902 // SPV clients don't have a good address DB available)
2903 if (!fFeeler && !m_msgproc->HasAllDesirableServiceFlags(addr.nServices)) {
2904 continue;
2905 } else if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
2906 continue;
2907 }
2908
2909 // Do not connect to bad ports, unless 50 invalid addresses have been selected already.
2910 if (nTries < 50 && (addr.IsIPv4() || addr.IsIPv6()) && IsBadPort(addr.GetPort())) {
2911 continue;
2912 }
2913
2914 // Do not make automatic outbound connections to addnode peers, to
2915 // not use our limited outbound slots for them and to ensure
2916 // addnode connections benefit from their intended protections.
2917 if (AddedNodesContain(addr)) {
2918 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "Not making automatic %s%s connection to %s peer selected for manual (addnode) connection%s\n",
2919 preferred_net.has_value() ? "network-specific " : "",
2920 ConnectionTypeAsString(conn_type), GetNetworkName(addr.GetNetwork()),
2921 fLogIPs ? strprintf(": %s", addr.ToStringAddrPort()) : "");
2922 continue;
2923 }
2924
2925 addrConnect = addr;
2926 break;
2927 }
2928
2929 if (addrConnect.IsValid()) {
2930 if (fFeeler) {
2931 // Add small amount of random noise before connection to avoid synchronization.
2932 if (!interruptNet.sleep_for(rng.rand_uniform_duration<CThreadInterrupt::Clock>(FEELER_SLEEP_WINDOW))) {
2933 return;
2934 }
2935 LogDebug(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToStringAddrPort());
2936 }
2937
2938 if (preferred_net != std::nullopt) LogDebug(BCLog::NET, "Making network specific connection to %s on %s.\n", addrConnect.ToStringAddrPort(), GetNetworkName(preferred_net.value()));
2939
2940 // Record addrman failure attempts when node has at least 2 persistent outbound connections to peers with
2941 // different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
2942 // Don't record addrman failure attempts when node is offline. This can be identified since all local
2943 // network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1.
2944 const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(m_max_automatic_connections - 1, 2)};
2945 // Use BIP324 transport when both us and them have NODE_V2_P2P set.
2946 const bool use_v2transport(addrConnect.nServices & GetLocalServices() & NODE_P2P_V2);
2947 OpenNetworkConnection(addrConnect, count_failures, std::move(grant), /*strDest=*/nullptr, conn_type, use_v2transport);
2948 }
2949 }
2950 }
2951
2952 std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
2953 {
2954 std::vector<CAddress> ret;
2955 LOCK(m_nodes_mutex);
2956 for (const CNode* pnode : m_nodes) {
2957 if (pnode->IsBlockOnlyConn()) {
2958 ret.push_back(pnode->addr);
2959 }
2960 }
2961
2962 return ret;
2963 }
2964
2965 std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo(bool include_connected) const
2966 {
2967 std::vector<AddedNodeInfo> ret;
2968
2969 std::list<AddedNodeParams> lAddresses(0);
2970 {
2971 LOCK(m_added_nodes_mutex);
2972 ret.reserve(m_added_node_params.size());
2973 std::copy(m_added_node_params.cbegin(), m_added_node_params.cend(), std::back_inserter(lAddresses));
2974 }
2975
2976
2977 // Build a map of all already connected addresses (by IP:port and by name) to inbound/outbound and resolved CService
2978 std::map<CService, bool> mapConnected;
2979 std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
2980 {
2981 LOCK(m_nodes_mutex);
2982 for (const CNode* pnode : m_nodes) {
2983 if (pnode->addr.IsValid()) {
2984 mapConnected[pnode->addr] = pnode->IsInboundConn();
2985 }
2986 std::string addrName{pnode->m_addr_name};
2987 if (!addrName.empty()) {
2988 mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->IsInboundConn(), static_cast<const CService&>(pnode->addr));
2989 }
2990 }
2991 }
2992
2993 for (const auto& addr : lAddresses) {
2994 CService service{MaybeFlipIPv6toCJDNS(LookupNumeric(addr.m_added_node, GetDefaultPort(addr.m_added_node)))};
2995 AddedNodeInfo addedNode{addr, CService(), false, false};
2996 if (service.IsValid()) {
2997 // strAddNode is an IP:port
2998 auto it = mapConnected.find(service);
2999 if (it != mapConnected.end()) {
3000 if (!include_connected) {
3001 continue;
3002 }
3003 addedNode.resolvedAddress = service;
3004 addedNode.fConnected = true;
3005 addedNode.fInbound = it->second;
3006 }
3007 } else {
3008 // strAddNode is a name
3009 auto it = mapConnectedByName.find(addr.m_added_node);
3010 if (it != mapConnectedByName.end()) {
3011 if (!include_connected) {
3012 continue;
3013 }
3014 addedNode.resolvedAddress = it->second.second;
3015 addedNode.fConnected = true;
3016 addedNode.fInbound = it->second.first;
3017 }
3018 }
3019 ret.emplace_back(std::move(addedNode));
3020 }
3021
3022 return ret;
3023 }
3024
3025 void CConnman::ThreadOpenAddedConnections()
3026 {
3027 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
3028 AssertLockNotHeld(m_reconnections_mutex);
3029 while (true)
3030 {
3031 CSemaphoreGrant grant(*semAddnode);
3032 std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo(/*include_connected=*/false);
3033 bool tried = false;
3034 for (const AddedNodeInfo& info : vInfo) {
3035 if (!grant) {
3036 // If we've used up our semaphore and need a new one, let's not wait here since while we are waiting
3037 // the addednodeinfo state might change.
3038 break;
3039 }
3040 tried = true;
3041 CAddress addr(CService(), NODE_NONE);
3042 OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
3043 if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
3044 grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
3045 }
3046 // See if any reconnections are desired.
3047 PerformReconnections();
3048 // Retry every 60 seconds if a connection was attempted, otherwise two seconds
3049 if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
3050 return;
3051 }
3052 }
3053
3054 // if successful, this moves the passed grant to the constructed node
3055 void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant&& grant_outbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport)
3056 {
3057 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
3058 assert(conn_type != ConnectionType::INBOUND);
3059
3060 //
3061 // Initiate outbound network connection
3062 //
3063 if (interruptNet) {
3064 return;
3065 }
3066 if (!fNetworkActive) {
3067 return;
3068 }
3069 if (!pszDest) {
3070 bool banned_or_discouraged = m_banman && (m_banman->IsDiscouraged(addrConnect) || m_banman->IsBanned(addrConnect));
3071 if (IsLocal(addrConnect) || banned_or_discouraged || AlreadyConnectedToAddress(addrConnect)) {
3072 return;
3073 }
3074 } else if (FindNode(std::string(pszDest)))
3075 return;
3076
3077 CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, use_v2transport);
3078
3079 if (!pnode)
3080 return;
3081 pnode->grantOutbound = std::move(grant_outbound);
3082
3083 m_msgproc->InitializeNode(*pnode, m_local_services);
3084 {
3085 LOCK(m_nodes_mutex);
3086 m_nodes.push_back(pnode);
3087
3088 // update connection count by network
3089 if (pnode->IsManualOrFullOutboundConn()) ++m_network_conn_counts[pnode->addr.GetNetwork()];
3090 }
3091
3092 TRACEPOINT(net, outbound_connection,
3093 pnode->GetId(),
3094 pnode->m_addr_name.c_str(),
3095 pnode->ConnectionTypeAsString().c_str(),
3096 pnode->ConnectedThroughNetwork(),
3097 GetNodeCount(ConnectionDirection::Out));
3098 }
3099
3100 Mutex NetEventsInterface::g_msgproc_mutex;
3101
3102 void CConnman::ThreadMessageHandler()
3103 {
3104 LOCK(NetEventsInterface::g_msgproc_mutex);
3105
3106 while (!flagInterruptMsgProc)
3107 {
3108 bool fMoreWork = false;
3109
3110 {
3111 // Randomize the order in which we process messages from/to our peers.
3112 // This prevents attacks in which an attacker exploits having multiple
3113 // consecutive connections in the m_nodes list.
3114 const NodesSnapshot snap{*this, /*shuffle=*/true};
3115
3116 for (CNode* pnode : snap.Nodes()) {
3117 if (pnode->fDisconnect)
3118 continue;
3119
3120 CpuTimer timer{[&pnode](std::chrono::nanoseconds elapsed) { pnode->m_cpu_time += elapsed; }};
3121
3122 // Receive messages
3123 bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
3124 fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
3125 if (flagInterruptMsgProc)
3126 return;
3127 // Send messages
3128 m_msgproc->SendMessages(pnode);
3129
3130 if (flagInterruptMsgProc)
3131 return;
3132 }
3133 }
3134
3135 WAIT_LOCK(mutexMsgProc, lock);
3136 if (!fMoreWork) {
3137 condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this]() EXCLUSIVE_LOCKS_REQUIRED(mutexMsgProc) { return fMsgProcWake; });
3138 }
3139 fMsgProcWake = false;
3140 }
3141 }
3142
3143 void CConnman::ThreadI2PAcceptIncoming()
3144 {
3145 static constexpr auto err_wait_begin = 1s;
3146 static constexpr auto err_wait_cap = 5min;
3147 auto err_wait = err_wait_begin;
3148
3149 bool advertising_listen_addr = false;
3150 i2p::Connection conn;
3151
3152 auto SleepOnFailure = [&]() {
3153 interruptNet.sleep_for(err_wait);
3154 if (err_wait < err_wait_cap) {
3155 err_wait += 1s;
3156 }
3157 };
3158
3159 while (!interruptNet) {
3160
3161 if (!m_i2p_sam_session->Listen(conn)) {
3162 if (advertising_listen_addr && conn.me.IsValid()) {
3163 RemoveLocal(conn.me);
3164 advertising_listen_addr = false;
3165 }
3166 SleepOnFailure();
3167 continue;
3168 }
3169
3170 if (!advertising_listen_addr) {
3171 AddLocal(conn.me, LOCAL_MANUAL);
3172 advertising_listen_addr = true;
3173 }
3174
3175 if (!m_i2p_sam_session->Accept(conn)) {
3176 SleepOnFailure();
3177 continue;
3178 }
3179
3180 CreateNodeFromAcceptedSocket(std::move(conn.sock), NetPermissionFlags::None, conn.me, conn.peer);
3181
3182 err_wait = err_wait_begin;
3183 }
3184 }
3185
3186 bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError, NetPermissionFlags permissions)
3187 {
3188 int nOne = 1;
3189
3190 // Create socket for listening for incoming connections
3191 struct sockaddr_storage sockaddr;
3192 socklen_t len = sizeof(sockaddr);
3193 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
3194 {
3195 strError = Untranslated(strprintf("Bind address family for %s not supported", addrBind.ToStringAddrPort()));
3196 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
3197 return false;
3198 }
3199
3200 std::unique_ptr<Sock> sock = CreateSock(addrBind.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP);
3201 if (!sock) {
3202 strError = Untranslated(strprintf("Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError())));
3203 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
3204 return false;
3205 }
3206
3207 // Allow binding if the port is still in TIME_WAIT state after
3208 // the program was closed and restarted.
3209 if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
3210 strError = Untranslated(strprintf("Error setting SO_REUSEADDR on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
3211 LogPrintf("%s\n", strError.original);
3212 }
3213
3214 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
3215 // and enable it by default or not. Try to enable it, if possible.
3216 if (addrBind.IsIPv6()) {
3217 #ifdef IPV6_V6ONLY
3218 if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
3219 strError = Untranslated(strprintf("Error setting IPV6_V6ONLY on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
3220 LogPrintf("%s\n", strError.original);
3221 }
3222 #endif
3223 #ifdef WIN32
3224 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
3225 if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)) == SOCKET_ERROR) {
3226 strError = Untranslated(strprintf("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
3227 LogPrintf("%s\n", strError.original);
3228 }
3229 #endif
3230 }
3231
3232 if (sock->Bind(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
3233 int nErr = WSAGetLastError();
3234 if (nErr == WSAEADDRINUSE)
3235 strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToStringAddrPort(), CLIENT_NAME);
3236 else
3237 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToStringAddrPort(), NetworkErrorString(nErr));
3238 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
3239 return false;
3240 }
3241 LogPrintf("Bound to %s\n", addrBind.ToStringAddrPort());
3242
3243 // Listen for incoming connections
3244 if (sock->Listen(SOMAXCONN) == SOCKET_ERROR)
3245 {
3246 strError = strprintf(_("Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
3247 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
3248 return false;
3249 }
3250
3251 vhListenSocket.emplace_back(std::move(sock), permissions);
3252 return true;
3253 }
3254
3255 void Discover()
3256 {
3257 if (!fDiscover)
3258 return;
3259
3260 for (const CNetAddr &addr: GetLocalAddresses()) {
3261 if (AddLocal(addr, LOCAL_IF))
3262 LogPrintf("%s: %s\n", __func__, addr.ToStringAddr());
3263 }
3264 }
3265
3266 void CConnman::SetNetworkActive(bool active)
3267 {
3268 LogPrintf("%s: %s\n", __func__, active);
3269
3270 if (fNetworkActive == active) {
3271 return;
3272 }
3273
3274 fNetworkActive = active;
3275
3276 if (m_client_interface) {
3277 m_client_interface->NotifyNetworkActiveChanged(fNetworkActive);
3278 }
3279 }
3280
3281 CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, AddrMan& addrman_in,
3282 const NetGroupManager& netgroupman, const CChainParams& params, bool network_active)
3283 : addrman(addrman_in)
3284 , m_netgroupman{netgroupman}
3285 , nSeed0(nSeed0In)
3286 , nSeed1(nSeed1In)
3287 , m_params(params)
3288 {
3289 SetTryNewOutboundPeer(false);
3290
3291 Options connOptions;
3292 Init(connOptions);
3293 SetNetworkActive(network_active);
3294 }
3295
3296 NodeId CConnman::GetNewNodeId()
3297 {
3298 return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
3299 }
3300
3301 uint16_t CConnman::GetDefaultPort(Network net) const
3302 {
3303 return net == NET_I2P ? I2P_SAM31_PORT : m_params.GetDefaultPort();
3304 }
3305
3306 uint16_t CConnman::GetDefaultPort(const std::string& addr) const
3307 {
3308 CNetAddr a;
3309 return a.SetSpecial(addr) ? GetDefaultPort(a.GetNetwork()) : m_params.GetDefaultPort();
3310 }
3311
3312 bool CConnman::Bind(const CService& addr_, unsigned int flags, NetPermissionFlags permissions)
3313 {
3314 const CService addr{MaybeFlipIPv6toCJDNS(addr_)};
3315
3316 bilingual_str strError;
3317 if (!BindListenPort(addr, strError, permissions)) {
3318 if ((flags & BF_REPORT_ERROR) && m_client_interface) {
3319 m_client_interface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
3320 }
3321 return false;
3322 }
3323
3324 if (addr.IsRoutable() && fDiscover && !(flags & BF_DONT_ADVERTISE) && !NetPermissions::HasFlag(permissions, NetPermissionFlags::NoBan)) {
3325 AddLocal(addr, LOCAL_BIND);
3326 }
3327
3328 return true;
3329 }
3330
3331 bool CConnman::InitBinds(const Options& options)
3332 {
3333 for (const auto& addrBind : options.vBinds) {
3334 if (!Bind(addrBind, BF_REPORT_ERROR, NetPermissionFlags::None)) {
3335 return false;
3336 }
3337 }
3338 for (const auto& addrBind : options.vWhiteBinds) {
3339 if (!Bind(addrBind.m_service, BF_REPORT_ERROR, addrBind.m_flags)) {
3340 return false;
3341 }
3342 }
3343 for (const auto& addr_bind : options.onion_binds) {
3344 if (!Bind(addr_bind, BF_REPORT_ERROR | BF_DONT_ADVERTISE, NetPermissionFlags::None)) {
3345 return false;
3346 }
3347 }
3348 if (options.bind_on_any) {
3349 // Don't consider errors to bind on IPv6 "::" fatal because the host OS
3350 // may not have IPv6 support and the user did not explicitly ask us to
3351 // bind on that.
3352 const CService ipv6_any{in6_addr(IN6ADDR_ANY_INIT), GetListenPort()}; // ::
3353 Bind(ipv6_any, BF_NONE, NetPermissionFlags::None);
3354
3355 struct in_addr inaddr_any;
3356 inaddr_any.s_addr = htonl(INADDR_ANY);
3357 const CService ipv4_any{inaddr_any, GetListenPort()}; // 0.0.0.0
3358 if (!Bind(ipv4_any, BF_REPORT_ERROR, NetPermissionFlags::None)) {
3359 int defaultPort = Params().GetDefaultPort();
3360 // If listening failed and another port than the standard port was specified,
3361 // ask if the user wants to connect via the standard port for the network instead
3362 if (GetListenPort() != defaultPort) {
3363 bool fRet = uiInterface.ThreadSafeQuestion(
3364 strprintf(_("Do you want to use the standard network port for %s (port %s) instead?"), CLIENT_NAME, defaultPort),
3365 strprintf(_("Listen on port %s failed."), GetListenPort()).translated,
3366 "", CClientUIInterface::MSG_INFORMATION | CClientUIInterface::MODAL | CClientUIInterface::BTN_OK | CClientUIInterface::BTN_ABORT);
3367
3368 if (fRet) {
3369 // FIXME: Unbind IPv6 on the other port
3370
3371 gArgs.ForceSetArg("-port", defaultPort);
3372 // Attempt to use standard port
3373 struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
3374 Bind(CService(inaddr6_any, defaultPort), BF_NONE, NetPermissionFlags::None);
3375 struct in_addr inaddr_any;
3376 inaddr_any.s_addr = INADDR_ANY;
3377 if (!Bind(CService(inaddr_any, defaultPort), BF_REPORT_ERROR, NetPermissionFlags::None)) {
3378 return false;
3379 }
3380 }
3381 }
3382 }
3383 }
3384 return true;
3385 }
3386
3387 bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
3388 {
3389 AssertLockNotHeld(m_total_bytes_sent_mutex);
3390 Init(connOptions);
3391
3392 if (fListen && !InitBinds(connOptions)) {
3393 if (m_client_interface) {
3394 m_client_interface->ThreadSafeMessageBox(
3395 _("Failed to listen on any port. Use -listen=0 if you want this."),
3396 "", CClientUIInterface::MSG_ERROR);
3397 }
3398 return false;
3399 }
3400
3401 Proxy i2p_sam;
3402 if (GetProxy(NET_I2P, i2p_sam) && connOptions.m_i2p_accept_incoming) {
3403 m_i2p_sam_session = std::make_unique<i2p::sam::Session>(gArgs.GetDataDirNet() / "i2p_private_key",
3404 i2p_sam, &interruptNet);
3405 }
3406
3407 // Randomize the order in which we may query seednode to potentially prevent connecting to the same one every restart (and signal that we have restarted)
3408 std::vector<std::string> seed_nodes = connOptions.vSeedNodes;
3409 if (!seed_nodes.empty()) {
3410 std::shuffle(seed_nodes.begin(), seed_nodes.end(), FastRandomContext{});
3411 }
3412
3413 if (m_use_addrman_outgoing) {
3414 // Load addresses from anchors.dat
3415 m_anchors = ReadAnchors(gArgs.GetDataDirNet() / ANCHORS_DATABASE_FILENAME);
3416 if (m_anchors.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) {
3417 m_anchors.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS);
3418 }
3419 LogPrintf("%i block-relay-only anchors will be tried for connections.\n", m_anchors.size());
3420 }
3421
3422 if (m_client_interface) {
3423 m_client_interface->InitMessage(_("Starting network threads…"));
3424 }
3425
3426 fAddressesInitialized = true;
3427
3428 if (semOutbound == nullptr) {
3429 // initialize semaphore
3430 semOutbound = std::make_unique<CSemaphore>(std::min(m_max_automatic_outbound, m_max_automatic_connections));
3431 }
3432 if (semAddnode == nullptr) {
3433 // initialize semaphore
3434 semAddnode = std::make_unique<CSemaphore>(m_max_addnode);
3435 }
3436
3437 //
3438 // Start threads
3439 //
3440 assert(m_msgproc);
3441 interruptNet.reset();
3442 flagInterruptMsgProc = false;
3443
3444 {
3445 LOCK(mutexMsgProc);
3446 fMsgProcWake = false;
3447 }
3448
3449 // Send and receive from sockets, accept connections
3450 threadSocketHandler = std::thread(&util::TraceThread, "net", [this] { ThreadSocketHandler(); });
3451
3452 if (!gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED))
3453 LogPrintf("DNS seeding disabled\n");
3454 else
3455 threadDNSAddressSeed = std::thread(&util::TraceThread, "dnsseed", [this] { ThreadDNSAddressSeed(); });
3456
3457 // Initiate manual connections
3458 threadOpenAddedConnections = std::thread(&util::TraceThread, "addcon", [this] { ThreadOpenAddedConnections(); });
3459
3460 if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
3461 if (m_client_interface) {
3462 m_client_interface->ThreadSafeMessageBox(
3463 _("Cannot provide specific connections and have addrman find outgoing connections at the same time."),
3464 "", CClientUIInterface::MSG_ERROR);
3465 }
3466 return false;
3467 }
3468 if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty()) {
3469 threadOpenConnections = std::thread(
3470 &util::TraceThread, "opencon",
3471 [this, connect = connOptions.m_specified_outgoing, seed_nodes = std::move(seed_nodes)] { ThreadOpenConnections(connect, seed_nodes); });
3472 }
3473
3474 // Process messages
3475 threadMessageHandler = std::thread(&util::TraceThread, "msghand", [this] { ThreadMessageHandler(); });
3476
3477 if (m_i2p_sam_session) {
3478 threadI2PAcceptIncoming =
3479 std::thread(&util::TraceThread, "i2paccept", [this] { ThreadI2PAcceptIncoming(); });
3480 }
3481
3482 // Dump network addresses
3483 scheduler.scheduleEvery([this] { DumpAddresses(); }, DUMP_PEERS_INTERVAL);
3484
3485 // Run the ASMap Health check once and then schedule it to run every 24h.
3486 if (m_netgroupman.UsingASMap()) {
3487 ASMapHealthCheck();
3488 scheduler.scheduleEvery([this] { ASMapHealthCheck(); }, ASMAP_HEALTH_CHECK_INTERVAL);
3489 }
3490
3491 return true;
3492 }
3493
3494 class CNetCleanup
3495 {
3496 public:
3497 CNetCleanup() = default;
3498
3499 ~CNetCleanup()
3500 {
3501 #ifdef WIN32
3502 // Shutdown Windows Sockets
3503 WSACleanup();
3504 #endif
3505 }
3506 };
3507 static CNetCleanup instance_of_cnetcleanup;
3508
3509 void CConnman::Interrupt()
3510 {
3511 {
3512 LOCK(mutexMsgProc);
3513 flagInterruptMsgProc = true;
3514 }
3515 condMsgProc.notify_all();
3516
3517 interruptNet();
3518 g_socks5_interrupt();
3519
3520 if (semOutbound) {
3521 for (int i=0; i<m_max_automatic_outbound; i++) {
3522 semOutbound->post();
3523 }
3524 }
3525
3526 if (semAddnode) {
3527 for (int i=0; i<m_max_addnode; i++) {
3528 semAddnode->post();
3529 }
3530 }
3531 }
3532
3533 void CConnman::StopThreads()
3534 {
3535 if (threadI2PAcceptIncoming.joinable()) {
3536 threadI2PAcceptIncoming.join();
3537 }
3538 if (threadMessageHandler.joinable())
3539 threadMessageHandler.join();
3540 if (threadOpenConnections.joinable())
3541 threadOpenConnections.join();
3542 if (threadOpenAddedConnections.joinable())
3543 threadOpenAddedConnections.join();
3544 if (threadDNSAddressSeed.joinable())
3545 threadDNSAddressSeed.join();
3546 if (threadSocketHandler.joinable())
3547 threadSocketHandler.join();
3548 }
3549
3550 void CConnman::StopNodes()
3551 {
3552 AssertLockNotHeld(m_reconnections_mutex);
3553
3554 if (fAddressesInitialized) {
3555 DumpAddresses();
3556 fAddressesInitialized = false;
3557
3558 if (m_use_addrman_outgoing) {
3559 // Anchor connections are only dumped during clean shutdown.
3560 std::vector<CAddress> anchors_to_dump = GetCurrentBlockRelayOnlyConns();
3561 if (anchors_to_dump.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) {
3562 anchors_to_dump.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS);
3563 }
3564 DumpAnchors(gArgs.GetDataDirNet() / ANCHORS_DATABASE_FILENAME, anchors_to_dump);
3565 }
3566 }
3567
3568 // Delete peer connections.
3569 std::vector<CNode*> nodes;
3570 WITH_LOCK(m_nodes_mutex, nodes.swap(m_nodes));
3571 for (CNode* pnode : nodes) {
3572 LogDebug(BCLog::NET, "Stopping node, %s", pnode->DisconnectMsg(fLogIPs));
3573 pnode->CloseSocketDisconnect();
3574 DeleteNode(pnode);
3575 }
3576
3577 for (CNode* pnode : m_nodes_disconnected) {
3578 DeleteNode(pnode);
3579 }
3580 m_nodes_disconnected.clear();
3581 WITH_LOCK(m_reconnections_mutex, m_reconnections.clear());
3582 vhListenSocket.clear();
3583 semOutbound.reset();
3584 semAddnode.reset();
3585 }
3586
3587 void CConnman::DeleteNode(CNode* pnode)
3588 {
3589 assert(pnode);
3590 m_msgproc->FinalizeNode(*pnode);
3591 delete pnode;
3592 }
3593
3594 CConnman::~CConnman()
3595 {
3596 Interrupt();
3597 Stop();
3598 }
3599
3600 std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
3601 {
3602 std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, network, filtered);
3603 if (m_banman) {
3604 addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
3605 [this](const CAddress& addr){return m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr);}),
3606 addresses.end());
3607 }
3608 return addresses;
3609 }
3610
3611 std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addresses, size_t max_pct)
3612 {
3613 auto local_socket_bytes = requestor.addrBind.GetAddrBytes();
3614 uint64_t network_id = requestor.m_network_key;
3615 const auto current_time = GetTime<std::chrono::microseconds>();
3616 auto r = m_addr_response_caches.emplace(network_id, CachedAddrResponse{});
3617 CachedAddrResponse& cache_entry = r.first->second;
3618 if (cache_entry.m_cache_entry_expiration < current_time) { // If emplace() added new one it has expiration 0.
3619 cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct, /*network=*/std::nullopt);
3620 // Choosing a proper cache lifetime is a trade-off between the privacy leak minimization
3621 // and the usefulness of ADDR responses to honest users.
3622 //
3623 // Longer cache lifetime makes it more difficult for an attacker to scrape
3624 // enough AddrMan data to maliciously infer something useful.
3625 // By the time an attacker scraped enough AddrMan records, most of
3626 // the records should be old enough to not leak topology info by
3627 // e.g. analyzing real-time changes in timestamps.
3628 //
3629 // It takes only several hundred requests to scrape everything from an AddrMan containing 100,000 nodes,
3630 // so ~24 hours of cache lifetime indeed makes the data less inferable by the time
3631 // most of it could be scraped (considering that timestamps are updated via
3632 // ADDR self-announcements and when nodes communicate).
3633 // We also should be robust to those attacks which may not require scraping *full* victim's AddrMan
3634 // (because even several timestamps of the same handful of nodes may leak privacy).
3635 //
3636 // On the other hand, longer cache lifetime makes ADDR responses
3637 // outdated and less useful for an honest requestor, e.g. if most nodes
3638 // in the ADDR response are no longer active.
3639 //
3640 // However, the churn in the network is known to be rather low. Since we consider
3641 // nodes to be "terrible" (see IsTerrible()) if the timestamps are older than 30 days,
3642 // max. 24 hours of "penalty" due to cache shouldn't make any meaningful difference
3643 // in terms of the freshness of the response.
3644 cache_entry.m_cache_entry_expiration = current_time +
3645 21h + FastRandomContext().randrange<std::chrono::microseconds>(6h);
3646 }
3647 return cache_entry.m_addrs_response_cache;
3648 }
3649
3650 bool CConnman::AddNode(const AddedNodeParams& add)
3651 {
3652 const CService resolved{MaybeFlipIPv6toCJDNS(LookupNumeric(add.m_added_node, GetDefaultPort(add.m_added_node)))};
3653 const bool resolved_invalid{!resolved.IsValid()};
3654
3655 LOCK(m_added_nodes_mutex);
3656 for (const auto& it : m_added_node_params) {
3657 if (add.m_added_node == it.m_added_node) return false;
3658 if (resolved_invalid) continue;
3659 const CService service{MaybeFlipIPv6toCJDNS(LookupNumeric(it.m_added_node, GetDefaultPort(it.m_added_node)))};
3660 if (resolved == service) return false;
3661 // Check if CJDNS address matches regardless of port to detect already-connected inbound peers.
3662 if (resolved.IsCJDNS() && static_cast<CNetAddr>(resolved) == static_cast<CNetAddr>(service)) return false;
3663 }
3664
3665 m_added_node_params.push_back(add);
3666 return true;
3667 }
3668
3669 bool CConnman::RemoveAddedNode(const std::string& strNode)
3670 {
3671 LOCK(m_added_nodes_mutex);
3672 for (auto it = m_added_node_params.begin(); it != m_added_node_params.end(); ++it) {
3673 if (strNode == it->m_added_node) {
3674 m_added_node_params.erase(it);
3675 return true;
3676 }
3677 }
3678 return false;
3679 }
3680
3681 bool CConnman::AddedNodesContain(const CAddress& addr) const
3682 {
3683 AssertLockNotHeld(m_added_nodes_mutex);
3684 const std::string addr_str{addr.ToStringAddr()};
3685 const std::string addr_port_str{addr.ToStringAddrPort()};
3686 LOCK(m_added_nodes_mutex);
3687 return (m_added_node_params.size() < 24 // bound the query to a reasonable limit
3688 && std::any_of(m_added_node_params.cbegin(), m_added_node_params.cend(),
3689 [&](const auto& p) { return p.m_added_node == addr_str || p.m_added_node == addr_port_str; }));
3690 }
3691
3692 size_t CConnman::GetNodeCount(ConnectionDirection flags) const
3693 {
3694 LOCK(m_nodes_mutex);
3695 if (flags == ConnectionDirection::Both) // Shortcut if we want total
3696 return m_nodes.size();
3697
3698 int nNum = 0;
3699 for (const auto& pnode : m_nodes) {
3700 if (flags & (pnode->IsInboundConn() ? ConnectionDirection::In : ConnectionDirection::Out)) {
3701 nNum++;
3702 }
3703 }
3704
3705 return nNum;
3706 }
3707
3708
3709 std::map<CNetAddr, LocalServiceInfo> CConnman::getNetLocalAddresses() const
3710 {
3711 LOCK(g_maplocalhost_mutex);
3712 return mapLocalHost;
3713 }
3714
3715 uint32_t CConnman::GetMappedAS(const CNetAddr& addr) const
3716 {
3717 return m_netgroupman.GetMappedAS(addr);
3718 }
3719
3720 void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
3721 {
3722 vstats.clear();
3723 LOCK(m_nodes_mutex);
3724 vstats.reserve(m_nodes.size());
3725 for (CNode* pnode : m_nodes) {
3726 vstats.emplace_back();
3727 pnode->CopyStats(vstats.back());
3728 vstats.back().m_mapped_as = GetMappedAS(pnode->addr);
3729 }
3730 }
3731
3732 bool CConnman::DisconnectNode(const std::string& strNode)
3733 {
3734 LOCK(m_nodes_mutex);
3735 if (CNode* pnode = FindNode(strNode)) {
3736 LogDebug(BCLog::NET, "disconnect by address%s match, %s", (fLogIPs ? strprintf("=%s", strNode) : ""), pnode->DisconnectMsg(fLogIPs));
3737 pnode->fDisconnect = true;
3738 return true;
3739 }
3740 return false;
3741 }
3742
3743 bool CConnman::DisconnectNode(const CSubNet& subnet)
3744 {
3745 bool disconnected = false;
3746 LOCK(m_nodes_mutex);
3747 for (CNode* pnode : m_nodes) {
3748 if (subnet.Match(pnode->addr)) {
3749 LogDebug(BCLog::NET, "disconnect by subnet%s match, %s", (fLogIPs ? strprintf("=%s", subnet.ToString()) : ""), pnode->DisconnectMsg(fLogIPs));
3750 pnode->fDisconnect = true;
3751 disconnected = true;
3752 }
3753 }
3754 return disconnected;
3755 }
3756
3757 bool CConnman::DisconnectNode(const CNetAddr& addr)
3758 {
3759 return DisconnectNode(CSubNet(addr));
3760 }
3761
3762 bool CConnman::DisconnectNode(NodeId id)
3763 {
3764 LOCK(m_nodes_mutex);
3765 for(CNode* pnode : m_nodes) {
3766 if (id == pnode->GetId()) {
3767 LogDebug(BCLog::NET, "disconnect by id, %s", pnode->DisconnectMsg(fLogIPs));
3768 pnode->fDisconnect = true;
3769 return true;
3770 }
3771 }
3772 return false;
3773 }
3774
3775 void CConnman::RecordBytesRecv(uint64_t bytes)
3776 {
3777 nTotalBytesRecv += bytes;
3778 }
3779
3780 void CConnman::RecordBytesSent(uint64_t bytes)
3781 {
3782 AssertLockNotHeld(m_total_bytes_sent_mutex);
3783 LOCK(m_total_bytes_sent_mutex);
3784
3785 nTotalBytesSent += bytes;
3786
3787 const auto now = GetTime<std::chrono::seconds>();
3788 if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now)
3789 {
3790 // timeframe expired, reset cycle
3791 nMaxOutboundCycleStartTime = now;
3792 nMaxOutboundTotalBytesSentInCycle = 0;
3793 }
3794
3795 nMaxOutboundTotalBytesSentInCycle += bytes;
3796 }
3797
3798 void CConnman::SetMaxOutboundTarget(uint64_t limit)
3799 {
3800 AssertLockNotHeld(m_total_bytes_sent_mutex);
3801 LOCK(m_total_bytes_sent_mutex);
3802 nMaxOutboundLimit = limit;
3803 }
3804
3805 uint64_t CConnman::GetMaxOutboundTarget() const
3806 {
3807 AssertLockNotHeld(m_total_bytes_sent_mutex);
3808 LOCK(m_total_bytes_sent_mutex);
3809 return nMaxOutboundLimit;
3810 }
3811
3812 std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
3813 {
3814 return MAX_UPLOAD_TIMEFRAME;
3815 }
3816
3817 std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
3818 {
3819 AssertLockNotHeld(m_total_bytes_sent_mutex);
3820 LOCK(m_total_bytes_sent_mutex);
3821 return GetMaxOutboundTimeLeftInCycle_();
3822 }
3823
3824 std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle_() const
3825 {
3826 AssertLockHeld(m_total_bytes_sent_mutex);
3827
3828 if (nMaxOutboundLimit == 0)
3829 return 0s;
3830
3831 if (nMaxOutboundCycleStartTime.count() == 0)
3832 return MAX_UPLOAD_TIMEFRAME;
3833
3834 const std::chrono::seconds cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME;
3835 const auto now = GetTime<std::chrono::seconds>();
3836 return (cycleEndTime < now) ? 0s : cycleEndTime - now;
3837 }
3838
3839 bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
3840 {
3841 AssertLockNotHeld(m_total_bytes_sent_mutex);
3842 LOCK(m_total_bytes_sent_mutex);
3843 if (nMaxOutboundLimit == 0)
3844 return false;
3845
3846 if (historicalBlockServingLimit)
3847 {
3848 // keep a large enough buffer to at least relay each block once
3849 const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle_();
3850 const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MAX_BLOCK_SERIALIZED_SIZE;
3851 if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
3852 return true;
3853 }
3854 else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
3855 return true;
3856
3857 return false;
3858 }
3859
3860 uint64_t CConnman::GetOutboundTargetBytesLeft() const
3861 {
3862 AssertLockNotHeld(m_total_bytes_sent_mutex);
3863 LOCK(m_total_bytes_sent_mutex);
3864 if (nMaxOutboundLimit == 0)
3865 return 0;
3866
3867 return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
3868 }
3869
3870 uint64_t CConnman::GetTotalBytesRecv() const
3871 {
3872 return nTotalBytesRecv;
3873 }
3874
3875 uint64_t CConnman::GetTotalBytesSent() const
3876 {
3877 AssertLockNotHeld(m_total_bytes_sent_mutex);
3878 LOCK(m_total_bytes_sent_mutex);
3879 return nTotalBytesSent;
3880 }
3881
3882 ServiceFlags CConnman::GetLocalServices() const
3883 {
3884 return m_local_services;
3885 }
3886
3887 static std::unique_ptr<Transport> MakeTransport(NodeId id, bool use_v2transport, bool inbound) noexcept
3888 {
3889 if (use_v2transport) {
3890 return std::make_unique<V2Transport>(id, /*initiating=*/!inbound);
3891 } else {
3892 return std::make_unique<V1Transport>(id);
3893 }
3894 }
3895
3896 CNode::CNode(NodeId idIn,
3897 std::shared_ptr<Sock> sock,
3898 const CAddress& addrIn,
3899 uint64_t nKeyedNetGroupIn,
3900 uint64_t nLocalHostNonceIn,
3901 const CService& addrBindIn,
3902 const std::string& addrNameIn,
3903 ConnectionType conn_type_in,
3904 bool inbound_onion,
3905 uint64_t network_key,
3906 CNodeOptions&& node_opts)
3907 : m_transport{MakeTransport(idIn, node_opts.use_v2transport, conn_type_in == ConnectionType::INBOUND)},
3908 m_permission_flags{node_opts.permission_flags},
3909 m_sock{sock},
3910 m_connected{GetTime<std::chrono::seconds>()},
3911 addr{addrIn},
3912 addrBind{addrBindIn},
3913 m_addr_name{addrNameIn.empty() ? addr.ToStringAddrPort() : addrNameIn},
3914 m_dest(addrNameIn),
3915 m_inbound_onion{inbound_onion},
3916 m_prefer_evict{node_opts.prefer_evict},
3917 m_forced_inbound{node_opts.forced_inbound},
3918 nKeyedNetGroup{nKeyedNetGroupIn},
3919 m_network_key{network_key},
3920 m_conn_type{conn_type_in},
3921 id{idIn},
3922 nLocalHostNonce{nLocalHostNonceIn},
3923 m_recv_flood_size{node_opts.recv_flood_size},
3924 m_i2p_sam_session{std::move(node_opts.i2p_sam_session)}
3925 {
3926 if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
3927
3928 for (const auto& msg : ALL_NET_MESSAGE_TYPES) {
3929 mapRecvBytesPerMsgType[msg] = 0;
3930 }
3931 mapRecvBytesPerMsgType[NET_MESSAGE_TYPE_OTHER] = 0;
3932
3933 if (fLogIPs) {
3934 LogDebug(BCLog::NET, "Added connection to %s peer=%d\n", m_addr_name, id);
3935 } else {
3936 LogDebug(BCLog::NET, "Added connection peer=%d\n", id);
3937 }
3938 }
3939
3940 void CNode::MarkReceivedMsgsForProcessing()
3941 {
3942 AssertLockNotHeld(m_msg_process_queue_mutex);
3943
3944 size_t nSizeAdded = 0;
3945 for (const auto& msg : vRecvMsg) {
3946 // vRecvMsg contains only completed CNetMessage
3947 // the single possible partially deserialized message are held by TransportDeserializer
3948 nSizeAdded += msg.GetMemoryUsage();
3949 }
3950
3951 LOCK(m_msg_process_queue_mutex);
3952 m_msg_process_queue.splice(m_msg_process_queue.end(), vRecvMsg);
3953 m_msg_process_queue_size += nSizeAdded;
3954 fPauseRecv = m_msg_process_queue_size > m_recv_flood_size;
3955 }
3956
3957 std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage()
3958 {
3959 LOCK(m_msg_process_queue_mutex);
3960 if (m_msg_process_queue.empty()) return std::nullopt;
3961
3962 std::list<CNetMessage> msgs;
3963 // Just take one message
3964 msgs.splice(msgs.begin(), m_msg_process_queue, m_msg_process_queue.begin());
3965 m_msg_process_queue_size -= msgs.front().GetMemoryUsage();
3966 fPauseRecv = m_msg_process_queue_size > m_recv_flood_size;
3967
3968 return std::make_pair(std::move(msgs.front()), !m_msg_process_queue.empty());
3969 }
3970
3971 bool CConnman::NodeFullyConnected(const CNode* pnode)
3972 {
3973 return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
3974 }
3975
3976 void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
3977 {
3978 AssertLockNotHeld(m_total_bytes_sent_mutex);
3979 size_t nMessageSize = msg.data.size();
3980 LogDebug(BCLog::NET, "sending %s (%d bytes) peer=%d\n", msg.m_type, nMessageSize, pnode->GetId());
3981 if (m_capture_messages) {
3982 CaptureMessage(pnode->addr, msg.m_type, msg.data, /*is_incoming=*/false);
3983 }
3984
3985 TRACEPOINT(net, outbound_message,
3986 pnode->GetId(),
3987 pnode->m_addr_name.c_str(),
3988 pnode->ConnectionTypeAsString().c_str(),
3989 msg.m_type.c_str(),
3990 msg.data.size(),
3991 msg.data.data()
3992 );
3993
3994 size_t nBytesSent = 0;
3995 {
3996 LOCK(pnode->cs_vSend);
3997 // Check if the transport still has unsent bytes, and indicate to it that we're about to
3998 // give it a message to send.
3999 const auto& [to_send, more, _msg_type] =
4000 pnode->m_transport->GetBytesToSend(/*have_next_message=*/true);
4001 const bool queue_was_empty{to_send.empty() && pnode->vSendMsg.empty()};
4002
4003 // Update memory usage of send buffer.
4004 pnode->m_send_memusage += msg.GetMemoryUsage();
4005 if (pnode->m_send_memusage + pnode->m_transport->GetSendMemoryUsage() > nSendBufferMaxSize) pnode->fPauseSend = true;
4006 // Move message to vSendMsg queue.
4007 pnode->vSendMsg.push_back(std::move(msg));
4008
4009 // If there was nothing to send before, and there is now (predicted by the "more" value
4010 // returned by the GetBytesToSend call above), attempt "optimistic write":
4011 // because the poll/select loop may pause for SELECT_TIMEOUT_MILLISECONDS before actually
4012 // doing a send, try sending from the calling thread if the queue was empty before.
4013 // With a V1Transport, more will always be true here, because adding a message always
4014 // results in sendable bytes there, but with V2Transport this is not the case (it may
4015 // still be in the handshake).
4016 if (queue_was_empty && more) {
4017 std::tie(nBytesSent, std::ignore) = SocketSendData(*pnode);
4018 }
4019 }
4020 if (nBytesSent) RecordBytesSent(nBytesSent);
4021 }
4022
4023 bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
4024 {
4025 CNode* found = nullptr;
4026 LOCK(m_nodes_mutex);
4027 for (auto&& pnode : m_nodes) {
4028 if(pnode->GetId() == id) {
4029 found = pnode;
4030 break;
4031 }
4032 }
4033 return found != nullptr && NodeFullyConnected(found) && func(found);
4034 }
4035
4036 CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
4037 {
4038 return CSipHasher(nSeed0, nSeed1).Write(id);
4039 }
4040
4041 uint64_t CConnman::CalculateKeyedNetGroup(const CNetAddr& address) const
4042 {
4043 std::vector<unsigned char> vchNetGroup(m_netgroupman.GetGroup(address));
4044
4045 return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup).Finalize();
4046 }
4047
4048 void CConnman::PerformReconnections()
4049 {
4050 AssertLockNotHeld(m_reconnections_mutex);
4051 AssertLockNotHeld(m_unused_i2p_sessions_mutex);
4052 while (true) {
4053 // Move first element of m_reconnections to todo (avoiding an allocation inside the lock).
4054 decltype(m_reconnections) todo;
4055 {
4056 LOCK(m_reconnections_mutex);
4057 if (m_reconnections.empty()) break;
4058 todo.splice(todo.end(), m_reconnections, m_reconnections.begin());
4059 }
4060
4061 auto& item = *todo.begin();
4062 OpenNetworkConnection(item.addr_connect,
4063 // We only reconnect if the first attempt to connect succeeded at
4064 // connection time, but then failed after the CNode object was
4065 // created. Since we already know connecting is possible, do not
4066 // count failure to reconnect.
4067 /*fCountFailure=*/false,
4068 std::move(item.grant),
4069 item.destination.empty() ? nullptr : item.destination.c_str(),
4070 item.conn_type,
4071 item.use_v2transport);
4072 }
4073 }
4074
4075 void CConnman::ASMapHealthCheck()
4076 {
4077 const std::vector<CAddress> v4_addrs{GetAddresses(/*max_addresses=*/ 0, /*max_pct=*/ 0, Network::NET_IPV4, /*filtered=*/ false)};
4078 const std::vector<CAddress> v6_addrs{GetAddresses(/*max_addresses=*/ 0, /*max_pct=*/ 0, Network::NET_IPV6, /*filtered=*/ false)};
4079 std::vector<CNetAddr> clearnet_addrs;
4080 clearnet_addrs.reserve(v4_addrs.size() + v6_addrs.size());
4081 std::transform(v4_addrs.begin(), v4_addrs.end(), std::back_inserter(clearnet_addrs),
4082 [](const CAddress& addr) { return static_cast<CNetAddr>(addr); });
4083 std::transform(v6_addrs.begin(), v6_addrs.end(), std::back_inserter(clearnet_addrs),
4084 [](const CAddress& addr) { return static_cast<CNetAddr>(addr); });
4085 m_netgroupman.ASMapHealthCheck(clearnet_addrs);
4086 }
4087
4088 // Dump binary message to file, with timestamp.
4089 static void CaptureMessageToFile(const CAddress& addr,
4090 const std::string& msg_type,
4091 Span<const unsigned char> data,
4092 bool is_incoming)
4093 {
4094 // Note: This function captures the message at the time of processing,
4095 // not at socket receive/send time.
4096 // This ensures that the messages are always in order from an application
4097 // layer (processing) perspective.
4098 auto now = GetTime<std::chrono::microseconds>();
4099
4100 // Windows folder names cannot include a colon
4101 std::string clean_addr = addr.ToStringAddrPort();
4102 std::replace(clean_addr.begin(), clean_addr.end(), ':', '_');
4103
4104 fs::path base_path = gArgs.GetDataDirNet() / "message_capture" / fs::u8path(clean_addr);
4105 fs::create_directories(base_path);
4106
4107 fs::path path = base_path / (is_incoming ? "msgs_recv.dat" : "msgs_sent.dat");
4108 AutoFile f{fsbridge::fopen(path, "ab")};
4109
4110 ser_writedata64(f, now.count());
4111 f << Span{msg_type};
4112 for (auto i = msg_type.length(); i < CMessageHeader::MESSAGE_TYPE_SIZE; ++i) {
4113 f << uint8_t{'\0'};
4114 }
4115 uint32_t size = data.size();
4116 ser_writedata32(f, size);
4117 f << data;
4118
4119 if (f.fclose() != 0) {
4120 throw std::ios_base::failure(
4121 strprintf("Error closing %s after write, file contents are likely incomplete", fs::PathToString(path)));
4122 }
4123 }
4124
4125 std::function<void(const CAddress& addr,
4126 const std::string& msg_type,
4127 Span<const unsigned char> data,
4128 bool is_incoming)>
4129 CaptureMessage = CaptureMessageToFile;
4130