Show contents
Chapter 4 of 7 · 20 min
Routing
By the end of this chapter, you'll be able to
- Explain the role of a router and of the default gateway
- Read a routing table
- Apply the longest prefix rule
- Write a small router in Python that chooses the right route
Same network or not?
Before sending a packet, a machine asks a single question: is the destination in my network?
- yes: it sends the packet directly to the machine, on the local network;
- no: it sends it to its default gateway, the router of its network, which will take care of the rest.
import ipaddress
my_interface = ipaddress.ip_interface("192.168.1.25/24")
gateway = ipaddress.ip_address("192.168.1.1")
for destination in ["192.168.1.40", "192.168.2.40", "8.8.8.8"]:
ip = ipaddress.ip_address(destination)
if ip in my_interface.network:
print(destination, "→ sent directly on the local network")
else:
print(destination, "→ sent to the gateway", gateway)
192.168.1.40 → sent directly on the local network
192.168.2.40 → sent to the gateway 192.168.1.1
8.8.8.8 → sent to the gateway 192.168.1.1
The routing table
A router connects several networks. For each packet, it checks its routing table: a list of lines saying "to reach this network, send to this next hop, through this interface".
Destination Next hop Interface
10.0.1.0/24 direct eth1 (students' room)
10.0.2.0/24 direct eth2 (administration)
10.0.0.0/16 10.0.255.2 eth3 (other building)
0.0.0.0/0 196.200.60.1 wan0 (default route: Internet)
The last line, 0.0.0.0/0, matches all addresses: it is the default route, used when no other route fits.
The longest prefix rule
A destination can match several lines. 10.0.1.30 is in 10.0.1.0/24, in 10.0.0.0/16 and in 0.0.0.0/0. The router then chooses the most precise route, the one with the longest prefix (longest prefix match).
The longest prefix is /24: the packet goes directly out on eth1, to the students' room.
Text description of the animation
- A packet arrives for 10.0.1.30.
- 10.0.1.30 is in 10.0.1.0/24: it is a candidate.
- 10.0.1.30 is not in 10.0.2.0/24: discarded.
- 10.0.1.30 is in 10.0.0.0/16: another candidate, less precise.
- The default route always matches.
- The longest prefix is /24: the packet goes directly out on eth1, to the students' room.
In Python, this router fits in a few lines:
import ipaddress
table = [
("10.0.1.0/24", "direct via eth1"),
("10.0.2.0/24", "direct via eth2"),
("10.0.0.0/16", "10.0.255.2 via eth3"),
("0.0.0.0/0", "196.200.60.1 via wan0"),
]
routes = [(ipaddress.ip_network(r), hop) for r, hop in table]
def route(destination):
ip = ipaddress.ip_address(destination)
candidates = [(network, hop) for network, hop in routes if ip in network]
network, hop = max(candidates, key=lambda c: c[0].prefixlen)
return f"{destination} → {hop} (route {network})"
for destination in ["10.0.1.30", "10.0.2.8", "10.0.7.1", "41.73.10.5"]:
print(route(destination))
10.0.1.30 → direct via eth1 (route 10.0.1.0/24)
10.0.2.8 → direct via eth2 (route 10.0.2.0/24)
10.0.7.1 → 10.0.255.2 via eth3 (route 10.0.0.0/16)
41.73.10.5 → 196.200.60.1 via wan0 (route 0.0.0.0/0)
max(..., key=...) keeps the candidate whose prefix length is the largest.
How the table gets filled
- Directly connected networks appear automatically as soon as an interface gets an address.
- Static routes are written by hand by the administrator: simple and predictable, suited to small networks.
- Dynamic routing protocols (OSPF in a company, BGP between operators on the Internet) make routers exchange their tables, so they adapt on their own when a link goes down.
Mini-lab: complete the tablePython 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: the campus router
A router's table is the following. For each of the destinations 172.16.5.9, 172.16.9.1, 172.17.0.4 and 105.235.0.1, give the chosen route, then check with Python.
172.16.0.0/16 → main building
172.16.5.0/24 → computer lab
172.16.8.0/22 → student residence
0.0.0.0/0 → Internet
Hint 1
172.16.8.0/22 covers 4 /24 networks: from 172.16.8.0 to 172.16.11.255.Hint 2
172.17.0.4 is not in 172.16.0.0/16: only a second byte of 16 belongs to it.Show the solution and common mistakes
import ipaddress
routes = [(ipaddress.ip_network(r), h) for r, h in [
("172.16.0.0/16", "main building"),
("172.16.5.0/24", "computer lab"),
("172.16.8.0/22", "student residence"),
("0.0.0.0/0", "Internet"),
]]
for destination in ["172.16.5.9", "172.16.9.1", "172.17.0.4", "105.235.0.1"]:
ip = ipaddress.ip_address(destination)
network, hop = max((r for r in routes if ip in r[0]), key=lambda r: r[0].prefixlen)
print(destination, "→", hop)
172.16.5.9 → computer lab
172.16.9.1 → student residence
172.17.0.4 → Internet
105.235.0.1 → Internet
Common mistakes:
- Sending
172.16.9.1to the main building: the residence's/22also matches and it is more precise than the/16. - Believing
172.17.0.4belongs to the campus because it "looks like" 172.16: only the computation with the mask counts.
Summary
- 1A machine sends packets for its own network directly, and the others to its default gateway.
- 2A routing table maps destination networks to a next hop and an interface.
- 3When several routes match, the router chooses the longest prefix.
- 4The route 0.0.0.0/0 is the default route: it matches all addresses.
- 5Routes come from connected networks, static configuration or protocols such as OSPF and BGP.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.