簡體   English   中英

ICMP標頭和IP標頭校驗和計算

[英]ICMP header and IP header checksum calculations

icmp標頭校驗和和IP標頭校驗和計算方法是否相同? 我的意思是,它們可能相似。 但我發現代碼用於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 791-Internet協議 ...

標頭校驗和: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控制消息協議 ...

標頭校驗和

  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頭校驗和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM