IP校验和计算

时间:2015-09-23 23:07:44

标签: c ip checksum

我试图计算ip地址头的校验和(没有选项),遵循算法:将头部分成16位字,对所有单词求和,对结果应用NOT运算符以获得校验和,但我仍然得到错误的结果,用wireshark嗅探数据包,我可以看到它们是错的,例如,这是我的方法:

void compute_ip_checksum(struct ip_hdr* ip){
unsigned short* begin = (unsigned short*)ip;
unsigned short* end = begin + (IP_NOPT_HEADER_LENGTH / 2);
unsigned short checksum = 0;

ip->checksum = 0;
for (; begin != end; begin++){
    checksum += *begin;
}

ip->checksum = htons(~checksum);
}

我构建的ip头是:

ip.version_and_length = (IPV4 << 4) | (IP_NOPT_HEADER_LENGTH/4);
ip.type_of_service = 0;
ip.total_length = htons(IP_NOPT_HEADER_LENGTH + TCP_NOPT_HEADER_LENGTH);
ip.frag_id = 0;
ip.flags_and_frag_offset = htons(DONT_FRAGMENT << 13);
ip.time_to_live = 128;
ip.protocol = TCP_PAYLOAD;
ip.src_ip = inet_addr("1.1.1.1");
ip.dst_ip = inet_addr("1.1.1.2");

由于我将所有值转换为网络字节顺序,因此只有在NOT操作之后我才会在校验和中进行任何转换,因为我几乎可以确定我的窗口是LITTLEENDIAN,如果是这样的话,结果将放在这个字节顺序中。我的函数的结果是:0x7a17,此标题的wireshark结果为0x7917。谁能解释这里有什么问题?我的推荐是:RFC 791How to Calculate IpHeader Checksum

1 个答案:

答案 0 :(得分:1)

所以在阅读完这个链接之后:wikipedia我可以看到校验和比预期的要复杂一点,现在这是适合我的代码:

void compute_ip_checksum(struct ip_hdr* ip, struct ip_options* opt){
unsigned short* begin = (unsigned short*)ip;
unsigned short* end = begin + IP_NOPT_HEADER_LENGTH / 2;
unsigned int checksum = 0, first_half, second_half;

ip->checksum = 0;
for (; begin != end; begin++){
    checksum += *begin;
}

first_half = (unsigned short)(checksum >> 16);
while (first_half){
    second_half = (unsigned short)((checksum << 16) >> 16);
    checksum = first_half + second_half;
    first_half = (unsigned short)(checksum >> 16);
}

ip->checksum = ~checksum;
}

正如你所看到的,在NOT操作之后不需要转换,我已经将进位计算放在循环中,因为我不知道有多少时间我必须执行此步骤,我认为在我的情况它不超过一个。