4 This file is part of avahi.
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.
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.
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
30 #include <netinet/in.h>
32 #include <avahi-common/malloc.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/timeval.h>
41 #include "wide-area.h"
42 #include "addr-util.h"
45 #define CACHE_ENTRIES_MAX 500
47 typedef struct AvahiWideAreaCacheEntry AvahiWideAreaCacheEntry;
49 struct AvahiWideAreaCacheEntry {
50 AvahiWideAreaLookupEngine *engine;
53 struct timeval timestamp;
54 struct timeval expiry;
56 AvahiTimeEvent *time_event;
58 AVAHI_LLIST_FIELDS(AvahiWideAreaCacheEntry, by_key);
59 AVAHI_LLIST_FIELDS(AvahiWideAreaCacheEntry, cache);
62 struct AvahiWideAreaLookup {
63 AvahiWideAreaLookupEngine *engine;
66 uint32_t id; /* effectively just an uint16_t, but we need it as an index for a hash table */
67 AvahiTimeEvent *time_event;
69 AvahiKey *key, *cname_key;
72 AvahiDnsPacket *packet;
74 AvahiWideAreaLookupCallback callback;
77 AvahiAddress dns_server_used;
79 AVAHI_LLIST_FIELDS(AvahiWideAreaLookup, lookups);
80 AVAHI_LLIST_FIELDS(AvahiWideAreaLookup, by_key);
83 struct AvahiWideAreaLookupEngine {
87 AvahiWatch *watch_ipv4, *watch_ipv6;
92 AVAHI_LLIST_HEAD(AvahiWideAreaCacheEntry, cache);
93 AvahiHashmap *cache_by_key;
94 unsigned cache_n_entries;
97 AVAHI_LLIST_HEAD(AvahiWideAreaLookup, lookups);
98 AvahiHashmap *lookups_by_id;
99 AvahiHashmap *lookups_by_key;
103 AvahiAddress dns_servers[AVAHI_WIDE_AREA_SERVERS_MAX];
104 unsigned n_dns_servers;
105 unsigned current_dns_server;
108 static AvahiWideAreaLookup* find_lookup(AvahiWideAreaLookupEngine *e, uint16_t id) {
109 AvahiWideAreaLookup *l;
114 if (!(l = avahi_hashmap_lookup(e->lookups_by_id, &i)))
125 static int send_to_dns_server(AvahiWideAreaLookup *l, AvahiDnsPacket *p) {
131 if (l->engine->n_dns_servers <= 0)
134 assert(l->engine->current_dns_server < l->engine->n_dns_servers);
136 a = &l->engine->dns_servers[l->engine->current_dns_server];
137 l->dns_server_used = *a;
139 if (a->proto == AVAHI_PROTO_INET) {
141 if (l->engine->fd_ipv4 < 0)
144 return avahi_send_dns_packet_ipv4(l->engine->fd_ipv4, AVAHI_IF_UNSPEC, p, NULL, &a->data.ipv4, AVAHI_DNS_PORT);
147 assert(a->proto == AVAHI_PROTO_INET6);
149 if (l->engine->fd_ipv6 < 0)
152 return avahi_send_dns_packet_ipv6(l->engine->fd_ipv6, AVAHI_IF_UNSPEC, p, NULL, &a->data.ipv6, AVAHI_DNS_PORT);
156 static void next_dns_server(AvahiWideAreaLookupEngine *e) {
159 e->current_dns_server++;
161 if (e->current_dns_server >= e->n_dns_servers)
162 e->current_dns_server = 0;
165 static void lookup_stop(AvahiWideAreaLookup *l) {
171 avahi_time_event_free(l->time_event);
172 l->time_event = NULL;
176 static void sender_timeout_callback(AvahiTimeEvent *e, void *userdata) {
177 AvahiWideAreaLookup *l = userdata;
182 /* Try another DNS server after three retries */
183 if (l->n_send >= 3 && avahi_address_cmp(&l->engine->dns_servers[l->engine->current_dns_server], &l->dns_server_used) == 0) {
184 next_dns_server(l->engine);
186 if (avahi_address_cmp(&l->engine->dns_servers[l->engine->current_dns_server], &l->dns_server_used) == 0)
187 /* There is no other DNS server, fail */
191 if (l->n_send >= 6) {
192 avahi_log_warn(__FILE__": Query timed out.");
193 avahi_server_set_errno(l->engine->server, AVAHI_ERR_TIMEOUT);
194 l->callback(l->engine, AVAHI_BROWSER_FAILURE, AVAHI_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
200 send_to_dns_server(l, l->packet);
203 avahi_time_event_update(e, avahi_elapse_time(&tv, 1000, 0));
206 AvahiWideAreaLookup *avahi_wide_area_lookup_new(
207 AvahiWideAreaLookupEngine *e,
209 AvahiWideAreaLookupCallback callback,
213 AvahiWideAreaLookup *l, *t;
221 l = avahi_new(AvahiWideAreaLookup, 1);
224 l->key = avahi_key_ref(key);
225 l->cname_key = avahi_key_new_cname(l->key);
226 l->callback = callback;
227 l->userdata = userdata;
229 /* If more than 65K wide area quries are issued simultaneously,
230 * this will break. This should be limited by some higher level */
232 for (;; e->next_id++)
233 if (!find_lookup(e, e->next_id))
234 break; /* This ID is not yet used. */
236 l->id = e->next_id++;
238 /* We keep the packet around in case we need to repeat our query */
239 l->packet = avahi_dns_packet_new(0);
241 avahi_dns_packet_set_field(l->packet, AVAHI_DNS_FIELD_ID, (uint16_t) l->id);
242 avahi_dns_packet_set_field(l->packet, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 1, 0, 0, 0, 0, 0));
244 p = avahi_dns_packet_append_key(l->packet, key, 0);
247 avahi_dns_packet_set_field(l->packet, AVAHI_DNS_FIELD_QDCOUNT, 1);
249 if (send_to_dns_server(l, l->packet) < 0) {
250 avahi_log_error(__FILE__": Failed to send packet.");
251 avahi_dns_packet_free(l->packet);
252 avahi_key_unref(l->key);
254 avahi_key_unref(l->cname_key);
261 l->time_event = avahi_time_event_new(e->server->time_event_queue, avahi_elapse_time(&tv, 500, 0), sender_timeout_callback, l);
263 avahi_hashmap_insert(e->lookups_by_id, &l->id, l);
265 t = avahi_hashmap_lookup(e->lookups_by_key, l->key);
266 AVAHI_LLIST_PREPEND(AvahiWideAreaLookup, by_key, t, l);
267 avahi_hashmap_replace(e->lookups_by_key, avahi_key_ref(l->key), t);
269 AVAHI_LLIST_PREPEND(AvahiWideAreaLookup, lookups, e->lookups, l);
274 static void lookup_destroy(AvahiWideAreaLookup *l) {
275 AvahiWideAreaLookup *t;
280 t = avahi_hashmap_lookup(l->engine->lookups_by_key, l->key);
281 AVAHI_LLIST_REMOVE(AvahiWideAreaLookup, by_key, t, l);
283 avahi_hashmap_replace(l->engine->lookups_by_key, avahi_key_ref(l->key), t);
285 avahi_hashmap_remove(l->engine->lookups_by_key, l->key);
287 AVAHI_LLIST_REMOVE(AvahiWideAreaLookup, lookups, l->engine->lookups, l);
289 avahi_hashmap_remove(l->engine->lookups_by_id, &l->id);
290 avahi_dns_packet_free(l->packet);
293 avahi_key_unref(l->key);
296 avahi_key_unref(l->cname_key);
301 void avahi_wide_area_lookup_free(AvahiWideAreaLookup *l) {
308 l->engine->cleanup_dead = 1;
312 void avahi_wide_area_cleanup(AvahiWideAreaLookupEngine *e) {
313 AvahiWideAreaLookup *l, *n;
316 while (e->cleanup_dead) {
319 for (l = e->lookups; l; l = n) {
328 static void cache_entry_free(AvahiWideAreaCacheEntry *c) {
329 AvahiWideAreaCacheEntry *t;
333 avahi_time_event_free(c->time_event);
335 AVAHI_LLIST_REMOVE(AvahiWideAreaCacheEntry, cache, c->engine->cache, c);
337 t = avahi_hashmap_lookup(c->engine->cache_by_key, c->record->key);
338 AVAHI_LLIST_REMOVE(AvahiWideAreaCacheEntry, by_key, t, c);
340 avahi_hashmap_replace(c->engine->cache_by_key, avahi_key_ref(c->record->key), t);
342 avahi_hashmap_remove(c->engine->cache_by_key, c->record->key);
344 c->engine->cache_n_entries --;
346 avahi_record_unref(c->record);
350 static void expiry_event(AvahiTimeEvent *te, void *userdata) {
351 AvahiWideAreaCacheEntry *e = userdata;
359 static AvahiWideAreaCacheEntry* find_record_in_cache(AvahiWideAreaLookupEngine *e, AvahiRecord *r) {
360 AvahiWideAreaCacheEntry *c;
365 for (c = avahi_hashmap_lookup(e->cache_by_key, r->key); c; c = c->by_key_next)
366 if (avahi_record_equal_no_ttl(r, c->record))
372 static void run_callbacks(AvahiWideAreaLookupEngine *e, AvahiRecord *r) {
373 AvahiWideAreaLookup *l;
378 for (l = avahi_hashmap_lookup(e->lookups_by_key, r->key); l; l = l->by_key_next) {
379 if (l->dead || !l->callback)
382 l->callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
385 if (r->key->clazz == AVAHI_DNS_CLASS_IN && r->key->type == AVAHI_DNS_TYPE_CNAME) {
386 /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
388 for (l = e->lookups; l; l = l->lookups_next) {
391 if (l->dead || !l->callback)
394 if ((key = avahi_key_new_cname(l->key))) {
395 if (avahi_key_equal(r->key, key))
396 l->callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
398 avahi_key_unref(key);
404 static void add_to_cache(AvahiWideAreaLookupEngine *e, AvahiRecord *r) {
405 AvahiWideAreaCacheEntry *c;
411 if ((c = find_record_in_cache(e, r))) {
414 /* Update the existing entry */
415 avahi_record_unref(c->record);
417 AvahiWideAreaCacheEntry *t;
421 /* Enforce cache size */
422 if (e->cache_n_entries >= CACHE_ENTRIES_MAX)
423 /* Eventually we should improve the caching algorithm here */
426 c = avahi_new(AvahiWideAreaCacheEntry, 1);
428 c->time_event = NULL;
430 AVAHI_LLIST_PREPEND(AvahiWideAreaCacheEntry, cache, e->cache, c);
432 /* Add the new entry to the cache entry hash table */
433 t = avahi_hashmap_lookup(e->cache_by_key, r->key);
434 AVAHI_LLIST_PREPEND(AvahiWideAreaCacheEntry, by_key, t, c);
435 avahi_hashmap_replace(e->cache_by_key, avahi_key_ref(r->key), t);
437 e->cache_n_entries ++;
440 c->record = avahi_record_ref(r);
442 gettimeofday(&c->timestamp, NULL);
443 c->expiry = c->timestamp;
444 avahi_timeval_add(&c->expiry, r->ttl * 1000000);
447 avahi_time_event_update(c->time_event, &c->expiry);
449 c->time_event = avahi_time_event_new(e->server->time_event_queue, &c->expiry, expiry_event, c);
457 static int map_dns_error(uint16_t error) {
458 static const int table[16] = {
460 AVAHI_ERR_DNS_FORMERR,
461 AVAHI_ERR_DNS_SERVFAIL,
462 AVAHI_ERR_DNS_NXDOMAIN,
463 AVAHI_ERR_DNS_NOTIMP,
464 AVAHI_ERR_DNS_REFUSED,
465 AVAHI_ERR_DNS_YXDOMAIN,
466 AVAHI_ERR_DNS_YXRRSET,
467 AVAHI_ERR_DNS_NXRRSET,
468 AVAHI_ERR_DNS_NOTAUTH,
469 AVAHI_ERR_DNS_NOTZONE,
470 AVAHI_ERR_INVALID_DNS_ERROR,
471 AVAHI_ERR_INVALID_DNS_ERROR,
472 AVAHI_ERR_INVALID_DNS_ERROR,
473 AVAHI_ERR_INVALID_DNS_ERROR,
474 AVAHI_ERR_INVALID_DNS_ERROR
482 static void handle_packet(AvahiWideAreaLookupEngine *e, AvahiDnsPacket *p) {
483 AvahiWideAreaLookup *l = NULL;
486 AvahiBrowserEvent final_event = AVAHI_BROWSER_ALL_FOR_NOW;
491 /* Some superficial validity tests */
492 if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
493 avahi_log_warn(__FILE__": Ignoring invalid response for wide area datagram.");
497 /* Look for the lookup that issued this query */
498 if (!(l = find_lookup(e, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID))) || l->dead)
501 /* Check whether this a packet indicating a failure */
502 if ((r = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & 15) != 0 ||
503 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0) {
505 avahi_server_set_errno(e->server, r == 0 ? AVAHI_ERR_NOT_FOUND : map_dns_error(r));
506 /* Tell the user about the failure */
507 final_event = AVAHI_BROWSER_FAILURE;
509 /* We go on here, since some of the records contained in the
510 reply might be interesting in some way */
513 /* Skip over the question */
514 for (i = (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); i > 0; i--) {
517 if (!(k = avahi_dns_packet_consume_key(p, NULL))) {
518 avahi_log_warn(__FILE__": Wide area response packet too short or invalid while reading question key. (Maybe a UTF-8 problem?)");
519 avahi_server_set_errno(e->server, AVAHI_ERR_INVALID_PACKET);
520 final_event = AVAHI_BROWSER_FAILURE;
527 /* Process responses */
528 for (i = (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
529 (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) +
530 (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); i > 0; i--) {
534 if (!(rr = avahi_dns_packet_consume_record(p, NULL))) {
535 avahi_log_warn(__FILE__": Wide area response packet too short or invalid while reading response record. (Maybe a UTF-8 problem?)");
536 avahi_server_set_errno(e->server, AVAHI_ERR_INVALID_PACKET);
537 final_event = AVAHI_BROWSER_FAILURE;
542 avahi_record_unref(rr);
549 l->callback(e, final_event, AVAHI_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
555 static void socket_event(AVAHI_GCC_UNUSED AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent events, void *userdata) {
556 AvahiWideAreaLookupEngine *e = userdata;
557 AvahiDnsPacket *p = NULL;
559 if (fd == e->fd_ipv4)
560 p = avahi_recv_dns_packet_ipv4(e->fd_ipv4, NULL, NULL, NULL, NULL, NULL);
562 assert(fd == e->fd_ipv6);
563 p = avahi_recv_dns_packet_ipv6(e->fd_ipv6, NULL, NULL, NULL, NULL, NULL);
568 avahi_dns_packet_free(p);
572 AvahiWideAreaLookupEngine *avahi_wide_area_engine_new(AvahiServer *s) {
573 AvahiWideAreaLookupEngine *e;
577 e = avahi_new(AvahiWideAreaLookupEngine, 1);
582 e->fd_ipv4 = s->config.use_ipv4 ? avahi_open_unicast_socket_ipv4() : -1;
583 e->fd_ipv6 = s->config.use_ipv6 ? avahi_open_unicast_socket_ipv6() : -1;
585 if (e->fd_ipv4 < 0 && e->fd_ipv6 < 0) {
586 avahi_log_error(__FILE__": Failed to create wide area sockets: %s", strerror(errno));
600 e->watch_ipv4 = e->watch_ipv6 = NULL;
603 e->watch_ipv4 = s->poll_api->watch_new(e->server->poll_api, e->fd_ipv4, AVAHI_WATCH_IN, socket_event, e);
605 e->watch_ipv6 = s->poll_api->watch_new(e->server->poll_api, e->fd_ipv6, AVAHI_WATCH_IN, socket_event, e);
607 e->n_dns_servers = e->current_dns_server = 0;
608 e->next_id = (uint16_t) rand();
610 /* Initialize cache */
611 AVAHI_LLIST_HEAD_INIT(AvahiWideAreaCacheEntry, e->cache);
612 e->cache_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
613 e->cache_n_entries = 0;
615 /* Initialize lookup list */
616 e->lookups_by_id = avahi_hashmap_new((AvahiHashFunc) avahi_int_hash, (AvahiEqualFunc) avahi_int_equal, NULL, NULL);
617 e->lookups_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
618 AVAHI_LLIST_HEAD_INIT(AvahiWideAreaLookup, e->lookups);
623 void avahi_wide_area_engine_free(AvahiWideAreaLookupEngine *e) {
626 avahi_wide_area_clear_cache(e);
629 lookup_destroy(e->lookups);
631 avahi_hashmap_free(e->cache_by_key);
632 avahi_hashmap_free(e->lookups_by_id);
633 avahi_hashmap_free(e->lookups_by_key);
636 e->server->poll_api->watch_free(e->watch_ipv4);
639 e->server->poll_api->watch_free(e->watch_ipv6);
650 void avahi_wide_area_clear_cache(AvahiWideAreaLookupEngine *e) {
654 cache_entry_free(e->cache);
656 assert(e->cache_n_entries == 0);
659 void avahi_wide_area_set_servers(AvahiWideAreaLookupEngine *e, const AvahiAddress *a, unsigned n) {
663 for (e->n_dns_servers = 0; n > 0 && e->n_dns_servers < AVAHI_WIDE_AREA_SERVERS_MAX; a++, n--)
664 if ((a->proto == AVAHI_PROTO_INET && e->fd_ipv4 >= 0) || (a->proto == AVAHI_PROTO_INET6 && e->fd_ipv6 >= 0))
665 e->dns_servers[e->n_dns_servers++] = *a;
668 e->n_dns_servers = 0;
671 e->current_dns_server = 0;
673 avahi_wide_area_clear_cache(e);
676 void avahi_wide_area_cache_dump(AvahiWideAreaLookupEngine *e, AvahiDumpCallback callback, void* userdata) {
677 AvahiWideAreaCacheEntry *c;
682 callback(";; WIDE AREA CACHE ;;; ", userdata);
684 for (c = e->cache; c; c = c->cache_next) {
685 char *t = avahi_record_to_string(c->record);
686 callback(t, userdata);
691 unsigned avahi_wide_area_scan_cache(AvahiWideAreaLookupEngine *e, AvahiKey *key, AvahiWideAreaLookupCallback callback, void *userdata) {
692 AvahiWideAreaCacheEntry *c;
700 for (c = avahi_hashmap_lookup(e->cache_by_key, key); c; c = c->by_key_next) {
701 callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA|AVAHI_LOOKUP_RESULT_CACHED, c->record, userdata);
705 if ((cname_key = avahi_key_new_cname(key))) {
707 for (c = avahi_hashmap_lookup(e->cache_by_key, cname_key); c; c = c->by_key_next) {
708 callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA|AVAHI_LOOKUP_RESULT_CACHED, c->record, userdata);
712 avahi_key_unref(cname_key);
718 int avahi_wide_area_has_servers(AvahiWideAreaLookupEngine *e) {
721 return e->n_dns_servers > 0;