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.
Input: 188.8.131.52 Output: 184.108.40.206 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, 220.127.116.11, 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: 18.104.22.168 Output: 22.214.171.124 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 “126.96.36.199” and the routing table has following entries:
Network’s IP Address Subnet Mask Gateway’s IP Address Interface Name 188.8.131.52 255.255.248.0 184.108.40.206 eth4 220.127.116.11 255.255.248.0 18.104.22.168 eth4 0.0.0.0 0.0.0.0 22.214.171.124 eth9 126.96.36.199 255.128.0.0 188.8.131.52 eth1 184.108.40.206 255.0.0.0 220.127.116.11 eth2 18.104.22.168 255.128.0.0 22.214.171.124 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 126.96.36.199 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. 188.8.131.52, 255.128.0.0, 184.108.40.206, eth1) which gives the longest prefix match for this packet. So packet will go out from eth1 interface and chooses the gateway 220.127.116.11 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.
- 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.
- Difference between Fact Table and Dimension Table
- C Program for Program to find area of a circle
- C Program for Program for array rotation
- Lex Program to remove comments from C program
- How does a C program executes?
- C Program to Add two Integers
- C Program for Cutting a Rod | DP-13
- C Program for ID3 Tagging
- Program to print Hut
- C Program for factorial of a number
- C/C++ Program for Linear Search
- Program to print the Cot Bed Pattern
- C program for file Transfer using UDP
- C Program to find whether a no is power of two
- C program for pipe in Linux
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.