2014-01-09 19:56:07 +00:00
|
|
|
#include "I2PEndian.h"
|
2014-01-09 22:55:53 +00:00
|
|
|
#include <thread>
|
2014-08-16 18:35:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2013-12-07 00:02:49 +00: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 00:56:42 +00:00
|
|
|
Tunnel::Tunnel (TunnelConfig * config):
|
|
|
|
m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Tunnel::~Tunnel ()
|
|
|
|
{
|
|
|
|
delete m_Config;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnel::Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel)
|
|
|
|
{
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
2014-08-16 18:35:35 +00:00
|
|
|
auto numHops = m_Config->GetNumHops ();
|
|
|
|
int numRecords = numHops <= STANDARD_NUM_RECORDS ? STANDARD_NUM_RECORDS : numHops;
|
2013-12-07 00:02:49 +00:00
|
|
|
I2NPMessage * msg = NewI2NPMessage ();
|
|
|
|
*msg->GetPayload () = numRecords;
|
2014-08-16 18:35:35 +00:00
|
|
|
msg->len += numRecords*sizeof (I2NPBuildRequestRecordElGamalEncrypted) + 1;
|
|
|
|
|
|
|
|
// 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 00:02:49 +00:00
|
|
|
|
2014-08-16 18:35:35 +00:00
|
|
|
// create real records
|
|
|
|
I2NPBuildRequestRecordElGamalEncrypted * records = (I2NPBuildRequestRecordElGamalEncrypted *)(msg->GetPayload () + 1);
|
2013-12-07 00:02:49 +00:00
|
|
|
TunnelHopConfig * hop = m_Config->GetFirstHop ();
|
|
|
|
int i = 0;
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-08-16 18:35:35 +00:00
|
|
|
int idx = recordIndicies[i];
|
2013-12-07 00:02:49 +00:00
|
|
|
EncryptBuildRequestRecord (*hop->router,
|
|
|
|
CreateBuildRequestRecord (hop->router->GetIdentHash (),
|
|
|
|
hop->tunnelID,
|
|
|
|
hop->nextRouter->GetIdentHash (),
|
|
|
|
hop->nextTunnelID,
|
|
|
|
hop->layerKey, hop->ivKey,
|
|
|
|
hop->replyKey, hop->replyIV,
|
|
|
|
hop->next ? rnd.GenerateWord32 () : replyMsgID, // we set replyMsgID for last hop only
|
|
|
|
hop->isGateway, hop->isEndpoint),
|
2014-08-16 18:35:35 +00:00
|
|
|
records[idx]);
|
|
|
|
hop->recordIndex = idx;
|
2013-12-07 00:02:49 +00:00
|
|
|
i++;
|
|
|
|
hop = hop->next;
|
|
|
|
}
|
2014-06-19 01:24:24 +00:00
|
|
|
// fill up fake records with random data
|
2014-08-16 18:35:35 +00:00
|
|
|
for (int i = numHops; i < numRecords; i++)
|
2014-06-19 01:24:24 +00:00
|
|
|
{
|
2014-08-16 18:35:35 +00:00
|
|
|
int idx = recordIndicies[i];
|
|
|
|
rnd.GenerateBlock ((uint8_t *)(records + idx), sizeof (records[idx]));
|
2014-06-19 01:24:24 +00:00
|
|
|
}
|
2014-08-16 18:35:35 +00:00
|
|
|
|
|
|
|
// decrypt real records
|
2014-05-15 22:58:26 +00:00
|
|
|
i2p::crypto::CBCDecryption decryption;
|
2013-12-07 00:02:49 +00:00
|
|
|
hop = m_Config->GetLastHop ()->prev;
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-15 22:58:26 +00:00
|
|
|
decryption.SetKey (hop->replyKey);
|
2014-06-18 23:38:21 +00:00
|
|
|
// decrypt records after current hop
|
|
|
|
TunnelHopConfig * hop1 = hop->next;
|
|
|
|
while (hop1)
|
|
|
|
{
|
2014-06-24 23:33:30 +00:00
|
|
|
decryption.SetIV (hop->replyIV);
|
2014-06-18 23:38:21 +00:00
|
|
|
decryption.Decrypt((uint8_t *)&records[hop1->recordIndex],
|
|
|
|
sizeof (I2NPBuildRequestRecordElGamalEncrypted),
|
|
|
|
(uint8_t *)&records[hop1->recordIndex]);
|
|
|
|
hop1 = hop1->next;
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
hop = hop->prev;
|
|
|
|
}
|
|
|
|
FillI2NPMessageHeader (msg, eI2NPVariableTunnelBuild);
|
2014-08-16 18:35:35 +00:00
|
|
|
|
|
|
|
// send message
|
2013-12-07 00:02:49 +00:00
|
|
|
if (outboundTunnel)
|
2014-01-06 02:25:48 +00:00
|
|
|
outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg);
|
2013-12-07 00:02:49 +00:00
|
|
|
else
|
2014-10-21 16:25:53 +00:00
|
|
|
i2p::transport::transports.SendMessage (GetNextIdentHash (), msg);
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Tunnel::HandleTunnelBuildResponse (uint8_t * msg, size_t len)
|
|
|
|
{
|
|
|
|
LogPrint ("TunnelBuildResponse ", (int)msg[0], " records.");
|
2014-06-18 23:38:21 +00:00
|
|
|
|
2014-05-15 22:58:26 +00:00
|
|
|
i2p::crypto::CBCDecryption decryption;
|
2013-12-07 00:02:49 +00:00
|
|
|
TunnelHopConfig * hop = m_Config->GetLastHop ();
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-15 22:58:26 +00:00
|
|
|
decryption.SetKey (hop->replyKey);
|
2014-06-18 23:38:21 +00:00
|
|
|
// decrypt records before and including current hop
|
|
|
|
TunnelHopConfig * hop1 = hop;
|
|
|
|
while (hop1)
|
|
|
|
{
|
2014-06-19 01:24:24 +00:00
|
|
|
auto idx = hop1->recordIndex;
|
|
|
|
if (idx >= 0 && idx < msg[0])
|
|
|
|
{
|
|
|
|
uint8_t * record = msg + 1 + idx*sizeof (I2NPBuildResponseRecord);
|
2014-06-24 23:33:30 +00:00
|
|
|
decryption.SetIV (hop->replyIV);
|
2014-06-19 01:24:24 +00:00
|
|
|
decryption.Decrypt(record, sizeof (I2NPBuildResponseRecord), record);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Tunnel hop index ", idx, " is out of range");
|
2014-06-18 23:38:21 +00:00
|
|
|
hop1 = hop1->prev;
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
hop = hop->prev;
|
|
|
|
}
|
|
|
|
|
2014-07-27 00:56:42 +00:00
|
|
|
bool established = true;
|
2014-06-19 01:24:24 +00:00
|
|
|
hop = m_Config->GetFirstHop ();
|
|
|
|
while (hop)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-06-19 01:24:24 +00:00
|
|
|
I2NPBuildResponseRecord * record = (I2NPBuildResponseRecord *)(msg + 1 + hop->recordIndex*sizeof (I2NPBuildResponseRecord));
|
2013-12-07 00:02:49 +00:00
|
|
|
LogPrint ("Ret code=", (int)record->ret);
|
|
|
|
if (record->ret)
|
|
|
|
// if any of participants declined the tunnel is not established
|
2014-07-27 00:56:42 +00:00
|
|
|
established = false;
|
2014-06-19 01:24:24 +00:00
|
|
|
hop = hop->next;
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
2014-07-27 00:56:42 +00:00
|
|
|
if (established)
|
2014-05-09 23:34:12 +00:00
|
|
|
{
|
|
|
|
// change reply keys to layer keys
|
2014-06-19 01:24:24 +00:00
|
|
|
hop = m_Config->GetFirstHop ();
|
2014-05-09 23:34:12 +00:00
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-15 22:58:26 +00:00
|
|
|
hop->decryption.SetKeys (hop->layerKey, hop->ivKey);
|
2014-05-09 23:34:12 +00:00
|
|
|
hop = hop->next;
|
|
|
|
}
|
|
|
|
}
|
2014-07-27 00:56:42 +00:00
|
|
|
if (established) m_State = eTunnelStateEstablished;
|
|
|
|
return established;
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg)
|
|
|
|
{
|
|
|
|
uint8_t * payload = tunnelMsg->GetPayload () + 4;
|
|
|
|
TunnelHopConfig * hop = m_Config->GetLastHop ();
|
|
|
|
while (hop)
|
|
|
|
{
|
2014-05-15 22:58:26 +00:00
|
|
|
hop->decryption.Decrypt (payload);
|
2013-12-07 00:02:49 +00:00
|
|
|
hop = hop->prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InboundTunnel::HandleTunnelDataMsg (I2NPMessage * msg)
|
|
|
|
{
|
2014-07-27 00:56:42 +00:00
|
|
|
if (IsFailed ()) SetState (eTunnelStateEstablished); // incoming messages means a tunnel is alive
|
2014-03-13 00:13:49 +00:00
|
|
|
msg->from = this;
|
2013-12-07 00:02:49 +00:00
|
|
|
EncryptTunnelMsg (msg);
|
|
|
|
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutboundTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
|
|
|
|
{
|
2014-02-20 03:08:57 +00: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 16:19:12 +00:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
2014-02-20 03:08:57 +00:00
|
|
|
m_Gateway.SendTunnelDataMsg (block);
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
2014-01-20 23:37:51 +00:00
|
|
|
|
2014-10-07 14:33:17 +00:00
|
|
|
void OutboundTunnel::SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-04-03 16:19:12 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
2014-01-20 23:37:51 +00:00
|
|
|
for (auto& it : msgs)
|
2014-02-20 02:24:55 +00:00
|
|
|
m_Gateway.PutTunnelDataMsg (it);
|
2014-01-20 23:37:51 +00:00
|
|
|
m_Gateway.SendBuffer ();
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
2014-01-20 23:37:51 +00:00
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
Tunnels tunnels;
|
|
|
|
|
2014-09-27 21:51:55 +00:00
|
|
|
Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr), m_ExploratoryPool (nullptr)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Tunnels::~Tunnels ()
|
|
|
|
{
|
|
|
|
for (auto& it : m_OutboundTunnels)
|
|
|
|
delete it;
|
|
|
|
m_OutboundTunnels.clear ();
|
|
|
|
|
|
|
|
for (auto& it : m_InboundTunnels)
|
|
|
|
delete it.second;
|
|
|
|
m_InboundTunnels.clear ();
|
|
|
|
|
|
|
|
for (auto& it : m_TransitTunnels)
|
|
|
|
delete it.second;
|
|
|
|
m_TransitTunnels.clear ();
|
|
|
|
|
2014-08-18 18:37:19 +00:00
|
|
|
/*for (auto& it : m_PendingTunnels)
|
2013-12-07 00:02:49 +00:00
|
|
|
delete it.second;
|
2014-08-18 18:37:19 +00:00
|
|
|
m_PendingTunnels.clear ();*/
|
2014-03-15 00:24:12 +00:00
|
|
|
|
|
|
|
for (auto& it: m_Pools)
|
2014-12-10 02:07:54 +00:00
|
|
|
delete it;
|
2014-03-15 00:24:12 +00:00
|
|
|
m_Pools.clear ();
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tunnel * Tunnels::GetPendingTunnel (uint32_t replyMsgID)
|
|
|
|
{
|
|
|
|
auto it = m_PendingTunnels.find(replyMsgID);
|
2014-09-28 19:06:07 +00:00
|
|
|
if (it != m_PendingTunnels.end () && it->second->GetState () == eTunnelStatePending)
|
2014-09-26 14:15:34 +00:00
|
|
|
{
|
|
|
|
it->second->SetState (eTunnelStateBuildReplyReceived);
|
2014-08-18 18:37:19 +00:00
|
|
|
return it->second;
|
2014-09-26 14:15:34 +00:00
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
InboundTunnel * Tunnels::GetNextInboundTunnel ()
|
|
|
|
{
|
|
|
|
InboundTunnel * tunnel = nullptr;
|
|
|
|
size_t minReceived = 0;
|
2014-10-02 16:44:11 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2013-12-07 00:02:49 +00:00
|
|
|
for (auto it : m_InboundTunnels)
|
2014-03-21 19:54:55 +00:00
|
|
|
{
|
2014-08-26 14:31:32 +00:00
|
|
|
if (!it.second->IsEstablished ()) continue;
|
2013-12-07 00:02:49 +00:00
|
|
|
if (!tunnel || it.second->GetNumReceivedBytes () < minReceived)
|
|
|
|
{
|
|
|
|
tunnel = it.second;
|
|
|
|
minReceived = it.second->GetNumReceivedBytes ();
|
2014-03-21 19:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
return tunnel;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutboundTunnel * Tunnels::GetNextOutboundTunnel ()
|
|
|
|
{
|
2013-12-29 15:48:57 +00:00
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
|
|
|
uint32_t ind = rnd.GenerateWord32 (0, m_OutboundTunnels.size () - 1), i = 0;
|
2014-03-21 19:54:55 +00:00
|
|
|
OutboundTunnel * tunnel = nullptr;
|
2014-10-02 16:44:11 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2013-12-29 15:48:57 +00:00
|
|
|
for (auto it: m_OutboundTunnels)
|
|
|
|
{
|
2014-08-26 14:31:32 +00:00
|
|
|
if (it->IsEstablished ())
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
|
|
|
tunnel = it;
|
2014-03-21 19:54:55 +00:00
|
|
|
i++;
|
|
|
|
}
|
2014-08-29 11:44:12 +00:00
|
|
|
if (i > ind && tunnel) break;
|
2014-03-21 19:54:55 +00:00
|
|
|
}
|
|
|
|
return tunnel;
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
2014-03-15 00:24:12 +00:00
|
|
|
|
2014-12-16 02:24:01 +00:00
|
|
|
TunnelPool * Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops)
|
2014-03-15 00:24:12 +00:00
|
|
|
{
|
2014-11-30 03:00:52 +00:00
|
|
|
auto pool = new TunnelPool (localDestination, numInboundHops, numOutboundHops);
|
2014-10-05 15:01:12 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
2014-12-10 02:07:54 +00:00
|
|
|
m_Pools.push_back (pool);
|
2014-03-15 00:51:51 +00:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::DeleteTunnelPool (TunnelPool * pool)
|
|
|
|
{
|
2014-10-11 13:47:24 +00:00
|
|
|
if (pool)
|
|
|
|
{
|
2014-10-13 15:21:57 +00:00
|
|
|
StopTunnelPool (pool);
|
2014-12-10 02:07:54 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
|
|
|
m_Pools.remove (pool);
|
|
|
|
}
|
2014-10-13 15:21:57 +00:00
|
|
|
for (auto it: m_PendingTunnels)
|
|
|
|
if (it.second->GetTunnelPool () == pool)
|
|
|
|
it.second->SetTunnelPool (nullptr);
|
|
|
|
delete pool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::StopTunnelPool (TunnelPool * pool)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
{
|
|
|
|
pool->SetActive (false);
|
2014-10-11 13:47:24 +00:00
|
|
|
pool->DetachTunnels ();
|
|
|
|
}
|
2014-03-15 00:24:12 +00:00
|
|
|
}
|
2014-10-13 15:21:57 +00:00
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
void Tunnels::AddTransitTunnel (TransitTunnel * tunnel)
|
|
|
|
{
|
2014-09-14 11:50:01 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
|
2013-12-07 00:02:49 +00:00
|
|
|
m_TransitTunnels[tunnel->GetTunnelID ()] = tunnel;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09 22:55:53 +00:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready
|
2013-12-07 00:02:49 +00:00
|
|
|
|
2014-01-09 19:56:07 +00:00
|
|
|
uint64_t lastTs = 0;
|
2013-12-07 00:02:49 +00:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
I2NPMessage * msg = m_Queue.GetNextWithTimeout (1000); // 1 sec
|
|
|
|
while (msg)
|
|
|
|
{
|
|
|
|
uint32_t tunnelID = be32toh (*(uint32_t *)msg->GetPayload ());
|
|
|
|
InboundTunnel * tunnel = GetInboundTunnel (tunnelID);
|
|
|
|
if (tunnel)
|
|
|
|
tunnel->HandleTunnelDataMsg (msg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TransitTunnel * transitTunnel = GetTransitTunnel (tunnelID);
|
|
|
|
if (transitTunnel)
|
|
|
|
transitTunnel->HandleTunnelDataMsg (msg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("Tunnel ", tunnelID, " not found");
|
|
|
|
i2p::DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg = m_Queue.Get ();
|
|
|
|
}
|
|
|
|
|
2014-01-09 19:56:07 +00:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2013-12-07 00:02:49 +00:00
|
|
|
if (ts - lastTs >= 15) // manage tunnels every 15 seconds
|
|
|
|
{
|
|
|
|
ManageTunnels ();
|
|
|
|
lastTs = ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint ("Tunnels: ", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::ManageTunnels ()
|
2014-10-06 16:50:36 +00:00
|
|
|
{
|
|
|
|
ManagePendingTunnels ();
|
|
|
|
ManageInboundTunnels ();
|
|
|
|
ManageOutboundTunnels ();
|
|
|
|
ManageTransitTunnels ();
|
|
|
|
ManageTunnelPools ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::ManagePendingTunnels ()
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-09-26 14:15:34 +00:00
|
|
|
// check pending tunnel. delete failed or timeout
|
2014-08-18 18:37:19 +00:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it = m_PendingTunnels.begin (); it != m_PendingTunnels.end ();)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-09-26 14:15:34 +00:00
|
|
|
auto tunnel = it->second;
|
|
|
|
switch (tunnel->GetState ())
|
2014-08-18 18:37:19 +00:00
|
|
|
{
|
2014-09-26 14:15:34 +00:00
|
|
|
case eTunnelStatePending:
|
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_CREATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
LogPrint ("Pending tunnel build request ", it->first, " timeout. Deleted");
|
|
|
|
delete tunnel;
|
|
|
|
it = m_PendingTunnels.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
break;
|
|
|
|
case eTunnelStateBuildFailed:
|
|
|
|
LogPrint ("Pending tunnel build request ", it->first, " failed. Deleted");
|
|
|
|
delete tunnel;
|
2014-08-18 18:37:19 +00:00
|
|
|
it = m_PendingTunnels.erase (it);
|
2014-09-26 14:15:34 +00:00
|
|
|
break;
|
|
|
|
case eTunnelStateBuildReplyReceived:
|
|
|
|
// intermidiate state, will be either established of build failed
|
2014-08-18 18:37:19 +00:00
|
|
|
it++;
|
2014-09-26 14:15:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
it = m_PendingTunnels.erase (it);
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
2014-10-06 16:50:36 +00:00
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
|
|
|
|
void Tunnels::ManageOutboundTunnels ()
|
|
|
|
{
|
2014-01-09 19:56:07 +00:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-08-31 12:56:03 +00:00
|
|
|
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-09-13 23:43:25 +00:00
|
|
|
auto tunnel = *it;
|
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
2014-08-31 12:56:03 +00:00
|
|
|
{
|
2014-09-13 23:43:25 +00:00
|
|
|
LogPrint ("Tunnel ", tunnel->GetTunnelID (), " expired");
|
2014-10-05 20:18:24 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
|
|
|
auto pool = tunnel->GetTunnelPool ();
|
|
|
|
if (pool)
|
|
|
|
pool->TunnelExpired (tunnel);
|
|
|
|
}
|
2014-10-01 18:32:57 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
|
|
|
it = m_OutboundTunnels.erase (it);
|
|
|
|
}
|
2014-09-20 20:26:36 +00:00
|
|
|
delete tunnel;
|
2014-08-31 12:56:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-13 23:43:25 +00:00
|
|
|
if (tunnel->IsEstablished () && ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
|
|
|
tunnel->SetState (eTunnelStateExpiring);
|
2014-08-31 12:56:03 +00:00
|
|
|
it++;
|
|
|
|
}
|
2014-08-26 14:31:32 +00:00
|
|
|
}
|
2014-08-31 12:56:03 +00:00
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
|
2014-04-03 00:42:02 +00:00
|
|
|
if (m_OutboundTunnels.size () < 5)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
|
|
|
// trying to create one more oubound tunnel
|
2013-12-10 13:10:49 +00:00
|
|
|
InboundTunnel * inboundTunnel = GetNextInboundTunnel ();
|
2014-04-03 00:42:02 +00:00
|
|
|
if (!inboundTunnel) return;
|
|
|
|
LogPrint ("Creating one hop outbound tunnel...");
|
|
|
|
CreateTunnel<OutboundTunnel> (
|
2014-11-21 15:46:11 +00:00
|
|
|
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
|
2014-04-03 00:42:02 +00:00
|
|
|
{
|
2014-11-21 15:46:11 +00:00
|
|
|
i2p::data::netdb.GetRandomRouter ()
|
2014-04-03 00:42:02 +00:00
|
|
|
},
|
|
|
|
inboundTunnel->GetTunnelConfig ()));
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::ManageInboundTunnels ()
|
|
|
|
{
|
2014-01-09 19:56:07 +00:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-08-31 12:56:03 +00:00
|
|
|
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-09-13 23:43:25 +00:00
|
|
|
auto tunnel = it->second;
|
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
2014-08-31 12:56:03 +00:00
|
|
|
{
|
2014-09-13 23:43:25 +00:00
|
|
|
LogPrint ("Tunnel ", tunnel->GetTunnelID (), " expired");
|
2014-10-05 20:18:24 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
|
|
|
auto pool = tunnel->GetTunnelPool ();
|
|
|
|
if (pool)
|
|
|
|
pool->TunnelExpired (tunnel);
|
|
|
|
}
|
2014-10-01 18:32:57 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
|
|
|
it = m_InboundTunnels.erase (it);
|
|
|
|
}
|
2014-09-20 20:26:36 +00:00
|
|
|
delete tunnel;
|
2014-08-31 12:56:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-13 23:43:25 +00:00
|
|
|
if (tunnel->IsEstablished () && ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
|
|
|
tunnel->SetState (eTunnelStateExpiring);
|
2014-08-31 12:56:03 +00:00
|
|
|
it++;
|
|
|
|
}
|
2014-08-26 14:31:32 +00:00
|
|
|
}
|
2014-08-31 12:56:03 +00:00
|
|
|
}
|
2013-12-10 13:10:49 +00:00
|
|
|
|
|
|
|
if (m_InboundTunnels.empty ())
|
|
|
|
{
|
|
|
|
LogPrint ("Creating zero hops inbound tunnel...");
|
|
|
|
CreateZeroHopsInboundTunnel ();
|
2014-04-03 00:42:02 +00:00
|
|
|
if (!m_ExploratoryPool)
|
2014-12-16 02:24:01 +00:00
|
|
|
m_ExploratoryPool = CreateTunnelPool (&i2p::context, 2, 2); // 2-hop exploratory
|
2013-12-10 13:10:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
|
2014-04-03 00:42:02 +00:00
|
|
|
if (m_OutboundTunnels.empty () || m_InboundTunnels.size () < 5)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-04-03 00:42:02 +00:00
|
|
|
// trying to create one more inbound tunnel
|
|
|
|
LogPrint ("Creating one hop inbound tunnel...");
|
|
|
|
CreateTunnel<InboundTunnel> (
|
2014-11-21 15:46:11 +00:00
|
|
|
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
|
2014-04-03 00:42:02 +00:00
|
|
|
{
|
2014-11-21 15:46:11 +00:00
|
|
|
i2p::data::netdb.GetRandomRouter ()
|
2014-04-03 00:42:02 +00:00
|
|
|
}));
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 03:56:28 +00: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)
|
|
|
|
{
|
|
|
|
LogPrint ("Transit tunnel ", it->second->GetTunnelID (), " expired");
|
2014-07-03 23:53:11 +00:00
|
|
|
auto tmp = it->second;
|
2014-09-14 11:50:01 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
|
|
|
|
it = m_TransitTunnels.erase (it);
|
|
|
|
}
|
2014-07-03 23:53:11 +00:00
|
|
|
delete tmp;
|
2014-01-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-15 00:24:12 +00:00
|
|
|
|
|
|
|
void Tunnels::ManageTunnelPools ()
|
|
|
|
{
|
2014-10-05 15:01:12 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PoolsMutex);
|
2014-10-13 15:21:57 +00:00
|
|
|
for (auto it: m_Pools)
|
2014-03-17 20:50:03 +00:00
|
|
|
{
|
2014-12-10 02:07:54 +00:00
|
|
|
TunnelPool * pool = it;
|
2014-10-13 15:21:57 +00:00
|
|
|
if (pool->IsActive ())
|
2014-10-11 13:01:08 +00:00
|
|
|
{
|
|
|
|
pool->CreateTunnels ();
|
|
|
|
pool->TestTunnels ();
|
|
|
|
}
|
2014-03-17 20:50:03 +00:00
|
|
|
}
|
2014-03-15 00:24:12 +00:00
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
|
|
|
|
void Tunnels::PostTunnelData (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
if (msg) m_Queue.Put (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class TTunnel>
|
|
|
|
TTunnel * Tunnels::CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel)
|
|
|
|
{
|
|
|
|
TTunnel * newTunnel = new TTunnel (config);
|
2014-09-27 21:51:55 +00:00
|
|
|
uint32_t replyMsgID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
|
|
|
m_PendingTunnels[replyMsgID] = newTunnel;
|
|
|
|
newTunnel->Build (replyMsgID, outboundTunnel);
|
2013-12-07 00:02:49 +00:00
|
|
|
return newTunnel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::AddOutboundTunnel (OutboundTunnel * newTunnel)
|
|
|
|
{
|
2014-08-31 12:56:03 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
2013-12-10 13:10:49 +00:00
|
|
|
m_OutboundTunnels.push_back (newTunnel);
|
2014-03-16 20:03:20 +00:00
|
|
|
auto pool = newTunnel->GetTunnelPool ();
|
2014-10-13 15:21:57 +00:00
|
|
|
if (pool && pool->IsActive ())
|
2014-03-16 20:03:20 +00:00
|
|
|
pool->TunnelCreated (newTunnel);
|
2014-10-13 15:21:57 +00:00
|
|
|
else
|
|
|
|
newTunnel->SetTunnelPool (nullptr);
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel)
|
|
|
|
{
|
2014-08-31 12:56:03 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
2014-03-15 00:51:51 +00:00
|
|
|
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel;
|
2014-03-14 19:13:34 +00:00
|
|
|
auto pool = newTunnel->GetTunnelPool ();
|
2014-03-15 00:24:12 +00:00
|
|
|
if (!pool)
|
2014-03-15 00:51:51 +00:00
|
|
|
{
|
2014-03-14 19:13:34 +00:00
|
|
|
// build symmetric outbound tunnel
|
|
|
|
CreateTunnel<OutboundTunnel> (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ());
|
|
|
|
}
|
2014-03-15 00:24:12 +00:00
|
|
|
else
|
2014-10-13 15:21:57 +00:00
|
|
|
{
|
|
|
|
if (pool->IsActive ())
|
|
|
|
pool->TunnelCreated (newTunnel);
|
|
|
|
else
|
|
|
|
newTunnel->SetTunnelPool (nullptr);
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::CreateZeroHopsInboundTunnel ()
|
|
|
|
{
|
2013-12-10 13:10:49 +00:00
|
|
|
CreateTunnel<InboundTunnel> (
|
2014-11-21 15:46:11 +00:00
|
|
|
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
|
2014-01-09 00:30:47 +00:00
|
|
|
{
|
2014-11-21 15:46:11 +00:00
|
|
|
i2p::context.GetSharedRouterInfo ()
|
2014-01-09 00:30:47 +00:00
|
|
|
}));
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|