2014-03-14 20:35:02 +04:00
|
|
|
#include "Tunnel.h"
|
2014-03-14 23:13:34 +04:00
|
|
|
#include "NetDb.h"
|
2014-03-15 04:24:12 +04:00
|
|
|
#include "Timestamp.h"
|
2014-03-14 20:35:02 +04:00
|
|
|
#include "TunnelPool.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
2014-03-14 23:13:34 +04:00
|
|
|
TunnelPool::TunnelPool (i2p::data::LocalDestination * owner, int numTunnels):
|
|
|
|
m_Owner (owner), m_NumTunnels (numTunnels)
|
2014-03-14 20:35:02 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TunnelPool::~TunnelPool ()
|
|
|
|
{
|
|
|
|
for (auto it: m_InboundTunnels)
|
2014-03-15 04:51:51 +04:00
|
|
|
it->SetTunnelPool (nullptr);
|
2014-03-14 23:13:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
|
|
|
|
{
|
|
|
|
m_InboundTunnels.insert (createdTunnel);
|
2014-03-15 17:16:55 +04:00
|
|
|
if (m_Owner)
|
|
|
|
m_Owner->UpdateLeaseSet ();
|
2014-03-14 23:13:34 +04:00
|
|
|
}
|
|
|
|
|
2014-03-15 05:22:59 +04:00
|
|
|
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
|
|
|
|
{
|
|
|
|
m_InboundTunnels.erase (expiredTunnel);
|
|
|
|
if (m_Owner)
|
|
|
|
m_Owner->UpdateLeaseSet ();
|
|
|
|
}
|
|
|
|
|
2014-03-14 23:13:34 +04:00
|
|
|
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 ()
|
|
|
|
{
|
2014-03-15 04:24:12 +04:00
|
|
|
int num = m_InboundTunnels.size ();
|
|
|
|
for (int i = num; i < m_NumTunnels; i++)
|
2014-03-14 23:13:34 +04:00
|
|
|
CreateInboundTunnel ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelPool::CreateInboundTunnel ()
|
|
|
|
{
|
|
|
|
OutboundTunnel * outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
|
|
|
LogPrint ("Creating destination inbound tunnel...");
|
|
|
|
auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr);
|
2014-03-15 05:22:59 +04:00
|
|
|
auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop);
|
2014-03-14 23:13:34 +04:00
|
|
|
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (
|
|
|
|
new TunnelConfig (std::vector<const i2p::data::RouterInfo *>
|
|
|
|
{
|
|
|
|
firstHop,
|
2014-03-15 17:16:55 +04:00
|
|
|
secondHop
|
|
|
|
// TODO: swithc to 3-hops later
|
|
|
|
/*i2p::data::netdb.GetRandomRouter (secondHop) */
|
2014-03-14 23:13:34 +04:00
|
|
|
}),
|
|
|
|
outboundTunnel);
|
2014-03-15 04:51:51 +04:00
|
|
|
tunnel->SetTunnelPool (this);
|
2014-03-14 20:35:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|