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
28 #include <avahi-common/malloc.h>
29 #include <avahi-common/timeval.h>
36 #include "multicast-lookup.h"
39 struct AvahiMulticastLookup {
40 AvahiMulticastLookupEngine *engine;
43 AvahiKey *key, *cname_key;
45 AvahiMulticastLookupCallback callback;
48 AvahiIfIndex interface;
49 AvahiProtocol protocol;
53 AvahiTimeEvent *all_for_now_event;
55 AVAHI_LLIST_FIELDS(AvahiMulticastLookup, lookups);
56 AVAHI_LLIST_FIELDS(AvahiMulticastLookup, by_key);
59 struct AvahiMulticastLookupEngine {
63 AVAHI_LLIST_HEAD(AvahiMulticastLookup, lookups);
64 AvahiHashmap *lookups_by_key;
69 static void all_for_now_callback(AvahiTimeEvent *e, void* userdata) {
70 AvahiMulticastLookup *l = userdata;
75 avahi_time_event_free(l->all_for_now_event);
76 l->all_for_now_event = NULL;
78 l->callback(l->engine, l->interface, l->protocol, AVAHI_BROWSER_ALL_FOR_NOW, AVAHI_LOOKUP_RESULT_MULTICAST, NULL, l->userdata);
81 AvahiMulticastLookup *avahi_multicast_lookup_new(
82 AvahiMulticastLookupEngine *e,
83 AvahiIfIndex interface,
84 AvahiProtocol protocol,
86 AvahiMulticastLookupCallback callback,
89 AvahiMulticastLookup *l, *t;
93 assert(AVAHI_IF_VALID(interface));
94 assert(AVAHI_PROTO_VALID(protocol));
98 l = avahi_new(AvahiMulticastLookup, 1);
101 l->key = avahi_key_ref(key);
102 l->cname_key = avahi_key_new_cname(l->key);
103 l->callback = callback;
104 l->userdata = userdata;
105 l->interface = interface;
106 l->protocol = protocol;
107 l->all_for_now_event = NULL;
108 l->queriers_added = 0;
110 t = avahi_hashmap_lookup(e->lookups_by_key, l->key);
111 AVAHI_LLIST_PREPEND(AvahiMulticastLookup, by_key, t, l);
112 avahi_hashmap_replace(e->lookups_by_key, avahi_key_ref(l->key), t);
114 AVAHI_LLIST_PREPEND(AvahiMulticastLookup, lookups, e->lookups, l);
116 avahi_querier_add_for_all(e->server, interface, protocol, l->key, &tv);
117 l->queriers_added = 1;
120 avahi_timeval_add(&tv, 1000000);
122 /* Issue the ALL_FOR_NOW event one second after the querier was initially created */
123 l->all_for_now_event = avahi_time_event_new(e->server->time_event_queue, &tv, all_for_now_callback, l);
128 static void lookup_stop(AvahiMulticastLookup *l) {
133 if (l->queriers_added) {
134 avahi_querier_remove_for_all(l->engine->server, l->interface, l->protocol, l->key);
135 l->queriers_added = 0;
138 if (l->all_for_now_event) {
139 avahi_time_event_free(l->all_for_now_event);
140 l->all_for_now_event = NULL;
144 static void lookup_destroy(AvahiMulticastLookup *l) {
145 AvahiMulticastLookup *t;
150 t = avahi_hashmap_lookup(l->engine->lookups_by_key, l->key);
151 AVAHI_LLIST_REMOVE(AvahiMulticastLookup, by_key, t, l);
153 avahi_hashmap_replace(l->engine->lookups_by_key, avahi_key_ref(l->key), t);
155 avahi_hashmap_remove(l->engine->lookups_by_key, l->key);
157 AVAHI_LLIST_REMOVE(AvahiMulticastLookup, lookups, l->engine->lookups, l);
160 avahi_key_unref(l->key);
163 avahi_key_unref(l->cname_key);
168 void avahi_multicast_lookup_free(AvahiMulticastLookup *l) {
175 l->engine->cleanup_dead = 1;
179 void avahi_multicast_lookup_engine_cleanup(AvahiMulticastLookupEngine *e) {
180 AvahiMulticastLookup *l, *n;
183 while (e->cleanup_dead) {
186 for (l = e->lookups; l; l = n) {
196 AvahiMulticastLookupEngine *engine;
197 AvahiMulticastLookupCallback callback;
199 AvahiKey *key, *cname_key;
200 AvahiInterface *interface;
204 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
205 struct cbdata *cbdata = userdata;
214 cbdata->interface->hardware->index,
215 cbdata->interface->protocol,
217 AVAHI_LOOKUP_RESULT_CACHED|AVAHI_LOOKUP_RESULT_MULTICAST,
226 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
227 struct cbdata *cbdata = userdata;
233 cbdata->interface = i;
235 avahi_cache_walk(i->cache, cbdata->key, scan_cache_callback, cbdata);
237 if (cbdata->cname_key)
238 avahi_cache_walk(i->cache, cbdata->cname_key, scan_cache_callback, cbdata);
240 cbdata->interface = NULL;
243 unsigned avahi_multicast_lookup_engine_scan_cache(
244 AvahiMulticastLookupEngine *e,
245 AvahiIfIndex interface,
246 AvahiProtocol protocol,
248 AvahiMulticastLookupCallback callback,
251 struct cbdata cbdata;
257 assert(AVAHI_IF_VALID(interface));
258 assert(AVAHI_PROTO_VALID(protocol));
262 cbdata.cname_key = avahi_key_new_cname(key);
263 cbdata.callback = callback;
264 cbdata.userdata = userdata;
265 cbdata.interface = NULL;
268 avahi_interface_monitor_walk(e->server->monitor, interface, protocol, scan_interface_callback, &cbdata);
270 if (cbdata.cname_key)
271 avahi_key_unref(cbdata.cname_key);
273 return cbdata.n_found;
276 void avahi_multicast_lookup_engine_new_interface(AvahiMulticastLookupEngine *e, AvahiInterface *i) {
277 AvahiMulticastLookup *l;
282 for (l = e->lookups; l; l = l->lookups_next) {
284 if (l->dead || !l->callback)
287 if (l->queriers_added && avahi_interface_match(i, l->interface, l->protocol))
288 avahi_querier_add(i, l->key, NULL);
292 void avahi_multicast_lookup_engine_notify(AvahiMulticastLookupEngine *e, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
293 AvahiMulticastLookup *l;
299 for (l = avahi_hashmap_lookup(e->lookups_by_key, record->key); l; l = l->by_key_next) {
300 if (l->dead || !l->callback)
303 if (avahi_interface_match(i, l->interface, l->protocol))
304 l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
308 if (record->key->clazz == AVAHI_DNS_CLASS_IN && record->key->type == AVAHI_DNS_TYPE_CNAME) {
309 /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
311 for (l = e->lookups; l; l = l->lookups_next) {
314 if (l->dead || !l->callback)
317 if ((key = avahi_key_new_cname(l->key))) {
318 if (avahi_key_equal(record->key, key))
319 l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
321 avahi_key_unref(key);
327 AvahiMulticastLookupEngine *avahi_multicast_lookup_engine_new(AvahiServer *s) {
328 AvahiMulticastLookupEngine *e;
332 e = avahi_new(AvahiMulticastLookupEngine, 1);
336 /* Initialize lookup list */
337 e->lookups_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
338 AVAHI_LLIST_HEAD_INIT(AvahiWideAreaLookup, e->lookups);
343 void avahi_multicast_lookup_engine_free(AvahiMulticastLookupEngine *e) {
347 lookup_destroy(e->lookups);
349 avahi_hashmap_free(e->lookups_by_key);