Click to See Complete Forum and Search --> : Problem regarding checksums


shredder12
August 15th, 2009, 03:19 PM
This is my first post to code guru forums..
I am new to networking as well as socket programming.. and i am having some problems while learning them.

I am pretty confused about the purpose of checksums. What are they basically used for ??

and,
while I was reading a simple implementation of sending raw sockets from this page..

http://mixter.void.ru/rawip.html

I came across a function to calculate header checksums..


unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}


As the comment says it is used to generate check sums but I am unable to understand how is it happening in this function ??
I can definitely use some help here..

hoxsiew
August 16th, 2009, 08:52 PM
If you're "new to networking as well as socket programming" I think you should worry about raw sockets much later--once you have a good handle on TCP and UDP sockets.

If you don't understand what that function is doing, you probably need a little more background in C/C++ in general. Basically, it adds up the ascii value of the first nwords characters in buf, then stirs the pot (like a hash function) by bit shifting/bit flipping.