C语言版IP校验和算法

//buf表示报文中需要计算校验和的各个数,n是数据个数

unsigned short checksum(unsigned short *buf, int n)
{
  unsigned sum;
  for(sum = 0; n > 0; n--)
    sum += *buf++;
  sum = (sum >> 16) + (sum & 0xFFFF);
  sum += (sum >> 16);
  return ~sum;
}

 

posted @ 2013-03-21 18:31  飞鸟_Asuka  阅读(883)  评论(0编辑  收藏  举报