Bitcoin Core  29.1.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 
16 constexpr size_t PCP_MAP_NONCE_SIZE = 12;
17 
19 typedef std::array<uint8_t, PCP_MAP_NONCE_SIZE> PCPMappingNonce;
20 
22 enum class MappingError {
26  NO_RESOURCES,
27 };
28 
30 struct MappingResult {
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;
36  CService internal;
40  uint32_t lifetime;
41 
43  std::string ToString() const;
44 };
45 
54 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));
55 
66 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));
67 
68 #endif // BITCOIN_COMMON_PCP_H
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
uint32_t lifetime
Granted lifetime of binding (seconds).
Definition: pcp.h:40
unsigned int nonce
Definition: miner_tests.cpp:75
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
Any kind of network-level error.
Any kind of protocol-level error, except unsupported version or no resources.
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
MappingError
Unsuccessful response to a port mapping.
Definition: pcp.h:22
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::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
Network address.
Definition: netaddress.h:111
uint8_t version
Protocol version, one of NATPMP_VERSION or PCP_VERSION.
Definition: pcp.h:34
std::string ToString() const
Format mapping as string for logging.
Definition: pcp.cpp:515
Unsupported protocol version.
No resources available (port probably already mapped).
CService external
External host:port.
Definition: pcp.h:38
Successful response to a port mapping.
Definition: pcp.h:30
constexpr size_t PCP_MAP_NONCE_SIZE
Mapping nonce size in bytes (see RFC6887 section 11.1).
Definition: pcp.h:16