2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-15 05:06:49 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-15 05:06:49 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-15 05:06:49 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_RFC1035_H
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// RFC 1035 "Domain Names" (Nov. 1987)
|
2018-01-15 05:06:49 +01:00
|
|
|
///
|
|
|
|
namespace ircd::rfc1035
|
|
|
|
{
|
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
|
|
|
|
struct header;
|
|
|
|
struct question;
|
|
|
|
struct answer;
|
2018-02-03 04:12:31 +01:00
|
|
|
struct record;
|
2018-01-15 05:06:49 +01:00
|
|
|
enum class op :uint8_t;
|
2018-11-30 02:56:32 +01:00
|
|
|
|
2019-03-13 21:09:21 +01:00
|
|
|
// Section 2.3.4 Size Limits
|
2022-06-18 04:57:38 +02:00
|
|
|
#ifndef NAME_MAX
|
2019-03-13 21:09:21 +01:00
|
|
|
constexpr size_t NAME_MAX {255};
|
2022-06-18 04:57:38 +02:00
|
|
|
#endif
|
|
|
|
constexpr size_t LABEL_MAX {63};
|
2019-03-13 21:09:21 +01:00
|
|
|
constexpr size_t TTL_MAX {std::numeric_limits<int32_t>::max()};
|
|
|
|
|
2019-05-27 01:53:08 +02:00
|
|
|
constexpr size_t LABEL_BUFSIZE {LABEL_MAX + 1};
|
|
|
|
constexpr size_t NAME_BUFSIZE {NAME_MAX + 1};
|
2019-03-13 21:09:21 +01:00
|
|
|
|
2018-01-15 05:06:49 +01:00
|
|
|
extern const std::array<string_view, 25> rcode;
|
|
|
|
extern const std::unordered_map<string_view, uint16_t> qtype;
|
2018-10-01 04:35:03 +02:00
|
|
|
extern const std::map<uint16_t, string_view> rqtype;
|
2018-01-15 05:06:49 +01:00
|
|
|
|
2019-03-13 18:31:11 +01:00
|
|
|
bool valid_label(std::nothrow_t, const string_view &);
|
|
|
|
bool valid_name(std::nothrow_t, const string_view &);
|
|
|
|
void valid_label(const string_view &);
|
|
|
|
void valid_name(const string_view &);
|
|
|
|
|
2018-01-28 23:16:49 +01:00
|
|
|
const_buffer make_name(const mutable_buffer &out, const string_view &fqdn);
|
|
|
|
size_t parse_name(const mutable_buffer &out, const const_buffer &in);
|
|
|
|
|
2018-01-15 05:06:49 +01:00
|
|
|
mutable_buffer make_query(const mutable_buffer &, const header &, const vector_view<const question> &);
|
2018-01-28 23:16:49 +01:00
|
|
|
mutable_buffer make_query(const mutable_buffer &, const uint16_t &id, const vector_view<const question> &);
|
|
|
|
mutable_buffer make_query(const mutable_buffer &, const uint16_t &id, const question &);
|
2018-01-15 05:06:49 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// Direct representation of the DNS header. This is laid out for
|
|
|
|
/// little-endian platforms only. The uint16_t's are big endian when this is
|
|
|
|
/// punned on the wire data. The debug() function makes it into a pretty
|
|
|
|
/// string but makes no endian adjustments.
|
|
|
|
///
|
|
|
|
struct ircd::rfc1035::header
|
|
|
|
{
|
|
|
|
uint16_t id; ///< query identification number
|
|
|
|
uint8_t rd:1; ///< 0 = recursion not desired; 1 = desired
|
|
|
|
uint8_t tc:1; ///< 0 = not-truncated; 1 = 512 bytes of reply only
|
|
|
|
uint8_t aa:1; ///< 0 = non-authoritative; 1 = authoritative
|
|
|
|
uint8_t opcode:4; ///< purpose of message
|
|
|
|
uint8_t qr:1; ///< 0 = query; 1 = respnse
|
|
|
|
uint8_t rcode:4; ///< response code
|
|
|
|
uint8_t cd:1; ///< checking disabled by resolver
|
|
|
|
uint8_t ad:1; ///< authentic data from named
|
|
|
|
uint8_t unused:1; ///< unused bits (MBZ as of 4.9.3a3)
|
|
|
|
uint8_t ra:1; ///< 1 = recursion available
|
|
|
|
uint16_t qdcount; ///< number of question entries
|
|
|
|
uint16_t ancount; ///< number of answer entries
|
|
|
|
uint16_t nscount; ///< number of authority entries
|
|
|
|
uint16_t arcount; ///< number of resource entries
|
|
|
|
|
|
|
|
std::string debug() const;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
sizeof(ircd::rfc1035::header) == 12,
|
|
|
|
"The RFC1035 header is not the right size in this environment"
|
|
|
|
);
|
|
|
|
|
|
|
|
/// DNS operation code
|
|
|
|
///
|
|
|
|
enum class ircd::rfc1035::op
|
|
|
|
:uint8_t
|
|
|
|
{
|
|
|
|
QUERY = 0, ///< Query [RFC 1035]
|
|
|
|
IQUERY = 1, ///< Inverse Query [RFC 1035, RFC 3425]
|
|
|
|
STATUS = 2, ///< Server status request [RFC 1035]
|
|
|
|
NOTIFY = 4, ///< Notify [RFC 1996]
|
|
|
|
UPDATE = 5, ///< Update [RFC 2136]
|
|
|
|
};
|
|
|
|
|
2018-01-28 23:16:49 +01:00
|
|
|
/// Helper class to construct or parse a question. An object is constructed
|
2018-02-04 21:13:57 +01:00
|
|
|
/// with a fully qualified domain string and the query type (qtype).
|
2018-01-15 05:06:49 +01:00
|
|
|
///
|
|
|
|
/// Note that each part of the fqdn cannot be longer than 63 characters. The
|
|
|
|
/// supplied buffer must be large enough to hold the output, which is about
|
|
|
|
/// the length of the fqdn + 6 bytes. The qtype can be specified as a string
|
|
|
|
/// i.e "A" or "PTR" and it will be translated into the protocol number for
|
2018-02-04 21:13:57 +01:00
|
|
|
/// you in the constructor. All integers are dealt with in host byte order.
|
2018-01-15 05:06:49 +01:00
|
|
|
///
|
|
|
|
struct ircd::rfc1035::question
|
|
|
|
{
|
2018-04-29 03:33:57 +02:00
|
|
|
uint16_t qtype {0};
|
2018-01-15 05:06:49 +01:00
|
|
|
uint16_t qclass {0x01};
|
2018-03-02 06:15:54 +01:00
|
|
|
string_view name;
|
2019-05-27 01:53:08 +02:00
|
|
|
char namebuf[NAME_BUFSIZE];
|
2018-01-15 05:06:49 +01:00
|
|
|
|
|
|
|
/// Composes the question into buffer, returns used portion
|
2018-01-28 23:16:49 +01:00
|
|
|
mutable_buffer print(const mutable_buffer &) const;
|
|
|
|
const_buffer parse(const const_buffer &);
|
2018-01-15 05:06:49 +01:00
|
|
|
|
2018-01-28 23:16:49 +01:00
|
|
|
/// Supply fully qualified domain name and numerical query type
|
|
|
|
question(const string_view &fqdn, const uint16_t &qtype);
|
2018-01-15 05:06:49 +01:00
|
|
|
|
|
|
|
/// Supply fully qualified domain name and name of query type i.e "A"
|
|
|
|
question(const string_view &fqdn, const string_view &qtype)
|
2018-01-28 23:16:49 +01:00
|
|
|
:question{fqdn, rfc1035::qtype.at(qtype)}
|
2018-01-15 05:06:49 +01:00
|
|
|
{}
|
2018-01-28 23:16:49 +01:00
|
|
|
|
|
|
|
question() = default;
|
2018-01-15 05:06:49 +01:00
|
|
|
};
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// Helper class to parse an answer. When the DNS header is received we get
|
|
|
|
/// an answer count. For each answer in the answer section parse() is called
|
|
|
|
/// which stocks this object and then returns a buffer tight to that specific
|
|
|
|
/// answer section. The `rdata` is the actual record content which the user
|
|
|
|
/// can then treat later with rfc1035::record. All integers are dealt with in
|
|
|
|
/// host byte order.
|
2018-01-28 23:16:49 +01:00
|
|
|
///
|
2018-01-15 05:06:49 +01:00
|
|
|
struct ircd::rfc1035::answer
|
|
|
|
{
|
2018-04-29 03:33:57 +02:00
|
|
|
uint16_t qtype {0};
|
|
|
|
uint16_t qclass {0};
|
|
|
|
uint32_t ttl {0};
|
|
|
|
uint16_t rdlength {0};
|
2018-01-28 23:16:49 +01:00
|
|
|
const_buffer rdata;
|
2018-02-03 04:17:22 +01:00
|
|
|
string_view name;
|
2019-05-27 01:53:08 +02:00
|
|
|
char namebuf[NAME_BUFSIZE];
|
2018-01-28 23:16:49 +01:00
|
|
|
|
|
|
|
const_buffer parse(const const_buffer &);
|
2018-01-15 05:06:49 +01:00
|
|
|
|
2018-01-28 23:16:49 +01:00
|
|
|
answer() = default;
|
2018-01-15 05:06:49 +01:00
|
|
|
};
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// Resource record abstract base. The purpose of this abstraction is to allow
|
|
|
|
/// records of any variety to all be dealt with via a `rfc1035::record *` ptr
|
|
|
|
/// and then be downcasted to the specific derived type elaborated below. Use
|
|
|
|
/// the as() template to downcast.
|
2018-01-15 05:06:49 +01:00
|
|
|
///
|
2018-02-04 21:13:57 +01:00
|
|
|
/// Generally this object is not instantiated directly; each record type will
|
|
|
|
/// construct this instead. Nevertheless, the full raw data and type number
|
|
|
|
/// for the record is available in here. All constructors (for both this
|
|
|
|
/// abstraction and for the derivations) take an already-parsed rfc1035::answer
|
|
|
|
/// as their argument; the qtype and ttl information from the answer header is
|
|
|
|
/// included while the legacy qclass is omitted.
|
2018-02-03 19:39:01 +01:00
|
|
|
///
|
2018-02-03 04:12:31 +01:00
|
|
|
struct ircd::rfc1035::record
|
|
|
|
{
|
|
|
|
struct A;
|
|
|
|
struct AAAA;
|
|
|
|
struct CNAME;
|
|
|
|
struct SRV;
|
2018-02-03 19:39:01 +01:00
|
|
|
|
2018-02-03 21:14:07 +01:00
|
|
|
uint16_t type {0};
|
|
|
|
time_t ttl {0};
|
2018-02-03 19:39:01 +01:00
|
|
|
const_buffer rdata;
|
|
|
|
|
|
|
|
template<class T> const T &as() const;
|
|
|
|
|
|
|
|
record(const answer &);
|
2018-03-02 08:34:59 +01:00
|
|
|
record(const uint16_t &type);
|
2018-02-03 21:14:07 +01:00
|
|
|
record() = default;
|
2018-02-03 19:39:01 +01:00
|
|
|
virtual ~record() noexcept;
|
2018-02-03 04:12:31 +01:00
|
|
|
};
|
|
|
|
|
2019-02-16 01:47:00 +01:00
|
|
|
namespace ircd::rfc1035
|
|
|
|
{
|
|
|
|
bool operator==(const record::A &, const record::A &);
|
|
|
|
bool operator!=(const record::A &, const record::A &);
|
|
|
|
bool operator==(const record::AAAA &, const record::AAAA &);
|
|
|
|
bool operator!=(const record::AAAA &, const record::AAAA &);
|
|
|
|
bool operator==(const record::CNAME &, const record::CNAME &);
|
|
|
|
bool operator!=(const record::CNAME &, const record::CNAME &);
|
|
|
|
bool operator==(const record::SRV &, const record::SRV &);
|
|
|
|
bool operator!=(const record::SRV &, const record::SRV &);
|
|
|
|
}
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// Downcast an abstract record reference to the specific record structure.
|
2018-02-03 19:39:01 +01:00
|
|
|
template<class T>
|
|
|
|
const T &
|
|
|
|
ircd::rfc1035::record::as()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return dynamic_cast<T &>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Types of records
|
|
|
|
//
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// IPv4 address record.
|
|
|
|
/// The integer is laid out in host byte order.
|
2018-02-03 04:12:31 +01:00
|
|
|
struct ircd::rfc1035::record::A
|
2018-02-03 19:39:01 +01:00
|
|
|
:record
|
2018-01-29 01:01:50 +01:00
|
|
|
{
|
2018-02-03 21:14:07 +01:00
|
|
|
uint32_t ip4 {0};
|
2018-01-29 01:01:50 +01:00
|
|
|
|
2019-03-18 20:46:27 +01:00
|
|
|
void append(json::stack::object &) const;
|
|
|
|
|
2018-02-03 19:39:01 +01:00
|
|
|
A(const answer &);
|
2018-03-02 08:34:59 +01:00
|
|
|
A();
|
2018-01-29 01:01:50 +01:00
|
|
|
};
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// IPv6 address record.
|
|
|
|
/// The integer is laid out in host byte order.
|
2018-02-03 04:12:31 +01:00
|
|
|
struct ircd::rfc1035::record::AAAA
|
2018-02-03 19:39:01 +01:00
|
|
|
:record
|
2018-01-29 01:01:50 +01:00
|
|
|
{
|
2018-02-03 21:14:07 +01:00
|
|
|
uint128_t ip6 {0};
|
2018-01-29 01:01:50 +01:00
|
|
|
|
2019-03-18 20:46:27 +01:00
|
|
|
void append(json::stack::object &) const;
|
|
|
|
|
2018-02-03 19:39:01 +01:00
|
|
|
AAAA(const answer &);
|
2018-03-02 08:34:59 +01:00
|
|
|
AAAA();
|
2018-01-29 01:01:50 +01:00
|
|
|
};
|
|
|
|
|
2018-02-03 19:39:01 +01:00
|
|
|
/// Canonical name aliasing record
|
2018-02-03 04:12:31 +01:00
|
|
|
struct ircd::rfc1035::record::CNAME
|
2018-02-03 19:39:01 +01:00
|
|
|
:record
|
2018-01-29 01:45:19 +01:00
|
|
|
{
|
2018-02-03 04:17:22 +01:00
|
|
|
string_view name;
|
2019-05-27 01:53:08 +02:00
|
|
|
char namebuf[NAME_BUFSIZE];
|
2018-01-29 01:45:19 +01:00
|
|
|
|
2019-03-18 20:46:27 +01:00
|
|
|
void append(json::stack::object &) const;
|
|
|
|
|
2018-02-03 19:39:01 +01:00
|
|
|
CNAME(const answer &);
|
2018-03-02 08:34:59 +01:00
|
|
|
CNAME();
|
2018-01-29 01:45:19 +01:00
|
|
|
};
|
|
|
|
|
2018-02-04 21:13:57 +01:00
|
|
|
/// Service record.
|
|
|
|
/// The integers are laid out in host byte order.
|
2018-02-03 04:12:31 +01:00
|
|
|
struct ircd::rfc1035::record::SRV
|
2018-02-03 19:39:01 +01:00
|
|
|
:record
|
2018-01-29 01:01:50 +01:00
|
|
|
{
|
2018-02-03 21:14:07 +01:00
|
|
|
uint16_t priority {0};
|
|
|
|
uint16_t weight {0};
|
|
|
|
uint16_t port {0};
|
2018-02-03 04:17:22 +01:00
|
|
|
string_view tgt;
|
2019-05-27 01:53:08 +02:00
|
|
|
char tgtbuf[NAME_BUFSIZE];
|
2018-01-29 01:01:50 +01:00
|
|
|
|
2019-03-18 20:46:27 +01:00
|
|
|
void append(json::stack::object &) const;
|
|
|
|
|
2018-02-03 19:39:01 +01:00
|
|
|
SRV(const answer &);
|
2018-03-02 08:34:59 +01:00
|
|
|
SRV();
|
2018-01-29 01:01:50 +01:00
|
|
|
};
|