Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/netaddress.h
Line
Count
Source
1
// Copyright (c) 2009-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_NETADDRESS_H
6
#define BITCOIN_NETADDRESS_H
7
8
#include <compat/compat.h>
9
#include <crypto/siphash.h>
10
#include <prevector.h>
11
#include <random.h>
12
#include <serialize.h>
13
#include <tinyformat.h>
14
#include <util/strencodings.h>
15
#include <util/string.h>
16
17
#include <array>
18
#include <cstdint>
19
#include <ios>
20
#include <string>
21
#include <string_view>
22
#include <vector>
23
24
/**
25
 * A network type.
26
 * @note An address may belong to more than one network, for example `10.0.0.1`
27
 * belongs to both `NET_UNROUTABLE` and `NET_IPV4`.
28
 * Keep these sequential starting from 0 and `NET_MAX` as the last entry.
29
 * We have loops like `for (int i = 0; i < NET_MAX; ++i)` that expect to iterate
30
 * over all enum values and also `GetExtNetwork()` "extends" this enum by
31
 * introducing standalone constants starting from `NET_MAX`.
32
 */
33
enum Network {
34
    /// Addresses from these networks are not publicly routable on the global Internet.
35
    NET_UNROUTABLE = 0,
36
37
    /// IPv4
38
    NET_IPV4,
39
40
    /// IPv6
41
    NET_IPV6,
42
43
    /// TOR (v2 or v3)
44
    NET_ONION,
45
46
    /// I2P
47
    NET_I2P,
48
49
    /// CJDNS
50
    NET_CJDNS,
51
52
    /// A set of addresses that represent the hash of a string or FQDN. We use
53
    /// them in AddrMan to keep track of which DNS seeds were used.
54
    NET_INTERNAL,
55
56
    /// Dummy value to indicate the number of NET_* constants.
57
    NET_MAX,
58
};
59
60
/// Prefix of an IPv6 address when it contains an embedded IPv4 address.
61
/// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
62
inline constexpr std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{
63
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF};
64
65
/// Prefix of an IPv6 address when it contains an embedded TORv2 address.
66
/// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
67
/// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
68
/// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
69
inline constexpr std::array<uint8_t, 6> TORV2_IN_IPV6_PREFIX{
70
    0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43};
71
72
/// Prefix of an IPv6 address when it contains an embedded "internal" address.
73
/// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
74
/// The prefix comes from 0xFD + SHA256("bitcoin")[0:5].
75
/// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
76
/// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
77
inline constexpr std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{
78
    0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 // 0xFD + sha256("bitcoin")[0:5].
79
};
80
81
/// All CJDNS addresses start with 0xFC. See
82
/// https://github.com/cjdelisle/cjdns/blob/master/doc/Whitepaper.md#pulling-it-all-together
83
inline constexpr uint8_t CJDNS_PREFIX{0xFC};
84
85
/// Size of IPv4 address (in bytes).
86
inline constexpr size_t ADDR_IPV4_SIZE = 4;
87
88
/// Size of IPv6 address (in bytes).
89
inline constexpr size_t ADDR_IPV6_SIZE = 16;
90
91
/// Size of TORv3 address (in bytes). This is the length of just the address
92
/// as used in BIP155, without the checksum and the version byte.
93
inline constexpr size_t ADDR_TORV3_SIZE = 32;
94
95
/// Size of I2P address (in bytes).
96
inline constexpr size_t ADDR_I2P_SIZE = 32;
97
98
/// Size of CJDNS address (in bytes).
99
inline constexpr size_t ADDR_CJDNS_SIZE = 16;
100
101
/// Size of "internal" (NET_INTERNAL) address (in bytes).
102
inline constexpr size_t ADDR_INTERNAL_SIZE = 10;
103
104
/// SAM 3.1 and earlier do not support specifying ports and force the port to 0.
105
inline constexpr uint16_t I2P_SAM31_PORT{0};
106
107
std::string OnionToString(std::span<const uint8_t> addr);
108
109
/**
110
 * Network address.
111
 */
