LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 476 557 85.5 %
Date: 2024-05-24 08:22:33 Functions: 66 75 88.0 %
Branches: 481 668 72.0 %

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

Generated by: LCOV version 1.16