From e787165da931cabf1669eb0c67f84cb32c4b68bc Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 13 Dec 2018 07:27:14 -0500 Subject: [PATCH] expire old snode sessions --- llarp/exit/session.cpp | 23 +++++++++++++++++++---- llarp/exit/session.hpp | 5 +++++ llarp/service/endpoint.cpp | 11 +++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 009a3bbe2..e7324de09 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -13,6 +13,7 @@ namespace llarp , m_ExitRouter(router) , m_WritePacket(writepkt) , m_Counter(0) + , m_LastUse(0) { r->crypto.identity_keygen(m_ExitIdentity); } @@ -76,10 +77,10 @@ namespace llarp bool BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b) { + m_LastUse = router->Now(); if(b == 0) - { llarp::LogInfo("obtained an exit via ", p->Endpoint()); - } + return true; } @@ -88,7 +89,13 @@ namespace llarp { (void)p; if(m_WritePacket) - return m_WritePacket(pkt); + { + if(!m_WritePacket(pkt)) + return false; + m_LastUse = router->Now(); + return true; + } + return false; } @@ -133,9 +140,16 @@ namespace llarp return AvailablePaths(llarp::path::ePathRoleExit) > 0; } + bool + BaseSession::IsExpired(llarp_time_t now) const + { + return m_LastUse && now > m_LastUse && now - m_LastUse > LifeSpan; + } + bool BaseSession::FlushUpstreamTraffic() { + auto now = router->Now(); auto path = PickRandomEstablishedPath(llarp::path::ePathRoleExit); if(!path) { @@ -151,7 +165,8 @@ namespace llarp { auto& msg = queue.front(); msg.S = path->NextSeqNo(); - path->SendRoutingMessage(&msg, router); + if(path->SendRoutingMessage(&msg, router)) + m_LastUse = now; queue.pop_front(); } } diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index 0313ef851..c2ca86a8a 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -16,6 +16,7 @@ namespace llarp struct BaseSession : public llarp::path::Builder { static constexpr size_t MaxUpstreamQueueLength = 256; + static constexpr llarp_time_t LifeSpan = 60 * 10 * 1000; BaseSession(const llarp::RouterID& exitRouter, std::function< bool(llarp_buffer_t) > writepkt, @@ -48,6 +49,9 @@ namespace llarp return m_ExitRouter; } + bool + IsExpired(llarp_time_t now) const; + protected: llarp::RouterID m_ExitRouter; std::function< bool(llarp_buffer_t) > m_WritePacket; @@ -72,6 +76,7 @@ namespace llarp TieredQueue_t m_Upstream; uint64_t m_Counter; llarp::SecretKey m_ExitIdentity; + llarp_time_t m_LastUse; }; struct ExitSession final : public BaseSession diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 2c6f2c102..1af523a0e 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -142,6 +142,17 @@ namespace llarp { RegenAndPublishIntroSet(now); } + // expire snode sessions + { + auto itr = m_SNodeSessions.begin(); + while(itr != m_SNodeSessions.end()) + { + if(itr->second->IsExpired(now)) + itr = m_SNodeSessions.erase(itr); + else + ++itr; + } + } // expire pending tx { std::set< service::IntroSet > empty;