mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-05 12:33:48 +01:00
recreate tunnels in random order
This commit is contained in:
parent
e8f5efd156
commit
b8d74dab47
2 changed files with 51 additions and 15 deletions
|
@ -281,6 +281,21 @@ namespace tunnel
|
||||||
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
|
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool InboundTunnel::Recreate ()
|
||||||
|
{
|
||||||
|
if (!IsRecreated ())
|
||||||
|
{
|
||||||
|
auto pool = GetTunnelPool ();
|
||||||
|
if (pool)
|
||||||
|
{
|
||||||
|
SetRecreated (true);
|
||||||
|
pool->RecreateInboundTunnel (std::static_pointer_cast<InboundTunnel>(shared_from_this ()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ZeroHopsInboundTunnel::ZeroHopsInboundTunnel ():
|
ZeroHopsInboundTunnel::ZeroHopsInboundTunnel ():
|
||||||
InboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
InboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
||||||
m_NumReceivedBytes (0)
|
m_NumReceivedBytes (0)
|
||||||
|
@ -331,6 +346,21 @@ namespace tunnel
|
||||||
LogPrint (eLogError, "Tunnel: Incoming message for outbound tunnel ", GetTunnelID ());
|
LogPrint (eLogError, "Tunnel: Incoming message for outbound tunnel ", GetTunnelID ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool OutboundTunnel::Recreate ()
|
||||||
|
{
|
||||||
|
if (!IsRecreated ())
|
||||||
|
{
|
||||||
|
auto pool = GetTunnelPool ();
|
||||||
|
if (pool)
|
||||||
|
{
|
||||||
|
SetRecreated (true);
|
||||||
|
pool->RecreateOutboundTunnel (std::static_pointer_cast<OutboundTunnel>(shared_from_this ()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ZeroHopsOutboundTunnel::ZeroHopsOutboundTunnel ():
|
ZeroHopsOutboundTunnel::ZeroHopsOutboundTunnel ():
|
||||||
OutboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
OutboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
||||||
m_NumSentBytes (0)
|
m_NumSentBytes (0)
|
||||||
|
@ -437,7 +467,7 @@ namespace tunnel
|
||||||
std::shared_ptr<OutboundTunnel> Tunnels::GetNextOutboundTunnel ()
|
std::shared_ptr<OutboundTunnel> Tunnels::GetNextOutboundTunnel ()
|
||||||
{
|
{
|
||||||
if (m_OutboundTunnels.empty ()) return nullptr;
|
if (m_OutboundTunnels.empty ()) return nullptr;
|
||||||
uint32_t ind = rand () % m_OutboundTunnels.size (), i = 0;
|
uint32_t ind = m_Rng () % m_OutboundTunnels.size (), i = 0;
|
||||||
std::shared_ptr<OutboundTunnel> tunnel;
|
std::shared_ptr<OutboundTunnel> tunnel;
|
||||||
for (const auto& it: m_OutboundTunnels)
|
for (const auto& it: m_OutboundTunnels)
|
||||||
{
|
{
|
||||||
|
@ -714,8 +744,17 @@ namespace tunnel
|
||||||
void Tunnels::ManageTunnels (uint64_t ts)
|
void Tunnels::ManageTunnels (uint64_t ts)
|
||||||
{
|
{
|
||||||
ManagePendingTunnels (ts);
|
ManagePendingTunnels (ts);
|
||||||
ManageInboundTunnels (ts);
|
std::vector<std::shared_ptr<Tunnel> > tunnelsToRecreate;
|
||||||
ManageOutboundTunnels (ts);
|
ManageInboundTunnels (ts, tunnelsToRecreate);
|
||||||
|
ManageOutboundTunnels (ts, tunnelsToRecreate);
|
||||||
|
// rec-create in random order
|
||||||
|
if (!tunnelsToRecreate.empty ())
|
||||||
|
{
|
||||||
|
if (tunnelsToRecreate.size () > 1)
|
||||||
|
std::shuffle (tunnelsToRecreate.begin(), tunnelsToRecreate.end(), m_Rng);
|
||||||
|
for (auto& it: tunnelsToRecreate)
|
||||||
|
it->Recreate ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::ManagePendingTunnels (uint64_t ts)
|
void Tunnels::ManagePendingTunnels (uint64_t ts)
|
||||||
|
@ -778,7 +817,7 @@ namespace tunnel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::ManageOutboundTunnels (uint64_t ts)
|
void Tunnels::ManageOutboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate)
|
||||||
{
|
{
|
||||||
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
|
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
|
||||||
{
|
{
|
||||||
|
@ -802,10 +841,7 @@ namespace tunnel
|
||||||
auto pool = tunnel->GetTunnelPool ();
|
auto pool = tunnel->GetTunnelPool ();
|
||||||
// let it die if the tunnel pool has been reconfigured and this is old
|
// let it die if the tunnel pool has been reconfigured and this is old
|
||||||
if (pool && tunnel->GetNumHops() == pool->GetNumOutboundHops())
|
if (pool && tunnel->GetNumHops() == pool->GetNumOutboundHops())
|
||||||
{
|
toRecreate.push_back (tunnel);
|
||||||
tunnel->SetRecreated (true);
|
|
||||||
pool->RecreateOutboundTunnel (tunnel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
||||||
tunnel->SetState (eTunnelStateExpiring);
|
tunnel->SetState (eTunnelStateExpiring);
|
||||||
|
@ -830,7 +866,7 @@ namespace tunnel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::ManageInboundTunnels (uint64_t ts)
|
void Tunnels::ManageInboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate)
|
||||||
{
|
{
|
||||||
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
|
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
|
||||||
{
|
{
|
||||||
|
@ -854,10 +890,7 @@ namespace tunnel
|
||||||
auto pool = tunnel->GetTunnelPool ();
|
auto pool = tunnel->GetTunnelPool ();
|
||||||
// let it die if the tunnel pool was reconfigured and has different number of hops
|
// let it die if the tunnel pool was reconfigured and has different number of hops
|
||||||
if (pool && tunnel->GetNumHops() == pool->GetNumInboundHops())
|
if (pool && tunnel->GetNumHops() == pool->GetNumInboundHops())
|
||||||
{
|
toRecreate.push_back (tunnel);
|
||||||
tunnel->SetRecreated (true);
|
|
||||||
pool->RecreateInboundTunnel (tunnel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
||||||
|
|
|
@ -99,6 +99,7 @@ namespace tunnel
|
||||||
void SetRecreated (bool recreated) { m_IsRecreated = recreated; };
|
void SetRecreated (bool recreated) { m_IsRecreated = recreated; };
|
||||||
int GetNumHops () const { return m_Hops.size (); };
|
int GetNumHops () const { return m_Hops.size (); };
|
||||||
virtual bool IsInbound() const = 0;
|
virtual bool IsInbound() const = 0;
|
||||||
|
virtual bool Recreate () = 0;
|
||||||
|
|
||||||
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
|
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
|
||||||
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
|
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
|
||||||
|
@ -150,6 +151,7 @@ namespace tunnel
|
||||||
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
||||||
|
|
||||||
bool IsInbound() const override { return false; }
|
bool IsInbound() const override { return false; }
|
||||||
|
bool Recreate () override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -166,6 +168,7 @@ namespace tunnel
|
||||||
void HandleTunnelDataMsg (std::shared_ptr<I2NPMessage>&& msg) override;
|
void HandleTunnelDataMsg (std::shared_ptr<I2NPMessage>&& msg) override;
|
||||||
virtual size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
|
virtual size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
|
||||||
bool IsInbound() const override { return true; }
|
bool IsInbound() const override { return true; }
|
||||||
|
bool Recreate () override;
|
||||||
|
|
||||||
// override TunnelBase
|
// override TunnelBase
|
||||||
void Cleanup () override { m_Endpoint.Cleanup (); };
|
void Cleanup () override { m_Endpoint.Cleanup (); };
|
||||||
|
@ -262,8 +265,8 @@ namespace tunnel
|
||||||
|
|
||||||
void Run ();
|
void Run ();
|
||||||
void ManageTunnels (uint64_t ts);
|
void ManageTunnels (uint64_t ts);
|
||||||
void ManageOutboundTunnels (uint64_t ts);
|
void ManageOutboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate);
|
||||||
void ManageInboundTunnels (uint64_t ts);
|
void ManageInboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate);
|
||||||
void ManagePendingTunnels (uint64_t ts);
|
void ManagePendingTunnels (uint64_t ts);
|
||||||
template<class PendingTunnels>
|
template<class PendingTunnels>
|
||||||
void ManagePendingTunnels (PendingTunnels& pendingTunnels, uint64_t ts);
|
void ManagePendingTunnels (PendingTunnels& pendingTunnels, uint64_t ts);
|
||||||
|
|
Loading…
Add table
Reference in a new issue