Sub topic 10
DNS messages and transport over UDP and TCP
This chapter assumes you know what a resolver does, what port numbers are, and how to read a hex value. It covers what a DNS message contains and how it travels over the network.
One format for queries and responses
A query and its response share one layout. A message has a fixed 12-byte header followed by four sections.
- Question: the name, type and class being asked about.
- Answer: records that answer the question.
- Authority: usually NS records pointing to the zone's authoritative servers, or an SOA record in negative answers.
- Additional: extra helpful records, such as the A record for a name server named in the authority section.
The header holds a 16-bit ID, a 16-bit flags field, and four 16-bit counts (one per section). The ID lets the client match a response to its query. The flags field is where most of the behavior lives.
| Flag | Meaning |
|---|---|
| QR | 0 for a query, 1 for a response |
| AA | The answer came from an authoritative server |
| TC | The response was truncated and did not fit |
| RD | Recursion desired, set by the client |
| RA | Recursion available, set by the server |
| RCODE | Result code: 0 NOERROR, 2 SERVFAIL, 3 NXDOMAIN |
These are the same flags dig prints in its flags: line.
Reading a header yourself
The script below builds a query by hand, sends it over UDP, and decodes the response header.
import socket
import struct
def build_query(name, qtype=1, query_id=0x1234):
header = struct.pack("!HHHHHH", query_id, 0x0100, 1, 0, 0, 0)
labels = name.rstrip(".").split(".")
qname = b"".join(bytes([len(p)]) + p.encode("ascii") for p in labels) + b"\x00"
return header + qname + struct.pack("!HH", qtype, 1)
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))
data, _ = s.recvfrom(4096)
qid, flags, qd, an, ns, ar = struct.unpack("!HHHHHH", data[:12])
print(f"id={qid:#06x} QR={flags >> 15} AA={(flags >> 10) & 1} "
f"TC={(flags >> 9) & 1} RD={(flags >> 8) & 1} "
f"RA={(flags >> 7) & 1} RCODE={flags & 0xF}")
print("questions:", qd, "answers:", an)Names are encoded as length-prefixed labels ending in a zero byte, so example.com becomes 7 example 3 com 0.
UDP first, TCP when needed
DNS uses port 53. Most queries use UDP because one datagram out and one back is cheap, with no handshake. The original limit for a UDP response was 512 bytes.
EDNS(0) lifts that limit. The client adds an OPT pseudo-record in the additional section that advertises a larger buffer size. Current guidance is to advertise about 1232 bytes, so responses avoid IP fragmentation.
If a response still does not fit, the server sets the TC flag and sends what fits. The client then repeats the query over TCP. TCP messages carry a 2-byte length prefix, because TCP is a byte stream with no message boundaries.
TCP is also used for zone transfers (AXFR and IXFR) between name servers, and by DoT, which runs DNS over TLS.
Trying it with dig
dig +tcp example.com A
dig +bufsize=512 example.com DNSKEY
dig +noedns example.com AThe first command forces TCP. The second advertises a small buffer, which can show tc in the flags for large answers. If dig retries over TCP automatically, that is the normal fallback.
Common errors
- A firewall that allows UDP 53 but blocks TCP 53 breaks large answers, such as DNSSEC responses, while small lookups keep working.
- Blocking or dropping fragmented UDP packets causes intermittent timeouts for large responses.
- A response with a mismatched ID or source address should be ignored by the client. This is one of the checks that makes spoofing harder.
Exercise
A lookup for a name with many TXT records works with dig +tcp but times out with plain dig. Which flag would you expect to see in the UDP response, and what is the likely network problem?
Show answer
The UDP response would carry TC=1 (truncated), or be too large and fragmented. The likely problem is that fragments are being dropped, or that the client cannot complete the TCP fallback because TCP port 53 is blocked. Check firewall rules for both UDP and TCP on port 53 and lower the EDNS buffer size to about 1232.