lokinet/llarp/handlers/null.hpp

126 lines
3.1 KiB
C++
Raw Normal View History

#pragma once
2018-12-12 02:15:08 +00:00
#include <llarp/service/endpoint.hpp>
#include <llarp/service/protocol_type.hpp>
#include <llarp/quic/tunnel.hpp>
#include <llarp/router/router.hpp>
#include <llarp/ev/ev.hpp>
2021-09-23 18:01:04 +00:00
#include <llarp/vpn/egres_packet_router.hpp>
2018-11-29 13:12:35 +00:00
2021-09-23 18:01:04 +00:00
namespace llarp::handlers
2018-11-29 13:12:35 +00:00
{
2021-09-23 18:01:04 +00:00
struct NullEndpoint final : public llarp::service::Endpoint,
public std::enable_shared_from_this<NullEndpoint>
2018-11-29 13:12:35 +00:00
{
NullEndpoint(Router* r, llarp::service::Context* parent)
2021-09-23 18:01:04 +00:00
: llarp::service::Endpoint{r, parent}
, m_PacketRouter{new vpn::EgresPacketRouter{[](auto from, auto pkt) {
var::visit(
[&pkt](auto&& from) {
LogError("unhandled traffic from: ", from, " of ", pkt.size(), " bytes");
},
from);
}}}
2018-11-29 13:12:35 +00:00
{
2021-09-23 18:01:04 +00:00
r->loop()->add_ticker([this] { Pump(Now()); });
}
virtual bool
HandleInboundPacket(
const service::ConvoTag tag,
const llarp_buffer_t& buf,
service::ProtocolType t,
uint64_t) override
{
LogTrace("Inbound ", t, " packet (", buf.sz, "B) on convo ", tag);
if (t == service::ProtocolType::Control)
{
2021-09-23 18:01:04 +00:00
return true;
}
2021-09-23 18:01:04 +00:00
if (t == service::ProtocolType::TrafficV4 or t == service::ProtocolType::TrafficV6)
2018-11-29 13:12:35 +00:00
{
2021-09-23 18:01:04 +00:00
if (auto from = GetEndpointWithConvoTag(tag))
{
2021-09-23 18:01:04 +00:00
net::IPPacket pkt{};
if (not pkt.Load(buf))
{
LogWarn("invalid ip packet from remote T=", tag);
return false;
}
m_PacketRouter->HandleIPPacketFrom(std::move(*from), std::move(pkt));
return true;
}
2021-09-23 18:01:04 +00:00
else
{
2021-09-23 18:01:04 +00:00
LogWarn("did not handle packet, no endpoint with convotag T=", tag);
return false;
}
2018-11-29 13:12:35 +00:00
}
2021-09-23 18:01:04 +00:00
if (t != service::ProtocolType::QUIC)
return false;
2018-11-29 13:12:35 +00:00
2021-09-23 18:01:04 +00:00
auto* quic = GetQUICTunnel();
if (!quic)
2020-08-21 15:07:37 +00:00
{
2021-09-23 18:01:04 +00:00
LogWarn("incoming quic packet but this endpoint is not quic capable; dropping");
return false;
2020-08-21 15:07:37 +00:00
}
2021-09-23 18:01:04 +00:00
if (buf.sz < 4)
2019-04-23 16:13:22 +00:00
{
2021-09-23 18:01:04 +00:00
LogWarn("invalid incoming quic packet, dropping");
return false;
2019-04-23 16:13:22 +00:00
}
2021-09-23 18:01:04 +00:00
quic->receive_packet(tag, buf);
return true;
}
2019-04-23 16:13:22 +00:00
2021-09-23 18:01:04 +00:00
std::string
GetIfName() const override
{
return "";
}
2021-09-23 18:01:04 +00:00
path::PathSet_ptr
GetSelf() override
{
return shared_from_this();
}
2020-05-21 14:18:23 +00:00
2021-09-23 18:01:04 +00:00
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
2021-09-23 18:01:04 +00:00
bool
SupportsV6() const override
{
return false;
}
2021-09-23 18:01:04 +00:00
void
SendPacketToRemote(const llarp_buffer_t&, service::ProtocolType) override{};
2022-10-20 22:23:14 +00:00
huint128_t
ObtainIPForAddr(std::variant<service::Address, RouterID>) override
2021-09-23 18:01:04 +00:00
{
return {0};
}
2022-10-20 22:23:14 +00:00
std::optional<std::variant<service::Address, RouterID>>
ObtainAddrForIP(huint128_t) const override
2021-09-23 18:01:04 +00:00
{
return std::nullopt;
}
vpn::EgresPacketRouter*
EgresPacketRouter() override
{
return m_PacketRouter.get();
}
private:
std::unique_ptr<vpn::EgresPacketRouter> m_PacketRouter;
};
} // namespace llarp::handlers