Skip to content

dns ​

The instance's DNS: servers (transports), the cache in front of them, the DNS router choosing a server by rules, FakeIP and the dns.listen listeners. A port of sing-box 1.14.4's dns/.

Crateours-mod-dns, modules/dns/dns
Binary featuredns (enables router); the transports dns-udp, dns-tcp, dns-tls, dns-https, dns-quic, dns-h3, dns-local, dns-mdns, dns-hosts, dns-fakeip
Crate featuresudp (+tcp), tcp, tls, https (ours-http), quic (ours-quic), h3 (ours-quinn-ext, ours-quic); the binary's dns-tls, dns-https, dns-quic, dns-h3 enable the TLS implementation (ours-mod-tls/implementation, for QUIC also quic), local (= udp, tcp, hosts, mdns), mdns, hosts, fakeip, unstable-ktls

requires ​

  • router — DNS rules are compiled by the router: build_with_services takes from the services the ours_dns_types::rules::Compiler that the router module's DnsRules unit publishes. The crate does not depend on ours-mod-router — the link goes through the service and the shared types of ours-dns-types.
  • dial — queries to servers go through a Dialer (detour, netns, bind) from the factory dial publishes (ours_config::dial); the crate does not depend on ours-mod-dial.
  • tls — DoT and DoH through ours_tls_options::Tls, DoQ and DoH3 through ours_quic::tls::Factory; both verify servers against the store of the certificate section.
  • cache — store_dns, store_rdrc, store_fakeip are written to the cache file it takes through the contract (ours_api::cache::of); the crate does not depend on ours-mod-cache.

provides ​

  • Provide::Dns(module::build_with_services) — the kernel's DNS slot: an ours_api::Dns object (lookup, exchange, fake_ip, reverse, set_real_answers).
  • Provide::Section { key: "dns" }, Provide::Schema { name: "dns" }.
  • Provide::Source { section: "dns", expand: syntax::listeners } — the entities the section means besides what is written: the dns.listen listeners, which appear as direct inbounds (supplied == Some("dns")) — so a DNS server for clients does not need a separate inbound type.
  • Provide::Unit(Ranges) — publishes ours_api::dns::FakeRanges: the FakeIP ranges of each configuration, so that other parts can tell fake addresses from real ones.
  • Provide::Point("dns_filter") — the point for client query filters; see the dns_filter point.

The filter hook ​

ours_dns_types::filter (crates/dns-types/src/filter.rs): trait Filter { check, answered }. Router::exchange (only client queries, not the proxy's own lookup) sets up a Gate per query: check is called once — in Router::finish before Client::exchange_noting, that is, after the rules have chosen the upstream and before the cache; check's answer is given to the client instead of the upstream's, and the fakeip label is not applied to it. If the answer was obtained otherwise (evaluate/respond, a race, legacy mode), check is called after it and may replace it. answered sees the final answer (it may rewrite it — the CNAME check), the time, a cache hit (exchange_noting sets a flag), an error. Queries to upstreams and by rules from DNSOptions::skip_filter do not reach check, but come to answered with skipped. The filter is taken from the services in attach_services on every reload.

Syntax: dns.

Dependencies ​

ours-dns-types (messages, options, the rule interface), ours-tls-options, ours-http (DoH), ours-net-info, ours-net-sys, ours-bufpool, ours-api, ours-codec, ours-journal (the record codec), ours-json; ours-quic, ours-quinn-ext (DoH3 on its HTTP/3), ring, socket2.

Design ​

  • src/module.rs — Module (implements ours_api::Dns), build*, the Ranges unit, the bootstrap check (a server cannot be resolved through itself or through a missing server). Without servers an implicit local is added. Reload keeps unchanged servers with their sockets, the FakeIP table and the cache contents.
  • src/router.rs — the DNS router (dns/router.go). The current mode: evaluate asks a server and keeps the answer for match_response/respond; a race rule waits for answers while the walk continues, and the first resolved final rule wins. The deprecated legacy mode: a rule is matched without address conditions, and the answer is checked afterwards. The mode is chosen by the rules and their rule-sets.
  • src/client.rs — the cache (dns/client.go): TTL, one query per identical question at a time, optimistic (a stale answer while a new query is in flight), query options from rules.
  • src/persist.rs — what is written to the cache file (store_dns, store_rdrc).
  • src/transport.rs, src/transport/ — UDP/TCP (a truncated answer is retried over TCP), DoT (a pool of length-prefixed connections), DoH (a shared HTTP/1.1 and a pooled HTTP/2), DoQ/DoH3 (one QUIC connection per server, a query per stream, one retry on a new connection; DoH3 speaks as quic-go's client, see differences), address.rs — server addresses are resolved only by the instance's DNS, never by the system resolver.
  • src/local.rs — local (like Linux without systemd-resolved): /etc/resolv.conf with rereading, in front of it /etc/hosts and neighbor_domain, link-local zones — over mDNS. src/mdns.rs, src/hosts.rs — the corresponding servers.
  • src/fakeip.rs — the FakeIP table in memory; with store_fakeip a journal in the cache, and a start with the same ranges restores it. src/fakeip_disabled.rs — a build without fakeip.
  • src/dialer.rs — how queries reach a server: own sockets or an outbound from detour.
  • src/syntax.rs, src/schema.rs — the dns section in native notation and the schema.

Tests ​

modules/dns/dns/tests/: client.rs, client_cache.rs, transports.rs, encrypted.rs, quic.rs, miekg.rs (miekg/dns bytes as in sing-box), mutations.rs; fuzzing — fuzz/; the examples examples/quic_endpoint.rs, examples/encrypted_endpoint.rs. Live DNS in a configuration — in the router tests (dns_live.rs, dns_router.rs, dns_rules.rs, dns_fakeip_label.rs).

GPL-3.0-or-later