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>
38 #include "domain-util.h"
41 #define AVAHI_LOOKUPS_PER_BROWSER_MAX 15
43 struct AvahiSRBLookup {
44 AvahiSRecordBrowser *record_browser;
48 AvahiIfIndex interface;
49 AvahiProtocol protocol;
50 AvahiLookupFlags flags;
54 AvahiWideAreaLookup *wide_area;
55 AvahiMulticastLookup *multicast;
57 AvahiRList *cname_lookups;
59 AVAHI_LLIST_FIELDS(AvahiSRBLookup, lookups);
62 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r);
63 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r);
65 static void transport_flags_from_domain(AvahiServer *s, AvahiLookupFlags *flags, const char *domain) {
69 assert(!((*flags & AVAHI_LOOKUP_USE_MULTICAST) && (*flags & AVAHI_LOOKUP_USE_WIDE_AREA)));
71 if (*flags & (AVAHI_LOOKUP_USE_MULTICAST|AVAHI_LOOKUP_USE_WIDE_AREA))
74 if (!s->wide_area_lookup_engine ||
75 !avahi_wide_area_has_servers(s->wide_area_lookup_engine) ||
76 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_LOCAL) ||
77 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV4) ||
78 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV6))
79 *flags |= AVAHI_LOOKUP_USE_MULTICAST;
81 *flags |= AVAHI_LOOKUP_USE_WIDE_AREA;
84 static AvahiSRBLookup* lookup_new(
85 AvahiSRecordBrowser *b,
86 AvahiIfIndex interface,
87 AvahiProtocol protocol,
88 AvahiLookupFlags flags,
94 assert(AVAHI_IF_VALID(interface));
95 assert(AVAHI_PROTO_VALID(protocol));
97 if (b->n_lookups >= AVAHI_LOOKUPS_PER_BROWSER_MAX)
98 /* We don't like cyclic CNAMEs */
101 if (!(l = avahi_new(AvahiSRBLookup, 1)))
105 l->record_browser = b;
106 l->interface = interface;
107 l->protocol = protocol;
108 l->key = avahi_key_ref(key);
111 l->cname_lookups = NULL;
114 transport_flags_from_domain(b->server, &l->flags, key->name);
116 AVAHI_LLIST_PREPEND(AvahiSRBLookup, lookups, b->lookups, l);
123 static void lookup_unref(AvahiSRBLookup *l) {
130 AVAHI_LLIST_REMOVE(AvahiSRBLookup, lookups, l->record_browser->lookups, l);
131 l->record_browser->n_lookups --;
134 avahi_wide_area_lookup_free(l->wide_area);
139 avahi_multicast_lookup_free(l->multicast);
143 while (l->cname_lookups) {
144 lookup_unref(l->cname_lookups->data);
145 l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, l->cname_lookups);
148 avahi_key_unref(l->key);
152 static AvahiSRBLookup* lookup_ref(AvahiSRBLookup *l) {
160 static AvahiSRBLookup *lookup_find(
161 AvahiSRecordBrowser *b,
162 AvahiIfIndex interface,
163 AvahiProtocol protocol,
164 AvahiLookupFlags flags,
171 for (l = b->lookups; l; l = l->lookups_next) {
173 if ((l->interface == AVAHI_IF_UNSPEC || l->interface == interface) &&
174 (l->interface == AVAHI_PROTO_UNSPEC || l->protocol == protocol) &&
176 avahi_key_equal(l->key, key))
184 static void browser_cancel(AvahiSRecordBrowser *b) {
187 if (b->root_lookup) {
188 lookup_unref(b->root_lookup);
189 b->root_lookup = NULL;
192 if (b->defer_time_event) {
193 avahi_time_event_free(b->defer_time_event);
194 b->defer_time_event = NULL;
198 static void lookup_wide_area_callback(
199 AvahiWideAreaLookupEngine *e,
200 AvahiBrowserEvent event,
201 AvahiLookupResultFlags flags,
205 AvahiSRBLookup *l = userdata;
206 AvahiSRecordBrowser *b;
212 b = l->record_browser;
220 case AVAHI_BROWSER_NEW:
223 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
224 r->key->type == AVAHI_DNS_TYPE_CNAME)
225 /* It's a CNAME record, so let's follow it. We only follow it on wide area DNS! */
226 lookup_handle_cname(l, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_WIDE_AREA, r);
228 /* It's a normal record, so let's call the user callback */
229 assert(avahi_key_equal(r->key, l->key));
231 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, r, flags, b->userdata);
235 case AVAHI_BROWSER_REMOVE:
236 case AVAHI_BROWSER_CACHE_EXHAUSTED:
237 /* Not defined for wide area DNS */
240 case AVAHI_BROWSER_ALL_FOR_NOW:
241 case AVAHI_BROWSER_FAILURE:
243 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
251 static void lookup_multicast_callback(
252 AvahiMulticastLookupEngine *e,
253 AvahiIfIndex interface,
254 AvahiProtocol protocol,
255 AvahiBrowserEvent event,
256 AvahiLookupResultFlags flags,
260 AvahiSRBLookup *l = userdata;
261 AvahiSRecordBrowser *b;
266 b = l->record_browser;
274 case AVAHI_BROWSER_NEW:
277 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
278 r->key->type == AVAHI_DNS_TYPE_CNAME)
279 /* It's a CNAME record, so let's follow it. We allow browsing on both multicast and wide area. */
280 lookup_handle_cname(l, interface, protocol, b->flags, r);
282 /* It's a normal record, so let's call the user callback */
284 if (avahi_server_is_record_local(b->server, interface, protocol, r))
285 flags |= AVAHI_LOOKUP_RESULT_LOCAL;
287 b->callback(b, interface, protocol, event, r, flags, b->userdata);
291 case AVAHI_BROWSER_REMOVE:
294 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
295 r->key->type == AVAHI_DNS_TYPE_CNAME)
296 /* It's a CNAME record, so let's drop that query! */
297 lookup_drop_cname(l, interface, protocol, 0, r);
299 /* It's a normal record, so let's call the user callback */
300 assert(avahi_key_equal(b->key, l->key));
302 b->callback(b, interface, protocol, event, r, flags, b->userdata);
306 case AVAHI_BROWSER_ALL_FOR_NOW:
308 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
311 case AVAHI_BROWSER_CACHE_EXHAUSTED:
312 case AVAHI_BROWSER_FAILURE:
313 /* Not defined for multicast DNS */
321 static int lookup_start(AvahiSRBLookup *l) {
324 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
325 assert(!l->wide_area && !l->multicast);
327 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
329 if (!(l->wide_area = avahi_wide_area_lookup_new(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l)))
333 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
335 if (!(l->multicast = avahi_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l)))
342 static int lookup_scan_cache(AvahiSRBLookup *l) {
347 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
350 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
351 n = (int) avahi_wide_area_scan_cache(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l);
354 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
355 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);
361 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
367 if ((l = lookup_find(b, interface, protocol, flags, key)))
368 return lookup_ref(l);
370 if (!(l = lookup_new(b, interface, protocol, flags, key)))
376 static int lookup_go(AvahiSRBLookup *l) {
380 if (l->record_browser->dead)
385 /* Browse the cache for the root request */
386 n = lookup_scan_cache(l);
388 /* Start the lookup */
389 if (!l->record_browser->dead && l->ref > 1) {
391 if ((l->flags & AVAHI_LOOKUP_USE_MULTICAST) || n == 0)
392 /* We do no start a query if the cache contained entries and we're on wide area */
394 if (lookup_start(l) < 0)
403 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
410 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
411 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
413 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
414 n = lookup_add(l->record_browser, interface, protocol, flags, k);
418 avahi_log_debug(__FILE__": Failed to create SRBLookup.");
422 l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
428 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
430 AvahiSRBLookup *n = NULL;
433 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
434 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
436 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
438 for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
443 if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
444 (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
446 avahi_key_equal(n->key, k))
453 l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
458 static void defer_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void *userdata) {
459 AvahiSRecordBrowser *b = userdata;
465 /* Remove the defer timeout */
466 if (b->defer_time_event) {
467 avahi_time_event_free(b->defer_time_event);
468 b->defer_time_event = NULL;
471 /* Create initial query */
472 assert(!b->root_lookup);
473 b->root_lookup = lookup_add(b, b->interface, b->protocol, b->flags, b->key);
474 assert(b->root_lookup);
476 n = lookup_go(b->root_lookup);
482 /* sending of the initial query failed */
484 avahi_server_set_errno(b->server, AVAHI_ERR_FAILURE);
487 b, b->interface, b->protocol, AVAHI_BROWSER_FAILURE, NULL,
488 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
495 /* Tell the client that we're done with the cache */
497 b, b->interface, b->protocol, AVAHI_BROWSER_CACHE_EXHAUSTED, NULL,
498 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
501 if (!b->dead && b->root_lookup && b->root_lookup->flags & AVAHI_LOOKUP_USE_WIDE_AREA && n > 0) {
503 /* If we do wide area lookups and the the cache contained
504 * entries, we assume that it is complete, and tell the user
505 * so by firing ALL_FOR_NOW. */
507 b->callback(b, b->interface, b->protocol, AVAHI_BROWSER_ALL_FOR_NOW, NULL, AVAHI_LOOKUP_RESULT_WIDE_AREA, b->userdata);
511 void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
517 /* Request a new iteration of the cache scanning */
518 if (!b->defer_time_event) {
519 b->defer_time_event = avahi_time_event_new(b->server->time_event_queue, NULL, defer_callback, b);
520 assert(b->defer_time_event);
524 AvahiSRecordBrowser *avahi_s_record_browser_new(
526 AvahiIfIndex interface,
527 AvahiProtocol protocol,
529 AvahiLookupFlags flags,
530 AvahiSRecordBrowserCallback callback,
533 AvahiSRecordBrowser *b;
539 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
540 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
541 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !avahi_key_is_pattern(key), AVAHI_ERR_IS_PATTERN);
542 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_key_is_valid(key), AVAHI_ERR_INVALID_KEY);
543 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
544 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !(flags & AVAHI_LOOKUP_USE_WIDE_AREA) || !(flags & AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
546 if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
547 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
553 b->interface = interface;
554 b->protocol = protocol;
555 b->key = avahi_key_ref(key);
557 b->callback = callback;
558 b->userdata = userdata;
560 AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
561 b->root_lookup = NULL;
563 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
565 /* The currently cached entries are scanned a bit later, and than we will start querying, too */
566 b->defer_time_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
567 assert(b->defer_time_event);
572 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
577 b->server->need_browser_cleanup = 1;
582 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
587 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
589 avahi_key_unref(b->key);
594 void avahi_browser_cleanup(AvahiServer *server) {
595 AvahiSRecordBrowser *b;
596 AvahiSRecordBrowser *n;
600 while (server->need_browser_cleanup) {
601 server->need_browser_cleanup = 0;
603 for (b = server->record_browsers; b; b = n) {
607 avahi_s_record_browser_destroy(b);
611 if (server->wide_area_lookup_engine)
612 avahi_wide_area_cleanup(server->wide_area_lookup_engine);
613 avahi_multicast_lookup_engine_cleanup(server->multicast_lookup_engine);