VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/libalias/alias_util.c@ 28800

最後變更 在這個檔案從28800是 20958,由 vboxsync 提交於 15 年 前

NAT: export of libalias

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.9 KB
 
1/*-
2 * Copyright (c) 2001 Charles Mott <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#ifndef VBOX
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: src/sys/netinet/libalias/alias_util.c,v 1.20.8.1 2009/04/15 03:14:26 kensmith Exp $");
30
31
32/*
33 Alias_util.c contains general utilities used by other functions
34 in the packet aliasing module. At the moment, there are functions
35 for computing IP header and TCP packet checksums.
36
37 The checksum routines are based upon example code in a Unix networking
38 text written by Stevens (sorry, I can't remember the title -- but
39 at least this is a good author).
40
41 Initial Version: August, 1996 (cjm)
42
43 Version 1.7: January 9, 1997
44 Added differential checksum update function.
45*/
46
47#ifdef _KERNEL
48#include <sys/param.h>
49#include <sys/proc.h>
50#else
51#include <sys/types.h>
52#include <stdio.h>
53#endif
54
55#include <netinet/in_systm.h>
56#include <netinet/in.h>
57#include <netinet/ip.h>
58#include <netinet/tcp.h>
59
60#ifdef _KERNEL
61#include <netinet/libalias/alias.h>
62#include <netinet/libalias/alias_local.h>
63#else
64#include "alias.h"
65#include "alias_local.h"
66#endif
67#else /* !VBOX */
68# include <slirp.h>
69# include "alias.h"
70# include "alias_local.h"
71#endif /* VBOX */
72
73/*
74 * Note: the checksum routines assume that the actual checksum word has
75 * been zeroed out. If the checksum word is filled with the proper value,
76 * then these routines will give a result of zero (useful for testing
77 * purposes);
78 */
79u_short
80LibAliasInternetChecksum(struct libalias *la __unused, u_short * ptr,
81 int nbytes)
82{
83 int sum, oddbyte;
84
85 LIBALIAS_LOCK(la);
86 sum = 0;
87 while (nbytes > 1) {
88 sum += *ptr++;
89 nbytes -= 2;
90 }
91 if (nbytes == 1) {
92 oddbyte = 0;
93 ((u_char *) & oddbyte)[0] = *(u_char *) ptr;
94 ((u_char *) & oddbyte)[1] = 0;
95 sum += oddbyte;
96 }
97 sum = (sum >> 16) + (sum & 0xffff);
98 sum += (sum >> 16);
99 LIBALIAS_UNLOCK(la);
100 return (~sum);
101}
102
103#ifndef _KERNEL
104u_short
105IpChecksum(struct ip *pip)
106{
107 return (LibAliasInternetChecksum(NULL, (u_short *) pip,
108 (pip->ip_hl << 2)));
109
110}
111
112u_short
113TcpChecksum(struct ip *pip)
114{
115 u_short *ptr;
116 struct tcphdr *tc;
117 int nhdr, ntcp, nbytes;
118 int sum, oddbyte;
119
120 nhdr = pip->ip_hl << 2;
121 ntcp = ntohs(pip->ip_len) - nhdr;
122
123 tc = (struct tcphdr *)ip_next(pip);
124 ptr = (u_short *) tc;
125
126/* Add up TCP header and data */
127 nbytes = ntcp;
128 sum = 0;
129 while (nbytes > 1) {
130 sum += *ptr++;
131 nbytes -= 2;
132 }
133 if (nbytes == 1) {
134 oddbyte = 0;
135 ((u_char *) & oddbyte)[0] = *(u_char *) ptr;
136 ((u_char *) & oddbyte)[1] = 0;
137 sum += oddbyte;
138 }
139/* "Pseudo-header" data */
140 ptr = (u_short *) & (pip->ip_dst);
141 sum += *ptr++;
142 sum += *ptr;
143 ptr = (u_short *) & (pip->ip_src);
144 sum += *ptr++;
145 sum += *ptr;
146 sum += htons((u_short) ntcp);
147 sum += htons((u_short) pip->ip_p);
148
149/* Roll over carry bits */
150 sum = (sum >> 16) + (sum & 0xffff);
151 sum += (sum >> 16);
152
153/* Return checksum */
154 return ((u_short) ~ sum);
155}
156#endif /* not _KERNEL */
157
158void
159DifferentialChecksum(u_short * cksum, void *newp, void *oldp, int n)
160{
161 int i;
162 int accumulate;
163 u_short *new = newp;
164 u_short *old = oldp;
165
166 accumulate = *cksum;
167 for (i = 0; i < n; i++) {
168 accumulate -= *new++;
169 accumulate += *old++;
170 }
171
172 if (accumulate < 0) {
173 accumulate = -accumulate;
174 accumulate = (accumulate >> 16) + (accumulate & 0xffff);
175 accumulate += accumulate >> 16;
176 *cksum = (u_short) ~ accumulate;
177 } else {
178 accumulate = (accumulate >> 16) + (accumulate & 0xffff);
179 accumulate += accumulate >> 16;
180 *cksum = (u_short) accumulate;
181 }
182}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette