LCOV - code coverage report
Current view: top level - src - netgroup.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 82.3 % 62 51
Test Date: 2026-02-04 04:43:42 Functions: 80.0 % 5 4
Branches: 57.3 % 96 55

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-present 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 <uint256.h>
      10                 :             : #include <util/asmap.h>
      11                 :             : 
      12                 :             : #include <cstddef>
      13                 :             : 
      14                 :          16 : uint256 NetGroupManager::GetAsmapVersion() const
      15                 :             : {
      16                 :          16 :     return AsmapVersion(m_asmap);
      17                 :             : }
      18                 :             : 
      19                 :       20739 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
      20                 :             : {
      21                 :       20739 :     std::vector<unsigned char> vchRet;
      22                 :             :     // If non-empty asmap is supplied and the address is IPv4/IPv6,
      23                 :             :     // return ASN to be used for bucketing.
      24         [ +  - ]:       20739 :     uint32_t asn = GetMappedAS(address);
      25         [ +  + ]:       20739 :     if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
      26         [ +  - ]:        3628 :         vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
      27         [ +  + ]:       18140 :         for (int i = 0; i < 4; i++) {
      28         [ +  - ]:       14512 :             vchRet.push_back((asn >> (8 * i)) & 0xFF);
      29                 :             :         }
      30                 :             :         return vchRet;
      31                 :             :     }
      32                 :             : 
      33   [ +  -  +  - ]:       17111 :     vchRet.push_back(address.GetNetClass());
      34                 :       17111 :     int nStartByte{0};
      35                 :       17111 :     int nBits{0};
      36                 :             : 
      37   [ +  -  +  + ]:       17111 :     if (address.IsLocal()) {
      38                 :             :         // all local addresses belong to the same group
      39   [ +  -  +  + ]:       17110 :     } else if (address.IsInternal()) {
      40                 :             :         // All internal-usage addresses get their own group.
      41                 :             :         // Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes().
      42                 :             :         nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
      43                 :             :         nBits = ADDR_INTERNAL_SIZE * 8;
      44   [ +  -  +  + ]:       17109 :     } else if (!address.IsRoutable()) {
      45                 :             :         // all other unroutable addresses belong to the same group
      46   [ +  -  +  + ]:       17083 :     } else if (address.HasLinkedIPv4()) {
      47                 :             :         // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
      48         [ +  - ]:       17071 :         uint32_t ipv4 = address.GetLinkedIPv4();
      49         [ +  - ]:       17071 :         vchRet.push_back((ipv4 >> 24) & 0xFF);
      50         [ +  - ]:       20739 :         vchRet.push_back((ipv4 >> 16) & 0xFF);
      51                 :             :         return vchRet;
      52   [ +  -  +  + ]:          12 :     } else if (address.IsTor() || address.IsI2P()) {
      53                 :             :         nBits = 4;
      54         [ +  - ]:           2 :     } else if (address.IsCJDNS()) {
      55                 :             :         // Treat in the same way as Tor and I2P because the address in all of
      56                 :             :         // them is "random" bytes (derived from a public key). However in CJDNS
      57                 :             :         // the first byte is a constant (see CJDNS_PREFIX), so the random bytes
      58                 :             :         // come after it. Thus skip the constant 8 bits at the start.
      59                 :             :         nBits = 12;
      60   [ +  -  +  + ]:           2 :     } else if (address.IsHeNet()) {
      61                 :             :         // for he.net, use /36 groups
      62                 :             :         nBits = 36;
      63                 :             :     } else {
      64                 :             :         // for the rest of the IPv6 network, use /32 groups
      65                 :           1 :         nBits = 32;
      66                 :             :     }
      67                 :             : 
      68                 :             :     // Push our address onto vchRet.
      69         [ +  - ]:          40 :     auto addr_bytes = address.GetAddrBytes();
      70                 :          40 :     const size_t num_bytes = nBits / 8;
      71         [ +  - ]:          40 :     vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
      72                 :          40 :     nBits %= 8;
      73                 :             :     // ...for the last byte, push nBits and for the rest of the byte push 1's
      74         [ +  + ]:          40 :     if (nBits > 0) {
      75   [ -  +  -  + ]:          11 :         assert(num_bytes < addr_bytes.size());
      76         [ +  - ]:          11 :         vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
      77                 :             :     }
      78                 :             : 
      79                 :          40 :     return vchRet;
      80                 :          40 : }
      81                 :             : 
      82                 :       23400 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
      83                 :             : {
      84                 :       23400 :     uint32_t net_class = address.GetNetClass();
      85   [ +  +  +  + ]:       23400 :     if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
      86                 :             :         return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
      87                 :             :     }
      88                 :        4126 :     std::vector<std::byte> ip_bytes(16);
      89   [ +  -  +  + ]:        4126 :     if (address.HasLinkedIPv4()) {
      90                 :             :         // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
      91                 :        4107 :         std::copy_n(std::as_bytes(std::span{IPV4_IN_IPV6_PREFIX}).begin(),
      92                 :             :                     IPV4_IN_IPV6_PREFIX.size(), ip_bytes.begin());
      93         [ +  - ]:        4107 :         uint32_t ipv4 = address.GetLinkedIPv4();
      94         [ +  + ]:       20535 :         for (int i = 0; i < 4; ++i) {
      95                 :       16428 :             ip_bytes[12 + i] = std::byte((ipv4 >> (24 - i * 8)) & 0xFF);
      96                 :             :         }
      97                 :             :     } else {
      98                 :             :         // Use all 128 bits of the IPv6 address otherwise
      99         [ -  + ]:          19 :         assert(address.IsIPv6());
     100         [ +  - ]:          19 :         auto addr_bytes = address.GetAddrBytes();
     101   [ -  +  -  +  :          19 :         assert(addr_bytes.size() == ip_bytes.size());
                   -  + ]
     102                 :          19 :         std::copy_n(std::as_bytes(std::span{addr_bytes}).begin(),
     103                 :             :                     addr_bytes.size(), ip_bytes.begin());
     104                 :          19 :     }
     105   [ -  +  +  - ]:        4126 :     uint32_t mapped_as = Interpret(m_asmap, ip_bytes);
     106                 :        4126 :     return mapped_as;
     107                 :        4126 : }
     108                 :             : 
     109                 :           0 : void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
     110                 :           0 :     std::set<uint32_t> clearnet_asns{};
     111                 :           0 :     int unmapped_count{0};
     112                 :             : 
     113         [ #  # ]:           0 :     for (const auto& addr : clearnet_addrs) {
     114         [ #  # ]:           0 :         uint32_t asn = GetMappedAS(addr);
     115         [ #  # ]:           0 :         if (asn == 0) {
     116                 :           0 :             ++unmapped_count;
     117                 :           0 :             continue;
     118                 :             :         }
     119         [ #  # ]:           0 :         clearnet_asns.insert(asn);
     120                 :             :     }
     121                 :             : 
     122   [ #  #  #  # ]:           0 :     LogInfo("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);
     123                 :           0 : }
     124                 :             : 
     125                 :           2 : bool NetGroupManager::UsingASMap() const {
     126                 :           2 :     return m_asmap.size() > 0;
     127                 :             : }
        

Generated by: LCOV version 2.0-1