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