netio/dns/mod.rs#
Source: lib/netio/dns/mod.rs
dns -- host names to addresses, and the records of any type
resolve(host, port) -> NetResult<Vec<SocketAddr>> numeric addresses, "localhost" and
names under .localhost, with a final dot as well (the loopback addresses,
RFC 6761) directly, on every system; Windows, macOS, iOS and Android: the
system resolver (plat::net::resolve, getaddrinfo on Apple: VPNs with name
servers of their own, .local over mDNS, iOS's profiles and the app
sandbox, none of which /etc/resolv.conf shows; on Android Bionic's, which
asks netd: private DNS, VPNs, the app's network); Linux: /etc/hosts, then
the name servers of /etc/resolv.conf
(search domains, ndots)
resolve_with(host, port, &Config) the same with a given configuration (tests, a server
of one's own, encrypted servers)
lookup(name, rtype) -> NetResult<Lookup> the records of one type (netpkt::dns::TYPE_MX,
TXT, SRV, CAA, TLSA, HTTPS, ...) from this computer's name servers
(Config::system; search domains as for resolve)
Resolver::new(Config) / Resolver::system() lookups with a cache: lookup(name, rtype),
resolve(host, port), with_cache(entries), clear_cache()
Lookup { name, aliases, records, ttl, security } the records (netpkt::dns::Rr) of the
asked type, of the name the CNAME chain led to (aliases: the names before
it); security: what DNSSEC says (Secure, Insecure; Unchecked when it is off)
Config::system() /etc/hosts + /etc/resolv.conf; Windows: the hosts file
of %SystemRoot% and the DNS servers of the adapters
(plat::net::dns_servers); Android: no name servers
(there is no /etc/resolv.conf, netd keeps them) and no
hosts: resolve_with answers numbers and localhost only
Config { secure: [Server::tls(addr, "dns.example"), Server::https(url)?], .. } DNS over
TLS (RFC 7858) and over HTTPS (RFC 8484); Server::Tls / Server::Https
with a tls::ClientConfig of one's own (a private CA, a pinned key)
read_reply(answer, query) -> Reply what an answer to one of these queries says (the
checks below), for a transport of the caller's
Config { dnssec: Some(Dnssec::root()), .. } DNSSEC validation (off by default): the root's
keys as anchors, or others (Dnssec::none().anchor(zone, ds)); RSA/SHA-1
with .sha1(true); a bogus answer is NetErrorKind::Other with code BOGUS
Name servers on IPv6 link-local addresses carry their zone into the socket address
(sin6_scope_id): "nameserver fe80::1%eth0" (the interface by name or by number, "%2";
plat::net::ip_scope_parse), the adapter's index on Windows. A server whose zone names no
interface of this computer is left out.
Queries of this resolver (resolve on Linux, resolve_with, lookup and Resolver everywhere; the
system resolver of Windows, macOS, iOS and Android checks its answers in its own way): one
question with recursion desired and EDNS(0) offering 1232 bytes over UDP (the DNS flag day
2020); a server that answers FORMERR without EDNS is asked again without it (RFC 6891 7);
timeout_ms per server and attempt (2 s; resolv.conf "options timeout:" in seconds), attempts
rounds over the servers (2; "attempts:"); over TCP when the answer is truncated. Over UDP an
answer counts only from the server asked, with the query's id, as a response (QR) to a
standard query; any answer must carry the query's question (an NXDOMAIN too; only a FORMERR
without EDNS to a query with EDNS may leave it out, and the query goes again without EDNS),
and only records
of the asked name count, or of the name its CNAME chain in the answer leads to (at most 8
steps): records for other names, of other classes and the authority and additional sections
are not taken (but an SOA there for the TTL of a negative answer), names compared label by
label, ASCII case ignored. Over UDP an answer that does not fit is ignored and the wait goes
on. A malformed message (netpkt::dns::Message::parse) does not count.
Results: records of the type (Ok), none because the name has none of the type (NODATA: Ok,
records empty), no such name (NXDOMAIN: NetErrorKind::NotFound), the servers failed or refused
(SERVFAIL, REFUSED, ...: Other), none answered (TimedOut; the names of the search list are not
asked then), no server configured (Other). The search list: a name with fewer than ndots dots
and no final dot is tried with each search domain first; the next name is asked after NXDOMAIN
and NODATA, the first NODATA comes back when no name has records. IPv4 addresses come first
in resolve (the system resolver keeps its own order, RFC 6724's, and a link-local address's
zone).
The cache (Resolver; not resolve, resolve_with and lookup, which leave caching to the system's
resolver): per name and type, the answers this resolver took (never another record of a
message), as long as their lowest TTL says (at most a day; a TTL over 2^31 - 1 counts as 0,
RFC 2181 8), negative answers by their SOA (the lower of its TTL and MINIMUM, RFC 2308 5; at
most an hour; without an SOA none); at most `entries` (1000), the one that expires first
goes when it is full. A cached answer comes back with the TTLs counted down.
Encrypted servers (Config::secure): when there are any, only they are asked -- no fallback to
the plain name servers; queries are padded to a multiple of 128 bytes (RFC 8467 4.1). TLS:
port 853 by the caller's address, the certificate checked for the server's name (the strict
profile of RFC 8310: the system's CAs unless the ClientConfig says else), the connection kept
for the next query (a new one when the server closed it). HTTPS: POST of the query with id 0
(RFC 8484 4.1) as application/dns-message, an answer counts with status 200 and that type;
the URL's host by dns::resolve, or the server's addrs (the certificate still checked for the
URL's host); connections kept (Client's Pool). Not here: mDNS.
DNSSEC (Config::dnssec; validate.rs, netpkt::dnssec): queries carry DO and CD (RFC 4035
4.9.2: the server's validation off, this side checks), so the server must pass signatures
through (systemd-resolved, Unbound, the public resolvers do). Every RRset an answer is made
of is checked against the keys of the zone that signed it, and those keys up to an anchor:
each zone's DNSKEY RRset must be signed by a key a DS of its parent names (or an anchor), each
DS RRset by the parent's keys. The DNSKEY and DS records are asked for as needed and what they
showed is kept per zone (500 zones, their TTL, at most a day). NXDOMAIN and NODATA need their
NSEC or NSEC3 proof (RFC 4035 5.4, RFC 5155 8; empty non-terminals, wildcards, RFC 9824's
compact answers, which come back as NXDOMAIN), a wildcard's answer the proof that the name
itself is not there, a DNAME's CNAME may be unsigned. Unsigned data is checked from the
anchor down, one DS query per label, until a delegation turns out unsigned. A secure answer is
kept no longer than its signatures hold (RFC 4035 5.3.3). Secure: all of
it verified. Insecure: part of it lies below a delegation without DS (by NSEC, NSEC3 or
opt-out), below DS records of no supported algorithm or digest, or is denied by NSEC3 with more
than 50 iterations (RFC 9276), or no anchor is above it. Anything else is bogus: refused, and
kept 30 s so it is not checked again at once. Algorithms: RSA/SHA-256, RSA/SHA-512, ECDSA
P-256 and P-384, Ed25519, Ed448; RSA/SHA-1 (5, 7) counts as unsupported (RFC 9905) unless
Dnssec::sha1 says else; DS digests SHA-1, SHA-256, SHA-384 (a SHA-1 one is ignored beside
the others, RFC 4509 3). Signatures hold from their inception to their expiration with a
tenth of that span as skew (one hour to a day). One lookup may cost 48 queries and 64
signatures at most (KeyTrap, CVE-2023-50387); beyond that it is bogus. The anchors of
Dnssec::root are the root zone's KSK-2017 and KSK-2024 (no RFC 5011 tracking: a later
rollover needs new ones).
The app's permissions: a name its net.connect rules do not cover fails with
NetErrorKind::Denied before any query; the addresses of a covered name are registered
(plat::policy::net_resolved), so connections to them follow that name's ports. Names under
.localhost need no rule, since nothing is asked for them, but their loopback addresses are
registered too: a rule for app.localhost:8080 covers them. An encrypted server is a
connection like any other: its address and port (853), or the DoH URL's host, need a rule.
Re-exports: netpkt::dnssec::Policy
struct Confighosts: String-- the text of a hosts filenameservers: Vec<SocketAddr>search: Vec<String>ndots: usize-- names with at least this many dots are tried as they are firstsecure: Vec<Server>-- encrypted name servers: when there are any,nameserversis not askedtimeout_ms: u32-- how long one server is waited for in each attempt (0: 2000)attempts: u32-- rounds over the servers (0: 2)dnssec: Option<Dnssec>-- DNSSEC validation (None: off): answers checked against these trust anchors, the bogus ones refused (NetErrorKind::Other, code BOGUS), Lookup::security for the rest
/// NetError::code of an answer DNSSEC refuses (bogus): Extended DNS Error 6, "DNSSEC Bogus"
/// (RFC 8914), negated so that it is no operating system's error code
BOGUS: i32 = -6
enum Securitywhat DNSSEC says of a LookupUnchecked-- not checked: DNSSEC validation is off (Config::dnssec), or the name is localhostSecure-- every record of the answer (the CNAME chain too, or the proof that there is none) verified up to a trust anchorInsecure-- part of the answer is provably unsigned: below a delegation without DS, DS records of unsupported algorithms or digests only, NSEC3 opt-out or too many iterations
struct DnssecDNSSEC validation's settings (Config::dnssec)anchors: Vec<(String, netpkt::dns::Ds)>-- where the chain of trust starts: DS records of these zones' keys ("." the root)policy: netpkt::dnssec::Policy-- the algorithms that count (RSA/SHA-1 not, RFC 9905; Policy::sha1 turns it on)
impl Dnssec
fn root() -> Dnssecthe root zone's key signing keys as anchors: KSK-2017 (20326) and KSK-2024 (38696), as IANA publishes them (root-anchors.xml); RFC 9905's policyfn none() -> Dnssecno anchors: only those added withanchor(a zone of one's own, a test)fn anchor(mut self, zone: &str, ds: netpkt::dns::Ds) -> Dnssecone more trust anchor: a DS record of the zone's keyfn sha1(mut self, on: bool) -> DnssecRSA/SHA-1 (algorithms 5 and 7) counts as supported (RFC 9905: operators treat it as not)
impl Config
fn system() -> Config/etc/hosts and /etc/resolv.conf of this machinefn from_resolv_conf(text: &str) -> Configstruct Lookupthe records a lookup foundname: String-- the name the records belong to: the asked one, or where its CNAME chain ledaliases: Vec<String>-- the names of the CNAME chain beforename, the asked one first (empty without a chain)records: Vec<netpkt::dns::Rr>-- the records of the asked type (none: the name has none of it)ttl: u32-- how many seconds the answer may be kept (the lowest TTL, of the chain too)security: Security-- what DNSSEC says of the answer (Unchecked when validation is off)
fn hosts_lookup(hosts: &str, name: &str) -> Vec<IpAddr>the addresses of a hosts file's lines forname(names compare case-insensitively)fn query(id: u16, name: &str, qtype: u16) -> Option<Vec<u8>>a query for one name and type (no EDNS: the plain form of RFC 1035)enum Answerwhat an answer says (parse_answer: addresses only)Found(Vec<IpAddr>)-- the addresses of the asked type (maybe none: the name exists without them)NoName-- the name does not existTruncated-- too large for UDP: ask over TCPBad-- a server failure or an answer that does not fit the question
fn parse_answer(m: &[u8], q: &[u8], qtype: u16) -> Answerthe A or AAAA records of an answer toq(a query ofquery), with the checks of read_replyfn resolve_with(host: &str, port: u16, cfg: &Config) -> NetResult<Vec<SocketAddr>>fn lookup(name: &str, rtype: u16) -> NetResult<Lookup>the records ofnameof one type (see the top of this file)fn resolve(host: &str, port: u16) -> NetResult<Vec<SocketAddr>>the addresses of a host (see the top of this file)