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
28 #include <netinet/in.h>
30 #include <catta/malloc.h>
31 #include <catta/error.h>
32 #include <catta/timeval.h>
37 #include <catta/log.h>
39 #include "wide-area.h"
40 #include "addr-util.h"
43 #define CACHE_ENTRIES_MAX 500
45 typedef struct CattaWideAreaCacheEntry CattaWideAreaCacheEntry;
47 struct CattaWideAreaCacheEntry {
48 CattaWideAreaLookupEngine *engine;
51 struct timeval timestamp;
52 struct timeval expiry;
54 CattaTimeEvent *time_event;
56 CATTA_LLIST_FIELDS(CattaWideAreaCacheEntry, by_key);
57 CATTA_LLIST_FIELDS(CattaWideAreaCacheEntry, cache);
60 struct CattaWideAreaLookup {
61 CattaWideAreaLookupEngine *engine;
64 uint32_t id; /* effectively just an uint16_t, but we need it as an index for a hash table */
65 CattaTimeEvent *time_event;
67 CattaKey *key, *cname_key;
70 CattaDnsPacket *packet;
72 CattaWideAreaLookupCallback callback;
75 CattaAddress dns_server_used;
77 CATTA_LLIST_FIELDS(CattaWideAreaLookup, lookups);
78 CATTA_LLIST_FIELDS(CattaWideAreaLookup, by_key);
81 struct CattaWideAreaLookupEngine {
85 CattaWatch *watch_ipv4, *watch_ipv6;
90 CATTA_LLIST_HEAD(CattaWideAreaCacheEntry, cache);
91 CattaHashmap *cache_by_key;
92 unsigned cache_n_entries;
95 CATTA_LLIST_HEAD(CattaWideAreaLookup, lookups);
96 CattaHashmap *lookups_by_id;
97 CattaHashmap *lookups_by_key;
101 CattaAddress dns_servers[CATTA_WIDE_AREA_SERVERS_MAX];
102 unsigned n_dns_servers;
103 unsigned current_dns_server;
106 static CattaWideAreaLookup* find_lookup(CattaWideAreaLookupEngine *e, uint16_t id) {
107 CattaWideAreaLookup *l;
112 if (!(l = catta_hashmap_lookup(e->lookups_by_id, &i)))
123 static int send_to_dns_server(CattaWideAreaLookup *l, CattaDnsPacket *p) {
129 if (l->engine->n_dns_servers <= 0)
132 assert(l->engine->current_dns_server < l->engine->n_dns_servers);
134 a = &l->engine->dns_servers[l->engine->current_dns_server];
135 l->dns_server_used = *a;
137 if (a->proto == CATTA_PROTO_INET) {
139 if (l->engine->fd_ipv4 < 0)
142 return catta_send_dns_packet_ipv4(l->engine->fd_ipv4, CATTA_IF_UNSPEC, p, NULL, &a->data.ipv4, CATTA_DNS_PORT);
145 assert(a->proto == CATTA_PROTO_INET6);
147 if (l->engine->fd_ipv6 < 0)
150 return catta_send_dns_packet_ipv6(l->engine->fd_ipv6, CATTA_IF_UNSPEC, p, NULL, &a->data.ipv6, CATTA_DNS_PORT);
154 static void next_dns_server(CattaWideAreaLookupEngine *e) {
157 e->current_dns_server++;
159 if (e->current_dns_server >= e->n_dns_servers)
160 e->current_dns_server = 0;
163 static void lookup_stop(CattaWideAreaLookup *l) {
169 catta_time_event_free(l->time_event);
170 l->time_event = NULL;
174 static void sender_timeout_callback(CattaTimeEvent *e, void *userdata) {
175 CattaWideAreaLookup *l = userdata;
180 /* Try another DNS server after three retries */
181 if (l->n_send >= 3 && catta_address_cmp(&l->engine->dns_servers[l->engine->current_dns_server], &l->dns_server_used) == 0) {
182 next_dns_server(l->engine);
184 if (catta_address_cmp(&l->engine->dns_servers[l->engine->current_dns_server], &l->dns_server_used) == 0)
185 /* There is no other DNS server, fail */
189 if (l->n_send >= 6) {
190 catta_log_warn(__FILE__": Query timed out.");
191 catta_server_set_errno(l->engine->server, CATTA_ERR_TIMEOUT);
192 l->callback(l->engine, CATTA_BROWSER_FAILURE, CATTA_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
198 send_to_dns_server(l, l->packet);
201 catta_time_event_update(e, catta_elapse_time(&tv, 1000, 0));
204 CattaWideAreaLookup *catta_wide_area_lookup_new(
205 CattaWideAreaLookupEngine *e,
207 CattaWideAreaLookupCallback callback,
211 CattaWideAreaLookup *l, *t;
219 l = catta_new(CattaWideAreaLookup, 1);
222 l->key = catta_key_ref(key);
223 l->cname_key = catta_key_new_cname(l->key);
224 l->callback = callback;
225 l->userdata = userdata;
227 /* If more than 65K wide area quries are issued simultaneously,
228 * this will break. This should be limited by some higher level */
230 for (;; e->next_id++)
231 if (!find_lookup(e, e->next_id))
232 break; /* This ID is not yet used. */
234 l->id = e->next_id++;
236 /* We keep the packet around in case we need to repeat our query */
237 l->packet = catta_dns_packet_new(0);
239 catta_dns_packet_set_field(l->packet, CATTA_DNS_FIELD_ID, (uint16_t) l->id);
240 catta_dns_packet_set_field(l->packet, CATTA_DNS_FIELD_FLAGS, CATTA_DNS_FLAGS(0, 0, 0, 0, 1, 0, 0, 0, 0, 0));
242 p = catta_dns_packet_append_key(l->packet, key, 0);
245 catta_dns_packet_set_field(l->packet, CATTA_DNS_FIELD_QDCOUNT, 1);
247 if (send_to_dns_server(l, l->packet) < 0) {
248 catta_log_error(__FILE__": Failed to send packet.");
249 catta_dns_packet_free(l->packet);
250 catta_key_unref(l->key);
252 catta_key_unref(l->cname_key);
259 l->time_event = catta_time_event_new(e->server->time_event_queue, catta_elapse_time(&tv, 500, 0), sender_timeout_callback, l);
261 catta_hashmap_insert(e->lookups_by_id, &l->id, l);
263 t = catta_hashmap_lookup(e->lookups_by_key, l->key);
264 CATTA_LLIST_PREPEND(CattaWideAreaLookup, by_key, t, l);
265 catta_hashmap_replace(e->lookups_by_key, catta_key_ref(l->key), t);
267 CATTA_LLIST_PREPEND(CattaWideAreaLookup, lookups, e->lookups, l);
272 static void lookup_destroy(CattaWideAreaLookup *l) {
273 CattaWideAreaLookup *t;
278 t = catta_hashmap_lookup(l->engine->lookups_by_key, l->key);
279 CATTA_LLIST_REMOVE(CattaWideAreaLookup, by_key, t, l);
281 catta_hashmap_replace(l->engine->lookups_by_key, catta_key_ref(l->key), t);
283 catta_hashmap_remove(l->engine->lookups_by_key, l->key);
285 CATTA_LLIST_REMOVE(CattaWideAreaLookup, lookups, l->engine->lookups, l);
287 catta_hashmap_remove(l->engine->lookups_by_id, &l->id);
288 catta_dns_packet_free(l->packet);
291 catta_key_unref(l->key);
294 catta_key_unref(l->cname_key);
299 void catta_wide_area_lookup_free(CattaWideAreaLookup *l) {
306 l->engine->cleanup_dead = 1;
310 void catta_wide_area_cleanup(CattaWideAreaLookupEngine *e) {
311 CattaWideAreaLookup *l, *n;
314 while (e->cleanup_dead) {
317 for (l = e->lookups; l; l = n) {
326 static void cache_entry_free(CattaWideAreaCacheEntry *c) {
327 CattaWideAreaCacheEntry *t;
331 catta_time_event_free(c->time_event);
333 CATTA_LLIST_REMOVE(CattaWideAreaCacheEntry, cache, c->engine->cache, c);
335 t = catta_hashmap_lookup(c->engine->cache_by_key, c->record->key);
336 CATTA_LLIST_REMOVE(CattaWideAreaCacheEntry, by_key, t, c);
338 catta_hashmap_replace(c->engine->cache_by_key, catta_key_ref(c->record->key), t);
340 catta_hashmap_remove(c->engine->cache_by_key, c->record->key);
342 c->engine->cache_n_entries --;
344 catta_record_unref(c->record);
348 static void expiry_event(CattaTimeEvent *te, void *userdata) {
349 CattaWideAreaCacheEntry *e = userdata;
357 static CattaWideAreaCacheEntry* find_record_in_cache(CattaWideAreaLookupEngine *e, CattaRecord *r) {
358 CattaWideAreaCacheEntry *c;
363 for (c = catta_hashmap_lookup(e->cache_by_key, r->key); c; c = c->by_key_next)
364 if (catta_record_equal_no_ttl(r, c->record))
370 static void run_callbacks(CattaWideAreaLookupEngine *e, CattaRecord *r) {
371 CattaWideAreaLookup *l;
376 for (l = catta_hashmap_lookup(e->lookups_by_key, r->key); l; l = l->by_key_next) {
377 if (l->dead || !l->callback)
380 l->callback(e, CATTA_BROWSER_NEW, CATTA_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
383 if (r->key->clazz == CATTA_DNS_CLASS_IN && r->key->type == CATTA_DNS_TYPE_CNAME) {
384 /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
386 for (l = e->lookups; l; l = l->lookups_next) {
389 if (l->dead || !l->callback)
392 if ((key = catta_key_new_cname(l->key))) {
393 if (catta_key_equal(r->key, key))
394 l->callback(e, CATTA_BROWSER_NEW, CATTA_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
396 catta_key_unref(key);
402 static void add_to_cache(CattaWideAreaLookupEngine *e, CattaRecord *r) {
403 CattaWideAreaCacheEntry *c;
409 if ((c = find_record_in_cache(e, r))) {
412 /* Update the existing entry */
413 catta_record_unref(c->record);
415 CattaWideAreaCacheEntry *t;
419 /* Enforce cache size */
420 if (e->cache_n_entries >= CACHE_ENTRIES_MAX)
421 /* Eventually we should improve the caching algorithm here */
424 c = catta_new(CattaWideAreaCacheEntry, 1);
426 c->time_event = NULL;
428 CATTA_LLIST_PREPEND(CattaWideAreaCacheEntry, cache, e->cache, c);
430 /* Add the new entry to the cache entry hash table */
431 t = catta_hashmap_lookup(e->cache_by_key, r->key);
432 CATTA_LLIST_PREPEND(CattaWideAreaCacheEntry, by_key, t, c);
433 catta_hashmap_replace(e->cache_by_key, catta_key_ref(r->key), t);
435 e->cache_n_entries ++;
438 c->record = catta_record_ref(r);
440 gettimeofday(&c->timestamp, NULL);
441 c->expiry = c->timestamp;
442 catta_timeval_add(&c->expiry, r->ttl * 1000000);
445 catta_time_event_update(c->time_event, &c->expiry);
447 c->time_event = catta_time_event_new(e->server->time_event_queue, &c->expiry, expiry_event, c);
455 static int map_dns_error(uint16_t error) {
456 static const int table[16] = {
458 CATTA_ERR_DNS_FORMERR,
459 CATTA_ERR_DNS_SERVFAIL,
460 CATTA_ERR_DNS_NXDOMAIN,
461 CATTA_ERR_DNS_NOTIMP,
462 CATTA_ERR_DNS_REFUSED,
463 CATTA_ERR_DNS_YXDOMAIN,
464 CATTA_ERR_DNS_YXRRSET,
465 CATTA_ERR_DNS_NXRRSET,
466 CATTA_ERR_DNS_NOTAUTH,
467 CATTA_ERR_DNS_NOTZONE,
468 CATTA_ERR_INVALID_DNS_ERROR,
469 CATTA_ERR_INVALID_DNS_ERROR,
470 CATTA_ERR_INVALID_DNS_ERROR,
471 CATTA_ERR_INVALID_DNS_ERROR,
472 CATTA_ERR_INVALID_DNS_ERROR
480 static void handle_packet(CattaWideAreaLookupEngine *e, CattaDnsPacket *p) {
481 CattaWideAreaLookup *l = NULL;
484 CattaBrowserEvent final_event = CATTA_BROWSER_ALL_FOR_NOW;
489 /* Some superficial validity tests */
490 if (catta_dns_packet_check_valid(p) < 0 || catta_dns_packet_is_query(p)) {
491 catta_log_warn(__FILE__": Ignoring invalid response for wide area datagram.");
495 /* Look for the lookup that issued this query */
496 if (!(l = find_lookup(e, catta_dns_packet_get_field(p, CATTA_DNS_FIELD_ID))) || l->dead)
499 /* Check whether this a packet indicating a failure */
500 if ((r = catta_dns_packet_get_field(p, CATTA_DNS_FIELD_FLAGS) & 15) != 0 ||
501 catta_dns_packet_get_field(p, CATTA_DNS_FIELD_ANCOUNT) == 0) {
503 catta_server_set_errno(e->server, r == 0 ? CATTA_ERR_NOT_FOUND : map_dns_error(r));
504 /* Tell the user about the failure */
505 final_event = CATTA_BROWSER_FAILURE;
507 /* We go on here, since some of the records contained in the
508 reply might be interesting in some way */
511 /* Skip over the question */
512 for (i = (int) catta_dns_packet_get_field(p, CATTA_DNS_FIELD_QDCOUNT); i > 0; i--) {
515 if (!(k = catta_dns_packet_consume_key(p, NULL))) {
516 catta_log_warn(__FILE__": Wide area response packet too short or invalid while reading question key. (Maybe a UTF-8 problem?)");
517 catta_server_set_errno(e->server, CATTA_ERR_INVALID_PACKET);
518 final_event = CATTA_BROWSER_FAILURE;
525 /* Process responses */
526 for (i = (int) catta_dns_packet_get_field(p, CATTA_DNS_FIELD_ANCOUNT) +
527 (int) catta_dns_packet_get_field(p, CATTA_DNS_FIELD_NSCOUNT) +
528 (int) catta_dns_packet_get_field(p, CATTA_DNS_FIELD_ARCOUNT); i > 0; i--) {
532 if (!(rr = catta_dns_packet_consume_record(p, NULL))) {
533 catta_log_warn(__FILE__": Wide area response packet too short or invalid while reading response record. (Maybe a UTF-8 problem?)");
534 catta_server_set_errno(e->server, CATTA_ERR_INVALID_PACKET);
535 final_event = CATTA_BROWSER_FAILURE;
540 catta_record_unref(rr);
547 l->callback(e, final_event, CATTA_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
553 static void socket_event(CATTA_GCC_UNUSED CattaWatch *w, int fd, CATTA_GCC_UNUSED CattaWatchEvent events, void *userdata) {
554 CattaWideAreaLookupEngine *e = userdata;
555 CattaDnsPacket *p = NULL;
557 if (fd == e->fd_ipv4)
558 p = catta_recv_dns_packet_ipv4(e->fd_ipv4, NULL, NULL, NULL, NULL, NULL);
560 assert(fd == e->fd_ipv6);
561 p = catta_recv_dns_packet_ipv6(e->fd_ipv6, NULL, NULL, NULL, NULL, NULL);
566 catta_dns_packet_free(p);
570 CattaWideAreaLookupEngine *catta_wide_area_engine_new(CattaServer *s) {
571 CattaWideAreaLookupEngine *e;
575 e = catta_new(CattaWideAreaLookupEngine, 1);
580 e->fd_ipv4 = s->config.use_ipv4 ? catta_open_unicast_socket_ipv4() : -1;
581 e->fd_ipv6 = s->config.use_ipv6 ? catta_open_unicast_socket_ipv6() : -1;
583 if (e->fd_ipv4 < 0 && e->fd_ipv6 < 0) {
584 catta_log_error(__FILE__": Failed to create wide area sockets: %s", strerror(errno));
598 e->watch_ipv4 = e->watch_ipv6 = NULL;
601 e->watch_ipv4 = s->poll_api->watch_new(e->server->poll_api, e->fd_ipv4, CATTA_WATCH_IN, socket_event, e);
603 e->watch_ipv6 = s->poll_api->watch_new(e->server->poll_api, e->fd_ipv6, CATTA_WATCH_IN, socket_event, e);
605 e->n_dns_servers = e->current_dns_server = 0;
606 e->next_id = (uint16_t) rand();
608 /* Initialize cache */
609 CATTA_LLIST_HEAD_INIT(CattaWideAreaCacheEntry, e->cache);
610 e->cache_by_key = catta_hashmap_new((CattaHashFunc) catta_key_hash, (CattaEqualFunc) catta_key_equal, (CattaFreeFunc) catta_key_unref, NULL);
611 e->cache_n_entries = 0;
613 /* Initialize lookup list */
614 e->lookups_by_id = catta_hashmap_new((CattaHashFunc) catta_int_hash, (CattaEqualFunc) catta_int_equal, NULL, NULL);
615 e->lookups_by_key = catta_hashmap_new((CattaHashFunc) catta_key_hash, (CattaEqualFunc) catta_key_equal, (CattaFreeFunc) catta_key_unref, NULL);
616 CATTA_LLIST_HEAD_INIT(CattaWideAreaLookup, e->lookups);
621 void catta_wide_area_engine_free(CattaWideAreaLookupEngine *e) {
624 catta_wide_area_clear_cache(e);
627 lookup_destroy(e->lookups);
629 catta_hashmap_free(e->cache_by_key);
630 catta_hashmap_free(e->lookups_by_id);
631 catta_hashmap_free(e->lookups_by_key);
634 e->server->poll_api->watch_free(e->watch_ipv4);
637 e->server->poll_api->watch_free(e->watch_ipv6);
648 void catta_wide_area_clear_cache(CattaWideAreaLookupEngine *e) {
652 cache_entry_free(e->cache);
654 assert(e->cache_n_entries == 0);
657 void catta_wide_area_set_servers(CattaWideAreaLookupEngine *e, const CattaAddress *a, unsigned n) {
661 for (e->n_dns_servers = 0; n > 0 && e->n_dns_servers < CATTA_WIDE_AREA_SERVERS_MAX; a++, n--)
662 if ((a->proto == CATTA_PROTO_INET && e->fd_ipv4 >= 0) || (a->proto == CATTA_PROTO_INET6 && e->fd_ipv6 >= 0))
663 e->dns_servers[e->n_dns_servers++] = *a;
666 e->n_dns_servers = 0;
669 e->current_dns_server = 0;
671 catta_wide_area_clear_cache(e);
674 void catta_wide_area_cache_dump(CattaWideAreaLookupEngine *e, CattaDumpCallback callback, void* userdata) {
675 CattaWideAreaCacheEntry *c;
680 callback(";; WIDE AREA CACHE ;;; ", userdata);
682 for (c = e->cache; c; c = c->cache_next) {
683 char *t = catta_record_to_string(c->record);
684 callback(t, userdata);
689 unsigned catta_wide_area_scan_cache(CattaWideAreaLookupEngine *e, CattaKey *key, CattaWideAreaLookupCallback callback, void *userdata) {
690 CattaWideAreaCacheEntry *c;
698 for (c = catta_hashmap_lookup(e->cache_by_key, key); c; c = c->by_key_next) {
699 callback(e, CATTA_BROWSER_NEW, CATTA_LOOKUP_RESULT_WIDE_AREA|CATTA_LOOKUP_RESULT_CACHED, c->record, userdata);
703 if ((cname_key = catta_key_new_cname(key))) {
705 for (c = catta_hashmap_lookup(e->cache_by_key, cname_key); c; c = c->by_key_next) {
706 callback(e, CATTA_BROWSER_NEW, CATTA_LOOKUP_RESULT_WIDE_AREA|CATTA_LOOKUP_RESULT_CACHED, c->record, userdata);
710 catta_key_unref(cname_key);
716 int catta_wide_area_has_servers(CattaWideAreaLookupEngine *e) {
719 return e->n_dns_servers > 0;