Sub topic 3
DNS records and message format
This chapter assumes you know the layered network model, what a UDP socket is, how a TCP connection carries a byte stream, and how to read hexadecimal numbers.
DNS resource records
A DNS resource record (RR) is one entry in a zone file or a DNS response. Each record has a name, a type, a time-to-live (TTL) in seconds, and data.
| Type | Meaning | Example data |
|---|---|---|
| A | IPv4 address | 93.184.216.34 |
| AAAA | IPv6 address | 2606:2800:220:1::248 |
| CNAME | Alias for another name | www.example.com points to example.com |
| MX | Mail server, with a preference value | 10 mail.example.com |
| NS | Authoritative name server for a zone | ns1.example.com |
DNS message format
A DNS message has a 12-byte header and four sections. The header holds a 16-bit ID, a flags field, and four 16-bit counts: QDCOUNT, ANCOUNT, NSCOUNT, and ARCOUNT.
HTTP messages
An HTTP/1.1 request has a request line, header lines, a blank line, and an optional body:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
A response has a status line in place of the request line, then headers, a blank line, and the body.
Methods, status codes, and common headers
- GET retrieves a resource. HEAD does the same but returns headers only.
- POST sends data to be processed. PUT replaces a resource. DELETE removes one.
- Status codes group by first digit: 1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error.
- Host is mandatory in HTTP/1.1 requests. Content-Length gives the body size in bytes.
Persistent connections, pipelining, and newer versions
HTTP/1.1 keeps a TCP connection open by default, so later requests skip a new handshake. Pipelining sends several requests before the first response arrives, but responses must return in order, so one slow response blocks the rest. HTTP/2 multiplexes streams over one TCP connection. HTTP/3 runs over QUIC, which uses UDP.
Cookies and caching
A server sends a cookie with Set-Cookie. The browser returns it in a Cookie request header. Cache-Control: max-age=300 means a cached copy stays fresh for 300 seconds. After that, the client revalidates it with a conditional request, and the server answers 304 Not Modified if the copy is still current.
Solving GATE-style problems
A response carries max-age=300, and the cached copy has an Age of 120 seconds. The remaining freshness is 300 − 120 = 180 seconds.
Exercise
How many bytes long is the header of a DNS message, and which four counts does it contain?
Show answer
The header is 12 bytes. It contains QDCOUNT, ANCOUNT, NSCOUNT, and ARCOUNT, each a 16-bit field.