4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-common/malloc.h>
33 #include "multicast-lookup.h"
35 struct AvahiMulticastLookup {
36 AvahiMulticastLookupEngine *engine;
39 AvahiKey *key, *cname_key;
41 AvahiMulticastLookupCallback callback;
44 AvahiIfIndex interface;
45 AvahiProtocol protocol;
49 AvahiTimeEvent *all_for_now_event;
51 AVAHI_LLIST_FIELDS(AvahiMulticastLookup, lookups);
52 AVAHI_LLIST_FIELDS(AvahiMulticastLookup, by_key);
55 struct AvahiMulticastLookupEngine {
59 AVAHI_LLIST_HEAD(AvahiMulticastLookup, lookups);
60 AvahiHashmap *lookups_by_key;
65 static void all_for_now_callback(AvahiTimeEvent *e, void* userdata) {
66 AvahiMulticastLookup *l = userdata;
71 avahi_time_event_free(l->all_for_now_event);
72 l->all_for_now_event = NULL;
74 l->callback(l->engine, l->interface, l->protocol, AVAHI_BROWSER_ALL_FOR_NOW, AVAHI_LOOKUP_RESULT_MULTICAST, NULL, l->userdata);
77 AvahiMulticastLookup *avahi_multicast_lookup_new(
78 AvahiMulticastLookupEngine *e,
79 AvahiIfIndex interface,
80 AvahiProtocol protocol,
82 AvahiMulticastLookupCallback callback,
85 AvahiMulticastLookup *l, *t;
89 assert(AVAHI_IF_VALID(interface));
90 assert(AVAHI_PROTO_VALID(protocol));
94 l = avahi_new(AvahiMulticastLookup, 1);
97 l->key = avahi_key_ref(key);
98 l->cname_key = avahi_key_new_cname(l->key);
99 l->callback = callback;
100 l->userdata = userdata;
101 l->interface = interface;
102 l->protocol = protocol;
103 l->all_for_now_event = NULL;
104 l->queriers_added = 0;
106 t = avahi_hashmap_lookup(e->lookups_by_key, l->key);
107 AVAHI_LLIST_PREPEND(AvahiMulticastLookup, by_key, t, l);
108 avahi_hashmap_replace(e->lookups_by_key, avahi_key_ref(l->key), t);
110 AVAHI_LLIST_PREPEND(AvahiMulticastLookup, lookups, e->lookups, l);
112 avahi_querier_add_for_all(e->server, interface, protocol, l->key, &tv);
113 l->queriers_added = 1;
116 avahi_timeval_add(&tv, 1000000);
118 l->all_for_now_event = avahi_time_event_new(e->server->time_event_queue, &tv, all_for_now_callback, l);
123 static void lookup_stop(AvahiMulticastLookup *l) {
128 if (l->queriers_added) {
129 avahi_querier_remove_for_all(l->engine->server, l->interface, l->protocol, l->key);
130 l->queriers_added = 0;
133 if (l->all_for_now_event) {
134 avahi_time_event_free(l->all_for_now_event);
135 l->all_for_now_event = NULL;
139 static void lookup_destroy(AvahiMulticastLookup *l) {
140 AvahiMulticastLookup *t;
145 t = avahi_hashmap_lookup(l->engine->lookups_by_key, l->key);
146 AVAHI_LLIST_REMOVE(AvahiMulticastLookup, by_key, t, l);
148 avahi_hashmap_replace(l->engine->lookups_by_key, avahi_key_ref(l->key), t);
150 avahi_hashmap_remove(l->engine->lookups_by_key, l->key);
152 AVAHI_LLIST_REMOVE(AvahiMulticastLookup, lookups, l->engine->lookups, l);
155 avahi_key_unref(l->key);
158 avahi_key_unref(l->cname_key);
163 void avahi_multicast_lookup_free(AvahiMulticastLookup *l) {
170 l->engine->cleanup_dead = 1;
174 void avahi_multicast_lookup_engine_cleanup(AvahiMulticastLookupEngine *e) {
175 AvahiMulticastLookup *l, *n;
178 while (e->cleanup_dead) {
181 for (l = e->lookups; l; l = n) {
191 AvahiMulticastLookupEngine *engine;
192 AvahiMulticastLookupCallback callback;
194 AvahiKey *key, *cname_key;
195 AvahiInterface *interface;
199 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
200 struct cbdata *cbdata = userdata;
209 cbdata->interface->hardware->index,
210 cbdata->interface->protocol,
212 AVAHI_LOOKUP_RESULT_CACHED|AVAHI_LOOKUP_RESULT_MULTICAST,
221 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
222 struct cbdata *cbdata = userdata;
228 cbdata->interface = i;
230 avahi_cache_walk(i->cache, cbdata->key, scan_cache_callback, cbdata);
232 if (cbdata->cname_key)
233 avahi_cache_walk(i->cache, cbdata->cname_key, scan_cache_callback, cbdata);
235 cbdata->interface = NULL;
238 unsigned avahi_multicast_lookup_engine_scan_cache(
239 AvahiMulticastLookupEngine *e,
240 AvahiIfIndex interface,
241 AvahiProtocol protocol,
243 AvahiMulticastLookupCallback callback,
246 struct cbdata cbdata;
252 assert(AVAHI_IF_VALID(interface));
253 assert(AVAHI_PROTO_VALID(protocol));
257 cbdata.cname_key = avahi_key_new_cname(key);
258 cbdata.callback = callback;
259 cbdata.userdata = userdata;
260 cbdata.interface = NULL;
263 avahi_interface_monitor_walk(e->server->monitor, interface, protocol, scan_interface_callback, &cbdata);
265 if (cbdata.cname_key)
266 avahi_key_unref(cbdata.cname_key);
268 return cbdata.n_found;
271 void avahi_multicast_lookup_engine_new_interface(AvahiMulticastLookupEngine *e, AvahiInterface *i) {
272 AvahiMulticastLookup *l;
277 for (l = e->lookups; l; l = l->lookups_next) {
279 if (l->dead || !l->callback)
282 if (l->queriers_added && avahi_interface_match(i, l->interface, l->protocol))
283 avahi_querier_add(i, l->key, NULL);
287 void avahi_multicast_lookup_engine_notify(AvahiMulticastLookupEngine *e, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
288 AvahiMulticastLookup *l;
294 for (l = avahi_hashmap_lookup(e->lookups_by_key, record->key); l; l = l->by_key_next) {
295 if (l->dead || !l->callback)
298 if (avahi_interface_match(i, l->interface, l->protocol))
299 l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
303 if (record->key->clazz == AVAHI_DNS_CLASS_IN && record->key->type == AVAHI_DNS_TYPE_CNAME) {
304 /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
306 for (l = e->lookups; l; l = l->lookups_next) {
309 if (l->dead || !l->callback)
312 if ((key = avahi_key_new_cname(l->key))) {
313 if (avahi_key_equal(record->key, key))
314 l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
316 avahi_key_unref(key);
322 AvahiMulticastLookupEngine *avahi_multicast_lookup_engine_new(AvahiServer *s) {
323 AvahiMulticastLookupEngine *e;
327 e = avahi_new(AvahiMulticastLookupEngine, 1);
331 /* Initialize lookup list */
332 e->lookups_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
333 AVAHI_LLIST_HEAD_INIT(AvahiWideAreaLookup, e->lookups);
338 void avahi_multicast_lookup_engine_free(AvahiMulticastLookupEngine *e) {
342 lookup_destroy(e->lookups);
344 avahi_hashmap_free(e->lookups_by_key);