Sub topic 8
Following a lookup from root to answer
This chapter assumes you know what IPv4 and IPv6 addresses are, how ports work, and how to run commands in a terminal. It also assumes you have met zones, delegation, NS records, and the difference between a stub resolver and a recursive resolver. Here we follow one lookup from start to finish.
The setup
Your application calls the stub resolver, which asks a recursive resolver for www.example.com. The recursive resolver starts with an empty cache. It does the work itself and sends iterative queries. Each server either answers or points it to a server closer to the answer. The stub resolver sent a recursive query ("do the work and give me the final answer"). The resolver then sends a series of iterative queries ("tell me what you know").
The resolver always starts from the root hints, a built-in list of the root server addresses.
The steps
- Root. The resolver asks a root server for
www.example.com. The root does not know the answer. It replies with a referral: NS records for thecom.servers, plus glue (their addresses) so the resolver can reach them. - TLD. The resolver asks a
.comserver the same question. It replies with a referral to the NS records forexample.com., again with glue when the name servers sit inside the zone they serve (such asns1.example.com). - Authoritative. The resolver asks a server for
example.com. This server holds the zone, so it replies with an authoritative answer. Theaaflag is set in the reply. - Return. The resolver caches each result and sends the final answer to the stub resolver.
If the answer is a CNAME, the resolver restarts the process for the target name. It may be answered from cache or require a new trip down the tree.
Watching it with dig
dig +trace makes dig do the resolver's job itself, starting at the root.
dig +trace example.comThe output shows a block per step. First the root NS records, then the com. NS records from a root server, then the example.com. NS records from a .com server, and finally the A record from an authoritative server. Each block names the server that gave it.
A normal query shows the result of the resolver's work. Look at the flags line:
dig +noall +comments +answer example.com Aqrmeans this is a response.rdmeans the client asked for recursion.rameans the server offers recursion.aameans the answer is authoritative. A recursive resolver answering from its cache normally does not set it.
Shortcuts in real life
A cold cache is the worst case. In practice the resolver already knows the NS records for com. and often for example.com., so it skips the early steps. Most lookups need zero or one query to an outside server.
Common errors
- Assuming the root knows the answer. Root and TLD servers only refer. They never hold
www.example.com. - Confusing the two query types. The stub resolver sends recursive queries. Resolvers send iterative queries to authoritative servers.
- Broken delegation. If the NS records at the parent do not match the zone, or a name server does not answer, the resolver gets
SERVFAILor times out.dig +traceshows where the chain breaks. - Missing glue. If
ns1.example.comservesexample.combut the parent has no address for it, the resolver cannot find the server.
Exercise
A resolver with an empty cache asks a .com TLD server for www.example.com. What does it get back, and what does it do next?
Show answer
It gets a referral, not an answer: NS records for example.com, usually with glue addresses. The resolver then sends the same question to one of those authoritative servers, which answers with the A record and the aa flag set.