IP check and COMPUTE

Source: Internet
Author: User

ip/icmp/igmp/TCP/UDP and other protocols are the same checksum algorithm, the algorithm is as follows:
in order to calculate the checksum of the IP packet when the data is sent. You should follow these steps:
(1) Set the checksum field of the IP packet to 0;
(2) The first is regarded as the number of 16-bit units, followed by binary inverse code summation;
(3) The resulting results are stored in the checksum field.
when receiving data, it is relatively simple to calculate the checksum of the packet, as follows:
(1) The first as a unit of 16 digits of the number, followed by binary inverse code summation, including the checksum field;
(2) Check whether the result of the computed checksum is equal to zero (the inverse code should be 16 1);
(3) if it equals zero, the description is divisible, and the checksum is correct. Otherwise, the checksum is wrong, and the protocol stack discards the packet.
the so-called binary inverse code summation, that is, the first binary summation, and then pair and reverse.
IP Datagram Format

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8C/41/wKioL1hnTW-iW2EMAABf2E4I1pw146.jpg-wh_500x0-wm_3 -wmp_4-s_1128173143.jpg "title=" 9.jpg "alt=" wkiol1hntw-iw2emaabf2e4i1pw146.jpg-wh_50 "style=" padding:0px;margin:0 Px;vertical-align:top;border:none; "/>


Assume that the IP header is: 4500 0046 17d9 0000 4011 ec1d(check field) ac1c 0f3b ac1c 0f3d

Calculation:

4500 + 0046 +17d9 + 0000 + 4011+ EC1D +ac1c + 0f3b + ac1c + 0f3d 

The checksum to be filled is the sum of the removed and added

When the IP packet is received, to check the IP header is correct, the IP header is inspected, the same way:

Calculation:

44500 + 0046 +17d9 + 0000 + 4011+ EC1D +ac1c + 0f3b + ac1c + 0f3d again with their sum and add a number again to FFFF, the results are all one, correct.

Now if a data is reported as the D4 CA E0 D2 ca C0 A8 00 02

According to the format of the IP datagram, it can be seen that its first check field is D2 How is it calculated?

Method: We replace the first check field with 0000 D2

4500+05d4+cae0+4000+7506+0000+ca62+3964+c0a8+0002=38f2a

And then put the one that comes out with the next 4 digits in hexadecimal addition, 8f2a+0003=8f2d

Finally, FFFF minus the calculated result is the FFFF-8F2D=70D2


Code implementation

Short checksum (ushort* buffer, int size)

{

unsigned long cksum = 0;

while (size>1)

{

Cksum + = *buffer++;

Size-= sizeof (USHORT);

}

if (size)

{

Cksum + = * (uchar*) buffer;

}

Cksum = (cksum>>16) + (CKSUM&0XFFFF);

Cksum + = (cksum>>16);

Return (USHORT) (~cksum);

}



This article from "12034961" blog, declined reprint!

IP check and COMPUTE

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.