2014-03-17 16:50:03 -04:00
|
|
|
#include "I2PEndian.h"
|
2014-03-15 22:02:33 -04:00
|
|
|
#include "CryptoConst.h"
|
2014-03-14 12:35:02 -04:00
|
|
|
#include "Tunnel.h"
|
2014-03-14 15:13:34 -04:00
|
|
|
#include "NetDb.h"
|
2014-03-14 20:24:12 -04:00
|
|
|
#include "Timestamp.h"
|
2014-03-17 16:50:03 -04:00
|
|
|
#include "Garlic.h"
|
2014-03-14 12:35:02 -04:00
|
|
|
#include "TunnelPool.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
2014-10-06 20:18:18 -04:00
|
|
|
TunnelPool::TunnelPool (i2p::garlic::GarlicDestination& localDestination, int numHops, int numTunnels):
|
2014-10-11 09:01:08 -04:00
|
|
|
m_LocalDestination (localDestination), m_NumHops (numHops), m_NumTunnels (numTunnels),
|
2014-10-13 11:21:57 -04:00
|
|
|
m_IsActive (true)
|
2014-03-14 12:35:02 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TunnelPool::~TunnelPool ()
|
2014-10-11 09:47:24 -04:00
|
|
|
{
|
|
|
|
DetachTunnels ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelPool::DetachTunnels ()
|
2014-03-14 12:35:02 -04:00
|
|
|
{
|
2014-10-06 12:50:36 -04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
for (auto it: m_InboundTunnels)
|
|
|
|
it->SetTunnelPool (nullptr);
|
2014-10-13 11:21:57 -04:00
|
|
|
m_InboundTunnels.clear ();
|
2014-10-06 12:50:36 -04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
for (auto it: m_OutboundTunnels)
|
|
|
|
it->SetTunnelPool (nullptr);
|
2014-10-13 11:21:57 -04:00
|
|
|
m_OutboundTunnels.clear ();
|
2014-10-06 12:50:36 -04:00
|
|
|
}
|
2014-10-13 11:21:57 -04:00
|
|
|
m_Tests.clear ();
|
2014-10-11 09:47:24 -04:00
|
|
|
}
|
|
|
|
|
2014-03-14 15:13:34 -04:00
|
|
|
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
|
|
|
|
{
|
2014-10-13 11:21:57 -04:00
|
|
|
if (!m_IsActive) return;
|
2014-10-03 10:35:11 -04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
m_InboundTunnels.insert (createdTunnel);
|
|
|
|
}
|
2014-08-15 19:21:30 -04:00
|
|
|
m_LocalDestination.SetLeaseSetUpdated ();
|
2014-03-14 15:13:34 -04:00
|
|
|
}
|
|
|
|
|
2014-03-14 21:22:59 -04:00
|
|
|
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
|
|
|
|
{
|
2014-03-18 08:15:43 -04:00
|
|
|
if (expiredTunnel)
|
|
|
|
{
|
|
|
|
expiredTunnel->SetTunnelPool (nullptr);
|
2014-07-09 21:43:33 -04:00
|
|
|
for (auto it: m_Tests)
|
|
|
|
if (it.second.second == expiredTunnel) it.second.second = nullptr;
|
2014-08-08 22:44:33 -04:00
|
|
|
RecreateInboundTunnel (expiredTunnel);
|
2014-10-03 10:35:11 -04:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
m_InboundTunnels.erase (expiredTunnel);
|
2014-03-18 08:15:43 -04:00
|
|
|
}
|
2014-03-14 21:22:59 -04:00
|
|
|
}
|
2014-03-16 16:03:20 -04:00
|
|
|
|
|
|
|
void TunnelPool::TunnelCreated (OutboundTunnel * createdTunnel)
|
|
|
|
{
|
2014-10-13 11:21:57 -04:00
|
|
|
if (!m_IsActive) return;
|
2014-10-03 10:35:11 -04:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2014-03-16 16:03:20 -04:00
|
|
|
m_OutboundTunnels.insert (createdTunnel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelPool::TunnelExpired (OutboundTunnel * expiredTunnel)
|
|
|
|
{
|
2014-03-18 08:15:43 -04:00
|
|
|
if (expiredTunnel)
|
2014-03-21 15:54:55 -04:00
|
|
|
{
|
2014-03-18 08:15:43 -04:00
|
|
|
expiredTunnel->SetTunnelPool (nullptr);
|
2014-07-09 21:43:33 -04:00
|
|
|
for (auto it: m_Tests)
|
|
|
|
if (it.second.first == expiredTunnel) it.second.first = nullptr;
|
2014-08-08 22:44:33 -04:00
|
|
|
RecreateOutboundTunnel (expiredTunnel);
|
2014-10-03 10:35:11 -04:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
m_OutboundTunnels.erase (expiredTunnel);
|
2014-03-21 15:54:55 -04:00
|
|
|
}
|
2014-03-16 16:03:20 -04:00
|
|
|
}
|
2014-03-14 21:22:59 -04:00
|
|
|
|
2014-03-14 15:13:34 -04:00
|
|
|
std::vector<InboundTunnel *> TunnelPool::GetInboundTunnels (int num) const
|
|
|
|
{
|
|
|
|
std::vector<InboundTunnel *> v;
|
|
|
|
int i = 0;
|
2014-10-03 10:35:11 -04:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2014-03-14 15:13:34 -04:00
|
|
|
for (auto it : m_InboundTunnels)
|
|
|
|
{
|
|
|
|
if (i >= num) break;
|
2014-08-27 21:53:44 -04:00
|
|
|
if (it->IsEstablished ())
|
2014-03-21 15:54:55 -04:00
|
|
|
{
|
|
|
|
v.push_back (it);
|
|
|
|
i++;
|
|
|
|
}
|
2014-03-14 15:13:34 -04:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-10-03 10:35:11 -04:00
|
|
|
OutboundTunnel * TunnelPool::GetNextOutboundTunnel (OutboundTunnel * suggested) const
|
2014-03-16 16:03:20 -04:00
|
|
|
{
|
2014-10-03 10:35:11 -04:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2014-08-17 14:42:49 -04:00
|
|
|
return GetNextTunnel (m_OutboundTunnels, suggested);
|
2014-03-16 16:03:20 -04:00
|
|
|
}
|
2014-04-03 16:27:37 -04:00
|
|
|
|
2014-10-03 10:35:11 -04:00
|
|
|
InboundTunnel * TunnelPool::GetNextInboundTunnel (InboundTunnel * suggested) const
|
2014-04-03 16:27:37 -04:00
|
|
|
{
|
2014-10-03 10:35:11 -04:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2014-08-17 14:42:49 -04:00
|
|
|
return GetNextTunnel (m_InboundTunnels, suggested);
|
2014-04-03 16:27:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class TTunnels>
|
2014-08-17 14:42:49 -04:00
|
|
|
typename TTunnels::value_type TunnelPool::GetNextTunnel (TTunnels& tunnels,
|
2014-10-03 10:35:11 -04:00
|
|
|
typename TTunnels::value_type suggested) const
|
2014-04-03 16:27:37 -04:00
|
|
|
{
|
|
|
|
if (tunnels.empty ()) return nullptr;
|
2014-08-27 21:53:44 -04:00
|
|
|
if (suggested && tunnels.count (suggested) > 0 && suggested->IsEstablished ())
|
2014-08-17 14:42:49 -04:00
|
|
|
return suggested;
|
2014-08-27 22:21:29 -04:00
|
|
|
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
|
|
|
uint32_t ind = rnd.GenerateWord32 (0, tunnels.size ()/2), i = 0;
|
|
|
|
typename TTunnels::value_type tunnel = nullptr;
|
2014-04-03 16:27:37 -04:00
|
|
|
for (auto it: tunnels)
|
2014-08-27 22:21:29 -04:00
|
|
|
{
|
2014-08-27 21:53:44 -04:00
|
|
|
if (it->IsEstablished ())
|
2014-08-27 22:21:29 -04:00
|
|
|
{
|
|
|
|
tunnel = it;
|
|
|
|
i++;
|
|
|
|
}
|
2014-08-29 07:44:12 -04:00
|
|
|
if (i > ind && tunnel) break;
|
2014-08-27 22:21:29 -04:00
|
|
|
}
|
|
|
|
return tunnel;
|
2014-04-03 16:27:37 -04:00
|
|
|
}
|
|
|
|
|
2014-03-14 15:13:34 -04:00
|
|
|
void TunnelPool::CreateTunnels ()
|
|
|
|
{
|
2014-08-27 21:53:44 -04:00
|
|
|
int num = 0;
|
2014-10-03 10:35:11 -04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
for (auto it : m_InboundTunnels)
|
|
|
|
if (it->IsEstablished ()) num++;
|
|
|
|
}
|
2014-03-14 20:24:12 -04:00
|
|
|
for (int i = num; i < m_NumTunnels; i++)
|
2014-03-14 15:13:34 -04:00
|
|
|
CreateInboundTunnel ();
|
2014-08-27 21:53:44 -04:00
|
|
|
|
|
|
|
num = 0;
|
2014-10-03 10:35:11 -04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
for (auto it : m_OutboundTunnels)
|
|
|
|
if (it->IsEstablished ()) num++;
|
|
|
|
}
|
2014-03-16 16:03:20 -04:00
|
|
|
for (int i = num; i < m_NumTunnels; i++)
|
|
|
|
CreateOutboundTunnel ();
|
2014-03-14 15:13:34 -04:00
|
|
|
}
|
|
|
|
|
2014-03-17 16:50:03 -04:00
|
|
|
void TunnelPool::TestTunnels ()
|
|
|
|
{
|
|
|
|
auto& rnd = i2p::context.GetRandomNumberGenerator ();
|
|
|
|
for (auto it: m_Tests)
|
|
|
|
{
|
|
|
|
LogPrint ("Tunnel test ", (int)it.first, " failed");
|
2014-07-26 20:56:42 -04:00
|
|
|
// if test failed again with another tunnel we consider it failed
|
2014-07-09 21:43:33 -04:00
|
|
|
if (it.second.first)
|
|
|
|
{
|
2014-07-26 20:56:42 -04:00
|
|
|
if (it.second.first->GetState () == eTunnelStateTestFailed)
|
|
|
|
{
|
|
|
|
it.second.first->SetState (eTunnelStateFailed);
|
2014-10-03 10:35:11 -04:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2014-07-26 20:56:42 -04:00
|
|
|
m_OutboundTunnels.erase (it.second.first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it.second.first->SetState (eTunnelStateTestFailed);
|
2014-07-09 21:43:33 -04:00
|
|
|
}
|
|
|
|
if (it.second.second)
|
|
|
|
{
|
2014-07-26 20:56:42 -04:00
|
|
|
if (it.second.second->GetState () == eTunnelStateTestFailed)
|
|
|
|
{
|
|
|
|
it.second.second->SetState (eTunnelStateFailed);
|
2014-10-03 10:35:11 -04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
m_InboundTunnels.erase (it.second.second);
|
|
|
|
}
|
2014-08-15 19:21:30 -04:00
|
|
|
m_LocalDestination.SetLeaseSetUpdated ();
|
2014-07-26 20:56:42 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
it.second.second->SetState (eTunnelStateTestFailed);
|
2014-07-09 21:43:33 -04:00
|
|
|
}
|
2014-03-17 16: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-21 18:26:11 -04:00
|
|
|
bool failed = false;
|
|
|
|
if ((*it1)->IsFailed ())
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
it1++;
|
|
|
|
}
|
|
|
|
if ((*it2)->IsFailed ())
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
it2++;
|
|
|
|
}
|
|
|
|
if (!failed)
|
2014-07-26 20:56:42 -04:00
|
|
|
{
|
2014-03-21 18:26:11 -04:00
|
|
|
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-17 16: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 ())
|
|
|
|
{
|
2014-07-26 20:56:42 -04:00
|
|
|
// restore from test failed state if any
|
2014-08-27 21:53:44 -04:00
|
|
|
if (it->second.first->GetState () == eTunnelStateTestFailed)
|
|
|
|
it->second.first->SetState (eTunnelStateEstablished);
|
|
|
|
if (it->second.second->GetState () == eTunnelStateTestFailed)
|
|
|
|
it->second.second->SetState (eTunnelStateEstablished);
|
2014-07-27 10:39:38 -04:00
|
|
|
LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - be64toh (deliveryStatus->timestamp), " milliseconds");
|
|
|
|
m_Tests.erase (it);
|
2014-08-15 16:15:17 -04:00
|
|
|
DeleteI2NPMessage (msg);
|
2014-03-17 16:50:03 -04:00
|
|
|
}
|
|
|
|
else
|
2014-10-08 14:17:17 -04:00
|
|
|
m_LocalDestination.ProcessDeliveryStatusMessage (msg);
|
2014-03-17 16:50:03 -04:00
|
|
|
}
|
|
|
|
|
2014-11-21 10:46:11 -05:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const
|
2014-09-25 21:08:20 -04:00
|
|
|
{
|
2014-11-21 10:46:11 -05:00
|
|
|
auto hop = m_NumHops >= 3 ? i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop) :
|
|
|
|
i2p::data::netdb.GetRandomRouter (prevHop);
|
2014-09-25 21:08:20 -04:00
|
|
|
if (!hop)
|
2014-11-21 10:46:11 -05:00
|
|
|
hop = i2p::data::netdb.GetRandomRouter ();
|
2014-09-25 21:08:20 -04:00
|
|
|
return hop;
|
|
|
|
}
|
|
|
|
|
2014-03-14 15:13:34 -04:00
|
|
|
void TunnelPool::CreateInboundTunnel ()
|
|
|
|
{
|
2014-09-02 08:16:46 -04:00
|
|
|
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
|
|
|
|
if (!outboundTunnel)
|
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
2014-03-14 15:13:34 -04:00
|
|
|
LogPrint ("Creating destination inbound tunnel...");
|
2014-11-21 10:46:11 -05:00
|
|
|
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
|
|
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
2014-07-11 07:34:45 -04:00
|
|
|
int numHops = m_NumHops;
|
|
|
|
if (outboundTunnel)
|
|
|
|
{
|
|
|
|
// last hop
|
|
|
|
auto hop = outboundTunnel->GetTunnelConfig ()->GetFirstHop ()->router;
|
2014-10-21 15:44:28 -04:00
|
|
|
if (hop->GetIdentHash () != i2p::context.GetIdentHash ()) // outbound shouldn't be zero-hop tunnel
|
2014-07-11 07:34:45 -04:00
|
|
|
{
|
|
|
|
prevHop = hop;
|
|
|
|
hops.push_back (prevHop);
|
|
|
|
numHops--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < numHops; i++)
|
|
|
|
{
|
2014-09-25 21:08:20 -04:00
|
|
|
auto hop = SelectNextHop (prevHop);
|
2014-07-11 07:34:45 -04:00
|
|
|
prevHop = hop;
|
|
|
|
hops.push_back (hop);
|
|
|
|
}
|
|
|
|
std::reverse (hops.begin (), hops.end ());
|
2014-07-17 07:40:34 -04:00
|
|
|
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel);
|
2014-03-14 20:51:51 -04:00
|
|
|
tunnel->SetTunnelPool (this);
|
2014-03-14 12:35:02 -04:00
|
|
|
}
|
2014-03-16 16:03:20 -04:00
|
|
|
|
2014-08-08 22:44:33 -04:00
|
|
|
void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel)
|
|
|
|
{
|
|
|
|
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
|
|
|
|
if (!outboundTunnel)
|
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
|
|
|
LogPrint ("Re-creating destination inbound tunnel...");
|
|
|
|
auto * newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel);
|
|
|
|
newTunnel->SetTunnelPool (this);
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:03:20 -04:00
|
|
|
void TunnelPool::CreateOutboundTunnel ()
|
|
|
|
{
|
2014-09-02 08:16:46 -04:00
|
|
|
InboundTunnel * inboundTunnel = GetNextInboundTunnel ();
|
|
|
|
if (!inboundTunnel)
|
|
|
|
inboundTunnel = tunnels.GetNextInboundTunnel ();
|
2014-03-16 16:03:20 -04:00
|
|
|
if (inboundTunnel)
|
|
|
|
{
|
|
|
|
LogPrint ("Creating destination outbound tunnel...");
|
2014-06-25 19:28:33 -04:00
|
|
|
|
2014-11-21 10:46:11 -05:00
|
|
|
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
|
|
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
2014-06-25 19:28:33 -04:00
|
|
|
for (int i = 0; i < m_NumHops; i++)
|
|
|
|
{
|
2014-09-25 21:08:20 -04:00
|
|
|
auto hop = SelectNextHop (prevHop);
|
2014-06-25 19:28:33 -04:00
|
|
|
prevHop = hop;
|
|
|
|
hops.push_back (hop);
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:03:20 -04:00
|
|
|
auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
2014-06-25 19:28:33 -04:00
|
|
|
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
|
2014-03-16 16:03:20 -04:00
|
|
|
tunnel->SetTunnelPool (this);
|
|
|
|
}
|
2014-09-14 07:50:01 -04:00
|
|
|
else
|
|
|
|
LogPrint ("Can't create outbound tunnel. No inbound tunnels found");
|
2014-03-16 16:03:20 -04:00
|
|
|
}
|
2014-09-25 21:08:20 -04:00
|
|
|
|
2014-08-08 22:44:33 -04:00
|
|
|
void TunnelPool::RecreateOutboundTunnel (OutboundTunnel * tunnel)
|
|
|
|
{
|
|
|
|
InboundTunnel * inboundTunnel = GetNextInboundTunnel ();
|
|
|
|
if (!inboundTunnel)
|
|
|
|
inboundTunnel = tunnels.GetNextInboundTunnel ();
|
2014-09-14 07:50:01 -04:00
|
|
|
if (inboundTunnel)
|
|
|
|
{
|
|
|
|
LogPrint ("Re-creating destination outbound tunnel...");
|
|
|
|
auto * newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
|
|
|
tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ()));
|
|
|
|
newTunnel->SetTunnelPool (this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found");
|
2014-08-08 22:44:33 -04:00
|
|
|
}
|
2014-03-14 12:35:02 -04:00
|
|
|
}
|
|
|
|
}
|