blob: 201da59fe26e746fd3552e5897f1de72c8b4a8b5 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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>
paul34204aa2005-11-03 09:00:23 +000010#include "checksum.h"
paul718e3742002-12-13 20:15:29 +000011
paul34204aa2005-11-03 09:00:23 +000012int /* return checksum in low-order 16 bits */
13in_cksum(void *parg, int nbytes)
paul718e3742002-12-13 20:15:29 +000014{
paul34204aa2005-11-03 09:00:23 +000015 u_short *ptr = parg;
paul718e3742002-12-13 20:15:29 +000016 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}