Coverage Report

Created: 2026-08-25 19:22

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::ContainsNoNUL;
25
using util::HasPrefix;
26
27
CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
28
21.5M
{
29
21.5M
    switch (m_net) {
  Branch (29:13): [True: 0, False: 21.5M]
30
3.92M
    case NET_IPV4:
  Branch (30:5): [True: 3.92M, False: 17.6M]
31
3.92M
        return BIP155Network::IPV4;
32
5.11M
    case NET_IPV6:
  Branch (32:5): [True: 5.11M, False: 16.4M]
33
5.11M
        return BIP155Network::IPV6;
34
3.98M
    case NET_ONION:
  Branch (34:5): [True: 3.98M, False: 17.5M]
35
3.98M
        return BIP155Network::TORV3;
36
4.57M
    case NET_I2P:
  Branch (36:5): [True: 4.57M, False: 16.9M]
37
4.57M
        return BIP155Network::I2P;
38
3.95M
    case NET_CJDNS:
  Branch (38:5): [True: 3.95M, False: 17.6M]
39
3.95M
        return BIP155Network::CJDNS;
40
0
    case NET_INTERNAL:   // should have been handled before calling this function
  Branch (40:5): [True: 0, False: 21.5M]
41
0
    case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
  Branch (41:5): [True: 0, False: 21.5M]
42
0
    case NET_MAX:        // m_net is never and should not be set to NET_MAX
  Branch (42:5): [True: 0, False: 21.5M]
43
0
        assert(false);
  Branch (43:9): [Folded - Ignored]
44
21.5M
    } // no default case, so the compiler can warn about missing cases
45
46
21.5M
    assert(false);
  Branch (46:5): [Folded - Ignored]
47
0
}
48
49
bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
50
40.7M
{
51
40.7M
    switch (possible_bip155_net) {
  Branch (51:13): [True: 1.06M, False: 39.7M]
52
6.22M
    case BIP155Network::IPV4:
  Branch (52:5): [True: 6.22M, False: 34.5M]
53
6.22M
        if (address_size == ADDR_IPV4_SIZE) {
  Branch (53:13): [True: 6.22M, False: 299]
54
6.22M
            m_net = NET_IPV4;
55
6.22M
            return true;
56
6.22M
        }
57
299
        throw std::ios_base::failure(
58
299
            strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
59
299
                      ADDR_IPV4_SIZE));
60
12.7M
    case BIP155Network::IPV6:
  Branch (60:5): [True: 12.7M, False: 28.0M]
61
12.7M
        if (address_size == ADDR_IPV6_SIZE) {
  Branch (61:13): [True: 12.7M, False: 254]
62
12.7M
            m_net = NET_IPV6;
63
12.7M
            return true;
64
12.7M
        }
65
254
        throw std::ios_base::failure(
66
254
            strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
67
254
                      ADDR_IPV6_SIZE));
68
6.23M
    case BIP155Network::TORV3:
  Branch (68:5): [True: 6.23M, False: 34.5M]
69
6.23M
        if (address_size == ADDR_TORV3_SIZE) {
  Branch (69:13): [True: 6.23M, False: 184]
70
6.23M
            m_net = NET_ONION;
71
6.23M
            return true;
72
6.23M
        }
73
184
        throw std::ios_base::failure(
74
184
            strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
75
184
                      ADDR_TORV3_SIZE));
76
8.22M
    case BIP155Network::I2P:
  Branch (76:5): [True: 8.22M, False: 32.5M]
77
8.22M
        if (address_size == ADDR_I2P_SIZE) {
  Branch (77:13): [True: 8.22M, False: 204]
78
8.22M
            m_net = NET_I2P;
79
8.22M
            return true;
80
8.22M
        }
81
204
        throw std::ios_base::failure(
82
204
            strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
83
204
                      ADDR_I2P_SIZE));
84
6.28M
    case BIP155Network::CJDNS:
  Branch (84:5): [True: 6.28M, False: 34.5M]
85
6.28M
        if (address_size == ADDR_CJDNS_SIZE) {
  Branch (85:13): [True: 6.28M, False: 167]
86
6.28M
            m_net = NET_CJDNS;
87
6.28M
            return true;
88
6.28M
        }
89
167
        throw std::ios_base::failure(
90
167
            strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
91
167
                      ADDR_CJDNS_SIZE));
92
40.7M
    }
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
1.06M
    return false;
98
40.7M
}
99
100
/**
101
 * Construct an unspecified IPv6 network address (::/128).
102
 *
103
 * @note This address is considered invalid by CNetAddr::IsValid()
104
 */
105
107M
CNetAddr::CNetAddr() = default;
106
107
void CNetAddr::SetIP(const CNetAddr& ipIn)
108
1.66k
{
109
    // Size check.
110
1.66k
    switch (ipIn.m_net) {
  Branch (110:13): [True: 0, False: 1.66k]
111
269
    case NET_IPV4:
  Branch (111:5): [True: 269, False: 1.39k]
112
269
        assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
  Branch (112:9): [True: 269, False: 0]
113
269
        break;
114
1.19k
    case NET_IPV6:
  Branch (114:5): [True: 1.19k, False: 473]
115
1.19k
        assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
  Branch (115:9): [True: 1.19k, False: 0]
116
1.19k
        break;
117
1.19k
    case NET_ONION:
  Branch (117:5): [True: 36, False: 1.63k]
118
36
        assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
  Branch (118:9): [True: 36, False: 0]
119
36
        break;
120
36
    case NET_I2P:
  Branch (120:5): [True: 12, False: 1.65k]
121
12
        assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
  Branch (121:9): [True: 12, False: 0]
122
12
        break;
123
16
    case NET_CJDNS:
  Branch (123:5): [True: 16, False: 1.65k]
124
16
        assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
  Branch (124:9): [True: 16, False: 0]
125
16
        break;
126
140
    case NET_INTERNAL:
  Branch (126:5): [True: 140, False: 1.52k]
127
140
        assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
  Branch (127:9): [True: 140, False: 0]
128
140
        break;
129
140
    case NET_UNROUTABLE:
  Branch (129:5): [True: 0, False: 1.66k]
130
0
    case NET_MAX:
  Branch (130:5): [True: 0, False: 1.66k]
131
0
        assert(false);
  Branch (131:9): [Folded - Ignored]
132
1.66k
    } // no default case, so the compiler can warn about missing cases
133
134
1.66k
    m_net = ipIn.m_net;
135
1.66k
    m_addr = ipIn.m_addr;
136
1.66k
}
137
138
void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
139
1.09M
{
140
1.09M
    assert(ipv6.size() == ADDR_IPV6_SIZE);
  Branch (140:5): [True: 1.09M, False: 0]
141
142
1.09M
    size_t skip{0};
143
144
1.09M
    if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
  Branch (144:9): [True: 5.87k, False: 1.09M]
145
        // IPv4-in-IPv6
146
5.87k
        m_net = NET_IPV4;
147
5.87k
        skip = sizeof(IPV4_IN_IPV6_PREFIX);
148
1.09M
    } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
  Branch (148:16): [True: 5.08k, False: 1.08M]
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
5.08k
        m_net = NET_IPV6;
