]> git.meshlink.io Git - catta/blob - src/browse.c
rename everything avahi to catta
[catta] / src / browse.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25
26 #include <catta/timeval.h>
27 #include <catta/malloc.h>
28 #include <catta/error.h>
29 #include <catta/domain.h>
30 #include <catta/rlist.h>
31 #include <catta/address.h>
32 #include <catta/log.h>
33
34 #include "browse.h"
35 #include "querier.h"
36 #include "domain-util.h"
37 #include "rr-util.h"
38
39 #define CATTA_LOOKUPS_PER_BROWSER_MAX 15
40
41 struct CattaSRBLookup {
42     CattaSRecordBrowser *record_browser;
43
44     unsigned ref;
45
46     CattaIfIndex interface;
47     CattaProtocol protocol;
48     CattaLookupFlags flags;
49
50     CattaKey *key;
51
52     CattaWideAreaLookup *wide_area;
53     CattaMulticastLookup *multicast;
54
55     CattaRList *cname_lookups;
56
57     CATTA_LLIST_FIELDS(CattaSRBLookup, lookups);
58 };
59
60 static void lookup_handle_cname(CattaSRBLookup *l, CattaIfIndex interface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r);
61 static void lookup_drop_cname(CattaSRBLookup *l, CattaIfIndex interface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r);
62
63 static void transport_flags_from_domain(CattaServer *s, CattaLookupFlags *flags, const char *domain) {
64     assert(flags);
65     assert(domain);
66
67     assert(!((*flags & CATTA_LOOKUP_USE_MULTICAST) && (*flags & CATTA_LOOKUP_USE_WIDE_AREA)));
68
69     if (*flags & (CATTA_LOOKUP_USE_MULTICAST|CATTA_LOOKUP_USE_WIDE_AREA))
70         return;
71
72     if (!s->wide_area_lookup_engine ||
73         !catta_wide_area_has_servers(s->wide_area_lookup_engine) ||
74         catta_domain_ends_with(domain, CATTA_MDNS_SUFFIX_LOCAL) ||
75         catta_domain_ends_with(domain, CATTA_MDNS_SUFFIX_ADDR_IPV4) ||
76         catta_domain_ends_with(domain, CATTA_MDNS_SUFFIX_ADDR_IPV6))
77         *flags |= CATTA_LOOKUP_USE_MULTICAST;
78     else
79         *flags |= CATTA_LOOKUP_USE_WIDE_AREA;
80 }
81
82 static CattaSRBLookup* lookup_new(
83     CattaSRecordBrowser *b,
84     CattaIfIndex interface,
85     CattaProtocol protocol,
86     CattaLookupFlags flags,
87     CattaKey *key) {
88
89     CattaSRBLookup *l;
90
91     assert(b);
92     assert(CATTA_IF_VALID(interface));
93     assert(CATTA_PROTO_VALID(protocol));
94
95     if (b->n_lookups >= CATTA_LOOKUPS_PER_BROWSER_MAX)
96         /* We don't like cyclic CNAMEs */
97         return NULL;
98
99     if (!(l = catta_new(CattaSRBLookup, 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 = catta_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     CATTA_LLIST_PREPEND(CattaSRBLookup, lookups, b->lookups, l);
115
116     b->n_lookups ++;
117
118     return l;
119 }
120
121 static void lookup_unref(CattaSRBLookup *l) {
122     assert(l);
123     assert(l->ref >= 1);
124
125     if (--l->ref >= 1)
126         return;
127
128     CATTA_LLIST_REMOVE(CattaSRBLookup, lookups, l->record_browser->lookups, l);
129     l->record_browser->n_lookups --;
130
131     if (l->wide_area) {
132         catta_wide_area_lookup_free(l->wide_area);
133         l->wide_area = NULL;
134     }
135
136     if (l->multicast) {
137         catta_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 = catta_rlist_remove_by_link(l->cname_lookups, l->cname_lookups);
144     }
145
146     catta_key_unref(l->key);
147     catta_free(l);
148 }
149
150 static CattaSRBLookup* lookup_ref(CattaSRBLookup *l) {
151     assert(l);
152     assert(l->ref >= 1);
153
154     l->ref++;
155     return l;
156 }
157
158 static CattaSRBLookup *lookup_find(
159     CattaSRecordBrowser *b,
160     CattaIfIndex interface,
161     CattaProtocol protocol,
162     CattaLookupFlags flags,
163     CattaKey *key) {
164
165     CattaSRBLookup *l;
166
167     assert(b);
168
169     for (l = b->lookups; l; l = l->lookups_next) {
170
171         if ((l->interface == CATTA_IF_UNSPEC || l->interface == interface) &&
172             (l->interface == CATTA_PROTO_UNSPEC || l->protocol == protocol) &&
173             l->flags == flags &&
174             catta_key_equal(l->key, key))
175
176             return l;
177     }
178
179     return NULL;
180 }
181
182 static void browser_cancel(CattaSRecordBrowser *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         catta_time_event_free(b->defer_time_event);
192         b->defer_time_event = NULL;
193     }
194 }
195
196 static void lookup_wide_area_callback(
197     CattaWideAreaLookupEngine *e,
198     CattaBrowserEvent event,
199     CattaLookupResultFlags flags,
200     CattaRecord *r,
201     void *userdata) {
202
203     CattaSRBLookup *l = userdata;
204     CattaSRecordBrowser *b;
205
206     assert(e);
207     assert(l);
208     assert(l->ref >= 1);
209
210     b = l->record_browser;
211
212     if (b->dead)
213         return;
214
215     lookup_ref(l);
216
217     switch (event) {
218         case CATTA_BROWSER_NEW:
219             assert(r);
220
221             if (r->key->clazz == CATTA_DNS_CLASS_IN &&
222                 r->key->type == CATTA_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, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, CATTA_LOOKUP_USE_WIDE_AREA, r);
225             else {
226                 /* It's a normal record, so let's call the user callback */
227                 assert(catta_key_equal(r->key, l->key));
228
229                 b->callback(b, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, event, r, flags, b->userdata);
230             }
231             break;
232
233         case CATTA_BROWSER_REMOVE:
234         case CATTA_BROWSER_CACHE_EXHAUSTED:
235             /* Not defined for wide area DNS */
236             abort();
237
238         case CATTA_BROWSER_ALL_FOR_NOW:
239         case CATTA_BROWSER_FAILURE:
240
241             b->callback(b, CATTA_IF_UNSPEC, CATTA_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     CattaMulticastLookupEngine *e,
251     CattaIfIndex interface,
252     CattaProtocol protocol,
253     CattaBrowserEvent event,
254     CattaLookupResultFlags flags,
255     CattaRecord *r,
256     void *userdata) {
257
258     CattaSRBLookup *l = userdata;
259     CattaSRecordBrowser *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 CATTA_BROWSER_NEW:
273             assert(r);
274
275             if (r->key->clazz == CATTA_DNS_CLASS_IN &&
276                 r->key->type == CATTA_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
282                 if (catta_server_is_record_local(b->server, interface, protocol, r))
283                     flags |= CATTA_LOOKUP_RESULT_LOCAL;
284
285                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
286             }
287             break;
288
289         case CATTA_BROWSER_REMOVE:
290             assert(r);
291
292             if (r->key->clazz == CATTA_DNS_CLASS_IN &&
293                 r->key->type == CATTA_DNS_TYPE_CNAME)
294                 /* It's a CNAME record, so let's drop that query! */
295                 lookup_drop_cname(l, interface, protocol, 0, r);
296             else {
297                 /* It's a normal record, so let's call the user callback */
298                 assert(catta_key_equal(b->key, l->key));
299
300                 b->callback(b, interface, protocol, event, r, flags, b->userdata);
301             }
302             break;
303
304         case CATTA_BROWSER_ALL_FOR_NOW:
305
306             b->callback(b, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, event, NULL, flags, b->userdata);
307             break;
308
309         case CATTA_BROWSER_CACHE_EXHAUSTED:
310         case CATTA_BROWSER_FAILURE:
311             /* Not defined for multicast DNS */
312             abort();
313
314     }
315
316     lookup_unref(l);
317 }
318
319 static int lookup_start(CattaSRBLookup *l) {
320     assert(l);
321
322     assert(!(l->flags & CATTA_LOOKUP_USE_WIDE_AREA) != !(l->flags & CATTA_LOOKUP_USE_MULTICAST));
323     assert(!l->wide_area && !l->multicast);
324
325     if (l->flags & CATTA_LOOKUP_USE_WIDE_AREA) {
326
327         if (!(l->wide_area = catta_wide_area_lookup_new(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l)))
328             return -1;
329
330     } else {
331         assert(l->flags & CATTA_LOOKUP_USE_MULTICAST);
332
333         if (!(l->multicast = catta_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l)))
334             return -1;
335     }
336
337     return 0;
338 }
339
340 static int lookup_scan_cache(CattaSRBLookup *l) {
341     int n = 0;
342
343     assert(l);
344
345     assert(!(l->flags & CATTA_LOOKUP_USE_WIDE_AREA) != !(l->flags & CATTA_LOOKUP_USE_MULTICAST));
346
347
348     if (l->flags & CATTA_LOOKUP_USE_WIDE_AREA) {
349         n = (int) catta_wide_area_scan_cache(l->record_browser->server->wide_area_lookup_engine, l->key, lookup_wide_area_callback, l);
350
351     } else {
352         assert(l->flags & CATTA_LOOKUP_USE_MULTICAST);
353         n = (int) catta_multicast_lookup_engine_scan_cache(l->record_browser->server->multicast_lookup_engine, l->interface, l->protocol, l->key, lookup_multicast_callback, l);
354     }
355
356     return n;
357 }
358
359 static CattaSRBLookup* lookup_add(CattaSRecordBrowser *b, CattaIfIndex interface, CattaProtocol protocol, CattaLookupFlags flags, CattaKey *key) {
360     CattaSRBLookup *l;
361
362     assert(b);
363     assert(!b->dead);
364
365     if ((l = lookup_find(b, interface, protocol, flags, key)))
366         return lookup_ref(l);
367
368     if (!(l = lookup_new(b, interface, protocol, flags, key)))
369         return NULL;
370
371     return l;
372 }
373
374 static int lookup_go(CattaSRBLookup *l) {
375     int n = 0;
376     assert(l);
377
378     if (l->record_browser->dead)
379         return 0;
380
381     lookup_ref(l);
382
383     /* Browse the cache for the root request */
384     n = lookup_scan_cache(l);
385
386     /* Start the lookup */
387     if (!l->record_browser->dead && l->ref > 1) {
388
389         if ((l->flags & CATTA_LOOKUP_USE_MULTICAST) || n == 0)
390             /* We do no start a query if the cache contained entries and we're on wide area */
391
392             if (lookup_start(l) < 0)
393                 n = -1;
394     }
395
396     lookup_unref(l);
397
398     return n;
399 }
400
401 static void lookup_handle_cname(CattaSRBLookup *l, CattaIfIndex interface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r) {
402     CattaKey *k;
403     CattaSRBLookup *n;
404
405     assert(l);
406     assert(r);
407
408     assert(r->key->clazz == CATTA_DNS_CLASS_IN);
409     assert(r->key->type == CATTA_DNS_TYPE_CNAME);
410
411     k = catta_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
412     n = lookup_add(l->record_browser, interface, protocol, flags, k);
413     catta_key_unref(k);
414
415     if (!n) {
416         catta_log_debug(__FILE__": Failed to create SRBLookup.");
417         return;
418     }
419
420     l->cname_lookups = catta_rlist_prepend(l->cname_lookups, lookup_ref(n));
421
422     lookup_go(n);
423     lookup_unref(n);
424 }
425
426 static void lookup_drop_cname(CattaSRBLookup *l, CattaIfIndex interface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r) {
427     CattaKey *k;
428     CattaSRBLookup *n = NULL;
429     CattaRList *rl;
430
431     assert(r->key->clazz == CATTA_DNS_CLASS_IN);
432     assert(r->key->type == CATTA_DNS_TYPE_CNAME);
433
434     k = catta_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
435
436     for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
437         n = rl->data;
438
439         assert(n);
440
441         if ((n->interface == CATTA_IF_UNSPEC || n->interface == interface) &&
442             (n->interface == CATTA_PROTO_UNSPEC || n->protocol == protocol) &&
443             n->flags == flags &&
444             catta_key_equal(n->key, k))
445             break;
446     }
447
448     catta_key_unref(k);
449
450     if (rl) {
451         l->cname_lookups = catta_rlist_remove_by_link(l->cname_lookups, rl);
452         lookup_unref(n);
453     }
454 }
455
456 static void defer_callback(CATTA_GCC_UNUSED CattaTimeEvent *e, void *userdata) {
457     CattaSRecordBrowser *b = userdata;
458     int n;
459
460     assert(b);
461     assert(!b->dead);
462
463     /* Remove the defer timeout */
464     if (b->defer_time_event) {
465         catta_time_event_free(b->defer_time_event);
466         b->defer_time_event = NULL;
467     }
468
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);
473
474     n = lookup_go(b->root_lookup);
475
476     if (b->dead)
477         return;
478
479     if (n < 0) {
480         /* sending of the initial query failed */
481
482         catta_server_set_errno(b->server, CATTA_ERR_FAILURE);
483
484         b->callback(
485             b, b->interface, b->protocol, CATTA_BROWSER_FAILURE, NULL,
486             b->flags & CATTA_LOOKUP_USE_WIDE_AREA ? CATTA_LOOKUP_RESULT_WIDE_AREA : CATTA_LOOKUP_RESULT_MULTICAST,
487             b->userdata);
488
489         browser_cancel(b);
490         return;
491     }
492
493     /* Tell the client that we're done with the cache */
494     b->callback(
495         b, b->interface, b->protocol, CATTA_BROWSER_CACHE_EXHAUSTED, NULL,
496         b->flags & CATTA_LOOKUP_USE_WIDE_AREA ? CATTA_LOOKUP_RESULT_WIDE_AREA : CATTA_LOOKUP_RESULT_MULTICAST,
497         b->userdata);
498
499     if (!b->dead && b->root_lookup && b->root_lookup->flags & CATTA_LOOKUP_USE_WIDE_AREA && n > 0) {
500
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. */
504
505         b->callback(b, b->interface, b->protocol, CATTA_BROWSER_ALL_FOR_NOW, NULL, CATTA_LOOKUP_RESULT_WIDE_AREA, b->userdata);
506     }
507 }
508
509 void catta_s_record_browser_restart(CattaSRecordBrowser *b) {
510     assert(b);
511     assert(!b->dead);
512
513     browser_cancel(b);
514
515     /* Request a new iteration of the cache scanning */
516     if (!b->defer_time_event) {
517         b->defer_time_event = catta_time_event_new(b->server->time_event_queue, NULL, defer_callback, b);
518         assert(b->defer_time_event);
519     }
520 }
521
522 CattaSRecordBrowser *catta_s_record_browser_new(
523     CattaServer *server,
524     CattaIfIndex interface,
525     CattaProtocol protocol,
526     CattaKey *key,
527     CattaLookupFlags flags,
528     CattaSRecordBrowserCallback callback,
529     void* userdata) {
530
531     CattaSRecordBrowser *b;
532
533     assert(server);
534     assert(key);
535     assert(callback);
536
537     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_IF_VALID(interface), CATTA_ERR_INVALID_INTERFACE);
538     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
539     CATTA_CHECK_VALIDITY_RETURN_NULL(server, !catta_key_is_pattern(key), CATTA_ERR_IS_PATTERN);
540     CATTA_CHECK_VALIDITY_RETURN_NULL(server, catta_key_is_valid(key), CATTA_ERR_INVALID_KEY);
541     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_FLAGS_VALID(flags, CATTA_LOOKUP_USE_WIDE_AREA|CATTA_LOOKUP_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
542     CATTA_CHECK_VALIDITY_RETURN_NULL(server, !(flags & CATTA_LOOKUP_USE_WIDE_AREA) || !(flags & CATTA_LOOKUP_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
543
544     if (!(b = catta_new(CattaSRecordBrowser, 1))) {
545         catta_server_set_errno(server, CATTA_ERR_NO_MEMORY);
546         return NULL;
547     }
548
549     b->dead = 0;
550     b->server = server;
551     b->interface = interface;
552     b->protocol = protocol;
553     b->key = catta_key_ref(key);
554     b->flags = flags;
555     b->callback = callback;
556     b->userdata = userdata;
557     b->n_lookups = 0;
558     CATTA_LLIST_HEAD_INIT(CattaSRBLookup, b->lookups);
559     b->root_lookup = NULL;
560
561     CATTA_LLIST_PREPEND(CattaSRecordBrowser, browser, server->record_browsers, b);
562
563     /* The currently cached entries are scanned a bit later, and than we will start querying, too */
564     b->defer_time_event = catta_time_event_new(server->time_event_queue, NULL, defer_callback, b);
565     assert(b->defer_time_event);
566
567     return b;
568 }
569
570 void catta_s_record_browser_free(CattaSRecordBrowser *b) {
571     assert(b);
572     assert(!b->dead);
573
574     b->dead = 1;
575     b->server->need_browser_cleanup = 1;
576
577     browser_cancel(b);
578 }
579
580 void catta_s_record_browser_destroy(CattaSRecordBrowser *b) {
581     assert(b);
582
583     browser_cancel(b);
584
585     CATTA_LLIST_REMOVE(CattaSRecordBrowser, browser, b->server->record_browsers, b);
586
587     catta_key_unref(b->key);
588
589     catta_free(b);
590 }
591
592 void catta_browser_cleanup(CattaServer *server) {
593     CattaSRecordBrowser *b;
594     CattaSRecordBrowser *n;
595
596     assert(server);
597
598     while (server->need_browser_cleanup) {
599         server->need_browser_cleanup = 0;
600
601         for (b = server->record_browsers; b; b = n) {
602             n = b->browser_next;
603
604             if (b->dead)
605                 catta_s_record_browser_destroy(b);
606         }
607     }
608
609     if (server->wide_area_lookup_engine)
610         catta_wide_area_cleanup(server->wide_area_lookup_engine);
611     catta_multicast_lookup_engine_cleanup(server->multicast_lookup_engine);
612 }
613