Not sure if the attachment was sucessfully applied. Here the test case
is inline:
#include
unsigned short checksum(unsigned short *addr, unsigned int count)
{
unsigned long sum = 0;
while( count > 1 ) {
sum += *(unsigned short *)addr++;
count -= 2;
}
if( count > 0 )
sum += *(unsigned char *)addr;
while (sum>>16)
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
unsigned short incr(unsigned short oldsum,
unsigned short *old,
unsigned short new)
{
unsigned short sum;
sum = ~(~oldsum + ~(*old) + new);
*old = new;
return sum;
}
int main()
{
unsigned short hdr[4], csum, oldsum, sum;
unsigned long dst_ip = 0xc3a91201, src_ip = 0xc3a91203;
hdr[0] = 0xc0a8;
hdr[1] = 0x4301;
hdr[2] = 0xc0a8;
hdr[3] = 0x4303;
oldsum = checksum((unsigned short *)&hdr, sizeof(hdr));
sum = incr(oldsum, &hdr[0], (dst_ip >> 16));
printf("Incremental checksum:\t\t%hx\n", sum);
printf("Header checksum after change:\t%hx\n",
(csum = checksum((unsigned short *)&hdr, sizeof(hdr))));
printf("Difference (incr - calc):\t%d\n", sum - csum);
oldsum = sum;
sum = incr(oldsum, &hdr[1], (unsigned short)dst_ip);
printf("Incremental checksum:\t\t%hx\n", sum);
printf("Header checksum after change:\t%hx\n",
(csum = checksum((unsigned short *)&hdr, sizeof(hdr))));
printf("Difference (incr - calc):\t%d\n", sum - csum);
oldsum = sum;
sum = incr(oldsum, &hdr[2], (unsigned short)(src_ip >> 16));
printf("Incremental checksum:\t\t%hx\n", sum);
printf("Header checksum after change:\t%hx\n",
(csum = checksum((unsigned short *)&hdr, sizeof(hdr))));
printf("Difference (incr - calc):\t%d\n", sum - csum);
oldsum = sum;
sum = incr(oldsum, &hdr[3], (unsigned short)src_ip);
printf("Incremental checksum:\t\t%hx\n", sum);
printf("Header checksum after change:\t%hx\n",
(csum = checksum((unsigned short *)&hdr, sizeof(hdr))));
printf("Difference (incr - calc):\t%d\n", sum - csum);
return 0;
}
--
Øyvind Hvamstad