# IP datagram header checksum Algorithm

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) When receiving an IP packet, you need to confirm the header and check whether the IP header is incorrect. The algorithm is the same as step 2 and step 3, and then judge whether the result of the reverse is 0, yes. Otherwise, it is incorrect.`

?
 `1. Sender``I) set the checksum field to 0 and divide the IP address header into multiple units based on 16 bits. For example, the header length is not a multiple of 16 bits, fill in the number of 16 bits with 0 bits;` `Ii) Apply the inverse code addition operation to each unit (that is, the high overflow bit will be added to the low level, and the general complement code operation will directly lose the overflow high level), and fill in the obtained inverse code with the checksum field;` `Iii) send data packets.` `2. Receiver``(I) divide an IP packet header into multiple units based on 16 bits. If the packet header length is not a multiple of 16 bits, fill it with 0 bits to a multiple of 16 bits;` `Ii) use the inverse code addition operation on each unit to check whether the obtained sum matches the full 1 (some implementations may obtain and obtain the reverse code, and then judge whether the final value is all 0 );` `Iii) If it is full 1, proceed to the next step; otherwise, it means that the package has changed and is discarded. It should be emphasized that anti-code and high overflow are used to add to the low level, such as 3-bit anti-code and operation: 100b + 101b = 010b (because 100b + 101b = 1001b, high overflow 1, it should be added to the low level, that is, 001b + 1b (high overflow bit) = 010b ).`

1. Instance

Please refer to the packet capture I used ominipeek

I. Set the checksum field to 0, and divide the IP address headers into multiple IP addresses by 16 bits.

Checksum: 0x618D of the Checksum Header to 0X0000

1. 0x4500

2. 0x0029

3. 0x44F1

4000 x

8006 x

6. 0x0000 -------> This is the Header Checksum value. We reset it to 0.

7. 0xC0A8

8. 0x01AE

9. 0x4A7D

+ 10. 0x477D

-------------------------------------------------------

Sum of the sum from 1 to 10: 0x29E70

II. Use the inverse code addition operation for each unit (that is, the high overflow bit will be added to the low level, and the general complement code operation will directly lose the overflow high level), and fill in the obtained inverse code with the checksum field.

0x0002 + 0x9E70 = 0x9E72

0x9E72 binary: 1001 1110 0111 0010

Reverse code: 0110 0001 1000 1101

0110 0001 1000 1101 hexadecimal: 0x618D

Check whether this is the same as Checksum in the IP address header.

========================================================== ============================

III. Use the inverse code addition operation on each unit to check whether the obtained sum matches the full 1 (some implementations may obtain the sum and then judge whether the final value is all 0)

1. 0x4500

2. 0x0029

3. 0x44F1

4000 x

8006 x

6. 0x618D -------> This is the Header Checksum value.

7. 0xC0A8

8. 0x01AE

9. 0x4A7D

+ 10. 0x477D

-------------------------------------------------------

Sum of the sum from 1 to 10: 0x2FFD

Apply the inverse code addition operation to each unit (that is, the high overflow bit is added to the low level, and the general complement code operation directly drops the overflow high level), and fill in the obtained inverse code with the checksum field:

0x0002 + 0x0FFD = 0 xFFFF

0xFFFF binary: 1111 1111 1111 1111

1111 1111 1111 1111 anti-code: 0

========================================================== ================

?
 `Segment IP headers:` `　　　　　　　　1.　　0x4500` `　　　　　　　　2.　　0x0029` `　　　　　　　　3.　　0x44F1` `　　　　　　　　4.　　0x4000` `　　　　　　　　5.　　0x8006` `6. 0x0000 -------> This is the Header Checksum value. We reset it to 0.` `　　　　　　　　7.　　0xC0A8` `　　　　　　　　8.　　0x01AE` `　　　　　　　　9.　　0x4A7D` `+　　　　　　　10.　　0x477D`
?
 `----------------------------------------------------------------------------------------------------------`

? ? ?

Source: http://www.cnblogs.com/fhefh/archive/2011/10/18/2216885.html
Related Keywords:

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.