Inside a DNS Message: Header, Sections, EDNS(0) and UDP Versus TCP
The DNS message byte by byte: the 12-byte header and its flags, opcodes and response codes, the four sections, resource record format, name compression, the EDNS(0) OPT record with cookies and the 1,232-byte buffer size, truncation and the switch to TCP, and retransmission.
kvi5hnuCode Sprout team member15 min readOn this page
Every DNS operation, whether a query, a response, a zone transfer, a notification or an update, uses one message format, defined in RFC 1035 and extended many times since. This article takes real messages from the lab apart byte by byte, then looks at the extension mechanism that lets DNS messages grow beyond their original 512-byte limit, and at when DNS switches from UDP to TCP.
A query and its response, in hex
The client asked ns1.example.com for the A record of www.example.com, with recursion and EDNS turned off to keep the messages minimal. The captured UDP payloads:
query (33 bytes)
0000 4e 96 00 20 00 01 00 00 00 00 00 00 03 77 77 77
0010 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 00 01 00
0020 01
response (173 bytes)
0000 4e 96 84 00 00 01 00 01 00 02 00 04 03 77 77 77
0010 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 00 01 00
0020 01 c0 0c 00 01 00 01 00 00 0e 10 00 04 c6 33 64
0030 0a c0 10 00 02 00 01 00 00 0e 10 00 06 03 6e 73
0040 32 c0 10 c0 10 00 02 00 01 00 00 0e 10 00 06 03
0050 6e 73 31 c0 10 c0 4f 00 01 00 01 00 00 0e 10 00
0060 04 c6 33 64 35 c0 3d 00 01 00 01 00 00 0e 10 00
0070 04 c6 33 64 36 c0 4f 00 1c 00 01 00 00 0e 10 00
0080 10 20 01 0d b8 00 02 00 00 00 00 00 00 00 00 00
0090 53 c0 3d 00 1c 00 01 00 00 0e 10 00 10 20 01 0d
00a0 b8 00 02 00 00 00 00 00 00 00 00 00 54
The query is just 33 bytes: a 12-byte header and a 21-byte question. The response repeats the question and adds 7 resource records in 173 bytes. Let's decode them.
The header

