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.
|
|
|
|
|
2019-06-24 10:17:06 +02:00
|
|
|
decltype(ircd::net::dns::resolver_instance)
|
|
|
|
ircd::net::dns::resolver_instance;
|
2018-10-01 20:52:59 +02:00
|
|
|
|
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" },
|
2020-04-30 23:22:49 +02:00
|
|
|
{ "default", 5000L },
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::send_rate)
|
|
|
|
ircd::net::dns::resolver::send_rate
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.send_rate" },
|
2020-11-07 06:47:50 +01:00
|
|
|
{ "default", 200L },
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::send_burst)
|
|
|
|
ircd::net::dns::resolver::send_burst
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.send_burst" },
|
2020-11-07 06:47:50 +01:00
|
|
|
{ "default", 4L },
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::net::dns::resolver::retry_max)
|
|
|
|
ircd::net::dns::resolver::retry_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.retry_max" },
|
2020-08-02 15:45:46 +02:00
|
|
|
{ "default", 20L },
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
2019-10-03 19:47:33 +02:00
|
|
|
decltype(ircd::net::dns::resolver::servers)
|
|
|
|
ircd::net::dns::resolver::servers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.dns.resolver.servers" },
|
|
|
|
{ "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" },
|
|
|
|
}, []
|
|
|
|
{
|
|
|
|
if(bool(ircd::net::dns::resolver_instance))
|
|
|
|
ircd::net::dns::resolver_instance->set_servers();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-01 22:27:46 +02:00
|
|
|
//
|
|
|
|
// interface
|
|
|
|
//
|
|
|
|
|
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
|
|
|
{
|
2019-06-24 10:17:06 +02:00
|
|
|
if(unlikely(!resolver_instance))
|
2018-10-03 07:38:08 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
auto &resolver
|
|
|
|
{
|
|
|
|
*dns::resolver_instance
|
|
|
|
};
|
|
|
|
|
2018-10-23 20:22:27 +02:00
|
|
|
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
|
|
|
|
{
|
2020-02-28 19:30:14 +01:00
|
|
|
"net.dns.R",
|
|
|
|
768_KiB,
|
|
|
|
std::bind(&resolver::recv_worker, this),
|
|
|
|
context::POST
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
,timeout_context
|
|
|
|
{
|
2020-02-28 19:30:14 +01:00
|
|
|
"net.dns.T",
|
|
|
|
512_KiB,
|
|
|
|
std::bind(&resolver::timeout_worker, this),
|
|
|
|
context::POST
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
,sendq_context
|
|
|
|
{
|
2020-02-28 19:30:14 +01:00
|
|
|
"net.dns.S",
|
|
|
|
256_KiB,
|
|
|
|
std::bind(&resolver::sendq_worker, this),
|
|
|
|
context::POST
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
ns.open(ip::udp::v4());
|
|
|
|
ns.non_blocking(true);
|
|
|
|
set_servers();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::dns::resolver::~resolver()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-08-26 01:58:52 +02:00
|
|
|
const ctx::uninterruptible::nothrow ui;
|
2022-06-24 04:18:05 +02:00
|
|
|
done.wait([this]() noexcept
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-08-26 01:58:52 +02:00
|
|
|
if(!tags.empty())
|
2019-03-22 02:24:36 +01:00
|
|
|
log::warning
|
|
|
|
{
|
2019-09-19 21:57:52 +02:00
|
|
|
log, "Waiting for %zu unfinished DNS resolutions...",
|
2019-04-24 00:00:13 +02:00
|
|
|
tags.size()
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
|
|
|
|
2019-08-26 01:58:52 +02:00
|
|
|
return tags.empty();
|
2019-03-22 02:24:36 +01:00
|
|
|
});
|
|
|
|
|
2019-09-20 21:27:46 +02:00
|
|
|
ns.close();
|
2019-09-19 21:57:52 +02:00
|
|
|
assert(!mutex.locked());
|
2019-07-24 23:06:49 +02:00
|
|
|
assert(sendq.empty());
|
2019-03-22 02:24:36 +01:00
|
|
|
assert(tags.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal resolver entry interface.
|
|
|
|
uint16_t
|
|
|
|
ircd::net::dns::resolver::operator()(const hostport &hp,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
2019-09-09 01:57:12 +02:00
|
|
|
const ctx::critical_assertion ca;
|
2019-03-22 02:24:36 +01:00
|
|
|
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)
|
|
|
|
{
|
2019-05-27 01:53:08 +02:00
|
|
|
thread_local char hostbuf[rfc1035::NAME_BUFSIZE * 2];
|
2019-03-22 02:24:36 +01:00
|
|
|
string_view hoststr;
|
|
|
|
switch(tag.opts.qtype)
|
|
|
|
{
|
2023-02-02 06:48:28 +01:00
|
|
|
[[unlikely]]
|
2019-03-22 02:24:36 +01:00
|
|
|
case 0: throw error
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2020-03-05 22:58:21 +01:00
|
|
|
"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
|
2020-03-05 20:07:39 +01:00
|
|
|
hoststr = make_SRV_key(hostbuf, tag.hp, tag.opts);
|
2019-03-22 02:24:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-08-03 07:03:30 +02:00
|
|
|
hoststr = tolower(hostbuf, host(tag.hp));
|
2019-03-22 02:24:36 +01:00
|
|
|
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;
|
|
|
|
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)
|
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
dock.wait([this]() noexcept
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
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
|
|
|
|
};
|
|
|
|
|
2019-08-09 03:48:36 +02:00
|
|
|
if(unlikely(sendq.empty()))
|
|
|
|
return;
|
|
|
|
|
2018-10-23 21:01:54 +02:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
// Dock here until somebody submits a request into the tag map. Also
|
|
|
|
// wait until recv_idle is asserted which indicates the UDP queue has
|
|
|
|
// been exhausted.
|
2022-06-24 04:18:05 +02:00
|
|
|
dock.wait([this]() noexcept
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
return !tags.empty() && recv_idle;
|
2018-10-01 05:10:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
check_timeouts(milliseconds(timeout));
|
|
|
|
}
|
|
|
|
}
|
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-09-12 20:03:54 +02:00
|
|
|
const auto cutoff
|
2018-10-23 21:01:54 +02:00
|
|
|
{
|
2019-09-12 20:03:54 +02:00
|
|
|
now<steady_point>() - timeout
|
2018-10-23 21:01:54 +02:00
|
|
|
};
|
|
|
|
|
2019-09-12 20:03:54 +02:00
|
|
|
std::unique_lock lock
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
2019-09-12 20:03:54 +02:00
|
|
|
mutex
|
2018-10-01 05:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it(begin(tags));
|
2019-09-12 20:03:54 +02:00
|
|
|
for(; it != end(tags); ++it)
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
|
|
|
auto &tag(it->second);
|
2019-09-12 20:03:54 +02:00
|
|
|
const auto &id(it->first);
|
2018-10-01 05:10:34 +02:00
|
|
|
if(check_timeout(id, tag, cutoff))
|
2019-09-12 20:03:54 +02:00
|
|
|
{
|
|
|
|
it = tags.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
2019-09-12 20:03:54 +02:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
ctx::sleep(1800ms);
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-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-04-17 00:01:21 +02:00
|
|
|
error_one(tag, ec, false);
|
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);
|
2019-04-17 13:26:08 +02:00
|
|
|
|
|
|
|
dock.notify_all();
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-12-21 15:43:15 +01:00
|
|
|
char buf[128];
|
2022-08-13 00:53:21 +02:00
|
|
|
log::logf
|
2019-01-09 01:48:09 +01:00
|
|
|
{
|
2022-08-13 00:53:21 +02:00
|
|
|
log, log::level::DEBUG,
|
|
|
|
"send tag:%u qtype:%u t:%u `%s' to %s",
|
2019-01-09 01:48:09 +01:00
|
|
|
tag.id,
|
|
|
|
tag.opts.qtype,
|
2019-07-24 23:06:49 +02:00
|
|
|
tag.tries,
|
|
|
|
host(tag.hp),
|
|
|
|
string(buf, make_ipport(ep)),
|
2019-01-09 01:48:09 +01:00
|
|
|
};
|
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-07-24 23:14:27 +02:00
|
|
|
tag.last = steady_point::min(); // tag to be ignored by the timeout worker
|
2019-03-22 02:24:36 +01:00
|
|
|
sendq.emplace_back(tag.id);
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2019-07-24 23:06:49 +02:00
|
|
|
log, "queu tag:%u qtype:%u t:%u (tags:%zu sendq:%zu)",
|
2019-03-22 02:24:36 +01:00
|
|
|
tag.id,
|
|
|
|
tag.opts.qtype,
|
2019-07-24 23:06:49 +02:00
|
|
|
tag.tries,
|
2019-03-22 02:24:36 +01:00
|
|
|
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-08-05 01:59:53 +02:00
|
|
|
try
|
2018-10-01 05:10:34 +02:00
|
|
|
{
|
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));
|
2019-12-04 19:03:36 +01:00
|
|
|
const ctx::uninterruptible::nothrow ui;
|
2018-10-01 05:10:34 +02:00
|
|
|
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-08-05 01:59:53 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-12-21 15:43:15 +01:00
|
|
|
char buf[128];
|
2019-08-05 01:59:53 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "send tag:%u qtype:%u t:%u `%s' to %s :%s",
|
|
|
|
tag.id,
|
|
|
|
tag.opts.qtype,
|
|
|
|
tag.tries,
|
|
|
|
host(tag.hp),
|
|
|
|
string(buf, make_ipport(ep)),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-10-01 05:10:34 +02:00
|
|
|
|
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
|
|
|
while(ns.is_open()) try
|
|
|
|
{
|
2020-11-07 06:03:53 +01:00
|
|
|
const auto &[from, reply]
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2020-11-07 06:03:53 +01:00
|
|
|
recv_recv(buf)
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
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
|
|
|
|
2020-11-07 06:03:53 +01:00
|
|
|
std::tuple<ircd::net::ipport, ircd::mutable_buffer>
|
|
|
|
ircd::net::dns::resolver::recv_recv(const mutable_buffer &buf)
|
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
static const ip::udp::socket::message_flags flags
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2020-11-07 06:03:53 +01:00
|
|
|
const asio::mutable_buffers_1 bufs
|
|
|
|
{
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
2020-11-07 06:33:30 +01:00
|
|
|
// First try a non-blocking receive to find and return anything in the
|
|
|
|
// queue. If this comes back as -EAGAIN we'll assert recv_idle and then
|
|
|
|
// conduct the normal blocking receive.
|
|
|
|
boost::system::error_code ec;
|
|
|
|
ip::udp::endpoint ep;
|
|
|
|
size_t recv
|
2020-11-07 06:03:53 +01:00
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
ns.receive_from(bufs, ep, flags, ec)
|
|
|
|
};
|
2020-11-07 06:03:53 +01:00
|
|
|
|
2020-11-07 06:33:30 +01:00
|
|
|
assert(!ec || recv == 0);
|
|
|
|
assert(!ec || ec == boost::system::errc::resource_unavailable_try_again);
|
|
|
|
|
|
|
|
// branch on any ec, not just -EAGAIN; this time it can throw...
|
|
|
|
if(likely(ec))
|
2020-11-07 06:03:53 +01:00
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
const scope_restore recv_idle
|
2020-11-07 06:03:53 +01:00
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
this->recv_idle, true
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto interruption
|
|
|
|
{
|
|
|
|
std::bind(&resolver::handle_interrupt, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
|
|
|
continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [this, &bufs, &recv, &ep]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
recv = ns.async_receive_from(bufs, ep, yield);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-11-07 06:03:53 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
{
|
2020-11-07 06:33:30 +01:00
|
|
|
make_ipport(ep), mutable_buffer
|
2020-11-07 06:03:53 +01:00
|
|
|
{
|
|
|
|
data(buf), recv
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-07 06:33:30 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::handle_interrupt(ctx::ctx *const &interruptor)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(!ns.is_open())
|
|
|
|
ns.cancel();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-04-09 23:58:48 +02:00
|
|
|
header.qdcount = ntoh(header.qdcount);
|
|
|
|
header.ancount = ntoh(header.ancount);
|
|
|
|
header.nscount = ntoh(header.nscount);
|
|
|
|
header.arcount = 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)
|
|
|
|
{
|
2020-12-21 15:43:15 +01:00
|
|
|
// The primary mutex is locked here while this result is
|
|
|
|
// processed. This locks out the sendq and timeout worker.
|
2019-03-22 02:24:36 +01:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
mutex
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
tags.find(header.id)
|
|
|
|
};
|
2019-01-09 01:48:09 +01:00
|
|
|
|
2020-12-21 15:43:15 +01:00
|
|
|
char strbuf[2][128];
|
2018-10-01 05:10:34 +02:00
|
|
|
if(it == end(tags))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"DNS reply from %s for unrecognized tag id:%u",
|
2019-07-24 23:06:49 +02:00
|
|
|
string(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-07-24 23:06:49 +02:00
|
|
|
string(strbuf[0], from),
|
2019-01-09 01:48:09 +01:00
|
|
|
header.id,
|
2019-07-24 23:06:49 +02:00
|
|
|
string(strbuf[1], tag.server)
|
2019-01-09 01:48:09 +01:00
|
|
|
};
|
|
|
|
|
2022-08-13 00:53:21 +02:00
|
|
|
log::logf
|
2019-01-09 01:48:09 +01:00
|
|
|
{
|
2022-08-13 00:53:21 +02:00
|
|
|
log, log::level::DEBUG,
|
|
|
|
"recv tag:%u qtype:%u t:%u from %s in %s qd:%u an:%u ns:%u ar:%u",
|
2019-01-09 01:48:09 +01:00
|
|
|
tag.id,
|
|
|
|
tag.opts.qtype,
|
2019-07-24 23:06:49 +02:00
|
|
|
tag.tries,
|
|
|
|
string(strbuf[0], from),
|
|
|
|
pretty(strbuf[1], nanoseconds(now<steady_point>() - tag.last), 1),
|
2019-01-09 01:48:09 +01:00
|
|
|
header.qdcount,
|
|
|
|
header.ancount,
|
|
|
|
header.nscount,
|
|
|
|
header.arcount,
|
|
|
|
};
|
|
|
|
|
2022-08-15 03:55:42 +02:00
|
|
|
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..."
|
|
|
|
};
|
|
|
|
|
|
|
|
if(header.qdcount < 1)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Response does not contain the question."
|
|
|
|
};
|
|
|
|
|
|
|
|
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)));
|
|
|
|
|
|
|
|
// Question must match or this is the mist of a packet spray with
|
|
|
|
// matching tag and source ip.
|
|
|
|
if(unlikely(!has(qd.at(0).name, host(tag.hp))))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Response contains the wrong question."
|
|
|
|
};
|
|
|
|
|
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.
|
2020-12-05 10:07:02 +01:00
|
|
|
if(header.rcode == 2 && tag.tries < size_t(server.size()))
|
2019-03-22 22:08:55 +01:00
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-07-24 23:06:49 +02:00
|
|
|
log, "recv tag:%u qtype:%u t:%u from %s protocol error #%u :%s",
|
2019-03-22 22:08:55 +01:00
|
|
|
tag.id,
|
|
|
|
tag.opts.qtype,
|
2019-07-24 23:06:49 +02:00
|
|
|
tag.tries,
|
|
|
|
string(strbuf[0], from),
|
2019-03-22 22:08:55 +01:00
|
|
|
header.rcode,
|
|
|
|
rfc1035::rcode.at(header.rcode)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(tag.tries > 0);
|
|
|
|
submit(tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-04 19:03:36 +01:00
|
|
|
// The tag is committed to being handled after this point; it will be
|
|
|
|
// removed from the tags map...
|
|
|
|
const ctx::uninterruptible::nothrow ui;
|
2019-03-22 22:08:55 +01:00
|
|
|
const unwind untag{[this, &tag, &it]
|
|
|
|
{
|
|
|
|
remove(tag, it);
|
|
|
|
}};
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
assert(tag.tries > 0);
|
2019-07-24 23:14:27 +02:00
|
|
|
tag.last = steady_point::min(); // tag ignored by the timeout worker during handling.
|
2019-03-22 02:24:36 +01:00
|
|
|
tag.rcode = header.rcode;
|
2022-08-15 03:55:42 +02:00
|
|
|
handle_reply(header, buffer, tag);
|
2018-10-01 05:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::handle_reply(const header &header,
|
2022-08-15 03:55:42 +02:00
|
|
|
const_buffer buffer,
|
2018-10-01 05:10:34 +02:00
|
|
|
tag &tag)
|
|
|
|
try
|
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
if(!handle_error(header, tag))
|
|
|
|
throw rfc1035::error
|
|
|
|
{
|
2020-04-22 08:28:22 +02:00
|
|
|
"protocol #%u :%s",
|
2019-03-22 02:24:36 +01:00
|
|
|
header.rcode,
|
|
|
|
rfc1035::rcode.at(header.rcode)
|
|
|
|
};
|
|
|
|
|
2018-10-01 05:10:34 +02:00
|
|
|
// 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)
|
|
|
|
{
|
2023-02-02 06:48:28 +01:00
|
|
|
[[likely]]
|
2018-10-01 05:10:34 +02:00
|
|
|
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
|
2019-04-17 00:01:21 +02:00
|
|
|
ircd::net::dns::resolver::cancel_all(const bool &remove)
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
|
|
|
static const std::error_code &ec
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::operation_canceled)
|
|
|
|
};
|
|
|
|
|
2019-04-17 00:01:21 +02:00
|
|
|
error_all(ec, remove);
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-17 00:01:21 +02:00
|
|
|
ircd::net::dns::resolver::error_all(const std::error_code &ec,
|
|
|
|
const bool &remove)
|
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)
|
2019-04-17 00:01:21 +02:00
|
|
|
error_one(p.second, eptr, false);
|
|
|
|
|
|
|
|
if(remove)
|
|
|
|
for(auto it(begin(tags)); it != end(tags); it = this->remove(it->second, it));
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::error_one(tag &tag,
|
2019-04-17 00:01:21 +02:00
|
|
|
const std::system_error &se,
|
|
|
|
const bool &remove)
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2019-04-17 00:01:21 +02:00
|
|
|
error_one(tag, std::make_exception_ptr(se), remove);
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::resolver::error_one(tag &tag,
|
2019-04-17 00:01:21 +02:00
|
|
|
const std::exception_ptr &eptr,
|
|
|
|
const bool &remove)
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-07-26 07:35:07 +02:00
|
|
|
log, "DNS error id:%u :%s",
|
2019-03-22 02:24:36 +01:00
|
|
|
tag.id,
|
|
|
|
what(eptr)
|
|
|
|
};
|
|
|
|
|
2019-07-24 23:14:27 +02:00
|
|
|
// value causes tag to be ignored by the timeout worker
|
|
|
|
tag.last = steady_point::min();
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
static const answers empty;
|
|
|
|
callback(eptr, tag, empty);
|
2019-04-17 00:01:21 +02:00
|
|
|
|
|
|
|
if(remove)
|
|
|
|
this->remove(tag);
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-07-24 23:06:49 +02:00
|
|
|
log, "fini tag:%u qtype:%u t:%u (tags:%zu sendq:%zu)",
|
2019-03-22 02:24:36 +01:00
|
|
|
tag.id,
|
|
|
|
tag.opts.qtype,
|
2019-07-24 23:06:49 +02:00
|
|
|
tag.tries,
|
2019-03-22 02:24:36 +01:00
|
|
|
tags.size(),
|
|
|
|
sendq.size()
|
|
|
|
};
|
2018-11-09 06:18:39 +01:00
|
|
|
|
2019-04-16 02:27:16 +02:00
|
|
|
if(it != end(tags))
|
|
|
|
unqueue(tag);
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
it != end(tags)?
|
|
|
|
tags.erase(it):
|
|
|
|
it
|
|
|
|
};
|
|
|
|
|
|
|
|
if(ret != end(tags))
|
|
|
|
done.notify_all();
|
|
|
|
|
|
|
|
return 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();
|
2019-06-24 10:17:06 +02:00
|
|
|
if(!ircd::net::dns::resolver_instance)
|
2019-03-11 19:52:42 +01:00
|
|
|
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
|
|
|
|
{
|
2020-03-05 18:48:17 +01:00
|
|
|
net::port(hp)?
|
|
|
|
net::port(hp):
|
|
|
|
uint16_t(53)
|
2019-03-11 19:52:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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()
|
|
|
|
};
|
|
|
|
}
|