Bitcoin Core 31.99.0
P2P Digital Currency
pcp.h
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#ifndef BITCOIN_COMMON_PCP_H
6#define BITCOIN_COMMON_PCP_H
7
8#include <netaddress.h>
9#include <util/time.h>
10
11#include <array>
12#include <cstddef>
13#include <cstdint>
14#include <string>
15#include <variant>
16
18
19// RFC6886 NAT-PMP and RFC6887 Port Control Protocol (PCP) implementation.
20// NAT-PMP and PCP use network byte order (big-endian).
21
23constexpr size_t PCP_MAP_NONCE_SIZE = 12;
24
26typedef std::array<uint8_t, PCP_MAP_NONCE_SIZE> PCPMappingNonce;
27
29enum class MappingError {
34};
35
38 MappingResult(uint8_t version, const CService &internal_in, const CService &external_in, uint32_t lifetime_in):
39 version(version), internal(internal_in), external(external_in), lifetime(lifetime_in) {}
41 uint8_t version;
47 uint32_t lifetime;
48
50 std::string ToString() const;
51};
52
61std::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));
62
73std::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));
74
75#endif // BITCOIN_COMMON_PCP_H
Network address.
Definition: netaddress.h:113
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
A helper class for interruptible sleeps.
unsigned int nonce
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:293
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:417
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:26
constexpr size_t PCP_MAP_NONCE_SIZE
Mapping nonce size in bytes (see RFC6887 section 11.1).
Definition: pcp.h:23
MappingError
Unsuccessful response to a port mapping.
Definition: pcp.h:29
@ 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:37
CService external
External host:port.
Definition: pcp.h:45
uint32_t lifetime
Granted lifetime of binding (seconds).
Definition: pcp.h:47
CService internal
Internal host:port.
Definition: pcp.h:43
MappingResult(uint8_t version, const CService &internal_in, const CService &external_in, uint32_t lifetime_in)
Definition: pcp.h:38
std::string ToString() const
Format mapping as string for logging.
Definition: pcp.cpp:555
uint8_t version
Protocol version, one of NATPMP_VERSION or PCP_VERSION.
Definition: pcp.h:41