From e1716bc05ea6d025785f83a3aa0f40f336ea5a76 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 3 Jan 2014 22:56:28 -0500 Subject: [PATCH] manage transit tunnels --- Tunnel.cpp | 24 ++++++++++++++++++++---- Tunnel.h | 12 ++++++------ TunnelBase.h | 10 ++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Tunnel.cpp b/Tunnel.cpp index eebb85fa..0a75649a 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -13,8 +13,7 @@ namespace i2p namespace tunnel { - Tunnel::Tunnel (TunnelConfig * config): m_Config (config), - m_CreationTime (i2p::util::GetSecondsSinceEpoch ()), m_IsEstablished (false) + Tunnel::Tunnel (TunnelConfig * config): m_Config (config), m_IsEstablished (false) { } @@ -320,6 +319,7 @@ namespace tunnel ManageInboundTunnels (); ManageOutboundTunnels (); + ManageTransitTunnels (); /* if (!m_IsTunnelCreated) { @@ -407,13 +407,29 @@ namespace tunnel { OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); LogPrint ("Creating two hops inbound tunnel..."); + auto router = outboundTunnel->GetTunnelConfig ()->GetFirstHop ()->router; CreateTunnel ( - new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (), - outboundTunnel->GetTunnelConfig ()->GetFirstHop ()->router), + new TunnelConfig (i2p::data::netdb.GetRandomNTCPRouter (), + router != &i2p::context.GetRouterInfo () ? router : i2p::data::netdb.GetRandomNTCPRouter ()), outboundTunnel); } } } + + void Tunnels::ManageTransitTunnels () + { + uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + for (auto it = m_TransitTunnels.begin (); it != m_TransitTunnels.end ();) + { + if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) + { + LogPrint ("Transit tunnel ", it->second->GetTunnelID (), " expired"); + it = m_TransitTunnels.erase (it); + } + else + it++; + } + } void Tunnels::PostTunnelData (I2NPMessage * msg) { diff --git a/Tunnel.h b/Tunnel.h index 9ecf6ff3..cec82958 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -33,9 +33,7 @@ namespace tunnel void Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel = 0); - virtual uint32_t GetTunnelID () const = 0; // as known at our side TunnelConfig * GetTunnelConfig () const { return m_Config; } - uint32_t GetCreationTime () const { return m_CreationTime; }; bool IsEstablished () const { return m_IsEstablished; }; bool HandleTunnelBuildResponse (uint8_t * msg, size_t len); @@ -54,7 +52,6 @@ namespace tunnel private: TunnelConfig * m_Config; - uint32_t m_CreationTime; // seconds since epoch bool m_IsEstablished; CryptoPP::ECB_Mode::Decryption m_ECBDecryption; @@ -70,9 +67,11 @@ namespace tunnel void SendTunnelDataMsg (i2p::I2NPMessage * msg); //local void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); - uint32_t GetTunnelID () const { return GetNextTunnelID (); }; TunnelGateway& GetTunnelGateway () { return m_Gateway; }; size_t GetNumSentBytes () const { return m_Gateway.GetNumSentBytes (); }; + + // implements TunnelBase + uint32_t GetTunnelID () const { return GetNextTunnelID (); }; private: @@ -85,10 +84,10 @@ namespace tunnel InboundTunnel (TunnelConfig * config): Tunnel (config) {}; void HandleTunnelDataMsg (I2NPMessage * msg); + size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); }; + // implements TunnelBase uint32_t GetTunnelID () const { return GetTunnelConfig ()->GetLastHop ()->nextTunnelID; }; - size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); }; - private: TunnelEndpoint m_Endpoint; @@ -128,6 +127,7 @@ namespace tunnel void ManageTunnels (); void ManageOutboundTunnels (); void ManageInboundTunnels (); + void ManageTransitTunnels (); void CreateZeroHopsInboundTunnel (); diff --git a/TunnelBase.h b/TunnelBase.h index 829270ce..bd760989 100644 --- a/TunnelBase.h +++ b/TunnelBase.h @@ -2,6 +2,7 @@ #define TUNNEL_BASE_H__ #include +#include "Timestamp.h" #include "I2NPProtocol.h" namespace i2p @@ -30,11 +31,20 @@ namespace tunnel { public: + TunnelBase (): m_CreationTime (i2p::util::GetSecondsSinceEpoch ()) {}; virtual ~TunnelBase () {}; virtual void EncryptTunnelMsg (I2NPMessage * tunnelMsg) = 0; virtual uint32_t GetNextTunnelID () const = 0; virtual const i2p::data::IdentHash& GetNextIdentHash () const = 0; + virtual uint32_t GetTunnelID () const = 0; // as known at our side + + uint32_t GetCreationTime () const { return m_CreationTime; }; + void SetCreationTime (uint32_t t) { m_CreationTime = t; }; + + private: + + uint32_t m_CreationTime; // seconds since epoch }; } }