TCP/IP datagram format

Source: Internet
Author: User

IP datagram format


 
Figure 1. IP datagram format

The version field is 4 characters long to indicate the IP version of the created datagram. The current IP version is IPv4 and IPv6 is in development. IPv4 is 0100.

Header Length (header length) refers to the number of 32-bit characters in the header, including any options. Because it is a 4-bit field, the header is up to 60 bytes. 15x32/8 = 60 bytes. the IP header is always an integer multiple of 32 bit. the minimum length of the IP datagram header is 20 characters (the IP header without the fill field and IP option field is the most common IP header, which is 20 bytes)
The total length field of the service type ToS (type of service) refers to the length of the entire I p datagram, in bytes. because this field is 16 bits long, the maximum I p datagram is 6 5 5 3 5 bytes (64 KB ). the total length field is necessary in the I p header. Data Length = total length-header length.
The identifier is 16 bits in length.

The flag length is 3 bits for segment control: 0th bits are reserved bits, and 1st bits indicate whether segments can be segments. When the value of this bit is 0, it indicates that the datagram cannot be segmented. If the value is 1, it indicates that the datagram can be segmented. If the value of the 2nd-bit segment is 0, it indicates that the segment is the last segment of the original datagram. If the value is 1, it indicates that there are no more segments.
When the length of the datagram to be sent by a network device is greater than that of the maximum transmission unit (MTU, Max transfer United) of the network, and the 1st-bit flag is set to not be segmented (0, the network device returns an Internet Control Message Protocol ICMP error message to the sender and discards the datagram. Except the last segment, the 2nd bits of the remaining segments are set to 1.

The length of a 13-bit segment offset, used to specify the position of a segment in the original datagram, in 8 bytes.

TTL length is 8 bits, which is used to specify the time that the datagram can be retained on the network.

The protocol field is 8 bits in length. It is used to specify which advanced protocol is used for the message carried in the datagram data zone. ICMP is 1, TCP is 6, and UDP is 17. Protocol Number: rfc790.

Header checksum (16 bits) is only used for IP header checksum.

Source IP address and destination IP address.

Fill field is used to ensure that the option field is filled with at least 32 bits to ensure that the IP header ends with 32 bits.

Segmentation: segmentation refers to the process of breaking a large IP datagram into several smaller datagram segments. When the IP module needs to transmit large data packets through a network with a small MTU, it must be segmented.

// Define the IP header data structure <br/> typedef struct _ iphdr <br/>{< br/> byte ver_len; // version 4-bit, header length 4-bit, the header length is in the unit of 32 bits. <br/> byte type; // type 8 bits <br/> byte length [2]; // The total length, 16 bits, indicates the total length of the packet in bytes. The packet length cannot exceed 65536 bytes. Otherwise, the packet is considered damaged. <br/> byte ID [2]; // indicates the packet, it is used for 16-bit <br/> byte flag_offset [2]; // flag, 3-bit data block offset of 13-bit <br/> byte time; // survival time, 8-bit <br/> byte protocol; // protocol, 8-bit <br/> byte crc_val [2]; // header checksum, 16-bit <br/> byte src_addr [4]; // source address, 32-bit <br/> byte tar_addr [4]; // target address, 32-bit <br/> byte options [4]; // options and fill, 32-bit <br/>} ip_header;

========================================================== ====================================
TCP Message format

The Transport Control Protocol (TCP) interfaces upstream to the user's application process, and downstream to the network layer protocol IP interface. A user application uses TCP (or UDP) to send data over an IP network, and then submits application data to TCP. TCP packages the data into segments and calls the IP module to send each data segment to the target host. The receiver's TCP puts the data in the segment into the receiving buffer, reinstalls the segment as the application data, and then sends the data to the target application process.
Although both TCP and UDP use the same network layer (IP), TCP provides services completely different from UDP to the application layer.
TCP provides a connection-oriented and reliable byte stream service.


Figure 2. TCP datagram format

The source port number (16 bits) identifies the application that initiates the transfer on the host, and the destination port number (16 bits) identifies the application to be delivered on the host. The source and target port numbers are used to find the initiator and receiver application processes. The two values plus the source I p address in the I p header and the destination I P address uniquely determine a t C P connection. An I P address and a port number are also known as a socket) (contains the customer's I p address, client slogan, server I P address, and server port number.) You can uniquely determine the two sides of each t C P connection in the interconnected network. The IP + TCP port uniquely identifies a TCP connection.

TCP identifies the application process at the source and target by using the "Port. The port number can use any number between 0 and 65535. When receiving a service request, the operating system dynamically assigns a port number to the client application. On the server side, each service provides services for users on the "well-known port" (well-know port.

● Sequence number field: 32 bits. Identifies the Data byte stream sent from the TCP source to the TCP target. It indicates the First Data byte in the packet segment.
● Validation number field: 32 bits. The validation number field is valid only when the ACK flag is 1. It contains the next Data byte that the target end expects to receive from the source end.
● Header length field: 4 bits. The number of 32 bits in the header is given. The length of the TCP Header without any option field is 20 bytes; a maximum of 60 bytes of the TCP header can be entered.

Reserved: it consists of 6 digits following the data offset field. The reserved value is usually 0.

● Flag field (u, A, P, R, S, f): 6 bits. The meaning of each bit is as follows:
◆ URG: Urgent Pointer is valid.
◆ Ack: confirm that the serial number is valid.
◆ PSH: the receiver should hand over the packet segment to the application layer as soon as possible.
◆ Rst: rebuild the connection.
◆ SYN: Initiate a connection.
◆ Fin: release a connection.
● Window size field: 16 bits. This field is used for traffic control. The Unit is the number of bytes. This value is the number of bytes that the local machine expects to receive at a time.
● TCP checksum field: 16 bits. The entire TCP packet segment, that is, the TCP header and TCP data, is verified by the target end.
● Emergency pointer field: 16 bits. It is an offset, and the sum of values in the serial number field indicates the sequence number of the last byte of the emergency data.
● Option field: 32 bits. It may include "window expansion factor", "timestamp", and other options.
// Define the TCP Header <br/> typedef struct _ tcphdr <br/> {<br/> byte source_port [2]; // The message sending terminal, 16-bit <br/> byte dest_port [2]; // receiver port number, 16-bit <br/> byte sequence_no [4]; // 32-bit, indicates the number of a byte in which the message end data is located in all data blocks <br/> byte ack_no [4]; // 32 bits, confirmation number, indicates the number of data blocks received by the sender at the receiving end. <br/> byte offset_reser_con [2]; // The data offset is 4 bits, and 6 bits are reserved, the control bit 6 is <br/> byte window [2]; // window 16 bits <br/> byte checksum [2]; // check code, 16-bit <br/> byte urgen_pointer [2]; // 16-bit, emergency Data Pointer <br/> byte options [3]; // select peaceful filling, 32-bit <br/>} tcp_header;

This article is transferred from:

Http://blog.pfan.cn/xman/44383.html

========================================================== ====================================
1. IP datagram format

Http://blog.csdn.net/byxdaz/archive/2007/08/01/1720958.aspx

2. TCP Message format

Http://blog.csdn.net/byxdaz/archive/2007/08/01/1720966.aspx

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.