2012-01-21 01:04:14 +00:00
|
|
|
/*
|
|
|
|
* OpenClonk, http://www.openclonk.org
|
|
|
|
*
|
2016-04-03 18:18:29 +00:00
|
|
|
* Copyright (c) 2012-2016, The OpenClonk Team and contributors
|
2012-01-21 01:04:14 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* Distributed under the terms of the ISC license; see accompanying file
|
|
|
|
* "COPYING" for details.
|
2012-01-21 01:04:14 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
|
|
|
|
* See accompanying file "TRADEMARK" for details.
|
2012-01-21 01:04:14 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* To redistribute this file separately, substitute the full license texts
|
|
|
|
* for the above references.
|
2012-01-21 01:04:14 +00:00
|
|
|
*/
|
2016-05-29 19:59:30 +00:00
|
|
|
/* Linux implementation of a UPnP port mapper (using miniupnpc) */
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-04-03 18:07:56 +00:00
|
|
|
#include "C4Include.h"
|
|
|
|
#include "game/C4Application.h"
|
|
|
|
#include "C4Version.h"
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-31 16:23:41 +00:00
|
|
|
#include <future>
|
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
#include <miniupnpc.h>
|
|
|
|
#include <upnpcommands.h>
|
|
|
|
#include <upnperrors.h>
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
#include "network/C4Network2UPnP.h"
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
static const char *description = "OpenClonk";
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-31 16:23:41 +00:00
|
|
|
class C4Network2UPnPP : C4InteractiveThread
|
2012-01-21 01:04:14 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
C4Network2UPnPP();
|
|
|
|
virtual ~C4Network2UPnPP();
|
|
|
|
|
|
|
|
void AddMapping(C4Network2IOProtocol protocol, uint16_t intport, uint16_t extport);
|
|
|
|
void ClearMappings();
|
|
|
|
|
|
|
|
private:
|
2016-05-31 16:23:41 +00:00
|
|
|
void Init();
|
|
|
|
|
2012-01-21 01:04:14 +00:00
|
|
|
struct PortMapping {
|
|
|
|
uint16_t external_port;
|
|
|
|
uint16_t internal_port;
|
|
|
|
std::string protocol;
|
|
|
|
};
|
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
void AddPortMapping(const PortMapping& mapping);
|
|
|
|
void RemovePortMapping(const PortMapping& mapping);
|
2012-01-21 01:04:14 +00:00
|
|
|
|
|
|
|
std::vector<PortMapping> added_mappings;
|
2016-05-29 19:59:30 +00:00
|
|
|
|
2016-05-31 16:23:41 +00:00
|
|
|
// Synchronization using futures.
|
|
|
|
std::future<void> action;
|
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
bool initialized = false;
|
|
|
|
char lanaddr[64];
|
|
|
|
UPNPDev *devlist = nullptr;
|
|
|
|
UPNPUrls upnp_urls;
|
|
|
|
IGDdatas igd_data;
|
2012-01-21 01:04:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
C4Network2UPnPP::C4Network2UPnPP()
|
2016-05-31 16:23:41 +00:00
|
|
|
{
|
|
|
|
action = std::async(&C4Network2UPnPP::Init, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void C4Network2UPnPP::Init()
|
2012-01-21 01:04:14 +00:00
|
|
|
{
|
2016-05-29 19:59:30 +00:00
|
|
|
int error, status;
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-06-03 21:18:44 +00:00
|
|
|
#if MINIUPNPC_API_VERSION == 10
|
2016-06-03 20:15:11 +00:00
|
|
|
// Distributed with Debian jessie.
|
2016-11-02 23:58:02 +00:00
|
|
|
if ((devlist = upnpDiscover(2000, nullptr, nullptr, 0, 0, &error)))
|
2016-06-03 20:15:11 +00:00
|
|
|
#else
|
2016-11-02 23:58:02 +00:00
|
|
|
if ((devlist = upnpDiscover(2000, nullptr, nullptr, UPNP_LOCAL_PORT_ANY, 0, 2, &error)))
|
2016-06-03 20:15:11 +00:00
|
|
|
#endif
|
2016-05-29 19:59:30 +00:00
|
|
|
{
|
|
|
|
if ((status = UPNP_GetValidIGD(devlist, &upnp_urls, &igd_data, lanaddr, sizeof(lanaddr))))
|
|
|
|
{
|
2016-05-31 16:23:41 +00:00
|
|
|
ThreadLogS("UPnP: Found IGD %s (status %d)", upnp_urls.controlURL, status);
|
2016-05-29 19:59:30 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-31 16:23:41 +00:00
|
|
|
ThreadLog("UPnP: No IGD found.");
|
2016-12-10 21:07:37 +00:00
|
|
|
freeUPNPDevlist(devlist);
|
2016-05-29 19:59:30 +00:00
|
|
|
}
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
2016-05-29 19:59:30 +00:00
|
|
|
else
|
2012-01-21 01:04:14 +00:00
|
|
|
{
|
2016-05-31 16:23:41 +00:00
|
|
|
ThreadLog("UPnP: No UPnP device found on the network.");
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
2016-05-29 19:59:30 +00:00
|
|
|
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
C4Network2UPnPP::~C4Network2UPnPP()
|
|
|
|
{
|
|
|
|
ClearMappings();
|
2016-05-31 16:23:41 +00:00
|
|
|
action.wait();
|
|
|
|
ProcessEvents(); // necessary for logging
|
2016-12-10 21:07:37 +00:00
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
FreeUPNPUrls(&upnp_urls);
|
|
|
|
freeUPNPDevlist(devlist);
|
|
|
|
}
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void C4Network2UPnPP::AddMapping(C4Network2IOProtocol protocol, uint16_t intport, uint16_t extport)
|
|
|
|
{
|
|
|
|
PortMapping mapping;
|
|
|
|
mapping.external_port = extport;
|
|
|
|
mapping.internal_port = intport;
|
|
|
|
mapping.protocol = (protocol == P_TCP ? "TCP" : "UDP");
|
|
|
|
|
|
|
|
added_mappings.push_back(mapping);
|
|
|
|
|
2016-06-03 19:58:19 +00:00
|
|
|
action = std::async([this, action = std::move(action), mapping]() {
|
2016-05-31 16:23:41 +00:00
|
|
|
action.wait();
|
|
|
|
AddPortMapping(mapping);
|
|
|
|
});
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void C4Network2UPnPP::ClearMappings()
|
|
|
|
{
|
2016-06-03 19:58:19 +00:00
|
|
|
action = std::async([this, action = std::move(action)]() {
|
2016-05-31 16:23:41 +00:00
|
|
|
action.wait();
|
|
|
|
|
|
|
|
for (auto mapping : added_mappings)
|
|
|
|
RemovePortMapping(mapping);
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-31 16:23:41 +00:00
|
|
|
added_mappings.clear();
|
|
|
|
});
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
void C4Network2UPnPP::AddPortMapping(const PortMapping& mapping)
|
2012-01-21 01:04:14 +00:00
|
|
|
{
|
2016-05-29 19:59:30 +00:00
|
|
|
if (!initialized) return; // Catches the case that UPnP initialization failed
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
auto eport = std::to_string(mapping.external_port);
|
|
|
|
auto iport = std::to_string(mapping.internal_port);
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
int r = UPNP_AddPortMapping(upnp_urls.controlURL, igd_data.first.servicetype,
|
|
|
|
eport.c_str(), iport.c_str(), lanaddr, description,
|
|
|
|
mapping.protocol.c_str(), 0, 0);
|
2016-05-31 16:23:41 +00:00
|
|
|
if (r == UPNPCOMMAND_SUCCESS)
|
|
|
|
ThreadLogS("UPnP: Added mapping %s %s -> %s:%s", mapping.protocol.c_str(), eport.c_str(), lanaddr, iport.c_str());
|
|
|
|
else
|
|
|
|
ThreadLog("UPnP: AddPortMapping failed with code %d (%s)", r, strupnperror(r));
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
void C4Network2UPnPP::RemovePortMapping(const PortMapping& mapping)
|
2012-01-21 01:04:14 +00:00
|
|
|
{
|
2016-05-29 19:59:30 +00:00
|
|
|
if(!initialized) return; // Catches the case that UPnP initialization failed
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
auto eport = std::to_string(mapping.external_port);
|
2012-01-21 01:04:14 +00:00
|
|
|
|
2016-05-29 19:59:30 +00:00
|
|
|
int r = UPNP_DeletePortMapping(upnp_urls.controlURL, igd_data.first.servicetype,
|
|
|
|
eport.c_str(), mapping.protocol.c_str(), 0);
|
2016-05-31 16:23:41 +00:00
|
|
|
if (r == UPNPCOMMAND_SUCCESS)
|
|
|
|
ThreadLogS("UPnP: Removed mapping %s %s", mapping.protocol.c_str(), eport.c_str());
|
|
|
|
else
|
|
|
|
ThreadLog("UPnP: DeletePortMapping failed with code %d (%s)", r, strupnperror(r));
|
2012-01-21 01:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
C4Network2UPnP::C4Network2UPnP():
|
|
|
|
p(new C4Network2UPnPP)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
C4Network2UPnP::~C4Network2UPnP()
|
|
|
|
{
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void C4Network2UPnP::AddMapping(C4Network2IOProtocol protocol, uint16_t intport, uint16_t extport)
|
|
|
|
{
|
|
|
|
p->AddMapping(protocol, intport, extport);
|
|
|
|
}
|
|
|
|
|
|
|
|
void C4Network2UPnP::ClearMappings()
|
|
|
|
{
|
|
|
|
p->ClearMappings();
|
|
|
|
}
|
|
|
|
|