2015-01-03 23:38:48 +03:00
# ifdef USE_UPNP
2014-02-10 00:15:47 +04:00
# include <string>
2015-01-03 23:38:48 +03:00
# include <thread>
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
# include <windows.h>
# endif
2015-01-03 23:38:48 +03:00
# include <boost/thread/thread.hpp>
# include <boost/asio.hpp>
2014-02-10 00:15:47 +04:00
# include <boost/bind.hpp>
2015-01-03 23:38:48 +03:00
2014-02-10 00:15:47 +04:00
# include "Log.h"
2015-01-03 23:38:48 +03:00
# include "RouterContext.h"
2014-02-10 00:15:47 +04:00
# include "UPnP.h"
2015-01-03 23:38:48 +03:00
# 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 * ) ;
2014-02-10 00:15:47 +04:00
namespace i2p
{
2015-05-06 19:19:20 +03:00
namespace transport
2015-01-03 23:38:48 +03:00
{
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 ( )
{
m_Thread = new std : : thread ( std : : bind ( & UPnP : : Run , this ) ) ;
}
UPnP : : ~ UPnP ( )
{
}
void UPnP : : Run ( )
{
# ifdef MAC_OSX
m_Module = dlopen ( " libminiupnpc.dylib " , RTLD_LAZY ) ;
2015-01-04 02:06:43 +03:00
# elif _WIN32
m_Module = LoadLibrary ( " libminiupnpc.dll " ) ;
if ( m_Module = = NULL )
{
LogPrint ( " Error loading UPNP library. This often happens if there is version mismatch! " ) ;
return ;
}
else
{
m_IsModuleLoaded = true ;
}
2015-01-03 23:38:48 +03:00
# else
m_Module = dlopen ( " libminiupnpc.so " , RTLD_LAZY ) ;
# endif
2015-01-04 02:06:43 +03:00
# ifndef _WIN32
2015-01-03 23:38:48 +03:00
if ( ! m_Module )
{
LogPrint ( " no UPnP module available ( " , dlerror ( ) , " ) " ) ;
return ;
}
else
{
m_IsModuleLoaded = true ;
}
2015-01-04 02:06:43 +03:00
# endif
2015-01-03 23:38:48 +03:00
for ( auto & address : context . GetRouterInfo ( ) . GetAddresses ( ) )
{
if ( ! address . host . is_v6 ( ) )
{
Discover ( ) ;
if ( address . transportStyle = = data : : RouterInfo : : eTransportSSU )
{
2015-05-06 19:19:20 +03:00
TryPortMapping ( I2P_UPNP_UDP , address . port ) ;
2015-01-03 23:38:48 +03:00
}
else if ( address . transportStyle = = data : : RouterInfo : : eTransportNTCP )
{
2015-05-06 19:19:20 +03:00
TryPortMapping ( I2P_UPNP_TCP , address . port ) ;
2015-01-03 23:38:48 +03:00
}
}
}
}
void UPnP : : Discover ( )
{
const char * error ;
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
upnp_upnpDiscoverFunc upnpDiscoverFunc = ( upnp_upnpDiscoverFunc ) GetProcAddress ( m_Module , " upnpDiscover " ) ;
# else
2015-01-03 23:38:48 +03:00
upnp_upnpDiscoverFunc upnpDiscoverFunc = ( upnp_upnpDiscoverFunc ) dlsym ( m_Module , " upnpDiscover " ) ;
// reinterpret_cast<upnp_upnpDiscoverFunc> (dlsym(...));
if ( ( error = dlerror ( ) ) )
{
LogPrint ( " Error loading UPNP library. This often happens if there is version mismatch! " ) ;
return ;
}
2015-01-04 02:06:43 +03:00
# endif // _WIN32
2015-01-03 23:38:48 +03:00
# 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
2015-01-04 02:06:43 +03:00
2015-01-03 23:38:48 +03:00
int r ;
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
upnp_UPNP_GetValidIGDFunc UPNP_GetValidIGDFunc = ( upnp_UPNP_GetValidIGDFunc ) GetProcAddress ( m_Module , " UPNP_GetValidIGD " ) ;
# else
2015-01-03 23:38:48 +03:00
upnp_UPNP_GetValidIGDFunc UPNP_GetValidIGDFunc = ( upnp_UPNP_GetValidIGDFunc ) dlsym ( m_Module , " UPNP_GetValidIGD " ) ;
2015-01-04 02:06:43 +03:00
# endif
2015-01-03 23:38:48 +03:00
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 ;
}
}
}
}
2015-05-06 19:19:20 +03:00
void UPnP : : TryPortMapping ( int type , int port )
2015-01-03 23:38:48 +03:00
{
2015-05-06 19:19:20 +03:00
std : : string strType , strPort ( std : : to_string ( port ) ) ;
2015-01-03 23:38:48 +03: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 ( ; ; ) {
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
upnp_UPNP_AddPortMappingFunc UPNP_AddPortMappingFunc = ( upnp_UPNP_AddPortMappingFunc ) GetProcAddress ( m_Module , " UPNP_AddPortMapping " ) ;
# else
upnp_UPNP_AddPortMappingFunc UPNP_AddPortMappingFunc = ( upnp_UPNP_AddPortMappingFunc ) dlsym ( m_Module , " UPNP_AddPortMapping " ) ;
# endif
2015-01-03 23:38:48 +03:00
# ifndef UPNPDISCOVER_SUCCESS
/* miniupnpc 1.5 */
2015-05-06 19:19:20 +03:00
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 ) ;
2015-01-03 23:38:48 +03:00
# else
/* miniupnpc 1.6 */
2015-05-06 19:19:20 +03:00
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 " ) ;
2015-01-03 23:38:48 +03:00
# endif
if ( r ! = UPNPCOMMAND_SUCCESS )
{
2015-05-06 19:19:20 +03:00
LogPrint ( " AddPortMapping ( " , strPort . c_str ( ) , " , " , strPort . c_str ( ) , " , " , m_NetworkAddr , " ) failed with code " , r ) ;
2015-01-03 23:38:48 +03:00
return ;
}
else
{
2015-05-06 19:19:20 +03:00
LogPrint ( " UPnP Port Mapping successful. ( " , m_NetworkAddr , " : " , strPort . c_str ( ) , " type " , strType . c_str ( ) , " -> " , m_externalIPAddress , " : " , strPort . c_str ( ) , " ) " ) ;
2015-01-03 23:38:48 +03:00
return ;
}
sleep ( 20 * 60 ) ;
}
}
catch ( boost : : thread_interrupted )
{
2015-05-06 19:19:20 +03:00
CloseMapping ( type , port ) ;
2015-01-03 23:38:48 +03:00
Close ( ) ;
throw ;
}
}
2015-05-06 19:19:20 +03:00
void UPnP : : CloseMapping ( int type , int port )
2015-01-03 23:38:48 +03:00
{
2015-05-06 19:19:20 +03:00
std : : string strType , strPort ( std : : to_string ( port ) ) ;
2015-01-03 23:38:48 +03:00
switch ( type )
{
case I2P_UPNP_TCP :
strType = " TCP " ;
break ;
case I2P_UPNP_UDP :
default :
strType = " UDP " ;
}
int r = 0 ;
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
upnp_UPNP_DeletePortMappingFunc UPNP_DeletePortMappingFunc = ( upnp_UPNP_DeletePortMappingFunc ) GetProcAddress ( m_Module , " UPNP_DeletePortMapping " ) ;
# else
2015-01-03 23:38:48 +03:00
upnp_UPNP_DeletePortMappingFunc UPNP_DeletePortMappingFunc = ( upnp_UPNP_DeletePortMappingFunc ) dlsym ( m_Module , " UPNP_DeletePortMapping " ) ;
2015-01-04 02:06:43 +03:00
# endif
2015-05-06 19:19:20 +03:00
r = UPNP_DeletePortMappingFunc ( m_upnpUrls . controlURL , m_upnpData . first . servicetype , strPort . c_str ( ) , strType . c_str ( ) , 0 ) ;
2015-01-03 23:38:48 +03:00
LogPrint ( " UPNP_DeletePortMapping() returned : " , r , " \n " ) ;
}
void UPnP : : Close ( )
{
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
upnp_freeUPNPDevlistFunc freeUPNPDevlistFunc = ( upnp_freeUPNPDevlistFunc ) GetProcAddress ( m_Module , " freeUPNPDevlist " ) ;
# else
2015-01-03 23:38:48 +03:00
upnp_freeUPNPDevlistFunc freeUPNPDevlistFunc = ( upnp_freeUPNPDevlistFunc ) dlsym ( m_Module , " freeUPNPDevlist " ) ;
2015-01-04 02:06:43 +03:00
# endif
2015-01-03 23:38:48 +03:00
freeUPNPDevlistFunc ( m_Devlist ) ;
m_Devlist = 0 ;
2015-01-04 02:06:43 +03:00
# ifdef _WIN32
upnp_FreeUPNPUrlsFunc FreeUPNPUrlsFunc = ( upnp_FreeUPNPUrlsFunc ) GetProcAddress ( m_Module , " FreeUPNPUrlsFunc " ) ;
# else
2015-01-03 23:38:48 +03:00
upnp_FreeUPNPUrlsFunc FreeUPNPUrlsFunc = ( upnp_FreeUPNPUrlsFunc ) dlsym ( m_Module , " FreeUPNPUrlsFunc " ) ;
2015-01-04 02:06:43 +03:00
# endif
2015-01-03 23:38:48 +03:00
FreeUPNPUrlsFunc ( & m_upnpUrls ) ;
2015-01-04 02:06:43 +03:00
# ifndef _WIN32
2015-01-03 23:38:48 +03:00
dlclose ( m_Module ) ;
2015-01-04 02:06:43 +03:00
# else
FreeLibrary ( m_Module ) ;
# endif
2015-01-03 23:38:48 +03:00
}
2014-02-10 00:15:47 +04:00
}
2015-01-03 23:38:48 +03:00
}
# endif