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