Linux Network Programming--IP Packet format detailed

Source: Internet
Author: User

IP Datagram Header





The TCP/IP protocol defines a packet that is transmitted over the Internet, called an IP datagram (IP Datagram). This is a hardware-independent virtual package, consisting of a header and a data component.


The first part of the header is a fixed length, a total of 20 bytes, which all IP datagrams must have. The fixed part of the header is followed by some optional fields whose length is variable. Both the source and destination addresses in the capital are IP protocol addresses.


The fields in the fixed section of the IP datagram header :

1) version : 4 bits, refers to the IP protocol version.

The IP protocol versions used by both sides of the communication must be consistent. The IP protocol version number that is widely used recently is 4 (that is, IPv4). IPV6 is still at the start stage.


2) first ministerial degree : 4 digits, the maximum decimal value that can be represented is 15.

Note that this field represents the number of units in 32-bit words (a 32-bit word length is 4 bytes), so when the IP header length is 1111 (that is, the decimal 15), the header length reaches 60 bytes.


When the header length of an IP packet is not an integer multiple of 4 bytes, it must be populated with the last fill field. Therefore, the data part will always start at 4-byte integer multiples, which makes it more convenient to implement the IP protocol. The disadvantage of a header length limit of 60 bytes is sometimes not enough. The purpose of this is to expect users to minimize overhead.


The most commonly used first ministerial level is 20 bytes (that is, the header length is 0101), and no options are used.


3) Services : 8-bit, used to obtain better service, generally do not use.

This field is called the service type in the old standard, but it has not been used in practice. 1998 the IETF renamed this field to differentiate service DS (differentiatedservices). This field only works if you are using differentiated services.


4) Total Length: Total length refers to the length of the capital and the sum of the data , in bytes.

Because the total Length field is 16 bits, the maximum datagram length is 65535 bytes. Each data link layer below the IP layer has its own frame format, which includes the maximum length of the data field in the frame format, which is the maximum Transfer unit MTU (Maximum Transfer unit). When a datagram is encapsulated into a link-layer frame, the total length of the datagram (that is, the header plus the data portion) must not exceed the MTU value of the data link layer below.


5) Identification (identification): 16 bits.

The IP software maintains a counter in memory, each generating a datagram, the counter adds 1, and assigns this value to the identity field. But this "identity" is not an ordinal, because IP is a disconnected service, the datagram does not have an ordered receive problem.


When a datagram must be fragmented because it is longer than the MTU of the network, the value of the identity field is copied to the identity field of all datagrams. The same value of the identity field causes the fragmented datagram to be correctly re-installed as the original datagram.


6) flag: 3 digits, but only 2 is meaningful.

The lowest bit in the Flag field is recorded as MF (more Fragment).

MF = 1 means a datagram with "shards" behind it.

MF = 0 indicates that this is the last of several datagrams.


the one in the middle of the flag field is recorded as DF (Don ' t Fragment), it means "can't shard". Shards are allowed only if DF = 0 o'clock.


7) chip offset : 13 bits. the relative position of a piece in the original group after a long grouping in the Shard .

That is, relative to the starting point of the User data field, where the film starts. The slice offset is offset in 8 bytes. This means that the length of each shard must be an integer multiple of 8 bytes (64 bits).



8) time -to-live: 8-bit, time-to-live fields are commonly used in English abbreviations, which indicate the lifespan of datagrams in the network.

This field is set by the source point at which the datagram is emitted. The aim is to prevent the inability to deliver the datagram in an unrestricted manner on the Internet, thus consuming network resources in vain.


The original design was in seconds as the TTL unit. each time a router is passed, the TTL is subtracted from the datagram when it is consumed by the router. If the datagram consumes less than 1 seconds on the router, the TTL value is reduced by 1. When the TTL value is 0 o'clock, the datagram is discarded.


9) Protocol : 8 bits, the Protocol field indicates what protocol is used for the data that this datagram carries, so that the IP layer of the destination host knows which processing process should be handed over to the data part, usually with ICMP (1), IGMP (2), TCP (6), UDP (17 ), IPV6 (+)


The first test and : accounted for 16. This field only examines the header of the datagram, but does not include the data section .

This is because the datagram has to be recalculated every time a router passes the capital test and (some fields, such as lifetime, flag, slice offset, and so on, can change). Do not test the data section to reduce the amount of computational effort.


Source Address : Sender IP Address


Destination Address : Receiver IP Address


variable part of IP datagram header :

The variable part of the IP capital is an optional field. Option fields are used to support troubleshooting, measurement, and security measures, and are rich in content. This field is variable in length, ranging from 1 bytes to 40 bytes, depending on the item selected.


Some options require only 1 bytes, and it includes only 1-byte option codes. However, there are several options that require multiple bytes, which are stitched together, with no delimiter in the middle, and then filled with a full 0 fill field to be an integer multiple of 4 bytes. Increasing the variable part of the capital is to increase the functionality of the IP datagram, but it also makes the header length of the IP datagram variable. This increases the overhead of each router handling datagrams.

in fact, these options are rarely used . The new IPv6 makes the header length of the IP datagram fixed.


Transferred from: http://blog.163.com/

Linux Network Programming--IP Packet format detailed

Related Article

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.