net: parse NAT-PMP result code as 16-bit BE in PCP fallback #36800

pull rahadbhuiya wants to merge 1 commits into bitcoin:master from rahadbhuiya:net-pcp-result-code-fix changing 2 files +29 −1
  1. rahadbhuiya commented at 6:24 AM on November 8, 2026: none

    When checking whether a gateway replied with NAT-PMP unsupported version (RFC 6887 appendix A and RFC 6886 section 3.5), PCPRequestPortMap mistakenly used PCP_RESPONSE_HDR_RESULT_OFS (3) and compared only a single byte against NATPMP_RESULT_UNSUPP_VERSION.

    NAT-PMP result codes are 16-bit big-endian values starting at offset 2 (NATPMP_RESPONSE_HDR_RESULT_OFS). A 16-bit result code with low byte 1 and a non-zero high byte (such as 0x0101) would previously be incorrectly treated as UNSUPP_VERSION.

    This PR fixes the check to use ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS) matching the rest of pcp.cpp (lines 348 and 390), and uses NATPMP_HDR_VERSION_OFS for the version field offset. A unit test is added to pcp_tests.cpp.

  2. DrahtBot added the label P2P on Nov 8, 2026
  3. DrahtBot commented at 6:24 AM on November 8, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK willcl-ark

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

    LLM Linter (✨ experimental)

    Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

    • src/test/pcp_tests.cpp: PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, g_interrupt, 1, 1000ms)

    <sup>2026-12-02 03:41:45</sup>

  4. fanquake commented at 3:26 PM on December 1, 2026: member

    @willcl-ark you might have some thoughts in regards to NAT-PMP?

  5. willcl-ark commented at 8:08 PM on December 1, 2026: member

    This change seems correct to me.

    Ralph flags a slight issue in the regression test we might want to tidy up before merging: link.

    Could we use {0ms, TestOp::SEND, {}, 0} here? The adjacent pcp_ipv4_fail_unsupported_version test already checks the exact outbound bytes.

  6. net: parse NAT-PMP result code as 16-bit BE in PCP fallback
    When checking whether a gateway replied with NAT-PMP unsupported version (RFC 6887 appendix A and RFC 6886 section 3.5), PCPRequestPortMap mistakenly used PCP_RESPONSE_HDR_RESULT_OFS (3) and compared only a single byte against NATPMP_RESULT_UNSUPP_VERSION.
    
    NAT-PMP result codes are 16-bit big-endian values starting at offset 2 (NATPMP_RESPONSE_HDR_RESULT_OFS). A 16-bit result code with low byte 1 and a non-zero high byte (such as 0x0101) would previously be incorrectly treated as UNSUPP_VERSION.
    
    Use ReadBE16 with NATPMP_RESPONSE_HDR_RESULT_OFS instead, matching the rest of pcp.cpp, and use NATPMP_HDR_VERSION_OFS for the version check. Add a unit test covering this case.
    53e997e41f
  7. rahadbhuiya force-pushed on Dec 2, 2026
  8. rahadbhuiya commented at 3:42 AM on December 2, 2026: none

    Thanks for the review @willcl-ark! Updated the test to use {0ms, TestOp::SEND, {}, 0} and added ASSERT_DEBUG_LOG("pcp: Response too small") so we explicitly verify the 8-byte 0x0101 response reaches the PCP size check.

  9. fanquake commented at 11:16 AM on December 2, 2026: member
  10. fanquake added this to the milestone 33.0 on Dec 2, 2026
  11. willcl-ark approved
  12. willcl-ark commented at 11:46 AM on December 2, 2026: member

    utACK 53e997e41f888ba211bda5d3a9168da3aad58bc0

    I don't have capacity to check this right now on my home network. But the changes look correct to me.

Labels

Milestone
33.0


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-05 04:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me