lokinet/llarp/vpn/packet_router.cpp
Jeff 871c3e3281
changeset for windows port
* wintun vpn platform for windows
* bundle config snippets into nsis installer for exit node, keyfile persisting, reduced hops mode.
* use wintun for vpn platform
* isolate all windows platform specific code into their own compilation units and libraries
* split up internal libraries into more specific components
* rename liblokinet.a target to liblokinet-amalgum.a to elimiate ambiguity with liblokinet.so
* DNS platform for win32
* rename llarp/ev/ev_libuv.{c,h}pp to llarp/ev/libuv.{c,h}pp as the old name was idiotic
* split up net platform into win32 and posix specific compilation units
* rename lokinet_init.c to easter_eggs.cpp as that is what they are for and it does not need to be a c compilation target
* add cmake option STRIP_SYMBOLS for seperating out debug symbols for windows builds
* intercept dns traffic on all interfaces on windows using windivert and feed it into lokinet
2022-09-08 14:24:59 -04:00

85 lines
2.2 KiB
C++

#include "packet_router.hpp"
namespace llarp::vpn
{
struct UDPPacketHandler : public Layer4Handler
{
PacketHandlerFunc_t m_BaseHandler;
std::unordered_map<nuint16_t, PacketHandlerFunc_t> m_LocalPorts;
explicit UDPPacketHandler(PacketHandlerFunc_t baseHandler)
: m_BaseHandler{std::move(baseHandler)}
{}
void
AddSubHandler(nuint16_t localport, PacketHandlerFunc_t handler) override
{
m_LocalPorts.emplace(localport, std::move(handler));
}
void
HandleIPPacket(llarp::net::IPPacket pkt) override
{
auto dstport = pkt.DstPort();
if (not dstport)
{
m_BaseHandler(std::move(pkt));
return;
}
if (auto itr = m_LocalPorts.find(*dstport); itr != m_LocalPorts.end())
itr->second(std::move(pkt));
else
m_BaseHandler(std::move(pkt));
}
};
struct GenericLayer4Handler : public Layer4Handler
{
PacketHandlerFunc_t m_BaseHandler;
explicit GenericLayer4Handler(PacketHandlerFunc_t baseHandler)
: m_BaseHandler{std::move(baseHandler)}
{}
void
HandleIPPacket(llarp::net::IPPacket pkt) override
{
m_BaseHandler(std::move(pkt));
}
};
PacketRouter::PacketRouter(PacketHandlerFunc_t baseHandler)
: m_BaseHandler{std::move(baseHandler)}
{}
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
PacketRouter::AddUDPHandler(huint16_t localport, PacketHandlerFunc_t func)
{
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
PacketRouter::AddIProtoHandler(uint8_t proto, PacketHandlerFunc_t func)
{
m_IPProtoHandler[proto] = std::make_unique<GenericLayer4Handler>(std::move(func));
}
} // namespace llarp::vpn