2 * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/libkern.h>
32 #include <sys/param.h>
34 #include <sys/rwlock.h>
38 #include <sys/types.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
47 #include <netinet/libalias/alias_local.h>
48 #include <netinet/libalias/alias_mod.h>
50 #include "alias_local.h"
51 #include "alias_mod.h"
54 /* Protocol and userland module handlers chains. */
55 static TAILQ_HEAD(handler_chain, proto_handler) handler_chain =
56 TAILQ_HEAD_INITIALIZER(handler_chain);
59 attach_handler(struct proto_handler *p)
61 struct proto_handler *b;
63 TAILQ_FOREACH(b, &handler_chain, link) {
64 if ((b->pri == p->pri) &&
66 (b->proto == p->proto))
68 if (b->pri > p->pri) {
69 TAILQ_INSERT_BEFORE(b, p, link);
74 TAILQ_INSERT_TAIL(&handler_chain, p, link);
80 LibAliasAttachHandlers(struct proto_handler *p)
84 while (p->dir != NODIR) {
85 error = attach_handler(p);
94 /* XXXGL: should be void, but no good reason to break ABI */
96 LibAliasDetachHandlers(struct proto_handler *p)
99 while (p->dir != NODIR) {
100 TAILQ_REMOVE(&handler_chain, p, link);
108 find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *ip,
109 struct alias_data *ad)
111 struct proto_handler *p;
113 TAILQ_FOREACH(p, &handler_chain, link)
114 if ((p->dir & dir) && (p->proto & proto) &&
115 p->fingerprint(la, ad) == 0)
116 return (p->protohandler(la, ip, ad));
121 struct proto_handler *
125 return (TAILQ_FIRST(&handler_chain));
129 /* Dll manipulation code - this code is not thread safe... */
130 SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain);
132 attach_dll(struct dll *p)
136 SLIST_FOREACH(b, &dll_chain, next) {
137 if (!strncmp(b->name, p->name, DLL_LEN))
138 return (EEXIST); /* Dll name conflict. */
140 SLIST_INSERT_HEAD(&dll_chain, p, next);
147 struct dll *b, *b_tmp;
152 SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
153 if (!strncmp(b->name, p, DLL_LEN)) {
154 SLIST_REMOVE(&dll_chain, b, dll, next);
166 t = SLIST_FIRST(&dll_chain);
169 SLIST_REMOVE_HEAD(&dll_chain, next);
172 #endif /* !_KERNEL */