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;

}

 

你可能感兴趣的:(C语言)