mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-10 00:00:29 +03:00
link UPnP with app if USE_UPNP is set
This commit is contained in:
parent
0f68bbac8e
commit
fedbf2cc44
@ -44,7 +44,7 @@ endif
|
|||||||
|
|
||||||
# UPNP Support (miniupnpc 1.5 or 1.6)
|
# UPNP Support (miniupnpc 1.5 or 1.6)
|
||||||
ifeq ($(USE_UPNP),1)
|
ifeq ($(USE_UPNP),1)
|
||||||
LDFLAGS += -ldl
|
LDFLAGS += -lminiupnpc
|
||||||
CXXFLAGS += -DUSE_UPNP
|
CXXFLAGS += -DUSE_UPNP
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
78
UPnP.cpp
78
UPnP.cpp
@ -6,13 +6,6 @@
|
|||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#define dlsym GetProcAddress
|
|
||||||
#else
|
|
||||||
#include <dlfcn.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
|
||||||
#include "RouterContext.h"
|
#include "RouterContext.h"
|
||||||
@ -24,32 +17,11 @@
|
|||||||
#include <miniupnpc/miniupnpc.h>
|
#include <miniupnpc/miniupnpc.h>
|
||||||
#include <miniupnpc/upnpcommands.h>
|
#include <miniupnpc/upnpcommands.h>
|
||||||
|
|
||||||
// These are per-process and are safe to reuse for all threads
|
|
||||||
decltype(upnpDiscover) *upnpDiscoverFunc;
|
|
||||||
decltype(UPNP_AddPortMapping) *UPNP_AddPortMappingFunc;
|
|
||||||
decltype(UPNP_GetValidIGD) *UPNP_GetValidIGDFunc;
|
|
||||||
decltype(UPNP_GetExternalIPAddress) *UPNP_GetExternalIPAddressFunc;
|
|
||||||
decltype(UPNP_DeletePortMapping) *UPNP_DeletePortMappingFunc;
|
|
||||||
decltype(freeUPNPDevlist) *freeUPNPDevlistFunc;
|
|
||||||
decltype(FreeUPNPUrls) *FreeUPNPUrlsFunc;
|
|
||||||
|
|
||||||
// Nice approach http://stackoverflow.com/a/21517513/673826
|
|
||||||
template<class M, typename F>
|
|
||||||
F GetKnownProcAddressImpl(M hmod, const char *name, F) {
|
|
||||||
auto proc = reinterpret_cast<F>(dlsym(hmod, name));
|
|
||||||
if (!proc) {
|
|
||||||
LogPrint(eLogError, "UPnP: Error resolving ", name, " from library, version mismatch?");
|
|
||||||
}
|
|
||||||
return proc;
|
|
||||||
}
|
|
||||||
#define GetKnownProcAddress(hmod, func) GetKnownProcAddressImpl(hmod, #func, func##Func);
|
|
||||||
|
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
namespace transport
|
namespace transport
|
||||||
{
|
{
|
||||||
UPnP::UPnP () : m_Thread (nullptr) , m_IsModuleLoaded (false)
|
UPnP::UPnP () : m_Thread (nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,33 +37,6 @@ namespace transport
|
|||||||
|
|
||||||
void UPnP::Start()
|
void UPnP::Start()
|
||||||
{
|
{
|
||||||
if (!m_IsModuleLoaded) {
|
|
||||||
#ifdef MAC_OSX
|
|
||||||
m_Module = dlopen ("libminiupnpc.dylib", RTLD_LAZY);
|
|
||||||
#elif _WIN32
|
|
||||||
m_Module = LoadLibrary ("libminiupnpc.dll"); // from MSYS2
|
|
||||||
#else
|
|
||||||
m_Module = dlopen ("libminiupnpc.so", RTLD_LAZY);
|
|
||||||
#endif
|
|
||||||
if (m_Module == NULL)
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "UPnP: Error loading UPNP library, version mismatch?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
upnpDiscoverFunc = GetKnownProcAddress (m_Module, upnpDiscover);
|
|
||||||
UPNP_GetValidIGDFunc = GetKnownProcAddress (m_Module, UPNP_GetValidIGD);
|
|
||||||
UPNP_GetExternalIPAddressFunc = GetKnownProcAddress (m_Module, UPNP_GetExternalIPAddress);
|
|
||||||
UPNP_AddPortMappingFunc = GetKnownProcAddress (m_Module, UPNP_AddPortMapping);
|
|
||||||
UPNP_DeletePortMappingFunc = GetKnownProcAddress (m_Module, UPNP_DeletePortMapping);
|
|
||||||
freeUPNPDevlistFunc = GetKnownProcAddress (m_Module, freeUPNPDevlist);
|
|
||||||
FreeUPNPUrlsFunc = GetKnownProcAddress (m_Module, FreeUPNPUrls);
|
|
||||||
if (upnpDiscoverFunc && UPNP_GetValidIGDFunc && UPNP_GetExternalIPAddressFunc && UPNP_AddPortMappingFunc &&
|
|
||||||
UPNP_DeletePortMappingFunc && freeUPNPDevlistFunc && FreeUPNPUrlsFunc)
|
|
||||||
m_IsModuleLoaded = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_Thread = new std::thread (std::bind (&UPnP::Run, this));
|
m_Thread = new std::thread (std::bind (&UPnP::Run, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,16 +68,16 @@ namespace transport
|
|||||||
{
|
{
|
||||||
int nerror = 0;
|
int nerror = 0;
|
||||||
#if MINIUPNPC_API_VERSION >= 14
|
#if MINIUPNPC_API_VERSION >= 14
|
||||||
m_Devlist = upnpDiscoverFunc (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, 2, &nerror);
|
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, 2, &nerror);
|
||||||
#else
|
#else
|
||||||
m_Devlist = upnpDiscoverFunc (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, &nerror);
|
m_Devlist = upnpDiscover (2000, m_MulticastIf, m_Minissdpdpath, 0, 0, &nerror);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
r = UPNP_GetValidIGDFunc (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
|
r = UPNP_GetValidIGD (m_Devlist, &m_upnpUrls, &m_upnpData, m_NetworkAddr, sizeof (m_NetworkAddr));
|
||||||
if (r == 1)
|
if (r == 1)
|
||||||
{
|
{
|
||||||
r = UPNP_GetExternalIPAddressFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_externalIPAddress);
|
r = UPNP_GetExternalIPAddress (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_externalIPAddress);
|
||||||
if(r != UPNPCOMMAND_SUCCESS)
|
if(r != UPNPCOMMAND_SUCCESS)
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "UPnP: UPNP_GetExternalIPAddress () returned ", r);
|
LogPrint (eLogError, "UPnP: UPNP_GetExternalIPAddress () returned ", r);
|
||||||
@ -171,7 +116,7 @@ namespace transport
|
|||||||
std::string strDesc = "I2Pd";
|
std::string strDesc = "I2Pd";
|
||||||
try {
|
try {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
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");
|
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");
|
||||||
if (r!=UPNPCOMMAND_SUCCESS)
|
if (r!=UPNPCOMMAND_SUCCESS)
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "UPnP: AddPortMapping (", strPort.c_str () ,", ", strPort.c_str () ,", ", m_NetworkAddr, ") failed with code ", r);
|
LogPrint (eLogError, "UPnP: AddPortMapping (", strPort.c_str () ,", ", strPort.c_str () ,", ", m_NetworkAddr, ") failed with code ", r);
|
||||||
@ -208,20 +153,15 @@ namespace transport
|
|||||||
strType = "UDP";
|
strType = "UDP";
|
||||||
}
|
}
|
||||||
int r = 0;
|
int r = 0;
|
||||||
r = UPNP_DeletePortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), 0);
|
r = UPNP_DeletePortMapping (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), 0);
|
||||||
LogPrint (eLogError, "UPnP: DeletePortMapping() returned : ", r, "\n");
|
LogPrint (eLogError, "UPnP: DeletePortMapping() returned : ", r, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPnP::Close ()
|
void UPnP::Close ()
|
||||||
{
|
{
|
||||||
freeUPNPDevlistFunc (m_Devlist);
|
freeUPNPDevlist (m_Devlist);
|
||||||
m_Devlist = 0;
|
m_Devlist = 0;
|
||||||
FreeUPNPUrlsFunc (&m_upnpUrls);
|
FreeUPNPUrls (&m_upnpUrls);
|
||||||
#ifndef _WIN32
|
|
||||||
dlclose (m_Module);
|
|
||||||
#else
|
|
||||||
FreeLibrary (m_Module);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
6
UPnP.h
6
UPnP.h
@ -48,12 +48,6 @@ namespace transport
|
|||||||
struct UPNPDev * m_Devlist = 0;
|
struct UPNPDev * m_Devlist = 0;
|
||||||
char m_NetworkAddr[64];
|
char m_NetworkAddr[64];
|
||||||
char m_externalIPAddress[40];
|
char m_externalIPAddress[40];
|
||||||
bool m_IsModuleLoaded;
|
|
||||||
#ifndef _WIN32
|
|
||||||
void *m_Module;
|
|
||||||
#else
|
|
||||||
HINSTANCE m_Module;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ QMAKE_CXXFLAGS *= -std=c++11 -DUSE_UPNP
|
|||||||
|
|
||||||
# git clone https://github.com/PurpleI2P/Boost-for-Android-Prebuilt.git
|
# git clone https://github.com/PurpleI2P/Boost-for-Android-Prebuilt.git
|
||||||
# git clone https://github.com/PurpleI2P/OpenSSL-for-Android-Prebuilt.git
|
# git clone https://github.com/PurpleI2P/OpenSSL-for-Android-Prebuilt.git
|
||||||
# git clone https://github.com/PurpleI2P/MiniUPnP-for-Android-Prebuilt
|
# git clone https://github.com/PurpleI2P/MiniUPnP-for-Android-Prebuilt.git
|
||||||
# git clone https://github.com/PurpleI2P/android-ifaddrs.git
|
# git clone https://github.com/PurpleI2P/android-ifaddrs.git
|
||||||
# change to your own
|
# change to your own
|
||||||
BOOST_PATH = /mnt/media/android/Boost-for-Android-Prebuilt
|
BOOST_PATH = /mnt/media/android/Boost-for-Android-Prebuilt
|
||||||
@ -159,7 +159,8 @@ LIBS += -L$$BOOST_PATH/boost_1_53_0/armeabi-v7a/lib \
|
|||||||
-lboost_date_time-gcc-mt-1_53 \
|
-lboost_date_time-gcc-mt-1_53 \
|
||||||
-lboost_filesystem-gcc-mt-1_53 \
|
-lboost_filesystem-gcc-mt-1_53 \
|
||||||
-lboost_program_options-gcc-mt-1_53 \
|
-lboost_program_options-gcc-mt-1_53 \
|
||||||
-L$$OPENSSL_PATH/openssl-1.0.2/armeabi-v7a/lib/ -lcrypto -lssl
|
-L$$OPENSSL_PATH/openssl-1.0.2/armeabi-v7a/lib/ -lcrypto -lssl \
|
||||||
|
-L$$MINIUPNP_PATH/miniupnp-2.0/armeabi-v7a/lib/ -lminiupnpc
|
||||||
|
|
||||||
PRE_TARGETDEPS += $$OPENSSL_PATH/openssl-1.0.2/armeabi-v7a/lib/libcrypto.a \
|
PRE_TARGETDEPS += $$OPENSSL_PATH/openssl-1.0.2/armeabi-v7a/lib/libcrypto.a \
|
||||||
$$OPENSSL_PATH/openssl-1.0.2/armeabi-v7a/lib/libssl.a
|
$$OPENSSL_PATH/openssl-1.0.2/armeabi-v7a/lib/libssl.a
|
||||||
@ -177,7 +178,8 @@ LIBS += -L$$BOOST_PATH/boost_1_53_0/x86/lib \
|
|||||||
-lboost_date_time-gcc-mt-1_53 \
|
-lboost_date_time-gcc-mt-1_53 \
|
||||||
-lboost_filesystem-gcc-mt-1_53 \
|
-lboost_filesystem-gcc-mt-1_53 \
|
||||||
-lboost_program_options-gcc-mt-1_53 \
|
-lboost_program_options-gcc-mt-1_53 \
|
||||||
-L$$OPENSSL_PATH/openssl-1.0.2/x86/lib/ -lcrypto -lssl
|
-L$$OPENSSL_PATH/openssl-1.0.2/x86/lib/ -lcrypto -lssl \
|
||||||
|
-L$$MINIUPNP_PATH/miniupnp-2.0/x86/lib/ -lminiupnpc
|
||||||
|
|
||||||
PRE_TARGETDEPS += $$OPENSSL_PATH/openssl-1.0.2/x86/lib/libcrypto.a \
|
PRE_TARGETDEPS += $$OPENSSL_PATH/openssl-1.0.2/x86/lib/libcrypto.a \
|
||||||
$$OPENSSL_PATH/openssl-1.0.2/x86/lib/libssl.a
|
$$OPENSSL_PATH/openssl-1.0.2/x86/lib/libssl.a
|
||||||
|
Loading…
Reference in New Issue
Block a user