From 7caa46b3819a0cf53b9d27263eaf10406510937f Mon Sep 17 00:00:00 2001
From: orignal <romakoshelkin@yandex.ru>
Date: Fri, 14 Mar 2014 12:35:02 -0400
Subject: [PATCH] TunnelPool added

---
 Makefile       |  2 +-
 Tunnel.cpp     |  8 +++++++-
 Tunnel.h       |  6 +++++-
 TunnelPool.cpp | 26 ++++++++++++++++++++++++++
 TunnelPool.h   | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 72 insertions(+), 3 deletions(-)
 create mode 100644 TunnelPool.cpp
 create mode 100644 TunnelPool.h

diff --git a/Makefile b/Makefile
index d9d9b4c9..84a1acc0 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ OBJECTS = obj/i2p.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transpor
 	obj/RouterContext.o obj/NetDb.o obj/LeaseSet.o obj/Tunnel.o obj/TunnelEndpoint.o \
     obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \
     obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \
-    obj/UPnP.o
+    obj/UPnP.o obj/TunnelPool.o
 INCFLAGS = 
 LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
 LIBS = 
diff --git a/Tunnel.cpp b/Tunnel.cpp
index ed9708cb..df1229a5 100644
--- a/Tunnel.cpp
+++ b/Tunnel.cpp
@@ -14,7 +14,7 @@ namespace i2p
 namespace tunnel
 {		
 	
-	Tunnel::Tunnel (TunnelConfig * config): m_Config (config), m_IsEstablished (false)
+	Tunnel::Tunnel (TunnelConfig * config): m_Config (config), m_Pool (nullptr), m_IsEstablished (false)
 	{
 	}	
 
@@ -343,6 +343,9 @@ namespace tunnel
 		for (auto& it : m_PendingTunnels)
 		{	
 			LogPrint ("Pending tunnel build request ", it.first, " has not been responded. Deleted");
+			auto pool = it.second->GetTunnelPool ();
+			if (pool)
+				pool->TunnelCreationFailed (it.second);
 			delete it.second;
 		}	
 		m_PendingTunnels.clear ();
@@ -418,6 +421,9 @@ namespace tunnel
 			if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
 			{
 				LogPrint ("Tunnel ", it->second->GetTunnelID (), " expired");
+				auto pool = it->second->GetTunnelPool ();
+				if (pool)
+					pool->TunnelExpired (it->second);
 				it = m_InboundTunnels.erase (it);
 			}	
 			else 
diff --git a/Tunnel.h b/Tunnel.h
index 7e7f94d5..2200dc2e 100644
--- a/Tunnel.h
+++ b/Tunnel.h
@@ -11,6 +11,7 @@
 #include <cryptopp/aes.h>
 #include "Queue.h"
 #include "TunnelConfig.h"
+#include "TunnelPool.h"
 #include "TransitTunnel.h"
 #include "TunnelEndpoint.h"
 #include "TunnelGateway.h"
@@ -36,7 +37,9 @@ namespace tunnel
 			
 			TunnelConfig * GetTunnelConfig () const { return m_Config; }
 			bool IsEstablished () const { return m_IsEstablished; };
-						
+			TunnelPool * GetTunnelPool () const { return m_Pool; };
+			void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; };			
+			
 			bool HandleTunnelBuildResponse (uint8_t * msg, size_t len);
 			
 			// implements TunnelBase
@@ -53,6 +56,7 @@ namespace tunnel
 		private:
 
 			TunnelConfig * m_Config;
+			TunnelPool * m_Pool; // pool, tunnel belongs to, or null
 			bool m_IsEstablished;
 
 			CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption m_ECBDecryption;
diff --git a/TunnelPool.cpp b/TunnelPool.cpp
new file mode 100644
index 00000000..2acdef6c
--- /dev/null
+++ b/TunnelPool.cpp
@@ -0,0 +1,26 @@
+#include "Tunnel.h"
+#include "TunnelPool.h"
+
+namespace i2p
+{
+namespace tunnel
+{
+	TunnelPool::TunnelPool ()
+	{
+	}
+
+	TunnelPool::~TunnelPool ()
+	{
+		for (auto it: m_InboundTunnels)
+			it->SetTunnelPool (nullptr);
+	}
+
+	void TunnelPool::TunnelCreationFailed (Tunnel * failedTunnel)
+	{
+	}	
+
+	void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
+	{
+	}
+}
+}
diff --git a/TunnelPool.h b/TunnelPool.h
new file mode 100644
index 00000000..c4b7e347
--- /dev/null
+++ b/TunnelPool.h
@@ -0,0 +1,33 @@
+#ifndef TUNNEL_POOL__
+#define TUNNEL_POOL__
+
+#include <list>
+#include "LeaseSet.h"
+
+namespace i2p
+{
+namespace tunnel
+{
+	class Tunnel;
+	class InboundTunnel;
+	class OutboundTunnel;
+
+	class TunnelPool // per local destination
+	{
+		public:
+
+			TunnelPool ();
+			~TunnelPool ();
+
+			void TunnelCreationFailed (Tunnel * failedTunnel);
+			void TunnelExpired (InboundTunnel * expiredTunnel);
+
+		private:
+
+			std::list<InboundTunnel *> m_InboundTunnels;	
+	};	
+}
+}
+
+#endif
+