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