Coverage Report

Created: 2026-09-01 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/netaddress.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 <netaddress.h>
7
8
#include <crypto/common.h>
9
#include <crypto/sha3.h>
10
#include <hash.h>
11
#include <prevector.h>
12
#include <tinyformat.h>
13
#include <util/strencodings.h>
14
#include <util/string.h>
15
16
#include <algorithm>
17
#include <array>
18
#include <cstdint>
19
#include <ios>
20
#include <iterator>
21
#include <string_view>
22
#include <tuple>
23
24
using util::ContainsNUL;
25
using util::HasPrefix;
26
27
CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
28
11.9M
{
29
11.9M
    switch (m_net) {
  Branch (29:13): [True: 0, False: 11.9M]
30
2.17M
    case NET_IPV4:
  Branch (30:5): [True: 2.17M, False: 9.75M]
31
2.17M
        return BIP155Network::IPV4;
32
2.82M
    case NET_IPV6:
  Branch (32:5): [True: 2.82M, False: 9.10M]
33
2.82M
        return BIP155Network::IPV6;
34
2.23M
    case NET_ONION:
  Branch (34:5): [True: 2.23M, False: 9.69M]
35
2.23M
        return BIP155Network::TORV3;
36
2.50M
    case NET_I2P:
  Branch (36:5): [True: 2.50M, False: 9.42M]
37
2.50M
        return BIP155Network::I2P;
38
2.18M
    case NET_CJDNS:
  Branch (38:5): [True: 2.18M, False: 9.74M]
39
2.18M
        return BIP155Network::CJDNS;
40
0
    case NET_INTERNAL:   // should have been handled before calling this function
  Branch (40:5): [True: 0, False: 11.9M]
41
0
    case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
  Branch (41:5): [True: 0, False: 11.9M]
42
0
    case NET_MAX:        // m_net is never and should not be set to NET_MAX
  Branch (42:5): [True: 0, False: 11.9M]
43
0
        assert(false);
  Branch (43:9): [Folded - Ignored]
44
11.9M
    } // no default case, so the compiler can warn about missing cases
45
46
11.9M
    assert(false);
  Branch (46:5): [Folded - Ignored]
47
0
}
48
49
bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
50
22.0M
{
51
22.0M
    switch (possible_bip155_net) {
  Branch (51:13): [True: 563k, False: 21.5M]
52
3.47M
    case BIP155Network::IPV4:
  Branch (52:5): [True: 3.47M, False: 18.6M]
53
3.47M
        if (address_size == ADDR_IPV4_SIZE) {
  Branch (53:13): [True: 3.47M, False: 168]
54
3.47M
            m_net = NET_IPV4;
55
3.47M
            return true;
56
3.47M
        }
57
168
        throw std::ios_base::failure(
58
168
            strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
59
168
                      ADDR_IPV4_SIZE));
60
6.64M
    case BIP155Network::IPV6:
  Branch (60:5): [True: 6.64M, False: 15.4M]
61
6.64M
        if (address_size == ADDR_IPV6_SIZE) {
  Branch (61:13): [True: 6.64M, False: 130]
62
6.64M
            m_net = NET_IPV6;
63
6.64M
            return true;
64
6.64M
        }
65
130
        throw std::ios_base::failure(
66
130
            strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
67
130
                      ADDR_IPV6_SIZE));
68
3.48M
    case BIP155Network::TORV3:
  Branch (68:5): [True: 3.48M, False: 18.5M]
69
3.48M
        if (address_size == ADDR_TORV3_SIZE) {
  Branch (69:13): [True: 3.48M, False: 114]
70
3.48M
            m_net = NET_ONION;
71
3.48M
            return true;
72
3.48M
        }
73
114
        throw std::ios_base::failure(
74
114
            strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
75
114
                      ADDR_TORV3_SIZE));
76
4.44M
    case BIP155Network::I2P:
  Branch (76:5): [True: 4.44M, False: 17.6M]
77
4.44M
        if (address_size == ADDR_I2P_SIZE) {
  Branch (77:13): [True: 4.43M, False: 113]
78
4.43M
            m_net = NET_I2P;
79
4.43M
            return true;
80
4.43M
        }
81
113
        throw std::ios_base::failure(
82
113
            strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
83
113
                      ADDR_I2P_SIZE));
84
3.48M
    case BIP155Network::CJDNS:
  Branch (84:5): [True: 3.48M, False: 18.5M]
85
3.48M
        if (address_size == ADDR_CJDNS_SIZE) {
  Branch (85:13): [True: 3.48M, False: 76]
86
3.48M
            m_net = NET_CJDNS;
87
3.48M
            return true;
88
3.48M
        }
89
76
        throw std::ios_base::failure(
90
76
            strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
91
76
                      ADDR_CJDNS_SIZE));
92
22.0M
    }
93
94
    // Don't throw on addresses with unknown network ids (maybe from the future).
95
    // Instead silently drop them and have the unserialization code consume
96
    // subsequent ones which may be known to us.
97
563k
    return false;
98
22.0M
}
99
100
/**
101
 * Construct an unspecified IPv6 network address (::/128).
102
 *
103
 * @note This address is considered invalid by CNetAddr::IsValid()
104
 */
105
59.8M
CNetAddr::CNetAddr() = default;
106
107
void CNetAddr::SetIP(const CNetAddr& ipIn)
108
1.24k
{
109
    // Size check.
110
1.24k
    switch (ipIn.m_net) {
  Branch (110:13): [True: 0, False: 1.24k]
111
163
    case NET_IPV4:
  Branch (111:5): [True: 163, False: 1.07k]
112
163
        assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
  Branch (112:9): [True: 163, False: 0]
113
163
        break;
114
902
    case NET_IPV6:
  Branch (114:5): [True: 902, False: 340]
115
902
        assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
  Branch (115:9): [True: 902, False: 0]
116
902
        break;
117
902
    case NET_ONION:
  Branch (117:5): [True: 32, False: 1.21k]
118
32
        assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
  Branch (118:9): [True: 32, False: 0]
119
32
        break;
120
32
    case NET_I2P:
  Branch (120:5): [True: 5, False: 1.23k]
121
5
        assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
  Branch (121:9): [True: 5, False: 0]
122
5
        break;
123
9
    case NET_CJDNS:
  Branch (123:5): [True: 9, False: 1.23k]
124
9
        assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
  Branch (124:9): [True: 9, False: 0]
125
9
        break;
126
131
    case NET_INTERNAL:
  Branch (126:5): [True: 131, False: 1.11k]
127
131
        assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
  Branch (127:9): [True: 131, False: 0]
128
131
        break;
129
131
    case NET_UNROUTABLE:
  Branch (129:5): [True: 0, False: 1.24k]
130
0
    case NET_MAX:
  Branch (130:5): [True: 0, False: 1.24k]
131
0
        assert(false);
  Branch (131:9): [Folded - Ignored]
132
1.24k
    } // no default case, so the compiler can warn about missing cases
133
134
1.24k
    m_net = ipIn.m_net;
135
1.24k
    m_addr = ipIn.m_addr;
136
1.24k
}
137
138
void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
139
523k
{
140
523k
    assert(ipv6.size() == ADDR_IPV6_SIZE);
  Branch (140:5): [True: 523k, False: 0]
141
142
523k
    size_t skip{0};
143
144
523k
    if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
  Branch (144:9): [True: 2.79k, False: 520k]
145
        // IPv4-in-IPv6
146
2.79k
        m_net = NET_IPV4;
147
2.79k
        skip = sizeof(IPV4_IN_IPV6_PREFIX);
148
520k
    } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
  Branch (148:16): [True: 2.03k, False: 518k]
