Detailed Explanation of TCP/IP: UDP with Best Efforts (Continuously Updated)

TCP/IP detailed explanation of UDP with all its strength

1. What is UDP?
UDP is a simple datagram-oriented transport layer protocol that preserves message boundaries. It does not provide error correction, queue management, deduplication, flow control, and congestion control (these are actually characteristics of TCP). However, it provides error detection, which is actually an end-to-end checksum in the transport layer. Since this protocol itself provides very few functions, the application using it needs to do a lot of control work on how the data packets are sent and processed. In short, it is only responsible for delivering data and does not guarantee that the data can reach the destination. .
Although these characteristics of it will make it not as reliable as a good man, BUT! It still has its advantages~
First of all, it uses less overhead than other transport protocols; second, broadcast and multicast operations directly use connectionless transport like UDP; finally, applications The ability to select one's own retransmission unit is an important consideration.

2. UDP header
Encapsulation of UDP datagram as a single IPv4 datagram is similar to IPv6, with only some differences in details, which will be discussed later.
A UDP datagram is composed of a UDP header and UDP data. The length of the header is usually 8 bytes, which are source port number, destination port number, length, and checksum. The four data all occupy 2 bytes.
UDP header

Port number : equivalent to a mailbox, it helps the protocol to identify the sending and receiving process. The source port number is optional, if the sender of the data packet does not require the other party to reply, it can be set to 0. Transport protocols use destination ports to help separate incoming data from the IP layer. Because the IP layer separates incoming IP datagrams to specific transport protocols based on the value of the protocol field in the IPv4 header or the value of the next header field in the IPv6 header, it means that the port number is different between different transport protocols Separately, TCP port numbers can only be used by TCP, and UDP port numbers can only be used by UDP. So it is possible for two completely different servers to use the same port number and IP address, as long as they use different transport protocols.
The checksum field is end-to-end and is calculated from the UDP pseudo-header containing the source address and destination address fields in the IP header, so any modification of these fields requires modification of the UDP checksum ( such as NAT).

UDP length is the total length of UDP header and data, the minimum value is 8, unless UDP with IPv6 oversized datagram is used. Sending a UDP datagram with 0 bytes of data is allowed, which means that the UDP length field is redundant, because the IPv4 header contains the total length of the datagram, that is, the length of a UDP/IPv4 datagram is equal to The total length of the IPv4 packet minus the length of the IPv4 header. The value of the UDP Length field should be consistent with the length calculated from the information provided by the IP layer.

The UDP checksum covers the UDP header, data, and a pseudo-header, while the checksum in the IPv4 header only covers the entire header, but does not cover any data in the IP packet. Calculate the checksum (why? Because the value of the TTL field will be reduced by the router during the datagram forwarding process). For UDP, the checksum is optional, but when used in IPv6, the calculation and use of the checksum is mandatory, because there is no header checksum at the IP layer.

Notice! ! ! ! Here comes the details! ! ! ! !

①The UDP datagram length can be an odd number of bytes, but the checksum algorithm only adds 16-bit words (even number of bytes), how to deal with it? The processing process of UDP is to append a padding (dummy) byte with a value of 0 at the end of the odd-length datagram, which is only for the calculation and verification of the checksum, and will not actually be transmitted.
② UDP includes (only) a 12-byte pseudo-header derived from the field of the IPv4 header or a 40-byte pseudo-header derived from the field of the IPv6 header when calculating its checksum, the pseudo-header Also virtual, same as "0" above. Used only for checksum calculations.
Fields used during UDP checksum calculation
3. Reassembly timeout
When any fragment of a datagram arrives first, the IP layer will start a timer. The timeout period is 30s or 60s. It will not be reset when a new fragment is received. If it times out, all fragments will be discarded. fragment, and reply with an ICMPv4 timeout (code 1) message, telling the sender that the datagram was lost, including a copy of the first fragment.

Guess you like

Origin blog.csdn.net/weixin_37778713/article/details/116948094