Bitcoin ABC 0.33.1
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>
10
11#include <array>
12#include <cstddef>
13#include <cstdint>
14#include <variant>
15
16// RFC6886 NAT-PMP and RFC6887 Port Control Protocol (PCP) implementation.
17// NAT-PMP and PCP use network byte order (big-endian).
18
20constexpr size_t PCP_MAP_NONCE_SIZE = 12;
21
24typedef std::array<uint8_t, PCP_MAP_NONCE_SIZE> PCPMappingNonce;
25
27enum class MappingError {
37};
38
41 MappingResult(uint8_t version, const CService &internal_in,
42 const CService &external_in, uint32_t lifetime_in)
43 : version(version), internal(internal_in), external(external_in),
44 lifetime(lifetime_in) {}
46 uint8_t version;
52 uint32_t lifetime;
53
55 std::string ToString() const;
56};
57
69std::variant<MappingResult, MappingError>
70NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime,
71 CThreadInterrupt &interrupt, int num_tries = 3,
72 std::chrono::milliseconds timeout_per_try =
73 std::chrono::milliseconds(1000));
74
90std::variant<MappingResult, MappingError>
91PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway,
92 const CNetAddr &bind, uint16_t port, uint32_t lifetime,
93 CThreadInterrupt &interrupt, int num_tries = 3,
94 std::chrono::milliseconds timeout_per_try =
95 std::chrono::milliseconds(1000));
96
97#endif // BITCOIN_COMMON_PCP_H
Network address.
Definition: netaddress.h:114
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:573
A helper class for interruptible sleeps.
std::variant< MappingResult, MappingError > NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, CThreadInterrupt &interrupt, 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:318
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=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:494
std::array< uint8_t, PCP_MAP_NONCE_SIZE > PCPMappingNonce
PCP mapping nonce.
Definition: pcp.h:24
constexpr size_t PCP_MAP_NONCE_SIZE
Mapping nonce size in bytes (see RFC6887 section 11.1).
Definition: pcp.h:20
MappingError
Unsuccessful response to a port mapping.
Definition: pcp.h:27
@ 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.
Successful response to a port mapping.
Definition: pcp.h:40
CService external
External host:port.
Definition: pcp.h:50
uint32_t lifetime
Granted lifetime of binding (seconds).
Definition: pcp.h:52
CService internal
Internal host:port.
Definition: pcp.h:48
MappingResult(uint8_t version, const CService &internal_in, const CService &external_in, uint32_t lifetime_in)
Definition: pcp.h:41
std::string ToString() const
Format mapping as string for logging.
Definition: pcp.cpp:702
uint8_t version
Protocol version, one of NATPMP_VERSION or PCP_VERSION.
Definition: pcp.h:46