Skip to content

Commit 4e91b1e

Browse files
committed
net: Add flags for port mapping protocols
1 parent 8b50d1b commit 4e91b1e

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/init.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,9 +1900,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19001900
Discover();
19011901

19021902
// Map ports with UPnP
1903-
if (args.GetBoolArg("-upnp", DEFAULT_UPNP)) {
1904-
StartMapPort();
1905-
}
1903+
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP));
19061904

19071905
CConnman::Options connOptions;
19081906
connOptions.nLocalServices = nLocalServices;

src/mapport.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
2626
#endif
2727

28+
#include <atomic>
2829
#include <cassert>
2930
#include <chrono>
3031
#include <functional>
@@ -34,6 +35,7 @@ static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"
3435
#ifdef USE_UPNP
3536
static CThreadInterrupt g_upnp_interrupt;
3637
static std::thread g_upnp_thread;
38+
static std::atomic_uint g_mapport_target_proto{MapPortProtoFlag::NONE};
3739

3840
using namespace std::chrono_literals;
3941
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD{20min};
@@ -117,14 +119,39 @@ static void ThreadMapPort()
117119
} while (g_upnp_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
118120
}
119121

120-
void StartMapPort()
122+
void StartThreadMapPort()
121123
{
122124
if (!g_upnp_thread.joinable()) {
123125
assert(!g_upnp_interrupt);
124126
g_upnp_thread = std::thread((std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
125127
}
126128
}
127129

130+
static void DispatchMapPort()
131+
{
132+
if (g_mapport_target_proto == MapPortProtoFlag::UPNP) {
133+
StartThreadMapPort();
134+
} else {
135+
InterruptMapPort();
136+
StopMapPort();
137+
}
138+
}
139+
140+
static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
141+
{
142+
if (enabled) {
143+
g_mapport_target_proto |= proto;
144+
} else {
145+
g_mapport_target_proto &= ~proto;
146+
}
147+
}
148+
149+
void StartMapPort(bool use_upnp)
150+
{
151+
MapPortProtoSetEnabled(MapPortProtoFlag::UPNP, use_upnp);
152+
DispatchMapPort();
153+
}
154+
128155
void InterruptMapPort()
129156
{
130157
if(g_upnp_thread.joinable()) {
@@ -141,7 +168,7 @@ void StopMapPort()
141168
}
142169

143170
#else
144-
void StartMapPort()
171+
void StartMapPort(bool use_upnp)
145172
{
146173
// Intentionally left blank.
147174
}

src/mapport.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ static const bool DEFAULT_UPNP = USE_UPNP;
1212
static const bool DEFAULT_UPNP = false;
1313
#endif
1414

15-
void StartMapPort();
15+
enum MapPortProtoFlag : unsigned int {
16+
NONE = 0x00,
17+
UPNP = 0x01,
18+
};
19+
20+
void StartMapPort(bool use_upnp);
1621
void InterruptMapPort();
1722
void StopMapPort();
1823

src/node/interfaces.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,7 @@ class NodeImpl : public Node
9494
}
9595
}
9696
bool shutdownRequested() override { return ShutdownRequested(); }
97-
void mapPort(bool use_upnp) override
98-
{
99-
if (use_upnp) {
100-
StartMapPort();
101-
} else {
102-
InterruptMapPort();
103-
StopMapPort();
104-
}
105-
}
97+
void mapPort(bool use_upnp) override { StartMapPort(use_upnp); }
10698
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
10799
size_t getNodeCount(CConnman::NumConnections flags) override
108100
{

0 commit comments

Comments
 (0)