112
class CNetAddr
113
{
114
protected:
115
    /**
116
     * Raw representation of the network address.
117
     * In network byte order (big endian) for IPv4 and IPv6.
118
     */
119
    prevector<ADDR_IPV6_SIZE, uint8_t> m_addr{ADDR_IPV6_SIZE, 0x0};
120
121
    /**
122
     * Network to which this address belongs.
123
     */
124
    Network m_net{NET_IPV6};
125
126
    /**
127
     * Scope id if scoped/link-local IPV6 address.
128
     * See https://tools.ietf.org/html/rfc4007
129
     */
130
    uint32_t m_scope_id{0};
131
132
public:
133
    CNetAddr();
134
    explicit CNetAddr(const struct in_addr& ipv4Addr);
135
    void SetIP(const CNetAddr& ip);
136
137
    /**
138
     * Set from a legacy IPv6 address.
139
     * Legacy IPv6 address may be a normal IPv6 address, or another address
140
     * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy
141
     * `addr` encoding.
142
     */
143
    void SetLegacyIPv6(std::span<const uint8_t> ipv6);
144
145
    bool SetInternal(const std::string& name);
146
147
    /**
148
     * Parse a Tor or I2P address and set this object to it.
149
     * @param[in] addr Address to parse, for example
150
     * pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion or
151
     * ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
152
     * @returns Whether the operation was successful.
153
     * @see CNetAddr::IsTor(), CNetAddr::IsI2P()
154
     */
155
    bool SetSpecial(std::string_view addr);
156
157
    bool IsBindAny() const; // INADDR_ANY equivalent
158
7.95M
    [[nodiscard]] bool IsIPv4() const { return m_net == NET_IPV4; } // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
159
8.39M
    [[nodiscard]] bool IsIPv6() const { return m_net == NET_IPV6; } // IPv6 address (not mapped IPv4, not Tor)
160
    bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
161
    bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
162
    bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
163
    bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
164
    bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
165
    bool IsRFC9637() const; // IPv6 documentation address (3FFF::/20)
166
    bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
167
    bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
168
    bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
169
    bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
170
    bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
171
    bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
172
    bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
173
    bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
174
    bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
175
    bool IsHeNet() const;   // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
176
49.1k
    [[nodiscard]] bool IsTor() const { return m_net == NET_ONION; }
177
2.41k
    [[nodiscard]] bool IsI2P() const { return m_net == NET_I2P; }
178
1.08M
    [[nodiscard]] bool IsCJDNS() const { return m_net == NET_CJDNS; }
179
983
    [[nodiscard]] bool HasCJDNSPrefix() const { return m_addr[0] == CJDNS_PREFIX; }
180
    bool IsLocal() const;
181
    bool IsRoutable() const;
182
    bool IsInternal() const;
183
    bool IsValid() const;
184
185
    /**
186
     * Whether this object is a privacy network.
187
     * TODO: consider adding IsCJDNS() here when more peers adopt CJDNS, see:
188
     * https://github.com/bitcoin/bitcoin/pull/27411#issuecomment-1497176155
189
     */
190
138
    [[nodiscard]] bool IsPrivacyNet() const { return IsTor() || IsI2P(); }
191
192
    /**
193
     * Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
194
     */
195
    bool IsAddrV1Compatible() const;
196
197
    enum Network GetNetwork() const;
198
    std::string ToStringAddr() const;
199
    bool GetInAddr(struct in_addr* pipv4Addr) const;
200
    Network GetNetClass() const;
201
202
    //! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
203
    uint32_t GetLinkedIPv4() const;
204
    //! Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
205
    bool HasLinkedIPv4() const;
206
207
    std::vector<unsigned char> GetAddrBytes() const;
208
    int GetReachabilityFrom(const CNetAddr& paddrPartner) const;
209
210
    explicit CNetAddr(const struct in6_addr& pipv6Addr, uint32_t scope = 0);
211
    bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
212
213
    friend bool operator==(const CNetAddr& a, const CNetAddr& b);
214
    friend bool operator<(const CNetAddr& a, const CNetAddr& b);
215
216
    /**
217
     * Whether this address should be relayed to other peers even if we can't reach it ourselves.
218
     */
219
    bool IsRelayable() const
220
2
    {
221
2
        return IsIPv4() || IsIPv6() || IsTor() || IsI2P() || IsCJDNS();
222
2
    }
223
224
    enum class Encoding {
225
        V1,
226
        V2, //!< BIP155 encoding
227
    };
228
    struct SerParams {
229
        const Encoding enc;
230
        SER_PARAMS_OPFUNC
231
    };
232
    static constexpr SerParams V1{Encoding::V1};
233
    static constexpr SerParams V2{Encoding::V2};
234
235
    /**
236
     * Serialize to a stream.
237
     */
238
    template <typename Stream>
239
    void Serialize(Stream& s) const
240
123k
    {
241
123k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
100k
            SerializeV2Stream(s);
243
100k
        } else {
244
22.4k
            SerializeV1Stream(s);
245
22.4k
        }
246
123k
    }
void CNetAddr::Serialize<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
240
23
    {
241
23
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
18
            SerializeV2Stream(s);
243
18
        } else {
244
5
            SerializeV1Stream(s);
245
5
        }
246
23
    }
void CNetAddr::Serialize<ParamsStream<DataStream&, CAddress::SerParams>>(ParamsStream<DataStream&, CAddress::SerParams>&) const
Line
Count
Source
240
27
    {
241
27
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
20
            SerializeV2Stream(s);
243
20
        } else {
244
7
            SerializeV1Stream(s);
245
7
        }
246
27
    }
void CNetAddr::Serialize<ParamsStream<VectorWriter&, CAddress::SerParams>>(ParamsStream<VectorWriter&, CAddress::SerParams>&) const
Line
Count
Source
240
1
    {
241
1
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
0
            SerializeV2Stream(s);
243
1
        } else {
244
1
            SerializeV1Stream(s);
245
1
        }
246
1
    }
void CNetAddr::Serialize<ParamsStream<VectorWriter&, CNetAddr::SerParams>>(ParamsStream<VectorWriter&, CNetAddr::SerParams>&) const
Line
Count
Source
240
3.43k
    {
241
3.43k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
0
            SerializeV2Stream(s);
243
3.43k
        } else {
244
3.43k
            SerializeV1Stream(s);
245
3.43k
        }
246
3.43k
    }
void CNetAddr::Serialize<ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
240
50.3k
    {
241
50.3k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
50.3k
            SerializeV2Stream(s);
243
50.3k
        } else {
244
0
            SerializeV1Stream(s);
245
0
        }
246
50.3k
    }
void CNetAddr::Serialize<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>>(ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&) const
Line
Count
Source
240
50.3k
    {
241
50.3k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
50.3k
            SerializeV2Stream(s);
243
50.3k
        } else {
244
0
            SerializeV1Stream(s);
245
0
        }
246
50.3k
    }
void CNetAddr::Serialize<ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
240
19.0k
    {
241
19.0k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
242
33
            SerializeV2Stream(s);
243
18.9k
        } else {
244
18.9k
            SerializeV1Stream(s);
245
18.9k
        }
246
19.0k
    }
247
248
    /**
249
     * Unserialize from a stream.
250
     */
251
    template <typename Stream>
252
    void Unserialize(Stream& s)
