2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <catta/malloc.h>
27 #include <catta/timeval.h>
32 #include <catta/log.h>
34 #include "multicast-lookup.h"
37 struct CattaMulticastLookup {
38 CattaMulticastLookupEngine *engine;
41 CattaKey *key, *cname_key;
43 CattaMulticastLookupCallback callback;
47 CattaProtocol protocol;
51 CattaTimeEvent *all_for_now_event;
53 CATTA_LLIST_FIELDS(CattaMulticastLookup, lookups);
54 CATTA_LLIST_FIELDS(CattaMulticastLookup, by_key);
57 struct CattaMulticastLookupEngine {
61 CATTA_LLIST_HEAD(CattaMulticastLookup, lookups);
62 CattaHashmap *lookups_by_key;
67 static void all_for_now_callback(CattaTimeEvent *e, void* userdata) {
68 CattaMulticastLookup *l = userdata;
73 catta_time_event_free(l->all_for_now_event);
74 l->all_for_now_event = NULL;
76 l->callback(l->engine, l->iface, l->protocol, CATTA_BROWSER_ALL_FOR_NOW, CATTA_LOOKUP_RESULT_MULTICAST, NULL, l->userdata);
79 CattaMulticastLookup *catta_multicast_lookup_new(
80 CattaMulticastLookupEngine *e,
82 CattaProtocol protocol,
84 CattaMulticastLookupCallback callback,
87 CattaMulticastLookup *l, *t;
91 assert(CATTA_IF_VALID(iface));
92 assert(CATTA_PROTO_VALID(protocol));
96 l = catta_new(CattaMulticastLookup, 1);
99 l->key = catta_key_ref(key);
100 l->cname_key = catta_key_new_cname(l->key);
101 l->callback = callback;
102 l->userdata = userdata;
104 l->protocol = protocol;
105 l->all_for_now_event = NULL;
106 l->queriers_added = 0;
108 t = catta_hashmap_lookup(e->lookups_by_key, l->key);
109 CATTA_LLIST_PREPEND(CattaMulticastLookup, by_key, t, l);
110 catta_hashmap_replace(e->lookups_by_key, catta_key_ref(l->key), t);
112 CATTA_LLIST_PREPEND(CattaMulticastLookup, lookups, e->lookups, l);
114 catta_querier_add_for_all(e->server, iface, protocol, l->key, &tv);
115 l->queriers_added = 1;
118 catta_timeval_add(&tv, 1000000);
120 /* Issue the ALL_FOR_NOW event one second after the querier was initially created */
121 l->all_for_now_event = catta_time_event_new(e->server->time_event_queue, &tv, all_for_now_callback, l);
126 static void lookup_stop(CattaMulticastLookup *l) {
131 if (l->queriers_added) {
132 catta_querier_remove_for_all(l->engine->server, l->iface, l->protocol, l->key);
133 l->queriers_added = 0;
136 if (l->all_for_now_event) {
137 catta_time_event_free(l->all_for_now_event);
138 l->all_for_now_event = NULL;
142 static void lookup_destroy(CattaMulticastLookup *l) {
143 CattaMulticastLookup *t;
148 t = catta_hashmap_lookup(l->engine->lookups_by_key, l->key);
149 CATTA_LLIST_REMOVE(CattaMulticastLookup, by_key, t, l);
151 catta_hashmap_replace(l->engine->lookups_by_key, catta_key_ref(l->key), t);
153 catta_hashmap_remove(l->engine->lookups_by_key, l->key);
155 CATTA_LLIST_REMOVE(CattaMulticastLookup, lookups, l->engine->lookups, l);
158 catta_key_unref(l->key);
161 catta_key_unref(l->cname_key);
166 void catta_multicast_lookup_free(CattaMulticastLookup *l) {
173 l->engine->cleanup_dead = 1;
177 void catta_multicast_lookup_engine_cleanup(CattaMulticastLookupEngine *e) {
178 CattaMulticastLookup *l, *n;
181 while (e->cleanup_dead) {
184 for (l = e->lookups; l; l = n) {
194 CattaMulticastLookupEngine *engine;
195 CattaMulticastLookupCallback callback;
197 CattaKey *key, *cname_key;
198 CattaInterface *iface;
202 static void* scan_cache_callback(CattaCache *c, CattaKey *pattern, CattaCacheEntry *e, void* userdata) {
203 struct cbdata *cbdata = userdata;
212 cbdata->iface->hardware->index,
213 cbdata->iface->protocol,
215 CATTA_LOOKUP_RESULT_CACHED|CATTA_LOOKUP_RESULT_MULTICAST,
224 static void scan_interface_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
225 struct cbdata *cbdata = userdata;
233 catta_cache_walk(i->cache, cbdata->key, scan_cache_callback, cbdata);
235 if (cbdata->cname_key)
236 catta_cache_walk(i->cache, cbdata->cname_key, scan_cache_callback, cbdata);
238 cbdata->iface = NULL;
241 unsigned catta_multicast_lookup_engine_scan_cache(
242 CattaMulticastLookupEngine *e,
244 CattaProtocol protocol,
246 CattaMulticastLookupCallback callback,
249 struct cbdata cbdata;
255 assert(CATTA_IF_VALID(iface));
256 assert(CATTA_PROTO_VALID(protocol));
260 cbdata.cname_key = catta_key_new_cname(key);
261 cbdata.callback = callback;
262 cbdata.userdata = userdata;
266 catta_interface_monitor_walk(e->server->monitor, iface, protocol, scan_interface_callback, &cbdata);
268 if (cbdata.cname_key)
269 catta_key_unref(cbdata.cname_key);
271 return cbdata.n_found;
274 void catta_multicast_lookup_engine_new_interface(CattaMulticastLookupEngine *e, CattaInterface *i) {
275 CattaMulticastLookup *l;
280 for (l = e->lookups; l; l = l->lookups_next) {
282 if (l->dead || !l->callback)
285 if (l->queriers_added && catta_interface_match(i, l->iface, l->protocol))
286 catta_querier_add(i, l->key, NULL);
290 void catta_multicast_lookup_engine_notify(CattaMulticastLookupEngine *e, CattaInterface *i, CattaRecord *record, CattaBrowserEvent event) {
291 CattaMulticastLookup *l;
297 for (l = catta_hashmap_lookup(e->lookups_by_key, record->key); l; l = l->by_key_next) {
298 if (l->dead || !l->callback)
301 if (catta_interface_match(i, l->iface, l->protocol))
302 l->callback(e, i->hardware->index, i->protocol, event, CATTA_LOOKUP_RESULT_MULTICAST, record, l->userdata);
306 if (record->key->clazz == CATTA_DNS_CLASS_IN && record->key->type == CATTA_DNS_TYPE_CNAME) {
307 /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
309 for (l = e->lookups; l; l = l->lookups_next) {
312 if (l->dead || !l->callback)
315 if ((key = catta_key_new_cname(l->key))) {
316 if (catta_key_equal(record->key, key))
317 l->callback(e, i->hardware->index, i->protocol, event, CATTA_LOOKUP_RESULT_MULTICAST, record, l->userdata);
319 catta_key_unref(key);
325 CattaMulticastLookupEngine *catta_multicast_lookup_engine_new(CattaServer *s) {
326 CattaMulticastLookupEngine *e;
330 e = catta_new(CattaMulticastLookupEngine, 1);
334 /* Initialize lookup list */
335 e->lookups_by_key = catta_hashmap_new((CattaHashFunc) catta_key_hash, (CattaEqualFunc) catta_key_equal, (CattaFreeFunc) catta_key_unref, NULL);
336 CATTA_LLIST_HEAD_INIT(CattaWideAreaLookup, e->lookups);
341 void catta_multicast_lookup_engine_free(CattaMulticastLookupEngine *e) {
345 lookup_destroy(e->lookups);
347 catta_hashmap_free(e->lookups_by_key);