# Detailed non-code 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. Perform the following 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, calculating the datagram checksum is relatively simple. 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 determines whether the inverse result is 0. If yes, it is correct; otherwise, it is wrong.

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 the IP Address Header: 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
----------------------------------------------------------------------------------------------------------

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.