Opscraft
How DNS works behind the scenes · Lesson 4 of 9 · 3 min read

DNS messages and transport

Message structure

Every DNS query and response has the same format:

Section Contents
Header 16-bit ID (matches responses to queries), flags, counts
Question Name, type, class being asked (api.example.com A IN)
Answer Records answering the question
Authority NS records for referrals, or the SOA for negative answers
Additional Helpful extras: glue A/AAAA for referred name servers, EDNS OPT record

Header flags: QR (response), AA (authoritative answer), TC (truncated), RD (recursion desired), RA (recursion available), AD (authenticated data — DNSSEC validated), CD (checking disabled), plus the RCODE: NOERROR, NXDOMAIN, SERVFAIL, REFUSED, FORMERR.

$ dig api.example.com
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31337
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:  EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;api.example.com.            IN  A
;; ANSWER SECTION:
api.example.com.     287     IN  A   203.0.113.10
;; Query time: 1 msec
;; SERVER: 10.0.0.2#53(10.0.0.2) (UDP)
Enlarge diagramHeaderID, flags (QR, AA, TC, RD, RA, AD), countsQuestionapi.example.com A INAnswerapi.example.com 287 IN A 203.0.113.10AuthorityNS records for referrals, or the SOA for "no such name"Additionalglue addresses for name servers, the EDNS OPT record
Figure 1. Every DNS query and response has the same five sections.
Swipe sideways to see all of it, or tap to open it full screen.

Transport

  • Default: UDP port 53 — one small packet each way, no handshake.
  • TCP 53 when a response is too large (the server sets TC and the client retries over TCP), for zone transfers, and increasingly by choice.
  • EDNS0 extends DNS: larger UDP payloads (the modern recommendation is about 1,232 bytes to avoid IP fragmentation), the DNSSEC OK bit, EDNS Client Subnet, cookies.
  • Encrypted DNS: DoT (DNS over TLS, TCP 853) and DoH (DNS over HTTPS, 443) protect queries from on-path snooping and tampering between client and resolver; DoQ (over QUIC) is newer. Browsers may use DoH directly, bypassing the OS resolver — a reason internal names sometimes fail in a browser but work in curl.
Enlarge diagramyour deviceresolver1.1.1.1, 8.8.8.8…authoritativeDoT 853 / DoH 443plain UDP/TCP 53DNSSEC (not encryption) is what lets the resolver check the answer itself is genuine.
Figure 2. Encrypted DNS protects the first hop from snooping and tampering. The resolver still talks to authoritative servers in plain DNS.
Swipe sideways to see all of it, or tap to open it full screen.