Every program you write depends on DNS, and most of us only see it through one function call that turns a name into an address. Behind that call there is a compact binary format, a tree of servers that refer you to each other, caches with timers, and a set of rules that exist because someone once found a way to poison a cache.
I wanted to see all of it, so I wrote it. dns is DNS from scratch in Go, using only the standard library. It has three parts: a message parser and builder, an authoritative server that answers from zone files, and an iterative resolver that starts at the root servers. It is a learning project, and this article walks through what is inside and what each part taught me.
What it does
dnsmsg: the wire format. An RFC 1035 message parser and builder with name compression. It handles A, AAAA, CNAME, NS, MX, TXT and SOA records, keeps unknown types as raw data (RFC 3597), and supports EDNS0 (RFC 6891): the OPT record, the buffer size, the DO bit and the extended RCODE.
zone and server: an authoritative server. It answers from zone files over UDP and TCP. The zone file parser covers the common subset of RFC 1035 section 5: $ORIGIN, $TTL, @, relative names, blank owners, TTL and class in either order, parentheses across lines, ; comments, quoted TXT strings and BIND TTL units such as 1h30m.
resolver: an iterative resolver. It starts at the root servers, follows referrals, chases CNAMEs and caches by TTL, including negative answers.
What I built from scratch, and what it teaches
Parsing a binary protocol defensively. DNS names are compressed: a name can end with a pointer to an earlier position in the packet. That saves bytes and creates a trap. A malformed packet can contain a pointer loop, and a naive parser follows it forever. This parser rejects pointer loops and forward pointers, so a bad packet cannot hang it. The tests go through every truncation point of a valid message and check that nothing panics.
How an authoritative answer is built. The logic follows RFC 1034 section 4.3.2, and it has more cases than "look up the name". The server picks the most specific zone, then:
- if there is a zone cut below the apex, it returns a referral: NS records plus glue, with the AA bit off;
- if the name has the requested type, it answers;
- if the name has a CNAME, it answers with it and follows the chain inside the zone;
- if the name exists, or is an empty non-terminal, it returns NODATA with the SOA;
- otherwise it returns NXDOMAIN with the SOA.
The difference between NXDOMAIN (the name does not exist) and NODATA (the name exists, but not with this type) matters for caching, and the SOA record gives the negative TTL (RFC 2308).
What a recursive resolver really does. The resolver starts with root hints and iterates. It first checks the cache, positive or negative. Then it starts at the closest cached zone cut, or at the roots, and sends the query. An answer, an NXDOMAIN or an authoritative NODATA is cached and returned. A referral is checked: it must point to a strictly deeper zone that contains the name. Sometimes the NS names come without glue addresses, and resolving them is a nested resolution of its own. The resolver also handles lame and upward referrals.
The bailiwick rule. A server can put extra records in the additional section of its reply. If a resolver believed all of them, a server for one domain could plant addresses for another. The rule is to keep only in-bailiwick glue for the NS hosts, which is what stops cache poisoning through the additional section. There is a test that rejects out-of-bailiwick glue.
UDP size limits. Without EDNS a UDP reply is limited to 512 bytes. With EDNS, the client announces its buffer size, and the server packs to the smaller of that size and 1232. If the answer does not fit, the server sets the TC bit and the client retries over TCP, where each message has a 2-byte length prefix and one connection can carry several queries.
How to use it
Use the packages (dnsmsg, zone, server, resolver) as a library:
go get github.com/AbdeldjalilChougui/dns@latest
Or install the dnsd binary:
go install github.com/AbdeldjalilChougui/dns/cmd/dnsd@latest
From a clone of the repository, you can try the three modes:
# Authoritative server for the sample zone
go run ./cmd/dnsd -addr 127.0.0.1:5353 -zone testdata/example.com.zone
dig @127.0.0.1 -p 5353 alias.example.com A +norec
# Recursive resolver (uses the real root servers; needs outbound UDP/TCP 53)
go run ./cmd/dnsd -addr 127.0.0.1:5354 -recursive
dig @127.0.0.1 -p 5354 www.wikipedia.org
# One-off iterative lookup
go run ./cmd/dnsd -lookup www.wikipedia.org -type A
The first command is safe to run anywhere. The second and third talk to the real root servers, so they need outbound access to port 53.
How it is tested
go test -race ./...
The resolver tests are the part I like most. They build a fake hierarchy in-process: a root, com and org, then example.com, example.org and noglue.com. Every query goes through the real wire format. On top of that hierarchy the tests check resolution from the root, query counts that prove the cache works, in-zone and cross-zone CNAMEs, CNAME loops, negative caching and its expiry, TTLs counting down on a fake clock, NS names without glue, failover past a dead server, and the rejection of out-of-bailiwick glue and upward referrals.
The server tests use real UDP and TCP on localhost. They cover EDNS negotiation, REFUSED outside the served zones, 512-byte truncation with TC and the TCP retry, and the FORMERR, BADVERS and NOTIMP replies. The dnsmsg tests include a byte-exact known query and a full round trip of every record type.
Things to know
This is a learning project, and the README is clear about what is missing:
- No DNSSEC. The DO bit is parsed, but nothing is signed or validated. The only protection against spoofing is random IDs, matching the question, and the bailiwick rule. There is no source-port randomisation logic and no 0x20 case randomisation.
- Record types are A, AAAA, CNAME, NS, MX, TXT and SOA. Others are carried as raw RDATA but cannot be written in zone files. There are no wildcards, DNAME, zone transfers (AXFR/IXFR), dynamic updates or NOTIFY.
- The resolver picks servers at random, with no RTT-based selection, no prefetch or serve-stale, and no QNAME minimisation.
- Truncation is simple: if the answer does not fit, all answer and authority data is dropped and TC is set.
-
$INCLUDEand$GENERATEare not supported in zone files.
So do not put it on the public internet as your resolver. Run it on localhost and read its traffic.
What I learned
DNS looked like a lookup table to me before this project. It is closer to a small distributed protocol with a trust model. Most of the resolver's code is not about finding the answer. It is about deciding which parts of a reply it is allowed to believe.
I also learned to respect negative answers. Caching "this does not exist" needs its own TTL and its own rules, and getting it wrong means either hammering servers or hiding a name that was just created.
And building a fake root hierarchy for the tests was worth the effort. It let me test failover, missing glue and poisoning attempts without touching the network.
Get the code
The source, the sample zone file and the tests are on GitHub: https://github.com/AbdeldjalilChougui/dns. It is MIT licensed.
Originally published at abdeldjalilchougui.biz.
Top comments (0)