Coverage Report

Created: 2025-08-19 07:21

/src/bitcoin-core/src/netgroup.cpp
Line
Count
Source (jump to first uncovered line)
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
uint256 NetGroupManager::GetAsmapChecksum() const
12
16.3k
{
13
16.3k
    if (!m_asmap.size()) return {};
14
15
4.81k
    return (HashWriter{} << m_asmap).GetHash();
16
16.3k
}
17
18
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
19
69.7M
{
20
69.7M
    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
69.7M
    uint32_t asn = GetMappedAS(address);
24
69.7M
    if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
25
8.73M
        vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
26
43.6M
        for (int i = 0; i < 4; i++) {
27
34.9M
            vchRet.push_back((asn >> (8 * i)) & 0xFF);
28
34.9M
        }
29
8.73M
        return vchRet;
30
8.73M
    }
31
32
60.9M
    vchRet.push_back(address.GetNetClass());
33
60.9M
    int nStartByte{0};
34
60.9M
    int nBits{0};
35
36
60.9M
    if (address.IsLocal()) {
37
        // all local addresses belong to the same group
38
60.6M
    } 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
749k
        nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
42
749k
        nBits = ADDR_INTERNAL_SIZE * 8;
43
59.8M
    } else if (!address.IsRoutable()) {
44
        // all other unroutable addresses belong to the same group
45
59.6M
    } else if (address.HasLinkedIPv4()) {
46
        // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
47
10.5M
        uint32_t ipv4 = address.GetLinkedIPv4();
48
10.5M
        vchRet.push_back((ipv4 >> 24) & 0xFF);
49
10.5M
        vchRet.push_back((ipv4 >> 16) & 0xFF);
50
10.5M
        return vchRet;
51
49.0M
    } else if (address.IsTor() || address.IsI2P()) {
52
25.3M
        nBits = 4;
53
25.3M
    } 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
11.8M
        nBits = 12;
59
11.8M
    } else if (address.IsHeNet()) {
60
        // for he.net, use /36 groups
61
7.86k
        nBits = 36;
62
11.8M
    } else {
63
        // for the rest of the IPv6 network, use /32 groups
64
11.8M
        nBits = 32;
65
11.8M
    }
66
67
    // Push our address onto vchRet.
68
50.4M
    auto addr_bytes = address.GetAddrBytes();
69
50.4M
    const size_t num_bytes = nBits / 8;
70
50.4M
    vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
71
50.4M
    nBits %= 8;
72
    // ...for the last byte, push nBits and for the rest of the byte push 1's
73
50.4M
    if (nBits > 0) {
74
37.1M
        assert(num_bytes < addr_bytes.size());
75
37.1M
        vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
76
37.1M
    }
77
78
50.4M
    return vchRet;
79
50.4M
}
80
81
uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
82
92.9M
{
83
92.9M
    uint32_t net_class = address.GetNetClass();
84
92.9M
    if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
85
78.5M
        return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
86
78.5M
    }
87
14.4M
    std::vector<bool> ip_bits(128);
88
14.4M
    if (address.HasLinkedIPv4()) {
89
        // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
90
57.5M
        for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
91
478M
            for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
92
425M
                ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
93
425M
            }
94
53.1M
        }
95
4.42M
        uint32_t ipv4 = address.GetLinkedIPv4();
96
146M
        for (int i = 0; i < 32; ++i) {
97
141M
            ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
98
141M
        }
99
9.97M
    } else {
100
        // Use all 128 bits of the IPv6 address otherwise
101
9.97M
        assert(address.IsIPv6());
102
9.97M
        auto addr_bytes = address.GetAddrBytes();
103
169M
        for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
104
159M
            uint8_t cur_byte = addr_bytes[byte_i];
105
1.43G
            for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
106
1.27G
                ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
107
1.27G
            }
108
159M
        }
109
9.97M
    }
110
14.4M
    uint32_t mapped_as = Interpret(m_asmap, ip_bits);
111
14.4M
    return mapped_as;
112
14.4M
}
113
114
7.72k
void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
115
7.72k
    std::set<uint32_t> clearnet_asns{};
116
7.72k
    int unmapped_count{0};
117
118
7.72k
    for (const auto& addr : clearnet_addrs) {
119
2.53k
        uint32_t asn = GetMappedAS(addr);
120
2.53k
        if (asn == 0) {
121
2.03k
            ++unmapped_count;
122
2.03k
            continue;
123
2.03k
        }
124
498
        clearnet_asns.insert(asn);
125
498
    }
126
127
7.72k
    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
7.72k
}
129
130
0
bool NetGroupManager::UsingASMap() const {
131
0
    return m_asmap.size() > 0;
132
0
}