mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
Libminiupnpc library support (v1.5 and v1.6), dynamic runtime linking.
This commit is contained in:
parent
f552f24e6e
commit
7acdc0a606
19
Daemon.cpp
19
Daemon.cpp
|
@ -18,6 +18,11 @@
|
||||||
#include "HTTPServer.h"
|
#include "HTTPServer.h"
|
||||||
#include "ClientContext.h"
|
#include "ClientContext.h"
|
||||||
|
|
||||||
|
#ifdef USE_UPNP
|
||||||
|
#include "UPnP.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
namespace util
|
namespace util
|
||||||
|
@ -89,11 +94,11 @@ namespace i2p
|
||||||
if (isDaemon)
|
if (isDaemon)
|
||||||
{
|
{
|
||||||
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
|
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
logfile_path.append("/i2pd.log");
|
logfile_path.append("/i2pd.log");
|
||||||
#else
|
#else
|
||||||
logfile_path.append("\\i2pd.log");
|
logfile_path.append("\\i2pd.log");
|
||||||
#endif
|
#endif
|
||||||
StartLog (logfile_path);
|
StartLog (logfile_path);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -111,7 +116,10 @@ namespace i2p
|
||||||
LogPrint("Tunnels started");
|
LogPrint("Tunnels started");
|
||||||
i2p::client::context.Start ();
|
i2p::client::context.Start ();
|
||||||
LogPrint("Client started");
|
LogPrint("Client started");
|
||||||
|
#ifdef USE_UPNP
|
||||||
|
i2p::UPnP::upnpc.Start();
|
||||||
|
LogPrint("UPnP module loaded");
|
||||||
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,6 +136,9 @@ namespace i2p
|
||||||
LogPrint("NetDB stoped");
|
LogPrint("NetDB stoped");
|
||||||
d.httpServer->Stop();
|
d.httpServer->Stop();
|
||||||
LogPrint("HTTP Server stoped");
|
LogPrint("HTTP Server stoped");
|
||||||
|
#ifdef USE_UPNP
|
||||||
|
i2p::UPnP::upnpc.Stop();
|
||||||
|
#endif
|
||||||
StopLog ();
|
StopLog ();
|
||||||
|
|
||||||
delete d.httpServer; d.httpServer = nullptr;
|
delete d.httpServer; d.httpServer = nullptr;
|
||||||
|
|
|
@ -36,6 +36,12 @@ else
|
||||||
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# UPNP Support (miniupnpc 1.5 or 1.6)
|
||||||
|
ifeq ($(USE_UPNP),1)
|
||||||
|
LDFLAGS += -ldl
|
||||||
|
CXXFLAGS += -DUSE_UPNP
|
||||||
|
endif
|
||||||
|
|
||||||
IS_64 := $(shell $(CXX) -dumpmachine 2>&1 | $(GREP) -c "64")
|
IS_64 := $(shell $(CXX) -dumpmachine 2>&1 | $(GREP) -c "64")
|
||||||
ifeq ($(USE_AESNI),yes)
|
ifeq ($(USE_AESNI),yes)
|
||||||
ifeq ($(IS_64),1)
|
ifeq ($(IS_64),1)
|
||||||
|
|
234
UPnP.cpp
234
UPnP.cpp
|
@ -1,19 +1,61 @@
|
||||||
|
#ifdef USE_UPNP
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <thread>
|
||||||
|
|
||||||
|
#include <boost/thread/thread.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
|
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
#include "RouterContext.h"
|
||||||
#include "UPnP.h"
|
#include "UPnP.h"
|
||||||
|
#include "NetDb.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <miniupnpc/miniupnpc.h>
|
||||||
|
#include <miniupnpc/upnpcommands.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
#ifndef UPNPDISCOVER_SUCCESS
|
||||||
|
/* miniupnpc 1.5 */
|
||||||
|
typedef UPNPDev* (*upnp_upnpDiscoverFunc) (int, const char *, const char *, int);
|
||||||
|
typedef int (*upnp_UPNP_AddPortMappingFunc) (const char *, const char *, const char *, const char *,
|
||||||
|
const char *, const char *, const char *, const char *);
|
||||||
|
#else
|
||||||
|
/* miniupnpc 1.6 */
|
||||||
|
typedef UPNPDev* (*upnp_upnpDiscoverFunc) (int, const char *, const char *, int, int, int *);
|
||||||
|
typedef int (*upnp_UPNP_AddPortMappingFunc) (const char *, const char *, const char *, const char *,
|
||||||
|
const char *, const char *, const char *, const char *, const char *);
|
||||||
|
#endif
|
||||||
|
typedef int (*upnp_UPNP_GetValidIGDFunc) (struct UPNPDev *, struct UPNPUrls *, struct IGDdatas *, char *, int);
|
||||||
|
typedef int (*upnp_UPNP_GetExternalIPAddressFunc) (const char *, const char *, char *);
|
||||||
|
typedef int (*upnp_UPNP_DeletePortMappingFunc) (const char *, const char *, const char *, const char *, const char *);
|
||||||
|
typedef void (*upnp_freeUPNPDevlistFunc) (struct UPNPDev *);
|
||||||
|
typedef void (*upnp_FreeUPNPUrlsFunc) (struct UPNPUrls *);
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
UPnP::UPnP (): m_Timer (m_Service),
|
namespace UPnP
|
||||||
m_Endpoint (boost::asio::ip::udp::v4 (), UPNP_REPLY_PORT),
|
{
|
||||||
m_MulticastEndpoint (boost::asio::ip::address::from_string (UPNP_GROUP), UPNP_PORT),
|
UPnP upnpc;
|
||||||
m_Socket (m_Service, m_Endpoint.protocol ())
|
|
||||||
|
UPnP::UPnP () : m_Thread (nullptr) , m_IsModuleLoaded (false)
|
||||||
{
|
{
|
||||||
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (65535));
|
}
|
||||||
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535));
|
|
||||||
m_Socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
|
void UPnP::Stop ()
|
||||||
|
{
|
||||||
|
if (m_Thread)
|
||||||
|
{
|
||||||
|
m_Thread->join ();
|
||||||
|
delete m_Thread;
|
||||||
|
m_Thread = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UPnP::Start()
|
||||||
|
{
|
||||||
|
m_Thread = new std::thread (std::bind (&UPnP::Run, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
UPnP::~UPnP ()
|
UPnP::~UPnP ()
|
||||||
|
@ -22,47 +64,163 @@ namespace i2p
|
||||||
|
|
||||||
void UPnP::Run ()
|
void UPnP::Run ()
|
||||||
{
|
{
|
||||||
DiscoverRouter ();
|
#ifdef MAC_OSX
|
||||||
m_Service.run ();
|
m_Module = dlopen ("libminiupnpc.dylib", RTLD_LAZY);
|
||||||
|
#elif WIN32
|
||||||
|
m_Module = dlopen ("libminiupnpc.dll", RTLD_LAZY);
|
||||||
|
#else
|
||||||
|
m_Module = dlopen ("libminiupnpc.so", RTLD_LAZY);
|
||||||
|
#endif
|
||||||
|
if (!m_Module)
|
||||||
|
{
|
||||||
|
LogPrint ("no UPnP module available (", dlerror (), ")");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_IsModuleLoaded = true;
|
||||||
|
}
|
||||||
|
for (auto& address : context.GetRouterInfo ().GetAddresses ())
|
||||||
|
{
|
||||||
|
if (!address.host.is_v6 ())
|
||||||
|
{
|
||||||
|
m_Port = std::to_string (util::config::GetArg ("-port", address.port));
|
||||||
|
Discover ();
|
||||||
|
if (address.transportStyle == data::RouterInfo::eTransportSSU )
|
||||||
|
{
|
||||||
|
TryPortMapping (I2P_UPNP_UDP);
|
||||||
|
}
|
||||||
|
else if (address.transportStyle == data::RouterInfo::eTransportNTCP )
|
||||||
|
{
|
||||||
|
TryPortMapping (I2P_UPNP_TCP);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPnP::DiscoverRouter ()
|
void UPnP::Discover ()
|
||||||
{
|
{
|
||||||
m_Timer.expires_from_now (boost::posix_time::seconds(5)); // 5 seconds
|
const char *error;
|
||||||
m_Timer.async_wait (boost::bind (&UPnP::HandleTimer, this, boost::asio::placeholders::error));
|
upnp_upnpDiscoverFunc upnpDiscoverFunc = (upnp_upnpDiscoverFunc) dlsym (m_Module, "upnpDiscover");
|
||||||
|
// reinterpret_cast<upnp_upnpDiscoverFunc> (dlsym(...));
|
||||||
std::string address = UPNP_GROUP;
|
if ( (error = dlerror ()))
|
||||||
address += ":" + boost::lexical_cast<std::string>(UPNP_PORT);
|
{
|
||||||
std::string request = "M-SEARCH * HTTP/1.1\r\n"
|
LogPrint ("Error loading UPNP library. This often happens if there is version mismatch!");
|
||||||
"HOST: " + address + "\r\n"
|
return;
|
||||||
"ST:" + UPNP_ROUTER + "\r\n"
|
}
|
||||||
"MAN:\"ssdp:discover\"\r\n"
|
#ifndef UPNPDISCOVER_SUCCESS
|
||||||
"MX:3\r\n"
|
/* miniupnpc 1.5 */
|
||||||
"\r\n\r\n";
|
m_Devlist = upnpDiscoverFunc (2000, m_MulticastIf, m_Minissdpdpath, 0);
|
||||||
m_Socket.send_to (boost::asio::buffer (request.c_str (), request.length ()), m_MulticastEndpoint);
|
#else
|
||||||
Receive ();
|
/* miniupnpc 1.6 */
|
||||||
|
int nerror = 0;
|
||||||
|
m_Devlist = upnpDiscoverFunc (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, &nerror);
|
||||||
|
#endif
|
||||||
|
int r;
|
||||||
|
upnp_UPNP_GetValidIGDFunc UPNP_GetValidIGDFunc = (upnp_UPNP_GetValidIGDFunc) dlsym (m_Module, "UPNP_GetValidIGD");
|
||||||
|
r = (*UPNP_GetValidIGDFunc) (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
|
||||||
|
if (r == 1)
|
||||||
|
{
|
||||||
|
upnp_UPNP_GetExternalIPAddressFunc UPNP_GetExternalIPAddressFunc = (upnp_UPNP_GetExternalIPAddressFunc) dlsym (m_Module, "UPNP_GetExternalIPAddress");
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPnP::Receive ()
|
void UPnP::TryPortMapping (int type)
|
||||||
{
|
{
|
||||||
m_Socket.async_receive_from (boost::asio::buffer (m_ReceiveBuffer, UPNP_MAX_PACKET_LEN), m_SenderEndpoint,
|
std::string strType;
|
||||||
boost::bind (&UPnP::HandleReceivedFrom, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
|
switch (type)
|
||||||
|
{
|
||||||
|
case I2P_UPNP_TCP:
|
||||||
|
strType = "TCP";
|
||||||
|
break;
|
||||||
|
case I2P_UPNP_UDP:
|
||||||
|
default:
|
||||||
|
strType = "UDP";
|
||||||
|
}
|
||||||
|
int r;
|
||||||
|
std::string strDesc = "I2Pd";
|
||||||
|
try {
|
||||||
|
for (;;) {
|
||||||
|
upnp_UPNP_AddPortMappingFunc UPNP_AddPortMappingFunc = (upnp_UPNP_AddPortMappingFunc) dlsym(m_Module, "UPNP_AddPortMapping");
|
||||||
|
#ifndef UPNPDISCOVER_SUCCESS
|
||||||
|
/* miniupnpc 1.5 */
|
||||||
|
r = UPNP_AddPortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_Port.c_str (), m_Port.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), 0);
|
||||||
|
#else
|
||||||
|
/* miniupnpc 1.6 */
|
||||||
|
r = UPNP_AddPortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_Port.c_str (), m_Port.c_str (), m_NetworkAddr, strDesc.c_str (), strType.c_str (), 0, "0");
|
||||||
|
#endif
|
||||||
|
if (r!=UPNPCOMMAND_SUCCESS)
|
||||||
|
{
|
||||||
|
LogPrint ("AddPortMapping (", m_Port.c_str () ,", ", m_Port.c_str () ,", ", m_NetworkAddr, ") failed with code ", r);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogPrint ("UPnP Port Mapping successful. (", m_NetworkAddr ,":", m_Port.c_str(), " type ", strType.c_str () ," -> ", m_externalIPAddress ,":", m_Port.c_str() ,")");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sleep(20*60);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (boost::thread_interrupted)
|
||||||
|
{
|
||||||
|
CloseMapping(type);
|
||||||
|
Close();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPnP::HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred)
|
void UPnP::CloseMapping (int type)
|
||||||
{
|
{
|
||||||
LogPrint ("UPnP: ", bytes_transferred, " received from ", m_SenderEndpoint.address ());
|
std::string strType;
|
||||||
std::string str (m_ReceiveBuffer, bytes_transferred);
|
switch (type)
|
||||||
LogPrint (str);
|
{
|
||||||
m_Timer.cancel ();
|
case I2P_UPNP_TCP:
|
||||||
|
strType = "TCP";
|
||||||
|
break;
|
||||||
|
case I2P_UPNP_UDP:
|
||||||
|
default:
|
||||||
|
strType = "UDP";
|
||||||
|
}
|
||||||
|
int r = 0;
|
||||||
|
upnp_UPNP_DeletePortMappingFunc UPNP_DeletePortMappingFunc = (upnp_UPNP_DeletePortMappingFunc) dlsym (m_Module, "UPNP_DeletePortMapping");
|
||||||
|
r = UPNP_DeletePortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_Port.c_str (), strType.c_str (), 0);
|
||||||
|
LogPrint ("UPNP_DeletePortMapping() returned : ", r, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPnP::HandleTimer (const boost::system::error_code& ecode)
|
void UPnP::Close ()
|
||||||
{
|
{
|
||||||
if (ecode != boost::asio::error::operation_aborted)
|
upnp_freeUPNPDevlistFunc freeUPNPDevlistFunc = (upnp_freeUPNPDevlistFunc) dlsym (m_Module, "freeUPNPDevlist");
|
||||||
{
|
freeUPNPDevlistFunc (m_Devlist);
|
||||||
LogPrint ("UPnP: timeout expired");
|
m_Devlist = 0;
|
||||||
m_Service.stop ();
|
upnp_FreeUPNPUrlsFunc FreeUPNPUrlsFunc = (upnp_FreeUPNPUrlsFunc) dlsym (m_Module, "FreeUPNPUrlsFunc");
|
||||||
}
|
FreeUPNPUrlsFunc (&m_upnpUrls);
|
||||||
|
dlclose (m_Module);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
64
UPnP.h
64
UPnP.h
|
@ -1,41 +1,61 @@
|
||||||
#ifndef UPNP_H__
|
#ifndef __UPNP_H__
|
||||||
#define UPNP_H__
|
#define __UPNP_H__
|
||||||
|
|
||||||
|
#ifdef USE_UPNP
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <miniupnpc/miniwget.h>
|
||||||
|
#include <miniupnpc/miniupnpc.h>
|
||||||
|
#include <miniupnpc/upnpcommands.h>
|
||||||
|
#include <miniupnpc/upnperrors.h>
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#define I2P_UPNP_TCP 1
|
||||||
|
#define I2P_UPNP_UDP 2
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
const int UPNP_MAX_PACKET_LEN = 1500;
|
namespace UPnP
|
||||||
const char UPNP_GROUP[] = "239.255.255.250";
|
{
|
||||||
const int UPNP_PORT = 1900;
|
|
||||||
const int UPNP_REPLY_PORT = 1901;
|
|
||||||
const char UPNP_ROUTER[] = "urn:schemas-upnp-org:device:InternetGatewayDevice:1";
|
|
||||||
|
|
||||||
class UPnP
|
class UPnP
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
UPnP ();
|
UPnP ();
|
||||||
~UPnP ();
|
~UPnP ();
|
||||||
|
void Close ();
|
||||||
|
|
||||||
|
void Start ();
|
||||||
|
void Stop ();
|
||||||
|
|
||||||
|
void Discover ();
|
||||||
|
void TryPortMapping (int type);
|
||||||
|
void CloseMapping (int type);
|
||||||
|
private:
|
||||||
void Run ();
|
void Run ();
|
||||||
|
|
||||||
|
std::thread * m_Thread;
|
||||||
|
struct UPNPUrls m_upnpUrls;
|
||||||
|
struct IGDdatas m_upnpData;
|
||||||
|
|
||||||
private:
|
// For miniupnpc
|
||||||
|
char * m_MulticastIf = 0;
|
||||||
void DiscoverRouter ();
|
char * m_Minissdpdpath = 0;
|
||||||
void Receive ();
|
struct UPNPDev * m_Devlist = 0;
|
||||||
void HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred);
|
char m_NetworkAddr[64];
|
||||||
void HandleTimer (const boost::system::error_code& ecode);
|
char m_externalIPAddress[40];
|
||||||
|
bool m_IsModuleLoaded;
|
||||||
private:
|
std::string m_Port = std::to_string (util::config::GetArg ("-port", 17070));
|
||||||
|
void *m_Module;
|
||||||
boost::asio::io_service m_Service;
|
|
||||||
boost::asio::deadline_timer m_Timer;
|
|
||||||
boost::asio::ip::udp::endpoint m_Endpoint, m_MulticastEndpoint, m_SenderEndpoint;
|
|
||||||
boost::asio::ip::udp::socket m_Socket;
|
|
||||||
char m_ReceiveBuffer[UPNP_MAX_PACKET_LEN];
|
|
||||||
};
|
};
|
||||||
|
extern UPnP upnpc;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue