In Unix operating system, there is a routing table which contains a certain number of tuples. These tuples are consist of network IP, subnet mask, gateway IP and interface name. These details are used to forward a packet to the outside of its network to connect to the internet. So this article gives an idea of how the system takes the decision when a packet is needed to be forwarded.
Examples:
Input: 201.2.2.2 Output: 12.23.44.1 eth9 Here, there is no network IP entry in the routing table which starts with "201". In this case it will choose default path(0.0.0.0, 0.0.0.0, 12.23.44.1, eth9). Still, it will perform bitwise AND operation with each entry and then chooses default path's interface and gateway to send packet outside. Default path means interface to which system is directly connected. Input: 200.200.200.1 Output: 190.164.1.2 eth0 Here bitwise AND operation is performed with each entry of routing table and correspondent network's interface name and gateway IP is returned.
How forwarding of packet works?
This can be understood easily with the help of a small example:
- Suppose there is a packet with IP address “20.129.0.1” and the routing table has following entries:
Network’s IP Address Subnet Mask Gateway’s IP Address Interface Name 200.200.16.0 255.255.248.0 192.13.2.55 eth4 200.200.200.0 255.255.248.0 192.13.2.55 eth4 0.0.0.0 0.0.0.0 12.23.44.1 eth9 20.128.0.0 255.128.0.0 12.1.1.1 eth1 20.0.0.0 255.0.0.0 12.1.1.1 eth2 20.0.0.0 255.128.0.0 12.1.1.1 eth3 - So when packet goes to the kernel of the system to find gateway and interface, it will first perform bitwise AND operation with subnet mask of each entry to find Longest prefix match.
- The result of the bitwise AND operation is then compared with the network’s IP address. So it will return the correspondent IP address of gateway and interface’s name through which packet can go out.
- Binary representation of 20.129.0.1 is 00010100.10000001.00000000.00000001. It then performs bitwise AND operation with subnet mask for each and every entry of routing table.
- In this table that entry number is 4( i.e. 20.128.0.0, 255.128.0.0, 12.1.1.1, eth1) which gives the longest prefix match for this packet. So packet will go out from eth1 interface and chooses the gateway 12.1.1.1 for forwarding.
Below is the implementation of the above approach using Linked List data structure. It takes 2 files as an input and returns an output in another file mentioned above.
Input files and Output files
- File “input.txt” represents the IP address of a packet.
- File “routing.txt” contains routing table entries to which IP address is going to be matched.
- File “output.txt” contains outputs for each input.
Program:
// C code to implement IP forwarding table lookup #include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define M 15 #define N 150 // Declaration of structure of linked list // to store ip address in each node struct node { char * data; struct node* next; } * head[15]; // This function fetch data from file // and store them into different arrays void storeData( FILE * fp, char buf[M][N], char net[M][N], char mask[M][N], char gateway[M][N], char port[M][N]) { char line[200]; int c, i = 0, j, k = 0, m = 0; // Read data from the file line by line // and each line is stored in array separately. while ( fgets (line, sizeof (line), fp)) { j = 0; for ( int l = 0; l < strlen (line); l++) { buf[i][j] = line[l]; j++; } i++; } // From each lines stored in buf, // network id, subnet mask, gateway // and port are extracted // and stored into individual arrays. for (i = 0; i < 15; i++) { k = 0; for (j = 0; buf[i][j] != ',' ; j++) { net[i][k] = buf[i][j]; k++; } m = j + 2; k = 0; for (j = m; buf[i][j] != ',' ; j++) { mask[i][k] = buf[i][j]; k++; } m = j + 2; k = 0; for (j = m; buf[i][j] != ',' ; j++) { gateway[i][k] = buf[i][j]; k++; } m = j + 2; k = 0; for (j = m; buf[i][j] != ' |