diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 8c8553922..5205d2a8a 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace llarp @@ -142,7 +143,8 @@ namespace llarp if (success) { - auto self = shared_from_this(); + auto self = std::static_pointer_cast(shared_from_this()); + for (auto& f : m_PendingCallbacks) f(self); } @@ -189,7 +191,6 @@ namespace llarp } }; ForEachPath(sendExitClose); - m_router->pathContext().RemovePathSet(shared_from_this()); return path::Builder::Stop(); } diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index d28227661..57bf29ab2 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -30,8 +30,7 @@ namespace llarp static constexpr auto LifeSpan = path::default_lifetime; /// a persisting exit session with an exit router - struct BaseSession : public llarp::path::Builder, - public std::enable_shared_from_this + struct BaseSession : public llarp::path::Builder { static constexpr size_t MaxUpstreamQueueLength = 256; @@ -45,18 +44,6 @@ namespace llarp ~BaseSession() override; - std::shared_ptr - GetSelf() override - { - return shared_from_this(); - } - - std::weak_ptr - GetWeak() override - { - return weak_from_this(); - } - void BlacklistSNode(const RouterID snode) override; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index f4c963385..414444697 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -161,12 +161,6 @@ namespace llarp return m_OwnedRanges; } - llarp_time_t - PathAlignmentTimeout() const override - { - return m_PathAlignmentTimeout; - } - /// ip packet against any exit policies we have /// returns false if this traffic is disallowed by any of those policies /// returns true otherwise diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index c66dca2ac..302069a89 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -27,7 +27,7 @@ namespace llarp { Path::Path( const std::vector& h, - std::weak_ptr pathset, + const std::shared_ptr& pathset, PathRole startingRoles, std::string shortName) : m_PathSet{std::move(pathset)}, _role{startingRoles}, m_shortName{std::move(shortName)} @@ -56,8 +56,8 @@ namespace llarp // initialize parts of the introduction intro.router = hops[hsz - 1].rc.pubkey; intro.pathID = hops[hsz - 1].txID; - if (auto parent = m_PathSet.lock()) - EnterState(ePathBuilding, parent->Now()); + + EnterState(ePathBuilding, pathset->Now()); } void diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index e453e6820..3f5a8aedb 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -95,7 +95,7 @@ namespace llarp Path( const std::vector& routers, - std::weak_ptr parent, + const std::shared_ptr& parent, PathRole startingRoles, std::string shortName); diff --git a/llarp/path/path_context.cpp b/llarp/path/path_context.cpp index 2fc3e0d0e..f3fe561e9 100644 --- a/llarp/path/path_context.cpp +++ b/llarp/path/path_context.cpp @@ -421,10 +421,6 @@ namespace llarp return nullptr; } - void - PathContext::RemovePathSet(PathSet_ptr) - {} - void PathContext::periodic_tick() { diff --git a/llarp/path/path_context.hpp b/llarp/path/path_context.hpp index 166d38c8d..473afe15d 100644 --- a/llarp/path/path_context.hpp +++ b/llarp/path/path_context.hpp @@ -111,9 +111,6 @@ namespace llarp void AddOwnPath(PathSet_ptr set, Path_ptr p); - void - RemovePathSet(PathSet_ptr set); - using TransitHopsMap_t = std::unordered_multimap; struct SyncTransitMap_t diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 65a6142e2..6508626c1 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -14,6 +14,7 @@ #include #include +#include namespace llarp { @@ -448,14 +449,17 @@ namespace llarp LogWarn(Name(), " building too fast to edge router ", edge); return; } + LogTrace("build one aligning to ", RouterID{hops.back().pubkey}); // async generate keys auto ctx = std::make_shared(); ctx->router = m_router; - auto self = GetSelf(); + auto self = shared_from_this(); + ctx->pathset = self; std::string path_shortName = "[path " + m_router->ShortName() + "-"; path_shortName = path_shortName + std::to_string(m_router->NextPathBuildNumber()) + "]"; - auto path = std::make_shared(hops, GetWeak(), roles, std::move(path_shortName)); + LogTrace("make ", path_shortName); + auto path = std::make_shared(hops, self, roles, std::move(path_shortName)); LogInfo(Name(), " build ", path->ShortName(), ": ", path->HopsString()); path->SetBuildResultHook([self](Path_ptr p) { self->HandlePathBuilt(p); }); diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index a01198974..7b8a6aab2 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace llarp diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index 0474544d2..8aaa9b50b 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace llarp @@ -67,7 +68,7 @@ namespace llarp { const auto now = llarp::time_now_ms(); Lock_t l{m_PathsMutex}; - for (auto& item : m_Paths) + for (const auto& item : m_Paths) { item.second->Tick(now, r); } @@ -77,17 +78,20 @@ namespace llarp PathSet::Tick(llarp_time_t) { std::unordered_set endpoints; - for (auto& item : m_Paths) + for (const auto& item : m_Paths) { endpoints.emplace(item.second->Endpoint()); } + if (endpoints.empty()) + return; + m_PathCache.clear(); for (const auto& ep : endpoints) { if (auto path = GetPathByRouter(ep)) { - m_PathCache[ep] = path->weak_from_this(); + m_PathCache.try_emplace(ep, std::weak_ptr{path}); } } } @@ -112,6 +116,7 @@ namespace llarp else ++itr; } + m_PathCache.clear(); } Path_ptr @@ -173,7 +178,8 @@ namespace llarp { if (auto itr = m_PathCache.find(id); itr != m_PathCache.end()) { - return itr->second.lock(); + if (auto ptr = itr->second.lock()) + chosen = ptr; } } auto itr = m_Paths.begin(); @@ -192,6 +198,7 @@ namespace llarp } ++itr; } + m_PathCache[id] = chosen; return chosen; } diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index f7fe5690e..1802ad69d 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -99,7 +100,7 @@ namespace llarp using PathSet_ptr = std::shared_ptr; /// a set of paths owned by an entity - struct PathSet + struct PathSet : std::enable_shared_from_this { /// maximum number of paths a path set can maintain static constexpr size_t max_paths = 32; @@ -107,14 +108,6 @@ namespace llarp /// @params numDesiredPaths the number of paths to maintain PathSet(size_t numDesiredPaths); - /// get a shared_ptr of ourself - virtual PathSet_ptr - GetSelf() = 0; - - /// get a weak_ptr of ourself - virtual std::weak_ptr - GetWeak() = 0; - virtual void BuildOne(PathRole roles = ePathRoleAny) = 0; @@ -319,7 +312,7 @@ namespace llarp PathMap_t m_Paths; private: - std::unordered_map> m_PathCache; + mutable std::unordered_map> m_PathCache; }; } // namespace path diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 9a4d28222..ab6c8fb54 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -74,18 +74,6 @@ namespace llarp m_quic = std::make_unique(*this); } - path::PathSet_ptr - Endpoint::GetSelf() - { - return std::static_pointer_cast(shared_from_this()); - } - - std::weak_ptr - Endpoint::GetWeak() - { - return std::weak_ptr{weak_from_this()}; - } - std::string_view Endpoint::endpoint_name() const { @@ -258,7 +246,7 @@ namespace llarp LookupNameAsync( name, - [ptr = std::static_pointer_cast(GetSelf()), + [ptr = std::static_pointer_cast(shared_from_this()), name, auth = AuthInfo{token}, ranges, diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 7a9858425..98a76aa49 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -66,8 +66,7 @@ namespace llarp struct Endpoint : public path::Builder, public ILookupHolder, public IDataHandler, - public EndpointBase, - private std::enable_shared_from_this + public EndpointBase { explicit Endpoint(AbstractRouter& r); ~Endpoint() override; @@ -330,7 +329,7 @@ namespace llarp bool ShouldBuildMore(llarp_time_t now) const override; - virtual llarp_time_t + constexpr llarp_time_t PathAlignmentTimeout() const { constexpr auto DefaultPathAlignmentTimeout = 10s; @@ -444,12 +443,6 @@ namespace llarp virtual void IntroSetPublished(); - path::PathSet_ptr - GetSelf() override; - - std::weak_ptr - GetWeak() override; - void AsyncProcessAuthMessage( std::shared_ptr msg, std::function hook); diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index 28b4a37af..fa4f0540a 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -200,12 +201,13 @@ namespace llarp path::Builder::HandlePathBuilt(p); p->SetDataHandler([self = weak_from_this()](auto path, auto frame) { if (auto ptr = self.lock()) - return ptr->HandleHiddenServiceFrame(path, frame); + return std::static_pointer_cast(ptr)->HandleHiddenServiceFrame( + path, frame); return false; }); p->SetDropHandler([self = weak_from_this()](auto path, auto id, auto seqno) { if (auto ptr = self.lock()) - return ptr->HandleDataDrop(path, id, seqno); + return std::static_pointer_cast(ptr)->HandleDataDrop(path, id, seqno); return false; }); if (markedBad) @@ -253,7 +255,8 @@ namespace llarp currentConvoTag, t); - ex->hook = [self = shared_from_this(), path](auto frame) { + ex->hook = [self = std::static_pointer_cast(shared_from_this()), + path](auto frame) { if (not self->Send(std::move(frame), path)) return; self->m_Endpoint->Loop()->call_later( @@ -296,7 +299,9 @@ namespace llarp { HiddenServiceAddressLookup* job = new HiddenServiceAddressLookup( m_Endpoint, - util::memFn(&OutboundContext::OnIntroSetUpdate, shared_from_this()), + util::memFn( + &OutboundContext::OnIntroSetUpdate, + std::static_pointer_cast(shared_from_this())), location, PubKey{addr.as_array()}, path->Endpoint(), diff --git a/llarp/service/outbound_context.hpp b/llarp/service/outbound_context.hpp index b005bcd19..61dd099e4 100644 --- a/llarp/service/outbound_context.hpp +++ b/llarp/service/outbound_context.hpp @@ -15,9 +15,7 @@ namespace llarp struct Endpoint; /// context needed to initiate an outbound hidden service session - struct OutboundContext : public path::Builder, - public SendContext, - public std::enable_shared_from_this + struct OutboundContext : public path::Builder, public SendContext { OutboundContext(const IntroSet& introSet, Endpoint* parent); @@ -35,18 +33,6 @@ namespace llarp bool ShouldBundleRC() const override; - path::PathSet_ptr - GetSelf() override - { - return shared_from_this(); - } - - std::weak_ptr - GetWeak() override - { - return weak_from_this(); - } - Address Addr() const;