Skip to content

Commit

Permalink
create inbound tunnels per local destination
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Mar 14, 2014
1 parent 7caa46b commit cba18fa
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ namespace data

mutable i2p::crypto::ElGamalEncryption * m_ElGamalEncryption; // use lazy initialization
};

class LocalDestination
{
public:

virtual void UpdateLeaseSet () = 0; // LeaseSet must be update
};
}
}

Expand Down
14 changes: 12 additions & 2 deletions Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ namespace stream

StreamingDestination * sharedLocalDestination = nullptr;

StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr)
StreamingDestination::StreamingDestination (): m_TunnelPool (this), m_LeaseSet (nullptr)
{
// TODO: read from file later
m_Keys = i2p::data::CreateRandomKeys ();
Expand All @@ -315,7 +315,17 @@ namespace stream
if (m_LeaseSet)
DeleteI2NPMessage (m_LeaseSet);
}


void StreamingDestination::Start ()
{
m_TunnelPool.CreateTunnels ();
}

void StreamingDestination::Stop ()
{
// TODO:
}

void StreamingDestination::HandleNextPacket (Packet * packet)
{
uint32_t sendStreamID = packet->GetSendStreamID ();
Expand Down
15 changes: 11 additions & 4 deletions Streaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "LeaseSet.h"
#include "I2NPProtocol.h"
#include "Tunnel.h"
#include "TunnelPool.h"

namespace i2p
{
Expand Down Expand Up @@ -97,22 +98,27 @@ namespace stream
i2p::tunnel::OutboundTunnel * m_OutboundTunnel;
};

class StreamingDestination
class StreamingDestination: public i2p::data::LocalDestination
{
public:

StreamingDestination ();
~StreamingDestination ();

void Start ();
void Stop ();

const i2p::data::Keys& GetKeys () const { return m_Keys; };
const i2p::data::Identity& GetIdentity () const { return m_Identity; };
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);
void DeleteStream (Stream * stream);
void HandleNextPacket (Packet * packet);

// implements LocalDestination
void UpdateLeaseSet () {}; // TODO:

private:

I2NPMessage * CreateLeaseSet () const;
Expand All @@ -124,6 +130,7 @@ namespace stream
i2p::data::Identity m_Identity;
i2p::data::IdentHash m_IdentHash;

i2p::tunnel::TunnelPool m_TunnelPool;
I2NPMessage * m_LeaseSet;

CryptoPP::DSA::PrivateKey m_SigningPrivateKey;
Expand Down
10 changes: 8 additions & 2 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,14 @@ namespace tunnel
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel)
{
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
// build symmetric outbound tunnel
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ());
auto pool = newTunnel->GetTunnelPool ();
if (pool)
pool->TunnelCreated (newTunnel);
else
{
// build symmetric outbound tunnel
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ());
}
}


Expand Down
11 changes: 11 additions & 0 deletions TunnelBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ namespace tunnel

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;
};
};
}
}

Expand Down
47 changes: 46 additions & 1 deletion TunnelPool.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "Tunnel.h"
#include "NetDb.h"
#include "TunnelPool.h"

namespace i2p
{
namespace tunnel
{
TunnelPool::TunnelPool ()
TunnelPool::TunnelPool (i2p::data::LocalDestination * owner, int numTunnels):
m_Owner (owner), m_NumTunnels (numTunnels)
{
}

Expand All @@ -17,10 +19,53 @@ namespace tunnel

void TunnelPool::TunnelCreationFailed (Tunnel * failedTunnel)
{
CreateInboundTunnel ();
}

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 changes: 17 additions & 3 deletions TunnelPool.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef TUNNEL_POOL__
#define TUNNEL_POOL__

#include <list>
#include <set>
#include <vector>
#include "Identity.h"
#include "LeaseSet.h"
#include "I2NPProtocol.h"
#include "TunnelBase.h"

namespace i2p
{
Expand All @@ -16,15 +20,25 @@ namespace tunnel
{
public:

TunnelPool ();
TunnelPool (i2p::data::LocalDestination * owner, int numTunnels = 5);
~TunnelPool ();

void CreateTunnels ();
std::vector<InboundTunnel *> GetInboundTunnels (int num) const;

void TunnelCreationFailed (Tunnel * failedTunnel);
void TunnelExpired (InboundTunnel * expiredTunnel);
void TunnelCreated (InboundTunnel * createdTunnel);

private:

void CreateInboundTunnel ();

private:

std::list<InboundTunnel *> m_InboundTunnels;
i2p::data::LocalDestination * m_Owner;
int m_NumTunnels;
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
};
}
}
Expand Down

0 comments on commit cba18fa

Please sign in to comment.