#include "packet.h" // shamelessly copied from: // http://www.roman10.net/how-to-calculate-iptcpudp-checksumpart-2-implementation/ unsigned short compute_checksum(unsigned short *addr, unsigned int count) { 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); } void compute_ip_checksum(struct iphdr* pkt) { pkt->check = 0x0000; pkt->check = compute_checksum( (unsigned short*) pkt, pkt->ihl * 4 ); }