Binary inverse sum of verification algorithms

Source: Internet
Author: User

The IP/ICMP/IGMP/TCP/UDP protocol has the same checksum algorithm. The algorithm is as follows:

When sending data, in order to calculate the number of IP reported checksum. Follow these steps:
(1) set the IP datagram header to 0, including the checksum field.
(2) regard the header as a number in the unit of 16 bits, and sum binary anticode in sequence.
(3) Save the result to the checksum field.
When receiving data, it is relatively simple to calculate the datagram checksum. follow these steps:
(1) regard the header as a number consisting of 16 bits, and sum the binary inverse code in sequence, including the checksum field.
(2) check whether the calculated checksum result is equal to zero.
(3) If it is equal to zero, it indicates that it is divided, and the verification is correct. Otherwise, the checksum is incorrect. The protocol stack must discard this packet.
Here, the calculation method of the binary anticode sum is as follows:
First, we compute the parts shown in the B-1. We add each column. If there is an increment, we add it to the next column. Note the following:
1 ------------------------ carry of column 16th
1 1 ------------------------ carry of column 15th
| 1
| 1 0
| 1 1
| 1 0
| 1 0
| 1 1
| 1 0
| 1 0
| 1 1
| 1 1
| 1 0 0 ----------- carry of column 3rd
| 1 0 0 ----------- carry of Column 2nd
| 1 1 --------- carry of column 1st
|
1 0 0 1 1 0 1 0 0 0 1 0 0 1 0
0 0 0 0 1 0 0 0 1 1 0 1 0 0 1
1 0 1 0 1 0 1 1 0 0 0 0 0 1 0
0 0 0 0 1 1 1 0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 1 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 0 1 0 0 0 1 0 1
0 1 0 1 0 1 1 0 1 0 1 0 1 0 0
0 1 0 0 1 0 1 0 1 0 0 1 1 1
0 1 0 0 0 1 1 1 0 0 0 0 0 0 0
__________________________________
1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 part
Part of the binary notation of graph B-1
1. When we add the 1st columns (the rightmost column), we get 7. In binary, 7 is 111. We keep the rightmost 1 and add the remaining bits to the 2nd and 3rd columns.
2. When we add 2nd columns, we include the carry from 1st columns. The result is 8, which is 1000 of the binary. We keep the first position (rightmost) and place the remaining 100 to 3rd columns, 4th columns, and 5th columns.
3. Repeat the above process for each column.
4. When we add the last column, we can add two 1 columns without adding them. The two 1 S should be added to the Partial Sum (Partial Sum) in the next step.
B .1.2 and
If the last column has no carry, the part and sum are. However, if there is an extra column (in this example, there is a column with two rows), you need to add it to the part and middle to get the sum. Given this computation, we have now come to the sum.
1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 part
1
1
____________________________________
1 0 0 1 0 1 1 0 1 1 1 0 1 1 and
0 1 1 0 1 0 0 1 0 0 0 1 0 0 checksum
Sum and checksum of binary notation of graph B-2
B .1.2 checksum
After the sum is calculated, we calculate the inverse code for each bit to obtain the test result. The graph B-2 also gives a test. Binary computation can be converted to decimal computation in the same principle.


Algorithm Implementation:
First, check the validation algorithm in the Linux 2.6 kernel, which is compiled in assembly language, obviously more efficient. The Code is as follows:
Unsigned short ip_fast_csum (unsigned char * IPH,
Unsigned int IHL)
{
Unsigned int sum;

_ ASM _ volatile __(
"Movl (% 1), % 0; \ n"
"Subl, % 2; \ n"
"Jbe 2f; \ n"
"Addl 4 (% 1), % 0; \ n"
"Adcl 8 (% 1), % 0; \ n"
"Adcl 12 (% 1), % 0; \ n"
"1: adcl 16 (% 1), % 0; \ n"
"Lea 4 (% 1), % 1; \ n"
"Decl % 2; \ n"
"JNE 1B; \ n"
"Adcl, % 0; \ n"
"Movl % 0, % 2; \ n"
"Shrl, % 0; \ n"
"Addw % W2, % W0; \ n"
"Adcl, % 0; \ n"
"Notl % 0; \ n"
"2:; \ n"

: "= R" (SUM), "= r" (IPH), "= r" (IHL)
: "1" (IPH), "2" (IHL)
: "Memory ");
Return (SUM );
}

In this function, the first parameter is obviously the first address of the IP datagram, and almost all algorithms are the same. Note that the second parameter directly uses the header length field in the IP data header information and does not need to be converted. Therefore, the speed is faster (the master is considerate ). The usage is provided in the following sample code.

The second algorithm is very common and is written in C language. I have read a lot of code that implements the network protocol stack. This algorithm is the most commonly used. Even if it changes, it is nothing more than reverse and sum. Considering the reason, it is estimated that the portability of C language is better. The following is the implementation of this function:
Unsigned short checksum (unsigned short * Buf, int nword)
{
Unsigned long sum;

For (sum = 0; nword> 0; nword --)
Sum + = * Buf ++;
Sum = (sum> 16) + (sum & 0 xFFFF );
Sum + = (sum> 16 );

Return ~ SUM;
}

Http://blog.csdn.net/wadehao/article/details/8797728

Binary inverse sum of verification algorithms

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.