lokinet/llarp/dns/server.hpp

116 lines
2.7 KiB
C++
Raw Normal View History

2018-12-03 22:22:59 +00:00
#ifndef LLARP_DNS_SERVER_HPP
#define LLARP_DNS_SERVER_HPP
#include <dns/message.hpp>
2019-01-11 01:19:36 +00:00
#include <ev/ev.h>
#include <net/net.hpp>
#include <util/string_view.hpp>
2019-09-01 13:26:16 +00:00
#include <util/thread/logic.hpp>
2018-12-03 22:22:59 +00:00
#include <unordered_map>
namespace llarp
{
namespace dns
{
/// handler of dns query hooking
struct IQueryHandler
{
2019-07-30 23:42:13 +00:00
virtual ~IQueryHandler() = default;
2018-12-13 22:20:12 +00:00
2018-12-03 22:22:59 +00:00
/// return true if we should hook this message
virtual bool
ShouldHookDNSMessage(const Message& msg) const = 0;
/// handle a hooked message
virtual bool
HandleHookedDNSMessage(dns::Message&& query,
2018-12-03 22:22:59 +00:00
std::function< void(Message) > sendReply) = 0;
};
2019-05-22 16:20:50 +00:00
struct Proxy : public std::enable_shared_from_this< Proxy >
2018-12-03 22:22:59 +00:00
{
2019-05-22 16:20:50 +00:00
using Logic_ptr = std::shared_ptr< Logic >;
Proxy(llarp_ev_loop_ptr serverLoop, Logic_ptr serverLogic,
llarp_ev_loop_ptr clientLoop, Logic_ptr clientLogic,
IQueryHandler* handler);
2018-12-03 22:22:59 +00:00
bool
Start(const llarp::Addr addr,
2018-12-03 22:22:59 +00:00
const std::vector< llarp::Addr >& resolvers);
void
Stop();
2019-11-01 13:40:31 +00:00
using Buffer_t = std::vector< uint8_t >;
2018-12-03 22:22:59 +00:00
private:
/// low level packet handler
static void
2018-12-04 16:35:25 +00:00
HandleUDPRecv_client(llarp_udp_io*, const struct sockaddr*,
2019-02-03 00:48:10 +00:00
ManagedBuffer);
2018-12-04 16:35:25 +00:00
static void
HandleUDPRecv_server(llarp_udp_io*, const struct sockaddr*,
2019-02-03 00:48:10 +00:00
ManagedBuffer);
2018-12-03 22:22:59 +00:00
/// low level ticker
static void
HandleTick(llarp_udp_io*);
void
Tick(llarp_time_t now);
void
2019-11-01 13:40:31 +00:00
HandlePktClient(llarp::Addr from, Buffer_t buf);
2018-12-04 16:35:25 +00:00
void
2019-11-01 13:40:31 +00:00
HandlePktServer(llarp::Addr from, Buffer_t buf);
2018-12-03 22:22:59 +00:00
void
SendClientMessageTo(llarp::Addr to, Message msg);
void
SendServerMessageTo(llarp::Addr to, Message msg);
2018-12-03 22:22:59 +00:00
llarp::Addr
PickRandomResolver() const;
private:
2018-12-04 16:35:25 +00:00
llarp_udp_io m_Server;
llarp_udp_io m_Client;
llarp_ev_loop_ptr m_ServerLoop;
llarp_ev_loop_ptr m_ClientLoop;
Logic_ptr m_ServerLogic;
Logic_ptr m_ClientLogic;
2018-12-03 22:22:59 +00:00
IQueryHandler* m_QueryHandler;
std::vector< llarp::Addr > m_Resolvers;
struct TX
{
MsgID_t txid;
llarp::Addr from;
bool
operator==(const TX& other) const
{
return txid == other.txid && from == other.from;
}
struct Hash
{
size_t
operator()(const TX& t) const noexcept
{
return t.txid ^ llarp::Addr::Hash()(t.from);
}
};
};
// maps tx to who to send reply to
std::unordered_map< TX, llarp::Addr, TX::Hash > m_Forwarded;
};
} // namespace dns
} // namespace llarp
#endif