]>
git.saurik.com Git - apple/network_cmds.git/blob - alias/alias_util.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 2001 Charles Mott <cmott@scientech.com>
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * $FreeBSD: src/lib/libalias/alias_util.c,v 1.4.2.2 2001/06/04 14:59:06 brian Exp $
52 Alias_util.c contains general utilities used by other functions
53 in the packet aliasing module. At the moment, there are functions
54 for computing IP header and TCP packet checksums.
56 The checksum routines are based upon example code in a Unix networking
57 text written by Stevens (sorry, I can't remember the title -- but
58 at least this is a good author).
60 Initial Version: August, 1996 (cjm)
62 Version 1.7: January 9, 1997
63 Added differential checksum update function.
67 Note: the checksum routines assume that the actual checksum word has
68 been zeroed out. If the checksum word is filled with the proper value,
69 then these routines will give a result of zero (useful for testing
73 #include <sys/types.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
80 #include "alias_local.h"
83 PacketAliasInternetChecksum(u_short
*ptr
, int nbytes
)
96 ((u_char
*) &oddbyte
)[0] = *(u_char
*) ptr
;
97 ((u_char
*) &oddbyte
)[1] = 0;
100 sum
= (sum
>> 16) + (sum
& 0xffff);
106 IpChecksum(struct ip
*pip
)
108 return( PacketAliasInternetChecksum((u_short
*) pip
,
109 (pip
->ip_hl
<< 2)) );
114 TcpChecksum(struct ip
*pip
)
118 int nhdr
, ntcp
, nbytes
;
121 nhdr
= pip
->ip_hl
<< 2;
122 ntcp
= ntohs(pip
->ip_len
) - nhdr
;
124 tc
= (struct tcphdr
*) ((char *) pip
+ nhdr
);
125 ptr
= (u_short
*) tc
;
127 /* Add up TCP header and data */
138 ((u_char
*) &oddbyte
)[0] = *(u_char
*) ptr
;
139 ((u_char
*) &oddbyte
)[1] = 0;
143 /* "Pseudo-header" data */
144 ptr
= (u_short
*) &(pip
->ip_dst
);
147 ptr
= (u_short
*) &(pip
->ip_src
);
150 sum
+= htons((u_short
) ntcp
);
151 sum
+= htons((u_short
) pip
->ip_p
);
153 /* Roll over carry bits */
154 sum
= (sum
>> 16) + (sum
& 0xffff);
157 /* Return checksum */
158 return((u_short
) ~sum
);
163 DifferentialChecksum(u_short
*cksum
, u_short
*new, u_short
*old
, int n
)
171 accumulate
-= *new++;
172 accumulate
+= *old
++;
177 accumulate
= -accumulate
;
178 accumulate
= (accumulate
>> 16) + (accumulate
& 0xffff);
179 accumulate
+= accumulate
>> 16;
180 *cksum
= (u_short
) ~accumulate
;
184 accumulate
= (accumulate
>> 16) + (accumulate
& 0xffff);
185 accumulate
+= accumulate
>> 16;
186 *cksum
= (u_short
) accumulate
;