i2pd/TunnelPool.cpp

355 lines
9.9 KiB
C++
Raw Normal View History

2014-03-17 20:50:03 +00:00
#include "I2PEndian.h"
#include "CryptoConst.h"
2014-03-14 16:35:02 +00:00
#include "Tunnel.h"
#include "NetDb.h"
2014-03-15 00:24:12 +00:00
#include "Timestamp.h"
2014-03-17 20:50:03 +00:00
#include "Garlic.h"
2014-03-14 16:35:02 +00:00
#include "TunnelPool.h"
namespace i2p
{
namespace tunnel
{
2014-12-16 02:24:01 +00:00
TunnelPool::TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numTunnels):
2014-11-30 03:00:52 +00:00
m_LocalDestination (localDestination), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
m_NumTunnels (numTunnels), m_IsActive (true)
2014-03-14 16:35:02 +00:00
{
}
TunnelPool::~TunnelPool ()
2014-10-11 13:47:24 +00:00
{
DetachTunnels ();
}
void TunnelPool::DetachTunnels ()
2014-03-14 16:35:02 +00:00
{
2014-10-06 16:50:36 +00:00
{
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
for (auto it: m_InboundTunnels)
it->SetTunnelPool (nullptr);
m_InboundTunnels.clear ();
2014-10-06 16:50:36 +00:00
}
{
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
for (auto it: m_OutboundTunnels)
it->SetTunnelPool (nullptr);
m_OutboundTunnels.clear ();
2014-10-06 16:50:36 +00:00
}
m_Tests.clear ();
2014-10-11 13:47:24 +00:00
}
2015-01-27 19:55:46 +00:00
void TunnelPool::TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel)
{
if (!m_IsActive) return;
2014-10-03 14:35:11 +00:00
{
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
m_InboundTunnels.insert (createdTunnel);
}
2014-12-16 02:24:01 +00:00
if (m_LocalDestination)
m_LocalDestination->SetLeaseSetUpdated ();
}
2015-01-27 19:55:46 +00:00
void TunnelPool::TunnelExpired (std::shared_ptr<InboundTunnel> expiredTunnel)
2014-03-15 01:22:59 +00:00
{
2014-03-18 12:15:43 +00:00
if (expiredTunnel)
{
expiredTunnel->SetTunnelPool (nullptr);
2014-07-10 01:43:33 +00:00
for (auto it: m_Tests)
if (it.second.second == expiredTunnel) it.second.second = nullptr;
2014-08-09 02:44:33 +00:00
RecreateInboundTunnel (expiredTunnel);
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
m_InboundTunnels.erase (expiredTunnel);
2014-03-18 12:15:43 +00:00
}
2014-03-15 01:22:59 +00:00
}
2014-03-16 20:03:20 +00:00
2015-01-27 19:55:46 +00:00
void TunnelPool::TunnelCreated (std::shared_ptr<OutboundTunnel> createdTunnel)
2014-03-16 20:03:20 +00:00
{
if (!m_IsActive) return;
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
2014-03-16 20:03:20 +00:00
m_OutboundTunnels.insert (createdTunnel);
}
2015-01-27 19:55:46 +00:00
void TunnelPool::TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel)
2014-03-16 20:03:20 +00:00
{
2014-03-18 12:15:43 +00:00
if (expiredTunnel)
2014-03-21 19:54:55 +00:00
{
2014-03-18 12:15:43 +00:00
expiredTunnel->SetTunnelPool (nullptr);
2014-07-10 01:43:33 +00:00
for (auto it: m_Tests)
if (it.second.first == expiredTunnel) it.second.first = nullptr;
2014-08-09 02:44:33 +00:00
RecreateOutboundTunnel (expiredTunnel);
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
m_OutboundTunnels.erase (expiredTunnel);
2014-03-21 19:54:55 +00:00
}
2014-03-16 20:03:20 +00:00
}
2014-03-15 01:22:59 +00:00
2015-01-27 19:55:46 +00:00
std::vector<std::shared_ptr<InboundTunnel> > TunnelPool::GetInboundTunnels (int num) const
{
2015-01-27 19:55:46 +00:00
std::vector<std::shared_ptr<InboundTunnel> > v;
int i = 0;
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
for (auto it : m_InboundTunnels)
{
if (i >= num) break;
2014-08-28 01:53:44 +00:00
if (it->IsEstablished ())
2014-03-21 19:54:55 +00:00
{
v.push_back (it);
i++;
}
}
return v;
}
std::shared_ptr<OutboundTunnel> TunnelPool::GetNextOutboundTunnel () const
2014-03-16 20:03:20 +00:00
{
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
return GetNextTunnel (m_OutboundTunnels);
2014-03-16 20:03:20 +00:00
}
std::shared_ptr<InboundTunnel> TunnelPool::GetNextInboundTunnel () const
{
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
return GetNextTunnel (m_InboundTunnels);
}
template<class TTunnels>
typename TTunnels::value_type TunnelPool::GetNextTunnel (TTunnels& tunnels) const
{
if (tunnels.empty ()) return nullptr;
2014-08-28 02:21:29 +00:00
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
uint32_t ind = rnd.GenerateWord32 (0, tunnels.size ()/2), i = 0;
typename TTunnels::value_type tunnel = nullptr;
for (auto it: tunnels)
2014-08-28 02:21:29 +00:00
{
2014-08-28 01:53:44 +00:00
if (it->IsEstablished ())
2014-08-28 02:21:29 +00:00
{
tunnel = it;
i++;
}
2014-08-29 11:44:12 +00:00
if (i > ind && tunnel) break;
2014-08-28 02:21:29 +00:00
}
return tunnel;
}
void TunnelPool::CreateTunnels ()
{
2014-08-28 01:53:44 +00:00
int num = 0;
2014-10-03 14:35:11 +00:00
{
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
for (auto it : m_InboundTunnels)
if (it->IsEstablished ()) num++;
}
2014-03-15 00:24:12 +00:00
for (int i = num; i < m_NumTunnels; i++)
CreateInboundTunnel ();
2014-08-28 01:53:44 +00:00
num = 0;
2014-10-03 14:35:11 +00:00
{
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
for (auto it : m_OutboundTunnels)
if (it->IsEstablished ()) num++;
}
2014-03-16 20:03:20 +00:00
for (int i = num; i < m_NumTunnels; i++)
CreateOutboundTunnel ();
}
2014-03-17 20:50:03 +00:00
void TunnelPool::TestTunnels ()
{
auto& rnd = i2p::context.GetRandomNumberGenerator ();
for (auto it: m_Tests)
{
LogPrint ("Tunnel test ", (int)it.first, " failed");
2014-07-27 00:56:42 +00:00
// if test failed again with another tunnel we consider it failed
2014-07-10 01:43:33 +00:00
if (it.second.first)
{
2014-07-27 00:56:42 +00:00
if (it.second.first->GetState () == eTunnelStateTestFailed)
{
it.second.first->SetState (eTunnelStateFailed);
2014-10-03 14:35:11 +00:00
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
2014-07-27 00:56:42 +00:00
m_OutboundTunnels.erase (it.second.first);
}
else
it.second.first->SetState (eTunnelStateTestFailed);
2014-07-10 01:43:33 +00:00
}
if (it.second.second)
{
2014-07-27 00:56:42 +00:00
if (it.second.second->GetState () == eTunnelStateTestFailed)
{
it.second.second->SetState (eTunnelStateFailed);
2014-10-03 14:35:11 +00:00
{
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
m_InboundTunnels.erase (it.second.second);
}
2014-12-16 02:24:01 +00:00
if (m_LocalDestination)
m_LocalDestination->SetLeaseSetUpdated ();
2014-07-27 00:56:42 +00:00
}
else
it.second.second->SetState (eTunnelStateTestFailed);
2014-07-10 01:43:33 +00:00
}
2014-03-17 20:50:03 +00:00
}
2014-11-28 21:19:56 +00:00
m_Tests.clear ();
// new tests
2014-03-17 20:50:03 +00:00
auto it1 = m_OutboundTunnels.begin ();
auto it2 = m_InboundTunnels.begin ();
while (it1 != m_OutboundTunnels.end () && it2 != m_InboundTunnels.end ())
{
2014-03-21 22:26:11 +00:00
bool failed = false;
if ((*it1)->IsFailed ())
{
failed = true;
it1++;
}
if ((*it2)->IsFailed ())
{
failed = true;
it2++;
}
if (!failed)
2014-07-27 00:56:42 +00:00
{
2014-12-09 00:33:50 +00:00
uint32_t msgID = rnd.GenerateWord32 ();
m_Tests[msgID] = std::make_pair (*it1, *it2);
(*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
2014-12-09 02:28:11 +00:00
CreateDeliveryStatusMsg (msgID));
2014-03-21 22:26:11 +00:00
it1++; it2++;
}
2014-03-17 20:50:03 +00:00
}
}
2014-12-16 02:24:01 +00:00
void TunnelPool::ProcessGarlicMessage (I2NPMessage * msg)
{
if (m_LocalDestination)
m_LocalDestination->ProcessGarlicMessage (msg);
else
{
LogPrint (eLogWarning, "Local destination doesn't exist. Dropped");
DeleteI2NPMessage (msg);
}
}
2014-03-17 20:50:03 +00:00
void TunnelPool::ProcessDeliveryStatus (I2NPMessage * msg)
{
2014-12-30 20:33:11 +00:00
const uint8_t * buf = msg->GetPayload ();
uint32_t msgID = bufbe32toh (buf);
buf += 4;
uint64_t timestamp = bufbe64toh (buf);
auto it = m_Tests.find (msgID);
2014-03-17 20:50:03 +00:00
if (it != m_Tests.end ())
{
2014-07-27 00:56:42 +00:00
// restore from test failed state if any
2014-08-28 01:53:44 +00:00
if (it->second.first->GetState () == eTunnelStateTestFailed)
it->second.first->SetState (eTunnelStateEstablished);
if (it->second.second->GetState () == eTunnelStateTestFailed)
it->second.second->SetState (eTunnelStateEstablished);
2014-12-30 20:33:11 +00:00
LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - timestamp, " milliseconds");
2014-07-27 14:39:38 +00:00
m_Tests.erase (it);
DeleteI2NPMessage (msg);
2014-03-17 20:50:03 +00:00
}
else
2014-12-16 02:24:01 +00:00
{
if (m_LocalDestination)
m_LocalDestination->ProcessDeliveryStatusMessage (msg);
else
{
LogPrint (eLogWarning, "Local destination doesn't exist. Dropped");
DeleteI2NPMessage (msg);
}
}
2014-03-17 20:50:03 +00:00
}
std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const
{
2014-12-16 02:24:01 +00:00
bool isExploratory = (m_LocalDestination == &i2p::context); // TODO: implement it better
2014-11-30 03:00:52 +00:00
auto hop = isExploratory ? i2p::data::netdb.GetRandomRouter (prevHop):
i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop);
if (!hop)
hop = i2p::data::netdb.GetRandomRouter ();
return hop;
}
void TunnelPool::CreateInboundTunnel ()
{
2015-01-27 19:55:46 +00:00
auto outboundTunnel = GetNextOutboundTunnel ();
2014-09-02 12:16:46 +00:00
if (!outboundTunnel)
outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Creating destination inbound tunnel...");
auto prevHop = i2p::context.GetSharedRouterInfo ();
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
2014-11-30 03:00:52 +00:00
int numHops = m_NumInboundHops;
if (outboundTunnel)
{
// last hop
auto hop = outboundTunnel->GetTunnelConfig ()->GetFirstHop ()->router;
2014-10-21 19:44:28 +00:00
if (hop->GetIdentHash () != i2p::context.GetIdentHash ()) // outbound shouldn't be zero-hop tunnel
{
prevHop = hop;
hops.push_back (prevHop);
numHops--;
}
}
for (int i = 0; i < numHops; i++)
{
auto hop = SelectNextHop (prevHop);
prevHop = hop;
hops.push_back (hop);
}
std::reverse (hops.begin (), hops.end ());
2015-01-27 19:55:46 +00:00
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel);
2015-01-20 03:28:13 +00:00
tunnel->SetTunnelPool (shared_from_this ());
2014-03-14 16:35:02 +00:00
}
2014-03-16 20:03:20 +00:00
2015-01-27 19:55:46 +00:00
void TunnelPool::RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel)
2014-08-09 02:44:33 +00:00
{
2015-01-27 19:55:46 +00:00
auto outboundTunnel = GetNextOutboundTunnel ();
2014-08-09 02:44:33 +00:00
if (!outboundTunnel)
outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Re-creating destination inbound tunnel...");
2015-01-27 19:55:46 +00:00
auto newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel);
2015-01-20 03:28:13 +00:00
newTunnel->SetTunnelPool (shared_from_this());
2014-08-09 02:44:33 +00:00
}
2014-03-16 20:03:20 +00:00
void TunnelPool::CreateOutboundTunnel ()
{
2015-01-27 19:55:46 +00:00
auto inboundTunnel = GetNextInboundTunnel ();
2014-09-02 12:16:46 +00:00
if (!inboundTunnel)
inboundTunnel = tunnels.GetNextInboundTunnel ();
2014-03-16 20:03:20 +00:00
if (inboundTunnel)
{
LogPrint ("Creating destination outbound tunnel...");
auto prevHop = i2p::context.GetSharedRouterInfo ();
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
2014-11-30 03:00:52 +00:00
for (int i = 0; i < m_NumOutboundHops; i++)
{
auto hop = SelectNextHop (prevHop);
prevHop = hop;
hops.push_back (hop);
}
2015-01-27 19:55:46 +00:00
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
2015-01-20 03:28:13 +00:00
tunnel->SetTunnelPool (shared_from_this ());
2014-03-16 20:03:20 +00:00
}
2014-09-14 11:50:01 +00:00
else
LogPrint ("Can't create outbound tunnel. No inbound tunnels found");
2014-03-16 20:03:20 +00:00
}
2015-01-27 19:55:46 +00:00
void TunnelPool::RecreateOutboundTunnel (std::shared_ptr<OutboundTunnel> tunnel)
2014-08-09 02:44:33 +00:00
{
2015-01-27 19:55:46 +00:00
auto inboundTunnel = GetNextInboundTunnel ();
2014-08-09 02:44:33 +00:00
if (!inboundTunnel)
inboundTunnel = tunnels.GetNextInboundTunnel ();
2014-09-14 11:50:01 +00:00
if (inboundTunnel)
{
LogPrint ("Re-creating destination outbound tunnel...");
2015-01-27 19:55:46 +00:00
auto newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
2014-09-14 11:50:01 +00:00
tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ()));
2015-01-20 03:28:13 +00:00
newTunnel->SetTunnelPool (shared_from_this ());
2014-09-14 11:50:01 +00:00
}
else
LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found");
2014-08-09 02:44:33 +00:00
}
2014-03-14 16:35:02 +00:00
}
}