From 2442d0e9107751caa1db071da689bd3da50d0595 Mon Sep 17 00:00:00 2001
From: orignal <i2porignal@yandex.ru>
Date: Wed, 6 May 2015 12:19:20 -0400
Subject: [PATCH] moved UPnP instance to Transports. Use actual port from
 RouterContext

---
 Daemon.cpp     | 13 +------------
 Transports.cpp |  8 ++++++++
 Transports.h   |  8 ++++++++
 UPnP.cpp       | 29 +++++++++++++----------------
 UPnP.h         |  8 +++-----
 5 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/Daemon.cpp b/Daemon.cpp
index e3ae8bbc..77cd0899 100644
--- a/Daemon.cpp
+++ b/Daemon.cpp
@@ -18,11 +18,6 @@
 #include "HTTPServer.h"
 #include "ClientContext.h"
 
-#ifdef USE_UPNP
-#include "UPnP.h"
-#endif
-
-
 namespace i2p
 {
 	namespace util
@@ -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;
 		}
 
@@ -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;
diff --git a/Transports.cpp b/Transports.cpp
index 0cb4a970..f871607e 100644
--- a/Transports.cpp
+++ b/Transports.cpp
@@ -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));
@@ -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)
diff --git a/Transports.h b/Transports.h
index 00bf5b6f..11f05190 100644
--- a/Transports.h
+++ b/Transports.h
@@ -20,6 +20,10 @@
 #include "I2NPProtocol.h"
 #include "Identity.h"
 
+#ifdef USE_UPNP
+#include "UPnP.h"
+#endif
+
 namespace i2p
 {
 namespace transport
@@ -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
diff --git a/UPnP.cpp b/UPnP.cpp
index e5b8ff79..227bbbd7 100644
--- a/UPnP.cpp
+++ b/UPnP.cpp
@@ -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)
     {
     }
@@ -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);
                 }
             }
         }
@@ -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:
@@ -192,19 +189,19 @@ 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);
@@ -212,15 +209,15 @@ namespace UPnP
         }
         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:
@@ -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");
     }
 
diff --git a/UPnP.h b/UPnP.h
index 8dc62b8d..4884c423 100644
--- a/UPnP.h
+++ b/UPnP.h
@@ -19,7 +19,7 @@
 
 namespace i2p
 {
-namespace UPnP
+namespace transport
 {
 	class UPnP
 	{
@@ -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 ();
 
@@ -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;
 }
 }