2018-10-01 22:27:46 +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-03-22 02:24:36 +01:00
|
|
|
#include <ircd/asio.h>
|
|
|
|
|
2018-10-01 22:27:46 +02:00
|
|
|
extern ircd::mapi::header
|
|
|
|
IRCD_MODULE;
|
|
|
|
|
|
|
|
namespace ircd::net::dns
|
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
struct tag;
|
|
|
|
|
|
|
|
using answers = vector_view<const rfc1035::answer>;
|
|
|
|
using answers_callback = std::function<void (std::exception_ptr, const tag &, const answers &)>;
|
2019-03-22 02:24:36 +01:00
|
|
|
|
2018-10-01 22:27:46 +02:00
|
|
|
constexpr const size_t MAX_COUNT {64};
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
template<class T> static rfc1035::record *new_record(mutable_buffer &, const rfc1035::answer &);
|
|
|
|
static void handle_resolved(std::exception_ptr, const tag &, const answers &);
|
2018-10-03 23:18:27 +02:00
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
static void handle_resolve_A_ipport(const hostport &, const json::object &rr, opts, uint16_t, callback_ipport);
|
|
|
|
static void handle_resolve_SRV_ipport(const hostport &, const json::object &rr, opts, callback_ipport);
|
|
|
|
static void handle_resolve_one(const hostport &, const json::array &rr, callback_one);
|
2019-08-16 09:27:09 +02:00
|
|
|
|
|
|
|
static void fini();
|
|
|
|
static void init();
|
2018-10-01 22:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd::net::dns::cache
|
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
struct waiter;
|
|
|
|
|
2019-09-12 19:58:55 +02:00
|
|
|
static bool operator==(const waiter &, const waiter &);
|
|
|
|
static bool operator!=(const waiter &, const waiter &);
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
static bool call_waiter(const string_view &, const string_view &, const json::array &, waiter &);
|
2019-08-16 09:27:09 +02:00
|
|
|
static size_t call_waiters(const string_view &, const string_view &, const json::array &);
|
2019-03-23 08:59:25 +01:00
|
|
|
static void handle(const m::event &, m::vm::eval &);
|
|
|
|
|
2019-05-03 06:39:53 +02:00
|
|
|
static bool put(const string_view &type, const string_view &state_key, const records &rrs);
|
|
|
|
static bool put(const string_view &type, const string_view &state_key, const uint &code, const string_view &msg);
|
|
|
|
|
2019-03-24 20:55:59 +01:00
|
|
|
extern conf::item<seconds> min_ttl IRCD_MODULE_EXPORT_DATA;
|
|
|
|
extern conf::item<seconds> error_ttl IRCD_MODULE_EXPORT_DATA;
|
|
|
|
extern conf::item<seconds> nxdomain_ttl IRCD_MODULE_EXPORT_DATA;
|
2019-03-23 08:59:25 +01:00
|
|
|
|
2019-03-17 22:00:02 +01:00
|
|
|
extern const m::room::id::buf room_id;
|
2019-03-22 02:24:36 +01:00
|
|
|
extern m::hookfn<m::vm::eval &> hook;
|
2019-03-23 08:59:25 +01:00
|
|
|
extern std::list<waiter> waiting;
|
2019-09-12 20:03:54 +02:00
|
|
|
extern ctx::mutex mutex;
|
2019-03-22 02:24:36 +01:00
|
|
|
extern ctx::dock dock;
|
2018-10-01 22:27:46 +02:00
|
|
|
}
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
struct ircd::net::dns::cache::waiter
|
|
|
|
{
|
|
|
|
dns::callback callback;
|
|
|
|
dns::opts opts;
|
2019-09-12 19:26:02 +02:00
|
|
|
uint16_t port;
|
2019-03-23 08:59:25 +01:00
|
|
|
string_view key;
|
2019-05-27 01:53:08 +02:00
|
|
|
char keybuf[rfc1035::NAME_BUFSIZE*2];
|
2019-03-23 08:59:25 +01:00
|
|
|
|
2019-09-12 19:26:02 +02:00
|
|
|
waiter(const hostport &hp, const dns::opts &opts, dns::callback &&callback);
|
2019-09-10 21:36:51 +02:00
|
|
|
waiter(waiter &&) = delete;
|
|
|
|
waiter(const waiter &) = delete;
|
|
|
|
waiter &operator=(waiter &&) = delete;
|
|
|
|
waiter &operator=(const waiter &) = delete;
|
2019-03-23 08:59:25 +01:00
|
|
|
};
|
|
|
|
|
2018-10-01 22:27:46 +02:00
|
|
|
//
|
|
|
|
// s_dns_resolver.cc
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::net::dns
|
|
|
|
{
|
|
|
|
// Resolver instance
|
2019-06-24 10:17:06 +02:00
|
|
|
struct resolver extern *resolver_instance;
|
2018-10-01 22:27:46 +02:00
|
|
|
|
2019-03-22 18:41:26 +01:00
|
|
|
uint16_t resolver_call(const hostport &, const opts &);
|
2019-03-22 02:24:36 +01:00
|
|
|
void resolver_init(answers_callback);
|
2018-10-01 22:27:46 +02:00
|
|
|
void resolver_fini();
|
|
|
|
}
|
2019-03-22 02:24:36 +01:00
|
|
|
|
|
|
|
struct ircd::net::dns::resolver
|
|
|
|
{
|
|
|
|
using header = rfc1035::header;
|
|
|
|
|
|
|
|
static conf::item<std::string> servers;
|
|
|
|
static conf::item<milliseconds> timeout;
|
|
|
|
static conf::item<milliseconds> send_rate;
|
|
|
|
static conf::item<size_t> send_burst;
|
|
|
|
static conf::item<size_t> retry_max;
|
|
|
|
|
|
|
|
answers_callback callback;
|
|
|
|
std::vector<ip::udp::endpoint> server; // The list of active servers
|
|
|
|
size_t server_next{0}; // Round-robin state to hit servers
|
|
|
|
ctx::dock dock, done;
|
|
|
|
ctx::mutex mutex;
|
|
|
|
std::map<uint16_t, tag> tags; // The active requests
|
|
|
|
steady_point send_last; // Time of last send
|
|
|
|
std::deque<uint16_t> sendq; // Queue of frames for rate-limiting
|
|
|
|
ip::udp::socket ns; // A pollable activity object
|
|
|
|
|
|
|
|
// util
|
|
|
|
void add_server(const ipport &);
|
|
|
|
void add_server(const string_view &);
|
|
|
|
void set_servers(const string_view &list);
|
|
|
|
void set_servers();
|
|
|
|
|
|
|
|
// removal (must have lock)
|
|
|
|
void unqueue(tag &);
|
|
|
|
void remove(tag &);
|
|
|
|
decltype(tags)::iterator remove(tag &, const decltype(tags)::iterator &);
|
2019-04-17 00:01:21 +02:00
|
|
|
void error_one(tag &, const std::exception_ptr &, const bool &remove = true);
|
|
|
|
void error_one(tag &, const std::system_error &, const bool &remove = true);
|
|
|
|
void error_all(const std::error_code &, const bool &remove = true);
|
|
|
|
void cancel_all(const bool &remove = true);
|
2019-03-22 02:24:36 +01:00
|
|
|
|
|
|
|
// reception
|
|
|
|
bool handle_error(const header &, tag &);
|
|
|
|
void handle_reply(const header &, const const_buffer &body, tag &);
|
|
|
|
void handle_reply(const ipport &, const header &, const const_buffer &body);
|
|
|
|
void handle(const ipport &, const mutable_buffer &);
|
|
|
|
void recv_worker();
|
|
|
|
ctx::context recv_context;
|
|
|
|
|
|
|
|
// submission
|
|
|
|
void send_query(const ip::udp::endpoint &, tag &);
|
|
|
|
void queue_query(tag &);
|
|
|
|
void send_query(tag &);
|
|
|
|
void submit(tag &);
|
|
|
|
|
|
|
|
// timeout
|
|
|
|
bool check_timeout(const uint16_t &id, tag &, const steady_point &expired);
|
|
|
|
void check_timeouts(const milliseconds &timeout);
|
|
|
|
void timeout_worker();
|
|
|
|
ctx::context timeout_context;
|
|
|
|
|
|
|
|
// sendq
|
|
|
|
void flush(const uint16_t &);
|
|
|
|
void sendq_work();
|
|
|
|
void sendq_clear();
|
|
|
|
void sendq_worker();
|
|
|
|
ctx::context sendq_context;
|
|
|
|
|
|
|
|
template<class... A> tag &set_tag(A&&...);
|
|
|
|
const_buffer make_query(const mutable_buffer &buf, tag &);
|
|
|
|
uint16_t operator()(const hostport &, const opts &);
|
|
|
|
|
|
|
|
resolver(answers_callback);
|
|
|
|
~resolver() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::net::dns::tag
|
|
|
|
{
|
|
|
|
uint16_t id {0};
|
|
|
|
hostport hp;
|
|
|
|
dns::opts opts; // note: invalid after query sent
|
|
|
|
const_buffer question;
|
|
|
|
steady_point last {steady_point::min()};
|
|
|
|
uint8_t tries {0};
|
|
|
|
uint rcode {0};
|
|
|
|
ipport server;
|
2019-05-27 01:53:08 +02:00
|
|
|
char hostbuf[rfc1035::NAME_BUFSIZE];
|
2019-03-22 02:24:36 +01:00
|
|
|
char qbuf[512];
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
tag(const hostport &hp, const dns::opts &opts)
|
|
|
|
:hp{hp}
|
|
|
|
,opts{opts}
|
|
|
|
{
|
|
|
|
this->hp.host = { hostbuf, copy(hostbuf, hp.host) };
|
|
|
|
this->hp.service = {};
|
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
tag(tag &&) = delete;
|
|
|
|
tag(const tag &) = delete;
|
2019-09-10 21:36:51 +02:00
|
|
|
tag &operator=(tag &&) = delete;
|
|
|
|
tag &operator=(const tag &) = delete;
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|