LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 88.7 % 521 462
Test Date: 2024-08-28 04:44:32 Functions: 95.8 % 72 69
Branches: 71.3 % 663 473

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

Generated by: LCOV version 2.0-1