![]() |
VOOZH | about |
In Unix operating system, there is a routing table that 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 the packet works?
This can be understood easily with the help of a small example:
| 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 |
Below is the implementation of the above approach using the Linked List data structure. It takes 2 files as an input and returns output in another file mentioned above.
Program: