IP校验和计算

IP checksum calculating

我正在尝试计算 ip 地址的校验和 header(没有选项),遵循算法:将 header 分成 16 位字,对所有字求和,不应用运算符在结果上获取校验和,但我仍然得到错误的结果,用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 header 是:

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 操作之后,因为我几乎可以肯定我的 windows是 LITTLEENDIAN,如果是这种情况,结果将按此字节顺序放置。我的函数的结果是:0x7a17 并且 wireshark 结果是这个 header 的 0x7917。有人可以解释这里出了什么问题吗?我的参考文献是:RFC 791 and How to Calculate IpHeader Checksum

所以在读完这个 link 之后: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操作后不需要转换,我把进位计算放在一个循环中,因为我不知道我要执行多少次这一步,我认为就我而言,它不超过一个。