Loading
0x30Lesson 4 of 7

Choose a route for a packet

Follow next hops and understand longest-prefix matching.

15 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Select the most specific matching route for a destination.

A router consults a forwarding table to choose where to send a packet next. Routes contain destination prefixes and next hops. When multiple routes match, longest-prefix matching chooses the most specific one. A default route (0.0.0.0/0) is the fallback when no more specific prefix matches.

network_demo.py
routes = [("10.1.0.0/16", "router A"), ("10.1.2.0/24", "router B"), ("0.0.0.0/0", "internet gateway")]
print("10.1.2.8 matches /16 and /24")
print("choose the /24 route: router B")
Output
10.1.2.8 matches /16 and /24
choose the /24 route: router B

A route describes a next hop, not necessarily the final destination. Routers decrement the packet’s hop limit as they forward it; this prevents packets from circulating forever in a routing loop.

Key takeaways

  • Select the most specific matching route for a destination.

  • The code examples simulate protocol behavior; they do not send network traffic.

  • Use the layer model to locate where an address, port, or message belongs.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: model networking concepts

Use small Python programs to model packet, address, and protocol behavior. These exercises run locally in your browser and do not send network traffic.

Exercise 1

Select the most specific route

+25 XP

For destination 10.1.2.8, choose between a /16, a /24, and a default route. Print the next hop from the most specific matching route.

  • Specific local route
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: