3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/lib/libatm/ip_checksum.c,v 1.3.2.1 2001/09/28 16:52:10 dillon Exp $
27 * @(#) $DragonFly: src/lib/libatm/ip_checksum.c,v 1.4 2008/09/30 16:57:04 swildner Exp $
31 #include <sys/cdefs.h>
34 * User Space Library Functions
35 * ----------------------------
37 * IP checksum computation
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netatm/port.h>
47 #include <netatm/atm.h>
48 #include <netatm/atm_if.h>
49 #include <netatm/atm_sap.h>
50 #include <netatm/atm_sys.h>
51 #include <netatm/atm_ioctl.h>
56 * Compute an IP checksum
58 * This code was taken from RFC 1071.
60 * "The following "C" code algorithm computes the checksum with an inner
61 * loop that sums 16 bits at a time in a 32-bit accumulator."
64 * addr pointer to the buffer whose checksum is to be computed
65 * count number of bytes to include in the checksum
68 * the computed checksum
72 ip_checksum(char *addr
, int count
)
74 /* Compute Internet Checksum for "count" bytes
75 * beginning at location "addr".
80 /* This is the inner loop */
81 sum
+= ntohs(* (unsigned short *) addr
);
82 addr
+= sizeof(unsigned short);
83 count
-= sizeof(unsigned short);
86 /* Add left-over byte, if any */
88 sum
+= * (unsigned char *) addr
;
90 /* Fold 32-bit sum to 16 bits */
92 sum
= (sum
& 0xffff) + (sum
>> 16);