The first 12 bytes of each message, decoded:
query : ID=0x4e96 flags=0x0020 QR=0 OPCODE=0 AA=0 TC=0 RD=0 RA=0 Z=0 AD=1 CD=0 RCODE=0 QD=1 AN=0 NS=0 AR=0
response: ID=0x4e96 flags=0x8400 QR=1 OPCODE=0 AA=1 TC=0 RD=0 RA=0 Z=0 AD=0 CD=0 RCODE=0 QD=1 AN=1 NS=2 AR=4
To see where the flags come from, write the flags word in binary. The response's 0x8400 is 1000 0100 0000 0000: the top bit is QR, and the sixth bit from the left is AA. Everything else is 0. The query's 0x0020 is 0000 0000 0010 0000, which is only AD.
- Transaction ID (
0x4e96): chosen by the client, copied into the response, so the client can match them. It must be random: a predictable ID makes it easier to forge responses (article 54). - QR: 0 for a query, 1 for a response.
- Opcode: the kind of message. 0 is a standard query, 2 is server status (obsolete), 4 is NOTIFY, 5 is UPDATE (article 53) and 6 is DNS Stateful Operations (RFC 8490). Opcode 1, inverse query, was removed long ago.
- AA (authoritative answer): set by a server answering from its own zone. It's set here, because
ns1is authoritative forexample.com. - TC (truncated): the response didn't fit and was cut short. More on this below.
- RD (recursion desired) and RA (recursion available): article 50. Both are 0 here, because the query was sent with
+norecurseto a server that doesn't recurse. - Z: reserved, always 0.
- AD (authentic data) and CD (checking disabled): flags for DNSSEC, the DNS Security Extensions that sign records so resolvers can verify them (article 84).
digsets AD in queries by default, which RFC 6840 allows as a way of saying "tell me whether the data was validated". - RCODE: the response code, 0 for success.
- The four counts: the number of entries in each section. The response has 1 question, 1 answer, 2 authority records and 4 additional records.
Response codes
The RCODE field has 4 bits. The common values, all seen in the lab:
query status
www.example.com at ns1 status: NOERROR
nothere.example.com at ns1 status: NXDOMAIN
www.example.org at ns1 (not its zone) status: REFUSED
opcode 2 (STATUS) at ns1 status: NOTIMP
| Value | Name | Meaning |
|---|---|---|
| 0 | NOERROR | Success. With an empty answer section, it means the name exists but has no data of that type (NODATA). |
| 1 | FORMERR | The server couldn't parse the query. |
| 2 | SERVFAIL | The server failed to process the query: for a resolver, usually because it couldn't get an answer from any authoritative server. |
| 3 | NXDOMAIN | The name doesn't exist. Only meaningful from, or derived from, an authoritative source. |
| 4 | NOTIMP | The server doesn't implement that kind of request. |
| 5 | REFUSED | The server won't answer, for example because it isn't authoritative for that zone and doesn't recurse for this client. |
| 6 to 10 | YXDOMAIN, YXRRSET, NXRRSET, NOTAUTH, NOTZONE | Used by dynamic updates (article 53). |
EDNS extends the field with 8 more bits (below), for codes such as 16 (BADVERS) and 23 (BADCOOKIE).
ns1 answered REFUSED for example.org, a zone it doesn't serve, and NOTIMP for the obsolete STATUS opcode. A SERVFAIL came from the resolver when it tried to resolve a name in lame.example.com, a zone delegated to a server that doesn't exist:
-- first query:
opcode: QUERY, status: SERVFAIL, id: 6455
;; Query time: 3098 msec
-- second query:
opcode: QUERY, status: SERVFAIL, id: 18834
;; Query time: 0 msec
The first query took about 3 seconds while the resolver tried the dead server and gave up. The second one was answered instantly, from the resolver's cache of failures, which RFC 9520 requires (article 50).
The question section
A question is a name, a 16-bit type and a 16-bit class. In the hex dump, it starts at offset 0x0c: the 17-byte name 03 www 07 example 03 com 00 (article 49), then type 00 01 (A) and class 00 01 (IN, the Internet). Class is almost always IN. Class CH (Chaos) survives for server diagnostics, such as dig CH TXT version.bind.
Although the format allows several questions, no server supports that, and RFC 9619 (2024) made it official: a standard query must have exactly one question. The lab sent one with two:
query with QDCOUNT=2 -> response RCODE 1 (FORMERR)
Resource records
The answer, authority and additional sections are lists of resource records (RRs), all with the same layout:

