Remove some unnecessary inclusions of <sys/cdefs.h> across the tree.
[dragonfly.git] / lib / libatm / ip_checksum.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
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.
12  *
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.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/lib/libatm/ip_checksum.c,v 1.3.2.1 2001/09/28 16:52:10 dillon Exp $
27  *
28  */
29
30 /*
31  * User Space Library Functions
32  * ----------------------------
33  *
34  * IP checksum computation
35  *
36  */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netatm/port.h>
44 #include <netatm/atm.h>
45 #include <netatm/atm_if.h>
46 #include <netatm/atm_sap.h>
47 #include <netatm/atm_sys.h>
48 #include <netatm/atm_ioctl.h>
49
50 #include "libatm.h"
51
52 /*
53  * Compute an IP checksum
54  *
55  * This code was taken from RFC 1071.
56  *
57  * "The following "C" code algorithm computes the checksum with an inner
58  * loop that sums 16 bits at a time in a 32-bit accumulator."
59  *
60  * Arguments:
61  *      addr    pointer to the buffer whose checksum is to be computed
62  *      count   number of bytes to include in the checksum
63  *
64  * Returns:
65  *      the computed checksum
66  *
67  */
68 short
69 ip_checksum(char *addr, int count)
70 {
71         /* Compute Internet Checksum for "count" bytes
72          * beginning at location "addr".
73          */
74         long sum = 0;
75
76         while( count > 1 ) {
77                 /* This is the inner loop */
78                 sum += ntohs(* (unsigned short *) addr);
79                 addr += sizeof(unsigned short);
80                 count -= sizeof(unsigned short);
81         }
82
83         /* Add left-over byte, if any */
84         if( count > 0 )
85                 sum += * (unsigned char *) addr;
86
87         /* Fold 32-bit sum to 16 bits */
88         while (sum>>16)
89                 sum = (sum & 0xffff) + (sum >> 16);
90
91         return((short)~sum);
92 }