Skip to content

Instantly share code, notes, and snippets.

@GreenRecycleBin
Created October 9, 2011 14:39
Show Gist options
  • Select an option

  • Save GreenRecycleBin/1273762 to your computer and use it in GitHub Desktop.

Select an option

Save GreenRecycleBin/1273762 to your computer and use it in GitHub Desktop.
IP checksum
uint16_t ip_checksum(struct ip *p_ip_header, size_t len)
{
register int sum = 0;
uint16_t *ptr = (unsigned short*)p_ip_header;
while (len > 1){
sum += *ptr++;
len -= 2;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return ~sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment