2020-06-09 16:59:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unbound.h>
|
|
|
|
#include <mutex>
|
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include <ev/ev.hpp>
|
|
|
|
#include <util/thread/logic.hpp>
|
|
|
|
|
|
|
|
#include <dns/message.hpp>
|
|
|
|
|
2020-08-04 14:53:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <thread>
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 16:59:49 +00:00
|
|
|
namespace llarp::dns
|
|
|
|
{
|
2020-06-15 18:22:52 +00:00
|
|
|
using ReplyFunction = std::function<void(SockAddr source, std::vector<byte_t> buf)>;
|
2020-06-15 18:15:10 +00:00
|
|
|
using FailFunction = std::function<void(SockAddr source, Message msg)>;
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
class UnboundResolver : public std::enable_shared_from_this<UnboundResolver>
|
|
|
|
{
|
2020-06-11 19:36:19 +00:00
|
|
|
private:
|
2020-06-09 16:59:49 +00:00
|
|
|
ub_ctx* unboundContext;
|
|
|
|
|
2020-08-04 14:53:32 +00:00
|
|
|
std::atomic<bool> started;
|
2020-10-27 21:34:09 +00:00
|
|
|
std::unique_ptr<std::thread> runner;
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
llarp_ev_loop_ptr eventLoop;
|
|
|
|
ReplyFunction replyFunc;
|
2020-06-15 18:15:10 +00:00
|
|
|
FailFunction failFunc;
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
|
|
|
Reset();
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
public:
|
2020-06-15 18:15:10 +00:00
|
|
|
UnboundResolver(llarp_ev_loop_ptr eventLoop, ReplyFunction replyFunc, FailFunction failFunc);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
static void
|
|
|
|
Callback(void* data, int err, ub_result* result);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-10-29 14:19:45 +00:00
|
|
|
// stop resolver thread
|
|
|
|
void
|
|
|
|
Stop();
|
|
|
|
|
2020-06-09 16:59:49 +00:00
|
|
|
// upstream resolver IP can be IPv4 or IPv6
|
2020-06-11 19:36:19 +00:00
|
|
|
bool
|
|
|
|
Init();
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
bool
|
|
|
|
AddUpstreamResolver(const std::string& upstreamResolverIP);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
2020-06-15 18:15:10 +00:00
|
|
|
Lookup(const SockAddr& source, Message msg);
|
2020-06-09 16:59:49 +00:00
|
|
|
};
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
} // namespace llarp::dns
|