LCOV - code coverage report
Current view: top level - src - netaddress.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 108 116 93.1 %
Date: 2020-10-06 11:46:21 Functions: 42 53 79.2 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 0 -

           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 const 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                 :            :         uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses
#     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                 :            :         bool SetSpecial(const std::string &strName); // for Tor addresses
#     151                 :            :         bool IsBindAny() const; // INADDR_ANY equivalent
#     152                 :            :         bool IsIPv4() const;    // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
#     153                 :            :         bool IsIPv6() const;    // IPv6 address (not mapped IPv4, not Tor)
#     154                 :            :         bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
#     155                 :            :         bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
#     156                 :            :         bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
#     157                 :            :         bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
#     158                 :            :         bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
#     159                 :            :         bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
#     160                 :            :         bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
#     161                 :            :         bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
#     162                 :            :         bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
#     163                 :            :         bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
#     164                 :            :         bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
#     165                 :            :         bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
#     166                 :            :         bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
#     167                 :            :         bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
#     168                 :            :         bool IsHeNet() const;   // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
#     169                 :            :         bool IsTor() const;
#     170                 :            :         bool IsI2P() const;
#     171                 :            :         bool IsCJDNS() const;
#     172                 :            :         bool IsLocal() const;
#     173                 :            :         bool IsRoutable() const;
#     174                 :            :         bool IsInternal() const;
#     175                 :            :         bool IsValid() const;
#     176                 :            : 
#     177                 :            :         /**
#     178                 :            :          * Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
#     179                 :            :          */
#     180                 :            :         bool IsAddrV1Compatible() const;
#     181                 :            : 
#     182                 :            :         enum Network GetNetwork() const;
#     183                 :            :         std::string ToString() const;
#     184                 :            :         std::string ToStringIP() const;
#     185                 :            :         uint64_t GetHash() const;
#     186                 :            :         bool GetInAddr(struct in_addr* pipv4Addr) const;
#     187                 :            :         uint32_t GetNetClass() const;
#     188                 :            : 
#     189                 :            :         //! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
#     190                 :            :         uint32_t GetLinkedIPv4() const;
#     191                 :            :         //! Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
#     192                 :            :         bool HasLinkedIPv4() const;
#     193                 :            : 
#     194                 :            :         // The AS on the BGP path to the node we use to diversify
#     195                 :            :         // peers in AddrMan bucketing based on the AS infrastructure.
#     196                 :            :         // The ip->AS mapping depends on how asmap is constructed.
#     197                 :            :         uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
#     198                 :            : 
#     199                 :            :         std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const;
#     200                 :            :         std::vector<unsigned char> GetAddrBytes() const;
#     201                 :            :         int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
#     202                 :            : 
#     203                 :            :         explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
#     204                 :            :         bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
#     205                 :            : 
#     206                 :            :         friend bool operator==(const CNetAddr& a, const CNetAddr& b);
#     207                 :          0 :         friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
#     208                 :            :         friend bool operator<(const CNetAddr& a, const CNetAddr& b);
#     209                 :            : 
#     210                 :            :         /**
#     211                 :            :          * Serialize to a stream.
#     212                 :            :          */
#     213                 :            :         template <typename Stream>
#     214                 :            :         void Serialize(Stream& s) const
#     215                 :      78423 :         {
#     216                 :      78423 :             if (s.GetVersion() & ADDRV2_FORMAT) {
#     217                 :      70836 :                 SerializeV2Stream(s);
#     218                 :       7587 :             } else {
#     219                 :       7587 :                 SerializeV1Stream(s);
#     220                 :       7587 :             }
#     221                 :      78423 :         }
#     222                 :            : 
#     223                 :            :         /**
#     224                 :            :          * Unserialize from a stream.
#     225                 :            :          */
#     226                 :            :         template <typename Stream>
#     227                 :            :         void Unserialize(Stream& s)
#     228                 :       3579 :         {
#     229                 :       3579 :             if (s.GetVersion() & ADDRV2_FORMAT) {
#     230                 :       1113 :                 UnserializeV2Stream(s);
#     231                 :       2466 :             } else {
#     232                 :       2466 :                 UnserializeV1Stream(s);
#     233                 :       2466 :             }
#     234                 :       3579 :         }
#     235                 :            : 
#     236                 :            :         friend class CSubNet;
#     237                 :            : 
#     238                 :            :     private:
#     239                 :            :         /**
#     240                 :            :          * BIP155 network ids recognized by this software.
#     241                 :            :          */
#     242                 :            :         enum BIP155Network : uint8_t {
#     243                 :            :             IPV4 = 1,
#     244                 :            :             IPV6 = 2,
#     245                 :            :             TORV2 = 3,
#     246                 :            :             TORV3 = 4,
#     247                 :            :             I2P = 5,
#     248                 :            :             CJDNS = 6,
#     249                 :            :         };
#     250                 :            : 
#     251                 :            :         /**
#     252                 :            :          * Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
#     253                 :            :          */
#     254                 :            :         static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE;
#     255                 :            : 
#     256                 :            :         /**
#     257                 :            :          * Maximum size of an address as defined in BIP155 (in bytes).
#     258                 :            :          * This is only the size of the address, not the entire CNetAddr object
#     259                 :            :          * when serialized.
#     260                 :            :          */
#     261                 :            :         static constexpr size_t MAX_ADDRV2_SIZE = 512;
#     262                 :            : 
#     263                 :            :         /**
#     264                 :            :          * Get the BIP155 network id of this address.
#     265                 :            :          * Must not be called for IsInternal() objects.
#     266                 :            :          * @returns BIP155 network id
#     267                 :            :          */
#     268                 :            :         BIP155Network GetBIP155Network() const;
#     269                 :            : 
#     270                 :            :         /**
#     271                 :            :          * Set `m_net` from the provided BIP155 network id and size after validation.
#     272                 :            :          * @retval true the network was recognized, is valid and `m_net` was set
#     273                 :            :          * @retval false not recognised (from future?) and should be silently ignored
#     274                 :            :          * @throws std::ios_base::failure if the network is one of the BIP155 founding
#     275                 :            :          * networks (id 1..6) with wrong address size.
#     276                 :            :          */
#     277                 :            :         bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size);
#     278                 :            : 
#     279                 :            :         /**
#     280                 :            :          * Serialize in pre-ADDRv2/BIP155 format to an array.
#     281                 :            :          */
#     282                 :            :         void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const
#     283                 :    1322921 :         {
#     284                 :    1322921 :             size_t prefix_size;
#     285                 :            : 
#     286                 :    1322921 :             switch (m_net) {
#     287                 :       1492 :             case NET_IPV6:
#     288                 :       1492 :                 assert(m_addr.size() == sizeof(arr));
#     289                 :       1492 :                 memcpy(arr, m_addr.data(), m_addr.size());
#     290                 :       1492 :                 return;
#     291                 :    1321418 :             case NET_IPV4:
#     292                 :    1321418 :                 prefix_size = sizeof(IPV4_IN_IPV6_PREFIX);
#     293                 :    1321418 :                 assert(prefix_size + m_addr.size() == sizeof(arr));
#     294                 :    1321418 :                 memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
#     295                 :    1321418 :                 memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
#     296                 :    1321418 :                 return;
#     297                 :          7 :             case NET_ONION:
#     298                 :          7 :                 if (m_addr.size() == ADDR_TORV3_SIZE) {
#     299                 :          2 :                     break;
#     300                 :          2 :                 }
#     301                 :          5 :                 prefix_size = sizeof(TORV2_IN_IPV6_PREFIX);
#     302                 :          5 :                 assert(prefix_size + m_addr.size() == sizeof(arr));
#     303                 :          5 :                 memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size);
#     304                 :          5 :                 memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
#     305                 :          5 :                 return;
#     306                 :          4 :             case NET_INTERNAL:
#     307                 :          4 :                 prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX);
#     308                 :          4 :                 assert(prefix_size + m_addr.size() == sizeof(arr));
#     309                 :          4 :                 memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
#     310                 :          4 :                 memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
#     311                 :          4 :                 return;
#     312                 :          0 :             case NET_I2P:
#     313                 :          0 :                 break;
#     314                 :          0 :             case NET_CJDNS:
#     315                 :          0 :                 break;
#     316                 :          0 :             case NET_UNROUTABLE:
#     317                 :          0 :             case NET_MAX:
#     318                 :          0 :                 assert(false);
#     319                 :    1322921 :             } // no default case, so the compiler can warn about missing cases
#     320                 :            : 
#     321                 :            :             // Serialize TORv3, I2P and CJDNS as all-zeros.
#     322                 :          2 :             memset(arr, 0x0, V1_SERIALIZATION_SIZE);
#     323                 :          2 :         }
#     324                 :            : 
#     325                 :            :         /**
#     326                 :            :          * Serialize in pre-ADDRv2/BIP155 format to a stream.
#     327                 :            :          */
#     328                 :            :         template <typename Stream>
#     329                 :            :         void SerializeV1Stream(Stream& s) const
#     330                 :       7589 :         {
#     331                 :       7589 :             uint8_t serialized[V1_SERIALIZATION_SIZE];
#     332                 :            : 
#     333                 :       7589 :             SerializeV1Array(serialized);
#     334                 :            : 
#     335                 :       7589 :             s << serialized;
#     336                 :       7589 :         }
#     337                 :            : 
#     338                 :            :         /**
#     339                 :            :          * Serialize as ADDRv2 / BIP155.
#     340                 :            :          */
#     341                 :            :         template <typename Stream>
#     342                 :            :         void SerializeV2Stream(Stream& s) const
#     343                 :      70836 :         {
#     344                 :      70836 :             if (IsInternal()) {
#     345                 :            :                 // Serialize NET_INTERNAL as embedded in IPv6. We need to
#     346                 :            :                 // serialize such addresses from addrman.
#     347                 :          2 :                 s << static_cast<uint8_t>(BIP155Network::IPV6);
#     348                 :          2 :                 s << COMPACTSIZE(ADDR_IPV6_SIZE);
#     349                 :          2 :                 SerializeV1Stream(s);
#     350                 :          2 :                 return;
#     351                 :          2 :             }
#     352                 :            : 
#     353                 :      70834 :             s << static_cast<uint8_t>(GetBIP155Network());
#     354                 :      70834 :             s << m_addr;
#     355                 :      70834 :         }
#     356                 :            : 
#     357                 :            :         /**
#     358                 :            :          * Unserialize from a pre-ADDRv2/BIP155 format from an array.
#     359                 :            :          */
#     360                 :            :         void UnserializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE])
#     361                 :       2466 :         {
#     362                 :            :             // Use SetLegacyIPv6() so that m_net is set correctly. For example
#     363                 :            :             // ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
#     364                 :       2466 :             SetLegacyIPv6(arr);
#     365                 :       2466 :         }
#     366                 :            : 
#     367                 :            :         /**
#     368                 :            :          * Unserialize from a pre-ADDRv2/BIP155 format from a stream.
#     369                 :            :          */
#     370                 :            :         template <typename Stream>
#     371                 :            :         void UnserializeV1Stream(Stream& s)
#     372                 :       2466 :         {
#     373                 :       2466 :             uint8_t serialized[V1_SERIALIZATION_SIZE];
#     374                 :            : 
#     375                 :       2466 :             s >> serialized;
#     376                 :            : 
#     377                 :       2466 :             UnserializeV1Array(serialized);
#     378                 :       2466 :         }
#     379                 :            : 
#     380                 :            :         /**
#     381                 :            :          * Unserialize from a ADDRv2 / BIP155 format.
#     382                 :            :          */
#     383                 :            :         template <typename Stream>
#     384                 :            :         void UnserializeV2Stream(Stream& s)
#     385                 :       1113 :         {
#     386                 :       1113 :             uint8_t bip155_net;
#     387                 :       1113 :             s >> bip155_net;
#     388                 :            : 
#     389                 :       1113 :             size_t address_size;
#     390                 :       1113 :             s >> COMPACTSIZE(address_size);
#     391                 :            : 
#     392                 :       1113 :             if (address_size > MAX_ADDRV2_SIZE) {
#     393                 :          5 :                 throw std::ios_base::failure(strprintf(
#     394                 :          5 :                     "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
#     395                 :          5 :             }
#     396                 :            : 
#     397                 :       1108 :             scopeId = 0;
#     398                 :            : 
#     399                 :       1108 :             if (SetNetFromBIP155Network(bip155_net, address_size)) {
#     400                 :       1091 :                 m_addr.resize(address_size);
#     401                 :       1091 :                 s >> MakeSpan(m_addr);
#     402                 :            : 
#     403                 :       1091 :                 if (m_net != NET_IPV6) {
#     404                 :       1065 :                     return;
#     405                 :       1065 :                 }
#     406                 :            : 
#     407                 :            :                 // Do some special checks on IPv6 addresses.
#     408                 :            : 
#     409                 :            :                 // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
#     410                 :            :                 // gossiped but could be coming from addrman, when unserializing from
#     411                 :            :                 // disk.
#     412                 :         26 :                 if (HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
#     413                 :          2 :                     m_net = NET_INTERNAL;
#     414                 :          2 :                     memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
#     415                 :          2 :                             ADDR_INTERNAL_SIZE);
#     416                 :          2 :                     m_addr.resize(ADDR_INTERNAL_SIZE);
#     417                 :          2 :                     return;
#     418                 :          2 :                 }
#     419                 :            : 
#     420                 :         24 :                 if (!HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
#     421                 :         20 :                     !HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
#     422                 :         18 :                     return;
#     423                 :         18 :                 }
#     424                 :            : 
#     425                 :            :                 // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
#     426                 :            :                 // encoding). Unserialize as !IsValid(), thus ignoring them.
#     427                 :         17 :             } else {
#     428                 :            :                 // If we receive an unknown BIP155 network id (from the future?) then
#     429                 :            :                 // ignore the address - unserialize as !IsValid().
#     430                 :         17 :                 s.ignore(address_size);
#     431                 :         17 :             }
#     432                 :            : 
#     433                 :            :             // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
#     434                 :            :             // will not be gossiped, but continue reading next addresses from the stream.
#     435                 :         23 :             m_net = NET_IPV6;
#     436                 :         23 :             m_addr.assign(ADDR_IPV6_SIZE, 0x0);
#     437                 :         23 :         }
#     438                 :            : };
#     439                 :            : 
#     440                 :            : class CSubNet
#     441                 :            : {
#     442                 :            :     protected:
#     443                 :            :         /// Network (base) address
#     444                 :            :         CNetAddr network;
#     445                 :            :         /// Netmask, in network byte order
#     446                 :            :         uint8_t netmask[16];
#     447                 :            :         /// Is this value valid? (only used to signal parse errors)
#     448                 :            :         bool valid;
#     449                 :            : 
#     450                 :            :     public:
#     451                 :            :         CSubNet();
#     452                 :            :         CSubNet(const CNetAddr& addr, uint8_t mask);
#     453                 :            :         CSubNet(const CNetAddr& addr, const CNetAddr& mask);
#     454                 :            : 
#     455                 :            :         //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
#     456                 :            :         explicit CSubNet(const CNetAddr& addr);
#     457                 :            : 
#     458                 :            :         bool Match(const CNetAddr &addr) const;
#     459                 :            : 
#     460                 :            :         std::string ToString() const;
#     461                 :            :         bool IsValid() const;
#     462                 :            : 
#     463                 :            :         friend bool operator==(const CSubNet& a, const CSubNet& b);
#     464                 :          2 :         friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
#     465                 :            :         friend bool operator<(const CSubNet& a, const CSubNet& b);
#     466                 :            : 
#     467                 :         65 :         SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
#     468                 :            : };
#     469                 :            : 
#     470                 :            : /** A combination of a network address (CNetAddr) and a (TCP) port */
#     471                 :            : class CService : public CNetAddr
#     472                 :            : {
#     473                 :            :     protected:
#     474                 :            :         uint16_t port; // host order
#     475                 :            : 
#     476                 :            :     public:
#     477                 :            :         CService();
#     478                 :            :         CService(const CNetAddr& ip, uint16_t port);
#     479                 :            :         CService(const struct in_addr& ipv4Addr, uint16_t port);
#     480                 :            :         explicit CService(const struct sockaddr_in& addr);
#     481                 :            :         uint16_t GetPort() const;
#     482                 :            :         bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
#     483                 :            :         bool SetSockAddr(const struct sockaddr* paddr);
#     484                 :            :         friend bool operator==(const CService& a, const CService& b);
#     485                 :        879 :         friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
#     486                 :            :         friend bool operator<(const CService& a, const CService& b);
#     487                 :            :         std::vector<unsigned char> GetKey() const;
#     488                 :            :         std::string ToString() const;
#     489                 :            :         std::string ToStringPort() const;
#     490                 :            :         std::string ToStringIPPort() const;
#     491                 :            : 
#     492                 :            :         CService(const struct in6_addr& ipv6Addr, uint16_t port);
#     493                 :            :         explicit CService(const struct sockaddr_in6& addr);
#     494                 :            : 
#     495                 :            :         SERIALIZE_METHODS(CService, obj)
#     496                 :      46439 :         {
#     497                 :      46439 :             READWRITEAS(CNetAddr, obj);
#     498                 :      46439 :             READWRITE(Using<BigEndianFormatter<2>>(obj.port));
#     499                 :      46439 :         }
#     500                 :            : };
#     501                 :            : 
#     502                 :            : bool SanityCheckASMap(const std::vector<bool>& asmap);
#     503                 :            : 
#     504                 :            : #endif // BITCOIN_NETADDRESS_H

Generated by: LCOV version 1.14