Browse Source

manage tunnel pools

pull/46/head
orignal 11 years ago
parent
commit
ab23327da6
  1. 11
      HTTPServer.cpp
  2. 13
      Streaming.cpp
  3. 4
      Streaming.h
  4. 30
      Tunnel.cpp
  5. 5
      Tunnel.h
  6. 38
      TunnelPool.cpp
  7. 11
      TunnelPool.h

11
HTTPServer.cpp

@ -148,6 +148,17 @@ namespace util
s << " " << (int)it.second->GetNumReceivedBytes () << "<BR>"; s << " " << (int)it.second->GetNumReceivedBytes () << "<BR>";
} }
s << "<P>Tunnel pools</P>";
for (auto it: i2p::tunnel::tunnels.GetTunnelPools ())
{
for (auto it1: it->GetInboundTunnels ())
{
it1->GetTunnelConfig ()->Print (s);
s << " " << (int)it1->GetNumReceivedBytes () << "<BR>";
}
s << "<BR>";
}
s << "<P>Transit tunnels</P>"; s << "<P>Transit tunnels</P>";
for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ()) for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ())
{ {

13
Streaming.cpp

@ -300,7 +300,7 @@ namespace stream
StreamingDestination * sharedLocalDestination = nullptr; StreamingDestination * sharedLocalDestination = nullptr;
StreamingDestination::StreamingDestination (): m_TunnelPool (this), m_LeaseSet (nullptr) StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr)
{ {
// TODO: read from file later // TODO: read from file later
m_Keys = i2p::data::CreateRandomKeys (); m_Keys = i2p::data::CreateRandomKeys ();
@ -308,6 +308,7 @@ namespace stream
m_IdentHash = i2p::data::CalculateIdentHash (m_Identity); m_IdentHash = i2p::data::CalculateIdentHash (m_Identity);
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
i2p::tunnel::tunnels.CreateTunnelPool (this);
} }
StreamingDestination::~StreamingDestination () StreamingDestination::~StreamingDestination ()
@ -316,16 +317,6 @@ namespace stream
DeleteI2NPMessage (m_LeaseSet); DeleteI2NPMessage (m_LeaseSet);
} }
void StreamingDestination::Start ()
{
m_TunnelPool.CreateTunnels ();
}
void StreamingDestination::Stop ()
{
// TODO:
}
void StreamingDestination::HandleNextPacket (Packet * packet) void StreamingDestination::HandleNextPacket (Packet * packet)
{ {
uint32_t sendStreamID = packet->GetSendStreamID (); uint32_t sendStreamID = packet->GetSendStreamID ();

4
Streaming.h

@ -11,7 +11,6 @@
#include "LeaseSet.h" #include "LeaseSet.h"
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "Tunnel.h" #include "Tunnel.h"
#include "TunnelPool.h"
namespace i2p namespace i2p
{ {
@ -104,8 +103,6 @@ namespace stream
StreamingDestination (); StreamingDestination ();
~StreamingDestination (); ~StreamingDestination ();
void Start ();
void Stop ();
const i2p::data::Keys& GetKeys () const { return m_Keys; }; const i2p::data::Keys& GetKeys () const { return m_Keys; };
const i2p::data::Identity& GetIdentity () const { return m_Identity; }; const i2p::data::Identity& GetIdentity () const { return m_Identity; };
@ -130,7 +127,6 @@ namespace stream
i2p::data::Identity m_Identity; i2p::data::Identity m_Identity;
i2p::data::IdentHash m_IdentHash; i2p::data::IdentHash m_IdentHash;
i2p::tunnel::TunnelPool m_TunnelPool;
I2NPMessage * m_LeaseSet; I2NPMessage * m_LeaseSet;
CryptoPP::DSA::PrivateKey m_SigningPrivateKey; CryptoPP::DSA::PrivateKey m_SigningPrivateKey;

30
Tunnel.cpp

@ -186,6 +186,10 @@ namespace tunnel
for (auto& it : m_PendingTunnels) for (auto& it : m_PendingTunnels)
delete it.second; delete it.second;
m_PendingTunnels.clear (); m_PendingTunnels.clear ();
for (auto& it: m_Pools)
delete it;
m_Pools.clear ();
} }
InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID) InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID)
@ -269,6 +273,11 @@ namespace tunnel
return tunnel;*/ return tunnel;*/
} }
void Tunnels::CreateTunnelPool (i2p::data::LocalDestination * localDestination)
{
m_Pools.push_back (new TunnelPool (localDestination));
}
void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) void Tunnels::AddTransitTunnel (TransitTunnel * tunnel)
{ {
m_TransitTunnels[tunnel->GetTunnelID ()] = tunnel; m_TransitTunnels[tunnel->GetTunnelID ()] = tunnel;
@ -343,9 +352,6 @@ namespace tunnel
for (auto& it : m_PendingTunnels) for (auto& it : m_PendingTunnels)
{ {
LogPrint ("Pending tunnel build request ", it.first, " has not been responded. Deleted"); LogPrint ("Pending tunnel build request ", it.first, " has not been responded. Deleted");
auto pool = it.second->GetTunnelPool ();
if (pool)
pool->TunnelCreationFailed (it.second);
delete it.second; delete it.second;
} }
m_PendingTunnels.clear (); m_PendingTunnels.clear ();
@ -353,6 +359,7 @@ namespace tunnel
ManageInboundTunnels (); ManageInboundTunnels ();
ManageOutboundTunnels (); ManageOutboundTunnels ();
ManageTransitTunnels (); ManageTransitTunnels ();
ManageTunnelPools ();
/* if (!m_IsTunnelCreated) /* if (!m_IsTunnelCreated)
{ {
@ -421,9 +428,6 @@ namespace tunnel
if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
{ {
LogPrint ("Tunnel ", it->second->GetTunnelID (), " expired"); LogPrint ("Tunnel ", it->second->GetTunnelID (), " expired");
auto pool = it->second->GetTunnelPool ();
if (pool)
pool->TunnelExpired (it->second);
it = m_InboundTunnels.erase (it); it = m_InboundTunnels.erase (it);
} }
else else
@ -481,6 +485,12 @@ namespace tunnel
} }
} }
void Tunnels::ManageTunnelPools ()
{
for (auto& it: m_Pools)
it->ManageTunnels ();
}
void Tunnels::PostTunnelData (I2NPMessage * msg) void Tunnels::PostTunnelData (I2NPMessage * msg)
{ {
if (msg) m_Queue.Put (msg); if (msg) m_Queue.Put (msg);
@ -503,15 +513,15 @@ namespace tunnel
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel) void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel)
{ {
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
auto pool = newTunnel->GetTunnelPool (); auto pool = newTunnel->GetTunnelPool ();
if (pool) if (!pool)
pool->TunnelCreated (newTunnel);
else
{ {
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
// build symmetric outbound tunnel // build symmetric outbound tunnel
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ()); CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ());
} }
else
pool->TunnelCreated (newTunnel);
} }

5
Tunnel.h

@ -105,7 +105,6 @@ namespace tunnel
Tunnels (); Tunnels ();
~Tunnels (); ~Tunnels ();
void Start (); void Start ();
void Stop (); void Stop ();
@ -121,6 +120,7 @@ namespace tunnel
void PostTunnelData (I2NPMessage * msg); void PostTunnelData (I2NPMessage * msg);
template<class TTunnel> template<class TTunnel>
TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0);
void CreateTunnelPool (i2p::data::LocalDestination * localDestination);
OutboundTunnel * CreateOneHopOutboundTestTunnel (InboundTunnel * replyTunnel); OutboundTunnel * CreateOneHopOutboundTestTunnel (InboundTunnel * replyTunnel);
InboundTunnel * CreateOneHopInboundTestTunnel (OutboundTunnel * outboundTunnel = 0); InboundTunnel * CreateOneHopInboundTestTunnel (OutboundTunnel * outboundTunnel = 0);
@ -134,6 +134,7 @@ namespace tunnel
void ManageOutboundTunnels (); void ManageOutboundTunnels ();
void ManageInboundTunnels (); void ManageInboundTunnels ();
void ManageTransitTunnels (); void ManageTransitTunnels ();
void ManageTunnelPools ();
void CreateZeroHopsInboundTunnel (); void CreateZeroHopsInboundTunnel ();
@ -147,6 +148,7 @@ namespace tunnel
std::map<uint32_t, InboundTunnel *> m_InboundTunnels; std::map<uint32_t, InboundTunnel *> m_InboundTunnels;
std::list<OutboundTunnel *> m_OutboundTunnels; std::list<OutboundTunnel *> m_OutboundTunnels;
std::map<uint32_t, TransitTunnel *> m_TransitTunnels; std::map<uint32_t, TransitTunnel *> m_TransitTunnels;
std::list<TunnelPool *> m_Pools;
i2p::util::Queue<I2NPMessage> m_Queue; i2p::util::Queue<I2NPMessage> m_Queue;
public: public:
@ -155,6 +157,7 @@ namespace tunnel
const decltype(m_OutboundTunnels)& GetOutboundTunnels () const { return m_OutboundTunnels; }; const decltype(m_OutboundTunnels)& GetOutboundTunnels () const { return m_OutboundTunnels; };
const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; }; const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; };
const decltype(m_TransitTunnels)& GetTransitTunnels () const { return m_TransitTunnels; }; const decltype(m_TransitTunnels)& GetTransitTunnels () const { return m_TransitTunnels; };
const decltype(m_Pools)& GetTunnelPools () const { return m_Pools; };
}; };
extern Tunnels tunnels; extern Tunnels tunnels;

38
TunnelPool.cpp

@ -1,5 +1,6 @@
#include "Tunnel.h" #include "Tunnel.h"
#include "NetDb.h" #include "NetDb.h"
#include "Timestamp.h"
#include "TunnelPool.h" #include "TunnelPool.h"
namespace i2p namespace i2p
@ -14,19 +15,7 @@ namespace tunnel
TunnelPool::~TunnelPool () TunnelPool::~TunnelPool ()
{ {
for (auto it: m_InboundTunnels) for (auto it: m_InboundTunnels)
it->SetTunnelPool (nullptr); delete it;
}
void TunnelPool::TunnelCreationFailed (Tunnel * failedTunnel)
{
CreateInboundTunnel ();
}
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
{
CreateInboundTunnel ();
if (m_Owner)
m_Owner->UpdateLeaseSet ();
} }
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel) void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
@ -49,7 +38,8 @@ namespace tunnel
void TunnelPool::CreateTunnels () void TunnelPool::CreateTunnels ()
{ {
for (int i = 0; i < m_NumTunnels; i++) int num = m_InboundTunnels.size ();
for (int i = num; i < m_NumTunnels; i++)
CreateInboundTunnel (); CreateInboundTunnel ();
} }
@ -67,5 +57,25 @@ namespace tunnel
outboundTunnel); outboundTunnel);
tunnel->SetTunnelPool (this); tunnel->SetTunnelPool (this);
} }
void TunnelPool::ManageTunnels ()
{
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
bool isLeaseSetUpdated = false;
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
{
if (ts > (*it)->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
{
LogPrint ("Destination tunnel ", (*it)->GetTunnelID (), " expired");
m_InboundTunnels.erase (it++);
isLeaseSetUpdated = true;
}
else
++it;
}
CreateTunnels ();
if (isLeaseSetUpdated && m_Owner)
m_Owner->UpdateLeaseSet ();
}
} }
} }

11
TunnelPool.h

@ -24,11 +24,9 @@ namespace tunnel
~TunnelPool (); ~TunnelPool ();
void CreateTunnels (); void CreateTunnels ();
std::vector<InboundTunnel *> GetInboundTunnels (int num) const;
void TunnelCreationFailed (Tunnel * failedTunnel);
void TunnelExpired (InboundTunnel * expiredTunnel);
void TunnelCreated (InboundTunnel * createdTunnel); void TunnelCreated (InboundTunnel * createdTunnel);
std::vector<InboundTunnel *> GetInboundTunnels (int num) const;
void ManageTunnels ();
private: private:
@ -39,6 +37,11 @@ namespace tunnel
i2p::data::LocalDestination * m_Owner; i2p::data::LocalDestination * m_Owner;
int m_NumTunnels; int m_NumTunnels;
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
public:
// for HTTP only
const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; };
}; };
} }
} }

Loading…
Cancel
Save