Calibration and Calculation methods

Source: Internet
Author: User

1. Description:

[1] The contents of the checksum coverage:

IP Checksum: IP header.

ICMP checksum: ICMP header +icmp data;

UDP, TCP Checksum: Header + Data + 12 bytes Pseudo header (source IP address, Destination IP address, protocol, TCP/UDP Kanenaga).

2. Steps to calculate the checksum:

[1] Set the checksum field to 0.

[2] The need to verify the data as a 16-bit units of the number of components, followed by binary inverse code summation.

[3] The resulting results are stored in the checksum field.

In addition, the length of UDP, TCP datagram can be odd number section, because the calculation is 16 bits, so at this time the checksum needs to be added at the end of a padding byte 0 (just calculate checksum, not sent out).

3. Receive-side checksum verification and steps:

[1] The need to verify the content (including the checksum field) as a number in 16-bit units, followed by binary inverse code summation, if the result is 0 is correct, otherwise represents an error.

4. Binary Inverse Code summation step:

[1] Binary anti-code summation, is the first to take the two counter, and then sum, if the highest bit has a carry, then the low into 1.

[2] In addition, first take the inverse and add the first and then take the inverse, the result is the same. So the implementation code is added first and then reversed.

5. Implementation code:

StaticInline uint16_t check_sum (Constuint16_t *buffer,intsize) {unsignedLongCksum =0; //16-bit for unit number addition     while(size>1) {cksum+ = *buffer++; Size-=sizeof(uint16_t); }    //length Odd case    if(size) cksum+ = * ((unsignedChar*) (buffer); //High has carry, carry to low, the following two lines of code to ensure that the height of 16 bits is 0. Cksum = (cksum>> -) + (cksum&0xFFFF); Cksum+ = (cksum>> -); //finally take the reverse    return(uint16_t) (~cksum);}

Calibration and Calculation methods

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.