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_NOT_FOUND:
226 case AVAHI_BROWSER_FAILURE:
228 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
236 static void lookup_multicast_callback(
237 AvahiMulticastLookupEngine *e,
238 AvahiIfIndex interface,
239 AvahiProtocol protocol,
240 AvahiBrowserEvent event,
241 AvahiLookupResultFlags flags,
245 AvahiSRBLookup *l = userdata;
246 AvahiSRecordBrowser *b;
251 b = l->record_browser;
259 case AVAHI_BROWSER_NEW:
262 if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
263 r->key->type == AVAHI_DNS_TYPE_CNAME)
264 /* It's a CNAME record, so let's follow it. We allow browsing on both multicast and wide area. */
265 lookup_handle_cname(l, interface, protocol, b->flags, r);
267 /* It's a normal record, so let's call the user callback */
268 assert(avahi_key_equal(b->key, l->key));
270 b->callback(b, interface, protocol, event, r, flags, b->userdata);
274 case AVAHI_BROWSER_REMOVE:
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 drop that query! */
280 lookup_drop_cname(l, interface, protocol, 0, r);
282 /* It's a normal record, so let's call the user callback */
283 assert(avahi_key_equal(b->key, l->key));
285 b->callback(b, interface, protocol, event, r, flags, b->userdata);
289 case AVAHI_BROWSER_ALL_FOR_NOW:
291 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
294 case AVAHI_BROWSER_CACHE_EXHAUSTED:
295 case AVAHI_BROWSER_NOT_FOUND:
296 case AVAHI_BROWSER_FAILURE:
297 /* Not defined for multicast DNS */
305 static int lookup_start(AvahiSRBLookup *l) {
308 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
309 assert(!l->wide_area && !l->multicast);
311 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
313 if (!(l->wide_area = avahi_wide_area_lookup_new(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l)))
317 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
319 if (!(l->multicast = avahi_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l)))
326 static int lookup_scan_cache(AvahiSRBLookup *l) {
331 assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
334 if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
335 n = (int) avahi_wide_area_scan_cache(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l);
338 assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
339 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);
345 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
351 if ((l = lookup_find(b, interface, protocol, flags, key)))
352 return lookup_ref(l);
354 if (!(l = lookup_new(b, interface, protocol, flags, key)))
360 static int lookup_go(AvahiSRBLookup *l) {
364 if (l->record_browser->dead)
369 /* Browse the cache for the root request */
370 n = lookup_scan_cache(l);
372 /* Start the lookup */
373 if (!l->record_browser->dead && l->ref > 1) {
375 if ((l->flags & AVAHI_LOOKUP_USE_MULTICAST) || n == 0)
376 /* We do no start a query if the cache contained entries and we're on wide area */
378 if (lookup_start(l) < 0)
387 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
394 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
395 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
397 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
398 n = lookup_add(l->record_browser, interface, protocol, flags, k);
402 avahi_log_debug(__FILE__": Failed to create SRBLookup.");
406 l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
411 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
416 assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
417 assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
419 k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
421 for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
426 if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
427 (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
429 avahi_key_equal(n->key, k))
436 l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
441 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
442 AvahiSRecordBrowser *b = userdata;
448 /* Remove the defer timeout */
449 if (b->defer_time_event) {
450 avahi_time_event_free(b->defer_time_event);
451 b->defer_time_event = NULL;
454 /* Create initial query */
455 assert(!b->root_lookup);
456 b->root_lookup = lookup_add(b, b->interface, b->protocol, b->flags, b->key);
457 assert(b->root_lookup);
459 n = lookup_go(b->root_lookup);
465 /* sending of the initial query failed */
468 b, b->interface, b->protocol, AVAHI_BROWSER_FAILURE, NULL,
469 b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_CALLBACK_WIDE_AREA : AVAHI_LOOKUP_CALLBACK_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_CALLBACK_WIDE_AREA : AVAHI_LOOKUP_CALLBACK_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_CALLBACK_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 #define CHECK_VALIDITY_RETURN_NULL(server, expression, error) { \
506 if (!(expression)) { \
507 avahi_server_set_errno((server), (error)); \
512 AvahiSRecordBrowser *avahi_s_record_browser_new(
514 AvahiIfIndex interface,
515 AvahiProtocol protocol,
517 AvahiLookupFlags flags,
518 AvahiSRecordBrowserCallback callback,
521 AvahiSRecordBrowser *b;
527 CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
528 CHECK_VALIDITY_RETURN_NULL(server, !avahi_key_is_pattern(key), AVAHI_ERR_IS_PATTERN);
529 CHECK_VALIDITY_RETURN_NULL(server, avahi_key_is_valid(key), AVAHI_ERR_INVALID_KEY);
530 CHECK_VALIDITY_RETURN_NULL(server, AVAHI_VALID_FLAGS(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
531 CHECK_VALIDITY_RETURN_NULL(server, !(flags & AVAHI_LOOKUP_USE_WIDE_AREA) || !(flags & AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
533 if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
534 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
540 b->interface = interface;
541 b->protocol = protocol;
542 b->key = avahi_key_ref(key);
544 b->callback = callback;
545 b->userdata = userdata;
548 AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
549 b->root_lookup = NULL;
551 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
553 /* The currently cached entries are scanned a bit later, and than we will start querying, too */
554 b->defer_time_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
555 assert(b->defer_time_event);
560 void avahi_s_record_browser_cancel(AvahiSRecordBrowser *b) {
563 if (b->root_lookup) {
564 lookup_unref(b->root_lookup);
565 b->root_lookup = NULL;
568 if (b->defer_time_event) {
569 avahi_time_event_free(b->defer_time_event);
570 b->defer_time_event = NULL;
577 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
582 b->server->need_browser_cleanup = 1;
584 avahi_s_record_browser_cancel(b);
587 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
590 avahi_s_record_browser_cancel(b);
592 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
594 avahi_key_unref(b->key);
599 void avahi_browser_cleanup(AvahiServer *server) {
600 AvahiSRecordBrowser *b;
601 AvahiSRecordBrowser *n;
605 while (server->need_browser_cleanup) {
606 server->need_browser_cleanup = 0;
608 for (b = server->record_browsers; b; b = n) {
612 avahi_s_record_browser_destroy(b);
616 if (server->wide_area_lookup_engine)
617 avahi_wide_area_cleanup(server->wide_area_lookup_engine);
618 avahi_multicast_lookup_engine_cleanup(server->multicast_lookup_engine);