DNS wire codec, cache, stub resolver and forwarder
README.md

nox-dns #

Warning. This is a work-in-progress experimental fork of ocaml-dns. Do not use it in production; use ocaml-dns instead.

A fork of ocaml-dns, imported at 10.2.5, that adds a forwarding resolver and injects the stub client's clock and random number generator.

Overview #

A stub resolver is the small client inside an application: it sends each query to a configured recursive server and trusts the answer. A recursive resolver walks the name hierarchy from the root servers itself, and a forwarding resolver sits between the two: it receives queries from stubs, passes them on to upstream servers and caches the answers.

ocaml-dns is a complete DNS implementation, but its stub client reads the clock and the random number generator from the transport module, so each transport binds them to process-wide globals. A unikernel or a deterministic test wants to pass its own. This fork exposes the ocaml-dns wire codec, its LRU cache and its transport-agnostic stub client as one module, Dns, and differs from upstream in two ways:

  1. The stub client takes its clock and RNG as arguments (see The client).
  2. It adds a DNS forwarding resolver, Dns.Forward, which ocaml-dns does not have (see The forwarder).

The companion package nox-dns-eio runs both over Eio.

Installation #

Install with opam:

$ opam install nox-dns

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-dns

The client #

Dns.Client.Make (T) takes a transport T and produces a stub resolver. In ocaml-dns the transport's module type S carries two values:

val rng   : int -> string
val clock : unit -> int64

Each upstream transport (dns-client.unix, .lwt, .mirage and .miou-unix) binds them to Mtime_clock.elapsed_ns and Mirage_crypto_rng.generate. This fork removes them from S, which is left with the I/O functions connect, send_recv and close, and passes them to the constructor Make.v instead (called create upstream):

val v :
  ?cache_size:int -> ?edns:... -> ?nameservers:... -> ?timeout:int64 ->
  clock:(unit -> int64) -> rng:(int -> string) ->
  T.stack -> t

Query building, CNAME following, response matching, the TCP length-prefix framing of RFC 7766 and the special names of RFC 6761 are unchanged from ocaml-dns.

The forwarder #

Dns.Forward is a DNS forwarding resolver modelled on the dns_forward library of moby/vpnkit. It decodes a query and answers it locally from /etc/hosts or the cache when it can. Otherwise it races the upstream servers configured for the query's zone, with per-upstream health tracking and three defences taken from current resolvers. 0x20 case randomisation flips the case of each letter of the query name at random (bit 0x20 of its ASCII code); servers echo the name as sent, so a forged reply must guess those bits as well as the 16-bit query id. EDNS payload sizing advertises at most 1232 bytes as the largest UDP reply accepted (RFC 6891), so a reply is never fragmented. Negative caching remembers that a name does not exist, for as long as the zone's SOA record allows (RFC 2308). The core does no I/O: Forward_eio, in nox-dns-eio, drives it, answering clients over UDP and over TCP (RFC 7766) and querying upstreams over UDP. A reply carries an OPT record when the query did and is sized from the query's advertised UDP payload size (RFC 6891), or at most 512 bytes without one (RFC 1035 section 4.2.1).

Its building blocks are separate modules: Dns.Forward_config (the upstream configuration, in vpnkit's file format), Dns.Hosts, Dns.Id_mux, Dns.Case0x20, Dns.Edns_policy, Dns.Server_select and Dns.Upstream_health. They use only the public wire codec, so a consumer that wants the codec alone can ignore them.

The codec #

The wire codec and cache are ocaml-dns's (copyright the ocaml-dns authors, ISC), split into one module per concept: Packet, Rr_map, the record types in Rr, Name for label compression, Edns, Tsig, the registry enums, and the LRU cache as Cache. Constructors are named v rather than create, as in Packet.v and Packet.Question.v.

Usage #

nox-dns is the transport-agnostic core. Provide a transport, a clock and an RNG:

module Client = Dns.Client.Make (My_transport)

let t = Client.v ~clock ~rng ~nameservers stack in
Client.getaddrinfo t Dns.Rr_map.A name

nox-dns-eio: the Eio transport #

Dns_eio answers single A, AAAA and TXT queries through the client above, over TCP (RFC 7766, the default) or UDP (~proto:\Udp). TCP needs only Eio.Net.connect, so it also works over a unikernel's TCP-only network stack. A resolver reads its nameservers from /etc/resolv.conf and falls back to Cloudflare (1.1.1.1) and Google (8.8.8.8`) if that file is missing or empty. The list can be set at construction:

# let resolver =
    Dns_eio.v
      ~nameservers:[ Ipaddr.of_string_exn "1.1.1.1" ]
      ~timeout_s:1.0 () in
  List.map Ipaddr.to_string (Dns_eio.nameservers resolver)
- : string list = ["1.1.1.1"]

Dns_eio.client binds that configuration to a network, a switch and the monotonic clock, and holds the response cache. Create one client and reuse it, so that repeat lookups within a record's TTL do not touch the network:

let lookup_txt env name =
  Eio.Switch.run @@ fun sw ->
  let client =
    Dns_eio.client ~sw
      ~net:(Eio.Stdenv.net env)
      ~mono:(Eio.Stdenv.mono_clock env)
      ~random:(Eio.Stdenv.secure_random env)
      (Dns_eio.v ())
  in
  match Dns_eio.txt client name with
  | Ok records -> List.iter print_endline records
  | Error e -> Fmt.pr "DNS error: %a@." Dns_eio.pp_error e

Dns_eio.a (IPv4) and Dns_eio.aaaa (IPv6) work the same way. A name with no matching records gives Ok [], and a name that does not exist gives Error `Nxdomain.

Licence #

ISC, as ocaml-dns. See LICENSE.md; the copyright is the ocaml-dns authors' plus this fork's changes.