build-a-dns-resolver / lesson-08.md
Lesson 08 · Names and the question section

The root name

The root of the DNS tree has an empty name, and it encodes to a single zero byte. Today you handle that edge so an empty or dot-only name does not produce a stray empty label.

The goal

Encode the empty root name as exactly one zero byte.

Start here - the target
TO DO
Scenario: The empty root name is a single zero byte
Giventhe empty name "" (and equivalently ".")
Whenit is encoded as a QNAME
Thenthe result is exactly the one byte 00
Andthe single-label name "a" still encodes to 01 61 00
Background

The very top of the domain namespace is the root, written as . or nothing at all, and its on-the-wire form is a name with no labels - just the zero terminator, a single 00 byte. Your lesson 7 encoder would trip on this: splitting "" on dots yields one empty string, which would encode as a bogus zero-length label and then another zero terminator. Both look like 00, so the result would be two zero bytes instead of one.

The fix is a small guard: strip a trailing dot, and if nothing is left, emit just the single terminator. This matters because the root name appears for real - it is the name you query when you ask a root server for a top-level domain’s servers, and it is the SOA and NS owner at a zone apex. Handle it now and every later name, from a to a long fully-qualified one, encodes cleanly.

Make it work
func encodeName(name string) []byte {
name = strings.TrimSuffix(name, ".")
if name == "" {
return []byte{0x00} // the root is just the terminator
}
// ...otherwise encode each label then append 0x00
}
CheckpointDONE
The root name encodes to a single zero byte. Commit and stop here.