LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 676 832 81.2 %
Date: 2022-04-21 14:51:19 Functions: 81 83 97.6 %
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: 470 652 72.1 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2021 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                 :      92686 : {
#      29         [ -  + ]:      92686 :     switch (m_net) {
#      30         [ +  + ]:      92651 :     case NET_IPV4:
#      31                 :      92651 :         return BIP155Network::IPV4;
#      32         [ +  + ]:         32 :     case NET_IPV6:
#      33                 :         32 :         return BIP155Network::IPV6;
#      34         [ +  + ]:          2 :     case NET_ONION:
#      35                 :          2 :         return BIP155Network::TORV3;
#      36         [ +  + ]:          1 :     case NET_I2P:
#      37                 :          1 :         return BIP155Network::I2P;
#      38         [ -  + ]:          0 :     case NET_CJDNS:
#      39                 :          0 :         return BIP155Network::CJDNS;
#      40         [ -  + ]:          0 :     case NET_INTERNAL:   // should have been handled before calling this function
#      41         [ -  + ]:          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#      42         [ -  + ]:          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#      43                 :          0 :         assert(false);
#      44                 :      92686 :     } // no default case, so the compiler can warn about missing cases
#      45                 :            : 
#      46                 :          0 :     assert(false);
#      47                 :          0 : }
#      48                 :            : 
#      49                 :            : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
#      50                 :       6330 : {
#      51         [ +  + ]:       6330 :     switch (possible_bip155_net) {
#      52         [ +  + ]:       6173 :     case BIP155Network::IPV4:
#      53         [ +  + ]:       6173 :         if (address_size == ADDR_IPV4_SIZE) {
#      54                 :       6171 :             m_net = NET_IPV4;
#      55                 :       6171 :             return true;
#      56                 :       6171 :         }
#      57                 :          2 :         throw std::ios_base::failure(
#      58                 :          2 :             strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
#      59                 :          2 :                       ADDR_IPV4_SIZE));
#      60         [ +  + ]:         34 :     case BIP155Network::IPV6:
#      61         [ +  + ]:         34 :         if (address_size == ADDR_IPV6_SIZE) {
#      62                 :         32 :             m_net = NET_IPV6;
#      63                 :         32 :             return true;
#      64                 :         32 :         }
#      65                 :          2 :         throw std::ios_base::failure(
#      66                 :          2 :             strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
#      67                 :          2 :                       ADDR_IPV6_SIZE));
#      68         [ +  + ]:          4 :     case BIP155Network::TORV3:
#      69         [ +  + ]:          4 :         if (address_size == ADDR_TORV3_SIZE) {
#      70                 :          2 :             m_net = NET_ONION;
#      71                 :          2 :             return true;
#      72                 :          2 :         }
#      73                 :          2 :         throw std::ios_base::failure(
#      74                 :          2 :             strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
#      75                 :          2 :                       ADDR_TORV3_SIZE));
#      76         [ +  + ]:        106 :     case BIP155Network::I2P:
#      77         [ +  + ]:        106 :         if (address_size == ADDR_I2P_SIZE) {
#      78                 :        104 :             m_net = NET_I2P;
#      79                 :        104 :             return true;
#      80                 :        104 :         }
#      81                 :          2 :         throw std::ios_base::failure(
#      82                 :          2 :             strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
#      83                 :          2 :                       ADDR_I2P_SIZE));
#      84         [ +  + ]:          6 :     case BIP155Network::CJDNS:
#      85         [ +  + ]:          6 :         if (address_size == ADDR_CJDNS_SIZE) {
#      86                 :          4 :             m_net = NET_CJDNS;
#      87                 :          4 :             return true;
#      88                 :          4 :         }
#      89                 :          2 :         throw std::ios_base::failure(
#      90                 :          2 :             strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
#      91                 :          2 :                       ADDR_CJDNS_SIZE));
#      92                 :       6330 :     }
#      93                 :            : 
#      94                 :            :     // Don't throw on addresses with unknown network ids (maybe from the future).
#      95                 :            :     // Instead silently drop them and have the unserialization code consume
#      96                 :            :     // subsequent ones which may be known to us.
#      97                 :          7 :     return false;
#      98                 :       6330 : }
#      99                 :            : 
#     100                 :            : /**
#     101                 :            :  * Construct an unspecified IPv6 network address (::/128).
#     102                 :            :  *
#     103                 :            :  * @note This address is considered invalid by CNetAddr::IsValid()
#     104                 :            :  */
#     105                 :    1902966 : CNetAddr::CNetAddr() {}
#     106                 :            : 
#     107                 :            : void CNetAddr::SetIP(const CNetAddr& ipIn)
#     108                 :          8 : {
#     109                 :            :     // Size check.
#     110         [ -  + ]:          8 :     switch (ipIn.m_net) {
#     111         [ +  + ]:          6 :     case NET_IPV4:
#     112                 :          6 :         assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
#     113                 :          0 :         break;
#     114         [ +  + ]:          2 :     case NET_IPV6:
#     115                 :          2 :         assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
#     116                 :          0 :         break;
#     117         [ -  + ]:          0 :     case NET_ONION:
#     118                 :          0 :         assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
#     119                 :          0 :         break;
#     120         [ -  + ]:          0 :     case NET_I2P:
#     121                 :          0 :         assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
#     122                 :          0 :         break;
#     123         [ -  + ]:          0 :     case NET_CJDNS:
#     124                 :          0 :         assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
#     125                 :          0 :         break;
#     126         [ -  + ]:          0 :     case NET_INTERNAL:
#     127                 :          0 :         assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
#     128                 :          0 :         break;
#     129         [ -  + ]:          0 :     case NET_UNROUTABLE:
#     130         [ -  + ]:          0 :     case NET_MAX:
#     131                 :          0 :         assert(false);
#     132                 :          8 :     } // no default case, so the compiler can warn about missing cases
#     133                 :            : 
#     134                 :          8 :     m_net = ipIn.m_net;
#     135                 :          8 :     m_addr = ipIn.m_addr;
#     136                 :          8 : }
#     137                 :            : 
#     138                 :            : void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
#     139                 :       8783 : {
#     140                 :       8783 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
#     141                 :            : 
#     142                 :          0 :     size_t skip{0};
#     143                 :            : 
#     144         [ +  + ]:       8783 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
#     145                 :            :         // IPv4-in-IPv6
#     146                 :       6262 :         m_net = NET_IPV4;
#     147                 :       6262 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
#     148         [ -  + ]:       6262 :     } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
#     149                 :            :         // TORv2-in-IPv6 (unsupported). Unserialize as !IsValid(), thus ignoring them.
#     150                 :            :         // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
#     151                 :            :         // will not be gossiped, but continue reading next addresses from the stream.
#     152                 :          0 :         m_net = NET_IPV6;
#     153                 :          0 :         m_addr.assign(ADDR_IPV6_SIZE, 0x0);
#     154                 :          0 :         return;
#     155         [ +  + ]:       2521 :     } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
#     156                 :            :         // Internal-in-IPv6
#     157                 :          2 :         m_net = NET_INTERNAL;
#     158                 :          2 :         skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
#     159                 :       2519 :     } else {
#     160                 :            :         // IPv6
#     161                 :       2519 :         m_net = NET_IPV6;
#     162                 :       2519 :     }
#     163                 :            : 
#     164                 :       8783 :     m_addr.assign(ipv6.begin() + skip, ipv6.end());
#     165                 :       8783 : }
#     166                 :            : 
#     167                 :            : /**
#     168                 :            :  * Create an "internal" address that represents a name or FQDN. AddrMan uses
#     169                 :            :  * these fake addresses to keep track of which DNS seeds were used.
#     170                 :            :  * @returns Whether or not the operation was successful.
#     171                 :            :  * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
#     172                 :            :  */
#     173                 :            : bool CNetAddr::SetInternal(const std::string &name)
#     174                 :        423 : {
#     175         [ -  + ]:        423 :     if (name.empty()) {
#     176                 :          0 :         return false;
#     177                 :          0 :     }
#     178                 :        423 :     m_net = NET_INTERNAL;
#     179                 :        423 :     unsigned char hash[32] = {};
#     180                 :        423 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
#     181                 :        423 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
#     182                 :        423 :     return true;
#     183                 :        423 : }
#     184                 :            : 
#     185                 :            : namespace torv3 {
#     186                 :            : // https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt#n2135
#     187                 :            : static constexpr size_t CHECKSUM_LEN = 2;
#     188                 :            : static const unsigned char VERSION[] = {3};
#     189                 :            : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
#     190                 :            : 
#     191                 :            : static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
#     192                 :         46 : {
#     193                 :            :     // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
#     194                 :         46 :     static const unsigned char prefix[] = ".onion checksum";
#     195                 :         46 :     static constexpr size_t prefix_len = 15;
#     196                 :            : 
#     197                 :         46 :     SHA3_256 hasher;
#     198                 :            : 
#     199                 :         46 :     hasher.Write(Span{prefix}.first(prefix_len));
#     200                 :         46 :     hasher.Write(addr_pubkey);
#     201                 :         46 :     hasher.Write(VERSION);
#     202                 :            : 
#     203                 :         46 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
#     204                 :            : 
#     205                 :         46 :     hasher.Finalize(checksum_full);
#     206                 :            : 
#     207                 :         46 :     memcpy(checksum, checksum_full, sizeof(checksum));
#     208                 :         46 : }
#     209                 :            : 
#     210                 :            : }; // namespace torv3
#     211                 :            : 
#     212                 :            : bool CNetAddr::SetSpecial(const std::string& addr)
#     213                 :     468635 : {
#     214         [ +  + ]:     468635 :     if (!ValidAsCString(addr)) {
#     215                 :          4 :         return false;
#     216                 :          4 :     }
#     217                 :            : 
#     218         [ +  + ]:     468631 :     if (SetTor(addr)) {
#     219                 :         25 :         return true;
#     220                 :         25 :     }
#     221                 :            : 
#     222         [ +  + ]:     468606 :     if (SetI2P(addr)) {
#     223                 :          4 :         return true;
#     224                 :          4 :     }
#     225                 :            : 
#     226                 :     468602 :     return false;
#     227                 :     468606 : }
#     228                 :            : 
#     229                 :            : bool CNetAddr::SetTor(const std::string& addr)
#     230                 :     468631 : {
#     231                 :     468631 :     static const char* suffix{".onion"};
#     232                 :     468631 :     static constexpr size_t suffix_len{6};
#     233                 :            : 
#     234 [ +  + ][ +  + ]:     468631 :     if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) {
#                 [ +  + ]
#     235                 :     468592 :         return false;
#     236                 :     468592 :     }
#     237                 :            : 
#     238                 :         39 :     bool invalid;
#     239                 :         39 :     const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
#     240                 :            : 
#     241         [ +  + ]:         39 :     if (invalid) {
#     242                 :          2 :         return false;
#     243                 :          2 :     }
#     244                 :            : 
#     245         [ +  + ]:         37 :     if (input.size() == torv3::TOTAL_LEN) {
#     246                 :         29 :         Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
#     247                 :         29 :         Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
#     248                 :         29 :         Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
#     249                 :            : 
#     250         [ +  + ]:         29 :         if (input_version != torv3::VERSION) {
#     251                 :          2 :             return false;
#     252                 :          2 :         }
#     253                 :            : 
#     254                 :         27 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
#     255                 :         27 :         torv3::Checksum(input_pubkey, calculated_checksum);
#     256                 :            : 
#     257         [ +  + ]:         27 :         if (input_checksum != calculated_checksum) {
#     258                 :          2 :             return false;
#     259                 :          2 :         }
#     260                 :            : 
#     261                 :         25 :         m_net = NET_ONION;
#     262                 :         25 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
#     263                 :         25 :         return true;
#     264                 :         27 :     }
#     265                 :            : 
#     266                 :          8 :     return false;
#     267                 :         37 : }
#     268                 :            : 
#     269                 :            : bool CNetAddr::SetI2P(const std::string& addr)
#     270                 :     468606 : {
#     271                 :            :     // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
#     272                 :     468606 :     static constexpr size_t b32_len{52};
#     273                 :     468606 :     static const char* suffix{".b32.i2p"};
#     274                 :     468606 :     static constexpr size_t suffix_len{8};
#     275                 :            : 
#     276 [ +  + ][ +  + ]:     468606 :     if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
#                 [ -  + ]
#     277                 :     468600 :         return false;
#     278                 :     468600 :     }
#     279                 :            : 
#     280                 :            :     // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
#     281                 :            :     // can decode it.
#     282                 :          6 :     const std::string b32_padded = addr.substr(0, b32_len) + "====";
#     283                 :            : 
#     284                 :          6 :     bool invalid;
#     285                 :          6 :     const auto& address_bytes = DecodeBase32(b32_padded.c_str(), &invalid);
#     286                 :            : 
#     287 [ +  + ][ -  + ]:          6 :     if (invalid || address_bytes.size() != ADDR_I2P_SIZE) {
#     288                 :          2 :         return false;
#     289                 :          2 :     }
#     290                 :            : 
#     291                 :          4 :     m_net = NET_I2P;
#     292                 :          4 :     m_addr.assign(address_bytes.begin(), address_bytes.end());
#     293                 :            : 
#     294                 :          4 :     return true;
#     295                 :          6 : }
#     296                 :            : 
#     297                 :            : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
#     298                 :     468845 : {
#     299                 :     468845 :     m_net = NET_IPV4;
#     300                 :     468845 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
#     301                 :     468845 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
#     302                 :     468845 : }
#     303                 :            : 
#     304                 :            : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
#     305                 :       1821 : {
#     306                 :       1821 :     SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
#     307                 :       1821 :     m_scope_id = scope;
#     308                 :       1821 : }
#     309                 :            : 
#     310                 :            : bool CNetAddr::IsBindAny() const
#     311                 :       1598 : {
#     312 [ +  + ][ +  + ]:       1598 :     if (!IsIPv4() && !IsIPv6()) {
#     313                 :          6 :         return false;
#     314                 :          6 :     }
#     315                 :      13463 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
#     316                 :       1598 : }
#     317                 :            : 
#     318                 :    6195676 : bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
#     319                 :            : 
#     320                 :    5742612 : bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
#     321                 :            : 
#     322                 :            : bool CNetAddr::IsRFC1918() const
#     323                 :     723946 : {
#     324         [ +  + ]:     723946 :     return IsIPv4() && (
#     325         [ +  + ]:     723649 :         m_addr[0] == 10 ||
#     326 [ +  + ][ +  + ]:     723649 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
#     327 [ +  + ][ +  + ]:     723649 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
#                 [ +  - ]
#     328                 :     723946 : }
#     329                 :            : 
#     330                 :            : bool CNetAddr::IsRFC2544() const
#     331                 :     723410 : {
#     332 [ +  + ][ +  + ]:     723410 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
#         [ +  + ][ +  + ]
#     333                 :     723410 : }
#     334                 :            : 
#     335                 :            : bool CNetAddr::IsRFC3927() const
#     336                 :     723408 : {
#     337 [ +  + ][ +  + ]:     723408 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
#     338                 :     723408 : }
#     339                 :            : 
#     340                 :            : bool CNetAddr::IsRFC6598() const
#     341                 :     723400 : {
#     342 [ +  + ][ +  + ]:     723400 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
#         [ -  + ][ #  # ]
#     343                 :     723400 : }
#     344                 :            : 
#     345                 :            : bool CNetAddr::IsRFC5737() const
#     346                 :     723400 : {
#     347 [ +  + ][ -  + ]:     723400 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
#     348         [ -  + ]:     723103 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
#     349         [ -  + ]:     723103 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
#     350                 :     723400 : }
#     351                 :            : 
#     352                 :            : bool CNetAddr::IsRFC3849() const
#     353                 :    1033873 : {
#     354 [ +  + ][ +  + ]:    1033873 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
#     355                 :    1033873 : }
#     356                 :            : 
#     357                 :            : bool CNetAddr::IsRFC3964() const
#     358                 :        117 : {
#     359 [ +  + ][ +  + ]:        117 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
#     360                 :        117 : }
#     361                 :            : 
#     362                 :            : bool CNetAddr::IsRFC6052() const
#     363                 :        127 : {
#     364         [ +  + ]:        127 :     return IsIPv6() &&
#     365         [ +  + ]:        127 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
#     366                 :         90 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
#     367                 :        127 : }
#     368                 :            : 
#     369                 :            : bool CNetAddr::IsRFC4380() const
#     370                 :        109 : {
#     371 [ +  + ][ +  + ]:        109 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
#     372                 :        109 : }
#     373                 :            : 
#     374                 :            : bool CNetAddr::IsRFC4862() const
#     375                 :     723402 : {
#     376 [ +  + ][ +  + ]:     723402 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
#     377                 :        202 :                                                                 0x00, 0x00, 0x00, 0x00});
#     378                 :     723402 : }
#     379                 :            : 
#     380                 :            : bool CNetAddr::IsRFC4193() const
#     381                 :     723402 : {
#     382 [ +  + ][ +  + ]:     723402 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
#     383                 :     723402 : }
#     384                 :            : 
#     385                 :            : bool CNetAddr::IsRFC6145() const
#     386                 :        129 : {
#     387         [ +  + ]:        129 :     return IsIPv6() &&
#     388         [ +  + ]:        129 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
#     389                 :         92 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
#     390                 :        129 : }
#     391                 :            : 
#     392                 :            : bool CNetAddr::IsRFC4843() const
#     393                 :     723402 : {
#     394 [ +  + ][ +  + ]:     723402 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     395         [ +  + ]:     723402 :            (m_addr[3] & 0xF0) == 0x10;
#     396                 :     723402 : }
#     397                 :            : 
#     398                 :            : bool CNetAddr::IsRFC7343() const
#     399                 :     723402 : {
#     400 [ +  + ][ +  + ]:     723402 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     401         [ +  + ]:     723402 :            (m_addr[3] & 0xF0) == 0x20;
#     402                 :     723402 : }
#     403                 :            : 
#     404                 :            : bool CNetAddr::IsHeNet() const
#     405                 :         11 : {
#     406 [ +  - ][ +  + ]:         11 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
#     407                 :         11 : }
#     408                 :            : 
#     409                 :            : /**
#     410                 :            :  * Check whether this object represents a TOR address.
#     411                 :            :  * @see CNetAddr::SetSpecial(const std::string &)
#     412                 :            :  */
#     413                 :        864 : bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
#     414                 :            : 
#     415                 :            : /**
#     416                 :            :  * Check whether this object represents an I2P address.
#     417                 :            :  */
#     418                 :        856 : bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
#     419                 :            : 
#     420                 :            : /**
#     421                 :            :  * Check whether this object represents a CJDNS address.
#     422                 :            :  */
#     423                 :    1033889 : bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
#     424                 :            : 
#     425                 :            : bool CNetAddr::IsLocal() const
#     426                 :     799330 : {
#     427                 :            :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
#     428 [ +  + ][ +  + ]:     799330 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
#                 [ +  + ]
#     429                 :      41383 :         return true;
#     430                 :      41383 :     }
#     431                 :            : 
#     432                 :            :     // IPv6 loopback (::1/128)
#     433                 :     757947 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
#     434 [ +  + ][ +  + ]:     757947 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
#     435                 :          4 :         return true;
#     436                 :          4 :     }
#     437                 :            : 
#     438                 :     757943 :     return false;
#     439                 :     757947 : }
#     440                 :            : 
#     441                 :            : /**
#     442                 :            :  * @returns Whether or not this network address is a valid address that @a could
#     443                 :            :  *          be used to refer to an actual host.
#     444                 :            :  *
#     445                 :            :  * @note A valid address may or may not be publicly routable on the global
#     446                 :            :  *       internet. As in, the set of valid addresses is a superset of the set of
#     447                 :            :  *       publicly routable addresses.
#     448                 :            :  *
#     449                 :            :  * @see CNetAddr::IsRoutable()
#     450                 :            :  */
#     451                 :            : bool CNetAddr::IsValid() const
#     452                 :    1054304 : {
#     453                 :            :     // unspecified IPv6 address (::/128)
#     454                 :    1054304 :     unsigned char ipNone6[16] = {};
#     455 [ +  + ][ +  + ]:    1054304 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
#     456                 :      20431 :         return false;
#     457                 :      20431 :     }
#     458                 :            : 
#     459                 :            :     // CJDNS addresses always start with 0xfc
#     460 [ +  + ][ +  + ]:    1033873 :     if (IsCJDNS() && (m_addr[0] != 0xFC)) {
#     461                 :          2 :         return false;
#     462                 :          2 :     }
#     463                 :            : 
#     464                 :            :     // documentation IPv6 address
#     465         [ -  + ]:    1033871 :     if (IsRFC3849())
#     466                 :          0 :         return false;
#     467                 :            : 
#     468         [ +  + ]:    1033871 :     if (IsInternal())
#     469                 :          2 :         return false;
#     470                 :            : 
#     471         [ +  + ]:    1033869 :     if (IsIPv4()) {
#     472                 :    1033481 :         const uint32_t addr = ReadBE32(m_addr.data());
#     473 [ +  + ][ +  + ]:    1033481 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
#     474                 :         74 :             return false;
#     475                 :         74 :         }
#     476                 :    1033481 :     }
#     477                 :            : 
#     478                 :    1033795 :     return true;
#     479                 :    1033869 : }
#     480                 :            : 
#     481                 :            : /**
#     482                 :            :  * @returns Whether or not this network address is publicly routable on the
#     483                 :            :  *          global internet.
#     484                 :            :  *
#     485                 :            :  * @note A routable address is always valid. As in, the set of routable addresses
#     486                 :            :  *       is a subset of the set of valid addresses.
#     487                 :            :  *
#     488                 :            :  * @see CNetAddr::IsValid()
#     489                 :            :  */
#     490                 :            : bool CNetAddr::IsRoutable() const
#     491                 :     725766 : {
#     492 [ +  + ][ +  + ]:     725766 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
#         [ -  + ][ +  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ +  + ][ -  + ]
#     493                 :     725766 : }
#     494                 :            : 
#     495                 :            : /**
#     496                 :            :  * @returns Whether or not this is a dummy address that represents a name.
#     497                 :            :  *
#     498                 :            :  * @see CNetAddr::SetInternal(const std::string &)
#     499                 :            :  */
#     500                 :            : bool CNetAddr::IsInternal() const
#     501                 :    2641044 : {
#     502                 :    2641044 :    return m_net == NET_INTERNAL;
#     503                 :    2641044 : }
#     504                 :            : 
#     505                 :            : bool CNetAddr::IsAddrV1Compatible() const
#     506                 :     179152 : {
#     507         [ -  + ]:     179152 :     switch (m_net) {
#     508         [ +  + ]:     179107 :     case NET_IPV4:
#     509         [ +  + ]:     179122 :     case NET_IPV6:
#     510         [ +  + ]:     179126 :     case NET_INTERNAL:
#     511                 :     179126 :         return true;
#     512         [ +  + ]:         14 :     case NET_ONION:
#     513         [ +  + ]:         23 :     case NET_I2P:
#     514         [ +  + ]:         26 :     case NET_CJDNS:
#     515                 :         26 :         return false;
#     516         [ -  + ]:          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     517         [ -  + ]:          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     518                 :          0 :         assert(false);
#     519                 :     179152 :     } // no default case, so the compiler can warn about missing cases
#     520                 :            : 
#     521                 :          0 :     assert(false);
#     522                 :          0 : }
#     523                 :            : 
#     524                 :            : enum Network CNetAddr::GetNetwork() const
#     525                 :       2777 : {
#     526         [ +  + ]:       2777 :     if (IsInternal())
#     527                 :          2 :         return NET_INTERNAL;
#     528                 :            : 
#     529         [ +  + ]:       2775 :     if (!IsRoutable())
#     530                 :       1463 :         return NET_UNROUTABLE;
#     531                 :            : 
#     532                 :       1312 :     return m_net;
#     533                 :       2775 : }
#     534                 :            : 
#     535                 :            : static std::string IPv4ToString(Span<const uint8_t> a)
#     536                 :     364113 : {
#     537                 :     364113 :     return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
#     538                 :     364113 : }
#     539                 :            : 
#     540                 :            : // Return an IPv6 address text representation with zero compression as described in RFC 5952
#     541                 :            : // ("A Recommendation for IPv6 Address Text Representation").
#     542                 :            : static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
#     543                 :       1759 : {
#     544                 :       1759 :     assert(a.size() == ADDR_IPV6_SIZE);
#     545                 :          0 :     const std::array groups{
#     546                 :       1759 :         ReadBE16(&a[0]),
#     547                 :       1759 :         ReadBE16(&a[2]),
#     548                 :       1759 :         ReadBE16(&a[4]),
#     549                 :       1759 :         ReadBE16(&a[6]),
#     550                 :       1759 :         ReadBE16(&a[8]),
#     551                 :       1759 :         ReadBE16(&a[10]),
#     552                 :       1759 :         ReadBE16(&a[12]),
#     553                 :       1759 :         ReadBE16(&a[14]),
#     554                 :       1759 :     };
#     555                 :            : 
#     556                 :            :     // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
#     557                 :            :     // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
#     558                 :       1759 :     struct ZeroSpan {
#     559                 :       1759 :         size_t start_index{0};
#     560                 :       1759 :         size_t len{0};
#     561                 :       1759 :     };
#     562                 :            : 
#     563                 :            :     // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
#     564                 :            :     // zero sequences of equal length are found.
#     565                 :       1759 :     ZeroSpan longest, current;
#     566         [ +  + ]:      15831 :     for (size_t i{0}; i < groups.size(); ++i) {
#     567         [ +  + ]:      14072 :         if (groups[i] != 0) {
#     568                 :       1400 :             current = {i + 1, 0};
#     569                 :       1400 :             continue;
#     570                 :       1400 :         }
#     571                 :      12672 :         current.len += 1;
#     572         [ +  + ]:      12672 :         if (current.len > longest.len) {
#     573                 :      12626 :             longest = current;
#     574                 :      12626 :         }
#     575                 :      12672 :     }
#     576                 :            : 
#     577                 :       1759 :     std::string r;
#     578                 :       1759 :     r.reserve(39);
#     579         [ +  + ]:      15831 :     for (size_t i{0}; i < groups.size(); ++i) {
#     580                 :            :         // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
#     581 [ +  + ][ +  + ]:      14072 :         if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
#                 [ +  + ]
#     582         [ +  + ]:      12616 :             if (i == longest.start_index) {
#     583                 :       1710 :                 r += "::";
#     584                 :       1710 :             }
#     585                 :      12616 :             continue;
#     586                 :      12616 :         }
#     587 [ +  + ][ +  + ]:       1456 :         r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
#     588                 :       1456 :     }
#     589                 :            : 
#     590         [ +  + ]:       1759 :     if (scope_id != 0) {
#     591                 :          2 :         r += strprintf("%%%u", scope_id);
#     592                 :          2 :     }
#     593                 :            : 
#     594                 :       1759 :     return r;
#     595                 :       1759 : }
#     596                 :            : 
#     597                 :            : static std::string OnionToString(Span<const uint8_t> addr)
#     598                 :         19 : {
#     599                 :         19 :     uint8_t checksum[torv3::CHECKSUM_LEN];
#     600                 :         19 :     torv3::Checksum(addr, checksum);
#     601                 :            :     // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
#     602                 :         19 :     prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
#     603                 :         19 :     address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
#     604                 :         19 :     address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
#     605                 :         19 :     return EncodeBase32(address) + ".onion";
#     606                 :         19 : }
#     607                 :            : 
#     608                 :            : std::string CNetAddr::ToStringIP() const
#     609                 :     365900 : {
#     610         [ -  + ]:     365900 :     switch (m_net) {
#     611         [ +  + ]:     364113 :     case NET_IPV4:
#     612                 :     364113 :         return IPv4ToString(m_addr);
#     613         [ +  + ]:       1756 :     case NET_IPV6:
#     614                 :       1756 :         return IPv6ToString(m_addr, m_scope_id);
#     615         [ +  + ]:         19 :     case NET_ONION:
#     616                 :         19 :         return OnionToString(m_addr);
#     617         [ +  + ]:          5 :     case NET_I2P:
#     618                 :          5 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
#     619         [ +  + ]:          3 :     case NET_CJDNS:
#     620                 :          3 :         return IPv6ToString(m_addr, 0);
#     621         [ +  + ]:          4 :     case NET_INTERNAL:
#     622                 :          4 :         return EncodeBase32(m_addr) + ".internal";
#     623         [ -  + ]:          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     624         [ -  + ]:          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     625                 :          0 :         assert(false);
#     626                 :     365900 :     } // no default case, so the compiler can warn about missing cases
#     627                 :            : 
#     628                 :          0 :     assert(false);
#     629                 :          0 : }
#     630                 :            : 
#     631                 :            : std::string CNetAddr::ToString() const
#     632                 :      25796 : {
#     633                 :      25796 :     return ToStringIP();
#     634                 :      25796 : }
#     635                 :            : 
#     636                 :            : bool operator==(const CNetAddr& a, const CNetAddr& b)
#     637                 :     151771 : {
#     638 [ +  + ][ +  + ]:     151771 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
#     639                 :     151771 : }
#     640                 :            : 
#     641                 :            : bool operator<(const CNetAddr& a, const CNetAddr& b)
#     642                 :      13753 : {
#     643                 :      13753 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
#     644                 :      13753 : }
#     645                 :            : 
#     646                 :            : /**
#     647                 :            :  * Try to get our IPv4 address.
#     648                 :            :  *
#     649                 :            :  * @param[out] pipv4Addr The in_addr struct to which to copy.
#     650                 :            :  *
#     651                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     652                 :            :  *          or not our address was an IPv4 address.
#     653                 :            :  *
#     654                 :            :  * @see CNetAddr::IsIPv4()
#     655                 :            :  */
#     656                 :            : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
#     657                 :       3730 : {
#     658         [ -  + ]:       3730 :     if (!IsIPv4())
#     659                 :          0 :         return false;
#     660                 :       3730 :     assert(sizeof(*pipv4Addr) == m_addr.size());
#     661                 :          0 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
#     662                 :       3730 :     return true;
#     663                 :       3730 : }
#     664                 :            : 
#     665                 :            : /**
#     666                 :            :  * Try to get our IPv6 (or CJDNS) address.
#     667                 :            :  *
#     668                 :            :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
#     669                 :            :  *
#     670                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     671                 :            :  *          or not our address was an IPv6 address.
#     672                 :            :  *
#     673                 :            :  * @see CNetAddr::IsIPv6()
#     674                 :            :  */
#     675                 :            : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
#     676                 :          8 : {
#     677 [ -  + ][ #  # ]:          8 :     if (!IsIPv6() && !IsCJDNS()) {
#     678                 :          0 :         return false;
#     679                 :          0 :     }
#     680                 :          8 :     assert(sizeof(*pipv6Addr) == m_addr.size());
#     681                 :          0 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
#     682                 :          8 :     return true;
#     683                 :          8 : }
#     684                 :            : 
#     685                 :            : bool CNetAddr::HasLinkedIPv4() const
#     686                 :     331943 : {
#     687 [ +  - ][ +  + ]:     331943 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#     688                 :     331943 : }
#     689                 :            : 
#     690                 :            : uint32_t CNetAddr::GetLinkedIPv4() const
#     691                 :      81614 : {
#     692         [ +  + ]:      81614 :     if (IsIPv4()) {
#     693                 :      81606 :         return ReadBE32(m_addr.data());
#     694 [ +  + ][ +  + ]:      81606 :     } else if (IsRFC6052() || IsRFC6145()) {
#     695                 :            :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
#     696                 :          4 :         return ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
#     697         [ +  + ]:          4 :     } else if (IsRFC3964()) {
#     698                 :            :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
#     699                 :          2 :         return ReadBE32(Span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
#     700         [ +  - ]:          2 :     } else if (IsRFC4380()) {
#     701                 :            :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
#     702                 :          2 :         return ~ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
#     703                 :          2 :     }
#     704                 :          0 :     assert(false);
#     705                 :          0 : }
#     706                 :            : 
#     707                 :            : Network CNetAddr::GetNetClass() const
#     708                 :     284838 : {
#     709                 :            :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
#     710                 :            : 
#     711                 :            :     // Check for "internal" first because such addresses are also !IsRoutable()
#     712                 :            :     // and we don't want to return NET_UNROUTABLE in that case.
#     713         [ +  + ]:     284838 :     if (IsInternal()) {
#     714                 :          4 :         return NET_INTERNAL;
#     715                 :          4 :     }
#     716         [ +  + ]:     284834 :     if (!IsRoutable()) {
#     717                 :      34521 :         return NET_UNROUTABLE;
#     718                 :      34521 :     }
#     719         [ +  + ]:     250313 :     if (HasLinkedIPv4()) {
#     720                 :     250230 :         return NET_IPV4;
#     721                 :     250230 :     }
#     722                 :         83 :     return m_net;
#     723                 :     250313 : }
#     724                 :            : 
#     725                 :     123744 : uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
#     726                 :     123744 :     uint32_t net_class = GetNetClass();
#     727 [ +  + ][ +  + ]:     123744 :     if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
#                 [ +  - ]
#     728                 :     115512 :         return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
#     729                 :     115512 :     }
#     730                 :       8232 :     std::vector<bool> ip_bits(128);
#     731         [ +  - ]:       8232 :     if (HasLinkedIPv4()) {
#     732                 :            :         // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
#     733         [ +  + ]:     107016 :         for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
#     734         [ +  + ]:     889056 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     735                 :     790272 :                 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
#     736                 :     790272 :             }
#     737                 :      98784 :         }
#     738                 :       8232 :         uint32_t ipv4 = GetLinkedIPv4();
#     739         [ +  + ]:     271656 :         for (int i = 0; i < 32; ++i) {
#     740                 :     263424 :             ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
#     741                 :     263424 :         }
#     742                 :       8232 :     } else {
#     743                 :            :         // Use all 128 bits of the IPv6 address otherwise
#     744                 :          0 :         assert(IsIPv6());
#     745         [ #  # ]:          0 :         for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
#     746                 :          0 :             uint8_t cur_byte = m_addr[byte_i];
#     747         [ #  # ]:          0 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     748                 :          0 :                 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
#     749                 :          0 :             }
#     750                 :          0 :         }
#     751                 :          0 :     }
#     752                 :          0 :     uint32_t mapped_as = Interpret(asmap, ip_bits);
#     753                 :       8232 :     return mapped_as;
#     754                 :     123744 : }
#     755                 :            : 
#     756                 :            : /**
#     757                 :            :  * Get the canonical identifier of our network group
#     758                 :            :  *
#     759                 :            :  * The groups are assigned in a way where it should be costly for an attacker to
#     760                 :            :  * obtain addresses with many different group identifiers, even if it is cheap
#     761                 :            :  * to obtain addresses with the same identifier.
#     762                 :            :  *
#     763                 :            :  * @note No two connections will be attempted to addresses with the same network
#     764                 :            :  *       group.
#     765                 :            :  */
#     766                 :            : std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
#     767                 :      83079 : {
#     768                 :      83079 :     std::vector<unsigned char> vchRet;
#     769                 :      83079 :     uint32_t net_class = GetNetClass();
#     770                 :            :     // If non-empty asmap is supplied and the address is IPv4/IPv6,
#     771                 :            :     // return ASN to be used for bucketing.
#     772                 :      83079 :     uint32_t asn = GetMappedAS(asmap);
#     773         [ +  + ]:      83079 :     if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
#     774                 :       7271 :         vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
#     775         [ +  + ]:      36355 :         for (int i = 0; i < 4; i++) {
#     776                 :      29084 :             vchRet.push_back((asn >> (8 * i)) & 0xFF);
#     777                 :      29084 :         }
#     778                 :       7271 :         return vchRet;
#     779                 :       7271 :     }
#     780                 :            : 
#     781                 :      75808 :     vchRet.push_back(net_class);
#     782                 :      75808 :     int nBits{0};
#     783                 :            : 
#     784         [ +  + ]:      75808 :     if (IsLocal()) {
#     785                 :            :         // all local addresses belong to the same group
#     786         [ +  + ]:      73461 :     } else if (IsInternal()) {
#     787                 :            :         // all internal-usage addresses get their own group
#     788                 :          2 :         nBits = ADDR_INTERNAL_SIZE * 8;
#     789         [ +  + ]:      73459 :     } else if (!IsRoutable()) {
#     790                 :            :         // all other unroutable addresses belong to the same group
#     791         [ +  + ]:      73398 :     } else if (HasLinkedIPv4()) {
#     792                 :            :         // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
#     793                 :      73382 :         uint32_t ipv4 = GetLinkedIPv4();
#     794                 :      73382 :         vchRet.push_back((ipv4 >> 24) & 0xFF);
#     795                 :      73382 :         vchRet.push_back((ipv4 >> 16) & 0xFF);
#     796                 :      73382 :         return vchRet;
#     797 [ +  + ][ -  + ]:      73382 :     } else if (IsTor() || IsI2P()) {
#     798                 :          4 :         nBits = 4;
#     799         [ +  + ]:         12 :     } else if (IsCJDNS()) {
#     800                 :            :         // Treat in the same way as Tor and I2P because the address in all of
#     801                 :            :         // them is "random" bytes (derived from a public key). However in CJDNS
#     802                 :            :         // the first byte is a constant 0xfc, so the random bytes come after it.
#     803                 :            :         // Thus skip the constant 8 bits at the start.
#     804                 :          1 :         nBits = 12;
#     805         [ +  + ]:         11 :     } else if (IsHeNet()) {
#     806                 :            :         // for he.net, use /36 groups
#     807                 :          2 :         nBits = 36;
#     808                 :          9 :     } else {
#     809                 :            :         // for the rest of the IPv6 network, use /32 groups
#     810                 :          9 :         nBits = 32;
#     811                 :          9 :     }
#     812                 :            : 
#     813                 :            :     // Push our address onto vchRet.
#     814                 :       2426 :     const size_t num_bytes = nBits / 8;
#     815                 :       2426 :     vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
#     816                 :       2426 :     nBits %= 8;
#     817                 :            :     // ...for the last byte, push nBits and for the rest of the byte push 1's
#     818         [ +  + ]:       2426 :     if (nBits > 0) {
#     819                 :          7 :         assert(num_bytes < m_addr.size());
#     820                 :          0 :         vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
#     821                 :          7 :     }
#     822                 :            : 
#     823                 :          0 :     return vchRet;
#     824                 :      75808 : }
#     825                 :            : 
#     826                 :            : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
#     827                 :     172221 : {
#     828         [ +  + ]:     172221 :     if (IsAddrV1Compatible()) {
#     829                 :     172212 :         uint8_t serialized[V1_SERIALIZATION_SIZE];
#     830                 :     172212 :         SerializeV1Array(serialized);
#     831                 :     172212 :         return {std::begin(serialized), std::end(serialized)};
#     832                 :     172212 :     }
#     833                 :          9 :     return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
#     834                 :     172221 : }
#     835                 :            : 
#     836                 :            : // private extensions to enum Network, only returned by GetExtNetwork,
#     837                 :            : // and only used in GetReachabilityFrom
#     838                 :            : static const int NET_UNKNOWN = NET_MAX + 0;
#     839                 :            : static const int NET_TEREDO  = NET_MAX + 1;
#     840                 :            : int static GetExtNetwork(const CNetAddr *addr)
#     841                 :          0 : {
#     842         [ #  # ]:          0 :     if (addr == nullptr)
#     843                 :          0 :         return NET_UNKNOWN;
#     844         [ #  # ]:          0 :     if (addr->IsRFC4380())
#     845                 :          0 :         return NET_TEREDO;
#     846                 :          0 :     return addr->GetNetwork();
#     847                 :          0 : }
#     848                 :            : 
#     849                 :            : /** Calculates a metric for how reachable (*this) is from a given partner */
#     850                 :            : int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
#     851                 :          2 : {
#     852                 :          2 :     enum Reachability {
#     853                 :          2 :         REACH_UNREACHABLE,
#     854                 :          2 :         REACH_DEFAULT,
#     855                 :          2 :         REACH_TEREDO,
#     856                 :          2 :         REACH_IPV6_WEAK,
#     857                 :          2 :         REACH_IPV4,
#     858                 :          2 :         REACH_IPV6_STRONG,
#     859                 :          2 :         REACH_PRIVATE
#     860                 :          2 :     };
#     861                 :            : 
#     862 [ +  - ][ #  # ]:          2 :     if (!IsRoutable() || IsInternal())
#     863                 :          2 :         return REACH_UNREACHABLE;
#     864                 :            : 
#     865                 :          0 :     int ourNet = GetExtNetwork(this);
#     866                 :          0 :     int theirNet = GetExtNetwork(paddrPartner);
#     867 [ #  # ][ #  # ]:          0 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
#                 [ #  # ]
#     868                 :            : 
#     869                 :          0 :     switch(theirNet) {
#     870         [ #  # ]:          0 :     case NET_IPV4:
#     871                 :          0 :         switch(ourNet) {
#     872         [ #  # ]:          0 :         default:       return REACH_DEFAULT;
#     873         [ #  # ]:          0 :         case NET_IPV4: return REACH_IPV4;
#     874                 :          0 :         }
#     875         [ #  # ]:          0 :     case NET_IPV6:
#     876                 :          0 :         switch(ourNet) {
#     877         [ #  # ]:          0 :         default:         return REACH_DEFAULT;
#     878         [ #  # ]:          0 :         case NET_TEREDO: return REACH_TEREDO;
#     879         [ #  # ]:          0 :         case NET_IPV4:   return REACH_IPV4;
#     880 [ #  # ][ #  # ]:          0 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
#     881                 :          0 :         }
#     882         [ #  # ]:          0 :     case NET_ONION:
#     883                 :          0 :         switch(ourNet) {
#     884         [ #  # ]:          0 :         default:         return REACH_DEFAULT;
#     885         [ #  # ]:          0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
#     886         [ #  # ]:          0 :         case NET_ONION:    return REACH_PRIVATE;
#     887                 :          0 :         }
#     888         [ #  # ]:          0 :     case NET_I2P:
#     889                 :          0 :         switch (ourNet) {
#     890         [ #  # ]:          0 :         case NET_I2P: return REACH_PRIVATE;
#     891         [ #  # ]:          0 :         default: return REACH_DEFAULT;
#     892                 :          0 :         }
#     893         [ #  # ]:          0 :     case NET_CJDNS:
#     894                 :          0 :         switch (ourNet) {
#     895         [ #  # ]:          0 :         case NET_CJDNS: return REACH_PRIVATE;
#     896         [ #  # ]:          0 :         default: return REACH_DEFAULT;
#     897                 :          0 :         }
#     898         [ #  # ]:          0 :     case NET_TEREDO:
#     899                 :          0 :         switch(ourNet) {
#     900         [ #  # ]:          0 :         default:          return REACH_DEFAULT;
#     901         [ #  # ]:          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     902         [ #  # ]:          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     903         [ #  # ]:          0 :         case NET_IPV4:    return REACH_IPV4;
#     904                 :          0 :         }
#     905         [ #  # ]:          0 :     case NET_UNKNOWN:
#     906         [ #  # ]:          0 :     case NET_UNROUTABLE:
#     907         [ #  # ]:          0 :     default:
#     908                 :          0 :         switch(ourNet) {
#     909         [ #  # ]:          0 :         default:          return REACH_DEFAULT;
#     910         [ #  # ]:          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     911         [ #  # ]:          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     912         [ #  # ]:          0 :         case NET_IPV4:    return REACH_IPV4;
#     913         [ #  # ]:          0 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
#     914                 :          0 :         }
#     915                 :          0 :     }
#     916                 :          0 : }
#     917                 :            : 
#     918                 :            : CService::CService() : port(0)
#     919                 :    1361473 : {
#     920                 :    1361473 : }
#     921                 :            : 
#     922                 :            : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
#     923                 :     451975 : {
#     924                 :     451975 : }
#     925                 :            : 
#     926                 :            : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
#     927                 :        740 : {
#     928                 :        740 : }
#     929                 :            : 
#     930                 :            : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
#     931                 :          2 : {
#     932                 :          2 : }
#     933                 :            : 
#     934                 :            : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
#     935                 :       1784 : {
#     936                 :       1784 :     assert(addr.sin_family == AF_INET);
#     937                 :       1784 : }
#     938                 :            : 
#     939                 :            : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
#     940                 :          3 : {
#     941                 :          3 :    assert(addr.sin6_family == AF_INET6);
#     942                 :          3 : }
#     943                 :            : 
#     944                 :            : bool CService::SetSockAddr(const struct sockaddr *paddr)
#     945                 :       1787 : {
#     946                 :       1787 :     switch (paddr->sa_family) {
#     947         [ +  + ]:       1784 :     case AF_INET:
#     948                 :       1784 :         *this = CService(*(const struct sockaddr_in*)paddr);
#     949                 :       1784 :         return true;
#     950         [ +  + ]:          3 :     case AF_INET6:
#     951                 :          3 :         *this = CService(*(const struct sockaddr_in6*)paddr);
#     952                 :          3 :         return true;
#     953         [ -  + ]:          0 :     default:
#     954                 :          0 :         return false;
#     955                 :       1787 :     }
#     956                 :       1787 : }
#     957                 :            : 
#     958                 :            : uint16_t CService::GetPort() const
#     959                 :      20903 : {
#     960                 :      20903 :     return port;
#     961                 :      20903 : }
#     962                 :            : 
#     963                 :            : bool operator==(const CService& a, const CService& b)
#     964                 :     114403 : {
#     965 [ +  + ][ +  + ]:     114403 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
#     966                 :     114403 : }
#     967                 :            : 
#     968                 :            : bool operator<(const CService& a, const CService& b)
#     969                 :       9551 : {
#     970 [ -  + ][ +  - ]:       9551 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
#                 [ +  + ]
#     971                 :       9551 : }
#     972                 :            : 
#     973                 :            : /**
#     974                 :            :  * Obtain the IPv4/6 socket address this represents.
#     975                 :            :  *
#     976                 :            :  * @param[out] paddr The obtained socket address.
#     977                 :            :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
#     978                 :            :  *                        to by paddr. The value that's pointed to by this
#     979                 :            :  *                        parameter might change after calling this function if
#     980                 :            :  *                        the size of the corresponding address structure
#     981                 :            :  *                        changed.
#     982                 :            :  *
#     983                 :            :  * @returns Whether or not the operation was successful.
#     984                 :            :  */
#     985                 :            : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
#     986                 :       3738 : {
#     987         [ +  + ]:       3738 :     if (IsIPv4()) {
#     988         [ -  + ]:       3730 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
#     989                 :          0 :             return false;
#     990                 :       3730 :         *addrlen = sizeof(struct sockaddr_in);
#     991                 :       3730 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
#     992                 :       3730 :         memset(paddrin, 0, *addrlen);
#     993         [ -  + ]:       3730 :         if (!GetInAddr(&paddrin->sin_addr))
#     994                 :          0 :             return false;
#     995                 :       3730 :         paddrin->sin_family = AF_INET;
#     996                 :       3730 :         paddrin->sin_port = htons(port);
#     997                 :       3730 :         return true;
#     998                 :       3730 :     }
#     999 [ +  - ][ #  # ]:          8 :     if (IsIPv6() || IsCJDNS()) {
#    1000         [ -  + ]:          8 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
#    1001                 :          0 :             return false;
#    1002                 :          8 :         *addrlen = sizeof(struct sockaddr_in6);
#    1003                 :          8 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
#    1004                 :          8 :         memset(paddrin6, 0, *addrlen);
#    1005         [ -  + ]:          8 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
#    1006                 :          0 :             return false;
#    1007                 :          8 :         paddrin6->sin6_scope_id = m_scope_id;
#    1008                 :          8 :         paddrin6->sin6_family = AF_INET6;
#    1009                 :          8 :         paddrin6->sin6_port = htons(port);
#    1010                 :          8 :         return true;
#    1011                 :          8 :     }
#    1012                 :          0 :     return false;
#    1013                 :          8 : }
#    1014                 :            : 
#    1015                 :            : /**
#    1016                 :            :  * @returns An identifier unique to this service's address and port number.
#    1017                 :            :  */
#    1018                 :            : std::vector<unsigned char> CService::GetKey() const
#    1019                 :     148094 : {
#    1020                 :     148094 :     auto key = GetAddrBytes();
#    1021                 :     148094 :     key.push_back(port / 0x100); // most significant byte of our port
#    1022                 :     148094 :     key.push_back(port & 0x0FF); // least significant byte of our port
#    1023                 :     148094 :     return key;
#    1024                 :     148094 : }
#    1025                 :            : 
#    1026                 :            : std::string CService::ToStringPort() const
#    1027                 :     321303 : {
#    1028                 :     321303 :     return strprintf("%u", port);
#    1029                 :     321303 : }
#    1030                 :            : 
#    1031                 :            : std::string CService::ToStringIPPort() const
#    1032                 :     321303 : {
#    1033 [ +  + ][ +  + ]:     321303 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
#         [ +  + ][ -  + ]
#    1034                 :     320467 :         return ToStringIP() + ":" + ToStringPort();
#    1035                 :     320467 :     } else {
#    1036                 :        836 :         return "[" + ToStringIP() + "]:" + ToStringPort();
#    1037                 :        836 :     }
#    1038                 :     321303 : }
#    1039                 :            : 
#    1040                 :            : std::string CService::ToString() const
#    1041                 :     320535 : {
#    1042                 :     320535 :     return ToStringIPPort();
#    1043                 :     320535 : }
#    1044                 :            : 
#    1045                 :            : CSubNet::CSubNet():
#    1046                 :            :     valid(false)
#    1047                 :       2354 : {
#    1048                 :       2354 :     memset(netmask, 0, sizeof(netmask));
#    1049                 :       2354 : }
#    1050                 :            : 
#    1051                 :            : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
#    1052                 :        872 : {
#    1053 [ +  + ][ +  + ]:        872 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
#    1054 [ +  + ][ +  + ]:        872 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
#    1055         [ +  + ]:        872 :     if (!valid) {
#    1056                 :          7 :         return;
#    1057                 :          7 :     }
#    1058                 :            : 
#    1059                 :        865 :     assert(mask <= sizeof(netmask) * 8);
#    1060                 :            : 
#    1061                 :          0 :     network = addr;
#    1062                 :            : 
#    1063                 :        865 :     uint8_t n = mask;
#    1064         [ +  + ]:       4565 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1065         [ +  + ]:       3700 :         const uint8_t bits = n < 8 ? n : 8;
#    1066                 :       3700 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
#    1067                 :       3700 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
#    1068                 :       3700 :         n -= bits;
#    1069                 :       3700 :     }
#    1070                 :        865 : }
#    1071                 :            : 
#    1072                 :            : /**
#    1073                 :            :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
#    1074                 :            :  *          the specified subnet mask is not a valid one, -1.
#    1075                 :            :  */
#    1076                 :            : static inline int NetmaskBits(uint8_t x)
#    1077                 :      14459 : {
#    1078                 :      14459 :     switch(x) {
#    1079         [ +  + ]:        216 :     case 0x00: return 0;
#    1080         [ +  + ]:         16 :     case 0x80: return 1;
#    1081         [ +  + ]:         16 :     case 0xc0: return 2;
#    1082         [ +  + ]:         22 :     case 0xe0: return 3;
#    1083         [ +  + ]:         16 :     case 0xf0: return 4;
#    1084         [ +  + ]:         16 :     case 0xf8: return 5;
#    1085         [ +  + ]:         22 :     case 0xfc: return 6;
#    1086         [ +  + ]:         18 :     case 0xfe: return 7;
#    1087         [ +  + ]:      14113 :     case 0xff: return 8;
#    1088         [ +  + ]:          4 :     default: return -1;
#    1089                 :      14459 :     }
#    1090                 :      14459 : }
#    1091                 :            : 
#    1092                 :            : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
#    1093                 :        100 : {
#    1094 [ +  + ][ +  + ]:        100 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
#                 [ +  + ]
#    1095         [ +  + ]:        100 :     if (!valid) {
#    1096                 :          6 :         return;
#    1097                 :          6 :     }
#    1098                 :            :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
#    1099                 :         94 :     bool zeros_found = false;
#    1100         [ +  + ]:        476 :     for (auto b : mask.m_addr) {
#    1101                 :        476 :         const int num_bits = NetmaskBits(b);
#    1102 [ +  + ][ +  + ]:        476 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
#                 [ +  + ]
#    1103                 :          8 :             valid = false;
#    1104                 :          8 :             return;
#    1105                 :          8 :         }
#    1106         [ +  + ]:        468 :         if (num_bits < 8) {
#    1107                 :        276 :             zeros_found = true;
#    1108                 :        276 :         }
#    1109                 :        468 :     }
#    1110                 :            : 
#    1111                 :         86 :     assert(mask.m_addr.size() <= sizeof(netmask));
#    1112                 :            : 
#    1113                 :          0 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
#    1114                 :            : 
#    1115                 :         86 :     network = addr;
#    1116                 :            : 
#    1117                 :            :     // Normalize network according to netmask
#    1118         [ +  + ]:        526 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
#    1119                 :        440 :         network.m_addr[x] &= netmask[x];
#    1120                 :        440 :     }
#    1121                 :         86 : }
#    1122                 :            : 
#    1123                 :            : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
#    1124                 :        955 : {
#    1125         [ -  + ]:        955 :     switch (addr.m_net) {
#    1126         [ +  + ]:        140 :     case NET_IPV4:
#    1127         [ +  + ]:        945 :     case NET_IPV6:
#    1128                 :        945 :         valid = true;
#    1129                 :        945 :         assert(addr.m_addr.size() <= sizeof(netmask));
#    1130                 :          0 :         memset(netmask, 0xFF, addr.m_addr.size());
#    1131                 :        945 :         break;
#    1132         [ +  + ]:         10 :     case NET_ONION:
#    1133         [ -  + ]:         10 :     case NET_I2P:
#    1134         [ -  + ]:         10 :     case NET_CJDNS:
#    1135                 :         10 :         valid = true;
#    1136                 :         10 :         break;
#    1137         [ -  + ]:          0 :     case NET_INTERNAL:
#    1138         [ -  + ]:          0 :     case NET_UNROUTABLE:
#    1139         [ -  + ]:          0 :     case NET_MAX:
#    1140                 :          0 :         return;
#    1141                 :        955 :     }
#    1142                 :            : 
#    1143                 :        955 :     network = addr;
#    1144                 :        955 : }
#    1145                 :            : 
#    1146                 :            : /**
#    1147                 :            :  * @returns True if this subnet is valid, the specified address is valid, and
#    1148                 :            :  *          the specified address belongs in this subnet.
#    1149                 :            :  */
#    1150                 :            : bool CSubNet::Match(const CNetAddr &addr) const
#    1151                 :     137847 : {
#    1152 [ +  + ][ +  + ]:     137847 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
#                 [ +  + ]
#    1153                 :         23 :         return false;
#    1154                 :            : 
#    1155         [ -  + ]:     137824 :     switch (network.m_net) {
#    1156         [ +  + ]:     137806 :     case NET_IPV4:
#    1157         [ +  + ]:     137818 :     case NET_IPV6:
#    1158                 :     137818 :         break;
#    1159         [ +  + ]:          6 :     case NET_ONION:
#    1160         [ -  + ]:          6 :     case NET_I2P:
#    1161         [ -  + ]:          6 :     case NET_CJDNS:
#    1162         [ -  + ]:          6 :     case NET_INTERNAL:
#    1163                 :          6 :         return addr == network;
#    1164         [ -  + ]:          0 :     case NET_UNROUTABLE:
#    1165         [ -  + ]:          0 :     case NET_MAX:
#    1166                 :          0 :         return false;
#    1167                 :     137824 :     }
#    1168                 :            : 
#    1169                 :     137818 :     assert(network.m_addr.size() == addr.m_addr.size());
#    1170         [ +  + ]:     689192 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
#    1171         [ +  + ]:     551390 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
#    1172                 :         16 :             return false;
#    1173                 :         16 :         }
#    1174                 :     551390 :     }
#    1175                 :     137802 :     return true;
#    1176                 :     137818 : }
#    1177                 :            : 
#    1178                 :            : std::string CSubNet::ToString() const
#    1179                 :       1735 : {
#    1180                 :       1735 :     std::string suffix;
#    1181                 :            : 
#    1182         [ -  + ]:       1735 :     switch (network.m_net) {
#    1183         [ +  + ]:        913 :     case NET_IPV4:
#    1184         [ +  + ]:       1728 :     case NET_IPV6: {
#    1185                 :       1728 :         assert(network.m_addr.size() <= sizeof(netmask));
#    1186                 :            : 
#    1187                 :          0 :         uint8_t cidr = 0;
#    1188                 :            : 
#    1189         [ +  + ]:      15711 :         for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1190         [ +  + ]:      14866 :             if (netmask[i] == 0x00) {
#    1191                 :        883 :                 break;
#    1192                 :        883 :             }
#    1193                 :      13983 :             cidr += NetmaskBits(netmask[i]);
#    1194                 :      13983 :         }
#    1195                 :            : 
#    1196                 :       1728 :         suffix = strprintf("/%u", cidr);
#    1197                 :       1728 :         break;
#    1198                 :        913 :     }
#    1199         [ +  + ]:          7 :     case NET_ONION:
#    1200         [ -  + ]:          7 :     case NET_I2P:
#    1201         [ -  + ]:          7 :     case NET_CJDNS:
#    1202         [ -  + ]:          7 :     case NET_INTERNAL:
#    1203         [ -  + ]:          7 :     case NET_UNROUTABLE:
#    1204         [ -  + ]:          7 :     case NET_MAX:
#    1205                 :          7 :         break;
#    1206                 :       1735 :     }
#    1207                 :            : 
#    1208                 :       1735 :     return network.ToString() + suffix;
#    1209                 :       1735 : }
#    1210                 :            : 
#    1211                 :            : bool CSubNet::IsValid() const
#    1212                 :        545 : {
#    1213                 :        545 :     return valid;
#    1214                 :        545 : }
#    1215                 :            : 
#    1216                 :            : bool CSubNet::SanityCheck() const
#    1217                 :          0 : {
#    1218         [ #  # ]:          0 :     switch (network.m_net) {
#    1219         [ #  # ]:          0 :     case NET_IPV4:
#    1220         [ #  # ]:          0 :     case NET_IPV6:
#    1221                 :          0 :         break;
#    1222         [ #  # ]:          0 :     case NET_ONION:
#    1223         [ #  # ]:          0 :     case NET_I2P:
#    1224         [ #  # ]:          0 :     case NET_CJDNS:
#    1225                 :          0 :         return true;
#    1226         [ #  # ]:          0 :     case NET_INTERNAL:
#    1227         [ #  # ]:          0 :     case NET_UNROUTABLE:
#    1228         [ #  # ]:          0 :     case NET_MAX:
#    1229                 :          0 :         return false;
#    1230                 :          0 :     }
#    1231                 :            : 
#    1232         [ #  # ]:          0 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
#    1233         [ #  # ]:          0 :         if (network.m_addr[x] & ~netmask[x]) return false;
#    1234                 :          0 :     }
#    1235                 :            : 
#    1236                 :          0 :     return true;
#    1237                 :          0 : }
#    1238                 :            : 
#    1239                 :            : bool operator==(const CSubNet& a, const CSubNet& b)
#    1240                 :          4 : {
#    1241 [ +  - ][ +  + ]:          4 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
#                 [ +  - ]
#    1242                 :          4 : }
#    1243                 :            : 
#    1244                 :            : bool operator<(const CSubNet& a, const CSubNet& b)
#    1245                 :        108 : {
#    1246 [ +  + ][ +  + ]:        108 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
#                 [ +  + ]
#    1247                 :        108 : }

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a