How to calculate UDP/tcp check and checksum

Source: Internet
Author: User

1. The figure below 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:

 

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.

Perform the calculation according to the above steps to get the test code 0x64b6. You can also try it.

IP datagram only checks the IP datagram header, But UDP checks both the header and the data part.

Note: most of this article is posted on the Internet.

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.