149
        // TORv2-in-IPv6 (unsupported). Unserialize as !IsValid(), thus ignoring them.
150
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
151
        // will not be gossiped, but continue reading next addresses from the stream.
152
2.03k
        m_net = NET_IPV6;
153
2.03k
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
154
2.03k
        return;
155
518k
    } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
  Branch (155:16): [True: 1.81k, False: 516k]
156
        // Internal-in-IPv6
157
1.81k
        m_net = NET_INTERNAL;
158
1.81k
        skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
159
516k
    } else {
160
        // IPv6
161
516k
        m_net = NET_IPV6;
162
516k
    }
163
164
521k
    m_addr.assign(ipv6.begin() + skip, ipv6.end());
165
521k
}
166
167
/**
168
 * Create an "internal" address that represents a name or FQDN. AddrMan uses
169
 * these fake addresses to keep track of which DNS seeds were used.
170
 * @returns Whether or not the operation was successful.
171
 * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
172
 */
173
bool CNetAddr::SetInternal(const std::string &name)
174
1.55M
{
175
1.55M
    if (name.empty()) {
  Branch (175:9): [True: 46, False: 1.55M]
176
46
        return false;
177
46
    }
178
1.55M
    m_net = NET_INTERNAL;
179
1.55M
    unsigned char hash[32] = {};
180
1.55M
    CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
181
1.55M
    m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
182
1.55M
    return true;
183
1.55M
}
184
185
namespace torv3 {
186
// https://gitlab.torproject.org/tpo/core/torspec/-/tree/main/spec/rend-spec
187
static constexpr size_t CHECKSUM_LEN = 2;
188
static const unsigned char VERSION[] = {3};
189
static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
190
191
static void Checksum(std::span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
192
45.2k
{
193
    // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
194
45.2k
    static const unsigned char prefix[] = ".onion checksum";
195
45.2k
    static constexpr size_t prefix_len = 15;
196
197
45.2k
    SHA3_256 hasher;
198
199
45.2k
    hasher.Write(std::span{prefix}.first(prefix_len));
200
45.2k
    hasher.Write(addr_pubkey);
201
45.2k
    hasher.Write(VERSION);
202
203
45.2k
    uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
204
205
45.2k
    hasher.Finalize(checksum_full);
206
207
45.2k
    memcpy(checksum, checksum_full, sizeof(checksum));
208
45.2k
}
209
210
}; // namespace torv3
211
212
bool CNetAddr::SetSpecial(std::string_view addr)
213
230k
{
214
230k
    if (ContainsNUL(addr)) {
  Branch (214:9): [True: 83.4k, False: 146k]
215
83.4k
        return false;
216
83.4k
    }
217
218
146k
    if (SetTor(addr)) {
  Branch (218:9): [True: 3.35k, False: 143k]
219
3.35k
        return true;
220
3.35k
    }
221
222
143k
    if (SetI2P(addr)) {
  Branch (222:9): [True: 10.2k, False: 133k]
223
10.2k
        return true;
224
10.2k
    }
225
226
133k
    return false;
227
143k
}
228
229
bool CNetAddr::SetTor(std::string_view addr)
230
146k
{
231
146k
    if (!addr.ends_with(".onion")) return false;
  Branch (231:9): [True: 133k, False: 13.5k]
232
13.5k
    addr.remove_suffix(6);
233
13.5k
    auto input = DecodeBase32(addr);
234
235
13.5k
    if (!input) {
  Branch (235:9): [True: 9.30k, False: 4.26k]
236
9.30k
        return false;
237
9.30k
    }
238
239
4.26k
    if (input->size() == torv3::TOTAL_LEN) {
  Branch (239:9): [True: 3.99k, False: 271]
240
3.99k
        std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
241
3.99k
        std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
242
3.99k
        std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
243
244
3.99k
        if (!std::ranges::equal(input_version, torv3::VERSION)) {
  Branch (244:13): [True: 259, False: 3.73k]
245
259
            return false;
246
259
        }
247
248
3.73k
        uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
249
3.73k
        torv3::Checksum(input_pubkey, calculated_checksum);
250
251
3.73k
        if (!std::ranges::equal(input_checksum, calculated_checksum)) {
  Branch (251:13): [True: 378, False: 3.35k]
252
378
            return false;
253
378
        }
254
255
3.35k
        m_net = NET_ONION;
256
3.35k
        m_addr.assign(input_pubkey.begin(), input_pubkey.end());
257
3.35k
        return true;
258
3.73k
    }
259
260
271
    return false;
261
4.26k
}
262
263
bool CNetAddr::SetI2P(std::string_view addr)
264
143k
{
265
    // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
266
143k
    static constexpr size_t b32_len{52};
267
143k
    static const char* suffix{".b32.i2p"};
268
143k
    static constexpr size_t suffix_len{8};
269
270
143k
    if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
  Branch (270:9): [True: 132k, False: 11.1k]
  Branch (270:9): [True: 132k, False: 10.6k]
  Branch (270:48): [True: 520, False: 10.6k]
271
132k
        return false;
272
132k
    }
273
274
    // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
275
    // can decode it.
276
10.6k
    const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
277
278
10.6k
    auto address_bytes = DecodeBase32(b32_padded);
279
280
10.6k
    if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
  Branch (280:9): [True: 286, False: 10.3k]
  Branch (280:27): [True: 85, False: 10.2k]
281
371
        return false;
282
371
    }
283
284
10.2k
    m_net = NET_I2P;
285
10.2k
    m_addr.assign(address_bytes->begin(), address_bytes->end());
286
287
10.2k
    return true;
