DHCP (Dynamic Host Configuration Protocol) is how a device that joins a network gets an IP address, and the other settings it needs to use it, without anyone typing numbers in by hand. Every time your phone joins a Wi-Fi network, this short exchange runs in the background before a single web page loads.
What a device needs before it can talk
An IP address on its own isn't enough. To send traffic anywhere useful, a device needs four things:
- An IP address, so other machines know where to send replies.
- A subnet mask, so it knows which addresses are on its local network and which are somewhere else.
- A default gateway, the router it hands traffic to when the destination isn't local. Without it, the device can reach its neighbours but not the internet.
- DNS servers, so it can turn names like example.com into addresses.
You can set all of these by hand. That's called a static configuration, and it works for a handful of servers. It falls apart on a real network: people mistype addresses, two machines end up with the same one, and a laptop set up for the office stops working the moment it joins the Wi-Fi at home. DHCP moves the job to one place, a DHCP server, which keeps track of which addresses are free and hands them out on request. On a home network, the server is almost always built into your router.
The four-message exchange
The conversation between a new device and the server has four steps, usually remembered as DORA: Discover, Offer, Request, Acknowledge.
Discover
The device has no address yet and doesn't know where the server is, so it can't send a normal message to anyone. Instead it broadcasts: it sends a DHCPDISCOVER to every machine on the local network, from the address 0.0.0.0 to the broadcast address 255.255.255.255. DHCP runs over UDP, with clients on port 68 and servers on port 67.
Wondering how this was made? How we make these