2020-06-09 16:59:49 +00:00
|
|
|
#include <dns/unbound_resolver.hpp>
|
|
|
|
|
|
|
|
#include <dns/server.hpp>
|
|
|
|
#include <util/buffer.hpp>
|
|
|
|
|
|
|
|
namespace llarp::dns
|
|
|
|
{
|
|
|
|
struct PendingUnboundLookup
|
|
|
|
{
|
|
|
|
std::weak_ptr<UnboundResolver> resolver;
|
|
|
|
Message msg;
|
|
|
|
SockAddr source;
|
|
|
|
};
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
|
|
|
UnboundResolver::Reset()
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
started = false;
|
|
|
|
if (unboundContext)
|
|
|
|
{
|
|
|
|
DeregisterPollFD();
|
|
|
|
ub_ctx_delete(unboundContext);
|
|
|
|
}
|
|
|
|
unboundContext = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
|
|
|
UnboundResolver::DeregisterPollFD()
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
eventLoop->deregister_poll_fd_readable(ub_fd(unboundContext));
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
|
|
|
UnboundResolver::RegisterPollFD()
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
2020-06-11 19:36:19 +00:00
|
|
|
eventLoop->register_poll_fd_readable(
|
|
|
|
ub_fd(unboundContext), [=]() { ub_process(unboundContext); });
|
2020-06-09 16:59:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:15:10 +00:00
|
|
|
UnboundResolver::UnboundResolver(
|
|
|
|
llarp_ev_loop_ptr eventLoop, ReplyFunction replyFunc, FailFunction failFunc)
|
|
|
|
: unboundContext(nullptr)
|
|
|
|
, started(false)
|
|
|
|
, eventLoop(eventLoop)
|
|
|
|
, replyFunc(replyFunc)
|
|
|
|
, failFunc(failFunc)
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// static callback
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
|
|
|
UnboundResolver::Callback(void* data, int err, ub_result* result)
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<PendingUnboundLookup> lookup{static_cast<PendingUnboundLookup*>(data)};
|
|
|
|
|
|
|
|
auto this_ptr = lookup->resolver.lock();
|
2020-06-11 19:36:19 +00:00
|
|
|
if (not this_ptr)
|
|
|
|
return; // resolver is gone, so we don't reply.
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
Message& msg = lookup->msg;
|
|
|
|
msg.AddServFail();
|
2020-06-15 18:15:10 +00:00
|
|
|
this_ptr->failFunc(lookup->source, msg);
|
2020-06-09 16:59:49 +00:00
|
|
|
ub_resolve_free(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = buf.cur = static_cast<byte_t*>(result->answer_packet);
|
|
|
|
buf.sz = result->answer_len;
|
|
|
|
|
|
|
|
MessageHeader hdr;
|
|
|
|
hdr.Decode(&buf);
|
|
|
|
hdr.id = lookup->msg.hdr_id;
|
|
|
|
|
2020-06-15 18:15:10 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
hdr.Encode(&buf);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-15 18:22:52 +00:00
|
|
|
std::vector<byte_t> buf_copy(buf.sz);
|
2020-06-15 18:15:10 +00:00
|
|
|
std::copy_n(buf.base, buf.sz, buf_copy.begin());
|
|
|
|
|
2020-06-15 18:22:52 +00:00
|
|
|
this_ptr->replyFunc(lookup->source, std::move(buf_copy));
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
ub_resolve_free(result);
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
bool
|
|
|
|
UnboundResolver::Init()
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
if (started)
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
unboundContext = ub_ctx_create();
|
|
|
|
|
|
|
|
if (not unboundContext)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegisterPollFD();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
bool
|
|
|
|
UnboundResolver::AddUpstreamResolver(const std::string& upstreamResolverIP)
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
if (ub_ctx_set_fwd(unboundContext, upstreamResolverIP.c_str()) != 0)
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
void
|
2020-06-15 18:15:10 +00:00
|
|
|
UnboundResolver::Lookup(const SockAddr& source, Message msg)
|
2020-06-09 16:59:49 +00:00
|
|
|
{
|
|
|
|
if (not unboundContext)
|
|
|
|
{
|
|
|
|
msg.AddServFail();
|
2020-06-15 18:15:10 +00:00
|
|
|
failFunc(source, std::move(msg));
|
2020-06-09 16:59:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
started = true;
|
|
|
|
|
|
|
|
const auto& q = msg.questions[0];
|
|
|
|
auto* lookup = new PendingUnboundLookup{weak_from_this(), msg, source};
|
2020-06-11 19:36:19 +00:00
|
|
|
int err = ub_resolve_async(
|
|
|
|
unboundContext,
|
|
|
|
q.Name().c_str(),
|
|
|
|
q.qtype,
|
|
|
|
q.qclass,
|
|
|
|
(void*)lookup,
|
|
|
|
&UnboundResolver::Callback,
|
|
|
|
nullptr);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
msg.AddServFail();
|
2020-06-15 18:15:10 +00:00
|
|
|
failFunc(source, std::move(msg));
|
2020-06-09 16:59:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
} // namespace llarp::dns
|