17 #include <util/threadinterrupt.h> 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)) {
50 LogInfo(
"portmap: Added mapping %s", mapping->ToString());
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);
80 LogDebug(
BCLog::NET,
"portmap: Got unsupported PCP version response, falling back to NAT-PMP\n");
95 if (!addr.IsRoutable() || !addr.IsIPv6())
continue;
103 LogWarning(
"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) {
108 LogWarning(
"portmap: Got impossibly short mapping lifetime of %d seconds\n", actual_lifetime);
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.
virtual bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
Sleep for the given duration.
static constexpr auto PORT_MAPPING_RETRY_PERIOD
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD
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, std::chrono::milliseconds timeout_per_try)
Try to open a port using RFC 6887 Port Control Protocol (PCP).
static std::thread g_mapport_thread
void GetRandBytes(std::span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
static CThreadInterrupt g_mapport_interrupt
bool AddLocal(const CService &addr_, int nScore)
void StartMapPort(bool enable)
MappingError
Unsuccessful response to a port mapping.
A helper class for interruptible sleeps.
std::optional< CNetAddr > QueryDefaultGateway(Network network)
Query the OS for the default gateway for network.
static void ThreadMapPort()
virtual void reset()
Reset to an non-interrupted state.
#define LogDebug(category,...)
std::variant< MappingResult, MappingError > NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, CThreadInterrupt &interrupt, int num_tries, std::chrono::milliseconds timeout_per_try)
Try to open a port using RFC 6886 NAT-PMP.
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.
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.