288
10.6k
}
289
290
CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
291
25.7k
{
292
25.7k
    m_net = NET_IPV4;
293
25.7k
    const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
294
25.7k
    m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
295
25.7k
}
296
297
CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
298
27.1k
{
299
27.1k
    SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
300
27.1k
    m_scope_id = scope;
301
27.1k
}
302
303
bool CNetAddr::IsBindAny() const
304
374
{
305
374
    if (!IsIPv4() && !IsIPv6()) {
  Branch (305:9): [True: 281, False: 93]
  Branch (305:22): [True: 177, False: 104]
306
177
        return false;
307
177
    }
308
490
    return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
309
374
}
310
311
bool CNetAddr::IsRFC1918() const
312
317M
{
313
317M
    return IsIPv4() && (
  Branch (313:12): [True: 64.0M, False: 253M]
314
64.0M
        m_addr[0] == 10 ||
  Branch (314:9): [True: 48.6k, False: 64.0M]
315
64.0M
        (m_addr[0] == 192 && m_addr[1] == 168) ||
  Branch (315:10): [True: 2.50M, False: 61.5M]
  Branch (315:30): [True: 11.5k, False: 2.48M]
316
64.0M
        (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
  Branch (316:10): [True: 226k, False: 63.7M]
  Branch (316:30): [True: 185k, False: 40.6k]
  Branch (316:49): [True: 21.9k, False: 163k]
317
317M
}
318
319
bool CNetAddr::IsRFC2544() const
320
317M
{
321
317M
    return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
  Branch (321:12): [True: 63.9M, False: 253M]
  Branch (321:24): [True: 311k, False: 63.6M]
  Branch (321:45): [True: 20.1k, False: 291k]
  Branch (321:64): [True: 26.0k, False: 265k]
322
317M
}
323
324
bool CNetAddr::IsRFC3927() const
325
317M
{
326
317M
    return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
  Branch (326:12): [True: 63.9M, False: 253M]
  Branch (326:24): [True: 11.2k, False: 63.9M]
327
317M
}
328
329
bool CNetAddr::IsRFC6598() const
330
317M
{
331
317M
    return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
  Branch (331:12): [True: 63.9M, False: 253M]
  Branch (331:24): [True: 415k, False: 63.4M]
  Branch (331:44): [True: 307k, False: 107k]
  Branch (331:63): [True: 16.8k, False: 291k]
332
317M
}
333
334
bool CNetAddr::IsRFC5737() const
335
317M
{
336
317M
    return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
  Branch (336:12): [True: 63.8M, False: 253M]
  Branch (336:25): [True: 1.03M, False: 62.8M]
337
63.8M
                        HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
  Branch (337:25): [True: 15.3k, False: 62.8M]
338
63.8M
                        HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
  Branch (338:25): [True: 15.4k, False: 62.8M]
339
317M
}
340
341
bool CNetAddr::IsRFC3849() const
342
345M
{
343
345M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
  Branch (343:12): [True: 98.6M, False: 246M]
  Branch (343:24): [True: 17.6k, False: 98.5M]
344
345M
}
345
346
bool CNetAddr::IsRFC3964() const
347
103M
{
348
103M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
  Branch (348:12): [True: 36.8M, False: 66.8M]
  Branch (348:24): [True: 27.5k, False: 36.7M]
349
103M
}
350
351
bool CNetAddr::IsRFC6052() const
352
103M
{
353
103M
    return IsIPv6() &&
  Branch (353:12): [True: 36.8M, False: 66.8M]
354
103M
           HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
  Branch (354:12): [True: 43.2k, False: 36.8M]
355
36.8M
                                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
356
103M
}
357
358
bool CNetAddr::IsRFC4380() const
359
108M
{
360
108M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
  Branch (360:12): [True: 37.5M, False: 71.3M]
  Branch (360:24): [True: 37.7k, False: 37.5M]
361
108M
}
362
363
bool CNetAddr::IsRFC4862() const
364
317M
{
365
317M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
  Branch (365:12): [True: 90.7M, False: 226M]
  Branch (365:24): [True: 12.5k, False: 90.7M]
366
90.7M
                                                                0x00, 0x00, 0x00, 0x00});
367
317M
}
368
369
bool CNetAddr::IsRFC4193() const
370
316M
{
371
316M
    return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
  Branch (371:12): [True: 90.7M, False: 225M]
  Branch (371:24): [True: 191k, False: 90.5M]
372
316M
}
373
374
bool CNetAddr::IsRFC6145() const
375
103M
{
376
103M
    return IsIPv6() &&
  Branch (376:12): [True: 36.8M, False: 66.8M]
377
103M
           HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  Branch (377:12): [True: 35.1k, False: 36.8M]
378
36.8M
                                                     0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
379
103M
}
380
381
bool CNetAddr::IsRFC4843() const
382
316M
{
383
316M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
  Branch (383:12): [True: 90.5M, False: 225M]
  Branch (383:24): [True: 119k, False: 90.4M]
384
316M
           (m_addr[3] & 0xF0) == 0x10;
  Branch (384:12): [True: 9.98k, False: 109k]
385
316M
}
386
387
bool CNetAddr::IsRFC7343() const
388
316M
{
389
316M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
  Branch (389:12): [True: 90.5M, False: 225M]
  Branch (389:24): [True: 109k, False: 90.4M]
390
316M
           (m_addr[3] & 0xF0) == 0x20;
  Branch (390:12): [True: 13.1k, False: 96.4k]
391
316M
}
392
393
bool CNetAddr::IsHeNet() const
394
6.19M
{
395
6.19M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
  Branch (395:12): [True: 6.19M, False: 0]
  Branch (395:24): [True: 7.05k, False: 6.19M]
396
6.19M
}
397
398
bool CNetAddr::IsLocal() const
399
351M
{
400
    // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
401
351M
    if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
  Branch (401:9): [True: 69.5M, False: 281M]
  Branch (401:22): [True: 132k, False: 69.4M]
  Branch (401:42): [True: 927k, False: 68.5M]
402
1.06M
        return true;
403
1.06M
    }
404
405
    // IPv6 loopback (::1/128)
406
350M
    static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
407
350M
    if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
  Branch (407:9): [True: 99.3M, False: 250M]
  Branch (407:21): [True: 81.1k, False: 99.2M]
408
81.1k
        return true;
409
81.1k
    }
410
411
350M
    return false;
412
350M
}
413
414
/**
415
 * @returns Whether or not this network address is a valid address that @a could
416
 *          be used to refer to an actual host.
417
 *
418
 * @note A valid address may or may not be publicly routable on the global
419
 *       internet. As in, the set of valid addresses is a superset of the set of
420
 *       publicly routable addresses.
421
 *
422
 * @see CNetAddr::IsRoutable()
423
 */
424
bool CNetAddr::IsValid() const
425
362M
{
426
    // unspecified IPv6 address (::/128)
427
362M
    unsigned char ipNone6[16] = {};
428
362M
    if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
  Branch (428:9): [True: 115M, False: 246M]
  Branch (428:21): [True: 17.1M, False: 98.6M]
429
17.1M
        return false;
430
17.1M
    }
431
432
345M
    if (IsCJDNS() && !HasCJDNSPrefix()) {
  Branch (432:9): [True: 54.4M, False: 290M]
  Branch (432:22): [True: 1.72k, False: 54.4M]
433
1.72k
        return false;
434
1.72k
    }
435
436
    // documentation IPv6 address
437
345M
    if (IsRFC3849())
  Branch (437:9): [True: 17.6k, False: 345M]
438
17.6k
        return false;
439
440
345M
    if (IsInternal())
  Branch (440:9): [True: 2.17M, False: 343M]
441
2.17M
        return false;
442
443
343M
    if (IsIPv4()) {
  Branch (443:9): [True: 68.8M, False: 274M]
444
68.8M
        const uint32_t addr = ReadBE32(m_addr.data());
445
68.8M
        if (addr == INADDR_ANY || addr == INADDR_NONE) {
  Branch (445:13): [True: 581k, False: 68.2M]
  Branch (445:35): [True: 112k, False: 68.1M]
446
693k
            return false;
447
693k
        }
448
68.8M
    }
449
450
342M
    return true;
451
343M
}
452
453
/**
454
 * @returns Whether or not this network address is publicly routable on the
455
 *          global internet.
456
 *
457
 * @note A routable address is always valid. As in, the set of routable addresses
458
 *       is a subset of the set of valid addresses.
459
 *
460
 * @see CNetAddr::IsValid()
461
 */
