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 "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"
|
2017-04-22 00:04:16 +00:00
|
|
|
#include "NetDb.hpp"
|
2015-01-03 20:38:48 +00:00
|
|
|
#include "util.h"
|
2016-03-09 06:42:58 +00:00
|
|
|
#include "RouterInfo.h"
|
2016-07-19 16:03:03 +00:00
|
|
|
#include "Config.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
|
|
|
{
|
2017-02-15 06:04:40 +00:00
|
|
|
UPnP::UPnP () : m_IsRunning(false), m_Thread (nullptr), m_Timer (m_Service)
|
|
|
|
{
|
|
|
|
}
|
2015-01-03 20:38:48 +00:00
|
|
|
|
2017-02-15 06:04:40 +00:00
|
|
|
void UPnP::Stop ()
|
|
|
|
{
|
2016-07-18 18:01:58 +00:00
|
|
|
if (m_IsRunning)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "UPnP: Stopping");
|
2017-02-15 06:04:40 +00:00
|
|
|
m_IsRunning = false;
|
2016-07-18 18:01:58 +00:00
|
|
|
m_Timer.cancel ();
|
|
|
|
m_Service.stop ();
|
2017-02-15 06:04:40 +00:00
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
m_Thread.reset (nullptr);
|
|
|
|
}
|
2016-07-18 18:01:58 +00:00
|
|
|
CloseMapping ();
|
2017-02-15 06:04:40 +00:00
|
|
|
Close ();
|
2016-07-18 18:01:58 +00:00
|
|
|
}
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
2015-01-03 20:38:48 +00:00
|
|
|
|
2017-02-15 06:04:40 +00:00
|
|
|
void UPnP::Start()
|
|
|
|
{
|
2016-07-18 18:01:58 +00:00
|
|
|
m_IsRunning = true;
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "UPnP: Starting");
|
2016-07-18 18:01:58 +00:00
|
|
|
m_Service.post (std::bind (&UPnP::Discover, this));
|
2016-07-31 14:22:41 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_StartedMutex);
|
2017-02-15 06:04:40 +00:00
|
|
|
m_Thread.reset (new std::thread (std::bind (&UPnP::Run, this)));
|
2016-07-31 14:22:41 +00:00
|
|
|
m_Started.wait_for (l, std::chrono::seconds (5)); // 5 seconds maximum
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UPnP::~UPnP ()
|
|
|
|
{
|
2016-07-18 18:01:58 +00:00
|
|
|
Stop ();
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
2015-01-03 20:38:48 +00:00
|
|
|
|
2017-02-15 06:04:40 +00:00
|
|
|
void UPnP::Run ()
|
|
|
|
{
|
2020-12-07 03:22:16 +00:00
|
|
|
i2p::util::SetThreadName("UPnP");
|
|
|
|
|
2016-07-18 18:01:58 +00:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
2017-02-15 06:04:40 +00:00
|
|
|
{
|
2016-07-18 18:01:58 +00:00
|
|
|
m_Service.run ();
|
2016-11-18 15:27:49 +00:00
|
|
|
// Discover failed
|
|
|
|
break; // terminate the thread
|
2016-07-18 18:01:58 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Runtime exception: ", ex.what ());
|
2016-11-18 15:27:49 +00:00
|
|
|
PortMapping ();
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Discover ()
|
|
|
|
{
|
2019-09-05 18:58:28 +00:00
|
|
|
bool isError;
|
2021-02-24 21:37:41 +00:00
|
|
|
int err;
|
2019-09-05 18:58:28 +00:00
|
|
|
|
|
|
|
#if ((MINIUPNPC_API_VERSION >= 8) || defined (UPNPDISCOVER_SUCCESS))
|
2021-02-24 21:37:41 +00:00
|
|
|
err = UPNPDISCOVER_SUCCESS;
|
2019-09-05 18:58:28 +00:00
|
|
|
|
|
|
|
#if (MINIUPNPC_API_VERSION >= 14)
|
|
|
|
m_Devlist = upnpDiscover (UPNP_RESPONSE_TIMEOUT, NULL, NULL, 0, 0, 2, &err);
|
2017-11-14 00:41:01 +00:00
|
|
|
#else
|
2019-09-05 18:58:28 +00:00
|
|
|
m_Devlist = upnpDiscover (UPNP_RESPONSE_TIMEOUT, NULL, NULL, 0, 0, &err);
|
2015-01-03 20:38:48 +00:00
|
|
|
#endif
|
2019-09-05 18:58:28 +00:00
|
|
|
|
|
|
|
isError = err != UPNPDISCOVER_SUCCESS;
|
2022-05-20 16:56:05 +00:00
|
|
|
#else // MINIUPNPC_API_VERSION >= 8
|
2021-02-24 21:37:41 +00:00
|
|
|
err = 0;
|
|
|
|
m_Devlist = upnpDiscover (UPNP_RESPONSE_TIMEOUT, NULL, NULL, 0);
|
2019-09-05 18:58:28 +00:00
|
|
|
isError = m_Devlist == NULL;
|
|
|
|
#endif // MINIUPNPC_API_VERSION >= 8
|
2016-07-31 14:22:41 +00:00
|
|
|
{
|
2019-09-05 18:58:28 +00:00
|
|
|
// notify starting thread
|
2016-07-31 14:22:41 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_StartedMutex);
|
|
|
|
m_Started.notify_all ();
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 18:58:28 +00:00
|
|
|
if (isError)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Unable to discover Internet Gateway Devices: error ", err);
|
2019-09-05 18:58:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = UPNP_GetValidIGD (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
|
2021-02-24 21:37:41 +00:00
|
|
|
m_upnpUrlsInitialized=err!=0;
|
2019-09-05 18:58:28 +00:00
|
|
|
if (err == UPNP_IGD_VALID_CONNECTED)
|
2017-02-15 06:04:40 +00:00
|
|
|
{
|
2021-02-24 21:37:41 +00:00
|
|
|
err = UPNP_GetExternalIPAddress (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_externalIPAddress);
|
2019-09-05 18:58:28 +00:00
|
|
|
if(err != UPNPCOMMAND_SUCCESS)
|
2017-02-15 06:04:40 +00:00
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Unable to get external address: error ", err);
|
2017-02-15 06:04:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Found Internet Gateway Device ", m_upnpUrls.controlURL);
|
2017-02-15 06:04:40 +00:00
|
|
|
if (!m_externalIPAddress[0])
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Found Internet Gateway Device doesn't know our external address");
|
2017-02-15 06:04:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-18 18:01:58 +00:00
|
|
|
else
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Unable to find valid Internet Gateway Device: error ", err);
|
2017-02-15 06:04:40 +00:00
|
|
|
return;
|
2016-07-18 18:01:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 06:04:40 +00:00
|
|
|
// UPnP discovered
|
2016-07-18 18:01:58 +00:00
|
|
|
LogPrint (eLogDebug, "UPnP: ExternalIPAddress is ", m_externalIPAddress);
|
2017-02-15 06:04:40 +00:00
|
|
|
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (m_externalIPAddress));
|
2016-07-18 18:01:58 +00:00
|
|
|
// port mapping
|
|
|
|
PortMapping ();
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
2015-01-03 20:38:48 +00:00
|
|
|
|
2019-09-05 18:58:28 +00:00
|
|
|
int UPnP::CheckMapping (const char* port, const char* type)
|
|
|
|
{
|
|
|
|
int err = UPNPCOMMAND_SUCCESS;
|
|
|
|
|
|
|
|
#if (MINIUPNPC_API_VERSION >= 10)
|
|
|
|
err = UPNP_GetSpecificPortMappingEntry(m_upnpUrls.controlURL, m_upnpData.first.servicetype, port, type, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
#elif ((MINIUPNPC_API_VERSION >= 8) || defined (UPNPDISCOVER_SUCCESS))
|
|
|
|
err = UPNP_GetSpecificPortMappingEntry(m_upnpUrls.controlURL, m_upnpData.first.servicetype, port, type, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
#else
|
|
|
|
err = UPNP_GetSpecificPortMappingEntry(m_upnpUrls.controlURL, m_upnpData.first.servicetype, port, type, NULL, NULL);
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-07-18 18:01:58 +00:00
|
|
|
void UPnP::PortMapping ()
|
|
|
|
{
|
2022-12-05 01:04:06 +00:00
|
|
|
auto a = context.GetRouterInfo().GetAddresses();
|
|
|
|
if (!a) return;
|
|
|
|
for (const auto& address : *a)
|
2017-02-15 06:04:40 +00:00
|
|
|
{
|
2023-01-04 13:41:54 +00:00
|
|
|
if (address && !address->host.is_v6 () && address->port)
|
2018-08-16 15:02:53 +00:00
|
|
|
TryPortMapping (address);
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
2019-09-05 18:58:28 +00:00
|
|
|
m_Timer.expires_from_now (boost::posix_time::minutes(20)); // every 20 minutes
|
2016-07-18 18:01:58 +00:00
|
|
|
m_Timer.async_wait ([this](const boost::system::error_code& ecode)
|
2017-02-15 06:04:40 +00:00
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
PortMapping ();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::TryPortMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address)
|
|
|
|
{
|
|
|
|
std::string strType (GetProto (address)), strPort (std::to_string (address->port));
|
|
|
|
std::string strDesc; i2p::config::GetOption("upnp.name", strDesc);
|
2019-09-05 18:58:28 +00:00
|
|
|
int err = UPNPCOMMAND_SUCCESS;
|
|
|
|
|
|
|
|
// check for existing mapping
|
|
|
|
err = CheckMapping (strPort.c_str (), strType.c_str ());
|
|
|
|
if (err != UPNPCOMMAND_SUCCESS) // if mapping not found
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogDebug, "UPnP: Port ", strPort, " is possibly not forwarded: return code ", err);
|
2019-09-05 18:58:28 +00:00
|
|
|
|
|
|
|
#if ((MINIUPNPC_API_VERSION >= 8) || defined (UPNPDISCOVER_SUCCESS))
|
|
|
|
err = UPNP_AddPortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strPort.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), NULL, NULL);
|
2017-11-14 00:41:01 +00:00
|
|
|
#else
|
2019-09-05 18:58:28 +00:00
|
|
|
err = UPNP_AddPortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strPort.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), NULL);
|
2017-11-14 00:41:01 +00:00
|
|
|
#endif
|
2019-09-05 18:58:28 +00:00
|
|
|
if (err != UPNPCOMMAND_SUCCESS)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "UPnP: Port forwarding to ", m_NetworkAddr, ":", strPort, " failed: return code ", err);
|
2019-09-05 18:58:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogInfo, "UPnP: Port successfully forwarded (", m_externalIPAddress ,":", strPort, " type ", strType, " -> ", m_NetworkAddr ,":", strPort ,")");
|
2019-09-05 18:58:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogDebug, "UPnP: External forward from ", m_NetworkAddr, ":", strPort, " exists on current Internet Gateway Device");
|
2017-02-15 06:04:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:58:28 +00:00
|
|
|
void UPnP::CloseMapping ()
|
|
|
|
{
|
2022-12-05 02:12:48 +00:00
|
|
|
auto a = context.GetRouterInfo().GetAddresses();
|
2022-12-05 01:04:06 +00:00
|
|
|
if (!a) return;
|
|
|
|
for (const auto& address : *a)
|
2019-09-05 18:58:28 +00:00
|
|
|
{
|
2023-01-04 13:41:54 +00:00
|
|
|
if (address && !address->host.is_v6 () && address->port)
|
2019-09-05 18:58:28 +00:00
|
|
|
CloseMapping (address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 06:04:40 +00:00
|
|
|
void UPnP::CloseMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address)
|
|
|
|
{
|
2021-02-24 21:37:41 +00:00
|
|
|
if(!m_upnpUrlsInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-15 06:04:40 +00:00
|
|
|
std::string strType (GetProto (address)), strPort (std::to_string (address->port));
|
2019-09-05 18:58:28 +00:00
|
|
|
int err = UPNPCOMMAND_SUCCESS;
|
2021-11-27 20:30:35 +00:00
|
|
|
|
2019-09-05 18:58:28 +00:00
|
|
|
err = CheckMapping (strPort.c_str (), strType.c_str ());
|
2021-02-24 21:37:41 +00:00
|
|
|
if (err == UPNPCOMMAND_SUCCESS)
|
2019-09-05 18:58:28 +00:00
|
|
|
{
|
|
|
|
err = UPNP_DeletePortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), NULL);
|
|
|
|
LogPrint (eLogError, "UPnP: DeletePortMapping() returned : ", err);
|
|
|
|
}
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UPnP::Close ()
|
|
|
|
{
|
|
|
|
freeUPNPDevlist (m_Devlist);
|
|
|
|
m_Devlist = 0;
|
2021-02-24 21:37:41 +00:00
|
|
|
if(m_upnpUrlsInitialized){
|
|
|
|
FreeUPNPUrls (&m_upnpUrls);
|
|
|
|
m_upnpUrlsInitialized=false;
|
|
|
|
}
|
|
|
|
}
|
2016-07-18 18:01:58 +00:00
|
|
|
|
|
|
|
std::string UPnP::GetProto (std::shared_ptr<i2p::data::RouterInfo::Address> address)
|
|
|
|
{
|
|
|
|
switch (address->transportStyle)
|
2017-02-15 06:04:40 +00:00
|
|
|
{
|
2022-11-23 20:54:08 +00:00
|
|
|
case i2p::data::RouterInfo::eTransportNTCP2:
|
2019-09-05 18:58:28 +00:00
|
|
|
return "TCP";
|
|
|
|
break;
|
2022-11-23 20:54:08 +00:00
|
|
|
case i2p::data::RouterInfo::eTransportSSU2:
|
2017-02-15 06:04:40 +00:00
|
|
|
default:
|
2019-09-05 18:58:28 +00:00
|
|
|
return "UDP";
|
2017-02-15 06:04:40 +00:00
|
|
|
}
|
2016-07-18 18:01:58 +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 */
|