mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-25 11:20:49 +02:00
net: Add flags for port mapping protocols
This commit is contained in:
parent
8b50d1b5bb
commit
4e91b1e24d
@ -1900,9 +1900,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
|
|||||||
Discover();
|
Discover();
|
||||||
|
|
||||||
// Map ports with UPnP
|
// Map ports with UPnP
|
||||||
if (args.GetBoolArg("-upnp", DEFAULT_UPNP)) {
|
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP));
|
||||||
StartMapPort();
|
|
||||||
}
|
|
||||||
|
|
||||||
CConnman::Options connOptions;
|
CConnman::Options connOptions;
|
||||||
connOptions.nLocalServices = nLocalServices;
|
connOptions.nLocalServices = nLocalServices;
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
|
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -34,6 +35,7 @@ static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"
|
|||||||
#ifdef USE_UPNP
|
#ifdef USE_UPNP
|
||||||
static CThreadInterrupt g_upnp_interrupt;
|
static CThreadInterrupt g_upnp_interrupt;
|
||||||
static std::thread g_upnp_thread;
|
static std::thread g_upnp_thread;
|
||||||
|
static std::atomic_uint g_mapport_target_proto{MapPortProtoFlag::NONE};
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD{20min};
|
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD{20min};
|
||||||
@ -117,7 +119,7 @@ static void ThreadMapPort()
|
|||||||
} while (g_upnp_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
|
} while (g_upnp_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
|
||||||
}
|
}
|
||||||
|
|
||||||
void StartMapPort()
|
void StartThreadMapPort()
|
||||||
{
|
{
|
||||||
if (!g_upnp_thread.joinable()) {
|
if (!g_upnp_thread.joinable()) {
|
||||||
assert(!g_upnp_interrupt);
|
assert(!g_upnp_interrupt);
|
||||||
@ -125,6 +127,31 @@ void StartMapPort()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void DispatchMapPort()
|
||||||
|
{
|
||||||
|
if (g_mapport_target_proto == MapPortProtoFlag::UPNP) {
|
||||||
|
StartThreadMapPort();
|
||||||
|
} else {
|
||||||
|
InterruptMapPort();
|
||||||
|
StopMapPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
|
||||||
|
{
|
||||||
|
if (enabled) {
|
||||||
|
g_mapport_target_proto |= proto;
|
||||||
|
} else {
|
||||||
|
g_mapport_target_proto &= ~proto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StartMapPort(bool use_upnp)
|
||||||
|
{
|
||||||
|
MapPortProtoSetEnabled(MapPortProtoFlag::UPNP, use_upnp);
|
||||||
|
DispatchMapPort();
|
||||||
|
}
|
||||||
|
|
||||||
void InterruptMapPort()
|
void InterruptMapPort()
|
||||||
{
|
{
|
||||||
if(g_upnp_thread.joinable()) {
|
if(g_upnp_thread.joinable()) {
|
||||||
@ -141,7 +168,7 @@ void StopMapPort()
|
|||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
void StartMapPort()
|
void StartMapPort(bool use_upnp)
|
||||||
{
|
{
|
||||||
// Intentionally left blank.
|
// Intentionally left blank.
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,12 @@ static const bool DEFAULT_UPNP = USE_UPNP;
|
|||||||
static const bool DEFAULT_UPNP = false;
|
static const bool DEFAULT_UPNP = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void StartMapPort();
|
enum MapPortProtoFlag : unsigned int {
|
||||||
|
NONE = 0x00,
|
||||||
|
UPNP = 0x01,
|
||||||
|
};
|
||||||
|
|
||||||
|
void StartMapPort(bool use_upnp);
|
||||||
void InterruptMapPort();
|
void InterruptMapPort();
|
||||||
void StopMapPort();
|
void StopMapPort();
|
||||||
|
|
||||||
|
@ -94,15 +94,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool shutdownRequested() override { return ShutdownRequested(); }
|
bool shutdownRequested() override { return ShutdownRequested(); }
|
||||||
void mapPort(bool use_upnp) override
|
void mapPort(bool use_upnp) override { StartMapPort(use_upnp); }
|
||||||
{
|
|
||||||
if (use_upnp) {
|
|
||||||
StartMapPort();
|
|
||||||
} else {
|
|
||||||
InterruptMapPort();
|
|
||||||
StopMapPort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
|
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
|
||||||
size_t getNodeCount(CConnman::NumConnections flags) override
|
size_t getNodeCount(CConnman::NumConnections flags) override
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user