English
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/.
| Crate | ours-mod-dns, modules/dns/dns |
| Binary feature | dns (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 features | udp (+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_servicestakes from the services theours_dns_types::rules::Compilerthat theroutermodule'sDnsRulesunit publishes. The crate does not depend onours-mod-router— the link goes through the service and the shared types ofours-dns-types.dial— queries to servers go through aDialer(detour,netns, bind) from the factorydialpublishes (ours_config::dial); the crate does not depend onours-mod-dial.tls— DoT and DoH throughours_tls_options::Tls, DoQ and DoH3 throughours_quic::tls::Factory; both verify servers against the store of thecertificatesection.cache—store_dns,store_rdrc,store_fakeipare written to the cache file it takes through the contract (ours_api::cache::of); the crate does not depend onours-mod-cache.
provides
Provide::Dns(module::build_with_services)— the kernel's DNS slot: anours_api::Dnsobject (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: thedns.listenlisteners, which appear asdirectinbounds (supplied == Some("dns")) — so a DNS server for clients does not need a separate inbound type.Provide::Unit(Ranges)— publishesours_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 thedns_filterpoint.
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(implementsours_api::Dns),build*, theRangesunit, the bootstrap check (a server cannot be resolved through itself or through a missing server). Without servers an implicitlocalis 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:evaluateasks a server and keeps the answer formatch_response/respond; aracerule 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.confwith rereading, in front of it/etc/hostsandneighbor_domain, link-local zones — over mDNS.src/mdns.rs,src/hosts.rs— the corresponding servers.src/fakeip.rs— the FakeIP table in memory; withstore_fakeipa journal in the cache, and a start with the same ranges restores it.src/fakeip_disabled.rs— a build withoutfakeip.src/dialer.rs— how queries reach a server: own sockets or an outbound fromdetour.src/syntax.rs,src/schema.rs— thednssection 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).