462
bool CNetAddr::IsRoutable() const
463
319M
{
464
319M
    return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
  Branch (464:12): [True: 317M, False: 1.61M]
  Branch (464:27): [True: 82.1k, False: 317M]
  Branch (464:42): [True: 46.1k, False: 317M]
  Branch (464:57): [True: 11.2k, False: 317M]
  Branch (464:72): [True: 12.5k, False: 317M]
  Branch (464:87): [True: 16.8k, False: 317M]
  Branch (464:102): [True: 1.06M, False: 316M]
  Branch (464:117): [True: 191k, False: 316M]
  Branch (464:132): [True: 9.98k, False: 316M]
  Branch (464:147): [True: 13.1k, False: 316M]
  Branch (464:162): [True: 843k, False: 315M]
  Branch (464:175): [True: 0, False: 315M]
465
319M
}
466
467
/**
468
 * @returns Whether or not this is a dummy address that represents a name.
469
 *
470
 * @see CNetAddr::SetInternal(const std::string &)
471
 */
472
bool CNetAddr::IsInternal() const
473
865M
{
474
865M
   return m_net == NET_INTERNAL;
475
865M
}
476
477
bool CNetAddr::IsAddrV1Compatible() const
478
95.2M
{
479
95.2M
    switch (m_net) {
  Branch (479:13): [True: 0, False: 95.2M]
480
10.8M
    case NET_IPV4:
  Branch (480:5): [True: 10.8M, False: 84.3M]
481
38.7M
    case NET_IPV6:
  Branch (481:5): [True: 27.8M, False: 67.3M]
482
38.9M
    case NET_INTERNAL:
  Branch (482:5): [True: 157k, False: 95.1M]
483
38.9M
        return true;
484
17.7M
    case NET_ONION:
  Branch (484:5): [True: 17.7M, False: 77.5M]
485
38.4M
    case NET_I2P:
  Branch (485:5): [True: 20.7M, False: 74.5M]
486
56.3M
    case NET_CJDNS:
  Branch (486:5): [True: 17.8M, False: 77.4M]
487
56.3M
        return false;
488
0
    case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
  Branch (488:5): [True: 0, False: 95.2M]
489
0
    case NET_MAX:        // m_net is never and should not be set to NET_MAX
  Branch (489:5): [True: 0, False: 95.2M]
490
0
        assert(false);
  Branch (490:9): [Folded - Ignored]
491
95.2M
    } // no default case, so the compiler can warn about missing cases
492
493
95.2M
    assert(false);
  Branch (493:5): [Folded - Ignored]
494
0
}
495
496
enum Network CNetAddr::GetNetwork() const
497
67.5M
{
498
67.5M
    if (IsInternal())
  Branch (498:9): [True: 1.83M, False: 65.7M]
499
1.83M
        return NET_INTERNAL;
500
501
65.7M
    if (!IsRoutable())
  Branch (501:9): [True: 1.11M, False: 64.5M]
502
1.11M
        return NET_UNROUTABLE;
503
504
64.5M
    return m_net;
505
65.7M
}
506
507
static std::string IPv4ToString(std::span<const uint8_t> a)
508
80.3k
{
509
80.3k
    return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
510
80.3k
}
511
512
// Return an IPv6 address text representation with zero compression as described in RFC 5952
513
// ("A Recommendation for IPv6 Address Text Representation").
514
static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
515
788k
{
516
788k
    assert(a.size() == ADDR_IPV6_SIZE);
  Branch (516:5): [True: 788k, False: 0]
517
788k
    const std::array groups{
518
788k
        ReadBE16(&a[0]),
519
788k
        ReadBE16(&a[2]),
520
788k
        ReadBE16(&a[4]),
521
788k
        ReadBE16(&a[6]),
522
788k
        ReadBE16(&a[8]),
523
788k
        ReadBE16(&a[10]),
524
788k
        ReadBE16(&a[12]),
525
788k
        ReadBE16(&a[14]),
526
788k
    };
527
528
    // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
529
    // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
530
788k
    struct ZeroSpan {
531
788k
        size_t start_index{0};
532
788k
        size_t len{0};
533
788k
    };
534
535
    // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
536
    // zero sequences of equal length are found.
537
788k
    ZeroSpan longest, current;
538
7.10M
    for (size_t i{0}; i < groups.size(); ++i) {
  Branch (538:23): [True: 6.31M, False: 788k]
539
6.31M
        if (groups[i] != 0) {
  Branch (539:13): [True: 5.73M, False: 577k]
540
5.73M
            current = {i + 1, 0};
541
5.73M
            continue;
542
5.73M
        }
543
577k
        current.len += 1;
544
577k
        if (current.len > longest.len) {
  Branch (544:13): [True: 546k, False: 30.8k]
545
546k
            longest = current;
546
546k
        }
547
577k
    }
548
549
788k
    std::string r;
550
788k
    r.reserve(39);
551
7.10M
    for (size_t i{0}; i < groups.size(); ++i) {
  Branch (551:23): [True: 6.31M, False: 788k]
552
        // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
553
6.31M
        if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
  Branch (553:13): [True: 718k, False: 5.59M]
  Branch (553:33): [True: 606k, False: 112k]
  Branch (553:61): [True: 499k, False: 107k]
554
499k
            if (i == longest.start_index) {
  Branch (554:17): [True: 89.8k, False: 409k]
555
89.8k
                r += "::";
556
89.8k
            }
557
499k
            continue;
558
499k
        }
559
5.81M
        r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
  Branch (559:34): [True: 5.07M, False: 732k]
  Branch (559:48): [True: 5.04M, False: 31.2k]
560
5.81M
    }
561
562
788k
    if (scope_id != 0) {
  Branch (562:9): [True: 14, False: 788k]
563
14
        r += strprintf("%%%u", scope_id);
564
14
    }
565
566
788k
    return r;
