diff --git a/daemon/main.cpp b/daemon/main.cpp index cf9e2bf41..06e054ef7 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -186,8 +186,8 @@ main(int argc, char *argv[]) // does this file exist? if(genconfigOnly) { - if(!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(), overWrite, - asRouter)) + if(!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(), + overWrite, asRouter)) return 1; } else diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 112508205..5c2adee54 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -25,8 +25,7 @@ namespace llarp { } - void - BaseSession::HandlePathDied(path::Path_ptr) + void BaseSession::HandlePathDied(path::Path_ptr) { } @@ -135,8 +134,9 @@ namespace llarp { if(success) { + auto self = shared_from_this(); for(auto& f : m_PendingCallbacks) - f(this); + f(self); } else { @@ -161,6 +161,7 @@ namespace llarp } }; ForEachPath(sendExitClose); + router->pathContext().RemovePathSet(shared_from_this()); return llarp::path::Builder::Stop(); } diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index 3f6b9b421..974d528f0 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -15,10 +15,13 @@ namespace llarp { struct BaseSession; - using SessionReadyFunc = std::function< void(BaseSession*) >; + using BaseSession_ptr = std::shared_ptr< BaseSession >; + + using SessionReadyFunc = std::function< void(BaseSession_ptr) >; /// a persisting exit session with an exit router - struct BaseSession : public llarp::path::Builder + struct BaseSession : public llarp::path::Builder, + public std::enable_shared_from_this< BaseSession > { static constexpr size_t MaxUpstreamQueueLength = 256; static constexpr llarp_time_t LifeSpan = 60 * 10 * 1000; @@ -29,6 +32,12 @@ namespace llarp virtual ~BaseSession(); + std::shared_ptr< path::PathSet > + GetSelf() override + { + return shared_from_this(); + } + util::StatusObject ExtractStatus() const; diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 47e708cb0..0c60a2c3b 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -552,14 +552,13 @@ namespace llarp huint32_t ip = GetIPForIdent(pubKey); if(m_SNodeKeys.emplace(pubKey).second) { - // this is a new service node make an outbound session to them - m_SNodeSessions.emplace( + auto session = std::make_shared< exit::SNodeSession >( other, - std::unique_ptr< exit::SNodeSession >(new exit::SNodeSession( - other, - std::bind(&ExitEndpoint::QueueSNodePacket, this, - std::placeholders::_1, ip), - GetRouter(), 2, 1, true))); + std::bind(&ExitEndpoint::QueueSNodePacket, this, + std::placeholders::_1, ip), + GetRouter(), 2, 1, true); + // this is a new service node make an outbound session to them + m_SNodeSessions.emplace(other, session); } return ip; } @@ -625,7 +624,10 @@ namespace llarp if(itr->second->IsExpired(now)) itr = m_SNodeSessions.erase(itr); else + { + itr->second->Tick(now); ++itr; + } } } { diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index fff883b2c..18dc28f42 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -147,7 +147,7 @@ namespace llarp SNodes_t m_SNodeKeys; using SNodeSessions_t = - std::unordered_map< RouterID, std::unique_ptr< exit::SNodeSession >, + std::unordered_map< RouterID, std::shared_ptr< exit::SNodeSession >, RouterID::Hash >; /// snode sessions we are talking to directly SNodeSessions_t m_SNodeSessions; diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index 009847614..fee9d4f8f 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -7,7 +7,9 @@ namespace llarp { namespace handlers { - struct NullEndpoint final : public llarp::service::Endpoint + struct NullEndpoint final + : public llarp::service::Endpoint, + public std::enable_shared_from_this< NullEndpoint > { NullEndpoint(const std::string &name, AbstractRouter *r, llarp::service::Context *parent) @@ -20,6 +22,12 @@ namespace llarp return true; } + path::PathSet_ptr + GetSelf() override + { + return shared_from_this(); + } + huint32_t ObtainIPForAddr(const AlignedBuffer< 32 > &, bool) override { diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 001dda7ea..b30fe1fb7 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -102,11 +102,11 @@ namespace llarp llarp::LogError(Name(), " bad exit router key: ", v); return false; } - m_Exit.reset(new llarp::exit::ExitSession( + m_Exit = std::make_shared< llarp::exit::ExitSession >( exitRouter, std::bind(&TunEndpoint::QueueInboundPacketForExit, this, std::placeholders::_1), - router, m_NumPaths, numHops)); + router, m_NumPaths, numHops); llarp::LogInfo(Name(), " using exit at ", exitRouter); } if(k == "local-dns") @@ -349,11 +349,11 @@ namespace llarp else if(addr.FromString(qname, ".snode")) { dns::Message *replyMsg = new dns::Message(std::move(msg)); - EnsurePathToSNode(addr.as_array(), - [=](const RouterID &remote, exit::BaseSession *s) { - SendDNSReply(remote, s, replyMsg, reply, true, - isV6); - }); + EnsurePathToSNode( + addr.as_array(), + [=](const RouterID &remote, exit::BaseSession_ptr s) { + SendDNSReply(remote, s, replyMsg, reply, true, isV6); + }); return true; } else @@ -580,7 +580,10 @@ namespace llarp // call tun code in endpoint logic in case of network isolation // EndpointLogic()->queue_job({this, handleTickTun}); if(m_Exit) + { EnsureRouterIsKnown(m_Exit->Endpoint()); + m_Exit->Tick(now); + } Endpoint::Tick(now); } diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 4615727e8..93683c051 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -20,12 +20,20 @@ namespace llarp static const char DefaultTunDstAddr[] = "10.10.0.1"; static const char DefaultTunSrcAddr[] = "10.10.0.2"; - struct TunEndpoint : public service::Endpoint, public dns::IQueryHandler + struct TunEndpoint : public service::Endpoint, + public dns::IQueryHandler, + public std::enable_shared_from_this< TunEndpoint > { TunEndpoint(const std::string& nickname, AbstractRouter* r, llarp::service::Context* parent); ~TunEndpoint(); + path::PathSet_ptr + GetSelf() override + { + return shared_from_this(); + } + virtual bool SetOption(const std::string& k, const std::string& v) override; @@ -198,7 +206,7 @@ namespace llarp template < typename Addr_t, typename Endpoint_t > void - SendDNSReply(Addr_t addr, Endpoint_t* ctx, dns::Message* query, + SendDNSReply(Addr_t addr, Endpoint_t ctx, dns::Message* query, std::function< void(dns::Message) > reply, bool snode, bool sendIPv6) { diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 365c88901..03b31c639 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -270,50 +270,34 @@ namespace llarp void PathContext::ExpirePaths(llarp_time_t now) { - util::Lock lock(&m_TransitPaths.first); - auto& map = m_TransitPaths.second; - auto itr = map.begin(); - while(itr != map.end()) { - if(itr->second->Expired(now)) + util::Lock lock(&m_TransitPaths.first); + auto& map = m_TransitPaths.second; + auto itr = map.begin(); + while(itr != map.end()) { - itr = map.erase(itr); + if(itr->second->Expired(now)) + { + itr = map.erase(itr); + } + else + ++itr; } - else - ++itr; } - - for(auto& builder : m_PathBuilders) - { - if(builder) - builder->ExpirePaths(now); - } - } - - void - PathContext::BuildPaths(llarp_time_t now) - { - for(auto& builder : m_PathBuilders) { - if(builder->ShouldBuildMore(now)) + util::Lock lock(&m_OurPaths.first); + auto& map = m_OurPaths.second; + for(auto& item : map) { - builder->BuildOne(); + item.second->ExpirePaths(now); } } } - - void - PathContext::TickPaths(llarp_time_t now) - { - for(auto& builder : m_PathBuilders) - builder->Tick(now, m_Router); - } - routing::MessageHandler_ptr PathContext::GetHandler(const PathID_t& id) { routing::MessageHandler_ptr h = nullptr; - auto pathset = GetLocalPathSet(id); + auto pathset = GetLocalPathSet(id); if(pathset) { h = pathset->GetPathByID(id); @@ -334,12 +318,6 @@ namespace llarp return nullptr; } - void - PathContext::AddPathBuilder(Builder_ptr ctx) - { - m_PathBuilders.emplace_back(ctx); - } - void PathContext::RemovePathSet(PathSet_ptr set) { @@ -355,13 +333,6 @@ namespace llarp } } - void - PathContext::RemovePathBuilder(Builder_ptr ctx) - { - m_PathBuilders.remove(ctx); - RemovePathSet(ctx); - } - std::ostream& TransitHop::print(std::ostream& stream, int level, int spaces) const { @@ -906,7 +877,7 @@ namespace llarp bool Path::InformExitResult(llarp_time_t B) { - auto self = shared_from_this(); + auto self = shared_from_this(); bool result = true; for(const auto& hook : m_ObtainedExitHooks) result &= hook(self, B); diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 28a7bcf05..00e3a88c6 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -149,7 +149,7 @@ namespace llarp uint64_t m_SequenceNum = 0; }; - using HopHandler_ptr = std::shared_ptr; + using HopHandler_ptr = std::shared_ptr< IHopHandler >; struct TransitHop : public IHopHandler, public routing::IMessageHandler { @@ -303,7 +303,9 @@ namespace llarp }; /// A path we made - struct Path : public IHopHandler, public routing::IMessageHandler, public std::enable_shared_from_this + struct Path : public IHopHandler, + public routing::IMessageHandler, + public std::enable_shared_from_this< Path > { using BuildResultHookFunc = std::function< void(Path_ptr) >; using CheckForDeadFunc = std::function< bool(Path_ptr, llarp_time_t) >; @@ -321,7 +323,7 @@ namespace llarp HopList hops; - PathSet *const m_PathSet; + PathSet* const m_PathSet; service::Introduction intro; @@ -579,19 +581,6 @@ namespace llarp void ExpirePaths(llarp_time_t now); - /// called from router tick function - /// builds all paths we need to build at current tick - void - BuildPaths(llarp_time_t now); - - /// called from router tick function - void - TickPaths(llarp_time_t now); - - /// track a path builder with this context - void - AddPathBuilder(Builder_ptr set); - void AllowTransit(); @@ -644,9 +633,6 @@ namespace llarp void AddOwnPath(PathSet_ptr set, Path_ptr p); - void - RemovePathBuilder(Builder_ptr ctx); - void RemovePathSet(PathSet_ptr set); @@ -689,9 +675,7 @@ namespace llarp private: AbstractRouter* m_Router; SyncTransitMap_t m_TransitPaths; - SyncTransitMap_t m_Paths; SyncOwnedPathsMap_t m_OurPaths; - std::list< Builder_ptr > m_PathBuilders; bool m_AllowTransit; }; } // namespace path diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 893daaee0..f620b564c 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -16,7 +16,7 @@ namespace llarp struct AsyncPathKeyExchangeContext { typedef path::Path_ptr Path_t; - typedef path::Builder_ptr PathSet_t; + typedef path::PathSet_ptr PathSet_t; PathSet_t pathset = nullptr; Path_t path = nullptr; typedef std::function< void(AsyncPathKeyExchangeContext< User >*) > Handler; @@ -79,9 +79,8 @@ namespace llarp else { hop.upstream = ctx->path->hops[ctx->idx].rc.pubkey; - if(ctx->pathset->ShouldBundleRC()) - record.nextRC = - std::make_unique< RouterContact >(ctx->path->hops[ctx->idx].rc); + record.nextRC = + std::make_unique< RouterContact >(ctx->path->hops[ctx->idx].rc); } // build record @@ -166,8 +165,6 @@ namespace llarp else LogError(ctx->pathset->Name(), " failed to send LRCM to ", remote); } - // decrement keygen counter - ctx->pathset->keygens--; } namespace path @@ -176,21 +173,27 @@ namespace llarp size_t pathNum, size_t hops) : path::PathSet(pathNum), router(p_router), dht(p_dht), numHops(hops) { - p_router->pathContext().AddPathBuilder(shared_from_this()); p_router->crypto()->encryption_keygen(enckey); _run.store(true); - keygens.store(0); } Builder::~Builder() { } + void + Builder::Tick(llarp_time_t now) + { + ExpirePaths(now); + if(ShouldBuildMore(now)) + BuildOne(); + TickPaths(now, router); + } + util::StatusObject Builder::ExtractStatus() const { - util::StatusObject obj{{"keygens", uint64_t(keygens.load())}, - {"numHops", uint64_t(numHops)}, + util::StatusObject obj{{"numHops", uint64_t(numHops)}, {"numPaths", uint64_t(m_NumPaths)}}; std::vector< util::StatusObject > pathObjs; std::transform(m_Paths.begin(), m_Paths.end(), @@ -243,7 +246,6 @@ namespace llarp Builder::Stop() { _run.store(false); - router->pathContext().RemovePathBuilder(shared_from_this()); return true; } @@ -256,9 +258,7 @@ namespace llarp bool Builder::ShouldRemove() const { - if(!IsStopped()) - return false; - return keygens.load() > 0; + return IsStopped(); } const SecretKey& @@ -337,10 +337,10 @@ namespace llarp AsyncPathKeyExchangeContext< Builder >* ctx = new AsyncPathKeyExchangeContext< Builder >(router->crypto()); ctx->router = router; - ctx->pathset = shared_from_this(); - auto path = std::make_shared(hops, this, roles); - path->SetBuildResultHook([this](Path_ptr p) { this->HandlePathBuilt(p); }); - ++keygens; + ctx->pathset = GetSelf(); + auto path = std::make_shared< path::Path >(hops, this, roles); + path->SetBuildResultHook( + [this](Path_ptr p) { this->HandlePathBuilt(p); }); ctx->AsyncGenerateKeys(path, router->logic(), router->threadpool(), this, &PathBuilderKeysGenerated); } diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index 2d8202f20..406711fe7 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -15,7 +15,7 @@ namespace llarp // milliseconds waiting between builds on a path constexpr llarp_time_t MIN_PATH_BUILD_INTERVAL = 1000; - struct Builder : public PathSet, std::enable_shared_from_this // yes private scope + struct Builder : public PathSet { protected: /// flag for PathSet::Stop() @@ -29,9 +29,6 @@ namespace llarp llarp_time_t lastBuild = 0; llarp_time_t buildIntervalLimit = MIN_PATH_BUILD_INTERVAL; - // how many keygens are currently happening - std::atomic< uint8_t > keygens; - /// construct Builder(AbstractRouter* p_router, llarp_dht_context* p_dht, size_t numPaths, size_t numHops); @@ -75,8 +72,11 @@ namespace llarp llarp_time_t Now() const override; + virtual void + Tick(llarp_time_t now) override; + void - BuildOne(PathRole roles = ePathRoleAny); + BuildOne(PathRole roles = ePathRoleAny) override; void Build(const std::vector< RouterContact >& hops, @@ -99,7 +99,7 @@ namespace llarp HandlePathBuildTimeout(Path_ptr p) override; }; - using Builder_ptr = std::shared_ptr; + using Builder_ptr = std::shared_ptr< Builder >; } // namespace path diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index b4ce52954..6289c4534 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -15,7 +15,7 @@ namespace llarp bool PathSet::ShouldBuildMore(llarp_time_t now) const { - (void) now; + (void)now; const auto building = NumInStatus(ePathBuilding); if(building > m_NumPaths) return false; @@ -61,7 +61,7 @@ namespace llarp } void - PathSet::Tick(llarp_time_t now, AbstractRouter* r) + PathSet::TickPaths(llarp_time_t now, AbstractRouter* r) { Lock_t l(&m_PathsMutex); for(auto& item : m_Paths) @@ -115,7 +115,7 @@ namespace llarp { Lock_t l(&m_PathsMutex); Path_ptr chosen = nullptr; - auto itr = m_Paths.begin(); + auto itr = m_Paths.begin(); while(itr != m_Paths.end()) { if(itr->second->IsReady() && itr->second->SupportsAnyRoles(roles)) @@ -138,7 +138,7 @@ namespace llarp { Lock_t l(&m_PathsMutex); Path_ptr chosen = nullptr; - auto itr = m_Paths.begin(); + auto itr = m_Paths.begin(); while(itr != m_Paths.end()) { if(itr->second->IsReady() && itr->second->SupportsAnyRoles(roles)) diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 7f4c9367d..1bbaef2fd 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -56,7 +56,11 @@ namespace llarp // forward declare struct Path; - using Path_ptr = std::shared_ptr; + using Path_ptr = std::shared_ptr< Path >; + + struct PathSet; + + using PathSet_ptr = std::shared_ptr< PathSet >; /// a set of paths owned by an entity struct PathSet @@ -65,9 +69,16 @@ namespace llarp /// @params numPaths the number of paths to maintain PathSet(size_t numPaths); + /// get a shared_ptr of ourself + virtual PathSet_ptr + GetSelf() = 0; + + virtual void + BuildOne(PathRole roles = ePathRoleAny) = 0; + /// tick owned paths - void - Tick(llarp_time_t now, AbstractRouter* r); + virtual void + Tick(llarp_time_t now) = 0; /// count the number of paths that will exist at this timestamp in future size_t @@ -210,7 +221,10 @@ namespace llarp size_t m_NumPaths; void - ForEachPath(std::function< void(const Path_ptr &) > visit) const + TickPaths(llarp_time_t now, AbstractRouter* r); + + void + ForEachPath(std::function< void(const Path_ptr&) > visit) const { Lock_t lock(&m_PathsMutex); auto itr = m_Paths.begin(); @@ -231,15 +245,14 @@ namespace llarp return RouterID::Hash()(i.first) ^ PathID_t::Hash()(i.second); } }; - using Mtx_t = util::NullMutex; - using Lock_t = util::NullLock; - using PathMap_t = std::unordered_map< PathInfo_t, Path_ptr, PathInfoHash >; + using Mtx_t = util::NullMutex; + using Lock_t = util::NullLock; + using PathMap_t = + std::unordered_map< PathInfo_t, Path_ptr, PathInfoHash >; mutable Mtx_t m_PathsMutex; PathMap_t m_Paths; }; - using PathSet_ptr = std::shared_ptr; - } // namespace path } // namespace llarp diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 9cd0444dc..198e9fa40 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -1073,9 +1073,11 @@ namespace llarp ServiceNodeLookupRouterWhenExpired(remote); return; } - _hiddenServiceContext.ForEachService([=](const std::string &, const std::shared_ptr & ep) -> bool { - return !ep->LookupRouterAnon(remote); - }); + _hiddenServiceContext.ForEachService( + [=](const std::string &, + const std::shared_ptr< service::Endpoint > &ep) -> bool { + return !ep->LookupRouterAnon(remote); + }); } bool @@ -1125,7 +1127,7 @@ namespace llarp return !IsBootstrapNode(rc.pubkey); }); } - paths.TickPaths(now); + // expire transit paths paths.ExpirePaths(now); { @@ -1183,9 +1185,10 @@ namespace llarp } if(!IsServiceNode()) + { _hiddenServiceContext.Tick(now); + } - paths.BuildPaths(now); _exitContext.Tick(now); if(rpcCaller) rpcCaller->Tick(now); diff --git a/llarp/routing/handler.hpp b/llarp/routing/handler.hpp index d3f87793b..b98b71a18 100644 --- a/llarp/routing/handler.hpp +++ b/llarp/routing/handler.hpp @@ -36,55 +36,55 @@ namespace llarp { virtual bool HandleObtainExitMessage(const ObtainExitMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleGrantExitMessage(const GrantExitMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleRejectExitMessage(const RejectExitMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleTransferTrafficMessage(const TransferTrafficMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleUpdateExitMessage(const UpdateExitMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleUpdateExitVerifyMessage(const UpdateExitVerifyMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleCloseExitMessage(const CloseExitMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleDataDiscardMessage(const DataDiscardMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandlePathTransferMessage(const PathTransferMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandleHiddenServiceFrame(const service::ProtocolFrame& msg) = 0; virtual bool HandlePathConfirmMessage(const PathConfirmMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool HandlePathLatencyMessage(const PathLatencyMessage& msg, - AbstractRouter *r) = 0; + AbstractRouter* r) = 0; virtual bool - HandleDHTMessage(const dht::IMessage& msg, AbstractRouter *r) = 0; + HandleDHTMessage(const dht::IMessage& msg, AbstractRouter* r) = 0; }; - using MessageHandler_ptr = std::shared_ptr; + using MessageHandler_ptr = std::shared_ptr< IMessageHandler >; } // namespace routing } // namespace llarp diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index c8865d0de..46fd6c6f9 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -102,7 +102,8 @@ namespace llarp expired.emplace_back(rc.pubkey); return true; }); - ForEachService([&](const std::string &, const std::shared_ptr &ep) -> bool { + ForEachService([&](const std::string &, + const std::shared_ptr< Endpoint > &ep) -> bool { // TODO: we need to stop looking up service nodes that are gone forever // how do? for(const auto &k : expired) @@ -198,22 +199,30 @@ namespace llarp keyfile = option.second; } - std::unique_ptr< service::Endpoint > service; + service::Endpoint_ptr service; static std::map< std::string, - std::function< std::unique_ptr< service::Endpoint >( + std::function< service::Endpoint_ptr( const std::string &, AbstractRouter *, service::Context *) > > endpointConstructors = { {"tun", [](const std::string &nick, AbstractRouter *r, - service::Context *c) -> std::unique_ptr< service::Endpoint > { - return std::make_unique< handlers::TunEndpoint >(nick, r, c); + service::Context *c) -> service::Endpoint_ptr { + return std::make_shared< handlers::TunEndpoint >(nick, r, c); + }}, + {"ios-tun", + [](const std::string &nick, AbstractRouter *r, + service::Context *c) -> service::Endpoint_ptr { + return nullptr; + /// SOOOOOOON (tm) + // return std::make_shared(nick, r, + // c); }}, {"null", [](const std::string &nick, AbstractRouter *r, - service::Context *c) -> std::unique_ptr< service::Endpoint > { - return std::make_unique< handlers::NullEndpoint >(nick, r, c); + service::Context *c) -> service::Endpoint_ptr { + return std::make_shared< handlers::NullEndpoint >(nick, r, c); }}}; { @@ -227,18 +236,22 @@ namespace llarp // construct service = itr->second(conf.first, m_Router, this); - - // if ephemeral, then we need to regen key - // if privkey file, then set it and load it - if(keyfile != "") + if(service) { - service->SetOption("keyfile", keyfile); - // load keyfile, so we have the correct name for logging + // if ephemeral, then we need to regen key + // if privkey file, then set it and load it + if(keyfile != "") + { + service->SetOption("keyfile", keyfile); + // load keyfile, so we have the correct name for logging + } + LogInfo("Establishing endpoint identity"); + service->LoadKeyFile(); // only start endpoint not tun + // now Name() will be correct } - LogInfo("Establishing endpoint identity"); - service->LoadKeyFile(); // only start endpoint not tun - // now Name() will be correct } + if(service == nullptr) + return false; // configure for(const auto &option : conf.second) { @@ -259,7 +272,7 @@ namespace llarp if(service->Start()) { LogInfo("autostarting hidden service endpoint ", service->Name()); - m_Endpoints.emplace(conf.first, std::move(service)); + m_Endpoints.emplace(conf.first, service); return true; } LogError("failed to start hidden service endpoint ", conf.first); @@ -268,7 +281,7 @@ namespace llarp else { LogInfo("added hidden service endpoint ", service->Name()); - m_Endpoints.emplace(conf.first, std::move(service)); + m_Endpoints.emplace(conf.first, service); return true; } } diff --git a/llarp/service/context.hpp b/llarp/service/context.hpp index 3683247a2..505649281 100644 --- a/llarp/service/context.hpp +++ b/llarp/service/context.hpp @@ -31,16 +31,15 @@ namespace llarp bool hasEndpoints(); - bool FindBestAddressFor(const AlignedBuffer< 32 > &addr, bool isSNode, huint32_t &); /// function visitor returns false to prematurely break iteration void - ForEachService(std::function< bool(const std::string &, - const Endpoint_ptr &) > - visit) const; + ForEachService( + std::function< bool(const std::string &, const Endpoint_ptr &) > + visit) const; /// add default endpoint with options bool diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index f290cdc83..ceb7aa47f 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -240,6 +240,7 @@ namespace llarp void Endpoint::Tick(llarp_time_t now) { + path::Builder::Tick(now); // publish descriptors if(ShouldPublishDescriptors(now)) { @@ -256,7 +257,8 @@ namespace llarp auto itr = m_SNodeSessions.begin(); while(itr != m_SNodeSessions.end()) { - if(itr->second->ShouldRemove() && itr->second->IsStopped()) + itr->second->Tick(now); + if(itr->second->ShouldRemove()) { itr = m_SNodeSessions.erase(itr); continue; @@ -362,6 +364,7 @@ namespace llarp auto itr = m_DeadSessions.begin(); while(itr != m_DeadSessions.end()) { + itr->second->Tick(now); if(itr->second->IsDone(now)) itr = m_DeadSessions.erase(itr); else @@ -373,14 +376,17 @@ namespace llarp auto itr = m_RemoteSessions.begin(); while(itr != m_RemoteSessions.end()) { - if(itr->second->Tick(now)) + if(itr->second->Pump(now)) { itr->second->Stop(); m_DeadSessions.emplace(itr->first, std::move(itr->second)); itr = m_RemoteSessions.erase(itr); } else + { + itr->second->Tick(now); ++itr; + } } } // expire convotags @@ -726,7 +732,7 @@ namespace llarp // make sure we have all paths that are established // in our introset bool should = false; - ForEachPath([&](const path::Path_ptr & p) { + ForEachPath([&](const path::Path_ptr& p) { if(!p->IsReady()) return; for(const auto& i : m_IntroSet.I) @@ -797,7 +803,7 @@ namespace llarp } auto it = m_RemoteSessions.emplace( - addr, std::make_unique< OutboundContext >(introset, this)); + addr, std::make_shared< OutboundContext >(introset, this)); LogInfo("Created New outbound context for ", addr.ToString()); // inform pending @@ -879,7 +885,8 @@ namespace llarp } bool - Endpoint::HandleDataDrop(path::Path_ptr p, const PathID_t& dst, uint64_t seq) + Endpoint::HandleDataDrop(path::Path_ptr p, const PathID_t& dst, + uint64_t seq) { LogWarn(Name(), " message ", seq, " dropped by endpoint ", p->Endpoint(), " via ", dst); @@ -964,7 +971,8 @@ namespace llarp return true; } if(!frame.AsyncDecryptAndVerify(EndpointLogic(), GetCrypto(), p, - CryptoWorker(), m_Identity, m_DataHandler)) + CryptoWorker(), m_Identity, + m_DataHandler)) { // send discard @@ -974,17 +982,15 @@ namespace llarp f.F = p->intro.pathID; if(!f.Sign(GetCrypto(), m_Identity)) return false; - auto d = std::make_shared(f, frame.F); - RouterLogic()->queue_func([=]() { - p->SendRoutingMessage(*d, router); - }); + auto d = + std::make_shared< const routing::PathTransferMessage >(f, frame.F); + RouterLogic()->queue_func([=]() { p->SendRoutingMessage(*d, router); }); return true; } return true; } - void - Endpoint::HandlePathDied(path::Path_ptr) + void Endpoint::HandlePathDied(path::Path_ptr) { RegenAndPublishIntroSet(Now(), true); } @@ -1073,21 +1079,20 @@ namespace llarp using namespace std::placeholders; if(m_SNodeSessions.count(snode) == 0) { - auto themIP = ObtainIPForAddr(snode, true); - m_SNodeSessions.emplace( + auto themIP = ObtainIPForAddr(snode, true); + auto session = std::make_shared< exit::SNodeSession >( snode, - std::make_unique< exit::SNodeSession >( - snode, - std::bind(&Endpoint::HandleWriteIPPacket, this, _1, - [themIP]() -> huint32_t { return themIP; }), - m_Router, 2, numHops)); + std::bind(&Endpoint::HandleWriteIPPacket, this, _1, + [themIP]() -> huint32_t { return themIP; }), + m_Router, 2, numHops); + m_SNodeSessions.emplace(snode, session); } auto range = m_SNodeSessions.equal_range(snode); auto itr = range.first; while(itr != range.second) { if(itr->second->IsReady()) - h(snode, itr->second.get()); + h(snode, itr->second); else itr->second->AddReadyHook(std::bind(h, snode, _1)); ++itr; @@ -1130,9 +1135,9 @@ namespace llarp auto itr = m_AddressToService.find(remote); if(itr != m_AddressToService.end()) { - auto transfer = std::make_shared(); + auto transfer = std::make_shared< routing::PathTransferMessage >(); ProtocolFrame& f = transfer->T; - std::shared_ptr p; + std::shared_ptr< path::Path > p; std::set< ConvoTag > tags; if(GetConvoTagsForService(itr->second, tags)) { @@ -1167,9 +1172,9 @@ namespace llarp m.proto = t; m.introReply = p->intro; PutReplyIntroFor(f.T, m.introReply); - m.sender = m_Identity.pub; - f.F = m.introReply.pathID; - f.S = GetSeqNoForConvo(f.T); + m.sender = m_Identity.pub; + f.F = m.introReply.pathID; + f.S = GetSeqNoForConvo(f.T); transfer->P = remoteIntro.pathID; if(!f.EncryptAndSign(Router()->crypto(), m, K, m_Identity)) { @@ -1178,9 +1183,8 @@ namespace llarp } LogDebug(Name(), " send ", data.sz, " via ", remoteIntro.router); auto router = Router(); - RouterLogic()->queue_func([=]() { - p->SendRoutingMessage(*transfer, router); - }); + RouterLogic()->queue_func( + [=]() { p->SendRoutingMessage(*transfer, router); }); return true; } } diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index ca2c5342a..b2691f8f2 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -66,7 +66,7 @@ namespace llarp SetOption(const std::string& k, const std::string& v); virtual void - Tick(llarp_time_t now); + Tick(llarp_time_t now) override; /// return true if we have a resolvable ip address virtual bool @@ -92,14 +92,14 @@ namespace llarp Logic* EndpointLogic(); - /// borrow endpoint's net loop for sending data to user on local network interface + /// borrow endpoint's net loop for sending data to user on local network + /// interface llarp_ev_loop_ptr EndpointNetLoop(); Crypto* GetCrypto(); - /// crypto worker threadpool llarp_threadpool* CryptoWorker(); @@ -239,7 +239,7 @@ namespace llarp uint64_t timeoutMS, bool lookupOnRandomPath = false); using SNodeEnsureHook = - std::function< void(RouterID, exit::BaseSession*) >; + std::function< void(RouterID, exit::BaseSession_ptr) >; /// ensure a path to a service node by public key void @@ -346,7 +346,7 @@ namespace llarp protected: IDataHandler* m_DataHandler = nullptr; Identity m_Identity; - std::unique_ptr< exit::BaseSession > m_Exit; + std::shared_ptr< exit::BaseSession > m_Exit; hooks::Backend_ptr m_OnUp; hooks::Backend_ptr m_OnDown; hooks::Backend_ptr m_OnReady; @@ -367,14 +367,14 @@ namespace llarp PendingTraffic m_PendingTraffic; using Sessions = - std::unordered_multimap< Address, std::unique_ptr< OutboundContext >, + std::unordered_multimap< Address, std::shared_ptr< OutboundContext >, Address::Hash >; Sessions m_RemoteSessions; Sessions m_DeadSessions; using SNodeSessions = std::unordered_multimap< - RouterID, std::unique_ptr< exit::BaseSession >, RouterID::Hash >; + RouterID, std::shared_ptr< exit::BaseSession >, RouterID::Hash >; SNodeSessions m_SNodeSessions; @@ -437,7 +437,7 @@ namespace llarp std::unordered_map< Tag, CachedTagResult, Tag::Hash > m_PrefetchedTags; }; - using Endpoint_ptr = std::shared_ptr; + using Endpoint_ptr = std::shared_ptr< Endpoint >; } // namespace service } // namespace llarp diff --git a/llarp/service/lookup.cpp b/llarp/service/lookup.cpp index c9a0fb7c0..16b45b971 100644 --- a/llarp/service/lookup.cpp +++ b/llarp/service/lookup.cpp @@ -25,10 +25,8 @@ namespace llarp auto msg = BuildRequestMessage(); if(!msg) return false; - endpoint = path->Endpoint(); - r->logic()->queue_func([=]() { - path->SendRoutingMessage(*msg, r); - }); + endpoint = path->Endpoint(); + r->logic()->queue_func([=]() { path->SendRoutingMessage(*msg, r); }); return true; } } // namespace service diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index dcad519f8..99d322ffd 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -274,7 +274,7 @@ namespace llarp } bool - OutboundContext::Tick(llarp_time_t now) + OutboundContext::Pump(llarp_time_t now) { // we are probably dead af if(m_LookupFails > 16 || m_BuildFails > 10) @@ -480,7 +480,7 @@ namespace llarp { // figure out how many paths to this router we have size_t num = 0; - ForEachPath([&](const path::Path_ptr & p) { + ForEachPath([&](const path::Path_ptr& p) { if(p->Endpoint() == endpoint && p->IsReady()) ++num; }); @@ -491,7 +491,7 @@ namespace llarp if(num == 1) { num = 0; - ForEachPath([&](const path::Path_ptr & p) { + ForEachPath([&](const path::Path_ptr& p) { if(p->Endpoint() == endpoint) ++num; }); @@ -521,7 +521,7 @@ namespace llarp m_NextIntro = picked; // check if we have a path to this router num = 0; - ForEachPath([&](const path::Path_ptr & p) { + ForEachPath([&](const path::Path_ptr& p) { if(p->Endpoint() == m_NextIntro.router) ++num; }); diff --git a/llarp/service/outbound_context.hpp b/llarp/service/outbound_context.hpp index 213e97671..be5fa3548 100644 --- a/llarp/service/outbound_context.hpp +++ b/llarp/service/outbound_context.hpp @@ -15,7 +15,10 @@ namespace llarp struct Endpoint; /// context needed to initiate an outbound hidden service session - struct OutboundContext : public path::Builder, public SendContext + struct OutboundContext + : public path::Builder, + public SendContext, + public std::enable_shared_from_this< OutboundContext > { OutboundContext(const IntroSet& introSet, Endpoint* parent); ~OutboundContext(); @@ -26,6 +29,12 @@ namespace llarp bool ShouldBundleRC() const override; + path::PathSet_ptr + GetSelf() override + { + return shared_from_this(); + } + bool Stop() override; @@ -54,10 +63,10 @@ namespace llarp bool ShouldBuildMore(llarp_time_t now) const override; - /// tick internal state + /// pump internal state /// return true to mark as dead bool - Tick(llarp_time_t now); + Pump(llarp_time_t now); /// return true if it's safe to remove ourselves bool