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.
pull/1576/head
Jason Rhinelander 3 years ago committed by Jeff Becker
parent 104f63543f
commit e27fdf4c9a
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -902,8 +902,6 @@ namespace llarp
TunEndpoint::FlushSend()
{
m_UserToNetworkPktQueue.Process([&](net::IPPacket& pkt) {
std::function<bool(const llarp_buffer_t&)> 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<service::Address, RouterID> 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;

@ -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<Address>(&addr))
{
return SendToServiceOrQueue(*ptr, pkt, t);
}
if (auto ptr = std::get_if<RouterID>(&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<net::IPPacket>();
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<Address, RouterID>& 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
{

@ -21,6 +21,7 @@
#include <optional>
#include <unordered_map>
#include <variant>
#include <oxenmq/variant.h>
#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<RouterID>&
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<Address, RouterID>& 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<AuthInfo>
MaybeGetAuthInfoForEndpoint(service::Address addr);

@ -29,8 +29,7 @@ namespace llarp
std::vector<byte_t> 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",

@ -86,7 +86,7 @@ namespace llarp
SendPacket(service::Address remote, std::vector<byte_t> 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);
});
}

Loading…
Cancel
Save