]> git.meshlink.io Git - catta/blob - avahi-core/browse.c
remove macosx compiler warning
[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
83 static AvahiSRBLookup* lookup_new(
84     AvahiSRecordBrowser *b,
85     AvahiIfIndex interface,
86     AvahiProtocol protocol,
87     AvahiLookupFlags flags,
88     AvahiKey *key) {
89
90     AvahiSRBLookup *l;
91     
92     assert(b);
93     assert(AVAHI_IF_VALID(interface));
94     assert(AVAHI_PROTO_VALID(protocol));
95
96     if (b->n_lookups >= AVAHI_MAX_LOOKUPS_PER_BROWSER)
97         /* We don't like cyclic CNAMEs */
98         return NULL;
99     
100     if (!(l = avahi_new(AvahiSRBLookup, 1)))
101         return NULL;
102     
103     l->ref = 1;
104     l->record_browser = b;
105     l->interface = interface;
106     l->protocol = protocol;
107     l->key = avahi_key_ref(key);
108     l->wide_area = NULL;
109     l->multicast = NULL;
110     l->cname_lookups = NULL;
111     l->flags = flags;
112
113     transport_flags_from_domain(b->server, &l->flags, key->name);
114     
115     AVAHI_LLIST_PREPEND(AvahiSRBLookup, lookups, b->lookups, l);
116
117     b->n_lookups ++;
118     
119     return l;
120 }
121
122 static void lookup_unref(AvahiSRBLookup *l) {
123     assert(l);
124     assert(l->ref >= 1);
125
126     if (--l->ref >= 1)
127         return;
128
129     AVAHI_LLIST_REMOVE(AvahiSRBLookup, lookups, l->record_browser->lookups, l);
130     l->record_browser->n_lookups --;
131
132     if (l->wide_area) {
133         avahi_wide_area_lookup_free(l->wide_area);
134         l->wide_area = NULL;
135     }
136     
137     if (l->multicast) {
138         avahi_multicast_lookup_free(l->multicast);
139         l->multicast = NULL;
140     }
141
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);
145     }
146     
147     avahi_key_unref(l->key);
148     avahi_free(l);
149 }
150
151 static AvahiSRBLookup* lookup_ref(AvahiSRBLookup *l) {
152     assert(l);
153     assert(l->ref >= 1);
154
155     l->ref++;
156     return l;
157 }
158
159 static AvahiSRBLookup *lookup_find(
160     AvahiSRecordBrowser *b,
161     AvahiIfIndex interface,
162     AvahiProtocol protocol,
163     AvahiLookupFlags flags,
164     AvahiKey *key) {
165     
166     AvahiSRBLookup *l;
167     
168     assert(b);
169
170     for (l = b->lookups; l; l = l->lookups_next) {
171
172         if ((l->interface == AVAHI_IF_UNSPEC || l->interface == interface) &&
173             (l->interface == AVAHI_PROTO_UNSPEC || l->protocol == protocol) &&
174             l->flags == flags &&
175             avahi_key_equal(l->key, key))
176
177             return l;
178     }
179
180     return NULL;
181 }
182
183 static void lookup_wide_area_callback(
184     AvahiWideAreaLookupEngine *e,
185     AvahiBrowserEvent event,
186     AvahiLookupResultFlags flags,
187     AvahiRecord *r,
188     void *userdata) {
189     
190     AvahiSRBLookup *l = userdata;
191     AvahiSRecordBrowser *b;
192
193     assert(e);
194     assert(l);
195
196     b = l->record_browser;
197
198     if (b->dead)
199         return;
200
201     lookup_ref(l);
202     
203     switch (event) {
204         case AVAHI_BROWSER_NEW:
205             assert(r);
206             
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);
211             else {
212                 /* It's a normal record, so let's call the user callback */
213                 assert(avahi_key_equal(r->key, l->key));
214
215                 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, r, flags, b->userdata);
216             }
217             break;
218
219         case AVAHI_BROWSER_REMOVE:
220         case AVAHI_BROWSER_CACHE_EXHAUSTED:
221             /* Not defined for wide area DNS */
222             abort();
223
224         case AVAHI_BROWSER_ALL_FOR_NOW:
225         case AVAHI_BROWSER_FAILURE:
226
227             b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
228             break;
229     }
230
231     lookup_unref(l);
232
233 }
234
235 static void lookup_multicast_callback(
236     AvahiMulticastLookupEngine *e,
237     AvahiIfIndex interface,
238     AvahiProtocol protocol,
239     AvahiBrowserEvent event,
240     AvahiLookupResultFlags flags,
241     AvahiRecord *r,
242     void *userdata) {
243
244     AvahiSRBLookup *l = userdata;
245     AvahiSRecordBrowser *b;
246     
247     assert(e);
248     assert(l);
249
250     b = l->record_browser;
251
252     if (b->dead)
253         return;
254
255     lookup_ref(l);
256     
257     switch (event) {
258         case AVAHI_BROWSER_NEW:
259             assert(r);
260             
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);
265             else {
266                 /* It's a normal record, so let's call the user callback */
267                 assert(avahi_key_equal(b->key, l->key));
268
269                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
270             }
271             break;
272
273         case AVAHI_BROWSER_REMOVE:
274             assert(r);
275
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);
280             else {
281                 /* It's a normal record, so let's call the user callback */
282                 assert(avahi_key_equal(b->key, l->key));
283
284                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
285             }
286             break;
287
288         case AVAHI_BROWSER_ALL_FOR_NOW:
289
290             b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
291             break;
292
293         case AVAHI_BROWSER_CACHE_EXHAUSTED:
294         case AVAHI_BROWSER_FAILURE:
295             /* Not defined for multicast DNS */
296             abort();
297
298     }
299
300     lookup_unref(l);
301 }
302
303 static int lookup_start(AvahiSRBLookup *l) {
304     assert(l);
305
306     assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
307     assert(!l->wide_area && !l->multicast);
308     
309     if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
310
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)))
312             return -1;
313         
314     } else {
315         assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
316
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)))
318             return -1;
319     }
320
321     return 0;
322 }
323
324 static int lookup_scan_cache(AvahiSRBLookup *l) {
325     int n = 0;
326     
327     assert(l);
328
329     assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
330
331     
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);
334         
335     } else {
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);
338     }
339
340     return n;
341 }
342
343 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
344     AvahiSRBLookup *l;
345     
346     assert(b);
347     assert(!b->dead);
348
349     if ((l = lookup_find(b, interface, protocol, flags, key)))
350         return lookup_ref(l);
351
352     if (!(l = lookup_new(b, interface, protocol, flags, key)))
353         return NULL;
354
355     return l;
356 }
357
358 static int lookup_go(AvahiSRBLookup *l) {
359     int n = 0;
360     assert(l);
361
362     if (l->record_browser->dead)
363         return 0;
364     
365     lookup_ref(l);
366     
367     /* Browse the cache for the root request */
368     n = lookup_scan_cache(l);
369
370     /* Start the lookup */
371     if (!l->record_browser->dead && l->ref > 1) {
372
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 */
375             
376             if (lookup_start(l) < 0)
377                 n = -1;
378     }
379
380     lookup_unref(l);
381
382     return n;
383 }
384
385 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
386     AvahiKey *k;
387     AvahiSRBLookup *n;
388     
389     assert(l);
390     assert(r);
391
392     assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
393     assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
394
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); 
397     avahi_key_unref(k);
398
399     if (!n) {
400         avahi_log_debug(__FILE__": Failed to create SRBLookup.");
401         return;
402     }
403
404     l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
405
406     lookup_go(n);
407 }
408
409 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
410     AvahiKey *k;
411     AvahiSRBLookup *n = NULL;
412     AvahiRList *rl;
413
414     assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
415     assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
416
417     k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
418
419     for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
420         n = rl->data;
421
422         assert(n);
423
424         if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
425             (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
426             n->flags == flags &&
427             avahi_key_equal(n->key, k))
428             break;
429     }
430     
431     avahi_key_unref(k);
432
433     if (rl) {
434         l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
435         lookup_unref(n);
436     }
437 }
438
439 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
440     AvahiSRecordBrowser *b = userdata;
441     int n;
442
443     assert(b);
444     assert(!b->dead);
445
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;
450     }
451
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);
456
457     n = lookup_go(b->root_lookup);
458
459     if (b->dead)
460         return;
461
462     if (n < 0) {
463         /* sending of the initial query failed */
464
465         avahi_server_set_errno(b->server, AVAHI_ERR_FAILURE);
466
467         b->callback(
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,
470             b->userdata);
471         
472         avahi_s_record_browser_cancel(b);
473         return;
474     }
475     
476     /* Tell the client that we're done with the cache */
477     b->callback(
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,
480         b->userdata);
481
482     if (!b->dead && b->root_lookup && b->root_lookup->flags & AVAHI_LOOKUP_USE_WIDE_AREA && n > 0) {
483
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. */
487         
488         b->callback(b, b->interface, b->protocol, AVAHI_BROWSER_ALL_FOR_NOW, NULL, AVAHI_LOOKUP_RESULT_WIDE_AREA, b->userdata);
489     }
490 }
491
492 void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
493     assert(b);
494     assert(!b->dead);
495
496     avahi_s_record_browser_cancel(b);
497
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);
502     }
503 }
504
505 AvahiSRecordBrowser *avahi_s_record_browser_new(
506     AvahiServer *server,
507     AvahiIfIndex interface,
508     AvahiProtocol protocol,
509     AvahiKey *key,
510     AvahiLookupFlags flags,
511     AvahiSRecordBrowserCallback callback,
512     void* userdata) {
513     
514     AvahiSRecordBrowser *b;
515
516     assert(server);
517     assert(key);
518     assert(callback);
519
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); 
526     
527     if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
528         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
529         return NULL;
530     }
531     
532     b->dead = 0;
533     b->server = server;
534     b->interface = interface;
535     b->protocol = protocol;
536     b->key = avahi_key_ref(key);
537     b->flags = flags;
538     b->callback = callback;
539     b->userdata = userdata;
540     b->n_lookups = 0;
541     AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
542     b->root_lookup = NULL;
543     
544     AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
545
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);
549     
550     return b;
551 }
552
553 void avahi_s_record_browser_cancel(AvahiSRecordBrowser *b) {
554     assert(b);
555
556     if (b->root_lookup) {
557         lookup_unref(b->root_lookup);
558         b->root_lookup = NULL;
559     }
560
561     if (b->defer_time_event) {
562         avahi_time_event_free(b->defer_time_event);
563         b->defer_time_event = NULL;
564     }
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     avahi_s_record_browser_cancel(b);
575 }
576
577 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
578     assert(b);
579
580     avahi_s_record_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