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"
40 #define AVAHI_LOOKUPS_PER_BROWSER_MAX 15
42 struct AvahiSRBLookup {
43 AvahiSRecordBrowser *record_browser;
47 AvahiIfIndex interface;
48 AvahiProtocol protocol;
49 AvahiLookupFlags flags;
53 AvahiWideAreaLookup *wide_area;
54 AvahiMulticastLookup *multicast;
56 AvahiRList *cname_lookups;
58 AVAHI_LLIST_FIELDS(AvahiSRBLookup, lookups);
61 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r);
62 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r);
64 static void transport_flags_from_domain(AvahiServer *s, AvahiLookupFlags *flags, const char *domain) {
68 assert(!((*flags & AVAHI_LOOKUP_USE_MULTICAST) && (*flags & AVAHI_LOOKUP_USE_WIDE_AREA)));
70 if (*flags & (AVAHI_LOOKUP_USE_MULTICAST|AVAHI_LOOKUP_USE_WIDE_AREA))
73 if (!s->wide_area_lookup_engine ||
74 !avahi_wide_area_has_servers(s->wide_area_lookup_engine) ||
75 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_LOCAL) ||
76 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV4) ||
77 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV6))
78 *flags |= AVAHI_LOOKUP_USE_MULTICAST;
80 *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_LOOKUPS_PER_BROWSER_MAX)
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 browser_cancel(AvahiSRecordBrowser *b) {
186 if (b->root_lookup) {
187 lookup_unref(b->root_lookup);
188 b->root_lookup = NULL;
191 if (b->defer_time_event) {
192 avahi_time_event_free(b->defer_time_event);
193 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;
211 b = l->record_browser;
219 case AVAHI_BROWSER_NEW:
222 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
223 r->key->type == AVAHI_DNS_TYPE_CNAME)
224 /* It's a CNAME record, so let's follow it. We only follow it on wide area DNS! */
225 lookup_handle_cname(l, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_WIDE_AREA, r);
227 /* It's a normal record, so let's call the user callback */
228 assert(avahi_key_equal(r->key, l->key));
230 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, r, flags, b->userdata);
234 case AVAHI_BROWSER_REMOVE:
235 case AVAHI_BROWSER_CACHE_EXHAUSTED:
236 /* Not defined for wide area DNS */
239 case AVAHI_BROWSER_ALL_FOR_NOW:
240 case AVAHI_BROWSER_FAILURE:
242 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
250 static void lookup_multicast_callback(
251 AvahiMulticastLookupEngine *e,
252 AvahiIfIndex interface,
253 AvahiProtocol protocol,
254 AvahiBrowserEvent event,
255 AvahiLookupResultFlags flags,
259 AvahiSRBLookup *l = userdata;
260 AvahiSRecordBrowser *b;
265 b = l->record_browser;
273 case AVAHI_BROWSER_NEW:
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 follow it. We allow browsing on both multicast and wide area. */
279 lookup_handle_cname(l, interface, protocol, b->flags, r);
281 /* It's a normal record, so let's call the user callback */
283 if (avahi_server_is_record_local(b->server, interface, protocol, r))
284 flags |= AVAHI_LOOKUP_RESULT_LOCAL;
286 b->callback(b, interface, protocol, event, r, flags, b->userdata);
290 case AVAHI_BROWSER_REMOVE:
293 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
294 r->key->type == AVAHI_DNS_TYPE_CNAME)
295 /* It's a CNAME record, so let's drop that query! */
296 lookup_drop_cname(l, interface, protocol, 0, r);
298 /* It's a normal record, so let's call the user callback */
299 assert(avahi_key_equal(b->key, l->key));
301 b->callback(b, interface, protocol, event, r, flags, b->userdata);
305 case AVAHI_BROWSER_ALL_FOR_NOW:
307 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
310 case AVAHI_BROWSER_CACHE_EXHAUSTED:
311 case AVAHI_BROWSER_FAILURE:
312 /* Not defined for multicast DNS */
320 static int lookup_start(AvahiSRBLookup *l) {
323 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
324 assert(!l->wide_area && !l->multicast);
326 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
328 if (!(l->wide_area = avahi_wide_area_lookup_new(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l)))
332 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
334 if (!(l->multicast = avahi_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l)))
341 static int lookup_scan_cache(AvahiSRBLookup *l) {
346 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
349 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
350 n = (int) avahi_wide_area_scan_cache(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l);
353 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
354 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);
360 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
366 if ((l = lookup_find(b, interface, protocol, flags, key)))
367 return lookup_ref(l);
369 if (!(l = lookup_new(b, interface, protocol, flags, key)))
375 static int lookup_go(AvahiSRBLookup *l) {
379 if (l->record_browser->dead)
384 /* Browse the cache for the root request */
385 n = lookup_scan_cache(l);
387 /* Start the lookup */
388 if (!l->record_browser->dead && l->ref > 1) {
390 if ((l->flags & AVAHI_LOOKUP_USE_MULTICAST) || n == 0)
391 /* We do no start a query if the cache contained entries and we're on wide area */
393 if (lookup_start(l) < 0)
402 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
409 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
410 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
412 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
413 n = lookup_add(l->record_browser, interface, protocol, flags, k);
417 avahi_log_debug(__FILE__": Failed to create SRBLookup.");
421 l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
426 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
428 AvahiSRBLookup *n = NULL;
431 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
432 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
434 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
436 for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
441 if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
442 (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
444 avahi_key_equal(n->key, k))
451 l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
456 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
457 AvahiSRecordBrowser *b = userdata;
463 /* Remove the defer timeout */
464 if (b->defer_time_event) {
465 avahi_time_event_free(b->defer_time_event);
466 b->defer_time_event = NULL;
469 /* Create initial query */
470 assert(!b->root_lookup);
471 b->root_lookup = lookup_add(b, b->interface, b->protocol, b->flags, b->key);
472 assert(b->root_lookup);
474 n = lookup_go(b->root_lookup);
480 /* sending of the initial query failed */
482 avahi_server_set_errno(b->server, AVAHI_ERR_FAILURE);
485 b, b->interface, b->protocol, AVAHI_BROWSER_FAILURE, NULL,
486 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
493 /* Tell the client that we're done with the cache */
495 b, b->interface, b->protocol, AVAHI_BROWSER_CACHE_EXHAUSTED, NULL,
496 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
499 if (!b->dead && b->root_lookup && b->root_lookup->flags & AVAHI_LOOKUP_USE_WIDE_AREA && n > 0) {
501 /* If we do wide area lookups and the the cache contained
502 * entries, we assume that it is complete, and tell the user
503 * so by firing ALL_FOR_NOW. */
505 b->callback(b, b->interface, b->protocol, AVAHI_BROWSER_ALL_FOR_NOW, NULL, AVAHI_LOOKUP_RESULT_WIDE_AREA, b->userdata);
509 void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
515 /* Request a new iteration of the cache scanning */
516 if (!b->defer_time_event) {
517 b->defer_time_event = avahi_time_event_new(b->server->time_event_queue, NULL, defer_callback, b);
518 assert(b->defer_time_event);
522 AvahiSRecordBrowser *avahi_s_record_browser_new(
524 AvahiIfIndex interface,
525 AvahiProtocol protocol,
527 AvahiLookupFlags flags,
528 AvahiSRecordBrowserCallback callback,
531 AvahiSRecordBrowser *b;
537 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
538 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
539 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !avahi_key_is_pattern(key), AVAHI_ERR_IS_PATTERN);
540 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_key_is_valid(key), AVAHI_ERR_INVALID_KEY);
541 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
542 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !(flags & AVAHI_LOOKUP_USE_WIDE_AREA) || !(flags & AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
544 if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
545 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
551 b->interface = interface;
552 b->protocol = protocol;
553 b->key = avahi_key_ref(key);
555 b->callback = callback;
556 b->userdata = userdata;
558 AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
559 b->root_lookup = NULL;
561 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
563 /* The currently cached entries are scanned a bit later, and than we will start querying, too */
564 b->defer_time_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
565 assert(b->defer_time_event);
570 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
575 b->server->need_browser_cleanup = 1;
580 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
585 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
587 avahi_key_unref(b->key);
592 void avahi_browser_cleanup(AvahiServer *server) {
593 AvahiSRecordBrowser *b;
594 AvahiSRecordBrowser *n;
598 while (server->need_browser_cleanup) {
599 server->need_browser_cleanup = 0;
601 for (b = server->record_browsers; b; b = n) {
605 avahi_s_record_browser_destroy(b);
609 if (server->wide_area_lookup_engine)
610 avahi_wide_area_cleanup(server->wide_area_lookup_engine);
611 avahi_multicast_lookup_engine_cleanup(server->multicast_lookup_engine);