Browse Source

shared_ptr for tunnels

pull/151/head
orignal 10 years ago
parent
commit
b3e08b2cf4
  1. 2
      Destination.cpp
  2. 2
      Garlic.cpp
  3. 2
      NetDb.cpp
  4. 2
      NetDb.h
  5. 7
      Streaming.cpp
  6. 2
      Streaming.h
  7. 57
      Tunnel.cpp
  8. 32
      Tunnel.h
  9. 3
      TunnelBase.h
  10. 36
      TunnelPool.cpp
  11. 24
      TunnelPool.h

2
Destination.cpp

@ -510,7 +510,7 @@ namespace client
I2NPMessage * msg = WrapMessage (*nextFloodfill, I2NPMessage * msg = WrapMessage (*nextFloodfill,
CreateLeaseSetDatabaseLookupMsg (dest, request->excluded, CreateLeaseSetDatabaseLookupMsg (dest, request->excluded,
replyTunnel, replyKey, replyTag)); replyTunnel.get (), replyKey, replyTag));
outboundTunnel->SendTunnelDataMsg ( outboundTunnel->SendTunnelDataMsg (
{ {
i2p::tunnel::TunnelMessageBlock i2p::tunnel::TunnelMessageBlock

2
Garlic.cpp

@ -475,7 +475,7 @@ namespace garlic
buf += 32; buf += 32;
uint32_t gwTunnel = bufbe32toh (buf); uint32_t gwTunnel = bufbe32toh (buf);
buf += 4; buf += 4;
i2p::tunnel::OutboundTunnel * tunnel = nullptr; std::shared_ptr<i2p::tunnel::OutboundTunnel> tunnel;
if (from && from->GetTunnelPool ()) if (from && from->GetTunnelPool ())
tunnel = from->GetTunnelPool ()->GetNextOutboundTunnel (); tunnel = from->GetTunnelPool ()->GetNextOutboundTunnel ();
if (tunnel) // we have send it through an outbound tunnel if (tunnel) // we have send it through an outbound tunnel

2
NetDb.cpp

@ -22,7 +22,7 @@ namespace i2p
namespace data namespace data
{ {
I2NPMessage * RequestedDestination::CreateRequestMessage (std::shared_ptr<const RouterInfo> router, I2NPMessage * RequestedDestination::CreateRequestMessage (std::shared_ptr<const RouterInfo> router,
const i2p::tunnel::InboundTunnel * replyTunnel) std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel)
{ {
I2NPMessage * msg = i2p::CreateRouterInfoDatabaseLookupMsg (m_Destination, I2NPMessage * msg = i2p::CreateRouterInfoDatabaseLookupMsg (m_Destination,
replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory, replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory,

2
NetDb.h

@ -38,7 +38,7 @@ namespace data
bool IsExploratory () const { return m_IsExploratory; }; bool IsExploratory () const { return m_IsExploratory; };
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); }; bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
uint64_t GetCreationTime () const { return m_CreationTime; }; uint64_t GetCreationTime () const { return m_CreationTime; };
I2NPMessage * CreateRequestMessage (std::shared_ptr<const RouterInfo>, const i2p::tunnel::InboundTunnel * replyTunnel); I2NPMessage * CreateRequestMessage (std::shared_ptr<const RouterInfo>, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel);
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill); I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);
void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; }; void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; };

7
Streaming.cpp

@ -15,8 +15,8 @@ namespace stream
std::shared_ptr<const i2p::data::LeaseSet> remote, int port): m_Service (service), m_SendStreamID (0), std::shared_ptr<const i2p::data::LeaseSet> remote, int port): m_Service (service), m_SendStreamID (0),
m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false),
m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local),
m_RemoteLeaseSet (remote), m_CurrentOutboundTunnel (nullptr), m_RemoteLeaseSet (remote), m_ReceiveTimer (m_Service),
m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service),
m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port) m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port)
{ {
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
@ -26,8 +26,7 @@ namespace stream
Stream::Stream (boost::asio::io_service& service, StreamingDestination& local): Stream::Stream (boost::asio::io_service& service, StreamingDestination& local):
m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1),
m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local),
m_CurrentOutboundTunnel (nullptr), m_ReceiveTimer (m_Service), m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service),
m_ResendTimer (m_Service), m_AckSendTimer (m_Service),
m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0) m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0)
{ {
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();

2
Streaming.h

@ -147,7 +147,7 @@ namespace stream
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet; std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession; std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
i2p::data::Lease m_CurrentRemoteLease; i2p::data::Lease m_CurrentRemoteLease;
i2p::tunnel::OutboundTunnel * m_CurrentOutboundTunnel; std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
std::queue<Packet *> m_ReceiveQueue; std::queue<Packet *> m_ReceiveQueue;
std::set<Packet *, PacketCmp> m_SavedPackets; std::set<Packet *, PacketCmp> m_SavedPackets;
std::set<Packet *, PacketCmp> m_SentPackets; std::set<Packet *, PacketCmp> m_SentPackets;

57
Tunnel.cpp

@ -27,7 +27,7 @@ namespace tunnel
delete m_Config; delete m_Config;
} }
void Tunnel::Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel) void Tunnel::Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel)
{ {
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
auto numHops = m_Config->GetNumHops (); auto numHops = m_Config->GetNumHops ();
@ -209,29 +209,12 @@ namespace tunnel
Tunnels::~Tunnels () Tunnels::~Tunnels ()
{ {
for (auto& it : m_OutboundTunnels)
delete it;
m_OutboundTunnels.clear ();
for (auto& it : m_InboundTunnels)
delete it.second;
m_InboundTunnels.clear ();
for (auto& it : m_TransitTunnels) for (auto& it : m_TransitTunnels)
delete it.second; delete it.second;
m_TransitTunnels.clear (); m_TransitTunnels.clear ();
ManagePendingTunnels ();
for (auto& it : m_PendingInboundTunnels)
delete it.second;
m_PendingInboundTunnels.clear ();
for (auto& it : m_PendingOutboundTunnels)
delete it.second;
m_PendingOutboundTunnels.clear ();
} }
InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID) std::shared_ptr<InboundTunnel> Tunnels::GetInboundTunnel (uint32_t tunnelID)
{ {
auto it = m_InboundTunnels.find(tunnelID); auto it = m_InboundTunnels.find(tunnelID);
if (it != m_InboundTunnels.end ()) if (it != m_InboundTunnels.end ())
@ -247,18 +230,18 @@ namespace tunnel
return nullptr; return nullptr;
} }
InboundTunnel * Tunnels::GetPendingInboundTunnel (uint32_t replyMsgID) std::shared_ptr<InboundTunnel> Tunnels::GetPendingInboundTunnel (uint32_t replyMsgID)
{ {
return GetPendingTunnel (replyMsgID, m_PendingInboundTunnels); return GetPendingTunnel (replyMsgID, m_PendingInboundTunnels);
} }
OutboundTunnel * Tunnels::GetPendingOutboundTunnel (uint32_t replyMsgID) std::shared_ptr<OutboundTunnel> Tunnels::GetPendingOutboundTunnel (uint32_t replyMsgID)
{ {
return GetPendingTunnel (replyMsgID, m_PendingOutboundTunnels); return GetPendingTunnel (replyMsgID, m_PendingOutboundTunnels);
} }
template<class TTunnel> template<class TTunnel>
TTunnel * Tunnels::GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, TTunnel *>& pendingTunnels) std::shared_ptr<TTunnel> Tunnels::GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels)
{ {
auto it = pendingTunnels.find(replyMsgID); auto it = pendingTunnels.find(replyMsgID);
if (it != pendingTunnels.end () && it->second->GetState () == eTunnelStatePending) if (it != pendingTunnels.end () && it->second->GetState () == eTunnelStatePending)
@ -269,9 +252,9 @@ namespace tunnel
return nullptr; return nullptr;
} }
InboundTunnel * Tunnels::GetNextInboundTunnel () std::shared_ptr<InboundTunnel> Tunnels::GetNextInboundTunnel ()
{ {
InboundTunnel * tunnel = nullptr; std::shared_ptr<InboundTunnel> tunnel;
size_t minReceived = 0; size_t minReceived = 0;
for (auto it : m_InboundTunnels) for (auto it : m_InboundTunnels)
{ {
@ -285,11 +268,11 @@ namespace tunnel
return tunnel; return tunnel;
} }
OutboundTunnel * Tunnels::GetNextOutboundTunnel () std::shared_ptr<OutboundTunnel> Tunnels::GetNextOutboundTunnel ()
{ {
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
uint32_t ind = rnd.GenerateWord32 (0, m_OutboundTunnels.size () - 1), i = 0; uint32_t ind = rnd.GenerateWord32 (0, m_OutboundTunnels.size () - 1), i = 0;
OutboundTunnel * tunnel = nullptr; std::shared_ptr<OutboundTunnel> tunnel;
for (auto it: m_OutboundTunnels) for (auto it: m_OutboundTunnels)
{ {
if (it->IsEstablished ()) if (it->IsEstablished ())
@ -384,7 +367,7 @@ namespace tunnel
prevTunnel->FlushTunnelDataMsgs (); prevTunnel->FlushTunnelDataMsgs ();
if (!tunnel && typeID == eI2NPTunnelData) if (!tunnel && typeID == eI2NPTunnelData)
tunnel = GetInboundTunnel (tunnelID); tunnel = GetInboundTunnel (tunnelID).get ();
if (!tunnel) if (!tunnel)
tunnel = GetTransitTunnel (tunnelID); tunnel = GetTransitTunnel (tunnelID);
if (tunnel) if (tunnel)
@ -499,7 +482,6 @@ namespace tunnel
if (ts > tunnel->GetCreationTime () + TUNNEL_CREATION_TIMEOUT) if (ts > tunnel->GetCreationTime () + TUNNEL_CREATION_TIMEOUT)
{ {
LogPrint ("Pending tunnel build request ", it->first, " timeout. Deleted"); LogPrint ("Pending tunnel build request ", it->first, " timeout. Deleted");
delete tunnel;
it = pendingTunnels.erase (it); it = pendingTunnels.erase (it);
} }
else else
@ -507,11 +489,10 @@ namespace tunnel
break; break;
case eTunnelStateBuildFailed: case eTunnelStateBuildFailed:
LogPrint ("Pending tunnel build request ", it->first, " failed. Deleted"); LogPrint ("Pending tunnel build request ", it->first, " failed. Deleted");
delete tunnel;
it = pendingTunnels.erase (it); it = pendingTunnels.erase (it);
break; break;
case eTunnelStateBuildReplyReceived: case eTunnelStateBuildReplyReceived:
// intermidiate state, will be either established of build failed // intermediate state, will be either established of build failed
it++; it++;
break; break;
default: default:
@ -536,7 +517,6 @@ namespace tunnel
pool->TunnelExpired (tunnel); pool->TunnelExpired (tunnel);
} }
it = m_OutboundTunnels.erase (it); it = m_OutboundTunnels.erase (it);
delete tunnel;
} }
else else
{ {
@ -550,7 +530,7 @@ namespace tunnel
if (m_OutboundTunnels.size () < 5) if (m_OutboundTunnels.size () < 5)
{ {
// trying to create one more oubound tunnel // trying to create one more oubound tunnel
InboundTunnel * inboundTunnel = GetNextInboundTunnel (); auto inboundTunnel = GetNextInboundTunnel ();
if (!inboundTunnel) return; if (!inboundTunnel) return;
LogPrint ("Creating one hop outbound tunnel..."); LogPrint ("Creating one hop outbound tunnel...");
CreateTunnel<OutboundTunnel> ( CreateTunnel<OutboundTunnel> (
@ -578,7 +558,6 @@ namespace tunnel
pool->TunnelExpired (tunnel); pool->TunnelExpired (tunnel);
} }
it = m_InboundTunnels.erase (it); it = m_InboundTunnels.erase (it);
delete tunnel;
} }
else else
{ {
@ -655,26 +634,26 @@ namespace tunnel
} }
template<class TTunnel> template<class TTunnel>
TTunnel * Tunnels::CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel) std::shared_ptr<TTunnel> Tunnels::CreateTunnel (TunnelConfig * config, std::shared_ptr<OutboundTunnel> outboundTunnel)
{ {
TTunnel * newTunnel = new TTunnel (config); auto newTunnel = std::make_shared<TTunnel> (config);
uint32_t replyMsgID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); uint32_t replyMsgID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
AddPendingTunnel (replyMsgID, newTunnel); AddPendingTunnel (replyMsgID, newTunnel);
newTunnel->Build (replyMsgID, outboundTunnel); newTunnel->Build (replyMsgID, outboundTunnel);
return newTunnel; return newTunnel;
} }
void Tunnels::AddPendingTunnel (uint32_t replyMsgID, InboundTunnel * tunnel) void Tunnels::AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<InboundTunnel> tunnel)
{ {
m_PendingInboundTunnels[replyMsgID] = tunnel; m_PendingInboundTunnels[replyMsgID] = tunnel;
} }
void Tunnels::AddPendingTunnel (uint32_t replyMsgID, OutboundTunnel * tunnel) void Tunnels::AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> tunnel)
{ {
m_PendingOutboundTunnels[replyMsgID] = tunnel; m_PendingOutboundTunnels[replyMsgID] = tunnel;
} }
void Tunnels::AddOutboundTunnel (OutboundTunnel * newTunnel) void Tunnels::AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel)
{ {
m_OutboundTunnels.push_back (newTunnel); m_OutboundTunnels.push_back (newTunnel);
auto pool = newTunnel->GetTunnelPool (); auto pool = newTunnel->GetTunnelPool ();
@ -684,7 +663,7 @@ namespace tunnel
newTunnel->SetTunnelPool (nullptr); newTunnel->SetTunnelPool (nullptr);
} }
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel) void Tunnels::AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel)
{ {
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel; m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
auto pool = newTunnel->GetTunnelPool (); auto pool = newTunnel->GetTunnelPool ();

32
Tunnel.h

@ -47,7 +47,7 @@ namespace tunnel
Tunnel (TunnelConfig * config); Tunnel (TunnelConfig * config);
~Tunnel (); ~Tunnel ();
void Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel = 0); void Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
TunnelConfig * GetTunnelConfig () const { return m_Config; } TunnelConfig * GetTunnelConfig () const { return m_Config; }
TunnelState GetState () const { return m_State; }; TunnelState GetState () const { return m_State; };
@ -120,23 +120,23 @@ namespace tunnel
void Start (); void Start ();
void Stop (); void Stop ();
InboundTunnel * GetInboundTunnel (uint32_t tunnelID); std::shared_ptr<InboundTunnel> GetInboundTunnel (uint32_t tunnelID);
InboundTunnel * GetPendingInboundTunnel (uint32_t replyMsgID); std::shared_ptr<InboundTunnel> GetPendingInboundTunnel (uint32_t replyMsgID);
OutboundTunnel * GetPendingOutboundTunnel (uint32_t replyMsgID); std::shared_ptr<OutboundTunnel> GetPendingOutboundTunnel (uint32_t replyMsgID);
InboundTunnel * GetNextInboundTunnel (); std::shared_ptr<InboundTunnel> GetNextInboundTunnel ();
OutboundTunnel * GetNextOutboundTunnel (); std::shared_ptr<OutboundTunnel> GetNextOutboundTunnel ();
std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; }; std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; };
TransitTunnel * GetTransitTunnel (uint32_t tunnelID); TransitTunnel * GetTransitTunnel (uint32_t tunnelID);
int GetTransitTunnelsExpirationTimeout (); int GetTransitTunnelsExpirationTimeout ();
void AddTransitTunnel (TransitTunnel * tunnel); void AddTransitTunnel (TransitTunnel * tunnel);
void AddOutboundTunnel (OutboundTunnel * newTunnel); void AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel);
void AddInboundTunnel (InboundTunnel * newTunnel); void AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel);
void PostTunnelData (I2NPMessage * msg); void PostTunnelData (I2NPMessage * msg);
void PostTunnelData (const std::vector<I2NPMessage *>& msgs); void PostTunnelData (const std::vector<I2NPMessage *>& msgs);
template<class TTunnel> template<class TTunnel>
TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); std::shared_ptr<TTunnel> CreateTunnel (TunnelConfig * config, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
void AddPendingTunnel (uint32_t replyMsgID, InboundTunnel * tunnel); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<InboundTunnel> tunnel);
void AddPendingTunnel (uint32_t replyMsgID, OutboundTunnel * tunnel); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> tunnel);
std::shared_ptr<TunnelPool> CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops); std::shared_ptr<TunnelPool> CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops);
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool); void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
void StopTunnelPool (std::shared_ptr<TunnelPool> pool); void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
@ -144,7 +144,7 @@ namespace tunnel
private: private:
template<class TTunnel> template<class TTunnel>
TTunnel * GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, TTunnel *>& pendingTunnels); std::shared_ptr<TTunnel> GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels);
void HandleTunnelGatewayMsg (TunnelBase * tunnel, I2NPMessage * msg); void HandleTunnelGatewayMsg (TunnelBase * tunnel, I2NPMessage * msg);
@ -164,10 +164,10 @@ namespace tunnel
bool m_IsRunning; bool m_IsRunning;
std::thread * m_Thread; std::thread * m_Thread;
std::map<uint32_t, InboundTunnel *> m_PendingInboundTunnels; // by replyMsgID std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_PendingInboundTunnels; // by replyMsgID
std::map<uint32_t, OutboundTunnel *> m_PendingOutboundTunnels; // by replyMsgID std::map<uint32_t, std::shared_ptr<OutboundTunnel> > m_PendingOutboundTunnels; // by replyMsgID
std::map<uint32_t, InboundTunnel *> m_InboundTunnels; std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_InboundTunnels;
std::list<OutboundTunnel *> m_OutboundTunnels; std::list<std::shared_ptr<OutboundTunnel> > m_OutboundTunnels;
std::mutex m_TransitTunnelsMutex; std::mutex m_TransitTunnelsMutex;
std::map<uint32_t, TransitTunnel *> m_TransitTunnels; std::map<uint32_t, TransitTunnel *> m_TransitTunnels;
std::mutex m_PoolsMutex; std::mutex m_PoolsMutex;

