2018-12-03 22:22:59 +00:00
|
|
|
#ifndef LLARP_DNS_SERVER_HPP
|
|
|
|
#define LLARP_DNS_SERVER_HPP
|
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <dns/message.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.h>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2020-06-09 16:59:49 +00:00
|
|
|
#include <dns/unbound_resolver.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
|
2020-04-07 18:38:56 +00:00
|
|
|
HandleHookedDNSMessage(Message query, std::function<void(Message)> sendReply) = 0;
|
2018-12-03 22:22:59 +00:00
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
struct Proxy : public std::enable_shared_from_this<Proxy>
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +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
|
2020-05-06 20:38:44 +00:00
|
|
|
Start(const IpAddress& addr, const std::vector<IpAddress>& resolvers);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Stop();
|
|
|
|
|
2021-02-05 21:48:57 +00:00
|
|
|
void
|
|
|
|
Restart();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using Buffer_t = std::vector<uint8_t>;
|
2019-11-01 13:40:31 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
private:
|
|
|
|
/// low level packet handler
|
|
|
|
static void
|
2020-05-06 20:38:44 +00:00
|
|
|
HandleUDPRecv_client(llarp_udp_io*, const SockAddr&, ManagedBuffer);
|
2018-12-04 16:35:25 +00:00
|
|
|
static void
|
2020-05-06 20:38:44 +00:00
|
|
|
HandleUDPRecv_server(llarp_udp_io*, const SockAddr&, ManagedBuffer);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
/// low level ticker
|
|
|
|
static void
|
|
|
|
HandleTick(llarp_udp_io*);
|
|
|
|
|
|
|
|
void
|
2020-05-06 20:38:44 +00:00
|
|
|
HandlePktClient(const SockAddr& from, Buffer_t buf);
|
2018-12-04 16:35:25 +00:00
|
|
|
|
|
|
|
void
|
2020-05-06 20:38:44 +00:00
|
|
|
HandlePktServer(const SockAddr& from, Buffer_t buf);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
void
|
2020-05-06 20:38:44 +00:00
|
|
|
SendClientMessageTo(const SockAddr& to, Message msg);
|
2019-05-22 16:20:03 +00:00
|
|
|
|
2020-06-15 18:15:10 +00:00
|
|
|
void
|
|
|
|
SendServerMessageBufferTo(const SockAddr& to, const llarp_buffer_t& buf);
|
|
|
|
|
2019-05-22 16:20:03 +00:00
|
|
|
void
|
2020-05-06 20:38:44 +00:00
|
|
|
SendServerMessageTo(const SockAddr& to, Message msg);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2020-06-15 18:15:10 +00:00
|
|
|
void
|
2020-06-15 18:22:52 +00:00
|
|
|
HandleUpstreamResponse(SockAddr to, std::vector<byte_t> buf);
|
2020-06-15 18:15:10 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
HandleUpstreamFailure(const SockAddr& to, Message msg);
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
IpAddress
|
2018-12-03 22:22:59 +00:00
|
|
|
PickRandomResolver() const;
|
|
|
|
|
2020-06-09 16:59:49 +00:00
|
|
|
bool
|
|
|
|
SetupUnboundResolver(const std::vector<IpAddress>& resolvers);
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
private:
|
2018-12-04 16:35:25 +00:00
|
|
|
llarp_udp_io m_Server;
|
|
|
|
llarp_udp_io m_Client;
|
2019-05-22 16:20:03 +00:00
|
|
|
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;
|
2020-05-06 20:38:44 +00:00
|
|
|
std::vector<IpAddress> m_Resolvers;
|
2020-06-09 16:59:49 +00:00
|
|
|
std::shared_ptr<UnboundResolver> m_UnboundResolver;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
struct TX
|
|
|
|
{
|
|
|
|
MsgID_t txid;
|
2020-05-06 20:38:44 +00:00
|
|
|
IpAddress from;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const TX& other) const
|
|
|
|
{
|
|
|
|
return txid == other.txid && from == other.from;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const TX& t) const noexcept
|
|
|
|
{
|
2020-05-06 20:38:44 +00:00
|
|
|
return t.txid ^ IpAddress::Hash()(t.from);
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// maps tx to who to send reply to
|
2020-05-06 20:38:44 +00:00
|
|
|
std::unordered_map<TX, IpAddress, TX::Hash> m_Forwarded;
|
2018-12-03 22:22:59 +00:00
|
|
|
};
|
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|