2015-01-03 20:38:48 +00:00
|
|
|
#ifdef USE_UPNP
|
2014-02-09 20:15:47 +00:00
|
|
|
#include <string>
|
2015-01-03 20:38:48 +00:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
#include <boost/asio.hpp>
|
2014-02-09 20:15:47 +00:00
|
|
|
#include <boost/bind.hpp>
|
2015-01-03 20:38:48 +00:00
|
|
|
|
2014-02-09 20:15:47 +00:00
|
|
|
#include "Log.h"
|
2015-06-06 18:53:22 +00:00
|
|
|
|
2015-01-03 20:38:48 +00:00
|
|
|
#include "RouterContext.h"
|
2014-02-09 20:15:47 +00:00
|
|
|
#include "UPnP.h"
|
2015-01-03 20:38:48 +00:00
|
|
|
#include "NetDb.h"
|
|
|
|
#include "util.h"
|
2016-03-09 06:42:58 +00:00
|
|
|
#include "RouterInfo.h"
|
2015-01-03 20:38:48 +00:00
|
|
|
|
|
|
|
#include <miniupnpc/miniupnpc.h>
|
|
|
|
#include <miniupnpc/upnpcommands.h>
|
|
|
|
|
2014-02-09 20:15:47 +00:00
|
|
|
namespace i2p
|
|
|
|
{
|
2015-05-06 16:19:20 +00:00
|
|
|
namespace transport
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
2016-06-24 17:15:51 +00:00
|
|
|
UPnP::UPnP () : m_Thread (nullptr)
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Stop ()
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint(eLogInfo, "UPnP: stopping");
|
2015-01-03 20:38:48 +00:00
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Start()
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint(eLogInfo, "UPnP: starting");
|
2015-01-03 20:38:48 +00:00
|
|
|
m_Thread = new std::thread (std::bind (&UPnP::Run, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
UPnP::~UPnP ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Run ()
|
|
|
|
{
|
2016-03-21 19:13:07 +00:00
|
|
|
const std::vector<std::shared_ptr<i2p::data::RouterInfo::Address> > a = context.GetRouterInfo().GetAddresses();
|
2016-03-21 17:45:35 +00:00
|
|
|
for (auto address : a)
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
2016-03-21 17:45:35 +00:00
|
|
|
if (!address->host.is_v6 ())
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
|
|
|
Discover ();
|
2016-03-21 17:45:35 +00:00
|
|
|
if (address->transportStyle == data::RouterInfo::eTransportSSU )
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
2016-03-21 17:45:35 +00:00
|
|
|
TryPortMapping (I2P_UPNP_UDP, address->port);
|
2015-01-03 20:38:48 +00:00
|
|
|
}
|
2016-03-21 17:45:35 +00:00
|
|
|
else if (address->transportStyle == data::RouterInfo::eTransportNTCP )
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
2016-03-21 17:45:35 +00:00
|
|
|
TryPortMapping (I2P_UPNP_TCP, address->port);
|
2015-01-03 20:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Discover ()
|
|
|
|
{
|
|
|
|
int nerror = 0;
|
2016-03-11 08:35:49 +00:00
|
|
|
#if MINIUPNPC_API_VERSION >= 14
|
2016-06-24 17:15:51 +00:00
|
|
|
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, 2, &nerror);
|
2016-03-09 06:29:32 +00:00
|
|
|
#else
|
2016-06-24 17:15:51 +00:00
|
|
|
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, &nerror);
|
2015-01-03 20:38:48 +00:00
|
|
|
#endif
|
2015-01-03 23:06:43 +00:00
|
|
|
|
2015-01-03 20:38:48 +00:00
|
|
|
int r;
|
2016-06-24 17:15:51 +00:00
|
|
|
r = UPNP_GetValidIGD (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
|
2015-01-03 20:38:48 +00:00
|
|
|
if (r == 1)
|
|
|
|
{
|
2016-06-24 17:15:51 +00:00
|
|
|
r = UPNP_GetExternalIPAddress (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_externalIPAddress);
|
2015-01-03 20:38:48 +00:00
|
|
|
if(r != UPNPCOMMAND_SUCCESS)
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint (eLogError, "UPnP: UPNP_GetExternalIPAddress() returned ", r);
|
2015-01-03 20:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_externalIPAddress[0])
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint (eLogDebug, "UPnP: ExternalIPAddress is ", m_externalIPAddress);
|
2015-01-03 20:38:48 +00:00
|
|
|
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (m_externalIPAddress));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint (eLogError, "UPnP: GetExternalIPAddress() failed.");
|
2015-01-03 20:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 16:19:20 +00:00
|
|
|
void UPnP::TryPortMapping (int type, int port)
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
2015-05-06 16:19:20 +00:00
|
|
|
std::string strType, strPort (std::to_string (port));
|
2015-01-03 20:38:48 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case I2P_UPNP_TCP:
|
|
|
|
strType = "TCP";
|
|
|
|
break;
|
|
|
|
case I2P_UPNP_UDP:
|
|
|
|
default:
|
|
|
|
strType = "UDP";
|
|
|
|
}
|
|
|
|
int r;
|
|
|
|
std::string strDesc = "I2Pd";
|
|
|
|
try {
|
|
|
|
for (;;) {
|
2016-06-24 17:15:51 +00:00
|
|
|
r = UPNP_AddPortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strPort.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), 0, "0");
|
2015-01-03 20:38:48 +00:00
|
|
|
if (r!=UPNPCOMMAND_SUCCESS)
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint (eLogError, "UPnP: AddPortMapping (", m_NetworkAddr, ":", strPort, ") failed with code ", r);
|
2015-01-03 20:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint (eLogDebug, "UPnP: Port Mapping successful. (", m_NetworkAddr ,":", strPort, " type ", strType, " -> ", m_externalIPAddress ,":", strPort ,")");
|
2015-01-03 20:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-06 18:53:22 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::minutes(20)); // c++11
|
|
|
|
//boost::this_thread::sleep_for(); // pre c++11
|
|
|
|
//sleep(20*60); // non-portable
|
2015-01-03 20:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (boost::thread_interrupted)
|
|
|
|
{
|
2015-05-06 16:19:20 +00:00
|
|
|
CloseMapping(type, port);
|
2015-01-03 20:38:48 +00:00
|
|
|
Close();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 16:19:20 +00:00
|
|
|
void UPnP::CloseMapping (int type, int port)
|
2015-01-03 20:38:48 +00:00
|
|
|
{
|
2015-05-06 16:19:20 +00:00
|
|
|
std::string strType, strPort (std::to_string (port));
|
2015-01-03 20:38:48 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case I2P_UPNP_TCP:
|
|
|
|
strType = "TCP";
|
|
|
|
break;
|
|
|
|
case I2P_UPNP_UDP:
|
|
|
|
default:
|
|
|
|
strType = "UDP";
|
|
|
|
}
|
|
|
|
int r = 0;
|
2016-06-24 17:15:51 +00:00
|
|
|
r = UPNP_DeletePortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), 0);
|
2016-07-12 00:00:00 +00:00
|
|
|
LogPrint (eLogError, "UPnP: DeletePortMapping() returned : ", r);
|
2015-01-03 20:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Close ()
|
|
|
|
{
|
2016-06-24 17:15:51 +00:00
|
|
|
freeUPNPDevlist (m_Devlist);
|
2015-01-03 20:38:48 +00:00
|
|
|
m_Devlist = 0;
|
2016-06-24 17:15:51 +00:00
|
|
|
FreeUPNPUrls (&m_upnpUrls);
|
2015-01-03 20:38:48 +00:00
|
|
|
}
|
2014-02-09 20:15:47 +00:00
|
|
|
}
|
2015-01-03 20:38:48 +00:00
|
|
|
}
|
2016-07-12 00:00:00 +00:00
|
|
|
#else /* USE_UPNP */
|
|
|
|
namespace i2p {
|
|
|
|
namespace transport {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* USE_UPNP */
|