567
788k
}
568
569
std::string OnionToString(std::span<const uint8_t> addr)
570
41.4k
{
571
41.4k
    uint8_t checksum[torv3::CHECKSUM_LEN];
572
41.4k
    torv3::Checksum(addr, checksum);
573
    // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
574
41.4k
    prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
575
41.4k
    address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
576
41.4k
    address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
577
41.4k
    return EncodeBase32(address) + ".onion";
578
41.4k
}
579
580
std::string CNetAddr::ToStringAddr() const
581
1.04M
{
582
1.04M
    switch (m_net) {
  Branch (582:13): [True: 0, False: 1.04M]
583
80.3k
    case NET_IPV4:
  Branch (583:5): [True: 80.3k, False: 967k]
584
80.3k
        return IPv4ToString(m_addr);
585
761k
    case NET_IPV6:
  Branch (585:5): [True: 761k, False: 287k]
586
761k
        return IPv6ToString(m_addr, m_scope_id);
587
41.4k
    case NET_ONION:
  Branch (587:5): [True: 41.4k, False: 1.00M]
588
41.4k
        return OnionToString(m_addr);
589
105k
    case NET_I2P:
  Branch (589:5): [True: 105k, False: 943k]
590
105k
        return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
591
27.7k
    case NET_CJDNS:
  Branch (591:5): [True: 27.7k, False: 1.02M]
592
27.7k
        return IPv6ToString(m_addr, 0);
593
32.3k
    case NET_INTERNAL:
  Branch (593:5): [True: 32.3k, False: 1.01M]
594
32.3k
        return EncodeBase32(m_addr) + ".internal";
595
0
    case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
  Branch (595:5): [True: 0, False: 1.04M]
596
0
    case NET_MAX:        // m_net is never and should not be set to NET_MAX
  Branch (596:5): [True: 0, False: 1.04M]
597
0
        assert(false);
  Branch (597:9): [Folded - Ignored]
598
1.04M
    } // no default case, so the compiler can warn about missing cases
599
600
1.04M
    assert(false);
  Branch (600:5): [Folded - Ignored]
601
0
}
602
603
bool operator==(const CNetAddr& a, const CNetAddr& b)
604
66.3M
{
605
66.3M
    return a.m_net == b.m_net && a.m_addr == b.m_addr;
  Branch (605:12): [True: 44.8M, False: 21.4M]
  Branch (605:34): [True: 28.8M, False: 16.0M]
606
66.3M
}
607
608
bool operator<(const CNetAddr& a, const CNetAddr& b)
609
31.6M
{
610
31.6M
    return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
611
31.6M
}
612
613
/**
614
 * Try to get our IPv4 address.
615
 *
616
 * @param[out] pipv4Addr The in_addr struct to which to copy.
617
 *
618
 * @returns Whether or not the operation was successful, in particular, whether
619
 *          or not our address was an IPv4 address.
620
 *
621
 * @see CNetAddr::IsIPv4()
622
 */
623
bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
624
8.50k
{
625
8.50k
    if (!IsIPv4())
  Branch (625:9): [True: 0, False: 8.50k]
626
0
        return false;
627
8.50k
    assert(sizeof(*pipv4Addr) == m_addr.size());
  Branch (627:5): [True: 8.50k, False: 0]
628
8.50k
    memcpy(pipv4Addr, m_addr.data(), m_addr.size());
629
8.50k
    return true;
630
8.50k
}
631
632
/**
633
 * Try to get our IPv6 (or CJDNS) address.
634
 *
635
 * @param[out] pipv6Addr The in6_addr struct to which to copy.
636
 *
637
 * @returns Whether or not the operation was successful, in particular, whether
638
 *          or not our address was an IPv6 address.
639
 *
640
 * @see CNetAddr::IsIPv6()
641
 */
