Add a new "prefs_register_protocol()" routine, which is like
[obnox/wireshark/wip.git] / in_cksum.c
1 /* in_cksum.c
2  * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
3  * pointers/lengths giving the pieces to be checksummed.
4  *
5  * $Id: in_cksum.c,v 1.2 2000/12/14 17:51:51 gram Exp $
6  */
7
8 /*
9  * Copyright (c) 1988, 1992, 1993
10  *      The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
37  */
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
45 #endif
46
47 #ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
49 #endif
50
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
53 #endif
54
55 #ifdef HAVE_WINSOCK_H
56 #include <winsock.h>
57 #endif
58
59 #include <glib.h>
60
61 #include "in_cksum.h"
62
63 /*
64  * Checksum routine for Internet Protocol family headers (Portable Version).
65  *
66  * This routine is very heavily used in the network
67  * code and should be modified for each CPU to be as fast as possible.
68  */
69
70 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
71 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
72
73 int
74 in_cksum(const vec_t *vec, int veclen)
75 {
76         register const guint16 *w;
77         register int sum = 0;
78         register int mlen = 0;
79         int byte_swapped = 0;
80
81         union {
82                 char    c[2];
83                 guint16 s;
84         } s_util;
85         union {
86                 guint16 s[2];
87                 long    l;
88         } l_util;
89
90         for (; veclen != 0; vec++, veclen--) {
91                 if (vec->len == 0)
92                         continue;
93                 w = (const guint16 *)vec->ptr;
94                 if (mlen == -1) {
95                         /*
96                          * The first byte of this chunk is the continuation
97                          * of a word spanning between this chunk and the
98                          * last chunk.
99                          *
100                          * s_util.c[0] is already saved when scanning previous 
101                          * chunk.
102                          */
103                         s_util.c[1] = *(char *)w;
104                         sum += s_util.s;
105                         w = (const guint16 *)((const guint8 *)w + 1);
106                         mlen = vec->len - 1;
107                 } else
108                         mlen = vec->len;
109                 /*
110                  * Force to even boundary.
111                  */
112                 if ((1 & (int) w) && (mlen > 0)) {
113                         REDUCE;
114                         sum <<= 8;
115                         s_util.c[0] = *(const guint8 *)w;
116                         w = (const guint16 *)((const guint8 *)w + 1);
117                         mlen--;
118                         byte_swapped = 1;
119                 }
120                 /*
121                  * Unroll the loop to make overhead from
122                  * branches &c small.
123                  */
124                 while ((mlen -= 32) >= 0) {
125                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
126                         sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
127                         sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
128                         sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
129                         w += 16;
130                 }
131                 mlen += 32;
132                 while ((mlen -= 8) >= 0) {
133                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
134                         w += 4;
135                 }
136                 mlen += 8;
137                 if (mlen == 0 && byte_swapped == 0)
138                         continue;
139                 REDUCE;
140                 while ((mlen -= 2) >= 0) {
141                         sum += *w++;
142                 }
143                 if (byte_swapped) {
144                         REDUCE;
145                         sum <<= 8;
146                         byte_swapped = 0;
147                         if (mlen == -1) {
148                                 s_util.c[1] = *(char *)w;
149                                 sum += s_util.s;
150                                 mlen = 0;
151                         } else
152                                 mlen = -1;
153                 } else if (mlen == -1)
154                         s_util.c[0] = *(char *)w;
155         }
156         if (mlen == -1) {
157                 /* The last mbuf has odd # of bytes. Follow the
158                    standard (the odd byte may be shifted left by 8 bits
159                    or not as determined by endian-ness of the machine) */
160                 s_util.c[1] = 0;
161                 sum += s_util.s;
162         }
163         REDUCE;
164         return (~sum & 0xffff);
165 }
166
167 /*
168  * Given the host-byte-order value of the checksum field in a packet
169  * header, and the one's complement negation of the host-byte-order
170  * checksum of the packet, compute what the checksum field *should*
171  * have been.
172  */
173 guint16
174 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
175 {
176         guint32 shouldbe;
177
178         /*
179          * The value that should have gone into the checksum field
180          * is the negative of the value gotten by summing up everything
181          * *but* the checksum field.
182          *
183          * We can compute that by subtracting the value of the checksum
184          * field from the sum of all the data in the packet, and then
185          * computing the negative of that value.
186          *
187          * "sum" is the value of the checksum field, and "computed_sum"
188          * is the negative of the sum of all the data in the packets,
189          * so that's -(-computed_sum - sum), or (sum + computed_sum).
190          *
191          * All the arithmetic in question is one's complement, so the
192          * addition must include an end-around carry; we do this by
193          * doing the arithmetic in 32 bits (with no sign-extension),
194          * and then adding the upper 16 bits of the sum, which contain
195          * the carry, to the lower 16 bits of the sum, and then do it
196          * again in case *that* sum produced a carry.
197          *
198          * As RFC 1071 notes, the checksum can be computed without
199          * byte-swapping the 16-bit words; summing 16-bit words
200          * on a big-endian machine gives a big-endian checksum, which
201          * can be directly stuffed into the big-endian checksum fields
202          * in protocol headers, and summing words on a little-endian
203          * machine gives a little-endian checksum, which must be
204          * byte-swapped before being stuffed into a big-endian checksum
205          * field.
206          *
207          * "computed_sum" is a host-byte-order value, so we must put
208          * it in network byte order before subtracting it from the
209          * network-byte-order value from the header; the adjusted
210          * checksum will be in network byte order, which is what
211          * we'll return.
212          */
213         shouldbe = sum;
214         shouldbe += htons(computed_sum);
215         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
216         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
217         return shouldbe;
218 }