![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 13
| checksum calculation 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);
}
|
| karthigayan is offline | |
| | #2 |
| Guest Join Date: Aug 2001
Posts: 4,922
| >> can any one please explain the code The comments weren't enough? |
| Sebastiani is offline | |
| | #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:
| |
| Elysia is offline | |
| | #4 |
| Algorithm Dissector 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |