TCP Message Format
TCP (transmission Control Protocol Transmission Protocol) is a connection-oriented, reliable, byte-stream-based Transport layer communication protocol.
The header of a TCP message segment has 10 required fields and an optional field. The header is at least 20 bytes. The data after the header is optional.
1) Source Port (16-bit)
Identifies the computer port or process that sent the message. A TCP message segment must include the source port number so that the destination host knows where to send the acknowledgement message.
2) Destination Port (16-bit)
The port or process that identifies the destination host on which the message is received.
3) serial number (also called serial number) (32-bit)
Used to identify each segment of a message so that the destination host can confirm that it has received the data from the specified message segment. When the source host is used to send a message to multiple message segments, the sequence number can be ordered by the destination host, even if the packets arrive at the destination host in a different order.
the SYN flag is not When set, this field indicates the ordinal of the first byte in the user's data area , and when the SYN flag is placed, the field indicates the sequence number that was initially sent.
In the first message segment that is sent when the connection is established, both parties provide an initial sequence number. The TCP standard recommends using a counter value that increments 1 at 4ms intervals as the value of this initial sequence number. Use counters to prevent the connection from closing and then reconnecting when the same serial number occurs.
For those segments that contain data, the number of the first data byte in the segment is the initial sequence number, followed by the data bytes sequentially. If the source host sends another segment using the same connection, the sequence number of this segment is equal to the sum of the number of data bytes in the previous message segment. For example , suppose the source host sends 3 segments, each segment has 100 bytes of data, and the first segment of the sequence number is 1000, then the second segment of the sequence number is 1100 (1000 + 100), the third segment of the sequence number is 1200 (1100 + 10 0).
If the serial number is increased to the maximum, it will be reset to 0.
4) Confirmation Number (32-bit)
destination host return confirmation number, Causes the source host to know that one or several segments of the message have been received. . The confirmation number equals the sequence number of the last message segment received sequentially plus 1, which is also the ordinal value of the message segment that the destination host expects to receive next. When the confirmation number is returned, the computer considers that it has received all data that is less than the confirmation number.
For example, The serial number equals the sum of the number of data bytes in the previous message segment. For example, assuming that the source host sends 3 segments, each segment has 100 bytes of data, and the first segment has a sequence number of 1000, the destination host returns a header with a confirmation number 1100 after the first segment is received. After receiving the second segment of the message (its ordinal number is 1100), the destination host returns the confirmation number 1200. After receiving the third segment, the destination host returns the confirmation number 1300.
The destination host does not necessarily return a confirmation number after each receiving segment 。 In the example above, the destination host may wait until all 3 message segments have been received, and then return a message segment with confirmation number 1300, indicating that all 1200 bytes of data have been received. However, if the destination host sends back the confirmation number before the wait time is too long, the source host will assume that the data did not reach the destination host, and automatically re-send.
In the above example, if the destination host receives the message segment number 1000 of the first message segment and the message segment number 1200 of the last message segment, you can return to the confirmation number 1100, but before returning to the confirmation number 1300, should wait for the message segment number 1100 of the intermediate message segment.
5) data offset ( first ministerial level ) (4-bit)
The beginning of the data of the TCP segment is far from the beginning of the TCP message segment, which is the first ministerial degree . Because the length of the TCP header varies depending on the contents of the TCP Option field, the header contains a field that specifies the header field. The field is in 32 bits, so the header length must be an integer multiple of 32 bits, sometimes needing to fill 0 at the end of the header. If the header does not have a TCP option field, the header length value is 5, indicating that the header has 160 bits, or 20 bytes.
6) reserved bits (6 bits)
Consists of 6 bits followed by the Data offset field, all 0.
7) Control bit (6-bit)
Emergency URG: This position 1, indicates that the emergency pointer field is valid, it tells the system that there is emergency data in this message section, should be transmitted as soon as possible.
Acknowledgment ack: only if Ack = 1 o'clock Confirmation Number field is valid, TCP specifies that all message segments communicated after the connection is established must have the ACK set to 1.
Push PSH: When two application processes are communicating interactively, sometimes the application process at one end wants to be able to receive a response immediately after typing a command. In this case, TCP can use push operations, at this point, the sender of TCP PSH 1, and immediately create a message segment sent out, the receiver received PSH = 1 of the message segment, as soon as possible (that is "push" forward) to the receiving application process, and no longer wait until the entire cache is filled and then to On delivery.
Reset RST: Used to reset the corresponding TCP connection
Synchronous SYN: Valid only when a TCP connection is established on a three-time handshake. When SYN = 1 and ack = 0 o'clock indicates that this is a connection request message segment, if the other party agrees to establish a connection, it should use SYN = 1 and ACK = 1 in the corresponding message segment. Therefore, the SYN 1 indicates that this is a connection request or a connection acceptance message.
terminate FIN: Used to release a connection. When FIN = 1 o'clock, it indicates that the sender's data for this segment has been sent and that the transport connection is released.
8) window (16-bit)
This field is used for flow control, which is the number of bytes that the native expects to receive at one time, that is, the size of the window that sends the data. Tell the other person how much data can be sent without waiting for confirmation. The maximum length represented here is 2^16-1 = 65535, if you need to use a larger window size, you need to use the window enlargement factor option in the options.
Refers to the receiving window of the party sending this section (not its own sending window).
9) Checksum (16-bit)
The source host and destination host compute checksums based on the contents of the TCP message segment and the pseudo-header. The pseudo-header holds information from the IP header and the length of the TCP segment. Like UDP, the pseudo-header is not transmitted over the network, and the purpose of including the pseudo-header in the checksum is to prevent the destination host from mistakenly receiving the error datagram that exists for the route.
pseudo -header, also known as pseudo-header (Pseudo header): In the TCP fragment or UDP datagram format, in front of the datagram header to increase the source IP address, destination IP address, IP packet Protocol field, TCP or UDP datagram total length, etc. total 12 bytes, The extension header structure that is formed. This pseudo-header is a temporary structure that does not move up or down, just to ensure that the socket is verified to be correct.
Emergency Hands (16-bit)
Meaning only in URG = 1 o'clock, it indicates the number of bytes of emergency data in this section (after the emergency data is finished), indicating the position of the end of the emergency data in the message, and note that emergency data can be sent even when the window is zero.
If the URG is 1, the emergency pointer marks the end of the emergency data. The value is the ordinal of the last 1 bytes of the emergency data, which represents the offset of the message segment ordinal. For example, if the sequence number of the message segment is 1000 and the first 8 bytes are emergency data, then the emergency pointer is 8. The general purpose of the emergency pointer is to enable the user to abort the process.
One) option, fill field
This may include options such as window enlargement factor, timestamp, and so on. The length is variable, up to 40 bytes long, and when no option is used, the TCP header length is 20 bytes.
Padding is used to guarantee that any option is an integer multiple of 32bit.
data (variable length)
The section after the end of the TCP header
UDP Message Format
UDP is the abbreviation of user Datagram Protocol, the Chinese name is the Subscriber datagram protocol, is a kind of non-connected Transport layer protocol, and provides simple and unreliable information transfer service for transaction.
1) Source port (2 bytes): sender-side port number
2) Destination port (2 bytes ): Receiver Port number
3) message length (2 bytes ): The total length of the UDP User datagram, in bytes.
4) Checksum (2 bytes): Detection of UDP User datagram in the transmission is wrong, the error is discarded.
The number field used to validate the UDP datagram and the "pseudo header" that contains the header of the UDP datagram.
pseudo -header, also known as pseudo-header (Pseudo header): In the TCP fragment or UDP datagram format, in front of the datagram header to increase the source IP address, destination IP address, IP packet Protocol field, TCP or UDP datagram total length, etc. total 12 bytes, The extension header structure that is formed. This pseudo-header is a temporary structure that does not move up or down, just to ensure that the socket is verified to be correct.
Linux Network programming--TCP and UDP datagram format detailed