Skip to content

Commit

Permalink
fixup: add result code map
Browse files Browse the repository at this point in the history
  • Loading branch information
laanwj committed May 2, 2024
1 parent 4d866db commit f66e2e8
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/ipv6-pinhole-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ constexpr size_t PCP_MAP_NONCE_SIZE = 12;
//! Result code representing SUCCESS status (7.4).
constexpr uint8_t PCP_RESULT_SUCCESS = 0;

//! Mapping of PCP result code to string (7.4).
static const std::map<uint8_t, std::string> PCP_RESULT_STR{
{0, "SUCCESS"},
{1, "UNSUPP_VERSION"},
{2, "NOT_AUTHORIZED"},
{3, "MALFORMED_REQUEST"},
{4, "UNSUPP_OPCODE"},
{5, "UNSUPP_OPTION"},
{6, "MALFORMED_OPTION"},
{7, "NETWORK_FAILURE"},
{8, "NO_RESOURCES"},
{9, "UNSUPP_PROTOCOL"},
{10, "USER_EX_QUOTA"},
{11, "CANNOT_PROVIDE_EXTERNAL"},
{12, "ADDRESS_MISMATCH"},
{13, "EXCESSIVE_REMOTE_PEER"},
};

//! Human-readable string from result code.
std::string PCPResultString(uint8_t result_code)
{
auto result_i = PCP_RESULT_STR.find(result_code);
return strprintf("%s (code %d)", result_i == PCP_RESULT_STR.end() ? "(unknown)" : result_i->second, result_code);
}

//! Find IPv6 default gateway.
std::optional<sockaddr_in6> FindIPv6DefaultGateway()
{
Expand Down Expand Up @@ -217,13 +242,13 @@ bool OpenPinhole(const sockaddr_in6 &gateway, const struct in6_addr &my_addr, ui
}
// If we get here, we got a valid MAP response to our request.
// Check to see if we got the result we expected.
int result_code = response[3];
uint8_t result_code = response[3];
uint32_t lifetime_ret = ReadBE32(response + 4);
uint16_t external_port = ReadBE16(response + PCP_RESPONSE_HDR_SIZE + 18);
struct in6_addr external_addr;
memcpy(&external_addr, response + PCP_RESPONSE_HDR_SIZE + 20, ADDR_IPV6_SIZE);
if (result_code != PCP_RESULT_SUCCESS) {
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp6: Mapping failed with result code %d\n", result_code);
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp6: Mapping failed with result %s\n", PCPResultString(result_code));
return false;
} else {
// Mapping was successful. Just to be sure, check that the external port and address match what we expect.
Expand Down

0 comments on commit f66e2e8

Please sign in to comment.