2 This file is part of avahi.
4 avahi 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 avahi 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 avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-common/malloc.h>
27 #include <avahi-common/timeval.h>
34 #include "multicast-lookup.h"
37 struct AvahiMulticastLookup {
38 AvahiMulticastLookupEngine *engine;
41 AvahiKey *key, *cname_key;
43 AvahiMulticastLookupCallback callback;
46 AvahiIfIndex interface;
47 AvahiProtocol protocol;
51 AvahiTimeEvent *all_for_now_event;
53 AVAHI_LLIST_FIELDS(AvahiMulticastLookup, lookups);
54 AVAHI_LLIST_FIELDS(AvahiMulticastLookup, by_key);
57 struct AvahiMulticastLookupEngine {
61 AVAHI_LLIST_HEAD(AvahiMulticastLookup, lookups);
62 AvahiHashmap *lookups_by_key;
67 static void all_for_now_callback(AvahiTimeEvent *e, void* userdata) {
68 AvahiMulticastLookup *l = userdata;
73 avahi_time_event_free(l->all_for_now_event);
74 l->all_for_now_event = NULL;
76 l->callback(l->engine, l->interface, l->protocol, AVAHI_BROWSER_ALL_FOR_NOW, AVAHI_LOOKUP_RESULT_MULTICAST, NULL, l->userdata);
79 AvahiMulticastLookup *avahi_multicast_lookup_new(
80 AvahiMulticastLookupEngine *e,
81 AvahiIfIndex interface,
82 AvahiProtocol protocol,
84 AvahiMulticastLookupCallback callback,
87 AvahiMulticastLookup *l, *t;
91 assert(AVAHI_IF_VALID(interface));
92 assert(AVAHI_PROTO_VALID(protocol));
96 l = avahi_new(AvahiMulticastLookup, 1);
99 l->key = avahi_key_ref(key);
100 l->cname_key = avahi_key_new_cname(l->key);
101 l->callback = callback;
102 l->userdata = userdata;
103 l->interface = interface;
104 l->protocol = protocol;
105 l->all_for_now_event = NULL;
106 l->queriers_added = 0;
108 t = avahi_hashmap_lookup(e->lookups_by_key, l->key);
109 AVAHI_LLIST_PREPEND(AvahiMulticastLookup, by_key, t, l);
110 avahi_hashmap_replace(e->lookups_by_key, avahi_key_ref(l->key), t);
112 AVAHI_LLIST_PREPEND(AvahiMulticastLookup, lookups, e->lookups, l);
114 avahi_querier_add_for_all(e->server, interface, protocol, l->key, &tv);
115 l->queriers_added = 1;
118 avahi_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 = avahi_time_event_new(e->server->time_event_queue, &tv, all_for_now_callback, l);
126 static void lookup_stop(AvahiMulticastLookup *l) {
131 if (l->queriers_added) {
132 avahi_querier_remove_for_all(l->engine->server, l->interface, l->protocol, l->key);
133 l->queriers_added = 0;
136 if (l->all_for_now_event) {
137 avahi_time_event_free(l->all_for_now_event);
138 l->all_for_now_event = NULL;
142 static void lookup_destroy(AvahiMulticastLookup *l) {
143 AvahiMulticastLookup *t;
148 t = avahi_hashmap_lookup(l->engine->lookups_by_key, l->key);
149 AVAHI_LLIST_REMOVE(AvahiMulticastLookup, by_key, t, l);
151 avahi_hashmap_replace(l->engine->lookups_by_key, avahi_key_ref(l->key), t);
153 avahi_hashmap_remove(l->engine->lookups_by_key, l->key);
155 AVAHI_LLIST_REMOVE(AvahiMulticastLookup, lookups, l->engine->lookups, l);
158 avahi_key_unref(l->key);
161 avahi_key_unref(l->cname_key);
166 void avahi_multicast_lookup_free(AvahiMulticastLookup *l) {
173 l->engine->cleanup_dead = 1;
177 void avahi_multicast_lookup_engine_cleanup(AvahiMulticastLookupEngine *e) {
178 AvahiMulticastLookup *l, *n;
181 while (e->cleanup_dead) {
184 for (l = e->lookups; l; l = n) {
194 AvahiMulticastLookupEngine *engine;
195 AvahiMulticastLookupCallback callback;
197 AvahiKey *key, *cname_key;
198 AvahiInterface *interface;
202 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
203 struct cbdata *cbdata = userdata;
212 cbdata->interface->hardware->index,
213 cbdata->interface->protocol,
215 AVAHI_LOOKUP_RESULT_CACHED|AVAHI_LOOKUP_RESULT_MULTICAST,
224 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
225 struct cbdata *cbdata = userdata;
231 cbdata->interface = i;
233 avahi_cache_walk(i->cache, cbdata->key, scan_cache_callback, cbdata);
235 if (cbdata->cname_key)
236 avahi_cache_walk(i->cache, cbdata->cname_key, scan_cache_callback, cbdata);
238 cbdata->interface = NULL;
241 unsigned avahi_multicast_lookup_engine_scan_cache(
242 AvahiMulticastLookupEngine *e,
243 AvahiIfIndex interface,
244 AvahiProtocol protocol,
246 AvahiMulticastLookupCallback callback,
249 struct cbdata cbdata;
255 assert(AVAHI_IF_VALID(interface));
256 assert(AVAHI_PROTO_VALID(protocol));
260 cbdata.cname_key = avahi_key_new_cname(key);
261 cbdata.callback = callback;
262 cbdata.userdata = userdata;
263 cbdata.interface = NULL;
266 avahi_interface_monitor_walk(e->server->monitor, interface, protocol, scan_interface_callback, &cbdata);
268 if (cbdata.cname_key)
269 avahi_key_unref(cbdata.cname_key);
271 return cbdata.n_found;
274 void avahi_multicast_lookup_engine_new_interface(AvahiMulticastLookupEngine *e, AvahiInterface *i) {
275 AvahiMulticastLookup *l;
280 for (l = e->lookups; l; l = l->lookups_next) {
282 if (l->dead || !l->callback)
285 if (l->queriers_added && avahi_interface_match(i, l->interface, l->protocol))
286 avahi_querier_add(i, l->key, NULL);
290 void avahi_multicast_lookup_engine_notify(AvahiMulticastLookupEngine *e, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
291 AvahiMulticastLookup *l;
297 for (l = avahi_hashmap_lookup(e->lookups_by_key, record->key); l; l = l->by_key_next) {
298 if (l->dead || !l->callback)
301 if (avahi_interface_match(i, l->interface, l->protocol))
302 l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
306 if (record->key->clazz == AVAHI_DNS_CLASS_IN && record->key->type == AVAHI_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 = avahi_key_new_cname(l->key))) {
316 if (avahi_key_equal(record->key, key))
317 l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
319 avahi_key_unref(key);
325 AvahiMulticastLookupEngine *avahi_multicast_lookup_engine_new(AvahiServer *s) {
326 AvahiMulticastLookupEngine *e;
330 e = avahi_new(AvahiMulticastLookupEngine, 1);
334 /* Initialize lookup list */
335 e->lookups_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
336 AVAHI_LLIST_HEAD_INIT(AvahiWideAreaLookup, e->lookups);
341 void avahi_multicast_lookup_engine_free(AvahiMulticastLookupEngine *e) {
345 lookup_destroy(e->lookups);
347 avahi_hashmap_free(e->lookups_by_key);