0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

net: Add flags for port mapping protocols

This commit is contained in:
Hennadii Stepanov 2020-02-23 00:10:48 +02:00
parent 8b50d1b5bb
commit 4e91b1e24d
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
4 changed files with 37 additions and 15 deletions

View file

@ -1900,9 +1900,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
Discover();
// Map ports with UPnP
if (args.GetBoolArg("-upnp", DEFAULT_UPNP)) {
StartMapPort();
}
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP));
CConnman::Options connOptions;
connOptions.nLocalServices = nLocalServices;

View file

@ -25,6 +25,7 @@
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
#endif
#include <atomic>
#include <cassert>
#include <chrono>
#include <functional>
@ -34,6 +35,7 @@ static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"
#ifdef USE_UPNP
static CThreadInterrupt g_upnp_interrupt;
static std::thread g_upnp_thread;
static std::atomic_uint g_mapport_target_proto{MapPortProtoFlag::NONE};
using namespace std::chrono_literals;
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));
}
void StartMapPort()
void StartThreadMapPort()
{
if (!g_upnp_thread.joinable()) {
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()
{
if(g_upnp_thread.joinable()) {
@ -141,7 +168,7 @@ void StopMapPort()
}
#else
void StartMapPort()
void StartMapPort(bool use_upnp)
{
// Intentionally left blank.
}

View file

@ -12,7 +12,12 @@ static const bool DEFAULT_UPNP = USE_UPNP;
static const bool DEFAULT_UPNP = false;
#endif
void StartMapPort();
enum MapPortProtoFlag : unsigned int {
NONE = 0x00,
UPNP = 0x01,
};
void StartMapPort(bool use_upnp);
void InterruptMapPort();
void StopMapPort();

View file

@ -94,15 +94,7 @@ public:
}
}
bool shutdownRequested() override { return ShutdownRequested(); }
void mapPort(bool use_upnp) override
{
if (use_upnp) {
StartMapPort();
} else {
InterruptMapPort();
StopMapPort();
}
}
void mapPort(bool use_upnp) override { StartMapPort(use_upnp); }
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
size_t getNodeCount(CConnman::NumConnections flags) override
{