aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2020-02-23 00:10:48 +0200
committerHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2021-01-07 18:07:08 +0200
commit4e91b1e24d96e0cdccdd2a3ed034413f3ba6bae6 (patch)
tree01f7e01ff0f66e58d98b5f3805780f571570476c
parent8b50d1b5bb29b7d1ea0245ba75a8df3144e312dc (diff)
net: Add flags for port mapping protocols
-rw-r--r--src/init.cpp4
-rw-r--r--src/mapport.cpp31
-rw-r--r--src/mapport.h7
-rw-r--r--src/node/interfaces.cpp10
4 files changed, 37 insertions, 15 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 5c24490215..8a260a3df2 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -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;
diff --git a/src/mapport.cpp b/src/mapport.cpp
index ced2b773f3..5091e9f44d 100644
--- a/src/mapport.cpp
+++ b/src/mapport.cpp
@@ -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.
}
diff --git a/src/mapport.h b/src/mapport.h
index 25ff2fd1eb..6e33b6ef74 100644
--- a/src/mapport.h
+++ b/src/mapport.h
@@ -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();
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
index ff6c9bf6b3..6b84cb0f05 100644
--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -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
{