Coverage Report

Created: 2025-06-10 13:21

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