153
5.08k
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
154
5.08k
        return;
155
1.08M
    } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
  Branch (155:16): [True: 4.59k, False: 1.08M]
156
        // Internal-in-IPv6
157
4.59k
        m_net = NET_INTERNAL;
158
4.59k
        skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
159
1.08M
    } else {
160
        // IPv6
161
1.08M
        m_net = NET_IPV6;
162
1.08M
    }
163
164
1.09M
    m_addr.assign(ipv6.begin() + skip, ipv6.end());
165
1.09M
}
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
2.63M
{
175
2.63M
    if (name.empty()) {
  Branch (175:9): [True: 82, False: 2.63M]
176
82
        return false;
177
82
    }
178
2.63M
    m_net = NET_INTERNAL;
179
2.63M
    unsigned char hash[32] = {};
180
2.63M
    CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
181
2.63M
    m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
182
2.63M
    return true;
183
2.63M
}
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
74.7k
{
193
    // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
194
74.7k
    static const unsigned char prefix[] = ".onion checksum";
195
74.7k
    static constexpr size_t prefix_len = 15;
196
197
74.7k
    SHA3_256 hasher;
198
199
74.7k
    hasher.Write(std::span{prefix}.first(prefix_len));
200
74.7k
    hasher.Write(addr_pubkey);
201
74.7k
    hasher.Write(VERSION);
202
203
74.7k
    uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
204
205
74.7k
    hasher.Finalize(checksum_full);
206
207
74.7k
    memcpy(checksum, checksum_full, sizeof(checksum));
208
74.7k
}
209
210
}; // namespace torv3
211
212
bool CNetAddr::SetSpecial(std::string_view addr)
213
368k
{
214
368k
    if (!ContainsNoNUL(addr)) {
  Branch (214:9): [True: 99.5k, False: 269k]
215
99.5k
        return false;
216
99.5k
    }
217
218
269k
    if (SetTor(addr)) {
  Branch (218:9): [True: 6.09k, False: 262k]
219
6.09k
        return true;
220
6.09k
    }
221
222
262k
    if (SetI2P(addr)) {
  Branch (222:9): [True: 16.3k, False: 246k]
223
16.3k
        return true;
224
16.3k
    }
225
226
246k
    return false;
227
262k
}
228
229
bool CNetAddr::SetTor(std::string_view addr)
230
269k
{
231
269k
    if (!addr.ends_with(".onion")) return false;
  Branch (231:9): [True: 231k, False: 37.8k]
232
37.8k
    addr.remove_suffix(6);
233
37.8k
    auto input = DecodeBase32(addr);
234
235
37.8k
    if (!input) {
  Branch (235:9): [True: 30.6k, False: 7.14k]
236
30.6k
        return false;
237
30.6k
    }
238
239
7.14k
    if (input->size() == torv3::TOTAL_LEN) {
  Branch (239:9): [True: 6.80k, False: 344]
240
6.80k
        std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
241
6.80k
        std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
242
6.80k
        std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
243
244
6.80k
        if (!std::ranges::equal(input_version, torv3::VERSION)) {
  Branch (244:13): [True: 295, False: 6.50k]
245
295
            return false;
246
295
        }
247
248
6.50k
        uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
249
6.50k
        torv3::Checksum(input_pubkey, calculated_checksum);
250
251
6.50k
        if (!std::ranges::equal(input_checksum, calculated_checksum)) {
  Branch (251:13): [True: 414, False: 6.09k]
252
414
            return false;
253
414
        }
254
255
6.09k
        m_net = NET_ONION;
256
6.09k
        m_addr.assign(input_pubkey.begin(), input_pubkey.end());
257
6.09k
        return true;
258
6.50k
    }
259
260
344
    return false;
261
7.14k
}
262
263
bool CNetAddr::SetI2P(std::string_view addr)
264
262k
{
265
    // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
266
262k
    static constexpr size_t b32_len{52};
267
262k
    static const char* suffix{".b32.i2p"};
268
262k
    static constexpr size_t suffix_len{8};
269
270
262k
    if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
  Branch (270:9): [True: 245k, False: 17.9k]
  Branch (270:9): [True: 245k, False: 17.0k]
  Branch (270:48): [True: 907, False: 17.0k]
271
245k
        return false;
272
245k
    }
273
274
    // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
275
    // can decode it.
276
17.0k
    const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
277
278
17.0k
    auto address_bytes = DecodeBase32(b32_padded);
279
280
17.0k
    if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
  Branch (280:9): [True: 534, False: 16.4k]
  Branch (280:27): [True: 91, False: 16.3k]
281
625
        return false;
282
625
    }
283
284
16.3k
    m_net = NET_I2P;
285
16.3k
    m_addr.assign(address_bytes->begin(), address_bytes->end());
286
287
16.3k
    return true;
