1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-02-07 07:44:13 +00:00

moved UPnP instance to Transports. Use actual port from RouterContext

This commit is contained in:
orignal 2015-05-06 12:19:20 -04:00
parent 7c13194d5a
commit 2442d0e910
5 changed files with 33 additions and 33 deletions

View File

@ -18,11 +18,6 @@
#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
@ -122,10 +117,6 @@ 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;
} }
@ -142,9 +133,7 @@ namespace i2p
LogPrint("NetDB stopped"); LogPrint("NetDB stopped");
d.httpServer->Stop(); d.httpServer->Stop();
LogPrint("HTTP Server stopped"); LogPrint("HTTP Server stopped");
#ifdef USE_UPNP
i2p::UPnP::upnpc.Stop();
#endif
StopLog (); StopLog ();
delete d.httpServer; d.httpServer = nullptr; delete d.httpServer; d.httpServer = nullptr;

View File

@ -109,6 +109,10 @@ namespace transport
void Transports::Start () void Transports::Start ()
{ {
#ifdef USE_UPNP
m_UPnP.Start ();
LogPrint(eLogInfo, "UPnP started");
#endif
m_DHKeysPairSupplier.Start (); m_DHKeysPairSupplier.Start ();
m_IsRunning = true; m_IsRunning = true;
m_Thread = new std::thread (std::bind (&Transports::Run, this)); m_Thread = new std::thread (std::bind (&Transports::Run, this));
@ -141,6 +145,10 @@ namespace transport
void Transports::Stop () void Transports::Stop ()
{ {
#ifdef USE_UPNP
m_UPnP.Stop ();
LogPrint(eLogInfo, "UPnP stopped");
#endif
m_PeerCleanupTimer.cancel (); m_PeerCleanupTimer.cancel ();
m_Peers.clear (); m_Peers.clear ();
if (m_SSUServer) if (m_SSUServer)

View File

@ -20,6 +20,10 @@
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "Identity.h" #include "Identity.h"
#ifdef USE_UPNP
#include "UPnP.h"
#endif
namespace i2p namespace i2p
{ {
namespace transport namespace transport
@ -137,6 +141,10 @@ namespace transport
uint64_t m_LastInBandwidthUpdateBytes, m_LastOutBandwidthUpdateBytes; uint64_t m_LastInBandwidthUpdateBytes, m_LastOutBandwidthUpdateBytes;
uint64_t m_LastBandwidthUpdateTime; uint64_t m_LastBandwidthUpdateTime;
#ifdef USE_UPNP
UPnP m_UPnP;
#endif
public: public:
// for HTTP only // for HTTP only

View File

@ -39,10 +39,8 @@ typedef void (*upnp_FreeUPNPUrlsFunc) (struct UPNPUrls *);
namespace i2p namespace i2p
{ {
namespace UPnP namespace transport
{ {
UPnP upnpc;
UPnP::UPnP () : m_Thread (nullptr) , m_IsModuleLoaded (false) UPnP::UPnP () : m_Thread (nullptr) , m_IsModuleLoaded (false)
{ {
} }
@ -99,15 +97,14 @@ namespace UPnP
{ {
if (!address.host.is_v6 ()) if (!address.host.is_v6 ())
{ {
m_Port = std::to_string (util::config::GetArg ("-port", address.port));
Discover (); Discover ();
if (address.transportStyle == data::RouterInfo::eTransportSSU ) if (address.transportStyle == data::RouterInfo::eTransportSSU )
{ {
TryPortMapping (I2P_UPNP_UDP); TryPortMapping (I2P_UPNP_UDP, address.port);
} }
else if (address.transportStyle == data::RouterInfo::eTransportNTCP ) else if (address.transportStyle == data::RouterInfo::eTransportNTCP )
{ {
TryPortMapping (I2P_UPNP_TCP); TryPortMapping (I2P_UPNP_TCP, address.port);
} }
} }
} }
@ -169,9 +166,9 @@ namespace UPnP
} }
} }
void UPnP::TryPortMapping (int type) void UPnP::TryPortMapping (int type, int port)
{ {
std::string strType; std::string strType, strPort (std::to_string (port));
switch (type) switch (type)
{ {
case I2P_UPNP_TCP: case I2P_UPNP_TCP:
@ -192,19 +189,19 @@ namespace UPnP
#endif #endif
#ifndef UPNPDISCOVER_SUCCESS #ifndef UPNPDISCOVER_SUCCESS
/* miniupnpc 1.5 */ /* 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); 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);
#else #else
/* miniupnpc 1.6 */ /* 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"); 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");
#endif #endif
if (r!=UPNPCOMMAND_SUCCESS) if (r!=UPNPCOMMAND_SUCCESS)
{ {
LogPrint ("AddPortMapping (", m_Port.c_str () ,", ", m_Port.c_str () ,", ", m_NetworkAddr, ") failed with code ", r); LogPrint ("AddPortMapping (", strPort.c_str () ,", ", strPort.c_str () ,", ", m_NetworkAddr, ") failed with code ", r);
return; return;
} }
else else
{ {
LogPrint ("UPnP Port Mapping successful. (", m_NetworkAddr ,":", m_Port.c_str(), " type ", strType.c_str () ," -> ", m_externalIPAddress ,":", m_Port.c_str() ,")"); LogPrint ("UPnP Port Mapping successful. (", m_NetworkAddr ,":", strPort.c_str(), " type ", strType.c_str () ," -> ", m_externalIPAddress ,":", strPort.c_str() ,")");
return; return;
} }
sleep(20*60); sleep(20*60);
@ -212,15 +209,15 @@ namespace UPnP
} }
catch (boost::thread_interrupted) catch (boost::thread_interrupted)
{ {
CloseMapping(type); CloseMapping(type, port);
Close(); Close();
throw; throw;
} }
} }
void UPnP::CloseMapping (int type) void UPnP::CloseMapping (int type, int port)
{ {
std::string strType; std::string strType, strPort (std::to_string (port));
switch (type) switch (type)
{ {
case I2P_UPNP_TCP: case I2P_UPNP_TCP:
@ -236,7 +233,7 @@ namespace UPnP
#else #else
upnp_UPNP_DeletePortMappingFunc UPNP_DeletePortMappingFunc = (upnp_UPNP_DeletePortMappingFunc) dlsym (m_Module, "UPNP_DeletePortMapping"); upnp_UPNP_DeletePortMappingFunc UPNP_DeletePortMappingFunc = (upnp_UPNP_DeletePortMappingFunc) dlsym (m_Module, "UPNP_DeletePortMapping");
#endif #endif
r = UPNP_DeletePortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, m_Port.c_str (), strType.c_str (), 0); r = UPNP_DeletePortMappingFunc (m_upnpUrls.controlURL, m_upnpData.first.servicetype, strPort.c_str (), strType.c_str (), 0);
LogPrint ("UPNP_DeletePortMapping() returned : ", r, "\n"); LogPrint ("UPNP_DeletePortMapping() returned : ", r, "\n");
} }

8
UPnP.h
View File

@ -19,7 +19,7 @@
namespace i2p namespace i2p
{ {
namespace UPnP namespace transport
{ {
class UPnP class UPnP
{ {
@ -33,8 +33,8 @@ namespace UPnP
void Stop (); void Stop ();
void Discover (); void Discover ();
void TryPortMapping (int type); void TryPortMapping (int type, int port);
void CloseMapping (int type); void CloseMapping (int type, int port);
private: private:
void Run (); void Run ();
@ -49,14 +49,12 @@ namespace UPnP
char m_NetworkAddr[64]; char m_NetworkAddr[64];
char m_externalIPAddress[40]; char m_externalIPAddress[40];
bool m_IsModuleLoaded; bool m_IsModuleLoaded;
std::string m_Port = std::to_string (util::config::GetArg ("-port", 17070));
#ifndef _WIN32 #ifndef _WIN32
void *m_Module; void *m_Module;
#else #else
HINSTANCE *m_Module; HINSTANCE *m_Module;
#endif #endif
}; };
extern UPnP upnpc;
} }
} }