aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2020-02-18 23:23:30 +0200
committerHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2021-01-07 18:07:07 +0200
commit8b50d1b5bb29b7d1ea0245ba75a8df3144e312dc (patch)
treee00bef9a28115a324be84547f39ef7e84884e81f
parent28e2961fd6a2a9101fc08fb748430989291aaf7e (diff)
net: Keep trying to use UPnP when -upnp=1
-rw-r--r--src/mapport.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/mapport.cpp b/src/mapport.cpp
index 1f24b75708..ced2b773f3 100644
--- a/src/mapport.cpp
+++ b/src/mapport.cpp
@@ -37,9 +37,11 @@ static std::thread g_upnp_thread;
using namespace std::chrono_literals;
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD{20min};
+static constexpr auto PORT_MAPPING_RETRY_PERIOD{5min};
-static void ThreadMapPort()
+static bool ProcessUpnp()
{
+ bool ret = false;
std::string port = strprintf("%u", GetListenPort());
const char * multicastif = nullptr;
const char * minissdpdpath = nullptr;
@@ -84,11 +86,15 @@ static void ThreadMapPort()
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
if (r != UPNPCOMMAND_SUCCESS) {
+ ret = false;
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n", port, port, lanaddr, r, strupnperror(r));
+ break;
} else {
+ ret = true;
LogPrintf("UPnP Port Mapping successful.\n");
}
} while (g_upnp_interrupt.sleep_for(PORT_MAPPING_REANNOUNCE_PERIOD));
+ g_upnp_interrupt.reset();
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
@@ -100,6 +106,15 @@ static void ThreadMapPort()
if (r != 0)
FreeUPNPUrls(&urls);
}
+
+ return ret;
+}
+
+static void ThreadMapPort()
+{
+ do {
+ if (ProcessUpnp()) return;
+ } while (g_upnp_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
}
void StartMapPort()