Tingkman-tcp-IP Protocol Analysis and Implementation 5-route addition, search, and selection

Source: Internet
Author: User

Tingkman-tcp-IP Protocol Analysis and Implementation 5-route addition, search, and selection

Part of the content of this article may come from the network, the content belongs to the original author, If you infringe your rights and interests, please inform me in time, I will delete immediately, the original content copyleft belongs to the tingkman@163.com, with GPL, You can freely copy and reprint the content. However, please maintain the integrity of the document and indicate the original author and original link. It is strictly prohibited to use the document for any commercial purposes. Welcome to the embedded communication group: 3462094 latest news, please follow Sina Weibo tingkman



In the section on tcp IP protocol analysis and implementation of 4-arp cache tables, we introduced the process of arp cache tables. In this section, we mentioned the addition of arp cache tables, correspondingly, there is a route entry and a route table. As mentioned above, the route table determines that the packet is sent out from that Nic. A simple network protocol stack can implement three route tables. One is the host address and the other is the subnet address, one is that the default route query route table can be queried in this order. First, you can query the route table in the host address route table. If not, you can check the subnet address. If there is no subnet address in the route table, you can find the default route, if no matching entries are found after all the table queries are executed, the packet is discarded and a packet (DestinnationUnreachable) that is not reachable by ICMP is sent back to the sender.

Some protocol stacks may be easier to implement. You can directly find all network devices, such as eth0, eth1, eth2, and eth3, no route table is directly used to compare the destination address of the packet with the IP address of the network device and the subnet mask, and decide to go out of the network device, next let's look at the network protocol code fnet_netif_t * fnet_ip_route (route) {fnet_netif_t * netif; fnet_netif_t * res_netif = route (); // first confirm a default network device such as eth1
For (netif = fnet_netif_list; netif! = 0; netif = netif-> next) {if (dest_ip & netif-> dependencies) = (netif-> ip4_addr.address & netif-> ip4_addr.subnetmask) {res_netif = netif; break ;}}
# Iffnet_assist_loopback/* Anythingsenttooneofthehost 'sownipaddressisenttotheloopbackinterface. */if (dest_ip = res_netif-> ip4_addr.address) // if the packet is sent to you, direct it to the loop_if network device {res_netif = FNET_LOOP_IF ;} # endif/* DIF */returnres_netif;} Here is an ip address and subnet mask such as eth0, 1, 2, and 3: netif ip4_addr mask: 192.168.0.21 255.255.255.0eth1: 192.168.1.21 255.255.255.0eth2: 192.168.2.21 255.255.255.0
Eth3: 192.168.3.21 255.255.255.0
1. Assume that the destination address (dest_ip) of your message is 192.168.1.90, And if (dest_ip & netif-> ip4_addr.subnetmask) ==( netif-> ip4_addr.address & netif-> latency )) if this condition is true, the message is sent through the network device. The calculation result is eth2. 2. Assume that the destination address (dest_ip) of your message is 192.168.0.21, that is, your own purpose, 2. if the destination address (dest_ip) of your message is 192.168.0.21 and if (dest_ip & netif-> ip4_addr.subnetmask) ==( netif-> ip4_addr.address & netif-> ip4_addr.subnetmask)
You will find the eth0 device first. Here we also make a judgment. If a loopback device is defined, it is a virtual network device, that is, itself # iffnet_assist_loopback/* Anythingsenttooneofthehost 'timeout. */if (dest_ip = res_netif-> ip4_addr.address) // if the packet is sent to you, direct it to the loop_if network device {res_netif = FNET_LOOP_IF ;} # endif/* fnet_assist_loopback */The network device points to itself. res_netif = FNET_LOOP_IF; 3. Assume that your destination Message Address (dest_ip) is 10.10.2.90, through the program, we can see that the network device is the default network device, that is, the default gateway res_netif = fnet_netif_get_default (); that is, eth1, we all know that the ip address for accessing the website is strange, so these are sent to the default network device, that is, the network interface of our computer. If a computer has n network interfaces, it can only be sent to the default one.

This chapter describes how to add, search, and select routes. The core of this chapter is to select routes by comparing IP addresses, subnet masks, and destination addresses. Of course, this is relatively simple. In fact, complex network protocols also include dynamic routing. Today we are talking about static networks. The principle is probably like this. The specific implementation will not be detailed here.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.