Coverage Report

Created: 2026-09-21 19:49

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