Bitcoin Core  24.99.0
P2P Digital Currency
netgroup.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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 <util/asmap.h>
9 
11 {
12  if (!m_asmap.size()) return {};
13 
14  return SerializeHash(m_asmap);
15 }
16 
17 std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
18 {
19  std::vector<unsigned char> vchRet;
20  // If non-empty asmap is supplied and the address is IPv4/IPv6,
21  // return ASN to be used for bucketing.
22  uint32_t asn = GetMappedAS(address);
23  if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
24  vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
25  for (int i = 0; i < 4; i++) {
26  vchRet.push_back((asn >> (8 * i)) & 0xFF);
27  }
28  return vchRet;
29  }
30 
31  vchRet.push_back(address.GetNetClass());
32  int nStartByte{0};
33  int nBits{0};
34 
35  if (address.IsLocal()) {
36  // all local addresses belong to the same group
37  } else if (address.IsInternal()) {
38  // All internal-usage addresses get their own group.
39  // Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes().
40  nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
41  nBits = ADDR_INTERNAL_SIZE * 8;
42  } else if (!address.IsRoutable()) {
43  // all other unroutable addresses belong to the same group
44  } else if (address.HasLinkedIPv4()) {
45  // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
46  uint32_t ipv4 = address.GetLinkedIPv4();
47  vchRet.push_back((ipv4 >> 24) & 0xFF);
48  vchRet.push_back((ipv4 >> 16) & 0xFF);
49  return vchRet;
50  } else if (address.IsTor() || address.IsI2P()) {
51  nBits = 4;
52  } else if (address.IsCJDNS()) {
53  // Treat in the same way as Tor and I2P because the address in all of
54  // them is "random" bytes (derived from a public key). However in CJDNS
55  // the first byte is a constant 0xfc, so the random bytes come after it.
56  // Thus skip the constant 8 bits at the start.
57  nBits = 12;
58  } else if (address.IsHeNet()) {
59  // for he.net, use /36 groups
60  nBits = 36;
61  } else {
62  // for the rest of the IPv6 network, use /32 groups
63  nBits = 32;
64  }
65 
66  // Push our address onto vchRet.
67  auto addr_bytes = address.GetAddrBytes();
68  const size_t num_bytes = nBits / 8;
69  vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
70  nBits %= 8;
71  // ...for the last byte, push nBits and for the rest of the byte push 1's
72  if (nBits > 0) {
73  assert(num_bytes < addr_bytes.size());
74  vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
75  }
76 
77  return vchRet;
78 }
79 
80 uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
81 {
82  uint32_t net_class = address.GetNetClass();
83  if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
84  return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
85  }
86  std::vector<bool> ip_bits(128);
87  if (address.HasLinkedIPv4()) {
88  // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
89  for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
90  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
91  ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
92  }
93  }
94  uint32_t ipv4 = address.GetLinkedIPv4();
95  for (int i = 0; i < 32; ++i) {
96  ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
97  }
98  } else {
99  // Use all 128 bits of the IPv6 address otherwise
100  assert(address.IsIPv6());
101  auto addr_bytes = address.GetAddrBytes();
102  for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
103  uint8_t cur_byte = addr_bytes[byte_i];
104  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
105  ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
106  }
107  }
108  }
109  uint32_t mapped_as = Interpret(m_asmap, ip_bits);
110  return mapped_as;
111 }
Network address.
Definition: netaddress.h:118
Network GetNetClass() const
Definition: netaddress.cpp:701
std::vector< unsigned char > GetAddrBytes() const
Definition: netaddress.cpp:719
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:417
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:407
bool IsRoutable() const
Definition: netaddress.cpp:484
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:679
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:684
bool IsHeNet() const
Definition: netaddress.cpp:398
bool IsLocal() const
Definition: netaddress.cpp:419
bool IsIPv6() const
Definition: netaddress.cpp:314
bool IsInternal() const
Definition: netaddress.cpp:494
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:412
uint256 GetAsmapChecksum() const
Get a checksum identifying the asmap being used.
Definition: netgroup.cpp:10
const std::vector< bool > m_asmap
Compressed IP->ASN mapping, loaded from a file when a node starts.
Definition: netgroup.h:63
std::vector< unsigned char > GetGroup(const CNetAddr &address) const
Get the canonical identifier of the network group for address.
Definition: netgroup.cpp:17
uint32_t GetMappedAS(const CNetAddr &address) const
Get the autonomous system on the BGP path to address.
Definition: netgroup.cpp:80
256-bit opaque blob.
Definition: uint256.h:119
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:204
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition: netaddress.h:109
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:88
@ NET_IPV6
IPv6.
Definition: netaddress.h:52
@ NET_IPV4
IPv4.
Definition: netaddress.h:49
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:73
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:88
assert(!tx.IsCoinBase())