Bitcoin Core 28.99.0
P2P Digital Currency
pcp.h
Go to the documentation of this file.
1// Copyright (c) 2024 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#ifndef BITCOIN_COMMON_PCP_H
6#define BITCOIN_COMMON_PCP_H
7
8#include <netaddress.h>
9
10#include <variant>
11
12// RFC6886 NAT-PMP and RFC6887 Port Control Protocol (PCP) implementation.
13// NAT-PMP and PCP use network byte order (big-endian).
14
16constexpr size_t PCP_MAP_NONCE_SIZE = 12;
17
19typedef std::array<uint8_t, PCP_MAP_NONCE_SIZE> PCPMappingNonce;
20
22enum class MappingError {
27};
28
31 MappingResult(uint8_t version, const CService &internal_in, const CService &external_in, uint32_t lifetime_in):
32 version(version), internal(internal_in), external(external_in), lifetime(lifetime_in) {}
34 uint8_t version;
40 uint32_t lifetime;
41
43 std::string ToString();
44};
45
54std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, int num_tries = 3, std::chrono::milliseconds timeout_per_try = std::chrono::milliseconds(1000));
55
66std::variant<MappingResult, MappingError> PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway, const CNetAddr &bind, uint16_t port, uint32_t lifetime, int num_tries = 3, std::chrono::milliseconds timeout_per_try = std::chrono::milliseconds(1000));
67
68#endif // BITCOIN_COMMON_PCP_H
Network address.
Definition: netaddress.h:112
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
unsigned int nonce
Definition: miner_tests.cpp:74
std::variant< MappingResult, MappingError > PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway, const CNetAddr &bind, uint16_t port, uint32_t lifetime, int num_tries=3, std::chrono::milliseconds timeout_per_try=std::chrono::milliseconds(1000))
Try to open a port using RFC 6887 Port Control Protocol (PCP).
Definition: pcp.cpp:387
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:19
constexpr size_t PCP_MAP_NONCE_SIZE
Mapping nonce size in bytes (see RFC6887 section 11.1).
Definition: pcp.h:16
MappingError
Unsuccessful response to a port mapping.
Definition: pcp.h:22
@ 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::variant< MappingResult, MappingError > NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, int num_tries=3, std::chrono::milliseconds timeout_per_try=std::chrono::milliseconds(1000))
Try to open a port using RFC 6886 NAT-PMP.
Definition: pcp.cpp:274
Successful response to a port mapping.
Definition: pcp.h:30
CService external
External host:port.
Definition: pcp.h:38
uint32_t lifetime
Granted lifetime of binding (seconds).
Definition: pcp.h:40
CService internal
Internal host:port.
Definition: pcp.h:36
MappingResult(uint8_t version, const CService &internal_in, const CService &external_in, uint32_t lifetime_in)
Definition: pcp.h:31
std::string ToString()
Format mapping as string for logging.
Definition: pcp.cpp:515
uint8_t version
Protocol version, one of NATPMP_VERSION or PCP_VERSION.
Definition: pcp.h:34