642
bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
643
24.4k
{
644
24.4k
    if (!IsIPv6() && !IsCJDNS()) {
  Branch (644:9): [True: 3.40k, False: 21.0k]
  Branch (644:22): [True: 0, False: 3.40k]
645
0
        return false;
646
0
    }
647
24.4k
    assert(sizeof(*pipv6Addr) == m_addr.size());
  Branch (647:5): [True: 24.4k, False: 0]
648
24.4k
    memcpy(pipv6Addr, m_addr.data(), m_addr.size());
649
24.4k
    return true;
650
24.4k
}
651
652
bool CNetAddr::HasLinkedIPv4() const
653
121M
{
654
121M
    return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
  Branch (654:12): [True: 121M, False: 0]
  Branch (654:29): [True: 23.4M, False: 98.2M]
  Branch (654:41): [True: 25.0k, False: 98.2M]
  Branch (654:56): [True: 32.2k, False: 98.2M]
  Branch (654:71): [True: 19.4k, False: 98.2M]
  Branch (654:86): [True: 25.7k, False: 98.1M]
655
121M
}
656
657
uint32_t CNetAddr::GetLinkedIPv4() const
658
7.24M
{
659
7.24M
    if (IsIPv4()) {
  Branch (659:9): [True: 7.21M, False: 36.7k]
660
7.21M
        return ReadBE32(m_addr.data());
661
7.21M
    } else if (IsRFC6052() || IsRFC6145()) {
  Branch (661:16): [True: 10.7k, False: 25.9k]
  Branch (661:31): [True: 9.23k, False: 16.7k]
662
        // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
663
20.0k
        return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
664
20.0k
    } else if (IsRFC3964()) {
  Branch (664:16): [True: 7.00k, False: 9.71k]
665
        // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
666
7.00k
        return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
667
9.71k
    } else if (IsRFC4380()) {
  Branch (667:16): [True: 9.71k, False: 0]
668
        // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
669
9.71k
        return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
670
9.71k
    }
671
7.24M
    assert(false);
  Branch (671:5): [Folded - Ignored]
672
0
}
673
674
Network CNetAddr::GetNetClass() const
675
90.1M
{
676
    // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
677
678
    // Check for "internal" first because such addresses are also !IsRoutable()
679
    // and we don't want to return NET_UNROUTABLE in that case.
680
90.1M
    if (IsInternal()) {
  Branch (680:9): [True: 3.24M, False: 86.9M]
681
3.24M
        return NET_INTERNAL;
682
3.24M
    }
683
86.9M
    if (!IsRoutable()) {
  Branch (683:9): [True: 2.36M, False: 84.5M]
684
2.36M
        return NET_UNROUTABLE;
685
2.36M
    }
686
84.5M
    if (HasLinkedIPv4()) {
  Branch (686:9): [True: 16.3M, False: 68.2M]
687
16.3M
        return NET_IPV4;
688
16.3M
    }
689
68.2M
    return m_net;
690
84.5M
}
691
692
std::vector<unsigned char> CNetAddr::GetAddrBytes() const
693
95.2M
{
694
95.2M
    if (IsAddrV1Compatible()) {
  Branch (694:9): [True: 38.9M, False: 56.3M]
695
38.9M
        uint8_t serialized[V1_SERIALIZATION_SIZE];
696
38.9M
        SerializeV1Array(serialized);
697
38.9M
        return {std::begin(serialized), std::end(serialized)};
698
38.9M
    }
699
56.3M
    return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
700
95.2M
}
701
702
// private extensions to enum Network, only returned by GetExtNetwork,
703
// and only used in GetReachabilityFrom
704
static const int NET_TEREDO = NET_MAX;
705
int static GetExtNetwork(const CNetAddr& addr)
706
10.7M
{
707
10.7M
    if (addr.IsRFC4380())
  Branch (707:9): [True: 2.34k, False: 10.7M]
708
2.34k
        return NET_TEREDO;
709
10.7M
    return addr.GetNetwork();
710
10.7M
}
711
712
/** Calculates a metric for how reachable (*this) is from a given partner */
713
int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
714
5.35M
{
715
5.35M
    enum Reachability {
716
5.35M
        REACH_UNREACHABLE,
717
5.35M
        REACH_DEFAULT,
718
5.35M
        REACH_TEREDO,
719
5.35M
        REACH_IPV6_WEAK,
720
5.35M
        REACH_IPV4,
721
5.35M
        REACH_IPV6_STRONG,
722
5.35M
        REACH_PRIVATE
723
5.35M
    };
724
725
5.35M
    if (!IsRoutable() || IsInternal())
  Branch (725:9): [True: 191, False: 5.35M]
  Branch (725:26): [True: 0, False: 5.35M]
726
191
        return REACH_UNREACHABLE;
727
728
5.35M
    int ourNet = GetExtNetwork(*this);
729
5.35M
    int theirNet = GetExtNetwork(paddrPartner);
730
5.35M
    bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
  Branch (730:20): [True: 1.03k, False: 5.35M]
  Branch (730:35): [True: 268, False: 5.35M]
  Branch (730:50): [True: 860, False: 5.35M]
731
732
5.35M
    switch(theirNet) {
733
1.94M
    case NET_IPV4:
  Branch (733:5): [True: 1.94M, False: 3.41M]
734
1.94M
        switch(ourNet) {
735
1.33M
        default:       return REACH_DEFAULT;
  Branch (735:9): [True: 1.33M, False: 608k]
736
608k
        case NET_IPV4: return REACH_IPV4;
  Branch (736:9): [True: 608k, False: 1.33M]
737
1.94M
        }
738
804k
    case NET_IPV6:
  Branch (738:5): [True: 804k, False: 4.55M]
739
804k
        switch(ourNet) {
740
117k
        default:         return REACH_DEFAULT;
  Branch (740:9): [True: 117k, False: 687k]
741
354
        case NET_TEREDO: return REACH_TEREDO;
  Branch (741:9): [True: 354, False: 803k]
742
286k
        case NET_IPV4:   return REACH_IPV4;
  Branch (742:9): [True: 286k, False: 517k]
743
400k
        case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
  Branch (743:9): [True: 400k, False: 403k]
  Branch (743:33): [True: 1.54k, False: 399k]
744
804k
        }
745
2.56k
    case NET_ONION:
  Branch (745:5): [True: 2.56k, False: 5.35M]
746
2.56k
        switch(ourNet) {
747
2
        default:         return REACH_DEFAULT;
  Branch (747:9): [True: 2, False: 2.56k]
748
0
        case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
  Branch (748:9): [True: 0, False: 2.56k]
749
2.56k
        case NET_ONION:    return REACH_PRIVATE;
  Branch (749:9): [True: 2.56k, False: 2]
750
2.56k
        }
751
3.31k
    case NET_I2P:
  Branch (751:5): [True: 3.31k, False: 5.35M]
752
3.31k
        switch (ourNet) {
753
2.08k
        case NET_I2P: return REACH_PRIVATE;
  Branch (753:9): [True: 2.08k, False: 1.22k]
754
1.22k
        default: return REACH_DEFAULT;
  Branch (754:9): [True: 1.22k, False: 2.08k]
755
3.31k
        }
756
46.9k
    case NET_CJDNS:
  Branch (756:5): [True: 46.9k, False: 5.30M]
757
46.9k
        switch (ourNet) {
758
735
        case NET_CJDNS: return REACH_PRIVATE;
  Branch (758:9): [True: 735, False: 46.2k]
759
46.2k
        default: return REACH_DEFAULT;
  Branch (759:9): [True: 46.2k, False: 735]
760
46.9k
        }
761
1.94k
    case NET_TEREDO:
  Branch (761:5): [True: 1.94k, False: 5.35M]
762
1.94k
        switch(ourNet) {
763
53
        default:          return REACH_DEFAULT;
  Branch (763:9): [True: 53, False: 1.89k]
764
7
        case NET_TEREDO:  return REACH_TEREDO;
  Branch (764:9): [True: 7, False: 1.93k]
765
747
        case NET_IPV6:    return REACH_IPV6_WEAK;
  Branch (765:9): [True: 747, False: 1.19k]
766
1.13k
        case NET_IPV4:    return REACH_IPV4;
  Branch (766:9): [True: 1.13k, False: 807]
767
1.94k
        }
768
723k
    case NET_UNROUTABLE:
  Branch (768:5): [True: 723k, False: 4.63M]
769
2.55M
    default:
  Branch (769:5): [True: 1.83M, False: 3.52M]
770
2.55M
        switch(ourNet) {
771
54.9k
        default:          return REACH_DEFAULT;
  Branch (771:9): [True: 54.9k, False: 2.50M]
772
36
        case NET_TEREDO:  return REACH_TEREDO;
  Branch (772:9): [True: 36, False: 2.55M]
773
1.57M
        case NET_IPV6:    return REACH_IPV6_WEAK;
  Branch (773:9): [True: 1.57M, False: 981k]
774
926k
        case NET_IPV4:    return REACH_IPV4;
  Branch (774:9): [True: 926k, False: 1.62M]
775
26
        case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
  Branch (775:9): [True: 26, False: 2.55M]
776
2.55M
        }
777
5.35M
    }
778
5.35M
}
779
780
23.6M
CService::CService() : port(0)
781
23.6M
{
782
23.6M
}
783
784
10.4M
CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
785
10.4M
{
786
10.4M
}
787
788
5.78k
CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
789
5.78k
{
790
5.78k
}
791
792
6.10k
CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
793
6.10k
{
794
6.10k
}
795
796
435
CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
797
435
{
798
435
    assert(addr.sin_family == AF_INET);
  Branch (798:5): [True: 435, False: 0]
799
435
}
800
801
1.92k
CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
802
1.92k
{
803
1.92k
   assert(addr.sin6_family == AF_INET6);
  Branch (803:4): [True: 1.92k, False: 0]
804
1.92k
}
805
806
bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
807
2.87k
{
808
2.87k
    switch (paddr->sa_family) {
809
439
    case AF_INET:
  Branch (809:5): [True: 439, False: 2.43k]
810
439
        if (addrlen != sizeof(struct sockaddr_in)) return false;
  Branch (810:13): [True: 4, False: 435]
811
435
        *this = CService(*(const struct sockaddr_in*)paddr);
812
435
        return true;
813
1.93k
    case AF_INET6:
  Branch (813:5): [True: 1.93k, False: 946]
814
1.93k
        if (addrlen != sizeof(struct sockaddr_in6)) return false;
  Branch (814:13): [True: 5, False: 1.92k]
815
1.92k
        *this = CService(*(const struct sockaddr_in6*)paddr);
816
1.92k
        return true;
817
507
    default:
  Branch (817:5): [True: 507, False: 2.37k]
818
507
        return false;
819
2.87k
    }
820
2.87k
}
821
822
sa_family_t CService::GetSAFamily() const
823
33.6k
{
824
33.6k
    switch (m_net) {
825
8.08k
    case NET_IPV4:
  Branch (825:5): [True: 8.08k, False: 25.5k]
826
8.08k
        return AF_INET;
827
20.9k
    case NET_IPV6:
  Branch (827:5): [True: 20.9k, False: 12.7k]
828
24.3k
    case NET_CJDNS:
  Branch (828:5): [True: 3.40k, False: 30.2k]
829
24.3k
        return AF_INET6;
830
1.25k
    default:
  Branch (830:5): [True: 1.25k, False: 32.3k]
831
1.25k
        return AF_UNSPEC;
832
33.6k
    }
833
33.6k
}
834
835
uint16_t CService::GetPort() const
836
129k
{
837
129k
    return port;
838
129k
}
839
840
bool operator==(const CService& a, const CService& b)
841
42.8M
{
842
42.8M
    return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
  Branch (842:12): [True: 24.0M, False: 18.7M]
  Branch (842:68): [True: 24.0M, False: 7.03k]
843
42.8M
}
844
845
bool operator<(const CService& a, const CService& b)
846
28.1M
{
847
28.1M
    return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
  Branch (847:12): [True: 14.7M, False: 13.3M]
  Branch (847:68): [True: 4.59M, False: 8.74M]
  Branch (847:124): [True: 1.21M, False: 3.37M]
848
28.1M
}
849
850
/**
851
 * Obtain the IPv4/6 socket address this represents.
852
 *
853
 * @param[out] paddr The obtained socket address.
854
 * @param[in,out] addrlen The size, in bytes, of the address structure pointed
855
 *                        to by paddr. The value that's pointed to by this
856
 *                        parameter might change after calling this function if
857
 *                        the size of the corresponding address structure
858
 *                        changed.
859
 *
860
 * @returns Whether or not the operation was successful.
861
 */
