paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Checksum routine for Internet Protocol family headers (C Version). |
| 3 | * |
| 4 | * Refer to "Computing the Internet Checksum" by R. Braden, D. Borman and |
| 5 | * C. Partridge, Computer Communication Review, Vol. 19, No. 2, April 1989, |
| 6 | * pp. 86-101, for additional details on computing this checksum. |
| 7 | */ |
| 8 | |
| 9 | #include <zebra.h> |
paul | 34204aa | 2005-11-03 09:00:23 +0000 | [diff] [blame] | 10 | #include "checksum.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 11 | |
paul | 34204aa | 2005-11-03 09:00:23 +0000 | [diff] [blame] | 12 | int /* return checksum in low-order 16 bits */ |
| 13 | in_cksum(void *parg, int nbytes) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 14 | { |
paul | 34204aa | 2005-11-03 09:00:23 +0000 | [diff] [blame] | 15 | u_short *ptr = parg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 16 | register long sum; /* assumes long == 32 bits */ |
| 17 | u_short oddbyte; |
| 18 | register u_short answer; /* assumes u_short == 16 bits */ |
| 19 | |
| 20 | /* |
| 21 | * Our algorithm is simple, using a 32-bit accumulator (sum), |
| 22 | * we add sequential 16-bit words to it, and at the end, fold back |
| 23 | * all the carry bits from the top 16 bits into the lower 16 bits. |
| 24 | */ |
| 25 | |
| 26 | sum = 0; |
| 27 | while (nbytes > 1) { |
| 28 | sum += *ptr++; |
| 29 | nbytes -= 2; |
| 30 | } |
| 31 | |
| 32 | /* mop up an odd byte, if necessary */ |
| 33 | if (nbytes == 1) { |
| 34 | oddbyte = 0; /* make sure top half is zero */ |
| 35 | *((u_char *) &oddbyte) = *(u_char *)ptr; /* one byte only */ |
| 36 | sum += oddbyte; |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Add back carry outs from top 16 bits to low 16 bits. |
| 41 | */ |
| 42 | |
| 43 | sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */ |
| 44 | sum += (sum >> 16); /* add carry */ |
| 45 | answer = ~sum; /* ones-complement, then truncate to 16 bits */ |
| 46 | return(answer); |
| 47 | } |