Bitcoin Core 30.99.0
P2P Digital Currency
pcp.cpp
Go to the documentation of this file.
1// Copyright (c) 2024-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4
5#include <common/pcp.h>
6
7#include <common/netif.h>
8#include <crypto/common.h>
9#include <logging.h>
10#include <netaddress.h>
11#include <netbase.h>
12#include <random.h>
13#include <span.h>
14#include <util/check.h>
15#include <util/readwritefile.h>
16#include <util/sock.h>
17#include <util/strencodings.h>
19
20namespace {
21
22// RFC6886 NAT-PMP and RFC6887 Port Control Protocol (PCP) implementation.
23// NAT-PMP and PCP use network byte order (big-endian).
24
25// NAT-PMP (v0) protocol constants.
27constexpr uint16_t NATPMP_SERVER_PORT = 5351;
29constexpr uint8_t NATPMP_VERSION = 0;
31constexpr uint8_t NATPMP_REQUEST = 0x00;
33constexpr uint8_t NATPMP_RESPONSE = 0x80;
35constexpr uint8_t NATPMP_OP_GETEXTERNAL = 0x00;
37constexpr uint8_t NATPMP_OP_MAP_TCP = 0x02;
39constexpr size_t NATPMP_REQUEST_HDR_SIZE = 2;
41constexpr size_t NATPMP_RESPONSE_HDR_SIZE = 8;
43constexpr size_t NATPMP_GETEXTERNAL_REQUEST_SIZE = NATPMP_REQUEST_HDR_SIZE + 0;
45constexpr size_t NATPMP_GETEXTERNAL_RESPONSE_SIZE = NATPMP_RESPONSE_HDR_SIZE + 4;
47constexpr size_t NATPMP_MAP_REQUEST_SIZE = NATPMP_REQUEST_HDR_SIZE + 10;
49constexpr size_t NATPMP_MAP_RESPONSE_SIZE = NATPMP_RESPONSE_HDR_SIZE + 8;
50
51// Shared header offsets (RFC6886 3.2, 3.3), relative to start of packet.
53constexpr size_t NATPMP_HDR_VERSION_OFS = 0;
55constexpr size_t NATPMP_HDR_OP_OFS = 1;
57constexpr size_t NATPMP_RESPONSE_HDR_RESULT_OFS = 2;
58
59// GETEXTERNAL response offsets (RFC6886 3.2), relative to start of packet.
61constexpr size_t NATPMP_GETEXTERNAL_RESPONSE_IP_OFS = 8;
62
63// MAP request offsets (RFC6886 3.3), relative to start of packet.
65constexpr size_t NATPMP_MAP_REQUEST_INTERNAL_PORT_OFS = 4;
67constexpr size_t NATPMP_MAP_REQUEST_EXTERNAL_PORT_OFS = 6;
69constexpr size_t NATPMP_MAP_REQUEST_LIFETIME_OFS = 8;
70
71// MAP response offsets (RFC6886 3.3), relative to start of packet.
73constexpr size_t NATPMP_MAP_RESPONSE_INTERNAL_PORT_OFS = 8;
75constexpr size_t NATPMP_MAP_RESPONSE_EXTERNAL_PORT_OFS = 10;
77constexpr size_t NATPMP_MAP_RESPONSE_LIFETIME_OFS = 12;
78
79// Relevant NETPMP result codes (RFC6886 3.5).
81constexpr uint8_t NATPMP_RESULT_SUCCESS = 0;
83constexpr uint8_t NATPMP_RESULT_UNSUPP_VERSION = 1;
85constexpr uint8_t NATPMP_RESULT_NO_RESOURCES = 4;
86
88const std::map<uint16_t, std::string> NATPMP_RESULT_STR{
89 {0, "SUCCESS"},
90 {1, "UNSUPP_VERSION"},
91 {2, "NOT_AUTHORIZED"},
92 {3, "NETWORK_FAILURE"},
93 {4, "NO_RESOURCES"},
94 {5, "UNSUPP_OPCODE"},
95};
96
97// PCP (v2) protocol constants.
99constexpr size_t PCP_MAX_SIZE = 1100;
101constexpr uint16_t PCP_SERVER_PORT = NATPMP_SERVER_PORT;
103constexpr uint8_t PCP_VERSION = 2;
105constexpr uint8_t PCP_REQUEST = NATPMP_REQUEST; // R = 0
107constexpr uint8_t PCP_RESPONSE = NATPMP_RESPONSE; // R = 1
109constexpr uint8_t PCP_OP_MAP = 0x01;
111constexpr uint16_t PCP_PROTOCOL_TCP = 6;
113constexpr size_t PCP_HDR_SIZE = 24;
115constexpr size_t PCP_MAP_SIZE = 36;
116
117// Header offsets shared between request and responses (RFC6887 7.1, 7.2), relative to start of packet.
119constexpr size_t PCP_HDR_VERSION_OFS = NATPMP_HDR_VERSION_OFS;
121constexpr size_t PCP_HDR_OP_OFS = NATPMP_HDR_OP_OFS;
123constexpr size_t PCP_HDR_LIFETIME_OFS = 4;
124
125// Request header offsets (RFC6887 7.1), relative to start of packet.
127constexpr size_t PCP_REQUEST_HDR_IP_OFS = 8;
128
129// Response header offsets (RFC6887 7.2), relative to start of packet.
131constexpr size_t PCP_RESPONSE_HDR_RESULT_OFS = 3;
132
133// MAP request/response offsets (RFC6887 11.1), relative to start of opcode-specific data.
135constexpr size_t PCP_MAP_NONCE_OFS = 0;
137constexpr size_t PCP_MAP_PROTOCOL_OFS = 12;
139constexpr size_t PCP_MAP_INTERNAL_PORT_OFS = 16;
141constexpr size_t PCP_MAP_EXTERNAL_PORT_OFS = 18;
143constexpr size_t PCP_MAP_EXTERNAL_IP_OFS = 20;
144
146constexpr uint8_t PCP_RESULT_SUCCESS = NATPMP_RESULT_SUCCESS;
148constexpr uint8_t PCP_RESULT_NO_RESOURCES = 8;
149
151const std::map<uint8_t, std::string> PCP_RESULT_STR{
152 {0, "SUCCESS"},
153 {1, "UNSUPP_VERSION"},
154 {2, "NOT_AUTHORIZED"},
155 {3, "MALFORMED_REQUEST"},
156 {4, "UNSUPP_OPCODE"},
157 {5, "UNSUPP_OPTION"},
158 {6, "MALFORMED_OPTION"},
159 {7, "NETWORK_FAILURE"},
160 {8, "NO_RESOURCES"},
161 {9, "UNSUPP_PROTOCOL"},
162 {10, "USER_EX_QUOTA"},
163 {11, "CANNOT_PROVIDE_EXTERNAL"},
164 {12, "ADDRESS_MISMATCH"},
165 {13, "EXCESSIVE_REMOTE_PEER"},
166};
167
169std::string NATPMPResultString(uint16_t result_code)
170{
171 auto result_i = NATPMP_RESULT_STR.find(result_code);
172 return strprintf("%s (code %d)", result_i == NATPMP_RESULT_STR.end() ? "(unknown)" : result_i->second, result_code);
173}
174
176std::string PCPResultString(uint8_t result_code)
177{
178 auto result_i = PCP_RESULT_STR.find(result_code);
179 return strprintf("%s (code %d)", result_i == PCP_RESULT_STR.end() ? "(unknown)" : result_i->second, result_code);
180}
181
183[[nodiscard]] bool PCPWrapAddress(std::span<uint8_t> wrapped_addr, const CNetAddr &addr)
184{
185 Assume(wrapped_addr.size() == ADDR_IPV6_SIZE);
186 if (addr.IsIPv4()) {
187 struct in_addr addr4;
188 if (!addr.GetInAddr(&addr4)) return false;
189 // Section 5: "When the address field holds an IPv4 address, an IPv4-mapped IPv6 address [RFC4291] is used (::ffff:0:0/96)."
190 std::memcpy(wrapped_addr.data(), IPV4_IN_IPV6_PREFIX.data(), IPV4_IN_IPV6_PREFIX.size());
191 std::memcpy(wrapped_addr.data() + IPV4_IN_IPV6_PREFIX.size(), &addr4, ADDR_IPV4_SIZE);
192 return true;
193 } else if (addr.IsIPv6()) {
194 struct in6_addr addr6;
195 if (!addr.GetIn6Addr(&addr6)) return false;
196 std::memcpy(wrapped_addr.data(), &addr6, ADDR_IPV6_SIZE);
197 return true;
198 } else {
199 return false;
200 }
201}
202
204CNetAddr PCPUnwrapAddress(std::span<const uint8_t> wrapped_addr)
205{
206 Assume(wrapped_addr.size() == ADDR_IPV6_SIZE);
207 if (util::HasPrefix(wrapped_addr, IPV4_IN_IPV6_PREFIX)) {
208 struct in_addr addr4;
209 std::memcpy(&addr4, wrapped_addr.data() + IPV4_IN_IPV6_PREFIX.size(), ADDR_IPV4_SIZE);
210 return CNetAddr(addr4);
211 } else {
212 struct in6_addr addr6;
213 std::memcpy(&addr6, wrapped_addr.data(), ADDR_IPV6_SIZE);
214 return CNetAddr(addr6);
215 }
216}
217
219std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &protocol, std::span<const uint8_t> request, int num_tries,
220 std::chrono::milliseconds timeout_per_try,
221 std::function<bool(std::span<const uint8_t>)> check_packet,
222 CThreadInterrupt& interrupt)
223{
224 using namespace std::chrono;
225 // UDP is a potentially lossy protocol, so we try to send again a few times.
226 uint8_t response[PCP_MAX_SIZE];
227 bool got_response = false;
228 int recvsz = 0;
229 for (int ntry = 0; !got_response && ntry < num_tries; ++ntry) {
230 if (ntry > 0) {
231 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Retrying (%d)\n", protocol, ntry);
232 }
233 // Dispatch packet to gateway.
234 if (sock.Send(request.data(), request.size(), 0) != static_cast<ssize_t>(request.size())) {
235 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Could not send request: %s\n", protocol, NetworkErrorString(WSAGetLastError()));
236 return std::nullopt; // Network-level error, probably no use retrying.
237 }
238
239 // Wait for response(s) until we get a valid response, a network error, or time out.
240 auto cur_time = time_point_cast<milliseconds>(MockableSteadyClock::now());
241 auto deadline = cur_time + timeout_per_try;
242 while ((cur_time = time_point_cast<milliseconds>(MockableSteadyClock::now())) < deadline) {
243 if (interrupt) return std::nullopt;
244 Sock::Event occurred = 0;
245 if (!sock.Wait(deadline - cur_time, Sock::RECV, &occurred)) {
246 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "%s: Could not wait on socket: %s\n", protocol, NetworkErrorString(WSAGetLastError()));
247 return std::nullopt; // Network-level error, probably no use retrying.
248 }
249 if (!occurred) {
250 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Timeout\n", protocol);
251 break; // Retry.
252 }
253
254 // Receive response.
255 recvsz = sock.Recv(response, sizeof(response), MSG_DONTWAIT);
256 if (recvsz < 0) {
257 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Could not receive response: %s\n", protocol, NetworkErrorString(WSAGetLastError()));
258 return std::nullopt; // Network-level error, probably no use retrying.
259 }
260 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Received response of %d bytes: %s\n", protocol, recvsz, HexStr(std::span(response, recvsz)));
261
262 if (check_packet(std::span<uint8_t>(response, recvsz))) {
263 got_response = true; // Got expected response, break from receive loop as well as from retry loop.
264 break;
265 }
266 }
267 }
268 if (!got_response) {
269 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Giving up after %d tries\n", protocol, num_tries);
270 return std::nullopt;
271 }
272 return std::vector<uint8_t>(response, response + recvsz);
273}
274
275}
276
277std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, CThreadInterrupt& interrupt, int num_tries, std::chrono::milliseconds timeout_per_try)
278{
279 struct sockaddr_storage dest_addr;
280 socklen_t dest_addrlen = sizeof(struct sockaddr_storage);
281
282 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "natpmp: Requesting port mapping port %d from gateway %s\n", port, gateway.ToStringAddr());
283
284 // Validate gateway, make sure it's IPv4. NAT-PMP does not support IPv6.
285 if (!CService(gateway, PCP_SERVER_PORT).GetSockAddr((struct sockaddr*)&dest_addr, &dest_addrlen)) return MappingError::NETWORK_ERROR;
286 if (dest_addr.ss_family != AF_INET) return MappingError::NETWORK_ERROR;
287
288 // Create IPv4 UDP socket
289 auto sock{CreateSock(AF_INET, SOCK_DGRAM, IPPROTO_UDP)};
290 if (!sock) {
291 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Could not create UDP socket: %s\n", NetworkErrorString(WSAGetLastError()));
293 }
294
295 // Associate UDP socket to gateway.
296 if (sock->Connect((struct sockaddr*)&dest_addr, dest_addrlen) != 0) {
297 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Could not connect to gateway: %s\n", NetworkErrorString(WSAGetLastError()));
299 }
300
301 // Use getsockname to get the address toward the default gateway (the internal address).
302 struct sockaddr_in internal;
303 socklen_t internal_addrlen = sizeof(struct sockaddr_in);
304 if (sock->GetSockName((struct sockaddr*)&internal, &internal_addrlen) != 0) {
305 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Could not get sock name: %s\n", NetworkErrorString(WSAGetLastError()));
307 }
308
309 // Request external IP address (RFC6886 section 3.2).
310 std::vector<uint8_t> request(NATPMP_GETEXTERNAL_REQUEST_SIZE);
311 request[NATPMP_HDR_VERSION_OFS] = NATPMP_VERSION;
312 request[NATPMP_HDR_OP_OFS] = NATPMP_REQUEST | NATPMP_OP_GETEXTERNAL;
313
314 auto recv_res = PCPSendRecv(*sock, "natpmp", request, num_tries, timeout_per_try,
315 [&](const std::span<const uint8_t> response) -> bool {
316 if (response.size() < NATPMP_GETEXTERNAL_RESPONSE_SIZE) {
317 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n");
318 return false; // Wasn't response to what we expected, try receiving next packet.
319 }
320 if (response[NATPMP_HDR_VERSION_OFS] != NATPMP_VERSION || response[NATPMP_HDR_OP_OFS] != (NATPMP_RESPONSE | NATPMP_OP_GETEXTERNAL)) {
321 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response to wrong command\n");
322 return false; // Wasn't response to what we expected, try receiving next packet.
323 }
324 return true;
325 },
326 interrupt);
327
328 struct in_addr external_addr;
329 if (recv_res) {
330 const std::span<const uint8_t> response = *recv_res;
331
332 Assume(response.size() >= NATPMP_GETEXTERNAL_RESPONSE_SIZE);
333 uint16_t result_code = ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS);
334 if (result_code != NATPMP_RESULT_SUCCESS) {
335 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Getting external address failed with result %s\n", NATPMPResultString(result_code));
337 }
338
339 std::memcpy(&external_addr, response.data() + NATPMP_GETEXTERNAL_RESPONSE_IP_OFS, ADDR_IPV4_SIZE);
340 } else {
342 }
343
344 // Create TCP mapping request (RFC6886 section 3.3).
345 request = std::vector<uint8_t>(NATPMP_MAP_REQUEST_SIZE);
346 request[NATPMP_HDR_VERSION_OFS] = NATPMP_VERSION;
347 request[NATPMP_HDR_OP_OFS] = NATPMP_REQUEST | NATPMP_OP_MAP_TCP;
348 WriteBE16(request.data() + NATPMP_MAP_REQUEST_INTERNAL_PORT_OFS, port);
349 WriteBE16(request.data() + NATPMP_MAP_REQUEST_EXTERNAL_PORT_OFS, port);
350 WriteBE32(request.data() + NATPMP_MAP_REQUEST_LIFETIME_OFS, lifetime);
351
352 recv_res = PCPSendRecv(*sock, "natpmp", request, num_tries, timeout_per_try,
353 [&](const std::span<const uint8_t> response) -> bool {
354 if (response.size() < NATPMP_MAP_RESPONSE_SIZE) {
355 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n");
356 return false; // Wasn't response to what we expected, try receiving next packet.
357 }
358 if (response[0] != NATPMP_VERSION || response[1] != (NATPMP_RESPONSE | NATPMP_OP_MAP_TCP)) {
359 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response to wrong command\n");
360 return false; // Wasn't response to what we expected, try receiving next packet.
361 }
362 uint16_t internal_port = ReadBE16(response.data() + NATPMP_MAP_RESPONSE_INTERNAL_PORT_OFS);
363 if (internal_port != port) {
364 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response port doesn't match request\n");
365 return false; // Wasn't response to what we expected, try receiving next packet.
366 }
367 return true;
368 },
369 interrupt);
370
371 if (recv_res) {
372 const std::span<uint8_t> response = *recv_res;
373
374 Assume(response.size() >= NATPMP_MAP_RESPONSE_SIZE);
375 uint16_t result_code = ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS);
376 if (result_code != NATPMP_RESULT_SUCCESS) {
377 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Port mapping failed with result %s\n", NATPMPResultString(result_code));
378 if (result_code == NATPMP_RESULT_NO_RESOURCES) {
380 }
382 }
383
384 uint32_t lifetime_ret = ReadBE32(response.data() + NATPMP_MAP_RESPONSE_LIFETIME_OFS);
385 uint16_t external_port = ReadBE16(response.data() + NATPMP_MAP_RESPONSE_EXTERNAL_PORT_OFS);
386 return MappingResult(NATPMP_VERSION, CService(internal.sin_addr, port), CService(external_addr, external_port), lifetime_ret);
387 } else {
389 }
390}
391
392std::variant<MappingResult, MappingError> PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway, const CNetAddr &bind, uint16_t port, uint32_t lifetime, CThreadInterrupt& interrupt, int num_tries, std::chrono::milliseconds timeout_per_try)
393{
394 struct sockaddr_storage dest_addr, bind_addr;
395 socklen_t dest_addrlen = sizeof(struct sockaddr_storage), bind_addrlen = sizeof(struct sockaddr_storage);
396
397 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "pcp: Requesting port mapping for addr %s port %d from gateway %s\n", bind.ToStringAddr(), port, gateway.ToStringAddr());
398
399 // Validate addresses, make sure they're the same network family.
400 if (!CService(gateway, PCP_SERVER_PORT).GetSockAddr((struct sockaddr*)&dest_addr, &dest_addrlen)) return MappingError::NETWORK_ERROR;
401 if (!CService(bind, 0).GetSockAddr((struct sockaddr*)&bind_addr, &bind_addrlen)) return MappingError::NETWORK_ERROR;
402 if (dest_addr.ss_family != bind_addr.ss_family) return MappingError::NETWORK_ERROR;
403
404 // Create UDP socket (IPv4 or IPv6 based on provided gateway).
405 auto sock{CreateSock(dest_addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)};
406 if (!sock) {
407 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not create UDP socket: %s\n", NetworkErrorString(WSAGetLastError()));
409 }
410
411 // Make sure that we send from requested destination address, anything else will be
412 // rejected by a security-conscious router.
413 if (sock->Bind((struct sockaddr*)&bind_addr, bind_addrlen) != 0) {
414 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not bind to address: %s\n", NetworkErrorString(WSAGetLastError()));
416 }
417
418 // Associate UDP socket to gateway.
419 if (sock->Connect((struct sockaddr*)&dest_addr, dest_addrlen) != 0) {
420 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not connect to gateway: %s\n", NetworkErrorString(WSAGetLastError()));
422 }
423
424 // Use getsockname to get the address toward the default gateway (the internal address),
425 // in case we don't know what address to map
426 // (this is only needed if bind is INADDR_ANY, but it doesn't hurt as an extra check).
427 struct sockaddr_storage internal_addr;
428 socklen_t internal_addrlen = sizeof(struct sockaddr_storage);
429 if (sock->GetSockName((struct sockaddr*)&internal_addr, &internal_addrlen) != 0) {
430 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not get sock name: %s\n", NetworkErrorString(WSAGetLastError()));
432 }
433 CService internal;
434 if (!internal.SetSockAddr((struct sockaddr*)&internal_addr, internal_addrlen)) return MappingError::NETWORK_ERROR;
435 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "pcp: Internal address after connect: %s\n", internal.ToStringAddr());
436
437 // Build request packet. Make sure the packet is zeroed so that reserved fields are zero
438 // as required by the spec (and not potentially leak data).
439 // Make sure there's space for the request header and MAP specific request data.
440 std::vector<uint8_t> request(PCP_HDR_SIZE + PCP_MAP_SIZE);
441 // Fill in request header, See RFC6887 Figure 2.
442 size_t ofs = 0;
443 request[ofs + PCP_HDR_VERSION_OFS] = PCP_VERSION;
444 request[ofs + PCP_HDR_OP_OFS] = PCP_REQUEST | PCP_OP_MAP;
445 WriteBE32(request.data() + ofs + PCP_HDR_LIFETIME_OFS, lifetime);
446 if (!PCPWrapAddress(std::span(request).subspan(ofs + PCP_REQUEST_HDR_IP_OFS, ADDR_IPV6_SIZE), internal)) return MappingError::NETWORK_ERROR;
447
448 ofs += PCP_HDR_SIZE;
449
450 // Fill in MAP request packet, See RFC6887 Figure 9.
451 // Randomize mapping nonce (this is repeated in the response, to be able to
452 // correlate requests and responses, and used to authenticate changes to the mapping).
453 std::memcpy(request.data() + ofs + PCP_MAP_NONCE_OFS, nonce.data(), PCP_MAP_NONCE_SIZE);
454 request[ofs + PCP_MAP_PROTOCOL_OFS] = PCP_PROTOCOL_TCP;
455 WriteBE16(request.data() + ofs + PCP_MAP_INTERNAL_PORT_OFS, port);
456 WriteBE16(request.data() + ofs + PCP_MAP_EXTERNAL_PORT_OFS, port);
457 if (!PCPWrapAddress(std::span(request).subspan(ofs + PCP_MAP_EXTERNAL_IP_OFS, ADDR_IPV6_SIZE), bind)) return MappingError::NETWORK_ERROR;
458
459 ofs += PCP_MAP_SIZE;
460 Assume(ofs == request.size());
461
462 // Receive loop.
463 bool is_natpmp = false;
464 auto recv_res = PCPSendRecv(*sock, "pcp", request, num_tries, timeout_per_try,
465 [&](const std::span<const uint8_t> response) -> bool {
466 // Unsupported version according to RFC6887 appendix A and RFC6886 section 3.5, can fall back to NAT-PMP.
467 if (response.size() == NATPMP_RESPONSE_HDR_SIZE && response[PCP_HDR_VERSION_OFS] == NATPMP_VERSION && response[PCP_RESPONSE_HDR_RESULT_OFS] == NATPMP_RESULT_UNSUPP_VERSION) {
468 is_natpmp = true;
469 return true; // Let it through to caller.
470 }
471 if (response.size() < (PCP_HDR_SIZE + PCP_MAP_SIZE)) {
472 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Response too small\n");
473 return false; // Wasn't response to what we expected, try receiving next packet.
474 }
475 if (response[PCP_HDR_VERSION_OFS] != PCP_VERSION || response[PCP_HDR_OP_OFS] != (PCP_RESPONSE | PCP_OP_MAP)) {
476 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Response to wrong command\n");
477 return false; // Wasn't response to what we expected, try receiving next packet.
478 }
479 // Handle MAP opcode response. See RFC6887 Figure 10.
480 // Check that returned mapping nonce matches our request.
481 if (!std::ranges::equal(response.subspan(PCP_HDR_SIZE + PCP_MAP_NONCE_OFS, PCP_MAP_NONCE_SIZE), nonce)) {
482 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Mapping nonce mismatch\n");
483 return false; // Wasn't response to what we expected, try receiving next packet.
484 }
485 uint8_t protocol = response[PCP_HDR_SIZE + 12];
486 uint16_t internal_port = ReadBE16(response.data() + PCP_HDR_SIZE + 16);
487 if (protocol != PCP_PROTOCOL_TCP || internal_port != port) {
488 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Response protocol or port doesn't match request\n");
489 return false; // Wasn't response to what we expected, try receiving next packet.
490 }
491 return true;
492 },
493 interrupt);
494
495 if (!recv_res) {
497 }
498 if (is_natpmp) {
500 }
501
502 const std::span<const uint8_t> response = *recv_res;
503 // If we get here, we got a valid MAP response to our request.
504 // Check to see if we got the result we expected.
505 Assume(response.size() >= (PCP_HDR_SIZE + PCP_MAP_SIZE));
506 uint8_t result_code = response[PCP_RESPONSE_HDR_RESULT_OFS];
507 uint32_t lifetime_ret = ReadBE32(response.data() + PCP_HDR_LIFETIME_OFS);
508 uint16_t external_port = ReadBE16(response.data() + PCP_HDR_SIZE + PCP_MAP_EXTERNAL_PORT_OFS);
509 CNetAddr external_addr{PCPUnwrapAddress(response.subspan(PCP_HDR_SIZE + PCP_MAP_EXTERNAL_IP_OFS, ADDR_IPV6_SIZE))};
510 if (result_code != PCP_RESULT_SUCCESS) {
511 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Mapping failed with result %s\n", PCPResultString(result_code));
512 if (result_code == PCP_RESULT_NO_RESOURCES) {
514 }
516 }
517
518 return MappingResult(PCP_VERSION, CService(internal, port), CService(external_addr, external_port), lifetime_ret);
519}
520
521std::string MappingResult::ToString() const
522{
523 Assume(version == NATPMP_VERSION || version == PCP_VERSION);
524 return strprintf("%s:%s -> %s (for %ds)",
525 version == NATPMP_VERSION ? "natpmp" : "pcp",
529 );
530}
if(!SetupNetworking())
#define Assume(val)
Assume is the identity function.
Definition: check.h:118
Network address.
Definition: netaddress.h:112
std::string ToStringAddr() const
Definition: netaddress.cpp:584
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 (or CJDNS) address.
Definition: netaddress.cpp:646
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:627
bool IsIPv4() const
Definition: netaddress.h:157
bool IsIPv6() const
Definition: netaddress.h:158
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
bool SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
Set CService from a network sockaddr.
Definition: netaddress.cpp:810
std::string ToStringAddrPort() const
Definition: netaddress.cpp:907
A helper class for interruptible sleeps.
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:27
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:45
virtual int Bind(const sockaddr *addr, socklen_t addr_len) const
bind(2) wrapper.
Definition: sock.cpp:60
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
Definition: sock.cpp:139
uint8_t Event
Definition: sock.h:138
virtual int GetSockName(sockaddr *name, socklen_t *name_len) const
getsockname(2) wrapper.
Definition: sock.cpp:106
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:143
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
Definition: sock.cpp:55
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:50
std::variant< MappingResult, MappingError > NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, CThreadInterrupt &interrupt, int num_tries, std::chrono::milliseconds timeout_per_try)
Try to open a port using RFC 6886 NAT-PMP.
Definition: pcp.cpp:277
std::variant< MappingResult, MappingError > PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway, const CNetAddr &bind, uint16_t port, uint32_t lifetime, CThreadInterrupt &interrupt, int num_tries, std::chrono::milliseconds timeout_per_try)
Try to open a port using RFC 6887 Port Control Protocol (PCP).
Definition: pcp.cpp:392
#define WSAGetLastError()
Definition: compat.h:47
#define MSG_DONTWAIT
Definition: compat.h:111
void WriteBE32(B *ptr, uint32_t x)
Definition: common.h:95
void WriteBE16(B *ptr, uint16_t x)
Definition: common.h:88
uint16_t ReadBE16(const B *ptr)
Definition: common.h:64
uint32_t ReadBE32(const B *ptr)
Definition: common.h:72
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
#define LogPrintLevel(category, level,...)
Definition: logging.h:372
unsigned int nonce
Definition: miner_tests.cpp:76
@ NET
Definition: logging.h:66
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
Definition: string.h:257
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:85
static const std::array< uint8_t, 12 > IPV4_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded IPv4 address.
Definition: netaddress.h:61
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:88
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:581
std::array< uint8_t, PCP_MAP_NONCE_SIZE > PCPMappingNonce
PCP mapping nonce. Arbitrary data chosen by the client to identify a mapping.
Definition: pcp.h:20
constexpr size_t PCP_MAP_NONCE_SIZE
Mapping nonce size in bytes (see RFC6887 section 11.1).
Definition: pcp.h:17
@ PROTOCOL_ERROR
Any kind of protocol-level error, except unsupported version or no resources.
@ NO_RESOURCES
No resources available (port probably already mapped).
@ UNSUPP_VERSION
Unsupported protocol version.
@ NETWORK_ERROR
Any kind of network-level error.
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:422
Successful response to a port mapping.
Definition: pcp.h:31
CService external
External host:port.
Definition: pcp.h:39
uint32_t lifetime
Granted lifetime of binding (seconds).
Definition: pcp.h:41
CService internal
Internal host:port.
Definition: pcp.h:37
std::string ToString() const
Format mapping as string for logging.
Definition: pcp.cpp:521
uint8_t version
Protocol version, one of NATPMP_VERSION or PCP_VERSION.
Definition: pcp.h:35
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:53
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172