Sub topic 8
Following a lookup from root to answer
This chapter assumes you know what an IP address is, what a UDP/TCP port is, and how to run commands in a terminal. It also assumes you have met the domain hierarchy, zones, and the idea of a recursive resolver. Here we put those pieces in motion and follow one query from start to finish.
The setting: who does what
Three roles take part in a lookup.
- The stub resolver is the small client in your operating system. It sends one question and expects one final answer. It does not chase referrals.
- The recursive resolver does the work. It is usually run by your ISP, your company, or a public provider. It asks other servers questions until it has an answer, then returns it to the stub.
- The authoritative servers hold the data. The root servers, the TLD servers, and the servers for the domain itself are all authoritative, but each one knows only about its own zone.
The resolver's technique is called iterative resolution. It asks each server the same question. Each server either answers it or says "I don't know, but ask these servers instead." That second kind of reply is a referral.
A cold-cache lookup, step by step
Suppose a stub asks for the A record of www.example.com and the resolver's cache is completely empty.
- Stub to resolver. The stub sends a query with the RD (Recursion Desired) flag set. This asks the resolver to do all the work.
- Resolver to a root server. The resolver does not know where
comis, but every resolver ships with a root hints file. This file lists the addresses of the 13 root server identities (a.root-servers.netthroughm.root-servers.net). The resolver sends the full question to one of them. The root server does not knowwww.example.com. It does know who runscom, so it replies with a referral. The NS records forcomgo in the authority section of the reply. - Resolver to a TLD server. The resolver repeats the question to one of the
comservers. That server knows which name servers are delegated forexample.comand returns another referral. - Resolver to the authoritative server. The resolver asks a server for
example.com. This server holds the zone, so it answers with the A record. It sets the AA (Authoritative Answer) flag. - Resolver to stub. The resolver caches what it learned and returns the answer with the RA (Recursion Available) flag set.
A cold lookup therefore costs three upstream queries. In practice it is rarely that many, as the caching section below explains.
Reading a referral
A referral is a normal DNS response, but it has no answer. Its parts look like this:
| Section | Content in a referral |
|---|---|
| Answer | Empty |
| Authority | NS records naming the servers for the next zone down |
| Additional | A and AAAA records for those servers, called **glue** |
| Flags | AA is not set, because this server is not authoritative for the name asked |
Glue solves a circular dependency. Suppose example.com is served by ns1.example.com. To find that server's address you would need to resolve ns1.example.com, which requires asking the example.com servers. The parent zone breaks the loop by supplying the address inside the referral. Glue is needed only when the name server's name is inside the zone it serves. A zone served by ns1.example.net needs no glue, because the resolver can look that name up separately.
You can see a referral yourself by asking a root server directly, with recursion turned off:
dig @a.root-servers.net www.example.com A +norecurseThe status will be NOERROR, the answer section will be empty, and the authority section will list the com name servers. The aa flag will be missing from the flags line.
Watching it with dig +trace
dig +trace makes dig play the part of a resolver. It does not use your configured resolver for the walk. It starts at the root hints and follows referrals itself.
dig +trace www.example.comThe output has one block per step, in this order.
- The root NS records, learned from dig's built-in hints.
- The referral from a root server, listing the
comservers. - The referral from a
comserver, listing theexample.comservers. - The final answer from an
example.comserver.
Each block ends with a line of the form ;; Received N bytes from ADDRESS#53(NAME) in M ms. This tells you which server gave that step, so you can spot a slow or failing one.
If a trace works but your normal lookups fail, the delegation chain is fine and the problem lies with your resolver, such as a stale cache or a filtering policy.
When the answer is a CNAME
If the authoritative server returns a CNAME instead of an address, the resolver is not done. The CNAME says "this name is an alias for another name." The resolver starts a new lookup for the target name, which may sit in a completely different zone. That lookup can mean another trip through the TLD and authoritative servers. It is the main reason a lookup can take more steps than the three above. The resolver returns the whole chain, CNAME first and then the final address records, in one answer.
How caching shortens the walk
Real resolvers rarely start at the root. They cache every record they receive, including NS records and glue from referrals, for the TTL each record carries.
- After the first lookup under
.com, the resolver remembers thecomname servers. It skips the root for later.comnames. - After a lookup under
example.com, it remembers those name servers too. A query formail.example.comgoes straight to them, so it takes one upstream query. - If the exact answer is cached and its TTL has not expired, the resolver answers immediately with no upstream query.
So the typical cost of a lookup ranges from zero upstream queries (full cache hit) through one (known zone) to three or more (cold cache).
Failure outcomes
The final status in the response header tells you how the walk ended.
| Status | Meaning | Typical cause |
|---|---|---|
| `NOERROR` with answers | Success | Normal case |
| `NOERROR` with no answers | The name exists but has no record of the type asked (often called NODATA) | Asking for AAAA on a name with only an A record |
| `NXDOMAIN` | The name does not exist | Typo, or record never created |
| `SERVFAIL` | The resolver could not get a usable answer | All servers for a zone are down or unreachable, a broken delegation, or a DNSSEC validation failure |
| `REFUSED` | A server declined to answer | An access policy, for example asking an authoritative server to recurse |
A lame delegation is a common cause of SERVFAIL. The parent zone lists a name server in its NS records, but that server does not actually serve the zone. The resolver tries the other listed servers. If it runs out of servers, it gives up.
Details that affect behavior
- Server selection. A zone usually lists several name servers. The resolver tracks response times and prefers the faster ones, but it still tries others to keep its estimates current. If a server times out, it moves on to the next.
- UDP first, TCP fallback. Queries normally use UDP port 53. If a reply is too large, the server sets the TC (truncated) flag and the resolver repeats the query over TCP.
- Query name minimization. A privacy-minded resolver can send only as much of the name as each server needs. The root server would see
cominstead ofwww.example.com, and thecomserver would seeexample.com. This reduces what each server learns about your lookups. - Caching of failures. Negative answers such as
NXDOMAINare cached too, for a time set by the zone's SOA record.
Putting it together
Follow these steps when a name does not resolve and you want to find the broken step.
- Query your normal resolver:
dig www.example.com A. Note the status. - Run
dig +trace www.example.comand look at where it stops or errors. - If the trace stalls after the TLD step, the problem is in the delegation or the authoritative servers.
- If the trace succeeds but the normal query fails, query the same name against another resolver to see whether yours is the outlier.
Exercise
A resolver has just finished a lookup for www.example.com, so its cache holds the NS records for com and example.com with long TTLs. A client now asks it for ftp.example.com, which is not cached. Which servers does the resolver contact, and how many upstream queries does it send, assuming the name has a plain A record?
Show answer
It contacts only an authoritative server for example.com, with one upstream query. It already knows the example.com name servers from the cached referral, so it skips the root and com servers. That server answers with the A record and the AA flag set.
Exercise
A root server's reply to a query for www.example.com has status NOERROR and an empty answer section. The authority section lists NS records for com. Is something wrong, and what does the resolver do next?
Show answer
Nothing is wrong. This is a referral. The root server is not authoritative for the name, so the AA flag is not set. The resolver takes the com name servers (using the glue addresses in the additional section) and sends the same question to one of them.