Re: icmp-checksum

Richard B. Johnson (root@analogic.com)
Fri, 27 Dec 1996 09:09:07 -0500 (EST)


On Fri, 27 Dec 1996, Martin Bauer wrote:

>
> Hi *,
>
> I know this is a little bit off-topic, sorry!
> I need a piece of c-code for re-computing the
> checksum in the icmp-header. Maybe I am just
> braindead, but my version of the rule in rfc0792
> does not work!
> Please help!
>
> Martin
>

This is a very inefficient routine that does the checksumming. The
linux checksum is very much more powerful and quicker. However this
gives you the general idea. Note that if you are going to checksum
a checksummed packet that includes the checksum, you have to compliment
the output. Also note that this works ONLY for an even number of bytes.

unsigned short int chksum(char *buffer, int len)
{
unsigned short int *word;
unsigned long accum;
unsigned long chksm;
int i;

accum = 0;
word = (unsigned short *) buffer;
len >>=1; /* Words only */
for(i=0; i< len; i++)
accum += (unsigned long) *word++;
chksm = (accum & 0xffff); /* Mask all but low word */
chksm += (accum >> 16); /* Sum all the carries */
if(chksm > 0xffff) /* If this also carried */
chksm++; /* Sum this too */
return (unsigned short) (chksm & 0xffff);

}

Cheers,
Dick Johnson
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Richard B. Johnson
Project Engineer
Analogic Corporation
Voice : (508) 977-3000 ext. 3754
Fax : (508) 532-6097
Modem : (508) 977-6870
Ftp : ftp@boneserver.analogic.com
Email : rjohnson@analogic.com, johnson@analogic.com
Penguin : Linux version 2.1.16 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-