288
17.0k
}
289
290
CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
291
49.8k
{
292
49.8k
    m_net = NET_IPV4;
293
49.8k
    const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
294
49.8k
    m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
295
49.8k
}
296
297
CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
298
48.1k
{
299
48.1k
    SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
300
48.1k
    m_scope_id = scope;
301
48.1k
}
302
303
bool CNetAddr::IsBindAny() const
304
558
{
305
558
    if (!IsIPv4() && !IsIPv6()) {
  Branch (305:9): [True: 410, False: 148]
  Branch (305:22): [True: 204, False: 206]
306
204
        return false;
307
204
    }
308
947
    return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
309
558
}
310
311
bool CNetAddr::IsRFC1918() const
312
593M
{
313
593M
    return IsIPv4() && (
  Branch (313:12): [True: 114M, False: 478M]
314
114M
        m_addr[0] == 10 ||
  Branch (314:9): [True: 83.1k, False: 114M]
315
114M
        (m_addr[0] == 192 && m_addr[1] == 168) ||
  Branch (315:10): [True: 4.03M, False: 110M]
  Branch (315:30): [True: 17.4k, False: 4.01M]
316
114M
        (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
  Branch (316:10): [True: 453k, False: 114M]
  Branch (316:30): [True: 369k, False: 84.2k]
  Branch (316:49): [True: 41.6k, False: 327k]
317
593M
}
318
319
bool CNetAddr::IsRFC2544() const
320
592M
{
321
592M
    return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
  Branch (321:12): [True: 114M, False: 478M]
  Branch (321:24): [True: 609k, False: 113M]
  Branch (321:45): [True: 28.8k, False: 581k]
  Branch (321:64): [True: 60.8k, False: 520k]
322
592M
}
323
324
bool CNetAddr::IsRFC3927() const
325
592M
{
326
592M
    return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
  Branch (326:12): [True: 114M, False: 478M]
  Branch (326:24): [True: 25.8k, False: 114M]
327
592M
}
328
329
bool CNetAddr::IsRFC6598() const
330
592M
{
331
592M
    return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
  Branch (331:12): [True: 114M, False: 478M]
  Branch (331:24): [True: 978k, False: 113M]
  Branch (331:44): [True: 789k, False: 189k]
  Branch (331:63): [True: 38.3k, False: 751k]
332
592M
}
333
334
bool CNetAddr::IsRFC5737() const
335
592M
{
336
592M
    return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
  Branch (336:12): [True: 114M, False: 478M]
  Branch (336:25): [True: 1.60M, False: 112M]
337
114M
                        HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
  Branch (337:25): [True: 35.4k, False: 112M]
338
114M
                        HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
  Branch (338:25): [True: 30.5k, False: 112M]
339
592M
}
340
341
bool CNetAddr::IsRFC3849() const
342
642M
{
343
642M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
  Branch (343:12): [True: 195M, False: 447M]
  Branch (343:24): [True: 28.8k, False: 195M]
344
642M
}
345
346
bool CNetAddr::IsRFC3964() const
347
195M
{
348
195M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
  Branch (348:12): [True: 72.1M, False: 123M]
  Branch (348:24): [True: 50.4k, False: 72.0M]
349
195M
}
350
351
bool CNetAddr::IsRFC6052() const
352
195M
{
353
195M
    return IsIPv6() &&
  Branch (353:12): [True: 72.2M, False: 123M]
354
195M
           HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
  Branch (354:12): [True: 73.8k, False: 72.1M]
355
72.2M
                                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
356
195M
}
357
358
bool CNetAddr::IsRFC4380() const
359
208M
{
360
208M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
  Branch (360:12): [True: 76.5M, False: 131M]
  Branch (360:24): [True: 66.7k, False: 76.4M]
361
208M
}
362
363
bool CNetAddr::IsRFC4862() const
364
592M
{
365
592M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
  Branch (365:12): [True: 181M, False: 411M]
  Branch (365:24): [True: 27.5k, False: 181M]
366
181M
                                                                0x00, 0x00, 0x00, 0x00});
367
592M
}
368
369
bool CNetAddr::IsRFC4193() const
370
591M
{
371
591M
    return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
  Branch (371:12): [True: 181M, False: 409M]
  Branch (371:24): [True: 416k, False: 180M]
372
591M
}
373
374
bool CNetAddr::IsRFC6145() const
375
195M
{
376
195M
    return IsIPv6() &&
  Branch (376:12): [True: 72.2M, False: 123M]
377
195M
           HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  Branch (377:12): [True: 64.8k, False: 72.1M]
378
72.2M
                                                     0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
379
195M
}
380
381
bool CNetAddr::IsRFC4843() const
382
590M
{
383
590M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
  Branch (383:12): [True: 180M, False: 409M]
  Branch (383:24): [True: 231k, False: 180M]
384
590M
           (m_addr[3] & 0xF0) == 0x10;
  Branch (384:12): [True: 25.4k, False: 206k]
385
590M
}
386
387
bool CNetAddr::IsRFC7343() const
388
590M
{
389
590M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
  Branch (389:12): [True: 180M, False: 409M]
  Branch (389:24): [True: 206k, False: 180M]
390
590M
           (m_addr[3] & 0xF0) == 0x20;
  Branch (390:12): [True: 17.0k, False: 189k]
391
590M
}
392
393
bool CNetAddr::IsHeNet() const
394
11.4M
{
395
11.4M
    return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
  Branch (395:12): [True: 11.4M, False: 0]
  Branch (395:24): [True: 12.3k, False: 11.4M]
396
11.4M
}
397
398
bool CNetAddr::IsLocal() const
399
651M
{
400
    // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
401
651M
    if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
  Branch (401:9): [True: 123M, False: 528M]
  Branch (401:22): [True: 170k, False: 123M]
  Branch (401:42): [True: 1.16M, False: 122M]
402
1.33M
        return true;
403
1.33M
    }
404
405
    // IPv6 loopback (::1/128)
406
650M
    static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
407
650M
    if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
  Branch (407:9): [True: 195M, False: 454M]
  Branch (407:21): [True: 325k, False: 195M]
408
325k
        return true;
409
325k
    }
410
411
650M
    return false;
412
650M
}
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
673M
{
426
    // unspecified IPv6 address (::/128)
427
673M
    unsigned char ipNone6[16] = {};
428
673M
    if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
  Branch (428:9): [True: 226M, False: 447M]
  Branch (428:21): [True: 30.6M, False: 195M]
429
30.6M
        return false;
430
30.6M
    }
431
432
642M
    if (IsCJDNS() && !HasCJDNSPrefix()) {
  Branch (432:9): [True: 98.5M, False: 543M]
  Branch (432:22): [True: 2.77k, False: 98.5M]
433
2.77k
        return false;
434
2.77k
    }
435
436
    // documentation IPv6 address
437
642M
    if (IsRFC3849())
  Branch (437:9): [True: 28.8k, False: 642M]
438
28.8k
        return false;
439
440
642M
    if (IsInternal())
  Branch (440:9): [True: 4.03M, False: 638M]
441
4.03M
        return false;
442
443
638M
    if (IsIPv4()) {
  Branch (443:9): [True: 122M, False: 515M]
444
122M
        const uint32_t addr = ReadBE32(m_addr.data());
445
122M
        if (addr == INADDR_ANY || addr == INADDR_NONE) {
  Branch (445:13): [True: 825k, False: 122M]
  Branch (445:35): [True: 230k, False: 121M]
446
1.05M
            return false;
447
1.05M
        }
448
122M
    }
449
450
637M
    return true;
