From ec21138bd249f43dfe2a920c6014d78f49f63eec Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 25 Jun 2014 19:28:33 -0400 Subject: [PATCH] specify number of hops for a tunnel pool --- Streaming.cpp | 4 ++-- Tunnel.cpp | 6 +++--- Tunnel.h | 2 +- TunnelConfig.h | 6 ++++-- TunnelPool.cpp | 23 +++++++++++++---------- TunnelPool.h | 4 ++-- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Streaming.cpp b/Streaming.cpp index 86105ca0..7b6ef7c6 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -340,7 +340,7 @@ namespace stream CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this); + m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel } StreamingDestination::StreamingDestination (const std::string& fullPath): m_LeaseSet (nullptr) @@ -356,7 +356,7 @@ namespace stream CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this); + m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel } StreamingDestination::~StreamingDestination () diff --git a/Tunnel.cpp b/Tunnel.cpp index 4e0ec7c0..0c1fe454 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -275,9 +275,9 @@ namespace tunnel return tunnel; } - TunnelPool * Tunnels::CreateTunnelPool (i2p::data::LocalDestination& localDestination) + TunnelPool * Tunnels::CreateTunnelPool (i2p::data::LocalDestination& localDestination, int numHops) { - auto pool = new TunnelPool (localDestination); + auto pool = new TunnelPool (localDestination, numHops); m_Pools[pool->GetIdentHash ()] = pool; return pool; } @@ -441,7 +441,7 @@ namespace tunnel LogPrint ("Creating zero hops inbound tunnel..."); CreateZeroHopsInboundTunnel (); if (!m_ExploratoryPool) - m_ExploratoryPool = CreateTunnelPool (i2p::context); + m_ExploratoryPool = CreateTunnelPool (i2p::context, 2); // 2-hop exploratory return; } diff --git a/Tunnel.h b/Tunnel.h index dc345875..8ae3e392 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -114,7 +114,7 @@ namespace tunnel void PostTunnelData (I2NPMessage * msg); template TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); - TunnelPool * CreateTunnelPool (i2p::data::LocalDestination& localDestination); + TunnelPool * CreateTunnelPool (i2p::data::LocalDestination& localDestination, int numHops); void DeleteTunnelPool (TunnelPool * pool); private: diff --git a/TunnelConfig.h b/TunnelConfig.h index 7cac47a0..429fa679 100644 --- a/TunnelConfig.h +++ b/TunnelConfig.h @@ -134,9 +134,9 @@ namespace tunnel return m_LastHop; } - size_t GetNumHops () const + int GetNumHops () const { - size_t num = 0; + int num = 0; TunnelHopConfig * hop = m_FirstHop; while (hop) { @@ -183,6 +183,8 @@ namespace tunnel newConfig->m_LastHop = newHop; if (hop->isGateway) // inbound tunnel newHop->SetReplyHop (m_FirstHop); // use it as reply tunnel + else + newHop->SetNextRouter (&i2p::context.GetRouterInfo ()); } if (!hop->next) newConfig->m_FirstHop = newHop; // last hop diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 875a51f6..1b6fa8cb 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -10,8 +10,8 @@ namespace i2p { namespace tunnel { - TunnelPool::TunnelPool (i2p::data::LocalDestination& localDestination, int numTunnels): - m_LocalDestination (localDestination), m_NumTunnels (numTunnels), m_LastOutboundTunnel (nullptr) + TunnelPool::TunnelPool (i2p::data::LocalDestination& localDestination, int numHops, int numTunnels): + m_LocalDestination (localDestination), m_NumHops (numHops), m_NumTunnels (numTunnels), m_LastOutboundTunnel (nullptr) { } @@ -189,15 +189,18 @@ namespace tunnel if (inboundTunnel) { LogPrint ("Creating destination outbound tunnel..."); - auto firstHop = i2p::data::netdb.GetRandomRouter (&i2p::context.GetRouterInfo ()); - auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop); + + const i2p::data::RouterInfo * prevHop = &i2p::context.GetRouterInfo (); + std::vector hops; + for (int i = 0; i < m_NumHops; i++) + { + auto hop = i2p::data::netdb.GetRandomRouter (prevHop); + prevHop = hop; + hops.push_back (hop); + } + auto * tunnel = tunnels.CreateTunnel ( - new TunnelConfig (std::vector - { - firstHop, - secondHop - }, - inboundTunnel->GetTunnelConfig ())); + new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ())); tunnel->SetTunnelPool (this); } } diff --git a/TunnelPool.h b/TunnelPool.h index 1ff66cf3..27848bfb 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -23,7 +23,7 @@ namespace tunnel { public: - TunnelPool (i2p::data::LocalDestination& localDestination, int numTunnels = 5); + TunnelPool (i2p::data::LocalDestination& localDestination, int numHops, int numTunnels = 5); ~TunnelPool (); const uint8_t * GetEncryptionPrivateKey () const { return m_LocalDestination.GetEncryptionPrivateKey (); }; @@ -53,7 +53,7 @@ namespace tunnel private: i2p::data::LocalDestination& m_LocalDestination; - int m_NumTunnels; + int m_NumHops, m_NumTunnels; std::set m_InboundTunnels; // recent tunnel appears first std::set m_OutboundTunnels; std::map > m_Tests;