Bitcoin Core  22.99.0
P2P Digital Currency
asmap.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <test/fuzz/fuzz.h>
7 #include <util/asmap.h>
8 
9 #include <cstdint>
10 #include <vector>
11 
13 static const std::vector<bool> IPV6_PREFIX_ASMAP = {};
14 
16 static const std::vector<bool> IPV4_PREFIX_ASMAP = {
17  true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
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, true, true, true, true, true, true, true, true, // Match 0xFF
28  true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true // Match 0xFF
29 };
30 
32 {
33  // Encoding: [7 bits: asmap size] [1 bit: ipv6?] [3-130 bytes: asmap] [4 or 16 bytes: addr]
34  if (buffer.size() < 1 + 3 + 4) return;
35  int asmap_size = 3 + (buffer[0] & 127);
36  bool ipv6 = buffer[0] & 128;
37  const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;
38  if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;
39  std::vector<bool> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
40  asmap.reserve(asmap.size() + 8 * asmap_size);
41  for (int i = 0; i < asmap_size; ++i) {
42  for (int j = 0; j < 8; ++j) {
43  asmap.push_back((buffer[1 + i] >> j) & 1);
44  }
45  }
46  if (!SanityCheckASMap(asmap, 128)) return;
47 
48  const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
49  CNetAddr net_addr;
50  if (ipv6) {
51  assert(addr_size == ADDR_IPV6_SIZE);
52  net_addr.SetLegacyIPv6(Span<const uint8_t>(addr_data, addr_size));
53  } else {
54  assert(addr_size == ADDR_IPV4_SIZE);
55  in_addr ipv4;
56  memcpy(&ipv4, addr_data, addr_size);
57  net_addr.SetIP(CNetAddr{ipv4});
58  }
59  (void)net_addr.GetMappedAS(asmap);
60 }
assert
assert(!tx.IsCoinBase())
asmap.h
CNetAddr::SetLegacyIPv6
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:138
CNetAddr
Network address.
Definition: netaddress.h:118
IPV6_PREFIX_ASMAP
static const std::vector< bool > IPV6_PREFIX_ASMAP
asmap code that consumes nothing
Definition: asmap.cpp:13
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
netaddress.h
CNetAddr::GetMappedAS
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:725
ADDR_IPV6_SIZE
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:97
ADDR_IPV4_SIZE
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:94
fuzz.h
IPV4_PREFIX_ASMAP
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:16
FUZZ_TARGET
FUZZ_TARGET(asmap)
Definition: asmap.cpp:31
CNetAddr::SetIP
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:107
SanityCheckASMap
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:129