DNS · Domain Name System

The internet’s phone book.

05 · Application · UDP/TCP 53

DNS

Domain Name System

The internet’s phone book.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a DNS exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

Basics

What the protocol is for, and the ideas it is built on.

1.1What it does

The protocol in plain words, before any bytes.

People remember names, networks route addresses. DNS translates example.com into 93.184.216.34. It is a distributed database: no single server knows everything, but each knows where to ask next.

Your device asks a recursive resolver. If the answer is not cached, the resolver walks the hierarchy: root servers, then the .com servers, then example.com’s own authoritative servers. Resolvers cache the answer for its TTL.

1.2Key ideas

The handful of ideas everything else follows from.

Records

A and AAAA give addresses, CNAME an alias, MX mail servers, TXT arbitrary text (SPF, DKIM), NS the delegated name servers.

TTL trades freshness for speed

Short TTLs allow fast changes and cost more queries. Long TTLs are cheap and slow to update.

Name compression

A two-byte pointer replaces repeated names, pointing back to the first occurrence.

Security

Plain DNS is unauthenticated. DNSSEC signs answers. DoH and DoT encrypt the transport.

EDNS0

Extends UDP size beyond 512 bytes and carries options such as client subnet.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. What is the difference between recursive and authoritative servers?
  2. Why does the QNAME have no dots on the wire?

The answers are in part 4.

Part 3 of 5

Detailed answer

Watch a real exchange step by step, then take each message apart field by field.

3.1Follow the packets

Step through the exchange between the two sides.

Browser
Resolver
Authoritative
A? example.com
A? example.com (cache miss)
93.184.216.34, TTL 3600
93.184.216.34
Step 1 of 4

The browser or OS asks the configured resolver, usually learned from DHCP.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

QueryResponse

A recursive question for the A record of example.com, sent as a UDP datagram to port 53.

00001a2b0100000100000000000007657861·+···········exa
00106d706c6503636f6d0000010001mple·com·····
FieldTransaction ID
Length2 bytes
Decimal6699

Random 16-bit ID. The reply must echo it. Predictable IDs enable cache poisoning.

FieldOffsetBytesHex
Transaction ID021a 2b
Flags2201 00
Questions4200 01
Answer RRs6200 00
Authority RRs8200 00
Additional RRs10200 00
QNAME121307 65 78 61 6d 70 6c 65 …
QTYPE25200 01
QCLASS27200 01

3.3Decode it in code

A small decoder, tested against the exact bytes shown above.

Encoding and decoding DNS

This parses the Query and Response on this page, including the compression pointer.

dns.tsTypeScriptCopy
1/** A name goes on the wire as length-prefixed labels, ending in a zero byte. */2export function encodeName(name: string): number[] {3  const out: number[] = [];4  for (const label of name.split(".")) out.push(label.length, ...new TextEncoder().encode(label));5  out.push(0);6  return out;7}8 9/** Reads a name. Two high bits set means a pointer to an earlier name, which saves space. */10export function readName(b: Uint8Array, start: number): { name: string; next: number } {11  const labels: string[] = [];12  let i = start;13  let next = -1;14  for (let jumps = 0; jumps < 32; jumps++) {15    const len = b[i];16    if (len === 0) return { name: labels.join("."), next: next === -1 ? i + 1 : next };17    if ((len & 0xc0) === 0xc0) {18      if (next === -1) next = i + 2; // the record continues after the first pointer19      i = ((len & 0x3f) << 8) | b[i + 1];20    } else {21      labels.push(new TextDecoder().decode(b.subarray(i + 1, i + 1 + len)));22      i += 1 + len;23    }24  }25  throw new Error("pointer loop in DNS name"); // a hostile packet could otherwise hang the parser26}27 28export function parseDns(b: Uint8Array) {29  const view = new DataView(b.buffer, b.byteOffset, b.byteLength);30  const flags = view.getUint16(2);31  let i = 12;32  const questions = [];33  for (let n = view.getUint16(4); n > 0; n--) {34    const { name, next } = readName(b, i);35    questions.push({ name, type: view.getUint16(next) });36    i = next + 4;37  }38  const answers = [];39  for (let n = view.getUint16(6); n > 0; n--) {40    const { name, next } = readName(b, i);41    const type = view.getUint16(next);42    const rdLength = view.getUint16(next + 8);43    const data = b.subarray(next + 10, next + 10 + rdLength);44    answers.push({ name, type, ttl: view.getUint32(next + 4), address: type === 1 ? [...data].join(".") : null });45    i = next + 10 + rdLength;46  }47  return { id: view.getUint16(0), isResponse: (flags & 0x8000) !== 0, wantsRecursion: (flags & 0x0100) !== 0, rcode: flags & 0x0f, questions, answers };48}
48 linesUTF-8TypeScript
out.push(label.length, ...)
Names go on the wire as length-prefixed labels, with no dots.
(len & 0xc0) === 0xc0
Two high bits mark a pointer to a name earlier in the packet.
next === -1
The record continues after the first pointer, not after the place it points to.
jumps < 32
A hostile packet can point at itself. Without this limit, the parser would hang.

Tested by npm run test:modules.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

What is the difference between recursive and authoritative servers?
A recursive resolver finds answers on your behalf and caches them. An authoritative server holds the real records for a zone.
Why does the QNAME have no dots on the wire?
A length prefixes each label and a zero byte ends the name, so dots are unnecessary.
Part 5 of 5

Extra depth

The mistakes people make with this protocol in real systems.

5.1Watch out for

  • “It’s always DNS” is a joke because a wrong record or a stale cache looks like an outage.
  • Changing a record does not take effect everywhere until old TTLs expire.
  • A CNAME cannot coexist with other records at the same name, which is why the zone apex needs ALIAS tricks.
← UDPMark as understoodTCP →