From 7ac5b29d5f0c7e34d0d60e65baacdb0ac770bb38 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 7 Mar 2023 13:15:30 -0800 Subject: [PATCH] massive code change to remove ConvoTag from llarp/quic --- delete_me | 53 -------------------------- llarp/endpoint_base.hpp | 4 +- llarp/exit/endpoint.cpp | 11 +++++- llarp/exit/session.cpp | 10 ++++- llarp/handlers/exit.cpp | 64 ++++++++++++++++---------------- llarp/handlers/exit.hpp | 5 ++- llarp/handlers/null.hpp | 9 ++++- llarp/handlers/tun.cpp | 22 ++++++----- llarp/lokinet_shared.cpp | 4 +- llarp/messages/dht_immediate.cpp | 2 +- llarp/net/sock_addr.cpp | 1 + llarp/quic/address.cpp | 6 ++- llarp/quic/address.hpp | 8 +++- llarp/quic/client.cpp | 20 +++++++--- llarp/quic/client.hpp | 6 ++- llarp/quic/endpoint.cpp | 14 +++++-- llarp/quic/endpoint.hpp | 2 +- llarp/quic/tunnel.cpp | 52 ++++++++++++++++---------- llarp/quic/tunnel.hpp | 7 +++- llarp/service/endpoint.cpp | 54 +++++++-------------------- llarp/service/endpoint.hpp | 12 +++--- 21 files changed, 177 insertions(+), 189 deletions(-) delete mode 100644 delete_me diff --git a/delete_me b/delete_me deleted file mode 100644 index 1cad819b2..000000000 --- a/delete_me +++ /dev/null @@ -1,53 +0,0 @@ - - - - -Directory listing for / - - -

Directory listing for /

