DHCP
Dynamic Host Configuration Protocol
How a device with nothing gets an address.
System design›Protocols
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.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why does the client send DISCOVER as a broadcast?
- What is the difference between OFFER and ACK?
The answers are in part 4.
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.
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.
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.
1 = BOOTREQUEST (client to server).
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| op | 0 | 1 | 01 | |
| htype | 1 | 1 | 01 | |
| hlen | 2 | 1 | 06 | |
| hops | 3 | 1 | 00 | |
| xid | 4 | 4 | 39 03 f3 26 | |
| secs | 8 | 2 | 00 00 | |
| flags | 10 | 2 | 80 00 | |
| ciaddr | 12 | 4 | 00 00 00 00 | |
| yiaddr | 16 | 4 | 00 00 00 00 | |
| siaddr | 20 | 4 | 00 00 00 00 | |
| giaddr | 24 | 4 | 00 00 00 00 | |
| chaddr | 28 | 16 | 00 0c 29 3e 1c 9a 00 00 … | |
| sname | 44 | 64 | 00 00 00 00 00 00 00 00 … | |
| file | 108 | 128 | 00 00 00 00 00 00 00 00 … | |
| Magic cookie | 236 | 4 | 63 82 53 63 | |
| Option 53 | 240 | 3 | 35 01 01 | |
| Option 61 | 243 | 9 | 3d 07 01 00 0c 29 3e 1c 9a | |
| Option 55 | 252 | 6 | 37 04 01 03 06 0f | |
| End | 258 | 1 | ff |
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.
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}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.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why does the client send DISCOVER as a broadcast?
What is the difference between OFFER and ACK?
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.