VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/libalias/alias_mod.c@ 21664

Last change on this file since 21664 was 20958, checked in by vboxsync, 16 years ago

NAT: export of libalias

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1/*-
2 * Copyright (c) 2005 Paolo Pisati <[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_mod.c,v 1.3.8.1 2009/04/15 03:14:26 kensmith Exp $");
30
31#ifdef _KERNEL
32#include <sys/libkern.h>
33#include <sys/param.h>
34#include <sys/lock.h>
35#include <sys/rwlock.h>
36#else
37#include <stdio.h>
38#include <string.h>
39#include <sys/types.h>
40#include <errno.h>
41#endif
42
43#include <netinet/in_systm.h>
44#include <netinet/in.h>
45#include <netinet/ip.h>
46
47#ifdef _KERNEL
48#include <netinet/libalias/alias_local.h>
49#include <netinet/libalias/alias_mod.h>
50#else
51#include "alias_local.h"
52#include "alias_mod.h"
53#endif
54#else /* !VBOX */
55# include <slirp.h>
56# include "alias_local.h"
57# include "alias_mod.h"
58#endif /* VBOX */
59
60/* Protocol and userland module handlers chains. */
61LIST_HEAD(handler_chain, proto_handler) handler_chain = LIST_HEAD_INITIALIZER(foo);
62#ifdef _KERNEL
63struct rwlock handler_rw;
64#endif
65SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(foo);
66
67#ifdef _KERNEL
68
69#define LIBALIAS_RWLOCK_INIT() \
70 rw_init(&handler_rw, "Libalias_modules_rwlock")
71#define LIBALIAS_RWLOCK_DESTROY() rw_destroy(&handler_rw)
72#define LIBALIAS_WLOCK_ASSERT() \
73 rw_assert(&handler_rw, RA_WLOCKED)
74
75static __inline void
76LIBALIAS_RLOCK(void)
77{
78 rw_rlock(&handler_rw);
79}
80
81static __inline void
82LIBALIAS_RUNLOCK(void)
83{
84 rw_runlock(&handler_rw);
85}
86
87static __inline void
88LIBALIAS_WLOCK(void)
89{
90 rw_wlock(&handler_rw);
91}
92
93static __inline void
94LIBALIAS_WUNLOCK(void)
95{
96 rw_wunlock(&handler_rw);
97}
98
99static void
100_handler_chain_init(void)
101{
102
103 if (!rw_initialized(&handler_rw))
104 LIBALIAS_RWLOCK_INIT();
105}
106
107static void
108_handler_chain_destroy(void)
109{
110
111 if (rw_initialized(&handler_rw))
112 LIBALIAS_RWLOCK_DESTROY();
113}
114
115#else
116#define LIBALIAS_RWLOCK_INIT() ;
117#define LIBALIAS_RWLOCK_DESTROY() ;
118#define LIBALIAS_WLOCK_ASSERT() ;
119#define LIBALIAS_RLOCK() ;
120#define LIBALIAS_RUNLOCK() ;
121#define LIBALIAS_WLOCK() ;
122#define LIBALIAS_WUNLOCK() ;
123#define _handler_chain_init() ;
124#define _handler_chain_destroy() ;
125#endif
126
127void
128handler_chain_init(void)
129{
130 _handler_chain_init();
131}
132
133void
134handler_chain_destroy(void)
135{
136 _handler_chain_destroy();
137}
138
139static int
140_attach_handler(struct proto_handler *p)
141{
142 struct proto_handler *b = NULL;
143
144 LIBALIAS_WLOCK_ASSERT();
145 LIST_FOREACH(b, &handler_chain, entries) {
146 if ((b->pri == p->pri) &&
147 (b->dir == p->dir) &&
148 (b->proto == p->proto))
149 return (EEXIST); /* Priority conflict. */
150 if (b->pri > p->pri) {
151 LIST_INSERT_BEFORE(b, p, entries);
152 return (0);
153 }
154 }
155 /* End of list or found right position, inserts here. */
156 if (b)
157 LIST_INSERT_AFTER(b, p, entries);
158 else
159 LIST_INSERT_HEAD(&handler_chain, p, entries);
160 return (0);
161}
162
163static int
164_detach_handler(struct proto_handler *p)
165{
166 struct proto_handler *b, *b_tmp;;
167
168 LIBALIAS_WLOCK_ASSERT();
169 LIST_FOREACH_SAFE(b, &handler_chain, entries, b_tmp) {
170 if (b == p) {
171 LIST_REMOVE(b, entries);
172 return (0);
173 }
174 }
175 return (ENOENT); /* Handler not found. */
176}
177
178int
179LibAliasAttachHandlers(struct proto_handler *_p)
180{
181 int i, error = -1;
182
183 LIBALIAS_WLOCK();
184 for (i=0; 1; i++) {
185 if (*((int *)&_p[i]) == EOH)
186 break;
187 error = _attach_handler(&_p[i]);
188 if (error != 0)
189 break;
190 }
191 LIBALIAS_WUNLOCK();
192 return (error);
193}
194
195int
196LibAliasDetachHandlers(struct proto_handler *_p)
197{
198 int i, error = -1;
199
200 LIBALIAS_WLOCK();
201 for (i=0; 1; i++) {
202 if (*((int *)&_p[i]) == EOH)
203 break;
204 error = _detach_handler(&_p[i]);
205 if (error != 0)
206 break;
207 }
208 LIBALIAS_WUNLOCK();
209 return (error);
210}
211
212int
213detach_handler(struct proto_handler *_p)
214{
215 int error = -1;
216
217 LIBALIAS_WLOCK();
218 error = _detach_handler(_p);
219 LIBALIAS_WUNLOCK();
220 return (error);
221}
222
223int
224find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *pip,
225 struct alias_data *ad)
226{
227 struct proto_handler *p;
228 int error = ENOENT;
229
230 LIBALIAS_RLOCK();
231
232 LIST_FOREACH(p, &handler_chain, entries) {
233 if ((p->dir & dir) && (p->proto & proto))
234 if (p->fingerprint(la, pip, ad) == 0) {
235 error = p->protohandler(la, pip, ad);
236 break;
237 }
238 }
239 LIBALIAS_RUNLOCK();
240 return (error);
241}
242
243struct proto_handler *
244first_handler(void)
245{
246
247 return (LIST_FIRST(&handler_chain));
248}
249
250/* Dll manipulation code - this code is not thread safe... */
251
252int
253attach_dll(struct dll *p)
254{
255 struct dll *b;
256
257 SLIST_FOREACH(b, &dll_chain, next) {
258 if (!strncmp(b->name, p->name, DLL_LEN))
259 return (EEXIST); /* Dll name conflict. */
260 }
261 SLIST_INSERT_HEAD(&dll_chain, p, next);
262 return (0);
263}
264
265void *
266detach_dll(char *p)
267{
268 struct dll *b = NULL, *b_tmp;
269 void *error = NULL;
270
271 SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
272 if (!strncmp(b->name, p, DLL_LEN)) {
273 SLIST_REMOVE(&dll_chain, b, dll, next);
274 error = b;
275 break;
276 }
277 return (error);
278}
279
280struct dll *
281walk_dll_chain(void)
282{
283 struct dll *t;
284
285 t = SLIST_FIRST(&dll_chain);
286 if (t == NULL)
287 return (NULL);
288 SLIST_REMOVE_HEAD(&dll_chain, next);
289 return (t);
290}
Note: See TracBrowser for help on using the repository browser.

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