net: Add flags for port mapping protocols

This commit is contained in:
Hennadii Stepanov committed 2021-01-07 18:07:08 +02:00
1 parent 8b50d1b5bb
commit 4e91b1e24d
4 files changed
+37 -15

No files matched your search

+1 -3
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;
+29 -2
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.
}
+6 -1
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();
+1 -9
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
{