2014-12-31 17:14:53 +03:00
|
|
|
#include <string.h>
|
2014-01-09 23:56:07 +04:00
|
|
|
#include "I2PEndian.h"
|
2014-01-10 02:55:53 +04:00
|
|
|
#include <thread>
|
2014-08-16 22:35:35 +04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2013-12-07 04:02:49 +04:00
|
|
|
#include <cryptopp/sha.h>
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "Timestamp.h"
|
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "Transports.h"
|
|
|
|
#include "NetDb.h"
|
|
|
|
#include "Tunnel.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
|
|
|
|
2014-07-27 04:56:42 +04:00
|
|
|
Tunnel::Tunnel (TunnelConfig * config):
|
|
|
|
m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Tunnel::~Tunnel ()
|
|
|
|
{
|
|
|
|
delete m_Config;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void Tunnel::Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
2014-08-16 22:35:35 +04:00
|
|
|
auto numHops = m_Config->GetNumHops ();
|
|
|
|
int numRecords = numHops <= STANDARD_NUM_RECORDS ? STANDARD_NUM_RECORDS : numHops;
|
2015-03-11 04:54:25 +03:00
|
|
|
I2NPMessage * msg = NewI2NPShortMessage ();
|
2013-12-07 04:02:49 +04:00
|
|
|
*msg->GetPayload () = numRecords;
|
2015-01-04 00:31:44 +03:00
|
|
|
msg->len += numRecords*TUNNEL_BUILD_RECORD_SIZE + 1;
|
2014-08-16 22:35:35 +04:00
|
|
|
|
|
|
|
// shuffle records
|
|
|
|
std::vector<int> recordIndicies;
|
|
|
|
for (int i = 0; i < numRecords; i++) recordIndicies.push_back(i);
|
|
|
|
std::random_shuffle (recordIndicies.begin(), recordIndicies.end());
|
2013-12-07 04:02:49 +04:00
|
|
|
|
2014-08-16 22:35:35 +04:00
|
|
|
// create real records
|
2015-01-04 00:31:44 +03:00
|
|
|
uint8_t * records = msg->GetPayload () + 1;
|
2013-12-07 04:02:49 +04:00
|
|
|
TunnelHopConfig * hop = m_Config->GetFirstHop ();
|
|
|
|
int i = 0;
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-08-16 22:35:35 +04:00
|
|
|
int idx = recordIndicies[i];
|
2015-01-04 17:33:19 +03:00
|
|
|
hop->CreateBuildRequestRecord (records + idx*TUNNEL_BUILD_RECORD_SIZE,
|
|
|
|
hop->next ? rnd.GenerateWord32 () : replyMsgID); // we set replyMsgID for last hop only
|
2014-08-16 22:35:35 +04:00
|
|
|
hop->recordIndex = idx;
|
2013-12-07 04:02:49 +04:00
|
|
|
i++;
|
|
|
|
hop = hop->next;
|
|
|
|
}
|
2014-06-19 05:24:24 +04:00
|
|
|
// fill up fake records with random data
|
2014-08-16 22:35:35 +04:00
|
|
|
for (int i = numHops; i < numRecords; i++)
|
2014-06-19 05:24:24 +04:00
|
|
|
{
|
2014-08-16 22:35:35 +04:00
|
|
|
int idx = recordIndicies[i];
|
2015-01-04 00:31:44 +03:00
|
|
|
rnd.GenerateBlock (records + idx*TUNNEL_BUILD_RECORD_SIZE, TUNNEL_BUILD_RECORD_SIZE);
|
2014-06-19 05:24:24 +04:00
|
|
|
}
|
2014-08-16 22:35:35 +04:00
|
|
|
|
|
|
|
// decrypt real records
|
2014-05-16 02:58:26 +04:00
|
|
|
i2p::crypto::CBCDecryption decryption;
|
2013-12-07 04:02:49 +04:00
|
|
|
hop = m_Config->GetLastHop ()->prev;
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-16 02:58:26 +04:00
|
|
|
decryption.SetKey (hop->replyKey);
|
2014-06-19 03:38:21 +04:00
|
|
|
// decrypt records after current hop
|
|
|
|
TunnelHopConfig * hop1 = hop->next;
|
|
|
|
while (hop1)
|
|
|
|
{
|
2014-06-25 03:33:30 +04:00
|
|
|
decryption.SetIV (hop->replyIV);
|
2015-01-04 00:31:44 +03:00
|
|
|
uint8_t * record = records + hop1->recordIndex*TUNNEL_BUILD_RECORD_SIZE;
|
|
|
|
decryption.Decrypt(record, TUNNEL_BUILD_RECORD_SIZE, record);
|
2014-06-19 03:38:21 +04:00
|
|
|
hop1 = hop1->next;
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
hop = hop->prev;
|
|
|
|
}
|
|
|
|
FillI2NPMessageHeader (msg, eI2NPVariableTunnelBuild);
|
2014-08-16 22:35:35 +04:00
|
|
|
|
|
|
|
// send message
|
2013-12-07 04:02:49 +04:00
|
|
|
if (outboundTunnel)
|
2014-01-06 06:25:48 +04:00
|
|
|
outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg);
|
2013-12-07 04:02:49 +04:00
|
|
|
else
|
2014-10-21 20:25:53 +04:00
|
|
|
i2p::transport::transports.SendMessage (GetNextIdentHash (), msg);
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Tunnel::HandleTunnelBuildResponse (uint8_t * msg, size_t len)
|
|
|
|
{
|
|
|
|
LogPrint ("TunnelBuildResponse ", (int)msg[0], " records.");
|
2014-06-19 03:38:21 +04:00
|
|
|
|
2014-05-16 02:58:26 +04:00
|
|
|
i2p::crypto::CBCDecryption decryption;
|
2013-12-07 04:02:49 +04:00
|
|
|
TunnelHopConfig * hop = m_Config->GetLastHop ();
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-16 02:58:26 +04:00
|
|
|
decryption.SetKey (hop->replyKey);
|
2014-06-19 03:38:21 +04:00
|
|
|
// decrypt records before and including current hop
|
|
|
|
TunnelHopConfig * hop1 = hop;
|
|
|
|
while (hop1)
|
|
|
|
{
|
2014-06-19 05:24:24 +04:00
|
|
|
auto idx = hop1->recordIndex;
|
|
|
|
if (idx >= 0 && idx < msg[0])
|
|
|
|
{
|
2015-01-03 17:47:24 +03:00
|
|
|
uint8_t * record = msg + 1 + idx*TUNNEL_BUILD_RECORD_SIZE;
|
2014-06-25 03:33:30 +04:00
|
|
|
decryption.SetIV (hop->replyIV);
|
2015-01-03 17:47:24 +03:00
|
|
|
decryption.Decrypt(record, TUNNEL_BUILD_RECORD_SIZE, record);
|
2014-06-19 05:24:24 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Tunnel hop index ", idx, " is out of range");
|
2014-06-19 03:38:21 +04:00
|
|
|
hop1 = hop1->prev;
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
hop = hop->prev;
|
|
|
|
}
|
|
|
|
|
2014-07-27 04:56:42 +04:00
|
|
|
bool established = true;
|
2014-06-19 05:24:24 +04:00
|
|
|
hop = m_Config->GetFirstHop ();
|
|
|
|
while (hop)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2015-01-03 17:47:24 +03:00
|
|
|
const uint8_t * record = msg + 1 + hop->recordIndex*TUNNEL_BUILD_RECORD_SIZE;
|
|
|
|
uint8_t ret = record[BUILD_RESPONSE_RECORD_RET_OFFSET];
|
|
|
|
LogPrint ("Ret code=", (int)ret);
|
2015-03-25 01:48:16 +03:00
|
|
|
hop->router->GetProfile ()->TunnelBuildResponse (ret);
|
2015-01-03 17:47:24 +03:00
|
|
|
if (ret)
|
2013-12-07 04:02:49 +04:00
|
|
|
// if any of participants declined the tunnel is not established
|
2014-07-27 04:56:42 +04:00
|
|
|
established = false;
|
2014-06-19 05:24:24 +04:00
|
|
|
hop = hop->next;
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
2014-07-27 04:56:42 +04:00
|
|
|
if (established)
|
2014-05-10 03:34:12 +04:00
|
|
|
{
|
|
|
|
// change reply keys to layer keys
|
2014-06-19 05:24:24 +04:00
|
|
|
hop = m_Config->GetFirstHop ();
|
2014-05-10 03:34:12 +04:00
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-16 02:58:26 +04:00
|
|
|
hop->decryption.SetKeys (hop->layerKey, hop->ivKey);
|
2014-05-10 03:34:12 +04:00
|
|
|
hop = hop->next;
|
|
|
|
}
|
|
|
|
}
|
2014-07-27 04:56:42 +04:00
|
|
|
if (established) m_State = eTunnelStateEstablished;
|
|
|
|
return established;
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg)
|
|
|
|
{
|
|
|
|
uint8_t * payload = tunnelMsg->GetPayload () + 4;
|
|
|
|
TunnelHopConfig * hop = m_Config->GetLastHop ();
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-16 02:58:26 +04:00
|
|
|
hop->decryption.Decrypt (payload);
|
2013-12-07 04:02:49 +04:00
|
|
|
hop = hop->prev;
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 00:26:39 +03:00
|
|
|
|
|
|
|
void Tunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Can't send I2NP messages without delivery instructions");
|
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
void InboundTunnel::HandleTunnelDataMsg (I2NPMessage * msg)
|
|
|
|
{
|
2014-07-27 04:56:42 +04:00
|
|
|
if (IsFailed ()) SetState (eTunnelStateEstablished); // incoming messages means a tunnel is alive
|
2015-02-06 02:53:43 +03:00
|
|
|
msg->from = shared_from_this ();
|
2013-12-07 04:02:49 +04:00
|
|
|
EncryptTunnelMsg (msg);
|
|
|
|
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutboundTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
|
|
|
|
{
|
2014-02-20 07:08:57 +04:00
|
|
|
TunnelMessageBlock block;
|
|
|
|
if (gwHash)
|
|
|
|
{
|
|
|
|
block.hash = gwHash;
|
|
|
|
if (gwTunnel)
|
|
|
|
{
|
|
|
|
block.deliveryType = eDeliveryTypeTunnel;
|
|
|
|
block.tunnelID = gwTunnel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
block.deliveryType = eDeliveryTypeRouter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
block.deliveryType = eDeliveryTypeLocal;
|
|
|
|
block.data = msg;
|
2014-04-03 20:19:12 +04:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
2014-02-20 07:08:57 +04:00
|
|
|
m_Gateway.SendTunnelDataMsg (block);
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
2014-01-21 03:37:51 +04:00
|
|
|
|
2014-10-07 18:33:17 +04:00
|
|
|
void OutboundTunnel::SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-04-03 20:19:12 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
2014-01-21 03:37:51 +04:00
|
|
|
for (auto& it : msgs)
|
2014-02-20 06:24:55 +04:00
|
|
|
m_Gateway.PutTunnelDataMsg (it);
|
2014-01-21 03:37:51 +04:00
|
|
|
m_Gateway.SendBuffer ();
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
2014-01-21 03:37:51 +04:00
|
|
|
|
2015-01-21 22:40:48 +03:00
|
|
|
void OutboundTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Incoming message for outbound tunnel ", GetTunnelID ());
|
|
|
|
DeleteI2NPMessage (tunnelMsg);
|
|
|
|
}
|
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
Tunnels tunnels;
|
|
|
|
|
2015-02-28 15:59:34 +03:00
|
|
|
Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr),
|
|
|
|
m_NumSuccesiveTunnelCreations (0), m_NumFailedTunnelCreations (0)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Tunnels::~Tunnels ()
|
|
|
|
{
|
|
|
|
for (auto& it : m_TransitTunnels)
|
|
|
|
delete it.second;
|
|
|
|
m_TransitTunnels.clear ();
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<InboundTunnel> Tunnels::GetInboundTunnel (uint32_t tunnelID)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
auto it = m_InboundTunnels.find(tunnelID);
|
|
|
|
if (it != m_InboundTunnels.end ())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransitTunnel * Tunnels::GetTransitTunnel (uint32_t tunnelID)
|
|
|
|
{
|
|
|
|
auto it = m_TransitTunnels.find(tunnelID);
|
|
|
|
if (it != m_TransitTunnels.end ())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-01-26 19:56:10 +03:00
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<InboundTunnel> Tunnels::GetPendingInboundTunnel (uint32_t replyMsgID)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2015-01-26 19:56:10 +03:00
|
|
|
return GetPendingTunnel (replyMsgID, m_PendingInboundTunnels);
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<OutboundTunnel> Tunnels::GetPendingOutboundTunnel (uint32_t replyMsgID)
|
2015-01-26 19:56:10 +03:00
|
|
|
{
|
|
|
|
return GetPendingTunnel (replyMsgID, m_PendingOutboundTunnels);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class TTunnel>
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<TTunnel> Tunnels::GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels)
|
2015-01-26 19:56:10 +03:00
|
|
|
{
|
|
|
|
auto it = pendingTunnels.find(replyMsgID);
|
|
|
|
if (it != pendingTunnels.end () && it->second->GetState () == eTunnelStatePending)
|
2014-09-26 18:15:34 +04:00
|
|
|
{
|
|
|
|
it->second->SetState (eTunnelStateBuildReplyReceived);
|
2014-08-18 22:37:19 +04:00
|
|
|
return it->second;
|
2014-09-26 18:15:34 +04:00
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<InboundTunnel> Tunnels::GetNextInboundTunnel ()
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<InboundTunnel> tunnel;
|
2013-12-07 04:02:49 +04:00
|
|
|
size_t minReceived = 0;
|
|
|
|
for (auto it : m_InboundTunnels)
|
2014-03-21 23:54:55 +04:00
|
|
|
{
|
2014-08-26 18:31:32 +04:00
|
|
|
if (!it.second->IsEstablished ()) continue;
|
2013-12-07 04:02:49 +04:00
|
|
|
if (!tunnel || it.second->GetNumReceivedBytes () < minReceived)
|
|
|
|
{
|
|
|
|
tunnel = it.second;
|
|
|
|
minReceived = it.second->GetNumReceivedBytes ();
|
2014-03-21 23:54:55 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
return tunnel;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<OutboundTunnel> Tunnels::GetNextOutboundTunnel ()
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2013-12-29 19:48:57 +04:00
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
|
|
|
uint32_t ind = rnd.GenerateWord32 (0, m_OutboundTunnels.size () - 1), i = 0;
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<OutboundTunnel> tunnel;
|
2013-12-29 19:48:57 +04:00
|
|
|
for (auto it: m_OutboundTunnels)
|
|
|
|
{
|
2014-08-26 18:31:32 +04:00
|
|
|
if (it->IsEstablished ())
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
tunnel = it;
|
2014-03-21 23:54:55 +04:00
|
|
|
i++;
|
|
|
|
}
|
2014-08-29 15:44:12 +04:00
|
|
|
if (i > ind && tunnel) break;
|
2014-03-21 23:54:55 +04:00
|
|
|
}
|
|
|
|
return tunnel;
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
2014-03-15 04:24:12 +04:00
|
|
|
|
2015-01-20 06:28:13 +03:00
|
|
|
std::shared_ptr<TunnelPool> Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops)
|
2014-03-15 04:24:12 +04:00
|
|
|
{
|
2015-01-20 06:28:13 +03:00
|
|
|
auto pool = std::make_shared<TunnelPool> (localDestination, numInboundHops, numOutboundHops);
|
2014-10-05 19:01:12 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
2014-12-10 05:07:54 +03:00
|
|
|
m_Pools.push_back (pool);
|
2014-03-15 04:51:51 +04:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2015-01-20 06:28:13 +03:00
|
|
|
void Tunnels::DeleteTunnelPool (std::shared_ptr<TunnelPool> pool)
|
2014-03-15 04:51:51 +04:00
|
|
|
{
|
2014-10-11 17:47:24 +04:00
|
|
|
if (pool)
|
|
|
|
{
|
2014-10-13 19:21:57 +04:00
|
|
|
StopTunnelPool (pool);
|
2014-12-10 05:07:54 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
|
|
|
m_Pools.remove (pool);
|
|
|
|
}
|
2014-10-13 19:21:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 06:28:13 +03:00
|
|
|
void Tunnels::StopTunnelPool (std::shared_ptr<TunnelPool> pool)
|
2014-10-13 19:21:57 +04:00
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
{
|
|
|
|
pool->SetActive (false);
|
2014-10-11 17:47:24 +04:00
|
|
|
pool->DetachTunnels ();
|
|
|
|
}
|
2014-03-15 04:24:12 +04:00
|
|
|
}
|
2014-10-13 19:21:57 +04:00
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
void Tunnels::AddTransitTunnel (TransitTunnel * tunnel)
|
|
|
|
{
|
2014-09-14 15:50:01 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
|
2015-02-10 17:39:49 +03:00
|
|
|
if (!m_TransitTunnels.insert (std::make_pair (tunnel->GetTunnelID (), tunnel)).second)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Transit tunnel ", tunnel->GetTunnelID (), " already exists");
|
|
|
|
delete tunnel;
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::Start ()
|
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
|
|
|
m_Thread = new std::thread (std::bind (&Tunnels::Run, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::Stop ()
|
|
|
|
{
|
|
|
|
m_IsRunning = false;
|
|
|
|
m_Queue.WakeUp ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::Run ()
|
|
|
|
{
|
2014-01-10 02:55:53 +04:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready
|
2013-12-07 04:02:49 +04:00
|
|
|
|
2014-01-09 23:56:07 +04:00
|
|
|
uint64_t lastTs = 0;
|
2013-12-07 04:02:49 +04:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
I2NPMessage * msg = m_Queue.GetNextWithTimeout (1000); // 1 sec
|
2015-01-22 05:50:46 +03:00
|
|
|
if (msg)
|
|
|
|
{
|
2015-01-27 04:49:16 +03:00
|
|
|
uint32_t prevTunnelID = 0, tunnelID = 0;
|
2015-01-31 18:39:29 +03:00
|
|
|
TunnelBase * prevTunnel = nullptr;
|
2015-01-22 05:50:46 +03:00
|
|
|
do
|
2015-01-21 23:13:46 +03:00
|
|
|
{
|
2015-01-31 18:39:29 +03:00
|
|
|
TunnelBase * tunnel = nullptr;
|
2015-01-27 04:49:16 +03:00
|
|
|
uint8_t typeID = msg->GetTypeID ();
|
|
|
|
switch (typeID)
|
|
|
|
{
|
|
|
|
case eI2NPTunnelData:
|
|
|
|
case eI2NPTunnelGateway:
|
|
|
|
{
|
|
|
|
tunnelID = bufbe32toh (msg->GetPayload ());
|
|
|
|
if (tunnelID == prevTunnelID)
|
|
|
|
tunnel = prevTunnel;
|
|
|
|
else if (prevTunnel)
|
|
|
|
prevTunnel->FlushTunnelDataMsgs ();
|
2015-01-22 05:50:46 +03:00
|
|
|
|
2015-01-27 04:49:16 +03:00
|
|
|
if (!tunnel && typeID == eI2NPTunnelData)
|
2015-01-27 22:55:46 +03:00
|
|
|
tunnel = GetInboundTunnel (tunnelID).get ();
|
2015-01-27 04:49:16 +03:00
|
|
|
if (!tunnel)
|
|
|
|
tunnel = GetTransitTunnel (tunnelID);
|
|
|
|
if (tunnel)
|
|
|
|
{
|
|
|
|
if (typeID == eI2NPTunnelData)
|
|
|
|
tunnel->HandleTunnelDataMsg (msg);
|
|
|
|
else // tunnel gateway assumed
|
|
|
|
HandleTunnelGatewayMsg (tunnel, msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Tunnel ", tunnelID, " not found");
|
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eI2NPVariableTunnelBuild:
|
|
|
|
case eI2NPVariableTunnelBuildReply:
|
|
|
|
case eI2NPTunnelBuild:
|
|
|
|
case eI2NPTunnelBuildReply:
|
|
|
|
{
|
|
|
|
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ());
|
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Unexpected messsage type ", (int)typeID);
|
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
}
|
2015-01-24 00:26:39 +03:00
|
|
|
}
|
2015-01-27 04:49:16 +03:00
|
|
|
|
2015-01-22 05:50:46 +03:00
|
|
|
msg = m_Queue.Get ();
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
prevTunnelID = tunnelID;
|
|
|
|
prevTunnel = tunnel;
|
|
|
|
}
|
|
|
|
else if (tunnel)
|
|
|
|
tunnel->FlushTunnelDataMsgs ();
|
2015-01-21 23:13:46 +03:00
|
|
|
}
|
2015-01-22 05:50:46 +03:00
|
|
|
while (msg);
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
|
2014-01-09 23:56:07 +04:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2013-12-07 04:02:49 +04:00
|
|
|
if (ts - lastTs >= 15) // manage tunnels every 15 seconds
|
|
|
|
{
|
|
|
|
ManageTunnels ();
|
|
|
|
lastTs = ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint ("Tunnels: ", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:26:39 +03:00
|
|
|
void Tunnels::HandleTunnelGatewayMsg (TunnelBase * tunnel, I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
if (!tunnel)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Missing tunnel for TunnelGateway");
|
|
|
|
i2p::DeleteI2NPMessage (msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const uint8_t * payload = msg->GetPayload ();
|
|
|
|
uint16_t len = bufbe16toh(payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET);
|
|
|
|
// we make payload as new I2NP message to send
|
|
|
|
msg->offset += I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE;
|
|
|
|
msg->len = msg->offset + len;
|
|
|
|
auto typeID = msg->GetTypeID ();
|
|
|
|
LogPrint (eLogDebug, "TunnelGateway of ", (int)len, " bytes for tunnel ", tunnel->GetTunnelID (), ". Msg type ", (int)typeID);
|
|
|
|
|
|
|
|
if (typeID == eI2NPDatabaseStore || typeID == eI2NPDatabaseSearchReply)
|
|
|
|
{
|
|
|
|
// transit DatabaseStore my contain new/updated RI
|
|
|
|
// or DatabaseSearchReply with new routers
|
|
|
|
auto ds = NewI2NPMessage ();
|
|
|
|
*ds = *msg;
|
|
|
|
i2p::data::netdb.PostI2NPMsg (ds);
|
|
|
|
}
|
|
|
|
tunnel->SendTunnelDataMsg (msg);
|
|
|
|
}
|
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
void Tunnels::ManageTunnels ()
|
2014-10-06 20:50:36 +04:00
|
|
|
{
|
|
|
|
ManagePendingTunnels ();
|
|
|
|
ManageInboundTunnels ();
|
|
|
|
ManageOutboundTunnels ();
|
|
|
|
ManageTransitTunnels ();
|
|
|
|
ManageTunnelPools ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::ManagePendingTunnels ()
|
2015-01-26 19:56:10 +03:00
|
|
|
{
|
|
|
|
ManagePendingTunnels (m_PendingInboundTunnels);
|
|
|
|
ManagePendingTunnels (m_PendingOutboundTunnels);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class PendingTunnels>
|
|
|
|
void Tunnels::ManagePendingTunnels (PendingTunnels& pendingTunnels)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-09-26 18:15:34 +04:00
|
|
|
// check pending tunnel. delete failed or timeout
|
2014-08-18 22:37:19 +04:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2015-01-26 19:56:10 +03:00
|
|
|
for (auto it = pendingTunnels.begin (); it != pendingTunnels.end ();)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-09-26 18:15:34 +04:00
|
|
|
auto tunnel = it->second;
|
|
|
|
switch (tunnel->GetState ())
|
2014-08-18 22:37:19 +04:00
|
|
|
{
|
2014-09-26 18:15:34 +04:00
|
|
|
case eTunnelStatePending:
|
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_CREATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
LogPrint ("Pending tunnel build request ", it->first, " timeout. Deleted");
|
2015-03-28 23:09:34 +03:00
|
|
|
// update stats
|
|
|
|
auto config = tunnel->GetTunnelConfig ();
|
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
auto hop = config->GetFirstHop ();
|
|
|
|
while (hop)
|
|
|
|
{
|
|
|
|
if (hop->router)
|
|
|
|
hop->router->GetProfile ()->TunnelNonReplied ();
|
|
|
|
hop = hop->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// delete
|
2015-01-26 19:56:10 +03:00
|
|
|
it = pendingTunnels.erase (it);
|
2015-02-28 15:59:34 +03:00
|
|
|
m_NumFailedTunnelCreations++;
|
2014-09-26 18:15:34 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
break;
|
|
|
|
case eTunnelStateBuildFailed:
|
|
|
|
LogPrint ("Pending tunnel build request ", it->first, " failed. Deleted");
|
2015-01-26 19:56:10 +03:00
|
|
|
it = pendingTunnels.erase (it);
|
2015-02-28 15:59:34 +03:00
|
|
|
m_NumFailedTunnelCreations++;
|
2014-09-26 18:15:34 +04:00
|
|
|
break;
|
|
|
|
case eTunnelStateBuildReplyReceived:
|
2015-01-27 22:55:46 +03:00
|
|
|
// intermediate state, will be either established of build failed
|
2014-08-18 22:37:19 +04:00
|
|
|
it++;
|
2014-09-26 18:15:34 +04:00
|
|
|
break;
|
|
|
|
default:
|
2015-02-28 15:59:34 +03:00
|
|
|
// success
|
2015-01-26 19:56:10 +03:00
|
|
|
it = pendingTunnels.erase (it);
|
2015-02-28 15:59:34 +03:00
|
|
|
m_NumSuccesiveTunnelCreations++;
|
2014-09-26 18:15:34 +04:00
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
2014-10-06 20:50:36 +04:00
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
void Tunnels::ManageOutboundTunnels ()
|
|
|
|
{
|
2014-01-09 23:56:07 +04:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-08-31 16:56:03 +04:00
|
|
|
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-09-14 03:43:25 +04:00
|
|
|
auto tunnel = *it;
|
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
2014-08-31 16:56:03 +04:00
|
|
|
{
|
2014-09-14 03:43:25 +04:00
|
|
|
LogPrint ("Tunnel ", tunnel->GetTunnelID (), " expired");
|
2014-10-06 00:18:24 +04:00
|
|
|
{
|
|
|
|
auto pool = tunnel->GetTunnelPool ();
|
|
|
|
if (pool)
|
|
|
|
pool->TunnelExpired (tunnel);
|
|
|
|
}
|
2015-01-27 04:49:16 +03:00
|
|
|
it = m_OutboundTunnels.erase (it);
|
2014-08-31 16:56:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-14 03:43:25 +04:00
|
|
|
if (tunnel->IsEstablished () && ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
|
|
|
tunnel->SetState (eTunnelStateExpiring);
|
2014-08-31 16:56:03 +04:00
|
|
|
it++;
|
|
|
|
}
|
2014-08-26 18:31:32 +04:00
|
|
|
}
|
2014-08-31 16:56:03 +04:00
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
|
2014-04-03 04:42:02 +04:00
|
|
|
if (m_OutboundTunnels.size () < 5)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
// trying to create one more oubound tunnel
|
2015-01-27 22:55:46 +03:00
|
|
|
auto inboundTunnel = GetNextInboundTunnel ();
|
2014-04-03 04:42:02 +04:00
|
|
|
if (!inboundTunnel) return;
|
|
|
|
LogPrint ("Creating one hop outbound tunnel...");
|
|
|
|
CreateTunnel<OutboundTunnel> (
|
2014-11-21 18:46:11 +03:00
|
|
|
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
|
2014-04-03 04:42:02 +04:00
|
|
|
{
|
2014-11-21 18:46:11 +03:00
|
|
|
i2p::data::netdb.GetRandomRouter ()
|
2014-04-03 04:42:02 +04:00
|
|
|
},
|
|
|
|
inboundTunnel->GetTunnelConfig ()));
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::ManageInboundTunnels ()
|
|
|
|
{
|
2014-01-09 23:56:07 +04:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-08-31 16:56:03 +04:00
|
|
|
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-09-14 03:43:25 +04:00
|
|
|
auto tunnel = it->second;
|
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
2014-08-31 16:56:03 +04:00
|
|
|
{
|
2014-09-14 03:43:25 +04:00
|
|
|
LogPrint ("Tunnel ", tunnel->GetTunnelID (), " expired");
|
2014-10-06 00:18:24 +04:00
|
|
|
{
|
|
|
|
auto pool = tunnel->GetTunnelPool ();
|
|
|
|
if (pool)
|
|
|
|
pool->TunnelExpired (tunnel);
|
|
|
|
}
|
2015-01-27 04:49:16 +03:00
|
|
|
it = m_InboundTunnels.erase (it);
|
2014-08-31 16:56:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-14 03:43:25 +04:00
|
|
|
if (tunnel->IsEstablished () && ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
|
|
|
tunnel->SetState (eTunnelStateExpiring);
|
2014-08-31 16:56:03 +04:00
|
|
|
it++;
|
|
|
|
}
|
2014-08-26 18:31:32 +04:00
|
|
|
}
|
2014-08-31 16:56:03 +04:00
|
|
|
}
|
2013-12-10 17:10:49 +04:00
|
|
|
|
|
|
|
if (m_InboundTunnels.empty ())
|
|
|
|
{
|
|
|
|
LogPrint ("Creating zero hops inbound tunnel...");
|
|
|
|
CreateZeroHopsInboundTunnel ();
|
2014-04-03 04:42:02 +04:00
|
|
|
if (!m_ExploratoryPool)
|
2014-12-16 05:24:01 +03:00
|
|
|
m_ExploratoryPool = CreateTunnelPool (&i2p::context, 2, 2); // 2-hop exploratory
|
2013-12-10 17:10:49 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
|
2014-04-03 04:42:02 +04:00
|
|
|
if (m_OutboundTunnels.empty () || m_InboundTunnels.size () < 5)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-04-03 04:42:02 +04:00
|
|
|
// trying to create one more inbound tunnel
|
|
|
|
LogPrint ("Creating one hop inbound tunnel...");
|
|
|
|
CreateTunnel<InboundTunnel> (
|
2014-11-21 18:46:11 +03:00
|
|
|
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
|
2014-04-03 04:42:02 +04:00
|
|
|
{
|
2014-11-21 18:46:11 +03:00
|
|
|
i2p::data::netdb.GetRandomRouter ()
|
2014-04-03 04:42:02 +04:00
|
|
|
}));
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 07:56:28 +04:00
|
|
|
|
|
|
|
void Tunnels::ManageTransitTunnels ()
|
|
|
|
{
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it = m_TransitTunnels.begin (); it != m_TransitTunnels.end ();)
|
|
|
|
{
|
|
|
|
if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
2014-07-04 03:53:11 +04:00
|
|
|
auto tmp = it->second;
|
2015-01-20 15:50:25 +03:00
|
|
|
LogPrint ("Transit tunnel ", tmp->GetTunnelID (), " expired");
|
2014-09-14 15:50:01 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
|
|
|
|
it = m_TransitTunnels.erase (it);
|
|
|
|
}
|
2014-07-04 03:53:11 +04:00
|
|
|
delete tmp;
|
2014-01-04 07:56:28 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-15 04:24:12 +04:00
|
|
|
|
|
|
|
void Tunnels::ManageTunnelPools ()
|
|
|
|
{
|
2014-10-05 19:01:12 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
2014-10-13 19:21:57 +04:00
|
|
|
for (auto it: m_Pools)
|
2014-03-18 00:50:03 +04:00
|
|
|
{
|
2015-01-20 06:28:13 +03:00
|
|
|
auto pool = it;
|
|
|
|
if (pool && pool->IsActive ())
|
2014-10-11 17:01:08 +04:00
|
|
|
{
|
|
|
|
pool->CreateTunnels ();
|
|
|
|
pool->TestTunnels ();
|
|
|
|
}
|
2014-03-18 00:50:03 +04:00
|
|
|
}
|
2014-03-15 04:24:12 +04:00
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
void Tunnels::PostTunnelData (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
if (msg) m_Queue.Put (msg);
|
|
|
|
}
|
|
|
|
|
2015-01-23 06:00:41 +03:00
|
|
|
void Tunnels::PostTunnelData (const std::vector<I2NPMessage *>& msgs)
|
|
|
|
{
|
|
|
|
m_Queue.Put (msgs);
|
|
|
|
}
|
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
template<class TTunnel>
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<TTunnel> Tunnels::CreateTunnel (TunnelConfig * config, std::shared_ptr<OutboundTunnel> outboundTunnel)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2015-01-27 22:55:46 +03:00
|
|
|
auto newTunnel = std::make_shared<TTunnel> (config);
|
2014-09-28 01:51:55 +04:00
|
|
|
uint32_t replyMsgID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
2015-01-26 19:56:10 +03:00
|
|
|
AddPendingTunnel (replyMsgID, newTunnel);
|
2014-09-28 01:51:55 +04:00
|
|
|
newTunnel->Build (replyMsgID, outboundTunnel);
|
2013-12-07 04:02:49 +04:00
|
|
|
return newTunnel;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void Tunnels::AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<InboundTunnel> tunnel)
|
2015-01-26 19:56:10 +03:00
|
|
|
{
|
|
|
|
m_PendingInboundTunnels[replyMsgID] = tunnel;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void Tunnels::AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> tunnel)
|
2015-01-26 19:56:10 +03:00
|
|
|
{
|
|
|
|
m_PendingOutboundTunnels[replyMsgID] = tunnel;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void Tunnels::AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2013-12-10 17:10:49 +04:00
|
|
|
m_OutboundTunnels.push_back (newTunnel);
|
2014-03-17 00:03:20 +04:00
|
|
|
auto pool = newTunnel->GetTunnelPool ();
|
2014-10-13 19:21:57 +04:00
|
|
|
if (pool && pool->IsActive ())
|
2014-03-17 00:03:20 +04:00
|
|
|
pool->TunnelCreated (newTunnel);
|
2014-10-13 19:21:57 +04:00
|
|
|
else
|
|
|
|
newTunnel->SetTunnelPool (nullptr);
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void Tunnels::AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel)
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-03-15 04:51:51 +04:00
|
|
|
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
|
2014-03-14 23:13:34 +04:00
|
|
|
auto pool = newTunnel->GetTunnelPool ();
|
2014-03-15 04:24:12 +04:00
|
|
|
if (!pool)
|
2014-03-15 04:51:51 +04:00
|
|
|
{
|
2014-03-14 23:13:34 +04:00
|
|
|
// build symmetric outbound tunnel
|
|
|
|
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ());
|
|
|
|
}
|
2014-03-15 04:24:12 +04:00
|
|
|
else
|
2014-10-13 19:21:57 +04:00
|
|
|
{
|
|
|
|
if (pool->IsActive ())
|
|
|
|
pool->TunnelCreated (newTunnel);
|
|
|
|
else
|
|
|
|
newTunnel->SetTunnelPool (nullptr);
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::CreateZeroHopsInboundTunnel ()
|
|
|
|
{
|
2013-12-10 17:10:49 +04:00
|
|
|
CreateTunnel<InboundTunnel> (
|
2014-11-21 18:46:11 +03:00
|
|
|
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
|
2014-01-09 04:30:47 +04:00
|
|
|
{
|
2014-11-21 18:46:11 +03:00
|
|
|
i2p::context.GetSharedRouterInfo ()
|
2014-01-09 04:30:47 +04:00
|
|
|
}));
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
2015-01-10 06:27:52 +03:00
|
|
|
|
|
|
|
int Tunnels::GetTransitTunnelsExpirationTimeout ()
|
|
|
|
{
|
|
|
|
int timeout = 0;
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
|
|
|
|
for (auto it: m_TransitTunnels)
|
|
|
|
{
|
|
|
|
int t = it.second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT - ts;
|
|
|
|
if (t > timeout) timeout = t;
|
|
|
|
}
|
|
|
|
return timeout;
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
}
|
|
|
|
}
|