Skip to content

Commit

Permalink
Merge pull request #4053 from codl/fix-upnp
Browse files Browse the repository at this point in the history
Fix bogus UPnP requests
  • Loading branch information
delroth committed Jul 24, 2016
2 parents aa34e5e + 9a01ced commit 7a8ca99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Source/Core/Core/NetPlayServer.cpp
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/NetPlayServer.h
Expand Up @@ -134,6 +134,7 @@ class NetPlayServer : public TraversalClientClient

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;
Expand Down

0 comments on commit 7a8ca99

Please sign in to comment.