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