451
638M
}
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
595M
{
464
595M
    return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
  Branch (464:12): [True: 593M, False: 2.81M]
  Branch (464:27): [True: 142k, False: 592M]
  Branch (464:42): [True: 89.6k, False: 592M]
  Branch (464:57): [True: 25.8k, False: 592M]
  Branch (464:72): [True: 27.5k, False: 592M]
  Branch (464:87): [True: 38.3k, False: 592M]
  Branch (464:102): [True: 1.66M, False: 591M]
  Branch (464:117): [True: 416k, False: 590M]
  Branch (464:132): [True: 25.4k, False: 590M]
  Branch (464:147): [True: 17.0k, False: 590M]
  Branch (464:162): [True: 1.16M, False: 589M]
  Branch (464:175): [True: 0, False: 589M]
465
595M
}
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
1.62G
{
474
1.62G
   return m_net == NET_INTERNAL;
475
1.62G
}
476
477
bool CNetAddr::IsAddrV1Compatible() const
478
176M
{
479
176M
    switch (m_net) {
  Branch (479:13): [True: 0, False: 176M]
480
20.0M
    case NET_IPV4:
  Branch (480:5): [True: 20.0M, False: 156M]
481
72.5M
    case NET_IPV6:
  Branch (481:5): [True: 52.5M, False: 123M]
482
72.8M
    case NET_INTERNAL:
  Branch (482:5): [True: 329k, False: 176M]
483
72.8M
        return true;
484
32.1M
    case NET_ONION:
  Branch (484:5): [True: 32.1M, False: 144M]
485
71.1M
    case NET_I2P:
  Branch (485:5): [True: 39.0M, False: 137M]
486
103M
    case NET_CJDNS:
  Branch (486:5): [True: 32.4M, False: 143M]
487
103M
        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: 176M]
489
0
    case NET_MAX:        // m_net is never and should not be set to NET_MAX
  Branch (489:5): [True: 0, False: 176M]
490
0
        assert(false);
  Branch (490:9): [Folded - Ignored]
491
176M
    } // no default case, so the compiler can warn about missing cases
492
493
176M
    assert(false);
  Branch (493:5): [Folded - Ignored]
494
0
}
495
496
enum Network CNetAddr::GetNetwork() const
497
133M
{
498
133M
    if (IsInternal())
  Branch (498:9): [True: 4.46M, False: 128M]
499
4.46M
        return NET_INTERNAL;
500
501
128M
    if (!IsRoutable())
  Branch (501:9): [True: 1.99M, False: 126M]
502
1.99M
        return NET_UNROUTABLE;
503
504
126M
    return m_net;
505
128M
}
506
507
static std::string IPv4ToString(std::span<const uint8_t> a)
508
123k
{
509
123k
    return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
510
123k
}
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
1.34M
{
516
1.34M
    assert(a.size() == ADDR_IPV6_SIZE);
  Branch (516:5): [True: 1.34M, False: 0]
517
1.34M
    const std::array groups{
518
1.34M
        ReadBE16(&a[0]),
519
1.34M
        ReadBE16(&a[2]),
520
1.34M
        ReadBE16(&a[4]),
521
1.34M
        ReadBE16(&a[6]),
522
1.34M
        ReadBE16(&a[8]),
523
1.34M
        ReadBE16(&a[10]),
524
1.34M
        ReadBE16(&a[12]),
525
1.34M
        ReadBE16(&a[14]),
526
1.34M
    };
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
1.34M
    struct ZeroSpan {
531
1.34M
        size_t start_index{0};
532
1.34M
        size_t len{0};
533
1.34M
    };
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
1.34M
    ZeroSpan longest, current;
538
12.1M
    for (size_t i{0}; i < groups.size(); ++i) {
  Branch (538:23): [True: 10.7M, False: 1.34M]
539
10.7M
        if (groups[i] != 0) {
  Branch (539:13): [True: 9.52M, False: 1.25M]
540
9.52M
            current = {i + 1, 0};
541
9.52M
            continue;
542
9.52M
        }
543
1.25M
        current.len += 1;
544
1.25M
        if (current.len > longest.len) {
  Branch (544:13): [True: 1.18M, False: 61.2k]
545
1.18M
            longest = current;
546
1.18M
        }
547
1.25M
    }
548
549
1.34M
    std::string r;
550
1.34M
    r.reserve(39);
551
12.1M
    for (size_t i{0}; i < groups.size(); ++i) {
  Branch (551:23): [True: 10.7M, False: 1.34M]
552
        // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
553
10.7M
        if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
  Branch (553:13): [True: 1.50M, False: 9.27M]
  Branch (553:33): [True: 1.30M, False: 198k]
  Branch (553:61): [True: 1.10M, False: 198k]
554
1.10M
            if (i == longest.start_index) {
  Branch (554:17): [True: 187k, False: 915k]
555
187k
                r += "::";
556
187k
            }
557
1.10M
            continue;
558
1.10M
        }
559
9.67M
        r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
  Branch (559:34): [True: 8.45M, False: 1.21M]
  Branch (559:48): [True: 8.39M, False: 57.1k]
560
9.67M
    }
561
562
1.34M
    if (scope_id != 0) {
  Branch (562:9): [True: 38, False: 1.34M]
563
38
        r += strprintf("%%%u", scope_id);
564
38
    }
565
566
1.34M
    return r;
567
1.34M
}
568
569
std::string OnionToString(std::span<const uint8_t> addr)
570
68.1k
{
571
68.1k
    uint8_t checksum[torv3::CHECKSUM_LEN];
572
68.1k
    torv3::Checksum(addr, checksum);
573
    // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
574
68.1k
    prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
575
68.1k
    address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
576
68.1k
    address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
577
68.1k
    return EncodeBase32(address) + ".onion";
578
68.1k
}
579
580
std::string CNetAddr::ToStringAddr() const
581
1.79M
{
582
1.79M
    switch (m_net) {
  Branch (582:13): [True: 0, False: 1.79M]
583
123k
    case NET_IPV4:
  Branch (583:5): [True: 123k, False: 1.67M]
584
123k
        return IPv4ToString(m_addr);
585
1.28M
    case NET_IPV6:
  Branch (585:5): [True: 1.28M, False: 511k]
586
1.28M
        return IPv6ToString(m_addr, m_scope_id);
587
68.1k
    case NET_ONION:
  Branch (587:5): [True: 68.1k, False: 1.72M]
588
68.1k
        return OnionToString(m_addr);
589
218k
    case NET_I2P:
  Branch (589:5): [True: 218k, False: 1.57M]
590
218k
        return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
591
61.5k
    case NET_CJDNS:
  Branch (591:5): [True: 61.5k, False: 1.73M]
592
61.5k
        return IPv6ToString(m_addr, 0);
593
39.2k
    case NET_INTERNAL:
  Branch (593:5): [True: 39.2k, False: 1.75M]
594
39.2k
        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.79M]
596
0
    case NET_MAX:        // m_net is never and should not be set to NET_MAX
  Branch (596:5): [True: 0, False: 1.79M]
597
0
        assert(false);
  Branch (597:9): [Folded - Ignored]
598
1.79M
    } // no default case, so the compiler can warn about missing cases
