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) 2024-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 <test/fuzz/FuzzedDataProvider.h>6#include <test/fuzz/fuzz.h>7#include <test/fuzz/util.h>8#include <test/fuzz/util/net.h>9#include <test/util/time.h>11#include <common/pcp.h>12#include <logging.h>13#include <util/check.h>14#include <util/threadinterrupt.h>16using namespace std::literals;18//! Fixed nonce to use in PCP port mapping requests.19constexpr PCPMappingNonce PCP_NONCE{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};21//! Number of attempts to request a NAT-PMP or PCP port mapping to the gateway.22constexpr int NUM_TRIES{5};24//! Timeout for each attempt to request a port mapping.25constexpr std::chrono::duration TIMEOUT{100ms};27void port_map_target_init()28{29LogInstance().DisableLogging();30}32FUZZ_TARGET(pcp_request_port_map, .init = port_map_target_init)33{34FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};35FakeSteadyClock steady_clock;37// Create a mocked socket between random (and potentially invalid) client and gateway addresses.38auto CreateSockOrig = CreateSock;39CreateSock = [&](int domain, int type, int protocol) {40if ((domain == AF_INET || domain == AF_INET6) && type == SOCK_DGRAM && protocol == IPPROTO_UDP) {41return std::make_unique<FuzzedSock>(fuzzed_data_provider, steady_clock);42}43return std::unique_ptr<FuzzedSock>();44};46// Perform the port mapping request. The mocked socket will return fuzzer-provided data.47const auto gateway_addr{ConsumeNetAddr(fuzzed_data_provider)};48const auto local_addr{ConsumeNetAddr(fuzzed_data_provider)};49const auto port{fuzzed_data_provider.ConsumeIntegral<uint16_t>()};50const auto lifetime{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};51CThreadInterrupt interrupt;52const auto res{PCPRequestPortMap(PCP_NONCE, gateway_addr, local_addr, port, lifetime, interrupt, NUM_TRIES, TIMEOUT)};54// In case of success the mapping must be consistent with the request.55if (const MappingResult* mapping = std::get_if<MappingResult>(&res)) {56Assert(mapping);57Assert(mapping->internal.GetPort() == port);58mapping->ToString();59}61CreateSock = CreateSockOrig;62}64FUZZ_TARGET(natpmp_request_port_map, .init = port_map_target_init)65{66FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};67FakeSteadyClock steady_clock;69// Create a mocked socket between random (and potentially invalid) client and gateway addresses.70auto CreateSockOrig = CreateSock;71CreateSock = [&](int domain, int type, int protocol) {72if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) {73return std::make_unique<FuzzedSock>(fuzzed_data_provider, steady_clock);74}75return std::unique_ptr<FuzzedSock>();76};78// Perform the port mapping request. The mocked socket will return fuzzer-provided data.79const auto gateway_addr{ConsumeNetAddr(fuzzed_data_provider)};80const auto port{fuzzed_data_provider.ConsumeIntegral<uint16_t>()};81const auto lifetime{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};82CThreadInterrupt interrupt;83const auto res{NATPMPRequestPortMap(gateway_addr, port, lifetime, interrupt, NUM_TRIES, TIMEOUT)};85// In case of success the mapping must be consistent with the request.86if (const MappingResult* mapping = std::get_if<MappingResult>(&res)) {87Assert(mapping);88Assert(mapping->internal.GetPort() == port);89mapping->ToString();90}92CreateSock = CreateSockOrig;93}
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.