From a61f846d33f31fd6d7bcf32c11cd6836d02d755c Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 16 Mar 2021 15:50:37 -0400 Subject: [PATCH] * add convotags to ensure path to snode * add service::Endpoint::EnsurePathTo which gives you a std::optional --- llarp/handlers/tun.cpp | 8 +++--- llarp/service/endpoint.cpp | 52 +++++++++++++++++++++++++++++++++----- llarp/service/endpoint.hpp | 8 +++++- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index c342f5c8d..a96a9af63 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -325,9 +325,11 @@ namespace llarp { auto ReplyToSNodeDNSWhenReady = [self = this, reply = reply]( RouterID snode, auto msg, bool isV6) -> bool { - return self->EnsurePathToSNode(snode, [=](const RouterID&, exit::BaseSession_ptr s) { - self->SendDNSReply(snode, s, msg, reply, isV6); - }); + return self->EnsurePathToSNode( + snode, + [=](const RouterID&, exit::BaseSession_ptr s, [[maybe_unused]] service::ConvoTag tag) { + self->SendDNSReply(snode, s, msg, reply, isV6); + }); }; auto ReplyToLokiDNSWhenReady = [self = this, reply = reply]( service::Address addr, auto msg, bool isV6) -> bool { diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index bfd17c56b..5df9ffa03 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -39,6 +39,7 @@ #include #include #include +#include namespace llarp { @@ -1367,10 +1368,10 @@ namespace llarp while (itr != range.second) { if (itr->second.first->IsReady()) - h(snode, itr->second.first); + h(snode, itr->second.first, itr->second.second); else { - itr->second.first->AddReadyHook(std::bind(h, snode, _1)); + itr->second.first->AddReadyHook(std::bind(h, snode, _1, itr->second.second)); itr->second.first->BuildOne(); } ++itr; @@ -1402,10 +1403,11 @@ namespace llarp auto pkt = std::make_shared(); if (!pkt->Load(buf)) return false; - EnsurePathToSNode(addr, [pkt, t](RouterID, exit::BaseSession_ptr s) { - if (s) - s->SendPacketToRemote(pkt->ConstBuffer(), t); - }); + EnsurePathToSNode( + addr, [pkt, t](RouterID, exit::BaseSession_ptr s, [[maybe_unused]] ConvoTag tag) { + if (s) + s->SendPacketToRemote(pkt->ConstBuffer(), t); + }); return true; } @@ -1472,6 +1474,44 @@ namespace llarp return std::nullopt; } + bool + Endpoint::EnsurePathTo( + std::variant addr, + std::function)> hook, + llarp_time_t timeout) + { + if (auto ptr = std::get_if
(&addr)) + { + return EnsurePathToService( + *ptr, + [hook](auto, auto* ctx) { + if (ctx) + { + hook(ctx->currentConvoTag); + } + else + { + hook(std::nullopt); + } + }, + timeout); + } + if (auto ptr = std::get_if(&addr)) + { + return EnsurePathToSNode(*ptr, [hook](auto, auto session, auto tag) { + if (session) + { + hook(tag); + } + else + { + hook(std::nullopt); + } + }); + } + return false; + } + bool Endpoint::SendToServiceOrQueue( const service::Address& remote, const llarp_buffer_t& data, ProtocolType t) diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 8a05a64a0..a92c72293 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -266,6 +266,12 @@ namespace llarp bool ShouldBuildMore(llarp_time_t now) const override; + bool + EnsurePathTo( + std::variant addr, + std::function)> hook, + llarp_time_t timeout); + // passed a sendto context when we have a path established otherwise // nullptr if the path was not made before the timeout using PathEnsureHook = std::function; @@ -275,7 +281,7 @@ namespace llarp bool EnsurePathToService(const Address remote, PathEnsureHook h, llarp_time_t timeoutMS); - using SNodeEnsureHook = std::function; + using SNodeEnsureHook = std::function; /// ensure a path to a service node by public key bool