Internet Checksum

W

wilk

I have a problem witch calculating TCP or UDP checksum.
This is what I found:

u16 in_cksum(u16 *addr,int count)
{
register long sum = 0;

/* add 16-bit words */
while (count > 1) {
/* this is the inner loop */
sum += * (unsigned short*)addr++;
count -= 2;
}

/* add leftover byte, if any */
if (count > 0)
#if BIG_ENDIAN
sum += (*(unsigned char *)addr) << 8;
#else
sum += *(unsigned char *)addr;
#endif

/* Fold 32-bit sum to 16-bit */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
sum = ~sum;
/*
* Return one's compliment of final sum.
*/
return (u16) sum;
}

But it doesn't work. I think that the problem is in the last while, when I
change 32-bit sum to 16-bit. But I didn't find anything else.
Please help me. (I use C not C++)
 
R

Ron Natalie

Define: "doesn't work"

It gives the wrong answer for the sum. This isn't surprising because the
algorithm
he uses is just a simple sum, and the IP checksum is defined as the "ones
complement
of a ones complement sum". Specifically after you add 2 16 bit quantities
you need
to take the carry bit (the 17th bit) and add it back to the low end of the
word.

A little googling will for sure point you to an implementation in C. It's
only about ten
lines or so.
 
W

wilk

I check sum with tcpdump. And it's bad.
It gives the wrong answer for the sum. This isn't surprising because the
algorithm
he uses is just a simple sum, and the IP checksum is defined as the "ones
complement
of a ones complement sum". Specifically after you add 2 16 bit quantities
you need
to take the carry bit (the 17th bit) and add it back to the low end of the
word.

A little googling will for sure point you to an implementation in C. It's
only about ten
lines or so.
I think I've googled the hole internet world :)
And all that I found was about 5-6 codes that implementing this algorithm.
All codes were exactly implementing this algorithm, and all codes were
descripted as working Internet Checksum Alg.

I'll try to calculate the carry for each 16 bit word as you say. (I know
what is "one's complement") But I think that I don't understand this
algorithm.
It's 5th day work on it. I'm exhausted. Maybe You can give me more details.
Maybe you've got exactly the same alg. that is descripted in RFC ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top