LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.2 % 524 504
Test Date: 2025-10-31 04:12:16 Functions: 100.0 % 72 72
Branches: 82.1 % 677 556

             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/strencodings.h>
      14                 :             : #include <util/string.h>
      15                 :             : 
      16                 :             : #include <algorithm>
      17                 :             : #include <array>
      18                 :             : #include <cstdint>
      19                 :             : #include <ios>
      20                 :             : #include <iterator>
      21                 :             : #include <string_view>
      22                 :             : #include <tuple>
      23                 :             : 
      24                 :             : using util::ContainsNoNUL;
      25                 :             : using util::HasPrefix;
      26                 :             : 
      27                 :    15227133 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
      28                 :             : {
      29   [ +  +  +  +  :    15227133 :     switch (m_net) {
                -  -  + ]
      30                 :             :     case NET_IPV4:
      31                 :             :         return BIP155Network::IPV4;
      32                 :     3667360 :     case NET_IPV6:
      33                 :     3667360 :         return BIP155Network::IPV6;
      34                 :     2784514 :     case NET_ONION:
      35                 :     2784514 :         return BIP155Network::TORV3;
      36                 :     3251916 :     case NET_I2P:
      37                 :     3251916 :         return BIP155Network::I2P;
      38                 :     2772539 :     case NET_CJDNS:
      39                 :     2772539 :         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                 :             :     } // no default case, so the compiler can warn about missing cases
      45                 :             : 
      46                 :           0 :     assert(false);
      47                 :             : }
      48                 :             : 
      49                 :    27720103 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
      50                 :             : {
      51   [ +  +  +  +  :    27720103 :     switch (possible_bip155_net) {
                   +  + ]
      52                 :     4358593 :     case BIP155Network::IPV4:
      53         [ +  + ]:     4358593 :         if (address_size == ADDR_IPV4_SIZE) {
      54                 :     4358369 :             m_net = NET_IPV4;
      55                 :     4358369 :             return true;
      56                 :             :         }
      57                 :         224 :         throw std::ios_base::failure(
      58         [ +  - ]:         224 :             strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
      59         [ +  - ]:         672 :                       ADDR_IPV4_SIZE));
      60                 :     8404128 :     case BIP155Network::IPV6:
      61         [ +  + ]:     8404128 :         if (address_size == ADDR_IPV6_SIZE) {
      62                 :     8403998 :             m_net = NET_IPV6;
      63                 :     8403998 :             return true;
      64                 :             :         }
      65                 :         130 :         throw std::ios_base::failure(
      66         [ +  - ]:         130 :             strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
      67         [ +  - ]:         390 :                       ADDR_IPV6_SIZE));
      68                 :     4359375 :     case BIP155Network::TORV3:
      69         [ +  + ]:     4359375 :         if (address_size == ADDR_TORV3_SIZE) {
      70                 :     4359310 :             m_net = NET_ONION;
      71                 :     4359310 :             return true;
      72                 :             :         }
      73                 :          65 :         throw std::ios_base::failure(
      74         [ +  - ]:          65 :             strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
      75         [ +  - ]:         195 :                       ADDR_TORV3_SIZE));
      76                 :     5536849 :     case BIP155Network::I2P:
      77         [ +  + ]:     5536849 :         if (address_size == ADDR_I2P_SIZE) {
      78                 :     5536761 :             m_net = NET_I2P;
      79                 :     5536761 :             return true;
      80                 :             :         }
      81                 :          88 :         throw std::ios_base::failure(
      82         [ +  - ]:          88 :             strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
      83         [ +  - ]:         264 :                       ADDR_I2P_SIZE));
      84                 :     4418272 :     case BIP155Network::CJDNS:
      85         [ +  + ]:     4418272 :         if (address_size == ADDR_CJDNS_SIZE) {
      86                 :     4418190 :             m_net = NET_CJDNS;
      87                 :     4418190 :             return true;
      88                 :             :         }
      89                 :          82 :         throw std::ios_base::failure(
      90         [ +  - ]:          82 :             strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
      91         [ +  - ]:         246 :                       ADDR_CJDNS_SIZE));
      92                 :             :     }
      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                 :             :     return false;
      98                 :             : }
      99                 :             : 
     100                 :             : /**
     101                 :             :  * Construct an unspecified IPv6 network address (::/128).
     102                 :             :  *
     103                 :             :  * @note This address is considered invalid by CNetAddr::IsValid()
     104                 :             :  */
     105                 :    67924512 : CNetAddr::CNetAddr() = default;
     106                 :             : 
     107                 :         575 : void CNetAddr::SetIP(const CNetAddr& ipIn)
     108                 :             : {
     109                 :             :     // Size check.
     110   [ +  +  +  +  :         575 :     switch (ipIn.m_net) {
             +  +  -  - ]
     111                 :         221 :     case NET_IPV4:
     112   [ -  +  -  + ]:         221 :         assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
     113                 :             :         break;
     114                 :         163 :     case NET_IPV6:
     115   [ -  +  -  + ]:         163 :         assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
     116                 :             :         break;
     117                 :          34 :     case NET_ONION:
     118   [ +  -  -  + ]:          34 :         assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
     119                 :             :         break;
     120                 :           9 :     case NET_I2P:
     121   [ +  -  -  + ]:           9 :         assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
     122                 :             :         break;
     123                 :          12 :     case NET_CJDNS:
     124   [ -  +  -  + ]:          12 :         assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
     125                 :             :         break;
     126                 :         136 :     case NET_INTERNAL:
     127   [ -  +  -  + ]:         136 :         assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
     128                 :             :         break;
     129                 :           0 :     case NET_UNROUTABLE:
     130                 :           0 :     case NET_MAX:
     131                 :           0 :         assert(false);
     132                 :             :     } // no default case, so the compiler can warn about missing cases
     133                 :             : 
     134                 :         575 :     m_net = ipIn.m_net;
     135                 :         575 :     m_addr = ipIn.m_addr;
     136                 :         575 : }
     137                 :             : 
     138                 :      726312 : void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
     139                 :             : {
     140         [ -  + ]:      726312 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
     141                 :             : 
     142                 :      726312 :     size_t skip{0};
     143                 :             : 
     144         [ +  + ]:      726312 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
     145                 :             :         // IPv4-in-IPv6
     146                 :        4215 :         m_net = NET_IPV4;
     147                 :        4215 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
     148         [ +  + ]:      722097 :     } 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                 :        3594 :         m_net = NET_IPV6;
     153                 :        3594 :         m_addr.assign(ADDR_IPV6_SIZE, 0x0);
     154                 :        3594 :         return;
     155         [ +  + ]:      718503 :     } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
     156                 :             :         // Internal-in-IPv6
     157                 :        3035 :         m_net = NET_INTERNAL;
     158                 :        3035 :         skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
     159                 :             :     } else {
     160                 :             :         // IPv6
     161                 :      715468 :         m_net = NET_IPV6;
     162                 :             :     }
     163                 :             : 
     164                 :      722718 :     m_addr.assign(ipv6.begin() + skip, ipv6.end());
     165                 :             : }
     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                 :     1837021 : bool CNetAddr::SetInternal(const std::string &name)
     174                 :             : {
     175         [ +  + ]:     1837021 :     if (name.empty()) {
     176                 :             :         return false;
     177                 :             :     }
     178                 :     1836971 :     m_net = NET_INTERNAL;
     179                 :     1836971 :     unsigned char hash[32] = {};
     180         [ -  + ]:     1836971 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
     181                 :     1836971 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
     182                 :     1836971 :     return true;
     183                 :             : }
     184                 :             : 
     185                 :             : namespace torv3 {
     186                 :             : // https://gitlab.torproject.org/tpo/core/torspec/-/tree/main/spec/rend-spec
     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                 :       68363 : static void Checksum(std::span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
     192                 :             : {
     193                 :             :     // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
     194                 :       68363 :     static const unsigned char prefix[] = ".onion checksum";
     195                 :       68363 :     static constexpr size_t prefix_len = 15;
     196                 :             : 
     197                 :       68363 :     SHA3_256 hasher;
     198                 :             : 
     199                 :       68363 :     hasher.Write(std::span{prefix}.first(prefix_len));
     200                 :       68363 :     hasher.Write(addr_pubkey);
     201                 :       68363 :     hasher.Write(VERSION);
     202                 :             : 
     203                 :       68363 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
     204                 :             : 
     205                 :       68363 :     hasher.Finalize(checksum_full);
     206                 :             : 
     207                 :       68363 :     memcpy(checksum, checksum_full, sizeof(checksum));
     208                 :       68363 : }
     209                 :             : 
     210                 :             : }; // namespace torv3
     211                 :             : 
     212                 :       83435 : bool CNetAddr::SetSpecial(std::string_view addr)
     213                 :             : {
     214         [ +  + ]:      166870 :     if (!ContainsNoNUL(addr)) {
     215                 :             :         return false;
     216                 :             :     }
     217                 :             : 
     218         [ +  + ]:       83398 :     if (SetTor(addr)) {
     219                 :             :         return true;
     220                 :             :     }
     221                 :             : 
     222         [ +  + ]:       82377 :     if (SetI2P(addr)) {
     223                 :        5316 :         return true;
     224                 :             :     }
     225                 :             : 
     226                 :             :     return false;
     227                 :             : }
     228                 :             : 
     229                 :       83398 : bool CNetAddr::SetTor(std::string_view addr)
     230                 :             : {
     231         [ +  + ]:       83398 :     if (!addr.ends_with(".onion")) return false;
     232                 :       23021 :     addr.remove_suffix(6);
     233                 :       23021 :     auto input = DecodeBase32(addr);
     234                 :             : 
     235         [ +  + ]:       23021 :     if (!input) {
     236                 :             :         return false;
     237                 :             :     }
     238                 :             : 
     239   [ -  +  +  + ]:        1261 :     if (input->size() == torv3::TOTAL_LEN) {
     240         [ +  + ]:        1226 :         std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
     241                 :        1226 :         std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
     242                 :        1226 :         std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
     243                 :             : 
     244         [ +  + ]:        1226 :         if (!std::ranges::equal(input_version, torv3::VERSION)) {
     245                 :             :             return false;
     246                 :             :         }
     247                 :             : 
     248                 :        1198 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
     249         [ +  - ]:        1198 :         torv3::Checksum(input_pubkey, calculated_checksum);
     250                 :             : 
     251         [ +  + ]:        1198 :         if (!std::ranges::equal(input_checksum, calculated_checksum)) {
     252                 :             :             return false;
     253                 :             :         }
     254                 :             : 
     255                 :        1021 :         m_net = NET_ONION;
     256                 :        1021 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
     257                 :        1021 :         return true;
     258                 :             :     }
     259                 :             : 
     260                 :             :     return false;
     261                 :       23021 : }
     262                 :             : 
     263                 :       82377 : bool CNetAddr::SetI2P(std::string_view addr)
     264                 :             : {
     265                 :             :     // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
     266                 :       82377 :     static constexpr size_t b32_len{52};
     267                 :       82377 :     static const char* suffix{".b32.i2p"};
     268                 :       82377 :     static constexpr size_t suffix_len{8};
     269                 :             : 
     270   [ +  +  +  +  :       87909 :     if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
                   +  + ]
     271                 :             :         return false;
     272                 :             :     }
     273                 :             : 
     274                 :             :     // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
     275                 :             :     // can decode it.
     276                 :        5365 :     const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
     277                 :             : 
     278   [ -  +  +  - ]:        5365 :     auto address_bytes = DecodeBase32(b32_padded);
     279                 :             : 
     280   [ +  +  -  +  :        5365 :     if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
                   +  + ]
     281                 :             :         return false;
     282                 :             :     }
     283                 :             : 
     284                 :        5316 :     m_net = NET_I2P;
     285                 :        5316 :     m_addr.assign(address_bytes->begin(), address_bytes->end());
     286                 :             : 
     287                 :        5316 :     return true;
     288                 :        5365 : }
     289                 :             : 
     290                 :       20423 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
     291                 :             : {
     292                 :       20423 :     m_net = NET_IPV4;
     293                 :       20423 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
     294                 :       20423 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
     295                 :       20423 : }
     296                 :             : 
     297                 :       30036 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
     298                 :             : {
     299         [ +  - ]:       30036 :     SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
     300                 :       30036 :     m_scope_id = scope;
     301                 :       30036 : }
     302                 :             : 
     303                 :         466 : bool CNetAddr::IsBindAny() const
     304                 :             : {
     305   [ +  +  +  + ]:         466 :     if (!IsIPv4() && !IsIPv6()) {
     306                 :             :         return false;
     307                 :             :     }
     308         [ +  - ]:         550 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
     309                 :             : }
     310                 :             : 
     311                 :   405744384 : bool CNetAddr::IsRFC1918() const
     312                 :             : {
     313         [ +  + ]:   405744384 :     return IsIPv4() && (
     314   [ +  +  +  +  :   141010386 :         m_addr[0] == 10 ||
                   +  + ]
     315   [ +  +  +  +  :    70834846 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
             +  +  +  + ]
     316   [ +  +  +  +  :    70721065 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
             +  +  +  + ]
     317                 :             : }
     318                 :             : 
     319                 :   405649405 : bool CNetAddr::IsRFC2544() const
     320                 :             : {
     321   [ +  +  +  +  :   476500042 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
          +  +  +  +  +  
                +  +  + ]
     322                 :             : }
     323                 :             : 
     324                 :   405588672 : bool CNetAddr::IsRFC3927() const
     325                 :             : {
     326   [ +  +  +  + ]:   405588672 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
     327                 :             : }
     328                 :             : 
     329                 :   405545498 : bool CNetAddr::IsRFC6598() const
     330                 :             : {
     331   [ +  +  +  +  :   476487413 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
          +  +  +  +  +  
                +  +  + ]
     332                 :             : }
     333                 :             : 
     334                 :   405528348 : bool CNetAddr::IsRFC5737() const
     335                 :             : {
     336   [ +  +  +  + ]:   405528348 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
     337         [ +  + ]:    70287834 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
     338         [ +  + ]:    70270036 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
     339                 :             : }
     340                 :             : 
     341                 :   434931440 : bool CNetAddr::IsRFC3849() const
     342                 :             : {
     343   [ +  +  +  + ]:   434931440 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
     344                 :             : }
     345                 :             : 
     346                 :   135592696 : bool CNetAddr::IsRFC3964() const
     347                 :             : {
     348   [ +  +  +  + ]:   135592696 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
     349                 :             : }
     350                 :             : 
     351                 :   135649040 : bool CNetAddr::IsRFC6052() const
     352                 :             : {
     353         [ +  + ]:   135649040 :     return IsIPv6() &&
     354         [ +  + ]:    49612252 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
     355                 :   135649040 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
     356                 :             : }
     357                 :             : 
     358                 :   144037378 : bool CNetAddr::IsRFC4380() const
     359                 :             : {
     360   [ +  +  +  + ]:   144037378 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
     361                 :             : }
     362                 :             : 
     363                 :   405563108 : bool CNetAddr::IsRFC4862() const
     364                 :             : {
     365   [ +  +  +  + ]:   405563108 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
     366                 :   405563108 :                                                                 0x00, 0x00, 0x00, 0x00});
     367                 :             : }
     368                 :             : 
     369                 :   405469773 : bool CNetAddr::IsRFC4193() const
     370                 :             : {
     371   [ +  +  +  +  :   532123312 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
                   +  + ]
     372                 :             : }
     373                 :             : 
     374                 :   135684157 : bool CNetAddr::IsRFC6145() const
     375                 :             : {
     376         [ +  + ]:   135684157 :     return IsIPv6() &&
     377         [ +  + ]:    49647369 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     378                 :   135684157 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
     379                 :             : }
     380                 :             : 
     381                 :   405193301 : bool CNetAddr::IsRFC4843() const
     382                 :             : {
     383   [ +  +  +  + ]:   405193301 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
     384   [ +  +  +  + ]:      281906 :            (m_addr[3] & 0xF0) == 0x10;
     385                 :             : }
     386                 :             : 
     387                 :   405178114 : bool CNetAddr::IsRFC7343() const
     388                 :             : {
     389   [ +  +  +  + ]:   405178114 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
     390   [ +  +  +  + ]:      251532 :            (m_addr[3] & 0xF0) == 0x20;
     391                 :             : }
     392                 :             : 
     393                 :     8058085 : bool CNetAddr::IsHeNet() const
     394                 :             : {
     395   [ +  -  +  + ]:     8058085 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
     396                 :             : }
     397                 :             : 
     398                 :   443352101 : bool CNetAddr::IsLocal() const
     399                 :             : {
     400                 :             :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
     401   [ +  +  +  +  :   519217203 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
             +  +  +  + ]
     402                 :             :         return true;
     403                 :             :     }
     404                 :             : 
     405                 :             :     // IPv6 loopback (::1/128)
     406                 :   442662290 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
     407   [ +  +  +  +  :   577572008 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
                   +  + ]
     408                 :       49347 :         return true;
     409                 :             :     }
     410                 :             : 
     411                 :             :     return false;
     412                 :             : }
     413                 :             : 
     414                 :             : /**
     415                 :             :  * @returns Whether or not this network address is a valid address that @a could
     416                 :             :  *          be used to refer to an actual host.
     417                 :             :  *
     418                 :             :  * @note A valid address may or may not be publicly routable on the global
     419                 :             :  *       internet. As in, the set of valid addresses is a superset of the set of
     420                 :             :  *       publicly routable addresses.
     421                 :             :  *
     422                 :             :  * @see CNetAddr::IsRoutable()
     423                 :             :  */
     424                 :   454673373 : bool CNetAddr::IsValid() const
     425                 :             : {
     426                 :             :     // unspecified IPv6 address (::/128)
     427                 :   454673373 :     unsigned char ipNone6[16] = {};
     428   [ +  +  +  +  :   608491670 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
                   +  + ]
     429                 :             :         return false;
     430                 :             :     }
     431                 :             : 
     432   [ +  +  +  + ]:   503243739 :     if (IsCJDNS() && !HasCJDNSPrefix()) {
     433                 :             :         return false;
     434                 :             :     }
     435                 :             : 
     436                 :             :     // documentation IPv6 address
     437         [ +  + ]:   434930974 :     if (IsRFC3849())
     438                 :             :         return false;
     439                 :             : 
     440         [ +  + ]:   434916113 :     if (IsInternal())
     441                 :             :         return false;
     442                 :             : 
     443         [ +  + ]:   433251125 :     if (IsIPv4()) {
     444   [ +  +  +  + ]:   151241818 :         const uint32_t addr = ReadBE32(m_addr.data());
     445         [ +  + ]:    75620909 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
     446                 :      280740 :             return false;
     447                 :             :         }
     448                 :             :     }
     449                 :             : 
     450                 :             :     return true;
     451                 :             : }
     452                 :             : 
     453                 :             : /**
     454                 :             :  * @returns Whether or not this network address is publicly routable on the
     455                 :             :  *          global internet.
     456                 :             :  *
     457                 :             :  * @note A routable address is always valid. As in, the set of routable addresses
     458                 :             :  *       is a subset of the set of valid addresses.
     459                 :             :  *
     460                 :             :  * @see CNetAddr::IsValid()
     461                 :             :  */
     462                 :   406842222 : bool CNetAddr::IsRoutable() const
     463                 :             : {
     464   [ +  +  +  +  :   406842222 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  -  
                      + ]
     465                 :             : }
     466                 :             : 
     467                 :             : /**
     468                 :             :  * @returns Whether or not this is a dummy address that represents a name.
     469                 :             :  *
     470                 :             :  * @see CNetAddr::SetInternal(const std::string &)
     471                 :             :  */
     472                 :  1103250455 : bool CNetAddr::IsInternal() const
     473                 :             : {
     474                 :  1103250455 :    return m_net == NET_INTERNAL;
     475                 :             : }
     476                 :             : 
     477                 :   123820618 : bool CNetAddr::IsAddrV1Compatible() const
     478                 :             : {
     479   [ +  -  -  + ]:   123820618 :     switch (m_net) {
     480                 :             :     case NET_IPV4:
     481                 :             :     case NET_IPV6:
     482                 :             :     case NET_INTERNAL:
     483                 :             :         return true;
     484                 :    73168324 :     case NET_ONION:
     485                 :    73168324 :     case NET_I2P:
     486                 :    73168324 :     case NET_CJDNS:
     487                 :    73168324 :         return false;
     488                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     489                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
     490                 :           0 :         assert(false);
     491                 :             :     } // no default case, so the compiler can warn about missing cases
     492                 :             : 
     493                 :           0 :     assert(false);
     494                 :             : }
     495                 :             : 
     496                 :    92516262 : enum Network CNetAddr::GetNetwork() const
     497                 :             : {
     498         [ +  + ]:    92516262 :     if (IsInternal())
     499                 :             :         return NET_INTERNAL;
     500                 :             : 
     501         [ +  + ]:    89792576 :     if (!IsRoutable())
     502                 :             :         return NET_UNROUTABLE;
     503                 :             : 
     504                 :    89052022 :     return m_net;
     505                 :             : }
     506                 :             : 
     507                 :      101588 : static std::string IPv4ToString(std::span<const uint8_t> a)
     508                 :             : {
     509                 :      101588 :     return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
     510                 :             : }
     511                 :             : 
     512                 :             : // Return an IPv6 address text representation with zero compression as described in RFC 5952
     513                 :             : // ("A Recommendation for IPv6 Address Text Representation").
     514                 :      896589 : static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
     515                 :             : {
     516         [ -  + ]:      896589 :     assert(a.size() == ADDR_IPV6_SIZE);
     517                 :      896589 :     const std::array groups{
     518                 :      896589 :         ReadBE16(&a[0]),
     519                 :      896589 :         ReadBE16(&a[2]),
     520                 :      896589 :         ReadBE16(&a[4]),
     521                 :      896589 :         ReadBE16(&a[6]),
     522                 :      896589 :         ReadBE16(&a[8]),
     523                 :      896589 :         ReadBE16(&a[10]),
     524                 :      896589 :         ReadBE16(&a[12]),
     525                 :      896589 :         ReadBE16(&a[14]),
     526                 :      896589 :     };
     527                 :             : 
     528                 :             :     // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
     529                 :             :     // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
     530                 :      896589 :     struct ZeroSpan {
     531                 :             :         size_t start_index{0};
     532                 :             :         size_t len{0};
     533                 :             :     };
     534                 :             : 
     535                 :             :     // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
     536                 :             :     // zero sequences of equal length are found.
     537                 :      896589 :     ZeroSpan longest, current;
     538         [ +  + ]:     8069301 :     for (size_t i{0}; i < groups.size(); ++i) {
     539         [ +  + ]:     7172712 :         if (groups[i] != 0) {
     540                 :     6440154 :             current = {i + 1, 0};
     541                 :     6440154 :             continue;
     542                 :             :         }
     543                 :      732558 :         current.len += 1;
     544         [ +  + ]:      732558 :         if (current.len > longest.len) {
     545                 :      674640 :             longest = current;
     546                 :             :         }
     547                 :             :     }
     548                 :             : 
     549         [ +  - ]:      896589 :     std::string r;
     550         [ +  - ]:      896589 :     r.reserve(39);
     551         [ +  + ]:     8069301 :     for (size_t i{0}; i < groups.size(); ++i) {
     552                 :             :         // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
     553   [ +  +  +  +  :     7172712 :         if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
                   +  + ]
     554         [ +  + ]:      606923 :             if (i == longest.start_index) {
     555         [ +  - ]:      115767 :                 r += "::";
     556                 :             :             }
     557                 :      606923 :             continue;
     558                 :             :         }
     559   [ +  +  +  +  :    18911243 :         r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
                   +  - ]
     560                 :             :     }
     561                 :             : 
     562         [ +  + ]:      896589 :     if (scope_id != 0) {
     563         [ +  - ]:          44 :         r += strprintf("%%%u", scope_id);
     564                 :             :     }
     565                 :             : 
     566                 :      896589 :     return r;
     567                 :           0 : }
     568                 :             : 
     569                 :       67165 : std::string OnionToString(std::span<const uint8_t> addr)
     570                 :             : {
     571                 :       67165 :     uint8_t checksum[torv3::CHECKSUM_LEN];
     572                 :       67165 :     torv3::Checksum(addr, checksum);
     573                 :             :     // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
     574                 :       67165 :     prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
     575                 :       67165 :     address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
     576                 :       67165 :     address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
     577   [ -  +  +  - ]:      268660 :     return EncodeBase32(address) + ".onion";
     578                 :       67165 : }
     579                 :             : 
     580                 :     1170594 : std::string CNetAddr::ToStringAddr() const
     581                 :             : {
     582   [ +  +  +  +  :     1170594 :     switch (m_net) {
             +  +  -  - ]
     583                 :      101588 :     case NET_IPV4:
     584         [ +  - ]:      203176 :         return IPv4ToString(m_addr);
     585                 :      880638 :     case NET_IPV6:
     586         [ +  - ]:     1761276 :         return IPv6ToString(m_addr, m_scope_id);
     587                 :       67165 :     case NET_ONION:
     588         [ -  + ]:      134330 :         return OnionToString(m_addr);
     589                 :      100920 :     case NET_I2P:
     590   [ -  +  +  - ]:      302760 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
     591                 :       15951 :     case NET_CJDNS:
     592         [ +  - ]:       31902 :         return IPv6ToString(m_addr, 0);
     593                 :        4332 :     case NET_INTERNAL:
     594   [ +  -  +  - ]:       12996 :         return EncodeBase32(m_addr) + ".internal";
     595                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     596                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
     597                 :           0 :         assert(false);
     598                 :             :     } // no default case, so the compiler can warn about missing cases
     599                 :             : 
     600                 :           0 :     assert(false);
     601                 :             : }
     602                 :             : 
     603                 :    68276123 : bool operator==(const CNetAddr& a, const CNetAddr& b)
     604                 :             : {
     605   [ +  +  +  + ]:    68276123 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
     606                 :             : }
     607                 :             : 
     608                 :     4144985 : bool operator<(const CNetAddr& a, const CNetAddr& b)
     609                 :             : {
     610                 :     4144985 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
     611                 :             : }
     612                 :             : 
     613                 :             : /**
     614                 :             :  * Try to get our IPv4 address.
     615                 :             :  *
     616                 :             :  * @param[out] pipv4Addr The in_addr struct to which to copy.
     617                 :             :  *
     618                 :             :  * @returns Whether or not the operation was successful, in particular, whether
     619                 :             :  *          or not our address was an IPv4 address.
     620                 :             :  *
     621                 :             :  * @see CNetAddr::IsIPv4()
     622                 :             :  */
     623                 :         570 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
     624                 :             : {
     625         [ +  - ]:         570 :     if (!IsIPv4())
     626                 :             :         return false;
     627   [ -  +  -  + ]:         570 :     assert(sizeof(*pipv4Addr) == m_addr.size());
     628         [ +  - ]:        1140 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
     629                 :         570 :     return true;
     630                 :             : }
     631                 :             : 
     632                 :             : /**
     633                 :             :  * Try to get our IPv6 (or CJDNS) address.
     634                 :             :  *
     635                 :             :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
     636                 :             :  *
     637                 :             :  * @returns Whether or not the operation was successful, in particular, whether
     638                 :             :  *          or not our address was an IPv6 address.
     639                 :             :  *
     640                 :             :  * @see CNetAddr::IsIPv6()
     641                 :             :  */
     642                 :        1450 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
     643                 :             : {
     644   [ +  +  +  - ]:        1450 :     if (!IsIPv6() && !IsCJDNS()) {
     645                 :             :         return false;
     646                 :             :     }
     647   [ -  +  -  + ]:        1450 :     assert(sizeof(*pipv6Addr) == m_addr.size());
     648         [ +  - ]:        2900 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
     649                 :        1450 :     return true;
     650                 :             : }
     651                 :             : 
     652                 :   152399548 : bool CNetAddr::HasLinkedIPv4() const
     653                 :             : {
     654   [ +  -  +  +  :   152399548 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
          +  +  +  +  +  
                +  +  + ]
     655                 :             : }
     656                 :             : 
     657                 :     9396389 : uint32_t CNetAddr::GetLinkedIPv4() const
     658                 :             : {
     659         [ +  + ]:     9396389 :     if (IsIPv4()) {
     660         [ +  + ]:    18685524 :         return ReadBE32(m_addr.data());
     661   [ +  +  +  + ]:       53627 :     } else if (IsRFC6052() || IsRFC6145()) {
     662                 :             :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
     663         [ +  + ]:       53396 :         return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
     664         [ +  + ]:       26929 :     } else if (IsRFC3964()) {
     665                 :             :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
     666         [ +  + ]:       27734 :         return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
     667         [ +  - ]:       13062 :     } else if (IsRFC4380()) {
     668                 :             :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
     669         [ +  + ]:       26124 :         return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
     670                 :             :     }
     671                 :           0 :     assert(false);
     672                 :             : }
     673                 :             : 
     674                 :   108694603 : Network CNetAddr::GetNetClass() const
     675                 :             : {
     676                 :             :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
     677                 :             : 
     678                 :             :     // Check for "internal" first because such addresses are also !IsRoutable()
     679                 :             :     // and we don't want to return NET_UNROUTABLE in that case.
     680         [ +  + ]:   108694603 :     if (IsInternal()) {
     681                 :             :         return NET_INTERNAL;
     682                 :             :     }
     683         [ +  + ]:   105426225 :     if (!IsRoutable()) {
     684                 :             :         return NET_UNROUTABLE;
     685                 :             :     }
     686         [ +  + ]:   104345062 :     if (HasLinkedIPv4()) {
     687                 :             :         return NET_IPV4;
     688                 :             :     }
     689                 :    88337964 :     return m_net;
     690                 :             : }
     691                 :             : 
     692                 :   123811337 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
     693                 :             : {
     694         [ +  + ]:   123811337 :     if (IsAddrV1Compatible()) {
     695                 :    50645948 :         uint8_t serialized[V1_SERIALIZATION_SIZE];
     696                 :    50645948 :         SerializeV1Array(serialized);
     697                 :    50645948 :         return {std::begin(serialized), std::end(serialized)};
     698                 :             :     }
     699         [ +  + ]:   146330778 :     return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
     700                 :             : }
     701                 :             : 
     702                 :             : // private extensions to enum Network, only returned by GetExtNetwork,
     703                 :             : // and only used in GetReachabilityFrom
     704                 :             : static const int NET_TEREDO = NET_MAX;
     705                 :    16992802 : int static GetExtNetwork(const CNetAddr& addr)
     706                 :             : {
     707         [ +  + ]:    16992802 :     if (addr.IsRFC4380())
     708                 :             :         return NET_TEREDO;
     709                 :    16992031 :     return addr.GetNetwork();
     710                 :             : }
     711                 :             : 
     712                 :             : /** Calculates a metric for how reachable (*this) is from a given partner */
     713                 :     8496611 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
     714                 :             : {
     715                 :     8496611 :     enum Reachability {
     716                 :             :         REACH_UNREACHABLE,
     717                 :             :         REACH_DEFAULT,
     718                 :             :         REACH_TEREDO,
     719                 :             :         REACH_IPV6_WEAK,
     720                 :             :         REACH_IPV4,
     721                 :             :         REACH_IPV6_STRONG,
     722                 :             :         REACH_PRIVATE
     723                 :             :     };
     724                 :             : 
     725   [ +  +  -  + ]:     8496611 :     if (!IsRoutable() || IsInternal())
     726                 :         210 :         return REACH_UNREACHABLE;
     727                 :             : 
     728                 :     8496401 :     int ourNet = GetExtNetwork(*this);
     729                 :     8496401 :     int theirNet = GetExtNetwork(paddrPartner);
     730   [ +  +  +  +  :     8496401 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
                   +  + ]
     731                 :             : 
     732   [ +  +  +  +  :     8496401 :     switch(theirNet) {
                +  +  + ]
     733                 :      949262 :     case NET_IPV4:
     734         [ +  + ]:      949262 :         switch(ourNet) {
     735                 :             :         default:       return REACH_DEFAULT;
     736                 :      301332 :         case NET_IPV4: return REACH_IPV4;
     737                 :             :         }
     738                 :     4617893 :     case NET_IPV6:
     739   [ +  +  +  + ]:     4617893 :         switch(ourNet) {
     740                 :             :         default:         return REACH_DEFAULT;
     741                 :         290 :         case NET_TEREDO: return REACH_TEREDO;
     742                 :     1517450 :         case NET_IPV4:   return REACH_IPV4;
     743         [ +  + ]:     2988789 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
     744                 :             :         }
     745                 :        2705 :     case NET_ONION:
     746      [ -  +  + ]:        2705 :         switch(ourNet) {
     747                 :             :         default:         return REACH_DEFAULT;
     748                 :           0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
     749                 :        2703 :         case NET_ONION:    return REACH_PRIVATE;
     750                 :             :         }
     751                 :        8793 :     case NET_I2P:
     752         [ +  + ]:        8793 :         switch (ourNet) {
     753                 :             :         case NET_I2P: return REACH_PRIVATE;
     754                 :        1055 :         default: return REACH_DEFAULT;
     755                 :             :         }
     756                 :        5381 :     case NET_CJDNS:
     757         [ +  + ]:        5381 :         switch (ourNet) {
     758                 :             :         case NET_CJDNS: return REACH_PRIVATE;
     759                 :        5095 :         default: return REACH_DEFAULT;
     760                 :             :         }
     761                 :         464 :     case NET_TEREDO:
     762   [ +  +  +  + ]:         464 :         switch(ourNet) {
     763                 :             :         default:          return REACH_DEFAULT;
     764                 :           3 :         case NET_TEREDO:  return REACH_TEREDO;
     765                 :         131 :         case NET_IPV6:    return REACH_IPV6_WEAK;
     766                 :         278 :         case NET_IPV4:    return REACH_IPV4;
     767                 :             :         }
     768                 :     2911903 :     case NET_UNROUTABLE:
     769                 :     2911903 :     default:
     770   [ +  +  +  +  :     2911903 :         switch(ourNet) {
                      + ]
     771                 :             :         default:          return REACH_DEFAULT;
     772                 :          12 :         case NET_TEREDO:  return REACH_TEREDO;
     773                 :     1855914 :         case NET_IPV6:    return REACH_IPV6_WEAK;
     774                 :     1011360 :         case NET_IPV4:    return REACH_IPV4;
     775                 :          26 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
     776                 :             :         }
     777                 :             :     }
     778                 :             : }
     779                 :             : 
     780                 :    22281420 : CService::CService() : port(0)
     781                 :             : {
     782                 :    22281420 : }
     783                 :             : 
     784                 :    12816550 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
     785                 :             : {
     786                 :    12816550 : }
     787                 :             : 
     788                 :          25 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
     789                 :             : {
     790                 :          25 : }
     791                 :             : 
     792                 :         414 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
     793                 :             : {
     794                 :         414 : }
     795                 :             : 
     796         [ -  + ]:          68 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
     797                 :             : {
     798         [ -  + ]:          68 :     assert(addr.sin_family == AF_INET);
     799                 :          68 : }
     800                 :             : 
     801         [ -  + ]:          46 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
     802                 :             : {
     803         [ -  + ]:          46 :    assert(addr.sin6_family == AF_INET6);
     804                 :          46 : }
     805                 :             : 
     806                 :         119 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
     807                 :             : {
     808      [ +  +  + ]:         119 :     switch (paddr->sa_family) {
     809                 :          70 :     case AF_INET:
     810         [ +  + ]:          70 :         if (addrlen != sizeof(struct sockaddr_in)) return false;
     811                 :          68 :         *this = CService(*(const struct sockaddr_in*)paddr);
     812                 :          68 :         return true;
     813                 :          47 :     case AF_INET6:
     814         [ +  + ]:          47 :         if (addrlen != sizeof(struct sockaddr_in6)) return false;
     815                 :          46 :         *this = CService(*(const struct sockaddr_in6*)paddr);
     816                 :          46 :         return true;
     817                 :             :     default:
     818                 :             :         return false;
     819                 :             :     }
     820                 :             : }
     821                 :             : 
     822                 :        1285 : sa_family_t CService::GetSAFamily() const
     823                 :             : {
     824      [ +  +  + ]:        1285 :     switch (m_net) {
     825                 :             :     case NET_IPV4:
     826                 :             :         return AF_INET;
     827                 :        1225 :     case NET_IPV6:
     828                 :        1225 :     case NET_CJDNS:
     829                 :        1225 :         return AF_INET6;
     830                 :           3 :     default:
     831                 :           3 :         return AF_UNSPEC;
     832                 :             :     }
     833                 :             : }
     834                 :             : 
     835                 :       54624 : uint16_t CService::GetPort() const
     836                 :             : {
     837                 :       54624 :     return port;
     838                 :             : }
     839                 :             : 
     840                 :    55228016 : bool operator==(const CService& a, const CService& b)
     841                 :             : {
     842   [ +  -  +  +  :   110456032 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
                   +  + ]
     843                 :             : }
     844                 :             : 
     845                 :      105747 : bool operator<(const CService& a, const CService& b)
     846                 :             : {
     847   [ +  -  +  +  :      304326 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
          +  -  +  +  +  
             +  +  +  -  
                      - ]
     848                 :             : }
     849                 :             : 
     850                 :             : /**
     851                 :             :  * Obtain the IPv4/6 socket address this represents.
     852                 :             :  *
     853                 :             :  * @param[out] paddr The obtained socket address.
     854                 :             :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
     855                 :             :  *                        to by paddr. The value that's pointed to by this
     856                 :             :  *                        parameter might change after calling this function if
     857                 :             :  *                        the size of the corresponding address structure
     858                 :             :  *                        changed.
     859                 :             :  *
     860                 :             :  * @returns Whether or not the operation was successful.
     861                 :             :  */
     862                 :        1881 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
     863                 :             : {
     864         [ +  + ]:        1881 :     if (IsIPv4()) {
     865         [ +  - ]:         396 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
     866                 :             :             return false;
     867                 :         396 :         *addrlen = sizeof(struct sockaddr_in);
     868                 :         396 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
     869                 :         396 :         memset(paddrin, 0, *addrlen);
     870         [ +  - ]:         396 :         if (!GetInAddr(&paddrin->sin_addr))
     871                 :             :             return false;
     872                 :         396 :         paddrin->sin_family = AF_INET;
     873                 :         396 :         paddrin->sin_port = htons(port);
     874                 :         396 :         return true;
     875                 :             :     }
     876   [ +  +  +  + ]:        1485 :     if (IsIPv6() || IsCJDNS()) {
     877         [ +  - ]:        1399 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
     878                 :             :             return false;
     879                 :        1399 :         *addrlen = sizeof(struct sockaddr_in6);
     880                 :        1399 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
     881                 :        1399 :         memset(paddrin6, 0, *addrlen);
     882         [ +  - ]:        1399 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
     883                 :             :             return false;
     884                 :        1399 :         paddrin6->sin6_scope_id = m_scope_id;
     885                 :        1399 :         paddrin6->sin6_family = AF_INET6;
     886                 :        1399 :         paddrin6->sin6_port = htons(port);
     887                 :        1399 :         return true;
     888                 :             :     }
     889                 :             :     return false;
     890                 :             : }
     891                 :             : 
     892                 :             : /**
     893                 :             :  * @returns An identifier unique to this service's address and port number.
     894                 :             :  */
     895                 :    70545877 : std::vector<unsigned char> CService::GetKey() const
     896                 :             : {
     897                 :    70545877 :     auto key = GetAddrBytes();
     898         [ +  - ]:    70545877 :     key.push_back(port / 0x100); // most significant byte of our port
     899         [ +  - ]:    70545877 :     key.push_back(port & 0x0FF); // least significant byte of our port
     900                 :    70545877 :     return key;
     901                 :           0 : }
     902                 :             : 
     903                 :      266389 : std::string CService::ToStringAddrPort() const
     904                 :             : {
     905                 :      266389 :     const auto port_str = strprintf("%u", port);
     906                 :             : 
     907   [ +  +  +  +  :      266389 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
          +  +  +  -  +  
                      + ]
     908   [ +  -  +  - ]:      476088 :         return ToStringAddr() + ":" + port_str;
     909                 :             :     } else {
     910   [ +  -  +  -  :      323079 :         return "[" + ToStringAddr() + "]:" + port_str;
                   +  - ]
     911                 :             :     }
     912                 :      266389 : }
     913                 :             : 
     914                 :       95821 : CSubNet::CSubNet():
     915                 :       95821 :     valid(false)
     916                 :             : {
     917                 :       95821 :     memset(netmask, 0, sizeof(netmask));
     918                 :       95821 : }
     919                 :             : 
     920                 :       28331 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
     921                 :             : {
     922   [ +  +  +  + ]:       28331 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
     923   [ +  +  +  + ]:       26077 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
     924         [ +  + ]:       28331 :     if (!valid) {
     925                 :             :         return;
     926                 :             :     }
     927                 :             : 
     928         [ -  + ]:       19938 :     assert(mask <= sizeof(netmask) * 8);
     929                 :             : 
     930                 :       19938 :     network = addr;
     931                 :             : 
     932                 :       19938 :     uint8_t n = mask;
     933   [ -  +  +  + ]:      311898 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
     934                 :      291960 :         const uint8_t bits = n < 8 ? n : 8;
     935                 :      291960 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
     936         [ +  - ]:      291960 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
     937                 :      291960 :         n -= bits;
     938                 :             :     }
     939                 :             : }
     940                 :             : 
     941                 :             : /**
     942                 :             :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
     943                 :             :  *          the specified subnet mask is not a valid one, -1.
     944                 :             :  */
     945                 :    12117347 : static inline int NetmaskBits(uint8_t x)
     946                 :             : {
     947   [ +  +  +  +  :    12117347 :     switch(x) {
          +  +  +  +  +  
                      + ]
     948                 :             :     case 0x00: return 0;
     949                 :        3518 :     case 0x80: return 1;
     950                 :         649 :     case 0xc0: return 2;
     951                 :        1388 :     case 0xe0: return 3;
     952                 :         925 :     case 0xf0: return 4;
     953                 :         800 :     case 0xf8: return 5;
     954                 :         547 :     case 0xfc: return 6;
     955                 :         741 :     case 0xfe: return 7;
     956                 :    12106015 :     case 0xff: return 8;
     957                 :         284 :     default: return -1;
     958                 :             :     }
     959                 :             : }
     960                 :             : 
     961                 :        2013 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
     962                 :             : {
     963   [ +  +  +  +  :        2013 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
                   +  + ]
     964         [ +  + ]:        2013 :     if (!valid) {
     965                 :             :         return;
     966                 :             :     }
     967                 :             :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
     968                 :        1674 :     bool zeros_found = false;
     969   [ +  -  +  + ]:        8307 :     for (auto b : mask.m_addr) {
     970                 :        6017 :         const int num_bits = NetmaskBits(b);
     971   [ +  +  +  + ]:        6017 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
     972                 :        1058 :             valid = false;
     973                 :        1058 :             return;
     974                 :             :         }
     975         [ +  + ]:        4959 :         if (num_bits < 8) {
     976                 :        2613 :             zeros_found = true;
     977                 :             :         }
     978                 :             :     }
     979                 :             : 
     980   [ -  +  -  - ]:         616 :     assert(mask.m_addr.size() <= sizeof(netmask));
     981                 :             : 
     982         [ -  + ]:         616 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
     983                 :             : 
     984                 :         616 :     network = addr;
     985                 :             : 
     986                 :             :     // Normalize network according to netmask
     987   [ -  +  +  + ]:        3560 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
     988         [ +  - ]:        5888 :         network.m_addr[x] &= netmask[x];
     989                 :             :     }
     990                 :             : }
     991                 :             : 
     992                 :       35350 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
     993                 :             : {
     994   [ +  +  -  + ]:       35350 :     switch (addr.m_net) {
     995                 :       23892 :     case NET_IPV4:
     996                 :       23892 :     case NET_IPV6:
     997                 :       23892 :         valid = true;
     998   [ -  +  -  - ]:       23892 :         assert(addr.m_addr.size() <= sizeof(netmask));
     999         [ -  + ]:       23892 :         memset(netmask, 0xFF, addr.m_addr.size());
    1000                 :       23892 :         break;
    1001                 :        7854 :     case NET_ONION:
    1002                 :        7854 :     case NET_I2P:
    1003                 :        7854 :     case NET_CJDNS:
    1004                 :        7854 :         valid = true;
    1005                 :        7854 :         break;
    1006                 :             :     case NET_INTERNAL:
    1007                 :             :     case NET_UNROUTABLE:
    1008                 :             :     case NET_MAX:
    1009                 :             :         return;
    1010                 :             :     }
    1011                 :             : 
    1012                 :       31746 :     network = addr;
    1013                 :             : }
    1014                 :             : 
    1015                 :             : /**
    1016                 :             :  * @returns True if this subnet is valid, the specified address is valid, and
    1017                 :             :  *          the specified address belongs in this subnet.
    1018                 :             :  */
    1019                 :       94536 : bool CSubNet::Match(const CNetAddr &addr) const
    1020                 :             : {
    1021   [ +  +  +  +  :       94536 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
                   +  + ]
    1022                 :       58090 :         return false;
    1023                 :             : 
    1024      [ +  +  - ]:       36446 :     switch (network.m_net) {
    1025                 :             :     case NET_IPV4:
    1026                 :             :     case NET_IPV6:
    1027                 :             :         break;
    1028                 :        2369 :     case NET_ONION:
    1029                 :        2369 :     case NET_I2P:
    1030                 :        2369 :     case NET_CJDNS:
    1031                 :        2369 :     case NET_INTERNAL:
    1032                 :        2369 :         return addr == network;
    1033                 :             :     case NET_UNROUTABLE:
    1034                 :             :     case NET_MAX:
    1035                 :             :         return false;
    1036                 :             :     }
    1037                 :             : 
    1038   [ -  +  -  +  :       34077 :     assert(network.m_addr.size() == addr.m_addr.size());
                   -  + ]
    1039         [ +  + ]:       45709 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
    1040   [ +  -  +  -  :      135339 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
                   +  + ]
    1041                 :             :             return false;
    1042                 :             :         }
    1043                 :             :     }
    1044                 :             :     return true;
    1045                 :             : }
    1046                 :             : 
    1047                 :      878805 : std::string CSubNet::ToString() const
    1048                 :             : {
    1049         [ +  + ]:      878805 :     std::string suffix;
    1050                 :             : 
    1051         [ +  + ]:      878805 :     switch (network.m_net) {
    1052                 :      773014 :     case NET_IPV4:
    1053                 :      773014 :     case NET_IPV6: {
    1054   [ -  +  -  - ]:      773014 :         assert(network.m_addr.size() <= sizeof(netmask));
    1055                 :             : 
    1056                 :      773014 :         uint8_t cidr = 0;
    1057                 :             : 
    1058   [ -  +  +  + ]:    12884344 :         for (size_t i = 0; i < network.m_addr.size(); ++i) {
    1059         [ +  + ]:    12121584 :             if (netmask[i] == 0x00) {
    1060                 :             :                 break;
    1061                 :             :             }
    1062                 :    12111330 :             cidr += NetmaskBits(netmask[i]);
    1063                 :             :         }
    1064                 :             : 
    1065         [ +  - ]:      773014 :         suffix = strprintf("/%u", cidr);
    1066                 :      773014 :         break;
    1067                 :             :     }
    1068                 :             :     case NET_ONION:
    1069                 :             :     case NET_I2P:
    1070                 :             :     case NET_CJDNS:
    1071                 :             :     case NET_INTERNAL:
    1072                 :             :     case NET_UNROUTABLE:
    1073                 :             :     case NET_MAX:
    1074                 :             :         break;
    1075                 :             :     }
    1076                 :             : 
    1077   [ +  -  +  - ]:     1757610 :     return network.ToStringAddr() + suffix;
    1078                 :      878805 : }
    1079                 :             : 
    1080                 :     1023116 : bool CSubNet::IsValid() const
    1081                 :             : {
    1082                 :     1023116 :     return valid;
    1083                 :             : }
    1084                 :             : 
    1085                 :        4431 : bool operator==(const CSubNet& a, const CSubNet& b)
    1086                 :             : {
    1087   [ +  -  +  -  :        4431 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
                   -  + ]
    1088                 :             : }
    1089                 :             : 
    1090                 :      455795 : bool operator<(const CSubNet& a, const CSubNet& b)
    1091                 :             : {
    1092   [ +  +  +  +  :      455795 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
                   +  + ]
    1093                 :             : }
        

Generated by: LCOV version 2.0-1