Skip to content

Commit

Permalink
fixup: use const spans for response access instead of vector reference
Browse files Browse the repository at this point in the history
  • Loading branch information
laanwj committed May 23, 2024
1 parent c6df2ee commit 0f26953
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/util/pcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ static CNetAddr PCPUnwrapAddress(Span<const uint8_t> wrapped_addr)

//! PCP or NAT-PMP send-receive loop.
static std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &protocol, Span<const uint8_t> request, int num_tries,
std::function<bool(Span<uint8_t>)> check_packet)
std::function<bool(const Span<const uint8_t>)> check_packet)
{
// UDP is a potentially lossy protocol, so we try to send again a few times.
uint8_t response[PCP_MAX_SIZE];
Expand Down Expand Up @@ -296,7 +296,7 @@ std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &g
request[NATPMP_HDR_OP_OFS] = NATPMP_REQUEST | NATPMP_OP_GETEXTERNAL;

auto recv_res = PCPSendRecv(sock, "natpmp", request, num_tries,
[&](Span<uint8_t> response) -> bool {
[&](const Span<const uint8_t> response) -> bool {
if (response.size() < NATPMP_GETEXTERNAL_RESPONSE_SIZE) {
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n");
return false; // Wasn't response to what we expected, try receiving next packet.
Expand All @@ -310,7 +310,7 @@ std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &g

struct in_addr external_addr;
if (recv_res) {
const std::vector<uint8_t> &response = *recv_res;
const std::span<const uint8_t> response = *recv_res;

Assume(response.size() >= NATPMP_GETEXTERNAL_RESPONSE_SIZE);
uint16_t result_code = ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS);
Expand All @@ -333,7 +333,7 @@ std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &g
WriteBE32(request.data() + NATPMP_MAP_REQUEST_LIFETIME_OFS, lifetime);

recv_res = PCPSendRecv(sock, "natpmp", request, num_tries,
[&](Span<uint8_t> response) -> bool {
[&](const Span<const uint8_t> response) -> bool {
if (response.size() < NATPMP_MAP_RESPONSE_SIZE) {
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n");
return false; // Wasn't response to what we expected, try receiving next packet.
Expand All @@ -351,7 +351,7 @@ std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &g
});

if (recv_res) {
const std::vector<uint8_t> &response = *recv_res;
const std::span<uint8_t> response = *recv_res;

Assume(response.size() >= NATPMP_MAP_RESPONSE_SIZE);
uint16_t result_code = ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS);
Expand Down Expand Up @@ -446,7 +446,7 @@ std::variant<MappingResult, MappingError> PCPRequestPortMap(const PCPMappingNonc
// Receive loop.
bool is_natpmp = false;
auto recv_res = PCPSendRecv(sock, "pcp", request, num_tries,
[&](Span<uint8_t> response) -> bool {
[&](const Span<const uint8_t> response) -> bool {
// Unsupported version according to RFC6887 appendix A and RFC6886 section 3.5, can fall back to NAT-PMP.
if (response.size() == NATPMP_RESPONSE_HDR_SIZE && response[PCP_HDR_VERSION_OFS] == NATPMP_VERSION && response[PCP_RESPONSE_HDR_RESULT_OFS] == NATPMP_RESULT_UNSUPP_VERSION) {
is_natpmp = true;
Expand Down Expand Up @@ -482,14 +482,14 @@ std::variant<MappingResult, MappingError> PCPRequestPortMap(const PCPMappingNonc
return MappingError::UNSUPP_VERSION;
}

const std::vector<uint8_t> &response = *recv_res;
const std::span<const uint8_t> response = *recv_res;
// If we get here, we got a valid MAP response to our request.
// Check to see if we got the result we expected.
Assume(response.size() >= (PCP_HDR_SIZE + PCP_MAP_SIZE));
uint8_t result_code = response[PCP_RESPONSE_HDR_RESULT_OFS];
uint32_t lifetime_ret = ReadBE32(response.data() + PCP_HDR_LIFETIME_OFS);
uint16_t external_port = ReadBE16(response.data() + PCP_HDR_SIZE + PCP_MAP_EXTERNAL_PORT_OFS);
CNetAddr external_addr{PCPUnwrapAddress(Span(response).subspan(PCP_HDR_SIZE + PCP_MAP_EXTERNAL_IP_OFS, 16))};
CNetAddr external_addr{PCPUnwrapAddress(response.subspan(PCP_HDR_SIZE + PCP_MAP_EXTERNAL_IP_OFS, 16))};
if (result_code != PCP_RESULT_SUCCESS) {
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Mapping failed with result %s\n", PCPResultString(result_code));
return MappingError::PROTOCOL_ERROR;
Expand Down

0 comments on commit 0f26953

Please sign in to comment.