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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。