Bitcoin Core 28.99.0
P2P Digital Currency
netgroup.cpp
Go to the documentation of this file.
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
12{
13 if (!m_asmap.size()) return {};
14
15 return (HashWriter{} << m_asmap).GetHash();
16}
17
18std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
19{
20 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 uint32_t asn = GetMappedAS(address);
24 if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
25 vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
26 for (int i = 0; i < 4; i++) {
27 vchRet.push_back((asn >> (8 * i)) & 0xFF);
28 }
29 return vchRet;
30 }
31
32 vchRet.push_back(address.GetNetClass());
33 int nStartByte{0};
34 int nBits{0};
35
36 if (address.IsLocal()) {
37 // all local addresses belong to the same group
38 } 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 nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
42 nBits = ADDR_INTERNAL_SIZE * 8;
43 } else if (!address.IsRoutable()) {
44 // all other unroutable addresses belong to the same group
45 } else if (address.HasLinkedIPv4()) {
46 // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
47 uint32_t ipv4 = address.GetLinkedIPv4();
48 vchRet.push_back((ipv4 >> 24) & 0xFF);
49 vchRet.push_back((ipv4 >> 16) & 0xFF);
50 return vchRet;
51 } else if (address.IsTor() || address.IsI2P()) {
52 nBits = 4;
53 } 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 nBits = 12;
59 } else if (address.IsHeNet()) {
60 // for he.net, use /36 groups
61 nBits = 36;
62 } else {
63 // for the rest of the IPv6 network, use /32 groups
64 nBits = 32;
65 }
66
67 // Push our address onto vchRet.
68 auto addr_bytes = address.GetAddrBytes();
69 const size_t num_bytes = nBits / 8;
70 vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
71 nBits %= 8;
72 // ...for the last byte, push nBits and for the rest of the byte push 1's
73 if (nBits > 0) {
74 assert(num_bytes < addr_bytes.size());
75 vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
76 }
77
78 return vchRet;
79}
80
81uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
82{
83 uint32_t net_class = address.GetNetClass();
84 if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
85 return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
86 }
87 std::vector<bool> ip_bits(128);
88 if (address.HasLinkedIPv4()) {
89 // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
90 for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
91 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
92 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
93 }
94 }
95 uint32_t ipv4 = address.GetLinkedIPv4();
96 for (int i = 0; i < 32; ++i) {
97 ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
98 }
99 } else {
100 // Use all 128 bits of the IPv6 address otherwise
101 assert(address.IsIPv6());
102 auto addr_bytes = address.GetAddrBytes();
103 for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
104 uint8_t cur_byte = addr_bytes[byte_i];
105 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
106 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
107 }
108 }
109 }
110 uint32_t mapped_as = Interpret(m_asmap, ip_bits);
111 return mapped_as;
112}
113
114void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
115 std::set<uint32_t> clearnet_asns{};
116 int unmapped_count{0};
117
118 for (const auto& addr : clearnet_addrs) {
119 uint32_t asn = GetMappedAS(addr);
120 if (asn == 0) {
121 ++unmapped_count;
122 continue;
123 }
124 clearnet_asns.insert(asn);
125 }
126
127 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}
129
131 return m_asmap.size() > 0;
132}
Network address.
Definition: netaddress.h:112
Network GetNetClass() const
Definition: netaddress.cpp:678
std::vector< unsigned char > GetAddrBytes() const
Definition: netaddress.cpp:696
bool IsCJDNS() const
Definition: netaddress.h:176
bool IsTor() const
Definition: netaddress.h:174
bool IsRoutable() const
Definition: netaddress.cpp:466
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:656
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:661
bool IsHeNet() const
Definition: netaddress.cpp:397
bool IsLocal() const
Definition: netaddress.cpp:402
bool IsIPv6() const
Definition: netaddress.h:158
bool IsInternal() const
Definition: netaddress.cpp:476
bool IsI2P() const
Definition: netaddress.h:175
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
uint256 GetAsmapChecksum() const
Get a checksum identifying the asmap being used.
Definition: netgroup.cpp:11
bool UsingASMap() const
Indicates whether ASMap is being used for clearnet bucketing.
Definition: netgroup.cpp:130
const std::vector< bool > m_asmap
Compressed IP->ASN mapping, loaded from a file when a node starts.
Definition: netgroup.h:73
void ASMapHealthCheck(const std::vector< CNetAddr > &clearnet_addrs) const
Analyze and log current health of ASMap based buckets.
Definition: netgroup.cpp:114
std::vector< unsigned char > GetGroup(const CNetAddr &address) const
Get the canonical identifier of the network group for address.
Definition: netgroup.cpp:18
uint32_t GetMappedAS(const CNetAddr &address) const
Get the autonomous system on the BGP path to address.
Definition: netgroup.cpp:81
256-bit opaque blob.
Definition: uint256.h:190
#define LogPrintf(...)
Definition: logging.h:266
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition: netaddress.h:101
static const std::array< uint8_t, 6 > INTERNAL_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded "internal" address.
Definition: netaddress.h:76
@ NET_IPV6
IPv6.
Definition: netaddress.h:40
@ NET_IPV4
IPv4.
Definition: netaddress.h:37
static const std::array< uint8_t, 12 > IPV4_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded IPv4 address.
Definition: netaddress.h:61
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:88
assert(!tx.IsCoinBase())