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_DNS_H
|
|
|
|
|
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
struct dns extern dns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DNS resolution suite.
|
|
|
|
///
|
|
|
|
/// This is a singleton class; public usage is to make calls on the singleton
|
|
|
|
/// object like `ircd::net::dns()` etc.
|
|
|
|
///
|
2018-03-02 08:08:22 +01:00
|
|
|
/// Note that in returned rfc1035::records that TTL values are modified from their
|
|
|
|
/// original value to an absolute epoch time in seconds which we use for caching.
|
|
|
|
/// This modification only occurs if the dns query allows result caching (default).
|
|
|
|
///
|
2018-01-28 18:37:13 +01:00
|
|
|
struct ircd::net::dns
|
|
|
|
{
|
2018-02-03 22:15:55 +01:00
|
|
|
struct opts;
|
2018-01-28 18:37:13 +01:00
|
|
|
struct resolver;
|
2018-03-02 08:08:22 +01:00
|
|
|
struct cache;
|
2018-01-28 18:37:13 +01:00
|
|
|
|
2018-03-02 08:08:22 +01:00
|
|
|
struct cache static cache;
|
2018-01-28 18:37:13 +01:00
|
|
|
struct resolver static *resolver;
|
2018-02-03 22:15:55 +01:00
|
|
|
struct opts static const opts_default;
|
2018-01-28 18:37:13 +01:00
|
|
|
|
2018-05-02 03:40:03 +02:00
|
|
|
using callback = std::function<void (std::exception_ptr, const hostport &, vector_view<const rfc1035::record *>)>;
|
|
|
|
using callback_A_one = std::function<void (std::exception_ptr, const hostport &, const rfc1035::record::A &)>;
|
|
|
|
using callback_SRV_one = std::function<void (std::exception_ptr, const hostport &, const rfc1035::record::SRV &)>;
|
|
|
|
using callback_ipport_one = std::function<void (std::exception_ptr, const hostport &, const ipport &)>;
|
2018-01-28 18:37:13 +01:00
|
|
|
|
2018-03-02 08:08:22 +01:00
|
|
|
// (internal) generate strings for rfc1035 questions or dns::cache keys.
|
|
|
|
static string_view make_SRV_key(const mutable_buffer &out, const hostport &, const opts &);
|
2018-04-29 02:39:08 +02:00
|
|
|
static string_view unmake_SRV_key(const string_view &);
|
2018-03-02 08:08:22 +01:00
|
|
|
|
|
|
|
public:
|
2018-04-15 01:20:21 +02:00
|
|
|
// Cache warming
|
|
|
|
static const callback_A_one prefetch_A;
|
|
|
|
static const callback_SRV_one prefetch_SRV;
|
|
|
|
static const callback_ipport_one prefetch_ipport;
|
|
|
|
|
2018-01-28 18:37:13 +01:00
|
|
|
// Callback-based interface
|
2018-02-03 22:15:55 +01:00
|
|
|
void operator()(const hostport &, const opts &, callback);
|
|
|
|
void operator()(const hostport &, const opts &, callback_A_one);
|
|
|
|
void operator()(const hostport &, const opts &, callback_SRV_one);
|
|
|
|
void operator()(const hostport &, const opts &, callback_ipport_one);
|
2018-01-28 18:37:13 +01:00
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
// Callback-based interface (default options)
|
|
|
|
template<class Callback> void operator()(const hostport &, Callback&&);
|
2018-01-28 18:37:13 +01:00
|
|
|
};
|
2018-01-28 23:19:35 +01:00
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// DNS resolution options
|
|
|
|
struct ircd::net::dns::opts
|
2018-01-28 23:19:35 +01:00
|
|
|
{
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Overrides the SRV query to make for this resolution. If empty an
|
|
|
|
/// SRV query may still be made from other deductions. This string is
|
|
|
|
/// copied at the start of the resolution. It must be a fully qualified
|
|
|
|
/// SRV query string: Example: "_matrix._tcp."
|
|
|
|
string_view srv;
|
2018-01-28 23:19:35 +01:00
|
|
|
|
2018-02-03 22:15:55 +01:00
|
|
|
/// Specifies the SRV protocol part when deducing the SRV query to be
|
|
|
|
/// made. This is used when the net::hostport.service string is just
|
|
|
|
/// the name of the service like "matrix" so we then use this value
|
|
|
|
/// to build the protocol part of the SRV query string. It is ignored
|
|
|
|
/// if `srv` is set or if no service is specified (thus no SRV query is
|
|
|
|
/// made in the first place).
|
|
|
|
string_view proto{"tcp"};
|
2018-03-02 08:08:22 +01:00
|
|
|
|
|
|
|
/// Whether the dns::cache is checked and may respond to the query.
|
|
|
|
bool cache_check {true};
|
|
|
|
|
|
|
|
/// Whether the result of this lookup from the nameserver should be
|
|
|
|
/// added to the cache. If true, the TTL value in result records will be
|
|
|
|
/// modified to an absolute expiration time. If false, no modification
|
|
|
|
/// occurs from the original value.
|
|
|
|
bool cache_result {true};
|
2018-04-29 01:42:18 +02:00
|
|
|
|
|
|
|
/// When false, nxdomain errors are not treated as exceptions and the
|
|
|
|
/// eptr of a callback will not be set. Instead, the returned record
|
|
|
|
/// will contain some nulled / empty data and the user is obliged to
|
|
|
|
/// know that this is not a real DNS answer but the error's result.
|
|
|
|
/// Note: Requires that cache_result is set to true. If not, this value
|
|
|
|
/// is ignored and always considered to be set to true; this is because
|
|
|
|
/// the returned record is a reference to the cached error.
|
|
|
|
bool nxdomain_exceptions {true};
|
2018-03-02 08:08:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// (internal) DNS cache
|
|
|
|
struct ircd::net::dns::cache
|
|
|
|
{
|
2018-04-15 03:35:48 +02:00
|
|
|
static conf::item<seconds> min_ttl;
|
2018-03-09 21:22:10 +01:00
|
|
|
static conf::item<seconds> clear_nxdomain;
|
|
|
|
|
2018-03-06 13:03:31 +01:00
|
|
|
std::multimap<std::string, rfc1035::record::A, std::less<>> A;
|
|
|
|
std::multimap<std::string, rfc1035::record::SRV, std::less<>> SRV;
|
2018-04-15 01:39:13 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
bool get(const hostport &, const opts &, const callback &);
|
|
|
|
rfc1035::record *put(const rfc1035::question &, const rfc1035::answer &);
|
2018-04-29 01:25:20 +02:00
|
|
|
rfc1035::record *put_error(const rfc1035::question &, const uint &code);
|
2018-01-28 23:19:35 +01:00
|
|
|
};
|
2018-02-03 22:15:55 +01:00
|
|
|
|
|
|
|
template<class Callback>
|
|
|
|
void
|
|
|
|
ircd::net::dns::operator()(const hostport &hostport,
|
|
|
|
Callback&& callback)
|
|
|
|
{
|
|
|
|
operator()(hostport, opts_default, std::forward<Callback>(callback));
|
|
|
|
}
|