Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2020 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 <prevector.h>
# 15 : : #include <serialize.h>
# 16 : : #include <tinyformat.h>
# 17 : : #include <util/strencodings.h>
# 18 : : #include <util/string.h>
# 19 : :
# 20 : : #include <array>
# 21 : : #include <cstdint>
# 22 : : #include <ios>
# 23 : : #include <string>
# 24 : : #include <vector>
# 25 : :
# 26 : : /**
# 27 : : * A flag that is ORed into the protocol version to designate that addresses
# 28 : : * should be serialized in (unserialized from) v2 format (BIP155).
# 29 : : * Make sure that this does not collide with any of the values in `version.h`
# 30 : : * or with `SERIALIZE_TRANSACTION_NO_WITNESS`.
# 31 : : */
# 32 : : static constexpr int ADDRV2_FORMAT = 0x20000000;
# 33 : :
# 34 : : /**
# 35 : : * A network type.
# 36 : : * @note An address may belong to more than one network, for example `10.0.0.1`
# 37 : : * belongs to both `NET_UNROUTABLE` and `NET_IPV4`.
# 38 : : * Keep these sequential starting from 0 and `NET_MAX` as the last entry.
# 39 : : * We have loops like `for (int i = 0; i < NET_MAX; ++i)` that expect to iterate
# 40 : : * over all enum values and also `GetExtNetwork()` "extends" this enum by
# 41 : : * introducing standalone constants starting from `NET_MAX`.
# 42 : : */
# 43 : : enum Network
# 44 : : {
# 45 : : /// Addresses from these networks are not publicly routable on the global Internet.
# 46 : : NET_UNROUTABLE = 0,
# 47 : :
# 48 : : /// IPv4
# 49 : : NET_IPV4,
# 50 : :
# 51 : : /// IPv6
# 52 : : NET_IPV6,
# 53 : :
# 54 : : /// TOR (v2 or v3)
# 55 : : NET_ONION,
# 56 : :
# 57 : : /// I2P
# 58 : : NET_I2P,
# 59 : :
# 60 : : /// CJDNS
# 61 : : NET_CJDNS,
# 62 : :
# 63 : : /// A set of addresses that represent the hash of a string or FQDN. We use
# 64 : : /// them in CAddrMan to keep track of which DNS seeds were used.
# 65 : : NET_INTERNAL,
# 66 : :
# 67 : : /// Dummy value to indicate the number of NET_* constants.
# 68 : : NET_MAX,
# 69 : : };
# 70 : :
# 71 : : /// Prefix of an IPv6 address when it contains an embedded IPv4 address.
# 72 : : /// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
# 73 : : static const std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{
# 74 : : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF
# 75 : : };
# 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 : :
# 85 : : /// Prefix of an IPv6 address when it contains an embedded "internal" address.
# 86 : : /// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
# 87 : : /// The prefix comes from 0xFD + SHA256("bitcoin")[0:5].
# 88 : : /// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
# 89 : : /// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
# 90 : : static const std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{
# 91 : : 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 // 0xFD + sha256("bitcoin")[0:5].
# 92 : : };
# 93 : :
# 94 : : /// Size of IPv4 address (in bytes).
# 95 : : static constexpr size_t ADDR_IPV4_SIZE = 4;
# 96 : :
# 97 : : /// Size of IPv6 address (in bytes).
# 98 : : static constexpr size_t ADDR_IPV6_SIZE = 16;
# 99 : :
# 100 : : /// Size of TORv2 address (in bytes).
# 101 : : static constexpr size_t ADDR_TORV2_SIZE = 10;
# 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 : : /**
# 117 : : * Network address.
# 118 : : */
# 119 : : class CNetAddr
# 120 : : {
# 121 : : protected:
# 122 : : /**
# 123 : : * Raw representation of the network address.
# 124 : : * In network byte order (big endian) for IPv4 and IPv6.
# 125 : : */
# 126 : : prevector<ADDR_IPV6_SIZE, uint8_t> m_addr{ADDR_IPV6_SIZE, 0x0};
# 127 : :
# 128 : : /**
# 129 : : * Network to which this address belongs.
# 130 : : */
# 131 : : Network m_net{NET_IPV6};
# 132 : :
# 133 : : /**
# 134 : : * Scope id if scoped/link-local IPV6 address.
# 135 : : * See https://tools.ietf.org/html/rfc4007
# 136 : : */
# 137 : : uint32_t m_scope_id{0};
# 138 : :
# 139 : : public:
# 140 : : CNetAddr();
# 141 : : explicit CNetAddr(const struct in_addr& ipv4Addr);
# 142 : : void SetIP(const CNetAddr& ip);
# 143 : :
# 144 : : /**
# 145 : : * Set from a legacy IPv6 address.
# 146 : : * Legacy IPv6 address may be a normal IPv6 address, or another address
# 147 : : * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy
# 148 : : * `addr` encoding.
# 149 : : */
# 150 : : void SetLegacyIPv6(Span<const uint8_t> ipv6);
# 151 : :
# 152 : : bool SetInternal(const std::string& name);
# 153 : :
# 154 : : /**
# 155 : : * Parse a Tor or I2P address and set this object to it.
# 156 : : * @param[in] addr Address to parse, for example
# 157 : : * pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion or
# 158 : : * ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
# 159 : : * @returns Whether the operation was successful.
# 160 : : * @see CNetAddr::IsTor(), CNetAddr::IsI2P()
# 161 : : */
# 162 : : bool SetSpecial(const std::string& addr);
# 163 : :
# 164 : : bool IsBindAny() const; // INADDR_ANY equivalent
# 165 : : bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
# 166 : : bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
# 167 : : bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
# 168 : : bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
# 169 : : bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
# 170 : : bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
# 171 : : bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
# 172 : : bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
# 173 : : bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
# 174 : : bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
# 175 : : bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
# 176 : : bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
# 177 : : bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
# 178 : : bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
# 179 : : bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
# 180 : : bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
# 181 : : bool IsHeNet() const; // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
# 182 : : bool IsTor() const;
# 183 : : bool IsI2P() const;
# 184 : : bool IsCJDNS() const;
# 185 : : bool IsLocal() const;
# 186 : : bool IsRoutable() const;
# 187 : : bool IsInternal() const;
# 188 : : bool IsValid() const;
# 189 : :
# 190 : : /**
# 191 : : * Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
# 192 : : */
# 193 : : bool IsAddrV1Compatible() const;
# 194 : :
# 195 : : enum Network GetNetwork() const;
# 196 : : std::string ToString() const;
# 197 : : std::string ToStringIP() const;
# 198 : : uint64_t GetHash() 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 : : // The AS on the BGP path to the node we use to diversify
# 208 : : // peers in AddrMan bucketing based on the AS infrastructure.
# 209 : : // The ip->AS mapping depends on how asmap is constructed.
# 210 : : uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
# 211 : :
# 212 : : std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const;
# 213 : : std::vector<unsigned char> GetAddrBytes() const;
# 214 : : int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
# 215 : :
# 216 : : explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
# 217 : : bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
# 218 : :
# 219 : : friend bool operator==(const CNetAddr& a, const CNetAddr& b);
# 220 : 0 : friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
# 221 : : friend bool operator<(const CNetAddr& a, const CNetAddr& b);
# 222 : :
# 223 : : /**
# 224 : : * Whether this address should be relayed to other peers even if we can't reach it ourselves.
# 225 : : */
# 226 : : bool IsRelayable() const
# 227 : 0 : {
# 228 [ # # ][ # # ]: 0 : return IsIPv4() || IsIPv6() || IsTor();
# [ # # ]
# 229 : 0 : }
# 230 : :
# 231 : : /**
# 232 : : * Serialize to a stream.
# 233 : : */
# 234 : : template <typename Stream>
# 235 : : void Serialize(Stream& s) const
# 236 : 137448 : {
# 237 [ - + ][ + + ]: 137448 : if (s.GetVersion() & ADDRV2_FORMAT) {
# [ + - ][ + - ]
# [ - + ][ + - ]
# [ + + ]
# 238 : 129156 : SerializeV2Stream(s);
# 239 : 129156 : } else {
# 240 : 8292 : SerializeV1Stream(s);
# 241 : 8292 : }
# 242 : 137448 : }
# 243 : :
# 244 : : /**
# 245 : : * Unserialize from a stream.
# 246 : : */
# 247 : : template <typename Stream>
# 248 : : void Unserialize(Stream& s)
# 249 : 5626 : {
# 250 [ + + ][ + + ]: 5626 : if (s.GetVersion() & ADDRV2_FORMAT) {
# [ + - ][ - + ]
# [ + + ]
# 251 : 2669 : UnserializeV2Stream(s);
# 252 : 2957 : } else {
# 253 : 2957 : UnserializeV1Stream(s);
# 254 : 2957 : }
# 255 : 5626 : }
# 256 : :
# 257 : : friend class CSubNet;
# 258 : :
# 259 : : private:
# 260 : : /**
# 261 : : * Parse a Tor address and set this object to it.
# 262 : : * @param[in] addr Address to parse, must be a valid C string, for example
# 263 : : * pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion or
# 264 : : * 6hzph5hv6337r6p2.onion.
# 265 : : * @returns Whether the operation was successful.
# 266 : : * @see CNetAddr::IsTor()
# 267 : : */
# 268 : : bool SetTor(const std::string& addr);
# 269 : :
# 270 : : /**
# 271 : : * Parse an I2P address and set this object to it.
# 272 : : * @param[in] addr Address to parse, must be a valid C string, for example
# 273 : : * ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
# 274 : : * @returns Whether the operation was successful.
# 275 : : * @see CNetAddr::IsI2P()
# 276 : : */
# 277 : : bool SetI2P(const std::string& addr);
# 278 : :
# 279 : : /**
# 280 : : * BIP155 network ids recognized by this software.
# 281 : : */
# 282 : : enum BIP155Network : uint8_t {
# 283 : : IPV4 = 1,
# 284 : : IPV6 = 2,
# 285 : : TORV2 = 3,
# 286 : : TORV3 = 4,
# 287 : : I2P = 5,
# 288 : : CJDNS = 6,
# 289 : : };
# 290 : :
# 291 : : /**
# 292 : : * Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
# 293 : : */
# 294 : : static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE;
# 295 : :
# 296 : : /**
# 297 : : * Maximum size of an address as defined in BIP155 (in bytes).
# 298 : : * This is only the size of the address, not the entire CNetAddr object
# 299 : : * when serialized.
# 300 : : */
# 301 : : static constexpr size_t MAX_ADDRV2_SIZE = 512;
# 302 : :
# 303 : : /**
# 304 : : * Get the BIP155 network id of this address.
# 305 : : * Must not be called for IsInternal() objects.
# 306 : : * @returns BIP155 network id
# 307 : : */
# 308 : : BIP155Network GetBIP155Network() const;
# 309 : :
# 310 : : /**
# 311 : : * Set `m_net` from the provided BIP155 network id and size after validation.
# 312 : : * @retval true the network was recognized, is valid and `m_net` was set
# 313 : : * @retval false not recognised (from future?) and should be silently ignored
# 314 : : * @throws std::ios_base::failure if the network is one of the BIP155 founding
# 315 : : * networks (id 1..6) with wrong address size.
# 316 : : */
# 317 : : bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size);
# 318 : :
# 319 : : /**
# 320 : : * Serialize in pre-ADDRv2/BIP155 format to an array.
# 321 : : */
# 322 : : void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const
# 323 : 288051676 : {
# 324 : 288051676 : size_t prefix_size;
# 325 : :
# 326 [ - + ]: 288051676 : switch (m_net) {
# 327 [ + + ]: 31466 : case NET_IPV6:
# 328 : 31466 : assert(m_addr.size() == sizeof(arr));
# 329 : 31466 : memcpy(arr, m_addr.data(), m_addr.size());
# 330 : 31466 : return;
# 331 [ + + ]: 288020197 : case NET_IPV4:
# 332 : 288020197 : prefix_size = sizeof(IPV4_IN_IPV6_PREFIX);
# 333 : 288020197 : assert(prefix_size + m_addr.size() == sizeof(arr));
# 334 : 288020197 : memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
# 335 : 288020197 : memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
# 336 : 288020197 : return;
# 337 [ + + ]: 9 : case NET_ONION:
# 338 [ + + ]: 9 : if (m_addr.size() == ADDR_TORV3_SIZE) {
# 339 : 4 : break;
# 340 : 4 : }
# 341 : 5 : prefix_size = sizeof(TORV2_IN_IPV6_PREFIX);
# 342 : 5 : assert(prefix_size + m_addr.size() == sizeof(arr));
# 343 : 5 : memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size);
# 344 : 5 : memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
# 345 : 5 : return;
# 346 [ + + ]: 5 : case NET_INTERNAL:
# 347 : 4 : prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX);
# 348 : 4 : assert(prefix_size + m_addr.size() == sizeof(arr));
# 349 : 4 : memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
# 350 : 4 : memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
# 351 : 4 : return;
# 352 [ - + ]: 5 : case NET_I2P:
# 353 : 0 : break;
# 354 [ - + ]: 5 : case NET_CJDNS:
# 355 : 0 : break;
# 356 [ - + ]: 5 : case NET_UNROUTABLE:
# 357 [ - + ]: 0 : case NET_MAX:
# 358 : 0 : assert(false);
# 359 : 288051676 : } // no default case, so the compiler can warn about missing cases
# 360 : :
# 361 : : // Serialize TORv3, I2P and CJDNS as all-zeros.
# 362 : 288051676 : memset(arr, 0x0, V1_SERIALIZATION_SIZE);
# 363 : 4 : }
# 364 : :
# 365 : : /**
# 366 : : * Serialize in pre-ADDRv2/BIP155 format to a stream.
# 367 : : */
# 368 : : template <typename Stream>
# 369 : : void SerializeV1Stream(Stream& s) const
# 370 : 8294 : {
# 371 : 8294 : uint8_t serialized[V1_SERIALIZATION_SIZE];
# 372 : :
# 373 : 8294 : SerializeV1Array(serialized);
# 374 : :
# 375 : 8294 : s << serialized;
# 376 : 8294 : }
# 377 : :
# 378 : : /**
# 379 : : * Serialize as ADDRv2 / BIP155.
# 380 : : */
# 381 : : template <typename Stream>
# 382 : : void SerializeV2Stream(Stream& s) const
# 383 : 129156 : {
# 384 [ - + ][ + + ]: 129156 : if (IsInternal()) {
# [ # # ][ - + ]
# [ # # ][ - + ]
# [ - + ]
# 385 : : // Serialize NET_INTERNAL as embedded in IPv6. We need to
# 386 : : // serialize such addresses from addrman.
# 387 : 2 : s << static_cast<uint8_t>(BIP155Network::IPV6);
# 388 : 2 : s << COMPACTSIZE(ADDR_IPV6_SIZE);
# 389 : 2 : SerializeV1Stream(s);
# 390 : 2 : return;
# 391 : 2 : }
# 392 : :
# 393 : 129154 : s << static_cast<uint8_t>(GetBIP155Network());
# 394 : 129154 : s << m_addr;
# 395 : 129154 : }
# 396 : :
# 397 : : /**
# 398 : : * Unserialize from a pre-ADDRv2/BIP155 format from an array.
# 399 : : */
# 400 : : void UnserializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE])
# 401 : 2957 : {
# 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 : 2957 : SetLegacyIPv6(arr);
# 405 : 2957 : }
# 406 : :
# 407 : : /**
# 408 : : * Unserialize from a pre-ADDRv2/BIP155 format from a stream.
# 409 : : */
# 410 : : template <typename Stream>
# 411 : : void UnserializeV1Stream(Stream& s)
# 412 : 2957 : {
# 413 : 2957 : uint8_t serialized[V1_SERIALIZATION_SIZE];
# 414 : :
# 415 : 2957 : s >> serialized;
# 416 : :
# 417 : 2957 : UnserializeV1Array(serialized);
# 418 : 2957 : }
# 419 : :
# 420 : : /**
# 421 : : * Unserialize from a ADDRv2 / BIP155 format.
# 422 : : */
# 423 : : template <typename Stream>
# 424 : : void UnserializeV2Stream(Stream& s)
# 425 : 2669 : {
# 426 : 2669 : uint8_t bip155_net;
# 427 : 2669 : s >> bip155_net;
# 428 : :
# 429 : 2669 : size_t address_size;
# 430 : 2669 : s >> COMPACTSIZE(address_size);
# 431 : :
# 432 [ + + ][ + + ]: 2669 : if (address_size > MAX_ADDRV2_SIZE) {
# [ - + ][ # # ]
# [ - + ]
# 433 : 5 : throw std::ios_base::failure(strprintf(
# 434 : 5 : "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
# 435 : 5 : }
# 436 : :
# 437 : 2664 : m_scope_id = 0;
# 438 : :
# 439 [ + + ][ + - ]: 2664 : if (SetNetFromBIP155Network(bip155_net, address_size)) {
# [ # # ][ + + ]
# [ + - ]
# 440 : 2647 : m_addr.resize(address_size);
# 441 : 2647 : s >> MakeSpan(m_addr);
# 442 : :
# 443 [ + + ][ + - ]: 2647 : if (m_net != NET_IPV6) {
# [ # # ][ + - ]
# [ + + ]
# 444 : 2601 : return;
# 445 : 2601 : }
# 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 [ + + ][ # # ]: 46 : if (HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
# [ # # ][ # # ]
# [ - + ]
# 453 : 2 : m_net = NET_INTERNAL;
# 454 : 2 : memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
# 455 : 2 : ADDR_INTERNAL_SIZE);
# 456 : 2 : m_addr.resize(ADDR_INTERNAL_SIZE);
# 457 : 2 : return;
# 458 : 2 : }
# 459 : :
# 460 [ + - ][ # # ]: 44 : if (!HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
# [ # # ][ # # ]
# [ + + ]
# 461 [ # # ][ # # ]: 44 : !HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
# [ + - ][ + + ]
# [ # # ]
# 462 : 38 : return;
# 463 : 38 : }
# 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 : 17 : } else {
# 468 : : // If we receive an unknown BIP155 network id (from the future?) then
# 469 : : // ignore the address - unserialize as !IsValid().
# 470 : 17 : s.ignore(address_size);
# 471 : 17 : }
# 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 : 2664 : m_net = NET_IPV6;
# 476 : 23 : m_addr.assign(ADDR_IPV6_SIZE, 0x0);
# 477 : 23 : }
# 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 : : bool SanityCheck() const;
# 491 : :
# 492 : : public:
# 493 : : /**
# 494 : : * Construct an invalid subnet (empty, `Match()` always returns false).
# 495 : : */
# 496 : : CSubNet();
# 497 : :
# 498 : : /**
# 499 : : * Construct from a given network start and number of bits (CIDR mask).
# 500 : : * @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is
# 501 : : * created.
# 502 : : * @param[in] mask CIDR mask, must be in [0, 32] for IPv4 addresses and in [0, 128] for
# 503 : : * IPv6 addresses. Otherwise an invalid subnet is created.
# 504 : : */
# 505 : : CSubNet(const CNetAddr& addr, uint8_t mask);
# 506 : :
# 507 : : /**
# 508 : : * Construct from a given network start and mask.
# 509 : : * @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is
# 510 : : * created.
# 511 : : * @param[in] mask Network mask, must be of the same type as `addr` and not contain 0-bits
# 512 : : * followed by 1-bits. Otherwise an invalid subnet is created.
# 513 : : */
# 514 : : CSubNet(const CNetAddr& addr, const CNetAddr& mask);
# 515 : :
# 516 : : /**
# 517 : : * Construct a single-host subnet.
# 518 : : * @param[in] addr The sole address to be contained in the subnet, can also be non-IPv[46].
# 519 : : */
# 520 : : explicit CSubNet(const CNetAddr& addr);
# 521 : :
# 522 : : bool Match(const CNetAddr &addr) const;
# 523 : :
# 524 : : std::string ToString() const;
# 525 : : bool IsValid() const;
# 526 : :
# 527 : : friend bool operator==(const CSubNet& a, const CSubNet& b);
# 528 : 2 : friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
# 529 : : friend bool operator<(const CSubNet& a, const CSubNet& b);
# 530 : :
# 531 : : SERIALIZE_METHODS(CSubNet, obj)
# 532 : 67 : {
# 533 : 67 : READWRITE(obj.network);
# 534 [ + + ][ + + ]: 67 : if (obj.network.IsIPv4()) {
# [ + + ]
# 535 : : // Before commit 102867c587f5f7954232fb8ed8e85cda78bb4d32, CSubNet used the last 4 bytes of netmask
# 536 : : // to store the relevant bytes for an IPv4 mask. For compatibility reasons, keep doing so in
# 537 : : // serialized form.
# 538 : 48 : unsigned char dummy[12] = {0};
# 539 : 48 : READWRITE(dummy);
# 540 : 48 : READWRITE(MakeSpan(obj.netmask).first(4));
# 541 : 48 : } else {
# 542 : 19 : READWRITE(obj.netmask);
# 543 : 19 : }
# 544 : 67 : READWRITE(obj.valid);
# 545 : : // Mark invalid if the result doesn't pass sanity checking.
# 546 : 67 : SER_READ(obj, if (obj.valid) obj.valid = obj.SanityCheck());
# 547 : 67 : }
# 548 : : };
# 549 : :
# 550 : : /** A combination of a network address (CNetAddr) and a (TCP) port */
# 551 : : class CService : public CNetAddr
# 552 : : {
# 553 : : protected:
# 554 : : uint16_t port; // host order
# 555 : :
# 556 : : public:
# 557 : : CService();
# 558 : : CService(const CNetAddr& ip, uint16_t port);
# 559 : : CService(const struct in_addr& ipv4Addr, uint16_t port);
# 560 : : explicit CService(const struct sockaddr_in& addr);
# 561 : : uint16_t GetPort() const;
# 562 : : bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
# 563 : : bool SetSockAddr(const struct sockaddr* paddr);
# 564 : : friend bool operator==(const CService& a, const CService& b);
# 565 : 1066 : friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
# 566 : : friend bool operator<(const CService& a, const CService& b);
# 567 : : std::vector<unsigned char> GetKey() const;
# 568 : : std::string ToString() const;
# 569 : : std::string ToStringPort() const;
# 570 : : std::string ToStringIPPort() const;
# 571 : :
# 572 : : CService(const struct in6_addr& ipv6Addr, uint16_t port);
# 573 : : explicit CService(const struct sockaddr_in6& addr);
# 574 : :
# 575 : : SERIALIZE_METHODS(CService, obj)
# 576 : 77572 : {
# 577 : 77572 : READWRITEAS(CNetAddr, obj);
# 578 : 77572 : READWRITE(Using<BigEndianFormatter<2>>(obj.port));
# 579 : 77572 : }
# 580 : : };
# 581 : :
# 582 : : bool SanityCheckASMap(const std::vector<bool>& asmap);
# 583 : :
# 584 : : #endif // BITCOIN_NETADDRESS_H
|