862
bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
863
64.1k
{
864
64.1k
    if (IsIPv4()) {
  Branch (864:9): [True: 8.37k, False: 55.7k]
865
8.37k
        if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
  Branch (865:13): [True: 0, False: 8.37k]
866
0
            return false;
867
8.37k
        *addrlen = sizeof(struct sockaddr_in);
868
8.37k
        struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
869
8.37k
        memset(paddrin, 0, *addrlen);
870
8.37k
        if (!GetInAddr(&paddrin->sin_addr))
  Branch (870:13): [True: 0, False: 8.37k]
871
0
            return false;
872
8.37k
        paddrin->sin_family = AF_INET;
873
8.37k
        paddrin->sin_port = htons(port);
874
8.37k
        return true;
875
8.37k
    }
876
55.7k
    if (IsIPv6() || IsCJDNS()) {
  Branch (876:9): [True: 21.0k, False: 34.7k]
  Branch (876:21): [True: 3.40k, False: 31.3k]
877
24.4k
        if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
  Branch (877:13): [True: 0, False: 24.4k]
878
0
            return false;
879
24.4k
        *addrlen = sizeof(struct sockaddr_in6);
880
24.4k
        struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
881
24.4k
        memset(paddrin6, 0, *addrlen);
882
24.4k
        if (!GetIn6Addr(&paddrin6->sin6_addr))
  Branch (882:13): [True: 0, False: 24.4k]
883
0
            return false;
884
24.4k
        paddrin6->sin6_scope_id = m_scope_id;
885
24.4k
        paddrin6->sin6_family = AF_INET6;
886
24.4k
        paddrin6->sin6_port = htons(port);
887
24.4k
        return true;
888
24.4k
    }
889
31.3k
    return false;
890
55.7k
}
891
892
/**
893
 * @returns An identifier unique to this service's address and port number.
894
 */
895
std::vector<unsigned char> CService::GetKey() const
896
53.8M
{
897
53.8M
    auto key = GetAddrBytes();
898
53.8M
    key.push_back(port / 0x100); // most significant byte of our port
899
53.8M
    key.push_back(port & 0x0FF); // least significant byte of our port
900
53.8M
    return key;
901
53.8M
}
902
903
std::string CService::ToStringAddrPort() const
904
211k
{
905
211k
    const auto port_str = strprintf("%u", port);
906
907
211k
    if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
  Branch (907:9): [True: 43.7k, False: 168k]
  Branch (907:21): [True: 16.2k, False: 151k]
  Branch (907:32): [True: 6.93k, False: 145k]
  Branch (907:43): [True: 30.4k, False: 114k]
908
97.4k
        return ToStringAddr() + ":" + port_str;
909
114k
    } else {
910
114k
        return "[" + ToStringAddr() + "]:" + port_str;
911
114k
    }
912
211k
}
913
914
CSubNet::CSubNet():
915
189k
    valid(false)
