lokinet/llarp/dns/server.hpp

104 lines
2.3 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>
2018-12-03 22:22:59 +00:00
#include <unordered_map>
namespace llarp
{
namespace dns
{
/// handler of dns query hooking
struct IQueryHandler
{
2018-12-13 22:22:53 +00:00
virtual ~IQueryHandler(){};
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;
};
struct Proxy
{
Proxy(llarp_ev_loop* loop, IQueryHandler* handler);
bool
Start(const llarp::Addr& addr,
const std::vector< llarp::Addr >& resolvers);
void
Stop();
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
2018-12-04 16:35:25 +00:00
HandlePktClient(llarp::Addr from, llarp_buffer_t* buf);
void
HandlePktServer(llarp::Addr from, llarp_buffer_t* buf);
2018-12-03 22:22:59 +00:00
void
SendMessageTo(llarp::Addr to, Message msg);
llarp::Addr
PickRandomResolver() const;
private:
2018-12-04 16:35:25 +00:00
llarp_udp_io m_Server;
llarp_udp_io m_Client;
2018-12-03 22:22:59 +00:00
llarp_ev_loop* m_Loop;
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