mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-23 14:10:15 +01:00
net: change CNetAddr::ip to have flexible size
Before this change `CNetAddr::ip` was a fixed-size array of 16 bytes, not being able to store larger addresses (e.g. TORv3) and encoded smaller ones as 16-byte IPv6 addresses. Change its type to `prevector`, so that it can hold larger addresses and do not disguise non-IPv6 addresses as IPv6. So the IPv4 address `1.2.3.4` is now encoded as `01020304` instead of `00000000000000000000FFFF01020304`. Rename `CNetAddr::ip` to `CNetAddr::m_addr` because it is not an "IP" or "IP address" (TOR addresses are not IP addresses). In order to preserve backward compatibility with serialization (where e.g. `1.2.3.4` is serialized as `00000000000000000000FFFF01020304`) introduce `CNetAddr` dedicated legacy serialize/unserialize methods. Adjust `CSubNet` accordingly. Still use `CSubNet::netmask[]` of fixed 16 bytes, but use the first 4 for IPv4 (not the last 4). Only allow subnetting for IPv4 and IPv6. Co-authored-by: Carl Dong <contact@carldong.me>
This commit is contained in:
@@ -33,7 +33,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
if (buffer.size() < 1 + 3 + 4) return;
|
||||
int asmap_size = 3 + (buffer[0] & 127);
|
||||
bool ipv6 = buffer[0] & 128;
|
||||
int addr_size = ipv6 ? 16 : 4;
|
||||
const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;
|
||||
if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;
|
||||
std::vector<bool> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
|
||||
asmap.reserve(asmap.size() + 8 * asmap_size);
|
||||
@@ -43,7 +43,17 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
}
|
||||
}
|
||||
if (!SanityCheckASMap(asmap)) return;
|
||||
|
||||
const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
|
||||
CNetAddr net_addr;
|
||||
net_addr.SetRaw(ipv6 ? NET_IPV6 : NET_IPV4, buffer.data() + 1 + asmap_size);
|
||||
if (ipv6) {
|
||||
assert(addr_size == ADDR_IPV6_SIZE);
|
||||
net_addr.SetLegacyIPv6(Span<const uint8_t>(addr_data, addr_size));
|
||||
} else {
|
||||
assert(addr_size == ADDR_IPV4_SIZE);
|
||||
in_addr ipv4;
|
||||
memcpy(&ipv4, addr_data, addr_size);
|
||||
net_addr.SetIP(CNetAddr{ipv4});
|
||||
}
|
||||
(void)net_addr.GetMappedAS(asmap);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user