Inside Bitcoin
Opening the selected file…
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) 2021-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 <netgroup.h>7#include <hash.h>8#include <uint256.h>9#include <util/asmap.h>10#include <util/log.h>12#include <cassert>13#include <cstddef>14#include <utility>16NetGroupManager::NetGroupManager(std::span<const std::byte> embedded_asmap, std::vector<std::byte>&& loaded_asmap)17: m_asmap{embedded_asmap},18m_loaded_asmap{std::move(loaded_asmap)},19m_asmap_version{AsmapVersion(m_asmap)}20{21assert(m_loaded_asmap.empty() || m_asmap.data() == m_loaded_asmap.data());22}24uint256 NetGroupManager::GetAsmapVersion() const25{26return m_asmap_version;27}29std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const30{31std::vector<unsigned char> vchRet;32// If non-empty asmap is supplied and the address is IPv4/IPv6,33// return ASN to be used for bucketing.34uint32_t asn = GetMappedAS(address);35if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).36vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket37for (int i = 0; i < 4; i++) {38vchRet.push_back((asn >> (8 * i)) & 0xFF);39}40return vchRet;41}43vchRet.push_back(address.GetNetClass());44int nStartByte{0};45int nBits{0};47if (address.IsLocal()) {48// all local addresses belong to the same group49} else if (address.IsInternal()) {50// All internal-usage addresses get their own group.51// Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes().52nStartByte = INTERNAL_IN_IPV6_PREFIX.size();53nBits = ADDR_INTERNAL_SIZE * 8;54} else if (!address.IsRoutable()) {55// all other unroutable addresses belong to the same group56} else if (address.HasLinkedIPv4()) {57// IPv4 addresses (and mapped IPv4 addresses) use /16 groups58uint32_t ipv4 = address.GetLinkedIPv4();59vchRet.push_back((ipv4 >> 24) & 0xFF);60vchRet.push_back((ipv4 >> 16) & 0xFF);61return vchRet;62} else if (address.IsTor() || address.IsI2P()) {63nBits = 4;64} else if (address.IsCJDNS()) {65// Treat in the same way as Tor and I2P because the address in all of66// them is "random" bytes (derived from a public key). However in CJDNS67// the first byte is a constant (see CJDNS_PREFIX), so the random bytes68// come after it. Thus skip the constant 8 bits at the start.69nBits = 12;70} else if (address.IsHeNet()) {71// for he.net, use /36 groups72nBits = 36;73} else {74// for the rest of the IPv6 network, use /32 groups75nBits = 32;76}78// Push our address onto vchRet.79auto addr_bytes = address.GetAddrBytes();80const size_t num_bytes = nBits / 8;81vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);82nBits %= 8;83// ...for the last byte, push nBits and for the rest of the byte push 1's84if (nBits > 0) {85assert(num_bytes < addr_bytes.size());86vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));87}89return vchRet;90}92uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const93{94uint32_t net_class = address.GetNetClass();95if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) {96return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.97}98std::vector<std::byte> ip_bytes(16);99if (address.HasLinkedIPv4()) {100// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)101std::copy_n(std::as_bytes(std::span{IPV4_IN_IPV6_PREFIX}).begin(),102IPV4_IN_IPV6_PREFIX.size(), ip_bytes.begin());103uint32_t ipv4 = address.GetLinkedIPv4();104for (int i = 0; i < 4; ++i) {105ip_bytes[12 + i] = std::byte((ipv4 >> (24 - i * 8)) & 0xFF);106}107} else {108// Use all 128 bits of the IPv6 address otherwise109assert(address.IsIPv6());110auto addr_bytes = address.GetAddrBytes();111assert(addr_bytes.size() == ip_bytes.size());112std::copy_n(std::as_bytes(std::span{addr_bytes}).begin(),113addr_bytes.size(), ip_bytes.begin());114}115uint32_t mapped_as = Interpret(m_asmap, ip_bytes);116return mapped_as;117}119void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {120std::set<uint32_t> clearnet_asns{};121int unmapped_count{0};123for (const auto& addr : clearnet_addrs) {124uint32_t asn = GetMappedAS(addr);125if (asn == 0) {126++unmapped_count;127continue;128}129clearnet_asns.insert(asn);130}132LogInfo("ASMap Health Check: %i clearnet peers are mapped to %i ASNs with %i peers being unmapped\n", clearnet_addrs.size(), clearnet_asns.size(), unmapped_count);133}135bool NetGroupManager::UsingASMap() const {136return m_asmap.size() > 0;137}