Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <netaddress.h>6#include <netgroup.h>7#include <test/fuzz/fuzz.h>8#include <util/asmap.h>9#include <util/strencodings.h>11#include <cstdint>12#include <vector>14using namespace util::hex_literals;16//! asmap code that consumes nothing17static const std::vector<std::byte> IPV6_PREFIX_ASMAP = {};19//! asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map)20static const auto IPV4_PREFIX_ASMAP = "fb03ec0fb03fc0fe00fb03ec0fb03fc0fe00fb03ec0fb0fffffeff"_hex_v;22FUZZ_TARGET(asmap)23{24// Encoding: [7 bits: asmap size] [1 bit: ipv6?] [3-130 bytes: asmap] [4 or 16 bytes: addr]25if (buffer.size() < 1 + 3 + 4) return;26int asmap_size = 3 + (buffer[0] & 127);27bool ipv6 = buffer[0] & 128;28const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;29if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;30std::vector<std::byte> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;31std::ranges::copy(std::as_bytes(buffer.subspan(1, asmap_size)), std::back_inserter(asmap));32if (!CheckStandardAsmap(asmap)) return;34const uint8_t* addr_data = buffer.data() + 1 + asmap_size;35CNetAddr net_addr;36if (ipv6) {37assert(addr_size == ADDR_IPV6_SIZE);38net_addr.SetLegacyIPv6({addr_data, addr_size});39} else {40assert(addr_size == ADDR_IPV4_SIZE);41in_addr ipv4;42memcpy(&ipv4, addr_data, addr_size);43net_addr.SetIP(CNetAddr{ipv4});44}45auto netgroupman{NetGroupManager::WithEmbeddedAsmap(asmap)};46(void)netgroupman.GetMappedAS(net_addr);47}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.