diff --git a/Destination.cpp b/Destination.cpp index 9c5bb700..67042276 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -82,7 +82,7 @@ namespace client LogPrint (eLogInfo, "Explicit peers set to ", it->second); } } - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this, inboundTunnelLen, outboundTunnelLen, inboundTunnelsQuantity, outboundTunnelsQuantity); + m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (inboundTunnelLen, outboundTunnelLen, inboundTunnelsQuantity, outboundTunnelsQuantity); if (explicitPeers) m_Pool->SetExplicitPeers (explicitPeers); if (m_IsPublic) @@ -122,7 +122,7 @@ namespace client if (!m_IsRunning) { m_IsRunning = true; - m_Pool->SetLocalDestination (this); + m_Pool->SetLocalDestination (shared_from_this ()); m_Pool->SetActive (true); m_Thread = new std::thread (std::bind (&ClientDestination::Run, this)); m_StreamingDestination = std::make_shared (shared_from_this ()); // TODO: @@ -199,7 +199,7 @@ namespace client void ClientDestination::UpdateLeaseSet () { - m_LeaseSet.reset (new i2p::data::LeaseSet (*m_Pool)); + m_LeaseSet.reset (new i2p::data::LeaseSet (m_Pool)); } bool ClientDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag) diff --git a/LeaseSet.cpp b/LeaseSet.cpp index 9a88401c..caeec262 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -21,11 +21,12 @@ namespace data ReadFromBuffer (); } - LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool): + LeaseSet::LeaseSet (std::shared_ptr pool): m_IsValid (true) { + if (!pool) return; // header - const i2p::data::LocalDestination * localDestination = pool.GetLocalDestination (); + auto localDestination = pool->GetLocalDestination (); if (!localDestination) { m_Buffer = nullptr; @@ -41,7 +42,7 @@ namespace data auto signingKeyLen = localDestination->GetIdentity ()->GetSigningPublicKeyLen (); memset (m_Buffer + m_BufferLen, 0, signingKeyLen); m_BufferLen += signingKeyLen; - auto tunnels = pool.GetInboundTunnels (5); // 5 tunnels maximum + auto tunnels = pool->GetInboundTunnels (5); // 5 tunnels maximum m_Buffer[m_BufferLen] = tunnels.size (); // num leases m_BufferLen++; // leases diff --git a/LeaseSet.h b/LeaseSet.h index eec5d89e..8135a7a1 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -37,7 +37,7 @@ namespace data public: LeaseSet (const uint8_t * buf, size_t len); - LeaseSet (const i2p::tunnel::TunnelPool& pool); + LeaseSet (std::shared_ptr pool); ~LeaseSet () { delete[] m_Buffer; }; void Update (const uint8_t * buf, size_t len); std::shared_ptr GetIdentity () const { return m_Identity; }; diff --git a/RouterContext.h b/RouterContext.h index 541b78d1..13027b44 100644 --- a/RouterContext.h +++ b/RouterContext.h @@ -39,6 +39,12 @@ namespace i2p return std::shared_ptr (&m_RouterInfo, [](const i2p::data::RouterInfo *) {}); } + std::shared_ptr GetSharedDestination () + { + return std::shared_ptr (this, + [](i2p::garlic::GarlicDestination *) {}); + } + uint32_t GetUptime () const; uint32_t GetStartupTime () const { return m_StartupTime; }; uint64_t GetLastUpdateTime () const { return m_LastUpdateTime; }; diff --git a/Tunnel.cpp b/Tunnel.cpp index 0f1608f2..8e5510f1 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -333,9 +333,10 @@ namespace tunnel return tunnel; } - std::shared_ptr Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels) + std::shared_ptr Tunnels::CreateTunnelPool (int numInboundHops, + int numOutboundHops, int numInboundTunnels, int numOutboundTunnels) { - auto pool = std::make_shared (localDestination, numInboundHops, numOutboundHops, numInboundTunnels, numOutboundTunnels); + auto pool = std::make_shared (numInboundHops, numOutboundHops, numInboundTunnels, numOutboundTunnels); std::unique_lock l(m_PoolsMutex); m_Pools.push_back (pool); return pool; @@ -649,7 +650,10 @@ namespace tunnel LogPrint ("Creating zero hops inbound tunnel..."); CreateZeroHopsInboundTunnel (); if (!m_ExploratoryPool) - m_ExploratoryPool = CreateTunnelPool (&i2p::context, 2, 2, 5, 5); // 2-hop exploratory, 5 tunnels + { + m_ExploratoryPool = CreateTunnelPool (2, 2, 5, 5); // 2-hop exploratory, 5 tunnels + m_ExploratoryPool->SetLocalDestination (i2p::context.GetSharedDestination ()); + } return; } diff --git a/Tunnel.h b/Tunnel.h index daf0f054..138a48fc 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -153,7 +153,8 @@ namespace tunnel std::shared_ptr CreateTunnel (std::shared_ptr 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, int numInboundTunnels, int numOutboundTunnels); + std::shared_ptr CreateTunnelPool (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 922ae34b..fcce1c22 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -14,8 +14,8 @@ namespace i2p { namespace tunnel { - TunnelPool::TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels): - m_LocalDestination (localDestination), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), + TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels): + m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_IsActive (true) { } @@ -298,7 +298,7 @@ namespace tunnel std::shared_ptr TunnelPool::SelectNextHop (std::shared_ptr prevHop) const { - bool isExploratory = (m_LocalDestination == &i2p::context); // TODO: implement it better + bool isExploratory = (i2p::tunnel::tunnels.GetExploratoryPool () == shared_from_this ()); auto hop = isExploratory ? i2p::data::netdb.GetRandomRouter (prevHop): i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop); diff --git a/TunnelPool.h b/TunnelPool.h index a836162f..83b163d4 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -27,11 +27,11 @@ namespace tunnel { public: - TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels); + TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels); ~TunnelPool (); - i2p::garlic::GarlicDestination * GetLocalDestination () const { return m_LocalDestination; }; - void SetLocalDestination (i2p::garlic::GarlicDestination * destination) { m_LocalDestination = destination; }; + std::shared_ptr GetLocalDestination () const { return m_LocalDestination; }; + void SetLocalDestination (std::shared_ptr destination) { m_LocalDestination = destination; }; void SetExplicitPeers (std::shared_ptr > explicitPeers); void CreateTunnels (); @@ -67,7 +67,7 @@ namespace tunnel private: - i2p::garlic::GarlicDestination * m_LocalDestination; + std::shared_ptr m_LocalDestination; int m_NumInboundHops, m_NumOutboundHops, m_NumInboundTunnels, m_NumOutboundTunnels; std::shared_ptr > m_ExplicitPeers; mutable std::mutex m_InboundTunnelsMutex;