LCOV - code coverage report
Current view: top level - src - netaddress.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 60 65 92.3 %
Date: 2020-07-30 17:06:35 Functions: 27 29 93.1 %
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                 :            : 
#      17                 :            : #include <cstdint>
#      18                 :            : #include <string>
#      19                 :            : #include <vector>
#      20                 :            : 
#      21                 :            : /**
#      22                 :            :  * A network type.
#      23                 :            :  * @note An address may belong to more than one network, for example `10.0.0.1`
#      24                 :            :  * belongs to both `NET_UNROUTABLE` and `NET_IPV4`.
#      25                 :            :  * Keep these sequential starting from 0 and `NET_MAX` as the last entry.
#      26                 :            :  * We have loops like `for (int i = 0; i < NET_MAX; i++)` that expect to iterate
#      27                 :            :  * over all enum values and also `GetExtNetwork()` "extends" this enum by
#      28                 :            :  * introducing standalone constants starting from `NET_MAX`.
#      29                 :            :  */
#      30                 :            : enum Network
#      31                 :            : {
#      32                 :            :     /// Addresses from these networks are not publicly routable on the global Internet.
#      33                 :            :     NET_UNROUTABLE = 0,
#      34                 :            : 
#      35                 :            :     /// IPv4
#      36                 :            :     NET_IPV4,
#      37                 :            : 
#      38                 :            :     /// IPv6
#      39                 :            :     NET_IPV6,
#      40                 :            : 
#      41                 :            :     /// TORv2
#      42                 :            :     NET_ONION,
#      43                 :            : 
#      44                 :            :     /// A set of dummy addresses that map a name to an IPv6 address. These
#      45                 :            :     /// addresses belong to RFC4193's fc00::/7 subnet (unique-local addresses).
#      46                 :            :     /// We use them to map a string or FQDN to an IPv6 address in CAddrMan to
#      47                 :            :     /// keep track of which DNS seeds were used.
#      48                 :            :     NET_INTERNAL,
#      49                 :            : 
#      50                 :            :     /// Dummy value to indicate the number of NET_* constants.
#      51                 :            :     NET_MAX,
#      52                 :            : };
#      53                 :            : 
#      54                 :            : /// If an IPv6 address begins with this, then we treat the rest of it as IPv4 address.
#      55                 :            : static constexpr uint8_t IPv4_IN_IPv6_PREFIX[12] = {
#      56                 :            :     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF
#      57                 :            : };
#      58                 :            : 
#      59                 :            : /// If an IPv6 address begins with this, then we treat the rest of it as TORv2 address.
#      60                 :            : static constexpr uint8_t TORv2_IN_IPv6_PREFIX[6] = {
#      61                 :            :     0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43
#      62                 :            : };
#      63                 :            : 
#      64                 :            : /// If an IPv6 address begins with this, then we treat the rest of it as an internal address.
#      65                 :            : static constexpr uint8_t INTERNAL_IN_IPv6_PREFIX[6] = {
#      66                 :            :     0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 // 0xFD + sha256("bitcoin")[0:5].
#      67                 :            : };
#      68                 :            : 
#      69                 :            : /// Size of IPv4 address (in bytes).
#      70                 :            : static constexpr size_t ADDR_IPv4_SIZE = 4;
#      71                 :            : 
#      72                 :            : /// Size of IPv6 address (in bytes).
#      73                 :            : static constexpr size_t ADDR_IPv6_SIZE = 16;
#      74                 :            : 
#      75                 :            : /// Size of TORv2 address (in bytes).
#      76                 :            : static constexpr size_t ADDR_TORv2_SIZE = 10;
#      77                 :            : 
#      78                 :            : /// Size of "internal" (NET_INTERNAL) address (in bytes).
#      79                 :            : static constexpr size_t ADDR_INTERNAL_SIZE = 10;
#      80                 :            : 
#      81                 :            : /**
#      82                 :            :  * Network address.
#      83                 :            :  */
#      84                 :            : class CNetAddr
#      85                 :            : {
#      86                 :            :     protected:
#      87                 :            :         /**
#      88                 :            :          * Raw representation of the network address.
#      89                 :            :          * In network byte order (big endian) for IPv4 and IPv6.
#      90                 :            :          */
#      91                 :            :         prevector<ADDR_IPv6_SIZE, uint8_t> m_addr{ADDR_IPv6_SIZE, 0x0};
#      92                 :            : 
#      93                 :            :         /**
#      94                 :            :          * Network to which this address belongs.
#      95                 :            :          */
#      96                 :            :         Network m_net{NET_IPV6};
#      97                 :            : 
#      98                 :            :         uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses
#      99                 :            : 
#     100                 :            :     public:
#     101                 :            :         CNetAddr();
#     102                 :            :         explicit CNetAddr(const struct in_addr& ipv4Addr);
#     103                 :            :         void SetIP(const CNetAddr& ip);
#     104                 :            : 
#     105                 :            :         /**
#     106                 :            :          * Set from a legacy IPv6 address.
#     107                 :            :          * Legacy IPv6 address may be a normal IPv6 address, or another address
#     108                 :            :          * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy
#     109                 :            :          * `addr` encoding.
#     110                 :            :          */
#     111                 :            :         void SetLegacyIPv6(const uint8_t (&ipv6)[ADDR_IPv6_SIZE]);
#     112                 :            : 
#     113                 :            :         bool SetInternal(const std::string& name);
#     114                 :            : 
#     115                 :            :         bool SetSpecial(const std::string &strName); // for Tor addresses
#     116                 :            :         bool IsBindAny() const; // INADDR_ANY equivalent
#     117                 :            :         bool IsIPv4() const;    // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
#     118                 :            :         bool IsIPv6() const;    // IPv6 address (not mapped IPv4, not Tor)
#     119                 :            :         bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
#     120                 :            :         bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
#     121                 :            :         bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
#     122                 :            :         bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
#     123                 :            :         bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
#     124                 :            :         bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
#     125                 :            :         bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
#     126                 :            :         bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
#     127                 :            :         bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
#     128                 :            :         bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
#     129                 :            :         bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
#     130                 :            :         bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
#     131                 :            :         bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
#     132                 :            :         bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
#     133                 :            :         bool IsHeNet() const;   // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
#     134                 :            :         bool IsTor() const;
#     135                 :            :         bool IsLocal() const;
#     136                 :            :         bool IsRoutable() const;
#     137                 :            :         bool IsInternal() const;
#     138                 :            :         bool IsValid() const;
#     139                 :            : 
#     140                 :            :         enum Network GetNetwork() const;
#     141                 :            :         std::string ToString() const;
#     142                 :            :         std::string ToStringIP() const;
#     143                 :            :         uint64_t GetHash() const;
#     144                 :            :         bool GetInAddr(struct in_addr* pipv4Addr) const;
#     145                 :            :         uint32_t GetNetClass() const;
#     146                 :            : 
#     147                 :            :         //! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
#     148                 :            :         uint32_t GetLinkedIPv4() const;
#     149                 :            :         //! Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
#     150                 :            :         bool HasLinkedIPv4() const;
#     151                 :            : 
#     152                 :            :         // The AS on the BGP path to the node we use to diversify
#     153                 :            :         // peers in AddrMan bucketing based on the AS infrastructure.
#     154                 :            :         // The ip->AS mapping depends on how asmap is constructed.
#     155                 :            :         uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
#     156                 :            : 
#     157                 :            :         std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const;
#     158                 :            :         std::vector<unsigned char> GetAddrBytes() const;
#     159                 :            :         int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
#     160                 :            : 
#     161                 :            :         explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
#     162                 :            :         bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
#     163                 :            : 
#     164                 :            :         friend bool operator==(const CNetAddr& a, const CNetAddr& b);
#     165                 :          0 :         friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
#     166                 :            :         friend bool operator<(const CNetAddr& a, const CNetAddr& b);
#     167                 :            : 
#     168                 :            :         /**
#     169                 :            :          * Serialize to a stream.
#     170                 :            :          */
#     171                 :            :         template <typename Stream>
#     172                 :            :         void Serialize(Stream& s) const
#     173                 :       1842 :         {
#     174                 :       1842 :             SerializeV1Stream(s);
#     175                 :       1842 :         }
#     176                 :            : 
#     177                 :            :         /**
#     178                 :            :          * Unserialize from a stream.
#     179                 :            :          */
#     180                 :            :         template <typename Stream>
#     181                 :            :         void Unserialize(Stream& s)
#     182                 :       2703 :         {
#     183                 :       2703 :             UnserializeV1Stream(s);
#     184                 :       2703 :         }
#     185                 :            : 
#     186                 :            :         friend class CSubNet;
#     187                 :            : 
#     188                 :            :     private:
#     189                 :            :         /**
#     190                 :            :          * Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
#     191                 :            :          */
#     192                 :            :         static constexpr size_t V1_SERIALIZATION_SIZE = 16;
#     193                 :            : 
#     194                 :            :         /**
#     195                 :            :          * Serialize in pre-ADDRv2/BIP155 format to an array.
#     196                 :            :          * Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format.
#     197                 :            :          */
#     198                 :            :         void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const
#     199                 :    1258290 :         {
#     200                 :    1258290 :             size_t prefix_size;
#     201                 :    1258290 : 
#     202                 :    1258290 :             switch (m_net) {
#     203                 :    1256857 :             case NET_IPV4:
#     204                 :    1256857 :                 prefix_size = sizeof(IPv4_IN_IPv6_PREFIX);
#     205                 :    1256857 :                 assert(prefix_size + m_addr.size() == sizeof(arr));
#     206                 :    1256857 :                 memcpy(arr, IPv4_IN_IPv6_PREFIX, prefix_size);
#     207                 :    1256857 :                 memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
#     208                 :    1256857 :                 break;
#     209                 :       1428 :             case NET_IPV6:
#     210                 :       1428 :                 assert(m_addr.size() == sizeof(arr));
#     211                 :       1428 :                 memcpy(arr, m_addr.data(), m_addr.size());
#     212                 :       1428 :                 break;
#     213                 :          3 :             case NET_ONION:
#     214                 :          3 :                 prefix_size = sizeof(TORv2_IN_IPv6_PREFIX);
#     215                 :          3 :                 assert(m_addr.size() + prefix_size == sizeof(arr));
#     216                 :          3 :                 memcpy(arr, TORv2_IN_IPv6_PREFIX, prefix_size);
#     217                 :          3 :                 memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
#     218                 :          3 :                 break;
#     219                 :          2 :             case NET_INTERNAL:
#     220                 :          2 :                 prefix_size = sizeof(INTERNAL_IN_IPv6_PREFIX);
#     221                 :          2 :                 assert(m_addr.size() + prefix_size == sizeof(arr));
#     222                 :          2 :                 memcpy(arr, INTERNAL_IN_IPv6_PREFIX, prefix_size);
#     223                 :          2 :                 memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
#     224                 :          2 :                 break;
#     225                 :          0 :             case NET_UNROUTABLE:
#     226                 :          0 :             case NET_MAX:
#     227                 :          0 :                 memset(arr, 0x0, sizeof(arr));
#     228                 :          0 :                 break;
#     229                 :    1258290 :             }
#     230                 :    1258290 :         }
#     231                 :            : 
#     232                 :            :         /**
#     233                 :            :          * Serialize in pre-ADDRv2/BIP155 format to a stream.
#     234                 :            :          * Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format.
#     235                 :            :          */
#     236                 :            :         template <typename Stream>
#     237                 :            :         void SerializeV1Stream(Stream& s) const
#     238                 :       1842 :         {
#     239                 :       1842 :             uint8_t serialized[V1_SERIALIZATION_SIZE];
#     240                 :       1842 : 
#     241                 :       1842 :             SerializeV1Array(serialized);
#     242                 :       1842 : 
#     243                 :       1842 :             s << serialized;
#     244                 :       1842 :         }
#     245                 :            : 
#     246                 :            :         /**
#     247                 :            :          * Unserialize from a pre-ADDRv2/BIP155 format from an array.
#     248                 :            :          */
#     249                 :            :         void UnserializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE])
#     250                 :       2703 :         {
#     251                 :       2703 :             // Use SetLegacyIPv6() so that m_net is set correctly. For example
#     252                 :       2703 :             // ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
#     253                 :       2703 :             SetLegacyIPv6(arr);
#     254                 :       2703 :         }
#     255                 :            : 
#     256                 :            :         /**
#     257                 :            :          * Unserialize from a pre-ADDRv2/BIP155 format from a stream.
#     258                 :            :          */
#     259                 :            :         template <typename Stream>
#     260                 :            :         void UnserializeV1Stream(Stream& s)
#     261                 :       2703 :         {
#     262                 :       2703 :             uint8_t serialized[V1_SERIALIZATION_SIZE];
#     263                 :       2703 : 
#     264                 :       2703 :             s >> serialized;
#     265                 :       2703 : 
#     266                 :       2703 :             UnserializeV1Array(serialized);
#     267                 :       2703 :         }
#     268                 :            : };
#     269                 :            : 
#     270                 :            : class CSubNet
#     271                 :            : {
#     272                 :            :     protected:
#     273                 :            :         /// Network (base) address
#     274                 :            :         CNetAddr network;
#     275                 :            :         /// Netmask, in network byte order
#     276                 :            :         uint8_t netmask[16];
#     277                 :            :         /// Is this value valid? (only used to signal parse errors)
#     278                 :            :         bool valid;
#     279                 :            : 
#     280                 :            :     public:
#     281                 :            :         CSubNet();
#     282                 :            :         CSubNet(const CNetAddr& addr, uint8_t mask);
#     283                 :            :         CSubNet(const CNetAddr& addr, const CNetAddr& mask);
#     284                 :            : 
#     285                 :            :         //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
#     286                 :            :         explicit CSubNet(const CNetAddr& addr);
#     287                 :            : 
#     288                 :            :         bool Match(const CNetAddr &addr) const;
#     289                 :            : 
#     290                 :            :         std::string ToString() const;
#     291                 :            :         bool IsValid() const;
#     292                 :            : 
#     293                 :            :         friend bool operator==(const CSubNet& a, const CSubNet& b);
#     294                 :          2 :         friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
#     295                 :            :         friend bool operator<(const CSubNet& a, const CSubNet& b);
#     296                 :            : 
#     297                 :         65 :         SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
#     298                 :            : };
#     299                 :            : 
#     300                 :            : /** A combination of a network address (CNetAddr) and a (TCP) port */
#     301                 :            : class CService : public CNetAddr
#     302                 :            : {
#     303                 :            :     protected:
#     304                 :            :         uint16_t port; // host order
#     305                 :            : 
#     306                 :            :     public:
#     307                 :            :         CService();
#     308                 :            :         CService(const CNetAddr& ip, uint16_t port);
#     309                 :            :         CService(const struct in_addr& ipv4Addr, uint16_t port);
#     310                 :            :         explicit CService(const struct sockaddr_in& addr);
#     311                 :            :         uint16_t GetPort() const;
#     312                 :            :         bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
#     313                 :            :         bool SetSockAddr(const struct sockaddr* paddr);
#     314                 :            :         friend bool operator==(const CService& a, const CService& b);
#     315                 :        876 :         friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
#     316                 :            :         friend bool operator<(const CService& a, const CService& b);
#     317                 :            :         std::vector<unsigned char> GetKey() const;
#     318                 :            :         std::string ToString() const;
#     319                 :            :         std::string ToStringPort() const;
#     320                 :            :         std::string ToStringIPPort() const;
#     321                 :            : 
#     322                 :            :         CService(const struct in6_addr& ipv6Addr, uint16_t port);
#     323                 :            :         explicit CService(const struct sockaddr_in6& addr);
#     324                 :            : 
#     325                 :            :         SERIALIZE_METHODS(CService, obj)
#     326                 :       4282 :         {
#     327                 :       4282 :             READWRITEAS(CNetAddr, obj);
#     328                 :       4282 :             READWRITE(Using<BigEndianFormatter<2>>(obj.port));
#     329                 :       4282 :         }
#     330                 :            : };
#     331                 :            : 
#     332                 :            : bool SanityCheckASMap(const std::vector<bool>& asmap);
#     333                 :            : 
#     334                 :            : #endif // BITCOIN_NETADDRESS_H

Generated by: LCOV version 1.14