ICS 451 Assignment 8: IP Routing Table Lookup

Assigned March 10th, to be completed by March 17th.

Turn in this assignment by e-mailing to jmoroney@hawaii.edu your code specified below.

You may do this assignment in groups of up to two.

This assignment asks you to implement the kind of IP routing table lookup found in a router or a host. This lookup is used as part of the forwarding process (which you do not have to implement). The routing tables for this assignment are static and specified in the command-line arguments, so you do not need to implement any routing protocol, or indeed, even a routing table.

Routing Table Lookup

Each address in IPv4 is 32 bits long.

Each routing table entry stores:

  1. a Destination, which is an IP address.
  2. a Gateway or Next Hop, which is also an IP address.
  3. a mask, which is a 32-bit string, but not an IP address.
  4. various flags, which for this assignment may be ignored
  5. a metric, which for this assignment may be ignored
  6. an interface, which is an arbitrary string

Most routers use automated methods such as routing protocols or DHCP to build their routing table. For this assignment, the routing table will be specified as a series of command-line arguments, each of the form:


Each of the destination, gateway, and mask are given in the numbers-and-dots notation supported by inet_aton. The interface is an arbitrary string.

Each such argument is used to build a routing table entry, in the order given on the command line.

To search the routing table, IP uses an algorithm called longest match. Of all the matching routing table entries, the one with the longest mask is used. If there are multiple matching entries with masks of the same length, the first one is used.

All the '1' bits in a valid mask are to the left of all the '0' bits, so the longest mask is also the numerically largest mask when comparing unsigned 32-bit integers.

A route matches an IP address when its destination ANDed with the mask is the same as the IP address ANDed with the mask, or

   (route[i].dest & route[i].mask) == (ip->dest & route[i].mask)

Both the all-zeros mask and the all-ones mask are legal. The first is a default route, used if no other route matches. The second is a host route, used only if the destination of the packet is the same as the destination in the routing table.

The very first parameter is an IP destination address to match to the routing table.


   $ ./rlookup
   forwarding packet for to next hop over interface eth1

The IP address ( matches all three of the routes that are given, and so the one with the longest match, which is the third one, is used. If the packet were sent to, we would have:

   $ ./rlookup
   forwarding packet for to next hop over interface eth0

Here, only the first two routes matched, so the second (longer of the two) routes was chosen.

Order does not matter if there is a single longest route:

   $ ./rlookup
   forwarding packet for to next hop over interface eth0

Default route is used only if there is no better match

   $ ./rlookup 
   forwarding packet for to next hop over interface eth0
   $ ./rlookup 
   forwarding packet for to next hop over interface eth1

In case of multiple longest matches, the first one is used

   $ ./rlookup 
   forwarding packet for to next hop over interface eth0

Sometimes there is no match at all:

   $ ./rlookup
   destination not found in routing table, dropping packet


Please follow the examples above in reporting the results of your routing table lookup. This makes it easier for the TA to grade a large number of assignments.

Turn in your source code for rlookup.


This assignment does not require you to build and populate a routing table. However, you are welcome to do so if you wish.

Netmasks are specified in one of two ways: as described above (in dotted-decimal notation), or with a / followed by the number of leading '1' bits. For example, the default route is /0, and a host route would be /32. If you wish, you may implement code to check whether the third part of a table entry contains the '.' character (this is probably a little harder than you may think), and if not (and the number is between 0 and 32), use the corresponding number as a number of 1 bits to be put into a mask (this may also be a little harder than you think).

If you do this, be sure that your code works correctly in the examples as given above. Your grade in the assignment will only depend on those.

Computer Networks, ICS 451
Instructor: Edo Biagioni