599
600
1.79M
    assert(false);
  Branch (600:5): [Folded - Ignored]
601
0
}
602
603
bool operator==(const CNetAddr& a, const CNetAddr& b)
604
118M
{
605
118M
    return a.m_net == b.m_net && a.m_addr == b.m_addr;
  Branch (605:12): [True: 79.8M, False: 38.8M]
  Branch (605:34): [True: 51.8M, False: 27.9M]
606
118M
}
607
608
bool operator<(const CNetAddr& a, const CNetAddr& b)
609
53.2M
{
610
53.2M
    return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
611
53.2M
}
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
9.56k
{
625
9.56k
    if (!IsIPv4())
  Branch (625:9): [True: 0, False: 9.56k]
626
0
        return false;
627
9.56k
    assert(sizeof(*pipv4Addr) == m_addr.size());
  Branch (627:5): [True: 9.56k, False: 0]
628
9.56k
    memcpy(pipv4Addr, m_addr.data(), m_addr.size());
629
9.56k
    return true;
630
9.56k
}
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
32.6k
{
644
32.6k
    if (!IsIPv6() && !IsCJDNS()) {
  Branch (644:9): [True: 5.38k, False: 27.2k]
  Branch (644:22): [True: 0, False: 5.38k]
645
0
        return false;
646
0
    }
647
32.6k
    assert(sizeof(*pipv6Addr) == m_addr.size());
  Branch (647:5): [True: 32.6k, False: 0]
648
32.6k
    memcpy(pipv6Addr, m_addr.data(), m_addr.size());
649
32.6k
    return true;
650
32.6k
}
651
652
bool CNetAddr::HasLinkedIPv4() const
653
222M
{
654
222M
    return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
  Branch (654:12): [True: 222M, False: 0]
  Branch (654:29): [True: 40.4M, False: 182M]
  Branch (654:41): [True: 46.2k, False: 182M]
  Branch (654:56): [True: 54.6k, False: 182M]
  Branch (654:71): [True: 36.4k, False: 182M]
  Branch (654:86): [True: 46.2k, False: 182M]
655
222M
}
656
657
uint32_t CNetAddr::GetLinkedIPv4() const
658
12.9M
{
659
12.9M
    if (IsIPv4()) {
  Branch (659:9): [True: 12.8M, False: 63.7k]
660
12.8M
        return ReadBE32(m_addr.data());
661
12.8M
    } else if (IsRFC6052() || IsRFC6145()) {
  Branch (661:16): [True: 18.2k, False: 45.4k]
  Branch (661:31): [True: 17.1k, False: 28.3k]
662
        // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
663
35.4k
        return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
664
35.4k
    } else if (IsRFC3964()) {
  Branch (664:16): [True: 12.5k, False: 15.8k]
665
        // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
666
12.5k
        return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
667
15.8k
    } else if (IsRFC4380()) {
  Branch (667:16): [True: 15.8k, False: 0]
668
        // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
669
15.8k
        return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
670
15.8k
    }
671
12.9M
    assert(false);
  Branch (671:5): [Folded - Ignored]
672
0
}
673
674
Network CNetAddr::GetNetClass() const
675
164M
{
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
164M
    if (IsInternal()) {
  Branch (680:9): [True: 6.57M, False: 158M]
681
6.57M
        return NET_INTERNAL;
682
6.57M
    }
683
158M
    if (!IsRoutable()) {
  Branch (683:9): [True: 3.58M, False: 154M]
684
3.58M
        return NET_UNROUTABLE;
685
3.58M
    }
686
154M
    if (HasLinkedIPv4()) {
  Branch (686:9): [True: 27.7M, False: 126M]
687
27.7M
        return NET_IPV4;
688
27.7M
    }
689
126M
    return m_net;
690
154M
}
691
692
std::vector<unsigned char> CNetAddr::GetAddrBytes() const
693
176M
{
694
176M
    if (IsAddrV1Compatible()) {
  Branch (694:9): [True: 72.8M, False: 103M]
695
72.8M
        uint8_t serialized[V1_SERIALIZATION_SIZE];
696
72.8M
        SerializeV1Array(serialized);
697
72.8M
        return {std::begin(serialized), std::end(serialized)};
698
72.8M
    }
699
103M
    return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
700
176M
}
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
26.1M
{
707
26.1M
    if (addr.IsRFC4380())
  Branch (707:9): [True: 4.69k, False: 26.1M]
708
4.69k
        return NET_TEREDO;
709
26.1M
    return addr.GetNetwork();
710
26.1M
}
711
712
/** Calculates a metric for how reachable (*this) is from a given partner */
713
int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
714
13.0M
{
715
13.0M
    enum Reachability {
716
13.0M
        REACH_UNREACHABLE,
717
13.0M
        REACH_DEFAULT,
718
13.0M
        REACH_TEREDO,
719
13.0M
        REACH_IPV6_WEAK,
720
13.0M
        REACH_IPV4,
721
13.0M
        REACH_IPV6_STRONG,
722
13.0M
        REACH_PRIVATE
723
13.0M
    };
724
725
13.0M
    if (!IsRoutable() || IsInternal())
  Branch (725:9): [True: 247, False: 13.0M]
  Branch (725:26): [True: 0, False: 13.0M]
726
247
        return REACH_UNREACHABLE;
727
728
13.0M
    int ourNet = GetExtNetwork(*this);
729
13.0M
    int theirNet = GetExtNetwork(paddrPartner);
730
13.0M
    bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
  Branch (730:20): [True: 1.44k, False: 13.0M]
  Branch (730:35): [True: 897, False: 13.0M]
  Branch (730:50): [True: 1.50k, False: 13.0M]
731
732
13.0M
    switch(theirNet) {
733
3.09M
    case NET_IPV4:
  Branch (733:5): [True: 3.09M, False: 9.97M]
734
3.09M
        switch(ourNet) {
735
2.12M
        default:       return REACH_DEFAULT;
  Branch (735:9): [True: 2.12M, False: 970k]
736
970k
        case NET_IPV4: return REACH_IPV4;
  Branch (736:9): [True: 970k, False: 2.12M]
737
3.09M
        }
738
4.41M
    case NET_IPV6:
  Branch (738:5): [True: 4.41M, False: 8.65M]
739
4.41M
        switch(ourNet) {
740
291k
        default:         return REACH_DEFAULT;
  Branch (740:9): [True: 291k, False: 4.12M]
741
1.01k
        case NET_TEREDO: return REACH_TEREDO;
  Branch (741:9): [True: 1.01k, False: 4.41M]
742
1.48M
        case NET_IPV4:   return REACH_IPV4;
  Branch (742:9): [True: 1.48M, False: 2.93M]
743
2.64M
        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: 2.64M, False: 1.77M]
  Branch (743:33): [True: 2.67k, False: 2.63M]
744
4.41M
        }
745
3.63k
    case NET_ONION:
  Branch (745:5): [True: 3.63k, False: 13.0M]
746
3.63k
        switch(ourNet) {
747
2
        default:         return REACH_DEFAULT;
  Branch (747:9): [True: 2, False: 3.63k]
748
0
        case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
  Branch (748:9): [True: 0, False: 3.63k]
749
3.63k
        case NET_ONION:    return REACH_PRIVATE;
  Branch (749:9): [True: 3.63k, False: 2]
750
3.63k
        }
751
6.16k
    case NET_I2P:
  Branch (751:5): [True: 6.16k, False: 13.0M]
752
6.16k
        switch (ourNet) {
753
4.45k
        case NET_I2P: return REACH_PRIVATE;
  Branch (753:9): [True: 4.45k, False: 1.71k]
754
1.71k
        default: return REACH_DEFAULT;
  Branch (754:9): [True: 1.71k, False: 4.45k]
755
6.16k
        }
756
72.4k
    case NET_CJDNS:
  Branch (756:5): [True: 72.4k, False: 13.0M]
757
72.4k
        switch (ourNet) {
758
1.06k
        case NET_CJDNS: return REACH_PRIVATE;
  Branch (758:9): [True: 1.06k, False: 71.3k]
759
71.3k
        default: return REACH_DEFAULT;
  Branch (759:9): [True: 71.3k, False: 1.06k]
760
72.4k
        }
761
3.64k
    case NET_TEREDO:
  Branch (761:5): [True: 3.64k, False: 13.0M]
762
3.64k
        switch(ourNet) {
763
55
        default:          return REACH_DEFAULT;
  Branch (763:9): [True: 55, False: 3.58k]
764
7
        case NET_TEREDO:  return REACH_TEREDO;
  Branch (764:9): [True: 7, False: 3.63k]
765
1.39k
        case NET_IPV6:    return REACH_IPV6_WEAK;
  Branch (765:9): [True: 1.39k, False: 2.25k]
766
2.18k
        case NET_IPV4:    return REACH_IPV4;
  Branch (766:9): [True: 2.18k, False: 1.45k]
767
3.64k
        }
768
1.02M
    case NET_UNROUTABLE:
  Branch (768:5): [True: 1.02M, False: 12.0M]
769
5.47M
    default:
  Branch (769:5): [True: 4.45M, False: 8.62M]
770
5.47M
        switch(ourNet) {
771
101k
        default:          return REACH_DEFAULT;
  Branch (771:9): [True: 101k, False: 5.37M]
772
36
        case NET_TEREDO:  return REACH_TEREDO;
  Branch (772:9): [True: 36, False: 5.47M]
773
3.40M
        case NET_IPV6:    return REACH_IPV6_WEAK;
  Branch (773:9): [True: 3.40M, False: 2.07M]
774
1.97M
        case NET_IPV4:    return REACH_IPV4;
  Branch (774:9): [True: 1.97M, False: 3.50M]
775
28
        case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
  Branch (775:9): [True: 28, False: 5.47M]
776
5.47M
        }
777
13.0M
    }
