Browse Source

create inbound tunnels per local destination

pull/46/head
orignal 11 years ago
parent
commit
cba18faa87
  1. 7
      Identity.h
  2. 14
      Streaming.cpp
  3. 15
      Streaming.h
  4. 10
      Tunnel.cpp
  5. 11
      TunnelBase.h
  6. 47
      TunnelPool.cpp
  7. 20
      TunnelPool.h

7
Identity.h

@ -104,6 +104,13 @@ namespace data
mutable i2p::crypto::ElGamalEncryption * m_ElGamalEncryption; // use lazy initialization mutable i2p::crypto::ElGamalEncryption * m_ElGamalEncryption; // use lazy initialization
}; };
class LocalDestination
{
public:
virtual void UpdateLeaseSet () = 0; // LeaseSet must be update
};
} }
} }

14
Streaming.cpp

@ -300,7 +300,7 @@ namespace stream
StreamingDestination * sharedLocalDestination = nullptr; StreamingDestination * sharedLocalDestination = nullptr;
StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr) StreamingDestination::StreamingDestination (): m_TunnelPool (this), m_LeaseSet (nullptr)
{ {
// TODO: read from file later // TODO: read from file later
m_Keys = i2p::data::CreateRandomKeys (); m_Keys = i2p::data::CreateRandomKeys ();
@ -315,7 +315,17 @@ namespace stream
if (m_LeaseSet) if (m_LeaseSet)
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 ();

15
Streaming.h

@ -11,6 +11,7 @@
#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
{ {
@ -97,22 +98,27 @@ namespace stream
i2p::tunnel::OutboundTunnel * m_OutboundTunnel; i2p::tunnel::OutboundTunnel * m_OutboundTunnel;
}; };
class StreamingDestination class StreamingDestination: public i2p::data::LocalDestination
{ {
public: public:
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; };
I2NPMessage * GetLeaseSet (); I2NPMessage * GetLeaseSet ();
void Sign (uint8_t * buf, int len, uint8_t * signature) const; void Sign (uint8_t * buf, int len, uint8_t * signature) const;
Stream * CreateNewStream (const i2p::data::LeaseSet& remote); Stream * CreateNewStream (const i2p::data::LeaseSet& remote);
void DeleteStream (Stream * stream); void DeleteStream (Stream * stream);
void HandleNextPacket (Packet * packet); void HandleNextPacket (Packet * packet);
// implements LocalDestination
void UpdateLeaseSet () {}; // TODO:
private: private:
I2NPMessage * CreateLeaseSet () const; I2NPMessage * CreateLeaseSet () const;
@ -124,6 +130,7 @@ 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;

10
Tunnel.cpp

@ -504,8 +504,14 @@ namespace tunnel
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel) void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel)
{ {
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel; m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
// build symmetric outbound tunnel auto pool = newTunnel->GetTunnelPool ();
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ()); if (pool)
pool->TunnelCreated (newTunnel);
else
{
// build symmetric outbound tunnel
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ());
}
} }

11
TunnelBase.h

@ -48,6 +48,17 @@ namespace tunnel
uint32_t m_CreationTime; // seconds since epoch uint32_t m_CreationTime; // seconds since epoch
}; };
struct TunnelCreationTimeCmp
{
bool operator() (const TunnelBase * t1, const TunnelBase * t2) const
{
if (t1->GetCreationTime () != t2->GetCreationTime ())
return t1->GetCreationTime () > t2->GetCreationTime ();
else
return t1 < t2;
};
};
} }
} }

47
TunnelPool.cpp

@ -1,11 +1,13 @@
#include "Tunnel.h" #include "Tunnel.h"
#include "NetDb.h"
#include "TunnelPool.h" #include "TunnelPool.h"
namespace i2p namespace i2p
{ {
namespace tunnel namespace tunnel
{ {
TunnelPool::TunnelPool () TunnelPool::TunnelPool (i2p::data::LocalDestination * owner, int numTunnels):
m_Owner (owner), m_NumTunnels (numTunnels)
{ {
} }
@ -17,10 +19,53 @@ namespace tunnel
void TunnelPool::TunnelCreationFailed (Tunnel * failedTunnel) void TunnelPool::TunnelCreationFailed (Tunnel * failedTunnel)
{ {
CreateInboundTunnel ();
} }
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel) void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
{ {
CreateInboundTunnel ();
if (m_Owner)
m_Owner->UpdateLeaseSet ();
}
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
{
m_InboundTunnels.insert (createdTunnel);
}
std::vector<InboundTunnel *> TunnelPool::GetInboundTunnels (int num) const
{
std::vector<InboundTunnel *> v;
int i = 0;
for (auto it : m_InboundTunnels)
{
if (i >= num) break;
v.push_back (it);
i++;
}
return v;
}
void TunnelPool::CreateTunnels ()
{
for (int i = 0; i < m_NumTunnels; i++)
CreateInboundTunnel ();
}
void TunnelPool::CreateInboundTunnel ()
{
OutboundTunnel * outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Creating destination inbound tunnel...");
auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr);
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (
new TunnelConfig (std::vector<const i2p::data::RouterInfo *>
{
firstHop,
i2p::data::netdb.GetRandomRouter (firstHop)
}),
outboundTunnel);
tunnel->SetTunnelPool (this);
} }
} }
} }

20
TunnelPool.h

@ -1,8 +1,12 @@
#ifndef TUNNEL_POOL__ #ifndef TUNNEL_POOL__
#define TUNNEL_POOL__ #define TUNNEL_POOL__
#include <list> #include <set>
#include <vector>
#include "Identity.h"
#include "LeaseSet.h" #include "LeaseSet.h"
#include "I2NPProtocol.h"
#include "TunnelBase.h"
namespace i2p namespace i2p
{ {
@ -16,15 +20,25 @@ namespace tunnel
{ {
public: public:
TunnelPool (); TunnelPool (i2p::data::LocalDestination * owner, int numTunnels = 5);
~TunnelPool (); ~TunnelPool ();
void CreateTunnels ();
std::vector<InboundTunnel *> GetInboundTunnels (int num) const;
void TunnelCreationFailed (Tunnel * failedTunnel); void TunnelCreationFailed (Tunnel * failedTunnel);
void TunnelExpired (InboundTunnel * expiredTunnel); void TunnelExpired (InboundTunnel * expiredTunnel);
void TunnelCreated (InboundTunnel * createdTunnel);
private:
void CreateInboundTunnel ();
private: private:
std::list<InboundTunnel *> m_InboundTunnels; i2p::data::LocalDestination * m_Owner;
int m_NumTunnels;
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
}; };
} }
} }

Loading…
Cancel
Save