/Learn
01 · Application · UDP 67 / 68

DHCP

Dynamic Host Configuration Protocol

How a device with nothing gets an address.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

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

Prove it: Ray Serve Distributed Actor Deployment

System designProtocols

Study with AI

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.

A device that has just joined a network knows nothing: no address, no router, no DNS server. DHCP is how it asks. It shouts into the network with a broadcast, and a DHCP server (usually your router) answers with a lease: an address to use for a while, plus the other settings the device needs.

Engineers call the exchange DORA: Discover, Offer, Request, Acknowledge. Four short UDP messages and the device is on the network.

1.2Key ideas

The handful of ideas everything else follows from.

A lease, not a gift

The address is lent for a lease time. At half the lease the client asks to renew; if the server is gone the client eventually starts again.

Broadcast is the bootstrap

With no address yet, a device can only talk through broadcast. That is why DHCP uses odd source 0.0.0.0 and port 68 to 67.

More than an address

Options carry subnet mask, default gateway, DNS servers, domain name, NTP servers and even a boot file for network boot.

Relays

Routers can forward broadcasts as unicast to a central server (a relay agent), filling in giaddr so the server knows which subnet to answer for.

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. Why does the client send DISCOVER as a broadcast?
  2. What is the difference between OFFER and ACK?

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.

Client
Server
DISCOVER (broadcast)
OFFER
REQUEST (broadcast)
ACK
Step 1 of 4

No address yet, so it sends from 0.0.0.0 to 255.255.255.255 and asks if any server can help.

3.2Decode it byte by byte

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

Discover

A new device has no address, so it broadcasts from 0.0.0.0 to 255.255.255.255, UDP port 68 to 67. Shown without the IP and UDP headers; “×N” means N zero bytes.

0000010106003903f3260000800000000000····9··&········
0010000000000000000000000000000c293e··············)>
00201c9a0000000000000000000000000000················
003000000000000000000000000000000000················
004000000000000000000000000000000000················
005000000000000000000000000000000000················
006000000000000000000000000000000000················
007000000000000000000000000000000000················
008000000000000000000000000000000000················
009000000000000000000000000000000000················
00a000000000000000000000000000000000················
00b000000000000000000000000000000000················
00c000000000000000000000000000000000················
00d000000000000000000000000000000000················
00e000000000000000000000000063825363············c·Sc
00f03501013d0701000c293e1c9a370401035··=····)>··7···
0100060fff···
Fieldop
Length1 byte
Decimal1

1 = BOOTREQUEST (client to server).

FieldOffsetBytesHex
op0101
htype1101
hlen2106
hops3100
xid4439 03 f3 26
secs8200 00
flags10280 00
ciaddr12400 00 00 00
yiaddr16400 00 00 00
siaddr20400 00 00 00
giaddr24400 00 00 00
chaddr281600 0c 29 3e 1c 9a 00 00 …
sname446400 00 00 00 00 00 00 00 …
file10812800 00 00 00 00 00 00 00 …
Magic cookie236463 82 53 63
Option 53240335 01 01
Option 6124393d 07 01 00 0c 29 3e 1c 9a
Option 55252637 04 01 03 06 0f
End2581ff

3.3Decode it in code

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

Decoding a DHCP packet

A test runs this against the Discover packet on this page.

dhcp.tsTypeScript
1/** The fixed BOOTP header is 236 bytes. Options start after the 4-byte magic cookie. */2export function parseDhcp(b: Uint8Array) {3  const view = new DataView(b.buffer, b.byteOffset, b.byteLength);4  const mac = [...b.subarray(28, 28 + b[2])].map((x) => x.toString(16).padStart(2, "0")).join(":");5  if (view.getUint32(236) !== 0x63825363) throw new Error("not DHCP: bad magic cookie");6 7  // Options are type, length, value. Type 0 is padding and 255 ends the list.8  const options = new Map<number, Uint8Array>();9  for (let i = 240; i < b.length && b[i] !== 255; ) {10    if (b[i] === 0) { i++; continue; }11    options.set(b[i], b.subarray(i + 2, i + 2 + b[i + 1]));12    i += 2 + b[i + 1];13  }14  const TYPES = ["", "DISCOVER", "OFFER", "REQUEST", "DECLINE", "ACK", "NAK", "RELEASE", "INFORM"];15  return {16    isRequest: b[0] === 1,17    xid: view.getUint32(4),18    wantsBroadcast: (view.getUint16(10) & 0x8000) !== 0,19    mac,20    messageType: TYPES[options.get(53)?.[0] ?? 0],21    options,22  };23}
23 linesUTF-8TypeScript
b.subarray(28, 28 + b[2])
The hardware address. Byte 2 says how long it is: 6 for Ethernet.
0x63825363
The magic cookie that separates DHCP from older BOOTP.
options.set(b[i], ...)
Type, length, value. Option 53 holds the message type.

Tested by npm run test:modules.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why does the client send DISCOVER as a broadcast?
It has no address and does not know any server, so broadcast is the only way to reach whoever is there.
What is the difference between OFFER and ACK?
An offer is a proposal and can be ignored. The ACK after the client’s REQUEST is the binding commitment.
Part 5 of 5

Extra depth

The mistakes people make with this protocol in real systems.

5.1Watch out for

  • Two DHCP servers on one network (a rogue router) hand out conflicting addresses and cause strange outages.
  • Short leases on busy networks create lots of renewals; long leases hold addresses for devices that left.
  • A device that sees no answer falls back to a 169.254.x.x link-local address.
← SSHIP →
AI Engineer · Protocol Explorer