778
13.0M
}
779
780
41.0M
CService::CService() : port(0)
781
41.0M
{
782
41.0M
}
783
784
19.4M
CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
785
19.4M
{
786
19.4M
}
787
788
6.03k
CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
789
6.03k
{
790
6.03k
}
791
792
6.66k
CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
793
6.66k
{
794
6.66k
}
795
796
495
CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
797
495
{
798
495
    assert(addr.sin_family == AF_INET);
  Branch (798:5): [True: 495, False: 0]
799
495
}
800
801
2.12k
CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
802
2.12k
{
803
2.12k
   assert(addr.sin6_family == AF_INET6);
  Branch (803:4): [True: 2.12k, False: 0]
804
2.12k
}
805
806
bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
807
3.17k
{
808
3.17k
    switch (paddr->sa_family) {
809
501
    case AF_INET:
  Branch (809:5): [True: 501, False: 2.67k]
810
501
        if (addrlen != sizeof(struct sockaddr_in)) return false;
  Branch (810:13): [True: 6, False: 495]
811
495
        *this = CService(*(const struct sockaddr_in*)paddr);
812
495
        return true;
813
2.13k
    case AF_INET6:
  Branch (813:5): [True: 2.13k, False: 1.04k]
814
2.13k
        if (addrlen != sizeof(struct sockaddr_in6)) return false;
  Branch (814:13): [True: 7, False: 2.12k]
815
2.12k
        *this = CService(*(const struct sockaddr_in6*)paddr);
816
2.12k
        return true;
817
546
    default:
  Branch (817:5): [True: 546, False: 2.63k]
818
546
        return false;
819
3.17k
    }
820
3.17k
}
821
822
sa_family_t CService::GetSAFamily() const
823
42.4k
{
824
42.4k
    switch (m_net) {
825
8.70k
    case NET_IPV4:
  Branch (825:5): [True: 8.70k, False: 33.7k]
826
8.70k
        return AF_INET;
827
26.9k
    case NET_IPV6:
  Branch (827:5): [True: 26.9k, False: 15.5k]
828
32.2k
    case NET_CJDNS:
  Branch (828:5): [True: 5.37k, False: 37.0k]
829
32.2k
        return AF_INET6;
830
1.44k
    default:
  Branch (830:5): [True: 1.44k, False: 40.9k]
831
1.44k
        return AF_UNSPEC;
832
42.4k
    }
833
42.4k
}
834
835
uint16_t CService::GetPort() const
836
195k
{
837
195k
    return port;
838
195k
}
839
840
bool operator==(const CService& a, const CService& b)
841
78.7M
{
842
78.7M
    return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
  Branch (842:12): [True: 44.3M, False: 34.3M]
  Branch (842:68): [True: 44.3M, False: 14.5k]
843
78.7M
}
844
845
bool operator<(const CService& a, const CService& b)
846
45.9M
{
847
45.9M
    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: 24.7M, False: 21.1M]
  Branch (847:68): [True: 7.14M, False: 14.0M]
  Branch (847:124): [True: 1.82M, False: 5.32M]
