C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2009, 01:39 AM   #1
Registered User
 
Join Date: Nov 2008
Posts: 13
checksum calculation

hi all,
When I tried to learn about IP checksum I came across the following function for checksum calculation.can any one please explain the code.

Code:
u_short
in_cksum(register u_short *addr, register int len)
{
    register int nleft = len;
    register u_short *w = addr;
    register u_short answer;
    register int sum = 0;

    /*
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
     *  we add sequential 16 bit words to it, and at the end, fold
     *  back all the carry bits from the top 16 bits into the lower
     *  16 bits.
     */
    while (nleft > 1)  {
        sum += *w++;
        nleft -= 2;
    }

    /* mop up an odd byte, if necessary */
    if (nleft == 1)
        sum += *(u_char *)w;

    /*
     * add back carry outs from top 16 bits to low 16 bits
     */
    sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
    sum += (sum >> 16);         /* add carry */
    answer = ~sum;              /* truncate to 16 bits */
    return (answer);
}
Thanks..
karthigayan is offline   Reply With Quote
Old 06-29-2009, 01:42 AM   #2
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,922
>> can any one please explain the code

The comments weren't enough?
Sebastiani is offline   Reply With Quote
Old 06-29-2009, 02:14 AM   #3
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Perhaps there are specific bits you don't understand?
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 06-29-2009, 03:18 AM   #4
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
Is there some other programming language you are more familiar with that we could translate it to perhaps?
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Making of checksum using XOR Subsonics C Programming 4 02-20-2009 01:19 PM
checksum sender vs reciever? silhoutte75 C Programming 1 04-13-2008 01:27 PM
weird checksum function sagitt13 C Programming 7 10-31-2006 01:25 AM
Help figuring out a checksum algorithm barem23 A Brief History of Cprogramming.com 5 01-11-2003 04:24 AM


All times are GMT -6. The time now is 05:27 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22