Home | History | Annotate | Download | only in avahi-core
      1 /***
      2   This file is part of avahi.
      3 
      4   avahi 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   avahi 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 avahi; 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 <errno.h>
     25 #include <string.h>
     26 #include <unistd.h>
     27 #include <stdlib.h>
     28 #include <netinet/in.h>
     29 
     30 #include "avahi-common/avahi-malloc.h"
     31 #include <avahi-common/error.h>
     32 #include <avahi-common/timeval.h>
     33 
     34 #include "internal.h"
     35 #include "browse.h"
     36 #include "socket.h"
     37 #include "log.h"
     38 #include "hashmap.h"
     39 #include "wide-area.h"
     40 #include "addr-util.h"
     41 #include "rr-util.h"
     42 
     43 #define CACHE_ENTRIES_MAX 500
     44 
     45 typedef struct AvahiWideAreaCacheEntry AvahiWideAreaCacheEntry;
     46 
     47 struct AvahiWideAreaCacheEntry {
     48     AvahiWideAreaLookupEngine *engine;
     49 
     50     AvahiRecord *record;
     51     struct timeval timestamp;
     52     struct timeval expiry;
     53 
     54     AvahiTimeEvent *time_event;
     55 
     56     AVAHI_LLIST_FIELDS(AvahiWideAreaCacheEntry, by_key);
     57     AVAHI_LLIST_FIELDS(AvahiWideAreaCacheEntry, cache);
     58 };
     59 
     60 struct AvahiWideAreaLookup {
     61     AvahiWideAreaLookupEngine *engine;
     62     int dead;
     63 
     64     uint32_t id;  /* effectively just an uint16_t, but we need it as an index for a hash table */
     65     AvahiTimeEvent *time_event;
     66 
     67     AvahiKey *key, *cname_key;
     68 
     69     int n_send;
     70     AvahiDnsPacket *packet;
     71 
     72     AvahiWideAreaLookupCallback callback;
     73     void *userdata;
     74 
     75     AvahiAddress dns_server_used;
     76 
     77     AVAHI_LLIST_FIELDS(AvahiWideAreaLookup, lookups);
     78     AVAHI_LLIST_FIELDS(AvahiWideAreaLookup, by_key);
     79 };
     80 
     81 struct AvahiWideAreaLookupEngine {
     82     AvahiServer *server;
     83 
     84     int fd_ipv4, fd_ipv6;
     85     AvahiWatch *watch_ipv4, *watch_ipv6;
     86 
     87     uint16_t next_id;
     88 
     89     /* Cache */
     90     AVAHI_LLIST_HEAD(AvahiWideAreaCacheEntry, cache);
     91     AvahiHashmap *cache_by_key;
     92     unsigned cache_n_entries;
     93 
     94     /* Lookups */
     95     AVAHI_LLIST_HEAD(AvahiWideAreaLookup, lookups);
     96     AvahiHashmap *lookups_by_id;
     97     AvahiHashmap *lookups_by_key;
     98 
     99     int cleanup_dead;
    100 
    101     AvahiAddress dns_servers[AVAHI_WIDE_AREA_SERVERS_MAX];
    102     unsigned n_dns_servers;
    103     unsigned current_dns_server;
    104 };
    105 
    106 static AvahiWideAreaLookup* find_lookup(AvahiWideAreaLookupEngine *e, uint16_t id) {
    107     AvahiWideAreaLookup *l;
    108     int i = (int) id;
    109 
    110     assert(e);
    111 
    112     if (!(l = avahi_hashmap_lookup(e->lookups_by_id, &i)))
    113         return NULL;
    114 
    115     assert(l->id == id);
    116 
    117     if (l->dead)
    118         return NULL;
    119 
    120     return l;
    121 }
    122 
    123 static int send_to_dns_server(AvahiWideAreaLookup *l, AvahiDnsPacket *p) {
    124     AvahiAddress *a;
    125 
    126     assert(l);
    127     assert(p);
    128 
    129     if (l->engine->n_dns_servers <= 0)
    130         return -1;
    131 
    132     assert(l->engine->current_dns_server < l->engine->n_dns_servers);
    133 
    134     a = &l->engine->dns_servers[l->engine->current_dns_server];
    135     l->dns_server_used = *a;
    136 
    137     if (a->proto == AVAHI_PROTO_INET) {
    138 
    139         if (l->engine->fd_ipv4 < 0)
    140             return -1;
    141 
    142         return avahi_send_dns_packet_ipv4(l->engine->fd_ipv4, AVAHI_IF_UNSPEC, p, NULL, &a->data.ipv4, AVAHI_DNS_PORT);
    143 
    144     } else {
    145         assert(a->proto == AVAHI_PROTO_INET6);
    146 
    147         if (l->engine->fd_ipv6 < 0)
    148             return -1;
    149 
    150         return avahi_send_dns_packet_ipv6(l->engine->fd_ipv6, AVAHI_IF_UNSPEC, p, NULL, &a->data.ipv6, AVAHI_DNS_PORT);
    151     }
    152 }
    153 
    154 static void next_dns_server(AvahiWideAreaLookupEngine *e) {
    155     assert(e);
    156 
    157     e->current_dns_server++;
    158 
    159     if (e->current_dns_server >= e->n_dns_servers)
    160         e->current_dns_server = 0;
    161 }
    162 
    163 static void lookup_stop(AvahiWideAreaLookup *l) {
    164     assert(l);
    165 
    166     l->callback = NULL;
    167 
    168     if (l->time_event) {
    169         avahi_time_event_free(l->time_event);
    170         l->time_event = NULL;
    171     }
    172 }
    173 
    174 static void sender_timeout_callback(AvahiTimeEvent *e, void *userdata) {
    175     AvahiWideAreaLookup *l = userdata;
    176     struct timeval tv;
    177 
    178     assert(l);
    179 
    180     /* Try another DNS server after three retries */
    181     if (l->n_send >= 3 && avahi_address_cmp(&l->engine->dns_servers[l->engine->current_dns_server], &l->dns_server_used) == 0) {
    182         next_dns_server(l->engine);
    183 
    184         if (avahi_address_cmp(&l->engine->dns_servers[l->engine->current_dns_server], &l->dns_server_used) == 0)
    185             /* There is no other DNS server, fail */
    186             l->n_send = 1000;
    187     }
    188 
    189     if (l->n_send >= 6) {
    190         avahi_log_warn(__FILE__": Query timed out.");
    191         avahi_server_set_errno(l->engine->server, AVAHI_ERR_TIMEOUT);
    192         l->callback(l->engine, AVAHI_BROWSER_FAILURE, AVAHI_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
    193         lookup_stop(l);
    194         return;
    195     }
    196 
    197     assert(l->packet);
    198     send_to_dns_server(l, l->packet);
    199     l->n_send++;
    200 
    201     avahi_time_event_update(e, avahi_elapse_time(&tv, 1000, 0));
    202 }
    203 
    204 AvahiWideAreaLookup *avahi_wide_area_lookup_new(
    205     AvahiWideAreaLookupEngine *e,
    206     AvahiKey *key,
    207     AvahiWideAreaLookupCallback callback,
    208     void *userdata) {
    209 
    210     struct timeval tv;
    211     AvahiWideAreaLookup *l, *t;
    212     uint8_t *p;
    213 
    214     assert(e);
    215     assert(key);
    216     assert(callback);
    217     assert(userdata);
    218 
    219     l = avahi_new(AvahiWideAreaLookup, 1);
    220     l->engine = e;
    221     l->dead = 0;
    222     l->key = avahi_key_ref(key);
    223     l->cname_key = avahi_key_new_cname(l->key);
    224     l->callback = callback;
    225     l->userdata = userdata;
    226 
    227     /* If more than 65K wide area quries are issued simultaneously,
    228      * this will break. This should be limited by some higher level */
    229 
    230     for (;; e->next_id++)
    231         if (!find_lookup(e, e->next_id))
    232             break; /* This ID is not yet used. */
    233 
    234     l->id = e->next_id++;
    235 
    236     /* We keep the packet around in case we need to repeat our query */
    237     l->packet = avahi_dns_packet_new(0);
    238 
    239     avahi_dns_packet_set_field(l->packet, AVAHI_DNS_FIELD_ID, (uint16_t) l->id);
    240     avahi_dns_packet_set_field(l->packet, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 1, 0, 0, 0, 0, 0));
    241 
    242     p = avahi_dns_packet_append_key(l->packet, key, 0);
    243     assert(p);
    244 
    245     avahi_dns_packet_set_field(l->packet, AVAHI_DNS_FIELD_QDCOUNT, 1);
    246 
    247     if (send_to_dns_server(l, l->packet) < 0) {
    248         avahi_log_error(__FILE__": Failed to send packet.");
    249         avahi_dns_packet_free(l->packet);
    250         avahi_key_unref(l->key);
    251         if (l->cname_key)
    252             avahi_key_unref(l->cname_key);
    253         avahi_free(l);
    254         return NULL;
    255     }
    256 
    257     l->n_send = 1;
    258 
    259     l->time_event = avahi_time_event_new(e->server->time_event_queue, avahi_elapse_time(&tv, 500, 0), sender_timeout_callback, l);
    260 
    261     avahi_hashmap_insert(e->lookups_by_id, &l->id, l);
    262 
    263     t = avahi_hashmap_lookup(e->lookups_by_key, l->key);
    264     AVAHI_LLIST_PREPEND(AvahiWideAreaLookup, by_key, t, l);
    265     avahi_hashmap_replace(e->lookups_by_key, avahi_key_ref(l->key), t);
    266 
    267     AVAHI_LLIST_PREPEND(AvahiWideAreaLookup, lookups, e->lookups, l);
    268 
    269     return l;
    270 }
    271 
    272 static void lookup_destroy(AvahiWideAreaLookup *l) {
    273     AvahiWideAreaLookup *t;
    274     assert(l);
    275 
    276     lookup_stop(l);
    277 
    278     t = avahi_hashmap_lookup(l->engine->lookups_by_key, l->key);
    279     AVAHI_LLIST_REMOVE(AvahiWideAreaLookup, by_key, t, l);
    280     if (t)
    281         avahi_hashmap_replace(l->engine->lookups_by_key, avahi_key_ref(l->key), t);
    282     else
    283         avahi_hashmap_remove(l->engine->lookups_by_key, l->key);
    284 
    285     AVAHI_LLIST_REMOVE(AvahiWideAreaLookup, lookups, l->engine->lookups, l);
    286 
    287     avahi_hashmap_remove(l->engine->lookups_by_id, &l->id);
    288     avahi_dns_packet_free(l->packet);
    289 
    290     if (l->key)
    291         avahi_key_unref(l->key);
    292 
    293     if (l->cname_key)
    294         avahi_key_unref(l->cname_key);
    295 
    296     avahi_free(l);
    297 }
    298 
    299 void avahi_wide_area_lookup_free(AvahiWideAreaLookup *l) {
    300     assert(l);
    301 
    302     if (l->dead)
    303         return;
    304 
    305     l->dead = 1;
    306     l->engine->cleanup_dead = 1;
    307     lookup_stop(l);
    308 }
    309 
    310 void avahi_wide_area_cleanup(AvahiWideAreaLookupEngine *e) {
    311     AvahiWideAreaLookup *l, *n;
    312     assert(e);
    313 
    314     while (e->cleanup_dead) {
    315         e->cleanup_dead = 0;
    316 
    317         for (l = e->lookups; l; l = n) {
    318             n = l->lookups_next;
    319 
    320             if (l->dead)
    321                 lookup_destroy(l);
    322         }
    323     }
    324 }
    325 
    326 static void cache_entry_free(AvahiWideAreaCacheEntry *c) {
    327     AvahiWideAreaCacheEntry *t;
    328     assert(c);
    329 
    330     if (c->time_event)
    331         avahi_time_event_free(c->time_event);
    332 
    333     AVAHI_LLIST_REMOVE(AvahiWideAreaCacheEntry, cache, c->engine->cache, c);
    334 
    335     t = avahi_hashmap_lookup(c->engine->cache_by_key, c->record->key);
    336     AVAHI_LLIST_REMOVE(AvahiWideAreaCacheEntry, by_key, t, c);
    337     if (t)
    338         avahi_hashmap_replace(c->engine->cache_by_key, avahi_key_ref(c->record->key), t);
    339     else
    340         avahi_hashmap_remove(c->engine->cache_by_key, c->record->key);
    341 
    342     c->engine->cache_n_entries --;
    343 
    344     avahi_record_unref(c->record);
    345     avahi_free(c);
    346 }
    347 
    348 static void expiry_event(AvahiTimeEvent *te, void *userdata) {
    349     AvahiWideAreaCacheEntry *e = userdata;
    350 
    351     assert(te);
    352     assert(e);
    353 
    354     cache_entry_free(e);
    355 }
    356 
    357 static AvahiWideAreaCacheEntry* find_record_in_cache(AvahiWideAreaLookupEngine *e, AvahiRecord *r) {
    358     AvahiWideAreaCacheEntry *c;
    359 
    360     assert(e);
    361     assert(r);
    362 
    363     for (c = avahi_hashmap_lookup(e->cache_by_key, r->key); c; c = c->by_key_next)
    364         if (avahi_record_equal_no_ttl(r, c->record))
    365             return c;
    366 
    367     return NULL;
    368 }
    369 
    370 static void run_callbacks(AvahiWideAreaLookupEngine *e, AvahiRecord *r) {
    371     AvahiWideAreaLookup *l;
    372 
    373     assert(e);
    374     assert(r);
    375 
    376     for (l = avahi_hashmap_lookup(e->lookups_by_key, r->key); l; l = l->by_key_next) {
    377         if (l->dead || !l->callback)
    378             continue;
    379 
    380         l->callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
    381     }
    382 
    383     if (r->key->clazz == AVAHI_DNS_CLASS_IN && r->key->type == AVAHI_DNS_TYPE_CNAME) {
    384         /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
    385 
    386         for (l = e->lookups; l; l = l->lookups_next) {
    387             AvahiKey *key;
    388 
    389             if (l->dead || !l->callback)
    390                 continue;
    391 
    392             if ((key = avahi_key_new_cname(l->key))) {
    393                 if (avahi_key_equal(r->key, key))
    394                     l->callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
    395 
    396                 avahi_key_unref(key);
    397             }
    398         }
    399     }
    400 }
    401 
    402 static void add_to_cache(AvahiWideAreaLookupEngine *e, AvahiRecord *r) {
    403     AvahiWideAreaCacheEntry *c;
    404     int is_new;
    405 
    406     assert(e);
    407     assert(r);
    408 
    409     if ((c = find_record_in_cache(e, r))) {
    410         is_new = 0;
    411 
    412         /* Update the existing entry */
    413         avahi_record_unref(c->record);
    414     } else {
    415         AvahiWideAreaCacheEntry *t;
    416 
    417         is_new = 1;
    418 
    419         /* Enforce cache size */
    420         if (e->cache_n_entries >= CACHE_ENTRIES_MAX)
    421             /* Eventually we should improve the caching algorithm here */
    422             goto finish;
    423 
    424         c = avahi_new(AvahiWideAreaCacheEntry, 1);
    425         c->engine = e;
    426         c->time_event = NULL;
    427 
    428         AVAHI_LLIST_PREPEND(AvahiWideAreaCacheEntry, cache, e->cache, c);
    429 
    430         /* Add the new entry to the cache entry hash table */
    431         t = avahi_hashmap_lookup(e->cache_by_key, r->key);
    432         AVAHI_LLIST_PREPEND(AvahiWideAreaCacheEntry, by_key, t, c);
    433         avahi_hashmap_replace(e->cache_by_key, avahi_key_ref(r->key), t);
    434 
    435         e->cache_n_entries ++;
    436     }
    437 
    438     c->record = avahi_record_ref(r);
    439 
    440     gettimeofday(&c->timestamp, NULL);
    441     c->expiry = c->timestamp;
    442     avahi_timeval_add(&c->expiry, r->ttl * 1000000);
    443 
    444     if (c->time_event)
    445         avahi_time_event_update(c->time_event, &c->expiry);
    446     else
    447         c->time_event = avahi_time_event_new(e->server->time_event_queue, &c->expiry, expiry_event, c);
    448 
    449 finish:
    450 
    451     if (is_new)
    452         run_callbacks(e, r);
    453 }
    454 
    455 static int map_dns_error(uint16_t error) {
    456     static const int table[16] = {
    457         AVAHI_OK,
    458         AVAHI_ERR_DNS_FORMERR,
    459         AVAHI_ERR_DNS_SERVFAIL,
    460         AVAHI_ERR_DNS_NXDOMAIN,
    461         AVAHI_ERR_DNS_NOTIMP,
    462         AVAHI_ERR_DNS_REFUSED,
    463         AVAHI_ERR_DNS_YXDOMAIN,
    464         AVAHI_ERR_DNS_YXRRSET,
    465         AVAHI_ERR_DNS_NXRRSET,
    466         AVAHI_ERR_DNS_NOTAUTH,
    467         AVAHI_ERR_DNS_NOTZONE,
    468         AVAHI_ERR_INVALID_DNS_ERROR,
    469         AVAHI_ERR_INVALID_DNS_ERROR,
    470         AVAHI_ERR_INVALID_DNS_ERROR,
    471         AVAHI_ERR_INVALID_DNS_ERROR,
    472         AVAHI_ERR_INVALID_DNS_ERROR
    473     };
    474 
    475     assert(error <= 15);
    476 
    477     return table[error];
    478 }
    479 
    480 static void handle_packet(AvahiWideAreaLookupEngine *e, AvahiDnsPacket *p) {
    481     AvahiWideAreaLookup *l = NULL;
    482     int i, r;
    483 
    484     AvahiBrowserEvent final_event = AVAHI_BROWSER_ALL_FOR_NOW;
    485 
    486     assert(e);
    487     assert(p);
    488 
    489     /* Some superficial validity tests */
    490     if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
    491         avahi_log_warn(__FILE__": Ignoring invalid response for wide area datagram.");
    492         goto finish;
    493     }
    494 
    495     /* Look for the lookup that issued this query */
    496     if (!(l = find_lookup(e, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID))) || l->dead)
    497         goto finish;
    498 
    499     /* Check whether this a packet indicating a failure */
    500     if ((r = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & 15) != 0 ||
    501         avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0) {
    502 
    503         avahi_server_set_errno(e->server, r == 0 ? AVAHI_ERR_NOT_FOUND : map_dns_error(r));
    504         /* Tell the user about the failure */
    505         final_event = AVAHI_BROWSER_FAILURE;
    506 
    507         /* We go on here, since some of the records contained in the
    508            reply might be interesting in some way */
    509     }
    510 
    511     /* Skip over the question */
    512     for (i = (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); i > 0; i--) {
    513         AvahiKey *k;
    514 
    515         if (!(k = avahi_dns_packet_consume_key(p, NULL))) {
    516             avahi_log_warn(__FILE__": Wide area response packet too short or invalid while reading question key. (Maybe a UTF-8 problem?)");
    517             avahi_server_set_errno(e->server, AVAHI_ERR_INVALID_PACKET);
    518             final_event = AVAHI_BROWSER_FAILURE;
    519             goto finish;
    520         }
    521 
    522         avahi_key_unref(k);
    523     }
    524 
    525     /* Process responses */
    526     for (i = (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
    527              (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) +
    528              (int) avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); i > 0; i--) {
    529 
    530         AvahiRecord *rr;
    531 
    532         if (!(rr = avahi_dns_packet_consume_record(p, NULL))) {
    533             avahi_log_warn(__FILE__": Wide area response packet too short or invalid while reading response record. (Maybe a UTF-8 problem?)");
    534             avahi_server_set_errno(e->server, AVAHI_ERR_INVALID_PACKET);
    535             final_event = AVAHI_BROWSER_FAILURE;
    536             goto finish;
    537         }
    538 
    539         add_to_cache(e, rr);
    540         avahi_record_unref(rr);
    541     }
    542 
    543 finish:
    544 
    545     if (l && !l->dead) {
    546         if (l->callback)
    547             l->callback(e, final_event, AVAHI_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
    548 
    549         lookup_stop(l);
    550     }
    551 }
    552 
    553 static void socket_event(AVAHI_GCC_UNUSED AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent events, void *userdata) {
    554     AvahiWideAreaLookupEngine *e = userdata;
    555     AvahiDnsPacket *p = NULL;
    556 
    557     if (fd == e->fd_ipv4)
    558         p = avahi_recv_dns_packet_ipv4(e->fd_ipv4, NULL, NULL, NULL, NULL, NULL);
    559     else {
    560         assert(fd == e->fd_ipv6);
    561         p = avahi_recv_dns_packet_ipv6(e->fd_ipv6, NULL, NULL, NULL, NULL, NULL);
    562     }
    563 
    564     if (p) {
    565         handle_packet(e, p);
    566         avahi_dns_packet_free(p);
    567     }
    568 }
    569 
    570 AvahiWideAreaLookupEngine *avahi_wide_area_engine_new(AvahiServer *s) {
    571     AvahiWideAreaLookupEngine *e;
    572 
    573     assert(s);
    574 
    575     e = avahi_new(AvahiWideAreaLookupEngine, 1);
    576     e->server = s;
    577     e->cleanup_dead = 0;
    578 
    579     /* Create sockets */
    580     e->fd_ipv4 = s->config.use_ipv4 ? avahi_open_unicast_socket_ipv4() : -1;
    581     e->fd_ipv6 = s->config.use_ipv6 ? avahi_open_unicast_socket_ipv6() : -1;
    582 
    583     if (e->fd_ipv4 < 0 && e->fd_ipv6 < 0) {
    584         avahi_log_error(__FILE__": Failed to create wide area sockets: %s", strerror(errno));
    585 
    586         if (e->fd_ipv6 >= 0)
    587             close(e->fd_ipv6);
    588 
    589         if (e->fd_ipv4 >= 0)
    590             close(e->fd_ipv4);
    591 
    592         avahi_free(e);
    593         return NULL;
    594     }
    595 
    596     /* Create watches */
    597 
    598     e->watch_ipv4 = e->watch_ipv6 = NULL;
    599 
    600     if (e->fd_ipv4 >= 0)
    601         e->watch_ipv4 = s->poll_api->watch_new(e->server->poll_api, e->fd_ipv4, AVAHI_WATCH_IN, socket_event, e);
    602     if (e->fd_ipv6 >= 0)
    603         e->watch_ipv6 = s->poll_api->watch_new(e->server->poll_api, e->fd_ipv6, AVAHI_WATCH_IN, socket_event, e);
    604 
    605     e->n_dns_servers = e->current_dns_server = 0;
    606     e->next_id = (uint16_t) rand();
    607 
    608     /* Initialize cache */
    609     AVAHI_LLIST_HEAD_INIT(AvahiWideAreaCacheEntry, e->cache);
    610     e->cache_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
    611     e->cache_n_entries = 0;
    612 
    613     /* Initialize lookup list */
    614     e->lookups_by_id = avahi_hashmap_new((AvahiHashFunc) avahi_int_hash, (AvahiEqualFunc) avahi_int_equal, NULL, NULL);
    615     e->lookups_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
    616     AVAHI_LLIST_HEAD_INIT(AvahiWideAreaLookup, e->lookups);
    617 
    618     return e;
    619 }
    620 
    621 void avahi_wide_area_engine_free(AvahiWideAreaLookupEngine *e) {
    622     assert(e);
    623 
    624     avahi_wide_area_clear_cache(e);
    625 
    626     while (e->lookups)
    627         lookup_destroy(e->lookups);
    628 
    629     avahi_hashmap_free(e->cache_by_key);
    630     avahi_hashmap_free(e->lookups_by_id);
    631     avahi_hashmap_free(e->lookups_by_key);
    632 
    633     if (e->watch_ipv4)
    634         e->server->poll_api->watch_free(e->watch_ipv4);
    635 
    636     if (e->watch_ipv6)
    637         e->server->poll_api->watch_free(e->watch_ipv6);
    638 
    639     if (e->fd_ipv6 >= 0)
    640         close(e->fd_ipv6);
    641 
    642     if (e->fd_ipv4 >= 0)
    643         close(e->fd_ipv4);
    644 
    645     avahi_free(e);
    646 }
    647 
    648 void avahi_wide_area_clear_cache(AvahiWideAreaLookupEngine *e) {
    649     assert(e);
    650 
    651     while (e->cache)
    652         cache_entry_free(e->cache);
    653 
    654     assert(e->cache_n_entries == 0);
    655 }
    656 
    657 void avahi_wide_area_set_servers(AvahiWideAreaLookupEngine *e, const AvahiAddress *a, unsigned n) {
    658     assert(e);
    659 
    660     if (a) {
    661         for (e->n_dns_servers = 0; n > 0 && e->n_dns_servers < AVAHI_WIDE_AREA_SERVERS_MAX; a++, n--)
    662             if ((a->proto == AVAHI_PROTO_INET && e->fd_ipv4 >= 0) || (a->proto == AVAHI_PROTO_INET6 && e->fd_ipv6 >= 0))
    663                 e->dns_servers[e->n_dns_servers++] = *a;
    664     } else {
    665         assert(n == 0);
    666         e->n_dns_servers = 0;
    667     }
    668 
    669     e->current_dns_server = 0;
    670 
    671     avahi_wide_area_clear_cache(e);
    672 }
    673 
    674 void avahi_wide_area_cache_dump(AvahiWideAreaLookupEngine *e, AvahiDumpCallback callback, void* userdata) {
    675     AvahiWideAreaCacheEntry *c;
    676 
    677     assert(e);
    678     assert(callback);
    679 
    680     callback(";; WIDE AREA CACHE ;;; ", userdata);
    681 
    682     for (c = e->cache; c; c = c->cache_next) {
    683         char *t = avahi_record_to_string(c->record);
    684         callback(t, userdata);
    685         avahi_free(t);
    686     }
    687 }
    688 
    689 unsigned avahi_wide_area_scan_cache(AvahiWideAreaLookupEngine *e, AvahiKey *key, AvahiWideAreaLookupCallback callback, void *userdata) {
    690     AvahiWideAreaCacheEntry *c;
    691     AvahiKey *cname_key;
    692     unsigned n = 0;
    693 
    694     assert(e);
    695     assert(key);
    696     assert(callback);
    697 
    698     for (c = avahi_hashmap_lookup(e->cache_by_key, key); c; c = c->by_key_next) {
    699         callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA|AVAHI_LOOKUP_RESULT_CACHED, c->record, userdata);
    700         n++;
    701     }
    702 
    703     if ((cname_key = avahi_key_new_cname(key))) {
    704 
    705         for (c = avahi_hashmap_lookup(e->cache_by_key, cname_key); c; c = c->by_key_next) {
    706             callback(e, AVAHI_BROWSER_NEW, AVAHI_LOOKUP_RESULT_WIDE_AREA|AVAHI_LOOKUP_RESULT_CACHED, c->record, userdata);
    707             n++;
    708         }
    709 
    710         avahi_key_unref(cname_key);
    711     }
    712 
    713     return n;
    714 }
    715 
    716 int avahi_wide_area_has_servers(AvahiWideAreaLookupEngine *e) {
    717     assert(e);
    718 
    719     return e->n_dns_servers > 0;
    720 }
    721 
    722 
    723 
    724