Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ip header checksum calculation

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

The calculation method of IP header checksum:

1. Zero the checksum field.

two。 Then the binary inverse code is summed for every 16 bits (2 bytes). The inverse code summation means that the sum of every 16 bits is summed first, and then the resulting sum is converted into the inverse code.

Next, the steps of anti-code summation are described in detail: take a look at the following code

Algorithm:

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)

}

The parameter buffer is a pointer to a 16-bit integer, starting with the starting address of the IP header, and the parameter size is the size of the IP header. The while loop adds the contents of the head of IP in 16-bit units. If there is no integral division (that is, size still has less than 16 bits left), then add the rest, and the cksum at this time is the result of addition. This result often exceeds 16 bits. Because the checksum is 16 bits, the high 16-bit and calculated cksum should be reprocessed.

The first step of reprocessing: cksum = (cksum > > 16) + (cksum&0xffff), sum > > 16 is to shift the high 16 bits to the low 16 bits, sum&0xffff is to take out the low 16 bits and add up to get a new cksum.

The second step of reprocessing: cksum + = (cksum > > 16); carry is likely to be generated when the first step is added, so move the carry to the lower 16 bits again.

In this way, it is processed, and then it is reversed and forced to 16 bits, so that the final checksum is obtained.

Now that the checksum is calculated, the next step is how to check:

When the receiver carries on the check, it also carries on the binary inverse code summation for every 16 bits. Compared with the header when the sender calculates the checksum, there is one more checksum calculated by the sender. Therefore, if there is no error in the first part during transmission, then the result of the receiver's calculation should be all-in-one, because the receiver calculates that the value other than the checksum is the inverse code of the checksum, and the extra checksum is of course all-in-one.

Attach:

IP header:

4500 0046

17d9 0000

4011 ec1d (check field)

Ac1c 0f3b

Ac1c 0f3d

Calculation: 4500 + 0046 + 17d9 + 0000 + 4011 + ec1d + ac1c + 0f3b + ac1c + 0f3d

The sum taken out is the checksum that should be filled.

When receiving an IP packet, to check whether the IP header is correct, check the IP header, using the same method as above:

Calculate:

The sum of 4500 + 0046 + 17d9 + 0000 + 4011 + ec1d + ac1c + 0f3b + ac1c + 0f3d adds a number to FFFF again, and the result is all-in-one and correct.

The result is all in one, correct.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report