Comprehensive Analysis of UDP

Source: Internet
Author: User

Comprehensive Analysis of UDP
How to calculate UDP check and checksum 1. The following figure shows a UDP test and all the information required, including three parts:
1. UDP pseudo Header
2. UDP Header
3. UDP data part (do not omit this part; otherwise ~ Vomiting Blood ~)



First, explain the concept of the pseudo-header. the pseudo-header contains some IP header fields. The purpose is to allow UDP to check whether the data has arrived at the destination correctly for verification purposes only.
Another concept is very important, that is, the total length of 16-bit UDP. Please note that this length is not the total length of the message, but only UDP (including the UDP header and data) the total length (because this concept was not clear before, it took a lot of detours to vomit blood ~~).

2. The checksum process is critical. It involves the following steps:
1. Add the pseudo header to UDP;
2. During initial calculation, the test and field must be added to zero;
3. Divide all characters into 16 characters (2 bytes ).

4. add all 16-bit characters. In case of carry, add the value of the carry part higher than 16 bytes to the carry part. For example, 0xBB5E + 0 xFCED = 0x1 B84B, put 1 in the forward position. The result is 0xB84C.
5. The result of adding all words should be a 16-bit number. If this number is reversed, we can get the test and checksum.

3. The answer is better than the argument. Let's take an example to analyze it. This example calculates a TCP test (consistent with the UDP algorithm)
The TCP computing test and packet structure are as follows:


The packet capture tool captures a TCP syn packet for research. The following is the entire packet:



1. Add the test part to zero first;
2. Divide the TCP pseudo-header, TCP header, and data into 16 hexadecimal numbers;
3. Add these numbers one by one. Remember to add the overflow part to the padding bit. This is a cyclic addition:
0xc0a8 + 0x0166 + ...... + 0x0402 = 0x9b49
4. Check the result and obtain the value 0x64B6.

After performing the above calculation, we can get the test code 0x64B6. You can also try IP datagram to check only the IP datagram header, But UDP checks both the header and the data part.
The IP address segment (MTU) is related to the MTU of the network. let's take a look at MTU: MTU (Maximum Transmission Unit) is the largest packet transmitted over the network. the unit of MTU is byte. for example, Ethernet can only send 1500 bytes in a frame, while a typical MTU of 16 MB/EN en Ring is 17914 bytes in each frame. RFC791 specifies that MTU consists of a maximum of 65535 bytes and a minimum of 68 bytes. when the IP layer receives an IP packet to be sent, if the packet is larger than the local MTU of the sending system, the system must divide the packet into multiple slices (fragmentation) for sending. during packet transmission, if the IP packet is too large to pass through a certain network segment between the sender and the receiver, the router segments the packet so that the packet can pass through the network smoothly. the IP packet After partitioning is reassembled only when it reaches the destination. re-assembly is completed by the IP layer of the target end. The purpose is to make the sharding and re-assembly process transparent to the transport layer (TCP and UDP. already sharded Packets that have been uploaded may be sharded again (more than once ). when IP data is sliced, each segment becomes a group and has its own IP header. The fragmented data packets are independent of each other and the best route is selected to reach the destination. in this way, the packets may be out of order when they arrive at the destination, and the receiver correctly assembles the packets based on the information in the IP header. although data can be sharded, we should avoid sharding as much as possible, because the IP layer itself does not have a timeout retransmission mechanism, and the higher level is responsible for timeout and retransmission (TCP has a timeout and retransmission mechanism ). when a packet is lost, TCP resends the entire TCP packet segment after the timeout, instead of re-transmitting a packet segment of the packet segment. The packet segment also increases the packet loss rate and reduces the network speed. does the router reorganize the ip packet of the shard? Each Shard is transmitted independently and transmitted along multiple paths in the network. A Router cannot collect all the shards. Partitioning and restructuring on a vro can simplify the vro protocol and reduce the workload. An ICMP non-arrival error occurs when an ICMP non-reachable error occurs (an IP segment is required). When the router receives a data packet that needs to be split, in the IP header, the flag bit of no partition (DF) is set. If a program needs to determine the minimum MTU on the way to the target end-a path MTU discovery mechanism, this error can be used by the program.

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.