Records with the same name, class and type form a resource record set (RRset): for example, the two NS records for example.com. An RRset is the unit DNS handles: it's cached, signed and returned as a whole, and all its records must have the same TTL (RFC 2181).
Name compression
The answer begins at offset 0x21 in the response, with just two bytes for its name:
answer starts at offset 0x0021: c0 0c = pointer to offset 0x00c (the question name)
then TYPE 1 CLASS 1 TTL 3600 RDLENGTH 4 RDATA 198.51.100.10
After the pointer come the fixed fields: 00 01 (type A), 00 01 (class IN), 00 00 0e 10 (TTL 0xe10 = 3,600 seconds), 00 04 (4 bytes of data), and the data c6 33 64 0a, which is 198.51.100.10.
A byte whose top two bits are both 1 (c0 here) isn't a label length but the start of a compression pointer: the remaining 14 bits give an offset from the start of the message where the rest of the name can be found. c0 0c means "the name at offset 12", which is the question's www.example.com. Pointers can also replace just the end of a name: in its authority section, 03 6e 73 31 c0 10 is ns1 followed by a pointer to offset 0x10, where example.com begins. Without compression, this response would spell out example.com nine more times after the question: in the answer, both NS owner names, both NS targets and all four additional records. Compression keeps messages small, and that's important when a UDP message has a size limit.
Implementations must check pointers carefully. A pointer that loops back on itself, or points outside the message, has caused crashes and vulnerabilities in DNS parsers many times.
EDNS(0): extending the message
The original format had a 512-byte limit on UDP messages, 4 bits of response code and no room for new flags. EDNS(0) (Extension Mechanisms for DNS, RFC 6891) adds all of these without changing the header. A client that supports EDNS adds a special pseudo-record, OPT (type 41), to the additional section of its query. Its fields are reused for new purposes:
- The class field holds the sender's UDP payload size: the largest UDP response it can receive.
- The TTL field holds the extended RCODE bits, the EDNS version (0), and flags, including DO ("DNSSEC OK", meaning send DNSSEC records).
- The RDATA holds a list of options, each a code, a length and a value.
The lab captured a query sent with +dnssec, and Wireshark's decoder shows its OPT record:
Additional records
<Root>: type OPT
Name: <Root>
Type: OPT (41)
UDP payload size: 1232
Higher bits in extended RCODE: 0x00
EDNS0 version: 0
Z: 0x8000
1... .... .... .... = DO bit: Accepts DNSSEC security RRs
.000 0000 0000 0000 = Reserved: 0x0000
Data length: 0
dig shows the server's OPT record as the OPT PSEUDOSECTION. Here the client also asked for the server's cookie and name server identifier. (good) means dig checked that the server returned its own client cookie correctly:
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 32d0821844df352b010000006b00d1fbaed328c6a40ba871 (good)
Common EDNS options:
| Option | Code | Purpose |
|---|---|---|
| NSID | 3 | Asks which server instance answered, useful with anycast (RFC 5001) |
| Client Subnet (ECS) | 8 | A resolver passes part of the client's address so CDNs (content delivery networks, which serve content from many sites around the world) can answer by location (RFC 7871). It has privacy costs and is often limited. |
| Cookie | 10 | Lightweight protection against forged responses and spoofed-source attacks (RFC 7873, RFC 9018). |
| Keepalive | 11 | Idle timeout for DNS over TCP connections (RFC 7828) |
| Padding | 12 | Pads encrypted messages so their size reveals less (RFC 7830, RFC 8467) |
| Extended DNS Error | 15 | Explains failures in detail: "stale answer", "DNSSEC bogus", "no reachable authority" (RFC 8914) |
A server that doesn't understand EDNS ignores the OPT record or answers FORMERR. Servers used to vary widely, and resolvers had workarounds for broken ones. On DNS Flag Day 2019, major resolvers dropped those workarounds, and servers that mishandled EDNS stopped working. Without EDNS, the response has no OPT record:
;; flags: qr aa; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 4
The 1,232-byte buffer size
The UDP payload size seen in the lab, 1,232 bytes, wasn't arbitrary. Large UDP responses get fragmented (article 46), and fragments are often dropped by firewalls or used to poison caches. DNS Flag Day 2020 made 1,232 bytes the default: it's the largest DNS message that fits in a 1,280-byte IPv6 packet (the IPv6 minimum MTU) after a 40-byte IPv6 header and an 8-byte UDP header. RFC 9715 (2025) recommends that resolvers advertise no more than 1,400 bytes, and also allows them to drop fragmented responses.
UDP or TCP
DNS uses port 53 over both UDP and TCP. UDP is used for nearly all queries: one datagram out, one back, no handshake. TCP is used when:
- A response doesn't fit. The server sends as much as fits in a UDP message and sets TC. The client then repeats the query over TCP.
- Zone transfers (AXFR, a full transfer, and IXFR, an incremental one, when large; article 53), which can be megabytes long.
- Clients choose it, for example to protect against spoofing or to keep a connection for many queries, and for encrypted DNS (article 55).
The lab zone has a TXT record set of about 2,000 bytes. Asked over UDP, with +ignore so that dig doesn't retry:
;; flags: qr aa tc; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
; EDNS: version: 0, flags:; udp: 1232
The response had tc set and an empty answer section. Without +ignore, dig retried over TCP automatically:
;; Truncated, retrying in TCP mode.
;; Got answer:
;; flags: qr aa; QUERY: 1, ANSWER: 8, AUTHORITY: 2, ADDITIONAL: 5
; EDNS: version: 0, flags:; udp: 1232
The capture shows the whole exchange: the UDP query and the truncated 44-byte reply (| in tcpdump marks TC), then a TCP connection carrying the answer: seq 1:2275 is 2,274 bytes of TCP payload, a 2-byte length field plus a 2,272-byte DNS message:
IP 192.0.2.10.46661 > 198.51.100.53.53: 64546 [1au] TXT? big.example.com. (44)
IP 198.51.100.53.53 > 192.0.2.10.46661: 64546*-| 0/0/1 (44)
IP 192.0.2.10.45531 > 198.51.100.53.53: Flags [S], seq 4243570081, win 64660, options [mss 1220,sackOK,TS val
IP 198.51.100.53.53 > 192.0.2.10.45531: Flags [S.], seq 3623150519, ack 4243570082, win 65232, options [mss 12
IP 192.0.2.10.45531 > 198.51.100.53.53: Flags [.], ack 1, win 506, options [nop,nop,TS val 2262361568 ecr 3608
IP 192.0.2.10.45531 > 198.51.100.53.53: Flags [P.], seq 1:47, ack 1, win 506, options [nop,nop,TS val 22623615
IP 198.51.100.53.53 > 192.0.2.10.45531: Flags [.], ack 47, win 510, options [nop,nop,TS val 3608195368 ecr 226
IP 198.51.100.53.53 > 192.0.2.10.45531: Flags [P.], seq 1:2275, ack 47, win 510, options [nop,nop,TS val 36081
IP 192.0.2.10.45531 > 198.51.100.53.53: Flags [.], ack 2275, win 542, options [nop,nop,TS val 2262361568 ecr 3
IP 192.0.2.10.45531 > 198.51.100.53.53: Flags [F.], seq 47, ack 2275, win 542, options [nop,nop,TS val 2262361
IP 198.51.100.53.53 > 192.0.2.10.45531: Flags [F.], seq 2275, ack 48, win 510, options [nop,nop,TS val 3608195
IP 192.0.2.10.45531 > 198.51.100.53.53: Flags [.], ack 2276, win 542, options [nop,nop,TS val 2262361568 ecr 3
Over TCP, each DNS message is preceded by a 2-byte length field, so the receiver knows where one message ends and the next begins:
client's TCP payload: 46 bytes; first two bytes 00 2c = length 44, then the DNS message
Truncation depends on the client's advertised buffer size. A record set of 40 A records is about 800 bytes. With three different buffer sizes advertised:
bufsize 512 flags:qr aa tc size 45 bytes
bufsize 1232 flags:qr aa size 809 bytes
bufsize 4096 flags:qr aa size 809 bytes
With 512 bytes advertised, the 809-byte response didn't fit, and only a 45-byte truncated header came back. With 1,232 or more, the whole answer came over UDP.
TCP is not optional
Early DNS treated TCP as a fallback for zone transfers, and some operators blocked TCP port 53. That breaks large responses, which DNSSEC made common. RFC 7766 (2016) made TCP support mandatory for all DNS implementations, and RFC 9210 (2022, BCP 235; BCP stands for Best Current Practice, the RFC series for operational advice) made it a requirement for operators too: authoritative and recursive servers must be reachable over TCP. RFC 7766 also asks implementations to support pipelining (several queries outstanding on one connection), out-of-order responses, and reuse of connections instead of one connection per query.
Timeouts and retransmission
DNS over UDP has no transport-level reliability, so the client retransmits. With the server's responses blocked, dig (told to wait 1 second and try 3 times) sent:
;; communications error to 198.51.100.53#53: timed out
;; communications error to 198.51.100.53#53: timed out
;; communications error to 198.51.100.53#53: timed out
;; no servers could be reached
23:04:38.988668 192.0.2.10.57460 > 198.51.100.53.53: 28044+ [1au]
23:04:39.989911 192.0.2.10.33699 > 198.51.100.53.53: 28044+ [1au]
23:04:40.994160 192.0.2.10.57226 > 198.51.100.53.53: 28044+ [1au]
Each try used the same query ID but a new source port, one second apart, before dig gave up. Real stub resolvers wait longer: glibc waits 5 seconds and tries each server twice by default, cycling through all servers listed in resolv.conf. Recursive resolvers use adaptive timeouts: they track each authoritative server's round-trip time and prefer the fastest, which also spreads queries across anycast and secondary servers.
Common questions
Why does DNS still use UDP if TCP is required?
Because most queries fit in one small packet, and UDP answers them in one round trip with no connection state on the server. A busy resolver or root server handles hundreds of thousands of queries per second, which is far cheaper over UDP. TCP is used when needed, and encrypted DNS uses long-lived TCP or QUIC connections.
What is the 512-byte limit, and does it still apply?
RFC 1035 limited UDP DNS messages to 512 bytes. It still applies to clients that don't use EDNS. With EDNS, the client states a larger size, typically 1,232 bytes today, and the server may send up to that much over UDP.
What does the tc flag mean in dig output?
The response was truncated because it didn't fit the UDP size limit, and the client should retry over TCP. dig does this automatically and prints "Truncated, retrying in TCP mode".
What's an OPT record? I can't find it in the zone.
It's a pseudo-record that exists only in messages, never in zones. Clients and servers add it to the additional section to signal EDNS support, their UDP buffer size, flags like DO, and options such as cookies.
Why is my DNS query getting FORMERR?
The server couldn't parse the query. Common causes are an old server that doesn't understand EDNS or a particular option, a middlebox that mangles DNS, or a malformed query, such as one with more than one question.
Why would a resolver return SERVFAIL for a name that works elsewhere?
The resolver couldn't get a usable answer: the zone's servers didn't respond (a lame delegation), DNSSEC validation failed, or the resolver cached a recent failure. Extended DNS Errors, if the resolver sends them, say which, and article 56 shows how to narrow it down with dig.
Summary
- A DNS message is a 12-byte header followed by question, answer, authority and additional sections.
- The header holds a random transaction ID, the QR, Opcode, AA, TC, RD, RA, AD and CD flags, a 4-bit RCODE, and four section counts. Standard queries have exactly one question (RFC 9619).
- Common RCODEs are NOERROR, FORMERR, SERVFAIL, NXDOMAIN, NOTIMP and REFUSED. NOERROR with no answer means NODATA.
- Resource records have a name, type, class, TTL, data length and data. Records with the same name, class and type form an RRset with one TTL.
- Name compression replaces a repeated name, or the end of one, with a 2-byte pointer to an earlier occurrence.
- EDNS(0) adds an OPT pseudo-record carrying the UDP payload size, extended RCODE, the DO flag and options such as cookies, NSID, padding and Extended DNS Errors. 1,232 bytes is the common payload size, chosen to avoid fragmentation.
- Responses too big for UDP are truncated with TC, and the client retries over TCP, where each message has a 2-byte length prefix. TCP support is mandatory for software and operators (RFC 7766, RFC 9210).
- Clients retransmit UDP queries themselves, with timeouts of a second to a few seconds.
Practice questions
1. A DNS header's flags word is 0x8180. Which flags are set, and what's the RCODE?
Answer
0x8180 is binary 1000 0001 1000 0000: QR = 1 (a response), Opcode = 0, AA = 0, TC = 0, RD = 1, RA = 1, and RCODE = 0. It's a typical successful response from a recursive resolver to a recursive query.
2. In a response, the bytes c0 2b appear where a name should be. What do they mean?
Answer
A compression pointer: the top two bits (11) mark it, and the other 14 bits give offset 0x02b (43). The name continues with whatever labels are at byte 43 of the message.
3. A client advertises an EDNS UDP payload size of 1,232 bytes, and the full response is 1,800 bytes. What happens?
Answer
The server sends a truncated UDP response with TC set, usually with an empty or partial answer. The client retries the query over TCP and gets the full 1,800-byte response.
4. Why is 1,232 bytes the default EDNS buffer size?
Answer
It's the largest DNS message that fits in a 1,280-byte IPv6 packet, the minimum IPv6 MTU, after subtracting 40 bytes for the IPv6 header and 8 for UDP. A DNS response of that size won't need fragmentation on any IPv6 path, and on practically no IPv4 path either.
5. A firewall allows UDP port 53 but blocks TCP port 53. What breaks?
Answer
Any response larger than the UDP limit: big record sets, many DNSSEC-signed responses and large TXT records. The client gets a truncated response, retries over TCP, and times out. Zone transfers and DNS over TLS also fail. RFC 9210 requires TCP to be reachable.
6. A server answers REFUSED. A different server answers SERVFAIL for the same query. What does each tell you?
Answer
REFUSED means the server decided not to answer, typically because it isn't authoritative for the name and won't recurse for you, or a policy forbids it. SERVFAIL means the server tried and failed, for example because it couldn't reach any authoritative server or DNSSEC validation failed.
7. How does a DNS client over TCP know where one response ends and the next begins?
Answer
Each DNS message over TCP is preceded by a 2-byte length field giving the size of the message that follows. That allows several queries and responses to be pipelined on one connection.
Previous: How a Name Is Resolved: Stub, Recursive and Authoritative Servers and Caching
Next: DNS Record Types: A, AAAA, CNAME, MX, NS, SOA, PTR, TXT, SRV, CAA, SVCB and HTTPS

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.