29using namespace std::chrono_literals;
40 bool no_resources =
false;
44 uint32_t actual_lifetime = 0;
45 std::chrono::milliseconds sleep_time;
48 auto handle_mapping = [&](std::variant<MappingResult, MappingError> &res) ->
void {
49 if (
MappingResult* mapping = std::get_if<MappingResult>(&res)) {
53 actual_lifetime = std::min(actual_lifetime, mapping->lifetime);
54 }
else if (
MappingError *err = std::get_if<MappingError>(&res)) {
63 actual_lifetime = requested_lifetime;
75 struct in_addr inaddr_any;
76 inaddr_any.s_addr = htonl(INADDR_ANY);
95 if (!addr.IsRoutable() || !addr.IsIPv6())
continue;
96 auto res =
PCPRequestPortMap(pcp_nonce, *gateway6, addr, private_port, requested_lifetime);
103 LogPrintLevel(
BCLog::NET,
BCLog::Level::Warning,
"portmap: At least one mapping failed because of a NO_RESOURCES error. This usually indicates that the port is already used on the router. If this is the only instance of bitcoin running on the network, this will resolve itself automatically. Otherwise, you might want to choose a different P2P port to prevent this conflict.\n");
107 if (actual_lifetime < 30) {
113 std::chrono::seconds sleep_time_min(actual_lifetime / 2);
114 std::chrono::seconds sleep_time_max(actual_lifetime * 5 / 8);
A helper class for interruptible sleeps.
bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
std::variant< MappingResult, MappingError > PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway, const CNetAddr &bind, uint16_t port, uint32_t lifetime, int num_tries, std::chrono::milliseconds timeout_per_try)
Try to open a port using RFC 6887 Port Control Protocol (PCP).
std::variant< MappingResult, MappingError > NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, int num_tries, std::chrono::milliseconds timeout_per_try)
Try to open a port using RFC 6886 NAT-PMP.
#define LogPrintLevel(category, level,...)
static void ThreadMapPort()
static std::thread g_mapport_thread
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD
static CThreadInterrupt g_mapport_interrupt
static constexpr auto PORT_MAPPING_RETRY_PERIOD
void StartMapPort(bool enable)
void StartThreadMapPort()
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
bool AddLocal(const CService &addr_, int nScore)
std::vector< CNetAddr > GetLocalAddresses()
Return all local non-loopback IPv4 and IPv6 network addresses.
std::optional< CNetAddr > QueryDefaultGateway(Network network)
Query the OS for the default gateway for network.
std::array< uint8_t, PCP_MAP_NONCE_SIZE > PCPMappingNonce
PCP mapping nonce. Arbitrary data chosen by the client to identify a mapping.
MappingError
Unsuccessful response to a port mapping.
@ NO_RESOURCES
No resources available (port probably already mapped).
@ UNSUPP_VERSION
Unsupported protocol version.
void GetRandBytes(Span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
Successful response to a port mapping.