|
|
@ -267,10 +267,10 @@ namespace tunnel |
|
|
|
return nullptr; |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<TransitTunnel> Tunnels::GetTransitTunnel (uint32_t tunnelID) |
|
|
|
std::shared_ptr<TunnelBase> Tunnels::GetTunnel (uint32_t tunnelID) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto it = m_TransitTunnels.find(tunnelID); |
|
|
|
auto it = m_Tunnels.find(tunnelID); |
|
|
|
if (it != m_TransitTunnels.end ()) |
|
|
|
if (it != m_Tunnels.end ()) |
|
|
|
return it->second; |
|
|
|
return it->second; |
|
|
|
return nullptr; |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
} |
|
|
@ -362,9 +362,10 @@ namespace tunnel |
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::AddTransitTunnel (std::shared_ptr<TransitTunnel> tunnel) |
|
|
|
void Tunnels::AddTransitTunnel (std::shared_ptr<TransitTunnel> tunnel) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex); |
|
|
|
if (m_Tunnels.emplace (tunnel->GetTunnelID (), tunnel).second) |
|
|
|
if (!m_TransitTunnels.insert (std::make_pair (tunnel->GetTunnelID (), tunnel)).second) |
|
|
|
m_TransitTunnels.push_back (tunnel); |
|
|
|
LogPrint (eLogError, "Tunnel: transit tunnel with id ", tunnel->GetTunnelID (), " already exists"); |
|
|
|
else |
|
|
|
|
|
|
|
LogPrint (eLogError, "Tunnel: tunnel with id ", tunnel->GetTunnelID (), " already exists"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::Start () |
|
|
|
void Tunnels::Start () |
|
|
@ -414,10 +415,8 @@ namespace tunnel |
|
|
|
else if (prevTunnel) |
|
|
|
else if (prevTunnel) |
|
|
|
prevTunnel->FlushTunnelDataMsgs (); |
|
|
|
prevTunnel->FlushTunnelDataMsgs (); |
|
|
|
|
|
|
|
|
|
|
|
if (!tunnel && typeID == eI2NPTunnelData) |
|
|
|
|
|
|
|
tunnel = GetInboundTunnel (tunnelID); |
|
|
|
|
|
|
|
if (!tunnel) |
|
|
|
if (!tunnel) |
|
|
|
tunnel = GetTransitTunnel (tunnelID); |
|
|
|
tunnel = GetTunnel (tunnelID); |
|
|
|
if (tunnel) |
|
|
|
if (tunnel) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (typeID == eI2NPTunnelData) |
|
|
|
if (typeID == eI2NPTunnelData) |
|
|
@ -574,6 +573,7 @@ namespace tunnel |
|
|
|
auto pool = tunnel->GetTunnelPool (); |
|
|
|
auto pool = tunnel->GetTunnelPool (); |
|
|
|
if (pool) |
|
|
|
if (pool) |
|
|
|
pool->TunnelExpired (tunnel); |
|
|
|
pool->TunnelExpired (tunnel); |
|
|
|
|
|
|
|
// we don't have outbound tunnels in m_Tunnels
|
|
|
|
it = m_OutboundTunnels.erase (it); |
|
|
|
it = m_OutboundTunnels.erase (it); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
@ -622,6 +622,7 @@ namespace tunnel |
|
|
|
auto pool = tunnel->GetTunnelPool (); |
|
|
|
auto pool = tunnel->GetTunnelPool (); |
|
|
|
if (pool) |
|
|
|
if (pool) |
|
|
|
pool->TunnelExpired (tunnel); |
|
|
|
pool->TunnelExpired (tunnel); |
|
|
|
|
|
|
|
m_Tunnels.erase (tunnel->GetTunnelID ()); |
|
|
|
it = m_InboundTunnels.erase (it); |
|
|
|
it = m_InboundTunnels.erase (it); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
@ -676,15 +677,13 @@ namespace tunnel |
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
for (auto it = m_TransitTunnels.begin (); it != m_TransitTunnels.end ();) |
|
|
|
for (auto it = m_TransitTunnels.begin (); it != m_TransitTunnels.end ();) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) |
|
|
|
auto tunnel = *it; |
|
|
|
{ |
|
|
|
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) |
|
|
|
auto tmp = it->second; |
|
|
|
|
|
|
|
LogPrint (eLogDebug, "Tunnel: Transit tunnel with id ", tmp->GetTunnelID (), " expired"); |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex); |
|
|
|
LogPrint (eLogDebug, "Tunnel: Transit tunnel with id ", tunnel->GetTunnelID (), " expired"); |
|
|
|
|
|
|
|
m_Tunnels.erase (tunnel->GetTunnelID ()); |
|
|
|
it = m_TransitTunnels.erase (it); |
|
|
|
it = m_TransitTunnels.erase (it); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
else |
|
|
|
it++; |
|
|
|
it++; |
|
|
|
} |
|
|
|
} |
|
|
@ -737,6 +736,7 @@ namespace tunnel |
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel) |
|
|
|
void Tunnels::AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// we don't need to insert it to m_Tunnels
|
|
|
|
m_OutboundTunnels.push_back (newTunnel); |
|
|
|
m_OutboundTunnels.push_back (newTunnel); |
|
|
|
auto pool = newTunnel->GetTunnelPool (); |
|
|
|
auto pool = newTunnel->GetTunnelPool (); |
|
|
|
if (pool && pool->IsActive ()) |
|
|
|
if (pool && pool->IsActive ()) |
|
|
@ -746,6 +746,8 @@ namespace tunnel |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel) |
|
|
|
void Tunnels::AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (m_Tunnels.emplace (newTunnel->GetTunnelID (), newTunnel).second) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel; |
|
|
|
m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel; |
|
|
|
auto pool = newTunnel->GetTunnelPool (); |
|
|
|
auto pool = newTunnel->GetTunnelPool (); |
|
|
@ -764,6 +766,9 @@ namespace tunnel |
|
|
|
newTunnel->SetTunnelPool (nullptr); |
|
|
|
newTunnel->SetTunnelPool (nullptr); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LogPrint (eLogError, "Tunnel: tunnel with id ", newTunnel->GetTunnelID (), " already exists"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Tunnels::CreateZeroHopsInboundTunnel () |
|
|
|
void Tunnels::CreateZeroHopsInboundTunnel () |
|
|
@ -779,10 +784,10 @@ namespace tunnel |
|
|
|
{ |
|
|
|
{ |
|
|
|
int timeout = 0; |
|
|
|
int timeout = 0; |
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex); |
|
|
|
// TODO: possible race condition with I2PControl
|
|
|
|
for (auto it: m_TransitTunnels) |
|
|
|
for (auto it: m_TransitTunnels) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int t = it.second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT - ts; |
|
|
|
int t = it->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT - ts; |
|
|
|
if (t > timeout) timeout = t; |
|
|
|
if (t > timeout) timeout = t; |
|
|
|
} |
|
|
|
} |
|
|
|
return timeout; |
|
|
|
return timeout; |
|
|
|