2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2022-01-07 18:39:12 +00:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-06-10 19:32:55 +00:00
|
|
|
#include <algorithm>
|
2020-01-17 06:10:15 +00:00
|
|
|
#include <random>
|
2014-03-17 20:50:03 +00:00
|
|
|
#include "I2PEndian.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Crypto.h"
|
2014-03-14 16:35:02 +00:00
|
|
|
#include "Tunnel.h"
|
2017-04-22 00:04:16 +00:00
|
|
|
#include "NetDb.hpp"
|
2014-03-15 00:24:12 +00:00
|
|
|
#include "Timestamp.h"
|
2014-03-17 20:50:03 +00:00
|
|
|
#include "Garlic.h"
|
2015-05-05 14:33:19 +00:00
|
|
|
#include "Transports.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Log.h"
|
2016-11-01 14:26:40 +00:00
|
|
|
#include "Tunnel.h"
|
2014-03-14 16:35:02 +00:00
|
|
|
#include "TunnelPool.h"
|
2016-10-20 16:14:32 +00:00
|
|
|
#include "Destination.h"
|
2014-03-14 16:35:02 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
2021-07-23 00:58:35 +00:00
|
|
|
void Path::Add (std::shared_ptr<const i2p::data::RouterInfo> r)
|
|
|
|
{
|
|
|
|
if (r)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-07-23 00:58:35 +00:00
|
|
|
peers.push_back (r->GetRouterIdentity ());
|
2021-11-27 20:30:35 +00:00
|
|
|
if (r->GetVersion () < i2p::data::NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION ||
|
2022-05-20 16:56:05 +00:00
|
|
|
r->GetRouterIdentity ()->GetCryptoKeyType () != i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD)
|
2021-07-23 00:58:35 +00:00
|
|
|
isShort = false;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 00:58:35 +00:00
|
|
|
void Path::Reverse ()
|
|
|
|
{
|
|
|
|
std::reverse (peers.begin (), peers.end ());
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:56:05 +00:00
|
|
|
TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels,
|
2022-03-08 03:20:11 +00:00
|
|
|
int numOutboundTunnels, int inboundVariance, int outboundVariance):
|
2015-12-16 19:52:48 +00:00
|
|
|
m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
|
2020-11-16 00:38:34 +00:00
|
|
|
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels),
|
2022-05-20 16:56:05 +00:00
|
|
|
m_InboundVariance (inboundVariance), m_OutboundVariance (outboundVariance),
|
2020-11-16 00:38:34 +00:00
|
|
|
m_IsActive (true), m_CustomPeerSelector(nullptr)
|
2014-03-14 16:35:02 +00:00
|
|
|
{
|
2021-11-27 20:30:35 +00:00
|
|
|
if (m_NumInboundTunnels > TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY)
|
2021-02-11 23:32:03 +00:00
|
|
|
m_NumInboundTunnels = TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY;
|
2021-11-27 20:30:35 +00:00
|
|
|
if (m_NumOutboundTunnels > TUNNEL_POOL_MAX_OUTBOUND_TUNNELS_QUANTITY)
|
|
|
|
m_NumOutboundTunnels = TUNNEL_POOL_MAX_OUTBOUND_TUNNELS_QUANTITY;
|
2022-03-08 03:20:11 +00:00
|
|
|
if (m_InboundVariance < 0 && m_NumInboundHops + m_InboundVariance <= 0)
|
|
|
|
m_InboundVariance = m_NumInboundHops ? -m_NumInboundHops + 1 : 0;
|
|
|
|
if (m_OutboundVariance < 0 && m_NumOutboundHops + m_OutboundVariance <= 0)
|
2022-05-20 16:56:05 +00:00
|
|
|
m_OutboundVariance = m_NumOutboundHops ? -m_NumOutboundHops + 1 : 0;
|
2022-03-08 03:20:11 +00:00
|
|
|
if (m_InboundVariance > 0 && m_NumInboundHops + m_InboundVariance > STANDARD_NUM_RECORDS)
|
|
|
|
m_InboundVariance = (m_NumInboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumInboundHops : 0;
|
|
|
|
if (m_OutboundVariance > 0 && m_NumOutboundHops + m_OutboundVariance > STANDARD_NUM_RECORDS)
|
2022-05-20 16:56:05 +00:00
|
|
|
m_OutboundVariance = (m_NumOutboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumOutboundHops : 0;
|
2020-11-16 00:38:34 +00:00
|
|
|
m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + rand () % TUNNEL_POOL_MANAGE_INTERVAL;
|
2014-03-14 16:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TunnelPool::~TunnelPool ()
|
2014-10-11 13:47:24 +00:00
|
|
|
{
|
|
|
|
DetachTunnels ();
|
|
|
|
}
|
|
|
|
|
2015-06-10 19:32:55 +00:00
|
|
|
void TunnelPool::SetExplicitPeers (std::shared_ptr<std::vector<i2p::data::IdentHash> > explicitPeers)
|
|
|
|
{
|
|
|
|
m_ExplicitPeers = explicitPeers;
|
|
|
|
if (m_ExplicitPeers)
|
|
|
|
{
|
|
|
|
int size = m_ExplicitPeers->size ();
|
2017-04-08 16:51:35 +00:00
|
|
|
if (m_NumInboundHops > size)
|
2015-06-10 19:32:55 +00:00
|
|
|
{
|
|
|
|
m_NumInboundHops = size;
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogInfo, "Tunnels: Inbound tunnel length has beed adjusted to ", size, " for explicit peers");
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
if (m_NumOutboundHops > size)
|
2015-06-10 19:32:55 +00:00
|
|
|
{
|
|
|
|
m_NumOutboundHops = size;
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogInfo, "Tunnels: Outbound tunnel length has beed adjusted to ", size, " for explicit peers");
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2015-06-10 19:32:55 +00:00
|
|
|
m_NumInboundTunnels = 1;
|
|
|
|
m_NumOutboundTunnels = 1;
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2015-06-10 19:32:55 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 13:47:24 +00:00
|
|
|
void TunnelPool::DetachTunnels ()
|
2014-03-14 16:35:02 +00:00
|
|
|
{
|
2014-10-06 16:50:36 +00:00
|
|
|
{
|
2017-04-08 16:51:35 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (auto& it: m_InboundTunnels)
|
2014-10-06 16:50:36 +00:00
|
|
|
it->SetTunnelPool (nullptr);
|
2014-10-13 15:21:57 +00:00
|
|
|
m_InboundTunnels.clear ();
|
2014-10-06 16:50:36 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (auto& it: m_OutboundTunnels)
|
2014-10-06 16:50:36 +00:00
|
|
|
it->SetTunnelPool (nullptr);
|
2014-10-13 15:21:57 +00:00
|
|
|
m_OutboundTunnels.clear ();
|
2014-10-06 16:50:36 +00:00
|
|
|
}
|
2014-10-13 15:21:57 +00:00
|
|
|
m_Tests.clear ();
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
bool TunnelPool::Reconfigure(int inHops, int outHops, int inQuant, int outQuant)
|
|
|
|
{
|
2018-01-02 16:34:58 +00:00
|
|
|
if( inHops >= 0 && outHops >= 0 && inQuant > 0 && outQuant > 0)
|
|
|
|
{
|
|
|
|
m_NumInboundHops = inHops;
|
|
|
|
m_NumOutboundHops = outHops;
|
|
|
|
m_NumInboundTunnels = inQuant;
|
|
|
|
m_NumOutboundTunnels = outQuant;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-01-27 19:55:46 +00:00
|
|
|
void TunnelPool::TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel)
|
2014-03-14 19:13:34 +00:00
|
|
|
{
|
2014-10-13 15:21:57 +00:00
|
|
|
if (!m_IsActive) return;
|
2014-10-03 14:35:11 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2021-09-10 23:57:38 +00:00
|
|
|
if (createdTunnel->IsRecreated ())
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-09-10 23:57:38 +00:00
|
|
|
// find and mark old tunnel as expired
|
|
|
|
createdTunnel->SetRecreated (false);
|
|
|
|
for (auto& it: m_InboundTunnels)
|
|
|
|
if (it->IsRecreated () && it->GetNextIdentHash () == createdTunnel->GetNextIdentHash ())
|
|
|
|
{
|
|
|
|
it->SetState (eTunnelStateExpiring);
|
|
|
|
break;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 14:35:11 +00:00
|
|
|
m_InboundTunnels.insert (createdTunnel);
|
|
|
|
}
|
2014-12-16 02:24:01 +00:00
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->SetLeaseSetUpdated ();
|
2014-03-14 19:13:34 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2016-10-20 16:14:32 +00:00
|
|
|
{
|
2014-03-18 12:15:43 +00:00
|
|
|
expiredTunnel->SetTunnelPool (nullptr);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (auto& it: m_Tests)
|
2014-07-10 01:43:33 +00:00
|
|
|
if (it.second.second == expiredTunnel) it.second.second = nullptr;
|
2014-10-03 14:35:11 +00:00
|
|
|
|
2021-11-01 02:03:34 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2014-10-03 14:35:11 +00:00
|
|
|
m_InboundTunnels.erase (expiredTunnel);
|
2017-04-08 16:51:35 +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
|
|
|
{
|
2014-10-13 15:21:57 +00:00
|
|
|
if (!m_IsActive) return;
|
2015-05-07 20:03:12 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
m_OutboundTunnels.insert (createdTunnel);
|
|
|
|
}
|
2014-03-16 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (auto& it: m_Tests)
|
2014-07-10 01:43:33 +00:00
|
|
|
if (it.second.first == expiredTunnel) it.second.first = nullptr;
|
2014-10-03 14:35:11 +00:00
|
|
|
|
2021-11-01 02:03:34 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2014-10-03 14:35:11 +00:00
|
|
|
m_OutboundTunnels.erase (expiredTunnel);
|
2014-03-21 19:54:55 +00:00
|
|
|
}
|
2014-03-16 20:03:20 +00:00
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2015-01-27 19:55:46 +00:00
|
|
|
std::vector<std::shared_ptr<InboundTunnel> > TunnelPool::GetInboundTunnels (int num) const
|
2014-03-14 19:13:34 +00:00
|
|
|
{
|
2015-01-27 19:55:46 +00:00
|
|
|
std::vector<std::shared_ptr<InboundTunnel> > v;
|
2014-03-14 19:13:34 +00:00
|
|
|
int i = 0;
|
2021-06-11 12:34:56 +00:00
|
|
|
std::shared_ptr<InboundTunnel> slowTunnel;
|
2014-10-03 14:35:11 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (const auto& it : m_InboundTunnels)
|
2014-03-14 19:13:34 +00:00
|
|
|
{
|
|
|
|
if (i >= num) break;
|
2014-08-28 01:53:44 +00:00
|
|
|
if (it->IsEstablished ())
|
2014-03-21 19:54:55 +00:00
|
|
|
{
|
2021-06-11 12:34:56 +00:00
|
|
|
if (it->IsSlow () && !slowTunnel)
|
|
|
|
slowTunnel = it;
|
|
|
|
else
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-06-11 12:34:56 +00:00
|
|
|
v.push_back (it);
|
|
|
|
i++;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 12:34:56 +00:00
|
|
|
if (slowTunnel && (int)v.size () < (num/2+1))
|
|
|
|
v.push_back (slowTunnel);
|
2014-03-14 19:13:34 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-11-05 18:51:24 +00:00
|
|
|
std::shared_ptr<OutboundTunnel> TunnelPool::GetNextOutboundTunnel (std::shared_ptr<OutboundTunnel> excluded,
|
|
|
|
i2p::data::RouterInfo::CompatibleTransports compatible) const
|
2014-03-16 20:03:20 +00:00
|
|
|
{
|
2016-11-15 15:46:58 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2021-11-05 18:51:24 +00:00
|
|
|
return GetNextTunnel (m_OutboundTunnels, excluded, compatible);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-04-03 20:27:37 +00:00
|
|
|
|
2021-11-05 18:51:24 +00:00
|
|
|
std::shared_ptr<InboundTunnel> TunnelPool::GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded,
|
|
|
|
i2p::data::RouterInfo::CompatibleTransports compatible) const
|
2014-04-03 20:27:37 +00:00
|
|
|
{
|
2016-11-15 15:46:58 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2021-11-05 18:51:24 +00:00
|
|
|
return GetNextTunnel (m_InboundTunnels, excluded, compatible);
|
2014-04-03 20:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class TTunnels>
|
2021-11-27 20:30:35 +00:00
|
|
|
typename TTunnels::value_type TunnelPool::GetNextTunnel (TTunnels& tunnels,
|
2021-11-05 18:51:24 +00:00
|
|
|
typename TTunnels::value_type excluded, i2p::data::RouterInfo::CompatibleTransports compatible) const
|
2014-04-03 20:27:37 +00:00
|
|
|
{
|
2017-04-08 16:51:35 +00:00
|
|
|
if (tunnels.empty ()) return nullptr;
|
2015-11-03 14:15:49 +00:00
|
|
|
uint32_t ind = rand () % (tunnels.size ()/2 + 1), i = 0;
|
2021-06-04 16:16:50 +00:00
|
|
|
bool skipped = false;
|
2014-08-28 02:21:29 +00:00
|
|
|
typename TTunnels::value_type tunnel = nullptr;
|
2016-08-09 22:16:24 +00:00
|
|
|
for (const auto& it: tunnels)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2021-11-05 18:51:24 +00:00
|
|
|
if (it->IsEstablished () && it != excluded && (compatible & it->GetFarEndTransports ()))
|
2014-08-28 02:21:29 +00:00
|
|
|
{
|
2021-11-27 20:30:35 +00:00
|
|
|
if (it->IsSlow () || (HasLatencyRequirement() && it->LatencyIsKnown() &&
|
2022-05-20 16:56:05 +00:00
|
|
|
!it->LatencyFitsRange(m_MinLatency, m_MaxLatency)))
|
2021-06-04 16:16:50 +00:00
|
|
|
{
|
|
|
|
i++; skipped = true;
|
2016-11-15 15:46:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-28 02:21:29 +00:00
|
|
|
tunnel = it;
|
|
|
|
i++;
|
|
|
|
}
|
2014-08-29 11:44:12 +00:00
|
|
|
if (i > ind && tunnel) break;
|
2015-03-21 20:26:14 +00:00
|
|
|
}
|
2021-11-27 20:30:35 +00:00
|
|
|
if (!tunnel && skipped)
|
2021-06-04 16:16:50 +00:00
|
|
|
{
|
2016-11-15 16:27:00 +00:00
|
|
|
ind = rand () % (tunnels.size ()/2 + 1), i = 0;
|
|
|
|
for (const auto& it: tunnels)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2016-11-15 16:27:00 +00:00
|
|
|
if (it->IsEstablished () && it != excluded)
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
tunnel = it;
|
|
|
|
i++;
|
2016-11-15 16:27:00 +00:00
|
|
|
}
|
|
|
|
if (i > ind && tunnel) break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-21 20:26:14 +00:00
|
|
|
if (!tunnel && excluded && excluded->IsEstablished ()) tunnel = excluded;
|
2014-08-28 02:21:29 +00:00
|
|
|
return tunnel;
|
2014-04-03 20:27:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:11:51 +00:00
|
|
|
std::shared_ptr<OutboundTunnel> TunnelPool::GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const
|
|
|
|
{
|
|
|
|
if (old && old->IsEstablished ()) return old;
|
2017-04-08 16:51:35 +00:00
|
|
|
std::shared_ptr<OutboundTunnel> tunnel;
|
2015-04-17 14:11:51 +00:00
|
|
|
if (old)
|
|
|
|
{
|
2017-04-08 16:51:35 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (const auto& it: m_OutboundTunnels)
|
2015-11-03 14:15:49 +00:00
|
|
|
if (it->IsEstablished () && old->GetEndpointIdentHash () == it->GetEndpointIdentHash ())
|
2015-04-17 15:36:42 +00:00
|
|
|
{
|
2015-04-17 14:11:51 +00:00
|
|
|
tunnel = it;
|
2015-04-17 15:36:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-04-17 14:11:51 +00:00
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2015-04-17 14:11:51 +00:00
|
|
|
if (!tunnel)
|
2017-04-08 16:51:35 +00:00
|
|
|
tunnel = GetNextOutboundTunnel ();
|
2015-04-17 14:11:51 +00:00
|
|
|
return tunnel;
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:13:34 +00:00
|
|
|
void TunnelPool::CreateTunnels ()
|
|
|
|
{
|
2014-08-28 01:53:44 +00:00
|
|
|
int num = 0;
|
2014-10-03 14:35:11 +00:00
|
|
|
{
|
2021-11-01 02:03:34 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2016-08-09 22:16:24 +00:00
|
|
|
for (const auto& it : m_OutboundTunnels)
|
2014-10-03 14:35:11 +00:00
|
|
|
if (it->IsEstablished ()) num++;
|
|
|
|
}
|
2015-05-05 16:32:13 +00:00
|
|
|
for (int i = num; i < m_NumOutboundTunnels; i++)
|
2017-04-08 16:51:35 +00:00
|
|
|
CreateOutboundTunnel ();
|
2016-11-16 15:59:11 +00:00
|
|
|
|
|
|
|
num = 0;
|
|
|
|
{
|
2021-11-01 02:03:34 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2016-11-16 15:59:11 +00:00
|
|
|
for (const auto& it : m_InboundTunnels)
|
|
|
|
if (it->IsEstablished ()) num++;
|
|
|
|
}
|
2020-12-05 13:26:21 +00:00
|
|
|
if (!num && !m_OutboundTunnels.empty () && m_NumOutboundHops > 0)
|
2020-11-15 22:02:01 +00:00
|
|
|
{
|
|
|
|
for (auto it: m_OutboundTunnels)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2020-11-15 22:02:01 +00:00
|
|
|
CreatePairedInboundTunnel (it);
|
|
|
|
num++;
|
|
|
|
if (num >= m_NumInboundTunnels) break;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-16 15:59:11 +00:00
|
|
|
for (int i = num; i < m_NumInboundTunnels; i++)
|
|
|
|
CreateInboundTunnel ();
|
|
|
|
|
2016-11-17 00:32:45 +00:00
|
|
|
if (num < m_NumInboundTunnels && m_NumInboundHops <= 0 && m_LocalDestination) // zero hops IB
|
2016-11-16 15:59:11 +00:00
|
|
|
m_LocalDestination->SetLeaseSetUpdated (); // update LeaseSet immediately
|
2014-03-14 19:13:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 20:50:03 +00:00
|
|
|
void TunnelPool::TestTunnels ()
|
|
|
|
{
|
2016-02-16 21:10:22 +00:00
|
|
|
decltype(m_Tests) tests;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_TestsMutex);
|
2016-08-09 22:16:24 +00:00
|
|
|
tests.swap(m_Tests);
|
2016-02-16 21:10:22 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 22:16:24 +00:00
|
|
|
for (auto& it: tests)
|
2014-03-17 20:50:03 +00:00
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogWarning, "Tunnels: Test of tunnel ", 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)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2014-07-27 00:56:42 +00:00
|
|
|
if (it.second.first->GetState () == eTunnelStateTestFailed)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2014-07-27 00:56:42 +00:00
|
|
|
it.second.first->SetState (eTunnelStateFailed);
|
2021-11-01 02:03:34 +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);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
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)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2014-07-27 00:56:42 +00:00
|
|
|
it.second.second->SetState (eTunnelStateFailed);
|
2014-10-03 14:35:11 +00:00
|
|
|
{
|
2021-11-01 02:03:34 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2014-10-03 14:35:11 +00:00
|
|
|
m_InboundTunnels.erase (it.second.second);
|
|
|
|
}
|
2014-12-16 02:24:01 +00:00
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->SetLeaseSetUpdated ();
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-07-27 00:56:42 +00:00
|
|
|
else
|
|
|
|
it.second.second->SetState (eTunnelStateTestFailed);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-03-17 20:50:03 +00:00
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
|
|
|
|
// new tests
|
2022-01-07 18:39:12 +00:00
|
|
|
std::unique_lock<std::mutex> l1(m_OutboundTunnelsMutex);
|
2014-03-17 20:50:03 +00:00
|
|
|
auto it1 = m_OutboundTunnels.begin ();
|
2022-01-07 18:39:12 +00:00
|
|
|
std::unique_lock<std::mutex> l2(m_InboundTunnelsMutex);
|
2014-03-17 20:50:03 +00:00
|
|
|
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 ())
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2014-03-21 22:26:11 +00:00
|
|
|
failed = true;
|
2016-08-09 22:16:24 +00:00
|
|
|
++it1;
|
2014-03-21 22:26:11 +00:00
|
|
|
}
|
|
|
|
if ((*it2)->IsFailed ())
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2014-03-21 22:26:11 +00:00
|
|
|
failed = true;
|
2016-08-09 22:16:24 +00:00
|
|
|
++it2;
|
2014-03-21 22:26:11 +00:00
|
|
|
}
|
|
|
|
if (!failed)
|
2014-07-27 00:56:42 +00:00
|
|
|
{
|
2018-01-06 04:01:44 +00:00
|
|
|
uint32_t msgID;
|
2015-11-03 14:15:49 +00:00
|
|
|
RAND_bytes ((uint8_t *)&msgID, 4);
|
2016-02-16 21:10:22 +00:00
|
|
|
{
|
2021-11-01 02:03:34 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_TestsMutex);
|
2018-01-06 04:01:44 +00:00
|
|
|
m_Tests[msgID] = std::make_pair (*it1, *it2);
|
2016-02-16 21:10:22 +00:00
|
|
|
}
|
2018-01-06 04:01:44 +00:00
|
|
|
(*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
|
2015-06-24 14:45:58 +00:00
|
|
|
CreateDeliveryStatusMsg (msgID));
|
2016-08-09 22:16:24 +00:00
|
|
|
++it1; ++it2;
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-03-17 20:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 00:38:34 +00:00
|
|
|
void TunnelPool::ManageTunnels (uint64_t ts)
|
|
|
|
{
|
2022-04-27 01:02:39 +00:00
|
|
|
if (ts > m_NextManageTime || ts + 2*TUNNEL_POOL_MANAGE_INTERVAL < m_NextManageTime) // in case if clock was adjusted
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2020-11-16 00:38:34 +00:00
|
|
|
CreateTunnels ();
|
|
|
|
TestTunnels ();
|
2021-11-27 20:30:35 +00:00
|
|
|
m_NextManageTime = ts + TUNNEL_POOL_MANAGE_INTERVAL + (rand () % TUNNEL_POOL_MANAGE_INTERVAL)/2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:14:14 +00:00
|
|
|
void TunnelPool::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-12-16 02:24:01 +00:00
|
|
|
{
|
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->ProcessGarlicMessage (msg);
|
|
|
|
else
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogWarning, "Tunnels: Local destination doesn't exist, dropped");
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 14:14:14 +00:00
|
|
|
void TunnelPool::ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg)
|
2014-03-17 20:50:03 +00:00
|
|
|
{
|
2014-12-30 20:33:11 +00:00
|
|
|
const uint8_t * buf = msg->GetPayload ();
|
|
|
|
uint32_t msgID = bufbe32toh (buf);
|
2017-04-08 16:51:35 +00:00
|
|
|
buf += 4;
|
2014-12-30 20:33:11 +00:00
|
|
|
uint64_t timestamp = bufbe64toh (buf);
|
|
|
|
|
2016-02-16 21:10:22 +00:00
|
|
|
decltype(m_Tests)::mapped_type test;
|
2017-04-08 16:51:35 +00:00
|
|
|
bool found = false;
|
2016-02-16 21:10:22 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_TestsMutex);
|
|
|
|
auto it = m_Tests.find (msgID);
|
|
|
|
if (it != m_Tests.end ())
|
|
|
|
{
|
|
|
|
found = true;
|
2017-04-08 16:51:35 +00:00
|
|
|
test = it->second;
|
2016-02-16 21:10:22 +00:00
|
|
|
m_Tests.erase (it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
2014-03-17 20:50:03 +00:00
|
|
|
{
|
2016-11-15 15:20:09 +00:00
|
|
|
uint64_t dlt = i2p::util::GetMillisecondsSinceEpoch () - timestamp;
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Test of ", msgID, " successful. ", dlt, " milliseconds");
|
2022-03-08 03:20:11 +00:00
|
|
|
int numHops = 0;
|
|
|
|
if (test.first) numHops += test.first->GetNumHops ();
|
2022-05-20 16:56:05 +00:00
|
|
|
if (test.second) numHops += test.second->GetNumHops ();
|
2020-11-16 17:56:22 +00:00
|
|
|
// restore from test failed state if any
|
|
|
|
if (test.first)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2020-11-16 17:56:22 +00:00
|
|
|
if (test.first->GetState () == eTunnelStateTestFailed)
|
|
|
|
test.first->SetState (eTunnelStateEstablished);
|
|
|
|
// update latency
|
2022-03-08 03:20:11 +00:00
|
|
|
uint64_t latency = 0;
|
2022-05-20 16:56:05 +00:00
|
|
|
if (numHops) latency = dlt*test.first->GetNumHops ()/numHops;
|
2022-03-08 03:20:11 +00:00
|
|
|
if (!latency) latency = dlt/2;
|
2020-11-16 17:56:22 +00:00
|
|
|
test.first->AddLatencySample(latency);
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2020-11-16 17:56:22 +00:00
|
|
|
if (test.second)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2020-11-16 17:56:22 +00:00
|
|
|
if (test.second->GetState () == eTunnelStateTestFailed)
|
|
|
|
test.second->SetState (eTunnelStateEstablished);
|
|
|
|
// update latency
|
2022-03-08 03:20:11 +00:00
|
|
|
uint64_t latency = 0;
|
2022-05-20 16:56:05 +00:00
|
|
|
if (numHops) latency = dlt*test.second->GetNumHops ()/numHops;
|
2022-03-08 03:20:11 +00:00
|
|
|
if (!latency) latency = dlt/2;
|
2020-11-16 17:56:22 +00:00
|
|
|
test.second->AddLatencySample(latency);
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
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
|
2021-11-01 02:03:34 +00:00
|
|
|
LogPrint (eLogWarning, "Tunnels: Local destination doesn't exist, dropped");
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-03-17 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 16:11:24 +00:00
|
|
|
bool TunnelPool::IsExploratory () const
|
|
|
|
{
|
|
|
|
return i2p::tunnel::tunnels.GetExploratoryPool () == shared_from_this ();
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:02:27 +00:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop, bool reverse) const
|
2014-09-26 01:08:20 +00:00
|
|
|
{
|
2021-05-28 16:11:24 +00:00
|
|
|
auto hop = IsExploratory () ? i2p::data::netdb.GetRandomRouter (prevHop, reverse):
|
2021-03-02 00:02:27 +00:00
|
|
|
i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop, reverse);
|
2015-05-04 17:01:27 +00:00
|
|
|
|
|
|
|
if (!hop || hop->GetProfile ()->IsBad ())
|
2021-03-02 00:02:27 +00:00
|
|
|
hop = i2p::data::netdb.GetRandomRouter (prevHop, reverse);
|
2017-04-08 16:51:35 +00:00
|
|
|
return hop;
|
|
|
|
}
|
2015-06-07 12:37:34 +00:00
|
|
|
|
2021-07-23 00:58:35 +00:00
|
|
|
bool StandardSelectPeers(Path & path, int numHops, bool inbound, SelectHopFunc nextHop)
|
2014-03-14 19:13:34 +00:00
|
|
|
{
|
2021-03-16 19:23:00 +00:00
|
|
|
int start = 0;
|
2021-07-14 18:46:56 +00:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> prevHop = i2p::context.GetSharedRouterInfo ();
|
2016-06-29 17:32:39 +00:00
|
|
|
if(i2p::transport::transports.RoutesRestricted())
|
2015-06-07 12:37:34 +00:00
|
|
|
{
|
2016-06-29 17:32:39 +00:00
|
|
|
/** if routes are restricted prepend trusted first hop */
|
|
|
|
auto hop = i2p::transport::transports.GetRestrictedPeer();
|
|
|
|
if(!hop) return false;
|
2021-07-23 00:58:35 +00:00
|
|
|
path.Add (hop);
|
2016-06-29 17:32:39 +00:00
|
|
|
prevHop = hop;
|
2021-03-16 19:23:00 +00:00
|
|
|
start++;
|
2014-07-11 11:34:45 +00:00
|
|
|
}
|
2021-03-16 22:45:51 +00:00
|
|
|
else if (i2p::transport::transports.GetNumPeers () > 100 ||
|
|
|
|
(inbound && i2p::transport::transports.GetNumPeers () > 25))
|
2016-07-07 02:34:24 +00:00
|
|
|
{
|
|
|
|
auto r = i2p::transport::transports.GetRandomPeer ();
|
2021-11-27 20:30:35 +00:00
|
|
|
if (r && r->IsECIES () && !r->GetProfile ()->IsBad () &&
|
2021-04-13 13:16:52 +00:00
|
|
|
(numHops > 1 || (r->IsV4 () && (!inbound || r->IsReachable ())))) // first inbound must be reachable
|
2017-04-08 19:15:59 +00:00
|
|
|
{
|
|
|
|
prevHop = r;
|
2021-07-23 00:58:35 +00:00
|
|
|
path.Add (r);
|
2021-03-16 19:23:00 +00:00
|
|
|
start++;
|
2017-04-08 19:15:59 +00:00
|
|
|
}
|
2016-07-07 02:34:24 +00:00
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2021-03-16 19:23:00 +00:00
|
|
|
for(int i = start; i < numHops; i++ )
|
2014-07-11 11:34:45 +00:00
|
|
|
{
|
2021-03-02 00:02:27 +00:00
|
|
|
auto hop = nextHop (prevHop, inbound);
|
2021-03-02 17:29:51 +00:00
|
|
|
if (!hop && !i) // if no suitable peer found for first hop, try already connected
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-03-02 17:29:51 +00:00
|
|
|
LogPrint (eLogInfo, "Tunnels: Can't select first hop for a tunnel. Trying already connected");
|
|
|
|
hop = i2p::transport::transports.GetRandomPeer ();
|
2021-09-05 13:08:29 +00:00
|
|
|
if (hop && !hop->IsECIES ()) hop = nullptr;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2015-04-03 14:02:45 +00:00
|
|
|
if (!hop)
|
|
|
|
{
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogError, "Tunnels: Can't select next hop for ", prevHop->GetIdentHashBase64 ());
|
2015-06-07 12:37:34 +00:00
|
|
|
return false;
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2021-04-13 13:16:52 +00:00
|
|
|
if ((i == numHops - 1) && (!hop->IsV4 () || // doesn't support ipv4
|
2022-05-20 16:56:05 +00:00
|
|
|
(inbound && !hop->IsReachable ()))) // IBGW is not reachable
|
2021-01-28 18:33:12 +00:00
|
|
|
{
|
2021-03-02 00:02:27 +00:00
|
|
|
auto hop1 = nextHop (prevHop, true);
|
2021-01-28 18:33:12 +00:00
|
|
|
if (hop1) hop = hop1;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2014-07-11 11:34:45 +00:00
|
|
|
prevHop = hop;
|
2021-07-23 00:58:35 +00:00
|
|
|
path.Add (hop);
|
2016-06-17 15:03:33 +00:00
|
|
|
}
|
2021-11-06 19:44:56 +00:00
|
|
|
path.farEndTransports = prevHop->GetCompatibleTransports (inbound); // last hop
|
2015-06-07 12:37:34 +00:00
|
|
|
return true;
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 00:58:35 +00:00
|
|
|
bool TunnelPool::SelectPeers (Path& path, bool isInbound)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2022-03-08 03:20:11 +00:00
|
|
|
// explicit peers in use
|
|
|
|
if (m_ExplicitPeers) return SelectExplicitPeers (path, isInbound);
|
|
|
|
// calculate num hops
|
|
|
|
int numHops;
|
|
|
|
if (isInbound)
|
2022-05-20 16:56:05 +00:00
|
|
|
{
|
|
|
|
numHops = m_NumInboundHops;
|
2022-03-08 03:20:11 +00:00
|
|
|
if (m_InboundVariance)
|
|
|
|
{
|
|
|
|
int offset = rand () % (std::abs (m_InboundVariance) + 1);
|
|
|
|
if (m_InboundVariance < 0) offset = -offset;
|
|
|
|
numHops += offset;
|
2022-05-20 16:56:05 +00:00
|
|
|
}
|
2022-03-08 03:20:11 +00:00
|
|
|
}
|
|
|
|
else
|
2022-05-20 16:56:05 +00:00
|
|
|
{
|
2022-03-08 03:20:11 +00:00
|
|
|
numHops = m_NumOutboundHops;
|
|
|
|
if (m_OutboundVariance)
|
|
|
|
{
|
|
|
|
int offset = rand () % (std::abs (m_OutboundVariance) + 1);
|
|
|
|
if (m_OutboundVariance < 0) offset = -offset;
|
|
|
|
numHops += offset;
|
2022-05-20 16:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
// peers is empty
|
|
|
|
if (numHops <= 0) return true;
|
|
|
|
// custom peer selector in use ?
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_CustomPeerSelectorMutex);
|
|
|
|
if (m_CustomPeerSelector)
|
2021-07-23 00:58:35 +00:00
|
|
|
return m_CustomPeerSelector->SelectPeers(path, numHops, isInbound);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2021-07-23 00:58:35 +00:00
|
|
|
return StandardSelectPeers(path, numHops, isInbound, std::bind(&TunnelPool::SelectNextHop, this, std::placeholders::_1, std::placeholders::_2));
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 00:58:35 +00:00
|
|
|
bool TunnelPool::SelectExplicitPeers (Path& path, bool isInbound)
|
2015-06-10 19:32:55 +00:00
|
|
|
{
|
2017-04-08 16:51:35 +00:00
|
|
|
int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
|
2021-07-28 23:08:55 +00:00
|
|
|
if (numHops > (int)m_ExplicitPeers->size ()) numHops = m_ExplicitPeers->size ();
|
2021-11-27 20:30:35 +00:00
|
|
|
if (!numHops) return false;
|
2017-04-08 16:51:35 +00:00
|
|
|
for (int i = 0; i < numHops; i++)
|
2015-06-10 19:32:55 +00:00
|
|
|
{
|
2021-07-28 23:08:55 +00:00
|
|
|
auto& ident = (*m_ExplicitPeers)[i];
|
2015-06-10 19:32:55 +00:00
|
|
|
auto r = i2p::data::netdb.FindRouter (ident);
|
|
|
|
if (r)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
|
|
|
if (r->IsECIES ())
|
|
|
|
{
|
2021-09-03 17:30:01 +00:00
|
|
|
path.Add (r);
|
2021-10-05 23:38:33 +00:00
|
|
|
if (i == numHops - 1)
|
|
|
|
path.farEndTransports = r->GetCompatibleTransports (isInbound);
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2021-09-03 17:30:01 +00:00
|
|
|
else
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-09-03 17:30:01 +00:00
|
|
|
LogPrint (eLogError, "Tunnels: ElGamal router ", ident.ToBase64 (), " is not supported");
|
2021-11-27 20:30:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 19:32:55 +00:00
|
|
|
else
|
|
|
|
{
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogInfo, "Tunnels: Can't find router for ", ident.ToBase64 ());
|
2015-06-10 19:32:55 +00:00
|
|
|
i2p::data::netdb.RequestDestination (ident);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2015-06-07 12:37:34 +00:00
|
|
|
void TunnelPool::CreateInboundTunnel ()
|
|
|
|
{
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Creating destination inbound tunnel...");
|
2021-07-23 00:58:35 +00:00
|
|
|
Path path;
|
|
|
|
if (SelectPeers (path, true))
|
2015-06-07 12:37:34 +00:00
|
|
|
{
|
2021-11-06 19:44:56 +00:00
|
|
|
auto outboundTunnel = GetNextOutboundTunnel (nullptr, path.farEndTransports);
|
|
|
|
if (!outboundTunnel)
|
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
2016-06-30 01:37:17 +00:00
|
|
|
std::shared_ptr<TunnelConfig> config;
|
|
|
|
if (m_NumInboundHops > 0)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2021-07-23 00:58:35 +00:00
|
|
|
path.Reverse ();
|
2021-10-05 23:38:33 +00:00
|
|
|
config = std::make_shared<TunnelConfig> (path.peers, path.isShort, path.farEndTransports);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2021-07-21 17:08:12 +00:00
|
|
|
auto tunnel = tunnels.CreateInboundTunnel (config, shared_from_this (), outboundTunnel);
|
2016-06-29 15:26:46 +00:00
|
|
|
if (tunnel->IsEstablished ()) // zero hops
|
|
|
|
TunnelCreated (tunnel);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2015-06-07 12:37:34 +00:00
|
|
|
else
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogError, "Tunnels: Can't create inbound tunnel, no peers available");
|
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
|
|
|
{
|
2021-06-02 18:45:21 +00:00
|
|
|
if (IsExploratory () || tunnel->IsSlow ()) // always create new exploratory tunnel or if slow
|
2021-05-28 16:11:24 +00:00
|
|
|
{
|
|
|
|
CreateInboundTunnel ();
|
|
|
|
return;
|
|
|
|
}
|
2021-11-06 19:44:56 +00:00
|
|
|
auto outboundTunnel = GetNextOutboundTunnel (nullptr, tunnel->GetFarEndTransports ());
|
2014-08-09 02:44:33 +00:00
|
|
|
if (!outboundTunnel)
|
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Re-creating destination inbound tunnel...");
|
2016-06-30 16:24:26 +00:00
|
|
|
std::shared_ptr<TunnelConfig> config;
|
2018-02-26 13:18:01 +00:00
|
|
|
if (m_NumInboundHops > 0 && tunnel->GetPeers().size())
|
2021-11-06 23:16:45 +00:00
|
|
|
config = std::make_shared<TunnelConfig>(tunnel->GetPeers (), tunnel->IsShortBuildMessage (), tunnel->GetFarEndTransports ());
|
2021-07-21 17:08:12 +00:00
|
|
|
if (!m_NumInboundHops || config)
|
2018-01-02 17:59:16 +00:00
|
|
|
{
|
2021-07-21 17:08:12 +00:00
|
|
|
auto newTunnel = tunnels.CreateInboundTunnel (config, shared_from_this(), outboundTunnel);
|
2018-01-02 17:59:16 +00:00
|
|
|
if (newTunnel->IsEstablished ()) // zero hops
|
|
|
|
TunnelCreated (newTunnel);
|
2021-09-10 23:57:38 +00:00
|
|
|
else
|
|
|
|
newTunnel->SetRecreated (true);
|
2018-01-02 17:59:16 +00:00
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 20:03:20 +00:00
|
|
|
void TunnelPool::CreateOutboundTunnel ()
|
|
|
|
{
|
2021-11-06 19:44:56 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Creating destination outbound tunnel...");
|
|
|
|
Path path;
|
|
|
|
if (SelectPeers (path, false))
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2021-11-06 19:44:56 +00:00
|
|
|
auto inboundTunnel = GetNextInboundTunnel (nullptr, path.farEndTransports);
|
|
|
|
if (!inboundTunnel)
|
|
|
|
inboundTunnel = tunnels.GetNextInboundTunnel ();
|
|
|
|
if (!inboundTunnel)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2021-11-06 19:44:56 +00:00
|
|
|
LogPrint (eLogError, "Tunnels: Can't create outbound tunnel, no inbound tunnels found");
|
|
|
|
return;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 19:44:56 +00:00
|
|
|
if (m_LocalDestination && !m_LocalDestination->SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))
|
|
|
|
path.isShort = false; // because can't handle ECIES encrypted reply
|
2021-11-27 20:30:35 +00:00
|
|
|
|
2021-11-06 19:44:56 +00:00
|
|
|
std::shared_ptr<TunnelConfig> config;
|
|
|
|
if (m_NumOutboundHops > 0)
|
2021-11-27 20:30:35 +00:00
|
|
|
config = std::make_shared<TunnelConfig>(path.peers, inboundTunnel->GetNextTunnelID (),
|
2021-11-06 19:44:56 +00:00
|
|
|
inboundTunnel->GetNextIdentHash (), path.isShort, path.farEndTransports);
|
|
|
|
|
|
|
|
std::shared_ptr<OutboundTunnel> tunnel;
|
|
|
|
if (path.isShort)
|
|
|
|
{
|
|
|
|
// TODO: implement it better
|
|
|
|
tunnel = tunnels.CreateOutboundTunnel (config, inboundTunnel->GetTunnelPool ());
|
|
|
|
tunnel->SetTunnelPool (shared_from_this ());
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
else
|
2021-11-06 19:44:56 +00:00
|
|
|
tunnel = tunnels.CreateOutboundTunnel (config, shared_from_this ());
|
|
|
|
if (tunnel && tunnel->IsEstablished ()) // zero hops
|
|
|
|
TunnelCreated (tunnel);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-09-14 11:50:01 +00:00
|
|
|
else
|
2021-11-06 19:44:56 +00:00
|
|
|
LogPrint (eLogError, "Tunnels: Can't create outbound tunnel, no peers available");
|
2017-04-08 16:51:35 +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
|
|
|
{
|
2021-06-02 18:45:21 +00:00
|
|
|
if (IsExploratory () || tunnel->IsSlow ()) // always create new exploratory tunnel or if slow
|
2021-05-28 16:11:24 +00:00
|
|
|
{
|
|
|
|
CreateOutboundTunnel ();
|
|
|
|
return;
|
|
|
|
}
|
2021-11-06 19:44:56 +00:00
|
|
|
auto inboundTunnel = GetNextInboundTunnel (nullptr, tunnel->GetFarEndTransports ());
|
2014-08-09 02:44:33 +00:00
|
|
|
if (!inboundTunnel)
|
|
|
|
inboundTunnel = tunnels.GetNextInboundTunnel ();
|
2014-09-14 11:50:01 +00:00
|
|
|
if (inboundTunnel)
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Re-creating destination outbound tunnel...");
|
2016-06-30 16:24:26 +00:00
|
|
|
std::shared_ptr<TunnelConfig> config;
|
2018-02-26 13:18:01 +00:00
|
|
|
if (m_NumOutboundHops > 0 && tunnel->GetPeers().size())
|
2018-01-02 17:59:16 +00:00
|
|
|
{
|
2021-11-27 20:30:35 +00:00
|
|
|
config = std::make_shared<TunnelConfig>(tunnel->GetPeers (), inboundTunnel->GetNextTunnelID (),
|
2021-11-06 23:16:45 +00:00
|
|
|
inboundTunnel->GetNextIdentHash (), inboundTunnel->IsShortBuildMessage (), tunnel->GetFarEndTransports ());
|
2018-01-02 17:59:16 +00:00
|
|
|
}
|
2020-12-05 13:26:21 +00:00
|
|
|
if (!m_NumOutboundHops || config)
|
2018-01-02 17:59:16 +00:00
|
|
|
{
|
2021-07-21 17:08:12 +00:00
|
|
|
auto newTunnel = tunnels.CreateOutboundTunnel (config, shared_from_this ());
|
2018-01-02 17:59:16 +00:00
|
|
|
if (newTunnel->IsEstablished ()) // zero hops
|
|
|
|
TunnelCreated (newTunnel);
|
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2014-09-14 11:50:01 +00:00
|
|
|
else
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Can't re-create outbound tunnel, no inbound tunnels found");
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
2015-05-07 20:03:12 +00:00
|
|
|
|
|
|
|
void TunnelPool::CreatePairedInboundTunnel (std::shared_ptr<OutboundTunnel> outboundTunnel)
|
|
|
|
{
|
2015-12-18 11:48:22 +00:00
|
|
|
LogPrint (eLogDebug, "Tunnels: Creating paired inbound tunnel...");
|
2020-12-05 13:26:21 +00:00
|
|
|
auto tunnel = tunnels.CreateInboundTunnel (
|
2021-10-05 23:38:33 +00:00
|
|
|
m_NumOutboundHops > 0 ? std::make_shared<TunnelConfig>(outboundTunnel->GetInvertedPeers (),
|
2021-11-27 20:30:35 +00:00
|
|
|
outboundTunnel->IsShortBuildMessage ()) : nullptr,
|
2022-05-20 16:56:05 +00:00
|
|
|
shared_from_this (), outboundTunnel);
|
2020-12-05 13:26:21 +00:00
|
|
|
if (tunnel->IsEstablished ()) // zero hops
|
|
|
|
TunnelCreated (tunnel);
|
2016-08-30 19:11:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 16:51:35 +00:00
|
|
|
void TunnelPool::SetCustomPeerSelector(ITunnelPeerSelector * selector)
|
2016-08-30 19:11:39 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_CustomPeerSelectorMutex);
|
|
|
|
m_CustomPeerSelector = selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelPool::UnsetCustomPeerSelector()
|
|
|
|
{
|
|
|
|
SetCustomPeerSelector(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TunnelPool::HasCustomPeerSelector()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_CustomPeerSelectorMutex);
|
|
|
|
return m_CustomPeerSelector != nullptr;
|
|
|
|
}
|
2016-11-15 15:20:09 +00:00
|
|
|
|
|
|
|
std::shared_ptr<InboundTunnel> TunnelPool::GetLowestLatencyInboundTunnel(std::shared_ptr<InboundTunnel> exclude) const
|
|
|
|
{
|
|
|
|
std::shared_ptr<InboundTunnel> tun = nullptr;
|
|
|
|
std::unique_lock<std::mutex> lock(m_InboundTunnelsMutex);
|
|
|
|
uint64_t min = 1000000;
|
|
|
|
for (const auto & itr : m_InboundTunnels) {
|
|
|
|
if(!itr->LatencyIsKnown()) continue;
|
|
|
|
auto l = itr->GetMeanLatency();
|
|
|
|
if (l >= min) continue;
|
|
|
|
tun = itr;
|
|
|
|
if(tun == exclude) continue;
|
|
|
|
min = l;
|
|
|
|
}
|
|
|
|
return tun;
|
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2016-11-15 15:20:09 +00:00
|
|
|
std::shared_ptr<OutboundTunnel> TunnelPool::GetLowestLatencyOutboundTunnel(std::shared_ptr<OutboundTunnel> exclude) const
|
|
|
|
{
|
|
|
|
std::shared_ptr<OutboundTunnel> tun = nullptr;
|
|
|
|
std::unique_lock<std::mutex> lock(m_OutboundTunnelsMutex);
|
|
|
|
uint64_t min = 1000000;
|
|
|
|
for (const auto & itr : m_OutboundTunnels) {
|
|
|
|
if(!itr->LatencyIsKnown()) continue;
|
|
|
|
auto l = itr->GetMeanLatency();
|
|
|
|
if (l >= min) continue;
|
|
|
|
tun = itr;
|
|
|
|
if(tun == exclude) continue;
|
|
|
|
min = l;
|
|
|
|
}
|
|
|
|
return tun;
|
|
|
|
}
|
2014-03-14 16:35:02 +00:00
|
|
|
}
|
|
|
|
}
|