-
- -
- - diff --git a/llarp/endpoint_base.hpp b/llarp/endpoint_base.hpp index 6bbd97bce..7857e030e 100644 --- a/llarp/endpoint_base.hpp +++ b/llarp/endpoint_base.hpp @@ -122,7 +122,7 @@ namespace llarp virtual bool EnsurePathTo( AddressVariant_t addr, - std::function)> hook, + std::function>)> hook, llarp_time_t timeout) = 0; virtual void @@ -134,7 +134,7 @@ namespace llarp virtual bool SendToOrQueue( - service::ConvoTag tag, const llarp_buffer_t& payload, service::ProtocolType t) = 0; + std::variant addr, const llarp_buffer_t& payload, service::ProtocolType t) = 0; /// lookup srv records async virtual void diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index 4bbad541c..c7f457e94 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -119,7 +119,16 @@ namespace llarp if (not quic) return false; m_TxRate += buf.size(); - quic->receive_packet(tag, std::move(buf)); + + + std::variant addr; + + if (auto maybe = m_Parent->GetEndpointWithConvoTag(tag)) + addr = *maybe; + else + return false; + + quic->receive_packet(std::move(addr), std::move(buf)); m_LastActive = m_Parent->Now(); return true; } diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 8c8553922..b9b9be1af 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -207,7 +207,15 @@ namespace llarp auto quic = m_Parent->GetQUICTunnel(); if (not quic) return false; - quic->receive_packet(tag, buf); + + std::variant addr; + + if (auto maybe = m_Parent->GetEndpointWithConvoTag(tag)) + addr = *maybe; + else + return false; + + quic->receive_packet(std::move(addr), buf); return true; } diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index a7840c090..055397c5f 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -97,42 +97,38 @@ namespace llarp bool ExitEndpoint::SendToOrQueue( - service::ConvoTag tag, const llarp_buffer_t& payload, service::ProtocolType type) + std::variant addr, const llarp_buffer_t& payload, service::ProtocolType type) { - if (auto maybeAddr = GetEndpointWithConvoTag(tag)) + if (std::holds_alternative(addr)) + return false; + if (auto* rid = std::get_if(&addr)) { - if (std::holds_alternative(*maybeAddr)) - return false; - if (auto* rid = std::get_if(&*maybeAddr)) + for (auto [itr, end] = m_ActiveExits.equal_range(PubKey{*rid}); itr != end; ++itr) { - for (auto [itr, end] = m_ActiveExits.equal_range(PubKey{*rid}); itr != end; ++itr) + if (not itr->second->LooksDead(Now())) { - if (not itr->second->LooksDead(Now())) - { - if (itr->second->QueueInboundTraffic(payload.copy(), type)) - return true; - } + if (itr->second->QueueInboundTraffic(payload.copy(), type)) + return true; } - - if (not m_Router->PathToRouterAllowed(*rid)) - return false; - - ObtainSNodeSession(*rid, [pkt = payload.copy(), type](auto session) mutable { - if (session and session->IsReady()) - { - session->SendPacketToRemote(std::move(pkt), type); - } - }); } - return true; + + if (not m_Router->PathToRouterAllowed(*rid)) + return false; + + ObtainSNodeSession(*rid, [pkt = payload.copy(), type](auto session) mutable { + if (session and session->IsReady()) + { + session->SendPacketToRemote(std::move(pkt), type); + } + }); } - return false; + return true; } bool ExitEndpoint::EnsurePathTo( AddressVariant_t addr, - std::function)> hook, + std::function>)> hook, llarp_time_t) { if (std::holds_alternative(addr)) @@ -142,15 +138,11 @@ namespace llarp if (m_SNodeKeys.count(PubKey{*rid}) or m_Router->PathToRouterAllowed(*rid)) { ObtainSNodeSession( - *rid, [hook, routerID = *rid](std::shared_ptr session) { - if (session and session->IsReady()) + *rid, [hook, routerID = *rid, this](std::shared_ptr session) { + if (session and session->IsReady(); auto path = session->GetPathByRouter(routerID)) { - if (auto path = session->GetPathByRouter(routerID)) - { - hook(service::ConvoTag{path->RXID().as_array()}); - } - else - hook(std::nullopt); + auto tag = service::ConvoTag{path->RXID().as_array()}; + hook(GetEndpointWithConvoTag(tag)); } else hook(std::nullopt); @@ -159,7 +151,13 @@ namespace llarp else { // probably a client - hook(GetBestConvoTagFor(addr)); + if (auto maybe_tag = GetBestConvoTagFor(addr); + auto maybe_addr = GetEndpointWithConvoTag(*maybe_tag)) + { + hook(maybe_addr); + } + else + hook(std::nullopt); } } return true; diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index 79fc2be47..cf701bb86 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -43,7 +43,7 @@ namespace llarp bool EnsurePathTo( AddressVariant_t addr, - std::function)> hook, + std::function>)> hook, llarp_time_t timeout) override; void @@ -62,9 +62,10 @@ namespace llarp void MarkAddressOutbound(AddressVariant_t) override{}; + // FIXME bool SendToOrQueue( - service::ConvoTag tag, const llarp_buffer_t& payload, service::ProtocolType t) override; + std::variant addr, const llarp_buffer_t& payload, service::ProtocolType t) override; void Tick(llarp_time_t now); diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index bfa0525e8..2a26098e8 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -70,7 +70,14 @@ namespace llarp::handlers LogWarn("invalid incoming quic packet, dropping"); return false; } - quic->receive_packet(tag, buf); + + std::variant addr; + if (auto maybe = GetEndpointWithConvoTag(tag)) + addr = *maybe; + else + return false; + + quic->receive_packet(std::move(addr), buf); return true; } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 6aa274b91..66b5521ba 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -1255,9 +1255,9 @@ namespace llarp } // try sending it on an existing convotag // this succeds for inbound convos, probably. - if (auto maybe = GetBestConvoTagFor(to)) + if (auto maybe_tag = GetBestConvoTagFor(to); auto maybe_addr = GetEndpointWithConvoTag(*maybe_tag)) { - if (SendToOrQueue(*maybe, pkt.ConstBuffer(), type)) + if (SendToOrQueue(*maybe_addr, pkt.ConstBuffer(), type)) { MarkIPActive(dst); Router()->TriggerPump(); @@ -1314,6 +1314,14 @@ namespace llarp uint64_t seqno) { LogTrace("Inbound ", t, " packet (", buf.sz, "B) on convo ", tag); + std::variant addr; + if (auto maybe = GetEndpointWithConvoTag(tag)) + { + addr = *maybe; + } + else + return false; + if (t == service::ProtocolType::QUIC) { auto* quic = GetQUICTunnel(); @@ -1328,20 +1336,14 @@ namespace llarp return false; } log::trace(logcat, "tag active T={}", tag); - quic->receive_packet(tag, buf); + + quic->receive_packet(std::move(addr), buf); return true; } if (t != service::ProtocolType::TrafficV4 && t != service::ProtocolType::TrafficV6 && t != service::ProtocolType::Exit) return false; - std::variant addr; - if (auto maybe = GetEndpointWithConvoTag(tag)) - { - addr = *maybe; - } - else - return false; huint128_t src, dst; net::IPPacket pkt; diff --git a/llarp/lokinet_shared.cpp b/llarp/lokinet_shared.cpp index 36a0870b6..66b70bc36 100644 --- a/llarp/lokinet_shared.cpp +++ b/llarp/lokinet_shared.cpp @@ -996,9 +996,9 @@ extern "C" return EINVAL; std::promise ret; ctx->impl->router->loop()->call([addr = *maybe, pkt = std::move(pkt), ep, &ret]() { - if (auto tag = ep->GetBestConvoTagFor(addr)) + if (auto tag = ep->GetBestConvoTagFor(addr); auto addr = ep->GetEndpointWithConvoTag(*tag)) { - if (ep->SendToOrQueue(*tag, pkt.ConstBuffer(), llarp::service::ProtocolType::TrafficV4)) + if (ep->SendToOrQueue(std::move(*addr), pkt.ConstBuffer(), llarp::service::ProtocolType::TrafficV4)) { ret.set_value(0); return; diff --git a/llarp/messages/dht_immediate.cpp b/llarp/messages/dht_immediate.cpp index 479932979..a5d886367 100644 --- a/llarp/messages/dht_immediate.cpp +++ b/llarp/messages/dht_immediate.cpp @@ -77,6 +77,6 @@ namespace llarp result = router->SendToOrQueue(session->GetPubKey(), reply); } } - return true; + return result; } } // namespace llarp diff --git a/llarp/net/sock_addr.cpp b/llarp/net/sock_addr.cpp index 8fdf457f1..18d593e90 100644 --- a/llarp/net/sock_addr.cpp +++ b/llarp/net/sock_addr.cpp @@ -72,6 +72,7 @@ namespace llarp init(); fromString(addr); } + SockAddr::SockAddr(std::string_view addr, huint16_t port) { init(); diff --git a/llarp/quic/address.cpp b/llarp/quic/address.cpp index 89e171104..b5db99091 100644 --- a/llarp/quic/address.cpp +++ b/llarp/quic/address.cpp @@ -1,12 +1,14 @@ #include "address.hpp" #include #include +#include namespace llarp::quic { using namespace std::literals; - Address::Address(const SockAddr& addr) : saddr{*addr.operator const sockaddr_in6*()} + Address::Address(const SockAddr& addr, std::optional> ep) + : saddr{*addr.operator const sockaddr_in6*()}, endpoint{ep} {} Address& @@ -14,6 +16,7 @@ namespace llarp::quic { std::memmove(&saddr, &other.saddr, sizeof(saddr)); a.addrlen = other.a.addrlen; + endpoint = other.endpoint; return *this; } @@ -39,6 +42,7 @@ namespace llarp::quic return result; } + // FIXME: remote now has std::variant std::string Path::ToString() const { diff --git a/llarp/quic/address.hpp b/llarp/quic/address.hpp index f6c6456ad..913018212 100644 --- a/llarp/quic/address.hpp +++ b/llarp/quic/address.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,6 +14,7 @@ #include #include +#include "llarp/service/address.hpp" namespace llarp::quic { @@ -25,12 +27,14 @@ namespace llarp::quic public: Address() = default; - Address(const SockAddr& addr); + Address(const SockAddr& addr, std::optional> ep = std::nullopt); Address(const Address& other) { *this = other; } + + std::optional> endpoint{}; Address& operator=(const Address&); @@ -89,7 +93,7 @@ namespace llarp::quic // ConvoTag operator operator SockAddr() const { - return SockAddr(saddr); + return SockAddr{saddr}; } std::string diff --git a/llarp/quic/client.cpp b/llarp/quic/client.cpp index f1f759545..57234e232 100644 --- a/llarp/quic/client.cpp +++ b/llarp/quic/client.cpp @@ -1,4 +1,5 @@ #include "client.hpp" +#include "llarp/net/net_int.hpp" #include "tunnel.hpp" #include #include @@ -14,7 +15,11 @@ namespace llarp::quic { static auto logcat = log::Cat("quic"); - Client::Client(EndpointBase& ep, const SockAddr& remote, uint16_t pseudo_port) : Endpoint{ep} + Client::Client( + EndpointBase& ep, + const uint16_t port, + std::variant&& remote, + uint16_t pseudo_port) : Endpoint{ep} { default_stream_buffer_size = 0; // We steal uvw's provided buffers so don't need an outgoing data buffer @@ -23,8 +28,7 @@ namespace llarp::quic // back to *this* client. local_addr.port(ToNet(huint16_t{pseudo_port})); - uint16_t tunnel_port = remote.getPort(); - if (tunnel_port == 0) + if (port == 0) throw std::logic_error{"Cannot tunnel to port 0"}; // TODO: need timers for: @@ -36,10 +40,14 @@ namespace llarp::quic // // - key_update_timer - Path path{local_addr, remote}; - log::debug(logcat, "Connecting to {}", remote); + Path path{ + Address{SockAddr{"::1"sv, huint16_t{pseudo_port}}, std::nullopt}, + Address{SockAddr{"::1"sv, huint16_t{port}}, std::move(remote)} + }; - auto conn = std::make_shared(*this, ConnectionID::random(), path, tunnel_port); + log::debug(logcat, "Connecting to {}", path.remote); + + auto conn = std::make_shared(*this, ConnectionID::random(), path, port); conn->io_ready(); conns.emplace(conn->base_cid, std::move(conn)); } diff --git a/llarp/quic/client.hpp b/llarp/quic/client.hpp index d6d728153..930befec0 100644 --- a/llarp/quic/client.hpp +++ b/llarp/quic/client.hpp @@ -14,7 +14,11 @@ namespace llarp::quic // `remote.getPort()` on the remote's lokinet address. `pseudo_port` is *our* unique local // identifier which we include in outgoing packets (so that the remote server knows where to // send the back to *this* client). - Client(EndpointBase& ep, const SockAddr& remote, uint16_t pseudo_port); + Client( + EndpointBase& ep, + const uint16_t port, + std::variant&& remote, + uint16_t pseudo_port); // Returns a reference to the client's connection to the server. Returns a nullptr if there is // no connection. diff --git a/llarp/quic/endpoint.cpp b/llarp/quic/endpoint.cpp index 9e02aec80..a2794a288 100644 --- a/llarp/quic/endpoint.cpp +++ b/llarp/quic/endpoint.cpp @@ -51,14 +51,20 @@ namespace llarp::quic return loop; } + // TODO: does the lookup need to be done every single packet? + // revisit this during libQUICinet + //Endpoint::receive_packet(const SockAddr& src, uint8_t ecn, bstring_view data) void - Endpoint::receive_packet(const SockAddr& src, uint8_t ecn, bstring_view data) + Endpoint::receive_packet(Address remote, uint8_t ecn, bstring_view data, uint16_t remote_port) { // ngtcp2 wants a local address but we don't necessarily have something so just set it to // IPv4 or IPv6 "unspecified" address (0.0.0.0 or ::) - SockAddr local = src.isIPv6() ? SockAddr{in6addr_any} : SockAddr{nuint32_t{INADDR_ANY}}; + //SockAddr local = src.isIPv6() ? SockAddr{in6addr_any} : SockAddr{nuint32_t{INADDR_ANY}}; - Packet pkt{Path{local, src}, data, ngtcp2_pkt_info{.ecn = ecn}}; + Packet pkt{ + Path{Address{SockAddr{"::1"sv, huint16_t{remote_port}}, std::nullopt}, remote}, + data, + ngtcp2_pkt_info{.ecn = ecn}}; log::trace(logcat, "[{},ecn={}]: received {} bytes", pkt.path, pkt.info.ecn, data.size()); // debug @@ -206,7 +212,7 @@ namespace llarp::quic bstring_view outgoing{buf_.data(), outgoing_len}; if (service_endpoint.SendToOrQueue( - to, llarp_buffer_t{outgoing.data(), outgoing.size()}, service::ProtocolType::QUIC)) + *to.endpoint, llarp_buffer_t{outgoing.data(), outgoing.size()}, service::ProtocolType::QUIC)) { log::trace(logcat, "[{}]: sent {}", to, buffer_printer{outgoing}); // debug diff --git a/llarp/quic/endpoint.hpp b/llarp/quic/endpoint.hpp index 0c49c362b..78bc536d6 100644 --- a/llarp/quic/endpoint.hpp +++ b/llarp/quic/endpoint.hpp @@ -43,7 +43,7 @@ namespace llarp::quic /// address based on the convo tag. The port is not used. /// \param ecn - the packet ecn parameter void - receive_packet(const SockAddr& src, uint8_t ecn, bstring_view data); + receive_packet(Address addr, uint8_t ecn, bstring_view data, uint16_t remote_port); /// Returns a shared pointer to the uvw loop. std::shared_ptr diff --git a/llarp/quic/tunnel.cpp b/llarp/quic/tunnel.cpp index d2da7c94e..aed5e45b3 100644 --- a/llarp/quic/tunnel.cpp +++ b/llarp/quic/tunnel.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "llarp/net/net_int.hpp" #include "stream.hpp" #include #include @@ -526,12 +527,13 @@ namespace llarp::quic // accept handler, and to let the accept handler know that `this` is still safe to use. ct.tcp->data(std::make_shared(pport)); - auto after_path = [this, port, pport = pport, remote_addr](auto maybe_convo) { - if (not continue_connecting(pport, (bool)maybe_convo, "path build", remote_addr)) + auto after_path = [this, port, pport = pport, remote_addr](auto maybe_addr) { + if (maybe_addr) + { + make_client(port, *maybe_addr, *client_tunnels_.find(pport)); return; - SockAddr dest{maybe_convo->ToV6()}; - dest.setPort(port); - make_client(dest, *client_tunnels_.find(pport)); + } + continue_connecting(pport, false, "path build", remote_addr); }; if (!maybe_remote) @@ -554,8 +556,9 @@ namespace llarp::quic auto& remote = *maybe_remote; // See if we have an existing convo tag we can use to start things immediately - if (auto maybe_convo = service_endpoint_.GetBestConvoTagFor(remote)) - after_path(maybe_convo); + if (auto maybe_convo = service_endpoint_.GetBestConvoTagFor(remote); + auto maybe_addr = service_endpoint_.GetEndpointWithConvoTag(*maybe_convo)) + after_path(maybe_addr); else { service_endpoint_.MarkAddressOutbound(remote); @@ -600,12 +603,15 @@ namespace llarp::quic } void - TunnelManager::make_client(const SockAddr& remote, std::pair& row) + TunnelManager::make_client( + const uint16_t port, + std::variant remote, + std::pair& row) { - assert(remote.getPort() > 0); + assert(port > 0); auto& [pport, tunnel] = row; assert(not tunnel.client); - tunnel.client = std::make_unique(service_endpoint_, remote, pport); + tunnel.client = std::make_unique(service_endpoint_, port, std::move(remote), pport); auto conn = tunnel.client->get_connection(); conn->on_stream_available = [this, id = row.first](Connection&) { @@ -680,7 +686,7 @@ namespace llarp::quic } void - TunnelManager::receive_packet(const service::ConvoTag& tag, const llarp_buffer_t& buf) + TunnelManager::receive_packet(std::variant remote, const llarp_buffer_t& buf) { if (buf.sz <= 4) { @@ -694,13 +700,17 @@ namespace llarp::quic auto ecn = static_cast(buf.base[3]); bstring_view data{reinterpret_cast(&buf.base[4]), buf.sz - 4}; - SockAddr remote{tag.ToV6()}; + //auto addr_data = var::visit([](auto& addr) { return addr.as_array(); }, remote); + //huint128_t ip{}; + //std::copy_n(addr_data.begin(), sizeof(ip.h), &ip.h); + huint16_t remote_port{pseudo_port}; + quic::Endpoint* ep = nullptr; + if (type == CLIENT_TO_SERVER) { - log::trace(logcat, "packet is client-to-server from client pport {}", pseudo_port); - // Client-to-server: the header port is the return port - remote.setPort(pseudo_port); + log::debug(logcat, "packet is client-to-server from client pport {}", pseudo_port); + if (!server_) { log::warning(logcat, "Dropping incoming quic packet to server: no listeners"); @@ -710,7 +720,7 @@ namespace llarp::quic } else if (type == SERVER_TO_CLIENT) { - log::trace(logcat, "packet is server-to-client to client pport {}", pseudo_port); + log::debug(logcat, "packet is server-to-client to client pport {}", pseudo_port); // Server-to-client: the header port tells us which client tunnel this is going to if (auto it = client_tunnels_.find(pseudo_port); it != client_tunnels_.end()) ep = it->second.client.get(); @@ -722,11 +732,11 @@ namespace llarp::quic } // The server doesn't send back the port because we already know it 1-to-1 from our outgoing - // connection. + // connection if (auto conn = static_cast(*ep).get_connection()) { - remote.setPort(conn->path.remote.port()); - log::trace(logcat, "remote port is {}", remote.getPort()); + remote_port = huint16_t{conn->path.remote.port().n}; + log::debug(logcat, "remote port is {}", remote_port); } else { @@ -740,6 +750,8 @@ namespace llarp::quic log::warning(logcat, "Invalid incoming quic packet type {}; dropping packet", type); return; } - ep->receive_packet(remote, ecn, data); + + auto remote_addr = Address{SockAddr{"::1"sv, huint16_t{remote_port}}, std::move(remote)}; + ep->receive_packet(std::move(remote_addr), ecn, data, pseudo_port); } } // namespace llarp::quic diff --git a/llarp/quic/tunnel.hpp b/llarp/quic/tunnel.hpp index ec002417f..704d26c97 100644 --- a/llarp/quic/tunnel.hpp +++ b/llarp/quic/tunnel.hpp @@ -133,7 +133,7 @@ namespace llarp::quic /// \param buf - the raw arriving packet /// void - receive_packet(const service::ConvoTag& tag, const llarp_buffer_t& buf); + receive_packet(std::variant remote, const llarp_buffer_t& buf); /// return true if we have any listeners added inline bool @@ -176,7 +176,10 @@ namespace llarp::quic uint16_t pseudo_port, bool step_success, std::string_view step_name, std::string_view addr); void - make_client(const SockAddr& remote, std::pair& row); + make_client( + const uint16_t port, + std::variant ep, + std::pair& row); void flush_pending_incoming(ClientTunnel& ct); diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 0a749137c..ca3629ec6 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -1703,37 +1703,7 @@ namespace llarp return true; } - bool - Endpoint::SendToOrQueue(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t) - { - if (tag.IsZero()) - { - LogWarn("SendToOrQueue failed: convo tag is zero"); - return false; - } - LogDebug(Name(), " send ", pkt.sz, " bytes on T=", tag); - if (auto maybe = GetEndpointWithConvoTag(tag)) - { - if (auto* ptr = std::get_if
(&*maybe)) - { - if (*ptr == m_Identity.pub.Addr()) - { - ConvoTagTX(tag); - m_state->m_Router->TriggerPump(); - if (not HandleInboundPacket(tag, pkt, t, 0)) - return false; - ConvoTagRX(tag); - return true; - } - } - if (not SendToOrQueue(*maybe, pkt, t)) - return false; - return true; - } - LogDebug("SendToOrQueue failed: no endpoint for convo tag ", tag); - return false; - } - + bool Endpoint::SendToOrQueue(const RouterID& addr, const llarp_buffer_t& buf, ProtocolType t) { @@ -1752,6 +1722,7 @@ namespace llarp return true; } + void Endpoint::Pump(llarp_time_t now) { @@ -1886,7 +1857,7 @@ namespace llarp bool Endpoint::EnsurePathTo( std::variant addr, - std::function)> hook, + std::function>)> hook, llarp_time_t timeout) { if (auto ptr = std::get_if
(&addr)) @@ -1895,14 +1866,15 @@ namespace llarp { ConvoTag tag{}; - if (auto maybe = GetBestConvoTagFor(*ptr)) - tag = *maybe; + if (auto maybe_tag = GetBestConvoTagFor(*ptr)) + tag = *maybe_tag; else tag.Randomize(); PutSenderFor(tag, m_Identity.pub, true); ConvoTagTX(tag); Sessions()[tag].forever = true; - Loop()->call_soon([tag, hook]() { hook(tag); }); + auto maybe_addr = GetEndpointWithConvoTag(tag); + Loop()->call_soon([maybe_addr, hook]() { hook(maybe_addr); }); return true; } if (not WantsOutboundSession(*ptr)) @@ -1914,10 +1886,11 @@ namespace llarp return EnsurePathToService( *ptr, - [hook](auto, auto* ctx) { + [hook, this](auto, auto* ctx) { if (ctx) { - hook(ctx->currentConvoTag); + if (auto maybe_addr = GetEndpointWithConvoTag(ctx->currentConvoTag)) + hook(maybe_addr); } else { @@ -1928,10 +1901,11 @@ namespace llarp } if (auto ptr = std::get_if(&addr)) { - return EnsurePathToSNode(*ptr, [hook](auto, auto session, auto tag) { + return EnsurePathToSNode(*ptr, [hook, this](auto, auto session, auto tag) { if (session) { - hook(tag); + if (auto maybe_addr = GetEndpointWithConvoTag(tag)) + hook(maybe_addr); } else { @@ -2082,7 +2056,7 @@ namespace llarp bool Endpoint::SendToOrQueue( - const std::variant& addr, const llarp_buffer_t& data, ProtocolType t) + std::variant addr, const llarp_buffer_t& data, ProtocolType t) { return var::visit([&](auto& addr) { return SendToOrQueue(addr, data, t); }, addr); } diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 46fd42e7b..5e0b2cee3 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -346,7 +346,7 @@ namespace llarp bool EnsurePathTo( std::variant addr, - std::function)> hook, + std::function>)> hook, llarp_time_t timeout) override; // passed a sendto context when we have a path established otherwise @@ -468,19 +468,19 @@ namespace llarp // Looks up the ConvoTag and, if it exists, calls SendToOrQueue to send it to a remote client // or a snode (or nothing, if the convo tag is unknown). - bool - SendToOrQueue(ConvoTag tag, const llarp_buffer_t& payload, ProtocolType t) override; + //bool + //SendToOrQueue(std::variant addr, const llarp_buffer_t& payload, ProtocolType t) override; // Send a to (or queues for sending) to either an address or router id bool SendToOrQueue( - const std::variant& addr, + std::variant addr, const llarp_buffer_t& payload, - ProtocolType t); + ProtocolType t) override; // Sends to (or queues for sending) to a remote client bool - SendToOrQueue(const Address& addr, const llarp_buffer_t& payload, ProtocolType t); + SendToOrQueue(const Address& remote, const llarp_buffer_t& payload, ProtocolType t); // Sends to (or queues for sending) to a router bool