848
45.9M
}
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
78.5k
{
864
78.5k
    if (IsIPv4()) {
  Branch (864:9): [True: 9.28k, False: 69.2k]
865
9.28k
        if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
  Branch (865:13): [True: 0, False: 9.28k]
866
0
            return false;
867
9.28k
        *addrlen = sizeof(struct sockaddr_in);
868
9.28k
        struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
869
9.28k
        memset(paddrin, 0, *addrlen);
870
9.28k
        if (!GetInAddr(&paddrin->sin_addr))
  Branch (870:13): [True: 0, False: 9.28k]
871
0
            return false;
872
9.28k
        paddrin->sin_family = AF_INET;
873
9.28k
        paddrin->sin_port = htons(port);
874
9.28k
        return true;
875
9.28k
    }
876
69.2k
    if (IsIPv6() || IsCJDNS()) {
  Branch (876:9): [True: 27.1k, False: 42.0k]
  Branch (876:21): [True: 5.38k, False: 36.7k]
877
32.5k
        if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
  Branch (877:13): [True: 0, False: 32.5k]
878
0
            return false;
879
32.5k
        *addrlen = sizeof(struct sockaddr_in6);
880
32.5k
        struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
881
32.5k
        memset(paddrin6, 0, *addrlen);
882
32.5k
        if (!GetIn6Addr(&paddrin6->sin6_addr))
  Branch (882:13): [True: 0, False: 32.5k]
883
0
            return false;
884
32.5k
        paddrin6->sin6_scope_id = m_scope_id;
885
32.5k
        paddrin6->sin6_family = AF_INET6;
886
32.5k
        paddrin6->sin6_port = htons(port);
887
32.5k
        return true;
888
32.5k
    }
889
36.7k
    return false;
890
69.2k
}
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
100M
{
897
100M
    auto key = GetAddrBytes();
898
100M
    key.push_back(port / 0x100); // most significant byte of our port
899
100M
    key.push_back(port & 0x0FF); // least significant byte of our port
900
100M
    return key;
901
100M
}
902
903
std::string CService::ToStringAddrPort() const
904
328k
{
905
328k
    const auto port_str = strprintf("%u", port);
906
907
328k
    if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
  Branch (907:9): [True: 64.1k, False: 264k]
  Branch (907:21): [True: 22.9k, False: 241k]
  Branch (907:32): [True: 10.8k, False: 230k]
  Branch (907:43): [True: 35.6k, False: 195k]
908
133k
        return ToStringAddr() + ":" + port_str;
909
195k
    } else {
910
195k
        return "[" + ToStringAddr() + "]:" + port_str;
911
195k
    }
912
328k
}
913
914
CSubNet::CSubNet():
915
304k
    valid(false)
