]> git.meshlink.io Git - catta/blob - avahi-core/browse.c
* move unicast DNS server registration/browsing routines to their own header dns...
[catta] / avahi-core / browse.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27
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>
34
35 #include "browse.h"
36 #include "log.h"
37 #include "querier.h"
38
39 #define AVAHI_MAX_LOOKUPS_PER_BROWSER 15
40
41 struct AvahiSRBLookup {
42     AvahiSRecordBrowser *record_browser;
43     
44     unsigned ref;
45
46     AvahiIfIndex interface;
47     AvahiProtocol protocol;
48     AvahiLookupFlags flags;
49     
50     AvahiKey *key;
51
52     AvahiWideAreaLookup *wide_area;
53     AvahiMulticastLookup *multicast;
54
55     AvahiRList *cname_lookups;
56     
57     AVAHI_LLIST_FIELDS(AvahiSRBLookup, lookups);
58 };
59
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);
62
63 static void transport_flags_from_domain(AvahiServer *s, AvahiLookupFlags *flags, const char *domain) {
64     assert(flags);
65     assert(domain);
66
67     assert(!((*flags & AVAHI_LOOKUP_USE_MULTICAST) && (*flags & AVAHI_LOOKUP_USE_WIDE_AREA)));
68
69     if (*flags & (AVAHI_LOOKUP_USE_MULTICAST|AVAHI_LOOKUP_USE_WIDE_AREA))
70         return;
71
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;
78     else
79         *flags |= AVAHI_LOOKUP_USE_WIDE_AREA;
80 }
81
82 static AvahiSRBLookup* lookup_new(
83     AvahiSRecordBrowser *b,
84     AvahiIfIndex interface,
85     AvahiProtocol protocol,
86     AvahiLookupFlags flags,
87     AvahiKey *key) {
88
89     AvahiSRBLookup *l;
90     
91     assert(b);
92     assert(AVAHI_IF_VALID(interface));
93     assert(AVAHI_PROTO_VALID(protocol));
94
95     if (b->n_lookups >= AVAHI_MAX_LOOKUPS_PER_BROWSER)
96         /* We don't like cyclic CNAMEs */
97         return NULL;
98     
99     if (!(l = avahi_new(AvahiSRBLookup, 1)))
100         return NULL;
101     
102     l->ref = 1;
103     l->record_browser = b;
104     l->interface = interface;
105     l->protocol = protocol;
106     l->key = avahi_key_ref(key);
107     l->wide_area = NULL;
108     l->multicast = NULL;
109     l->cname_lookups = NULL;
110     l->flags = flags;
111
112     transport_flags_from_domain(b->server, &l->flags, key->name);
113     
114     AVAHI_LLIST_PREPEND(AvahiSRBLookup, lookups, b->lookups, l);
115
116     b->n_lookups ++;
117     
118     return l;
119 }
120
121 static void lookup_unref(AvahiSRBLookup *l) {
122     assert(l);
123     assert(l->ref >= 1);
124
125     if (--l->ref >= 1)
126         return;
127
128     AVAHI_LLIST_REMOVE(AvahiSRBLookup, lookups, l->record_browser->lookups, l);
129     l->record_browser->n_lookups --;
130
131     if (l->wide_area) {
132         avahi_wide_area_lookup_free(l->wide_area);
133         l->wide_area = NULL;
134     }
135     
136     if (l->multicast) {
137         avahi_multicast_lookup_free(l->multicast);
138         l->multicast = NULL;
139     }
140
141     while (l->cname_lookups) {
142         lookup_unref(l->cname_lookups->data);
143         l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, l->cname_lookups);
144     }
145     
146     avahi_key_unref(l->key);
147     avahi_free(l);
148 }
149
150 static AvahiSRBLookup* lookup_ref(AvahiSRBLookup *l) {
151     assert(l);
152     assert(l->ref >= 1);
153
154     l->ref++;
155     return l;
156 }
157
158 static AvahiSRBLookup *lookup_find(
159     AvahiSRecordBrowser *b,
160     AvahiIfIndex interface,
161     AvahiProtocol protocol,
162     AvahiLookupFlags flags,
163     AvahiKey *key) {
164     
165     AvahiSRBLookup *l;
166     
167     assert(b);
168
169     for (l = b->lookups; l; l = l->lookups_next) {
170
171         if ((l->interface == AVAHI_IF_UNSPEC || l->interface == interface) &&
172             (l->interface == AVAHI_PROTO_UNSPEC || l->protocol == protocol) &&
173             l->flags == flags &&
174             avahi_key_equal(l->key, key))
175
176             return l;
177     }
178
179     return NULL;
180 }
181
182 static void browser_cancel(AvahiSRecordBrowser *b) {
183     assert(b);
184
185     if (b->root_lookup) {
186         lookup_unref(b->root_lookup);
187         b->root_lookup = NULL;
188     }
189
190     if (b->defer_time_event) {
191         avahi_time_event_free(b->defer_time_event);
192         b->defer_time_event = NULL;
193     }
194 }
195
196
197 static void lookup_wide_area_callback(
198     AvahiWideAreaLookupEngine *e,
199     AvahiBrowserEvent event,
200     AvahiLookupResultFlags flags,
201     AvahiRecord *r,
202     void *userdata) {
203     
204     AvahiSRBLookup *l = userdata;
205     AvahiSRecordBrowser *b;
206
207     assert(e);
208     assert(l);
209
210     b = l->record_browser;
211
212     if (b->dead)
213         return;
214
215     lookup_ref(l);
216     
217     switch (event) {
218         case AVAHI_BROWSER_NEW:
219             assert(r);
220             
221             if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
222                 r->key->type == AVAHI_DNS_TYPE_CNAME)
223                 /* It's a CNAME record, so let's follow it. We only follow it on wide area DNS! */
224                 lookup_handle_cname(l, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_WIDE_AREA, r);
225             else {
226                 /* It's a normal record, so let's call the user callback */
227                 assert(avahi_key_equal(r->key, l->key));
228
229                 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, r, flags, b->userdata);
230             }
231             break;
232
233         case AVAHI_BROWSER_REMOVE:
234         case AVAHI_BROWSER_CACHE_EXHAUSTED:
235             /* Not defined for wide area DNS */
236             abort();
237
238         case AVAHI_BROWSER_ALL_FOR_NOW:
239         case AVAHI_BROWSER_FAILURE:
240
241             b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
242             break;
243     }
244
245     lookup_unref(l);
246
247 }
248
249 static void lookup_multicast_callback(
250     AvahiMulticastLookupEngine *e,
251     AvahiIfIndex interface,
252     AvahiProtocol protocol,
253     AvahiBrowserEvent event,
254     AvahiLookupResultFlags flags,
255     AvahiRecord *r,
256     void *userdata) {
257
258     AvahiSRBLookup *l = userdata;
259     AvahiSRecordBrowser *b;
260     
261     assert(e);
262     assert(l);
263
264     b = l->record_browser;
265
266     if (b->dead)
267         return;
268
269     lookup_ref(l);
270     
271     switch (event) {
272         case AVAHI_BROWSER_NEW:
273             assert(r);
274             
275             if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
276                 r->key->type == AVAHI_DNS_TYPE_CNAME)
277                 /* It's a CNAME record, so let's follow it. We allow browsing on both multicast and wide area. */
278                 lookup_handle_cname(l, interface, protocol, b->flags, r);
279             else {
280                 /* It's a normal record, so let's call the user callback */
281                 assert(avahi_key_equal(b->key, l->key));
282
283                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
284             }
285             break;
286
287         case AVAHI_BROWSER_REMOVE:
288             assert(r);
289
290             if (r->key->clazz == AVAHI_DNS_CLASS_IN &&
291                 r->key->type == AVAHI_DNS_TYPE_CNAME)
292                 /* It's a CNAME record, so let's drop that query! */
293                 lookup_drop_cname(l, interface, protocol, 0, r);
294             else {
295                 /* It's a normal record, so let's call the user callback */
296                 assert(avahi_key_equal(b->key, l->key));
297
298                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
299             }
300             break;
301
302         case AVAHI_BROWSER_ALL_FOR_NOW:
303
304             b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
305             break;
306
307         case AVAHI_BROWSER_CACHE_EXHAUSTED:
308         case AVAHI_BROWSER_FAILURE:
309             /* Not defined for multicast DNS */
310             abort();
311
312     }
313
314     lookup_unref(l);
315 }
316
317 static int lookup_start(AvahiSRBLookup *l) {
318     assert(l);
319
320     assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
321     assert(!l->wide_area && !l->multicast);
322     
323     if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
324
325         if (!(l->wide_area = avahi_wide_area_lookup_new(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l)))
326             return -1;
327         
328     } else {
329         assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
330
331         if (!(l->multicast = avahi_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l)))
332             return -1;
333     }
334
335     return 0;
336 }
337
338 static int lookup_scan_cache(AvahiSRBLookup *l) {
339     int n = 0;
340     
341     assert(l);
342
343     assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
344
345     
346     if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
347         n = (int) avahi_wide_area_scan_cache(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l);
348         
349     } else {
350         assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
351         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);
352     }
353
354     return n;
355 }
356
357 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
358     AvahiSRBLookup *l;
359     
360     assert(b);
361     assert(!b->dead);
362
363     if ((l = lookup_find(b, interface, protocol, flags, key)))
364         return lookup_ref(l);
365
366     if (!(l = lookup_new(b, interface, protocol, flags, key)))
367         return NULL;
368
369     return l;
370 }
371
372 static int lookup_go(AvahiSRBLookup *l) {
373     int n = 0;
374     assert(l);
375
376     if (l->record_browser->dead)
377         return 0;
378     
379     lookup_ref(l);
380     
381     /* Browse the cache for the root request */
382     n = lookup_scan_cache(l);
383
384     /* Start the lookup */
385     if (!l->record_browser->dead && l->ref > 1) {
386
387         if ((l->flags & AVAHI_LOOKUP_USE_MULTICAST) || n == 0)
388             /* We do no start a query if the cache contained entries and we're on wide area */
389             
390             if (lookup_start(l) < 0)
391                 n = -1;
392     }
393
394     lookup_unref(l);
395
396     return n;
397 }
398
399 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
400     AvahiKey *k;
401     AvahiSRBLookup *n;
402     
403     assert(l);
404     assert(r);
405
406     assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
407     assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
408
409     k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
410     n = lookup_add(l->record_browser, interface, protocol, flags, k); 
411     avahi_key_unref(k);
412
413     if (!n) {
414         avahi_log_debug(__FILE__": Failed to create SRBLookup.");
415         return;
416     }
417
418     l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
419
420     lookup_go(n);
421 }
422
423 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
424     AvahiKey *k;
425     AvahiSRBLookup *n = NULL;
426     AvahiRList *rl;
427
428     assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
429     assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
430
431     k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
432
433     for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
434         n = rl->data;
435
436         assert(n);
437
438         if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
439             (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
440             n->flags == flags &&
441             avahi_key_equal(n->key, k))
442             break;
443     }
444     
445     avahi_key_unref(k);
446
447     if (rl) {
448         l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
449         lookup_unref(n);
450     }
451 }
452
453 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
454     AvahiSRecordBrowser *b = userdata;
455     int n;
456
457     assert(b);
458     assert(!b->dead);
459
460     /* Remove the defer timeout */
461     if (b->defer_time_event) {
462         avahi_time_event_free(b->defer_time_event);
463         b->defer_time_event = NULL;
464     }
465
466     /* Create initial query */
467     assert(!b->root_lookup);
468     b->root_lookup = lookup_add(b, b->interface, b->protocol, b->flags, b->key);
469     assert(b->root_lookup);
470
471     n = lookup_go(b->root_lookup);
472
473     if (b->dead)
474         return;
475
476     if (n < 0) {
477         /* sending of the initial query failed */
478
479         avahi_server_set_errno(b->server, AVAHI_ERR_FAILURE);
480
481         b->callback(
482             b, b->interface, b->protocol, AVAHI_BROWSER_FAILURE, NULL,
483             b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
484             b->userdata);
485         
486         browser_cancel(b);
487         return;
488     }
489     
490     /* Tell the client that we're done with the cache */
491     b->callback(
492         b, b->interface, b->protocol, AVAHI_BROWSER_CACHE_EXHAUSTED, NULL,
493         b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
494         b->userdata);
495
496     if (!b->dead && b->root_lookup && b->root_lookup->flags & AVAHI_LOOKUP_USE_WIDE_AREA && n > 0) {
497
498         /* If we do wide area lookups and the the cache contained
499          * entries, we assume that it is complete, and tell the user
500          * so by firing ALL_FOR_NOW. */
501         
502         b->callback(b, b->interface, b->protocol, AVAHI_BROWSER_ALL_FOR_NOW, NULL, AVAHI_LOOKUP_RESULT_WIDE_AREA, b->userdata);
503     }
504 }
505
506 void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
507     assert(b);
508     assert(!b->dead);
509
510     browser_cancel(b);
511
512     /* Request a new iteration of the cache scanning */
513     if (!b->defer_time_event) {
514         b->defer_time_event = avahi_time_event_new(b->server->time_event_queue, NULL, defer_callback, b);
515         assert(b->defer_time_event);
516     }
517 }
518
519 AvahiSRecordBrowser *avahi_s_record_browser_new(
520     AvahiServer *server,
521     AvahiIfIndex interface,
522     AvahiProtocol protocol,
523     AvahiKey *key,
524     AvahiLookupFlags flags,
525     AvahiSRecordBrowserCallback callback,
526     void* userdata) {
527     
528     AvahiSRecordBrowser *b;
529
530     assert(server);
531     assert(key);
532     assert(callback);
533
534     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
535     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
536     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !avahi_key_is_pattern(key), AVAHI_ERR_IS_PATTERN);
537     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_key_is_valid(key), AVAHI_ERR_INVALID_KEY);
538     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
539     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !(flags & AVAHI_LOOKUP_USE_WIDE_AREA) || !(flags & AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS); 
540     
541     if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
542         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
543         return NULL;
544     }
545     
546     b->dead = 0;
547     b->server = server;
548     b->interface = interface;
549     b->protocol = protocol;
550     b->key = avahi_key_ref(key);
551     b->flags = flags;
552     b->callback = callback;
553     b->userdata = userdata;
554     b->n_lookups = 0;
555     AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
556     b->root_lookup = NULL;
557     
558     AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
559
560     /* The currently cached entries are scanned a bit later, and than we will start querying, too */
561     b->defer_time_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
562     assert(b->defer_time_event);
563     
564     return b;
565 }
566
567 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
568     assert(b);
569     assert(!b->dead);
570
571     b->dead = 1;
572     b->server->need_browser_cleanup = 1;
573
574     browser_cancel(b);
575 }
576
577 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
578     assert(b);
579
580     browser_cancel(b);
581     
582     AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
583
584     avahi_key_unref(b->key);
585     
586     avahi_free(b);
587 }
588
589 void avahi_browser_cleanup(AvahiServer *server) {
590     AvahiSRecordBrowser *b;
591     AvahiSRecordBrowser *n;
592     
593     assert(server);
594
595     while (server->need_browser_cleanup) {
596         server->need_browser_cleanup = 0;
597         
598         for (b = server->record_browsers; b; b = n) {
599             n = b->browser_next;
600             
601             if (b->dead)
602                 avahi_s_record_browser_destroy(b);
603         }
604     } 
605
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);
609 }
610