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 <uint256.h>
9 : : #include <util/asmap.h>
10 : : #include <util/log.h>
11 : :
12 : : #include <cassert>
13 : : #include <cstddef>
14 : : #include <utility>
15 : :
16 : 6923 : NetGroupManager::NetGroupManager(std::span<const std::byte> embedded_asmap, std::vector<std::byte>&& loaded_asmap)
17 : 6923 : : m_asmap{embedded_asmap},
18 [ + - ]: 6923 : m_loaded_asmap{std::move(loaded_asmap)},
19 [ + - ]: 6923 : m_asmap_version{AsmapVersion(m_asmap)}
20 : : {
21 [ + + - + ]: 6923 : assert(m_loaded_asmap.empty() || m_asmap.data() == m_loaded_asmap.data());
22 : 6923 : }
23 : :
24 : 4854 : uint256 NetGroupManager::GetAsmapVersion() const
25 : : {
26 : 4854 : return m_asmap_version;
27 : : }
28 : :
29 : 34023056 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
30 : : {
31 : 34023056 : 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 [ + - ]: 34023056 : uint32_t asn = GetMappedAS(address);
35 [ + + ]: 34023056 : if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
36 [ + - ]: 4553903 : vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
37 [ + + ]: 22769515 : for (int i = 0; i < 4; i++) {
38 [ + - ]: 18215612 : vchRet.push_back((asn >> (8 * i)) & 0xFF);
39 : : }
40 : : return vchRet;
41 : : }
42 : :
43 [ + - + - ]: 29469153 : vchRet.push_back(address.GetNetClass());
44 : 29469153 : int nStartByte{0};
45 : 29469153 : int nBits{0};
46 : :
47 [ + - + + ]: 29469153 : if (address.IsLocal()) {
48 : : // all local addresses belong to the same group
49 [ + - + + ]: 29337066 : } 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 : : nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
53 : : nBits = ADDR_INTERNAL_SIZE * 8;
54 [ + - + + ]: 29185701 : } else if (!address.IsRoutable()) {
55 : : // all other unroutable addresses belong to the same group
56 [ + - + + ]: 28998921 : } else if (address.HasLinkedIPv4()) {
57 : : // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
58 [ + - ]: 4354009 : uint32_t ipv4 = address.GetLinkedIPv4();
59 [ + - ]: 4354009 : vchRet.push_back((ipv4 >> 24) & 0xFF);
60 [ + - ]: 34023056 : vchRet.push_back((ipv4 >> 16) & 0xFF);
61 : : return vchRet;
62 [ + + + + ]: 24644912 : } else if (address.IsTor() || address.IsI2P()) {
63 : : nBits = 4;
64 [ + + ]: 12034500 : } 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 : : nBits = 12;
70 [ + - + + ]: 6177411 : } else if (address.IsHeNet()) {
71 : : // for he.net, use /36 groups
72 : : nBits = 36;
73 : : } else {
74 : : // for the rest of the IPv6 network, use /32 groups
75 : 6170556 : nBits = 32;
76 : : }
77 : :
78 : : // Push our address onto vchRet.
79 [ + - ]: 25115144 : auto addr_bytes = address.GetAddrBytes();
80 : 25115144 : const size_t num_bytes = nBits / 8;
81 [ + - ]: 25115144 : vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
82 : 25115144 : nBits %= 8;
83 : : // ...for the last byte, push nBits and for the rest of the byte push 1's
84 [ + + ]: 25115144 : if (nBits > 0) {
85 [ - + - + ]: 18474356 : assert(num_bytes < addr_bytes.size());
86 [ + - ]: 18474356 : vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
87 : : }
88 : :
89 : 25115144 : return vchRet;
90 : 25115144 : }
91 : :
92 : 45909233 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
93 : : {
94 : 45909233 : uint32_t net_class = address.GetNetClass();
95 [ + + + + ]: 45909233 : if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
96 : : return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
97 : : }
98 : 8103282 : std::vector<std::byte> ip_bytes(16);
99 [ + - + + ]: 8103282 : if (address.HasLinkedIPv4()) {
100 : : // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
101 : 2865868 : std::copy_n(std::as_bytes(std::span{IPV4_IN_IPV6_PREFIX}).begin(),
102 : : IPV4_IN_IPV6_PREFIX.size(), ip_bytes.begin());
103 [ + - ]: 2865868 : uint32_t ipv4 = address.GetLinkedIPv4();
104 [ + + ]: 14329340 : for (int i = 0; i < 4; ++i) {
105 : 11463472 : ip_bytes[12 + i] = std::byte((ipv4 >> (24 - i * 8)) & 0xFF);
106 : : }
107 : : } else {
108 : : // Use all 128 bits of the IPv6 address otherwise
109 [ - + ]: 5237414 : assert(address.IsIPv6());
110 [ + - ]: 5237414 : auto addr_bytes = address.GetAddrBytes();
111 [ - + - + : 5237414 : assert(addr_bytes.size() == ip_bytes.size());
- + ]
112 : 5237414 : std::copy_n(std::as_bytes(std::span{addr_bytes}).begin(),
113 : : addr_bytes.size(), ip_bytes.begin());
114 : 5237414 : }
115 [ - + + - ]: 8103282 : uint32_t mapped_as = Interpret(m_asmap, ip_bytes);
116 : 8103282 : return mapped_as;
117 : 8103282 : }
118 : :
119 : 3353 : void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
120 : 3353 : std::set<uint32_t> clearnet_asns{};
121 : 3353 : int unmapped_count{0};
122 : :
123 [ + + ]: 4634 : for (const auto& addr : clearnet_addrs) {
124 [ + - ]: 1281 : uint32_t asn = GetMappedAS(addr);
125 [ + + ]: 1281 : if (asn == 0) {
126 : 954 : ++unmapped_count;
127 : 954 : continue;
128 : : }
129 [ + - ]: 327 : clearnet_asns.insert(asn);
130 : : }
131 : :
132 [ - + + - ]: 3353 : 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 : 3353 : }
134 : :
135 : 1 : bool NetGroupManager::UsingASMap() const {
136 : 1 : return m_asmap.size() > 0;
137 : : }
|