expire old snode sessions

pull/145/head
Jeff Becker 6 years ago
parent bb0cd81f02
commit e787165da9
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -13,6 +13,7 @@ namespace llarp
, m_ExitRouter(router) , m_ExitRouter(router)
, m_WritePacket(writepkt) , m_WritePacket(writepkt)
, m_Counter(0) , m_Counter(0)
, m_LastUse(0)
{ {
r->crypto.identity_keygen(m_ExitIdentity); r->crypto.identity_keygen(m_ExitIdentity);
} }
@ -76,10 +77,10 @@ namespace llarp
bool bool
BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b) BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b)
{ {
m_LastUse = router->Now();
if(b == 0) if(b == 0)
{
llarp::LogInfo("obtained an exit via ", p->Endpoint()); llarp::LogInfo("obtained an exit via ", p->Endpoint());
}
return true; return true;
} }
@ -88,7 +89,13 @@ namespace llarp
{ {
(void)p; (void)p;
if(m_WritePacket) if(m_WritePacket)
return m_WritePacket(pkt); {
if(!m_WritePacket(pkt))
return false;
m_LastUse = router->Now();
return true;
}
return false; return false;
} }
@ -133,9 +140,16 @@ namespace llarp
return AvailablePaths(llarp::path::ePathRoleExit) > 0; 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 bool
BaseSession::FlushUpstreamTraffic() BaseSession::FlushUpstreamTraffic()
{ {
auto now = router->Now();
auto path = PickRandomEstablishedPath(llarp::path::ePathRoleExit); auto path = PickRandomEstablishedPath(llarp::path::ePathRoleExit);
if(!path) if(!path)
{ {
@ -151,7 +165,8 @@ namespace llarp
{ {
auto& msg = queue.front(); auto& msg = queue.front();
msg.S = path->NextSeqNo(); msg.S = path->NextSeqNo();
path->SendRoutingMessage(&msg, router); if(path->SendRoutingMessage(&msg, router))
m_LastUse = now;
queue.pop_front(); queue.pop_front();
} }
} }

@ -16,6 +16,7 @@ namespace llarp
struct BaseSession : public llarp::path::Builder struct BaseSession : public llarp::path::Builder
{ {
static constexpr size_t MaxUpstreamQueueLength = 256; static constexpr size_t MaxUpstreamQueueLength = 256;
static constexpr llarp_time_t LifeSpan = 60 * 10 * 1000;
BaseSession(const llarp::RouterID& exitRouter, BaseSession(const llarp::RouterID& exitRouter,
std::function< bool(llarp_buffer_t) > writepkt, std::function< bool(llarp_buffer_t) > writepkt,
@ -48,6 +49,9 @@ namespace llarp
return m_ExitRouter; return m_ExitRouter;
} }
bool
IsExpired(llarp_time_t now) const;
protected: protected:
llarp::RouterID m_ExitRouter; llarp::RouterID m_ExitRouter;
std::function< bool(llarp_buffer_t) > m_WritePacket; std::function< bool(llarp_buffer_t) > m_WritePacket;
@ -72,6 +76,7 @@ namespace llarp
TieredQueue_t m_Upstream; TieredQueue_t m_Upstream;
uint64_t m_Counter; uint64_t m_Counter;
llarp::SecretKey m_ExitIdentity; llarp::SecretKey m_ExitIdentity;
llarp_time_t m_LastUse;
}; };
struct ExitSession final : public BaseSession struct ExitSession final : public BaseSession

@ -142,6 +142,17 @@ namespace llarp
{ {
RegenAndPublishIntroSet(now); 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 // expire pending tx
{ {
std::set< service::IntroSet > empty; std::set< service::IntroSet > empty;

Loading…
Cancel
Save