2016-03-19 200 views
0

icmp頭校驗和和IP頭校驗和計算方法相同嗎?我的意思是,他們可能是相似的。但是我發現this代碼用於ip頭校驗和。我是否也可以使用此代碼進行icmp頭校驗?任何其他的幫助將是偉大的。ICMP頭和IP頭校驗和計算

 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; 
    } 
+1

您應該檢查RFC 792中的ICMP「_Header Checksum - 標題中所有16位字的補碼總和的16位補碼,爲了計算校驗和,校驗和字段應該爲零,這個校驗和可能是在未來取代。「RFC RFC 1071和1141詳細說明了如何進行IP校驗和。 –

+0

@Ron所以,你說他們(頭校驗和和頭校驗和計算方法)不一樣? –

回答

1

RFC 791 - Internet Protocol ...

頭校驗:16個比特

上僅將報頭的校驗和。由於一些標題字段改變了 (例如,生存時間),因此在每個點 處重新計算並驗證了互聯網標題被處理。

校驗算法是:

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頭校驗和是安全的。