Coverage Report

Created: 2026-07-29 23:27

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