Inside the table
A router joins several networks. For every packet it reads its routing table: a list of destinations written as prefixes. Each row says where matching packets go next: out of which port (socket) and to which next-hop router.
Connected rows are networks plugged straight into a port. Static rows were typed in by a person. Learned rows came from other routers through OSPF, a routing protocol. If two sources offer the same prefix, the lower administrative distance (a trust score) wins.
10.1.0.0/16 means the first 16 bits of an address must equal those of 10.1.0.0. A bigger number after the slash means more bits must match: a smaller, more specific group. 0.0.0.0/0 needs no matching bits, so it matches everything.
Longest prefix match
A packet for 10.1.2.7 arrives. The router checks every row: it matches /0, /8, /16 and /24. When several rows match, the longest prefix wins, because it is the most specific. Winner: 10.1.2.0/24.
10.1.2.0/24 is connected to port 1, so there is no next hop: the router delivers the packet straight to 10.1.2.7 on that LAN, after finding its MAC address with ARP.
Next, 10.1.9.9. It matches /0, /8 and /16, but not 10.1.2.0/24: its third number is 9, not 2. The longest match is 10.1.0.0/16.
The /16 row was learned from router B: next hop 172.16.0.2, out of port 2. Router A hands the packet over, and router B looks it up again in its own table.
10.200.0.1 matches /0 and /8 only: its second number is 200, not 1, so the /16 and /24 rows fail. The longest match is 10.0.0.0/8.
The /8 row is a static route a person typed in: next hop 172.16.0.6, out of port 3. Router C will look the packet up again in its own table.
203.0.113.50 is in none of the 10 networks and not on the office LAN. Only 0.0.0.0/0 matches: the default route, the catch-all used when nothing more specific fits.
The default route sends it out of port 4 to the ISP's router, 198.51.100.2. Every lookup without a better match lands here. A router with no default route would drop the packet.
The lookup, row by row
;; step 1/11 · routing-table Router A · routing table prefix next hop port source AD/metric 0.0.0.0/0 198.51.100.2 port 4 static 1/0 10.0.0.0/8 172.16.0.6 port 3 static 1/0 10.1.0.0/16 172.16.0.2 port 2 learned (OSPF) 110/20 10.1.2.0/24 - port 1 connected 0/0 192.168.1.0/24 - port 0 connected 0/0 "connected" rows have no next hop: the destination is on that port's own network. + three connected /30 link rows (172.16.0.0/30, 172.16.0.4/30, 198.51.100.0/30), not drawn
A real table also lists the three small /30 link networks to routers B and C and to the ISP as connected rows. The figure leaves them off the card; the lookup still checks them, and none of the four packets matches them. Administrative distance numbers are vendor defaults (Cisco: connected 0, static 1, OSPF 110). They only choose between routes for the same prefix; a longer prefix always beats a shorter one.