2018-01-28 18:37:13 +01: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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_NET_RESOLVER_H
|
|
|
|
|
|
|
|
// This file is not included with the IRCd standard include stack because
|
|
|
|
// it requires symbols we can't forward declare without boost headers. It
|
|
|
|
// is part of the <ircd/asio.h> stack which can be included in your
|
|
|
|
// definition file if you need low level access to this resolver API.
|
|
|
|
|
|
|
|
/// Internal resolver service
|
|
|
|
struct ircd::net::dns::resolver
|
|
|
|
{
|
2018-01-28 23:19:35 +01:00
|
|
|
struct tag;
|
|
|
|
using header = rfc1035::header;
|
2018-01-28 18:37:13 +01:00
|
|
|
|
|
|
|
std::vector<ip::udp::endpoint> server; // The list of active servers
|
|
|
|
size_t server_next{0}; // Round-robin state to hit servers
|
2018-01-28 23:19:35 +01:00
|
|
|
void init_servers();
|
|
|
|
|
|
|
|
ctx::dock dock;
|
|
|
|
std::map<uint16_t, tag> tags; // The active requests
|
2018-01-28 18:37:13 +01:00
|
|
|
|
2018-01-28 23:19:35 +01:00
|
|
|
ip::udp::socket ns; // A pollable activity object
|
2018-02-03 22:15:55 +01:00
|
|
|
ip::udp::endpoint reply_from; // Remote addr of recv
|
|
|
|
char reply[64_KiB] alignas(16); // Buffer for recv
|
2018-01-28 18:37:13 +01:00
|
|
|
|
2018-01-28 23:19:35 +01:00
|
|
|
bool handle_error(const error_code &ec) const;
|
|
|
|
void handle_reply(const header &, const const_buffer &body, tag &);
|
|
|
|
void handle_reply(const header &, const const_buffer &body);
|
2018-01-28 18:37:13 +01:00
|
|
|
void handle(const error_code &ec, const size_t &) noexcept;
|
|
|
|
void set_handle();
|
|
|
|
|
|
|
|
void send_query(const ip::udp::endpoint &, const const_buffer &);
|
|
|
|
void send_query(const const_buffer &);
|
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
tag &set_tag(tag &&);
|
|
|
|
const_buffer make_query(const mutable_buffer &buf, const tag &) const;
|
|
|
|
void operator()(const hostport &, const opts &, callback);
|
2018-01-28 23:19:35 +01:00
|
|
|
|
|
|
|
bool check_timeout(const uint16_t &id, tag &, const steady_point &now);
|
|
|
|
void check_timeouts();
|
|
|
|
void worker();
|
|
|
|
ctx::context context;
|
2018-01-28 18:37:13 +01:00
|
|
|
|
|
|
|
resolver();
|
|
|
|
~resolver() noexcept;
|
|
|
|
};
|
2018-01-28 23:19:35 +01:00
|
|
|
|
|
|
|
struct ircd::net::dns::resolver::tag
|
|
|
|
{
|
2018-02-03 22:15:55 +01:00
|
|
|
uint16_t id {0};
|
2018-01-28 23:19:35 +01:00
|
|
|
hostport hp;
|
2018-02-03 22:15:55 +01:00
|
|
|
dns::opts opts;
|
|
|
|
callback cb;
|
2018-01-28 23:19:35 +01:00
|
|
|
steady_point last {ircd::now<steady_point>()};
|
|
|
|
uint8_t tries {0};
|
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
tag(const hostport &, const dns::opts &, callback);
|
2018-01-28 23:19:35 +01:00
|
|
|
};
|
2018-02-03 22:15:55 +01:00
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::net::dns::resolver::tag::tag(const hostport &hp,
|
|
|
|
const dns::opts &opts,
|
|
|
|
callback cb)
|
|
|
|
:hp{hp}
|
|
|
|
,opts{opts}
|
|
|
|
,cb{std::move(cb)}
|
|
|
|
{}
|