VirtualBox

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

Last change on this file since 46260 was 40423, checked in by vboxsync, 13 years ago

NAT: warnings [-Wunused-macros]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 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
55/* Protocol and userland module handlers chains. */
56LIST_HEAD(handler_chain, proto_handler) handler_chain = LIST_HEAD_INITIALIZER(foo);
57#else /* !VBOX */
58# include <slirp.h>
59# include "alias_local.h"
60# include "alias_mod.h"
61#endif /* VBOX */
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 /* VBOX */
116# define LIBALIAS_WLOCK_ASSERT() ;
117# define LIBALIAS_RLOCK() ;
118# define LIBALIAS_RUNLOCK() ;
119# define LIBALIAS_WLOCK() ;
120# define LIBALIAS_WUNLOCK() ;
121# define _handler_chain_init() ;
122# define _handler_chain_destroy() ;
123#endif
124
125void
126handler_chain_init(void)
127{
128 _handler_chain_init();
129}
130
131void
132handler_chain_destroy(void)
133{
134 _handler_chain_destroy();
135}
136
137static int
138#ifdef VBOX
139_attach_handler(PNATState pData, struct proto_handler *p)
140#else
141_attach_handler(struct proto_handler *p)
142#endif
143{
144 struct proto_handler *b = NULL;
145
146 LIBALIAS_WLOCK_ASSERT();
147 LIST_FOREACH(b, &handler_chain, entries) {
148 if ((b->pri == p->pri) &&
149 (b->dir == p->dir) &&
150 (b->proto == p->proto))
151 return (EEXIST); /* Priority conflict. */
152 if (b->pri > p->pri) {
153 LIST_INSERT_BEFORE(b, p, entries);
154 return (0);
155 }
156 }
157 /* End of list or found right position, inserts here. */
158 if (b)
159 LIST_INSERT_AFTER(b, p, entries);
160 else
161 LIST_INSERT_HEAD(&handler_chain, p, entries);
162 return (0);
163}
164
165static int
166#ifdef VBOX
167_detach_handler(PNATState pData, struct proto_handler *p)
168#else
169_detach_handler(struct proto_handler *p)
170#endif
171{
172 struct proto_handler *b, *b_tmp;;
173
174 LIBALIAS_WLOCK_ASSERT();
175 LIST_FOREACH_SAFE(b, &handler_chain, entries, b_tmp) {
176 if (b == p) {
177 LIST_REMOVE(b, entries);
178 return (0);
179 }
180 }
181 return (ENOENT); /* Handler not found. */
182}
183
184int
185#ifdef VBOX
186LibAliasAttachHandlers(PNATState pData, struct proto_handler *_p)
187#else
188LibAliasAttachHandlers(struct proto_handler *_p)
189#endif
190{
191 int i, error = -1;
192
193 LIBALIAS_WLOCK();
194 for (i=0; 1; i++) {
195 if (*((int *)&_p[i]) == EOH)
196 break;
197#ifdef VBOX
198 error = _attach_handler(pData, &_p[i]);
199#else
200 error = _attach_handler(&_p[i]);
201#endif
202 if (error != 0)
203 break;
204 }
205 LIBALIAS_WUNLOCK();
206 return (error);
207}
208
209int
210#ifdef VBOX
211LibAliasDetachHandlers(PNATState pData, struct proto_handler *_p)
212#else
213LibAliasDetachHandlers(struct proto_handler *_p)
214#endif
215{
216 int i, error = -1;
217
218 LIBALIAS_WLOCK();
219 for (i=0; 1; i++) {
220 if (*((int *)&_p[i]) == EOH)
221 break;
222#ifdef VBOX
223 error = _detach_handler(pData, &_p[i]);
224#else
225 error = _detach_handler(&_p[i]);
226#endif
227 if (error != 0)
228 break;
229 }
230 LIBALIAS_WUNLOCK();
231 return (error);
232}
233
234int
235#ifdef VBOX
236detach_handler(PNATState pData, struct proto_handler *_p)
237#else
238detach_handler(struct proto_handler *_p)
239#endif
240{
241 int error = -1;
242
243 LIBALIAS_WLOCK();
244#ifdef VBOX
245 error = _detach_handler(pData, _p);
246#else
247 error = _detach_handler(_p);
248#endif
249 LIBALIAS_WUNLOCK();
250 return (error);
251}
252
253int
254find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *pip,
255 struct alias_data *ad)
256{
257#ifdef VBOX
258 PNATState pData = la->pData;
259#endif
260 struct proto_handler *p;
261 int error = ENOENT;
262
263 LIBALIAS_RLOCK();
264
265 LIST_FOREACH(p, &handler_chain, entries) {
266 if ((p->dir & dir) && (p->proto & proto))
267 if (p->fingerprint(la, pip, ad) == 0) {
268 error = p->protohandler(la, pip, ad);
269 break;
270 }
271 }
272 LIBALIAS_RUNLOCK();
273 return (error);
274}
275
276struct proto_handler *
277#ifdef VBOX
278first_handler(PNATState pData)
279#else
280first_handler(void)
281#endif
282{
283
284 return (LIST_FIRST(&handler_chain));
285}
286
287/* Dll manipulation code - this code is not thread safe... */
288
289int
290attach_dll(struct dll *p)
291{
292 struct dll *b;
293
294 SLIST_FOREACH(b, &dll_chain, next) {
295 if (!strncmp(b->name, p->name, DLL_LEN))
296 return (EEXIST); /* Dll name conflict. */
297 }
298 SLIST_INSERT_HEAD(&dll_chain, p, next);
299 return (0);
300}
301
302void *
303detach_dll(char *p)
304{
305 struct dll *b = NULL, *b_tmp;
306 void *error = NULL;
307
308 SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
309 if (!strncmp(b->name, p, DLL_LEN)) {
310 SLIST_REMOVE(&dll_chain, b, dll, next);
311 error = b;
312 break;
313 }
314 return (error);
315}
316
317struct dll *
318walk_dll_chain(void)
319{
320 struct dll *t;
321
322 t = SLIST_FIRST(&dll_chain);
323 if (t == NULL)
324 return (NULL);
325 SLIST_REMOVE_HEAD(&dll_chain, next);
326 return (t);
327}
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