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 <test/fuzz/FuzzedDataProvider.h>7#include <test/fuzz/fuzz.h>8#include <test/fuzz/util/net.h>9#include <test/util/random.h>11#include <cassert>12#include <cstdint>13#include <vector>15FUZZ_TARGET(netaddress)16{17SeedRandomStateForTest(SeedRand::ZEROS);18FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());20const CNetAddr net_addr = ConsumeNetAddr(fuzzed_data_provider);21(void)net_addr.GetNetClass();22if (net_addr.GetNetwork() == Network::NET_IPV4) {23assert(net_addr.IsIPv4());24}25if (net_addr.GetNetwork() == Network::NET_IPV6) {26assert(net_addr.IsIPv6());27}28if (net_addr.GetNetwork() == Network::NET_ONION) {29assert(net_addr.IsTor());30}31if (net_addr.GetNetwork() == Network::NET_I2P) {32assert(net_addr.IsI2P());33}34if (net_addr.GetNetwork() == Network::NET_CJDNS) {35assert(net_addr.IsCJDNS());36}37if (net_addr.GetNetwork() == Network::NET_INTERNAL) {38assert(net_addr.IsInternal());39}40if (net_addr.GetNetwork() == Network::NET_UNROUTABLE) {41assert(!net_addr.IsRoutable());42}43(void)net_addr.IsBindAny();44if (net_addr.IsInternal()) {45assert(net_addr.GetNetwork() == Network::NET_INTERNAL);46}47if (net_addr.IsIPv4()) {48assert(net_addr.GetNetwork() == Network::NET_IPV4 || net_addr.GetNetwork() == Network::NET_UNROUTABLE);49}50if (net_addr.IsIPv6()) {51assert(net_addr.GetNetwork() == Network::NET_IPV6 || net_addr.GetNetwork() == Network::NET_UNROUTABLE);52}53(void)net_addr.IsLocal();54if (net_addr.IsRFC1918() || net_addr.IsRFC2544() || net_addr.IsRFC6598() || net_addr.IsRFC5737() || net_addr.IsRFC3927()) {55assert(net_addr.IsIPv4());56}57(void)net_addr.IsRFC2544();58if (net_addr.IsRFC3849() || net_addr.IsRFC3964() || net_addr.IsRFC4380() || net_addr.IsRFC4843() || net_addr.IsRFC7343() || net_addr.IsRFC4862() || net_addr.IsRFC6052() || net_addr.IsRFC6145() || net_addr.IsRFC9637()) {59assert(net_addr.IsIPv6());60}61(void)net_addr.IsRFC3927();62(void)net_addr.IsRFC3964();63if (net_addr.IsRFC4193()) {64assert(net_addr.GetNetwork() == Network::NET_INTERNAL || net_addr.GetNetwork() == Network::NET_UNROUTABLE);65}66(void)net_addr.IsRFC4380();67(void)net_addr.IsRFC4843();68(void)net_addr.IsRFC4862();69(void)net_addr.IsRFC5737();70(void)net_addr.IsRFC6052();71(void)net_addr.IsRFC6145();72(void)net_addr.IsRFC6598();73(void)net_addr.IsRFC7343();74(void)net_addr.IsRFC9637();75const bool routable{net_addr.IsRoutable()};76if (!routable) {77assert(net_addr.GetNetwork() == Network::NET_UNROUTABLE || net_addr.GetNetwork() == Network::NET_INTERNAL);78}79if (net_addr.IsTor()) {80assert(net_addr.GetNetwork() == Network::NET_ONION);81}82if (net_addr.IsI2P()) {83assert(net_addr.GetNetwork() == Network::NET_I2P);84}85if (net_addr.IsCJDNS()) {86assert(net_addr.GetNetwork() == Network::NET_CJDNS);87}88if (!net_addr.IsValid()) {89assert(!routable);90}91(void)net_addr.ToStringAddr();93const CSubNet sub_net{net_addr, fuzzed_data_provider.ConsumeIntegral<uint8_t>()};94(void)sub_net.IsValid();95(void)sub_net.ToString();97const CService service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};98(void)service.GetKey();99(void)service.GetPort();100(void)service.ToStringAddrPort();101(void)CServiceHash()(service);102(void)CServiceHash(0, 0)(service);104const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);105(void)net_addr.GetReachabilityFrom(other_net_addr);106(void)sub_net.Match(other_net_addr);108const CService other_service{fuzzed_data_provider.ConsumeBool() ? net_addr : other_net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};109assert((service == other_service) != (service != other_service));110(void)(service < other_service);112if (service.ToStringAddrPort() == other_service.ToStringAddrPort()) {113assert(static_cast<CNetAddr>(service) == static_cast<CNetAddr>(other_service));114}116const CSubNet sub_net_copy_1{net_addr, other_net_addr};117const CSubNet sub_net_copy_2{net_addr};119CNetAddr mutable_net_addr;120mutable_net_addr.SetIP(net_addr);121assert(net_addr == mutable_net_addr);122}
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.