IP Datagram Header checksum algorithm

Source: Internet
Author: User

when using Google search IP datagram header checksum algorithm, always see the code, do not see the process, so there is this article, if there are errors please correct me. The article omitted a little, hehe
IP/ICMP/IGMP/TCP/UDP and other protocols are the same checksum algorithm, the algorithm is as follows:
在发送数据时,为了计算数IP据报的校验和。应该按如下步骤:(1)把IP数据报的首部都置为0,包括校验和字段。(2)把首部看成以16位为单位的数字组成,依次进行二进制反码求和。(3)把得到的结果存入校验和字段中。在接收数据时,计算数据报的校验和相对简单,按如下步骤:(1)当接收IP包时,需要对报头进行确认,检查IP头是否有误,算法同上2、3步,然后判断取反的结果是否为0,是则正确,否则有错。

  

1、发送方  i)将校验和字段置为0,然后将IP包头按16比特分成多个单元,如包头长度不是16比特的倍数,则用0比特填充到16比特的倍数;  ii)对各个单元采用反码加法运算(即高位溢出位会加到低位,通常的补码运算是直接丢掉溢出的高位),将得到的和的反码填入校验和字段;   iii)发送数据包。 2、接收方  i)将IP包头按16比特分成多个单元,如包头长度不是16比特的倍数,则用0比特填充到16比特的倍数;   ii)对各个单元采用反码加法运算,检查得到的和是否符合是全1(有的实现可能对得到的和会取反码,然后判断最终值是不是全0);iii)如果是全1则进行下步处理,否则意味着包已变化从而丢弃之。需要强调的是反码和是采用高位溢出加到低位的,如3比特的反码和运算:100b+101b=010b(因为100b+101b=1001b,高位溢出1,其应该加到低位,即001b+1b(高位溢出位)=010b)。

1. Example

Look, I'm using Ominipeek's grab bag.

I. Officer and field is set to 0, then the IP header is divided into 16 bits

Checksum header checksum:0x618d resets it to 0x0000

Segment the IP header:

1.0x4500

2.0x0029

3.0x44f1

4.0x4000

5.0x8006

6.0x0000-------> This is the header checksum value, we reset it to 0 before

7.0xc0a8

8.0x01ae

9.0x4a7d

+ 10. 0x477d

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

Add 1 to 10 for the sum of: 0x29e70

Ii. Inverse Code addition for each unit (that is , the high-level overflow bit will be added to the low , the usual complement operation is to throw away the overflow high ), the resulting and the inverse code to fill in the checksum field

0x0002+0x9e70=0x9e72

0x9e72 binary: 1001 1110 0111 0010

Anti-code: 0110 0001 1000 1101

0110 0001 1000 1101 of 16 binary: 0x618d

See if this is the same as the checksum in the IP header

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

When an IP is received to detect it

Iii. for each unit using the inverse Code addition operation, check that the resulting and whether the match is full 1 (some implementations may be to get the and take the inverse code, and then determine whether the final value is all 0)

When you receive the IP Data Bureau package, verify that the IP header is correct, you can do this

1.0x4500

2.0x0029

3.0x44f1

4.0x4000

5.0x8006

6.0x618d -------> This is the value of the header checksum

7.0xc0a8

8.0x01ae

9.0x4a7d

+ 10. 0x477d

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

Add 1 to 10 for the sum of: 0x2ffd

Anti-code addition operation for each unit ( that is, the high overflow bit will be added to the low , the usual complement operation is to throw away the overflow high ), the resulting and the inverse code to fill in the checksum field:

0x0002+0x0ffd=0xffff

0xFFFF binary: 1111 1111 1111 1111

1111 1111 1111 1111 Anti-code: 0

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

With regard to this part of the supplementary note,

segment IP header  :   1.  0x4500   2.  0x0029   3.  0X44F1   4.  0x4000   5.  0x8006   6.  0x0000-------> This is the value of the header checksum, which we previously reset to 0     8.  0x01ae   9.  0x4a7d  + 10. 0x477d
----------------------------------------------------------------------------------------------------------

IP Datagram Header checksum algorithm

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.