LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 649 774 83.9 %
Date: 2020-09-17 15:21:04 Functions: 77 79 97.5 %
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-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2020 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #include <netaddress.h>
#       7                 :            : 
#       8                 :            : #include <crypto/common.h>
#       9                 :            : #include <crypto/sha3.h>
#      10                 :            : #include <hash.h>
#      11                 :            : #include <prevector.h>
#      12                 :            : #include <tinyformat.h>
#      13                 :            : #include <util/asmap.h>
#      14                 :            : #include <util/strencodings.h>
#      15                 :            : #include <util/string.h>
#      16                 :            : 
#      17                 :            : #include <algorithm>
#      18                 :            : #include <array>
#      19                 :            : #include <cstdint>
#      20                 :            : #include <ios>
#      21                 :            : #include <iterator>
#      22                 :            : #include <tuple>
#      23                 :            : 
#      24                 :            : constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE;
#      25                 :            : constexpr size_t CNetAddr::MAX_ADDRV2_SIZE;
#      26                 :            : 
#      27                 :            : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
#      28                 :         10 : {
#      29                 :         10 :     switch (m_net) {
#      30                 :          2 :     case NET_IPV4:
#      31                 :          2 :         return BIP155Network::IPV4;
#      32                 :          4 :     case NET_IPV6:
#      33                 :          4 :         return BIP155Network::IPV6;
#      34                 :          4 :     case NET_ONION:
#      35                 :          4 :         switch (m_addr.size()) {
#      36                 :          2 :         case ADDR_TORV2_SIZE:
#      37                 :          2 :             return BIP155Network::TORV2;
#      38                 :          2 :         case ADDR_TORV3_SIZE:
#      39                 :          2 :             return BIP155Network::TORV3;
#      40                 :          0 :         default:
#      41                 :          0 :             assert(false);
#      42                 :          4 :         }
#      43                 :          0 :     case NET_I2P:
#      44                 :          0 :         return BIP155Network::I2P;
#      45                 :          0 :     case NET_CJDNS:
#      46                 :          0 :         return BIP155Network::CJDNS;
#      47                 :          0 :     case NET_INTERNAL:   // should have been handled before calling this function
#      48                 :          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#      49                 :          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#      50                 :          0 :         assert(false);
#      51                 :         10 :     } // no default case, so the compiler can warn about missing cases
#      52                 :         10 : 
#      53                 :          0 :     assert(false);
#      54                 :          0 : }
#      55                 :            : 
#      56                 :            : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
#      57                 :         36 : {
#      58                 :         36 :     switch (possible_bip155_net) {
#      59                 :          6 :     case BIP155Network::IPV4:
#      60                 :          6 :         if (address_size == ADDR_IPV4_SIZE) {
#      61                 :          4 :             m_net = NET_IPV4;
#      62                 :          4 :             return true;
#      63                 :          4 :         }
#      64                 :          2 :         throw std::ios_base::failure(
#      65                 :          2 :             strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
#      66                 :          2 :                       ADDR_IPV4_SIZE));
#      67                 :         10 :     case BIP155Network::IPV6:
#      68                 :         10 :         if (address_size == ADDR_IPV6_SIZE) {
#      69                 :          8 :             m_net = NET_IPV6;
#      70                 :          8 :             return true;
#      71                 :          8 :         }
#      72                 :          2 :         throw std::ios_base::failure(
#      73                 :          2 :             strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
#      74                 :          2 :                       ADDR_IPV6_SIZE));
#      75                 :          4 :     case BIP155Network::TORV2:
#      76                 :          4 :         if (address_size == ADDR_TORV2_SIZE) {
#      77                 :          2 :             m_net = NET_ONION;
#      78                 :          2 :             return true;
#      79                 :          2 :         }
#      80                 :          2 :         throw std::ios_base::failure(
#      81                 :          2 :             strprintf("BIP155 TORv2 address with length %u (should be %u)", address_size,
#      82                 :          2 :                       ADDR_TORV2_SIZE));
#      83                 :          4 :     case BIP155Network::TORV3:
#      84                 :          4 :         if (address_size == ADDR_TORV3_SIZE) {
#      85                 :          2 :             m_net = NET_ONION;
#      86                 :          2 :             return true;
#      87                 :          2 :         }
#      88                 :          2 :         throw std::ios_base::failure(
#      89                 :          2 :             strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
#      90                 :          2 :                       ADDR_TORV3_SIZE));
#      91                 :          4 :     case BIP155Network::I2P:
#      92                 :          4 :         if (address_size == ADDR_I2P_SIZE) {
#      93                 :          2 :             m_net = NET_I2P;
#      94                 :          2 :             return true;
#      95                 :          2 :         }
#      96                 :          2 :         throw std::ios_base::failure(
#      97                 :          2 :             strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
#      98                 :          2 :                       ADDR_I2P_SIZE));
#      99                 :          4 :     case BIP155Network::CJDNS:
#     100                 :          4 :         if (address_size == ADDR_CJDNS_SIZE) {
#     101                 :          2 :             m_net = NET_CJDNS;
#     102                 :          2 :             return true;
#     103                 :          2 :         }
#     104                 :          2 :         throw std::ios_base::failure(
#     105                 :          2 :             strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
#     106                 :          2 :                       ADDR_CJDNS_SIZE));
#     107                 :          4 :     }
#     108                 :          4 : 
#     109                 :            :     // Don't throw on addresses with unknown network ids (maybe from the future).
#     110                 :            :     // Instead silently drop them and have the unserialization code consume
#     111                 :            :     // subsequent ones which may be known to us.
#     112                 :          4 :     return false;
#     113                 :          4 : }
#     114                 :            : 
#     115                 :            : /**
#     116                 :            :  * Construct an unspecified IPv6 network address (::/128).
#     117                 :            :  *
#     118                 :            :  * @note This address is considered invalid by CNetAddr::IsValid()
#     119                 :            :  */
#     120                 :    1450388 : CNetAddr::CNetAddr() {}
#     121                 :            : 
#     122                 :            : void CNetAddr::SetIP(const CNetAddr& ipIn)
#     123                 :          2 : {
#     124                 :            :     // Size check.
#     125                 :          2 :     switch (ipIn.m_net) {
#     126                 :          0 :     case NET_IPV4:
#     127                 :          0 :         assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
#     128                 :          0 :         break;
#     129                 :          2 :     case NET_IPV6:
#     130                 :          2 :         assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
#     131                 :          2 :         break;
#     132                 :          0 :     case NET_ONION:
#     133                 :          0 :         assert(ipIn.m_addr.size() == ADDR_TORV2_SIZE || ipIn.m_addr.size() == ADDR_TORV3_SIZE);
#     134                 :          0 :         break;
#     135                 :          0 :     case NET_I2P:
#     136                 :          0 :         assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
#     137                 :          0 :         break;
#     138                 :          0 :     case NET_CJDNS:
#     139                 :          0 :         assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
#     140                 :          0 :         break;
#     141                 :          0 :     case NET_INTERNAL:
#     142                 :          0 :         assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
#     143                 :          0 :         break;
#     144                 :          0 :     case NET_UNROUTABLE:
#     145                 :          0 :     case NET_MAX:
#     146                 :          0 :         assert(false);
#     147                 :          2 :     } // no default case, so the compiler can warn about missing cases
#     148                 :          2 : 
#     149                 :          2 :     m_net = ipIn.m_net;
#     150                 :          2 :     m_addr = ipIn.m_addr;
#     151                 :          2 : }
#     152                 :            : 
#     153                 :            : void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
#     154                 :      13662 : {
#     155                 :      13662 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
#     156                 :      13662 : 
#     157                 :      13662 :     size_t skip{0};
#     158                 :      13662 : 
#     159                 :      13662 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
#     160                 :            :         // IPv4-in-IPv6
#     161                 :      11519 :         m_net = NET_IPV4;
#     162                 :      11519 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
#     163                 :       2143 :     } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
#     164                 :            :         // TORv2-in-IPv6
#     165                 :          8 :         m_net = NET_ONION;
#     166                 :          8 :         skip = sizeof(TORV2_IN_IPV6_PREFIX);
#     167                 :       2135 :     } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
#     168                 :            :         // Internal-in-IPv6
#     169                 :          2 :         m_net = NET_INTERNAL;
#     170                 :          2 :         skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
#     171                 :       2133 :     } else {
#     172                 :            :         // IPv6
#     173                 :       2133 :         m_net = NET_IPV6;
#     174                 :       2133 :     }
#     175                 :      13662 : 
#     176                 :      13662 :     m_addr.assign(ipv6.begin() + skip, ipv6.end());
#     177                 :      13662 : }
#     178                 :            : 
#     179                 :            : /**
#     180                 :            :  * Create an "internal" address that represents a name or FQDN. CAddrMan uses
#     181                 :            :  * these fake addresses to keep track of which DNS seeds were used.
#     182                 :            :  * @returns Whether or not the operation was successful.
#     183                 :            :  * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
#     184                 :            :  */
#     185                 :            : bool CNetAddr::SetInternal(const std::string &name)
#     186                 :        438 : {
#     187                 :        438 :     if (name.empty()) {
#     188                 :          0 :         return false;
#     189                 :          0 :     }
#     190                 :        438 :     m_net = NET_INTERNAL;
#     191                 :        438 :     unsigned char hash[32] = {};
#     192                 :        438 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
#     193                 :        438 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
#     194                 :        438 :     return true;
#     195                 :        438 : }
#     196                 :            : 
#     197                 :            : namespace torv3 {
#     198                 :            : // https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt#n2135
#     199                 :            : static constexpr size_t CHECKSUM_LEN = 2;
#     200                 :            : static constexpr size_t VERSION_LEN = 1;
#     201                 :            : static const unsigned char VERSION[] = "\x03";
#     202                 :            : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + VERSION_LEN;
#     203                 :            : 
#     204                 :            : static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
#     205                 :         14 : {
#     206                 :            :     // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
#     207                 :         14 :     static const unsigned char prefix[] = ".onion checksum";
#     208                 :         14 :     static constexpr size_t prefix_len = 15;
#     209                 :         14 : 
#     210                 :         14 :     SHA3_256 hasher;
#     211                 :         14 : 
#     212                 :         14 :     hasher.Write(MakeSpan(prefix).first(prefix_len));
#     213                 :         14 :     hasher.Write(addr_pubkey);
#     214                 :         14 :     hasher.Write(MakeSpan(torv3::VERSION).first(torv3::VERSION_LEN));
#     215                 :         14 : 
#     216                 :         14 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
#     217                 :         14 : 
#     218                 :         14 :     hasher.Finalize(checksum_full);
#     219                 :         14 : 
#     220                 :         14 :     memcpy(checksum, checksum_full, sizeof(checksum));
#     221                 :         14 : }
#     222                 :            : 
#     223                 :            : }; // namespace torv3
#     224                 :            : 
#     225                 :            : /**
#     226                 :            :  * Parse a TOR address and set this object to it.
#     227                 :            :  *
#     228                 :            :  * @returns Whether or not the operation was successful.
#     229                 :            :  *
#     230                 :            :  * @see CNetAddr::IsTor()
#     231                 :            :  */
#     232                 :            : bool CNetAddr::SetSpecial(const std::string& str)
#     233                 :     263551 : {
#     234                 :     263551 :     static const char* suffix{".onion"};
#     235                 :     263551 :     static constexpr size_t suffix_len{6};
#     236                 :     263551 : 
#     237                 :     263551 :     if (!ValidAsCString(str) || str.size() <= suffix_len ||
#     238                 :     263522 :         str.substr(str.size() - suffix_len) != suffix) {
#     239                 :     263522 :         return false;
#     240                 :     263522 :     }
#     241                 :         29 : 
#     242                 :         29 :     bool invalid;
#     243                 :         29 :     const auto& input = DecodeBase32(str.substr(0, str.size() - suffix_len).c_str(), &invalid);
#     244                 :         29 : 
#     245                 :         29 :     if (invalid) {
#     246                 :          2 :         return false;
#     247                 :          2 :     }
#     248                 :         27 : 
#     249                 :         27 :     switch (input.size()) {
#     250                 :         15 :     case ADDR_TORV2_SIZE:
#     251                 :         15 :         m_net = NET_ONION;
#     252                 :         15 :         m_addr.assign(input.begin(), input.end());
#     253                 :         15 :         return true;
#     254                 :         10 :     case torv3::TOTAL_LEN: {
#     255                 :         10 :         Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
#     256                 :         10 :         Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
#     257                 :         10 :         Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, torv3::VERSION_LEN};
#     258                 :         10 : 
#     259                 :         10 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
#     260                 :         10 :         torv3::Checksum(input_pubkey, calculated_checksum);
#     261                 :         10 : 
#     262                 :         10 :         if (input_checksum != calculated_checksum ||
#     263                 :          8 :             input_version != MakeSpan(torv3::VERSION).first(torv3::VERSION_LEN)) {
#     264                 :          4 :             return false;
#     265                 :          4 :         }
#     266                 :          6 : 
#     267                 :          6 :         m_net = NET_ONION;
#     268                 :          6 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
#     269                 :          6 :         return true;
#     270                 :          6 :     }
#     271                 :          2 :     }
#     272                 :          2 : 
#     273                 :          2 :     return false;
#     274                 :          2 : }
#     275                 :            : 
#     276                 :            : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
#     277                 :     263522 : {
#     278                 :     263522 :     m_net = NET_IPV4;
#     279                 :     263522 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
#     280                 :     263522 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
#     281                 :     263522 : }
#     282                 :            : 
#     283                 :            : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
#     284                 :       1188 : {
#     285                 :       1188 :     SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
#     286                 :       1188 :     scopeId = scope;
#     287                 :       1188 : }
#     288                 :            : 
#     289                 :            : bool CNetAddr::IsBindAny() const
#     290                 :       1054 : {
#     291                 :       1054 :     if (!IsIPv4() && !IsIPv6()) {
#     292                 :          6 :         return false;
#     293                 :          6 :     }
#     294                 :       8869 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
#     295                 :       1048 : }
#     296                 :            : 
#     297                 :    4324674 : bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
#     298                 :            : 
#     299                 :    3632259 : bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
#     300                 :            : 
#     301                 :            : bool CNetAddr::IsRFC1918() const
#     302                 :     457058 : {
#     303                 :     457058 :     return IsIPv4() && (
#     304                 :     456934 :         m_addr[0] == 10 ||
#     305                 :     456142 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
#     306                 :     456140 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
#     307                 :     457058 : }
#     308                 :            : 
#     309                 :            : bool CNetAddr::IsRFC2544() const
#     310                 :     456266 : {
#     311                 :     456266 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
#     312                 :     456266 : }
#     313                 :            : 
#     314                 :            : bool CNetAddr::IsRFC3927() const
#     315                 :     456264 : {
#     316                 :     456264 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
#     317                 :     456264 : }
#     318                 :            : 
#     319                 :            : bool CNetAddr::IsRFC6598() const
#     320                 :     456257 : {
#     321                 :     456257 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
#     322                 :     456257 : }
#     323                 :            : 
#     324                 :            : bool CNetAddr::IsRFC5737() const
#     325                 :     456257 : {
#     326                 :     456257 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
#     327                 :     456133 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
#     328                 :     456133 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
#     329                 :     456257 : }
#     330                 :            : 
#     331                 :            : bool CNetAddr::IsRFC3849() const
#     332                 :     623507 : {
#     333                 :     623507 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
#     334                 :     623507 : }
#     335                 :            : 
#     336                 :            : bool CNetAddr::IsRFC3964() const
#     337                 :         45 : {
#     338                 :         45 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
#     339                 :         45 : }
#     340                 :            : 
#     341                 :            : bool CNetAddr::IsRFC6052() const
#     342                 :         55 : {
#     343                 :         55 :     return IsIPv6() &&
#     344                 :         40 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
#     345                 :         40 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
#     346                 :         55 : }
#     347                 :            : 
#     348                 :            : bool CNetAddr::IsRFC4380() const
#     349                 :         37 : {
#     350                 :         37 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
#     351                 :         37 : }
#     352                 :            : 
#     353                 :            : bool CNetAddr::IsRFC4862() const
#     354                 :     456259 : {
#     355                 :     456259 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
#     356                 :         86 :                                                                 0x00, 0x00, 0x00, 0x00});
#     357                 :     456259 : }
#     358                 :            : 
#     359                 :            : bool CNetAddr::IsRFC4193() const
#     360                 :     456258 : {
#     361                 :     456258 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
#     362                 :     456258 : }
#     363                 :            : 
#     364                 :            : bool CNetAddr::IsRFC6145() const
#     365                 :         57 : {
#     366                 :         57 :     return IsIPv6() &&
#     367                 :         42 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
#     368                 :         42 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
#     369                 :         57 : }
#     370                 :            : 
#     371                 :            : bool CNetAddr::IsRFC4843() const
#     372                 :     456258 : {
#     373                 :     456258 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     374                 :         18 :            (m_addr[3] & 0xF0) == 0x10;
#     375                 :     456258 : }
#     376                 :            : 
#     377                 :            : bool CNetAddr::IsRFC7343() const
#     378                 :     456258 : {
#     379                 :     456258 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     380                 :         18 :            (m_addr[3] & 0xF0) == 0x20;
#     381                 :     456258 : }
#     382                 :            : 
#     383                 :            : bool CNetAddr::IsHeNet() const
#     384                 :          4 : {
#     385                 :          4 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
#     386                 :          4 : }
#     387                 :            : 
#     388                 :            : /**
#     389                 :            :  * Check whether this object represents a TOR address.
#     390                 :            :  * @see CNetAddr::SetSpecial(const std::string &)
#     391                 :            :  */
#     392                 :       1453 : bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
#     393                 :            : 
#     394                 :            : /**
#     395                 :            :  * Check whether this object represents an I2P address.
#     396                 :            :  */
#     397                 :       1436 : bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
#     398                 :            : 
#     399                 :            : /**
#     400                 :            :  * Check whether this object represents a CJDNS address.
#     401                 :            :  */
#     402                 :          4 : bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
#     403                 :            : 
#     404                 :            : bool CNetAddr::IsLocal() const
#     405                 :     525879 : {
#     406                 :            :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
#     407                 :     525879 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
#     408                 :      39727 :         return true;
#     409                 :      39727 :     }
#     410                 :     486152 : 
#     411                 :            :     // IPv6 loopback (::1/128)
#     412                 :     486152 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
#     413                 :     486152 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
#     414                 :          4 :         return true;
#     415                 :          4 :     }
#     416                 :     486148 : 
#     417                 :     486148 :     return false;
#     418                 :     486148 : }
#     419                 :            : 
#     420                 :            : /**
#     421                 :            :  * @returns Whether or not this network address is a valid address that @a could
#     422                 :            :  *          be used to refer to an actual host.
#     423                 :            :  *
#     424                 :            :  * @note A valid address may or may not be publicly routable on the global
#     425                 :            :  *       internet. As in, the set of valid addresses is a superset of the set of
#     426                 :            :  *       publicly routable addresses.
#     427                 :            :  *
#     428                 :            :  * @see CNetAddr::IsRoutable()
#     429                 :            :  */
#     430                 :            : bool CNetAddr::IsValid() const
#     431                 :     691809 : {
#     432                 :            :     // unspecified IPv6 address (::/128)
#     433                 :     691809 :     unsigned char ipNone6[16] = {};
#     434                 :     691809 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
#     435                 :      68303 :         return false;
#     436                 :      68303 :     }
#     437                 :     623506 : 
#     438                 :            :     // documentation IPv6 address
#     439                 :     623506 :     if (IsRFC3849())
#     440                 :          0 :         return false;
#     441                 :     623506 : 
#     442                 :     623506 :     if (IsInternal())
#     443                 :          2 :         return false;
#     444                 :     623504 : 
#     445                 :     623504 :     if (IsIPv4()) {
#     446                 :     623339 :         const uint32_t addr = ReadBE32(m_addr.data());
#     447                 :     623339 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
#     448                 :         11 :             return false;
#     449                 :         11 :         }
#     450                 :     623493 :     }
#     451                 :     623493 : 
#     452                 :     623493 :     return true;
#     453                 :     623493 : }
#     454                 :            : 
#     455                 :            : /**
#     456                 :            :  * @returns Whether or not this network address is publicly routable on the
#     457                 :            :  *          global internet.
#     458                 :            :  *
#     459                 :            :  * @note A routable address is always valid. As in, the set of routable addresses
#     460                 :            :  *       is a subset of the set of valid addresses.
#     461                 :            :  *
#     462                 :            :  * @see CNetAddr::IsValid()
#     463                 :            :  */
#     464                 :            : bool CNetAddr::IsRoutable() const
#     465                 :     493892 : {
#     466                 :     493892 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
#     467                 :     493892 : }
#     468                 :            : 
#     469                 :            : /**
#     470                 :            :  * @returns Whether or not this is a dummy address that represents a name.
#     471                 :            :  *
#     472                 :            :  * @see CNetAddr::SetInternal(const std::string &)
#     473                 :            :  */
#     474                 :            : bool CNetAddr::IsInternal() const
#     475                 :    1578931 : {
#     476                 :    1578931 :    return m_net == NET_INTERNAL;
#     477                 :    1578931 : }
#     478                 :            : 
#     479                 :            : enum Network CNetAddr::GetNetwork() const
#     480                 :      10727 : {
#     481                 :      10727 :     if (IsInternal())
#     482                 :          2 :         return NET_INTERNAL;
#     483                 :      10725 : 
#     484                 :      10725 :     if (!IsRoutable())
#     485                 :       1003 :         return NET_UNROUTABLE;
#     486                 :       9722 : 
#     487                 :       9722 :     return m_net;
#     488                 :       9722 : }
#     489                 :            : 
#     490                 :            : static std::string IPv6ToString(Span<const uint8_t> a)
#     491                 :          2 : {
#     492                 :          2 :     assert(a.size() == ADDR_IPV6_SIZE);
#     493                 :            :     // clang-format off
#     494                 :          2 :     return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
#     495                 :          2 :                      ReadBE16(&a[0]),
#     496                 :          2 :                      ReadBE16(&a[2]),
#     497                 :          2 :                      ReadBE16(&a[4]),
#     498                 :          2 :                      ReadBE16(&a[6]),
#     499                 :          2 :                      ReadBE16(&a[8]),
#     500                 :          2 :                      ReadBE16(&a[10]),
#     501                 :          2 :                      ReadBE16(&a[12]),
#     502                 :          2 :                      ReadBE16(&a[14]));
#     503                 :            :     // clang-format on
#     504                 :          2 : }
#     505                 :            : 
#     506                 :            : std::string CNetAddr::ToStringIP() const
#     507                 :     231021 : {
#     508                 :     231021 :     switch (m_net) {
#     509                 :     231000 :     case NET_IPV4:
#     510                 :     231000 :     case NET_IPV6: {
#     511                 :     231000 :         CService serv(*this, 0);
#     512                 :     231000 :         struct sockaddr_storage sockaddr;
#     513                 :     231000 :         socklen_t socklen = sizeof(sockaddr);
#     514                 :     231000 :         if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
#     515                 :     231000 :             char name[1025] = "";
#     516                 :     231000 :             if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name,
#     517                 :     231000 :                              sizeof(name), nullptr, 0, NI_NUMERICHOST))
#     518                 :     231000 :                 return std::string(name);
#     519                 :          0 :         }
#     520                 :          0 :         if (m_net == NET_IPV4) {
#     521                 :          0 :             return strprintf("%u.%u.%u.%u", m_addr[0], m_addr[1], m_addr[2], m_addr[3]);
#     522                 :          0 :         }
#     523                 :          0 :         return IPv6ToString(m_addr);
#     524                 :          0 :     }
#     525                 :         13 :     case NET_ONION:
#     526                 :         13 :         switch (m_addr.size()) {
#     527                 :          9 :         case ADDR_TORV2_SIZE:
#     528                 :          9 :             return EncodeBase32(m_addr) + ".onion";
#     529                 :          4 :         case ADDR_TORV3_SIZE: {
#     530                 :          4 : 
#     531                 :          4 :             uint8_t checksum[torv3::CHECKSUM_LEN];
#     532                 :          4 :             torv3::Checksum(m_addr, checksum);
#     533                 :          4 : 
#     534                 :            :             // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
#     535                 :          4 :             prevector<torv3::TOTAL_LEN, uint8_t> address{m_addr.begin(), m_addr.end()};
#     536                 :          4 :             address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
#     537                 :          4 :             address.insert(address.end(), torv3::VERSION, torv3::VERSION + torv3::VERSION_LEN);
#     538                 :          4 : 
#     539                 :          4 :             return EncodeBase32(address) + ".onion";
#     540                 :          0 :         }
#     541                 :          0 :         default:
#     542                 :          0 :             assert(false);
#     543                 :         13 :         }
#     544                 :          2 :     case NET_I2P:
#     545                 :          2 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
#     546                 :          2 :     case NET_CJDNS:
#     547                 :          2 :         return IPv6ToString(m_addr);
#     548                 :          4 :     case NET_INTERNAL:
#     549                 :          4 :         return EncodeBase32(m_addr) + ".internal";
#     550                 :          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     551                 :          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     552                 :          0 :         assert(false);
#     553                 :     231021 :     } // no default case, so the compiler can warn about missing cases
#     554                 :     231021 : 
#     555                 :          0 :     assert(false);
#     556                 :          0 : }
#     557                 :            : 
#     558                 :            : std::string CNetAddr::ToString() const
#     559                 :      15148 : {
#     560                 :      15148 :     return ToStringIP();
#     561                 :      15148 : }
#     562                 :            : 
#     563                 :            : bool operator==(const CNetAddr& a, const CNetAddr& b)
#     564                 :      30610 : {
#     565                 :      30610 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
#     566                 :      30610 : }
#     567                 :            : 
#     568                 :            : bool operator<(const CNetAddr& a, const CNetAddr& b)
#     569                 :    1412796 : {
#     570                 :    1412796 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
#     571                 :    1412796 : }
#     572                 :            : 
#     573                 :            : /**
#     574                 :            :  * Try to get our IPv4 address.
#     575                 :            :  *
#     576                 :            :  * @param[out] pipv4Addr The in_addr struct to which to copy.
#     577                 :            :  *
#     578                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     579                 :            :  *          or not our address was an IPv4 address.
#     580                 :            :  *
#     581                 :            :  * @see CNetAddr::IsIPv4()
#     582                 :            :  */
#     583                 :            : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
#     584                 :     230494 : {
#     585                 :     230494 :     if (!IsIPv4())
#     586                 :          0 :         return false;
#     587                 :     230494 :     assert(sizeof(*pipv4Addr) == m_addr.size());
#     588                 :     230494 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
#     589                 :     230494 :     return true;
#     590                 :     230494 : }
#     591                 :            : 
#     592                 :            : /**
#     593                 :            :  * Try to get our IPv6 address.
#     594                 :            :  *
#     595                 :            :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
#     596                 :            :  *
#     597                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     598                 :            :  *          or not our address was an IPv6 address.
#     599                 :            :  *
#     600                 :            :  * @see CNetAddr::IsIPv6()
#     601                 :            :  */
#     602                 :            : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
#     603                 :       1984 : {
#     604                 :       1984 :     if (!IsIPv6()) {
#     605                 :          0 :         return false;
#     606                 :          0 :     }
#     607                 :       1984 :     assert(sizeof(*pipv6Addr) == m_addr.size());
#     608                 :       1984 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
#     609                 :       1984 :     return true;
#     610                 :       1984 : }
#     611                 :            : 
#     612                 :            : bool CNetAddr::HasLinkedIPv4() const
#     613                 :     203218 : {
#     614                 :     203218 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
#     615                 :     203218 : }
#     616                 :            : 
#     617                 :            : uint32_t CNetAddr::GetLinkedIPv4() const
#     618                 :      60181 : {
#     619                 :      60181 :     if (IsIPv4()) {
#     620                 :      60173 :         return ReadBE32(m_addr.data());
#     621                 :          8 :     } else if (IsRFC6052() || IsRFC6145()) {
#     622                 :            :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
#     623                 :          4 :         return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
#     624                 :          4 :     } else if (IsRFC3964()) {
#     625                 :            :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
#     626                 :          2 :         return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
#     627                 :          2 :     } else if (IsRFC4380()) {
#     628                 :            :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
#     629                 :          2 :         return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
#     630                 :          2 :     }
#     631                 :          0 :     assert(false);
#     632                 :          0 : }
#     633                 :            : 
#     634                 :            : uint32_t CNetAddr::GetNetClass() const
#     635                 :     193615 : {
#     636                 :            :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
#     637                 :     193615 : 
#     638                 :            :     // Check for "internal" first because such addresses are also !IsRoutable()
#     639                 :            :     // and we don't want to return NET_UNROUTABLE in that case.
#     640                 :     193615 :     if (IsInternal()) {
#     641                 :          4 :         return NET_INTERNAL;
#     642                 :          4 :     }
#     643                 :     193611 :     if (!IsRoutable()) {
#     644                 :      50583 :         return NET_UNROUTABLE;
#     645                 :      50583 :     }
#     646                 :     143028 :     if (HasLinkedIPv4()) {
#     647                 :     143010 :         return NET_IPV4;
#     648                 :     143010 :     }
#     649                 :         18 :     return m_net;
#     650                 :         18 : }
#     651                 :            : 
#     652                 :     116863 : uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
#     653                 :     116863 :     uint32_t net_class = GetNetClass();
#     654                 :     116863 :     if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
#     655                 :     104041 :         return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
#     656                 :     104041 :     }
#     657                 :      12822 :     std::vector<bool> ip_bits(128);
#     658                 :      12822 :     if (HasLinkedIPv4()) {
#     659                 :            :         // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
#     660                 :     166686 :         for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
#     661                 :    1384776 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     662                 :    1230912 :                 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
#     663                 :    1230912 :             }
#     664                 :     153864 :         }
#     665                 :      12822 :         uint32_t ipv4 = GetLinkedIPv4();
#     666                 :     423126 :         for (int i = 0; i < 32; ++i) {
#     667                 :     410304 :             ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
#     668                 :     410304 :         }
#     669                 :          0 :     } else {
#     670                 :            :         // Use all 128 bits of the IPv6 address otherwise
#     671                 :          0 :         assert(IsIPv6());
#     672                 :          0 :         for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
#     673                 :          0 :             uint8_t cur_byte = m_addr[byte_i];
#     674                 :          0 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     675                 :          0 :                 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
#     676                 :          0 :             }
#     677                 :          0 :         }
#     678                 :          0 :     }
#     679                 :      12822 :     uint32_t mapped_as = Interpret(asmap, ip_bits);
#     680                 :      12822 :     return mapped_as;
#     681                 :      12822 : }
#     682                 :            : 
#     683                 :            : /**
#     684                 :            :  * Get the canonical identifier of our network group
#     685                 :            :  *
#     686                 :            :  * The groups are assigned in a way where it should be costly for an attacker to
#     687                 :            :  * obtain addresses with many different group identifiers, even if it is cheap
#     688                 :            :  * to obtain addresses with the same identifier.
#     689                 :            :  *
#     690                 :            :  * @note No two connections will be attempted to addresses with the same network
#     691                 :            :  *       group.
#     692                 :            :  */
#     693                 :            : std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
#     694                 :      76752 : {
#     695                 :      76752 :     std::vector<unsigned char> vchRet;
#     696                 :      76752 :     uint32_t net_class = GetNetClass();
#     697                 :            :     // If non-empty asmap is supplied and the address is IPv4/IPv6,
#     698                 :            :     // return ASN to be used for bucketing.
#     699                 :      76752 :     uint32_t asn = GetMappedAS(asmap);
#     700                 :      76752 :     if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
#     701                 :       7246 :         vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
#     702                 :      36230 :         for (int i = 0; i < 4; i++) {
#     703                 :      28984 :             vchRet.push_back((asn >> (8 * i)) & 0xFF);
#     704                 :      28984 :         }
#     705                 :       7246 :         return vchRet;
#     706                 :       7246 :     }
#     707                 :      69506 : 
#     708                 :      69506 :     vchRet.push_back(net_class);
#     709                 :      69506 :     int nBits{0};
#     710                 :      69506 : 
#     711                 :      69506 :     if (IsLocal()) {
#     712                 :            :         // all local addresses belong to the same group
#     713                 :      59303 :     } else if (IsInternal()) {
#     714                 :            :         // all internal-usage addresses get their own group
#     715                 :          2 :         nBits = ADDR_INTERNAL_SIZE * 8;
#     716                 :      59301 :     } else if (!IsRoutable()) {
#     717                 :            :         // all other unroutable addresses belong to the same group
#     718                 :      47368 :     } else if (HasLinkedIPv4()) {
#     719                 :            :         // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
#     720                 :      47359 :         uint32_t ipv4 = GetLinkedIPv4();
#     721                 :      47359 :         vchRet.push_back((ipv4 >> 24) & 0xFF);
#     722                 :      47359 :         vchRet.push_back((ipv4 >> 16) & 0xFF);
#     723                 :      47359 :         return vchRet;
#     724                 :          9 :     } else if (IsTor() || IsI2P() || IsCJDNS()) {
#     725                 :          5 :         nBits = 4;
#     726                 :          4 :     } else if (IsHeNet()) {
#     727                 :            :         // for he.net, use /36 groups
#     728                 :          2 :         nBits = 36;
#     729                 :          2 :     } else {
#     730                 :            :         // for the rest of the IPv6 network, use /32 groups
#     731                 :          2 :         nBits = 32;
#     732                 :          2 :     }
#     733                 :      69506 : 
#     734                 :            :     // Push our address onto vchRet.
#     735                 :      22147 :     const size_t num_bytes = nBits / 8;
#     736                 :      22147 :     vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
#     737                 :      22147 :     nBits %= 8;
#     738                 :            :     // ...for the last byte, push nBits and for the rest of the byte push 1's
#     739                 :      22147 :     if (nBits > 0) {
#     740                 :          7 :         assert(num_bytes < m_addr.size());
#     741                 :          7 :         vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
#     742                 :          7 :     }
#     743                 :      22147 : 
#     744                 :      22147 :     return vchRet;
#     745                 :      69506 : }
#     746                 :            : 
#     747                 :            : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
#     748                 :    1321934 : {
#     749                 :    1321934 :     uint8_t serialized[V1_SERIALIZATION_SIZE];
#     750                 :    1321934 :     SerializeV1Array(serialized);
#     751                 :    1321934 :     return {std::begin(serialized), std::end(serialized)};
#     752                 :    1321934 : }
#     753                 :            : 
#     754                 :            : uint64_t CNetAddr::GetHash() const
#     755                 :         10 : {
#     756                 :         10 :     uint256 hash = Hash(m_addr);
#     757                 :         10 :     uint64_t nRet;
#     758                 :         10 :     memcpy(&nRet, &hash, sizeof(nRet));
#     759                 :         10 :     return nRet;
#     760                 :         10 : }
#     761                 :            : 
#     762                 :            : // private extensions to enum Network, only returned by GetExtNetwork,
#     763                 :            : // and only used in GetReachabilityFrom
#     764                 :            : static const int NET_UNKNOWN = NET_MAX + 0;
#     765                 :            : static const int NET_TEREDO  = NET_MAX + 1;
#     766                 :            : int static GetExtNetwork(const CNetAddr *addr)
#     767                 :          0 : {
#     768                 :          0 :     if (addr == nullptr)
#     769                 :          0 :         return NET_UNKNOWN;
#     770                 :          0 :     if (addr->IsRFC4380())
#     771                 :          0 :         return NET_TEREDO;
#     772                 :          0 :     return addr->GetNetwork();
#     773                 :          0 : }
#     774                 :            : 
#     775                 :            : /** Calculates a metric for how reachable (*this) is from a given partner */
#     776                 :            : int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
#     777                 :          2 : {
#     778                 :          2 :     enum Reachability {
#     779                 :          2 :         REACH_UNREACHABLE,
#     780                 :          2 :         REACH_DEFAULT,
#     781                 :          2 :         REACH_TEREDO,
#     782                 :          2 :         REACH_IPV6_WEAK,
#     783                 :          2 :         REACH_IPV4,
#     784                 :          2 :         REACH_IPV6_STRONG,
#     785                 :          2 :         REACH_PRIVATE
#     786                 :          2 :     };
#     787                 :          2 : 
#     788                 :          2 :     if (!IsRoutable() || IsInternal())
#     789                 :          2 :         return REACH_UNREACHABLE;
#     790                 :          0 : 
#     791                 :          0 :     int ourNet = GetExtNetwork(this);
#     792                 :          0 :     int theirNet = GetExtNetwork(paddrPartner);
#     793                 :          0 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
#     794                 :          0 : 
#     795                 :          0 :     switch(theirNet) {
#     796                 :          0 :     case NET_IPV4:
#     797                 :          0 :         switch(ourNet) {
#     798                 :          0 :         default:       return REACH_DEFAULT;
#     799                 :          0 :         case NET_IPV4: return REACH_IPV4;
#     800                 :          0 :         }
#     801                 :          0 :     case NET_IPV6:
#     802                 :          0 :         switch(ourNet) {
#     803                 :          0 :         default:         return REACH_DEFAULT;
#     804                 :          0 :         case NET_TEREDO: return REACH_TEREDO;
#     805                 :          0 :         case NET_IPV4:   return REACH_IPV4;
#     806                 :          0 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
#     807                 :          0 :         }
#     808                 :          0 :     case NET_ONION:
#     809                 :          0 :         switch(ourNet) {
#     810                 :          0 :         default:         return REACH_DEFAULT;
#     811                 :          0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
#     812                 :          0 :         case NET_ONION:    return REACH_PRIVATE;
#     813                 :          0 :         }
#     814                 :          0 :     case NET_TEREDO:
#     815                 :          0 :         switch(ourNet) {
#     816                 :          0 :         default:          return REACH_DEFAULT;
#     817                 :          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     818                 :          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     819                 :          0 :         case NET_IPV4:    return REACH_IPV4;
#     820                 :          0 :         }
#     821                 :          0 :     case NET_UNKNOWN:
#     822                 :          0 :     case NET_UNROUTABLE:
#     823                 :          0 :     default:
#     824                 :          0 :         switch(ourNet) {
#     825                 :          0 :         default:          return REACH_DEFAULT;
#     826                 :          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     827                 :          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     828                 :          0 :         case NET_IPV4:    return REACH_IPV4;
#     829                 :          0 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
#     830                 :          0 :         }
#     831                 :          0 :     }
#     832                 :          0 : }
#     833                 :            : 
#     834                 :            : CService::CService() : port(0)
#     835                 :     823147 : {
#     836                 :     823147 : }
#     837                 :            : 
#     838                 :            : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
#     839                 :     479922 : {
#     840                 :     479922 : }
#     841                 :            : 
#     842                 :            : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
#     843                 :          5 : {
#     844                 :          5 : }
#     845                 :            : 
#     846                 :            : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
#     847                 :          3 : {
#     848                 :          3 : }
#     849                 :            : 
#     850                 :            : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
#     851                 :       1177 : {
#     852                 :       1177 :     assert(addr.sin_family == AF_INET);
#     853                 :       1177 : }
#     854                 :            : 
#     855                 :            : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
#     856                 :          0 : {
#     857                 :          0 :    assert(addr.sin6_family == AF_INET6);
#     858                 :          0 : }
#     859                 :            : 
#     860                 :            : bool CService::SetSockAddr(const struct sockaddr *paddr)
#     861                 :       1177 : {
#     862                 :       1177 :     switch (paddr->sa_family) {
#     863                 :       1177 :     case AF_INET:
#     864                 :       1177 :         *this = CService(*(const struct sockaddr_in*)paddr);
#     865                 :       1177 :         return true;
#     866                 :          0 :     case AF_INET6:
#     867                 :          0 :         *this = CService(*(const struct sockaddr_in6*)paddr);
#     868                 :          0 :         return true;
#     869                 :          0 :     default:
#     870                 :          0 :         return false;
#     871                 :       1177 :     }
#     872                 :       1177 : }
#     873                 :            : 
#     874                 :            : uint16_t CService::GetPort() const
#     875                 :      17892 : {
#     876                 :      17892 :     return port;
#     877                 :      17892 : }
#     878                 :            : 
#     879                 :            : bool operator==(const CService& a, const CService& b)
#     880                 :       1001 : {
#     881                 :       1001 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
#     882                 :       1001 : }
#     883                 :            : 
#     884                 :            : bool operator<(const CService& a, const CService& b)
#     885                 :       5864 : {
#     886                 :       5864 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
#     887                 :       5864 : }
#     888                 :            : 
#     889                 :            : /**
#     890                 :            :  * Obtain the IPv4/6 socket address this represents.
#     891                 :            :  *
#     892                 :            :  * @param[out] paddr The obtained socket address.
#     893                 :            :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
#     894                 :            :  *                        to by paddr. The value that's pointed to by this
#     895                 :            :  *                        parameter might change after calling this function if
#     896                 :            :  *                        the size of the corresponding address structure
#     897                 :            :  *                        changed.
#     898                 :            :  *
#     899                 :            :  * @returns Whether or not the operation was successful.
#     900                 :            :  */
#     901                 :            : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
#     902                 :     232478 : {
#     903                 :     232478 :     if (IsIPv4()) {
#     904                 :     230494 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
#     905                 :          0 :             return false;
#     906                 :     230494 :         *addrlen = sizeof(struct sockaddr_in);
#     907                 :     230494 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
#     908                 :     230494 :         memset(paddrin, 0, *addrlen);
#     909                 :     230494 :         if (!GetInAddr(&paddrin->sin_addr))
#     910                 :          0 :             return false;
#     911                 :     230494 :         paddrin->sin_family = AF_INET;
#     912                 :     230494 :         paddrin->sin_port = htons(port);
#     913                 :     230494 :         return true;
#     914                 :     230494 :     }
#     915                 :       1984 :     if (IsIPv6()) {
#     916                 :       1984 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
#     917                 :          0 :             return false;
#     918                 :       1984 :         *addrlen = sizeof(struct sockaddr_in6);
#     919                 :       1984 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
#     920                 :       1984 :         memset(paddrin6, 0, *addrlen);
#     921                 :       1984 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
#     922                 :          0 :             return false;
#     923                 :       1984 :         paddrin6->sin6_scope_id = scopeId;
#     924                 :       1984 :         paddrin6->sin6_family = AF_INET6;
#     925                 :       1984 :         paddrin6->sin6_port = htons(port);
#     926                 :       1984 :         return true;
#     927                 :       1984 :     }
#     928                 :          0 :     return false;
#     929                 :          0 : }
#     930                 :            : 
#     931                 :            : /**
#     932                 :            :  * @returns An identifier unique to this service's address and port number.
#     933                 :            :  */
#     934                 :            : std::vector<unsigned char> CService::GetKey() const
#     935                 :    1292233 : {
#     936                 :    1292233 :     auto key = GetAddrBytes();
#     937                 :    1292233 :     key.push_back(port / 0x100); // most significant byte of our port
#     938                 :    1292233 :     key.push_back(port & 0x0FF); // least significant byte of our port
#     939                 :    1292233 :     return key;
#     940                 :    1292233 : }
#     941                 :            : 
#     942                 :            : std::string CService::ToStringPort() const
#     943                 :     164446 : {
#     944                 :     164446 :     return strprintf("%u", port);
#     945                 :     164446 : }
#     946                 :            : 
#     947                 :            : std::string CService::ToStringIPPort() const
#     948                 :     164446 : {
#     949                 :     164446 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
#     950                 :     163014 :         return ToStringIP() + ":" + ToStringPort();
#     951                 :       1432 :     } else {
#     952                 :       1432 :         return "[" + ToStringIP() + "]:" + ToStringPort();
#     953                 :       1432 :     }
#     954                 :     164446 : }
#     955                 :            : 
#     956                 :            : std::string CService::ToString() const
#     957                 :     159485 : {
#     958                 :     159485 :     return ToStringIPPort();
#     959                 :     159485 : }
#     960                 :            : 
#     961                 :            : CSubNet::CSubNet():
#     962                 :            :     valid(false)
#     963                 :       1649 : {
#     964                 :       1649 :     memset(netmask, 0, sizeof(netmask));
#     965                 :       1649 : }
#     966                 :            : 
#     967                 :            : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
#     968                 :        593 : {
#     969                 :        593 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
#     970                 :         24 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
#     971                 :        593 :     if (!valid) {
#     972                 :          5 :         return;
#     973                 :          5 :     }
#     974                 :        588 : 
#     975                 :        588 :     assert(mask <= sizeof(netmask) * 8);
#     976                 :        588 : 
#     977                 :        588 :     network = addr;
#     978                 :        588 : 
#     979                 :        588 :     uint8_t n = mask;
#     980                 :       3168 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
#     981                 :       1818 :         const uint8_t bits = n < 8 ? n : 8;
#     982                 :       2580 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
#     983                 :       2580 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
#     984                 :       2580 :         n -= bits;
#     985                 :       2580 :     }
#     986                 :        588 : }
#     987                 :            : 
#     988                 :            : /**
#     989                 :            :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
#     990                 :            :  *          the specified subnet mask is not a valid one, -1.
#     991                 :            :  */
#     992                 :            : static inline int NetmaskBits(uint8_t x)
#     993                 :       9706 : {
#     994                 :       9706 :     switch(x) {
#     995                 :        216 :     case 0x00: return 0;
#     996                 :         16 :     case 0x80: return 1;
#     997                 :         16 :     case 0xc0: return 2;
#     998                 :         19 :     case 0xe0: return 3;
#     999                 :         16 :     case 0xf0: return 4;
#    1000                 :         16 :     case 0xf8: return 5;
#    1001                 :         20 :     case 0xfc: return 6;
#    1002                 :         18 :     case 0xfe: return 7;
#    1003                 :       9365 :     case 0xff: return 8;
#    1004                 :          4 :     default: return -1;
#    1005                 :       9706 :     }
#    1006                 :       9706 : }
#    1007                 :            : 
#    1008                 :            : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
#    1009                 :        100 : {
#    1010                 :        100 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
#    1011                 :        100 :     if (!valid) {
#    1012                 :          6 :         return;
#    1013                 :          6 :     }
#    1014                 :            :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
#    1015                 :         94 :     bool zeros_found = false;
#    1016                 :        476 :     for (auto b : mask.m_addr) {
#    1017                 :        476 :         const int num_bits = NetmaskBits(b);
#    1018                 :        476 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
#    1019                 :          8 :             valid = false;
#    1020                 :          8 :             return;
#    1021                 :          8 :         }
#    1022                 :        468 :         if (num_bits < 8) {
#    1023                 :        276 :             zeros_found = true;
#    1024                 :        276 :         }
#    1025                 :        468 :     }
#    1026                 :         94 : 
#    1027                 :         86 :     assert(mask.m_addr.size() <= sizeof(netmask));
#    1028                 :         86 : 
#    1029                 :         86 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
#    1030                 :         86 : 
#    1031                 :         86 :     network = addr;
#    1032                 :         86 : 
#    1033                 :            :     // Normalize network according to netmask
#    1034                 :        526 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
#    1035                 :        440 :         network.m_addr[x] &= netmask[x];
#    1036                 :        440 :     }
#    1037                 :         86 : }
#    1038                 :            : 
#    1039                 :            : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
#    1040                 :        630 : {
#    1041                 :        630 :     valid = addr.IsIPv4() || addr.IsIPv6();
#    1042                 :        630 :     if (!valid) {
#    1043                 :          2 :         return;
#    1044                 :          2 :     }
#    1045                 :        628 : 
#    1046                 :        628 :     assert(addr.m_addr.size() <= sizeof(netmask));
#    1047                 :        628 : 
#    1048                 :        628 :     memset(netmask, 0xFF, addr.m_addr.size());
#    1049                 :        628 : 
#    1050                 :        628 :     network = addr;
#    1051                 :        628 : }
#    1052                 :            : 
#    1053                 :            : /**
#    1054                 :            :  * @returns True if this subnet is valid, the specified address is valid, and
#    1055                 :            :  *          the specified address belongs in this subnet.
#    1056                 :            :  */
#    1057                 :            : bool CSubNet::Match(const CNetAddr &addr) const
#    1058                 :      74832 : {
#    1059                 :      74832 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
#    1060                 :         16 :         return false;
#    1061                 :      74816 :     assert(network.m_addr.size() == addr.m_addr.size());
#    1062                 :     374190 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
#    1063                 :     299388 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
#    1064                 :         14 :             return false;
#    1065                 :         14 :         }
#    1066                 :     299388 :     }
#    1067                 :      74802 :     return true;
#    1068                 :      74816 : }
#    1069                 :            : 
#    1070                 :            : std::string CSubNet::ToString() const
#    1071                 :       1155 : {
#    1072                 :       1155 :     assert(network.m_addr.size() <= sizeof(netmask));
#    1073                 :       1155 : 
#    1074                 :       1155 :     uint8_t cidr = 0;
#    1075                 :       1155 : 
#    1076                 :      10385 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1077                 :       9826 :         if (netmask[i] == 0x00) {
#    1078                 :        596 :             break;
#    1079                 :        596 :         }
#    1080                 :       9230 :         cidr += NetmaskBits(netmask[i]);
#    1081                 :       9230 :     }
#    1082                 :       1155 : 
#    1083                 :       1155 :     return network.ToString() + strprintf("/%u", cidr);
#    1084                 :       1155 : }
#    1085                 :            : 
#    1086                 :            : bool CSubNet::IsValid() const
#    1087                 :        349 : {
#    1088                 :        349 :     return valid;
#    1089                 :        349 : }
#    1090                 :            : 
#    1091                 :            : bool operator==(const CSubNet& a, const CSubNet& b)
#    1092                 :          4 : {
#    1093                 :          4 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
#    1094                 :          4 : }
#    1095                 :            : 
#    1096                 :            : bool operator<(const CSubNet& a, const CSubNet& b)
#    1097                 :        103 : {
#    1098                 :        103 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
#    1099                 :        103 : }
#    1100                 :            : 
#    1101                 :            : bool SanityCheckASMap(const std::vector<bool>& asmap)
#    1102                 :          5 : {
#    1103                 :          5 :     return SanityCheckASMap(asmap, 128); // For IP address lookups, the input is 128 bits
#    1104                 :          5 : }

Generated by: LCOV version 1.14