ip 数据包校验和

u_short MyIPCore::checksum(u_short* data, int len)
{
    u_long sum=0;
    for(;len>1;len-=2)
    {
        sum+=*data++;
        if(sum&0x80000000)
            sum=(sum&0xffff)+(sum>>16);
    }
    if(len==1)
    {
        u_short i=0;
        *(u_char*)(&i)=*(u_char*)data;
        sum+=i;
    }
    while(sum>>16)
        sum=(sum&0xffff)+(sum>>16);
    return (sum==0xffff)?sum:~sum;
}

你可能感兴趣的:(ip 数据包校验和)