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

Compute IP's reported checksum

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

The checksum algorithms of protocols such as IP/ICMP/IGMP/TCP/UDP are the same, and the algorithms are as follows:

When sending data, in order to calculate the checksum reported by IP. The following steps should be followed:

(1) set the header of the IP Datagram to 0, including checksum fields.

(2) the head is regarded as a 16-bit number, and the binary inverse code is summed in turn.

(3) store the result in the checksum field.

When receiving data, it is relatively easy to calculate the checksum of the Datagram, as follows:

(1) when receiving the IP packet, it is necessary to confirm the header and check whether the IP header is wrong. The algorithm is the same as the above steps 2 and 3, and then judge whether the result of inversion is 0, which is correct, otherwise there is an error.

1. Sender

I) set the checksum field to 0, and then divide the IP header into multiple units according to 16 bits. If the length of the header is not a multiple of 16 bits, fill it with 0 bits to a multiple of 16 bits

Ii) uses the inverse code addition operation for each unit (that is, the high overflow bit will be added to the low bit, and the usual complement operation is to lose the overflow high bit directly), and the inverse code of the sum will be filled into the checksum field.

Iii) sends a packet.

2. Recipient

I) divide the IP header into multiple units according to 16 bits. If the length of the header is not a multiple of 16 bits, fill it with 0 bits to a multiple of 16 bits

Ii) uses anti-code addition operation on each unit to check whether the sum obtained is all 1 (some implementations may reverse the sum and determine whether the final value is all 0)

Iii) if it is all 1, proceed to the next step, otherwise it means that the packet has been changed and discarded. It needs to be emphasized that the sum of inverse codes is added to low bits by high overflow, such as 3-bit inverse code and operation: 100b+101b=010b (because 100b+101b=1001b, high overflow 1, it should be added to low, that is, 001b+1b (high overflow bit) = 010b).

Now suppose a Datagram is 45 00 05 D4 CA E0 40 00 75 06 70 D2 CA 62 39 64 C0 A8 00 02

According to the format of the IP Datagram, we can see that its first check field is 70D2. How is it calculated?

Method: we replace the first check field, 70D2, with 0000.

4500+05D4+CAE0+4000+7506+0000+CA62+3964+C0A8+0002=38F2A

Then the one who comes out and the last four are added in hexadecimal, 8F2A+0003=8F2D.

Finally, use FFFF to subtract the calculated result, that is, FFFF-8F2D=70D2.

The function code is as follows.

USHORT 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)

}

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