2 This file is part of catta.
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.
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.
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
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>
36 #include "domain-util.h"
39 #define CATTA_LOOKUPS_PER_BROWSER_MAX 15
41 struct CattaSRBLookup {
42 CattaSRecordBrowser *record_browser;
47 CattaProtocol protocol;
48 CattaLookupFlags flags;
52 CattaWideAreaLookup *wide_area;
53 CattaMulticastLookup *multicast;
55 CattaRList *cname_lookups;
57 CATTA_LLIST_FIELDS(CattaSRBLookup, lookups);
60 static void lookup_handle_cname(CattaSRBLookup *l, CattaIfIndex iface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r);
61 static void lookup_drop_cname(CattaSRBLookup *l, CattaIfIndex iface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r);
63 static void transport_flags_from_domain(CattaServer *s, CattaLookupFlags *flags, const char *domain) {
67 assert(!((*flags & CATTA_LOOKUP_USE_MULTICAST) && (*flags & CATTA_LOOKUP_USE_WIDE_AREA)));
69 if (*flags & (CATTA_LOOKUP_USE_MULTICAST|CATTA_LOOKUP_USE_WIDE_AREA))
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;
79 *flags |= CATTA_LOOKUP_USE_WIDE_AREA;
82 static CattaSRBLookup* lookup_new(
83 CattaSRecordBrowser *b,
85 CattaProtocol protocol,
86 CattaLookupFlags flags,
92 assert(CATTA_IF_VALID(iface));
93 assert(CATTA_PROTO_VALID(protocol));
95 if (b->n_lookups >= CATTA_LOOKUPS_PER_BROWSER_MAX)
96 /* We don't like cyclic CNAMEs */
99 if (!(l = catta_new(CattaSRBLookup, 1)))
103 l->record_browser = b;
105 l->protocol = protocol;
106 l->key = catta_key_ref(key);
109 l->cname_lookups = NULL;
112 transport_flags_from_domain(b->server, &l->flags, key->name);
114 CATTA_LLIST_PREPEND(CattaSRBLookup, lookups, b->lookups, l);
121 static void lookup_unref(CattaSRBLookup *l) {
128 CATTA_LLIST_REMOVE(CattaSRBLookup, lookups, l->record_browser->lookups, l);
129 l->record_browser->n_lookups --;
132 catta_wide_area_lookup_free(l->wide_area);
137 catta_multicast_lookup_free(l->multicast);
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);
146 catta_key_unref(l->key);
150 static CattaSRBLookup* lookup_ref(CattaSRBLookup *l) {
158 static CattaSRBLookup *lookup_find(
159 CattaSRecordBrowser *b,
161 CattaProtocol protocol,
162 CattaLookupFlags flags,
169 for (l = b->lookups; l; l = l->lookups_next) {
171 if ((l->iface == CATTA_IF_UNSPEC || l->iface == iface) &&
172 (l->iface == CATTA_PROTO_UNSPEC || l->protocol == protocol) &&
174 catta_key_equal(l->key, key))
182 static void browser_cancel(CattaSRecordBrowser *b) {
185 if (b->root_lookup) {
186 lookup_unref(b->root_lookup);
187 b->root_lookup = NULL;
190 if (b->defer_time_event) {
191 catta_time_event_free(b->defer_time_event);
192 b->defer_time_event = NULL;
196 static void lookup_wide_area_callback(
197 CattaWideAreaLookupEngine *e,
198 CattaBrowserEvent event,
199 CattaLookupResultFlags flags,
203 CattaSRBLookup *l = userdata;
204 CattaSRecordBrowser *b;
210 b = l->record_browser;
218 case CATTA_BROWSER_NEW:
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);
226 /* It's a normal record, so let's call the user callback */
227 assert(catta_key_equal(r->key, l->key));
229 b->callback(b, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, event, r, flags, b->userdata);
233 case CATTA_BROWSER_REMOVE:
234 case CATTA_BROWSER_CACHE_EXHAUSTED:
235 /* Not defined for wide area DNS */
238 case CATTA_BROWSER_ALL_FOR_NOW:
239 case CATTA_BROWSER_FAILURE:
241 b->callback(b, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, event, NULL, flags, b->userdata);
249 static void lookup_multicast_callback(
250 CattaMulticastLookupEngine *e,
252 CattaProtocol protocol,
253 CattaBrowserEvent event,
254 CattaLookupResultFlags flags,
258 CattaSRBLookup *l = userdata;
259 CattaSRecordBrowser *b;
264 b = l->record_browser;
272 case CATTA_BROWSER_NEW:
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, iface, protocol, b->flags, r);
280 /* It's a normal record, so let's call the user callback */
282 if (catta_server_is_record_local(b->server, iface, protocol, r))
283 flags |= CATTA_LOOKUP_RESULT_LOCAL;
285 b->callback(b, iface, protocol, event, r, flags, b->userdata);
289 case CATTA_BROWSER_REMOVE:
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, iface, protocol, 0, r);
297 /* It's a normal record, so let's call the user callback */
298 assert(catta_key_equal(b->key, l->key));
300 b->callback(b, iface, protocol, event, r, flags, b->userdata);
304 case CATTA_BROWSER_ALL_FOR_NOW:
306 b->callback(b, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, event, NULL, flags, b->userdata);
309 case CATTA_BROWSER_CACHE_EXHAUSTED:
310 case CATTA_BROWSER_FAILURE:
311 /* Not defined for multicast DNS */
319 static int lookup_start(CattaSRBLookup *l) {
322 assert(!(l->flags & CATTA_LOOKUP_USE_WIDE_AREA) != !(l->flags & CATTA_LOOKUP_USE_MULTICAST));
323 assert(!l->wide_area && !l->multicast);
325 if (l->flags & CATTA_LOOKUP_USE_WIDE_AREA) {
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)))
331 assert(l->flags & CATTA_LOOKUP_USE_MULTICAST);
333 if (!(l->multicast = catta_multicast_lookup_new(l->record_browser->server->multicast_lookup_engine, l->iface, l->protocol, l->key, lookup_multicast_callback, l)))
340 static int lookup_scan_cache(CattaSRBLookup *l) {
345 assert(!(l->flags & CATTA_LOOKUP_USE_WIDE_AREA) != !(l->flags & CATTA_LOOKUP_USE_MULTICAST));
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);
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->iface, l->protocol, l->key, lookup_multicast_callback, l);
359 static CattaSRBLookup* lookup_add(CattaSRecordBrowser *b, CattaIfIndex iface, CattaProtocol protocol, CattaLookupFlags flags, CattaKey *key) {
365 if ((l = lookup_find(b, iface, protocol, flags, key)))
366 return lookup_ref(l);
368 if (!(l = lookup_new(b, iface, protocol, flags, key)))
374 static int lookup_go(CattaSRBLookup *l) {
378 if (l->record_browser->dead)
383 /* Browse the cache for the root request */
384 n = lookup_scan_cache(l);
386 /* Start the lookup */
387 if (!l->record_browser->dead && l->ref > 1) {
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 */
392 if (lookup_start(l) < 0)
401 static void lookup_handle_cname(CattaSRBLookup *l, CattaIfIndex iface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r) {
408 assert(r->key->clazz == CATTA_DNS_CLASS_IN);
409 assert(r->key->type == CATTA_DNS_TYPE_CNAME);
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, iface, protocol, flags, k);
416 catta_log_debug(__FILE__": Failed to create SRBLookup.");
420 l->cname_lookups = catta_rlist_prepend(l->cname_lookups, lookup_ref(n));
426 static void lookup_drop_cname(CattaSRBLookup *l, CattaIfIndex iface, CattaProtocol protocol, CattaLookupFlags flags, CattaRecord *r) {
428 CattaSRBLookup *n = NULL;
431 assert(r->key->clazz == CATTA_DNS_CLASS_IN);
432 assert(r->key->type == CATTA_DNS_TYPE_CNAME);
434 k = catta_key_new(r->data.ptr.name, l->record_browser->key->clazz, l->record_browser->key->type);
436 for (rl = l->cname_lookups; rl; rl = rl->rlist_next) {
441 if ((n->iface == CATTA_IF_UNSPEC || n->iface == iface) &&
442 (n->iface == CATTA_PROTO_UNSPEC || n->protocol == protocol) &&
444 catta_key_equal(n->key, k))
451 l->cname_lookups = catta_rlist_remove_by_link(l->cname_lookups, rl);
456 static void defer_callback(CATTA_GCC_UNUSED CattaTimeEvent *e, void *userdata) {
457 CattaSRecordBrowser *b = userdata;
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;
469 /* Create initial query */
470 assert(!b->root_lookup);
471 b->root_lookup = lookup_add(b, b->iface, b->protocol, b->flags, b->key);
472 assert(b->root_lookup);
474 n = lookup_go(b->root_lookup);
480 /* sending of the initial query failed */
482 catta_server_set_errno(b->server, CATTA_ERR_FAILURE);
485 b, b->iface, b->protocol, CATTA_BROWSER_FAILURE, NULL,
486 b->flags & CATTA_LOOKUP_USE_WIDE_AREA ? CATTA_LOOKUP_RESULT_WIDE_AREA : CATTA_LOOKUP_RESULT_MULTICAST,
493 /* Tell the client that we're done with the cache */
495 b, b->iface, b->protocol, CATTA_BROWSER_CACHE_EXHAUSTED, NULL,
496 b->flags & CATTA_LOOKUP_USE_WIDE_AREA ? CATTA_LOOKUP_RESULT_WIDE_AREA : CATTA_LOOKUP_RESULT_MULTICAST,
499 if (!b->dead && b->root_lookup && b->root_lookup->flags & CATTA_LOOKUP_USE_WIDE_AREA && n > 0) {
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. */
505 b->callback(b, b->iface, b->protocol, CATTA_BROWSER_ALL_FOR_NOW, NULL, CATTA_LOOKUP_RESULT_WIDE_AREA, b->userdata);
509 void catta_s_record_browser_restart(CattaSRecordBrowser *b) {
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);
522 CattaSRecordBrowser *catta_s_record_browser_new(
525 CattaProtocol protocol,
527 CattaLookupFlags flags,
528 CattaSRecordBrowserCallback callback,
531 CattaSRecordBrowser *b;
537 CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_IF_VALID(iface), 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);
544 if (!(b = catta_new(CattaSRecordBrowser, 1))) {
545 catta_server_set_errno(server, CATTA_ERR_NO_MEMORY);
552 b->protocol = protocol;
553 b->key = catta_key_ref(key);
555 b->callback = callback;
556 b->userdata = userdata;
558 CATTA_LLIST_HEAD_INIT(CattaSRBLookup, b->lookups);
559 b->root_lookup = NULL;
561 CATTA_LLIST_PREPEND(CattaSRecordBrowser, browser, server->record_browsers, b);
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);
570 void catta_s_record_browser_free(CattaSRecordBrowser *b) {
575 b->server->need_browser_cleanup = 1;
580 void catta_s_record_browser_destroy(CattaSRecordBrowser *b) {
585 CATTA_LLIST_REMOVE(CattaSRecordBrowser, browser, b->server->record_browsers, b);
587 catta_key_unref(b->key);
592 void catta_browser_cleanup(CattaServer *server) {
593 CattaSRecordBrowser *b;
594 CattaSRecordBrowser *n;
598 while (server->need_browser_cleanup) {
599 server->need_browser_cleanup = 0;
601 for (b = server->record_browsers; b; b = n) {
605 catta_s_record_browser_destroy(b);
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);