2015-06-10 22:32:55 +03:00
|
|
|
#include <algorithm>
|
2015-11-03 17:15:49 +03:00
|
|
|
#include <openssl/rand.h>
|
2014-03-18 00:50:03 +04:00
|
|
|
#include "I2PEndian.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Crypto.h"
|
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-18 00:50:03 +04:00
|
|
|
#include "Garlic.h"
|
2015-05-05 17:33:19 +03:00
|
|
|
#include "Transports.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Log.h"
|
2014-03-14 20:35:02 +04:00
|
|
|
#include "TunnelPool.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
2015-05-05 19:32:13 +03:00
|
|
|
TunnelPool::TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels):
|
2014-11-30 06:00:52 +03:00
|
|
|
m_LocalDestination (localDestination), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
|
2015-05-05 19:32:13 +03:00
|
|
|
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_IsActive (true)
|
2014-03-14 20:35:02 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TunnelPool::~TunnelPool ()
|
2014-10-11 17:47:24 +04:00
|
|
|
{
|
|
|
|
DetachTunnels ();
|
|
|
|
}
|
|
|
|
|
2015-06-10 22:32:55 +03:00
|
|
|
void TunnelPool::SetExplicitPeers (std::shared_ptr<std::vector<i2p::data::IdentHash> > explicitPeers)
|
|
|
|
{
|
|
|
|
m_ExplicitPeers = explicitPeers;
|
|
|
|
if (m_ExplicitPeers)
|
|
|
|
{
|
|
|
|
int size = m_ExplicitPeers->size ();
|
|
|
|
if (m_NumInboundHops > size)
|
|
|
|
{
|
|
|
|
m_NumInboundHops = size;
|
|
|
|
LogPrint (eLogInfo, "Inbound tunnel length has beed adjusted to ", size, " for explicit peers");
|
|
|
|
}
|
|
|
|
if (m_NumOutboundHops > size)
|
|
|
|
{
|
|
|
|
m_NumOutboundHops = size;
|
|
|
|
LogPrint (eLogInfo, "Outbound tunnel length has beed adjusted to ", size, " for explicit peers");
|
|
|
|
}
|
|
|
|
m_NumInboundTunnels = 1;
|
|
|
|
m_NumOutboundTunnels = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 17:47:24 +04:00
|
|
|
void TunnelPool::DetachTunnels ()
|
2014-03-14 20:35:02 +04:00
|
|
|
{
|
2014-10-06 20:50:36 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
for (auto it: m_InboundTunnels)
|
|
|
|
it->SetTunnelPool (nullptr);
|
2014-10-13 19:21:57 +04:00
|
|
|
m_InboundTunnels.clear ();
|
2014-10-06 20:50:36 +04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
for (auto it: m_OutboundTunnels)
|
|
|
|
it->SetTunnelPool (nullptr);
|
2014-10-13 19:21:57 +04:00
|
|
|
m_OutboundTunnels.clear ();
|
2014-10-06 20:50:36 +04:00
|
|
|
}
|
2014-10-13 19:21:57 +04:00
|
|
|
m_Tests.clear ();
|
2014-10-11 17:47:24 +04:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void TunnelPool::TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel)
|
2014-03-14 23:13:34 +04:00
|
|
|
{
|
2014-10-13 19:21:57 +04:00
|
|
|
if (!m_IsActive) return;
|
2014-10-03 18:35:11 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
m_InboundTunnels.insert (createdTunnel);
|
|
|
|
}
|
2014-12-16 05:24:01 +03:00
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->SetLeaseSetUpdated ();
|
2014-03-14 23:13:34 +04:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void TunnelPool::TunnelExpired (std::shared_ptr<InboundTunnel> expiredTunnel)
|
2014-03-15 05:22:59 +04:00
|
|
|
{
|
2014-03-18 16:15:43 +04:00
|
|
|
if (expiredTunnel)
|
|
|
|
{
|
|
|
|
expiredTunnel->SetTunnelPool (nullptr);
|
2014-07-10 05:43:33 +04:00
|
|
|
for (auto it: m_Tests)
|
|
|
|
if (it.second.second == expiredTunnel) it.second.second = nullptr;
|
2014-10-03 18:35:11 +04:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
m_InboundTunnels.erase (expiredTunnel);
|
2014-03-18 16:15:43 +04:00
|
|
|
}
|
2014-03-15 05:22:59 +04:00
|
|
|
}
|
2014-03-17 00:03:20 +04:00
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void TunnelPool::TunnelCreated (std::shared_ptr<OutboundTunnel> createdTunnel)
|
2014-03-17 00:03:20 +04:00
|
|
|
{
|
2014-10-13 19:21:57 +04:00
|
|
|
if (!m_IsActive) return;
|
2015-05-07 23:03:12 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
m_OutboundTunnels.insert (createdTunnel);
|
|
|
|
}
|
2015-06-07 15:37:34 +03:00
|
|
|
//CreatePairedInboundTunnel (createdTunnel);
|
2014-03-17 00:03:20 +04:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void TunnelPool::TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel)
|
2014-03-17 00:03:20 +04:00
|
|
|
{
|
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);
|
2014-07-10 05:43:33 +04:00
|
|
|
for (auto it: m_Tests)
|
|
|
|
if (it.second.first == expiredTunnel) it.second.first = nullptr;
|
2014-10-03 18:35:11 +04:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
m_OutboundTunnels.erase (expiredTunnel);
|
2014-03-21 23:54:55 +04:00
|
|
|
}
|
2014-03-17 00:03:20 +04:00
|
|
|
}
|
2014-03-15 05:22:59 +04:00
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::vector<std::shared_ptr<InboundTunnel> > TunnelPool::GetInboundTunnels (int num) const
|
2014-03-14 23:13:34 +04:00
|
|
|
{
|
2015-01-27 22:55:46 +03:00
|
|
|
std::vector<std::shared_ptr<InboundTunnel> > v;
|
2014-03-14 23:13:34 +04:00
|
|
|
int i = 0;
|
2014-10-03 18:35:11 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2014-03-14 23:13:34 +04:00
|
|
|
for (auto it : m_InboundTunnels)
|
|
|
|
{
|
|
|
|
if (i >= num) break;
|
2014-08-28 05:53:44 +04:00
|
|
|
if (it->IsEstablished ())
|
2014-03-21 23:54:55 +04:00
|
|
|
{
|
|
|
|
v.push_back (it);
|
|
|
|
i++;
|
|
|
|
}
|
2014-03-14 23:13:34 +04:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-03-21 23:26:14 +03:00
|
|
|
std::shared_ptr<OutboundTunnel> TunnelPool::GetNextOutboundTunnel (std::shared_ptr<OutboundTunnel> excluded) const
|
2014-03-17 00:03:20 +04:00
|
|
|
{
|
2014-10-03 18:35:11 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2015-03-21 23:26:14 +03:00
|
|
|
return GetNextTunnel (m_OutboundTunnels, excluded);
|
2014-03-17 00:03:20 +04:00
|
|
|
}
|
2014-04-04 00:27:37 +04:00
|
|
|
|
2015-03-21 23:26:14 +03:00
|
|
|
std::shared_ptr<InboundTunnel> TunnelPool::GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded) const
|
2014-04-04 00:27:37 +04:00
|
|
|
{
|
2014-10-03 18:35:11 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2015-03-21 23:26:14 +03:00
|
|
|
return GetNextTunnel (m_InboundTunnels, excluded);
|
2014-04-04 00:27:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class TTunnels>
|
2015-03-21 23:26:14 +03:00
|
|
|
typename TTunnels::value_type TunnelPool::GetNextTunnel (TTunnels& tunnels, typename TTunnels::value_type excluded) const
|
2014-04-04 00:27:37 +04:00
|
|
|
{
|
2015-01-28 06:31:57 +03:00
|
|
|
if (tunnels.empty ()) return nullptr;
|
2015-11-03 17:15:49 +03:00
|
|
|
uint32_t ind = rand () % (tunnels.size ()/2 + 1), i = 0;
|
2014-08-28 06:21:29 +04:00
|
|
|
typename TTunnels::value_type tunnel = nullptr;
|
2014-04-04 00:27:37 +04:00
|
|
|
for (auto it: tunnels)
|
2014-08-28 06:21:29 +04:00
|
|
|
{
|
2015-03-21 23:26:14 +03:00
|
|
|
if (it->IsEstablished () && it != excluded)
|
2014-08-28 06:21:29 +04:00
|
|
|
{
|
|
|
|
tunnel = it;
|
|
|
|
i++;
|
|
|
|
}
|
2014-08-29 15:44:12 +04:00
|
|
|
if (i > ind && tunnel) break;
|
2015-03-21 23:26:14 +03:00
|
|
|
}
|
|
|
|
if (!tunnel && excluded && excluded->IsEstablished ()) tunnel = excluded;
|
2014-08-28 06:21:29 +04:00
|
|
|
return tunnel;
|
2014-04-04 00:27:37 +04:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:11:51 +03:00
|
|
|
std::shared_ptr<OutboundTunnel> TunnelPool::GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const
|
|
|
|
{
|
|
|
|
if (old && old->IsEstablished ()) return old;
|
|
|
|
std::shared_ptr<OutboundTunnel> tunnel;
|
|
|
|
if (old)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
for (auto it: m_OutboundTunnels)
|
2015-11-03 17:15:49 +03:00
|
|
|
if (it->IsEstablished () && old->GetEndpointIdentHash () == it->GetEndpointIdentHash ())
|
2015-04-17 18:36:42 +03:00
|
|
|
{
|
2015-04-17 17:11:51 +03:00
|
|
|
tunnel = it;
|
2015-04-17 18:36:42 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-04-17 17:11:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tunnel)
|
|
|
|
tunnel = GetNextOutboundTunnel ();
|
|
|
|
return tunnel;
|
|
|
|
}
|
|
|
|
|
2014-03-14 23:13:34 +04:00
|
|
|
void TunnelPool::CreateTunnels ()
|
|
|
|
{
|
2014-08-28 05:53:44 +04:00
|
|
|
int num = 0;
|
2014-10-03 18:35:11 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
for (auto it : m_InboundTunnels)
|
|
|
|
if (it->IsEstablished ()) num++;
|
|
|
|
}
|
2015-05-05 19:32:13 +03:00
|
|
|
for (int i = num; i < m_NumInboundTunnels; i++)
|
2014-03-14 23:13:34 +04:00
|
|
|
CreateInboundTunnel ();
|
2014-08-28 05:53:44 +04:00
|
|
|
|
|
|
|
num = 0;
|
2014-10-03 18:35:11 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
for (auto it : m_OutboundTunnels)
|
|
|
|
if (it->IsEstablished ()) num++;
|
|
|
|
}
|
2015-05-05 19:32:13 +03:00
|
|
|
for (int i = num; i < m_NumOutboundTunnels; i++)
|
2014-03-17 00:03:20 +04:00
|
|
|
CreateOutboundTunnel ();
|
2014-03-14 23:13:34 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 00:50:03 +04:00
|
|
|
void TunnelPool::TestTunnels ()
|
|
|
|
{
|
|
|
|
for (auto it: m_Tests)
|
|
|
|
{
|
|
|
|
LogPrint ("Tunnel test ", (int)it.first, " failed");
|
2014-07-27 04:56:42 +04:00
|
|
|
// if test failed again with another tunnel we consider it failed
|
2014-07-10 05:43:33 +04:00
|
|
|
if (it.second.first)
|
|
|
|
{
|
2014-07-27 04:56:42 +04:00
|
|
|
if (it.second.first->GetState () == eTunnelStateTestFailed)
|
|
|
|
{
|
|
|
|
it.second.first->SetState (eTunnelStateFailed);
|
2014-10-03 18:35:11 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2014-07-27 04:56:42 +04:00
|
|
|
m_OutboundTunnels.erase (it.second.first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it.second.first->SetState (eTunnelStateTestFailed);
|
2014-07-10 05:43:33 +04:00
|
|
|
}
|
|
|
|
if (it.second.second)
|
|
|
|
{
|
2014-07-27 04:56:42 +04:00
|
|
|
if (it.second.second->GetState () == eTunnelStateTestFailed)
|
|
|
|
{
|
|
|
|
it.second.second->SetState (eTunnelStateFailed);
|
2014-10-03 18:35:11 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
m_InboundTunnels.erase (it.second.second);
|
|
|
|
}
|
2014-12-16 05:24:01 +03:00
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->SetLeaseSetUpdated ();
|
2014-07-27 04:56:42 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
it.second.second->SetState (eTunnelStateTestFailed);
|
2014-07-10 05:43:33 +04:00
|
|
|
}
|
2014-03-18 00:50:03 +04:00
|
|
|
}
|
2014-11-29 00:19:56 +03:00
|
|
|
m_Tests.clear ();
|
|
|
|
// new tests
|
2014-03-18 00:50:03 +04:00
|
|
|
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)
|
2014-07-27 04:56:42 +04:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
uint32_t msgID;
|
|
|
|
RAND_bytes ((uint8_t *)&msgID, 4);
|
2014-12-09 03:33:50 +03:00
|
|
|
m_Tests[msgID] = std::make_pair (*it1, *it2);
|
|
|
|
(*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
|
2015-06-24 17:45:58 +03:00
|
|
|
CreateDeliveryStatusMsg (msgID));
|
2014-03-22 02:26:11 +04:00
|
|
|
it1++; it2++;
|
|
|
|
}
|
2014-03-18 00:50:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void TunnelPool::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-12-16 05:24:01 +03:00
|
|
|
{
|
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->ProcessGarlicMessage (msg);
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "Local destination doesn't exist. Dropped");
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void TunnelPool::ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg)
|
2014-03-18 00:50:03 +04:00
|
|
|
{
|
2014-12-30 23:33:11 +03: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-18 00:50:03 +04:00
|
|
|
if (it != m_Tests.end ())
|
|
|
|
{
|
2014-07-27 04:56:42 +04:00
|
|
|
// restore from test failed state if any
|
2014-08-28 05: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-12-30 23:33:11 +03:00
|
|
|
LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - timestamp, " milliseconds");
|
2014-07-27 18:39:38 +04:00
|
|
|
m_Tests.erase (it);
|
2014-03-18 00:50:03 +04:00
|
|
|
}
|
|
|
|
else
|
2014-12-16 05:24:01 +03:00
|
|
|
{
|
|
|
|
if (m_LocalDestination)
|
|
|
|
m_LocalDestination->ProcessDeliveryStatusMessage (msg);
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "Local destination doesn't exist. Dropped");
|
|
|
|
}
|
2014-03-18 00:50:03 +04:00
|
|
|
}
|
|
|
|
|
2014-11-21 18:46:11 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
2014-12-16 05:24:01 +03:00
|
|
|
bool isExploratory = (m_LocalDestination == &i2p::context); // TODO: implement it better
|
2015-03-28 03:34:31 +03:00
|
|
|
auto hop = isExploratory ? i2p::data::netdb.GetRandomRouter (prevHop):
|
2014-11-30 06:00:52 +03:00
|
|
|
i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop);
|
2015-05-04 20:01:27 +03:00
|
|
|
|
|
|
|
if (!hop || hop->GetProfile ()->IsBad ())
|
2014-11-21 18:46:11 +03:00
|
|
|
hop = i2p::data::netdb.GetRandomRouter ();
|
2014-09-26 05:08:20 +04:00
|
|
|
return hop;
|
|
|
|
}
|
2015-06-07 15:37:34 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
bool TunnelPool::SelectPeers (std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers, bool isInbound)
|
2014-03-14 23:13:34 +04:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
if (m_ExplicitPeers) return SelectExplicitPeers (peers, isInbound);
|
2014-11-21 18:46:11 +03:00
|
|
|
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
2015-06-10 22:32:55 +03:00
|
|
|
int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
|
2015-06-07 15:37:34 +03:00
|
|
|
if (i2p::transport::transports.GetNumPeers () > 25)
|
|
|
|
{
|
|
|
|
auto r = i2p::transport::transports.GetRandomPeer ();
|
|
|
|
if (r && !r->GetProfile ()->IsBad ())
|
|
|
|
{
|
|
|
|
prevHop = r;
|
2015-11-03 17:15:49 +03:00
|
|
|
peers.push_back (r->GetRouterIdentity ());
|
2014-07-11 15:34:45 +04:00
|
|
|
numHops--;
|
|
|
|
}
|
|
|
|
}
|
2015-06-07 15:37:34 +03:00
|
|
|
|
2014-07-11 15:34:45 +04:00
|
|
|
for (int i = 0; i < numHops; i++)
|
|
|
|
{
|
2014-09-26 05:08:20 +04:00
|
|
|
auto hop = SelectNextHop (prevHop);
|
2015-04-03 17:02:45 +03:00
|
|
|
if (!hop)
|
|
|
|
{
|
2015-06-07 15:37:34 +03:00
|
|
|
LogPrint (eLogError, "Can't select next hop");
|
|
|
|
return false;
|
2015-04-03 17:02:45 +03:00
|
|
|
}
|
2014-07-11 15:34:45 +04:00
|
|
|
prevHop = hop;
|
2015-11-03 17:15:49 +03:00
|
|
|
peers.push_back (hop->GetRouterIdentity ());
|
2014-07-11 15:34:45 +04:00
|
|
|
}
|
2015-06-07 15:37:34 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-10 22:32:55 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
bool TunnelPool::SelectExplicitPeers (std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers, bool isInbound)
|
2015-06-10 22:32:55 +03:00
|
|
|
{
|
|
|
|
int size = m_ExplicitPeers->size ();
|
|
|
|
std::vector<int> peerIndicies;
|
|
|
|
for (int i = 0; i < size; i++) peerIndicies.push_back(i);
|
|
|
|
std::random_shuffle (peerIndicies.begin(), peerIndicies.end());
|
|
|
|
|
|
|
|
int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
|
|
|
|
for (int i = 0; i < numHops; i++)
|
|
|
|
{
|
|
|
|
auto& ident = (*m_ExplicitPeers)[peerIndicies[i]];
|
|
|
|
auto r = i2p::data::netdb.FindRouter (ident);
|
|
|
|
if (r)
|
2015-11-03 17:15:49 +03:00
|
|
|
peers.push_back (r->GetRouterIdentity ());
|
2015-06-10 22:32:55 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Can't find router for ", ident.ToBase64 ());
|
|
|
|
i2p::data::netdb.RequestDestination (ident);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-07 15:37:34 +03:00
|
|
|
void TunnelPool::CreateInboundTunnel ()
|
|
|
|
{
|
|
|
|
auto outboundTunnel = GetNextOutboundTunnel ();
|
|
|
|
if (!outboundTunnel)
|
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
|
|
|
LogPrint ("Creating destination inbound tunnel...");
|
2015-11-03 17:15:49 +03:00
|
|
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
|
|
|
|
if (SelectPeers (peers, true))
|
2015-06-07 15:37:34 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
std::reverse (peers.begin (), peers.end ());
|
|
|
|
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (peers), outboundTunnel);
|
2015-06-07 15:37:34 +03:00
|
|
|
tunnel->SetTunnelPool (shared_from_this ());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "Can't create inbound tunnel. No peers available");
|
2014-03-14 20:35:02 +04:00
|
|
|
}
|
2014-03-17 00:03:20 +04:00
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void TunnelPool::RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel)
|
2014-08-09 06:44:33 +04:00
|
|
|
{
|
2015-01-27 22:55:46 +03:00
|
|
|
auto outboundTunnel = GetNextOutboundTunnel ();
|
2014-08-09 06:44:33 +04:00
|
|
|
if (!outboundTunnel)
|
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
|
|
|
LogPrint ("Re-creating destination inbound tunnel...");
|
2015-11-03 17:15:49 +03:00
|
|
|
auto newTunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig>(tunnel->GetPeers ()), outboundTunnel);
|
2015-01-20 06:28:13 +03:00
|
|
|
newTunnel->SetTunnelPool (shared_from_this());
|
2014-08-09 06:44:33 +04:00
|
|
|
}
|
|
|
|
|
2014-03-17 00:03:20 +04:00
|
|
|
void TunnelPool::CreateOutboundTunnel ()
|
|
|
|
{
|
2015-01-27 22:55:46 +03:00
|
|
|
auto inboundTunnel = GetNextInboundTunnel ();
|
2014-09-02 16:16:46 +04:00
|
|
|
if (!inboundTunnel)
|
|
|
|
inboundTunnel = tunnels.GetNextInboundTunnel ();
|
2014-03-17 00:03:20 +04:00
|
|
|
if (inboundTunnel)
|
|
|
|
{
|
|
|
|
LogPrint ("Creating destination outbound tunnel...");
|
2015-11-03 17:15:49 +03:00
|
|
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
|
|
|
|
if (SelectPeers (peers, false))
|
2015-06-07 15:37:34 +03:00
|
|
|
{
|
|
|
|
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
2015-11-03 17:15:49 +03:00
|
|
|
std::make_shared<TunnelConfig> (peers, inboundTunnel->GetNextTunnelID (), inboundTunnel->GetNextIdentHash ()));
|
2015-06-07 15:37:34 +03:00
|
|
|
tunnel->SetTunnelPool (shared_from_this ());
|
2014-06-26 03:28:33 +04:00
|
|
|
}
|
2015-06-07 15:37:34 +03:00
|
|
|
else
|
|
|
|
LogPrint (eLogError, "Can't create outbound tunnel. No peers available");
|
2014-03-17 00:03:20 +04:00
|
|
|
}
|
2014-09-14 15:50:01 +04:00
|
|
|
else
|
2015-04-03 17:02:45 +03:00
|
|
|
LogPrint (eLogError, "Can't create outbound tunnel. No inbound tunnels found");
|
2014-03-17 00:03:20 +04:00
|
|
|
}
|
2014-09-26 05:08:20 +04:00
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void TunnelPool::RecreateOutboundTunnel (std::shared_ptr<OutboundTunnel> tunnel)
|
2014-08-09 06:44:33 +04:00
|
|
|
{
|
2015-01-27 22:55:46 +03:00
|
|
|
auto inboundTunnel = GetNextInboundTunnel ();
|
2014-08-09 06:44:33 +04:00
|
|
|
if (!inboundTunnel)
|
|
|
|
inboundTunnel = tunnels.GetNextInboundTunnel ();
|
2014-09-14 15:50:01 +04:00
|
|
|
if (inboundTunnel)
|
|
|
|
{
|
|
|
|
LogPrint ("Re-creating destination outbound tunnel...");
|
2015-01-27 22:55:46 +03:00
|
|
|
auto newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
2015-11-03 17:15:49 +03:00
|
|
|
std::make_shared<TunnelConfig> (tunnel->GetPeers (),
|
|
|
|
inboundTunnel->GetNextTunnelID (), inboundTunnel->GetNextIdentHash ()));
|
2015-01-20 06:28:13 +03:00
|
|
|
newTunnel->SetTunnelPool (shared_from_this ());
|
2014-09-14 15:50:01 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found");
|
2015-05-07 23:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelPool::CreatePairedInboundTunnel (std::shared_ptr<OutboundTunnel> outboundTunnel)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Creating paired inbound tunnel...");
|
2015-11-03 17:15:49 +03:00
|
|
|
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig>(outboundTunnel->GetInvertedPeers ()), outboundTunnel);
|
2015-05-07 23:03:12 +03:00
|
|
|
tunnel->SetTunnelPool (shared_from_this ());
|
|
|
|
}
|
2014-03-14 20:35:02 +04:00
|
|
|
}
|
|
|
|
}
|