LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.2 % 528 508
Test Date: 2026-10-13 06:24:39 Functions: 100.0 % 73 73
Branches: 82.4 % 687 566

             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::ContainsNUL;
      25                 :             : using util::HasPrefix;
      26                 :             : 
      27                 :    11922399 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
      28                 :             : {
      29   [ +  +  +  +  :    11922399 :     switch (m_net) {
                -  -  + ]
      30                 :             :     case NET_IPV4:
      31                 :             :         return BIP155Network::IPV4;
      32                 :     2819573 :     case NET_IPV6:
      33                 :     2819573 :         return BIP155Network::IPV6;
      34                 :     2238451 :     case NET_ONION:
      35                 :     2238451 :         return BIP155Network::TORV3;
      36                 :     2508142 :     case NET_I2P:
      37                 :     2508142 :         return BIP155Network::I2P;
      38                 :     2183010 :     case NET_CJDNS:
      39                 :     2183010 :         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                 :    21664744 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
      50                 :             : {
      51   [ +  +  +  +  :    21664744 :     switch (possible_bip155_net) {
                   +  + ]
      52                 :     3413180 :     case BIP155Network::IPV4:
      53         [ +  + ]:     3413180 :         if (address_size == ADDR_IPV4_SIZE) {
      54                 :     3413012 :             m_net = NET_IPV4;
      55                 :     3413012 :             return true;
      56                 :             :         }
      57                 :         168 :         throw std::ios_base::failure(
      58         [ +  - ]:         168 :             strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
      59         [ +  - ]:         504 :                       ADDR_IPV4_SIZE));
      60                 :     6579471 :     case BIP155Network::IPV6:
      61         [ +  + ]:     6579471 :         if (address_size == ADDR_IPV6_SIZE) {
      62                 :     6579341 :             m_net = NET_IPV6;
      63                 :     6579341 :             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                 :     3461617 :     case BIP155Network::TORV3:
      69         [ +  + ]:     3461617 :         if (address_size == ADDR_TORV3_SIZE) {
      70                 :     3461503 :             m_net = NET_ONION;
      71                 :     3461503 :             return true;
      72                 :             :         }
      73                 :         114 :         throw std::ios_base::failure(
      74         [ +  - ]:         114 :             strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
      75         [ +  - ]:         342 :                       ADDR_TORV3_SIZE));
      76                 :     4167389 :     case BIP155Network::I2P:
      77         [ +  + ]:     4167389 :         if (address_size == ADDR_I2P_SIZE) {
      78                 :     4167276 :             m_net = NET_I2P;
      79                 :     4167276 :             return true;
      80                 :             :         }
      81                 :         113 :         throw std::ios_base::failure(
      82         [ +  - ]:         113 :             strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
      83         [ +  - ]:         339 :                       ADDR_I2P_SIZE));
      84                 :     3479407 :     case BIP155Network::CJDNS:
      85         [ +  + ]:     3479407 :         if (address_size == ADDR_CJDNS_SIZE) {
      86                 :     3479336 :             m_net = NET_CJDNS;
      87                 :     3479336 :             return true;
      88                 :             :         }
      89                 :          71 :         throw std::ios_base::failure(
      90         [ +  - ]:          71 :             strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
      91         [ +  - ]:         213 :                       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                 :    58802391 : CNetAddr::CNetAddr() = default;
     106                 :             : 
     107                 :        1168 : void CNetAddr::SetIP(const CNetAddr& ipIn)
     108                 :             : {
     109                 :             :     // Size check.
     110   [ +  +  +  +  :        1168 :     switch (ipIn.m_net) {
             +  +  -  - ]
     111                 :         162 :     case NET_IPV4:
     112   [ -  +  -  + ]:         162 :         assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
     113                 :             :         break;
     114                 :         829 :     case NET_IPV6:
     115   [ -  +  -  + ]:         829 :         assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
     116                 :             :         break;
     117                 :          32 :     case NET_ONION:
     118   [ +  -  -  + ]:          32 :         assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
     119                 :             :         break;
     120                 :           5 :     case NET_I2P:
     121   [ +  -  -  + ]:           5 :         assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
     122                 :             :         break;
     123                 :           9 :     case NET_CJDNS:
     124   [ -  +  -  + ]:           9 :         assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
     125                 :             :         break;
     126                 :         131 :     case NET_INTERNAL:
     127   [ -  +  -  + ]:         131 :         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                 :        1168 :     m_net = ipIn.m_net;
     135                 :        1168 :     m_addr = ipIn.m_addr;
     136                 :        1168 : }
     137                 :             : 
     138                 :      514277 : void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
     139                 :             : {
     140         [ -  + ]:      514277 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
     141                 :             : 
     142                 :      514277 :     size_t skip{0};
     143                 :             : 
     144         [ +  + ]:      514277 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
     145                 :             :         // IPv4-in-IPv6
     146                 :        2760 :         m_net = NET_IPV4;
     147                 :        2760 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
     148         [ +  + ]:      511517 :     } 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                 :        2023 :         m_net = NET_IPV6;
     153                 :        2023 :         m_addr.assign(ADDR_IPV6_SIZE, 0x0);
     154                 :        2023 :         return;
     155         [ +  + ]:      509494 :     } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
     156                 :             :         // Internal-in-IPv6
     157                 :        1817 :         m_net = NET_INTERNAL;
     158                 :        1817 :         skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
     159                 :             :     } else {
     160                 :             :         // IPv6
     161                 :      507677 :         m_net = NET_IPV6;
     162                 :             :     }
     163                 :             : 
     164                 :      512254 :     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                 :     1511018 : bool CNetAddr::SetInternal(const std::string &name)
     174                 :             : {
     175         [ +  + ]:     1511018 :     if (name.empty()) {
     176                 :             :         return false;
     177                 :             :     }
     178                 :     1510975 :     m_net = NET_INTERNAL;
     179                 :     1510975 :     unsigned char hash[32] = {};
     180         [ -  + ]:     1510975 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
     181                 :     1510975 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
     182                 :     1510975 :     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                 :       41323 : 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                 :       41323 :     static const unsigned char prefix[] = ".onion checksum";
     195                 :       41323 :     static constexpr size_t prefix_len = 15;
     196                 :             : 
     197                 :       41323 :     SHA3_256 hasher;
     198                 :             : 
     199                 :       41323 :     hasher.Write(std::span{prefix}.first(prefix_len));
     200                 :       41323 :     hasher.Write(addr_pubkey);
     201                 :       41323 :     hasher.Write(VERSION);
     202                 :             : 
     203                 :       41323 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
     204                 :             : 
     205                 :       41323 :     hasher.Finalize(checksum_full);
     206                 :             : 
     207                 :       41323 :     memcpy(checksum, checksum_full, sizeof(checksum));
     208                 :       41323 : }
     209                 :             : 
     210                 :             : }; // namespace torv3
     211                 :             : 
     212                 :      224564 : bool CNetAddr::SetSpecial(std::string_view addr)
     213                 :             : {
     214         [ +  + ]:      449128 :     if (ContainsNUL(addr)) {
     215                 :             :         return false;
     216                 :             :     }
     217                 :             : 
     218         [ +  + ]:      141522 :     if (SetTor(addr)) {
     219                 :             :         return true;
     220                 :             :     }
     221                 :             : 
     222         [ +  + ]:      138171 :     if (SetI2P(addr)) {
     223                 :       10242 :         return true;
     224                 :             :     }
     225                 :             : 
     226                 :             :     return false;
     227                 :             : }
     228                 :             : 
     229                 :      141522 : bool CNetAddr::SetTor(std::string_view addr)
     230                 :             : {
     231         [ +  + ]:      141522 :     if (!addr.ends_with(".onion")) return false;
     232                 :       13524 :     addr.remove_suffix(6);
     233                 :       13524 :     auto input = DecodeBase32(addr);
     234                 :             : 
     235         [ +  + ]:       13524 :     if (!input) {
     236                 :             :         return false;
     237                 :             :     }
     238                 :             : 
     239   [ -  +  +  + ]:        4253 :     if (input->size() == torv3::TOTAL_LEN) {
     240         [ +  + ]:        3982 :         std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
     241                 :        3982 :         std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
     242                 :        3982 :         std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
     243                 :             : 
     244         [ +  + ]:        3982 :         if (!std::ranges::equal(input_version, torv3::VERSION)) {
     245                 :             :             return false;
     246                 :             :         }
     247                 :             : 
     248                 :        3723 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
     249         [ +  - ]:        3723 :         torv3::Checksum(input_pubkey, calculated_checksum);
     250                 :             : 
     251         [ +  + ]:        3723 :         if (!std::ranges::equal(input_checksum, calculated_checksum)) {
     252                 :             :             return false;
     253                 :             :         }
     254                 :             : 
     255                 :        3351 :         m_net = NET_ONION;
     256                 :        3351 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
     257                 :        3351 :         return true;
     258                 :             :     }
     259                 :             : 
     260                 :             :     return false;
     261                 :       13524 : }
     262                 :             : 
     263                 :      138171 : bool CNetAddr::SetI2P(std::string_view addr)
     264                 :             : {
     265                 :             :     // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
     266                 :      138171 :     static constexpr size_t b32_len{52};
     267                 :      138171 :     static const char* suffix{".b32.i2p"};
     268                 :      138171 :     static constexpr size_t suffix_len{8};
     269                 :             : 
     270   [ +  +  +  +  :      149306 :     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                 :       10615 :     const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
     277                 :             : 
     278   [ -  +  +  - ]:       10615 :     auto address_bytes = DecodeBase32(b32_padded);
     279                 :             : 
     280   [ +  +  -  +  :       10615 :     if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
                   +  + ]
     281                 :             :         return false;
     282                 :             :     }
     283                 :             : 
     284                 :       10242 :     m_net = NET_I2P;
     285                 :       10242 :     m_addr.assign(address_bytes->begin(), address_bytes->end());
     286                 :             : 
     287                 :       10242 :     return true;
     288                 :       10615 : }
     289                 :             : 
     290                 :       25200 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
     291                 :             : {
     292                 :       25200 :     m_net = NET_IPV4;
     293                 :       25200 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
     294                 :       25200 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
     295                 :       25200 : }
     296                 :             : 
     297                 :       25803 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
     298                 :             : {
     299         [ +  - ]:       25803 :     SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
     300                 :       25803 :     m_scope_id = scope;
     301                 :       25803 : }
     302                 :             : 
     303                 :         374 : bool CNetAddr::IsBindAny() const
     304                 :             : {
     305   [ +  +  +  + ]:         374 :     if (!IsIPv4() && !IsIPv6()) {
     306                 :             :         return false;
     307                 :             :     }
     308         [ +  - ]:         394 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
     309                 :             : }
     310                 :             : 
     311                 :   311271144 : bool CNetAddr::IsRFC1918() const
     312                 :             : {
     313         [ +  + ]:   311271144 :     return IsIPv4() && (
     314   [ +  +  +  +  :   119861032 :         m_addr[0] == 10 ||
                   +  + ]
     315   [ +  +  +  +  :    62377237 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
             +  +  +  + ]
     316   [ +  +  +  +  :    60097058 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
             +  +  +  + ]
     317                 :             : }
     318                 :             : 
     319                 :   311189803 : bool CNetAddr::IsRFC2544() const
     320                 :             : {
     321                 :   371348445 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
           [ +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
     322                 :             : }
     323                 :             : 
     324                 :   311143831 : bool CNetAddr::IsRFC3927() const
     325                 :             : {
     326   [ +  +  +  + ]:   311143831 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
     327                 :             : }
     328                 :             : 
     329                 :   311123369 : bool CNetAddr::IsRFC6598() const
     330                 :             : {
     331                 :   371328792 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
           [ +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
     332                 :             : }
     333                 :             : 
     334                 :   311107996 : bool CNetAddr::IsRFC5737() const
     335                 :             : {
     336   [ +  +  +  + ]:   311107996 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
     337         [ +  + ]:    58747106 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
     338         [ +  + ]:    58732009 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
     339                 :             : }
     340                 :             : 
     341                 :   338215084 : bool CNetAddr::IsRFC3849() const
     342                 :             : {
     343   [ +  +  +  + ]:   338215084 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
     344                 :             : }
     345                 :             : 
     346                 :   338198032 : bool CNetAddr::IsRFC9637() const
     347                 :             : {
     348   [ +  +  +  + ]:   338198032 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x3F, 0xFF}) &&
     349   [ +  +  +  + ]:       26162 :            (m_addr[2] & 0xF0) == 0x00;
     350                 :             : }
     351                 :             : 
     352                 :   102471608 : bool CNetAddr::IsRFC3964() const
     353                 :             : {
     354   [ +  +  +  + ]:   102471608 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
     355                 :             : }
     356                 :             : 
     357                 :   102520758 : bool CNetAddr::IsRFC6052() const
     358                 :             : {
     359         [ +  + ]:   102520758 :     return IsIPv6() &&
     360         [ +  + ]:    36371368 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
     361                 :   102520758 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
     362                 :             : }
     363                 :             : 
     364                 :   107801526 : bool CNetAddr::IsRFC4380() const
     365                 :             : {
     366   [ +  +  +  + ]:   107801526 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
     367                 :             : }
     368                 :             : 
     369                 :   311135406 : bool CNetAddr::IsRFC4862() const
     370                 :             : {
     371   [ +  +  +  + ]:   311135406 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
     372                 :   311135406 :                                                                 0x00, 0x00, 0x00, 0x00});
     373                 :             : }
     374                 :             : 
     375                 :   310045079 : bool CNetAddr::IsRFC4193() const
     376                 :             : {
     377   [ +  +  +  +  :   399822978 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
                   +  + ]
     378                 :             : }
     379                 :             : 
     380                 :   102532851 : bool CNetAddr::IsRFC6145() const
     381                 :             : {
     382         [ +  + ]:   102532851 :     return IsIPv6() &&
     383         [ +  + ]:    36383461 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     384                 :   102532851 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
     385                 :             : }
     386                 :             : 
     387                 :   309853860 : bool CNetAddr::IsRFC4843() const
     388                 :             : {
     389   [ +  +  +  + ]:   309853860 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
     390   [ +  +  +  + ]:      240992 :            (m_addr[3] & 0xF0) == 0x10;
     391                 :             : }
     392                 :             : 
     393                 :   309843877 : bool CNetAddr::IsRFC7343() const
     394                 :             : {
     395   [ +  +  +  + ]:   309843877 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
     396   [ +  +  +  + ]:      221026 :            (m_addr[3] & 0xF0) == 0x20;
     397                 :             : }
     398                 :             : 
     399                 :     6177356 : bool CNetAddr::IsHeNet() const
     400                 :             : {
     401   [ +  -  +  + ]:     6177356 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
     402                 :             : }
     403                 :             : 
     404                 :   341779043 : bool CNetAddr::IsLocal() const
     405                 :             : {
     406                 :             :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
     407   [ +  +  +  +  :   405220300 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
             +  +  +  + ]
     408                 :             :         return true;
     409                 :             :     }
     410                 :             : 
     411                 :             :     // IPv6 loopback (::1/128)
     412                 :   340838762 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
     413   [ +  +  +  +  :   438760295 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
                   +  + ]
     414                 :       79116 :         return true;
     415                 :             :     }
     416                 :             : 
     417                 :             :     return false;
     418                 :             : }
     419                 :             : 
     420                 :             : /**
     421                 :             :  * @returns Whether or not this network address is a valid address that @a could
     422                 :             :  *          be used to refer to an actual host.
     423                 :             :  *
     424                 :             :  * @note A valid address may or may not be publicly routable on the global
     425                 :             :  *       internet. As in, the set of valid addresses is a superset of the set of
     426                 :             :  *       publicly routable addresses.
     427                 :             :  *
     428                 :             :  * @see CNetAddr::IsRoutable()
     429                 :             :  */
     430                 :   355285993 : bool CNetAddr::IsValid() const
     431                 :             : {
     432                 :             :     // unspecified IPv6 address (::/128)
     433                 :   355285993 :     unsigned char ipNone6[16] = {};
     434   [ +  +  +  +  :   469730048 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
                   +  + ]
     435                 :             :         return false;
     436                 :             :     }
     437                 :             : 
     438   [ +  +  +  + ]:   392614719 :     if (IsCJDNS() && !HasCJDNSPrefix()) {
     439                 :             :         return false;
     440                 :             :     }
     441                 :             : 
     442                 :             :     // documentation IPv6 address
     443   [ +  +  +  + ]:   338214710 :     if (IsRFC3849() || IsRFC9637())
     444                 :       17531 :         return false;
     445                 :             : 
     446         [ +  + ]:   338197179 :     if (IsInternal())
     447                 :             :         return false;
     448                 :             : 
     449         [ +  + ]:   336025900 :     if (IsIPv4()) {
     450   [ +  +  +  + ]:   128811910 :         const uint32_t addr = ReadBE32(m_addr.data());
     451         [ +  + ]:    64405955 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
     452                 :      630723 :             return false;
     453                 :             :         }
     454                 :             :     }
     455                 :             : 
     456                 :             :     return true;
     457                 :             : }
     458                 :             : 
     459                 :             : /**
     460                 :             :  * @returns Whether or not this network address is publicly routable on the
     461                 :             :  *          global internet.
     462                 :             :  *
     463                 :             :  * @note A routable address is always valid. As in, the set of routable addresses
     464                 :             :  *       is a subset of the set of valid addresses.
     465                 :             :  *
     466                 :             :  * @see CNetAddr::IsValid()
     467                 :             :  */
     468                 :   312799635 : bool CNetAddr::IsRoutable() const
     469                 :             : {
     470                 :   312799635 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
           [ +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  -  
                      + ]
     471                 :             : }
     472                 :             : 
     473                 :             : /**
     474                 :             :  * @returns Whether or not this is a dummy address that represents a name.
     475                 :             :  *
     476                 :             :  * @see CNetAddr::SetInternal(const std::string &)
     477                 :             :  */
     478                 :   848990217 : bool CNetAddr::IsInternal() const
     479                 :             : {
     480                 :   848990217 :    return m_net == NET_INTERNAL;
     481                 :             : }
     482                 :             : 
     483                 :    95133550 : bool CNetAddr::IsAddrV1Compatible() const
     484                 :             : {
     485   [ +  -  -  + ]:    95133550 :     switch (m_net) {
     486                 :             :     case NET_IPV4:
     487                 :             :     case NET_IPV6:
     488                 :             :     case NET_INTERNAL:
     489                 :             :         return true;
     490                 :    56274163 :     case NET_ONION:
     491                 :    56274163 :     case NET_I2P:
     492                 :    56274163 :     case NET_CJDNS:
     493                 :    56274163 :         return false;
     494                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     495                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
     496                 :           0 :         assert(false);
     497                 :             :     } // no default case, so the compiler can warn about missing cases
     498                 :             : 
     499                 :           0 :     assert(false);
     500                 :             : }
     501                 :             : 
     502                 :    67501057 : enum Network CNetAddr::GetNetwork() const
     503                 :             : {
     504         [ +  + ]:    67501057 :     if (IsInternal())
     505                 :             :         return NET_INTERNAL;
     506                 :             : 
     507         [ +  + ]:    65663335 :     if (!IsRoutable())
     508                 :             :         return NET_UNROUTABLE;
     509                 :             : 
     510                 :    64548002 :     return m_net;
     511                 :             : }
     512                 :             : 
     513                 :       65277 : static std::string IPv4ToString(std::span<const uint8_t> a)
     514                 :             : {
     515                 :       65277 :     return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
     516                 :             : }
     517                 :             : 
     518                 :             : // Return an IPv6 address text representation with zero compression as described in RFC 5952
     519                 :             : // ("A Recommendation for IPv6 Address Text Representation").
     520                 :      777382 : static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
     521                 :             : {
     522         [ -  + ]:      777382 :     assert(a.size() == ADDR_IPV6_SIZE);
     523                 :      777382 :     const std::array groups{
     524                 :      777382 :         ReadBE16(&a[0]),
     525                 :      777382 :         ReadBE16(&a[2]),
     526                 :      777382 :         ReadBE16(&a[4]),
     527                 :      777382 :         ReadBE16(&a[6]),
     528                 :      777382 :         ReadBE16(&a[8]),
     529                 :      777382 :         ReadBE16(&a[10]),
     530                 :      777382 :         ReadBE16(&a[12]),
     531                 :      777382 :         ReadBE16(&a[14]),
     532                 :      777382 :     };
     533                 :             : 
     534                 :             :     // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
     535                 :             :     // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
     536                 :      777382 :     struct ZeroSpan {
     537                 :             :         size_t start_index{0};
     538                 :             :         size_t len{0};
     539                 :             :     };
     540                 :             : 
     541                 :             :     // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
     542                 :             :     // zero sequences of equal length are found.
     543                 :      777382 :     ZeroSpan longest, current;
     544         [ +  + ]:     6996438 :     for (size_t i{0}; i < groups.size(); ++i) {
     545         [ +  + ]:     6219056 :         if (groups[i] != 0) {
     546                 :     5646278 :             current = {i + 1, 0};
     547                 :     5646278 :             continue;
     548                 :             :         }
     549                 :      572778 :         current.len += 1;
     550         [ +  + ]:      572778 :         if (current.len > longest.len) {
     551                 :      542767 :             longest = current;
     552                 :             :         }
     553                 :             :     }
     554                 :             : 
     555         [ +  - ]:      777382 :     std::string r;
     556         [ +  - ]:      777382 :     r.reserve(39);
     557         [ +  + ]:     6996438 :     for (size_t i{0}; i < groups.size(); ++i) {
     558                 :             :         // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
     559   [ +  +  +  +  :     6219056 :         if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
                   +  + ]
     560         [ +  + ]:      496942 :             if (i == longest.start_index) {
     561         [ +  - ]:       88752 :                 r += "::";
     562                 :             :             }
     563                 :      496942 :             continue;
     564                 :             :         }
     565   [ +  +  +  +  :    16475498 :         r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
                   +  - ]
     566                 :             :     }
     567                 :             : 
     568         [ +  + ]:      777382 :     if (scope_id != 0) {
     569         [ +  - ]:          28 :         r += strprintf("%%%u", scope_id);
     570                 :             :     }
     571                 :             : 
     572                 :      777382 :     return r;
     573                 :           0 : }
     574                 :             : 
     575                 :       37600 : std::string OnionToString(std::span<const uint8_t> addr)
     576                 :             : {
     577                 :       37600 :     uint8_t checksum[torv3::CHECKSUM_LEN];
     578                 :       37600 :     torv3::Checksum(addr, checksum);
     579                 :             :     // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
     580                 :       37600 :     prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
     581                 :       37600 :     address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
     582                 :       37600 :     address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
     583   [ -  +  +  - ]:      150400 :     return EncodeBase32(address) + ".onion";
     584                 :       37600 : }
     585                 :             : 
     586                 :      989437 : std::string CNetAddr::ToStringAddr() const
     587                 :             : {
     588   [ +  +  +  +  :      989437 :     switch (m_net) {
             +  +  -  - ]
     589                 :       65277 :     case NET_IPV4:
     590         [ +  - ]:      130554 :         return IPv4ToString(m_addr);
     591                 :      749900 :     case NET_IPV6:
     592         [ +  - ]:     1499800 :         return IPv6ToString(m_addr, m_scope_id);
     593                 :       37600 :     case NET_ONION:
     594         [ -  + ]:       75200 :         return OnionToString(m_addr);
     595                 :      103761 :     case NET_I2P:
     596   [ -  +  +  - ]:      311283 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
     597                 :       27482 :     case NET_CJDNS:
     598         [ +  - ]:       54964 :         return IPv6ToString(m_addr, 0);
     599                 :        5417 :     case NET_INTERNAL:
     600   [ +  -  +  - ]:       16251 :         return EncodeBase32(m_addr) + ".internal";
     601                 :           0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
     602                 :           0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
     603                 :           0 :         assert(false);
     604                 :             :     } // no default case, so the compiler can warn about missing cases
     605                 :             : 
     606                 :           0 :     assert(false);
     607                 :             : }
     608                 :             : 
     609                 :    64494142 : bool operator==(const CNetAddr& a, const CNetAddr& b)
     610                 :             : {
     611   [ +  +  +  + ]:    64494142 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
     612                 :             : }
     613                 :             : 
     614                 :    28820245 : bool operator<(const CNetAddr& a, const CNetAddr& b)
     615                 :             : {
     616                 :    28820245 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
     617                 :             : }
     618                 :             : 
     619                 :             : /**
     620                 :             :  * Try to get our IPv4 address.
     621                 :             :  *
     622                 :             :  * @param[out] pipv4Addr The in_addr struct to which to copy.
     623                 :             :  *
     624                 :             :  * @returns Whether or not the operation was successful, in particular, whether
     625                 :             :  *          or not our address was an IPv4 address.
     626                 :             :  *
     627                 :             :  * @see CNetAddr::IsIPv4()
     628                 :             :  */
     629                 :        7523 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
     630                 :             : {
     631         [ +  - ]:        7523 :     if (!IsIPv4())
     632                 :             :         return false;
     633   [ -  +  -  + ]:        7523 :     assert(sizeof(*pipv4Addr) == m_addr.size());
     634         [ +  - ]:       15046 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
     635                 :        7523 :     return true;
     636                 :             : }
     637                 :             : 
     638                 :             : /**
     639                 :             :  * Try to get our IPv6 (or CJDNS) address.
     640                 :             :  *
     641                 :             :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
     642                 :             :  *
     643                 :             :  * @returns Whether or not the operation was successful, in particular, whether
     644                 :             :  *          or not our address was an IPv6 address.
     645                 :             :  *
     646                 :             :  * @see CNetAddr::IsIPv6()
     647                 :             :  */
     648                 :       23043 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
     649                 :             : {
     650   [ +  +  +  - ]:       23043 :     if (!IsIPv6() && !IsCJDNS()) {
     651                 :             :         return false;
     652                 :             :     }
     653   [ -  +  -  + ]:       23043 :     assert(sizeof(*pipv6Addr) == m_addr.size());
     654         [ +  - ]:       46086 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
     655                 :       23043 :     return true;
     656                 :             : }
     657                 :             : 
     658                 :   118581599 : bool CNetAddr::HasLinkedIPv4() const
     659                 :             : {
     660                 :   118581599 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
           [ +  -  +  +  
          +  +  +  +  +  
                +  +  + ]
     661                 :             : }
     662                 :             : 
     663                 :     7219877 : uint32_t CNetAddr::GetLinkedIPv4() const
     664                 :             : {
     665         [ +  + ]:     7219877 :     if (IsIPv4()) {
     666         [ +  + ]:    14368376 :         return ReadBE32(m_addr.data());
     667   [ +  +  +  + ]:       35689 :     } else if (IsRFC6052() || IsRFC6145()) {
     668                 :             :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
     669         [ +  + ]:       37660 :         return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
     670         [ +  + ]:       16859 :     } else if (IsRFC3964()) {
     671                 :             :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
     672         [ +  + ]:       13638 :         return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
     673         [ +  - ]:       10040 :     } else if (IsRFC4380()) {
     674                 :             :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
     675         [ +  + ]:       20080 :         return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
     676                 :             :     }
     677                 :           0 :     assert(false);
     678                 :             : }
     679                 :             : 
     680                 :    86927813 : Network CNetAddr::GetNetClass() const
     681                 :             : {
     682                 :             :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
     683                 :             : 
     684                 :             :     // Check for "internal" first because such addresses are also !IsRoutable()
     685                 :             :     // and we don't want to return NET_UNROUTABLE in that case.
     686         [ +  + ]:    86927813 :     if (IsInternal()) {
     687                 :             :         return NET_INTERNAL;
     688                 :             :     }
     689         [ +  + ]:    83701619 :     if (!IsRoutable()) {
     690                 :             :         return NET_UNROUTABLE;
     691                 :             :     }
     692         [ +  + ]:    81479454 :     if (HasLinkedIPv4()) {
     693                 :             :         return NET_IPV4;
     694                 :             :     }
     695                 :    67170883 :     return m_net;
     696                 :             : }
     697                 :             : 
     698                 :    95118534 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
     699                 :             : {
     700         [ +  + ]:    95118534 :     if (IsAddrV1Compatible()) {
     701                 :    38848077 :         uint8_t serialized[V1_SERIALIZATION_SIZE];
     702                 :    38848077 :         SerializeV1Array(serialized);
     703                 :    38848077 :         return {std::begin(serialized), std::end(serialized)};
     704                 :             :     }
     705         [ +  + ]:   112540914 :     return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
     706                 :             : }
     707                 :             : 
     708                 :             : // private extensions to enum Network, only returned by GetExtNetwork,
     709                 :             : // and only used in GetReachabilityFrom
     710                 :             : static const int NET_TEREDO = NET_MAX;
     711                 :    10711334 : int static GetExtNetwork(const CNetAddr& addr)
     712                 :             : {
     713         [ +  + ]:    10711334 :     if (addr.IsRFC4380())
     714                 :             :         return NET_TEREDO;
     715                 :    10708991 :     return addr.GetNetwork();
     716                 :             : }
     717                 :             : 
     718                 :             : /** Calculates a metric for how reachable (*this) is from a given partner */
     719                 :     5355858 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
     720                 :             : {
     721                 :     5355858 :     enum Reachability {
     722                 :             :         REACH_UNREACHABLE,
     723                 :             :         REACH_DEFAULT,
     724                 :             :         REACH_TEREDO,
     725                 :             :         REACH_IPV6_WEAK,
     726                 :             :         REACH_IPV4,
     727                 :             :         REACH_IPV6_STRONG,
     728                 :             :         REACH_PRIVATE
     729                 :             :     };
     730                 :             : 
     731   [ +  +  -  + ]:     5355858 :     if (!IsRoutable() || IsInternal())
     732                 :         191 :         return REACH_UNREACHABLE;
     733                 :             : 
     734                 :     5355667 :     int ourNet = GetExtNetwork(*this);
     735                 :     5355667 :     int theirNet = GetExtNetwork(paddrPartner);
     736   [ +  +  +  +  :     5355667 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
                   +  + ]
     737                 :             : 
     738   [ +  +  +  +  :     5355667 :     switch(theirNet) {
                +  +  + ]
     739                 :     1940548 :     case NET_IPV4:
     740         [ +  + ]:     1940548 :         switch(ourNet) {
     741                 :             :         default:       return REACH_DEFAULT;
     742                 :      608390 :         case NET_IPV4: return REACH_IPV4;
     743                 :             :         }
     744                 :      804353 :     case NET_IPV6:
     745   [ +  +  +  + ]:      804353 :         switch(ourNet) {
     746                 :             :         default:         return REACH_DEFAULT;
     747                 :         354 :         case NET_TEREDO: return REACH_TEREDO;
     748                 :      286355 :         case NET_IPV4:   return REACH_IPV4;
     749         [ +  + ]:      400612 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
     750                 :             :         }
     751                 :        2569 :     case NET_ONION:
     752      [ -  +  + ]:        2569 :         switch(ourNet) {
     753                 :             :         default:         return REACH_DEFAULT;
     754                 :           0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
     755                 :        2567 :         case NET_ONION:    return REACH_PRIVATE;
     756                 :             :         }
     757                 :        3311 :     case NET_I2P:
     758         [ +  + ]:        3311 :         switch (ourNet) {
     759                 :             :         case NET_I2P: return REACH_PRIVATE;
     760                 :        1223 :         default: return REACH_DEFAULT;
     761                 :             :         }
     762                 :       46968 :     case NET_CJDNS:
     763         [ +  + ]:       46968 :         switch (ourNet) {
     764                 :             :         case NET_CJDNS: return REACH_PRIVATE;
     765                 :       46233 :         default: return REACH_DEFAULT;
     766                 :             :         }
     767                 :        1944 :     case NET_TEREDO:
     768   [ +  +  +  + ]:        1944 :         switch(ourNet) {
     769                 :             :         default:          return REACH_DEFAULT;
     770                 :           7 :         case NET_TEREDO:  return REACH_TEREDO;
     771                 :         747 :         case NET_IPV6:    return REACH_IPV6_WEAK;
     772                 :        1137 :         case NET_IPV4:    return REACH_IPV4;
     773                 :             :         }
     774                 :     2555974 :     case NET_UNROUTABLE:
     775                 :     2555974 :     default:
     776   [ +  +  +  +  :     2555974 :         switch(ourNet) {
                      + ]
     777                 :             :         default:          return REACH_DEFAULT;
     778                 :          36 :         case NET_TEREDO:  return REACH_TEREDO;
     779                 :     1574495 :         case NET_IPV6:    return REACH_IPV6_WEAK;
     780                 :      926504 :         case NET_IPV4:    return REACH_IPV4;
     781                 :          26 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
     782                 :             :         }
     783                 :             :     }
     784                 :             : }
     785                 :             : 
     786                 :    23242704 : CService::CService() : port(0)
     787                 :             : {
     788                 :    23242704 : }
     789                 :             : 
     790                 :    10095507 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
     791                 :             : {
     792                 :    10095507 : }
     793                 :             : 
     794                 :        5214 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
     795                 :             : {
     796                 :        5214 : }
     797                 :             : 
     798                 :        5533 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
     799                 :             : {
     800                 :        5533 : }
     801                 :             : 
     802         [ -  + ]:         432 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
     803                 :             : {
     804         [ -  + ]:         432 :     assert(addr.sin_family == AF_INET);
     805                 :         432 : }
     806                 :             : 
     807         [ -  + ]:        1192 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
     808                 :             : {
     809         [ -  + ]:        1192 :    assert(addr.sin6_family == AF_INET6);
     810                 :        1192 : }
     811                 :             : 
     812                 :        1854 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
     813                 :             : {
     814      [ +  +  + ]:        1854 :     switch (paddr->sa_family) {
     815                 :         434 :     case AF_INET:
     816         [ +  + ]:         434 :         if (addrlen != sizeof(struct sockaddr_in)) return false;
     817                 :         432 :         *this = CService(*(const struct sockaddr_in*)paddr);
     818                 :         432 :         return true;
     819                 :        1197 :     case AF_INET6:
     820         [ +  + ]:        1197 :         if (addrlen != sizeof(struct sockaddr_in6)) return false;
     821                 :        1192 :         *this = CService(*(const struct sockaddr_in6*)paddr);
     822                 :        1192 :         return true;
     823                 :             :     default:
     824                 :             :         return false;
     825                 :             :     }
     826                 :             : }
     827                 :             : 
     828                 :       30916 : sa_family_t CService::GetSAFamily() const
     829                 :             : {
     830      [ +  +  + ]:       30916 :     switch (m_net) {
     831                 :             :     case NET_IPV4:
     832                 :             :         return AF_INET;
     833                 :       22894 :     case NET_IPV6:
     834                 :       22894 :     case NET_CJDNS:
     835                 :       22894 :         return AF_INET6;
     836                 :         924 :     default:
     837                 :         924 :         return AF_UNSPEC;
     838                 :             :     }
     839                 :             : }
     840                 :             : 
     841                 :       98044 : uint16_t CService::GetPort() const
     842                 :             : {
     843                 :       98044 :     return port;
     844                 :             : }
     845                 :             : 
     846                 :    42802404 : bool operator==(const CService& a, const CService& b)
     847                 :             : {
     848   [ +  -  +  +  :    85604808 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
                   +  + ]
     849                 :             : }
     850                 :             : 
     851                 :    25279939 : bool operator<(const CService& a, const CService& b)
     852                 :             : {
     853                 :    73749960 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
           [ +  -  +  +  
          +  -  +  +  +  
             +  +  +  -  
                      - ]
     854                 :             : }
     855                 :             : 
     856                 :             : /**
     857                 :             :  * Obtain the IPv4/6 socket address this represents.
     858                 :             :  *
     859                 :             :  * @param[out] paddr The obtained socket address.
     860                 :             :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
     861                 :             :  *                        to by paddr. The value that's pointed to by this
     862                 :             :  *                        parameter might change after calling this function if
     863                 :             :  *                        the size of the corresponding address structure
     864                 :             :  *                        changed.
     865                 :             :  *
     866                 :             :  * @returns Whether or not the operation was successful.
     867                 :             :  */
     868                 :       33253 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
     869                 :             : {
     870         [ +  + ]:       33253 :     if (IsIPv4()) {
     871         [ +  - ]:        7386 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
     872                 :             :             return false;
     873                 :        7386 :         *addrlen = sizeof(struct sockaddr_in);
     874                 :        7386 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
     875                 :        7386 :         memset(paddrin, 0, *addrlen);
     876         [ +  - ]:        7386 :         if (!GetInAddr(&paddrin->sin_addr))
     877                 :             :             return false;
     878                 :        7386 :         paddrin->sin_family = AF_INET;
     879                 :        7386 :         paddrin->sin_port = htons(port);
     880                 :        7386 :         return true;
     881                 :             :     }
     882   [ +  +  +  + ]:       25867 :     if (IsIPv6() || IsCJDNS()) {
     883         [ +  - ]:       23011 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
     884                 :             :             return false;
     885                 :       23011 :         *addrlen = sizeof(struct sockaddr_in6);
     886                 :       23011 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
     887                 :       23011 :         memset(paddrin6, 0, *addrlen);
     888         [ +  - ]:       23011 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
     889                 :             :             return false;
     890                 :       23011 :         paddrin6->sin6_scope_id = m_scope_id;
     891                 :       23011 :         paddrin6->sin6_family = AF_INET6;
     892                 :       23011 :         paddrin6->sin6_port = htons(port);
     893                 :       23011 :         return true;
     894                 :             :     }
     895                 :             :     return false;
     896                 :             : }
     897                 :             : 
     898                 :             : /**
     899                 :             :  * @returns An identifier unique to this service's address and port number.
     900                 :             :  */
     901                 :    53839177 : std::vector<unsigned char> CService::GetKey() const
     902                 :             : {
     903                 :    53839177 :     auto key = GetAddrBytes();
     904         [ +  - ]:    53839177 :     key.push_back(port / 0x100); // most significant byte of our port
     905         [ +  - ]:    53839177 :     key.push_back(port & 0x0FF); // least significant byte of our port
     906                 :    53839177 :     return key;
     907                 :           0 : }
     908                 :             : 
     909                 :      153551 : std::string CService::ToStringAddrPort() const
     910                 :             : {
     911                 :      153551 :     const auto port_str = strprintf("%u", port);
     912                 :             : 
     913   [ +  +  +  +  :      153551 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
          +  +  +  -  +  
                      + ]
     914   [ +  -  +  - ]:      151305 :         return ToStringAddr() + ":" + port_str;
     915                 :             :     } else {
     916   [ +  -  +  -  :      309348 :         return "[" + ToStringAddr() + "]:" + port_str;
                   +  - ]
     917                 :             :     }
     918                 :      153551 : }
     919                 :             : 
     920                 :       96286 : CSubNet::CSubNet():
     921                 :       96286 :     valid(false)
     922                 :             : {
     923                 :       96286 :     memset(netmask, 0, sizeof(netmask));
     924                 :       96286 : }
     925                 :             : 
     926                 :       29275 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
     927                 :             : {
     928   [ +  +  +  + ]:       29275 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
     929   [ +  +  +  + ]:       26733 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
     930         [ +  + ]:       29275 :     if (!valid) {
     931                 :             :         return;
     932                 :             :     }
     933                 :             : 
     934         [ -  + ]:       19931 :     assert(mask <= sizeof(netmask) * 8);
     935                 :             : 
     936                 :       19931 :     network = addr;
     937                 :             : 
     938                 :       19931 :     uint8_t n = mask;
     939   [ -  +  +  + ]:      308323 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
     940                 :      288392 :         const uint8_t bits = n < 8 ? n : 8;
     941                 :      288392 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
     942         [ +  - ]:      288392 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
     943                 :      288392 :         n -= bits;
     944                 :             :     }
     945                 :             : }
     946                 :             : 
     947                 :             : /**
     948                 :             :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
     949                 :             :  *          the specified subnet mask is not a valid one, -1.
     950                 :             :  */
     951                 :    10319515 : static inline int NetmaskBits(uint8_t x)
     952                 :             : {
     953   [ +  +  +  +  :    10319515 :     switch(x) {
          +  +  +  +  +  
                      + ]
     954                 :             :     case 0x00: return 0;
     955                 :        2334 :     case 0x80: return 1;
     956                 :        1062 :     case 0xc0: return 2;
     957                 :        1353 :     case 0xe0: return 3;
     958                 :        1247 :     case 0xf0: return 4;
     959                 :         625 :     case 0xf8: return 5;
     960                 :         420 :     case 0xfc: return 6;
     961                 :         558 :     case 0xfe: return 7;
     962                 :    10309594 :     case 0xff: return 8;
     963                 :         429 :     default: return -1;
     964                 :             :     }
     965                 :             : }
     966                 :             : 
     967                 :        1588 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
     968                 :             : {
     969   [ +  +  +  +  :        1588 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
                   +  + ]
     970         [ +  + ]:        1588 :     if (!valid) {
     971                 :             :         return;
     972                 :             :     }
     973                 :             :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
     974                 :        1293 :     bool zeros_found = false;
     975   [ +  -  +  + ]:        5955 :     for (auto b : mask.m_addr) {
     976                 :        4303 :         const int num_bits = NetmaskBits(b);
     977   [ +  +  +  + ]:        4303 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
     978                 :         934 :             valid = false;
     979                 :         934 :             return;
     980                 :             :         }
     981         [ +  + ]:        3369 :         if (num_bits < 8) {
     982                 :        2093 :             zeros_found = true;
     983                 :             :         }
     984                 :             :     }
     985                 :             : 
     986   [ -  +  -  - ]:         359 :     assert(mask.m_addr.size() <= sizeof(netmask));
     987                 :             : 
     988         [ -  + ]:         359 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
     989                 :             : 
     990                 :         359 :     network = addr;
     991                 :             : 
     992                 :             :     // Normalize network according to netmask
     993   [ -  +  +  + ]:        2239 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
     994         [ +  - ]:        3760 :         network.m_addr[x] &= netmask[x];
     995                 :             :     }
     996                 :             : }
     997                 :             : 
     998                 :       36896 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
     999                 :             : {
    1000   [ +  +  -  + ]:       36896 :     switch (addr.m_net) {
    1001                 :       23337 :     case NET_IPV4:
    1002                 :       23337 :     case NET_IPV6:
    1003                 :       23337 :         valid = true;
    1004   [ -  +  -  - ]:       23337 :         assert(addr.m_addr.size() <= sizeof(netmask));
    1005         [ -  + ]:       23337 :         memset(netmask, 0xFF, addr.m_addr.size());
    1006                 :       23337 :         break;
    1007                 :        8860 :     case NET_ONION:
    1008                 :        8860 :     case NET_I2P:
    1009                 :        8860 :     case NET_CJDNS:
    1010                 :        8860 :         valid = true;
    1011                 :        8860 :         break;
    1012                 :             :     case NET_INTERNAL:
    1013                 :             :     case NET_UNROUTABLE:
    1014                 :             :     case NET_MAX:
    1015                 :             :         return;
    1016                 :             :     }
    1017                 :             : 
    1018                 :       32197 :     network = addr;
    1019                 :             : }
    1020                 :             : 
    1021                 :             : /**
    1022                 :             :  * @returns True if this subnet is valid, the specified address is valid, and
    1023                 :             :  *          the specified address belongs in this subnet.
    1024                 :             :  */
    1025                 :     2668422 : bool CSubNet::Match(const CNetAddr &addr) const
    1026                 :             : {
    1027   [ +  +  +  +  :     2668422 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
                   +  + ]
    1028                 :     2521308 :         return false;
    1029                 :             : 
    1030      [ +  +  - ]:      147114 :     switch (network.m_net) {
    1031                 :             :     case NET_IPV4:
    1032                 :             :     case NET_IPV6:
    1033                 :             :         break;
    1034                 :        3009 :     case NET_ONION:
    1035                 :        3009 :     case NET_I2P:
    1036                 :        3009 :     case NET_CJDNS:
    1037                 :        3009 :     case NET_INTERNAL:
    1038                 :        3009 :         return addr == network;
    1039                 :             :     case NET_UNROUTABLE:
    1040                 :             :     case NET_MAX:
    1041                 :             :         return false;
    1042                 :             :     }
    1043                 :             : 
    1044   [ -  +  -  +  :      144105 :     assert(network.m_addr.size() == addr.m_addr.size());
                   -  + ]
    1045         [ +  + ]:      308760 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
    1046   [ +  -  +  -  :      903402 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
                   +  + ]
    1047                 :             :             return false;
    1048                 :             :         }
    1049                 :             :     }
    1050                 :             :     return true;
    1051                 :             : }
    1052                 :             : 
    1053                 :      816957 : std::string CSubNet::ToString() const
    1054                 :             : {
    1055         [ +  + ]:      816957 :     std::string suffix;
    1056                 :             : 
    1057         [ +  + ]:      816957 :     switch (network.m_net) {
    1058                 :      676043 :     case NET_IPV4:
    1059                 :      676043 :     case NET_IPV6: {
    1060   [ -  +  -  - ]:      676043 :         assert(network.m_addr.size() <= sizeof(netmask));
    1061                 :             : 
    1062                 :      676043 :         uint8_t cidr = 0;
    1063                 :             : 
    1064   [ -  +  +  + ]:    10991255 :         for (size_t i = 0; i < network.m_addr.size(); ++i) {
    1065         [ +  + ]:    10324930 :             if (netmask[i] == 0x00) {
    1066                 :             :                 break;
    1067                 :             :             }
    1068                 :    10315212 :             cidr += NetmaskBits(netmask[i]);
    1069                 :             :         }
    1070                 :             : 
    1071         [ +  - ]:      676043 :         suffix = strprintf("/%u", cidr);
    1072                 :      676043 :         break;
    1073                 :             :     }
    1074                 :             :     case NET_ONION:
    1075                 :             :     case NET_I2P:
    1076                 :             :     case NET_CJDNS:
    1077                 :             :     case NET_INTERNAL:
    1078                 :             :     case NET_UNROUTABLE:
    1079                 :             :     case NET_MAX:
    1080                 :             :         break;
    1081                 :             :     }
    1082                 :             : 
    1083   [ +  -  +  - ]:     1633914 :     return network.ToStringAddr() + suffix;
    1084                 :      816957 : }
    1085                 :             : 
    1086                 :      929841 : bool CSubNet::IsValid() const
    1087                 :             : {
    1088                 :      929841 :     return valid;
    1089                 :             : }
    1090                 :             : 
    1091                 :        4771 : bool operator==(const CSubNet& a, const CSubNet& b)
    1092                 :             : {
    1093   [ +  -  +  -  :        4771 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
                   -  + ]
    1094                 :             : }
    1095                 :             : 
    1096                 :      349460 : bool operator<(const CSubNet& a, const CSubNet& b)
    1097                 :             : {
    1098   [ +  +  +  +  :      349460 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
                   +  + ]
    1099                 :             : }
        

Generated by: LCOV version 2.5.0-full