LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 737 861 85.6 %
Date: 2021-06-29 14:35:33 Functions: 83 84 98.8 %
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: 503 664 75.8 %

           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                 :     129154 : {
#      29         [ -  + ]:     129154 :     switch (m_net) {
#      30         [ +  + ]:     129086 :     case NET_IPV4:
#      31                 :     129086 :         return BIP155Network::IPV4;
#      32         [ +  + ]:         48 :     case NET_IPV6:
#      33                 :         48 :         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         [ +  + ]:         16 :     case NET_I2P:
#      44                 :         16 :         return BIP155Network::I2P;
#      45         [ -  + ]:          4 :     case NET_CJDNS:
#      46                 :          0 :         return BIP155Network::CJDNS;
#      47         [ -  + ]:          4 :     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                 :     129154 :     } // no default case, so the compiler can warn about missing cases
#      52                 :            : 
#      53                 :     129154 :     assert(false);
#      54                 :          0 : }
#      55                 :            : 
#      56                 :            : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
#      57                 :       2664 : {
#      58         [ +  + ]:       2664 :     switch (possible_bip155_net) {
#      59         [ +  + ]:       2579 :     case BIP155Network::IPV4:
#      60         [ +  + ]:       2579 :         if (address_size == ADDR_IPV4_SIZE) {
#      61                 :       2577 :             m_net = NET_IPV4;
#      62                 :       2577 :             return true;
#      63                 :       2577 :         }
#      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         [ +  + ]:         46 :     case BIP155Network::IPV6:
#      68         [ +  + ]:         46 :         if (address_size == ADDR_IPV6_SIZE) {
#      69                 :         44 :             m_net = NET_IPV6;
#      70                 :         44 :             return true;
#      71                 :         44 :         }
#      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         [ +  + ]:         20 :     case BIP155Network::I2P:
#      92         [ +  + ]:         20 :         if (address_size == ADDR_I2P_SIZE) {
#      93                 :         18 :             m_net = NET_I2P;
#      94                 :         18 :             return true;
#      95                 :         18 :         }
#      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         [ +  + ]:          6 :     case BIP155Network::CJDNS:
#     100         [ +  + ]:          6 :         if (address_size == ADDR_CJDNS_SIZE) {
#     101                 :          4 :             m_net = NET_CJDNS;
#     102                 :          4 :             return true;
#     103                 :          4 :         }
#     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                 :          5 :     }
#     108                 :            : 
#     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                 :          5 :     return false;
#     113                 :          5 : }
#     114                 :            : 
#     115                 :            : /**
#     116                 :            :  * Construct an unspecified IPv6 network address (::/128).
#     117                 :            :  *
#     118                 :            :  * @note This address is considered invalid by CNetAddr::IsValid()
#     119                 :            :  */
#     120                 :    1736043 : 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                 :            : 
#     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                 :       4524 : {
#     155                 :       4524 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
#     156                 :            : 
#     157                 :       4524 :     size_t skip{0};
#     158                 :            : 
#     159         [ +  + ]:       4524 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
#     160                 :            :         // IPv4-in-IPv6
#     161                 :       1758 :         m_net = NET_IPV4;
#     162                 :       1758 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
#     163         [ +  + ]:       2766 :     } 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         [ +  + ]:       2758 :     } 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                 :       2756 :     } else {
#     172                 :            :         // IPv6
#     173                 :       2756 :         m_net = NET_IPV6;
#     174                 :       2756 :     }
#     175                 :            : 
#     176                 :       4524 :     m_addr.assign(ipv6.begin() + skip, ipv6.end());
#     177                 :       4524 : }
#     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                 :        417 : {
#     187         [ -  + ]:        417 :     if (name.empty()) {
#     188                 :          0 :         return false;
#     189                 :          0 :     }
#     190                 :        417 :     m_net = NET_INTERNAL;
#     191                 :        417 :     unsigned char hash[32] = {};
#     192                 :        417 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
#     193                 :        417 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
#     194                 :        417 :     return true;
#     195                 :        417 : }
#     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 const unsigned char VERSION[] = {3};
#     201                 :            : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
#     202                 :            : 
#     203                 :            : static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
#     204                 :         28 : {
#     205                 :            :     // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
#     206                 :         28 :     static const unsigned char prefix[] = ".onion checksum";
#     207                 :         28 :     static constexpr size_t prefix_len = 15;
#     208                 :            : 
#     209                 :         28 :     SHA3_256 hasher;
#     210                 :            : 
#     211                 :         28 :     hasher.Write(MakeSpan(prefix).first(prefix_len));
#     212                 :         28 :     hasher.Write(addr_pubkey);
#     213                 :         28 :     hasher.Write(VERSION);
#     214                 :            : 
#     215                 :         28 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
#     216                 :            : 
#     217                 :         28 :     hasher.Finalize(checksum_full);
#     218                 :            : 
#     219                 :         28 :     memcpy(checksum, checksum_full, sizeof(checksum));
#     220                 :         28 : }
#     221                 :            : 
#     222                 :            : }; // namespace torv3
#     223                 :            : 
#     224                 :            : bool CNetAddr::SetSpecial(const std::string& addr)
#     225                 :     385299 : {
#     226         [ +  + ]:     385299 :     if (!ValidAsCString(addr)) {
#     227                 :          4 :         return false;
#     228                 :          4 :     }
#     229                 :            : 
#     230         [ +  + ]:     385295 :     if (SetTor(addr)) {
#     231                 :         27 :         return true;
#     232                 :         27 :     }
#     233                 :            : 
#     234         [ +  + ]:     385268 :     if (SetI2P(addr)) {
#     235                 :         20 :         return true;
#     236                 :         20 :     }
#     237                 :            : 
#     238                 :     385248 :     return false;
#     239                 :     385248 : }
#     240                 :            : 
#     241                 :            : bool CNetAddr::SetTor(const std::string& addr)
#     242                 :     385295 : {
#     243                 :     385295 :     static const char* suffix{".onion"};
#     244                 :     385295 :     static constexpr size_t suffix_len{6};
#     245                 :            : 
#     246 [ +  + ][ +  + ]:     385295 :     if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) {
#                 [ +  + ]
#     247                 :     385260 :         return false;
#     248                 :     385260 :     }
#     249                 :            : 
#     250                 :         35 :     bool invalid;
#     251                 :         35 :     const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
#     252                 :            : 
#     253         [ +  + ]:         35 :     if (invalid) {
#     254                 :          2 :         return false;
#     255                 :          2 :     }
#     256                 :            : 
#     257         [ +  + ]:         33 :     switch (input.size()) {
#     258         [ +  + ]:         13 :     case ADDR_TORV2_SIZE:
#     259                 :         13 :         m_net = NET_ONION;
#     260                 :         13 :         m_addr.assign(input.begin(), input.end());
#     261                 :         13 :         return true;
#     262         [ +  + ]:         18 :     case torv3::TOTAL_LEN: {
#     263                 :         18 :         Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
#     264                 :         18 :         Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
#     265                 :         18 :         Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
#     266                 :            : 
#     267         [ +  + ]:         18 :         if (input_version != torv3::VERSION) {
#     268                 :          2 :             return false;
#     269                 :          2 :         }
#     270                 :            : 
#     271                 :         16 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
#     272                 :         16 :         torv3::Checksum(input_pubkey, calculated_checksum);
#     273                 :            : 
#     274         [ +  + ]:         16 :         if (input_checksum != calculated_checksum) {
#     275                 :          2 :             return false;
#     276                 :          2 :         }
#     277                 :            : 
#     278                 :         14 :         m_net = NET_ONION;
#     279                 :         14 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
#     280                 :         14 :         return true;
#     281                 :         14 :     }
#     282                 :          2 :     }
#     283                 :            : 
#     284                 :          2 :     return false;
#     285                 :          2 : }
#     286                 :            : 
#     287                 :            : bool CNetAddr::SetI2P(const std::string& addr)
#     288                 :     385268 : {
#     289                 :            :     // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
#     290                 :     385268 :     static constexpr size_t b32_len{52};
#     291                 :     385268 :     static const char* suffix{".b32.i2p"};
#     292                 :     385268 :     static constexpr size_t suffix_len{8};
#     293                 :            : 
#     294 [ +  + ][ +  + ]:     385268 :     if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
#                 [ -  + ]
#     295                 :     385246 :         return false;
#     296                 :     385246 :     }
#     297                 :            : 
#     298                 :            :     // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
#     299                 :            :     // can decode it.
#     300                 :         22 :     const std::string b32_padded = addr.substr(0, b32_len) + "====";
#     301                 :            : 
#     302                 :         22 :     bool invalid;
#     303                 :         22 :     const auto& address_bytes = DecodeBase32(b32_padded.c_str(), &invalid);
#     304                 :            : 
#     305 [ +  + ][ -  + ]:         22 :     if (invalid || address_bytes.size() != ADDR_I2P_SIZE) {
#     306                 :          2 :         return false;
#     307                 :          2 :     }
#     308                 :            : 
#     309                 :         20 :     m_net = NET_I2P;
#     310                 :         20 :     m_addr.assign(address_bytes.begin(), address_bytes.end());
#     311                 :            : 
#     312                 :         20 :     return true;
#     313                 :         20 : }
#     314                 :            : 
#     315                 :            : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
#     316                 :     385892 : {
#     317                 :     385892 :     m_net = NET_IPV4;
#     318                 :     385892 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
#     319                 :     385892 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
#     320                 :     385892 : }
#     321                 :            : 
#     322                 :            : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
#     323                 :       1567 : {
#     324                 :       1567 :     SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
#     325                 :       1567 :     m_scope_id = scope;
#     326                 :       1567 : }
#     327                 :            : 
#     328                 :            : bool CNetAddr::IsBindAny() const
#     329                 :       1340 : {
#     330 [ +  + ][ +  + ]:       1340 :     if (!IsIPv4() && !IsIPv6()) {
#     331                 :          8 :         return false;
#     332                 :          8 :     }
#     333                 :      11253 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
#     334                 :       1332 : }
#     335                 :            : 
#     336                 :   78979750 : bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
#     337                 :            : 
#     338                 :   72757373 : bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
#     339                 :            : 
#     340                 :            : bool CNetAddr::IsRFC1918() const
#     341                 :   10125518 : {
#     342         [ +  + ]:   10125518 :     return IsIPv4() && (
#     343         [ +  + ]:   10123873 :         m_addr[0] == 10 ||
#     344 [ +  + ][ +  + ]:   10123873 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
#     345 [ +  + ][ +  + ]:   10123873 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
#                 [ +  - ]
#     346                 :   10125518 : }
#     347                 :            : 
#     348                 :            : bool CNetAddr::IsRFC2544() const
#     349                 :   10124982 : {
#     350 [ +  + ][ +  + ]:   10124982 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
#         [ +  + ][ +  + ]
#     351                 :   10124982 : }
#     352                 :            : 
#     353                 :            : bool CNetAddr::IsRFC3927() const
#     354                 :   10124980 : {
#     355 [ +  + ][ +  + ]:   10124980 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
#     356                 :   10124980 : }
#     357                 :            : 
#     358                 :            : bool CNetAddr::IsRFC6598() const
#     359                 :   10124972 : {
#     360 [ +  + ][ +  + ]:   10124972 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
#         [ -  + ][ #  # ]
#     361                 :   10124972 : }
#     362                 :            : 
#     363                 :            : bool CNetAddr::IsRFC5737() const
#     364                 :   10124972 : {
#     365 [ +  + ][ -  + ]:   10124972 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
#     366         [ -  + ]:   10123327 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
#     367         [ -  + ]:   10123327 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
#     368                 :   10124972 : }
#     369                 :            : 
#     370                 :            : bool CNetAddr::IsRFC3849() const
#     371                 :   10389357 : {
#     372 [ +  + ][ +  + ]:   10389357 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
#     373                 :   10389357 : }
#     374                 :            : 
#     375                 :            : bool CNetAddr::IsRFC3964() const
#     376                 :        786 : {
#     377 [ +  + ][ +  + ]:        786 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
#     378                 :        786 : }
#     379                 :            : 
#     380                 :            : bool CNetAddr::IsRFC6052() const
#     381                 :        796 : {
#     382         [ +  + ]:        796 :     return IsIPv6() &&
#     383         [ +  + ]:        796 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
#     384                 :         91 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
#     385                 :        796 : }
#     386                 :            : 
#     387                 :            : bool CNetAddr::IsRFC4380() const
#     388                 :        778 : {
#     389 [ +  + ][ +  + ]:        778 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
#     390                 :        778 : }
#     391                 :            : 
#     392                 :            : bool CNetAddr::IsRFC4862() const
#     393                 :   10124974 : {
#     394 [ +  + ][ +  + ]:   10124974 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
#     395                 :        204 :                                                                 0x00, 0x00, 0x00, 0x00});
#     396                 :   10124974 : }
#     397                 :            : 
#     398                 :            : bool CNetAddr::IsRFC4193() const
#     399                 :   10124974 : {
#     400 [ +  + ][ +  + ]:   10124974 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
#     401                 :   10124974 : }
#     402                 :            : 
#     403                 :            : bool CNetAddr::IsRFC6145() const
#     404                 :        798 : {
#     405         [ +  + ]:        798 :     return IsIPv6() &&
#     406         [ +  + ]:        798 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
#     407                 :         93 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
#     408                 :        798 : }
#     409                 :            : 
#     410                 :            : bool CNetAddr::IsRFC4843() const
#     411                 :   10124974 : {
#     412 [ +  + ][ +  + ]:   10124974 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     413         [ +  + ]:   10124974 :            (m_addr[3] & 0xF0) == 0x10;
#     414                 :   10124974 : }
#     415                 :            : 
#     416                 :            : bool CNetAddr::IsRFC7343() const
#     417                 :   10124974 : {
#     418 [ +  + ][ +  + ]:   10124974 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     419         [ +  + ]:   10124974 :            (m_addr[3] & 0xF0) == 0x20;
#     420                 :   10124974 : }
#     421                 :            : 
#     422                 :            : bool CNetAddr::IsHeNet() const
#     423                 :         13 : {
#     424 [ +  - ][ +  + ]:         13 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
#     425                 :         13 : }
#     426                 :            : 
#     427                 :            : /**
#     428                 :            :  * Check whether this object represents a TOR address.
#     429                 :            :  * @see CNetAddr::SetSpecial(const std::string &)
#     430                 :            :  */
#     431                 :       2058 : bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
#     432                 :            : 
#     433                 :            : /**
#     434                 :            :  * Check whether this object represents an I2P address.
#     435                 :            :  */
#     436                 :      12036 : bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
#     437                 :            : 
#     438                 :            : /**
#     439                 :            :  * Check whether this object represents a CJDNS address.
#     440                 :            :  */
#     441                 :   10389374 : bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
#     442                 :            : 
#     443                 :            : bool CNetAddr::IsLocal() const
#     444                 :   11393296 : {
#     445                 :            :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
#     446 [ +  + ][ +  + ]:   11393296 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
#                 [ +  + ]
#     447                 :      26689 :         return true;
#     448                 :      26689 :     }
#     449                 :            : 
#     450                 :            :     // IPv6 loopback (::1/128)
#     451                 :   11366607 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
#     452 [ +  + ][ +  + ]:   11366607 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
#     453                 :          4 :         return true;
#     454                 :          4 :     }
#     455                 :            : 
#     456                 :   11366603 :     return false;
#     457                 :   11366603 : }
#     458                 :            : 
#     459                 :            : /**
#     460                 :            :  * @returns Whether or not this network address is a valid address that @a could
#     461                 :            :  *          be used to refer to an actual host.
#     462                 :            :  *
#     463                 :            :  * @note A valid address may or may not be publicly routable on the global
#     464                 :            :  *       internet. As in, the set of valid addresses is a superset of the set of
#     465                 :            :  *       publicly routable addresses.
#     466                 :            :  *
#     467                 :            :  * @see CNetAddr::IsRoutable()
#     468                 :            :  */
#     469                 :            : bool CNetAddr::IsValid() const
#     470                 :   10496279 : {
#     471                 :            :     // unspecified IPv6 address (::/128)
#     472                 :   10496279 :     unsigned char ipNone6[16] = {};
#     473 [ +  + ][ +  + ]:   10496279 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
#     474                 :     106922 :         return false;
#     475                 :     106922 :     }
#     476                 :            : 
#     477                 :            :     // CJDNS addresses always start with 0xfc
#     478 [ +  + ][ +  + ]:   10389357 :     if (IsCJDNS() && (m_addr[0] != 0xFC)) {
#     479                 :          2 :         return false;
#     480                 :          2 :     }
#     481                 :            : 
#     482                 :            :     // documentation IPv6 address
#     483         [ -  + ]:   10389355 :     if (IsRFC3849())
#     484                 :          0 :         return false;
#     485                 :            : 
#     486         [ +  + ]:   10389355 :     if (IsInternal())
#     487                 :          2 :         return false;
#     488                 :            : 
#     489         [ +  + ]:   10389353 :     if (IsIPv4()) {
#     490                 :   10387617 :         const uint32_t addr = ReadBE32(m_addr.data());
#     491 [ +  + ][ +  + ]:   10387617 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
#     492                 :         51 :             return false;
#     493                 :         51 :         }
#     494                 :   10389302 :     }
#     495                 :            : 
#     496                 :   10389302 :     return true;
#     497                 :   10389302 : }
#     498                 :            : 
#     499                 :            : /**
#     500                 :            :  * @returns Whether or not this network address is publicly routable on the
#     501                 :            :  *          global internet.
#     502                 :            :  *
#     503                 :            :  * @note A routable address is always valid. As in, the set of routable addresses
#     504                 :            :  *       is a subset of the set of valid addresses.
#     505                 :            :  *
#     506                 :            :  * @see CNetAddr::IsValid()
#     507                 :            :  */
#     508                 :            : bool CNetAddr::IsRoutable() const
#     509                 :   10182935 : {
#     510 [ +  + ][ +  + ]:   10182935 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
#         [ -  + ][ +  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ #  # ][ -  + ]
#         [ -  + ][ +  + ]
#                 [ -  + ]
#     511                 :   10182935 : }
#     512                 :            : 
#     513                 :            : /**
#     514                 :            :  * @returns Whether or not this is a dummy address that represents a name.
#     515                 :            :  *
#     516                 :            :  * @see CNetAddr::SetInternal(const std::string &)
#     517                 :            :  */
#     518                 :            : bool CNetAddr::IsInternal() const
#     519                 :   26121500 : {
#     520                 :   26121500 :    return m_net == NET_INTERNAL;
#     521                 :   26121500 : }
#     522                 :            : 
#     523                 :            : bool CNetAddr::IsAddrV1Compatible() const
#     524                 :  288062880 : {
#     525         [ -  + ]:  288062880 :     switch (m_net) {
#     526         [ +  + ]:  288020204 :     case NET_IPV4:
#     527         [ +  + ]:  288049697 :     case NET_IPV6:
#     528         [ +  + ]:  288049701 :     case NET_INTERNAL:
#     529                 :  288049701 :         return true;
#     530         [ +  + ]:  288049697 :     case NET_ONION:
#     531                 :         17 :         return m_addr.size() == ADDR_TORV2_SIZE;
#     532         [ +  + ]:  288049697 :     case NET_I2P:
#     533         [ +  + ]:      13162 :     case NET_CJDNS:
#     534                 :      13162 :         return false;
#     535         [ -  + ]:      13160 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     536         [ -  + ]:          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     537                 :          0 :         assert(false);
#     538                 :  288062880 :     } // no default case, so the compiler can warn about missing cases
#     539                 :            : 
#     540                 :  288062880 :     assert(false);
#     541                 :          0 : }
#     542                 :            : 
#     543                 :            : enum Network CNetAddr::GetNetwork() const
#     544                 :      14104 : {
#     545         [ +  + ]:      14104 :     if (IsInternal())
#     546                 :          2 :         return NET_INTERNAL;
#     547                 :            : 
#     548         [ +  + ]:      14102 :     if (!IsRoutable())
#     549                 :       1017 :         return NET_UNROUTABLE;
#     550                 :            : 
#     551                 :      13085 :     return m_net;
#     552                 :      13085 : }
#     553                 :            : 
#     554                 :            : static std::string IPv4ToString(Span<const uint8_t> a)
#     555                 :    1516479 : {
#     556                 :    1516479 :     return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
#     557                 :    1516479 : }
#     558                 :            : 
#     559                 :            : // Return an IPv6 address text representation with zero compression as described in RFC 5952
#     560                 :            : // ("A Recommendation for IPv6 Address Text Representation").
#     561                 :            : static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
#     562                 :       2638 : {
#     563                 :       2638 :     assert(a.size() == ADDR_IPV6_SIZE);
#     564                 :       2638 :     const std::array groups{
#     565                 :       2638 :         ReadBE16(&a[0]),
#     566                 :       2638 :         ReadBE16(&a[2]),
#     567                 :       2638 :         ReadBE16(&a[4]),
#     568                 :       2638 :         ReadBE16(&a[6]),
#     569                 :       2638 :         ReadBE16(&a[8]),
#     570                 :       2638 :         ReadBE16(&a[10]),
#     571                 :       2638 :         ReadBE16(&a[12]),
#     572                 :       2638 :         ReadBE16(&a[14]),
#     573                 :       2638 :     };
#     574                 :            : 
#     575                 :            :     // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
#     576                 :            :     // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
#     577                 :       2638 :     struct ZeroSpan {
#     578                 :       2638 :         size_t start_index{0};
#     579                 :       2638 :         size_t len{0};
#     580                 :       2638 :     };
#     581                 :            : 
#     582                 :            :     // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
#     583                 :            :     // zero sequences of equal length are found.
#     584                 :       2638 :     ZeroSpan longest, current;
#     585         [ +  + ]:      23742 :     for (size_t i{0}; i < groups.size(); ++i) {
#     586         [ +  + ]:      21104 :         if (groups[i] != 0) {
#     587                 :       1226 :             current = {i + 1, 0};
#     588                 :       1226 :             continue;
#     589                 :       1226 :         }
#     590                 :      19878 :         current.len += 1;
#     591         [ +  + ]:      19878 :         if (current.len > longest.len) {
#     592                 :      19832 :             longest = current;
#     593                 :      19832 :         }
#     594                 :      19878 :     }
#     595                 :            : 
#     596                 :       2638 :     std::string r;
#     597                 :       2638 :     r.reserve(39);
#     598         [ +  + ]:      23742 :     for (size_t i{0}; i < groups.size(); ++i) {
#     599                 :            :         // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
#     600 [ +  + ][ +  + ]:      21104 :         if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
#                 [ +  + ]
#     601         [ +  + ]:      19822 :             if (i == longest.start_index) {
#     602                 :       2592 :                 r += "::";
#     603                 :       2592 :             }
#     604                 :      19822 :             continue;
#     605                 :      19822 :         }
#     606 [ +  + ][ +  + ]:       1282 :         r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
#     607                 :       1282 :     }
#     608                 :            : 
#     609         [ +  + ]:       2638 :     if (scope_id != 0) {
#     610                 :          2 :         r += strprintf("%%%u", scope_id);
#     611                 :          2 :     }
#     612                 :            : 
#     613                 :       2638 :     return r;
#     614                 :       2638 : }
#     615                 :            : 
#     616                 :            : std::string CNetAddr::ToStringIP() const
#     617                 :    1519359 : {
#     618         [ -  + ]:    1519359 :     switch (m_net) {
#     619         [ +  + ]:    1516479 :     case NET_IPV4:
#     620                 :    1516479 :         return IPv4ToString(m_addr);
#     621         [ +  + ]:       2636 :     case NET_IPV6: {
#     622                 :       2636 :         return IPv6ToString(m_addr, m_scope_id);
#     623                 :          0 :     }
#     624         [ +  + ]:         21 :     case NET_ONION:
#     625                 :         21 :         switch (m_addr.size()) {
#     626         [ +  + ]:          9 :         case ADDR_TORV2_SIZE:
#     627                 :          9 :             return EncodeBase32(m_addr) + ".onion";
#     628         [ +  + ]:         12 :         case ADDR_TORV3_SIZE: {
#     629                 :            : 
#     630                 :         12 :             uint8_t checksum[torv3::CHECKSUM_LEN];
#     631                 :         12 :             torv3::Checksum(m_addr, checksum);
#     632                 :            : 
#     633                 :            :             // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
#     634                 :         12 :             prevector<torv3::TOTAL_LEN, uint8_t> address{m_addr.begin(), m_addr.end()};
#     635                 :         12 :             address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
#     636                 :         12 :             address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
#     637                 :            : 
#     638                 :         12 :             return EncodeBase32(address) + ".onion";
#     639                 :          0 :         }
#     640         [ -  + ]:          0 :         default:
#     641                 :          0 :             assert(false);
#     642                 :         21 :         }
#     643         [ +  + ]:        217 :     case NET_I2P:
#     644                 :        217 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
#     645         [ +  + ]:         21 :     case NET_CJDNS:
#     646                 :          2 :         return IPv6ToString(m_addr, 0);
#     647         [ +  + ]:         21 :     case NET_INTERNAL:
#     648                 :          4 :         return EncodeBase32(m_addr) + ".internal";
#     649         [ -  + ]:         21 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     650         [ -  + ]:          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     651                 :          0 :         assert(false);
#     652                 :    1519359 :     } // no default case, so the compiler can warn about missing cases
#     653                 :            : 
#     654                 :    1519359 :     assert(false);
#     655                 :          0 : }
#     656                 :            : 
#     657                 :            : std::string CNetAddr::ToString() const
#     658                 :      25770 : {
#     659                 :      25770 :     return ToStringIP();
#     660                 :      25770 : }
#     661                 :            : 
#     662                 :            : bool operator==(const CNetAddr& a, const CNetAddr& b)
#     663                 :      40416 : {
#     664 [ +  + ][ +  + ]:      40416 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
#     665                 :      40416 : }
#     666                 :            : 
#     667                 :            : bool operator<(const CNetAddr& a, const CNetAddr& b)
#     668                 : 5428749750 : {
#     669                 : 5428749750 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
#     670                 : 5428749750 : }
#     671                 :            : 
#     672                 :            : /**
#     673                 :            :  * Try to get our IPv4 address.
#     674                 :            :  *
#     675                 :            :  * @param[out] pipv4Addr The in_addr struct to which to copy.
#     676                 :            :  *
#     677                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     678                 :            :  *          or not our address was an IPv4 address.
#     679                 :            :  *
#     680                 :            :  * @see CNetAddr::IsIPv4()
#     681                 :            :  */
#     682                 :            : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
#     683                 :       3564 : {
#     684         [ -  + ]:       3564 :     if (!IsIPv4())
#     685                 :          0 :         return false;
#     686                 :       3564 :     assert(sizeof(*pipv4Addr) == m_addr.size());
#     687                 :       3564 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
#     688                 :       3564 :     return true;
#     689                 :       3564 : }
#     690                 :            : 
#     691                 :            : /**
#     692                 :            :  * Try to get our IPv6 address.
#     693                 :            :  *
#     694                 :            :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
#     695                 :            :  *
#     696                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     697                 :            :  *          or not our address was an IPv6 address.
#     698                 :            :  *
#     699                 :            :  * @see CNetAddr::IsIPv6()
#     700                 :            :  */
#     701                 :            : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
#     702                 :         10 : {
#     703         [ -  + ]:         10 :     if (!IsIPv6()) {
#     704                 :          0 :         return false;
#     705                 :          0 :     }
#     706                 :         10 :     assert(sizeof(*pipv6Addr) == m_addr.size());
#     707                 :         10 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
#     708                 :         10 :     return true;
#     709                 :         10 : }
#     710                 :            : 
#     711                 :            : bool CNetAddr::HasLinkedIPv4() const
#     712                 :    5037488 : {
#     713 [ +  - ][ +  + ]:    5037488 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#     714                 :    5037488 : }
#     715                 :            : 
#     716                 :            : uint32_t CNetAddr::GetLinkedIPv4() const
#     717                 :    1261006 : {
#     718         [ +  + ]:    1261006 :     if (IsIPv4()) {
#     719                 :    1260998 :         return ReadBE32(m_addr.data());
#     720 [ +  + ][ +  + ]:    1260998 :     } else if (IsRFC6052() || IsRFC6145()) {
#     721                 :            :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
#     722                 :          4 :         return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
#     723         [ +  + ]:          4 :     } else if (IsRFC3964()) {
#     724                 :            :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
#     725                 :          2 :         return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
#     726         [ +  - ]:          2 :     } else if (IsRFC4380()) {
#     727                 :            :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
#     728                 :          2 :         return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
#     729                 :          2 :     }
#     730                 :          0 :     assert(false);
#     731                 :          0 : }
#     732                 :            : 
#     733                 :            : Network CNetAddr::GetNetClass() const
#     734                 :    3835248 : {
#     735                 :            :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
#     736                 :            : 
#     737                 :            :     // Check for "internal" first because such addresses are also !IsRoutable()
#     738                 :            :     // and we don't want to return NET_UNROUTABLE in that case.
#     739         [ +  + ]:    3835248 :     if (IsInternal()) {
#     740                 :          4 :         return NET_INTERNAL;
#     741                 :          4 :     }
#     742         [ +  + ]:    3835244 :     if (!IsRoutable()) {
#     743                 :      58944 :         return NET_UNROUTABLE;
#     744                 :      58944 :     }
#     745         [ +  + ]:    3776300 :     if (HasLinkedIPv4()) {
#     746                 :    3775714 :         return NET_IPV4;
#     747                 :    3775714 :     }
#     748                 :        586 :     return m_net;
#     749                 :        586 : }
#     750                 :            : 
#     751                 :    2497214 : uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
#     752                 :    2497214 :     uint32_t net_class = GetNetClass();
#     753 [ +  + ][ +  + ]:    2497214 :     if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
#                 [ +  - ]
#     754                 :    2484392 :         return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
#     755                 :    2484392 :     }
#     756                 :      12822 :     std::vector<bool> ip_bits(128);
#     757         [ +  - ]:      12822 :     if (HasLinkedIPv4()) {
#     758                 :            :         // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
#     759         [ +  + ]:     166686 :         for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
#     760         [ +  + ]:    1384776 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     761                 :    1230912 :                 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
#     762                 :    1230912 :             }
#     763                 :     153864 :         }
#     764                 :      12822 :         uint32_t ipv4 = GetLinkedIPv4();
#     765         [ +  + ]:     423126 :         for (int i = 0; i < 32; ++i) {
#     766                 :     410304 :             ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
#     767                 :     410304 :         }
#     768                 :      12822 :     } else {
#     769                 :            :         // Use all 128 bits of the IPv6 address otherwise
#     770                 :          0 :         assert(IsIPv6());
#     771         [ #  # ]:          0 :         for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
#     772                 :          0 :             uint8_t cur_byte = m_addr[byte_i];
#     773         [ #  # ]:          0 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     774                 :          0 :                 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
#     775                 :          0 :             }
#     776                 :          0 :         }
#     777                 :          0 :     }
#     778                 :      12822 :     uint32_t mapped_as = Interpret(asmap, ip_bits);
#     779                 :      12822 :     return mapped_as;
#     780                 :      12822 : }
#     781                 :            : 
#     782                 :            : /**
#     783                 :            :  * Get the canonical identifier of our network group
#     784                 :            :  *
#     785                 :            :  * The groups are assigned in a way where it should be costly for an attacker to
#     786                 :            :  * obtain addresses with many different group identifiers, even if it is cheap
#     787                 :            :  * to obtain addresses with the same identifier.
#     788                 :            :  *
#     789                 :            :  * @note No two connections will be attempted to addresses with the same network
#     790                 :            :  *       group.
#     791                 :            :  */
#     792                 :            : std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
#     793                 :    1275451 : {
#     794                 :    1275451 :     std::vector<unsigned char> vchRet;
#     795                 :    1275451 :     uint32_t net_class = GetNetClass();
#     796                 :            :     // If non-empty asmap is supplied and the address is IPv4/IPv6,
#     797                 :            :     // return ASN to be used for bucketing.
#     798                 :    1275451 :     uint32_t asn = GetMappedAS(asmap);
#     799         [ +  + ]:    1275451 :     if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
#     800                 :       7246 :         vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
#     801         [ +  + ]:      36230 :         for (int i = 0; i < 4; i++) {
#     802                 :      28984 :             vchRet.push_back((asn >> (8 * i)) & 0xFF);
#     803                 :      28984 :         }
#     804                 :       7246 :         return vchRet;
#     805                 :       7246 :     }
#     806                 :            : 
#     807                 :    1268205 :     vchRet.push_back(net_class);
#     808                 :    1268205 :     int nBits{0};
#     809                 :            : 
#     810         [ +  + ]:    1268205 :     if (IsLocal()) {
#     811                 :            :         // all local addresses belong to the same group
#     812         [ +  + ]:    1267042 :     } else if (IsInternal()) {
#     813                 :            :         // all internal-usage addresses get their own group
#     814                 :          2 :         nBits = ADDR_INTERNAL_SIZE * 8;
#     815         [ +  + ]:    1267040 :     } else if (!IsRoutable()) {
#     816                 :            :         // all other unroutable addresses belong to the same group
#     817         [ +  + ]:    1248366 :     } else if (HasLinkedIPv4()) {
#     818                 :            :         // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
#     819                 :    1248184 :         uint32_t ipv4 = GetLinkedIPv4();
#     820                 :    1248184 :         vchRet.push_back((ipv4 >> 24) & 0xFF);
#     821                 :    1248184 :         vchRet.push_back((ipv4 >> 16) & 0xFF);
#     822                 :    1248184 :         return vchRet;
#     823 [ +  + ][ +  + ]:    1248184 :     } else if (IsTor() || IsI2P() || IsCJDNS()) {
#                 [ -  + ]
#     824                 :        169 :         nBits = 4;
#     825         [ +  + ]:        169 :     } else if (IsHeNet()) {
#     826                 :            :         // for he.net, use /36 groups
#     827                 :          2 :         nBits = 36;
#     828                 :         11 :     } else {
#     829                 :            :         // for the rest of the IPv6 network, use /32 groups
#     830                 :         11 :         nBits = 32;
#     831                 :         11 :     }
#     832                 :            : 
#     833                 :            :     // Push our address onto vchRet.
#     834                 :    1268205 :     const size_t num_bytes = nBits / 8;
#     835                 :      20021 :     vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
#     836                 :      20021 :     nBits %= 8;
#     837                 :            :     // ...for the last byte, push nBits and for the rest of the byte push 1's
#     838         [ +  + ]:      20021 :     if (nBits > 0) {
#     839                 :        171 :         assert(num_bytes < m_addr.size());
#     840                 :        171 :         vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
#     841                 :        171 :     }
#     842                 :            : 
#     843                 :      20021 :     return vchRet;
#     844                 :    1268205 : }
#     845                 :            : 
#     846                 :            : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
#     847                 :  288056550 : {
#     848         [ +  + ]:  288056550 :     if (IsAddrV1Compatible()) {
#     849                 :  288043389 :         uint8_t serialized[V1_SERIALIZATION_SIZE];
#     850                 :  288043389 :         SerializeV1Array(serialized);
#     851                 :  288043389 :         return {std::begin(serialized), std::end(serialized)};
#     852                 :  288043389 :     }
#     853                 :      13161 :     return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
#     854                 :      13161 : }
#     855                 :            : 
#     856                 :            : uint64_t CNetAddr::GetHash() const
#     857                 :         27 : {
#     858                 :         27 :     uint256 hash = Hash(m_addr);
#     859                 :         27 :     uint64_t nRet;
#     860                 :         27 :     memcpy(&nRet, &hash, sizeof(nRet));
#     861                 :         27 :     return nRet;
#     862                 :         27 : }
#     863                 :            : 
#     864                 :            : // private extensions to enum Network, only returned by GetExtNetwork,
#     865                 :            : // and only used in GetReachabilityFrom
#     866                 :            : static const int NET_UNKNOWN = NET_MAX + 0;
#     867                 :            : static const int NET_TEREDO  = NET_MAX + 1;
#     868                 :            : int static GetExtNetwork(const CNetAddr *addr)
#     869                 :          0 : {
#     870         [ #  # ]:          0 :     if (addr == nullptr)
#     871                 :          0 :         return NET_UNKNOWN;
#     872         [ #  # ]:          0 :     if (addr->IsRFC4380())
#     873                 :          0 :         return NET_TEREDO;
#     874                 :          0 :     return addr->GetNetwork();
#     875                 :          0 : }
#     876                 :            : 
#     877                 :            : /** Calculates a metric for how reachable (*this) is from a given partner */
#     878                 :            : int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
#     879                 :          2 : {
#     880                 :          2 :     enum Reachability {
#     881                 :          2 :         REACH_UNREACHABLE,
#     882                 :          2 :         REACH_DEFAULT,
#     883                 :          2 :         REACH_TEREDO,
#     884                 :          2 :         REACH_IPV6_WEAK,
#     885                 :          2 :         REACH_IPV4,
#     886                 :          2 :         REACH_IPV6_STRONG,
#     887                 :          2 :         REACH_PRIVATE
#     888                 :          2 :     };
#     889                 :            : 
#     890 [ +  - ][ #  # ]:          2 :     if (!IsRoutable() || IsInternal())
#     891                 :          2 :         return REACH_UNREACHABLE;
#     892                 :            : 
#     893                 :          0 :     int ourNet = GetExtNetwork(this);
#     894                 :          0 :     int theirNet = GetExtNetwork(paddrPartner);
#     895 [ #  # ][ #  # ]:          0 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
#                 [ #  # ]
#     896                 :            : 
#     897                 :          0 :     switch(theirNet) {
#     898         [ #  # ]:          0 :     case NET_IPV4:
#     899                 :          0 :         switch(ourNet) {
#     900         [ #  # ]:          0 :         default:       return REACH_DEFAULT;
#     901         [ #  # ]:          0 :         case NET_IPV4: return REACH_IPV4;
#     902                 :          0 :         }
#     903         [ #  # ]:          0 :     case NET_IPV6:
#     904                 :          0 :         switch(ourNet) {
#     905         [ #  # ]:          0 :         default:         return REACH_DEFAULT;
#     906         [ #  # ]:          0 :         case NET_TEREDO: return REACH_TEREDO;
#     907         [ #  # ]:          0 :         case NET_IPV4:   return REACH_IPV4;
#     908 [ #  # ][ #  # ]:          0 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
#     909                 :          0 :         }
#     910         [ #  # ]:          0 :     case NET_ONION:
#     911                 :          0 :         switch(ourNet) {
#     912         [ #  # ]:          0 :         default:         return REACH_DEFAULT;
#     913         [ #  # ]:          0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
#     914         [ #  # ]:          0 :         case NET_ONION:    return REACH_PRIVATE;
#     915                 :          0 :         }
#     916         [ #  # ]:          0 :     case NET_I2P:
#     917                 :          0 :         switch (ourNet) {
#     918         [ #  # ]:          0 :         case NET_I2P: return REACH_PRIVATE;
#     919         [ #  # ]:          0 :         default: return REACH_DEFAULT;
#     920                 :          0 :         }
#     921         [ #  # ]:          0 :     case NET_TEREDO:
#     922                 :          0 :         switch(ourNet) {
#     923         [ #  # ]:          0 :         default:          return REACH_DEFAULT;
#     924         [ #  # ]:          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     925         [ #  # ]:          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     926         [ #  # ]:          0 :         case NET_IPV4:    return REACH_IPV4;
#     927                 :          0 :         }
#     928         [ #  # ]:          0 :     case NET_UNKNOWN:
#     929         [ #  # ]:          0 :     case NET_UNROUTABLE:
#     930         [ #  # ]:          0 :     default:
#     931                 :          0 :         switch(ourNet) {
#     932         [ #  # ]:          0 :         default:          return REACH_DEFAULT;
#     933         [ #  # ]:          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     934         [ #  # ]:          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     935         [ #  # ]:          0 :         case NET_IPV4:    return REACH_IPV4;
#     936         [ #  # ]:          0 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
#     937                 :          0 :         }
#     938                 :          0 :     }
#     939                 :          0 : }
#     940                 :            : 
#     941                 :            : CService::CService() : port(0)
#     942                 :    1203738 : {
#     943                 :    1203738 : }
#     944                 :            : 
#     945                 :            : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
#     946                 :     370272 : {
#     947                 :     370272 : }
#     948                 :            : 
#     949                 :            : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
#     950                 :        626 : {
#     951                 :        626 : }
#     952                 :            : 
#     953                 :            : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
#     954                 :          3 : {
#     955                 :          3 : }
#     956                 :            : 
#     957                 :            : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
#     958                 :       1587 : {
#     959                 :       1587 :     assert(addr.sin_family == AF_INET);
#     960                 :       1587 : }
#     961                 :            : 
#     962                 :            : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
#     963                 :          3 : {
#     964                 :          3 :    assert(addr.sin6_family == AF_INET6);
#     965                 :          3 : }
#     966                 :            : 
#     967                 :            : bool CService::SetSockAddr(const struct sockaddr *paddr)
#     968                 :       1590 : {
#     969                 :       1590 :     switch (paddr->sa_family) {
#     970         [ +  + ]:       1587 :     case AF_INET:
#     971                 :       1587 :         *this = CService(*(const struct sockaddr_in*)paddr);
#     972                 :       1587 :         return true;
#     973         [ +  + ]:          3 :     case AF_INET6:
#     974                 :          3 :         *this = CService(*(const struct sockaddr_in6*)paddr);
#     975                 :          3 :         return true;
#     976         [ -  + ]:          0 :     default:
#     977                 :          0 :         return false;
#     978                 :       1590 :     }
#     979                 :       1590 : }
#     980                 :            : 
#     981                 :            : uint16_t CService::GetPort() const
#     982                 :      26966 : {
#     983                 :      26966 :     return port;
#     984                 :      26966 : }
#     985                 :            : 
#     986                 :            : bool operator==(const CService& a, const CService& b)
#     987                 :       2323 : {
#     988 [ +  + ][ +  + ]:       2323 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
#     989                 :       2323 : }
#     990                 :            : 
#     991                 :            : bool operator<(const CService& a, const CService& b)
#     992                 :      13248 : {
#     993 [ +  + ][ +  + ]:      13248 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
#                 [ +  + ]
#     994                 :      13248 : }
#     995                 :            : 
#     996                 :            : /**
#     997                 :            :  * Obtain the IPv4/6 socket address this represents.
#     998                 :            :  *
#     999                 :            :  * @param[out] paddr The obtained socket address.
#    1000                 :            :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
#    1001                 :            :  *                        to by paddr. The value that's pointed to by this
#    1002                 :            :  *                        parameter might change after calling this function if
#    1003                 :            :  *                        the size of the corresponding address structure
#    1004                 :            :  *                        changed.
#    1005                 :            :  *
#    1006                 :            :  * @returns Whether or not the operation was successful.
#    1007                 :            :  */
#    1008                 :            : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
#    1009                 :       3574 : {
#    1010         [ +  + ]:       3574 :     if (IsIPv4()) {
#    1011         [ -  + ]:       3564 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
#    1012                 :          0 :             return false;
#    1013                 :       3564 :         *addrlen = sizeof(struct sockaddr_in);
#    1014                 :       3564 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
#    1015                 :       3564 :         memset(paddrin, 0, *addrlen);
#    1016         [ -  + ]:       3564 :         if (!GetInAddr(&paddrin->sin_addr))
#    1017                 :          0 :             return false;
#    1018                 :       3564 :         paddrin->sin_family = AF_INET;
#    1019                 :       3564 :         paddrin->sin_port = htons(port);
#    1020                 :       3564 :         return true;
#    1021                 :       3564 :     }
#    1022         [ +  - ]:         10 :     if (IsIPv6()) {
#    1023         [ -  + ]:         10 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
#    1024                 :          0 :             return false;
#    1025                 :         10 :         *addrlen = sizeof(struct sockaddr_in6);
#    1026                 :         10 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
#    1027                 :         10 :         memset(paddrin6, 0, *addrlen);
#    1028         [ -  + ]:         10 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
#    1029                 :          0 :             return false;
#    1030                 :         10 :         paddrin6->sin6_scope_id = m_scope_id;
#    1031                 :         10 :         paddrin6->sin6_family = AF_INET6;
#    1032                 :         10 :         paddrin6->sin6_port = htons(port);
#    1033                 :         10 :         return true;
#    1034                 :         10 :     }
#    1035                 :          0 :     return false;
#    1036                 :          0 : }
#    1037                 :            : 
#    1038                 :            : /**
#    1039                 :            :  * @returns An identifier unique to this service's address and port number.
#    1040                 :            :  */
#    1041                 :            : std::vector<unsigned char> CService::GetKey() const
#    1042                 :  288035563 : {
#    1043                 :  288035563 :     auto key = GetAddrBytes();
#    1044                 :  288035563 :     key.push_back(port / 0x100); // most significant byte of our port
#    1045                 :  288035563 :     key.push_back(port & 0x0FF); // least significant byte of our port
#    1046                 :  288035563 :     return key;
#    1047                 :  288035563 : }
#    1048                 :            : 
#    1049                 :            : std::string CService::ToStringPort() const
#    1050                 :     263792 : {
#    1051                 :     263792 :     return strprintf("%u", port);
#    1052                 :     263792 : }
#    1053                 :            : 
#    1054                 :            : std::string CService::ToStringIPPort() const
#    1055                 :     263792 : {
#    1056 [ +  + ][ +  + ]:     263792 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
#         [ +  + ][ -  + ]
#    1057                 :     261957 :         return ToStringIP() + ":" + ToStringPort();
#    1058                 :     261957 :     } else {
#    1059                 :       1835 :         return "[" + ToStringIP() + "]:" + ToStringPort();
#    1060                 :       1835 :     }
#    1061                 :     263792 : }
#    1062                 :            : 
#    1063                 :            : std::string CService::ToString() const
#    1064                 :     238745 : {
#    1065                 :     238745 :     return ToStringIPPort();
#    1066                 :     238745 : }
#    1067                 :            : 
#    1068                 :            : CSubNet::CSubNet():
#    1069                 :            :     valid(false)
#    1070                 :       2015 : {
#    1071                 :       2015 :     memset(netmask, 0, sizeof(netmask));
#    1072                 :       2015 : }
#    1073                 :            : 
#    1074                 :            : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
#    1075                 :        735 : {
#    1076 [ +  + ][ +  + ]:        735 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
#    1077 [ +  + ][ +  + ]:        735 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
#    1078         [ +  + ]:        735 :     if (!valid) {
#    1079                 :          7 :         return;
#    1080                 :          7 :     }
#    1081                 :            : 
#    1082                 :        728 :     assert(mask <= sizeof(netmask) * 8);
#    1083                 :            : 
#    1084                 :        728 :     network = addr;
#    1085                 :            : 
#    1086                 :        728 :     uint8_t n = mask;
#    1087         [ +  + ]:       3868 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1088         [ +  + ]:       3140 :         const uint8_t bits = n < 8 ? n : 8;
#    1089                 :       3140 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
#    1090                 :       3140 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
#    1091                 :       3140 :         n -= bits;
#    1092                 :       3140 :     }
#    1093                 :        728 : }
#    1094                 :            : 
#    1095                 :            : /**
#    1096                 :            :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
#    1097                 :            :  *          the specified subnet mask is not a valid one, -1.
#    1098                 :            :  */
#    1099                 :            : static inline int NetmaskBits(uint8_t x)
#    1100                 :      12086 : {
#    1101                 :      12086 :     switch(x) {
#    1102         [ +  + ]:        216 :     case 0x00: return 0;
#    1103         [ +  + ]:         16 :     case 0x80: return 1;
#    1104         [ +  + ]:         16 :     case 0xc0: return 2;
#    1105         [ +  + ]:         19 :     case 0xe0: return 3;
#    1106         [ +  + ]:         16 :     case 0xf0: return 4;
#    1107         [ +  + ]:         16 :     case 0xf8: return 5;
#    1108         [ +  + ]:         20 :     case 0xfc: return 6;
#    1109         [ +  + ]:         18 :     case 0xfe: return 7;
#    1110         [ +  + ]:      11745 :     case 0xff: return 8;
#    1111         [ +  + ]:          4 :     default: return -1;
#    1112                 :      12086 :     }
#    1113                 :      12086 : }
#    1114                 :            : 
#    1115                 :            : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
#    1116                 :        100 : {
#    1117 [ +  + ][ +  + ]:        100 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
#                 [ +  + ]
#    1118         [ +  + ]:        100 :     if (!valid) {
#    1119                 :          6 :         return;
#    1120                 :          6 :     }
#    1121                 :            :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
#    1122                 :         94 :     bool zeros_found = false;
#    1123         [ +  + ]:        476 :     for (auto b : mask.m_addr) {
#    1124                 :        476 :         const int num_bits = NetmaskBits(b);
#    1125 [ +  + ][ +  + ]:        476 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
#                 [ +  + ]
#    1126                 :          8 :             valid = false;
#    1127                 :          8 :             return;
#    1128                 :          8 :         }
#    1129         [ +  + ]:        468 :         if (num_bits < 8) {
#    1130                 :        276 :             zeros_found = true;
#    1131                 :        276 :         }
#    1132                 :        468 :     }
#    1133                 :            : 
#    1134                 :         94 :     assert(mask.m_addr.size() <= sizeof(netmask));
#    1135                 :            : 
#    1136                 :         86 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
#    1137                 :            : 
#    1138                 :         86 :     network = addr;
#    1139                 :            : 
#    1140                 :            :     // Normalize network according to netmask
#    1141         [ +  + ]:        526 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
#    1142                 :        440 :         network.m_addr[x] &= netmask[x];
#    1143                 :        440 :     }
#    1144                 :         86 : }
#    1145                 :            : 
#    1146                 :            : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
#    1147                 :        802 : {
#    1148         [ -  + ]:        802 :     switch (addr.m_net) {
#    1149         [ +  + ]:        118 :     case NET_IPV4:
#    1150         [ +  + ]:        793 :     case NET_IPV6:
#    1151                 :        793 :         valid = true;
#    1152                 :        793 :         assert(addr.m_addr.size() <= sizeof(netmask));
#    1153                 :        793 :         memset(netmask, 0xFF, addr.m_addr.size());
#    1154                 :        793 :         break;
#    1155         [ +  + ]:        118 :     case NET_ONION:
#    1156         [ -  + ]:          9 :     case NET_I2P:
#    1157         [ -  + ]:          9 :     case NET_CJDNS:
#    1158                 :          9 :         valid = true;
#    1159                 :          9 :         break;
#    1160         [ -  + ]:          9 :     case NET_INTERNAL:
#    1161         [ -  + ]:          0 :     case NET_UNROUTABLE:
#    1162         [ -  + ]:          0 :     case NET_MAX:
#    1163                 :          0 :         return;
#    1164                 :        802 :     }
#    1165                 :            : 
#    1166                 :        802 :     network = addr;
#    1167                 :        802 : }
#    1168                 :            : 
#    1169                 :            : /**
#    1170                 :            :  * @returns True if this subnet is valid, the specified address is valid, and
#    1171                 :            :  *          the specified address belongs in this subnet.
#    1172                 :            :  */
#    1173                 :            : bool CSubNet::Match(const CNetAddr &addr) const
#    1174                 :     111476 : {
#    1175 [ +  + ][ +  + ]:     111476 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
#                 [ +  + ]
#    1176                 :         23 :         return false;
#    1177                 :            : 
#    1178         [ -  + ]:     111453 :     switch (network.m_net) {
#    1179         [ +  + ]:     111435 :     case NET_IPV4:
#    1180         [ +  + ]:     111447 :     case NET_IPV6:
#    1181                 :     111447 :         break;
#    1182         [ +  + ]:     111435 :     case NET_ONION:
#    1183         [ -  + ]:          6 :     case NET_I2P:
#    1184         [ -  + ]:          6 :     case NET_CJDNS:
#    1185         [ -  + ]:          6 :     case NET_INTERNAL:
#    1186                 :          6 :         return addr == network;
#    1187         [ -  + ]:          6 :     case NET_UNROUTABLE:
#    1188         [ -  + ]:          0 :     case NET_MAX:
#    1189                 :          0 :         return false;
#    1190                 :     111447 :     }
#    1191                 :            : 
#    1192                 :     111447 :     assert(network.m_addr.size() == addr.m_addr.size());
#    1193         [ +  + ]:     557337 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
#    1194         [ +  + ]:     445906 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
#    1195                 :         16 :             return false;
#    1196                 :         16 :         }
#    1197                 :     445906 :     }
#    1198                 :     111447 :     return true;
#    1199                 :     111447 : }
#    1200                 :            : 
#    1201                 :            : std::string CSubNet::ToString() const
#    1202                 :       1439 : {
#    1203                 :       1439 :     std::string suffix;
#    1204                 :            : 
#    1205         [ -  + ]:       1439 :     switch (network.m_net) {
#    1206         [ +  + ]:        759 :     case NET_IPV4:
#    1207         [ +  + ]:       1435 :     case NET_IPV6: {
#    1208                 :       1435 :         assert(network.m_addr.size() <= sizeof(netmask));
#    1209                 :            : 
#    1210                 :       1435 :         uint8_t cidr = 0;
#    1211                 :            : 
#    1212         [ +  + ]:      13045 :         for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1213         [ +  + ]:      12346 :             if (netmask[i] == 0x00) {
#    1214                 :        736 :                 break;
#    1215                 :        736 :             }
#    1216                 :      11610 :             cidr += NetmaskBits(netmask[i]);
#    1217                 :      11610 :         }
#    1218                 :            : 
#    1219                 :       1435 :         suffix = strprintf("/%u", cidr);
#    1220                 :       1435 :         break;
#    1221                 :        759 :     }
#    1222         [ +  + ]:        759 :     case NET_ONION:
#    1223         [ -  + ]:          4 :     case NET_I2P:
#    1224         [ -  + ]:          4 :     case NET_CJDNS:
#    1225         [ -  + ]:          4 :     case NET_INTERNAL:
#    1226         [ -  + ]:          4 :     case NET_UNROUTABLE:
#    1227         [ -  + ]:          4 :     case NET_MAX:
#    1228                 :          4 :         break;
#    1229                 :       1439 :     }
#    1230                 :            : 
#    1231                 :       1439 :     return network.ToString() + suffix;
#    1232                 :       1439 : }
#    1233                 :            : 
#    1234                 :            : bool CSubNet::IsValid() const
#    1235                 :        503 : {
#    1236                 :        503 :     return valid;
#    1237                 :        503 : }
#    1238                 :            : 
#    1239                 :            : bool CSubNet::SanityCheck() const
#    1240                 :          5 : {
#    1241         [ -  + ]:          5 :     switch (network.m_net) {
#    1242         [ +  + ]:          4 :     case NET_IPV4:
#    1243         [ +  + ]:          5 :     case NET_IPV6:
#    1244                 :          5 :         break;
#    1245         [ -  + ]:          4 :     case NET_ONION:
#    1246         [ -  + ]:          0 :     case NET_I2P:
#    1247         [ -  + ]:          0 :     case NET_CJDNS:
#    1248                 :          0 :         return true;
#    1249         [ -  + ]:          0 :     case NET_INTERNAL:
#    1250         [ -  + ]:          0 :     case NET_UNROUTABLE:
#    1251         [ -  + ]:          0 :     case NET_MAX:
#    1252                 :          0 :         return false;
#    1253                 :          5 :     }
#    1254                 :            : 
#    1255         [ +  + ]:         37 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
#    1256         [ -  + ]:         32 :         if (network.m_addr[x] & ~netmask[x]) return false;
#    1257                 :         32 :     }
#    1258                 :            : 
#    1259                 :          5 :     return true;
#    1260                 :          5 : }
#    1261                 :            : 
#    1262                 :            : bool operator==(const CSubNet& a, const CSubNet& b)
#    1263                 :          4 : {
#    1264 [ +  - ][ +  + ]:          4 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
#                 [ +  - ]
#    1265                 :          4 : }
#    1266                 :            : 
#    1267                 :            : bool operator<(const CSubNet& a, const CSubNet& b)
#    1268                 :        107 : {
#    1269 [ +  + ][ +  + ]:        107 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
#                 [ +  + ]
#    1270                 :        107 : }
#    1271                 :            : 
#    1272                 :            : bool SanityCheckASMap(const std::vector<bool>& asmap)
#    1273                 :          5 : {
#    1274                 :          5 :     return SanityCheckASMap(asmap, 128); // For IP address lookups, the input is 128 bits
#    1275                 :          5 : }

Generated by: LCOV version 1.14