ICMP 头和 IP 头校验和计算 [英] ICMP header and IP header checksum calculations

查看:84
本文介绍了ICMP 头和 IP 头校验和计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

icmp header checksum 和 ip header checksum 计算方法一样吗?我的意思是,它们可能很相似.但我发现了this ip 头校验和的代码.我也可以将此代码用于 icmp 标头校验和吗?任何其他帮助都会很棒.

Are icmp header checksum and ip header checksum calculation methods are same? I mean, they may be similar. But I found this code for ip header checksum. Can I use this code for icmp header checksum too? Any other help would be great.

     unsigned short cksum(struct ip *ip, int len){
       long sum = 0;  /* assume 32 bit long, 16 bit short */

       while(len > 1){
         sum += *((unsigned short*) ip)++;
         if(sum & 0x80000000)   /* if high order bit set, fold */
           sum = (sum & 0xFFFF) + (sum >> 16);
         len -= 2;
       }

       if(len)       /* take care of left over byte */
         sum += (unsigned short) *(unsigned char *)ip;

       while(sum>>16)
         sum = (sum & 0xFFFF) + (sum >> 16);

       return ~sum;
     }

推荐答案

RFC 791 - 互联网协议...

头部校验和:16 位

仅对标头的校验和.由于某些标题字段更改(例如,生存时间),这将在每个点重新计算和验证处理 Internet 标头.

A checksum on the header only. Since some header fields change (e.g., time to live), this is recomputed and verified at each point that the internet header is processed.

校验和算法为:

 The checksum field is the 16 bit one's complement of the one's
 complement sum of all 16 bit words in the header.  For purposes of
computing the checksum, the value of the checksum field is zero.

这是一个简单的计算校验和和实验证据表示它是足够的,但它是临时的,可能会被替换通过 CRC 程序,取决于进一步的经验.

This is a simple to compute checksum and experimental evidence indicates it is adequate, but it is provisional and may be replaced by a CRC procedure, depending on further experience.

注意:CRC 程序"从未实施.

Note: The "CRC procedure" was never implemented.

RFC 792 - 互联网控制消息协议...

标题校验和

 The 16 bit one's complement of the one's complement sum of all 16
 bit words in the header.  For computing the checksum, the checksum
 field should be zero.  This checksum may be replaced in the
 future.

注意:同样,这个算法从未被替换过.

Note: Again, this algorithm was never replaced.

因此,可以安全地假设这两种算法是相同的,是的,您可以使用相同的 BSD 代码(当然,为了理智起见,更改 struct ip 内容)来计算 ICMP标头校验和.

So, it's safe to assume that both algorithms are the same, and yes, you can use the same BSD code (changing the struct ip stuff for sanity's sake, of course) for calculating a ICMP header checksum.

这篇关于ICMP 头和 IP 头校验和计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