2021-03-09 22:24:35 +00:00
|
|
|
#include "packet_router.hpp"
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
namespace llarp::vpn
|
|
|
|
{
|
|
|
|
struct UDPPacketHandler : public Layer4Handler
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
PacketHandlerFunc_t m_BaseHandler;
|
|
|
|
std::unordered_map<nuint16_t, PacketHandlerFunc_t> m_LocalPorts;
|
2021-03-02 18:18:22 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
explicit UDPPacketHandler(PacketHandlerFunc_t baseHandler)
|
|
|
|
: m_BaseHandler{std::move(baseHandler)}
|
2021-03-02 18:18:22 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
AddSubHandler(nuint16_t localport, PacketHandlerFunc_t handler) override
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
|
|
|
m_LocalPorts.emplace(localport, std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleIPPacket(llarp::net::IPPacket pkt) override
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
auto dstport = pkt.DstPort();
|
|
|
|
if (not dstport)
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
m_BaseHandler(std::move(pkt));
|
|
|
|
return;
|
2021-03-02 18:18:22 +00:00
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
if (auto itr = m_LocalPorts.find(*dstport); itr != m_LocalPorts.end())
|
|
|
|
itr->second(std::move(pkt));
|
2021-03-02 18:18:22 +00:00
|
|
|
else
|
|
|
|
m_BaseHandler(std::move(pkt));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GenericLayer4Handler : public Layer4Handler
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
PacketHandlerFunc_t m_BaseHandler;
|
2021-03-02 18:18:22 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
explicit GenericLayer4Handler(PacketHandlerFunc_t baseHandler)
|
2021-03-02 18:18:22 +00:00
|
|
|
: m_BaseHandler{std::move(baseHandler)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleIPPacket(llarp::net::IPPacket pkt) override
|
|
|
|
{
|
|
|
|
m_BaseHandler(std::move(pkt));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
PacketRouter::PacketRouter(PacketHandlerFunc_t baseHandler)
|
|
|
|
: m_BaseHandler{std::move(baseHandler)}
|
2021-03-02 18:18:22 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
PacketRouter::HandleIPPacket(llarp::net::IPPacket pkt)
|
|
|
|
{
|
|
|
|
const auto proto = pkt.Header()->protocol;
|
|
|
|
if (const auto itr = m_IPProtoHandler.find(proto); itr != m_IPProtoHandler.end())
|
|
|
|
itr->second->HandleIPPacket(std::move(pkt));
|
|
|
|
else
|
|
|
|
m_BaseHandler(std::move(pkt));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
PacketRouter::AddUDPHandler(huint16_t localport, PacketHandlerFunc_t func)
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
|
|
|
constexpr byte_t udp_proto = 0x11;
|
|
|
|
|
|
|
|
if (m_IPProtoHandler.find(udp_proto) == m_IPProtoHandler.end())
|
|
|
|
{
|
|
|
|
m_IPProtoHandler.emplace(udp_proto, std::make_unique<UDPPacketHandler>(m_BaseHandler));
|
|
|
|
}
|
|
|
|
m_IPProtoHandler[udp_proto]->AddSubHandler(ToNet(localport), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
PacketRouter::AddIProtoHandler(uint8_t proto, PacketHandlerFunc_t func)
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
|
|
|
m_IPProtoHandler[proto] = std::make_unique<GenericLayer4Handler>(std::move(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::vpn
|