i2pd/TunnelPool.cpp

208 lines
5.5 KiB
C++
Raw Normal View History

2014-03-18 00:50:03 +04:00
#include "I2PEndian.h"
#include "CryptoConst.h"
2014-03-14 20:35:02 +04:00
#include "Tunnel.h"
#include "NetDb.h"
2014-03-15 04:24:12 +04:00
#include "Timestamp.h"
2014-03-18 00:50:03 +04:00
#include "Garlic.h"
2014-03-14 20:35:02 +04:00
#include "TunnelPool.h"
namespace i2p
{
namespace tunnel
{
TunnelPool::TunnelPool (i2p::data::LocalDestination& localDestination, int numHops, int numTunnels):
m_LocalDestination (localDestination), m_NumHops (numHops), 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-17 00:03:20 +04:00
for (auto it: m_OutboundTunnels)
it->SetTunnelPool (nullptr);
}
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
{
m_InboundTunnels.insert (createdTunnel);
}
2014-03-15 05:22:59 +04:00
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
{
2014-03-18 16:15:43 +04:00
if (expiredTunnel)
{
expiredTunnel->SetTunnelPool (nullptr);
m_InboundTunnels.erase (expiredTunnel);
2014-07-10 05:43:33 +04:00
for (auto it: m_Tests)
if (it.second.second == expiredTunnel) it.second.second = nullptr;
2014-03-18 16:15:43 +04:00
}
2014-04-01 21:55:09 +04:00
m_LocalDestination.UpdateLeaseSet ();
2014-03-15 05:22:59 +04:00
}
2014-03-17 00:03:20 +04:00
void TunnelPool::TunnelCreated (OutboundTunnel * createdTunnel)
{
m_OutboundTunnels.insert (createdTunnel);
}
void TunnelPool::TunnelExpired (OutboundTunnel * expiredTunnel)
{
2014-03-18 16:15:43 +04:00
if (expiredTunnel)
2014-03-21 23:54:55 +04:00
{
2014-03-18 16:15:43 +04:00
expiredTunnel->SetTunnelPool (nullptr);
m_OutboundTunnels.erase (expiredTunnel);
2014-07-10 05:43:33 +04:00
for (auto it: m_Tests)
if (it.second.first == expiredTunnel) it.second.first = nullptr;
2014-03-21 23:54:55 +04:00
}
2014-03-17 00:03:20 +04:00
}
2014-03-15 05:22:59 +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;
2014-03-21 23:54:55 +04:00
if (!it->IsFailed ())
{
v.push_back (it);
i++;
}
}
return v;
}
2014-03-17 00:03:20 +04:00
OutboundTunnel * TunnelPool::GetNextOutboundTunnel ()
{
return GetNextTunnel (m_OutboundTunnels);
2014-03-17 00:03:20 +04:00
}
InboundTunnel * TunnelPool::GetNextInboundTunnel ()
{
return GetNextTunnel (m_InboundTunnels);
}
template<class TTunnels>
typename TTunnels::value_type TunnelPool::GetNextTunnel (TTunnels& tunnels)
{
if (tunnels.empty ()) return nullptr;
for (auto it: tunnels)
if (!it->IsFailed ())
return it;
return nullptr;
}
void TunnelPool::CreateTunnels ()
{
2014-03-15 04:24:12 +04:00
int num = m_InboundTunnels.size ();
for (int i = num; i < m_NumTunnels; i++)
CreateInboundTunnel ();
2014-03-17 00:03:20 +04:00
num = m_OutboundTunnels.size ();
for (int i = num; i < m_NumTunnels; i++)
CreateOutboundTunnel ();
}
2014-03-18 00:50:03 +04:00
void TunnelPool::TestTunnels ()
{
auto& rnd = i2p::context.GetRandomNumberGenerator ();
for (auto it: m_Tests)
{
LogPrint ("Tunnel test ", (int)it.first, " failed");
// both outbound and inbound tunnels considered as invalid
2014-07-10 05:43:33 +04:00
if (it.second.first)
{
it.second.first->SetFailed (true);
m_OutboundTunnels.erase (it.second.first);
}
if (it.second.second)
{
it.second.second->SetFailed (true);
m_InboundTunnels.erase (it.second.second);
}
2014-03-18 00:50:03 +04:00
}
m_Tests.clear ();
auto it1 = m_OutboundTunnels.begin ();
auto it2 = m_InboundTunnels.begin ();
while (it1 != m_OutboundTunnels.end () && it2 != m_InboundTunnels.end ())
{
2014-03-22 02:26:11 +04:00
bool failed = false;
if ((*it1)->IsFailed ())
{
failed = true;
it1++;
}
if ((*it2)->IsFailed ())
{
failed = true;
it2++;
}
if (!failed)
{
uint32_t msgID = rnd.GenerateWord32 ();
m_Tests[msgID] = std::make_pair (*it1, *it2);
(*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
CreateDeliveryStatusMsg (msgID));
it1++; it2++;
}
2014-03-18 00:50:03 +04:00
}
}
void TunnelPool::ProcessDeliveryStatus (I2NPMessage * msg)
{
I2NPDeliveryStatusMsg * deliveryStatus = (I2NPDeliveryStatusMsg *)msg->GetPayload ();
auto it = m_Tests.find (be32toh (deliveryStatus->msgID));
if (it != m_Tests.end ())
{
LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - be64toh (deliveryStatus->timestamp), " milliseconds");
m_Tests.erase (it);
}
else
i2p::garlic::routing.HandleDeliveryStatusMessage (msg->GetPayload (), msg->GetLength ()); // TODO:
DeleteI2NPMessage (msg);
}
void TunnelPool::CreateInboundTunnel ()
{
2014-03-17 00:03:20 +04:00
OutboundTunnel * outboundTunnel = m_OutboundTunnels.size () > 0 ?
*m_OutboundTunnels.begin () : tunnels.GetNextOutboundTunnel ();
LogPrint ("Creating destination inbound tunnel...");
auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr);
2014-04-07 02:30:27 +04:00
auto secondHop = outboundTunnel ? outboundTunnel->GetTunnelConfig ()->GetFirstHop ()->router : nullptr;
if (!secondHop || secondHop->GetIdentHash () == i2p::context.GetIdentHash ())
secondHop = i2p::data::netdb.GetRandomRouter (firstHop);
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (
new TunnelConfig (std::vector<const i2p::data::RouterInfo *>
{
firstHop,
2014-03-15 17:16:55 +04:00
secondHop
}),
outboundTunnel);
2014-03-15 04:51:51 +04:00
tunnel->SetTunnelPool (this);
2014-03-14 20:35:02 +04:00
}
2014-03-17 00:03:20 +04:00
void TunnelPool::CreateOutboundTunnel ()
{
InboundTunnel * inboundTunnel = m_InboundTunnels.size () > 0 ?
*m_InboundTunnels.begin () : tunnels.GetNextInboundTunnel ();
if (inboundTunnel)
{
LogPrint ("Creating destination outbound tunnel...");
const i2p::data::RouterInfo * prevHop = &i2p::context.GetRouterInfo ();
std::vector<const i2p::data::RouterInfo *> hops;
for (int i = 0; i < m_NumHops; i++)
{
auto hop = i2p::data::netdb.GetRandomRouter (prevHop);
prevHop = hop;
hops.push_back (hop);
}
2014-03-17 00:03:20 +04:00
auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> (
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
2014-03-17 00:03:20 +04:00
tunnel->SetTunnelPool (this);
}
}
2014-03-14 20:35:02 +04:00
}
}