253
49.8k
    {
254
49.8k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
42.2k
            UnserializeV2Stream(s);
256
42.2k
        } else {
257
7.59k
            UnserializeV1Stream(s);
258
7.59k
        }
259
49.8k
    }
void CNetAddr::Unserialize<ParamsStream<DataStream&, CAddress::SerParams>>(ParamsStream<DataStream&, CAddress::SerParams>&)
Line
Count
Source
253
33
    {
254
33
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
32
            UnserializeV2Stream(s);
256
32
        } else {
257
1
            UnserializeV1Stream(s);
258
1
        }
259
33
    }
void CNetAddr::Unserialize<ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
253
7
    {
254
7
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
3
            UnserializeV2Stream(s);
256
4
        } else {
257
4
            UnserializeV1Stream(s);
258
4
        }
259
7
    }
void CNetAddr::Unserialize<ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
253
20.5k
    {
254
20.5k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
20.5k
            UnserializeV2Stream(s);
256
20.5k
        } else {
257
0
            UnserializeV1Stream(s);
258
0
        }
259
20.5k
    }
Unexecuted instantiation: void CNetAddr::Unserialize<ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Unexecuted instantiation: void CNetAddr::Unserialize<ParamsStream<AutoFile&, CAddress::SerParams>>(ParamsStream<AutoFile&, CAddress::SerParams>&)
void CNetAddr::Unserialize<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>>(ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&)
Line
Count
Source
253
20.5k
    {
254
20.5k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
20.5k
            UnserializeV2Stream(s);
256
20.5k
        } else {
257
0
            UnserializeV1Stream(s);
258
0
        }
259
20.5k
    }
void CNetAddr::Unserialize<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
253
6.92k
    {
254
6.92k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
1.03k
            UnserializeV2Stream(s);
256
5.88k
        } else {
257
5.88k
            UnserializeV1Stream(s);
258
5.88k
        }
259
6.92k
    }
void CNetAddr::Unserialize<ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
253
4
    {
254
4
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
3
            UnserializeV2Stream(s);
256
3
        } else {
257
1
            UnserializeV1Stream(s);
258
1
        }
259
4
    }
void CNetAddr::Unserialize<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>>(ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&)
Line
Count
Source
253
4
    {
254
4
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
3
            UnserializeV2Stream(s);
256
3
        } else {
257
1
            UnserializeV1Stream(s);
258
1
        }
259
4
    }
Unexecuted instantiation: void CNetAddr::Unserialize<ParamsStream<SpanReader, CAddress::SerParams>>(ParamsStream<SpanReader, CAddress::SerParams>&)
void CNetAddr::Unserialize<ParamsStream<DataStream&, CNetAddr::SerParams>>(ParamsStream<DataStream&, CNetAddr::SerParams>&)
Line
Count
Source
253
1.70k
    {
254
1.70k
        if (s.template GetParams<SerParams>().enc == Encoding::V2) {
255
0
            UnserializeV2Stream(s);
256
1.70k
        } else {
257
1.70k
            UnserializeV1Stream(s);
258
1.70k
        }
259
1.70k
    }
260
261
    /**
262
     * BIP155 network ids recognized by this software.
263
     */
264
    enum BIP155Network : uint8_t {
265
        IPV4 = 1,
266
        IPV6 = 2,
267
        TORV2 = 3,
268
        TORV3 = 4,
269
        I2P = 5,
270
        CJDNS = 6,
271
    };
272
273
    friend class CSubNet;
274
275
private:
276
    /**
277
     * Parse a Tor address and set this object to it.
278
     * @param[in] addr Address to parse, must be a valid C string, for example
279
     * pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.
280
     * @returns Whether the operation was successful.
281
     * @see CNetAddr::IsTor()
282
     */
283
    bool SetTor(std::string_view addr);
284
285
    /**
286
     * Parse an I2P address and set this object to it.
287
     * @param[in] addr Address to parse, must be a valid C string, for example
288
     * ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
289
     * @returns Whether the operation was successful.
290
     * @see CNetAddr::IsI2P()
291
     */
292
    bool SetI2P(std::string_view addr);
293
294
    /**
295
     * Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
296
     */
297
    static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE;
298
299
    /**
300
     * Maximum size of an address as defined in BIP155 (in bytes).
301
     * This is only the size of the address, not the entire CNetAddr object
302
     * when serialized.
303
     */
304
    static constexpr size_t MAX_ADDRV2_SIZE = 512;
305
306
    /**
307
     * Get the BIP155 network id of this address.
308
     * Must not be called for IsInternal() objects.
309
     * @returns BIP155 network id, except TORV2 which is no longer supported.
310
     */
311
    BIP155Network GetBIP155Network() const;
312
313
    /**
314
     * Set `m_net` from the provided BIP155 network id and size after validation.
315
     * @retval true the network was recognized, is valid and `m_net` was set
316
     * @retval false not recognised (from future?) and should be silently ignored
317
     * @throws std::ios_base::failure if the network is one of the BIP155 founding
318
     * networks (id 1..6) with wrong address size.
319
     */
320
    bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size);
321
322
    /**
323
     * Serialize in pre-ADDRv2/BIP155 format to an array.
324
     */
325
    void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const
326
263k
    {
327
263k
        size_t prefix_size;
328
329
263k
        switch (m_net) {
330
3.96k
        case NET_IPV6:
331
3.96k
            assert(m_addr.size() == sizeof(arr));
332
3.96k
            memcpy(arr, m_addr.data(), m_addr.size());
333
3.96k
            return;
334
259k
        case NET_IPV4:
335
259k
            prefix_size = sizeof(IPV4_IN_IPV6_PREFIX);
336
259k
            assert(prefix_size + m_addr.size() == sizeof(arr));
337
259k
            memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
338
259k
            memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
339
259k
            return;
340
3
        case NET_INTERNAL:
341
3
            prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX);
342
3
            assert(prefix_size + m_addr.size() == sizeof(arr));
343
3
            memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
344
3
            memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
345
3
            return;
346
1
        case NET_ONION:
347
1
        case NET_I2P:
348
1
        case NET_CJDNS:
349
1
            break;
350
0
        case NET_UNROUTABLE:
351
0
        case NET_MAX:
352
0
            assert(false);
353
263k
        } // no default case, so the compiler can warn about missing cases
