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);
std::array< uint8_t, PCP_MAP_NONCE_SIZE > PCPMappingNonce
PCP mapping nonce. Arbitrary data chosen by the client to identify a mapping.
bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
static constexpr auto PORT_MAPPING_RETRY_PERIOD
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD
static std::thread g_mapport_thread
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.
static CThreadInterrupt g_mapport_interrupt
bool AddLocal(const CService &addr_, int nScore)
void StartMapPort(bool enable)
MappingError
Unsuccessful response to a port mapping.
#define LogPrintLevel(category, level,...)
A helper class for interruptible sleeps.
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::optional< CNetAddr > QueryDefaultGateway(Network network)
Query the OS for the default gateway for network.
static void ThreadMapPort()
void StartThreadMapPort()
std::vector< CNetAddr > GetLocalAddresses()
Return all local non-loopback IPv4 and IPv6 network addresses.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
void GetRandBytes(Span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
Unsupported protocol version.
No resources available (port probably already mapped).
Successful response to a port mapping.
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.