2020-06-09 16:59:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/ev/ev.hpp>
|
2021-06-09 10:38:11 +00:00
|
|
|
#include <llarp/util/fs.hpp>
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "message.hpp"
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-08-04 14:53:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <thread>
|
2021-09-01 17:05:58 +00:00
|
|
|
#else
|
|
|
|
#include <uvw.hpp>
|
2020-08-04 14:53:32 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-01 21:58:00 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
struct ub_ctx;
|
|
|
|
struct ub_result;
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:59:49 +00:00
|
|
|
namespace llarp::dns
|
|
|
|
{
|
2021-03-02 18:18:22 +00:00
|
|
|
using ReplyFunction =
|
2021-09-01 17:36:38 +00:00
|
|
|
std::function<void(const SockAddr& reply_to, const SockAddr& from_resolver, OwnedBuffer buf)>;
|
2021-03-02 02:06:20 +00:00
|
|
|
using FailFunction =
|
2021-09-01 17:36:38 +00:00
|
|
|
std::function<void(const SockAddr& reply_to, const SockAddr& from_resolver, 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;
|
2021-09-01 17:05:58 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
std::thread runner;
|
|
|
|
#else
|
|
|
|
std::weak_ptr<uvw::Loop> loop;
|
|
|
|
std::shared_ptr<uvw::PollHandle> udp;
|
|
|
|
#endif
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
ReplyFunction replyFunc;
|
2020-06-15 18:15:10 +00:00
|
|
|
FailFunction failFunc;
|
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:
|
2021-03-02 07:02:59 +00:00
|
|
|
UnboundResolver(EventLoop_ptr loop, 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
|
2021-05-30 17:29:00 +00:00
|
|
|
AddUpstreamResolver(const SockAddr& upstreamResolverIP);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2021-06-09 10:38:11 +00:00
|
|
|
void
|
|
|
|
AddHostsFile(const fs::path& file);
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
2021-03-02 18:18:22 +00:00
|
|
|
Lookup(SockAddr to, SockAddr from, Message msg);
|
2020-06-09 16:59:49 +00:00
|
|
|
};
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
} // namespace llarp::dns
|