By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Internet Checksum

P: n/a
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++)
--
--
--------------------------------------
Pozdrawiam WILK
--------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
wilk wrote:
[snip]
But it doesn't work.


Define: "doesn't work"

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
But it doesn't work.


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.
Jul 22 '05 #3

P: n/a
Użytkownik "Ron Natalie" <ro*@sensor.com> napisał w wiadomo¶ci
news:3f*********************@news.newshosting.com. ..
But it doesn't work.
Define: "doesn't work"

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 ?

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.