In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Calculation of ipv4 checksum
Principle:
Calculation method 1: remove the two bits of the checksum and add the other bits: 45+00+00+3c+55+81+00+00+40+01+ac+1c
0fend0dfolk acidity 1cparts 0frank0e =
Calculation method 2:
The checksum (checksum) algorithm is simply a 16-bit cumulative inverse operation:
The calculation function is as follows:
When we calculate, it is the host byte order, and when the calculation result is encapsulated into the IP packet, it is the network byte order. Pay attention to the difference between the two. When we read from the IP package, we need to convert it to the host byte order, and when we save it into the IP packet, we need to convert it to the network byte order.
UINT32 Checksum (UINT32 cksum, VOID*pBuffer, UINT32 size)
{
INT8 num = 0
UINT8 * p = (UINT8 *) pBuffer
If ((NULL = = pBuffer) | | (0 = size))
{
Return cksum
}
While (size > 1)
{
Cksum + = ((UINT16) p [num] 0)
/ / if the length is odd
{
Cksum + = (UINT16) p [num] > 16)
{
Cksum = (cksum & 0xFFFF) + (cksum > > 16)
/ / add the high 16 bits to the low 16 bits after accumulation, and repeat the process until the high 16 bits are all 0
}
Return cksum
}
Note: the type of UINT32 cksum, which is 4 bytes here, prevents data overflow during accumulation (for example, memory overflow occurs when 0xFF is accumulated)
The detailed calculation process and principle are as follows
1: calculation of ip headers:
Accumulate the header data directly (excluding the original checksum value):
1. Ipv4 header
IpHeadLen = (pIpHeader- > ver_ihl & 0x0F) chksum = 0
Because the original checksum value is not included, set the value of checksum to 0 before each calculation, and then calculate
Sum = Checksum (0, (VOID *) pIpHeader, ipHeadLen)
The accumulation of the entire ip header
PIpHeader- > chksum = HTONS ((UINT16) (~ sum))
The result is the inverse of the calculated value, (don't forget to convert it to network byte order)
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.