From e27fdf4c9a3979c1fd2e34b923cc8a2b13a2a5ee Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 23 Mar 2021 15:18:21 -0300 Subject: [PATCH] Simplify/unify SendTo* methods Overload them to the same name to make it easier to call and/or visit them with either a RouterID, Address, Variant, or ConvoTag. --- llarp/handlers/tun.cpp | 38 +++++++++-------------------- llarp/service/endpoint.cpp | 26 +++++++++----------- llarp/service/endpoint.hpp | 24 +++++++++++++----- pybind/llarp/context.cpp | 3 +-- pybind/llarp/handlers/pyhandler.hpp | 2 +- 5 files changed, 42 insertions(+), 51 deletions(-) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index a96a9af63..59f44fdb5 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -902,8 +902,6 @@ namespace llarp TunEndpoint::FlushSend() { m_UserToNetworkPktQueue.Process([&](net::IPPacket& pkt) { - std::function sendFunc; - huint128_t dst, src; if (pkt.IsV4()) { @@ -927,7 +925,7 @@ namespace llarp for (const auto& [ip, addr] : m_IPToAddr) { (void)ip; - SendToServiceOrQueue( + SendToOrQueue( service::Address{addr.as_array()}, pkt.ConstBuffer(), service::ProtocolType::Exit); } return; @@ -963,41 +961,27 @@ namespace llarp { ctx->sendTimeout = 5s; } - self->SendToServiceOrQueue(addr, pkt.ConstBuffer(), service::ProtocolType::Exit); + self->SendToOrQueue(addr, pkt.ConstBuffer(), service::ProtocolType::Exit); }, 1s); } return; } bool rewriteAddrs = true; + std::variant to; + service::ProtocolType type; if (m_SNodes.at(itr->second)) { - sendFunc = std::bind( - &TunEndpoint::SendToSNodeOrQueue, - this, - RouterID{itr->second.as_array()}, - std::placeholders::_1, - service::ProtocolType::TrafficV4); - } - else if (m_state->m_ExitEnabled and src != m_OurIP) - { - rewriteAddrs = false; - sendFunc = std::bind( - &TunEndpoint::SendToServiceOrQueue, - this, - service::Address{itr->second.as_array()}, - std::placeholders::_1, - service::ProtocolType::Exit); + to = RouterID{itr->second.as_array()}; + type = service::ProtocolType::TrafficV4; } else { - sendFunc = std::bind( - &TunEndpoint::SendToServiceOrQueue, - this, - service::Address{itr->second.as_array()}, - std::placeholders::_1, - pkt.ServiceProtocol()); + to = service::Address{itr->second.as_array()}; + type = m_state->m_ExitEnabled and src != m_OurIP ? service::ProtocolType::Exit + : pkt.ServiceProtocol(); } + // prepare packet for insertion into network // this includes clearing IP addresses, recalculating checksums, etc if (rewriteAddrs) @@ -1007,7 +991,7 @@ namespace llarp else pkt.UpdateIPv6Address({0}, {0}); } - if (sendFunc && sendFunc(pkt.Buffer())) + if (SendToOrQueue(to, pkt.Buffer(), type)) { MarkIPActive(dst); return; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 426e75b8b..fae0b7182 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -1381,25 +1381,15 @@ namespace llarp } bool - Endpoint::SendTo(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t) + Endpoint::SendToOrQueue(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t) { if (auto maybe = GetEndpointWithConvoTag(tag)) - { - auto addr = *maybe; - if (auto ptr = std::get_if
(&addr)) - { - return SendToServiceOrQueue(*ptr, pkt, t); - } - if (auto ptr = std::get_if(&addr)) - { - return SendToSNodeOrQueue(*ptr, pkt, t); - } - } + return SendToOrQueue(*maybe, pkt, t); return false; } bool - Endpoint::SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& buf, ProtocolType t) + Endpoint::SendToOrQueue(const RouterID& addr, const llarp_buffer_t& buf, ProtocolType t) { auto pkt = std::make_shared(); if (!pkt->Load(buf)) @@ -1514,8 +1504,7 @@ namespace llarp } bool - Endpoint::SendToServiceOrQueue( - const service::Address& remote, const llarp_buffer_t& data, ProtocolType t) + Endpoint::SendToOrQueue(const Address& remote, const llarp_buffer_t& data, ProtocolType t) { if (data.sz == 0) return false; @@ -1626,6 +1615,13 @@ namespace llarp return false; } + bool + Endpoint::SendToOrQueue( + const std::variant& addr, const llarp_buffer_t& data, ProtocolType t) + { + return var::visit([&](auto& addr) { return SendToOrQueue(addr, data, t); }, addr); + } + bool Endpoint::HasConvoTag(const ConvoTag& t) const { diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index a92c72293..a461000df 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "endpoint_types.hpp" #include "auth.hpp" @@ -227,9 +228,6 @@ namespace llarp void HandlePathBuilt(path::Path_ptr path) override; - bool - SendTo(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t); - bool HandleDataDrop(path::Path_ptr p, const PathID_t& dst, uint64_t s); @@ -365,11 +363,25 @@ namespace llarp const std::set& SnodeBlacklist() const; + // 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); + + // Send a to (or queues for sending) to either an address or router id + bool + SendToOrQueue( + const std::variant& addr, + const llarp_buffer_t& payload, + ProtocolType t); + + // Sends to (or queues for sending) to a remote client bool - SendToServiceOrQueue( - const service::Address& addr, const llarp_buffer_t& payload, ProtocolType t); + SendToOrQueue(const Address& addr, const llarp_buffer_t& payload, ProtocolType t); + + // Sends to (or queues for sending) to a router bool - SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& payload, ProtocolType t); + SendToOrQueue(const RouterID& addr, const llarp_buffer_t& payload, ProtocolType t); std::optional MaybeGetAuthInfoForEndpoint(service::Address addr); diff --git a/pybind/llarp/context.cpp b/pybind/llarp/context.cpp index 7fb369468..db9fad6f3 100644 --- a/pybind/llarp/context.cpp +++ b/pybind/llarp/context.cpp @@ -29,8 +29,7 @@ namespace llarp std::vector buf; buf.resize(pkt.size()); std::copy_n(pkt.c_str(), pkt.size(), buf.data()); - return ep - and ep->SendToServiceOrQueue(to, std::move(buf), service::ProtocolType::Control); + return ep and ep->SendToOrQueue(to, std::move(buf), service::ProtocolType::Control); }) .def( "AddEndpoint", diff --git a/pybind/llarp/handlers/pyhandler.hpp b/pybind/llarp/handlers/pyhandler.hpp index 452a29ea2..19bcc9d05 100644 --- a/pybind/llarp/handlers/pyhandler.hpp +++ b/pybind/llarp/handlers/pyhandler.hpp @@ -86,7 +86,7 @@ namespace llarp SendPacket(service::Address remote, std::vector pkt, service::ProtocolType proto) { m_router->loop()->call([remote, pkt, proto, self = shared_from_this()]() { - self->SendToServiceOrQueue(remote, llarp_buffer_t(pkt), proto); + self->SendToOrQueue(remote, llarp_buffer_t(pkt), proto); }); }