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.
502// PCP IPv4 failure (no resources).503BOOST_AUTO_TEST_CASE(pcp_ipv4_fail_no_resources)504{505const std::vector<TestOp> script{506{5070ms, TestOp::SEND,508{5090x02, 0x01, 0x00, 0x00, // version, opcode5100x00, 0x00, 0x03, 0xe8, // lifetime5110x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP5120x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce5130x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved5140x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port5150x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP516}, 0517},518{519500ms, TestOp::RECV,520{5210x02, 0x81, 0x00, 0x08, // version, opcode, result 0x08: no resources5220x00, 0x00, 0x00, 0x00, // granted lifetime5230x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved5240x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce5250x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved5260x04, 0xd2, 0x00, 0x00, // internal port, assigned external port5270x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // assigned external IP528}, 0529},530};531CreateSock = [this, &script](int domain, int type, int protocol) {532if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);533return std::unique_ptr<PCPTestSock>();534};536auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, g_interrupt, 3, 1000ms);538MappingError* err = std::get_if<MappingError>(&res);539BOOST_REQUIRE(err);540BOOST_CHECK_EQUAL(*err, MappingError::NO_RESOURCES);541}543// PCP IPv4 failure (test NATPMP downgrade scenario).544BOOST_AUTO_TEST_CASE(pcp_ipv4_fail_unsupported_version)545{546const std::vector<TestOp> script{547{5480ms, TestOp::SEND,549{5500x02, 0x01, 0x00, 0x00, // version, opcode5510x00, 0x00, 0x03, 0xe8, // lifetime5520x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP5530x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce5540x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved5550x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port5560x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP557}, 0558},559{560500ms, TestOp::RECV,561{5620x00, 0x81, 0x00, 0x01, // version, opcode, result 0x01: unsupported version5630x00, 0x00, 0x00, 0x00,564}, 0565},566};567CreateSock = [this, &script](int domain, int type, int protocol) {568if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);569return std::unique_ptr<PCPTestSock>();570};572auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, g_interrupt, 3, 1000ms);574MappingError* err = std::get_if<MappingError>(&res);575BOOST_REQUIRE(err);576BOOST_CHECK_EQUAL(*err, MappingError::UNSUPP_VERSION);577}579// NAT-PMP IPv4 protocol error scenarii.580BOOST_AUTO_TEST_CASE(natpmp_protocol_error)581{582// First scenario: non-0 result code when requesting external IP.583std::vector<TestOp> script{584{5850ms, TestOp::SEND,586{5870x00, 0x00, // version, opcode (request external IP)588}, 0589},590{5912ms, TestOp::RECV,592{5930x00, 0x80, 0x00, 0x42, // version, opcode (external IP), result code (*NOT* success)5940x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch5950x01, 0x02, 0x03, 0x04, // external IP address596}, 0597},598};599CreateSock = [this, &script](int domain, int type, int protocol) {600if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);601return std::unique_ptr<PCPTestSock>();602};604auto res = NATPMPRequestPortMap(default_gateway_ipv4, 1234, 1000, g_interrupt, 1, 200ms);606MappingError* err = std::get_if<MappingError>(&res);607BOOST_REQUIRE(err);608BOOST_CHECK_EQUAL(*err, MappingError::PROTOCOL_ERROR);610// First scenario: non-0 result code when requesting port mapping.611script = {612{6130ms, TestOp::SEND,614{6150x00, 0x00, // version, opcode (request external IP)616}, 0617},618{6192ms, TestOp::RECV,620{6210x00, 0x80, 0x00, 0x00, // version, opcode (external IP), result code (success)6220x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch6230x01, 0x02, 0x03, 0x04, // external IP address624}, 0625},626{6270ms, TestOp::SEND,628{6290x00, 0x02, 0x00, 0x00, // version, opcode (request map TCP)6300x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port6310x00, 0x00, 0x03, 0xe8, // requested mapping lifetime in seconds632}, 0633},634{6352ms, TestOp::RECV,636{6370x00, 0x82, 0x00, 0x43, // version, opcode (mapped TCP)6380x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch6390x04, 0xd2, 0x04, 0xd2, // internal port, mapped external port6400x00, 0x00, 0x01, 0xf4, // mapping lifetime in seconds641}, 0642},643};644CreateSock = [this, &script](int domain, int type, int protocol) {645if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);646return std::unique_ptr<PCPTestSock>();647};649res = NATPMPRequestPortMap(default_gateway_ipv4, 1234, 1000, g_interrupt, 1, 200ms);651err = std::get_if<MappingError>(&res);652BOOST_REQUIRE(err);653BOOST_CHECK_EQUAL(*err, MappingError::PROTOCOL_ERROR);654}656// PCP IPv4 protocol error scenario.657BOOST_AUTO_TEST_CASE(pcp_protocol_error)658{659const std::vector<TestOp> script{660{6610ms, TestOp::SEND,662{6630x02, 0x01, 0x00, 0x00, // version, opcode6640x00, 0x00, 0x03, 0xe8, // lifetime6650x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP6660x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce6670x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved6680x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port6690x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP670}, 0671},672{673250ms, TestOp::RECV, // 250ms delay before answer674{6750x02, 0x81, 0x00, 0x42, // version, opcode, result error6760x00, 0x00, 0x01, 0xf4, // granted lifetime6770x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved6780x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce6790x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved6800x04, 0xd2, 0x04, 0xd2, // internal port, assigned external port6810x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, // assigned external IP682}, 0683},684};685CreateSock = [this, &script](int domain, int type, int protocol) {686if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);687return std::unique_ptr<PCPTestSock>();688};690auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, g_interrupt, 1, 1000ms);692MappingError* err = std::get_if<MappingError>(&res);693BOOST_REQUIRE(err);694BOOST_CHECK_EQUAL(*err, MappingError::PROTOCOL_ERROR);695}697BOOST_AUTO_TEST_SUITE_END()
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.