mywiki:linux:ip_checksum_apis
This is an old revision of the document!
Table of Contents
IP Checksum APIs
IP Header Checksum Calculation Implementation
- ip_checksum.c
/* set ip checksum of a given ip header*/ void compute_ip_checksum(struct iphdr *iphdrp) { iphdrp->check = 0; iphdrp->check = compute_checksum((unsigned short *)iphdrp, iphdrp->ihl << 2); } /* Compute checksum for count bytes starting at addr, using one's complement of one's complement sum*/ static unsigned short compute_checksum(unsigned short *addr, unsigned int count) { register unsigned long sum = 0; while (count > 1) { sum += * addr++; count -= 2; } //if any bytes left, pad the bytes and add if (count > 0) sum += ((*addr)&htons(0xFF00)); //Fold sum to 16 bits: add carrier to result while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16); //one's complement sum = ~sum; return ((unsigned short)sum); }
mywiki/linux/ip_checksum_apis.1431048758.txt.gz · Last modified: (external edit)
