LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 93.0 % 529 492
Test Date: 2026-12-05 06:37:21 Functions: 100.0 % 73 73
Branches: 73.4 % 693 509

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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/check.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 <string_view>
      23                 :             : #include <tuple>
      24                 :             : 
      25                 :             : using util::ContainsNUL;
      26                 :             : using util::HasPrefix;
      27                 :             : 
      28                 :      100742 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
      29                 :             : {
      30   [ +  +  +  +  :      100742 :     switch (m_net) {
                -  -  + ]
      31                 :             :     case NET_IPV4:
      32                 :             :         return BIP155Network::IPV4;
      33                 :         158 :     case NET_IPV6:
      34                 :         158 :         return BIP155Network::IPV6;
      35                 :         170 :     case NET_ONION:
      36                 :         170 :         return BIP155Network::TORV3;
      37                 :         117 :     case NET_I2P:
      38                 :         117 :         return BIP155Network::I2P;
      39                 :         128 :     case NET_CJDNS:
      40                 :         128 :         return BIP155Network::CJDNS;
      41                 :           0 :     case NET_INTERNAL:   // should have been handled before calling this function
      42                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
      43                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
      44                 :           0 :         AssertUnreachable();
      45                 :             :     } // no default case, so the compiler can warn about missing cases
      46                 :             : 
      47                 :           0 :     AssertUnreachable();
      48                 :             : }
      49                 :             : 
      50                 :       42200 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
      51                 :             : {
      52   [ +  +  +  +  :       42200 :     switch (possible_bip155_net) {
                   +  + ]
      53                 :       41804 :     case BIP155Network::IPV4:
      54         [ +  + ]:       41804 :         if (address_size == ADDR_IPV4_SIZE) {
      55                 :       41803 :             m_net = NET_IPV4;
      56                 :       41803 :             return true;
      57                 :             :         }
      58                 :           1 :         throw std::ios_base::failure(
      59         [ +  - ]:           1 :             strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
      60         [ +  - ]:           3 :                       ADDR_IPV4_SIZE));
      61                 :          61 :     case BIP155Network::IPV6:
      62         [ +  + ]:          61 :         if (address_size == ADDR_IPV6_SIZE) {
      63                 :          60 :             m_net = NET_IPV6;
      64                 :          60 :             return true;
      65                 :             :         }
      66                 :           1 :         throw std::ios_base::failure(
      67         [ +  - ]:           1 :             strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
      68         [ +  - ]:           3 :                       ADDR_IPV6_SIZE));
      69                 :         148 :     case BIP155Network::TORV3:
      70         [ +  + ]:         148 :         if (address_size == ADDR_TORV3_SIZE) {
      71                 :         147 :             m_net = NET_ONION;
      72                 :         147 :             return true;
      73                 :             :         }
      74                 :           1 :         throw std::ios_base::failure(
      75         [ +  - ]:           1 :             strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
      76         [ +  - ]:           3 :                       ADDR_TORV3_SIZE));
      77                 :         140 :     case BIP155Network::I2P:
      78         [ +  + ]:         140 :         if (address_size == ADDR_I2P_SIZE) {
      79                 :         139 :             m_net = NET_I2P;
      80                 :         139 :             return true;
      81                 :             :         }
      82                 :           1 :         throw std::ios_base::failure(
      83         [ +  - ]:           1 :             strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
      84         [ +  - ]:           3 :                       ADDR_I2P_SIZE));
      85                 :          43 :     case BIP155Network::CJDNS:
      86         [ +  + ]:          43 :         if (address_size == ADDR_CJDNS_SIZE) {
      87                 :          42 :             m_net = NET_CJDNS;
      88                 :          42 :             return true;
      89                 :             :         }
      90                 :           1 :         throw std::ios_base::failure(
      91         [ +  - ]:           1 :             strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
      92         [ +  - ]:           3 :                       ADDR_CJDNS_SIZE));
      93                 :             :     }
      94                 :             : 
      95                 :             :     // Don't throw on addresses with unknown network ids (maybe from the future).
      96                 :             :     // Instead silently drop them and have the unserialization code consume
      97                 :             :     // subsequent ones which may be known to us.
      98                 :             :     return false;
      99                 :             : }
     100                 :             : 
     101                 :             : /**
     102                 :             :  * Construct an unspecified IPv6 network address (::/128).
     103                 :             :  *
     104                 :             :  * @note This address is considered invalid by CNetAddr::IsValid()
     105                 :             :  */
     106                 :      288675 : CNetAddr::CNetAddr() = default;
     107                 :             : 
     108                 :           3 : void CNetAddr::SetIP(const CNetAddr& ipIn)
     109                 :             : {
     110                 :             :     // Size check.
     111   [ +  +  -  -  :           3 :     switch (ipIn.m_net) {
             -  -  -  - ]
     112                 :           2 :     case NET_IPV4:
     113   [ -  +  -  + ]:           2 :         assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
     114                 :             :         break;
     115                 :           1 :     case NET_IPV6:
     116   [ -  +  -  + ]:           1 :         assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
     117                 :             :         break;
     118                 :           0 :     case NET_ONION:
     119   [ #  #  #  # ]:           0 :         assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
     120                 :             :         break;
     121                 :           0 :     case NET_I2P:
     122   [ #  #  #  # ]:           0 :         assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
     123                 :             :         break;
     124                 :           0 :     case NET_CJDNS:
     125   [ #  #  #  # ]:           0 :         assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
     126                 :             :         break;
     127                 :           0 :     case NET_INTERNAL:
     128   [ #  #  #  # ]:           0 :         assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
     129                 :             :         break;
     130                 :           0 :     case NET_UNROUTABLE:
     131                 :           0 :     case NET_MAX:
     132                 :           0 :         AssertUnreachable();
     133                 :             :     } // no default case, so the compiler can warn about missing cases
     134                 :             : 
     135                 :           3 :     m_net = ipIn.m_net;
     136                 :           3 :     m_addr = ipIn.m_addr;
     137                 :           3 : }
     138                 :             : 
     139                 :       10946 : void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
     140                 :             : {
     141         [ -  + ]:       10946 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
     142                 :             : 
     143                 :       10946 :     size_t skip{0};
     144                 :             : 
     145         [ +  + ]:       10946 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
     146                 :             :         // IPv4-in-IPv6
     147                 :        6803 :         m_net = NET_IPV4;
     148                 :        6803 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
     149         [ -  + ]:        4143 :     } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
     150                 :             :         // TORv2-in-IPv6 (unsupported). Unserialize as !IsValid(), thus ignoring them.
     151                 :             :         // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
     152                 :             :         // will not be gossiped, but continue reading next addresses from the stream.
     153                 :           0 :         m_net = NET_IPV6;
     154                 :           0 :         m_addr.assign(ADDR_IPV6_SIZE, 0x0);
     155                 :           0 :         return;
     156         [ +  + ]:        4143 :     } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
     157                 :             :         // Internal-in-IPv6
     158                 :           1 :         m_net = NET_INTERNAL;
     159                 :           1 :         skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
     160                 :             :     } else {
     161                 :             :         // IPv6
     162                 :        4142 :         m_net = NET_IPV6;
     163                 :             :     }
     164                 :             : 
     165                 :       10946 :     m_addr.assign(ipv6.begin() + skip, ipv6.end());
     166                 :             : }
     167                 :             : 
     168                 :             : /**
     169                 :             :  * Create an "internal" address that represents a name or FQDN. AddrMan uses
     170                 :             :  * these fake addresses to keep track of which DNS seeds were used.
     171                 :             :  * @returns Whether or not the operation was successful.
     172                 :             :  * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
     173                 :             :  */
     174                 :          10 : bool CNetAddr::SetInternal(const std::string &name)
     175                 :             : {
     176         [ +  - ]:          10 :     if (name.empty()) {
     177                 :             :         return false;
     178                 :             :     }
     179                 :          10 :     m_net = NET_INTERNAL;
     180                 :          10 :     unsigned char hash[32] = {};
     181         [ -  + ]:          10 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
     182                 :          10 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
     183                 :          10 :     return true;
     184                 :             : }
     185                 :             : 
     186                 :             : namespace torv3 {
     187                 :             : // https://gitlab.torproject.org/tpo/core/torspec/-/tree/main/spec/rend-spec
     188                 :             : static constexpr size_t CHECKSUM_LEN = 2;
     189                 :             : static const unsigned char VERSION[] = {3};
     190                 :             : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
     191                 :             : 
     192                 :         392 : static void Checksum(std::span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
     193                 :             : {
     194                 :             :     // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
     195                 :         392 :     static const unsigned char prefix[] = ".onion checksum";
     196                 :         392 :     static constexpr size_t prefix_len = 15;
     197                 :             : 
     198                 :         392 :     SHA3_256 hasher;
     199                 :             : 
     200                 :         392 :     hasher.Write(std::span{prefix}.first(prefix_len));
     201                 :         392 :     hasher.Write(addr_pubkey);
     202                 :         392 :     hasher.Write(VERSION);
     203                 :             : 
     204                 :         392 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
     205                 :             : 
     206                 :         392 :     hasher.Finalize(checksum_full);
     207                 :             : 
     208                 :         392 :     memcpy(checksum, checksum_full, sizeof(checksum));
     209                 :         392 : }
     210                 :             : 
     211                 :             : }; // namespace torv3
     212                 :             : 
     213                 :       57358 : bool CNetAddr::SetSpecial(std::string_view addr)
     214                 :             : {
     215         [ +  + ]:      114716 :     if (ContainsNUL(addr)) {
     216                 :             :         return false;
     217                 :             :     }
     218                 :             : 
     219         [ +  + ]:       57356 :     if (SetTor(addr)) {
     220                 :             :         return true;
     221                 :             :     }
     222                 :             : 
     223         [ +  + ]:       57262 :     if (SetI2P(addr)) {
     224                 :          42 :         return true;
     225                 :             :     }
     226                 :             : 
     227                 :             :     return false;
     228                 :             : }
     229                 :             : 
     230                 :       57356 : bool CNetAddr::SetTor(std::string_view addr)
     231                 :             : {
     232                 :       57356 :     static constexpr std::string_view suffix{".onion"};
     233   [ +  +  -  +  :      112162 :     if (addr.size() < suffix.size() || ToLower(addr.substr(addr.size() - suffix.size())) != suffix) return false;
             +  +  +  + ]
     234                 :         103 :     addr.remove_suffix(suffix.size());
     235                 :         103 :     auto input = DecodeBase32(addr);
     236                 :             : 
     237         [ +  + ]:         103 :     if (!input) {
     238                 :             :         return false;
     239                 :             :     }
     240                 :             : 
     241   [ -  +  +  + ]:         102 :     if (input->size() == torv3::TOTAL_LEN) {
     242         [ +  + ]:          98 :         std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
     243                 :          98 :         std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
     244                 :          98 :         std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
     245                 :             : 
     246         [ +  + ]:          98 :         if (!std::ranges::equal(input_version, torv3::VERSION)) {
     247                 :             :             return false;
     248                 :             :         }
     249                 :             : 
     250                 :          97 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
     251         [ +  - ]:          97 :         torv3::Checksum(input_pubkey, calculated_checksum);
     252                 :             : 
     253         [ +  + ]:          97 :         if (!std::ranges::equal(input_checksum, calculated_checksum)) {
     254                 :             :             return false;
     255                 :             :         }
     256                 :             : 
     257                 :          94 :         m_net = NET_ONION;
     258                 :          94 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
     259                 :          94 :         return true;
     260                 :             :     }
     261                 :             : 
     262                 :             :     return false;
     263                 :         103 : }
     264                 :             : 
     265                 :       57262 : bool CNetAddr::SetI2P(std::string_view addr)
     266                 :             : {
     267                 :             :     // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
     268                 :       57262 :     static constexpr size_t b32_len{52};
     269                 :       57262 :     static const char* suffix{".b32.i2p"};
     270                 :       57262 :     static constexpr size_t suffix_len{8};
     271                 :             : 
     272   [ +  +  +  -  :       57305 :     if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
                   +  + ]
     273                 :             :         return false;
     274                 :             :     }
     275                 :             : 
     276                 :             :     // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
     277                 :             :     // can decode it.
     278                 :          43 :     const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
     279                 :             : 
     280   [ -  +  +  - ]:          43 :     auto address_bytes = DecodeBase32(b32_padded);
     281                 :             : 
     282   [ +  +  -  +  :          43 :     if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
                   +  - ]
     283                 :             :         return false;
     284                 :             :     }
     285                 :             : 
     286                 :          42 :     m_net = NET_I2P;
     287                 :          42 :     m_addr.assign(address_bytes->begin(), address_bytes->end());
     288                 :             : 
     289                 :          42 :     return true;
     290                 :          43 : }
     291                 :             : 
     292                 :       61923 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
     293                 :             : {
     294                 :       61923 :     m_net = NET_IPV4;
     295                 :       61923 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
     296                 :       61923 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
     297                 :       61923 : }
     298                 :             : 
     299                 :        3244 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
     300                 :             : {
     301         [ +  - ]:        3244 :     SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
     302                 :        3244 :     m_scope_id = scope;
     303                 :        3244 : }
     304                 :             : 
     305                 :        3677 : bool CNetAddr::IsBindAny() const
     306                 :             : {
     307   [ +  +  +  + ]:        3677 :     if (!IsIPv4() && !IsIPv6()) {
     308                 :             :         return false;
     309                 :             :     }
     310         [ +  - ]:        7348 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
     311                 :             : }
     312                 :             : 
     313                 :     1057849 : bool CNetAddr::IsRFC1918() const
     314                 :             : {
     315         [ +  + ]:     1057849 :     return IsIPv4() && (
     316   [ +  -  +  +  :     2099564 :         m_addr[0] == 10 ||
                   +  + ]
     317   [ +  +  +  -  :     1050945 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
             +  +  +  + ]
     318   [ +  +  +  -  :     1049340 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
             +  +  +  - ]
     319                 :             : }
     320                 :             : 
     321                 :     1057062 : bool CNetAddr::IsRFC2544() const
     322                 :             : {
     323   [ +  +  +  -  :     2106388 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
          +  +  +  -  +  
                +  +  + ]
     324                 :             : }
     325                 :             : 
     326                 :     1057061 : bool CNetAddr::IsRFC3927() const
     327                 :             : {
     328   [ +  +  +  + ]:     1057061 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
     329                 :             : }
     330                 :             : 
     331                 :     1057057 : bool CNetAddr::IsRFC6598() const
     332                 :             : {
     333   [ +  +  +  -  :     2106539 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
          +  +  +  -  -  
                +  -  - ]
     334                 :             : }
     335                 :             : 
     336                 :     1057057 : bool CNetAddr::IsRFC5737() const
     337                 :             : {
     338   [ +  +  +  - ]:     1057057 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
     339         [ +  - ]:     1048990 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
     340         [ -  + ]:     1048990 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
     341                 :             : }
     342                 :             : 
     343                 :     1141708 : bool CNetAddr::IsRFC3849() const
     344                 :             : {
     345   [ +  +  +  + ]:     1141708 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
     346                 :             : }
     347                 :             : 
     348                 :     1141710 : bool CNetAddr::IsRFC9637() const
     349                 :             : {
     350   [ +  +  +  + ]:     1141710 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x3F, 0xFF}) &&
     351   [ +  -  +  + ]:           8 :            (m_addr[2] & 0xF0) == 0x00;
     352                 :             : }
     353                 :             : 
     354                 :        2679 : bool CNetAddr::IsRFC3964() const
     355                 :             : {
     356   [ +  +  +  + ]:        2679 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
     357                 :             : }
     358                 :             : 
     359                 :        2678 : bool CNetAddr::IsRFC6052() const
     360                 :             : {
     361         [ +  + ]:        2678 :     return IsIPv6() &&
     362         [ +  + ]:        1560 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
     363                 :        2678 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
     364                 :             : }
     365                 :             : 
     366                 :        2723 : bool CNetAddr::IsRFC4380() const
     367                 :             : {
     368   [ +  +  +  + ]:        2723 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
     369                 :             : }
     370                 :             : 
     371                 :     1057058 : bool CNetAddr::IsRFC4862() const
     372                 :             : {
     373   [ +  +  +  + ]:     1057058 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
     374                 :     1057058 :                                                                 0x00, 0x00, 0x00, 0x00});
     375                 :             : }
     376                 :             : 
     377                 :     1057058 : bool CNetAddr::IsRFC4193() const
     378                 :             : {
     379   [ +  +  +  -  :     1061853 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
                   +  + ]
     380                 :             : }
     381                 :             : 
     382                 :        2679 : bool CNetAddr::IsRFC6145() const
     383                 :             : {
     384         [ +  + ]:        2679 :     return IsIPv6() &&
     385         [ +  + ]:        1561 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     386                 :        2679 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
     387                 :             : }
     388                 :             : 
     389                 :     1057058 : bool CNetAddr::IsRFC4843() const
     390                 :             : {
     391   [ +  +  +  + ]:     1057058 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
     392   [ +  -  +  + ]:          86 :            (m_addr[3] & 0xF0) == 0x10;
     393                 :             : }
     394                 :             : 
     395                 :     1057058 : bool CNetAddr::IsRFC7343() const
     396                 :             : {
     397   [ +  +  +  + ]:     1057058 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
     398   [ +  -  +  + ]:          86 :            (m_addr[3] & 0xF0) == 0x20;
     399                 :             : }
     400                 :             : 
     401                 :         360 : bool CNetAddr::IsHeNet() const
     402                 :             : {
     403   [ +  -  +  + ]:         360 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
     404                 :             : }
     405                 :             : 
     406                 :     1140835 : bool CNetAddr::IsLocal() const
     407                 :             : {
     408                 :             :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
     409   [ +  +  +  -  :     2272817 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
             +  +  +  + ]
     410                 :             :         return true;
     411                 :             :     }
     412                 :             : 
     413                 :             :     // IPv6 loopback (::1/128)
     414                 :     1083182 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
     415   [ +  +  +  -  :     1088498 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
                   +  + ]
     416                 :           2 :         return true;
     417                 :             :     }
     418                 :             : 
     419                 :             :     return false;
     420                 :             : }
     421                 :             : 
     422                 :             : /**
     423                 :             :  * @returns Whether or not this network address is a valid address that @a could
     424                 :             :  *          be used to refer to an actual host.
     425                 :             :  *
     426                 :             :  * @note A valid address may or may not be publicly routable on the global
     427                 :             :  *       internet. As in, the set of valid addresses is a superset of the set of
     428                 :             :  *       publicly routable addresses.
     429                 :             :  *
     430                 :             :  * @see CNetAddr::IsRoutable()
     431                 :             :  */
     432                 :     1173688 : bool CNetAddr::IsValid() const
     433                 :             : {
     434                 :             :     // unspecified IPv6 address (::/128)
     435                 :     1173688 :     unsigned char ipNone6[16] = {};
     436   [ +  +  +  +  :     1210646 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
                   +  + ]
     437                 :             :         return false;
     438                 :             :     }
     439                 :             : 
     440   [ +  +  +  + ]:     1142644 :     if (IsCJDNS() && !HasCJDNSPrefix()) {
     441                 :             :         return false;
     442                 :             :     }
     443                 :             : 
     444                 :             :     // documentation IPv6 address
     445   [ +  -  +  + ]:     1141707 :     if (IsRFC3849() || IsRFC9637())
     446                 :           1 :         return false;
     447                 :             : 
     448         [ +  + ]:     1141706 :     if (IsInternal())
     449                 :             :         return false;
     450                 :             : 
     451         [ +  + ]:     1141705 :     if (IsIPv4()) {
     452   [ +  -  +  + ]:     2266550 :         const uint32_t addr = ReadBE32(m_addr.data());
     453         [ +  + ]:     1133275 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
     454                 :         924 :             return false;
     455                 :             :         }
     456                 :             :     }
     457                 :             : 
     458                 :             :     return true;
     459                 :             : }
     460                 :             : 
     461                 :             : /**
     462                 :             :  * @returns Whether or not this network address is publicly routable on the
     463                 :             :  *          global internet.
     464                 :             :  *
     465                 :             :  * @note A routable address is always valid. As in, the set of routable addresses
     466                 :             :  *       is a subset of the set of valid addresses.
     467                 :             :  *
     468                 :             :  * @see CNetAddr::IsValid()
     469                 :             :  */
     470                 :     1060673 : bool CNetAddr::IsRoutable() const
     471                 :             : {
     472   [ +  +  +  +  :     1060673 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
          +  -  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  +  -  
                      + ]
     473                 :             : }
     474                 :             : 
     475                 :             : /**
     476                 :             :  * @returns Whether or not this is a dummy address that represents a name.
     477                 :             :  *
     478                 :             :  * @see CNetAddr::SetInternal(const std::string &)
     479                 :             :  */
     480                 :     2905007 : bool CNetAddr::IsInternal() const
     481                 :             : {
     482                 :     2905007 :    return m_net == NET_INTERNAL;
     483                 :             : }
     484                 :             : 
     485                 :      261475 : bool CNetAddr::IsAddrV1Compatible() const
     486                 :             : {
     487   [ +  -  -  + ]:      261475 :     switch (m_net) {
     488                 :             :     case NET_IPV4:
     489                 :             :     case NET_IPV6:
     490                 :             :     case NET_INTERNAL:
     491                 :             :         return true;
     492                 :         571 :     case NET_ONION:
     493                 :         571 :     case NET_I2P:
     494                 :         571 :     case NET_CJDNS:
     495                 :         571 :         return false;
     496                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     497                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
     498                 :           0 :         AssertUnreachable();
     499                 :             :     } // no default case, so the compiler can warn about missing cases
     500                 :             : 
     501                 :           0 :     AssertUnreachable();
     502                 :             : }
     503                 :             : 
     504                 :      140002 : enum Network CNetAddr::GetNetwork() const
     505                 :             : {
     506         [ +  + ]:      140002 :     if (IsInternal())
     507                 :             :         return NET_INTERNAL;
     508                 :             : 
     509         [ +  + ]:      140001 :     if (!IsRoutable())
     510                 :             :         return NET_UNROUTABLE;
     511                 :             : 
     512                 :      138482 :     return m_net;
     513                 :             : }
     514                 :             : 
     515                 :      371101 : static std::string IPv4ToString(std::span<const uint8_t> a)
     516                 :             : {
     517                 :      371101 :     return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
     518                 :             : }
     519                 :             : 
     520                 :             : // Return an IPv6 address text representation with zero compression as described in RFC 5952
     521                 :             : // ("A Recommendation for IPv6 Address Text Representation").
     522                 :        3521 : static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
     523                 :             : {
     524         [ -  + ]:        3521 :     assert(a.size() == ADDR_IPV6_SIZE);
     525                 :        3521 :     const std::array groups{
     526                 :        3521 :         ReadBE16(&a[0]),
     527                 :        3521 :         ReadBE16(&a[2]),
     528                 :        3521 :         ReadBE16(&a[4]),
     529                 :        3521 :         ReadBE16(&a[6]),
     530                 :        3521 :         ReadBE16(&a[8]),
     531                 :        3521 :         ReadBE16(&a[10]),
     532                 :        3521 :         ReadBE16(&a[12]),
     533                 :        3521 :         ReadBE16(&a[14]),
     534                 :        3521 :     };
     535                 :             : 
     536                 :             :     // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
     537                 :             :     // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
     538                 :        3521 :     struct ZeroSpan {
     539                 :             :         size_t start_index{0};
     540                 :             :         size_t len{0};
     541                 :             :     };
     542                 :             : 
     543                 :             :     // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
     544                 :             :     // zero sequences of equal length are found.
     545                 :        3521 :     ZeroSpan longest, current;
     546         [ +  + ]:       31689 :     for (size_t i{0}; i < groups.size(); ++i) {
     547         [ +  + ]:       28168 :         if (groups[i] != 0) {
     548                 :        5229 :             current = {i + 1, 0};
     549                 :        5229 :             continue;
     550                 :             :         }
     551                 :       22939 :         current.len += 1;
     552         [ +  + ]:       22939 :         if (current.len > longest.len) {
     553                 :       22904 :             longest = current;
     554                 :             :         }
     555                 :             :     }
     556                 :             : 
     557         [ +  - ]:        3521 :     std::string r;
     558         [ +  - ]:        3521 :     r.reserve(39);
     559         [ +  + ]:       31689 :     for (size_t i{0}; i < groups.size(); ++i) {
     560                 :             :         // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
     561   [ +  +  +  +  :       28168 :         if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
                   +  + ]
     562         [ +  + ]:       22898 :             if (i == longest.start_index) {
     563         [ +  - ]:        3436 :                 r += "::";
     564                 :             :             }
     565                 :       22898 :             continue;
     566                 :             :         }
     567   [ +  +  +  +  :       17011 :         r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
                   +  - ]
     568                 :             :     }
     569                 :             : 
     570         [ +  + ]:        3521 :     if (scope_id != 0) {
     571         [ +  - ]:           2 :         r += strprintf("%%%u", scope_id);
     572                 :             :     }
     573                 :             : 
     574                 :        3521 :     return r;
     575                 :           0 : }
     576                 :             : 
     577                 :         295 : std::string OnionToString(std::span<const uint8_t> addr)
     578                 :             : {
     579                 :         295 :     uint8_t checksum[torv3::CHECKSUM_LEN];
     580                 :         295 :     torv3::Checksum(addr, checksum);
     581                 :             :     // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
     582                 :         295 :     prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
     583                 :         295 :     address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
     584                 :         295 :     address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
     585   [ -  +  +  - ]:        1180 :     return EncodeBase32(address) + ".onion";
     586                 :         295 : }
     587                 :             : 
     588                 :      375005 : std::string CNetAddr::ToStringAddr() const
     589                 :             : {
     590   [ +  +  +  +  :      375005 :     switch (m_net) {
             +  +  -  - ]
     591                 :      371101 :     case NET_IPV4:
     592         [ +  - ]:      742202 :         return IPv4ToString(m_addr);
     593                 :        3459 :     case NET_IPV6:
     594         [ +  - ]:        6918 :         return IPv6ToString(m_addr, m_scope_id);
     595                 :         292 :     case NET_ONION:
     596         [ -  + ]:         584 :         return OnionToString(m_addr);
     597                 :          89 :     case NET_I2P:
     598   [ -  +  +  - ]:         267 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
     599                 :          62 :     case NET_CJDNS:
     600         [ +  + ]:         124 :         return IPv6ToString(m_addr, 0);
     601                 :           2 :     case NET_INTERNAL:
     602   [ +  +  +  - ]:           6 :         return EncodeBase32(m_addr) + ".internal";
     603                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     604                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
     605                 :           0 :         AssertUnreachable();
     606                 :             :     } // no default case, so the compiler can warn about missing cases
     607                 :             : 
     608                 :           0 :     AssertUnreachable();
     609                 :             : }
     610                 :             : 
     611                 :      220145 : bool operator==(const CNetAddr& a, const CNetAddr& b)
     612                 :             : {
     613   [ +  +  +  + ]:      220145 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
     614                 :             : }
     615                 :             : 
     616                 :       10838 : bool operator<(const CNetAddr& a, const CNetAddr& b)
     617                 :             : {
     618                 :       10838 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
     619                 :             : }
     620                 :             : 
     621                 :             : /**
     622                 :             :  * Try to get our IPv4 address.
     623                 :             :  *
     624                 :             :  * @param[out] pipv4Addr The in_addr struct to which to copy.
     625                 :             :  *
     626                 :             :  * @returns Whether or not the operation was successful, in particular, whether
     627                 :             :  *          or not our address was an IPv4 address.
     628                 :             :  *
     629                 :             :  * @see CNetAddr::IsIPv4()
     630                 :             :  */
     631                 :        4261 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
     632                 :             : {
     633         [ +  - ]:        4261 :     if (!IsIPv4())
     634                 :             :         return false;
     635   [ -  +  -  + ]:        4261 :     assert(sizeof(*pipv4Addr) == m_addr.size());
     636         [ +  - ]:        8522 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
     637                 :        4261 :     return true;
     638                 :             : }
     639                 :             : 
     640                 :             : /**
     641                 :             :  * Try to get our IPv6 (or CJDNS) address.
     642                 :             :  *
     643                 :             :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
     644                 :             :  *
     645                 :             :  * @returns Whether or not the operation was successful, in particular, whether
     646                 :             :  *          or not our address was an IPv6 address.
     647                 :             :  *
     648                 :             :  * @see CNetAddr::IsIPv6()
     649                 :             :  */
     650                 :        1282 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
     651                 :             : {
     652   [ -  +  -  - ]:        1282 :     if (!IsIPv6() && !IsCJDNS()) {
     653                 :             :         return false;
     654                 :             :     }
     655   [ -  +  -  + ]:        1282 :     assert(sizeof(*pipv6Addr) == m_addr.size());
     656         [ +  - ]:        2564 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
     657                 :        1282 :     return true;
     658                 :             : }
     659                 :             : 
     660                 :      416763 : bool CNetAddr::HasLinkedIPv4() const
     661                 :             : {
     662   [ +  -  +  +  :      416763 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
          +  +  +  +  +  
                +  +  + ]
     663                 :             : }
     664                 :             : 
     665                 :       84014 : uint32_t CNetAddr::GetLinkedIPv4() const
     666                 :             : {
     667         [ +  + ]:       84014 :     if (IsIPv4()) {
     668         [ +  - ]:      168020 :         return ReadBE32(m_addr.data());
     669   [ +  +  +  + ]:           4 :     } else if (IsRFC6052() || IsRFC6145()) {
     670                 :             :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
     671         [ +  - ]:           4 :         return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
     672         [ +  + ]:           2 :     } else if (IsRFC3964()) {
     673                 :             :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
     674         [ +  - ]:           2 :         return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
     675         [ +  - ]:           1 :     } else if (IsRFC4380()) {
     676                 :             :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
     677         [ +  - ]:           2 :         return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
     678                 :             :     }
     679                 :           0 :     AssertUnreachable();
     680                 :             : }
     681                 :             : 
     682                 :      380890 : Network CNetAddr::GetNetClass() const
     683                 :             : {
     684                 :             :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
     685                 :             : 
     686                 :             :     // Check for "internal" first because such addresses are also !IsRoutable()
     687                 :             :     // and we don't want to return NET_UNROUTABLE in that case.
     688         [ +  + ]:      380890 :     if (IsInternal()) {
     689                 :             :         return NET_INTERNAL;
     690                 :             :     }
     691         [ +  + ]:      380888 :     if (!IsRoutable()) {
     692                 :             :         return NET_UNROUTABLE;
     693                 :             :     }
     694         [ +  + ]:      332110 :     if (HasLinkedIPv4()) {
     695                 :             :         return NET_IPV4;
     696                 :             :     }
     697                 :        1952 :     return m_net;
     698                 :             : }
     699                 :             : 
     700                 :      241938 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
     701                 :             : {
     702         [ +  + ]:      241938 :     if (IsAddrV1Compatible()) {
     703                 :      241376 :         uint8_t serialized[V1_SERIALIZATION_SIZE];
     704                 :      241376 :         SerializeV1Array(serialized);
     705                 :      241376 :         return {std::begin(serialized), std::end(serialized)};
     706                 :             :     }
     707         [ +  + ]:        1124 :     return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
     708                 :             : }
     709                 :             : 
     710                 :             : // private extensions to enum Network, only returned by GetExtNetwork,
     711                 :             : // and only used in GetReachabilityFrom
     712                 :             : static const int NET_TEREDO = NET_MAX;
     713                 :         116 : int static GetExtNetwork(const CNetAddr& addr)
     714                 :             : {
     715         [ +  + ]:         116 :     if (addr.IsRFC4380())
     716                 :             :         return NET_TEREDO;
     717                 :         105 :     return addr.GetNetwork();
     718                 :             : }
     719                 :             : 
     720                 :             : /** Calculates a metric for how reachable (*this) is from a given partner */
     721                 :          59 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
     722                 :             : {
     723                 :          59 :     enum Reachability {
     724                 :             :         REACH_UNREACHABLE,
     725                 :             :         REACH_DEFAULT,
     726                 :             :         REACH_TEREDO,
     727                 :             :         REACH_IPV6_WEAK,
     728                 :             :         REACH_IPV4,
     729                 :             :         REACH_IPV6_STRONG,
     730                 :             :         REACH_PRIVATE
     731                 :             :     };
     732                 :             : 
     733   [ +  +  -  + ]:          59 :     if (!IsRoutable() || IsInternal())
     734                 :           1 :         return REACH_UNREACHABLE;
     735                 :             : 
     736                 :          58 :     int ourNet = GetExtNetwork(*this);
     737                 :          58 :     int theirNet = GetExtNetwork(paddrPartner);
     738   [ +  +  +  -  :          58 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
                   -  + ]
     739                 :             : 
     740   [ +  +  +  +  :          58 :     switch(theirNet) {
                +  +  + ]
     741                 :           6 :     case NET_IPV4:
     742         [ +  + ]:           6 :         switch(ourNet) {
     743                 :             :         default:       return REACH_DEFAULT;
     744                 :           2 :         case NET_IPV4: return REACH_IPV4;
     745                 :             :         }
     746                 :          12 :     case NET_IPV6:
     747   [ +  +  +  + ]:          12 :         switch(ourNet) {
     748                 :             :         default:         return REACH_DEFAULT;
     749                 :           2 :         case NET_TEREDO: return REACH_TEREDO;
     750                 :           4 :         case NET_IPV4:   return REACH_IPV4;
     751         [ +  + ]:           4 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
     752                 :             :         }
     753                 :           2 :     case NET_ONION:
     754      [ -  +  - ]:           2 :         switch(ourNet) {
     755                 :             :         default:         return REACH_DEFAULT;
     756                 :           0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
     757                 :           2 :         case NET_ONION:    return REACH_PRIVATE;
     758                 :             :         }
     759                 :           2 :     case NET_I2P:
     760         [ -  + ]:           2 :         switch (ourNet) {
     761                 :             :         case NET_I2P: return REACH_PRIVATE;
     762                 :           0 :         default: return REACH_DEFAULT;
     763                 :             :         }
     764                 :           6 :     case NET_CJDNS:
     765         [ +  + ]:           6 :         switch (ourNet) {
     766                 :             :         case NET_CJDNS: return REACH_PRIVATE;
     767                 :           5 :         default: return REACH_DEFAULT;
     768                 :             :         }
     769                 :           6 :     case NET_TEREDO:
     770   [ +  +  +  + ]:           6 :         switch(ourNet) {
     771                 :             :         default:          return REACH_DEFAULT;
     772                 :           1 :         case NET_TEREDO:  return REACH_TEREDO;
     773                 :           2 :         case NET_IPV6:    return REACH_IPV6_WEAK;
     774                 :           2 :         case NET_IPV4:    return REACH_IPV4;
     775                 :             :         }
     776                 :          24 :     case NET_UNROUTABLE:
     777                 :          24 :     default:
     778   [ -  -  +  -  :          24 :         switch(ourNet) {
                      - ]
     779                 :             :         default:          return REACH_DEFAULT;
     780                 :           0 :         case NET_TEREDO:  return REACH_TEREDO;
     781                 :           0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
     782                 :          24 :         case NET_IPV4:    return REACH_IPV4;
     783                 :           0 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
     784                 :             :         }
     785                 :             :     }
     786                 :             : }
     787                 :             : 
     788                 :      164155 : CService::CService() : port(0)
     789                 :             : {
     790                 :      164155 : }
     791                 :             : 
     792                 :       49342 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
     793                 :             : {
     794                 :       49342 : }
     795                 :             : 
     796                 :          42 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
     797                 :             : {
     798                 :          42 : }
     799                 :             : 
     800                 :          11 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
     801                 :             : {
     802                 :          11 : }
     803                 :             : 
     804         [ -  + ]:        8229 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
     805                 :             : {
     806         [ -  + ]:        8229 :     assert(addr.sin_family == AF_INET);
     807                 :        8229 : }
     808                 :             : 
     809         [ -  + ]:           9 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
     810                 :             : {
     811         [ -  + ]:           9 :    assert(addr.sin6_family == AF_INET6);
     812                 :           9 : }
     813                 :             : 
     814                 :        8242 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
     815                 :             : {
     816      [ +  +  + ]:        8242 :     switch (paddr->sa_family) {
     817                 :        8229 :     case AF_INET:
     818         [ +  - ]:        8229 :         if (addrlen != sizeof(struct sockaddr_in)) return false;
     819                 :        8229 :         *this = CService(*(const struct sockaddr_in*)paddr);
     820                 :        8229 :         return true;
     821                 :           9 :     case AF_INET6:
     822         [ +  - ]:           9 :         if (addrlen != sizeof(struct sockaddr_in6)) return false;
     823                 :           9 :         *this = CService(*(const struct sockaddr_in6*)paddr);
     824                 :           9 :         return true;
     825                 :             :     default:
     826                 :             :         return false;
     827                 :             :     }
     828                 :             : }
     829                 :             : 
     830                 :        5497 : sa_family_t CService::GetSAFamily() const
     831                 :             : {
     832      [ +  -  + ]:        5497 :     switch (m_net) {
     833                 :             :     case NET_IPV4:
     834                 :             :         return AF_INET;
     835                 :        1272 :     case NET_IPV6:
     836                 :        1272 :     case NET_CJDNS:
     837                 :        1272 :         return AF_INET6;
     838                 :           0 :     default:
     839                 :           0 :         return AF_UNSPEC;
     840                 :             :     }
     841                 :             : }
     842                 :             : 
     843                 :       32116 : uint16_t CService::GetPort() const
     844                 :             : {
     845                 :       32116 :     return port;
     846                 :             : }
     847                 :             : 
     848                 :      174692 : bool operator==(const CService& a, const CService& b)
     849                 :             : {
     850   [ +  -  +  +  :      349384 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
                   +  + ]
     851                 :             : }
     852                 :             : 
     853                 :       10532 : bool operator<(const CService& a, const CService& b)
     854                 :             : {
     855   [ +  -  +  +  :       41304 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
          +  -  +  +  +  
             +  +  +  -  
                      - ]
     856                 :             : }
     857                 :             : 
     858                 :             : /**
     859                 :             :  * Obtain the IPv4/6 socket address this represents.
     860                 :             :  *
     861                 :             :  * @param[out] paddr The obtained socket address.
     862                 :             :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
     863                 :             :  *                        to by paddr. The value that's pointed to by this
     864                 :             :  *                        parameter might change after calling this function if
     865                 :             :  *                        the size of the corresponding address structure
     866                 :             :  *                        changed.
     867                 :             :  *
     868                 :             :  * @returns Whether or not the operation was successful.
     869                 :             :  */
     870                 :        5528 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
     871                 :             : {
     872         [ +  + ]:        5528 :     if (IsIPv4()) {
     873         [ +  - ]:        4249 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
     874                 :             :             return false;
     875                 :        4249 :         *addrlen = sizeof(struct sockaddr_in);
     876                 :        4249 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
     877                 :        4249 :         memset(paddrin, 0, *addrlen);
     878         [ +  - ]:        4249 :         if (!GetInAddr(&paddrin->sin_addr))
     879                 :             :             return false;
     880                 :        4249 :         paddrin->sin_family = AF_INET;
     881                 :        4249 :         paddrin->sin_port = htons(port);
     882                 :        4249 :         return true;
     883                 :             :     }
     884   [ +  +  -  + ]:        1279 :     if (IsIPv6() || IsCJDNS()) {
     885         [ +  - ]:        1278 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
     886                 :             :             return false;
     887                 :        1278 :         *addrlen = sizeof(struct sockaddr_in6);
     888                 :        1278 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
     889                 :        1278 :         memset(paddrin6, 0, *addrlen);
     890         [ +  - ]:        1278 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
     891                 :             :             return false;
     892                 :        1278 :         paddrin6->sin6_scope_id = m_scope_id;
     893                 :        1278 :         paddrin6->sin6_family = AF_INET6;
     894                 :        1278 :         paddrin6->sin6_port = htons(port);
     895                 :        1278 :         return true;
     896                 :             :     }
     897                 :             :     return false;
     898                 :             : }
     899                 :             : 
     900                 :             : /**
     901                 :             :  * @returns An identifier unique to this service's address and port number.
     902                 :             :  */
     903                 :      199210 : std::vector<unsigned char> CService::GetKey() const
     904                 :             : {
     905                 :      199210 :     auto key = GetAddrBytes();
     906         [ +  - ]:      199210 :     key.push_back(port / 0x100); // most significant byte of our port
     907         [ +  - ]:      199210 :     key.push_back(port & 0x0FF); // least significant byte of our port
     908                 :      199210 :     return key;
     909                 :           0 : }
     910                 :             : 
     911                 :      313418 : std::string CService::ToStringAddrPort() const
     912                 :             : {
     913                 :      313418 :     const auto port_str = strprintf("%u", port);
     914                 :             : 
     915   [ +  +  +  +  :      313418 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
          +  +  +  -  -  
                      + ]
     916   [ +  -  +  - ]:      933999 :         return ToStringAddr() + ":" + port_str;
     917                 :             :     } else {
     918   [ +  -  +  -  :        6255 :         return "[" + ToStringAddr() + "]:" + port_str;
                   +  - ]
     919                 :             :     }
     920                 :      313418 : }
     921                 :             : 
     922                 :        3632 : CSubNet::CSubNet():
     923                 :        3632 :     valid(false)
     924                 :             : {
     925                 :        3632 :     memset(netmask, 0, sizeof(netmask));
     926                 :        3632 : }
     927                 :             : 
     928                 :        1320 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
     929                 :             : {
     930   [ +  +  +  + ]:        1320 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
     931   [ +  +  +  + ]:          18 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
     932         [ +  + ]:        1320 :     if (!valid) {
     933                 :             :         return;
     934                 :             :     }
     935                 :             : 
     936         [ -  + ]:        1315 :     assert(mask <= sizeof(netmask) * 8);
     937                 :             : 
     938                 :        1315 :     network = addr;
     939                 :             : 
     940                 :        1315 :     uint8_t n = mask;
     941   [ -  +  +  + ]:        6731 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
     942                 :        5416 :         const uint8_t bits = n < 8 ? n : 8;
     943                 :        5416 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
     944         [ +  - ]:        5416 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
     945                 :        5416 :         n -= bits;
     946                 :             :     }
     947                 :             : }
     948                 :             : 
     949                 :             : /**
     950                 :             :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
     951                 :             :  *          the specified subnet mask is not a valid one, -1.
     952                 :             :  */
     953                 :       22274 : static inline int NetmaskBits(uint8_t x)
     954                 :             : {
     955   [ +  +  +  +  :       22274 :     switch(x) {
          +  +  +  +  +  
                      + ]
     956                 :             :     case 0x00: return 0;
     957                 :           8 :     case 0x80: return 1;
     958                 :           8 :     case 0xc0: return 2;
     959                 :          22 :     case 0xe0: return 3;
     960                 :           8 :     case 0xf0: return 4;
     961                 :           8 :     case 0xf8: return 5;
     962                 :          11 :     case 0xfc: return 6;
     963                 :           9 :     case 0xfe: return 7;
     964                 :       22090 :     case 0xff: return 8;
     965                 :           2 :     default: return -1;
     966                 :             :     }
     967                 :             : }
     968                 :             : 
     969                 :          50 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
     970                 :             : {
     971   [ +  +  +  +  :          50 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
                   +  + ]
     972         [ +  + ]:          50 :     if (!valid) {
     973                 :             :         return;
     974                 :             :     }
     975                 :             :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
     976                 :          47 :     bool zeros_found = false;
     977   [ +  -  +  + ]:         328 :     for (auto b : mask.m_addr) {
     978                 :         238 :         const int num_bits = NetmaskBits(b);
     979   [ +  +  +  + ]:         238 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
     980                 :           4 :             valid = false;
     981                 :           4 :             return;
     982                 :             :         }
     983         [ +  + ]:         234 :         if (num_bits < 8) {
     984                 :         138 :             zeros_found = true;
     985                 :             :         }
     986                 :             :     }
     987                 :             : 
     988   [ -  +  -  - ]:          43 :     assert(mask.m_addr.size() <= sizeof(netmask));
     989                 :             : 
     990         [ -  + ]:          43 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
     991                 :             : 
     992                 :          43 :     network = addr;
     993                 :             : 
     994                 :             :     // Normalize network according to netmask
     995   [ -  +  +  + ]:         263 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
     996         [ +  - ]:         440 :         network.m_addr[x] &= netmask[x];
     997                 :             :     }
     998                 :             : }
     999                 :             : 
    1000                 :        1577 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
    1001                 :             : {
    1002   [ +  +  -  - ]:        1577 :     switch (addr.m_net) {
    1003                 :        1563 :     case NET_IPV4:
    1004                 :        1563 :     case NET_IPV6:
    1005                 :        1563 :         valid = true;
    1006   [ -  +  -  - ]:        1563 :         assert(addr.m_addr.size() <= sizeof(netmask));
    1007         [ -  + ]:        1563 :         memset(netmask, 0xFF, addr.m_addr.size());
    1008                 :        1563 :         break;
    1009                 :          14 :     case NET_ONION:
    1010                 :          14 :     case NET_I2P:
    1011                 :          14 :     case NET_CJDNS:
    1012                 :          14 :         valid = true;
    1013                 :          14 :         break;
    1014                 :             :     case NET_INTERNAL:
    1015                 :             :     case NET_UNROUTABLE:
    1016                 :             :     case NET_MAX:
    1017                 :             :         return;
    1018                 :             :     }
    1019                 :             : 
    1020                 :        1577 :     network = addr;
    1021                 :             : }
    1022                 :             : 
    1023                 :             : /**
    1024                 :             :  * @returns True if this subnet is valid, the specified address is valid, and
    1025                 :             :  *          the specified address belongs in this subnet.
    1026                 :             :  */
    1027                 :        5665 : bool CSubNet::Match(const CNetAddr &addr) const
    1028                 :             : {
    1029   [ +  +  +  +  :        5665 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
                   +  + ]
    1030                 :          29 :         return false;
    1031                 :             : 
    1032      [ +  +  - ]:        5636 :     switch (network.m_net) {
    1033                 :             :     case NET_IPV4:
    1034                 :             :     case NET_IPV6:
    1035                 :             :         break;
    1036                 :           2 :     case NET_ONION:
    1037                 :           2 :     case NET_I2P:
    1038                 :           2 :     case NET_CJDNS:
    1039                 :           2 :     case NET_INTERNAL:
    1040                 :           2 :         return addr == network;
    1041                 :             :     case NET_UNROUTABLE:
    1042                 :             :     case NET_MAX:
    1043                 :             :         return false;
    1044                 :             :     }
    1045                 :             : 
    1046   [ -  +  -  +  :        5634 :     assert(network.m_addr.size() == addr.m_addr.size());
                   -  + ]
    1047         [ +  + ]:       28149 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
    1048   [ +  -  +  -  :       67629 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
                   +  + ]
    1049                 :             :             return false;
    1050                 :             :         }
    1051                 :             :     }
    1052                 :             :     return true;
    1053                 :             : }
    1054                 :             : 
    1055                 :        2708 : std::string CSubNet::ToString() const
    1056                 :             : {
    1057         [ +  + ]:        2708 :     std::string suffix;
    1058                 :             : 
    1059         [ +  + ]:        2708 :     switch (network.m_net) {
    1060                 :        2683 :     case NET_IPV4:
    1061                 :        2683 :     case NET_IPV6: {
    1062   [ -  +  -  - ]:        2683 :         assert(network.m_addr.size() <= sizeof(netmask));
    1063                 :             : 
    1064                 :        2683 :         uint8_t cidr = 0;
    1065                 :             : 
    1066   [ -  +  +  + ]:       24719 :         for (size_t i = 0; i < network.m_addr.size(); ++i) {
    1067         [ +  + ]:       23376 :             if (netmask[i] == 0x00) {
    1068                 :             :                 break;
    1069                 :             :             }
    1070                 :       22036 :             cidr += NetmaskBits(netmask[i]);
    1071                 :             :         }
    1072                 :             : 
    1073         [ +  - ]:        2683 :         suffix = strprintf("/%u", cidr);
    1074                 :        2683 :         break;
    1075                 :             :     }
    1076                 :             :     case NET_ONION:
    1077                 :             :     case NET_I2P:
    1078                 :             :     case NET_CJDNS:
    1079                 :             :     case NET_INTERNAL:
    1080                 :             :     case NET_UNROUTABLE:
    1081                 :             :     case NET_MAX:
    1082                 :             :         break;
    1083                 :             :     }
    1084                 :             : 
    1085   [ +  -  +  - ]:        5416 :     return network.ToStringAddr() + suffix;
    1086                 :        2708 : }
    1087                 :             : 
    1088                 :         884 : bool CSubNet::IsValid() const
    1089                 :             : {
    1090                 :         884 :     return valid;
    1091                 :             : }
    1092                 :             : 
    1093                 :           2 : bool operator==(const CSubNet& a, const CSubNet& b)
    1094                 :             : {
    1095   [ +  -  +  +  :           2 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
                   -  + ]
    1096                 :             : }
    1097                 :             : 
    1098                 :         188 : bool operator<(const CSubNet& a, const CSubNet& b)
    1099                 :             : {
    1100   [ +  +  +  +  :         188 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
                   +  + ]
    1101                 :             : }
        

Generated by: LCOV version 2.0-1