Lesson 12 of 15

Routing Tables

Routing Tables

A routing table determines where network packets should be forwarded. Each entry maps a destination network to a next hop (gateway) and interface.

Routing Table Structure

Destination      Netmask           Gateway         Interface
192.168.1.0      255.255.255.0     0.0.0.0         eth0
10.0.0.0         255.0.0.0         192.168.1.1     eth0
0.0.0.0          0.0.0.0           192.168.1.1     eth0    (default)

Longest Prefix Match

When multiple routes match a destination, the router uses the longest prefix match — the route with the most specific (longest) subnet mask wins.

For example, if a packet is destined for 10.1.2.3:

  • 10.0.0.0/8 matches (prefix length 8)
  • 10.1.0.0/16 matches (prefix length 16) -- this wins
  • 0.0.0.0/0 matches (prefix length 0)

Your Task

Implement a RoutingTable class:

  • addRoute(network, prefix, gateway, iface) — adds a route (e.g., "192.168.1.0", 24, "0.0.0.0", "eth0")
  • lookup(destIP) — finds the best matching route using longest prefix match, returns { gateway, iface } or null
Node.js loading...
Loading...
Click "Run" to execute your code.