mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
248 lines
8.1 KiB
248 lines
8.1 KiB
10 years ago
|
#ifdef USE_UPNP
|
||
11 years ago
|
#include <string>
|
||
10 years ago
|
#include <thread>
|
||
|
|
||
|
#include <boost/thread/thread.hpp>
|
||
|
#include <boost/asio.hpp>
|
||
11 years ago
|
#include <boost/bind.hpp>
|
||
10 years ago
|
|
||
10 years ago
|
#ifdef _WIN32
|
||
|
#include <windows.h>
|
||
|
#define dlsym GetProcAddress
|
||
|
#else
|
||
|
#include <dlfcn.h>
|
||
|
#endif
|
||
|
|
||
9 years ago
|
#include "util/Log.h"
|
||
10 years ago
|
|
||
10 years ago
|
#include "RouterContext.h"
|
||
11 years ago
|
#include "UPnP.h"
|
||
10 years ago
|
#include "NetDb.h"
|
||
9 years ago
|
#include "util/util.h"
|
||
10 years ago
|
|
||
|
#include <miniupnpc/miniupnpc.h>
|
||
|
#include <miniupnpc/upnpcommands.h>
|
||
|
|
||
10 years ago
|
// These are per-process and are safe to reuse for all threads
|
||
10 years ago
|
#ifndef UPNPDISCOVER_SUCCESS
|
||
|
/* miniupnpc 1.5 */
|
||
10 years ago
|
UPNPDev* (*upnpDiscoverFunc) (int, const char *, const char *, int);
|
||
|
int (*UPNP_AddPortMappingFunc) (const char *, const char *, const char *, const char *,
|
||
10 years ago
|
const char *, const char *, const char *, const char *);
|
||
|
#else
|
||
|
/* miniupnpc 1.6 */
|
||
10 years ago
|
UPNPDev* (*upnpDiscoverFunc) (int, const char *, const char *, int, int, int *);
|
||
|
int (*UPNP_AddPortMappingFunc) (const char *, const char *, const char *, const char *,
|
||
10 years ago
|
const char *, const char *, const char *, const char *, const char *);
|
||
|
#endif
|
||
10 years ago
|
int (*UPNP_GetValidIGDFunc) (struct UPNPDev *, struct UPNPUrls *, struct IGDdatas *, char *, int);
|
||
|
int (*UPNP_GetExternalIPAddressFunc) (const char *, const char *, char *);
|
||
|
int (*UPNP_DeletePortMappingFunc) (const char *, const char *, const char *, const char *, const char *);
|
||
|
void (*freeUPNPDevlistFunc) (struct UPNPDev *);
|
||
|
void (*FreeUPNPUrlsFunc) (struct UPNPUrls *);
|
||
|
|
||
|
// Nice approach http://stackoverflow.com/a/21517513/673826
|
||
10 years ago
|
template<class M, typename F>
|
||
|
F GetKnownProcAddressImpl(M hmod, const char *name, F) {
|
||
10 years ago
|
auto proc = reinterpret_cast<F>(dlsym(hmod, name));
|
||
|
if (!proc) {
|
||
|
LogPrint("Error resolving ", name, " from UPNP library. This often happens if there is version mismatch!");
|
||
|
}
|
||
|
return proc;
|
||
|
}
|
||
|
#define GetKnownProcAddress(hmod, func) GetKnownProcAddressImpl(hmod, #func, func##Func);
|
||
|
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace transport
|
||
10 years ago
|
{
|
||
|
UPnP::UPnP () : m_Thread (nullptr) , m_IsModuleLoaded (false)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void UPnP::Stop ()
|
||
|
{
|
||
|
if (m_Thread)
|
||
|
{
|
||
|
m_Thread->join ();
|
||
|
delete m_Thread;
|
||
|
m_Thread = nullptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void UPnP::Start()
|
||
|
{
|
||
10 years ago
|
if (!m_IsModuleLoaded) {
|
||
|
#ifdef MAC_OSX
|
||
|
m_Module = dlopen ("libminiupnpc.dylib", RTLD_LAZY);
|
||
|
#elif _WIN32
|
||
|
m_Module = LoadLibrary ("miniupnpc.dll"); // official prebuilt binary, e.g., in upnpc-exe-win32-20140422.zip
|
||
|
#else
|
||
|
m_Module = dlopen ("libminiupnpc.so", RTLD_LAZY);
|
||
|
#endif
|
||
|
if (m_Module == NULL)
|
||
|
{
|
||
|
LogPrint ("Error loading UPNP library. This often happens if there is version mismatch!");
|
||
|
return;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
upnpDiscoverFunc = GetKnownProcAddress (m_Module, upnpDiscover);
|
||
|
UPNP_GetValidIGDFunc = GetKnownProcAddress (m_Module, UPNP_GetValidIGD);
|
||
|
UPNP_GetExternalIPAddressFunc = GetKnownProcAddress (m_Module, UPNP_GetExternalIPAddress);
|
||
|
UPNP_AddPortMappingFunc = GetKnownProcAddress (m_Module, UPNP_AddPortMapping);
|
||
|
UPNP_DeletePortMappingFunc = GetKnownProcAddress (m_Module, UPNP_DeletePortMapping);
|
||
|
freeUPNPDevlistFunc = GetKnownProcAddress (m_Module, freeUPNPDevlist);
|
||
|
FreeUPNPUrlsFunc = GetKnownProcAddress (m_Module, FreeUPNPUrls);
|
||
|
if (upnpDiscoverFunc && UPNP_GetValidIGDFunc && UPNP_GetExternalIPAddressFunc && UPNP_AddPortMappingFunc &&
|
||
|
UPNP_DeletePortMappingFunc && freeUPNPDevlistFunc && FreeUPNPUrlsFunc)
|
||
|
m_IsModuleLoaded = true;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
m_Thread = new std::thread (std::bind (&UPnP::Run, this));
|
||
|
}
|
||
|
|
||
|
UPnP::~UPnP ()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void UPnP::Run ()
|
||
|
{
|
||
|
for (auto& address : context.GetRouterInfo ().GetAddresses ())
|
||
|
{
|
||
|
if (!address.host.is_v6 ())
|
||
|
{
|
||
|
Discover ();
|
||
|
if (address.transportStyle == data::RouterInfo::eTransportSSU )
|
||
|
{
|
||
10 years ago
|
TryPortMapping (I2P_UPNP_UDP, address.port);
|
||
10 years ago
|
}
|
||
|
else if (address.transportStyle == data::RouterInfo::eTransportNTCP )
|
||
|
{
|
||
10 years ago
|
TryPortMapping (I2P_UPNP_TCP, address.port);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void UPnP::Discover ()
|
||
|
{
|
||
|
#ifndef UPNPDISCOVER_SUCCESS
|
||
|
/* miniupnpc 1.5 */
|
||
|
m_Devlist = upnpDiscoverFunc (2000, m_MulticastIf, m_Minissdpdpath, 0);
|
||
|
#else
|
||
|
/* miniupnpc 1.6 */
|
||
|
int nerror = 0;
|
||
|
m_Devlist = upnpDiscoverFunc (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, &nerror);
|
||
|
#endif
|
||
10 years ago
|
|
||
10 years ago
|
int r;
|
||
10 years ago
|
r = UPNP_GetValidIGDFunc (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
|
||
10 years ago
|
if (r == 1)
|
||
|
{
|
||
|
r = UPNP_GetExternalIPAddressFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_externalIPAddress);
|
||
|
if(r != UPNPCOMMAND_SUCCESS)
|
||
|
{
|
||
|
LogPrint ("UPnP: UPNP_GetExternalIPAddress () returned ", r);
|
||
|
return;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (m_externalIPAddress[0])
|
||
|
{
|
||
|
LogPrint ("UPnP: ExternalIPAddress = ", m_externalIPAddress);
|
||
|
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (m_externalIPAddress));
|
||
|
return;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
LogPrint ("UPnP: GetExternalIPAddress failed.");
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void UPnP::TryPortMapping (int type, int port)
|
||
10 years ago
|
{
|
||
10 years ago
|
std::string strType, strPort (std::to_string (port));
|
||
10 years ago
|
switch (type)
|
||
|
{
|
||
|
case I2P_UPNP_TCP:
|
||
|
strType = "TCP";
|
||
|
break;
|
||
|
case I2P_UPNP_UDP:
|
||
|
default:
|
||
|
strType = "UDP";
|
||
|
}
|
||
|
int r;
|
||
|
std::string strDesc = "I2Pd";
|
||
|
try {
|
||
|
for (;;) {
|
||
|
#ifndef UPNPDISCOVER_SUCCESS
|
||
|
/* miniupnpc 1.5 */
|
||
10 years ago
|
r = UPNP_AddPortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strPort.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), 0);
|
||
10 years ago
|
#else
|
||
|
/* miniupnpc 1.6 */
|
||
10 years ago
|
r = UPNP_AddPortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strPort.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), 0, "0");
|
||
10 years ago
|
#endif
|
||
|
if (r!=UPNPCOMMAND_SUCCESS)
|
||
|
{
|
||
10 years ago
|
LogPrint ("AddPortMapping (", strPort.c_str () ,", ", strPort.c_str () ,", ", m_NetworkAddr, ") failed with code ", r);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
LogPrint ("UPnP Port Mapping successful. (", m_NetworkAddr ,":", strPort.c_str(), " type ", strType.c_str () ," -> ", m_externalIPAddress ,":", strPort.c_str() ,")");
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
std::this_thread::sleep_for(std::chrono::minutes(20)); // c++11
|
||
|
//boost::this_thread::sleep_for(); // pre c++11
|
||
|
//sleep(20*60); // non-portable
|
||
10 years ago
|
}
|
||
|
}
|
||
|
catch (boost::thread_interrupted)
|
||
|
{
|
||
10 years ago
|
CloseMapping(type, port);
|
||
10 years ago
|
Close();
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void UPnP::CloseMapping (int type, int port)
|
||
10 years ago
|
{
|
||
10 years ago
|
std::string strType, strPort (std::to_string (port));
|
||
10 years ago
|
switch (type)
|
||
|
{
|
||
|
case I2P_UPNP_TCP:
|
||
|
strType = "TCP";
|
||
|
break;
|
||
|
case I2P_UPNP_UDP:
|
||
|
default:
|
||
|
strType = "UDP";
|
||
|
}
|
||
|
int r = 0;
|
||
10 years ago
|
r = UPNP_DeletePortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), 0);
|
||
10 years ago
|
LogPrint ("UPNP_DeletePortMapping() returned : ", r, "\n");
|
||
|
}
|
||
|
|
||
|
void UPnP::Close ()
|
||
|
{
|
||
|
freeUPNPDevlistFunc (m_Devlist);
|
||
|
m_Devlist = 0;
|
||
|
FreeUPNPUrlsFunc (&m_upnpUrls);
|
||
10 years ago
|
#ifndef _WIN32
|
||
10 years ago
|
dlclose (m_Module);
|
||
10 years ago
|
#else
|
||
|
FreeLibrary (m_Module);
|
||
|
#endif
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
#endif
|
||
|
|