Sub topic 12
DNS Records and Message Format
This chapter covers what a DNS server stores (resource records) and how queries and answers are laid out on the wire. It assumes you know the DNS hierarchy and the difference between iterative and recursive resolution from the previous chapters. It also assumes you can read binary and hexadecimal numbers and run short Python programs.
Resource records
A DNS database is a collection of resource records (RR). Each record is a four-part tuple, written in zone files as one line:
name TTL class type valueThe fields are:
- name: the domain name the record describes, such as
www.example.com. - TTL: how many seconds a cache may keep the record before discarding it.
- class: almost always
IN(Internet). - type: what kind of data the value holds.
- value: the data. Its meaning depends on the type.
Textbook treatments (Kurose and Ross) describe the tuple as (Name, Value, Type, TTL). The content is the same. GATE questions usually use that form.
Common record types
| Type | Name field holds | Value field holds | Typical use |
|---|---|---|---|
| A | Hostname | IPv4 address | Map a name to an IPv4 address |
| AAAA | Hostname | IPv6 address | Map a name to an IPv6 address |
| NS | Domain (for example `example.com`) | Hostname of an authoritative name server for that domain | Delegate a zone to a name server |
| CNAME | Alias name | Canonical (real) hostname | Make one name an alias of another |
| MX | Domain | Mail server hostname, plus a preference number | Route email for the domain |
| PTR | Reverse name, such as `4.3.2.1.in-addr.arpa` | Hostname | Reverse lookup, from address to name |
| SOA | Zone | Zone parameters: primary server, serial number, refresh and retry timers | Describe the start of authority for a zone |
| TXT | Name | Free text | Verification and policy data |
Key points for each type:
- NS records are how delegation works. A TLD server that does not know
www.example.comreturns an NS record forexample.com. It also returns an A record for that name server, called a glue record, when the name server's own name lies inside the delegated domain. Without glue you would need to resolve the name server's name, which requires the very server you are trying to find. - CNAME records point to another name, not an address. A resolver that gets a CNAME must then look up the canonical name. A CNAME cannot coexist with other record types at the same name, so you cannot place one at a zone apex that also needs NS and SOA records.
- MX records carry a preference value. The mail server with the lowest number is tried first.
- PTR records live under the special domains
in-addr.arpa(IPv4) andip6.arpa(IPv6). The IPv4 octets are written in reverse order, so the address 192.0.2.5 is looked up as5.2.0.192.in-addr.arpa. The reversal matches the hierarchy, because the most general part of an address is the first octet, while in a domain name the most general part is on the right.
A worked example
Suppose a resolver asks for www.example.com, and the zone for example.com contains:
www.example.com. 300 IN CNAME web1.example.com.
web1.example.com. 300 IN A 192.0.2.10
example.com. 3600 IN MX 10 mail.example.com.
example.com. 86400 IN NS ns1.example.com.
ns1.example.com. 86400 IN A 192.0.2.53The answer to the query for the A record of www.example.com contains two records: the CNAME and then the A record of web1.example.com. The server returns both in one response, because it holds both. If a server only held the CNAME, the resolver would issue a second query for the target.
A mail client sending to [email protected] asks for the MX record of example.com. It receives mail.example.com, then asks for its A record.
TTL and caching
TTL controls how long a resolver or client may reuse a record. A long TTL reduces load on authoritative servers and speeds up repeated lookups. A short TTL lets changes spread quickly. If you change a server's address, caches can keep serving the old address until the old TTL expires. Operators therefore lower the TTL before a planned change. For a GATE question about a cache hit, the saved lookup uses the record only while its TTL has not run out.
The DNS message format
DNS queries and responses share one format. Over UDP, a message normally travels in a single datagram to port 53. Classic DNS limited UDP messages to 512 bytes. If a response is truncated, the server sets the TC flag and the client retries over TCP, also on port 53. Extension mechanisms (EDNS0) later allowed larger UDP messages.
A message has a fixed 12-byte header followed by four variable-length sections.
The header
| Field | Size | Meaning |
|---|---|---|
| ID | 16 bits | Chosen by the client. The server copies it into the reply so the client can match replies to queries. |
| Flags | 16 bits | Described below. |
| QDCOUNT | 16 bits | Number of entries in the question section. |
| ANCOUNT | 16 bits | Number of records in the answer section. |
| NSCOUNT | 16 bits | Number of records in the authority section. |
| ARCOUNT | 16 bits | Number of records in the additional section. |
The 16 flag bits are, from the most significant bit:
| Flag | Size | Meaning |
|---|---|---|
| QR | 1 bit | 0 for a query, 1 for a response. |
| Opcode | 4 bits | Kind of query. 0 is a standard query. |
| AA | 1 bit | Authoritative answer. The responding server is authoritative for the name. |
| TC | 1 bit | Truncated. The message was cut to fit the transport. |
| RD | 1 bit | Recursion desired. Set by the client. |
| RA | 1 bit | Recursion available. Set by the server. |
| Z | 3 bits | Reserved. |
| RCODE | 4 bits | Response code. 0 means no error, 3 means the name does not exist (NXDOMAIN). |
The RD and RA bits connect to the previous chapter. A stub resolver on your machine sets RD=1 to ask its local resolver to do the full work. The local resolver answers with RA=1 to show it offers recursion. A root server typically answers with RA=0.
The four sections
- Question: the name being asked about, a query type (such as A or MX), and a query class. A query normally holds one question.
- Answer: records that answer the question.
- Authority: records that point to authoritative servers, usually NS records in a referral.
- Additional: helpful extra records, such as the A record (glue) for a name server listed in the authority section.
A referral from a TLD server for www.example.com has an empty answer section, NS records in authority, and glue A records in additional. An answer from an authoritative server has the records in the answer section and AA=1.
How names are encoded
A name in a message is a series of labels. Each label starts with a length byte, followed by that many characters. The name ends with a zero-length byte, which is the empty root label. So www.example.com becomes:
3 w w w 7 e x a m p l e 3 c o m 0Each label is at most 63 bytes, and a whole name is at most 255 bytes.
Answers often repeat the name from the question, so DNS uses compression. If the two high bits of a length byte are 11, the byte and the next one form a 14-bit pointer to an earlier offset in the message, where the rest of the name appears. A typical answer record starts with the pointer 0xC00C, which points to offset 12, the first byte after the header, where the question name begins.
Building and reading a query
The program below builds a DNS query by hand, sends it over UDP, and parses the header of the reply. It uses only the standard library. Replace the server address if 8.8.8.8 is not reachable on your network.
import socket
import struct
def encode_name(name):
out = b""
for label in name.rstrip(".").split("."):
out += bytes([len(label)]) + label.encode("ascii")
return out + b"\x00"
def build_query(name, qtype=1, query_id=0x1234):
flags = 0x0100 # QR=0, Opcode=0, RD=1
header = struct.pack("!HHHHHH", query_id, flags, 1, 0, 0, 0)
question = encode_name(name) + struct.pack("!HH", qtype, 1) # class IN
return header + question
query = build_query("example.com")
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(3)
s.sendto(query, ("8.8.8.8", 53))
reply, _ = s.recvfrom(512)
rid, flags, qd, an, ns, ar = struct.unpack("!HHHHHH", reply[:12])
print("ID matches:", rid == 0x1234)
print("QR:", (flags >> 15) & 1)
print("RA:", (flags >> 7) & 1)
print("RCODE:", flags & 0xF)
print("answers:", an)The ! in the format string selects network (big-endian) byte order. The query has a 12-byte header, 13 bytes for the encoded name example.com (1+7+1+3+1), and 4 bytes for type and class, so it is 29 bytes long. A well-formed reply shows QR=1, and usually RA=1 and RCODE=0.
Common mistakes
- Confusing the name field of an NS record with the server's name. The name is the domain being delegated, and the value is the server's hostname.
- Saying a CNAME maps to an IP address. It maps to another name.
- Forgetting that the PTR name is written with the octets reversed.
- Treating the Authority section as the answer. It holds referrals.
- Assuming DNS always uses UDP. Large responses and zone transfers use TCP.
- Reading the TTL as a value set by the client. The authoritative server sets it, and caches count it down.
Exercises
Exercise
A TLD server receives a query for www.shop.example.org, and it knows the delegation for shop.example.org, whose name server is ns.shop.example.org. Which record types does it return, and in which message sections?
Show answer
It returns an NS record (name shop.example.org, value ns.shop.example.org) in the authority section. Because the name server's name is inside the delegated domain, it also returns an A record for ns.shop.example.org (glue) in the additional section. The answer section is empty and AA is 0.
Exercise
How is the IPv4 address 203.0.113.9 looked up in a reverse query, and which record type is returned?
Show answer
The octets are reversed and the suffix is added, giving 9.113.0.203.in-addr.arpa. The query type is PTR, and the returned value is a hostname.
Exercise
A DNS query for the name a.bc.de is encoded in the question section with type A and class IN. How many bytes does the question section occupy?
Show answer
The encoded name is 1+1 (label a) + 1+2 (label bc) + 1+2 (label de) + 1 (terminating zero) = 10 bytes. Type and class add 2 bytes each, so the question section is 14 bytes.
Exercise
A resolver sends a query with ID 0x00A1. The response arrives with ID 0x00A2. Should the resolver accept it, and why?
Show answer
No. The server copies the query ID into its reply, so a different ID means the response does not belong to this query. The resolver discards it. Checking the ID (together with the source port and address) also helps against forged replies.