Sub topic 5
How a name is resolved, step by step
This chapter assumes you know what an IP address is, how a domain name is split into labels, and that DNS is organized as root, TLD, and authoritative levels served by name servers. Here you follow one lookup from the program that needs an address to the server that holds the answer.
The three roles in a lookup
Three kinds of software take part in resolution.
- Stub resolver: a small client built into the operating system. Programs call it, and it asks one configured recursive resolver for the answer. It does no searching of its own.
- Recursive resolver: a server, usually run by your ISP, your company, or a public provider such as 1.1.1.1. It does the searching on the client's behalf and caches what it learns.
- Authoritative server: a name server that holds the records for a zone and gives the final answer for it.
The stub sends a recursive query, which means "get me the complete answer". The recursive resolver then sends iterative queries to other servers. An iterative query means "tell me what you know, even if that is only a referral to someone closer".
import socket
for family, _, _, _, sockaddr in socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM):
print(family.name, sockaddr[0])getaddrinfo calls the operating system's stub resolver. This is the lookup a program makes before it opens a connection. The output has one line per address, such as AF_INET for IPv4 and AF_INET6 for IPv6.
The walk, step by step
Suppose the resolver's cache is empty and a client asks for www.example.com.
- The stub resolver sends the question to the recursive resolver.
- The recursive resolver asks a root server. It knows the root servers' addresses in advance. The root does not know the answer. It replies with a referral to the
.comTLD servers. - The resolver asks a
.comserver. That server replies with a referral to the name servers forexample.com. - The resolver asks one of those authoritative servers. It returns the A or AAAA record, flagged as an authoritative answer.
- The resolver caches the result for its TTL and returns it to the stub, which hands it to the program.
Watching it with dig
The +trace option makes dig do the resolver's job itself, so each referral is printed.
dig +trace www.example.comThe output has one block per level: the root servers, then the .com servers, then the authoritative servers with the final record. Without +trace, dig asks your configured resolver and shows only the finished answer.
You can also pick the resolver with @.
dig @1.1.1.1 example.com AComparing answers from two resolvers shows whether they hold the same data.
Shortcuts: caching
Real lookups are usually much shorter than the full walk. The recursive resolver keeps answers until their TTL expires. It also caches referrals, so the root and .com servers are rarely asked again. A typical query for a popular name is answered straight from cache. The stub resolver and the application may add caches of their own.
Common errors
- Assuming the stub does the walk. If lookups fail, check which recursive resolver the stub points to first.
- Reading a cached answer as current. An answer from a recursive resolver may be old. Only an answer from an authoritative server, marked by the
aaflag indigoutput, is first-hand. - Confusing a referral with an answer. A referral names other servers to ask. It carries no address for the target name.
- Expecting
NXDOMAINto mean the server is down. It is a valid answer meaning the name does not exist.
Exercise
In the lookup for www.example.com, what does the root server return, and why can the resolver not stop there?
Show answer
It returns a referral to the .com TLD servers, not an address. The root knows only who runs each top-level domain, so the resolver must keep asking, first the TLD server and then the example.com authoritative server, until it gets an address record.
Exercise
You run dig example.com A and the answer lacks the aa flag. What does that tell you?
Show answer
The answer came from a recursive resolver's cache, not directly from an authoritative server. It may be out of date until its TTL expires.