916
189k
{
917
189k
    memset(netmask, 0, sizeof(netmask));
918
189k
}
919
920
121k
CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
921
121k
{
922
121k
    valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
  Branch (922:14): [True: 3.91k, False: 117k]
  Branch (922:31): [True: 2.57k, False: 1.34k]
923
121k
            (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
  Branch (923:14): [True: 20.3k, False: 98.3k]
  Branch (923:31): [True: 17.8k, False: 2.50k]
924
121k
    if (!valid) {
  Branch (924:9): [True: 100k, False: 20.3k]
925
100k
        return;
926
100k
    }
927
928
121k
    assert(mask <= sizeof(netmask) * 8);
  Branch (928:5): [True: 20.3k, False: 0]
929
930
20.3k
    network = addr;
931
932
20.3k
    uint8_t n = mask;
933
315k
    for (size_t i = 0; i < network.m_addr.size(); ++i) {
  Branch (933:24): [True: 295k, False: 20.3k]
934
295k
        const uint8_t bits = n < 8 ? n : 8;
  Branch (934:30): [True: 101k, False: 194k]
935
295k
        netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
936
295k
        network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
937
295k
        n -= bits;
938
295k
    }
939
20.3k
}
940
941
/**
942
 * @returns The number of 1-bits in the prefix of the specified subnet mask. If
943
 *          the specified subnet mask is not a valid one, -1.
944
 */
945
static inline int NetmaskBits(uint8_t x)
946
10.3M
{
947
10.3M
    switch(x) {
948
1.89k
    case 0x00: return 0;
  Branch (948:5): [True: 1.89k, False: 10.3M]
949
2.33k
    case 0x80: return 1;
  Branch (949:5): [True: 2.33k, False: 10.3M]
950
1.06k
    case 0xc0: return 2;
  Branch (950:5): [True: 1.06k, False: 10.3M]
951
1.35k
    case 0xe0: return 3;
  Branch (951:5): [True: 1.35k, False: 10.3M]
952
1.24k
    case 0xf0: return 4;
  Branch (952:5): [True: 1.24k, False: 10.3M]
953
625
    case 0xf8: return 5;
  Branch (953:5): [True: 625, False: 10.3M]
954
420
    case 0xfc: return 6;
  Branch (954:5): [True: 420, False: 10.3M]
955
558
    case 0xfe: return 7;
  Branch (955:5): [True: 558, False: 10.3M]
956
10.3M
    case 0xff: return 8;
  Branch (956:5): [True: 10.3M, False: 9.92k]
957
429
    default: return -1;
  Branch (957:5): [True: 429, False: 10.3M]
958
10.3M
    }
959
10.3M
}
960
961
1.58k
CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
962
1.58k
{
963
1.58k
    valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
  Branch (963:14): [True: 1.24k, False: 343]
  Branch (963:31): [True: 156, False: 187]
  Branch (963:49): [True: 1.29k, False: 108]
964
1.58k
    if (!valid) {
  Branch (964:9): [True: 295, False: 1.29k]
965
295
        return;
966
295
    }
967
    // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
968
1.29k
    bool zeros_found = false;
969
4.30k
    for (auto b : mask.m_addr) {
  Branch (969:17): [True: 4.30k, False: 359]
970
4.30k
        const int num_bits = NetmaskBits(b);
971
4.30k
        if (num_bits == -1 || (zeros_found && num_bits != 0)) {
  Branch (971:13): [True: 429, False: 3.87k]
  Branch (971:32): [True: 1.64k, False: 2.23k]
  Branch (971:47): [True: 505, False: 1.13k]
972
934
            valid = false;
973
934
            return;
974
934
        }
975
3.36k
        if (num_bits < 8) {
  Branch (975:13): [True: 2.09k, False: 1.27k]
976
2.09k
            zeros_found = true;
977
2.09k
        }
978
3.36k
    }
979
980
1.29k
    assert(mask.m_addr.size() <= sizeof(netmask));
  Branch (980:5): [True: 359, False: 0]
981
982
359
    memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
983
984
359
    network = addr;
985
986
    // Normalize network according to netmask
987
2.23k
    for (size_t x = 0; x < network.m_addr.size(); ++x) {
  Branch (987:24): [True: 1.88k, False: 359]
988
1.88k
        network.m_addr[x] &= netmask[x];
989
1.88k
    }
990
359
}
991
992
38.4k
CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
993
38.4k
{
994
38.4k
    switch (addr.m_net) {
  Branch (994:13): [True: 0, False: 38.4k]
995
4.98k
    case NET_IPV4:
  Branch (995:5): [True: 4.98k, False: 33.4k]
996
24.8k
    case NET_IPV6:
  Branch (996:5): [True: 19.8k, False: 18.5k]
997
24.8k
        valid = true;
998
24.8k
        assert(addr.m_addr.size() <= sizeof(netmask));
  Branch (998:9): [True: 24.8k, False: 0]
999
24.8k
        memset(netmask, 0xFF, addr.m_addr.size());
1000
24.8k
        break;
1001
1.53k
    case NET_ONION:
  Branch (1001:5): [True: 1.53k, False: 36.9k]
1002
7.15k
    case NET_I2P:
  Branch (1002:5): [True: 5.62k, False: 32.8k]
1003
8.88k
    case NET_CJDNS:
  Branch (1003:5): [True: 1.73k, False: 36.7k]
1004
8.88k
        valid = true;
1005
8.88k
        break;
1006
4.72k
    case NET_INTERNAL:
  Branch (1006:5): [True: 4.72k, False: 33.7k]
1007
4.72k
    case NET_UNROUTABLE:
  Branch (1007:5): [True: 0, False: 38.4k]
1008
4.72k
    case NET_MAX:
  Branch (1008:5): [True: 0, False: 38.4k]
1009
4.72k
        return;
1010
38.4k
    }
1011
1012
33.7k
    network = addr;
1013
33.7k
}
1014
1015
/**
1016
 * @returns True if this subnet is valid, the specified address is valid, and
1017
 *          the specified address belongs in this subnet.
1018
 */
1019
bool CSubNet::Match(const CNetAddr &addr) const
1020
2.94M
{
1021
2.94M
    if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
  Branch (1021:9): [True: 2.51M, False: 429k]
  Branch (1021:19): [True: 26.6k, False: 403k]
  Branch (1021:38): [True: 83.1k, False: 319k]
1022
2.62M
        return false;
1023
1024
319k
    switch (network.m_net) {
  Branch (1024:13): [True: 0, False: 319k]
1025
1.62k
    case NET_IPV4:
  Branch (1025:5): [True: 1.62k, False: 318k]
1026
316k
    case NET_IPV6:
  Branch (1026:5): [True: 314k, False: 5.14k]
1027
316k
        break;
1028
932
    case NET_ONION:
  Branch (1028:5): [True: 932, False: 319k]
1029
2.44k
    case NET_I2P:
  Branch (1029:5): [True: 1.51k, False: 318k]
1030
3.52k
    case NET_CJDNS:
  Branch (1030:5): [True: 1.07k, False: 318k]
1031
3.52k
    case NET_INTERNAL:
  Branch (1031:5): [True: 0, False: 319k]
1032
3.52k
        return addr == network;
1033
0
    case NET_UNROUTABLE:
  Branch (1033:5): [True: 0, False: 319k]
1034
0
    case NET_MAX:
  Branch (1034:5): [True: 0, False: 319k]
1035
0
        return false;
1036
319k
    }
1037
1038
319k
    assert(network.m_addr.size() == addr.m_addr.size());
  Branch (1038:5): [True: 316k, False: 0]
1039
533k
    for (size_t x = 0; x < addr.m_addr.size(); ++x) {
  Branch (1039:24): [True: 525k, False: 7.77k]
1040
525k
        if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
  Branch (1040:13): [True: 308k, False: 217k]
1041
308k
            return false;
1042
308k
        }
1043
525k
    }
1044
7.77k
    return true;
1045
316k
}
1046
1047
std::string CSubNet::ToString() const
1048
816k
{
1049
816k
    std::string suffix;
1050
1051
816k
    switch (network.m_net) {
  Branch (1051:13): [True: 0, False: 816k]
1052
34.3k
    case NET_IPV4:
  Branch (1052:5): [True: 34.3k, False: 782k]
1053
676k
    case NET_IPV6: {
  Branch (1053:5): [True: 641k, False: 175k]
1054
676k
        assert(network.m_addr.size() <= sizeof(netmask));
  Branch (1054:9): [True: 676k, False: 0]
1055
1056
676k
        uint8_t cidr = 0;
1057
1058
10.9M
        for (size_t i = 0; i < network.m_addr.size(); ++i) {
  Branch (1058:28): [True: 10.3M, False: 666k]
1059
10.3M
            if (netmask[i] == 0x00) {
  Branch (1059:17): [True: 9.71k, False: 10.3M]
1060
9.71k
                break;
1061
9.71k
            }
1062
10.3M
            cidr += NetmaskBits(netmask[i]);
1063
10.3M
        }
1064
1065
676k
        suffix = strprintf("/%u", cidr);
1066
676k
        break;
1067
676k
    }
1068
23.9k
    case NET_ONION:
  Branch (1068:5): [True: 23.9k, False: 792k]
1069
119k
    case NET_I2P:
  Branch (1069:5): [True: 95.2k, False: 721k]
1070
140k
    case NET_CJDNS:
  Branch (1070:5): [True: 21.7k, False: 795k]
1071
140k
    case NET_INTERNAL:
  Branch (1071:5): [True: 0, False: 816k]
1072
140k
    case NET_UNROUTABLE:
  Branch (1072:5): [True: 0, False: 816k]
1073
140k
    case NET_MAX:
  Branch (1073:5): [True: 0, False: 816k]
1074
140k
        break;
1075
816k
    }
1076
1077
816k
    return network.ToStringAddr() + suffix;
1078
816k
}
1079
1080
bool CSubNet::IsValid() const
1081
929k
{
1082
929k
    return valid;
1083
929k
}
1084
1085
bool operator==(const CSubNet& a, const CSubNet& b)
1086
4.77k
{
1087
4.77k
    return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
  Branch (1087:12): [True: 4.77k, False: 0]
  Branch (1087:34): [True: 4.77k, False: 0]
  Branch (1087:60): [True: 4.77k, False: 0]
1088
4.77k
}
1089
1090
bool operator<(const CSubNet& a, const CSubNet& b)
1091
349k
{
1092
349k
    return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
  Branch (1092:13): [True: 214k, False: 134k]
  Branch (1092:39): [True: 44.8k, False: 89.6k]
  Branch (1092:65): [True: 1.06k, False: 43.8k]
1093
349k
}