Sub topic 12
DNS Records and Message Format
Prerequisites and the DNS data model
This chapter assumes that you know the layered network model, UDP and TCP basics, IP addresses, ports, binary arithmetic, and the difference between recursive and iterative DNS resolution.
The Domain Name System (DNS) stores information in resource records. A resource record maps a domain name to a value with a specified meaning. For example, an A record maps a name to an IPv4 address.
A record has these main fields:
| Field | Meaning |
|---|---|
| Name | Domain name to which the record belongs |
| Type | Meaning of the data, such as `A`, `MX`, or `NS` |
| Class | Usually `IN`, meaning Internet |
| TTL | Time to live, in seconds |
| RDATA | Type-specific record data |
The TTL tells a resolver how long it may cache the record. A cached record should not be used after its TTL expires.
Common DNS record types
A: maps a name to an IPv4 address.AAAA: maps a name to an IPv6 address.NS: identifies an authoritative name server for a zone.CNAME: gives an alias for another canonical name. The resolver must continue looking up the target name.MX: identifies mail servers. It includes a preference value, and lower preference values are tried first.SOA: describes the start of authority for a DNS zone. It includes the primary name server, responsible mailbox, serial number, refresh interval, retry interval, expiration time, and minimum TTL.TXT: stores text data, often used for verification or email security policies.PTR: maps an address back to a name, commonly for reverse DNS.
A CNAME record should not coexist with other ordinary data records for the same name. For example, a name with a CNAME should not also have an A record.
DNS message layout
A DNS message contains a 12-byte header followed by four variable-length sections:
header
question section
answer section
authority section
additional sectionThe question section describes what is being requested. Each question contains a queried name, QTYPE, and QCLASS.
The answer section contains records that directly answer the question. The authority section identifies name servers or gives other authority information. The additional section provides useful related records, such as the IP address of a name server listed in the authority section.
The header contains an identifier, flags, and four counts:
QDCOUNT: number of questionsANCOUNT: number of answer recordsNSCOUNT: number of authority recordsARCOUNT: number of additional records
Important flags include:
QR: zero for a query and one for a responseAA: response is authoritativeRD: recursion desiredRA: recursion availableRCODE: result code, such as zero for success or three for name error
Names are encoded as length-prefixed labels. For example, www.example.com is encoded as the labels www, example, and com, followed by a zero-length label. DNS messages can use compression pointers to avoid repeating names.
Exercise
A DNS response has QDCOUNT = 1, ANCOUNT = 2, NSCOUNT = 1, and ARCOUNT = 2. How many records are in its answer, authority, and additional sections combined?
Show answer
There are 2 + 1 + 2 = 5 records. The question is counted separately.