HAMMER Utilities: enhanced show, timeout option
[dfdiff.git] / lib / libatm / ip_checksum.c
blobf6b27e3219fc73191ec9c972ffee61aa11795eac
1 /*
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.3 2004/10/25 19:38:01 drhodus 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>
44 #include <net/if.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>
53 #include "libatm.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."
63 * Arguments:
64 * addr pointer to the buffer whose checksum is to be computed
65 * count number of bytes to include in the checksum
67 * Returns:
68 * the computed checksum
71 short
72 ip_checksum(addr, count)
73 char *addr;
74 int count;
76 /* Compute Internet Checksum for "count" bytes
77 * beginning at location "addr".
79 long sum = 0;
81 while( count > 1 ) {
82 /* This is the inner loop */
83 sum += ntohs(* (unsigned short *) addr);
84 addr += sizeof(unsigned short);
85 count -= sizeof(unsigned short);
88 /* Add left-over byte, if any */
89 if( count > 0 )
90 sum += * (unsigned char *) addr;
92 /* Fold 32-bit sum to 16 bits */
93 while (sum>>16)
94 sum = (sum & 0xffff) + (sum >> 16);
96 return((short)~sum);