From fa41fc6a1a7d492b894e206f83e0c9786b44a2f0 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Tue, 22 Apr 2025 23:18:46 +0200 Subject: [PATCH 1/5] refactor: Operate on bytes instead of bits in Asmap code Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com> --- src/bench/addrman.cpp | 2 +- src/init.cpp | 3 +- src/netgroup.cpp | 28 +++---- src/netgroup.h | 5 +- src/test/addrman_tests.cpp | 28 ++----- src/test/fuzz/asmap.cpp | 31 ++------ src/test/fuzz/asmap_direct.cpp | 42 +++++++--- src/test/fuzz/util.h | 5 -- src/test/fuzz/util/net.h | 5 +- src/test/netbase_tests.cpp | 13 +--- src/test/util/setup_common.cpp | 2 +- src/util/asmap.cpp | 129 ++++++++++++++++--------------- src/util/asmap.h | 7 +- test/functional/feature_asmap.py | 2 +- 14 files changed, 142 insertions(+), 160 deletions(-) diff --git a/src/bench/addrman.cpp b/src/bench/addrman.cpp index 907c7d2e22b..3f900b3693e 100644 --- a/src/bench/addrman.cpp +++ b/src/bench/addrman.cpp @@ -24,7 +24,7 @@ static constexpr size_t NUM_SOURCES = 64; static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256; -static NetGroupManager EMPTY_NETGROUPMAN{std::vector()}; +static NetGroupManager EMPTY_NETGROUPMAN{{}}; static constexpr uint32_t ADDRMAN_CONSISTENCY_CHECK_RATIO{0}; static std::vector g_sources; diff --git a/src/init.cpp b/src/init.cpp index 571d8b9c02d..6c14f501eef 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -96,6 +96,7 @@ #include #include #include +#include #include #include #include @@ -1561,7 +1562,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) { // Read asmap file if configured - std::vector asmap; + std::vector asmap; if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) { fs::path asmap_path = args.GetPathArg("-asmap"); if (asmap_path.empty()) { diff --git a/src/netgroup.cpp b/src/netgroup.cpp index 474d8a9cbe0..970aa2b4120 100644 --- a/src/netgroup.cpp +++ b/src/netgroup.cpp @@ -8,6 +8,8 @@ #include #include +#include + uint256 NetGroupManager::GetAsmapChecksum() const { if (!m_asmap.size()) return {}; @@ -81,33 +83,27 @@ std::vector NetGroupManager::GetGroup(const CNetAddr& address) co uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const { uint32_t net_class = address.GetNetClass(); - if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) { + if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) { return 0; // Indicates not found, safe because AS0 is reserved per RFC7607. } - std::vector ip_bits(128); + std::vector ip_bytes(16); if (address.HasLinkedIPv4()) { // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits) - for (int8_t byte_i = 0; byte_i < 12; ++byte_i) { - for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { - ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1; - } - } + std::copy_n(std::as_bytes(std::span{IPV4_IN_IPV6_PREFIX}).begin(), + IPV4_IN_IPV6_PREFIX.size(), ip_bytes.begin()); uint32_t ipv4 = address.GetLinkedIPv4(); - for (int i = 0; i < 32; ++i) { - ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1; + for (int i = 0; i < 4; ++i) { + ip_bytes[12 + i] = std::byte((ipv4 >> (24 - i * 8)) & 0xFF); } } else { // Use all 128 bits of the IPv6 address otherwise assert(address.IsIPv6()); auto addr_bytes = address.GetAddrBytes(); - for (int8_t byte_i = 0; byte_i < 16; ++byte_i) { - uint8_t cur_byte = addr_bytes[byte_i]; - for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { - ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1; - } - } + assert(addr_bytes.size() == ip_bytes.size()); + std::copy_n(std::as_bytes(std::span{addr_bytes}).begin(), + addr_bytes.size(), ip_bytes.begin()); } - uint32_t mapped_as = Interpret(m_asmap, ip_bits); + uint32_t mapped_as = Interpret(m_asmap, ip_bytes); return mapped_as; } diff --git a/src/netgroup.h b/src/netgroup.h index 74dc7503939..399876e5bac 100644 --- a/src/netgroup.h +++ b/src/netgroup.h @@ -8,6 +8,7 @@ #include #include +#include #include /** @@ -15,7 +16,7 @@ */ class NetGroupManager { public: - explicit NetGroupManager(std::vector asmap) + explicit NetGroupManager(std::vector&& asmap) : m_asmap{std::move(asmap)} {} @@ -70,7 +71,7 @@ private: * * This is initialized in the constructor, const, and therefore is * thread-safe. */ - const std::vector m_asmap; + const std::vector m_asmap; }; #endif // BITCOIN_NETGROUP_H diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 4debdc16a33..9cd667bac3d 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -24,7 +24,7 @@ using namespace std::literals; using node::NodeContext; using util::ToString; -static NetGroupManager EMPTY_NETGROUPMAN{std::vector()}; +static NetGroupManager EMPTY_NETGROUPMAN{{}}; static const bool DETERMINISTIC{true}; static int32_t GetCheckRatio(const NodeContext& node_ctx) @@ -46,20 +46,6 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0) return serv.value_or(CService{}); } - -static std::vector FromBytes(std::span source) -{ - int vector_size(source.size() * 8); - std::vector result(vector_size); - for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) { - uint8_t cur_byte{std::to_integer(source[byte_i])}; - for (int bit_i = 0; bit_i < 8; ++bit_i) { - result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1; - } - } - return result; -} - BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(addrman_simple) @@ -598,8 +584,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy) // 101.8.0.0/16 AS8 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) { - std::vector asmap = FromBytes(test::data::asmap); - NetGroupManager ngm_asmap{asmap}; + std::vector asmap(test::data::asmap.begin(), test::data::asmap.end()); + NetGroupManager ngm_asmap{std::move(asmap)}; CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE); @@ -652,8 +638,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { - std::vector asmap = FromBytes(test::data::asmap); - NetGroupManager ngm_asmap{asmap}; + std::vector asmap(test::data::asmap.begin(), test::data::asmap.end()); + NetGroupManager ngm_asmap{std::move(asmap)}; CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); @@ -730,8 +716,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) BOOST_AUTO_TEST_CASE(addrman_serialization) { - std::vector asmap1 = FromBytes(test::data::asmap); - NetGroupManager netgroupman{asmap1}; + std::vector asmap1(test::data::asmap.begin(), test::data::asmap.end()); + NetGroupManager netgroupman{std::move(asmap1)}; const auto ratio = GetCheckRatio(m_node); auto addrman_asmap1 = std::make_unique(netgroupman, DETERMINISTIC, ratio); diff --git a/src/test/fuzz/asmap.cpp b/src/test/fuzz/asmap.cpp index fe019f9334d..40dde196db6 100644 --- a/src/test/fuzz/asmap.cpp +++ b/src/test/fuzz/asmap.cpp @@ -6,28 +6,18 @@ #include #include #include +#include #include #include +using namespace util::hex_literals; + //! asmap code that consumes nothing -static const std::vector IPV6_PREFIX_ASMAP = {}; +static const std::vector IPV6_PREFIX_ASMAP = {}; //! asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map) -static const std::vector IPV4_PREFIX_ASMAP = { - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00 - true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, // Match 0xFF - true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true // Match 0xFF -}; +static const auto IPV4_PREFIX_ASMAP = "fb03ec0fb03fc0fe00fb03ec0fb03fc0fe00fb03ec0fb0fffffeff"_hex_v; FUZZ_TARGET(asmap) { @@ -37,13 +27,8 @@ FUZZ_TARGET(asmap) bool ipv6 = buffer[0] & 128; 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 asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP; - asmap.reserve(asmap.size() + 8 * asmap_size); - for (int i = 0; i < asmap_size; ++i) { - for (int j = 0; j < 8; ++j) { - asmap.push_back((buffer[1 + i] >> j) & 1); - } - } + std::vector asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP; + std::ranges::copy(std::as_bytes(buffer.subspan(1, asmap_size)), std::back_inserter(asmap)); if (!SanityCheckASMap(asmap, 128)) return; const uint8_t* addr_data = buffer.data() + 1 + asmap_size; @@ -57,6 +42,6 @@ FUZZ_TARGET(asmap) memcpy(&ipv4, addr_data, addr_size); net_addr.SetIP(CNetAddr{ipv4}); } - NetGroupManager netgroupman{asmap}; + NetGroupManager netgroupman{std::move(asmap)}; (void)netgroupman.GetMappedAS(net_addr); } diff --git a/src/test/fuzz/asmap_direct.cpp b/src/test/fuzz/asmap_direct.cpp index 1d693e5ecf5..fa77553282e 100644 --- a/src/test/fuzz/asmap_direct.cpp +++ b/src/test/fuzz/asmap_direct.cpp @@ -12,6 +12,24 @@ #include +std::vector BitsToBytes(std::span bits) noexcept +{ + std::vector ret; + uint8_t next_byte{0}; + int next_byte_bits{0}; + for (uint8_t val : bits) { + next_byte |= (val & 1) << (next_byte_bits++); + if (next_byte_bits == 8) { + ret.push_back(std::byte(next_byte)); + next_byte = 0; + next_byte_bits = 0; + } + } + if (next_byte_bits) ret.push_back(std::byte(next_byte)); + + return ret; +} + FUZZ_TARGET(asmap_direct) { // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte] @@ -28,22 +46,24 @@ FUZZ_TARGET(asmap_direct) } if (!sep_pos_opt) return; // Needs exactly 1 separator const size_t sep_pos{sep_pos_opt.value()}; - if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address + const size_t ip_len{buffer.size() - sep_pos - 1}; + if (ip_len > 128) return; // At most 128 bits in IP address // Checks on asmap - std::vector asmap(buffer.begin(), buffer.begin() + sep_pos); - if (SanityCheckASMap(asmap, buffer.size() - 1 - sep_pos)) { + auto asmap = BitsToBytes(buffer.first(sep_pos)); + if (SanityCheckASMap(asmap, ip_len)) { // Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid. - std::vector asmap_prefix = asmap; - while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == false) { - asmap_prefix.pop_back(); - } - while (!asmap_prefix.empty()) { - asmap_prefix.pop_back(); - assert(!SanityCheckASMap(asmap_prefix, buffer.size() - 1 - sep_pos)); + for (size_t prefix_len = sep_pos - 1; prefix_len > 0; --prefix_len) { + auto prefix = BitsToBytes(buffer.first(prefix_len)); + // We have to skip the prefixes of the same length as the original + // asmap, since they will contain some zero padding bits in the last + // byte. + if (prefix.size() == asmap.size()) continue; + assert(!SanityCheckASMap(prefix, ip_len)); } + // No address input should trigger assertions in interpreter - std::vector addr(buffer.begin() + sep_pos + 1, buffer.end()); + auto addr = BitsToBytes(buffer.subspan(sep_pos + 1)); (void)Interpret(asmap, addr); } } diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index a7b1bfd54ed..d72ec2f880e 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -65,11 +65,6 @@ template return ret; } -[[nodiscard]] inline std::vector ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional& max_length = std::nullopt) noexcept -{ - return BytesToBits(ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)); -} - [[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional& max_length = std::nullopt) noexcept { return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)}; diff --git a/src/test/fuzz/util/net.h b/src/test/fuzz/util/net.h index 90c018a4275..93aabcc3e51 100644 --- a/src/test/fuzz/util/net.h +++ b/src/test/fuzz/util/net.h @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -234,9 +235,9 @@ public: [[nodiscard]] inline NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider& fuzzed_data_provider) noexcept { - std::vector asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider); + std::vector asmap{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; if (!SanityCheckASMap(asmap, 128)) asmap.clear(); - return NetGroupManager(asmap); + return NetGroupManager(std::move(asmap)); } inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp index 266d952f4ae..379978451d8 100644 --- a/src/test/netbase_tests.cpp +++ b/src/test/netbase_tests.cpp @@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(subnet_test) BOOST_AUTO_TEST_CASE(netbase_getgroup) { - NetGroupManager netgroupman{std::vector()}; // use /16 + NetGroupManager netgroupman{{}}; // use /16 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector({0})); // Local -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector({0})); // !Valid -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector({0})); // RFC1918 -> !Routable() @@ -630,17 +630,8 @@ BOOST_AUTO_TEST_CASE(asmap_test_vectors) "33e53662a7d72a29477b5beb35710591d3e23e5f0379baea62ffdee535bcdf879cbf69b88d7ea37c8015381cf" "63dc33d28f757a4a5e15d6a08"_hex}; - // Convert to std::vector format that the ASMap interpreter uses. - std::vector asmap_bits; - asmap_bits.reserve(ASMAP_DATA.size() * 8); - for (auto byte : ASMAP_DATA) { - for (int bit = 0; bit < 8; ++bit) { - asmap_bits.push_back((std::to_integer(byte) >> bit) & 1); - } - } - // Construct NetGroupManager with this data. - NetGroupManager netgroup{std::move(asmap_bits)}; + NetGroupManager netgroup{std::vector(ASMAP_DATA.begin(), ASMAP_DATA.end())}; BOOST_CHECK(netgroup.UsingASMap()); // Check some randomly-generated IPv6 addresses in it (biased towards the very beginning and diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 4e8866399e9..454f7a74912 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -344,7 +344,7 @@ TestingSetup::TestingSetup( if (!opts.setup_net) return; - m_node.netgroupman = std::make_unique(/*asmap=*/std::vector()); + m_node.netgroupman = std::make_unique(/*asmap=*/std::vector{}); m_node.addrman = std::make_unique(*m_node.netgroupman, /*deterministic=*/false, m_node.args->GetIntArg("-checkaddrman", 0)); diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index c7388c779d5..23cf6d165ac 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -21,16 +22,28 @@ namespace { constexpr uint32_t INVALID = 0xFFFFFFFF; -uint32_t DecodeBits(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos, uint8_t minval, const std::vector &bit_sizes) +inline bool ConsumeBitLE(size_t& bitpos, std::span bytes) noexcept +{ + const bool bit = (std::to_integer(bytes[bitpos / 8]) >> (bitpos % 8)) & 1; + ++bitpos; + return bit; +} + +inline bool ConsumeBitBE(uint8_t& bitpos, std::span bytes) noexcept +{ + const bool bit = (std::to_integer(bytes[bitpos / 8]) >> (7 - (bitpos % 8))) & 1; + ++bitpos; + return bit; +} + +uint32_t DecodeBits(size_t& bitpos, const std::vector& data, uint8_t minval, const std::vector& bit_sizes) { uint32_t val = minval; bool bit; - for (std::vector::const_iterator bit_sizes_it = bit_sizes.begin(); - bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) { + for (auto bit_sizes_it = bit_sizes.begin(); bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) { if (bit_sizes_it + 1 != bit_sizes.end()) { - if (bitpos == endpos) break; - bit = *bitpos; - bitpos++; + if (bitpos >= data.size() * 8) break; + bit = ConsumeBitLE(bitpos, data); } else { bit = 0; } @@ -38,9 +51,8 @@ uint32_t DecodeBits(std::vector::const_iterator& bitpos, const std::vector val += (1 << *bit_sizes_it); } else { for (int b = 0; b < *bit_sizes_it; b++) { - if (bitpos == endpos) return INVALID; // Reached EOF in mantissa - bit = *bitpos; - bitpos++; + if (bitpos >= data.size() * 8) return INVALID; // Reached EOF in mantissa + bit = ConsumeBitLE(bitpos, data); val += bit << (*bit_sizes_it - 1 - b); } return val; @@ -58,69 +70,68 @@ enum class Instruction : uint32_t }; const std::vector TYPE_BIT_SIZES{0, 0, 1}; -Instruction DecodeType(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos) +Instruction DecodeType(size_t& bitpos, const std::vector& data) { - return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES)); + return Instruction(DecodeBits(bitpos, data, 0, TYPE_BIT_SIZES)); } const std::vector ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; -uint32_t DecodeASN(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos) +uint32_t DecodeASN(size_t& bitpos, const std::vector& data) { - return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES); + return DecodeBits(bitpos, data, 1, ASN_BIT_SIZES); } const std::vector MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8}; -uint32_t DecodeMatch(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos) +uint32_t DecodeMatch(size_t& bitpos, const std::vector& data) { - return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES); + return DecodeBits(bitpos, data, 2, MATCH_BIT_SIZES); } const std::vector JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; -uint32_t DecodeJump(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos) +uint32_t DecodeJump(size_t& bitpos, const std::vector& data) { - return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES); + return DecodeBits(bitpos, data, 17, JUMP_BIT_SIZES); } } -uint32_t Interpret(const std::vector &asmap, const std::vector &ip) +uint32_t Interpret(const std::vector& asmap, const std::vector& ip) { - std::vector::const_iterator pos = asmap.begin(); - const std::vector::const_iterator endpos = asmap.end(); - uint8_t bits = ip.size(); + size_t pos{0}; + const size_t endpos{asmap.size() * 8}; + uint8_t ip_bit{0}; + const uint8_t ip_bits_end = ip.size() * 8; uint32_t default_asn = 0; uint32_t jump, match, matchlen; Instruction opcode; - while (pos != endpos) { - opcode = DecodeType(pos, endpos); + while (pos < endpos) { + opcode = DecodeType(pos, asmap); if (opcode == Instruction::RETURN) { - default_asn = DecodeASN(pos, endpos); + default_asn = DecodeASN(pos, asmap); if (default_asn == INVALID) break; // ASN straddles EOF return default_asn; } else if (opcode == Instruction::JUMP) { - jump = DecodeJump(pos, endpos); + jump = DecodeJump(pos, asmap); if (jump == INVALID) break; // Jump offset straddles EOF - if (bits == 0) break; // No input bits left - if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF - if (ip[ip.size() - bits]) { + if (ip_bit == ip_bits_end) break; // No input bits left + if (int64_t{jump} >= static_cast(endpos - pos)) break; // Jumping past EOF + if (ConsumeBitBE(ip_bit, ip)) { pos += jump; } - bits--; } else if (opcode == Instruction::MATCH) { - match = DecodeMatch(pos, endpos); + match = DecodeMatch(pos, asmap); if (match == INVALID) break; // Match bits straddle EOF matchlen = std::bit_width(match) - 1; - if (bits < matchlen) break; // Not enough input bits + if ((ip_bits_end - ip_bit) < matchlen) break; // Not enough input bits for (uint32_t bit = 0; bit < matchlen; bit++) { - if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) { + if (ConsumeBitBE(ip_bit, ip) != ((match >> (matchlen - 1 - bit)) & 1)) { return default_asn; } - bits--; } } else if (opcode == Instruction::DEFAULT) { - default_asn = DecodeASN(pos, endpos); + default_asn = DecodeASN(pos, asmap); if (default_asn == INVALID) break; // ASN straddles EOF } else { break; // Instruction straddles EOF @@ -130,50 +141,47 @@ uint32_t Interpret(const std::vector &asmap, const std::vector &ip) return 0; // 0 is not a valid ASN } -bool SanityCheckASMap(const std::vector& asmap, int bits) +bool SanityCheckASMap(const std::vector& asmap, int bits) { - const std::vector::const_iterator begin = asmap.begin(), endpos = asmap.end(); - std::vector::const_iterator pos = begin; + size_t pos{0}; + const size_t endpos{asmap.size() * 8}; std::vector> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left) jumps.reserve(bits); Instruction prevopcode = Instruction::JUMP; bool had_incomplete_match = false; while (pos != endpos) { - uint32_t offset = pos - begin; - if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction - Instruction opcode = DecodeType(pos, endpos); + if (!jumps.empty() && pos >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction + Instruction opcode = DecodeType(pos, asmap); if (opcode == Instruction::RETURN) { if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN) - uint32_t asn = DecodeASN(pos, endpos); + uint32_t asn = DecodeASN(pos, asmap); if (asn == INVALID) return false; // ASN straddles EOF if (jumps.empty()) { // Nothing to execute anymore if (endpos - pos > 7) return false; // Excessive padding while (pos != endpos) { - if (*pos) return false; // Nonzero padding bit - ++pos; + if (ConsumeBitLE(pos, asmap)) return false; // Nonzero padding bit } return true; // Sanely reached EOF } else { // Continue by pretending we jumped to the next instruction - offset = pos - begin; - if (offset != jumps.back().first) return false; // Unreachable code + if (pos != jumps.back().first) return false; // Unreachable code bits = jumps.back().second; // Restore the number of bits we would have had left after this jump jumps.pop_back(); prevopcode = Instruction::JUMP; } } else if (opcode == Instruction::JUMP) { - uint32_t jump = DecodeJump(pos, endpos); + uint32_t jump = DecodeJump(pos, asmap); if (jump == INVALID) return false; // Jump offset straddles EOF - if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range + if (int64_t{jump} > static_cast(endpos - pos)) return false; // Jump out of range if (bits == 0) return false; // Consuming bits past the end of the input --bits; - uint32_t jump_offset = pos - begin + jump; + uint32_t jump_offset = pos + jump; if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps jumps.emplace_back(jump_offset, bits); prevopcode = Instruction::JUMP; } else if (opcode == Instruction::MATCH) { - uint32_t match = DecodeMatch(pos, endpos); + uint32_t match = DecodeMatch(pos, asmap); if (match == INVALID) return false; // Match bits straddle EOF int matchlen = std::bit_width(match) - 1; if (prevopcode != Instruction::MATCH) had_incomplete_match = false; @@ -184,7 +192,7 @@ bool SanityCheckASMap(const std::vector& asmap, int bits) prevopcode = Instruction::MATCH; } else if (opcode == Instruction::DEFAULT) { if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one) - uint32_t asn = DecodeASN(pos, endpos); + uint32_t asn = DecodeASN(pos, asmap); if (asn == INVALID) return false; // ASN straddles EOF prevopcode = Instruction::DEFAULT; } else { @@ -194,27 +202,24 @@ bool SanityCheckASMap(const std::vector& asmap, int bits) return false; // Reached EOF without RETURN instruction } -std::vector DecodeAsmap(fs::path path) +std::vector DecodeAsmap(fs::path path) { - std::vector bits; FILE *filestr = fsbridge::fopen(path, "rb"); AutoFile file{filestr}; if (file.IsNull()) { LogWarning("Failed to open asmap file from disk"); - return bits; + return {}; } int64_t length{file.size()}; LogInfo("Opened asmap file %s (%d bytes) from disk", fs::quoted(fs::PathToString(path)), length); - uint8_t cur_byte; - for (int i = 0; i < length; ++i) { - file >> cur_byte; - for (int bit = 0; bit < 8; ++bit) { - bits.push_back((cur_byte >> bit) & 1); - } - } - if (!SanityCheckASMap(bits, 128)) { + + std::vector buffer(length); + file.read(buffer); + + if (!SanityCheckASMap(buffer, 128)) { LogWarning("Sanity check of asmap file %s failed", fs::quoted(fs::PathToString(path))); return {}; } - return bits; + + return buffer; } diff --git a/src/util/asmap.h b/src/util/asmap.h index 872608693fc..b107ad25f46 100644 --- a/src/util/asmap.h +++ b/src/util/asmap.h @@ -7,14 +7,15 @@ #include +#include #include #include -uint32_t Interpret(const std::vector &asmap, const std::vector &ip); +uint32_t Interpret(const std::vector& asmap, const std::vector& ip); -bool SanityCheckASMap(const std::vector& asmap, int bits); +bool SanityCheckASMap(const std::vector& asmap, int bits); /** Read asmap from provided binary file */ -std::vector DecodeAsmap(fs::path path); +std::vector DecodeAsmap(fs::path path); #endif // BITCOIN_UTIL_ASMAP_H diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index 65298270737..762f5a1434b 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -18,7 +18,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal ASMAP = 'src/test/data/asmap.raw' # path to unit test skeleton asmap -VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853' +VERSION = '6dfbc157b8a97b6e9fc7fc08d4e43d30247bbf62055eaa1098f46db9885855e3' def expected_messages(filename): return [f'Opened asmap file "{filename}" (59 bytes) from disk', From 385c34a05261846dac2b42d47f69b317f534dd40 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Tue, 22 Apr 2025 23:44:06 +0200 Subject: [PATCH 2/5] refactor: Unify asmap version calculation and naming Calculate the asmap version only in one place: A dedicated function in util/asmap. The version was also referred to as asmap checksum in several places. To avoid confusion call it asmap version everywhere. --- src/addrman.cpp | 16 ++++++++-------- src/init.cpp | 2 +- src/netgroup.cpp | 6 ++---- src/netgroup.h | 4 ++-- src/util/asmap.cpp | 11 +++++++++++ src/util/asmap.h | 3 +++ 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 206b54118e8..2e5149093c4 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -156,7 +156,7 @@ void AddrManImpl::Serialize(Stream& s_) const * * for each new bucket: * * number of elements * * for each element: index in the serialized "all new addresses" - * * asmap checksum + * * asmap version * * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it * as incompatible. This is necessary because it did not check the version number on @@ -222,9 +222,9 @@ void AddrManImpl::Serialize(Stream& s_) const } } } - // Store asmap checksum after bucket entries so that it + // Store asmap version after bucket entries so that it // can be ignored by older clients for backward compatibility. - s << m_netgroupman.GetAsmapChecksum(); + s << m_netgroupman.GetAsmapVersion(); } template @@ -330,16 +330,16 @@ void AddrManImpl::Unserialize(Stream& s_) } } - // If the bucket count and asmap checksum haven't changed, then attempt + // If the bucket count and asmap version haven't changed, then attempt // to restore the entries to the buckets/positions they were in before // serialization. - uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum()}; - uint256 serialized_asmap_checksum; + uint256 supplied_asmap_version{m_netgroupman.GetAsmapVersion()}; + uint256 serialized_asmap_version; if (format >= Format::V2_ASMAP) { - s >> serialized_asmap_checksum; + s >> serialized_asmap_version; } const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && - serialized_asmap_checksum == supplied_asmap_checksum}; + serialized_asmap_version == supplied_asmap_version}; if (!restore_bucketing) { LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n"); diff --git a/src/init.cpp b/src/init.cpp index 6c14f501eef..198c07aec55 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1581,7 +1581,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); return false; } - const uint256 asmap_version = (HashWriter{} << asmap).GetHash(); + const uint256 asmap_version = AsmapVersion(asmap);; LogInfo("Using asmap version %s for IP bucketing", asmap_version.ToString()); } else { LogInfo("Using /16 prefix for IP bucketing"); diff --git a/src/netgroup.cpp b/src/netgroup.cpp index 970aa2b4120..5a81cd6bd9f 100644 --- a/src/netgroup.cpp +++ b/src/netgroup.cpp @@ -10,11 +10,9 @@ #include -uint256 NetGroupManager::GetAsmapChecksum() const +uint256 NetGroupManager::GetAsmapVersion() const { - if (!m_asmap.size()) return {}; - - return (HashWriter{} << m_asmap).GetHash(); + return AsmapVersion(m_asmap); } std::vector NetGroupManager::GetGroup(const CNetAddr& address) const diff --git a/src/netgroup.h b/src/netgroup.h index 399876e5bac..7f08fef766e 100644 --- a/src/netgroup.h +++ b/src/netgroup.h @@ -20,8 +20,8 @@ public: : m_asmap{std::move(asmap)} {} - /** Get a checksum identifying the asmap being used. */ - uint256 GetAsmapChecksum() const; + /** Get the asmap version, a checksum identifying the asmap being used. */ + uint256 GetAsmapVersion() const; /** * Get the canonical identifier of the network group for address. diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index 23cf6d165ac..1993103a9d9 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -5,9 +5,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -223,3 +225,12 @@ std::vector DecodeAsmap(fs::path path) return buffer; } + +uint256 AsmapVersion(const std::vector& data) +{ + if (data.empty()) return {}; + + HashWriter asmap_hasher; + asmap_hasher << data; + return asmap_hasher.GetHash(); +} diff --git a/src/util/asmap.h b/src/util/asmap.h index b107ad25f46..722aeec7ab5 100644 --- a/src/util/asmap.h +++ b/src/util/asmap.h @@ -5,6 +5,7 @@ #ifndef BITCOIN_UTIL_ASMAP_H #define BITCOIN_UTIL_ASMAP_H +#include #include #include @@ -17,5 +18,7 @@ bool SanityCheckASMap(const std::vector& asmap, int bits); /** Read asmap from provided binary file */ std::vector DecodeAsmap(fs::path path); +/** Calculate the asmap version, a checksum identifying the asmap being used. */ +uint256 AsmapVersion(const std::vector& data); #endif // BITCOIN_UTIL_ASMAP_H From cf4943fdcdd167a56c278ba094cecb0fa241a8f8 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Wed, 23 Apr 2025 00:13:32 +0200 Subject: [PATCH 3/5] refactor: Use span instead of vector for data in util/asmap This prevents holding the asmap data in memory twice. The version hash changes due to spans being serialized without their size-prefix (unlike vectors). --- src/bench/addrman.cpp | 2 +- src/init.cpp | 16 ++++++------- src/netgroup.cpp | 1 + src/netgroup.h | 39 +++++++++++++++++++++++++++----- src/test/addrman_tests.cpp | 11 ++++----- src/test/fuzz/asmap.cpp | 4 ++-- src/test/fuzz/p2p_handshake.cpp | 2 +- src/test/fuzz/util/net.h | 6 +++-- src/test/netbase_tests.cpp | 4 ++-- src/test/util/setup_common.cpp | 2 +- src/util/asmap.cpp | 38 +++++++++++++++++++------------ src/util/asmap.h | 11 +++++---- test/functional/feature_asmap.py | 2 +- 13 files changed, 87 insertions(+), 51 deletions(-) diff --git a/src/bench/addrman.cpp b/src/bench/addrman.cpp index 3f900b3693e..d28030e4baa 100644 --- a/src/bench/addrman.cpp +++ b/src/bench/addrman.cpp @@ -24,7 +24,7 @@ static constexpr size_t NUM_SOURCES = 64; static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256; -static NetGroupManager EMPTY_NETGROUPMAN{{}}; +static auto EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()}; static constexpr uint32_t ADDRMAN_CONSISTENCY_CHECK_RATIO{0}; static std::vector g_sources; diff --git a/src/init.cpp b/src/init.cpp index 198c07aec55..6bd055f753c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1560,9 +1560,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) ApplyArgsManOptions(args, peerman_opts); { - - // Read asmap file if configured - std::vector asmap; + // Read asmap file if configured and initialize + // Netgroupman with or without it + assert(!node.netgroupman); if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) { fs::path asmap_path = args.GetPathArg("-asmap"); if (asmap_path.empty()) { @@ -1576,21 +1576,19 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); return false; } - asmap = DecodeAsmap(asmap_path); + std::vector asmap{DecodeAsmap(asmap_path)}; if (asmap.size() == 0) { InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); return false; } - const uint256 asmap_version = AsmapVersion(asmap);; + const uint256 asmap_version = AsmapVersion(asmap); + node.netgroupman = std::make_unique(NetGroupManager::WithLoadedAsmap(std::move(asmap))); LogInfo("Using asmap version %s for IP bucketing", asmap_version.ToString()); } else { + node.netgroupman = std::make_unique(NetGroupManager::NoAsmap()); LogInfo("Using /16 prefix for IP bucketing"); } - // Initialize netgroup manager - assert(!node.netgroupman); - node.netgroupman = std::make_unique(std::move(asmap)); - // Initialize addrman assert(!node.addrman); uiInterface.InitMessage(_("Loading P2P addresses…")); diff --git a/src/netgroup.cpp b/src/netgroup.cpp index 5a81cd6bd9f..9b0d4d8d3c2 100644 --- a/src/netgroup.cpp +++ b/src/netgroup.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include diff --git a/src/netgroup.h b/src/netgroup.h index 7f08fef766e..2482d66a817 100644 --- a/src/netgroup.h +++ b/src/netgroup.h @@ -16,9 +16,22 @@ */ class NetGroupManager { public: - explicit NetGroupManager(std::vector&& asmap) - : m_asmap{std::move(asmap)} - {} + NetGroupManager(const NetGroupManager&) = delete; + NetGroupManager(NetGroupManager&&) = default; + NetGroupManager& operator=(const NetGroupManager&) = delete; + NetGroupManager& operator=(NetGroupManager&&) = delete; + + static NetGroupManager WithEmbeddedAsmap(std::span asmap) { + return NetGroupManager(asmap, {}); + } + + static NetGroupManager WithLoadedAsmap(std::vector&& asmap) { + return NetGroupManager(std::span{asmap}, std::move(asmap)); + } + + static NetGroupManager NoAsmap() { + return NetGroupManager({}, {}); + } /** Get the asmap version, a checksum identifying the asmap being used. */ uint256 GetAsmapVersion() const; @@ -53,7 +66,10 @@ public: bool UsingASMap() const; private: - /** Compressed IP->ASN mapping, loaded from a file when a node starts. + /** Compressed IP->ASN mapping. + * + * Data may be loaded from a file when a node starts or embedded in the + * binary. * * This mapping is then used for bucketing nodes in Addrman and for * ensuring we connect to a diverse set of peers in Connman. The map is @@ -70,8 +86,19 @@ private: * re-bucketed. * * This is initialized in the constructor, const, and therefore is - * thread-safe. */ - const std::vector m_asmap; + * thread-safe. m_asmap can either point to m_loaded_asmap which holds + * data loaded from an external file at runtime or it can point to embedded + * asmap data. + */ + const std::span m_asmap; + std::vector m_loaded_asmap; + + explicit NetGroupManager(std::span embedded_asmap, std::vector&& loaded_asmap) + : m_asmap{embedded_asmap}, + m_loaded_asmap{std::move(loaded_asmap)} + { + assert(m_loaded_asmap.empty() || m_asmap.data() == m_loaded_asmap.data()); + } }; #endif // BITCOIN_NETGROUP_H diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 9cd667bac3d..b70d97ee313 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -24,7 +24,7 @@ using namespace std::literals; using node::NodeContext; using util::ToString; -static NetGroupManager EMPTY_NETGROUPMAN{{}}; +static auto EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()}; static const bool DETERMINISTIC{true}; static int32_t GetCheckRatio(const NodeContext& node_ctx) @@ -584,8 +584,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy) // 101.8.0.0/16 AS8 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) { - std::vector asmap(test::data::asmap.begin(), test::data::asmap.end()); - NetGroupManager ngm_asmap{std::move(asmap)}; + auto ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)}; CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE); @@ -638,8 +637,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { - std::vector asmap(test::data::asmap.begin(), test::data::asmap.end()); - NetGroupManager ngm_asmap{std::move(asmap)}; + auto ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)}; CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); @@ -716,8 +714,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) BOOST_AUTO_TEST_CASE(addrman_serialization) { - std::vector asmap1(test::data::asmap.begin(), test::data::asmap.end()); - NetGroupManager netgroupman{std::move(asmap1)}; + auto netgroupman{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)}; const auto ratio = GetCheckRatio(m_node); auto addrman_asmap1 = std::make_unique(netgroupman, DETERMINISTIC, ratio); diff --git a/src/test/fuzz/asmap.cpp b/src/test/fuzz/asmap.cpp index 40dde196db6..edf783386cf 100644 --- a/src/test/fuzz/asmap.cpp +++ b/src/test/fuzz/asmap.cpp @@ -29,7 +29,7 @@ FUZZ_TARGET(asmap) if (buffer.size() < size_t(1 + asmap_size + addr_size)) return; std::vector asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP; std::ranges::copy(std::as_bytes(buffer.subspan(1, asmap_size)), std::back_inserter(asmap)); - if (!SanityCheckASMap(asmap, 128)) return; + if (!CheckStandardAsmap(asmap)) return; const uint8_t* addr_data = buffer.data() + 1 + asmap_size; CNetAddr net_addr; @@ -42,6 +42,6 @@ FUZZ_TARGET(asmap) memcpy(&ipv4, addr_data, addr_size); net_addr.SetIP(CNetAddr{ipv4}); } - NetGroupManager netgroupman{std::move(asmap)}; + auto netgroupman{NetGroupManager::WithEmbeddedAsmap(asmap)}; (void)netgroupman.GetMappedAS(net_addr); } diff --git a/src/test/fuzz/p2p_handshake.cpp b/src/test/fuzz/p2p_handshake.cpp index aec7387eb2f..a56e1e5f1fb 100644 --- a/src/test/fuzz/p2p_handshake.cpp +++ b/src/test/fuzz/p2p_handshake.cpp @@ -48,7 +48,7 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize) chainman.ResetIbd(); node::Warnings warnings{}; - NetGroupManager netgroupman{{}}; + auto netgroupman{NetGroupManager::NoAsmap()}; AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0}; auto peerman = PeerManager::make(connman, addrman, /*banman=*/nullptr, chainman, diff --git a/src/test/fuzz/util/net.h b/src/test/fuzz/util/net.h index 93aabcc3e51..2c169392df8 100644 --- a/src/test/fuzz/util/net.h +++ b/src/test/fuzz/util/net.h @@ -236,8 +236,10 @@ public: [[nodiscard]] inline NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider& fuzzed_data_provider) noexcept { std::vector asmap{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; - if (!SanityCheckASMap(asmap, 128)) asmap.clear(); - return NetGroupManager(std::move(asmap)); + if (!CheckStandardAsmap(asmap)) { + return NetGroupManager::NoAsmap(); + } + return NetGroupManager::WithLoadedAsmap(std::move(asmap)); } inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp index 379978451d8..24f3fd80b3c 100644 --- a/src/test/netbase_tests.cpp +++ b/src/test/netbase_tests.cpp @@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(subnet_test) BOOST_AUTO_TEST_CASE(netbase_getgroup) { - NetGroupManager netgroupman{{}}; // use /16 + auto netgroupman{NetGroupManager::NoAsmap()}; // use /16 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector({0})); // Local -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector({0})); // !Valid -> !Routable() BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector({0})); // RFC1918 -> !Routable() @@ -631,7 +631,7 @@ BOOST_AUTO_TEST_CASE(asmap_test_vectors) "63dc33d28f757a4a5e15d6a08"_hex}; // Construct NetGroupManager with this data. - NetGroupManager netgroup{std::vector(ASMAP_DATA.begin(), ASMAP_DATA.end())}; + auto netgroup{NetGroupManager::WithEmbeddedAsmap(ASMAP_DATA)}; BOOST_CHECK(netgroup.UsingASMap()); // Check some randomly-generated IPv6 addresses in it (biased towards the very beginning and diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 454f7a74912..6f962a17f4c 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -344,7 +344,7 @@ TestingSetup::TestingSetup( if (!opts.setup_net) return; - m_node.netgroupman = std::make_unique(/*asmap=*/std::vector{}); + m_node.netgroupman = std::make_unique(NetGroupManager::NoAsmap()); m_node.addrman = std::make_unique(*m_node.netgroupman, /*deterministic=*/false, m_node.args->GetIntArg("-checkaddrman", 0)); diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index 1993103a9d9..a40a0615914 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -38,7 +39,7 @@ inline bool ConsumeBitBE(uint8_t& bitpos, std::span bytes) noex return bit; } -uint32_t DecodeBits(size_t& bitpos, const std::vector& data, uint8_t minval, const std::vector& bit_sizes) +uint32_t DecodeBits(size_t& bitpos, const std::span data, uint8_t minval, const std::span bit_sizes) { uint32_t val = minval; bool bit; @@ -71,35 +72,33 @@ enum class Instruction : uint32_t DEFAULT = 3, }; -const std::vector TYPE_BIT_SIZES{0, 0, 1}; -Instruction DecodeType(size_t& bitpos, const std::vector& data) +constexpr uint8_t TYPE_BIT_SIZES[]{0, 0, 1}; +Instruction DecodeType(size_t& bitpos, const std::span data) { return Instruction(DecodeBits(bitpos, data, 0, TYPE_BIT_SIZES)); } -const std::vector ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; -uint32_t DecodeASN(size_t& bitpos, const std::vector& data) +constexpr uint8_t ASN_BIT_SIZES[]{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; +uint32_t DecodeASN(size_t& bitpos, const std::span data) { return DecodeBits(bitpos, data, 1, ASN_BIT_SIZES); } - -const std::vector MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8}; -uint32_t DecodeMatch(size_t& bitpos, const std::vector& data) +constexpr uint8_t MATCH_BIT_SIZES[]{1, 2, 3, 4, 5, 6, 7, 8}; +uint32_t DecodeMatch(size_t& bitpos, const std::span data) { return DecodeBits(bitpos, data, 2, MATCH_BIT_SIZES); } - -const std::vector JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; -uint32_t DecodeJump(size_t& bitpos, const std::vector& data) +constexpr uint8_t JUMP_BIT_SIZES[]{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; +uint32_t DecodeJump(size_t& bitpos, const std::span data) { return DecodeBits(bitpos, data, 17, JUMP_BIT_SIZES); } } -uint32_t Interpret(const std::vector& asmap, const std::vector& ip) +uint32_t Interpret(const std::span asmap, const std::span ip) { size_t pos{0}; const size_t endpos{asmap.size() * 8}; @@ -143,7 +142,7 @@ uint32_t Interpret(const std::vector& asmap, const std::vector& asmap, int bits) +bool SanityCheckASMap(const std::span asmap, int bits) { size_t pos{0}; const size_t endpos{asmap.size() * 8}; @@ -204,6 +203,15 @@ bool SanityCheckASMap(const std::vector& asmap, int bits) return false; // Reached EOF without RETURN instruction } +bool CheckStandardAsmap(const std::span data) +{ + if (!SanityCheckASMap(data, 128)) { + LogWarning("Sanity check of asmap data failed\n"); + return false; + } + return true; +} + std::vector DecodeAsmap(fs::path path) { FILE *filestr = fsbridge::fopen(path, "rb"); @@ -218,7 +226,7 @@ std::vector DecodeAsmap(fs::path path) std::vector buffer(length); file.read(buffer); - if (!SanityCheckASMap(buffer, 128)) { + if (!CheckStandardAsmap(buffer)) { LogWarning("Sanity check of asmap file %s failed", fs::quoted(fs::PathToString(path))); return {}; } @@ -226,7 +234,7 @@ std::vector DecodeAsmap(fs::path path) return buffer; } -uint256 AsmapVersion(const std::vector& data) +uint256 AsmapVersion(const std::span data) { if (data.empty()) return {}; diff --git a/src/util/asmap.h b/src/util/asmap.h index 722aeec7ab5..1d5f6e81187 100644 --- a/src/util/asmap.h +++ b/src/util/asmap.h @@ -10,15 +10,18 @@ #include #include +#include #include -uint32_t Interpret(const std::vector& asmap, const std::vector& ip); +uint32_t Interpret(std::span asmap, std::span ip); -bool SanityCheckASMap(const std::vector& asmap, int bits); +bool SanityCheckASMap(std::span asmap, int bits); +/** Check standard asmap data (128 bits for IPv6) */ +bool CheckStandardAsmap(std::span data); -/** Read asmap from provided binary file */ +/** Read and check asmap from provided binary file */ std::vector DecodeAsmap(fs::path path); /** Calculate the asmap version, a checksum identifying the asmap being used. */ -uint256 AsmapVersion(const std::vector& data); +uint256 AsmapVersion(std::span data); #endif // BITCOIN_UTIL_ASMAP_H diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index 762f5a1434b..8ad59c82ccf 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -18,7 +18,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal ASMAP = 'src/test/data/asmap.raw' # path to unit test skeleton asmap -VERSION = '6dfbc157b8a97b6e9fc7fc08d4e43d30247bbf62055eaa1098f46db9885855e3' +VERSION = 'bafc9da308f45179443bd1d22325400ac9104f741522d003e3fac86700f68895' def expected_messages(filename): return [f'Opened asmap file "{filename}" (59 bytes) from disk', From 79e97d45c16f043d23ba318a661cc39ec53cf760 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sat, 15 Nov 2025 21:53:11 +0200 Subject: [PATCH 4/5] doc: Add more extensive docs to asmap implementation Also makes minor improvement on the python implementation documentation. --- contrib/asmap/asmap.py | 2 +- src/util/asmap.cpp | 147 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 131 insertions(+), 18 deletions(-) diff --git a/contrib/asmap/asmap.py b/contrib/asmap/asmap.py index 2ae84a3f311..292048b6d84 100644 --- a/contrib/asmap/asmap.py +++ b/contrib/asmap/asmap.py @@ -157,7 +157,7 @@ class _Instruction(Enum): JUMP = 1 # A match instruction, encoded as [1,1,0] inspects 1 or more of the next unused bits # in the input with its argument. If they all match, execution continues. If they do - # not, failure is returned. If a default instruction has been executed before, instead + # not, failure (represented by 0) is returned. If a default instruction has been executed before, instead # of failure the default instruction's argument is returned. It is followed by an # integer in match encoding, and a subprogram. That value is at least 2 bits and at # most 9 bits. An n-bit value signifies matching (n-1) bits in the input with the lower diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index a40a0615914..ed1d3b0be4b 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -21,10 +21,36 @@ #include #include +/* + * ASMap (Autonomous System Map) Implementation + * + * Provides a compressed mapping from IP address prefixes to Autonomous System Numbers (ASNs). + * Uses a binary trie structure encoded as bytecode instructions that are interpreted + * at runtime to find the ASN for a given IP address. + * + * The format of the asmap data is a bit-packed binary format where the entire mapping + * is treated as a continuous sequence of bits. Instructions and their arguments are + * encoded using variable numbers of bits and concatenated together without regard for + * byte boundaries. The bits are stored in bytes using little-endian bit ordering. + * + * The data structure internally represents the mapping as a binary trie where: + * - Unassigned subnets (no ASN mapping present) map to 0 + * - Subnets mapped entirely to one ASN become leaf nodes + * - Subnets whose lower and upper halves have different mappings branch into subtrees + * + * The encoding uses variable-length integers and four instruction types (RETURN, JUMP, + * MATCH, DEFAULT) to efficiently represent the trie. + */ + namespace { +// Indicates decoding errors or invalid data constexpr uint32_t INVALID = 0xFFFFFFFF; +/** + * Extract a single bit from byte array using little-endian bit ordering (LSB first). + * Used for ASMap data. + */ inline bool ConsumeBitLE(size_t& bitpos, std::span bytes) noexcept { const bool bit = (std::to_integer(bytes[bitpos / 8]) >> (bitpos % 8)) & 1; @@ -32,6 +58,10 @@ inline bool ConsumeBitLE(size_t& bitpos, std::span bytes) noexc return bit; } +/** + * Extract a single bit from byte array using big-endian bit ordering (MSB first). + * Used for IP addresses to match network byte order conventions. + */ inline bool ConsumeBitBE(uint8_t& bitpos, std::span bytes) noexcept { const bool bit = (std::to_integer(bytes[bitpos / 8]) >> (7 - (bitpos % 8))) & 1; @@ -39,24 +69,43 @@ inline bool ConsumeBitBE(uint8_t& bitpos, std::span bytes) noex return bit; } +/** + * Variable-length integer decoder using a custom encoding scheme. + * + * The encoding is easiest to describe using an example. Let's say minval=100 and + * bit_sizes=[4,2,2,3]. In that case: + * - x in [100..115]: encoded as [0] + [4-bit BE encoding of (x-100)] + * - x in [116..119]: encoded as [1,0] + [2-bit BE encoding of (x-116)] + * - x in [120..123]: encoded as [1,1,0] + [2-bit BE encoding of (x-120)] + * - x in [124..131]: encoded as [1,1,1] + [3-bit BE encoding of (x-124)] + * + * In general, every number is encoded as: + * - First, k "1"-bits, where k is the class the number falls in + * - Then, a "0"-bit, unless k is the highest class + * - Lastly, bit_sizes[k] bits encoding in big endian the position within that class + */ uint32_t DecodeBits(size_t& bitpos, const std::span data, uint8_t minval, const std::span bit_sizes) { - uint32_t val = minval; + uint32_t val = minval; // Start with minimum encodable value bool bit; for (auto bit_sizes_it = bit_sizes.begin(); bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) { - if (bit_sizes_it + 1 != bit_sizes.end()) { + // Read continuation bit to determine if we're in this class + if (bit_sizes_it + 1 != bit_sizes.end()) { // Unless we're in the last class if (bitpos >= data.size() * 8) break; bit = ConsumeBitLE(bitpos, data); } else { - bit = 0; + bit = 0; // Last class has no continuation bit } if (bit) { - val += (1 << *bit_sizes_it); + // If the value will not fit in this class, subtract its range from val, + // emit a "1" bit and continue with the next class + val += (1 << *bit_sizes_it); // Add size of this class } else { + // Decode the position within this class in big endian for (int b = 0; b < *bit_sizes_it; b++) { if (bitpos >= data.size() * 8) return INVALID; // Reached EOF in mantissa bit = ConsumeBitLE(bitpos, data); - val += bit << (*bit_sizes_it - 1 - b); + val += bit << (*bit_sizes_it - 1 - b); // Big-endian within the class } return val; } @@ -64,40 +113,72 @@ uint32_t DecodeBits(size_t& bitpos, const std::span data, uint8 return INVALID; // Reached EOF in exponent } +/** + * Instruction Set + * + * The instruction set is designed to efficiently encode a binary trie + * that maps IP prefixes to ASNs. Each instruction type serves a specific + * role in trie traversal and evaluation. + */ enum class Instruction : uint32_t { + // A return instruction, encoded as [0], returns a constant ASN. + // It is followed by an integer using the ASN encoding. RETURN = 0, + // A jump instruction, encoded as [1,0], inspects the next unused bit in the input + // and either continues execution (if 0), or skips a specified number of bits (if 1). + // It is followed by an integer using jump encoding. JUMP = 1, + // A match instruction, encoded as [1,1,0], inspects 1 or more of the next unused bits + // in the input. If they all match, execution continues. If not, the default ASN is returned + // (or 0 if unset). The match value encodes both the pattern and its length. MATCH = 2, + // A default instruction, encoded as [1,1,1], sets the default variable to its argument, + // and continues execution. It is followed by an integer in ASN encoding. DEFAULT = 3, }; +// Instruction type encoding: RETURN=[0], JUMP=[1,0], MATCH=[1,1,0], DEFAULT=[1,1,1] constexpr uint8_t TYPE_BIT_SIZES[]{0, 0, 1}; Instruction DecodeType(size_t& bitpos, const std::span data) { return Instruction(DecodeBits(bitpos, data, 0, TYPE_BIT_SIZES)); } +// ASN encoding: Can encode ASNs from 1 to ~16.7 million. +// Uses variable-length encoding optimized for real-world ASN distribution. +// ASN 0 is reserved and used if there isn't a match. constexpr uint8_t ASN_BIT_SIZES[]{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; uint32_t DecodeASN(size_t& bitpos, const std::span data) { return DecodeBits(bitpos, data, 1, ASN_BIT_SIZES); } +// MATCH argument: Values in [2, 511]. The highest set bit determines the match length +// n ∈ [1,8]; the lower n-1 bits are the pattern to compare. constexpr uint8_t MATCH_BIT_SIZES[]{1, 2, 3, 4, 5, 6, 7, 8}; uint32_t DecodeMatch(size_t& bitpos, const std::span data) { return DecodeBits(bitpos, data, 2, MATCH_BIT_SIZES); } +// JUMP offset: Minimum value 17. Variable-length coded and may be large +// for skipping big subtrees. constexpr uint8_t JUMP_BIT_SIZES[]{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; uint32_t DecodeJump(size_t& bitpos, const std::span data) { return DecodeBits(bitpos, data, 17, JUMP_BIT_SIZES); } -} +} // anonymous namespace +/** + * Execute the ASMap bytecode to find the ASN for an IP + * + * This function interprets the asmap bytecode and uses bits from the IP + * address to navigate through the encoded trie structure, ultimately + * returning an ASN value. + */ uint32_t Interpret(const std::span asmap, const std::span ip) { size_t pos{0}; @@ -110,38 +191,53 @@ uint32_t Interpret(const std::span asmap, const std::span= static_cast(endpos - pos)) break; // Jumping past EOF - if (ConsumeBitBE(ip_bit, ip)) { - pos += jump; + if (ConsumeBitBE(ip_bit, ip)) { // Check next IP bit (big-endian) + pos += jump; // Bit = 1: skip to right subtree } + // Bit = 0: fall through to left subtree } else if (opcode == Instruction::MATCH) { + // Compare multiple IP bits against a pattern + // The match value encodes both length and pattern: + // - highest set bit position determines length (bit_width - 1) + // - lower bits contain the pattern to compare match = DecodeMatch(pos, asmap); if (match == INVALID) break; // Match bits straddle EOF - matchlen = std::bit_width(match) - 1; + matchlen = std::bit_width(match) - 1; // An n-bit value matches n-1 input bits if ((ip_bits_end - ip_bit) < matchlen) break; // Not enough input bits for (uint32_t bit = 0; bit < matchlen; bit++) { if (ConsumeBitBE(ip_bit, ip) != ((match >> (matchlen - 1 - bit)) & 1)) { - return default_asn; + return default_asn; // Pattern mismatch - use default } } + // Pattern matched - continue execution } else if (opcode == Instruction::DEFAULT) { + // Update the default ASN for subsequent MATCH failures default_asn = DecodeASN(pos, asmap); if (default_asn == INVALID) break; // ASN straddles EOF } else { break; // Instruction straddles EOF } } - assert(false); // Reached EOF without RETURN, or aborted (see any of the breaks above) - should have been caught by SanityCheckASMap below + // Reached EOF without RETURN, or aborted (see any of the breaks above) + // - should have been caught by SanityCheckASMap below + assert(false); return 0; // 0 is not a valid ASN } +/** + * Validates ASMap structure by simulating all possible execution paths. + * Ensures well-formed bytecode, valid jumps, and proper termination. + */ bool SanityCheckASMap(const std::span asmap, int bits) { size_t pos{0}; @@ -149,12 +245,16 @@ bool SanityCheckASMap(const std::span asmap, int bits) std::vector> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left) jumps.reserve(bits); Instruction prevopcode = Instruction::JUMP; - bool had_incomplete_match = false; + bool had_incomplete_match = false; // Track <8 bit matches for efficiency check + while (pos != endpos) { - if (!jumps.empty() && pos >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction + // There was a jump into the middle of the previous instruction + if (!jumps.empty() && pos >= jumps.back().first) return false; + Instruction opcode = DecodeType(pos, asmap); if (opcode == Instruction::RETURN) { - if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN) + // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN) + if (prevopcode == Instruction::DEFAULT) return false; uint32_t asn = DecodeASN(pos, asmap); if (asn == INVALID) return false; // ASN straddles EOF if (jumps.empty()) { @@ -179,20 +279,22 @@ bool SanityCheckASMap(const std::span asmap, int bits) --bits; uint32_t jump_offset = pos + jump; if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps - jumps.emplace_back(jump_offset, bits); + jumps.emplace_back(jump_offset, bits); // Queue jump target for validation prevopcode = Instruction::JUMP; } else if (opcode == Instruction::MATCH) { uint32_t match = DecodeMatch(pos, asmap); if (match == INVALID) return false; // Match bits straddle EOF int matchlen = std::bit_width(match) - 1; if (prevopcode != Instruction::MATCH) had_incomplete_match = false; - if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete + // Within a sequence of matches only at most one should be incomplete + if (matchlen < 8 && had_incomplete_match) return false; had_incomplete_match = (matchlen < 8); if (bits < matchlen) return false; // Consuming bits past the end of the input bits -= matchlen; prevopcode = Instruction::MATCH; } else if (opcode == Instruction::DEFAULT) { - if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one) + // There should not be two successive DEFAULTs (they could be combined into one) + if (prevopcode == Instruction::DEFAULT) return false; uint32_t asn = DecodeASN(pos, asmap); if (asn == INVALID) return false; // ASN straddles EOF prevopcode = Instruction::DEFAULT; @@ -203,6 +305,10 @@ bool SanityCheckASMap(const std::span asmap, int bits) return false; // Reached EOF without RETURN instruction } +/** + * Provides a safe interface for validating ASMap data before use. + * Returns true if the data is valid for 128 bits long inputs. + */ bool CheckStandardAsmap(const std::span data) { if (!SanityCheckASMap(data, 128)) { @@ -212,6 +318,9 @@ bool CheckStandardAsmap(const std::span data) return true; } +/** + * Loads an ASMap file from disk and validates it. + */ std::vector DecodeAsmap(fs::path path) { FILE *filestr = fsbridge::fopen(path, "rb"); @@ -223,6 +332,7 @@ std::vector DecodeAsmap(fs::path path) int64_t length{file.size()}; LogInfo("Opened asmap file %s (%d bytes) from disk", fs::quoted(fs::PathToString(path)), length); + // Read entire file into memory std::vector buffer(length); file.read(buffer); @@ -234,6 +344,9 @@ std::vector DecodeAsmap(fs::path path) return buffer; } +/** + * Computes SHA256 hash of ASMap data for versioning and consistency checks. + */ uint256 AsmapVersion(const std::span data) { if (data.empty()) return {}; From 4fec726c4d352daf2fb4a7e5ed463e44c8815ddb Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sun, 16 Nov 2025 23:21:29 +0200 Subject: [PATCH 5/5] refactor: Simplify Interpret asmap function This aligns it more with SanityCheckAsmap and reduces variable scope. Also unify asmap casing in SanityCheckAsmap function name. Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com> --- src/test/fuzz/asmap_direct.cpp | 4 ++-- src/util/asmap.cpp | 24 +++++++++++------------- src/util/asmap.h | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/test/fuzz/asmap_direct.cpp b/src/test/fuzz/asmap_direct.cpp index fa77553282e..a338c1336f4 100644 --- a/src/test/fuzz/asmap_direct.cpp +++ b/src/test/fuzz/asmap_direct.cpp @@ -51,7 +51,7 @@ FUZZ_TARGET(asmap_direct) // Checks on asmap auto asmap = BitsToBytes(buffer.first(sep_pos)); - if (SanityCheckASMap(asmap, ip_len)) { + if (SanityCheckAsmap(asmap, ip_len)) { // Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid. for (size_t prefix_len = sep_pos - 1; prefix_len > 0; --prefix_len) { auto prefix = BitsToBytes(buffer.first(prefix_len)); @@ -59,7 +59,7 @@ FUZZ_TARGET(asmap_direct) // asmap, since they will contain some zero padding bits in the last // byte. if (prefix.size() == asmap.size()) continue; - assert(!SanityCheckASMap(prefix, ip_len)); + assert(!SanityCheckAsmap(prefix, ip_len)); } // No address input should trigger assertions in interpreter diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index ed1d3b0be4b..4d4ed4fe869 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -186,18 +186,16 @@ uint32_t Interpret(const std::span asmap, const std::span= static_cast(endpos - pos)) break; // Jumping past EOF @@ -210,11 +208,11 @@ uint32_t Interpret(const std::span asmap, const std::span> (matchlen - 1 - bit)) & 1)) { return default_asn; // Pattern mismatch - use default } @@ -229,7 +227,7 @@ uint32_t Interpret(const std::span asmap, const std::span asmap, const std::span asmap, int bits) +bool SanityCheckAsmap(const std::span asmap, int bits) { size_t pos{0}; const size_t endpos{asmap.size() * 8}; @@ -311,7 +309,7 @@ bool SanityCheckASMap(const std::span asmap, int bits) */ bool CheckStandardAsmap(const std::span data) { - if (!SanityCheckASMap(data, 128)) { + if (!SanityCheckAsmap(data, 128)) { LogWarning("Sanity check of asmap data failed\n"); return false; } diff --git a/src/util/asmap.h b/src/util/asmap.h index 1d5f6e81187..5d65e219318 100644 --- a/src/util/asmap.h +++ b/src/util/asmap.h @@ -15,7 +15,7 @@ uint32_t Interpret(std::span asmap, std::span ip); -bool SanityCheckASMap(std::span asmap, int bits); +bool SanityCheckAsmap(std::span asmap, int bits); /** Check standard asmap data (128 bits for IPv6) */ bool CheckStandardAsmap(std::span data);