f9fe9fce8ce8c0cb9706785f4f4bb8e22cd7a769
[games.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.2 2003/06/17 04:26:41 dillon 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(addr, count)
73         char    *addr;
74         int     count;
75 {
76         /* Compute Internet Checksum for "count" bytes
77          * beginning at location "addr".
78          */
79         register long sum = 0;
80
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);
86         }
87
88         /* Add left-over byte, if any */
89         if( count > 0 )
90                 sum += * (unsigned char *) addr;
91
92         /* Fold 32-bit sum to 16 bits */
93         while (sum>>16)
94                 sum = (sum & 0xffff) + (sum >> 16);
95
96         return((short)~sum);
97 }