916
304k
{
917
304k
    memset(netmask, 0, sizeof(netmask));
918
304k
}
919
920
165k
CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
921
165k
{
922
165k
    valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
  Branch (922:14): [True: 8.00k, False: 156k]
  Branch (922:31): [True: 5.55k, False: 2.45k]
923
165k
            (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
  Branch (923:14): [True: 42.2k, False: 117k]
  Branch (923:31): [True: 36.0k, False: 6.15k]
924
165k
    if (!valid) {
  Branch (924:9): [True: 123k, False: 41.6k]
925
123k
        return;
926
123k
    }
927
928
165k
    assert(mask <= sizeof(netmask) * 8);
  Branch (928:5): [True: 41.6k, False: 0]
929
930
41.6k
    network = addr;
931
932
41.6k
    uint8_t n = mask;
933
640k
    for (size_t i = 0; i < network.m_addr.size(); ++i) {
  Branch (933:24): [True: 599k, False: 41.6k]
934
599k
        const uint8_t bits = n < 8 ? n : 8;
  Branch (934:30): [True: 258k, False: 340k]
935
599k
        netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
936
599k
        network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
937
599k
        n -= bits;
938
599k
    }
939
41.6k
}
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
17.2M
{
947
17.2M
    switch(x) {
948
4.62k
    case 0x00: return 0;
  Branch (948:5): [True: 4.62k, False: 17.2M]
949
6.70k
    case 0x80: return 1;
  Branch (949:5): [True: 6.70k, False: 17.2M]
950
2.09k
    case 0xc0: return 2;
  Branch (950:5): [True: 2.09k, False: 17.2M]
951
2.89k
    case 0xe0: return 3;
  Branch (951:5): [True: 2.89k, False: 17.2M]
952
2.11k
    case 0xf0: return 4;
  Branch (952:5): [True: 2.11k, False: 17.2M]
953
1.36k
    case 0xf8: return 5;
  Branch (953:5): [True: 1.36k, False: 17.2M]
954
1.04k
    case 0xfc: return 6;
  Branch (954:5): [True: 1.04k, False: 17.2M]
955
1.53k
    case 0xfe: return 7;
  Branch (955:5): [True: 1.53k, False: 17.2M]
956
17.2M
    case 0xff: return 8;
  Branch (956:5): [True: 17.2M, False: 23.2k]
957
892
    default: return -1;
  Branch (957:5): [True: 892, False: 17.2M]
958
17.2M
    }
959
17.2M
}
960
961
3.94k
CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
962
3.94k
{
963
3.94k
    valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
  Branch (963:14): [True: 3.45k, False: 495]
  Branch (963:31): [True: 274, False: 221]
  Branch (963:49): [True: 3.55k, False: 177]
964
3.94k
    if (!valid) {
  Branch (964:9): [True: 398, False: 3.55k]
965
398
        return;
966
398
    }
967
    // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
968
3.55k
    bool zeros_found = false;
969
11.6k
    for (auto b : mask.m_addr) {
  Branch (969:17): [True: 11.6k, False: 1.09k]
970
11.6k
        const int num_bits = NetmaskBits(b);
971
11.6k
        if (num_bits == -1 || (zeros_found && num_bits != 0)) {
  Branch (971:13): [True: 892, False: 10.7k]
  Branch (971:32): [True: 4.12k, False: 6.64k]
  Branch (971:47): [True: 1.56k, False: 2.55k]
972
2.45k
            valid = false;
973
2.45k
            return;
974
2.45k
        }
975
9.19k
        if (num_bits < 8) {
  Branch (975:13): [True: 5.03k, False: 4.16k]
976
5.03k
            zeros_found = true;
977
5.03k
        }
978
9.19k
    }
979
980
3.55k
    assert(mask.m_addr.size() <= sizeof(netmask));
  Branch (980:5): [True: 1.09k, False: 0]
981
982
1.09k
    memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
983
984
1.09k
    network = addr;
985
986
    // Normalize network according to netmask
987
6.08k
    for (size_t x = 0; x < network.m_addr.size(); ++x) {
  Branch (987:24): [True: 4.99k, False: 1.09k]
988
4.99k
        network.m_addr[x] &= netmask[x];
989
4.99k
    }
990
1.09k
}
991
992
71.5k
CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
993
71.5k
{
994
71.5k
    switch (addr.m_net) {
  Branch (994:13): [True: 0, False: 71.5k]
995
11.0k
    case NET_IPV4:
  Branch (995:5): [True: 11.0k, False: 60.4k]
996
45.8k
    case NET_IPV6:
  Branch (996:5): [True: 34.8k, False: 36.7k]
997
45.8k
        valid = true;
998
45.8k
        assert(addr.m_addr.size() <= sizeof(netmask));
  Branch (998:9): [True: 45.8k, False: 0]
999
45.8k
        memset(netmask, 0xFF, addr.m_addr.size());
1000
45.8k
        break;
1001
2.57k
    case NET_ONION:
  Branch (1001:5): [True: 2.57k, False: 68.9k]
1002
14.2k
    case NET_I2P:
  Branch (1002:5): [True: 11.6k, False: 59.8k]
1003
17.8k
    case NET_CJDNS:
  Branch (1003:5): [True: 3.61k, False: 67.9k]
1004
17.8k
        valid = true;
1005
17.8k
        break;
1006
7.81k
    case NET_INTERNAL:
  Branch (1006:5): [True: 7.81k, False: 63.7k]
1007
7.81k
    case NET_UNROUTABLE:
  Branch (1007:5): [True: 0, False: 71.5k]
1008
7.81k
    case NET_MAX:
  Branch (1008:5): [True: 0, False: 71.5k]
1009
7.81k
        return;
1010
71.5k
    }
1011
1012
63.7k
    network = addr;
1013
63.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
5.17M
{
1021
5.17M
    if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
  Branch (1021:9): [True: 4.58M, False: 588k]
  Branch (1021:19): [True: 46.5k, False: 542k]
  Branch (1021:38): [True: 122k, False: 419k]
1022
4.75M
        return false;
1023
1024
419k
    switch (network.m_net) {
  Branch (1024:13): [True: 0, False: 419k]
1025
3.13k
    case NET_IPV4:
  Branch (1025:5): [True: 3.13k, False: 416k]
1026
412k
    case NET_IPV6:
  Branch (1026:5): [True: 409k, False: 10.2k]
1027
412k
        break;
1028
1.60k
    case NET_ONION:
  Branch (1028:5): [True: 1.60k, False: 417k]
1029
4.16k
    case NET_I2P:
  Branch (1029:5): [True: 2.56k, False: 416k]
1030
7.11k
    case NET_CJDNS:
  Branch (1030:5): [True: 2.94k, False: 416k]
1031
7.11k
    case NET_INTERNAL:
  Branch (1031:5): [True: 0, False: 419k]
1032
7.11k
        return addr == network;
1033
0
    case NET_UNROUTABLE:
  Branch (1033:5): [True: 0, False: 419k]
1034
0
    case NET_MAX:
  Branch (1034:5): [True: 0, False: 419k]
1035
0
        return false;
1036
419k
    }
1037
1038
419k
    assert(network.m_addr.size() == addr.m_addr.size());
  Branch (1038:5): [True: 412k, False: 0]
1039
782k
    for (size_t x = 0; x < addr.m_addr.size(); ++x) {
  Branch (1039:24): [True: 765k, False: 16.2k]
1040
765k
        if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
  Branch (1040:13): [True: 395k, False: 369k]
1041
395k
            return false;
1042
395k
        }
1043
765k
    }
1044
16.2k
    return true;
1045
412k
}
1046
1047
std::string CSubNet::ToString() const
1048
1.43M
{
1049
1.43M
    std::string suffix;
1050
1051
1.43M
    switch (network.m_net) {
  Branch (1051:13): [True: 0, False: 1.43M]
1052
55.5k
    case NET_IPV4:
  Branch (1052:5): [True: 55.5k, False: 1.37M]
1053
1.13M
    case NET_IPV6: {
  Branch (1053:5): [True: 1.07M, False: 353k]
1054
1.13M
        assert(network.m_addr.size() <= sizeof(netmask));
  Branch (1054:9): [True: 1.13M, False: 0]
1055
1056
1.13M
        uint8_t cidr = 0;
1057
1058
18.3M
        for (size_t i = 0; i < network.m_addr.size(); ++i) {
  Branch (1058:28): [True: 17.2M, False: 1.11M]
1059
17.2M
            if (netmask[i] == 0x00) {
  Branch (1059:17): [True: 21.2k, False: 17.2M]
1060
21.2k
                break;
1061
21.2k
            }
1062
17.2M
            cidr += NetmaskBits(netmask[i]);
1063
17.2M
        }
1064
1065
1.13M
        suffix = strprintf("/%u", cidr);
1066
1.13M
        break;
1067
1.13M
    }
1068
43.4k
    case NET_ONION:
  Branch (1068:5): [True: 43.4k, False: 1.38M]
1069
245k
    case NET_I2P:
  Branch (1069:5): [True: 201k, False: 1.22M]
1070
298k
    case NET_CJDNS:
  Branch (1070:5): [True: 52.7k, False: 1.37M]
1071
298k
    case NET_INTERNAL:
  Branch (1071:5): [True: 0, False: 1.43M]
1072
298k
    case NET_UNROUTABLE:
  Branch (1072:5): [True: 0, False: 1.43M]
1073
298k
    case NET_MAX:
  Branch (1073:5): [True: 0, False: 1.43M]
1074
298k
        break;
1075
1.43M
    }
1076
1077
1.43M
    return network.ToStringAddr() + suffix;
1078
1.43M
}
1079
1080
bool CSubNet::IsValid() const
1081
1.67M
{
1082
1.67M
    return valid;
1083
1.67M
}
1084
1085
bool operator==(const CSubNet& a, const CSubNet& b)
1086
9.14k
{
1087
9.14k
    return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
  Branch (1087:12): [True: 9.14k, False: 0]
  Branch (1087:34): [True: 9.14k, False: 0]
  Branch (1087:60): [True: 9.14k, False: 0]
1088
9.14k
}
1089
1090
bool operator<(const CSubNet& a, const CSubNet& b)
1091
694k
{
1092
694k
    return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
  Branch (1092:13): [True: 413k, False: 281k]
  Branch (1092:39): [True: 101k, False: 179k]
  Branch (1092:65): [True: 3.40k, False: 98.4k]
1093
694k
}