]> git.meshlink.io Git - catta/blob - avahi-core/browse.c
s/AVAHI_LOOKUP_CALLBACK/AVAHI_LOOKUP_RESULT/g
[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_NOT_FOUND:
226         case AVAHI_BROWSER_FAILURE:
227
228             b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
229             break;
230     }
231
232     lookup_unref(l);
233
234 }
235
236 static void lookup_multicast_callback(
237     AvahiMulticastLookupEngine *e,
238     AvahiIfIndex interface,
239     AvahiProtocol protocol,
240     AvahiBrowserEvent event,
241     AvahiLookupResultFlags flags,
242     AvahiRecord *r,
243     void *userdata) {
244
245     AvahiSRBLookup *l = userdata;
246     AvahiSRecordBrowser *b;
247     
248     assert(e);
249     assert(l);
250
251     b = l->record_browser;
252
253     if (b->dead)
254         return;
255
256     lookup_ref(l);
257     
258     switch (event) {
259         case AVAHI_BROWSER_NEW:
260             assert(r);
261             
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);
266             else {
267                 /* It's a normal record, so let's call the user callback */
268                 assert(avahi_key_equal(b->key, l->key));
269
270                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
271             }
272             break;
273
274         case AVAHI_BROWSER_REMOVE:
275             assert(r);
276
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);
281             else {
282                 /* It's a normal record, so let's call the user callback */
283                 assert(avahi_key_equal(b->key, l->key));
284
285                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
286             }
287             break;
288
289         case AVAHI_BROWSER_ALL_FOR_NOW:
290
291             b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, flags, b->userdata);
292             break;
293
294         case AVAHI_BROWSER_CACHE_EXHAUSTED:
295         case AVAHI_BROWSER_NOT_FOUND:
296         case AVAHI_BROWSER_FAILURE:
297             /* Not defined for multicast DNS */
298             abort();
299
300     }
301
302     lookup_unref(l);
303 }
304
305 static int lookup_start(AvahiSRBLookup *l) {
306     assert(l);
307
308     assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
309     assert(!l->wide_area && !l->multicast);
310     
311     if (l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) {
312
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)))
314             return -1;
315         
316     } else {
317         assert(l->flags & AVAHI_LOOKUP_USE_MULTICAST);
318
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)))
320             return -1;
321     }
322
323     return 0;
324 }
325
326 static int lookup_scan_cache(AvahiSRBLookup *l) {
327     int n = 0;
328     
329     assert(l);
330
331     assert(!(l->flags & AVAHI_LOOKUP_USE_WIDE_AREA) != !(l->flags & AVAHI_LOOKUP_USE_MULTICAST));
332
333     
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);
336         
337     } else {
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);
340     }
341
342     return n;
343 }
344
345 static AvahiSRBLookup* lookup_add(AvahiSRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiKey *key) {
346     AvahiSRBLookup *l;
347     
348     assert(b);
349     assert(!b->dead);
350
351     if ((l = lookup_find(b, interface, protocol, flags, key)))
352         return lookup_ref(l);
353
354     if (!(l = lookup_new(b, interface, protocol, flags, key)))
355         return NULL;
356
357     return l;
358 }
359
360 static int lookup_go(AvahiSRBLookup *l) {
361     int n = 0;
362     assert(l);
363
364     if (l->record_browser->dead)
365         return 0;
366     
367     lookup_ref(l);
368     
369     /* Browse the cache for the root request */
370     n = lookup_scan_cache(l);
371
372     /* Start the lookup */
373     if (!l->record_browser->dead && l->ref > 1) {
374
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 */
377             
378             if (lookup_start(l) < 0)
379                 n = -1;
380     }
381
382     lookup_unref(l);
383
384     return n;
385 }
386
387 static void lookup_handle_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
388     AvahiKey *k;
389     AvahiSRBLookup *n;
390     
391     assert(l);
392     assert(r);
393
394     assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
395     assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
396
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); 
399     avahi_key_unref(k);
400
401     if (!n) {
402         avahi_log_debug(__FILE__": Failed to create SRBLookup.");
403         return;
404     }
405
406     l->cname_lookups = avahi_rlist_prepend(l->cname_lookups, lookup_ref(n));
407
408     lookup_go(n);
409 }
410
411 static void lookup_drop_cname(AvahiSRBLookup *l, AvahiIfIndex interface, AvahiProtocol protocol, AvahiLookupFlags flags, AvahiRecord *r) {
412     AvahiKey *k;
413     AvahiSRBLookup *n;
414     AvahiRList *rl;
415
416     assert(r->key->clazz == AVAHI_DNS_CLASS_IN);
417     assert(r->key->type == AVAHI_DNS_TYPE_CNAME);
418
419     k = avahi_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
420
421     for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
422         n = rl->data;
423
424         assert(n);
425
426         if ((n->interface == AVAHI_IF_UNSPEC || n->interface == interface) &&
427             (n->interface == AVAHI_PROTO_UNSPEC || n->protocol == protocol) &&
428             n->flags == flags &&
429             avahi_key_equal(n->key, k))
430             break;
431     }
432     
433     avahi_key_unref(k);
434
435     if (rl) {
436         l->cname_lookups = avahi_rlist_remove_by_link(l->cname_lookups, rl);
437         lookup_unref(n);
438     }
439 }
440
441 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
442     AvahiSRecordBrowser *b = userdata;
443     int n;
444
445     assert(b);
446     assert(!b->dead);
447
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;
452     }
453
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);
458
459     n = lookup_go(b->root_lookup);
460
461     if (b->dead)
462         return;
463
464     if (n < 0) {
465         /* sending of the initial query failed */
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 #define CHECK_VALIDITY_RETURN_NULL(server, expression, error) { \
506         if (!(expression)) { \
507             avahi_server_set_errno((server), (error)); \
508             return NULL; \
509         } \
510 }
511
512 AvahiSRecordBrowser *avahi_s_record_browser_new(
513     AvahiServer *server,
514     AvahiIfIndex interface,
515     AvahiProtocol protocol,
516     AvahiKey *key,
517     AvahiLookupFlags flags,
518     AvahiSRecordBrowserCallback callback,
519     void* userdata) {
520     
521     AvahiSRecordBrowser *b;
522
523     assert(server);
524     assert(key);
525     assert(callback);
526
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); 
532     
533     if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
534         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
535         return NULL;
536     }
537     
538     b->dead = 0;
539     b->server = server;
540     b->interface = interface;
541     b->protocol = protocol;
542     b->key = avahi_key_ref(key);
543     b->flags = flags;
544     b->callback = callback;
545     b->userdata = userdata;
546
547     b->n_lookups = 0;
548     AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
549     b->root_lookup = NULL;
550     
551     AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
552
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);
556     
557     return b;
558 }
559
560 void avahi_s_record_browser_cancel(AvahiSRecordBrowser *b) {
561     assert(b);
562
563     if (b->root_lookup) {
564         lookup_unref(b->root_lookup);
565         b->root_lookup = NULL;
566     }
567
568     if (b->defer_time_event) {
569         avahi_time_event_free(b->defer_time_event);
570         b->defer_time_event = NULL;
571     }
572
573
574     
575 }
576
577 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
578     assert(b);
579     assert(!b->dead);
580
581     b->dead = 1;
582     b->server->need_browser_cleanup = 1;
583
584     avahi_s_record_browser_cancel(b);
585 }
586
587 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
588     assert(b);
589
590     avahi_s_record_browser_cancel(b);
591     
592     AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
593
594     avahi_key_unref(b->key);
595     
596     avahi_free(b);
597 }
598
599 void avahi_browser_cleanup(AvahiServer *server) {
600     AvahiSRecordBrowser *b;
601     AvahiSRecordBrowser *n;
602     
603     assert(server);
604
605     while (server->need_browser_cleanup) {
606         server->need_browser_cleanup = 0;
607         
608         for (b = server->record_browsers; b; b = n) {
609             n = b->browser_next;
610             
611             if (b->dead)
612                 avahi_s_record_browser_destroy(b);
613         }
614     } 
615
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);
619 }
620