我試圖通過使用RFC 1071的C語言代碼來計算合適的IP報頭校驗和,但有最好用代碼來描述一個問題:RFC 1071 - 用C
設置了IP報頭:
#include <linux/ip.h>
typedef struct iphdr tipheader;
int main(int argc, char **argv)
{
tipheader * iphead = (tipheader *) malloc(sizeof(tipheader));
iphead->ihl = 5;
iphead->version = 4;
iphead->tos = 0;
iphead->tot_len = 60;
....
unsigned short checksum = getChecksum((unsigned short *) iphead, 20);
}
校驗功能:
unsigned short getChecksum(unsigned short * iphead, int count)
{
unsigned long int sum = 0;
unsigned short checksum = 0;
printf("\nStarting adress: %p\n", iphead);
while(count > 1) {
sum += * (unsigned short *) (iphead);
count -=2;
printf("a: %p, content is: %d, new sum: %ld\n", iphead, (unsigned short) *(iphead), sum);
iphead++;
}
if(count > 0) {
sum += * (unsigned short *) (iphead);
}
while(sum >> 16) {
sum = (sum & 0xffff) + (sum >> 16);
}
checksum = ~sum;
return checksum;
}
遍歷存儲器指向iphead一個無符號短指針顯示以下輸出的第一兩次迭代之後:
Starting address: 0x603090
a: 0x603090, content is: 69, new sum: 69
a: 0x603092, content is: 60, new sum: 129
所以指針起作用「預期」和由2每次迭代增加。 但爲什麼前兩個字節的內容解釋爲69(0×45),其中它應該是0x4500
Thx的澄清
排序不匹配? – fferri
hton可以解決這個問題,但rfc不會浪費任何文字給 –
爲校驗和函數提供的部分代碼。 「iphead」的聲明在哪裏? – Pynchia