Skip to content

Commit

Permalink
moved UPnP instance to Transports. Use actual port from RouterContext
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed May 6, 2015
1 parent 7c13194 commit 2442d0e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
13 changes: 1 addition & 12 deletions Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
#include "HTTPServer.h"
#include "ClientContext.h"

#ifdef USE_UPNP
#include "UPnP.h"
#endif


namespace i2p
{
namespace util
Expand Down Expand Up @@ -122,10 +117,6 @@ namespace i2p
LogPrint("Tunnels started");
i2p::client::context.Start ();
LogPrint("Client started");
#ifdef USE_UPNP
i2p::UPnP::upnpc.Start();
LogPrint("UPnP module loaded");
#endif
return true;
}

Expand All @@ -142,9 +133,7 @@ namespace i2p
LogPrint("NetDB stopped");
d.httpServer->Stop();
LogPrint("HTTP Server stopped");
#ifdef USE_UPNP
i2p::UPnP::upnpc.Stop();
#endif

StopLog ();

delete d.httpServer; d.httpServer = nullptr;
Expand Down
8 changes: 8 additions & 0 deletions Transports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ namespace transport

void Transports::Start ()
{
#ifdef USE_UPNP
m_UPnP.Start ();
LogPrint(eLogInfo, "UPnP started");
#endif
m_DHKeysPairSupplier.Start ();
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&Transports::Run, this));
Expand Down Expand Up @@ -141,6 +145,10 @@ namespace transport

void Transports::Stop ()
{
#ifdef USE_UPNP
m_UPnP.Stop ();
LogPrint(eLogInfo, "UPnP stopped");
#endif
m_PeerCleanupTimer.cancel ();
m_Peers.clear ();
if (m_SSUServer)
Expand Down
8 changes: 8 additions & 0 deletions Transports.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "I2NPProtocol.h"
#include "Identity.h"

#ifdef USE_UPNP
#include "UPnP.h"
#endif

namespace i2p
{
namespace transport
Expand Down Expand Up @@ -137,6 +141,10 @@ namespace transport
uint64_t m_LastInBandwidthUpdateBytes, m_LastOutBandwidthUpdateBytes;
uint64_t m_LastBandwidthUpdateTime;

#ifdef USE_UPNP
UPnP m_UPnP;
#endif

public:

// for HTTP only
Expand Down
29 changes: 13 additions & 16 deletions UPnP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ typedef void (*upnp_FreeUPNPUrlsFunc) (struct UPNPUrls *);

namespace i2p
{
namespace UPnP
namespace transport
{
UPnP upnpc;

UPnP::UPnP () : m_Thread (nullptr) , m_IsModuleLoaded (false)
{
}
Expand Down Expand Up @@ -99,15 +97,14 @@ namespace UPnP
{
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);
TryPortMapping (I2P_UPNP_UDP, address.port);
}
else if (address.transportStyle == data::RouterInfo::eTransportNTCP )
{
TryPortMapping (I2P_UPNP_TCP);
TryPortMapping (I2P_UPNP_TCP, address.port);
}
}
}
Expand Down Expand Up @@ -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)
{
case I2P_UPNP_TCP:
Expand All @@ -192,35 +189,35 @@ namespace UPnP
#endif
#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);
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
/* 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
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;
}
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;
}
sleep(20*60);
}
}
catch (boost::thread_interrupted)
{
CloseMapping(type);
CloseMapping(type, port);
Close();
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)
{
case I2P_UPNP_TCP:
Expand All @@ -236,7 +233,7 @@ namespace UPnP
#else
upnp_UPNP_DeletePortMappingFunc UPNP_DeletePortMappingFunc = (upnp_UPNP_DeletePortMappingFunc) dlsym (m_Module, "UPNP_DeletePortMapping");
#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");
}

Expand Down
8 changes: 3 additions & 5 deletions UPnP.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace i2p
{
namespace UPnP
namespace transport
{
class UPnP
{
Expand All @@ -33,8 +33,8 @@ namespace UPnP
void Stop ();

void Discover ();
void TryPortMapping (int type);
void CloseMapping (int type);
void TryPortMapping (int type, int port);
void CloseMapping (int type, int port);
private:
void Run ();

Expand All @@ -49,14 +49,12 @@ namespace UPnP
char m_NetworkAddr[64];
char m_externalIPAddress[40];
bool m_IsModuleLoaded;
std::string m_Port = std::to_string (util::config::GetArg ("-port", 17070));
#ifndef _WIN32
void *m_Module;
#else
HINSTANCE *m_Module;
#endif
};
extern UPnP upnpc;
}
}

Expand Down

0 comments on commit 2442d0e

Please sign in to comment.