TCP/IP basics for Linux Network Programming (III): IP datagram format and IP address Routing

Source: Internet
Author: User

I. IP datagram format

The IP datagram format is as follows:


Version
The IP protocol version number. The length is 4 bits, the IPv4 field value is 4, and the IPv6 field value is 6.


Header Length
In 32-bit characters, this field is 4 characters in length and the minimum value is 5, that is, the first 20 bytes of the IP address without any options; the maximum value is 15, therefore, the header length is up to 60 bytes.


Service type (ToS)
The length is 8 bits. This field contains three-bit priority (ignored now), four-bit service type subfields, and one-bit reserved bits (must be set to 0 ). The four-bit service types are minimum latency (D), maximum throughput (t), maximum reliability (R), and minimum cost (f), for example.



Total Length
The length of this field is 16 bits in bytes. The length of this field contains the IP header and the data part (payload ). An IP datagram can contain up to 65535 bytes.


Identifier
A 16-bit identifier is used to identify an IP datagram. Each time this value is sent, 1 is added. It can be used to partition and reassemble data packets.


Offset between logo and slice

The first digit is not used. Every two DF (don't fragment) bits are 1. If the transmitted datagram exceeds the maximum transmission unit, the datagram is discarded, and sends an ICMP error message. The third-digit MF (more fragment) indicates whether more slices exist. The value of this parameter is 1, indicating that more fragments exist in the future. The last mf is 0.
After an IP packet is split, each datagram has its own header, but the part offset value is different. You can re-assemble the IP packet through the receiver of the part offset value.


TTL
TTL (time to live) indicates the maximum number of routers that a datagram can pass. Each time a datagram passes through a vro, the TTL is reduced by 1, and 0 is dropped, and an ICMP message is sent to notify the source host. TTL can avoid continuous circulation of data between routers.


Protocol Type
Indicates which advanced protocol is hosted on the IP layer. In the process of encapsulation and sub-use, the Protocol Stack knows which layer the protocol should be handed over. 1 ICMP 2 IGMP 6 TCP 17udp


Header checksum
Ensure the data integrity of the datagram header, but the verification does not include the data part. The purpose of this solution is as follows: first, all high-level protocols that encapsulate data in IP data packets contain a checksum that overwrites the entire data, therefore, it is not necessary to verify the data part carried by the IP datagram. Second, each time a router passes through, the IP datagram header must be changed (such as TTL), while the data part remains unchanged. In this way, only the changed header is verified, obviously, it will not waste too much time. In order to reduce the computing time, CRC verification code is generally not used, but a simpler Internet checksum (Internet
Checksum ).


Source IP Address
Host IP address for sending data


Destination IP address
Host IP address for receiving data


Option and fill (the option is 4-byte integer multiple, otherwise fill with 0)
Security and Handling Restrictions
Path record: record the IP address of the router
Timestamp: record the IP address and time of the router
Kuan song Origin Site route: Specifies the IP address that a data packet must go through. It can go through an unspecified IP address.
Strict origin route: Specifies the IP address that a data packet must go through. It cannot go through an unspecified IP address.


Ii. IP addresses and routes

The IPv4 IP address is 4 bytes in length. It is usually expressed as 192.168.0.2 in dot-decimal notation (dotted decimal representation), for example, 0xc0a80002. The Internet is separated by various routers and gateway devices into many CIDR blocks. To identify different CIDR blocks, you need to divide the 32-bit IP addresses into two parts: the network number and the host number, each host with the same network number is located in the same network segment and can communicate with each other directly. The communication between hosts with different network numbers needs to be forwarded through the router.

Assume that the network interface configuration and route table on a host are as follows:


This host only has one network interface connected to the 192.168.232.0/24 network. The destination of the route table is the destination network address, genmask is the subnet mask, Gateway is the next hop address, iface is the sending interface, and the U mark in flags indicates that the entry is valid (some entries can be disabled ), the G flag indicates that the next hop address of this route entry is the address of a vro, And the entry without the G flag indicates that the destination network address is a network directly connected to the local interface and does not need to be forwarded by the vro, therefore, the next hop address is marked.
If the destination address of the packet to be sent is 192.168.232.1 and the subnet mask of the third row is calculated to obtain 192.168.232.0, which is the destination network address of the third row. Therefore, the packet is sent from the eth0 interface, because 192.168.232.0/24 is a network directly connected to the eth0 interface, it can be directly sent to the target host without forwarding through the router.
If the destination address of the packet to be sent is 202.10.1.2 and does not match the route table entries of the last two lines, send the packet from the eth0 interface according to the default route entry and first send it to the 192.168.232.2 router, then let the router decide the next hop address based on its route table.


The routing process is as follows. For the ARP part, refer to here:

Refer:

Linux C Programming one-stop learning

Chapter 1 TCP/IP details

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.