From 97bae708172ce3309280f5459e2fd97d524097c3 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 24 Sep 2018 06:23:11 -0400 Subject: [PATCH] close sessions on link close not on destructor --- llarp/link/server.cpp | 18 ++++++++++++++ llarp/service.cpp | 7 +++--- llarp/service/endpoint.cpp | 48 ++++++++++++++++++++++++++------------ 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 51138dff5..baf22f1b0 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -127,6 +127,24 @@ namespace llarp { if(m_Logic && tick_id) llarp_logic_remove_call(m_Logic, tick_id); + { + Lock l(m_AuthedLinksMutex); + auto itr = m_AuthedLinks.begin(); + while(itr != m_AuthedLinks.end()) + { + itr->second->SendClose(); + itr = m_AuthedLinks.erase(itr); + } + } + { + Lock l(m_PendingMutex); + auto itr = m_Pending.begin(); + while(itr != m_Pending.end()) + { + (*itr)->SendClose(); + itr = m_Pending.erase(itr); + } + } } void diff --git a/llarp/service.cpp b/llarp/service.cpp index ce5946438..d0ab5df39 100644 --- a/llarp/service.cpp +++ b/llarp/service.cpp @@ -109,10 +109,9 @@ namespace llarp bool IntroSet::IsExpired(llarp_time_t now) const { - llarp_time_t highest = 0; - for(const auto& i : I) - highest = std::max(i.expiresAt, highest); - return highest < now; + if(now > T) + return false; + return now - T > DEFAULT_PATH_LIFETIME; } Introduction::~Introduction() diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 8fa381c51..6cdbcfc4f 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -102,26 +102,23 @@ namespace llarp ManualRebuild(1); return; } - IntroSet introset = m_IntroSet; - introset.I.clear(); + m_IntroSet.I.clear(); for(const auto& intro : I) { - llarp::LogInfo(intro); if(!intro.ExpiresSoon(now)) - introset.I.push_back(intro); + m_IntroSet.I.push_back(intro); } - if(introset.I.size() == 0) + if(m_IntroSet.I.size() == 0) { llarp::LogWarn("not enough intros to publish introset for ", Name()); return; } - introset.topic = m_Tag; - if(!m_Identity.SignIntroSet(introset, &m_Router->crypto)) + m_IntroSet.topic = m_Tag; + if(!m_Identity.SignIntroSet(m_IntroSet, &m_Router->crypto)) { llarp::LogWarn("failed to sign introset for endpoint ", Name()); return; } - m_IntroSet = introset; if(PublishIntroSet(m_Router)) { llarp::LogInfo("(re)publishing introset for endpoint ", Name()); @@ -483,9 +480,11 @@ namespace llarp bool Endpoint::PublishIntroSet(llarp_router* r) { + // publish via near router auto path = GetEstablishedPathClosestTo(m_Identity.pub.Addr().data()); if(path && PublishIntroSetVia(r, path)) { + // publish via far router path = PickRandomEstablishedPath(); return path && PublishIntroSetVia(r, path); } @@ -718,13 +717,12 @@ namespace llarp Endpoint::OutboundContext::HandleDataDrop(path::Path* p, const PathID_t& dst, uint64_t seq) { - llarp::LogWarn(Name(), " message ", seq, " dropped by endpoint ", - p->Endpoint(), " via ", dst); // pick another intro if(dst == remoteIntro.pathID && remoteIntro.router == p->Endpoint()) { + llarp::LogWarn(Name(), " message ", seq, " dropped by endpoint ", + p->Endpoint(), " via ", dst); MarkCurrentIntroBad(); - ShiftIntroduction(); UpdateIntroSet(); } return true; @@ -865,7 +863,8 @@ namespace llarp { updatingIntroSet = false; - ShiftIntroduction(); + if(intro.I.size()) + remoteIntro = intro.I[0]; } Endpoint::OutboundContext::~OutboundContext() @@ -989,7 +988,29 @@ namespace llarp void Endpoint::OutboundContext::MarkCurrentIntroBad() { + auto now = llarp_time_now_ms(); + bool shifted = false; + // insert bad intro m_BadIntros.insert(remoteIntro); + // shift off current intro + for(const auto& intro : currentIntroSet.I) + { + if(m_BadIntros.count(intro) == 0 && !intro.ExpiresSoon(now)) + { + shifted = intro.router != remoteIntro.router; + remoteIntro = intro; + break; + } + } + // don't rebuild paths rapidly + if(now - lastShift < MIN_SHIFT_INTERVAL) + return; + // rebuild path if shifted + if(shifted) + { + lastShift = now; + ManualRebuild(1); + } } void @@ -1152,7 +1173,6 @@ namespace llarp if(remoteIntro.ExpiresSoon(now)) { MarkCurrentIntroBad(); - ShiftIntroduction(); } routing::PathTransferMessage transfer(msg, remoteIntro.pathID); if(!path->SendRoutingMessage(&transfer, m_Endpoint->Router())) @@ -1201,7 +1221,6 @@ namespace llarp if(remoteIntro.ExpiresSoon(now)) { MarkCurrentIntroBad(); - ShiftIntroduction(); } m_Endpoint->EnsureRouterIsKnown(remoteIntro.router); auto itr = m_BadIntros.begin(); @@ -1276,7 +1295,6 @@ namespace llarp { // shift intro MarkCurrentIntroBad(); - ShiftIntroduction(); } auto path = m_PathSet->GetNewestPathByRouter(remoteIntro.router);