2021-03-02 18:18:22 +00:00
|
|
|
#pragma once
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/net/ip_packet.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/net/net_int.hpp>
|
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace llarp::vpn
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
using PacketHandlerFunc_t = std::function<void(llarp::net::IPPacket)>;
|
2021-03-02 18:18:22 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
struct Layer4Handler;
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
class PacketRouter
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
PacketHandlerFunc_t m_BaseHandler;
|
2021-03-02 18:18:22 +00:00
|
|
|
std::unordered_map<uint8_t, std::unique_ptr<Layer4Handler>> m_IPProtoHandler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// baseHandler will be called if no other handlers matches a packet
|
2022-07-28 16:07:38 +00:00
|
|
|
explicit PacketRouter(PacketHandlerFunc_t baseHandler);
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
/// feed in an ip packet for handling
|
|
|
|
void
|
|
|
|
HandleIPPacket(llarp::net::IPPacket pkt);
|
|
|
|
|
|
|
|
/// add a non udp packet handler using ip protocol proto
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
AddIProtoHandler(uint8_t proto, PacketHandlerFunc_t func);
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
/// helper that adds a udp packet handler for UDP destinted for localport
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
AddUDPHandler(huint16_t localport, PacketHandlerFunc_t func);
|
2021-09-23 18:01:04 +00:00
|
|
|
|
|
|
|
/// remove a udp handler that is already set up by bound port
|
|
|
|
void
|
|
|
|
RemoveUDPHandler(huint16_t localport);
|
2021-03-02 18:18:22 +00:00
|
|
|
};
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
struct Layer4Handler
|
|
|
|
{
|
|
|
|
virtual ~Layer4Handler() = default;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
HandleIPPacket(llarp::net::IPPacket pkt) = 0;
|
|
|
|
|
|
|
|
virtual void AddSubHandler(nuint16_t, PacketHandlerFunc_t){};
|
|
|
|
};
|
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
} // namespace llarp::vpn
|