【问题标题】:ICMP header and IP header checksum calculationsICMP 标头和 IP 标头校验和计算
【发布时间】:2016-03-19 19:13:17
【问题描述】:

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

     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 792 中的 ICMP "Header Checksum - 标头中所有 16 位字的反码和的 16 位反码。为了计算校验和,校验和字段应该为零。将来可能会替换此校验和。" RFC 1071 和 1141 详细说明了如何进行 IP 校验和。
  • @Ron so,你说他们(header checksum和ip header checksum计算方法)不一样?

标签: c sockets networking icmp


【解决方案1】:

RFC 791 - Internet Protocol...

标头校验和:16 位

仅在标头上的校验和。由于某些标头字段发生了变化 (例如,生存时间),这在每个点都被重新计算和验证 处理 Internet 标头。

校验和算法为:

 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 程序,取决于进一步的经验。

注意:从未实施过“CRC 程序”。

RFC 792 - Internet Control Message Protocol...

标头校验和

 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.

注意:同样,该算法从未被替换。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2014-03-12
    • 2014-03-09
    • 1970-01-01
    相关资源
    最近更新 更多