Merge pull request #4053 from codl/fix-upnp

Fix bogus UPnP requests
This commit is contained in:
Pierre Bourdon 2016-07-24 13:06:12 +02:00 committed by GitHub
commit 7a8ca99cda
2 changed files with 12 additions and 11 deletions

View File

@ -937,6 +937,7 @@ std::vector<std::pair<std::string, std::string>> 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<UPNPDev*> 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<char, decltype(&std::free)> descXML(nullptr, std::free);
int statusCode = 200;
#if MINIUPNPC_API_VERSION >= 16
descXML.reset(static_cast<char*>(miniwget(dev->descURL, &descXMLsize, 0, &statusCode)));
descXML.reset(static_cast<char*>(
miniwget_getaddr(dev->descURL, &descXMLsize, cIP, sizeof(cIP), 0, &statusCode)));
#else
descXML.reset(static_cast<char*>(miniwget(dev->descURL, &descXMLsize, 0)));
descXML.reset(
static_cast<char*>(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;
}

View File

@ -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;