354
355
        // Serialize ONION, I2P and CJDNS as all-zeros.
356
1
        memset(arr, 0x0, V1_SERIALIZATION_SIZE);
357
1
    }
358
359
    /**
360
     * Serialize in pre-ADDRv2/BIP155 format to a stream.
361
     */
362
    template <typename Stream>
363
    void SerializeV1Stream(Stream& s) const
364
22.4k
    {
365
22.4k
        uint8_t serialized[V1_SERIALIZATION_SIZE];
366
367
22.4k
        SerializeV1Array(serialized);
368
369
22.4k
        s << serialized;
370
22.4k
    }
void CNetAddr::SerializeV1Stream<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
364
5
    {
365
5
        uint8_t serialized[V1_SERIALIZATION_SIZE];
366
367
5
        SerializeV1Array(serialized);
368
369
5
        s << serialized;
370
5
    }
void CNetAddr::SerializeV1Stream<ParamsStream<DataStream&, CAddress::SerParams>>(ParamsStream<DataStream&, CAddress::SerParams>&) const
Line
Count
Source
364
8
    {
365
8
        uint8_t serialized[V1_SERIALIZATION_SIZE];
366
367
8
        SerializeV1Array(serialized);
368
369
8
        s << serialized;
370
8
    }
void CNetAddr::SerializeV1Stream<ParamsStream<VectorWriter&, CAddress::SerParams>>(ParamsStream<VectorWriter&, CAddress::SerParams>&) const
Line
Count
Source
364
1
    {
365
1
        uint8_t serialized[V1_SERIALIZATION_SIZE];
366
367
1
        SerializeV1Array(serialized);
368
369
1
        s << serialized;
370
1
    }
void CNetAddr::SerializeV1Stream<ParamsStream<VectorWriter&, CNetAddr::SerParams>>(ParamsStream<VectorWriter&, CNetAddr::SerParams>&) const
Line
Count
Source
364
3.43k
    {
365
3.43k
        uint8_t serialized[V1_SERIALIZATION_SIZE];
366
367
3.43k
        SerializeV1Array(serialized);
368
369
3.43k
        s << serialized;
370
3.43k
    }
Unexecuted instantiation: void CNetAddr::SerializeV1Stream<ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Unexecuted instantiation: void CNetAddr::SerializeV1Stream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>>(ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&) const
void CNetAddr::SerializeV1Stream<ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
364
18.9k
    {
365
18.9k
        uint8_t serialized[V1_SERIALIZATION_SIZE];
366
367
18.9k
        SerializeV1Array(serialized);
368
369
18.9k
        s << serialized;
370
18.9k
    }
371
372
    /**
373
     * Serialize as ADDRv2 / BIP155.
374
     */
375
    template <typename Stream>
376
    void SerializeV2Stream(Stream& s) const
377
100k
    {
378
100k
        if (IsInternal()) {
379
            // Serialize NET_INTERNAL as embedded in IPv6. We need to
380
            // serialize such addresses from addrman.
381
1
            s << static_cast<uint8_t>(BIP155Network::IPV6);
382
1
            s << COMPACTSIZE(ADDR_IPV6_SIZE);
383
1
            SerializeV1Stream(s);
384
1
            return;
385
1
        }
386
387
100k
        s << static_cast<uint8_t>(GetBIP155Network());
388
100k
        s << m_addr;
389
100k
    }
void CNetAddr::SerializeV2Stream<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
377
18
    {
378
18
        if (IsInternal()) {
379
            // Serialize NET_INTERNAL as embedded in IPv6. We need to
380
            // serialize such addresses from addrman.
381
0
            s << static_cast<uint8_t>(BIP155Network::IPV6);
382
0
            s << COMPACTSIZE(ADDR_IPV6_SIZE);
383
0
            SerializeV1Stream(s);
384
0
            return;
385
0
        }
386
387
18
        s << static_cast<uint8_t>(GetBIP155Network());
388
18
        s << m_addr;
389
18
    }
void CNetAddr::SerializeV2Stream<ParamsStream<DataStream&, CAddress::SerParams>>(ParamsStream<DataStream&, CAddress::SerParams>&) const
Line
Count
Source
377
20
    {
378
20
        if (IsInternal()) {
379
            // Serialize NET_INTERNAL as embedded in IPv6. We need to
380
            // serialize such addresses from addrman.
381
1
            s << static_cast<uint8_t>(BIP155Network::IPV6);
382
1
            s << COMPACTSIZE(ADDR_IPV6_SIZE);
383
1
            SerializeV1Stream(s);
384
1
            return;
385
1
        }
386
387
19
        s << static_cast<uint8_t>(GetBIP155Network());
388
19
        s << m_addr;
389
19
    }
