When google is used to search for the IP datagram header checksum algorithm, the code is always displayed and the process is not displayed, so this article is available. If there is an error, please correct it. Leave this article omitted.
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. Sender I) sets the checksum field to 0 and divides 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) use the inverse code addition operation for each unit (that is, the high overflow bit will be added to the low position, usually, the complement operation directly drops the overflow high.) Fill in the inverse code of the obtained sum in the checksum field. iii) send the data packet. 2. Receiver I) divides an IP address header into multiple units based on 16 bits. If the header length is not a multiple of 16 bits, it is filled 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 sum of 1 (some implementations may obtain the sum and then judge whether the final value is all 0); iii) if it is full 1, proceed to the next step; otherwise, it means the package has changed and is discarded. It should be emphasized that the anti-Code and the Anti-code are added to the low position by high overflow, such as 3-bit anti-code and computation: 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
Segment IP headers:
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.
========================================================== ============================
When an IP address is received, it is detected.
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)
When you receive an IP address data board package, verify that the IP address header is correct.
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
========================================================== ================
Additional instructions on this part,
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
----------------------------------------------------------------------------------------------------------