Turn in this assignment by e-mailing to firstname.lastname@example.org 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.
Each address in IPv4 is 32 bits long.
Each routing table entry stores:
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 18.104.22.168 22.214.171.124/126.96.36.199/255.0.0.0/eth0 188.8.131.52/184.108.40.206/255.255.0.0/eth0 220.127.116.11/18.104.22.168/255.255.255.0/eth1 forwarding packet for 22.214.171.124 to next hop 126.96.36.199 over interface eth1
The IP address (188.8.131.52) 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 184.108.40.206, we would have:
$ ./rlookup 220.127.116.11 18.104.22.168/22.214.171.124/255.0.0.0/eth0 126.96.36.199/188.8.131.52/255.255.0.0/eth0 184.108.40.206/220.127.116.11/255.255.255.0/eth1 forwarding packet for 18.104.22.168 to next hop 22.214.171.124 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 126.96.36.199 188.8.131.52/184.108.40.206/255.255.0.0/eth0 220.127.116.11/18.104.22.168/255.0.0.0/eth0 22.214.171.124/126.96.36.199/255.255.255.0/eth1 forwarding packet for 188.8.131.52 to next hop 184.108.40.206 over interface eth0
Default route is used only if there is no better match
$ ./rlookup 220.127.116.11 0.0.0.0/18.104.22.168/0.0.0.0/eth1 22.214.171.124/126.96.36.199/255.255.0.0/eth0 forwarding packet for 188.8.131.52 to next hop 184.108.40.206 over interface eth0 $ ./rlookup 220.127.116.11 0.0.0.0/18.104.22.168/0.0.0.0/eth1 22.214.171.124/126.96.36.199/255.255.0.0/eth0 forwarding packet for 188.8.131.52 to next hop 184.108.40.206 over interface eth1
In case of multiple longest matches, the first one is used
$ ./rlookup 220.127.116.11 18.104.22.168/22.214.171.124/255.255.0.0/eth0 126.96.36.199/188.8.131.52/255.255.0.0/eth1 forwarding packet for 184.108.40.206 to next hop 220.127.116.11 over interface eth0
Sometimes there is no match at all:
$ ./rlookup 18.104.22.168 22.214.171.124/126.96.36.199/255.0.0.0/eth0 188.8.131.52/184.108.40.206/255.255.0.0/eth0 220.127.116.11/18.104.22.168/255.255.255.0/eth1 destination 22.214.171.124 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