Browse Source

check if clock was adjusted backwards

pull/1872/head
orignal 2 years ago
parent
commit
aac9a8d18f
  1. 15
      libi2pd/NetDb.cpp
  2. 144
      libi2pd/Tunnel.cpp
  3. 12
      libi2pd/Tunnel.h

15
libi2pd/NetDb.cpp

@ -153,13 +153,13 @@ namespace data
if (!i2p::transport::transports.IsOnline ()) continue; // don't manage netdb when offline if (!i2p::transport::transports.IsOnline ()) continue; // don't manage netdb when offline
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
if (ts - lastManageRequest >= 15) // manage requests every 15 seconds if (ts - lastManageRequest >= 15 || ts + 15 < lastManageRequest) // manage requests every 15 seconds
{ {
m_Requests.ManageRequests (); m_Requests.ManageRequests ();
lastManageRequest = ts; lastManageRequest = ts;
} }
if (ts - lastSave >= 60) // save routers, manage leasesets and validate subscriptions every minute if (ts - lastSave >= 60 || ts + 60 < lastSave) // save routers, manage leasesets and validate subscriptions every minute
{ {
if (lastSave) if (lastSave)
{ {
@ -169,13 +169,15 @@ namespace data
lastSave = ts; lastSave = ts;
} }
if (ts - lastDestinationCleanup >= i2p::garlic::INCOMING_TAGS_EXPIRATION_TIMEOUT) if (ts - lastDestinationCleanup >= i2p::garlic::INCOMING_TAGS_EXPIRATION_TIMEOUT ||
ts + i2p::garlic::INCOMING_TAGS_EXPIRATION_TIMEOUT < lastDestinationCleanup)
{ {
i2p::context.CleanupDestination (); i2p::context.CleanupDestination ();
lastDestinationCleanup = ts; lastDestinationCleanup = ts;
} }
if (ts - lastProfilesCleanup >= (uint64_t)(i2p::data::PEER_PROFILE_AUTOCLEAN_TIMEOUT + profilesCleanupVariance)) if (ts - lastProfilesCleanup >= (uint64_t)(i2p::data::PEER_PROFILE_AUTOCLEAN_TIMEOUT + profilesCleanupVariance) ||
ts + i2p::data::PEER_PROFILE_AUTOCLEAN_TIMEOUT < lastProfilesCleanup)
{ {
DeleteObsoleteProfiles (); DeleteObsoleteProfiles ();
lastProfilesCleanup = ts; lastProfilesCleanup = ts;
@ -192,7 +194,8 @@ namespace data
if (ts - lastPublish >= NETDB_PUBLISH_CONFIRMATION_TIMEOUT) publish = true; if (ts - lastPublish >= NETDB_PUBLISH_CONFIRMATION_TIMEOUT) publish = true;
} }
else if (i2p::context.GetLastUpdateTime () > lastPublish || else if (i2p::context.GetLastUpdateTime () > lastPublish ||
ts - lastPublish >= NETDB_PUBLISH_INTERVAL) ts - lastPublish >= NETDB_PUBLISH_INTERVAL ||
ts + NETDB_PUBLISH_INTERVAL < lastPublish)
{ {
// new publish // new publish
m_PublishExcluded.clear (); m_PublishExcluded.clear ();
@ -208,7 +211,7 @@ namespace data
} }
} }
if (ts - lastExploratory >= 30) // exploratory every 30 seconds if (ts - lastExploratory >= 30 || ts + 30 < lastExploratory) // exploratory every 30 seconds
{ {
auto numRouters = m_RouterInfos.size (); auto numRouters = m_RouterInfos.size ();
if (!numRouters) if (!numRouters)

144
libi2pd/Tunnel.cpp

@ -537,17 +537,20 @@ namespace tunnel
if (i2p::transport::transports.IsOnline()) if (i2p::transport::transports.IsOnline())
{ {
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
if (ts - lastTs >= TUNNEL_MANAGE_INTERVAL) // manage tunnels every 15 seconds if (ts - lastTs >= TUNNEL_MANAGE_INTERVAL || // manage tunnels every 15 seconds
ts + TUNNEL_MANAGE_INTERVAL < lastTs)
{ {
ManageTunnels (); ManageTunnels (ts);
lastTs = ts; lastTs = ts;
} }
if (ts - lastPoolsTs >= TUNNEL_POOLS_MANAGE_INTERVAL) // manage pools every 5 seconds if (ts - lastPoolsTs >= TUNNEL_POOLS_MANAGE_INTERVAL || // manage pools every 5 secondsts
ts + TUNNEL_POOLS_MANAGE_INTERVAL < lastPoolsTs)
{ {
ManageTunnelPools (ts); ManageTunnelPools (ts);
lastPoolsTs = ts; lastPoolsTs = ts;
} }
if (ts - lastMemoryPoolTs >= TUNNEL_MEMORY_POOL_MANAGE_INTERVAL) // manage memory pool every 2 minutes if (ts - lastMemoryPoolTs >= TUNNEL_MEMORY_POOL_MANAGE_INTERVAL ||
ts + TUNNEL_MEMORY_POOL_MANAGE_INTERVAL < lastMemoryPoolTs) // manage memory pool every 2 minutes
{ {
m_I2NPTunnelEndpointMessagesMemoryPool.CleanUpMt (); m_I2NPTunnelEndpointMessagesMemoryPool.CleanUpMt ();
m_I2NPTunnelMessagesMemoryPool.CleanUpMt (); m_I2NPTunnelMessagesMemoryPool.CleanUpMt ();
@ -589,32 +592,32 @@ namespace tunnel
tunnel->SendTunnelDataMsg (msg); tunnel->SendTunnelDataMsg (msg);
} }
void Tunnels::ManageTunnels () void Tunnels::ManageTunnels (uint64_t ts)
{ {
ManagePendingTunnels (); ManagePendingTunnels (ts);
ManageInboundTunnels (); ManageInboundTunnels (ts);
ManageOutboundTunnels (); ManageOutboundTunnels (ts);
ManageTransitTunnels (); ManageTransitTunnels (ts);
} }
void Tunnels::ManagePendingTunnels () void Tunnels::ManagePendingTunnels (uint64_t ts)
{ {
ManagePendingTunnels (m_PendingInboundTunnels); ManagePendingTunnels (m_PendingInboundTunnels, ts);
ManagePendingTunnels (m_PendingOutboundTunnels); ManagePendingTunnels (m_PendingOutboundTunnels, ts);
} }
template<class PendingTunnels> template<class PendingTunnels>
void Tunnels::ManagePendingTunnels (PendingTunnels& pendingTunnels) void Tunnels::ManagePendingTunnels (PendingTunnels& pendingTunnels, uint64_t ts)
{ {
// check pending tunnel. delete failed or timeout // check pending tunnel. delete failed or timeout
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
for (auto it = pendingTunnels.begin (); it != pendingTunnels.end ();) for (auto it = pendingTunnels.begin (); it != pendingTunnels.end ();)
{ {
auto tunnel = it->second; auto tunnel = it->second;
switch (tunnel->GetState ()) switch (tunnel->GetState ())
{ {
case eTunnelStatePending: case eTunnelStatePending:
if (ts > tunnel->GetCreationTime () + TUNNEL_CREATION_TIMEOUT) if (ts > tunnel->GetCreationTime () + TUNNEL_CREATION_TIMEOUT ||
ts + TUNNEL_CREATION_TIMEOUT < tunnel->GetCreationTime ())
{ {
LogPrint (eLogDebug, "Tunnel: Pending build request ", it->first, " timeout, deleted"); LogPrint (eLogDebug, "Tunnel: Pending build request ", it->first, " timeout, deleted");
// update stats // update stats
@ -657,41 +660,38 @@ namespace tunnel
} }
} }
void Tunnels::ManageOutboundTunnels () void Tunnels::ManageOutboundTunnels (uint64_t ts)
{ {
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
{ {
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();) auto tunnel = *it;
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
{ {
auto tunnel = *it; LogPrint (eLogDebug, "Tunnel: Tunnel with id ", tunnel->GetTunnelID (), " expired");
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) auto pool = tunnel->GetTunnelPool ();
{ if (pool)
LogPrint (eLogDebug, "Tunnel: Tunnel with id ", tunnel->GetTunnelID (), " expired"); pool->TunnelExpired (tunnel);
auto pool = tunnel->GetTunnelPool (); // we don't have outbound tunnels in m_Tunnels
if (pool) it = m_OutboundTunnels.erase (it);
pool->TunnelExpired (tunnel); }
// we don't have outbound tunnels in m_Tunnels else
it = m_OutboundTunnels.erase (it); {
} if (tunnel->IsEstablished ())
else
{ {
if (tunnel->IsEstablished ()) if (!tunnel->IsRecreated () && ts + TUNNEL_RECREATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
{ {
if (!tunnel->IsRecreated () && ts + TUNNEL_RECREATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) auto pool = tunnel->GetTunnelPool ();
// let it die if the tunnel pool has been reconfigured and this is old
if (pool && tunnel->GetNumHops() == pool->GetNumOutboundHops())
{ {
auto pool = tunnel->GetTunnelPool (); tunnel->SetRecreated (true);
// let it die if the tunnel pool has been reconfigured and this is old pool->RecreateOutboundTunnel (tunnel);
if (pool && tunnel->GetNumHops() == pool->GetNumOutboundHops())
{
tunnel->SetRecreated (true);
pool->RecreateOutboundTunnel (tunnel);
}
} }
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
tunnel->SetState (eTunnelStateExpiring);
} }
++it; if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
tunnel->SetState (eTunnelStateExpiring);
} }
++it;
} }
} }
@ -711,44 +711,42 @@ namespace tunnel
} }
} }
void Tunnels::ManageInboundTunnels () void Tunnels::ManageInboundTunnels (uint64_t ts)
{ {
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
{ {
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();) auto tunnel = *it;
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT ||
ts + TUNNEL_EXPIRATION_TIMEOUT < tunnel->GetCreationTime ())
{ {
auto tunnel = *it; LogPrint (eLogDebug, "Tunnel: Tunnel with id ", tunnel->GetTunnelID (), " expired");
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) auto pool = tunnel->GetTunnelPool ();
{ if (pool)
LogPrint (eLogDebug, "Tunnel: Tunnel with id ", tunnel->GetTunnelID (), " expired"); pool->TunnelExpired (tunnel);
auto pool = tunnel->GetTunnelPool (); m_Tunnels.erase (tunnel->GetTunnelID ());
if (pool) it = m_InboundTunnels.erase (it);
pool->TunnelExpired (tunnel); }
m_Tunnels.erase (tunnel->GetTunnelID ()); else
it = m_InboundTunnels.erase (it); {
} if (tunnel->IsEstablished ())
else
{ {
if (tunnel->IsEstablished ()) if (!tunnel->IsRecreated () && ts + TUNNEL_RECREATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
{ {
if (!tunnel->IsRecreated () && ts + TUNNEL_RECREATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) auto pool = tunnel->GetTunnelPool ();
// let it die if the tunnel pool was reconfigured and has different number of hops
if (pool && tunnel->GetNumHops() == pool->GetNumInboundHops())
{ {
auto pool = tunnel->GetTunnelPool (); tunnel->SetRecreated (true);
// let it die if the tunnel pool was reconfigured and has different number of hops pool->RecreateInboundTunnel (tunnel);
if (pool && tunnel->GetNumHops() == pool->GetNumInboundHops())
{
tunnel->SetRecreated (true);
pool->RecreateInboundTunnel (tunnel);
}
} }
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
tunnel->SetState (eTunnelStateExpiring);
else // we don't need to cleanup expiring tunnels
tunnel->Cleanup ();
} }
it++;
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
tunnel->SetState (eTunnelStateExpiring);
else // we don't need to cleanup expiring tunnels
tunnel->Cleanup ();
} }
it++;
} }
} }
@ -787,13 +785,13 @@ namespace tunnel
} }
} }
void Tunnels::ManageTransitTunnels () void Tunnels::ManageTransitTunnels (uint64_t ts)
{ {
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 ();)
{ {
auto tunnel = *it; auto tunnel = *it;
if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) if (ts > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT ||
ts + TUNNEL_EXPIRATION_TIMEOUT < tunnel->GetCreationTime ())
{ {
LogPrint (eLogDebug, "Tunnel: Transit tunnel with id ", tunnel->GetTunnelID (), " expired"); LogPrint (eLogDebug, "Tunnel: Transit tunnel with id ", tunnel->GetTunnelID (), " expired");
m_Tunnels.erase (tunnel->GetTunnelID ()); m_Tunnels.erase (tunnel->GetTunnelID ());

12
libi2pd/Tunnel.h

@ -241,13 +241,13 @@ namespace tunnel
void HandleTunnelGatewayMsg (std::shared_ptr<TunnelBase> tunnel, std::shared_ptr<I2NPMessage> msg); void HandleTunnelGatewayMsg (std::shared_ptr<TunnelBase> tunnel, std::shared_ptr<I2NPMessage> msg);
void Run (); void Run ();
void ManageTunnels (); void ManageTunnels (uint64_t ts);
void ManageOutboundTunnels (); void ManageOutboundTunnels (uint64_t ts);
void ManageInboundTunnels (); void ManageInboundTunnels (uint64_t ts);
void ManageTransitTunnels (); void ManageTransitTunnels (uint64_t ts);
void ManagePendingTunnels (); void ManagePendingTunnels (uint64_t ts);
template<class PendingTunnels> template<class PendingTunnels>
void ManagePendingTunnels (PendingTunnels& pendingTunnels); void ManagePendingTunnels (PendingTunnels& pendingTunnels, uint64_t ts);
void ManageTunnelPools (uint64_t ts); void ManageTunnelPools (uint64_t ts);
std::shared_ptr<ZeroHopsInboundTunnel> CreateZeroHopsInboundTunnel (std::shared_ptr<TunnelPool> pool); std::shared_ptr<ZeroHopsInboundTunnel> CreateZeroHopsInboundTunnel (std::shared_ptr<TunnelPool> pool);

Loading…
Cancel
Save