3
TunnelBase.h

@ -2,6 +2,7 @@
#define TUNNEL_BASE_H__ #define TUNNEL_BASE_H__
#include <inttypes.h> #include <inttypes.h>
#include <memory>
#include "Timestamp.h" #include "Timestamp.h"
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "Identity.h" #include "Identity.h"
@ -54,7 +55,7 @@ namespace tunnel
struct TunnelCreationTimeCmp struct TunnelCreationTimeCmp
{ {
bool operator() (const TunnelBase * t1, const TunnelBase * t2) const bool operator() (std::shared_ptr<const TunnelBase> t1, std::shared_ptr<const TunnelBase> t2) const
{ {
if (t1->GetCreationTime () != t2->GetCreationTime ()) if (t1->GetCreationTime () != t2->GetCreationTime ())
return t1->GetCreationTime () > t2->GetCreationTime (); return t1->GetCreationTime () > t2->GetCreationTime ();

36
TunnelPool.cpp

@ -38,7 +38,7 @@ namespace tunnel
m_Tests.clear (); m_Tests.clear ();
} }
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel) void TunnelPool::TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel)
{ {
if (!m_IsActive) return; if (!m_IsActive) return;
{ {
@ -49,7 +49,7 @@ namespace tunnel
m_LocalDestination->SetLeaseSetUpdated (); m_LocalDestination->SetLeaseSetUpdated ();
} }
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel) void TunnelPool::TunnelExpired (std::shared_ptr<InboundTunnel> expiredTunnel)
{ {
if (expiredTunnel) if (expiredTunnel)
{ {
@ -63,14 +63,14 @@ namespace tunnel
} }
} }
void TunnelPool::TunnelCreated (OutboundTunnel * createdTunnel) void TunnelPool::TunnelCreated (std::shared_ptr<OutboundTunnel> createdTunnel)
{ {
if (!m_IsActive) return; if (!m_IsActive) return;
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex); std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
m_OutboundTunnels.insert (createdTunnel); m_OutboundTunnels.insert (createdTunnel);
} }
void TunnelPool::TunnelExpired (OutboundTunnel * expiredTunnel) void TunnelPool::TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel)
{ {
if (expiredTunnel) if (expiredTunnel)
{ {
@ -84,9 +84,9 @@ namespace tunnel
} }
} }
std::vector<InboundTunnel *> TunnelPool::GetInboundTunnels (int num) const std::vector<std::shared_ptr<InboundTunnel> > TunnelPool::GetInboundTunnels (int num) const
{ {
std::vector<InboundTunnel *> v; std::vector<std::shared_ptr<InboundTunnel> > v;
int i = 0; int i = 0;
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex); std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
for (auto it : m_InboundTunnels) for (auto it : m_InboundTunnels)
@ -101,13 +101,13 @@ namespace tunnel
return v; return v;
} }
OutboundTunnel * TunnelPool::GetNextOutboundTunnel (OutboundTunnel * suggested) const std::shared_ptr<OutboundTunnel> TunnelPool::GetNextOutboundTunnel (std::shared_ptr<OutboundTunnel> suggested) const
{ {
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex); std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
return GetNextTunnel (m_OutboundTunnels, suggested); return GetNextTunnel (m_OutboundTunnels, suggested);
} }
InboundTunnel * TunnelPool::GetNextInboundTunnel (InboundTunnel * suggested) const std::shared_ptr<InboundTunnel> TunnelPool::GetNextInboundTunnel (std::shared_ptr<InboundTunnel> suggested) const
{ {
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex); std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
return GetNextTunnel (m_InboundTunnels, suggested); return GetNextTunnel (m_InboundTunnels, suggested);
@ -274,7 +274,7 @@ namespace tunnel
void TunnelPool::CreateInboundTunnel () void TunnelPool::CreateInboundTunnel ()
{ {
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); auto outboundTunnel = GetNextOutboundTunnel ();
if (!outboundTunnel) if (!outboundTunnel)
outboundTunnel = tunnels.GetNextOutboundTunnel (); outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Creating destination inbound tunnel..."); LogPrint ("Creating destination inbound tunnel...");
@ -299,23 +299,23 @@ namespace tunnel
hops.push_back (hop); hops.push_back (hop);
} }
std::reverse (hops.begin (), hops.end ()); std::reverse (hops.begin (), hops.end ());
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel); auto tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel);
tunnel->SetTunnelPool (shared_from_this ()); tunnel->SetTunnelPool (shared_from_this ());
} }
void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel) void TunnelPool::RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel)
{ {
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel (); auto outboundTunnel = GetNextOutboundTunnel ();
if (!outboundTunnel) if (!outboundTunnel)
outboundTunnel = tunnels.GetNextOutboundTunnel (); outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Re-creating destination inbound tunnel..."); LogPrint ("Re-creating destination inbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel); auto newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel);
newTunnel->SetTunnelPool (shared_from_this()); newTunnel->SetTunnelPool (shared_from_this());
} }
void TunnelPool::CreateOutboundTunnel () void TunnelPool::CreateOutboundTunnel ()
{ {
InboundTunnel * inboundTunnel = GetNextInboundTunnel (); auto inboundTunnel = GetNextInboundTunnel ();
if (!inboundTunnel) if (!inboundTunnel)
inboundTunnel = tunnels.GetNextInboundTunnel (); inboundTunnel = tunnels.GetNextInboundTunnel ();
if (inboundTunnel) if (inboundTunnel)
@ -331,7 +331,7 @@ namespace tunnel
hops.push_back (hop); hops.push_back (hop);
} }
auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> ( auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ())); new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
tunnel->SetTunnelPool (shared_from_this ()); tunnel->SetTunnelPool (shared_from_this ());
} }
@ -339,15 +339,15 @@ namespace tunnel
LogPrint ("Can't create outbound tunnel. No inbound tunnels found"); LogPrint ("Can't create outbound tunnel. No inbound tunnels found");
} }
void TunnelPool::RecreateOutboundTunnel (OutboundTunnel * tunnel) void TunnelPool::RecreateOutboundTunnel (std::shared_ptr<OutboundTunnel> tunnel)
{ {
InboundTunnel * inboundTunnel = GetNextInboundTunnel (); auto inboundTunnel = GetNextInboundTunnel ();
if (!inboundTunnel) if (!inboundTunnel)
inboundTunnel = tunnels.GetNextInboundTunnel (); inboundTunnel = tunnels.GetNextInboundTunnel ();
if (inboundTunnel) if (inboundTunnel)
{ {
LogPrint ("Re-creating destination outbound tunnel..."); LogPrint ("Re-creating destination outbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<OutboundTunnel> ( auto newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ())); tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ()));
newTunnel->SetTunnelPool (shared_from_this ()); newTunnel->SetTunnelPool (shared_from_this ());
} }

24
TunnelPool.h

@ -34,13 +34,13 @@ namespace tunnel
void SetLocalDestination (i2p::garlic::GarlicDestination * destination) { m_LocalDestination = destination; }; void SetLocalDestination (i2p::garlic::GarlicDestination * destination) { m_LocalDestination = destination; };
void CreateTunnels (); void CreateTunnels ();
void TunnelCreated (InboundTunnel * createdTunnel); void TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel);
void TunnelExpired (InboundTunnel * expiredTunnel); void TunnelExpired (std::shared_ptr<InboundTunnel> expiredTunnel);
void TunnelCreated (OutboundTunnel * createdTunnel); void TunnelCreated (std::shared_ptr<OutboundTunnel> createdTunnel);
void TunnelExpired (OutboundTunnel * expiredTunnel); void TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel);
std::vector<InboundTunnel *> GetInboundTunnels (int num) const; std::vector<std::shared_ptr<InboundTunnel> > GetInboundTunnels (int num) const;
OutboundTunnel * GetNextOutboundTunnel (OutboundTunnel * suggested = nullptr) const; std::shared_ptr<OutboundTunnel> GetNextOutboundTunnel (std::shared_ptr<OutboundTunnel> suggested = nullptr) const;
InboundTunnel * GetNextInboundTunnel (InboundTunnel * suggested = nullptr) const; std::shared_ptr<InboundTunnel> GetNextInboundTunnel (std::shared_ptr<InboundTunnel> suggested = nullptr) const;
void TestTunnels (); void TestTunnels ();
void ProcessGarlicMessage (I2NPMessage * msg); void ProcessGarlicMessage (I2NPMessage * msg);
@ -54,8 +54,8 @@ namespace tunnel
void CreateInboundTunnel (); void CreateInboundTunnel ();
void CreateOutboundTunnel (); void CreateOutboundTunnel ();
void RecreateInboundTunnel (InboundTunnel * tunnel); void RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel);
void RecreateOutboundTunnel (OutboundTunnel * tunnel); void RecreateOutboundTunnel (std::shared_ptr<OutboundTunnel> tunnel);
template<class TTunnels> template<class TTunnels>
typename TTunnels::value_type GetNextTunnel (TTunnels& tunnels, typename TTunnels::value_type GetNextTunnel (TTunnels& tunnels,
typename TTunnels::value_type suggested = nullptr) const; typename TTunnels::value_type suggested = nullptr) const;
@ -66,10 +66,10 @@ namespace tunnel
i2p::garlic::GarlicDestination * m_LocalDestination; i2p::garlic::GarlicDestination * m_LocalDestination;
int m_NumInboundHops, m_NumOutboundHops, m_NumTunnels; int m_NumInboundHops, m_NumOutboundHops, m_NumTunnels;
mutable std::mutex m_InboundTunnelsMutex; mutable std::mutex m_InboundTunnelsMutex;
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first std::set<std::shared_ptr<InboundTunnel>, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
mutable std::mutex m_OutboundTunnelsMutex; mutable std::mutex m_OutboundTunnelsMutex;
std::set<OutboundTunnel *, TunnelCreationTimeCmp> m_OutboundTunnels; std::set<std::shared_ptr<OutboundTunnel>, TunnelCreationTimeCmp> m_OutboundTunnels;
std::map<uint32_t, std::pair<OutboundTunnel *, InboundTunnel *> > m_Tests; std::map<uint32_t, std::pair<std::shared_ptr<OutboundTunnel>, std::shared_ptr<InboundTunnel> > > m_Tests;
bool m_IsActive; bool m_IsActive;
public: public:

Loading…
Cancel
Save