From 9a01ced032b5c85c0def2afefc6437fa18da8fe8 Mon Sep 17 00:00:00 2001 From: codl Date: Sat, 23 Jul 2016 20:04:07 +0200 Subject: [PATCH] Fix bogus UPnP requests UPNP_AddPortMapping needs our IP address, however enet_address_get_host will return 0.0.0.0 or a host name in most cases. This gets our IP address from the socket to the IGD. --- Source/Core/Core/NetPlayServer.cpp | 22 +++++++++++----------- Source/Core/Core/NetPlayServer.h | 1 + 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 3e3a6e16b4..02173d36a2 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -937,6 +937,7 @@ std::vector> NetPlayServer::GetInterfaceList struct UPNPUrls NetPlayServer::m_upnp_urls; struct IGDdatas NetPlayServer::m_upnp_data; +std::string NetPlayServer::m_upnp_ourip; u16 NetPlayServer::m_upnp_mapped = 0; bool NetPlayServer::m_upnp_inited = false; bool NetPlayServer::m_upnp_error = false; @@ -953,23 +954,17 @@ void NetPlayServer::TryPortmapping(u16 port) // UPnP thread: try to map a port void NetPlayServer::mapPortThread(const u16 port) { - ENetAddress adr = {ENET_HOST_ANY, port}; - char cIP[20]; - - enet_address_get_host(&adr, cIP, 20); - std::string ourIP(cIP); - if (!m_upnp_inited) if (!initUPnP()) goto fail; - if (!UPnPMapPort(ourIP, port)) + if (!UPnPMapPort(m_upnp_ourip, port)) goto fail; - NOTICE_LOG(NETPLAY, "Successfully mapped port %d to %s.", port, ourIP.c_str()); + NOTICE_LOG(NETPLAY, "Successfully mapped port %d to %s.", port, m_upnp_ourip.c_str()); return; fail: - WARN_LOG(NETPLAY, "Failed to map port %d to %s.", port, ourIP.c_str()); + WARN_LOG(NETPLAY, "Failed to map port %d to %s.", port, m_upnp_ourip.c_str()); return; } @@ -986,6 +981,7 @@ bool NetPlayServer::initUPnP() { std::vector igds; int descXMLsize = 0, upnperror = 0; + char cIP[20]; // Don't init if already inited if (m_upnp_inited) @@ -1027,15 +1023,19 @@ bool NetPlayServer::initUPnP() std::unique_ptr descXML(nullptr, std::free); int statusCode = 200; #if MINIUPNPC_API_VERSION >= 16 - descXML.reset(static_cast(miniwget(dev->descURL, &descXMLsize, 0, &statusCode))); + descXML.reset(static_cast( + miniwget_getaddr(dev->descURL, &descXMLsize, cIP, sizeof(cIP), 0, &statusCode))); #else - descXML.reset(static_cast(miniwget(dev->descURL, &descXMLsize, 0))); + descXML.reset( + static_cast(miniwget_getaddr(dev->descURL, &descXMLsize, cIP, sizeof(cIP), 0))); #endif if (descXML && statusCode == 200) { parserootdesc(descXML.get(), descXMLsize, &m_upnp_data); GetUPNPUrls(&m_upnp_urls, &m_upnp_data, dev->descURL, 0); + m_upnp_ourip = cIP; + NOTICE_LOG(NETPLAY, "Got info from IGD at %s.", dev->descURL); break; } diff --git a/Source/Core/Core/NetPlayServer.h b/Source/Core/Core/NetPlayServer.h index f75431fcfa..1e356cd271 100644 --- a/Source/Core/Core/NetPlayServer.h +++ b/Source/Core/Core/NetPlayServer.h @@ -134,6 +134,7 @@ private: static struct UPNPUrls m_upnp_urls; static struct IGDdatas m_upnp_data; + static std::string m_upnp_ourip; static u16 m_upnp_mapped; static bool m_upnp_inited; static bool m_upnp_error;