Bitcoin Core  24.99.0
P2P Digital Currency
asmap.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-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 <netaddress.h>
6 #include <netgroup.h>
7 #include <test/fuzz/fuzz.h>
8 #include <util/asmap.h>
9 
10 #include <cstdint>
11 #include <vector>
12 
14 static const std::vector<bool> IPV6_PREFIX_ASMAP = {};
15 
17 static const std::vector<bool> IPV4_PREFIX_ASMAP = {
18  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
19  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
20  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
21  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
22  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
23  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
24  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
25  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
26  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
27  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
28  true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, // Match 0xFF
29  true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true // Match 0xFF
30 };
31 
33 {
34  // Encoding: [7 bits: asmap size] [1 bit: ipv6?] [3-130 bytes: asmap] [4 or 16 bytes: addr]
35  if (buffer.size() < 1 + 3 + 4) return;
36  int asmap_size = 3 + (buffer[0] & 127);
37  bool ipv6 = buffer[0] & 128;
38  const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;
39  if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;
40  std::vector<bool> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
41  asmap.reserve(asmap.size() + 8 * asmap_size);
42  for (int i = 0; i < asmap_size; ++i) {
43  for (int j = 0; j < 8; ++j) {
44  asmap.push_back((buffer[1 + i] >> j) & 1);
45  }
46  }
47  if (!SanityCheckASMap(asmap, 128)) return;
48 
49  const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
50  CNetAddr net_addr;
51  if (ipv6) {
52  assert(addr_size == ADDR_IPV6_SIZE);
53  net_addr.SetLegacyIPv6({addr_data, addr_size});
54  } else {
55  assert(addr_size == ADDR_IPV4_SIZE);
56  in_addr ipv4;
57  memcpy(&ipv4, addr_data, addr_size);
58  net_addr.SetIP(CNetAddr{ipv4});
59  }
60  NetGroupManager netgroupman{asmap};
61  (void)netgroupman.GetMappedAS(net_addr);
62 }
Network address.
Definition: netaddress.h:118
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:103
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:134
Netgroup manager.
Definition: netgroup.h:16
uint32_t GetMappedAS(const CNetAddr &address) const
Get the autonomous system on the BGP path to address.
Definition: netgroup.cpp:80
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:93
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:96
static const std::vector< bool > IPV4_PREFIX_ASMAP
asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map)
Definition: asmap.cpp:17
FUZZ_TARGET(asmap)
Definition: asmap.cpp:32
static const std::vector< bool > IPV6_PREFIX_ASMAP
asmap code that consumes nothing
Definition: asmap.cpp:14
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:133
assert(!tx.IsCoinBase())