From 86b6cf2d65d40d92c800d9a9705adcb2dfc829c1 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 20 Mar 2023 22:13:12 +0100 Subject: [PATCH] UPnP: handle port mapping conflicts --- src/util.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/util.cpp b/src/util.cpp index 569d88f..e88439f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -650,6 +650,22 @@ void add_portmapping(int external_port, int internal_port) const std::string iport = std::to_string(internal_port); result = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, eport.c_str(), iport.c_str(), local_addr, "P2Pool", "TCP", nullptr, nullptr); + + // ConflictInMappingEntry: try to delete the old record and then add the new one again + if (result == 718) { + LOGWARN(1, "UPNP_AddPortMapping failed: ConflictInMappingEntry"); + + result = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, eport.c_str(), "TCP", nullptr); + if (result) { + LOGWARN(1, "UPNP_DeletePortMapping returned error " << result); + return; + } + else { + LOGINFO(1, "UPnP: Deleted mapping for external port " << external_port); + result = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, eport.c_str(), iport.c_str(), local_addr, "P2Pool", "TCP", nullptr, nullptr); + } + } + if (result) { LOGWARN(1, "UPNP_AddPortMapping returned error " << result); }