2018-10-01 05:10:34 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2018-10-01 22:27:46 +02:00
|
|
|
#include "s_dns.h"
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2018-10-01 20:52:59 +02:00
|
|
|
decltype(ircd::net::dns::resolver)
|
|
|
|
ircd::net::dns::resolver;
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
decltype(ircd::net::dns::resolver::servers)
|
|
|
|
ircd::net::dns::resolver::servers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.servers" },
|
2019-03-11 20:32:42 +01:00
|
|
|
{ "default", "4.2.2.1 4.2.2.2 4.2.2.3 4.2.2.4 4.2.2.5 4.2.2.6" },
|
2018-10-01 05:10:34 +02:00
|
|
|
}, []
|
|
|
|
{
|
2018-10-01 20:52:59 +02:00
|
|
|
if(bool(ircd::net::dns::resolver))
|
|
|
|
ircd::net::dns::resolver->set_servers();
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::timeout)
|
|
|
|
ircd::net::dns::resolver::timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.timeout" },
|
|
|
|
{ "default", 10000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::send_rate)
|
|
|
|
ircd::net::dns::resolver::send_rate
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.send_rate" },
|
|
|
|
{ "default", 60L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::send_burst)
|
|
|
|
ircd::net::dns::resolver::send_burst
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.send_burst" },
|
|
|
|
{ "default", 8L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::retry_max)
|
|
|
|
ircd::net::dns::resolver::retry_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.retry_max" },
|
|
|
|
{ "default", 4L },
|
|
|
|
};
|
|
|
|
|
2018-10-01 22:27:46 +02:00
|
|
|
//
|
|
|
|
// interface
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolver_init(answers_callback callback)
|
2018-10-01 22:27:46 +02:00
|
|
|
{
|
|
|
|
assert(!ircd::net::dns::resolver);
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolver = new typename ircd::net::dns::resolver
|
|
|
|
{
|
|
|
|
std::move(callback)
|
|
|
|
};
|
2018-10-01 22:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver_fini()
|
|
|
|
{
|
|
|
|
delete ircd::net::dns::resolver;
|
|
|
|
ircd::net::dns::resolver = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-22 18:41:26 +01:00
|
|
|
uint16_t
|
2018-10-03 07:38:08 +02:00
|
|
|
ircd::net::dns::resolver_call(const hostport &hp,
|
2019-03-22 02:24:36 +01:00
|
|
|
const opts &opts)
|
2018-10-03 07:38:08 +02:00
|
|
|
{
|
|
|
|
if(unlikely(!resolver))
|
|
|
|
throw error
|
|
|
|
{
|
2018-10-23 20:18:45 +02:00
|
|
|
"Cannot resolve '%s': resolver unavailable.",
|
|
|
|
host(hp)
|
2018-10-03 07:38:08 +02:00
|
|
|
};
|
|
|
|
|
2018-10-23 20:22:27 +02:00
|
|
|
auto &resolver{*dns::resolver};
|
|
|
|
if(unlikely(!resolver.ns.is_open()))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Cannot resolve '%s': resolver is closed.",
|
|
|
|
host(hp)
|
|
|
|
};
|
|
|
|
|
2019-03-22 18:41:26 +01:00
|
|
|
return resolver(hp, opts);
|
2018-10-03 07:38:08 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
//
|
|
|
|
// resolver::resolver
|
|
|
|
//
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolver::resolver(answers_callback callback)
|
|
|
|
:callback
|
|
|
|
{
|
|
|
|
std::move(callback)
|
|
|
|
}
|
|
|
|
,ns
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
ios::get()
|
|
|
|
}
|
|
|
|
,recv_context
|
|
|
|
{
|
2019-03-25 22:37:21 +01:00
|
|
|
"dnsres R", 1_MiB, std::bind(&resolver::recv_worker, this), context::POST
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
,timeout_context
|
|
|
|
{
|
|
|
|
"dnsres T", 64_KiB, std::bind(&resolver::timeout_worker, this), context::POST
|
|
|
|
}
|
|
|
|
,sendq_context
|
|
|
|
{
|
|
|
|
"dnsres S", 64_KiB, std::bind(&resolver::sendq_worker, this), context::POST
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ns.open(ip::udp::v4());
|
|
|
|
ns.non_blocking(true);
|
|
|
|
set_servers();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::dns::resolver::~resolver()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
if(ns.is_open())
|
|
|
|
ns.close();
|
|
|
|
|
2018-10-27 22:39:48 +02:00
|
|
|
timeout_context.terminate();
|
2019-03-22 02:24:36 +01:00
|
|
|
sendq_context.terminate();
|
|
|
|
recv_context.terminate();
|
|
|
|
done.wait([this]
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
const bool ret(tags.empty());
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Waiting for %zu unfinished DNS resolutions", tags.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(tags.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal resolver entry interface.
|
|
|
|
uint16_t
|
|
|
|
ircd::net::dns::resolver::operator()(const hostport &hp,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
auto &tag(set_tag(hp, opts)); try
|
|
|
|
{
|
|
|
|
tag.question = make_query(tag.qbuf, tag);
|
|
|
|
submit(tag);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
remove(tag);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::net::dns::resolver::make_query(const mutable_buffer &buf,
|
|
|
|
tag &tag)
|
|
|
|
{
|
|
|
|
thread_local char hostbuf[rfc1035::NAME_BUF_SIZE * 2];
|
|
|
|
string_view hoststr;
|
|
|
|
switch(tag.opts.qtype)
|
|
|
|
{
|
|
|
|
case 0: throw error
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
"A query type is required to form a question."
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
case 33: // SRV
|
|
|
|
hoststr = make_SRV_key(hostbuf, host(tag.hp), tag.opts);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
hoststr = host(tag.hp);
|
|
|
|
break;
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
assert(hoststr);
|
|
|
|
assert(tag.opts.qtype);
|
|
|
|
const rfc1035::question question
|
|
|
|
{
|
|
|
|
hoststr, tag.opts.qtype
|
|
|
|
};
|
|
|
|
|
|
|
|
return rfc1035::make_query(buf, tag.id, question);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... A>
|
|
|
|
ircd::net::dns::tag &
|
|
|
|
ircd::net::dns::resolver::set_tag(A&&... args)
|
|
|
|
{
|
|
|
|
while(tags.size() < 65535)
|
|
|
|
{
|
|
|
|
auto id
|
|
|
|
{
|
|
|
|
ircd::rand::integer(1, 65535)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it{tags.lower_bound(id)};
|
|
|
|
if(it != end(tags) && it->first == id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
it = tags.emplace_hint(it,
|
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(id),
|
|
|
|
std::forward_as_tuple(std::forward<A>(args)...));
|
|
|
|
it->second.id = id;
|
|
|
|
dock.notify_one();
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw panic
|
|
|
|
{
|
|
|
|
"Too many DNS queries"
|
|
|
|
};
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
__attribute__((noreturn))
|
2018-10-01 05:10:34 +02:00
|
|
|
ircd::net::dns::resolver::sendq_worker()
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
dock.wait([this]
|
|
|
|
{
|
2018-10-23 21:01:54 +02:00
|
|
|
assert(sendq.empty() || !tags.empty());
|
2019-03-11 19:52:42 +01:00
|
|
|
return !sendq.empty() && !server.empty();
|
2018-10-01 05:10:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(tags.size() > size_t(send_burst))
|
|
|
|
ctx::sleep(milliseconds(send_rate));
|
|
|
|
|
2018-10-23 21:01:54 +02:00
|
|
|
sendq_work();
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:01:54 +02:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::sendq_work()
|
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
mutex
|
|
|
|
};
|
|
|
|
|
2018-10-23 21:01:54 +02:00
|
|
|
assert(!sendq.empty());
|
|
|
|
assert(sendq.size() < 65535);
|
|
|
|
assert(sendq.size() <= tags.size());
|
|
|
|
const uint16_t next(sendq.front());
|
|
|
|
sendq.pop_front();
|
|
|
|
flush(next);
|
|
|
|
}
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::flush(const uint16_t &next)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &tag
|
|
|
|
{
|
|
|
|
tags.at(next)
|
|
|
|
};
|
|
|
|
|
2019-03-11 19:51:02 +01:00
|
|
|
submit(tag);
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2018-10-23 21:01:54 +02:00
|
|
|
log, "Queued tag id[%u] is no longer mapped", next
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::timeout_worker()
|
2018-10-23 21:01:54 +02:00
|
|
|
try
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
dock.wait([this]
|
|
|
|
{
|
|
|
|
return !tags.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx::sleep(milliseconds(timeout));
|
|
|
|
check_timeouts(milliseconds(timeout));
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 22:39:48 +02:00
|
|
|
catch(const ctx::terminated &)
|
2018-10-23 21:01:54 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
const ctx::exception_handler eh;
|
|
|
|
cancel_all();
|
2018-10-23 21:01:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolver::check_timeouts(const milliseconds &timeout)
|
2018-10-23 21:01:54 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
const std::lock_guard lock
|
2018-10-23 21:01:54 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
mutex
|
2018-10-23 21:01:54 +02:00
|
|
|
};
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
const auto cutoff
|
|
|
|
{
|
|
|
|
now<steady_point>() - timeout
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(begin(tags));
|
|
|
|
while(it != end(tags))
|
|
|
|
{
|
|
|
|
const auto &id(it->first);
|
|
|
|
auto &tag(it->second);
|
|
|
|
if(check_timeout(id, tag, cutoff))
|
2019-03-12 01:39:45 +01:00
|
|
|
it = remove(tag, it);
|
2018-10-01 05:10:34 +02:00
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::resolver::check_timeout(const uint16_t &id,
|
|
|
|
tag &tag,
|
|
|
|
const steady_point &cutoff)
|
|
|
|
{
|
2018-10-25 22:39:02 +02:00
|
|
|
if(tag.last == steady_point::min())
|
2018-10-01 05:10:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if(tag.last > cutoff)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
log::warning
|
|
|
|
{
|
2018-10-14 13:12:28 +02:00
|
|
|
log, "DNS timeout id:%u on attempt %u of %u '%s'",
|
|
|
|
id,
|
|
|
|
tag.tries,
|
|
|
|
size_t(retry_max),
|
|
|
|
host(tag.hp)
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2018-10-25 22:39:02 +02:00
|
|
|
tag.last = steady_point::min();
|
2018-10-23 21:01:54 +02:00
|
|
|
if(tag.tries < size_t(retry_max))
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
|
|
|
submit(tag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:01:54 +02:00
|
|
|
static const std::system_error ec
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::timed_out)
|
|
|
|
};
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
error_one(tag, ec);
|
2018-10-23 21:01:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
//
|
|
|
|
// submit
|
|
|
|
//
|
2018-10-01 05:10:34 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::submit(tag &tag)
|
|
|
|
{
|
2019-03-11 19:51:02 +01:00
|
|
|
if(!ns.is_open() || server.empty())
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2019-03-12 02:08:46 +01:00
|
|
|
log, "dns tag:%u submit queued because no nameserver is available.",
|
2019-03-11 19:51:02 +01:00
|
|
|
tag.id
|
|
|
|
};
|
|
|
|
|
|
|
|
queue_query(tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!server.empty());
|
2018-10-01 05:10:34 +02:00
|
|
|
const auto rate(milliseconds(send_rate) / server.size());
|
|
|
|
const auto elapsed(now<steady_point>() - send_last);
|
2019-03-12 02:18:58 +01:00
|
|
|
if(elapsed >= rate || tags.size() <= size_t(send_burst))
|
2018-10-01 05:10:34 +02:00
|
|
|
send_query(tag);
|
|
|
|
else
|
|
|
|
queue_query(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::send_query(tag &tag)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(!server.empty());
|
|
|
|
++server_next %= server.size();
|
|
|
|
const auto &ep
|
|
|
|
{
|
|
|
|
server.at(server_next)
|
|
|
|
};
|
|
|
|
|
|
|
|
send_query(ep, tag);
|
2019-01-09 01:48:09 +01:00
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
thread_local char buf[128];
|
|
|
|
log::debug
|
|
|
|
{
|
2019-03-12 02:08:46 +01:00
|
|
|
log, "dns %s send tag:%u t:%u qtype:%u `%s'",
|
2019-01-09 01:48:09 +01:00
|
|
|
string(buf, make_ipport(ep)),
|
|
|
|
tag.id,
|
|
|
|
tag.tries,
|
|
|
|
tag.opts.qtype,
|
2019-03-12 02:08:46 +01:00
|
|
|
host(tag.hp)
|
2019-01-09 01:48:09 +01:00
|
|
|
};
|
|
|
|
#endif
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &)
|
|
|
|
{
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"No DNS servers available for query"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::queue_query(tag &tag)
|
|
|
|
{
|
|
|
|
assert(sendq.size() <= tags.size());
|
2019-03-28 01:11:12 +01:00
|
|
|
if(std::find(begin(sendq), end(sendq), tag.id) != end(sendq))
|
|
|
|
return;
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
sendq.emplace_back(tag.id);
|
|
|
|
dock.notify_one();
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "dns tag:%u t:%u qtype:%u added to sendq (tags:%zu sendq:%zu)",
|
|
|
|
tag.id,
|
|
|
|
tag.tries,
|
|
|
|
tag.opts.qtype,
|
|
|
|
tags.size(),
|
|
|
|
sendq.size()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::send_query(const ip::udp::endpoint &ep,
|
|
|
|
tag &tag)
|
|
|
|
{
|
2019-03-11 19:51:02 +01:00
|
|
|
assert(ns.is_open());
|
2018-10-01 05:10:34 +02:00
|
|
|
assert(ns.non_blocking());
|
|
|
|
assert(!empty(tag.question));
|
|
|
|
const const_buffer &buf{tag.question};
|
2019-01-09 01:48:09 +01:00
|
|
|
const auto sent
|
|
|
|
{
|
|
|
|
ns.send_to(asio::const_buffers_1(buf), ep)
|
|
|
|
};
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
send_last = now<steady_point>();
|
|
|
|
tag.last = send_last;
|
2019-01-09 01:48:09 +01:00
|
|
|
tag.server = make_ipport(ep);
|
2018-10-01 05:10:34 +02:00
|
|
|
tag.tries++;
|
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
//
|
|
|
|
// recv
|
|
|
|
//
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolver::recv_worker()
|
|
|
|
try
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
64_KiB
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
const auto interruption{[this]
|
|
|
|
(ctx::ctx *const &)
|
|
|
|
{
|
|
|
|
if(this->ns.is_open())
|
|
|
|
this->ns.cancel();
|
|
|
|
}};
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
const asio::mutable_buffers_1 bufs{buf};
|
|
|
|
ip::udp::endpoint ep;
|
|
|
|
while(ns.is_open()) try
|
|
|
|
{
|
|
|
|
size_t recv; continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this, &bufs, &recv, &ep]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
recv = ns.async_receive_from(bufs, ep, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer &reply
|
|
|
|
{
|
|
|
|
data(buf), recv
|
|
|
|
};
|
|
|
|
|
|
|
|
const net::ipport &from
|
|
|
|
{
|
|
|
|
make_ipport(ep)
|
|
|
|
};
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
handle(from, reply);
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
switch(make_error_code(e).value())
|
|
|
|
{
|
|
|
|
case int(std::errc::operation_canceled):
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-03-22 22:08:55 +01:00
|
|
|
log::critical
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
|
|
|
log, "%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::handle(const ipport &from,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(unlikely(size(buf) < sizeof(rfc1035::header)))
|
2018-10-01 05:10:34 +02:00
|
|
|
throw rfc1035::error
|
|
|
|
{
|
|
|
|
"Got back %zu bytes < rfc1035 %zu byte header",
|
2019-03-22 02:24:36 +01:00
|
|
|
size(buf),
|
2018-10-01 05:10:34 +02:00
|
|
|
sizeof(rfc1035::header)
|
|
|
|
};
|
|
|
|
|
|
|
|
rfc1035::header &header
|
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
*reinterpret_cast<rfc1035::header *>(data(buf))
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2019-04-07 02:01:06 +02:00
|
|
|
ntoh(&header.qdcount);
|
|
|
|
ntoh(&header.ancount);
|
|
|
|
ntoh(&header.nscount);
|
|
|
|
ntoh(&header.arcount);
|
2018-10-01 05:10:34 +02:00
|
|
|
|
|
|
|
const const_buffer body
|
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
data(buf) + sizeof(header), size(buf) - sizeof(header)
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
handle_reply(from, header, body);
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
log::error
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
log, "%s", e.what()
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolver::handle_reply(const ipport &from,
|
|
|
|
const header &header,
|
2018-10-01 05:10:34 +02:00
|
|
|
const const_buffer &body)
|
|
|
|
{
|
2019-01-09 01:48:09 +01:00
|
|
|
thread_local char addr_strbuf[2][128];
|
2019-03-22 02:24:36 +01:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
// The primary mutex is locked here while this result is
|
|
|
|
// processed. This locks out the sendq and timeout worker.
|
|
|
|
mutex
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
tags.find(header.id)
|
|
|
|
};
|
2019-01-09 01:48:09 +01:00
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
if(it == end(tags))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"DNS reply from %s for unrecognized tag id:%u",
|
2019-03-22 02:24:36 +01:00
|
|
|
string(addr_strbuf[0], from),
|
2019-01-09 01:48:09 +01:00
|
|
|
header.id
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto &tag{it->second};
|
2019-03-22 02:24:36 +01:00
|
|
|
if(from != tag.server)
|
2019-01-09 01:48:09 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"DNS reply from %s for tag:%u which we sent to %s",
|
2019-03-22 02:24:36 +01:00
|
|
|
string(addr_strbuf[0], from),
|
2019-01-09 01:48:09 +01:00
|
|
|
header.id,
|
|
|
|
string(addr_strbuf[1], tag.server)
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2019-03-12 02:08:46 +01:00
|
|
|
log, "dns %s recv tag:%u t:%u qtype:%u qd:%u an:%u ns:%u ar:%u",
|
2019-03-22 02:24:36 +01:00
|
|
|
string(addr_strbuf[0], from),
|
2019-01-09 01:48:09 +01:00
|
|
|
tag.id,
|
|
|
|
tag.tries,
|
|
|
|
tag.opts.qtype,
|
|
|
|
header.qdcount,
|
|
|
|
header.ancount,
|
|
|
|
header.nscount,
|
|
|
|
header.arcount,
|
|
|
|
};
|
|
|
|
|
2019-03-22 22:08:55 +01:00
|
|
|
// Handle ServFail as a special case here. We can try again without
|
|
|
|
// handling this tag or propagating this error any further yet.
|
|
|
|
if(header.rcode == 2 && tag.tries < size_t(retry_max))
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "dns %s recv tag:%u t:%u qtype:%u protocol error #%u :%s",
|
|
|
|
string(addr_strbuf[0], from),
|
|
|
|
tag.id,
|
|
|
|
tag.tries,
|
|
|
|
tag.opts.qtype,
|
|
|
|
header.rcode,
|
|
|
|
rfc1035::rcode.at(header.rcode)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(tag.tries > 0);
|
|
|
|
tag.last = steady_point::min();
|
|
|
|
submit(tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The tag is committed to being handled after this point. It will be
|
|
|
|
// removed from the tags map and any retry must c
|
|
|
|
const unwind untag{[this, &tag, &it]
|
|
|
|
{
|
|
|
|
remove(tag, it);
|
|
|
|
}};
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
assert(tag.tries > 0);
|
2018-10-25 22:39:02 +02:00
|
|
|
tag.last = steady_point::min();
|
2019-03-22 02:24:36 +01:00
|
|
|
tag.rcode = header.rcode;
|
2018-10-01 05:10:34 +02:00
|
|
|
handle_reply(header, body, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::handle_reply(const header &header,
|
|
|
|
const const_buffer &body,
|
|
|
|
tag &tag)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(unlikely(header.qr != 1))
|
|
|
|
throw rfc1035::error
|
|
|
|
{
|
|
|
|
"Response header is marked as 'Query' and not 'Response'"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(header.qdcount > MAX_COUNT || header.ancount > MAX_COUNT)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Response contains too many sections..."
|
|
|
|
};
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
if(header.qdcount < 1)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Response does not contain the question."
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!handle_error(header, tag))
|
|
|
|
throw rfc1035::error
|
|
|
|
{
|
|
|
|
"protocol error #%u :%s",
|
|
|
|
header.rcode,
|
|
|
|
rfc1035::rcode.at(header.rcode)
|
|
|
|
};
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
const_buffer buffer
|
|
|
|
{
|
|
|
|
body
|
|
|
|
};
|
|
|
|
|
|
|
|
// Questions are regurgitated back to us so they must be parsed first
|
|
|
|
thread_local std::array<rfc1035::question, MAX_COUNT> qd;
|
|
|
|
for(size_t i(0); i < header.qdcount; ++i)
|
|
|
|
consume(buffer, size(qd.at(i).parse(buffer)));
|
|
|
|
|
|
|
|
// Answers are parsed into this buffer
|
|
|
|
thread_local std::array<rfc1035::answer, MAX_COUNT> an;
|
|
|
|
for(size_t i(0); i < header.ancount; ++i)
|
2018-12-30 01:01:19 +01:00
|
|
|
consume(buffer, size(an.at(i).parse(buffer)));
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
const vector_view<const rfc1035::answer> answers
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
an.data(), header.ancount
|
|
|
|
};
|
2018-12-30 01:01:19 +01:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
callback({}, tag, answers);
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
// There's no need to flash red to the log for NXDOMAIN which is
|
|
|
|
// common in this system when probing SRV.
|
2019-03-22 20:47:14 +01:00
|
|
|
const auto level
|
|
|
|
{
|
|
|
|
header.rcode != 3? log::ERROR : log::DERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, level, "resolver tag:%u: %s",
|
|
|
|
tag.id,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 20:47:14 +01:00
|
|
|
const auto eptr(std::current_exception());
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
callback(eptr, tag, answers{});
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::resolver::handle_error(const header &header,
|
|
|
|
tag &tag)
|
|
|
|
{
|
|
|
|
switch(header.rcode)
|
|
|
|
{
|
|
|
|
case 0: // NoError; continue
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 3: // NXDomain; exception
|
2019-03-22 02:24:36 +01:00
|
|
|
if(!tag.opts.nxdomain_exceptions)
|
|
|
|
return true;
|
2018-10-01 05:10:34 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default: // Unhandled error; exception
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
//
|
|
|
|
// removal
|
|
|
|
//
|
|
|
|
// This whole stack must be called under lock
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::cancel_all()
|
|
|
|
{
|
|
|
|
static const std::error_code &ec
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::operation_canceled)
|
|
|
|
};
|
|
|
|
|
|
|
|
error_all(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::error_all(const std::error_code &ec)
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
if(tags.empty())
|
|
|
|
return;
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
log::dwarning
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
log, "Attempting to cancel all %zu pending tags.", tags.size()
|
|
|
|
};
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
const auto eptr
|
|
|
|
{
|
|
|
|
make_system_eptr(ec)
|
|
|
|
};
|
2018-11-09 06:18:39 +01:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
for(auto &p : tags)
|
|
|
|
error_one(p.second, eptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::error_one(tag &tag,
|
|
|
|
const std::system_error &se)
|
|
|
|
{
|
|
|
|
error_one(tag, std::make_exception_ptr(se));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::error_one(tag &tag,
|
|
|
|
const std::exception_ptr &eptr)
|
|
|
|
{
|
|
|
|
thread_local char hpbuf[128];
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "DNS error id:%u for '%s' :%s",
|
|
|
|
tag.id,
|
|
|
|
string(hpbuf, tag.hp),
|
|
|
|
what(eptr)
|
|
|
|
};
|
|
|
|
|
|
|
|
static const answers empty;
|
|
|
|
callback(eptr, tag, empty);
|
|
|
|
remove(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::remove(tag &tag)
|
|
|
|
{
|
|
|
|
remove(tag, tags.find(tag.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::tags)::iterator
|
|
|
|
ircd::net::dns::resolver::remove(tag &tag,
|
|
|
|
const decltype(tags)::iterator &it)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "dns tag:%u t:%u qtype:%u removing (tags:%zu sendq:%zu)",
|
|
|
|
tag.id,
|
|
|
|
tag.tries,
|
|
|
|
tag.opts.qtype,
|
|
|
|
tags.size(),
|
|
|
|
sendq.size()
|
|
|
|
};
|
2018-11-09 06:18:39 +01:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
unqueue(tag);
|
|
|
|
done.notify_all();
|
|
|
|
return it != end(tags)? tags.erase(it) : it;
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::unqueue(tag &tag)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find(begin(sendq), end(sendq), tag.id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it != end(sendq))
|
|
|
|
sendq.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::set_servers()
|
2019-03-11 19:52:42 +01:00
|
|
|
try
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
|
|
|
const std::string &list(resolver::servers);
|
|
|
|
set_servers(list);
|
2019-03-11 19:52:42 +01:00
|
|
|
dock.notify_all();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-03-12 02:08:46 +01:00
|
|
|
log, "Erroneous configuration; falling back to defaults :%s",
|
2019-03-11 19:52:42 +01:00
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
resolver::servers.fault();
|
|
|
|
if(!ircd::net::dns::resolver)
|
|
|
|
set_servers();
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::set_servers(const string_view &list)
|
|
|
|
{
|
|
|
|
server.clear();
|
|
|
|
server_next = 0;
|
2019-03-11 20:32:42 +01:00
|
|
|
tokens(list, ' ', [this]
|
2019-03-11 19:52:42 +01:00
|
|
|
(const string_view &hp)
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-11 19:52:42 +01:00
|
|
|
add_server(hp);
|
|
|
|
});
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-11 19:52:42 +01:00
|
|
|
if(!empty(list) && server.empty())
|
|
|
|
throw error
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-03-11 19:52:42 +01:00
|
|
|
"Failed to set any valid DNS servers from a non-empty list."
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
2019-03-11 19:52:42 +01:00
|
|
|
}
|
2018-10-01 05:10:34 +02:00
|
|
|
|
2019-03-11 19:52:42 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::add_server(const string_view &str)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const hostport hp
|
|
|
|
{
|
|
|
|
str
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &port
|
|
|
|
{
|
|
|
|
net::port(hp) != canon_port? net::port(hp) : uint16_t(53)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ipport ipp
|
|
|
|
{
|
|
|
|
host(hp), port
|
|
|
|
};
|
|
|
|
|
|
|
|
add_server(ipp);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-03-12 02:08:46 +01:00
|
|
|
log, "Failed to add server '%s' :%s",
|
2019-03-11 19:52:42 +01:00
|
|
|
str,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::add_server(const ipport &ipp)
|
|
|
|
{
|
|
|
|
server.emplace_back(make_endpoint_udp(ipp));
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Adding [%s] as DNS server #%zu",
|
|
|
|
string(ipp),
|
|
|
|
server.size()
|
|
|
|
};
|
|
|
|
}
|