Skip to content

Commit

Permalink
net: Add flags for port mapping protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Jun 14, 2021
1 parent 2abea67 commit 06dc0dd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,9 +1940,7 @@ bool AppInitMain()
Discover();

// Map ports with UPnP
if (gArgs.GetBoolArg("-upnp", DEFAULT_UPNP)) {
StartMapPort();
}
StartMapPort(gArgs.GetBoolArg("-upnp", DEFAULT_UPNP));

std::string strNodeError;
CConnman::Options connOptions;
Expand Down
31 changes: 29 additions & 2 deletions src/mapport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
#endif

#include <atomic>
#include <cassert>
#include <chrono>
#include <functional>
Expand All @@ -35,6 +36,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};

static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD = std::chrono::minutes(20);
static constexpr auto PORT_MAPPING_RETRY_PERIOD = std::chrono::minutes(5);
Expand Down Expand Up @@ -117,14 +119,39 @@ 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);
g_upnp_thread = std::thread(std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
}
}

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()) {
Expand All @@ -141,7 +168,7 @@ void StopMapPort()
}

#else
void StartMapPort()
void StartMapPort(bool use_upnp)
{
// Intentionally left blank.
}
Expand Down
7 changes: 6 additions & 1 deletion src/mapport.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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();

Expand Down
7 changes: 1 addition & 6 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,7 @@ bool OptionsModel::setData(const QModelIndex& index, const QVariant& value, int
break;
case MapPortUPnP: // core option - can be changed on-the-fly
settings.setValue("fUseUPnP", value.toBool());
if (value.toBool()) {
StartMapPort();
} else {
InterruptMapPort();
StopMapPort();
}
StartMapPort(value.toBool());
break;
case MinimizeOnClose:
fMinimizeOnClose = value.toBool();
Expand Down

0 comments on commit 06dc0dd

Please sign in to comment.