From 42d49bde869037c0ad8e0564bb4f4287d6236a6e Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 5 May 2015 12:32:13 -0400 Subject: [PATCH] handle tunnels quantity params --- Destination.cpp | 24 +++++++++++++++++++++++- Destination.h | 4 ++++ Tunnel.cpp | 6 +++--- Tunnel.h | 2 +- TunnelPool.cpp | 8 ++++---- TunnelPool.h | 4 ++-- 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Destination.cpp b/Destination.cpp index 4c7d382a..606bd619 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -22,6 +22,8 @@ namespace client i2p::crypto::GenerateElGamalKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); int inboundTunnelLen = DEFAULT_INBOUND_TUNNEL_LENGTH; int outboundTunnelLen = DEFAULT_OUTBOUND_TUNNEL_LENGTH; + int inboundTunnelsQuantity = DEFAULT_INBOUND_TUNNELS_QUANTITY; + int outboundTunnelsQuantity = DEFAULT_OUTBOUND_TUNNELS_QUANTITY; if (params) { auto it = params->find (I2CP_PARAM_INBOUND_TUNNEL_LENGTH); @@ -44,8 +46,28 @@ namespace client LogPrint (eLogInfo, "Outbound tunnel length set to ", len); } } + it = params->find (I2CP_PARAM_INBOUND_TUNNELS_QUANTITY); + if (it != params->end ()) + { + int quantity = boost::lexical_cast(it->second); + if (quantity > 0) + { + inboundTunnelsQuantity = quantity; + LogPrint (eLogInfo, "Inbound tunnels quantity set to ", quantity); + } + } + it = params->find (I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY); + if (it != params->end ()) + { + int quantity = boost::lexical_cast(it->second); + if (quantity > 0) + { + outboundTunnelsQuantity = quantity; + LogPrint (eLogInfo, "Outbound tunnels quantity set to ", quantity); + } + } } - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this, inboundTunnelLen, outboundTunnelLen); + m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this, inboundTunnelLen, outboundTunnelLen, inboundTunnelsQuantity, outboundTunnelsQuantity); if (m_IsPublic) LogPrint (eLogInfo, "Local address ", i2p::client::GetB32Address(GetIdentHash()), " created"); m_StreamingDestination = std::make_shared (*this); // TODO: diff --git a/Destination.h b/Destination.h index 29c3b230..bf2dfcca 100644 --- a/Destination.h +++ b/Destination.h @@ -36,6 +36,10 @@ namespace client const int DEFAULT_INBOUND_TUNNEL_LENGTH = 3; const char I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH[] = "outbound.length"; const int DEFAULT_OUTBOUND_TUNNEL_LENGTH = 3; + const char I2CP_PARAM_INBOUND_TUNNELS_QUANTITY[] = "inbound.quantity"; + const int DEFAULT_INBOUND_TUNNELS_QUANTITY = 5; + const char I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY[] = "outbound.quantity"; + const int DEFAULT_OUTBOUND_TUNNELS_QUANTITY = 5; const int STREAM_REQUEST_TIMEOUT = 60; //in seconds typedef std::function stream)> StreamRequestComplete; diff --git a/Tunnel.cpp b/Tunnel.cpp index c44921c1..65638040 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -287,9 +287,9 @@ namespace tunnel return tunnel; } - std::shared_ptr Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops) + std::shared_ptr Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels) { - auto pool = std::make_shared (localDestination, numInboundHops, numOutboundHops); + auto pool = std::make_shared (localDestination, numInboundHops, numOutboundHops, numInboundTunnels, numOutboundTunnels); std::unique_lock l(m_PoolsMutex); m_Pools.push_back (pool); return pool; @@ -613,7 +613,7 @@ namespace tunnel LogPrint ("Creating zero hops inbound tunnel..."); CreateZeroHopsInboundTunnel (); if (!m_ExploratoryPool) - m_ExploratoryPool = CreateTunnelPool (&i2p::context, 2, 2); // 2-hop exploratory + m_ExploratoryPool = CreateTunnelPool (&i2p::context, 2, 2, 5, 5); // 2-hop exploratory, 5 tunnels return; } diff --git a/Tunnel.h b/Tunnel.h index cd8a1379..adf9a30b 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -141,7 +141,7 @@ namespace tunnel std::shared_ptr CreateTunnel (TunnelConfig * config, std::shared_ptr outboundTunnel = nullptr); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr tunnel); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr tunnel); - std::shared_ptr CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops); + std::shared_ptr CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops, int numInboundTunnels, int numOutboundTunnels); void DeleteTunnelPool (std::shared_ptr pool); void StopTunnelPool (std::shared_ptr pool); diff --git a/TunnelPool.cpp b/TunnelPool.cpp index e7d75f11..8ed90403 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -11,9 +11,9 @@ namespace i2p { namespace tunnel { - TunnelPool::TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numTunnels): + TunnelPool::TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels): m_LocalDestination (localDestination), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), - m_NumTunnels (numTunnels), m_IsActive (true) + m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_IsActive (true) { } @@ -160,7 +160,7 @@ namespace tunnel for (auto it : m_InboundTunnels) if (it->IsEstablished ()) num++; } - for (int i = num; i < m_NumTunnels; i++) + for (int i = num; i < m_NumInboundTunnels; i++) CreateInboundTunnel (); num = 0; @@ -169,7 +169,7 @@ namespace tunnel for (auto it : m_OutboundTunnels) if (it->IsEstablished ()) num++; } - for (int i = num; i < m_NumTunnels; i++) + for (int i = num; i < m_NumOutboundTunnels; i++) CreateOutboundTunnel (); } diff --git a/TunnelPool.h b/TunnelPool.h index 947bd3cc..407558f7 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -27,7 +27,7 @@ namespace tunnel { public: - TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numTunnels = 5); + TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels); ~TunnelPool (); i2p::garlic::GarlicDestination * GetLocalDestination () const { return m_LocalDestination; }; @@ -64,7 +64,7 @@ namespace tunnel private: i2p::garlic::GarlicDestination * m_LocalDestination; - int m_NumInboundHops, m_NumOutboundHops, m_NumTunnels; + int m_NumInboundHops, m_NumOutboundHops, m_NumInboundTunnels, m_NumOutboundTunnels; mutable std::mutex m_InboundTunnelsMutex; std::set, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first mutable std::mutex m_OutboundTunnelsMutex;