i2pd/daemon/UPnP.cpp

213 lines
5.1 KiB
C++
Raw Normal View History

#ifdef USE_UPNP
2014-02-10 00:15:47 +04:00
#include <string>
#include <thread>
#include <boost/thread/thread.hpp>
#include <boost/asio.hpp>
2014-02-10 00:15:47 +04:00
#include <boost/bind.hpp>
2014-02-10 00:15:47 +04:00
#include "Log.h"
#include "RouterContext.h"
2014-02-10 00:15:47 +04:00
#include "UPnP.h"
#include "NetDb.hpp"
#include "util.h"
#include "RouterInfo.h"
2016-07-19 19:03:03 +03:00
#include "Config.h"
#include <miniupnpc/miniupnpc.h>
#include <miniupnpc/upnpcommands.h>
2014-02-10 00:15:47 +04:00
namespace i2p
{
namespace transport
{
UPnP::UPnP () : m_IsRunning(false), m_Thread (nullptr), m_Timer (m_Service)
{
}
void UPnP::Stop ()
{
2016-07-18 21:01:58 +03:00
if (m_IsRunning)
{
LogPrint(eLogInfo, "UPnP: stopping");
m_IsRunning = false;
2016-07-18 21:01:58 +03:00
m_Timer.cancel ();
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
m_Thread.reset (nullptr);
}
2016-07-18 21:01:58 +03:00
CloseMapping ();
Close ();
2016-07-18 21:01:58 +03:00
}
}
void UPnP::Start()
{
2016-07-18 21:01:58 +03:00
m_IsRunning = true;
LogPrint(eLogInfo, "UPnP: starting");
2016-07-18 21:01:58 +03:00
m_Service.post (std::bind (&UPnP::Discover, this));
2016-07-31 17:22:41 +03:00
std::unique_lock<std::mutex> l(m_StartedMutex);
m_Thread.reset (new std::thread (std::bind (&UPnP::Run, this)));
2016-07-31 17:22:41 +03:00
m_Started.wait_for (l, std::chrono::seconds (5)); // 5 seconds maximum
}
UPnP::~UPnP ()
{
2016-07-18 21:01:58 +03:00
Stop ();
}
void UPnP::Run ()
{
2016-07-18 21:01:58 +03:00
while (m_IsRunning)
{
try
{
2016-07-18 21:01:58 +03:00
m_Service.run ();
2016-11-18 18:27:49 +03:00
// Discover failed
break; // terminate the thread
2016-07-18 21:01:58 +03:00
}
catch (std::exception& ex)
{
LogPrint (eLogError, "UPnP: runtime exception: ", ex.what ());
2016-11-18 18:27:49 +03:00
PortMapping ();
}
}
}
void UPnP::Discover ()
{
#if MINIUPNPC_API_VERSION >= 14
2017-11-14 06:34:57 +03:00
int nerror = 0;
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, 2, &nerror);
#elif ( MINIUPNPC_API_VERSION >= 8 || defined(UPNPDISCOVER_SUCCESS) )
2017-11-14 06:34:57 +03:00
int nerror = 0;
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, &nerror);
2017-11-14 03:41:01 +03:00
#else
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0);
#endif
2016-07-31 17:22:41 +03:00
{
// notify satrting thread
2016-07-31 17:22:41 +03:00
std::unique_lock<std::mutex> l(m_StartedMutex);
m_Started.notify_all ();
}
int r;
r = UPNP_GetValidIGD (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
if (r == 1)
{
r = UPNP_GetExternalIPAddress (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_externalIPAddress);
if(r != UPNPCOMMAND_SUCCESS)
{
LogPrint (eLogError, "UPnP: UPNP_GetExternalIPAddress() returned ", r);
return;
}
else
{
if (!m_externalIPAddress[0])
{
LogPrint (eLogError, "UPnP: GetExternalIPAddress() failed.");
return;
}
}
}
2016-07-18 21:01:58 +03:00
else
{
LogPrint (eLogError, "UPnP: GetValidIGD() failed.");
return;
2016-07-18 21:01:58 +03:00
}
// UPnP discovered
2016-07-18 21:01:58 +03:00
LogPrint (eLogDebug, "UPnP: ExternalIPAddress is ", m_externalIPAddress);
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (m_externalIPAddress));
2016-07-18 21:01:58 +03:00
// port mapping
PortMapping ();
}
2016-07-18 21:01:58 +03:00
void UPnP::PortMapping ()
{
2016-08-10 01:16:24 +03:00
const auto& a = context.GetRouterInfo().GetAddresses();
for (const auto& address : a)
{
2018-08-16 18:02:53 +03:00
if (!address->host.is_v6 () && address->port)
TryPortMapping (address);
}
2016-07-18 21:01:58 +03:00
m_Timer.expires_from_now (boost::posix_time::minutes(20)); // every 20 minutes
m_Timer.async_wait ([this](const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
PortMapping ();
});
2016-07-18 21:01:58 +03:00
}
2016-07-18 21:01:58 +03:00
void UPnP::CloseMapping ()
{
2016-08-10 01:16:24 +03:00
const auto& a = context.GetRouterInfo().GetAddresses();
for (const auto& address : a)
{
2018-08-16 18:02:53 +03:00
if (!address->host.is_v6 () && address->port)
CloseMapping (address);
}
}
void UPnP::TryPortMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address)
{
std::string strType (GetProto (address)), strPort (std::to_string (address->port));
int r;
std::string strDesc; i2p::config::GetOption("upnp.name", strDesc);
#ifdef UPNPDISCOVER_SUCCESS
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");
2017-11-14 03:41:01 +03:00
#else
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);
#endif
if (r!=UPNPCOMMAND_SUCCESS)
{
LogPrint (eLogError, "UPnP: AddPortMapping (", m_NetworkAddr, ":", strPort, ") failed with code ", r);
return;
}
else
{
LogPrint (eLogDebug, "UPnP: Port Mapping successful. (", m_NetworkAddr ,":", strPort, " type ", strType, " -> ", m_externalIPAddress ,":", strPort ,")");
return;
}
}
void UPnP::CloseMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address)
{
std::string strType (GetProto (address)), strPort (std::to_string (address->port));
int r = 0;
r = UPNP_DeletePortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), 0);
LogPrint (eLogError, "UPnP: DeletePortMapping() returned : ", r);
}
void UPnP::Close ()
{
freeUPNPDevlist (m_Devlist);
m_Devlist = 0;
FreeUPNPUrls (&m_upnpUrls);
}
2016-07-18 21:01:58 +03:00
std::string UPnP::GetProto (std::shared_ptr<i2p::data::RouterInfo::Address> address)
{
switch (address->transportStyle)
{
case i2p::data::RouterInfo::eTransportNTCP:
return "TCP";
break;
case i2p::data::RouterInfo::eTransportSSU:
default:
return "UDP";
}
2016-07-18 21:01:58 +03:00
}
2014-02-10 00:15:47 +04:00
}
}
#else /* USE_UPNP */
namespace i2p {
namespace transport {
}
}
#endif /* USE_UPNP */