LCOV - code coverage report
Current view: top level - src - netaddress.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 616 741 83.1 %
Date: 2020-10-06 11:46:21 Functions: 78 80 97.5 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 0 -

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2020 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/asmap.h>
#      14                 :            : #include <util/strencodings.h>
#      15                 :            : #include <util/string.h>
#      16                 :            : 
#      17                 :            : #include <algorithm>
#      18                 :            : #include <array>
#      19                 :            : #include <cstdint>
#      20                 :            : #include <ios>
#      21                 :            : #include <iterator>
#      22                 :            : #include <tuple>
#      23                 :            : 
#      24                 :            : constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE;
#      25                 :            : constexpr size_t CNetAddr::MAX_ADDRV2_SIZE;
#      26                 :            : 
#      27                 :            : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
#      28                 :      70834 : {
#      29                 :      70834 :     switch (m_net) {
#      30                 :      70810 :     case NET_IPV4:
#      31                 :      70810 :         return BIP155Network::IPV4;
#      32                 :         20 :     case NET_IPV6:
#      33                 :         20 :         return BIP155Network::IPV6;
#      34                 :          4 :     case NET_ONION:
#      35                 :          4 :         switch (m_addr.size()) {
#      36                 :          2 :         case ADDR_TORV2_SIZE:
#      37                 :          2 :             return BIP155Network::TORV2;
#      38                 :          2 :         case ADDR_TORV3_SIZE:
#      39                 :          2 :             return BIP155Network::TORV3;
#      40                 :          0 :         default:
#      41                 :          0 :             assert(false);
#      42                 :          4 :         }
#      43                 :          0 :     case NET_I2P:
#      44                 :          0 :         return BIP155Network::I2P;
#      45                 :          0 :     case NET_CJDNS:
#      46                 :          0 :         return BIP155Network::CJDNS;
#      47                 :          0 :     case NET_INTERNAL:   // should have been handled before calling this function
#      48                 :          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#      49                 :          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#      50                 :          0 :         assert(false);
#      51                 :      70834 :     } // no default case, so the compiler can warn about missing cases
#      52                 :            : 
#      53                 :          0 :     assert(false);
#      54                 :          0 : }
#      55                 :            : 
#      56                 :            : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
#      57                 :       1108 : {
#      58                 :       1108 :     switch (possible_bip155_net) {
#      59                 :       1061 :     case BIP155Network::IPV4:
#      60                 :       1061 :         if (address_size == ADDR_IPV4_SIZE) {
#      61                 :       1059 :             m_net = NET_IPV4;
#      62                 :       1059 :             return true;
#      63                 :       1059 :         }
#      64                 :          2 :         throw std::ios_base::failure(
#      65                 :          2 :             strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
#      66                 :          2 :                       ADDR_IPV4_SIZE));
#      67                 :         26 :     case BIP155Network::IPV6:
#      68                 :         26 :         if (address_size == ADDR_IPV6_SIZE) {
#      69                 :         24 :             m_net = NET_IPV6;
#      70                 :         24 :             return true;
#      71                 :         24 :         }
#      72                 :          2 :         throw std::ios_base::failure(
#      73                 :          2 :             strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
#      74                 :          2 :                       ADDR_IPV6_SIZE));
#      75                 :          4 :     case BIP155Network::TORV2:
#      76                 :          4 :         if (address_size == ADDR_TORV2_SIZE) {
#      77                 :          2 :             m_net = NET_ONION;
#      78                 :          2 :             return true;
#      79                 :          2 :         }
#      80                 :          2 :         throw std::ios_base::failure(
#      81                 :          2 :             strprintf("BIP155 TORv2 address with length %u (should be %u)", address_size,
#      82                 :          2 :                       ADDR_TORV2_SIZE));
#      83                 :          4 :     case BIP155Network::TORV3:
#      84                 :          4 :         if (address_size == ADDR_TORV3_SIZE) {
#      85                 :          2 :             m_net = NET_ONION;
#      86                 :          2 :             return true;
#      87                 :          2 :         }
#      88                 :          2 :         throw std::ios_base::failure(
#      89                 :          2 :             strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
#      90                 :          2 :                       ADDR_TORV3_SIZE));
#      91                 :          4 :     case BIP155Network::I2P:
#      92                 :          4 :         if (address_size == ADDR_I2P_SIZE) {
#      93                 :          2 :             m_net = NET_I2P;
#      94                 :          2 :             return true;
#      95                 :          2 :         }
#      96                 :          2 :         throw std::ios_base::failure(
#      97                 :          2 :             strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
#      98                 :          2 :                       ADDR_I2P_SIZE));
#      99                 :          4 :     case BIP155Network::CJDNS:
#     100                 :          4 :         if (address_size == ADDR_CJDNS_SIZE) {
#     101                 :          2 :             m_net = NET_CJDNS;
#     102                 :          2 :             return true;
#     103                 :          2 :         }
#     104                 :          2 :         throw std::ios_base::failure(
#     105                 :          2 :             strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
#     106                 :          2 :                       ADDR_CJDNS_SIZE));
#     107                 :          5 :     }
#     108                 :            : 
#     109                 :            :     // Don't throw on addresses with unknown network ids (maybe from the future).
#     110                 :            :     // Instead silently drop them and have the unserialization code consume
#     111                 :            :     // subsequent ones which may be known to us.
#     112                 :          5 :     return false;
#     113                 :          5 : }
#     114                 :            : 
#     115                 :            : /**
#     116                 :            :  * Construct an unspecified IPv6 network address (::/128).
#     117                 :            :  *
#     118                 :            :  * @note This address is considered invalid by CNetAddr::IsValid()
#     119                 :            :  */
#     120                 :    1658824 : CNetAddr::CNetAddr() {}
#     121                 :            : 
#     122                 :            : void CNetAddr::SetIP(const CNetAddr& ipIn)
#     123                 :          2 : {
#     124                 :            :     // Size check.
#     125                 :          2 :     switch (ipIn.m_net) {
#     126                 :          0 :     case NET_IPV4:
#     127                 :          0 :         assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
#     128                 :          0 :         break;
#     129                 :          2 :     case NET_IPV6:
#     130                 :          2 :         assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
#     131                 :          2 :         break;
#     132                 :          0 :     case NET_ONION:
#     133                 :          0 :         assert(ipIn.m_addr.size() == ADDR_TORV2_SIZE || ipIn.m_addr.size() == ADDR_TORV3_SIZE);
#     134                 :          0 :         break;
#     135                 :          0 :     case NET_I2P:
#     136                 :          0 :         assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
#     137                 :          0 :         break;
#     138                 :          0 :     case NET_CJDNS:
#     139                 :          0 :         assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
#     140                 :          0 :         break;
#     141                 :          0 :     case NET_INTERNAL:
#     142                 :          0 :         assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
#     143                 :          0 :         break;
#     144                 :          0 :     case NET_UNROUTABLE:
#     145                 :          0 :     case NET_MAX:
#     146                 :          0 :         assert(false);
#     147                 :          2 :     } // no default case, so the compiler can warn about missing cases
#     148                 :            : 
#     149                 :          2 :     m_net = ipIn.m_net;
#     150                 :          2 :     m_addr = ipIn.m_addr;
#     151                 :          2 : }
#     152                 :            : 
#     153                 :            : void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
#     154                 :       3682 : {
#     155                 :       3682 :     assert(ipv6.size() == ADDR_IPV6_SIZE);
#     156                 :            : 
#     157                 :       3682 :     size_t skip{0};
#     158                 :            : 
#     159                 :       3682 :     if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
#     160                 :            :         // IPv4-in-IPv6
#     161                 :       1495 :         m_net = NET_IPV4;
#     162                 :       1495 :         skip = sizeof(IPV4_IN_IPV6_PREFIX);
#     163                 :       2187 :     } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
#     164                 :            :         // TORv2-in-IPv6
#     165                 :          8 :         m_net = NET_ONION;
#     166                 :          8 :         skip = sizeof(TORV2_IN_IPV6_PREFIX);
#     167                 :       2179 :     } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
#     168                 :            :         // Internal-in-IPv6
#     169                 :          2 :         m_net = NET_INTERNAL;
#     170                 :          2 :         skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
#     171                 :       2177 :     } else {
#     172                 :            :         // IPv6
#     173                 :       2177 :         m_net = NET_IPV6;
#     174                 :       2177 :     }
#     175                 :            : 
#     176                 :       3682 :     m_addr.assign(ipv6.begin() + skip, ipv6.end());
#     177                 :       3682 : }
#     178                 :            : 
#     179                 :            : /**
#     180                 :            :  * Create an "internal" address that represents a name or FQDN. CAddrMan uses
#     181                 :            :  * these fake addresses to keep track of which DNS seeds were used.
#     182                 :            :  * @returns Whether or not the operation was successful.
#     183                 :            :  * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
#     184                 :            :  */
#     185                 :            : bool CNetAddr::SetInternal(const std::string &name)
#     186                 :        438 : {
#     187                 :        438 :     if (name.empty()) {
#     188                 :          0 :         return false;
#     189                 :          0 :     }
#     190                 :        438 :     m_net = NET_INTERNAL;
#     191                 :        438 :     unsigned char hash[32] = {};
#     192                 :        438 :     CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
#     193                 :        438 :     m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
#     194                 :        438 :     return true;
#     195                 :        438 : }
#     196                 :            : 
#     197                 :            : namespace torv3 {
#     198                 :            : // https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt#n2135
#     199                 :            : static constexpr size_t CHECKSUM_LEN = 2;
#     200                 :            : static const unsigned char VERSION[] = {3};
#     201                 :            : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
#     202                 :            : 
#     203                 :            : static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
#     204                 :         14 : {
#     205                 :            :     // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
#     206                 :         14 :     static const unsigned char prefix[] = ".onion checksum";
#     207                 :         14 :     static constexpr size_t prefix_len = 15;
#     208                 :            : 
#     209                 :         14 :     SHA3_256 hasher;
#     210                 :            : 
#     211                 :         14 :     hasher.Write(MakeSpan(prefix).first(prefix_len));
#     212                 :         14 :     hasher.Write(addr_pubkey);
#     213                 :         14 :     hasher.Write(VERSION);
#     214                 :            : 
#     215                 :         14 :     uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
#     216                 :            : 
#     217                 :         14 :     hasher.Finalize(checksum_full);
#     218                 :            : 
#     219                 :         14 :     memcpy(checksum, checksum_full, sizeof(checksum));
#     220                 :         14 : }
#     221                 :            : 
#     222                 :            : }; // namespace torv3
#     223                 :            : 
#     224                 :            : /**
#     225                 :            :  * Parse a TOR address and set this object to it.
#     226                 :            :  *
#     227                 :            :  * @returns Whether or not the operation was successful.
#     228                 :            :  *
#     229                 :            :  * @see CNetAddr::IsTor()
#     230                 :            :  */
#     231                 :            : bool CNetAddr::SetSpecial(const std::string& str)
#     232                 :     305937 : {
#     233                 :     305937 :     static const char* suffix{".onion"};
#     234                 :     305937 :     static constexpr size_t suffix_len{6};
#     235                 :            : 
#     236                 :     305937 :     if (!ValidAsCString(str) || str.size() <= suffix_len ||
#     237                 :     305908 :         str.substr(str.size() - suffix_len) != suffix) {
#     238                 :     305908 :         return false;
#     239                 :     305908 :     }
#     240                 :            : 
#     241                 :         29 :     bool invalid;
#     242                 :         29 :     const auto& input = DecodeBase32(str.substr(0, str.size() - suffix_len).c_str(), &invalid);
#     243                 :            : 
#     244                 :         29 :     if (invalid) {
#     245                 :          2 :         return false;
#     246                 :          2 :     }
#     247                 :            : 
#     248                 :         27 :     switch (input.size()) {
#     249                 :         15 :     case ADDR_TORV2_SIZE:
#     250                 :         15 :         m_net = NET_ONION;
#     251                 :         15 :         m_addr.assign(input.begin(), input.end());
#     252                 :         15 :         return true;
#     253                 :         10 :     case torv3::TOTAL_LEN: {
#     254                 :         10 :         Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
#     255                 :         10 :         Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
#     256                 :         10 :         Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
#     257                 :            : 
#     258                 :         10 :         uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
#     259                 :         10 :         torv3::Checksum(input_pubkey, calculated_checksum);
#     260                 :            : 
#     261                 :         10 :         if (input_checksum != calculated_checksum || input_version != torv3::VERSION) {
#     262                 :          4 :             return false;
#     263                 :          4 :         }
#     264                 :            : 
#     265                 :          6 :         m_net = NET_ONION;
#     266                 :          6 :         m_addr.assign(input_pubkey.begin(), input_pubkey.end());
#     267                 :          6 :         return true;
#     268                 :          6 :     }
#     269                 :          2 :     }
#     270                 :            : 
#     271                 :          2 :     return false;
#     272                 :          2 : }
#     273                 :            : 
#     274                 :            : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
#     275                 :     306165 : {
#     276                 :     306165 :     m_net = NET_IPV4;
#     277                 :     306165 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
#     278                 :     306165 :     m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
#     279                 :     306165 : }
#     280                 :            : 
#     281                 :            : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
#     282                 :       1216 : {
#     283                 :       1216 :     SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
#     284                 :       1216 :     scopeId = scope;
#     285                 :       1216 : }
#     286                 :            : 
#     287                 :            : bool CNetAddr::IsBindAny() const
#     288                 :       1076 : {
#     289                 :       1076 :     if (!IsIPv4() && !IsIPv6()) {
#     290                 :          6 :         return false;
#     291                 :          6 :     }
#     292                 :       9056 :     return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
#     293                 :       1070 : }
#     294                 :            : 
#     295                 :    4914576 : bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
#     296                 :            : 
#     297                 :    4121186 : bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
#     298                 :            : 
#     299                 :            : bool CNetAddr::IsRFC1918() const
#     300                 :     514265 : {
#     301                 :     514265 :     return IsIPv4() && (
#     302                 :     514141 :         m_addr[0] == 10 ||
#     303                 :     513605 :         (m_addr[0] == 192 && m_addr[1] == 168) ||
#     304                 :     513603 :         (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
#     305                 :     514265 : }
#     306                 :            : 
#     307                 :            : bool CNetAddr::IsRFC2544() const
#     308                 :     513729 : {
#     309                 :     513729 :     return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
#     310                 :     513729 : }
#     311                 :            : 
#     312                 :            : bool CNetAddr::IsRFC3927() const
#     313                 :     513727 : {
#     314                 :     513727 :     return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
#     315                 :     513727 : }
#     316                 :            : 
#     317                 :            : bool CNetAddr::IsRFC6598() const
#     318                 :     513719 : {
#     319                 :     513719 :     return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
#     320                 :     513719 : }
#     321                 :            : 
#     322                 :            : bool CNetAddr::IsRFC5737() const
#     323                 :     513719 : {
#     324                 :     513719 :     return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
#     325                 :     513595 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
#     326                 :     513595 :                         HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
#     327                 :     513719 : }
#     328                 :            : 
#     329                 :            : bool CNetAddr::IsRFC3849() const
#     330                 :     707880 : {
#     331                 :     707880 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
#     332                 :     707880 : }
#     333                 :            : 
#     334                 :            : bool CNetAddr::IsRFC3964() const
#     335                 :         45 : {
#     336                 :         45 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
#     337                 :         45 : }
#     338                 :            : 
#     339                 :            : bool CNetAddr::IsRFC6052() const
#     340                 :         55 : {
#     341                 :         55 :     return IsIPv6() &&
#     342                 :         40 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
#     343                 :         40 :                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
#     344                 :         55 : }
#     345                 :            : 
#     346                 :            : bool CNetAddr::IsRFC4380() const
#     347                 :         37 : {
#     348                 :         37 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
#     349                 :         37 : }
#     350                 :            : 
#     351                 :            : bool CNetAddr::IsRFC4862() const
#     352                 :     513721 : {
#     353                 :     513721 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
#     354                 :         86 :                                                                 0x00, 0x00, 0x00, 0x00});
#     355                 :     513721 : }
#     356                 :            : 
#     357                 :            : bool CNetAddr::IsRFC4193() const
#     358                 :     513721 : {
#     359                 :     513721 :     return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
#     360                 :     513721 : }
#     361                 :            : 
#     362                 :            : bool CNetAddr::IsRFC6145() const
#     363                 :         57 : {
#     364                 :         57 :     return IsIPv6() &&
#     365                 :         42 :            HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
#     366                 :         42 :                                                      0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
#     367                 :         57 : }
#     368                 :            : 
#     369                 :            : bool CNetAddr::IsRFC4843() const
#     370                 :     513721 : {
#     371                 :     513721 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     372                 :         18 :            (m_addr[3] & 0xF0) == 0x10;
#     373                 :     513721 : }
#     374                 :            : 
#     375                 :            : bool CNetAddr::IsRFC7343() const
#     376                 :     513721 : {
#     377                 :     513721 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
#     378                 :         18 :            (m_addr[3] & 0xF0) == 0x20;
#     379                 :     513721 : }
#     380                 :            : 
#     381                 :            : bool CNetAddr::IsHeNet() const
#     382                 :          4 : {
#     383                 :          4 :     return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
#     384                 :          4 : }
#     385                 :            : 
#     386                 :            : /**
#     387                 :            :  * Check whether this object represents a TOR address.
#     388                 :            :  * @see CNetAddr::SetSpecial(const std::string &)
#     389                 :            :  */
#     390                 :       1480 : bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
#     391                 :            : 
#     392                 :            : /**
#     393                 :            :  * Check whether this object represents an I2P address.
#     394                 :            :  */
#     395                 :       1465 : bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
#     396                 :            : 
#     397                 :            : /**
#     398                 :            :  * Check whether this object represents a CJDNS address.
#     399                 :            :  */
#     400                 :          6 : bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
#     401                 :            : 
#     402                 :            : bool CNetAddr::IsLocal() const
#     403                 :     587565 : {
#     404                 :            :     // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
#     405                 :     587565 :     if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
#     406                 :      11061 :         return true;
#     407                 :      11061 :     }
#     408                 :            : 
#     409                 :            :     // IPv6 loopback (::1/128)
#     410                 :     576504 :     static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
#     411                 :     576504 :     if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
#     412                 :          4 :         return true;
#     413                 :          4 :     }
#     414                 :            : 
#     415                 :     576500 :     return false;
#     416                 :     576500 : }
#     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                 :            : bool CNetAddr::IsValid() const
#     429                 :     776057 : {
#     430                 :            :     // unspecified IPv6 address (::/128)
#     431                 :     776057 :     unsigned char ipNone6[16] = {};
#     432                 :     776057 :     if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
#     433                 :      68179 :         return false;
#     434                 :      68179 :     }
#     435                 :            : 
#     436                 :            :     // documentation IPv6 address
#     437                 :     707878 :     if (IsRFC3849())
#     438                 :          0 :         return false;
#     439                 :            : 
#     440                 :     707878 :     if (IsInternal())
#     441                 :          2 :         return false;
#     442                 :            : 
#     443                 :     707876 :     if (IsIPv4()) {
#     444                 :     707712 :         const uint32_t addr = ReadBE32(m_addr.data());
#     445                 :     707712 :         if (addr == INADDR_ANY || addr == INADDR_NONE) {
#     446                 :         11 :             return false;
#     447                 :         11 :         }
#     448                 :     707865 :     }
#     449                 :            : 
#     450                 :     707865 :     return true;
#     451                 :     707865 : }
#     452                 :            : 
#     453                 :            : /**
#     454                 :            :  * @returns Whether or not this network address is publicly routable on the
#     455                 :            :  *          global internet.
#     456                 :            :  *
#     457                 :            :  * @note A routable address is always valid. As in, the set of routable addresses
#     458                 :            :  *       is a subset of the set of valid addresses.
#     459                 :            :  *
#     460                 :            :  * @see CNetAddr::IsValid()
#     461                 :            :  */
#     462                 :            : bool CNetAddr::IsRoutable() const
#     463                 :     551171 : {
#     464                 :     551171 :     return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
#     465                 :     551171 : }
#     466                 :            : 
#     467                 :            : /**
#     468                 :            :  * @returns Whether or not this is a dummy address that represents a name.
#     469                 :            :  *
#     470                 :            :  * @see CNetAddr::SetInternal(const std::string &)
#     471                 :            :  */
#     472                 :            : bool CNetAddr::IsInternal() const
#     473                 :    1869418 : {
#     474                 :    1869418 :    return m_net == NET_INTERNAL;
#     475                 :    1869418 : }
#     476                 :            : 
#     477                 :            : bool CNetAddr::IsAddrV1Compatible() const
#     478                 :    1321399 : {
#     479                 :    1321399 :     switch (m_net) {
#     480                 :    1321387 :     case NET_IPV4:
#     481                 :    1321387 :     case NET_IPV6:
#     482                 :    1321387 :     case NET_INTERNAL:
#     483                 :    1321387 :         return true;
#     484                 :          8 :     case NET_ONION:
#     485                 :          8 :         return m_addr.size() == ADDR_TORV2_SIZE;
#     486                 :          4 :     case NET_I2P:
#     487                 :          4 :     case NET_CJDNS:
#     488                 :          4 :         return false;
#     489                 :          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     490                 :          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     491                 :          0 :         assert(false);
#     492                 :    1321399 :     }
#     493                 :            : 
#     494                 :          0 :     return false;
#     495                 :    1321399 : }
#     496                 :            : 
#     497                 :            : enum Network CNetAddr::GetNetwork() const
#     498                 :       4876 : {
#     499                 :       4876 :     if (IsInternal())
#     500                 :          2 :         return NET_INTERNAL;
#     501                 :            : 
#     502                 :       4874 :     if (!IsRoutable())
#     503                 :        504 :         return NET_UNROUTABLE;
#     504                 :            : 
#     505                 :       4370 :     return m_net;
#     506                 :       4370 : }
#     507                 :            : 
#     508                 :            : static std::string IPv6ToString(Span<const uint8_t> a)
#     509                 :          2 : {
#     510                 :          2 :     assert(a.size() == ADDR_IPV6_SIZE);
#     511                 :            :     // clang-format off
#     512                 :          2 :     return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
#     513                 :          2 :                      ReadBE16(&a[0]),
#     514                 :          2 :                      ReadBE16(&a[2]),
#     515                 :          2 :                      ReadBE16(&a[4]),
#     516                 :          2 :                      ReadBE16(&a[6]),
#     517                 :          2 :                      ReadBE16(&a[8]),
#     518                 :          2 :                      ReadBE16(&a[10]),
#     519                 :          2 :                      ReadBE16(&a[12]),
#     520                 :          2 :                      ReadBE16(&a[14]));
#     521                 :            :     // clang-format on
#     522                 :          2 : }
#     523                 :            : 
#     524                 :            : std::string CNetAddr::ToStringIP() const
#     525                 :     271177 : {
#     526                 :     271177 :     switch (m_net) {
#     527                 :     271156 :     case NET_IPV4:
#     528                 :     271156 :     case NET_IPV6: {
#     529                 :     271156 :         CService serv(*this, 0);
#     530                 :     271156 :         struct sockaddr_storage sockaddr;
#     531                 :     271156 :         socklen_t socklen = sizeof(sockaddr);
#     532                 :     271156 :         if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
#     533                 :     271156 :             char name[1025] = "";
#     534                 :     271156 :             if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name,
#     535                 :     271156 :                              sizeof(name), nullptr, 0, NI_NUMERICHOST))
#     536                 :     271157 :                 return std::string(name);
#     537                 :>1844*10^16 :         }
#     538                 :>1844*10^16 :         if (m_net == NET_IPV4) {
#     539                 :          0 :             return strprintf("%u.%u.%u.%u", m_addr[0], m_addr[1], m_addr[2], m_addr[3]);
#     540                 :          0 :         }
#     541                 :>1844*10^16 :         return IPv6ToString(m_addr);
#     542                 :>1844*10^16 :     }
#     543                 :         13 :     case NET_ONION:
#     544                 :         13 :         switch (m_addr.size()) {
#     545                 :          9 :         case ADDR_TORV2_SIZE:
#     546                 :          9 :             return EncodeBase32(m_addr) + ".onion";
#     547                 :          4 :         case ADDR_TORV3_SIZE: {
#     548                 :            : 
#     549                 :          4 :             uint8_t checksum[torv3::CHECKSUM_LEN];
#     550                 :          4 :             torv3::Checksum(m_addr, checksum);
#     551                 :            : 
#     552                 :            :             // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
#     553                 :          4 :             prevector<torv3::TOTAL_LEN, uint8_t> address{m_addr.begin(), m_addr.end()};
#     554                 :          4 :             address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
#     555                 :          4 :             address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
#     556                 :            : 
#     557                 :          4 :             return EncodeBase32(address) + ".onion";
#     558                 :          0 :         }
#     559                 :          0 :         default:
#     560                 :          0 :             assert(false);
#     561                 :         13 :         }
#     562                 :          2 :     case NET_I2P:
#     563                 :          2 :         return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
#     564                 :          2 :     case NET_CJDNS:
#     565                 :          2 :         return IPv6ToString(m_addr);
#     566                 :          4 :     case NET_INTERNAL:
#     567                 :          4 :         return EncodeBase32(m_addr) + ".internal";
#     568                 :          0 :     case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
#     569                 :          0 :     case NET_MAX:        // m_net is never and should not be set to NET_MAX
#     570                 :          0 :         assert(false);
#     571                 :     271177 :     } // no default case, so the compiler can warn about missing cases
#     572                 :            : 
#     573                 :          0 :     assert(false);
#     574                 :          0 : }
#     575                 :            : 
#     576                 :            : std::string CNetAddr::ToString() const
#     577                 :      24650 : {
#     578                 :      24650 :     return ToStringIP();
#     579                 :      24650 : }
#     580                 :            : 
#     581                 :            : bool operator==(const CNetAddr& a, const CNetAddr& b)
#     582                 :      31265 : {
#     583                 :      31265 :     return a.m_net == b.m_net && a.m_addr == b.m_addr;
#     584                 :      31265 : }
#     585                 :            : 
#     586                 :            : bool operator<(const CNetAddr& a, const CNetAddr& b)
#     587                 :    1359772 : {
#     588                 :    1359772 :     return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
#     589                 :    1359772 : }
#     590                 :            : 
#     591                 :            : /**
#     592                 :            :  * Try to get our IPv4 address.
#     593                 :            :  *
#     594                 :            :  * @param[out] pipv4Addr The in_addr struct to which to copy.
#     595                 :            :  *
#     596                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     597                 :            :  *          or not our address was an IPv4 address.
#     598                 :            :  *
#     599                 :            :  * @see CNetAddr::IsIPv4()
#     600                 :            :  */
#     601                 :            : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
#     602                 :     270650 : {
#     603                 :     270650 :     if (!IsIPv4())
#     604                 :          0 :         return false;
#     605                 :     270650 :     assert(sizeof(*pipv4Addr) == m_addr.size());
#     606                 :     270650 :     memcpy(pipv4Addr, m_addr.data(), m_addr.size());
#     607                 :     270650 :     return true;
#     608                 :     270650 : }
#     609                 :            : 
#     610                 :            : /**
#     611                 :            :  * Try to get our IPv6 address.
#     612                 :            :  *
#     613                 :            :  * @param[out] pipv6Addr The in6_addr struct to which to copy.
#     614                 :            :  *
#     615                 :            :  * @returns Whether or not the operation was successful, in particular, whether
#     616                 :            :  *          or not our address was an IPv6 address.
#     617                 :            :  *
#     618                 :            :  * @see CNetAddr::IsIPv6()
#     619                 :            :  */
#     620                 :            : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
#     621                 :       2022 : {
#     622                 :       2022 :     if (!IsIPv6()) {
#     623                 :          0 :         return false;
#     624                 :          0 :     }
#     625                 :       2022 :     assert(sizeof(*pipv6Addr) == m_addr.size());
#     626                 :       2022 :     memcpy(pipv6Addr, m_addr.data(), m_addr.size());
#     627                 :       2022 :     return true;
#     628                 :       2022 : }
#     629                 :            : 
#     630                 :            : bool CNetAddr::HasLinkedIPv4() const
#     631                 :     244218 : {
#     632                 :     244218 :     return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
#     633                 :     244218 : }
#     634                 :            : 
#     635                 :            : uint32_t CNetAddr::GetLinkedIPv4() const
#     636                 :      73844 : {
#     637                 :      73844 :     if (IsIPv4()) {
#     638                 :      73836 :         return ReadBE32(m_addr.data());
#     639                 :          8 :     } else if (IsRFC6052() || IsRFC6145()) {
#     640                 :            :         // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
#     641                 :          4 :         return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
#     642                 :          4 :     } else if (IsRFC3964()) {
#     643                 :            :         // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
#     644                 :          2 :         return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
#     645                 :          2 :     } else if (IsRFC4380()) {
#     646                 :            :         // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
#     647                 :          2 :         return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
#     648                 :          2 :     }
#     649                 :          0 :     assert(false);
#     650                 :          0 : }
#     651                 :            : 
#     652                 :            : uint32_t CNetAddr::GetNetClass() const
#     653                 :     201993 : {
#     654                 :            :     // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
#     655                 :            : 
#     656                 :            :     // Check for "internal" first because such addresses are also !IsRoutable()
#     657                 :            :     // and we don't want to return NET_UNROUTABLE in that case.
#     658                 :     201993 :     if (IsInternal()) {
#     659                 :          4 :         return NET_INTERNAL;
#     660                 :          4 :     }
#     661                 :     201989 :     if (!IsRoutable()) {
#     662                 :      31624 :         return NET_UNROUTABLE;
#     663                 :      31624 :     }
#     664                 :     170365 :     if (HasLinkedIPv4()) {
#     665                 :     170347 :         return NET_IPV4;
#     666                 :     170347 :     }
#     667                 :         18 :     return m_net;
#     668                 :         18 : }
#     669                 :            : 
#     670                 :     121017 : uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
#     671                 :     121017 :     uint32_t net_class = GetNetClass();
#     672                 :     121017 :     if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
#     673                 :     108195 :         return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
#     674                 :     108195 :     }
#     675                 :      12822 :     std::vector<bool> ip_bits(128);
#     676                 :      12822 :     if (HasLinkedIPv4()) {
#     677                 :            :         // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
#     678                 :     166686 :         for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
#     679                 :    1384776 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     680                 :    1230912 :                 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
#     681                 :    1230912 :             }
#     682                 :     153864 :         }
#     683                 :      12822 :         uint32_t ipv4 = GetLinkedIPv4();
#     684                 :     423126 :         for (int i = 0; i < 32; ++i) {
#     685                 :     410304 :             ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
#     686                 :     410304 :         }
#     687                 :          0 :     } else {
#     688                 :            :         // Use all 128 bits of the IPv6 address otherwise
#     689                 :          0 :         assert(IsIPv6());
#     690                 :          0 :         for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
#     691                 :          0 :             uint8_t cur_byte = m_addr[byte_i];
#     692                 :          0 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
#     693                 :          0 :                 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
#     694                 :          0 :             }
#     695                 :          0 :         }
#     696                 :          0 :     }
#     697                 :      12822 :     uint32_t mapped_as = Interpret(asmap, ip_bits);
#     698                 :      12822 :     return mapped_as;
#     699                 :      12822 : }
#     700                 :            : 
#     701                 :            : /**
#     702                 :            :  * Get the canonical identifier of our network group
#     703                 :            :  *
#     704                 :            :  * The groups are assigned in a way where it should be costly for an attacker to
#     705                 :            :  * obtain addresses with many different group identifiers, even if it is cheap
#     706                 :            :  * to obtain addresses with the same identifier.
#     707                 :            :  *
#     708                 :            :  * @note No two connections will be attempted to addresses with the same network
#     709                 :            :  *       group.
#     710                 :            :  */
#     711                 :            : std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
#     712                 :      80976 : {
#     713                 :      80976 :     std::vector<unsigned char> vchRet;
#     714                 :      80976 :     uint32_t net_class = GetNetClass();
#     715                 :            :     // If non-empty asmap is supplied and the address is IPv4/IPv6,
#     716                 :            :     // return ASN to be used for bucketing.
#     717                 :      80976 :     uint32_t asn = GetMappedAS(asmap);
#     718                 :      80976 :     if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
#     719                 :       7246 :         vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
#     720                 :      36230 :         for (int i = 0; i < 4; i++) {
#     721                 :      28984 :             vchRet.push_back((asn >> (8 * i)) & 0xFF);
#     722                 :      28984 :         }
#     723                 :       7246 :         return vchRet;
#     724                 :       7246 :     }
#     725                 :            : 
#     726                 :      73730 :     vchRet.push_back(net_class);
#     727                 :      73730 :     int nBits{0};
#     728                 :            : 
#     729                 :      73730 :     if (IsLocal()) {
#     730                 :            :         // all local addresses belong to the same group
#     731                 :      72985 :     } else if (IsInternal()) {
#     732                 :            :         // all internal-usage addresses get their own group
#     733                 :          2 :         nBits = ADDR_INTERNAL_SIZE * 8;
#     734                 :      72983 :     } else if (!IsRoutable()) {
#     735                 :            :         // all other unroutable addresses belong to the same group
#     736                 :      61031 :     } else if (HasLinkedIPv4()) {
#     737                 :            :         // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
#     738                 :      61022 :         uint32_t ipv4 = GetLinkedIPv4();
#     739                 :      61022 :         vchRet.push_back((ipv4 >> 24) & 0xFF);
#     740                 :      61022 :         vchRet.push_back((ipv4 >> 16) & 0xFF);
#     741                 :      61022 :         return vchRet;
#     742                 :          9 :     } else if (IsTor() || IsI2P() || IsCJDNS()) {
#     743                 :          5 :         nBits = 4;
#     744                 :          4 :     } else if (IsHeNet()) {
#     745                 :            :         // for he.net, use /36 groups
#     746                 :          2 :         nBits = 36;
#     747                 :          2 :     } else {
#     748                 :            :         // for the rest of the IPv6 network, use /32 groups
#     749                 :          2 :         nBits = 32;
#     750                 :          2 :     }
#     751                 :            : 
#     752                 :            :     // Push our address onto vchRet.
#     753                 :      12708 :     const size_t num_bytes = nBits / 8;
#     754                 :      12708 :     vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
#     755                 :      12708 :     nBits %= 8;
#     756                 :            :     // ...for the last byte, push nBits and for the rest of the byte push 1's
#     757                 :      12708 :     if (nBits > 0) {
#     758                 :          7 :         assert(num_bytes < m_addr.size());
#     759                 :          7 :         vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
#     760                 :          7 :     }
#     761                 :            : 
#     762                 :      12708 :     return vchRet;
#     763                 :      73730 : }
#     764                 :            : 
#     765                 :            : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
#     766                 :    1315332 : {
#     767                 :    1315332 :     if (IsAddrV1Compatible()) {
#     768                 :    1315332 :         uint8_t serialized[V1_SERIALIZATION_SIZE];
#     769                 :    1315332 :         SerializeV1Array(serialized);
#     770                 :    1315332 :         return {std::begin(serialized), std::end(serialized)};
#     771                 :    1315332 :     }
#     772                 :          0 :     return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
#     773                 :          0 : }
#     774                 :            : 
#     775                 :            : uint64_t CNetAddr::GetHash() const
#     776                 :         21 : {
#     777                 :         21 :     uint256 hash = Hash(m_addr);
#     778                 :         21 :     uint64_t nRet;
#     779                 :         21 :     memcpy(&nRet, &hash, sizeof(nRet));
#     780                 :         21 :     return nRet;
#     781                 :         21 : }
#     782                 :            : 
#     783                 :            : // private extensions to enum Network, only returned by GetExtNetwork,
#     784                 :            : // and only used in GetReachabilityFrom
#     785                 :            : static const int NET_UNKNOWN = NET_MAX + 0;
#     786                 :            : static const int NET_TEREDO  = NET_MAX + 1;
#     787                 :            : int static GetExtNetwork(const CNetAddr *addr)
#     788                 :          0 : {
#     789                 :          0 :     if (addr == nullptr)
#     790                 :          0 :         return NET_UNKNOWN;
#     791                 :          0 :     if (addr->IsRFC4380())
#     792                 :          0 :         return NET_TEREDO;
#     793                 :          0 :     return addr->GetNetwork();
#     794                 :          0 : }
#     795                 :            : 
#     796                 :            : /** Calculates a metric for how reachable (*this) is from a given partner */
#     797                 :            : int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
#     798                 :          2 : {
#     799                 :          2 :     enum Reachability {
#     800                 :          2 :         REACH_UNREACHABLE,
#     801                 :          2 :         REACH_DEFAULT,
#     802                 :          2 :         REACH_TEREDO,
#     803                 :          2 :         REACH_IPV6_WEAK,
#     804                 :          2 :         REACH_IPV4,
#     805                 :          2 :         REACH_IPV6_STRONG,
#     806                 :          2 :         REACH_PRIVATE
#     807                 :          2 :     };
#     808                 :            : 
#     809                 :          2 :     if (!IsRoutable() || IsInternal())
#     810                 :          2 :         return REACH_UNREACHABLE;
#     811                 :            : 
#     812                 :          0 :     int ourNet = GetExtNetwork(this);
#     813                 :          0 :     int theirNet = GetExtNetwork(paddrPartner);
#     814                 :          0 :     bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
#     815                 :            : 
#     816                 :          0 :     switch(theirNet) {
#     817                 :          0 :     case NET_IPV4:
#     818                 :          0 :         switch(ourNet) {
#     819                 :          0 :         default:       return REACH_DEFAULT;
#     820                 :          0 :         case NET_IPV4: return REACH_IPV4;
#     821                 :          0 :         }
#     822                 :          0 :     case NET_IPV6:
#     823                 :          0 :         switch(ourNet) {
#     824                 :          0 :         default:         return REACH_DEFAULT;
#     825                 :          0 :         case NET_TEREDO: return REACH_TEREDO;
#     826                 :          0 :         case NET_IPV4:   return REACH_IPV4;
#     827                 :          0 :         case NET_IPV6:   return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
#     828                 :          0 :         }
#     829                 :          0 :     case NET_ONION:
#     830                 :          0 :         switch(ourNet) {
#     831                 :          0 :         default:         return REACH_DEFAULT;
#     832                 :          0 :         case NET_IPV4:   return REACH_IPV4; // Tor users can connect to IPv4 as well
#     833                 :          0 :         case NET_ONION:    return REACH_PRIVATE;
#     834                 :          0 :         }
#     835                 :          0 :     case NET_TEREDO:
#     836                 :          0 :         switch(ourNet) {
#     837                 :          0 :         default:          return REACH_DEFAULT;
#     838                 :          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     839                 :          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     840                 :          0 :         case NET_IPV4:    return REACH_IPV4;
#     841                 :          0 :         }
#     842                 :          0 :     case NET_UNKNOWN:
#     843                 :          0 :     case NET_UNROUTABLE:
#     844                 :          0 :     default:
#     845                 :          0 :         switch(ourNet) {
#     846                 :          0 :         default:          return REACH_DEFAULT;
#     847                 :          0 :         case NET_TEREDO:  return REACH_TEREDO;
#     848                 :          0 :         case NET_IPV6:    return REACH_IPV6_WEAK;
#     849                 :          0 :         case NET_IPV4:    return REACH_IPV4;
#     850                 :          0 :         case NET_ONION:     return REACH_PRIVATE; // either from Tor, or don't care about our address
#     851                 :          0 :         }
#     852                 :          0 :     }
#     853                 :          0 : }
#     854                 :            : 
#     855                 :            : CService::CService() : port(0)
#     856                 :     924151 : {
#     857                 :     924151 : }
#     858                 :            : 
#     859                 :            : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
#     860                 :     562432 : {
#     861                 :     562432 : }
#     862                 :            : 
#     863                 :            : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
#     864                 :          5 : {
#     865                 :          5 : }
#     866                 :            : 
#     867                 :            : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
#     868                 :          3 : {
#     869                 :          3 : }
#     870                 :            : 
#     871                 :            : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
#     872                 :       1456 : {
#     873                 :       1456 :     assert(addr.sin_family == AF_INET);
#     874                 :       1456 : }
#     875                 :            : 
#     876                 :            : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
#     877                 :          0 : {
#     878                 :          0 :    assert(addr.sin6_family == AF_INET6);
#     879                 :          0 : }
#     880                 :            : 
#     881                 :            : bool CService::SetSockAddr(const struct sockaddr *paddr)
#     882                 :       1456 : {
#     883                 :       1456 :     switch (paddr->sa_family) {
#     884                 :       1456 :     case AF_INET:
#     885                 :       1456 :         *this = CService(*(const struct sockaddr_in*)paddr);
#     886                 :       1456 :         return true;
#     887                 :          0 :     case AF_INET6:
#     888                 :          0 :         *this = CService(*(const struct sockaddr_in6*)paddr);
#     889                 :          0 :         return true;
#     890                 :          0 :     default:
#     891                 :          0 :         return false;
#     892                 :       1456 :     }
#     893                 :       1456 : }
#     894                 :            : 
#     895                 :            : uint16_t CService::GetPort() const
#     896                 :      26823 : {
#     897                 :      26823 :     return port;
#     898                 :      26823 : }
#     899                 :            : 
#     900                 :            : bool operator==(const CService& a, const CService& b)
#     901                 :       1009 : {
#     902                 :       1009 :     return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
#     903                 :       1009 : }
#     904                 :            : 
#     905                 :            : bool operator<(const CService& a, const CService& b)
#     906                 :       6502 : {
#     907                 :       6502 :     return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
#     908                 :       6502 : }
#     909                 :            : 
#     910                 :            : /**
#     911                 :            :  * Obtain the IPv4/6 socket address this represents.
#     912                 :            :  *
#     913                 :            :  * @param[out] paddr The obtained socket address.
#     914                 :            :  * @param[in,out] addrlen The size, in bytes, of the address structure pointed
#     915                 :            :  *                        to by paddr. The value that's pointed to by this
#     916                 :            :  *                        parameter might change after calling this function if
#     917                 :            :  *                        the size of the corresponding address structure
#     918                 :            :  *                        changed.
#     919                 :            :  *
#     920                 :            :  * @returns Whether or not the operation was successful.
#     921                 :            :  */
#     922                 :            : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
#     923                 :     272672 : {
#     924                 :     272672 :     if (IsIPv4()) {
#     925                 :     270650 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
#     926                 :          0 :             return false;
#     927                 :     270650 :         *addrlen = sizeof(struct sockaddr_in);
#     928                 :     270650 :         struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
#     929                 :     270650 :         memset(paddrin, 0, *addrlen);
#     930                 :     270650 :         if (!GetInAddr(&paddrin->sin_addr))
#     931                 :          0 :             return false;
#     932                 :     270650 :         paddrin->sin_family = AF_INET;
#     933                 :     270650 :         paddrin->sin_port = htons(port);
#     934                 :     270650 :         return true;
#     935                 :     270650 :     }
#     936                 :       2022 :     if (IsIPv6()) {
#     937                 :       2022 :         if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
#     938                 :          0 :             return false;
#     939                 :       2022 :         *addrlen = sizeof(struct sockaddr_in6);
#     940                 :       2022 :         struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
#     941                 :       2022 :         memset(paddrin6, 0, *addrlen);
#     942                 :       2022 :         if (!GetIn6Addr(&paddrin6->sin6_addr))
#     943                 :          0 :             return false;
#     944                 :       2022 :         paddrin6->sin6_scope_id = scopeId;
#     945                 :       2022 :         paddrin6->sin6_family = AF_INET6;
#     946                 :       2022 :         paddrin6->sin6_port = htons(port);
#     947                 :       2022 :         return true;
#     948                 :       2022 :     }
#     949                 :          0 :     return false;
#     950                 :          0 : }
#     951                 :            : 
#     952                 :            : /**
#     953                 :            :  * @returns An identifier unique to this service's address and port number.
#     954                 :            :  */
#     955                 :            : std::vector<unsigned char> CService::GetKey() const
#     956                 :    1286059 : {
#     957                 :    1286059 :     auto key = GetAddrBytes();
#     958                 :    1286059 :     key.push_back(port / 0x100); // most significant byte of our port
#     959                 :    1286059 :     key.push_back(port & 0x0FF); // least significant byte of our port
#     960                 :    1286059 :     return key;
#     961                 :    1286059 : }
#     962                 :            : 
#     963                 :            : std::string CService::ToStringPort() const
#     964                 :     186209 : {
#     965                 :     186209 :     return strprintf("%u", port);
#     966                 :     186209 : }
#     967                 :            : 
#     968                 :            : std::string CService::ToStringIPPort() const
#     969                 :     186209 : {
#     970                 :     186209 :     if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
#     971                 :     184750 :         return ToStringIP() + ":" + ToStringPort();
#     972                 :       1459 :     } else {
#     973                 :       1459 :         return "[" + ToStringIP() + "]:" + ToStringPort();
#     974                 :       1459 :     }
#     975                 :     186209 : }
#     976                 :            : 
#     977                 :            : std::string CService::ToString() const
#     978                 :     181235 : {
#     979                 :     181235 :     return ToStringIPPort();
#     980                 :     181235 : }
#     981                 :            : 
#     982                 :            : CSubNet::CSubNet():
#     983                 :            :     valid(false)
#     984                 :       1677 : {
#     985                 :       1677 :     memset(netmask, 0, sizeof(netmask));
#     986                 :       1677 : }
#     987                 :            : 
#     988                 :            : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
#     989                 :        604 : {
#     990                 :        604 :     valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
#     991                 :         24 :             (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
#     992                 :        604 :     if (!valid) {
#     993                 :          5 :         return;
#     994                 :          5 :     }
#     995                 :            : 
#     996                 :        599 :     assert(mask <= sizeof(netmask) * 8);
#     997                 :            : 
#     998                 :        599 :     network = addr;
#     999                 :            : 
#    1000                 :        599 :     uint8_t n = mask;
#    1001                 :       3223 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1002                 :       1851 :         const uint8_t bits = n < 8 ? n : 8;
#    1003                 :       2624 :         netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
#    1004                 :       2624 :         network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
#    1005                 :       2624 :         n -= bits;
#    1006                 :       2624 :     }
#    1007                 :        599 : }
#    1008                 :            : 
#    1009                 :            : /**
#    1010                 :            :  * @returns The number of 1-bits in the prefix of the specified subnet mask. If
#    1011                 :            :  *          the specified subnet mask is not a valid one, -1.
#    1012                 :            :  */
#    1013                 :            : static inline int NetmaskBits(uint8_t x)
#    1014                 :       9893 : {
#    1015                 :       9893 :     switch(x) {
#    1016                 :        216 :     case 0x00: return 0;
#    1017                 :         16 :     case 0x80: return 1;
#    1018                 :         16 :     case 0xc0: return 2;
#    1019                 :         19 :     case 0xe0: return 3;
#    1020                 :         16 :     case 0xf0: return 4;
#    1021                 :         16 :     case 0xf8: return 5;
#    1022                 :         20 :     case 0xfc: return 6;
#    1023                 :         18 :     case 0xfe: return 7;
#    1024                 :       9552 :     case 0xff: return 8;
#    1025                 :          4 :     default: return -1;
#    1026                 :       9893 :     }
#    1027                 :       9893 : }
#    1028                 :            : 
#    1029                 :            : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
#    1030                 :        100 : {
#    1031                 :        100 :     valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
#    1032                 :        100 :     if (!valid) {
#    1033                 :          6 :         return;
#    1034                 :          6 :     }
#    1035                 :            :     // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
#    1036                 :         94 :     bool zeros_found = false;
#    1037                 :        476 :     for (auto b : mask.m_addr) {
#    1038                 :        476 :         const int num_bits = NetmaskBits(b);
#    1039                 :        476 :         if (num_bits == -1 || (zeros_found && num_bits != 0)) {
#    1040                 :          8 :             valid = false;
#    1041                 :          8 :             return;
#    1042                 :          8 :         }
#    1043                 :        468 :         if (num_bits < 8) {
#    1044                 :        276 :             zeros_found = true;
#    1045                 :        276 :         }
#    1046                 :        468 :     }
#    1047                 :            : 
#    1048                 :         86 :     assert(mask.m_addr.size() <= sizeof(netmask));
#    1049                 :            : 
#    1050                 :         86 :     memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
#    1051                 :            : 
#    1052                 :         86 :     network = addr;
#    1053                 :            : 
#    1054                 :            :     // Normalize network according to netmask
#    1055                 :        526 :     for (size_t x = 0; x < network.m_addr.size(); ++x) {
#    1056                 :        440 :         network.m_addr[x] &= netmask[x];
#    1057                 :        440 :     }
#    1058                 :         86 : }
#    1059                 :            : 
#    1060                 :            : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
#    1061                 :        643 : {
#    1062                 :        643 :     valid = addr.IsIPv4() || addr.IsIPv6();
#    1063                 :        643 :     if (!valid) {
#    1064                 :          2 :         return;
#    1065                 :          2 :     }
#    1066                 :            : 
#    1067                 :        641 :     assert(addr.m_addr.size() <= sizeof(netmask));
#    1068                 :            : 
#    1069                 :        641 :     memset(netmask, 0xFF, addr.m_addr.size());
#    1070                 :            : 
#    1071                 :        641 :     network = addr;
#    1072                 :        641 : }
#    1073                 :            : 
#    1074                 :            : /**
#    1075                 :            :  * @returns True if this subnet is valid, the specified address is valid, and
#    1076                 :            :  *          the specified address belongs in this subnet.
#    1077                 :            :  */
#    1078                 :            : bool CSubNet::Match(const CNetAddr &addr) const
#    1079                 :      85607 : {
#    1080                 :      85607 :     if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
#    1081                 :         16 :         return false;
#    1082                 :      85591 :     assert(network.m_addr.size() == addr.m_addr.size());
#    1083                 :     428065 :     for (size_t x = 0; x < addr.m_addr.size(); ++x) {
#    1084                 :     342488 :         if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
#    1085                 :         14 :             return false;
#    1086                 :         14 :         }
#    1087                 :     342488 :     }
#    1088                 :      85577 :     return true;
#    1089                 :      85591 : }
#    1090                 :            : 
#    1091                 :            : std::string CSubNet::ToString() const
#    1092                 :       1177 : {
#    1093                 :       1177 :     assert(network.m_addr.size() <= sizeof(netmask));
#    1094                 :            : 
#    1095                 :       1177 :     uint8_t cidr = 0;
#    1096                 :            : 
#    1097                 :      10594 :     for (size_t i = 0; i < network.m_addr.size(); ++i) {
#    1098                 :      10024 :         if (netmask[i] == 0x00) {
#    1099                 :        607 :             break;
#    1100                 :        607 :         }
#    1101                 :       9417 :         cidr += NetmaskBits(netmask[i]);
#    1102                 :       9417 :     }
#    1103                 :            : 
#    1104                 :       1177 :     return network.ToString() + strprintf("/%u", cidr);
#    1105                 :       1177 : }
#    1106                 :            : 
#    1107                 :            : bool CSubNet::IsValid() const
#    1108                 :        353 : {
#    1109                 :        353 :     return valid;
#    1110                 :        353 : }
#    1111                 :            : 
#    1112                 :            : bool operator==(const CSubNet& a, const CSubNet& b)
#    1113                 :          4 : {
#    1114                 :          4 :     return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
#    1115                 :          4 : }
#    1116                 :            : 
#    1117                 :            : bool operator<(const CSubNet& a, const CSubNet& b)
#    1118                 :        103 : {
#    1119                 :        103 :     return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
#    1120                 :        103 : }
#    1121                 :            : 
#    1122                 :            : bool SanityCheckASMap(const std::vector<bool>& asmap)
#    1123                 :          5 : {
#    1124                 :          5 :     return SanityCheckASMap(asmap, 128); // For IP address lookups, the input is 128 bits
#    1125                 :          5 : }

Generated by: LCOV version 1.14