net: Keep trying to use UPnP when -upnp=1

This commit is contained in:
Hennadii Stepanov 2020-02-18 23:23:30 +02:00
parent 28e2961fd6
commit 8b50d1b5bb
No known key found for this signature in database
GPG Key ID: 410108112E7EA81F

View File

@ -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()