Unexecuted instantiation: void CNetAddr::SerializeV2Stream<ParamsStream<VectorWriter&, CAddress::SerParams>>(ParamsStream<VectorWriter&, CAddress::SerParams>&) const
Unexecuted instantiation: void CNetAddr::SerializeV2Stream<ParamsStream<VectorWriter&, CNetAddr::SerParams>>(ParamsStream<VectorWriter&, CNetAddr::SerParams>&) const
void CNetAddr::SerializeV2Stream<ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
377
50.3k
    {
378
50.3k
        if (IsInternal()) {
379
            // Serialize NET_INTERNAL as embedded in IPv6. We need to
380
            // serialize such addresses from addrman.
381
0
            s << static_cast<uint8_t>(BIP155Network::IPV6);
382
0
            s << COMPACTSIZE(ADDR_IPV6_SIZE);
383
0
            SerializeV1Stream(s);
384
0
            return;
385
0
        }
386
387
50.3k
        s << static_cast<uint8_t>(GetBIP155Network());
388
50.3k
        s << m_addr;
389
50.3k
    }
void CNetAddr::SerializeV2Stream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>>(ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&) const
Line
Count
Source
377
50.3k
    {
378
50.3k
        if (IsInternal()) {
379
            // Serialize NET_INTERNAL as embedded in IPv6. We need to
380
            // serialize such addresses from addrman.
381
0
            s << static_cast<uint8_t>(BIP155Network::IPV6);
382
0
            s << COMPACTSIZE(ADDR_IPV6_SIZE);
383
0
            SerializeV1Stream(s);
384
0
            return;
385
0
        }
386
387
50.3k
        s << static_cast<uint8_t>(GetBIP155Network());
388
50.3k
        s << m_addr;
389
50.3k
    }
void CNetAddr::SerializeV2Stream<ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>&) const
Line
Count
Source
377
33
    {
378
33
        if (IsInternal()) {
379
            // Serialize NET_INTERNAL as embedded in IPv6. We need to
380
            // serialize such addresses from addrman.
381
0
            s << static_cast<uint8_t>(BIP155Network::IPV6);
382
0
            s << COMPACTSIZE(ADDR_IPV6_SIZE);
383
0
            SerializeV1Stream(s);
384
0
            return;
385
0
        }
386
387
33
        s << static_cast<uint8_t>(GetBIP155Network());
388
33
        s << m_addr;
389
33
    }
390
391
    /**
392
     * Unserialize from a pre-ADDRv2/BIP155 format from an array.
393
     *
394
     * This function is only called from UnserializeV1Stream() and is a wrapper
395
     * for SetLegacyIPv6(); however, we keep it for symmetry with
396
     * SerializeV1Array() to have pairs of ser/unser functions and to make clear
397
     * that if one is altered, a corresponding reverse modification should be
398
     * applied to the other.
399
     */
400
    void UnserializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE])
401
7.59k
    {
402
        // Use SetLegacyIPv6() so that m_net is set correctly. For example
403
        // ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
404
7.59k
        SetLegacyIPv6(arr);
405
7.59k
    }
406
407
    /**
408
     * Unserialize from a pre-ADDRv2/BIP155 format from a stream.
409
     */
410
    template <typename Stream>
411
    void UnserializeV1Stream(Stream& s)
412
7.59k
    {
413
7.59k
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
7.59k
        s >> serialized;
416
417
7.59k
        UnserializeV1Array(serialized);
418
7.59k
    }
void CNetAddr::UnserializeV1Stream<ParamsStream<DataStream&, CAddress::SerParams>>(ParamsStream<DataStream&, CAddress::SerParams>&)
Line
Count
Source
412
1
    {
413
1
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
1
        s >> serialized;
416
417
1
        UnserializeV1Array(serialized);
418
1
    }
void CNetAddr::UnserializeV1Stream<ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
412
4
    {
413
4
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
4
        s >> serialized;
416
417
4
        UnserializeV1Array(serialized);
418
4
    }
Unexecuted instantiation: void CNetAddr::UnserializeV1Stream<ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Unexecuted instantiation: void CNetAddr::UnserializeV1Stream<ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Unexecuted instantiation: void CNetAddr::UnserializeV1Stream<ParamsStream<AutoFile&, CAddress::SerParams>>(ParamsStream<AutoFile&, CAddress::SerParams>&)
Unexecuted instantiation: void CNetAddr::UnserializeV1Stream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>>(ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&)
void CNetAddr::UnserializeV1Stream<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
412
5.88k
    {
413
5.88k
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
5.88k
        s >> serialized;
416
417
5.88k
        UnserializeV1Array(serialized);
418
5.88k
    }
void CNetAddr::UnserializeV1Stream<ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
412
1
    {
413
1
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
1
        s >> serialized;
416
417
1
        UnserializeV1Array(serialized);
418
1
    }
void CNetAddr::UnserializeV1Stream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>>(ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&)
Line
Count
Source
412
1
    {
413
1
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
1
        s >> serialized;
416
417
1
        UnserializeV1Array(serialized);
418
1
    }
Unexecuted instantiation: void CNetAddr::UnserializeV1Stream<ParamsStream<SpanReader, CAddress::SerParams>>(ParamsStream<SpanReader, CAddress::SerParams>&)
void CNetAddr::UnserializeV1Stream<ParamsStream<DataStream&, CNetAddr::SerParams>>(ParamsStream<DataStream&, CNetAddr::SerParams>&)
Line
Count
Source
412
1.70k
    {
413
1.70k
        uint8_t serialized[V1_SERIALIZATION_SIZE];
414
415
1.70k
        s >> serialized;
416
417
1.70k
        UnserializeV1Array(serialized);
418
1.70k
    }
419
420
    /**
421
     * Unserialize from a ADDRv2 / BIP155 format.
422
     */
423
    template <typename Stream>
424
    void UnserializeV2Stream(Stream& s)
