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/timeval.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/error.h>
31 #include <avahi-common/domain.h>
32 #include <avahi-common/rlist.h>
33 #include <avahi-common/address.h>
39 #define AVAHI_MAX_LOOKUPS_PER_BROWSER 15
41 struct AvahiSRBLookup {
42 AvahiSRecordBrowser *record_browser;
46 AvahiIfIndex interface;
47 AvahiProtocol protocol;
48 AvahiLookupFlags flags;
52 AvahiWideAreaLookup *wide_area;
53 AvahiMulticastLookup *multicast;
55 AvahiRList *cname_lookups;
57 AVAHI_LLIST_FIELDS(AvahiSRBLookup, lookups);
60 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r);
61 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r);
63 static void transport_flags_from_domain(AvahiServer *s, AvahiLookupFlags *flags, const char *domain) {
67 assert(!((*flags & AVAHI_LOOKUP_USE_MULTICAST) && (*flags & AVAHI_LOOKUP_USE_WIDE_AREA)));
69 if (*flags & (AVAHI_LOOKUP_USE_MULTICAST|AVAHI_LOOKUP_USE_WIDE_AREA))
72 if (!s->wide_area_lookup_engine ||
73 !avahi_wide_area_has_servers(s->wide_area_lookup_engine) ||
74 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_LOCAL) ||
75 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV4) ||
76 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV6))
77 *flags |= AVAHI_LOOKUP_USE_MULTICAST;
79 *flags |= AVAHI_LOOKUP_USE_WIDE_AREA;
83 static AvahiSRBLookup* lookup_new(
84 AvahiSRecordBrowser *b,
85 AvahiIfIndex interface,
86 AvahiProtocol protocol,
87 AvahiLookupFlags flags,
93 assert(AVAHI_IF_VALID(interface));
94 assert(AVAHI_PROTO_VALID(protocol));
96 if (b->n_lookups >= AVAHI_MAX_LOOKUPS_PER_BROWSER)
97 /* We don't like cyclic CNAMEs */
100 if (!(l = avahi_new(AvahiSRBLookup, 1)))
104 l->record_browser = b;
105 l->interface = interface;
106 l->protocol = protocol;
107 l->key = avahi_key_ref(key);
110 l->cname_lookups = NULL;
113 transport_flags_from_domain(b->server, &l->flags, key->name);
115 AVAHI_LLIST_PREPEND(AvahiSRBLookup, lookups, b->lookups, l);
122 static void lookup_unref(AvahiSRBLookup *l) {
129 AVAHI_LLIST_REMOVE(AvahiSRBLookup, lookups, l->record_browser->lookups, l);
130 l->record_browser->n_lookups --;
133 avahi_wide_area_lookup_free(l->wide_area);
138 avahi_multicast_lookup_free(l->multicast);
142 while (l->cname_lookups) {
143 lookup_unref(l->cname_lookups->data);
144 l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, l->cname_lookups);
147 avahi_key_unref(l->key);
151 static AvahiSRBLookup* lookup_ref(AvahiSRBLookup *l) {
159 static AvahiSRBLookup *lookup_find(
160 AvahiSRecordBrowser *b,
161 AvahiIfIndex interface,
162 AvahiProtocol protocol,
163 AvahiLookupFlags flags,
170 for (l = b->lookups; l; l = l->lookups_next) {
172 if ((l->interface == AVAHI_IF_UNSPEC || l->interface == interface) &&
173 (l->interface == AVAHI_PROTO_UNSPEC || l->protocol == protocol) &&
175 avahi_key_equal(l->key, key))
183 static void lookup_wide_area_callback(
184 AvahiWideAreaLookupEngine *e,
185 AvahiBrowserEvent event,
186 AvahiLookupResultFlags flags,
190 AvahiSRBLookup *l = userdata;
191 AvahiSRecordBrowser *b;
196 b = l->record_browser;
204 case AVAHI_BROWSER_NEW:
207 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
208 r->key->type == AVAHI_DNS_TYPE_CNAME)
209 /* It's a CNAME record, so let's follow it. We only follow it on wide area DNS! */
210 lookup_handle_cname(l, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_WIDE_AREA, r);
212 /* It's a normal record, so let's call the user callback */
213 assert(avahi_key_equal(r->key, l->key));
215 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, r, flags, b->userdata);
219 case AVAHI_BROWSER_REMOVE:
220 case AVAHI_BROWSER_CACHE_EXHAUSTED:
221 /* Not defined for wide area DNS */
224 case AVAHI_BROWSER_ALL_FOR_NOW:
225 case AVAHI_BROWSER_FAILURE:
227 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
235 static void lookup_multicast_callback(
236 AvahiMulticastLookupEngine *e,
237 AvahiIfIndex interface,
238 AvahiProtocol protocol,
239 AvahiBrowserEvent event,
240 AvahiLookupResultFlags flags,
244 AvahiSRBLookup *l = userdata;
245 AvahiSRecordBrowser *b;
250 b = l->record_browser;
258 case AVAHI_BROWSER_NEW:
261 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
262 r->key->type == AVAHI_DNS_TYPE_CNAME)
263 /* It's a CNAME record, so let's follow it. We allow browsing on both multicast and wide area. */
264 lookup_handle_cname(l, interface, protocol, b->flags, r);
266 /* It's a normal record, so let's call the user callback */
267 assert(avahi_key_equal(b->key, l->key));
269 b->callback(b, interface, protocol, event, r, flags, b->userdata);
273 case AVAHI_BROWSER_REMOVE:
276 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
277 r->key->type == AVAHI_DNS_TYPE_CNAME)
278 /* It's a CNAME record, so let's drop that query! */
279 lookup_drop_cname(l, interface, protocol, 0, r);
281 /* It's a normal record, so let's call the user callback */
282 assert(avahi_key_equal(b->key, l->key));
284 b->callback(b, interface, protocol, event, r, flags, b->userdata);
288 case AVAHI_BROWSER_ALL_FOR_NOW:
290 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
293 case AVAHI_BROWSER_CACHE_EXHAUSTED:
294 case AVAHI_BROWSER_FAILURE:
295 /* Not defined for multicast DNS */
303 static int lookup_start(AvahiSRBLookup *l) {
306 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
307 assert(!l->wide_area && !l->multicast);
309 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
311 if (!(l->wide_area = avahi_wide_area_lookup_new(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l)))
315 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
317 if (!(l->multicast = avahi_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l)))
324 static int lookup_scan_cache(AvahiSRBLookup *l) {
329 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
332 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
333 n = (int) avahi_wide_area_scan_cache(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l);
336 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
337 n = (int) avahi_multicast_lookup_engine_scan_cache(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l);
343 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
349 if ((l = lookup_find(b, interface, protocol, flags, key)))
350 return lookup_ref(l);
352 if (!(l = lookup_new(b, interface, protocol, flags, key)))
358 static int lookup_go(AvahiSRBLookup *l) {
362 if (l->record_browser->dead)
367 /* Browse the cache for the root request */
368 n = lookup_scan_cache(l);
370 /* Start the lookup */
371 if (!l->record_browser->dead && l->ref > 1) {
373 if ((l->flags & AVAHI_LOOKUP_USE_MULTICAST) || n == 0)
374 /* We do no start a query if the cache contained entries and we're on wide area */
376 if (lookup_start(l) < 0)
385 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
392 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
393 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
395 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
396 n = lookup_add(l->record_browser, interface, protocol, flags, k);
400 avahi_log_debug(__FILE__": Failed to create SRBLookup.");
404 l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
409 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
411 AvahiSRBLookup *n = NULL;
414 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
415 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
417 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
419 for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
424 if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
425 (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
427 avahi_key_equal(n->key, k))
434 l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
439 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
440 AvahiSRecordBrowser *b = userdata;
446 /* Remove the defer timeout */
447 if (b->defer_time_event) {
448 avahi_time_event_free(b->defer_time_event);
449 b->defer_time_event = NULL;
452 /* Create initial query */
453 assert(!b->root_lookup);
454 b->root_lookup = lookup_add(b, b->interface, b->protocol, b->flags, b->key);
455 assert(b->root_lookup);
457 n = lookup_go(b->root_lookup);
463 /* sending of the initial query failed */
465 avahi_server_set_errno(b->server, AVAHI_ERR_FAILURE);
468 b, b->interface, b->protocol, AVAHI_BROWSER_FAILURE, NULL,
469 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
472 avahi_s_record_browser_cancel(b);
476 /* Tell the client that we're done with the cache */
478 b, b->interface, b->protocol, AVAHI_BROWSER_CACHE_EXHAUSTED, NULL,
479 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
482 if (!b->dead && b->root_lookup && b->root_lookup->flags & AVAHI_LOOKUP_USE_WIDE_AREA && n > 0) {
484 /* If we do wide area lookups and the the cache contained
485 * entries, we assume that it is complete, and tell the user
486 * so by firing ALL_FOR_NOW. */
488 b->callback(b, b->interface, b->protocol, AVAHI_BROWSER_ALL_FOR_NOW, NULL, AVAHI_LOOKUP_RESULT_WIDE_AREA, b->userdata);
492 void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
496 avahi_s_record_browser_cancel(b);
498 /* Request a new iteration of the cache scanning */
499 if (!b->defer_time_event) {
500 b->defer_time_event = avahi_time_event_new(b->server->time_event_queue, NULL, defer_callback, b);
501 assert(b->defer_time_event);
505 AvahiSRecordBrowser *avahi_s_record_browser_new(
507 AvahiIfIndex interface,
508 AvahiProtocol protocol,
510 AvahiLookupFlags flags,
511 AvahiSRecordBrowserCallback callback,
514 AvahiSRecordBrowser *b;
520 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
521 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
522 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !avahi_key_is_pattern(key), AVAHI_ERR_IS_PATTERN);
523 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_key_is_valid(key), AVAHI_ERR_INVALID_KEY);
524 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
525 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !(flags & AVAHI_LOOKUP_USE_WIDE_AREA) || !(flags & AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
527 if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
528 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
534 b->interface = interface;
535 b->protocol = protocol;
536 b->key = avahi_key_ref(key);
538 b->callback = callback;
539 b->userdata = userdata;
541 AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
542 b->root_lookup = NULL;
544 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
546 /* The currently cached entries are scanned a bit later, and than we will start querying, too */
547 b->defer_time_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
548 assert(b->defer_time_event);
553 void avahi_s_record_browser_cancel(AvahiSRecordBrowser *b) {
556 if (b->root_lookup) {
557 lookup_unref(b->root_lookup);
558 b->root_lookup = NULL;
561 if (b->defer_time_event) {
562 avahi_time_event_free(b->defer_time_event);
563 b->defer_time_event = NULL;
567 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
572 b->server->need_browser_cleanup = 1;
574 avahi_s_record_browser_cancel(b);
577 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
580 avahi_s_record_browser_cancel(b);
582 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
584 avahi_key_unref(b->key);
589 void avahi_browser_cleanup(AvahiServer *server) {
590 AvahiSRecordBrowser *b;
591 AvahiSRecordBrowser *n;
595 while (server->need_browser_cleanup) {
596 server->need_browser_cleanup = 0;
598 for (b = server->record_browsers; b; b = n) {
602 avahi_s_record_browser_destroy(b);
606 if (server->wide_area_lookup_engine)
607 avahi_wide_area_cleanup(server->wide_area_lookup_engine);
608 avahi_multicast_lookup_engine_cleanup(server->multicast_lookup_engine);