Introduction
A new phone joins your Wi-Fi and a few seconds later it can browse the web. Nobody typed in an address, a mask or a gateway. And your whole family shares one public address from the provider. Two small protocols make this work: DHCP hands out addresses automatically, and NAT lets many private devices share one public address. Both sit at the edge of every home and office network.
How it works
A device that joins a network has no address yet, so it cannot talk to a server in the usual way. DHCP solves this with four messages, remembered as DORA:
- Discover: the device broadcasts "is there a DHCP server?" from address
0.0.0.0to255.255.255.255. - Offer: a server answers with an address it can lend.
- Request: the device asks for that address.
- Acknowledge: the server confirms. The device now has an address, a subnet mask, a default gateway, a DNS server and a lease: a time (often hours or a day) after which it must ask again.
The device starts renewing halfway through the lease. If it leaves and the lease runs out, the server may give the address to someone else.
pool = ["192.168.1.100", "192.168.1.101", "192.168.1.102"]
leases = {} # client MAC -> address
def discover(mac):
if mac in leases: # a known client gets the same address again
return leases[mac]
for address in pool:
if address not in leases.values():
leases[mac] = address
return address
return None
for mac in ["aa:01", "aa:02", "aa:01", "aa:03", "aa:04"]:
answer = discover(mac)
if answer is None:
print(mac, "DISCOVER -> no free address")
else:
print(mac, "DISCOVER -> OFFER and ACK", answer)The private addresses DHCP hands out cannot be used on the open internet, so the router does NAT (network address translation). For each outgoing packet it replaces the private source address and port with its own public address and a new port, and writes the swap into a NAT table. When the reply comes back to that public port, the router looks it up, puts the private address and port back, and forwards the packet inside. Because ports are used to tell conversations apart, one public address can serve thousands of devices at once.
A packet from the outside to a port that has no table entry is dropped. A server inside the network can still be reached with a fixed rule called port forwarding: "send everything for public port 8080 to 192.168.1.20:80".
Example
The example builds a small NAT table. Two inside devices use the same source port 5000, and the router gives each conversation its own public port.
PUBLIC = "203.0.113.5"
mapping = {} # (private ip, private port) -> public port
reverse = {} # public port -> (private ip, private port)
next_port = [40000]
def outgoing(ip, port):
if (ip, port) not in mapping:
mapping[(ip, port)] = next_port[0]
reverse[next_port[0]] = (ip, port)
next_port[0] += 1
return PUBLIC + ":" + str(mapping[(ip, port)])
for ip, port in [("192.168.1.10", 5000), ("192.168.1.11", 5000), ("192.168.1.10", 5000), ("192.168.1.10", 5001)]:
print(ip + ":" + str(port), "->", outgoing(ip, port))
for public_port in [40001, 40009]:
if public_port in reverse:
ip, port = reverse[public_port]
print("reply to port", public_port, "goes to", ip + ":" + str(port))
else:
print("reply to port", public_port, "dropped: no mapping")Line by line
PUBLICis the router's one public address.mappinggoes from an inside (address, port) to a public port;reversegoes the other way, for replies.outgoingcreates a mapping the first time it sees an inside address and port, using the next free public port starting at 40000, and reuses it after that.- The first loop sends four packets. The third repeats the first, so it gets the same public port, while the others get new ones.
- The second loop handles replies: port 40001 is in the table and is sent to the inside device; port 40009 is not, so it is dropped.
Expected result
192.168.1.10:5000 -> 203.0.113.5:40000
192.168.1.11:5000 -> 203.0.113.5:40001
192.168.1.10:5000 -> 203.0.113.5:40000
192.168.1.10:5001 -> 203.0.113.5:40002
reply to port 40001 goes to 192.168.1.11:5000
reply to port 40009 dropped: no mapping
In the real world
Almost every home network runs both: the router is the DHCP server for the inside and the NAT box towards the provider. Mobile providers go further with carrier-grade NAT, where thousands of customers share one address, which is why hosting a game server from a phone is hard. NAT also explains why peer-to-peer programs, video calls and games use extra helper servers to punch through it. IPv6 has enough addresses for every device, so it needs no NAT, though home routers still block unsolicited inbound traffic with a firewall. When two DHCP servers run on one network by mistake, devices get conflicting settings, a common office fault.
Common mistakes
- Treating NAT as a security feature. It drops unsolicited inbound packets as a side effect, but a firewall is what is meant to do that.
- Thinking every device keeps its address forever. DHCP addresses are leases; use a reservation or a static address for servers and printers.
- Forgetting that the port is rewritten too. NAT changes address and port; two devices with the same source port still get different public ports.
- Running two DHCP servers on the same network without splitting their ranges.
Best practices
- Give servers and printers fixed addresses (or DHCP reservations outside the normal pool).
- Choose a pool and a lease time that suit the network: short leases for guest Wi-Fi, longer for offices.
- Open only the ports you really need with port forwarding, and keep that service updated.
- Prefer IPv6 where it is available, so devices can be reached without translation.
Summary
DHCP gives a joining device an address, mask, gateway, DNS server and lease through Discover, Offer, Request and Acknowledge. NAT lets many private devices share one public address by rewriting addresses and ports and remembering the swap in a table; unknown inbound packets are dropped.
Quick check
Key takeaways
- DHCP gives a device an address, mask, gateway, DNS server and a lease, using four messages: Discover, Offer, Request, Acknowledge.
- A lease must be renewed; an expired address can be given to someone else.
- NAT rewrites the source address and port of outgoing packets and keeps a table to undo it for the replies.
- Inbound packets with no table entry are dropped, so NAT hides inside devices but is not a real firewall.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.