Merge branch 'vendor/TEXINFO'
[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  *      @(#) $DragonFly: src/lib/libatm/ip_checksum.c,v 1.4 2008/09/30 16:57:04 swildner Exp $
28  *
29  */
30
31 #include <sys/cdefs.h>
32
33 /*
34  * User Space Library Functions
35  * ----------------------------
36  *
37  * IP checksum computation
38  *
39  */
40
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>
52
53 #include "libatm.h"
54
55 /*
56  * Compute an IP checksum
57  *
58  * This code was taken from RFC 1071.
59  *
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."
62  *
63  * Arguments:
64  *      addr    pointer to the buffer whose checksum is to be computed
65  *      count   number of bytes to include in the checksum
66  *
67  * Returns:
68  *      the computed checksum
69  *
70  */
71 short
72 ip_checksum(char *addr, int count)
73 {
74         /* Compute Internet Checksum for "count" bytes
75          * beginning at location "addr".
76          */
77         long sum = 0;
78
79         while( count > 1 ) {
80                 /* This is the inner loop */
81                 sum += ntohs(* (unsigned short *) addr);
82                 addr += sizeof(unsigned short);
83                 count -= sizeof(unsigned short);
84         }
85
86         /* Add left-over byte, if any */
87         if( count > 0 )
88                 sum += * (unsigned char *) addr;
89
90         /* Fold 32-bit sum to 16 bits */
91         while (sum>>16)
92                 sum = (sum & 0xffff) + (sum >> 16);
93
94         return((short)~sum);
95 }