31using namespace std::chrono_literals;
42 bool no_resources =
false;
46 uint32_t actual_lifetime = 0;
47 std::chrono::milliseconds sleep_time;
50 auto handle_mapping = [&](std::variant<MappingResult, MappingError> &res) ->
void {
51 if (
MappingResult* mapping = std::get_if<MappingResult>(&res)) {
55 actual_lifetime = std::min(actual_lifetime, mapping->lifetime);
56 }
else if (
MappingError *err = std::get_if<MappingError>(&res)) {
65 actual_lifetime = requested_lifetime;
77 struct in_addr inaddr_any;
78 inaddr_any.s_addr = htonl(INADDR_ANY);
97 if (!addr.IsRoutable() || !addr.IsIPv6())
continue;
98 auto res =
PCPRequestPortMap(pcp_nonce, *gateway6, addr, private_port, requested_lifetime);
105 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");
109 if (actual_lifetime < 30) {
115 std::chrono::seconds sleep_time_min(actual_lifetime / 2);
116 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.
#define LogPrintLevel(category, level,...)
static void ThreadMapPort()
static std::thread g_mapport_thread
void StartMapPort(bool use_pcp)
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD
static CThreadInterrupt g_mapport_interrupt
static std::atomic< MapPortProtoFlag > g_mapport_current_proto
static void DispatchMapPort()
static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
static constexpr auto PORT_MAPPING_RETRY_PERIOD
static std::atomic_uint g_mapport_enabled_protos
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::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.
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.