LCOV - code coverage report
Current view: top level - src - netgroup.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 83.7 % 86 72
Test Date: 2024-09-01 05:20:30 Functions: 60.0 % 5 3
Branches: 61.3 % 119 73

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <netgroup.h>
       6                 :             : 
       7                 :             : #include <hash.h>
       8                 :             : #include <logging.h>
       9                 :             : #include <util/asmap.h>
      10                 :             : 
      11                 :        8129 : uint256 NetGroupManager::GetAsmapChecksum() const
      12                 :             : {
      13         [ +  + ]:        8129 :     if (!m_asmap.size()) return {};
      14                 :             : 
      15                 :        3059 :     return (HashWriter{} << m_asmap).GetHash();
      16                 :        8129 : }
      17                 :             : 
      18                 :    65749737 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
      19                 :             : {
      20                 :    65749737 :     std::vector<unsigned char> vchRet;
      21                 :             :     // If non-empty asmap is supplied and the address is IPv4/IPv6,
      22                 :             :     // return ASN to be used for bucketing.
      23         [ +  - ]:    65749737 :     uint32_t asn = GetMappedAS(address);
      24         [ +  + ]:    65749737 :     if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
      25         [ +  - ]:     9110075 :         vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
      26         [ +  + ]:    45550375 :         for (int i = 0; i < 4; i++) {
      27         [ +  - ]:    36440300 :             vchRet.push_back((asn >> (8 * i)) & 0xFF);
      28                 :    36440300 :         }
      29                 :     9110075 :         return vchRet;
      30                 :             :     }
      31                 :             : 
      32   [ +  -  +  - ]:    56639662 :     vchRet.push_back(address.GetNetClass());
      33                 :    56639662 :     int nStartByte{0};
      34                 :    56639662 :     int nBits{0};
      35                 :             : 
      36   [ +  -  +  + ]:    56639662 :     if (address.IsLocal()) {
      37                 :             :         // all local addresses belong to the same group
      38   [ +  -  +  + ]:    56639662 :     } else if (address.IsInternal()) {
      39                 :             :         // All internal-usage addresses get their own group.
      40                 :             :         // Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes().
      41                 :      368148 :         nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
      42                 :      368148 :         nBits = ADDR_INTERNAL_SIZE * 8;
      43   [ +  -  +  + ]:    56441969 :     } else if (!address.IsRoutable()) {
      44                 :             :         // all other unroutable addresses belong to the same group
      45   [ +  -  +  + ]:    56073821 :     } else if (address.HasLinkedIPv4()) {
      46                 :             :         // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
      47         [ +  - ]:     7432710 :         uint32_t ipv4 = address.GetLinkedIPv4();
      48         [ +  - ]:     7432710 :         vchRet.push_back((ipv4 >> 24) & 0xFF);
      49         [ +  - ]:     7432710 :         vchRet.push_back((ipv4 >> 16) & 0xFF);
      50                 :     7432710 :         return vchRet;
      51   [ +  -  +  +  :    55568542 :     } else if (address.IsTor() || address.IsI2P()) {
             +  -  +  + ]
      52                 :    24885177 :         nBits = 4;
      53   [ +  -  +  + ]:    48135832 :     } else if (address.IsCJDNS()) {
      54                 :             :         // Treat in the same way as Tor and I2P because the address in all of
      55                 :             :         // them is "random" bytes (derived from a public key). However in CJDNS
      56                 :             :         // the first byte is a constant (see CJDNS_PREFIX), so the random bytes
      57                 :             :         // come after it. Thus skip the constant 8 bits at the start.
      58                 :    10805009 :         nBits = 12;
      59   [ +  -  +  + ]:    23250655 :     } else if (address.IsHeNet()) {
      60                 :             :         // for he.net, use /36 groups
      61                 :        8578 :         nBits = 36;
      62                 :        8578 :     } else {
      63                 :             :         // for the rest of the IPv6 network, use /32 groups
      64                 :    12437068 :         nBits = 32;
      65                 :             :     }
      66                 :             : 
      67                 :             :     // Push our address onto vchRet.
      68         [ +  - ]:    49206952 :     auto addr_bytes = address.GetAddrBytes();
      69                 :    49206952 :     const size_t num_bytes = nBits / 8;
      70         [ +  - ]:    49206952 :     vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
      71                 :    49206952 :     nBits %= 8;
      72                 :             :     // ...for the last byte, push nBits and for the rest of the byte push 1's
      73         [ +  + ]:    49206952 :     if (nBits > 0) {
      74         [ +  - ]:    35698764 :         assert(num_bytes < addr_bytes.size());
      75         [ +  - ]:    35698764 :         vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
      76                 :    35698764 :     }
      77                 :             : 
      78                 :    49206952 :     return vchRet;
      79         [ +  - ]:    65749737 : }
      80                 :             : 
      81                 :    85600818 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
      82                 :             : {
      83                 :    85600818 :     uint32_t net_class = address.GetNetClass();
      84   [ +  +  +  +  :    85600818 :     if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
                   +  + ]
      85                 :    70473071 :         return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
      86                 :             :     }
      87         [ +  - ]:    15127747 :     std::vector<bool> ip_bits(128);
      88   [ +  -  +  + ]:    15127747 :     if (address.HasLinkedIPv4()) {
      89                 :             :         // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
      90         [ +  + ]:    67900872 :         for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
      91         [ +  + ]:   564099552 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
      92         [ +  - ]:   501421824 :                 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
      93                 :   501421824 :             }
      94                 :    62677728 :         }
      95         [ +  - ]:     5223144 :         uint32_t ipv4 = address.GetLinkedIPv4();
      96         [ +  + ]:   172363752 :         for (int i = 0; i < 32; ++i) {
      97         [ +  - ]:   167140608 :             ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
      98                 :   167140608 :         }
      99                 :     5223144 :     } else {
     100                 :             :         // Use all 128 bits of the IPv6 address otherwise
     101   [ +  -  +  - ]:     9904603 :         assert(address.IsIPv6());
     102         [ +  - ]:     9904603 :         auto addr_bytes = address.GetAddrBytes();
     103         [ +  + ]:   168378251 :         for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
     104                 :   158473648 :             uint8_t cur_byte = addr_bytes[byte_i];
     105         [ +  + ]:  1426262832 :             for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
     106         [ +  - ]:  1267789184 :                 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
     107                 :  1267789184 :             }
     108                 :   158473648 :         }
     109                 :     9904603 :     }
     110         [ +  - ]:    15127747 :     uint32_t mapped_as = Interpret(m_asmap, ip_bits);
     111                 :    15127747 :     return mapped_as;
     112                 :    85600818 : }
     113                 :             : 
     114                 :           0 : void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
     115                 :           0 :     std::set<uint32_t> clearnet_asns{};
     116                 :           0 :     int unmapped_count{0};
     117                 :             : 
     118         [ #  # ]:           0 :     for (const auto& addr : clearnet_addrs) {
     119         [ #  # ]:           0 :         uint32_t asn = GetMappedAS(addr);
     120         [ #  # ]:           0 :         if (asn == 0) {
     121                 :           0 :             ++unmapped_count;
     122                 :           0 :             continue;
     123                 :             :         }
     124         [ #  # ]:           0 :         clearnet_asns.insert(asn);
     125   [ #  #  #  #  :           0 :     }
                      # ]
     126                 :             : 
     127         [ #  # ]:           0 :     LogPrintf("ASMap Health Check: %i clearnet peers are mapped to %i ASNs with %i peers being unmapped\n", clearnet_addrs.size(), clearnet_asns.size(), unmapped_count);
     128                 :           0 : }
     129                 :             : 
     130                 :           0 : bool NetGroupManager::UsingASMap() const {
     131                 :           0 :     return m_asmap.size() > 0;
     132                 :             : }
        

Generated by: LCOV version 2.0-1