425
42.2k
    {
426
42.2k
        uint8_t bip155_net;
427
42.2k
        s >> bip155_net;
428
429
42.2k
        size_t address_size;
430
42.2k
        s >> COMPACTSIZE(address_size);
431
432
42.2k
        if (address_size > MAX_ADDRV2_SIZE) {
433
3
            throw std::ios_base::failure(strprintf(
434
3
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
3
        }
436
437
42.2k
        m_scope_id = 0;
438
439
42.2k
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
42.2k
            m_addr.resize(address_size);
441
42.2k
            s >> std::span{m_addr};
442
443
42.2k
            if (m_net != NET_IPV6) {
444
42.1k
                return;
445
42.1k
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
61
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
1
                m_net = NET_INTERNAL;
454
1
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
1
                        ADDR_INTERNAL_SIZE);
456
1
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
1
                return;
458
1
            }
459
460
60
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
60
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
57
                return;
463
57
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
60
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
9
            s.ignore(address_size);
471
9
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
12
        m_net = NET_IPV6;
476
12
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
12
    }
void CNetAddr::UnserializeV2Stream<ParamsStream<DataStream&, CAddress::SerParams>>(ParamsStream<DataStream&, CAddress::SerParams>&)
Line
Count
Source
425
32
    {
426
32
        uint8_t bip155_net;
427
32
        s >> bip155_net;
428
429
32
        size_t address_size;
430
32
        s >> COMPACTSIZE(address_size);
431
432
32
        if (address_size > MAX_ADDRV2_SIZE) {
433
2
            throw std::ios_base::failure(strprintf(
434
2
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
2
        }
436
437
30
        m_scope_id = 0;
438
439
30
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
22
            m_addr.resize(address_size);
441
22
            s >> std::span{m_addr};
442
443
22
            if (m_net != NET_IPV6) {
444
8
                return;
445
8
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
14
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
1
                m_net = NET_INTERNAL;
454
1
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
1
                        ADDR_INTERNAL_SIZE);
456
1
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
1
                return;
458
1
            }
459
460
13
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
13
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
10
                return;
463
10
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
13
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
8
            s.ignore(address_size);
471
8
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
11
        m_net = NET_IPV6;
476
11
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
11
    }
void CNetAddr::UnserializeV2Stream<ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
425
3
    {
426
3
        uint8_t bip155_net;
427
3
        s >> bip155_net;
428
429
3
        size_t address_size;
430
3
        s >> COMPACTSIZE(address_size);
431
432
3
        if (address_size > MAX_ADDRV2_SIZE) {
433
0
            throw std::ios_base::failure(strprintf(
434
0
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
0
        }
436
437
3
        m_scope_id = 0;
438
439
3
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
3
            m_addr.resize(address_size);
441
3
            s >> std::span{m_addr};
442
443
3
            if (m_net != NET_IPV6) {
444
0
                return;
445
0
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
3
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
0
                m_net = NET_INTERNAL;
454
0
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
0
                        ADDR_INTERNAL_SIZE);
456
0
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
0
                return;
458
0
            }
459
460
3
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
3
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
3
                return;
463
3
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
3
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
0
            s.ignore(address_size);
471
0
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
0
        m_net = NET_IPV6;
476
0
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
0
    }
void CNetAddr::UnserializeV2Stream<ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
425
20.5k
    {
426
20.5k
        uint8_t bip155_net;
427
20.5k
        s >> bip155_net;
428
429
20.5k
        size_t address_size;
430
20.5k
        s >> COMPACTSIZE(address_size);
431
432
20.5k
        if (address_size > MAX_ADDRV2_SIZE) {
433
0
            throw std::ios_base::failure(strprintf(
434
0
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
0
        }
436
437
20.5k
        m_scope_id = 0;
438
439
20.5k
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
20.5k
            m_addr.resize(address_size);
441
20.5k
            s >> std::span{m_addr};
442
443
20.5k
            if (m_net != NET_IPV6) {
444
20.5k
                return;
445
20.5k
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
22
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
0
                m_net = NET_INTERNAL;
454
0
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
0
                        ADDR_INTERNAL_SIZE);
456
0
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
0
                return;
458
0
            }
459
460
22
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
22
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
22
                return;
463
22
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
22
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
0
            s.ignore(address_size);
471
0
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
0
        m_net = NET_IPV6;
476
0
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
0
    }
Unexecuted instantiation: void CNetAddr::UnserializeV2Stream<ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Unexecuted instantiation: void CNetAddr::UnserializeV2Stream<ParamsStream<AutoFile&, CAddress::SerParams>>(ParamsStream<AutoFile&, CAddress::SerParams>&)
void CNetAddr::UnserializeV2Stream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>>(ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&)
Line
Count
Source
425
20.5k
    {
426
20.5k
        uint8_t bip155_net;
427
20.5k
        s >> bip155_net;
428
429
20.5k
        size_t address_size;
430
20.5k
        s >> COMPACTSIZE(address_size);
431
432
20.5k
        if (address_size > MAX_ADDRV2_SIZE) {
433
0
            throw std::ios_base::failure(strprintf(
434
0
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
0
        }
436
437
20.5k
        m_scope_id = 0;
438
439
20.5k
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
20.5k
            m_addr.resize(address_size);
441
20.5k
            s >> std::span{m_addr};
442
443
20.5k
            if (m_net != NET_IPV6) {
444
20.5k
                return;
445
20.5k
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
22
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
0
                m_net = NET_INTERNAL;
454
0
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
0
                        ADDR_INTERNAL_SIZE);
456
0
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
0
                return;
458
0
            }
459
460
22
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
22
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
22
                return;
463
22
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
22
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
0
            s.ignore(address_size);
471
0
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
0
        m_net = NET_IPV6;
476
0
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
0
    }
void CNetAddr::UnserializeV2Stream<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
425
1.03k
    {
426
1.03k
        uint8_t bip155_net;
427
1.03k
        s >> bip155_net;
428
429
1.03k
        size_t address_size;
430
1.03k
        s >> COMPACTSIZE(address_size);
431
432
1.03k
        if (address_size > MAX_ADDRV2_SIZE) {
433
1
            throw std::ios_base::failure(strprintf(
434
1
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
1
        }
436
437
1.03k
        m_scope_id = 0;
438
439
1.03k
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
1.03k
            m_addr.resize(address_size);
441
1.03k
            s >> std::span{m_addr};
442
443
1.03k
            if (m_net != NET_IPV6) {
444
1.03k
                return;
445
1.03k
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
0
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
0
                m_net = NET_INTERNAL;
454
0
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
0
                        ADDR_INTERNAL_SIZE);
456
0
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
0
                return;
458
0
            }
459
460
0
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
0
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
0
                return;
463
0
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
1
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
1
            s.ignore(address_size);
471
1
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
1
        m_net = NET_IPV6;
476
1
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
1
    }
void CNetAddr::UnserializeV2Stream<ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>>(ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>&)
Line
Count
Source
425
3
    {
426
3
        uint8_t bip155_net;
427
3
        s >> bip155_net;
428
429
3
        size_t address_size;
430
3
        s >> COMPACTSIZE(address_size);
431
432
3
        if (address_size > MAX_ADDRV2_SIZE) {
433
0
            throw std::ios_base::failure(strprintf(
434
0
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
0
        }
436
437
3
        m_scope_id = 0;
438
439
3
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
3
            m_addr.resize(address_size);
441
3
            s >> std::span{m_addr};
442
443
3
            if (m_net != NET_IPV6) {
444
3
                return;
445
3
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
0
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
0
                m_net = NET_INTERNAL;
454
0
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
0
                        ADDR_INTERNAL_SIZE);
456
0
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
0
                return;
458
0
            }
459
460
0
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
0
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
0
                return;
463
0
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
0
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
0
            s.ignore(address_size);
471
0
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
0
        m_net = NET_IPV6;
476
0
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
0
    }
void CNetAddr::UnserializeV2Stream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>>(ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&)
Line
Count
Source
425
3
    {
426
3
        uint8_t bip155_net;
427
3
        s >> bip155_net;
428
429
3
        size_t address_size;
430
3
        s >> COMPACTSIZE(address_size);
431
432
3
        if (address_size > MAX_ADDRV2_SIZE) {
433
0
            throw std::ios_base::failure(strprintf(
434
0
                "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
435
0
        }
436
437
3
        m_scope_id = 0;
438
439
3
        if (SetNetFromBIP155Network(bip155_net, address_size)) {
440
3
            m_addr.resize(address_size);
441
3
            s >> std::span{m_addr};
442
443
3
            if (m_net != NET_IPV6) {
444
3
                return;
445
3
            }
446
447
            // Do some special checks on IPv6 addresses.
448
449
            // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
450
            // gossiped but could be coming from addrman, when unserializing from
451
            // disk.
452
0
            if (util::HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
453
0
                m_net = NET_INTERNAL;
454
0
                memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
455
0
                        ADDR_INTERNAL_SIZE);
456
0
                m_addr.resize(ADDR_INTERNAL_SIZE);
457
0
                return;
458
0
            }
459
460
0
            if (!util::HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
461
0
                !util::HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
462
0
                return;
463
0
            }
464
465
            // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
466
            // encoding). Unserialize as !IsValid(), thus ignoring them.
467
0
        } else {
468
            // If we receive an unknown BIP155 network id (from the future?) then
469
            // ignore the address - unserialize as !IsValid().
470
0
            s.ignore(address_size);
471
0
        }
472
473
        // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
474
        // will not be gossiped, but continue reading next addresses from the stream.
475
0
        m_net = NET_IPV6;
476
0
        m_addr.assign(ADDR_IPV6_SIZE, 0x0);
477
0
    }
Unexecuted instantiation: void CNetAddr::UnserializeV2Stream<ParamsStream<SpanReader, CAddress::SerParams>>(ParamsStream<SpanReader, CAddress::SerParams>&)
Unexecuted instantiation: void CNetAddr::UnserializeV2Stream<ParamsStream<DataStream&, CNetAddr::SerParams>>(ParamsStream<DataStream&, CNetAddr::SerParams>&)
478
};
479
480
class CSubNet
481
{
482
protected:
483
    /// Network (base) address
484
    CNetAddr network;
485
    /// Netmask, in network byte order
486
    uint8_t netmask[16];
487
    /// Is this value valid? (only used to signal parse errors)
488
    bool valid;
489
490
public:
491
    /**
492
     * Construct an invalid subnet (empty, `Match()` always returns false).
493
     */
494
    CSubNet();
495
496
    /**
497
     * Construct from a given network start and number of bits (CIDR mask).
498
     * @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is
499
     * created.
500
     * @param[in] mask CIDR mask, must be in [0, 32] for IPv4 addresses and in [0, 128] for
501
     * IPv6 addresses. Otherwise an invalid subnet is created.
502
     */
503
    CSubNet(const CNetAddr& addr, uint8_t mask);
504
505
    /**
506
     * Construct from a given network start and mask.
507
     * @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is
508
     * created.
509
     * @param[in] mask Network mask, must be of the same type as `addr` and not contain 0-bits
510
     * followed by 1-bits. Otherwise an invalid subnet is created.
511
     */
512
    CSubNet(const CNetAddr& addr, const CNetAddr& mask);
513
514
    /**
515
     * Construct a single-host subnet.
516
     * @param[in] addr The sole address to be contained in the subnet, can also be non-IPv[46].
517
     */
518
    explicit CSubNet(const CNetAddr& addr);
519
520
    bool Match(const CNetAddr& addr) const;
521
522
    std::string ToString() const;
523
    bool IsValid() const;
524
525
    friend bool operator==(const CSubNet& a, const CSubNet& b);
526
    friend bool operator<(const CSubNet& a, const CSubNet& b);
527
};
528
529
/** A combination of a network address (CNetAddr) and a (TCP) port */
530
class CService : public CNetAddr
531
{
532
protected:
533
    uint16_t port; // host order
534
535
public:
536
    CService();
537
    CService(const CNetAddr& ip, uint16_t port);
538
    CService(const struct in_addr& ipv4Addr, uint16_t port);
539
    explicit CService(const struct sockaddr_in& addr);
540
    uint16_t GetPort() const;
541
    bool GetSockAddr(struct sockaddr* paddr, socklen_t* addrlen) const;
542
    /**
543
     * Set CService from a network sockaddr.
544
     * @param[in] paddr Pointer to sockaddr structure
545
     * @param[in] addrlen Length of sockaddr structure in bytes. This will be checked to exactly match the length of
546
     * a socket address of the provided family, unless std::nullopt is passed
547
     * @returns true on success
548
     */
549
    bool SetSockAddr(const struct sockaddr* paddr, socklen_t addrlen);
550
    /**
551
     * Get the address family
552
     * @returns AF_UNSPEC if unspecified
553
     */
554
    [[nodiscard]] sa_family_t GetSAFamily() const;
555
    friend bool operator==(const CService& a, const CService& b);
556
    friend bool operator<(const CService& a, const CService& b);
557
    std::vector<unsigned char> GetKey() const;
558
    std::string ToStringAddrPort() const;
559
560
    CService(const struct in6_addr& ipv6Addr, uint16_t port);
561
    explicit CService(const struct sockaddr_in6& addr);
562
563
    SERIALIZE_METHODS(CService, obj)
564
101k
    {
565
101k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
101k
    }
void CService::SerializationOps<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>, CService const, ActionSerialize>(CService const&, ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionSerialize)
Line
Count
Source
564
23
    {
565
23
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
23
    }
void CService::SerializationOps<ParamsStream<VectorWriter&, CAddress::SerParams>, CService const, ActionSerialize>(CService const&, ParamsStream<VectorWriter&, CAddress::SerParams>&, ActionSerialize)
Line
Count
Source
564
1
    {
565
1
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
1
    }
void CService::SerializationOps<ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<ParamsStream<SpanReader&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionUnserialize)
Line
Count
Source
564
7
    {
565
7
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
7
    }
void CService::SerializationOps<ParamsStream<VectorWriter&, CNetAddr::SerParams>, CService const, ActionSerialize>(CService const&, ParamsStream<VectorWriter&, CNetAddr::SerParams>&, ActionSerialize)
Line
Count
Source
564
3.43k
    {
565
3.43k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
3.43k
    }
void CService::SerializationOps<ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>, CService const, ActionSerialize>(CService const&, ParamsStream<ParamsStream<HashedSourceWriter<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionSerialize)
Line
Count
Source
564
50.3k
    {
565
50.3k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
50.3k
    }
void CService::SerializationOps<ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<ParamsStream<HashVerifier<AutoFile>&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionUnserialize)
Line
Count
Source
564
20.5k
    {
565
20.5k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
20.5k
    }
Unexecuted instantiation: void CService::SerializationOps<ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<ParamsStream<AutoFile&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionUnserialize)
void CService::SerializationOps<ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<ParamsStream<DataStream&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionUnserialize)
Line
Count
Source
564
6.92k
    {
565
6.92k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
6.92k
    }
void CService::SerializationOps<ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<ParamsStream<HashVerifier<DataStream>&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionUnserialize)
Line
Count
Source
564
4
    {
565
4
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
4
    }
Unexecuted instantiation: void CService::SerializationOps<ParamsStream<SpanReader, CAddress::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<SpanReader, CAddress::SerParams>&, ActionUnserialize)
void CService::SerializationOps<ParamsStream<DataStream&, CNetAddr::SerParams>, CService, ActionUnserialize>(CService&, ParamsStream<DataStream&, CNetAddr::SerParams>&, ActionUnserialize)
Line
Count
Source
564
1.70k
    {
565
1.70k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
1.70k
    }
void CService::SerializationOps<ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>, CService const, ActionSerialize>(CService const&, ParamsStream<ParamsStream<VectorWriter&, CAddress::SerParams>&, CNetAddr::SerParams>&, ActionSerialize)
Line
Count
Source
564
19.0k
    {
565
19.0k
        READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));
566
19.0k
    }
567
568
    friend class CServiceHash;
569
    friend CService MaybeFlipIPv6toCJDNS(const CService& service);
570
};
571
572
class CServiceHash
573
{
574
public:
575
    CServiceHash()
576
1.86k
        : m_salt_k0{FastRandomContext().rand64()},
577
1.86k
          m_salt_k1{FastRandomContext().rand64()}
578
1.86k
    {
579
1.86k
    }
580
581
53
    CServiceHash(uint64_t salt_k0, uint64_t salt_k1) : m_salt_k0{salt_k0}, m_salt_k1{salt_k1} {}
582
583
    size_t operator()(const CService& a) const noexcept
584
347k
    {
585
347k
        CSipHasher hasher(m_salt_k0, m_salt_k1);
586
347k
        hasher.Write(a.m_net);
587
347k
        hasher.Write(a.port);
588
347k
        hasher.Write(a.m_addr);
589
347k
        return static_cast<size_t>(hasher.Finalize());
590
347k
    }
591
592
private:
593
    const uint64_t m_salt_k0;
594
    const uint64_t m_salt_k1;
595
};
596
597
#endif // BITCOIN_NETADDRESS_H