Show contents
Chapter 5 of 7 · 20 min
NAT, DHCP and IPv6
By the end of this chapter, you'll be able to
- Explain how NAT shares one public address between several machines
- Describe the four steps of an address assignment by DHCP
- Read and shorten an IPv6 address
- Compare IPv4 and IPv6
NAT: one public address for a whole network
Private addresses (192.168.x.x…) do not travel on the Internet. So the box (or the school's router) performs address translation (NAT, Network Address Translation): when a packet goes out, it replaces the sender's private address with its own public address, and records the mapping in a translation table. When the reply comes back, it checks the table and forwards it to the right machine.
To tell conversations apart, the box also uses ports: a number from 0 to 65,535 that identifies a conversation on a machine. This is often called PAT or NAT overload.
The box finds 40001 in its table and sends the reply back to the phone.
Text description of the animation
- The phone sends a request to a web server (port 443), from its private address.
- The box replaces the sender with its public address and a free port, and records: 40001 ↔ 192.168.1.25:51000.
- The server only sees the box's public address, and replies to it.
- The box finds 40001 in its table and sends the reply back to the phone.
nat_table = {}
next_port = 40001
PUBLIC_ADDRESS = "41.73.10.5"
def outgoing(private_source):
global next_port
nat_table[next_port] = private_source
public = f"{PUBLIC_ADDRESS}:{next_port}"
next_port += 1
return public
def incoming(public_port):
return nat_table.get(public_port, "unknown: packet dropped")
print(outgoing("192.168.1.25:51000"))
print(outgoing("192.168.1.31:51000"))
print(incoming(40001), "|", incoming(40002), "|", incoming(45000))
41.73.10.5:40001
41.73.10.5:40002
192.168.1.25:51000 | 192.168.1.31:51000 | unknown: packet dropped
A useful side effect: a packet arriving from the Internet with no mapping in the table is dropped. Machines in the private network are therefore not directly reachable from outside.
DHCP: an address without configuration
DHCP (Dynamic Host Configuration Protocol) automatically gives every device that joins the network an IP address, the mask, the gateway and the DNS servers. The four steps form the acronym DORA:
1. Discover: the phone shouts to the whole network "Is there a DHCP server?"
2. Offer : the server (often the box) proposes "Take 192.168.1.40"
3. Request : the phone answers "I accept 192.168.1.40"
4. Ack : the server confirms and lends the address for a while (the lease)
The address is only lent: at the end of the lease (often a few hours), the device must renew it. A printer or a server, which everyone must find, rather gets a reservation (always the same address) or a fixed address outside the DHCP range.
IPv6: the end of the shortage
IPv6 uses 128-bit addresses instead of 32: about 3.4 × 10 to the power of 38 addresses, enough to give a public address to every device. An IPv6 address is written as 8 groups of 4 hexadecimal digits (0 to 9 and a to f), separated by :. Two rules shorten it:
- leading zeros in each group are removed (
0db8becomesdb8); - a single run of zero groups is replaced by
::.
import ipaddress
for text in ["2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:db8::ff00:42:8329"]:
ip = ipaddress.ip_address(text)
print(ip.compressed, "|", ip.exploded)
2001:db8::ff00:42:8329 | 2001:0db8:0000:0000:0000:ff00:0042:8329
2001:db8::ff00:42:8329 | 2001:0db8:0000:0000:0000:ff00:0042:8329
Both spellings designate the same address. The prefix 2001:db8::/32 is reserved for documentation, as in this example.
| IPv4 | IPv6 | |
|---|---|---|
| Size | 32 bits | 128 bits |
| Notation | 192.168.1.25 | 2001:db8::ff00:42:8329 |
| Number of addresses | about 4.3 billion | about 3.4 × 10^38 |
| NAT | essential today | in principle unnecessary |
| Automatic configuration | DHCP | SLAAC (automatic) or DHCPv6 |
Mini-lab: shortening IPv6 addressesPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: an Internet café's NAT
An Internet café has the public address 102.164.20.7. Three workstations (192.168.0.11, .12 and .13) each open a connection from their port 50000. Simulate the translation table starting from public port 30000, print the three outgoing translations, then handle a packet coming back on port 30001 and another on port 30009.
Hint 1
{public_port: "private_address:port"} is enough as a table.Hint 2
table.get(port, "dropped") returns a default value when the port is unknown.Show the solution and common mistakes
table = {}
port = 30000
for station in ["192.168.0.11", "192.168.0.12", "192.168.0.13"]:
table[port] = f"{station}:50000"
print(f"{station}:50000 → 102.164.20.7:{port}")
port += 1
for arrival in [30001, 30009]:
print("return on", arrival, "→", table.get(arrival, "dropped (no connection)"))
192.168.0.11:50000 → 102.164.20.7:30000
192.168.0.12:50000 → 102.164.20.7:30001
192.168.0.13:50000 → 102.164.20.7:30002
return on 30001 → 192.168.0.12:50000
return on 30009 → dropped (no connection)
All three stations use the same private port: it is the public port, different for each one, that lets the box tell them apart.
Common mistakes:
- Indexing the table by private address: on the way back, the box only knows the public port, so that is the key.
- Letting the packet for port 30009 through: with no mapping, it answers no connection opened from inside and must be dropped.
Summary
- 1NAT replaces private addresses with the box's public address and records each conversation in a table.
- 2Ports let several machines share the same public address.
- 3DHCP lends an address in four steps: Discover, Offer, Request, Ack.
- 4IPv6 uses 128 bits, written in hexadecimal, and :: shortens only one run of zeros.
- 5With IPv6, every device can have a public address, which in principle makes NAT unnecessary.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.