]> git.meshlink.io Git - catta/blob - src/wide-area.c
2ae6d2d655dd691ef41ab0d43bdbf52f6e4bcf2e
[catta] / src / wide-area.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 <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <netinet/in.h>
29
30 #include <catta/malloc.h>
31 #include <catta/error.h>
32 #include <catta/timeval.h>
33
34 #include "internal.h"
35 #include "browse.h"
36 #include "socket.h"
37 #include <catta/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 CattaWideAreaCacheEntry CattaWideAreaCacheEntry;
46
47 struct CattaWideAreaCacheEntry {
48     CattaWideAreaLookupEngine *engine;
49
50     CattaRecord *record;
51     struct timeval timestamp;
52     struct timeval expiry;
53
54     CattaTimeEvent *time_event;
55
56     CATTA_LLIST_FIELDS(CattaWideAreaCacheEntry, by_key);
57     CATTA_LLIST_FIELDS(CattaWideAreaCacheEntry, cache);
58 };
59
60 struct CattaWideAreaLookup {
61     CattaWideAreaLookupEngine *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     CattaTimeEvent *time_event;
66
67     CattaKey *key, *cname_key;
68
69     int n_send;
70     CattaDnsPacket *packet;
71
72     CattaWideAreaLookupCallback callback;
73     void *userdata;
74
75     CattaAddress dns_server_used;
76
77     CATTA_LLIST_FIELDS(CattaWideAreaLookup, lookups);
78     CATTA_LLIST_FIELDS(CattaWideAreaLookup, by_key);
79 };
80
81 struct CattaWideAreaLookupEngine {
82     CattaServer *server;
83
84     int fd_ipv4, fd_ipv6;
85     CattaWatch *watch_ipv4, *watch_ipv6;
86
87     uint16_t next_id;
88
89     /* Cache */
90     CATTA_LLIST_HEAD(CattaWideAreaCacheEntry, cache);
91     CattaHashmap *cache_by_key;
92     unsigned cache_n_entries;
93
94     /* Lookups */
95     CATTA_LLIST_HEAD(CattaWideAreaLookup, lookups);
96     CattaHashmap *lookups_by_id;
97     CattaHashmap *lookups_by_key;
98
99     int cleanup_dead;
100
101     CattaAddress dns_servers[CATTA_WIDE_AREA_SERVERS_MAX];
102     unsigned n_dns_servers;
103     unsigned current_dns_server;
104 };
105
106 static CattaWideAreaLookup* find_lookup(CattaWideAreaLookupEngine *e, uint16_t id) {
107     CattaWideAreaLookup *l;
108     int i = (int) id;
109
110     assert(e);
111
112     if (!(l = catta_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(CattaWideAreaLookup *l, CattaDnsPacket *p) {
124     CattaAddress *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 == CATTA_PROTO_INET) {
138
139         if (l->engine->fd_ipv4 < 0)
140             return -1;
141
142         return catta_send_dns_packet_ipv4(l->engine->fd_ipv4, CATTA_IF_UNSPEC, p, NULL, &a->data.ipv4, CATTA_DNS_PORT);
143
144     } else {
145         assert(a->proto == CATTA_PROTO_INET6);
146
147         if (l->engine->fd_ipv6 < 0)
148             return -1;
149
150         return catta_send_dns_packet_ipv6(l->engine->fd_ipv6, CATTA_IF_UNSPEC, p, NULL, &a->data.ipv6, CATTA_DNS_PORT);
151     }
152 }
153
154 static void next_dns_server(CattaWideAreaLookupEngine *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(CattaWideAreaLookup *l) {
164     assert(l);
165
166     l->callback = NULL;
167
168     if (l->time_event) {
169         catta_time_event_free(l->time_event);
170         l->time_event = NULL;
171     }
172 }
173
174 static void sender_timeout_callback(CattaTimeEvent *e, void *userdata) {
175     CattaWideAreaLookup *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 && catta_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 (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 */
186             l->n_send = 1000;
187     }
188
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);
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     catta_time_event_update(e, catta_elapse_time(&tv, 1000, 0));
202 }
203
204 CattaWideAreaLookup *catta_wide_area_lookup_new(
205     CattaWideAreaLookupEngine *e,
206     CattaKey *key,
207     CattaWideAreaLookupCallback callback,
208     void *userdata) {
209
210     struct timeval tv;
211     CattaWideAreaLookup *l, *t;
212     uint8_t *p;
213
214     assert(e);
215     assert(key);
216     assert(callback);
217     assert(userdata);
218
219     l = catta_new(CattaWideAreaLookup, 1);
220     l->engine = e;
221     l->dead = 0;
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;
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 = catta_dns_packet_new(0);
238
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));
241
242     p = catta_dns_packet_append_key(l->packet, key, 0);
243     assert(p);
244
245     catta_dns_packet_set_field(l->packet, CATTA_DNS_FIELD_QDCOUNT, 1);
246
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);
251         if (l->cname_key)
252             catta_key_unref(l->cname_key);
253         catta_free(l);
254         return NULL;
255     }
256
257     l->n_send = 1;
258
259     l->time_event = catta_time_event_new(e->server->time_event_queue, catta_elapse_time(&tv, 500, 0), sender_timeout_callback, l);
260
261     catta_hashmap_insert(e->lookups_by_id, &l->id, l);
262
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);
266
267     CATTA_LLIST_PREPEND(CattaWideAreaLookup, lookups, e->lookups, l);
268
269     return l;
270 }
271
272 static void lookup_destroy(CattaWideAreaLookup *l) {
273     CattaWideAreaLookup *t;
274     assert(l);
275
276     lookup_stop(l);
277
278     t = catta_hashmap_lookup(l->engine->lookups_by_key, l->key);
279     CATTA_LLIST_REMOVE(CattaWideAreaLookup, by_key, t, l);
280     if (t)
281         catta_hashmap_replace(l->engine->lookups_by_key, catta_key_ref(l->key), t);
282     else
283         catta_hashmap_remove(l->engine->lookups_by_key, l->key);
284
285     CATTA_LLIST_REMOVE(CattaWideAreaLookup, lookups, l->engine->lookups, l);
286
287     catta_hashmap_remove(l->engine->lookups_by_id, &l->id);
288     catta_dns_packet_free(l->packet);
289
290     if (l->key)
291         catta_key_unref(l->key);
292
293     if (l->cname_key)
294         catta_key_unref(l->cname_key);
295
296     catta_free(l);
297 }
298
299 void catta_wide_area_lookup_free(CattaWideAreaLookup *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 catta_wide_area_cleanup(CattaWideAreaLookupEngine *e) {
311     CattaWideAreaLookup *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(CattaWideAreaCacheEntry *c) {
327     CattaWideAreaCacheEntry *t;
328     assert(c);
329
330     if (c->time_event)
331         catta_time_event_free(c->time_event);
332
333     CATTA_LLIST_REMOVE(CattaWideAreaCacheEntry, cache, c->engine->cache, c);
334
335     t = catta_hashmap_lookup(c->engine->cache_by_key, c->record->key);
336     CATTA_LLIST_REMOVE(CattaWideAreaCacheEntry, by_key, t, c);
337     if (t)
338         catta_hashmap_replace(c->engine->cache_by_key, catta_key_ref(c->record->key), t);
339     else
340         catta_hashmap_remove(c->engine->cache_by_key, c->record->key);
341
342     c->engine->cache_n_entries --;
343
344     catta_record_unref(c->record);
345     catta_free(c);
346 }
347
348 static void expiry_event(CattaTimeEvent *te, void *userdata) {
349     CattaWideAreaCacheEntry *e = userdata;
350
351     assert(te);
352     assert(e);
353
354     cache_entry_free(e);
355 }
356
357 static CattaWideAreaCacheEntry* find_record_in_cache(CattaWideAreaLookupEngine *e, CattaRecord *r) {
358     CattaWideAreaCacheEntry *c;
359
360     assert(e);
361     assert(r);
362
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))
365             return c;
366
367     return NULL;
368 }
369
370 static void run_callbacks(CattaWideAreaLookupEngine *e, CattaRecord *r) {
371     CattaWideAreaLookup *l;
372
373     assert(e);
374     assert(r);
375
376     for (l = catta_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, CATTA_BROWSER_NEW, CATTA_LOOKUP_RESULT_WIDE_AREA, r, l->userdata);
381     }
382
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 */
385
386         for (l = e->lookups; l; l = l->lookups_next) {
387             CattaKey *key;
388
389             if (l->dead || !l->callback)
390                 continue;
391
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);
395
396                 catta_key_unref(key);
397             }
398         }
399     }
400 }
401
402 static void add_to_cache(CattaWideAreaLookupEngine *e, CattaRecord *r) {
403     CattaWideAreaCacheEntry *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         catta_record_unref(c->record);
414     } else {
415         CattaWideAreaCacheEntry *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 = catta_new(CattaWideAreaCacheEntry, 1);
425         c->engine = e;
426         c->time_event = NULL;
427
428         CATTA_LLIST_PREPEND(CattaWideAreaCacheEntry, cache, e->cache, c);
429
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);
434
435         e->cache_n_entries ++;
436     }
437
438     c->record = catta_record_ref(r);
439
440     gettimeofday(&c->timestamp, NULL);
441     c->expiry = c->timestamp;
442     catta_timeval_add(&c->expiry, r->ttl * 1000000);
443
444     if (c->time_event)
445         catta_time_event_update(c->time_event, &c->expiry);
446     else
447         c->time_event = catta_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         CATTA_OK,
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
473     };
474
475     assert(error <= 15);
476
477     return table[error];
478 }
479
480 static void handle_packet(CattaWideAreaLookupEngine *e, CattaDnsPacket *p) {
481     CattaWideAreaLookup *l = NULL;
482     int i, r;
483
484     CattaBrowserEvent final_event = CATTA_BROWSER_ALL_FOR_NOW;
485
486     assert(e);
487     assert(p);
488
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.");
492         goto finish;
493     }
494
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)
497         goto finish;
498
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) {
502
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;
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) catta_dns_packet_get_field(p, CATTA_DNS_FIELD_QDCOUNT); i > 0; i--) {
513         CattaKey *k;
514
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;
519             goto finish;
520         }
521
522         catta_key_unref(k);
523     }
524
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--) {
529
530         CattaRecord *rr;
531
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;
536             goto finish;
537         }
538
539         add_to_cache(e, rr);
540         catta_record_unref(rr);
541     }
542
543 finish:
544
545     if (l && !l->dead) {
546         if (l->callback)
547             l->callback(e, final_event, CATTA_LOOKUP_RESULT_WIDE_AREA, NULL, l->userdata);
548
549         lookup_stop(l);
550     }
551 }
552
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;
556
557     if (fd == e->fd_ipv4)
558         p = catta_recv_dns_packet_ipv4(e->fd_ipv4, NULL, NULL, NULL, NULL, NULL);
559     else {
560         assert(fd == e->fd_ipv6);
561         p = catta_recv_dns_packet_ipv6(e->fd_ipv6, NULL, NULL, NULL, NULL, NULL);
562     }
563
564     if (p) {
565         handle_packet(e, p);
566         catta_dns_packet_free(p);
567     }
568 }
569
570 CattaWideAreaLookupEngine *catta_wide_area_engine_new(CattaServer *s) {
571     CattaWideAreaLookupEngine *e;
572
573     assert(s);
574
575     e = catta_new(CattaWideAreaLookupEngine, 1);
576     e->server = s;
577     e->cleanup_dead = 0;
578
579     /* Create sockets */
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;
582
583     if (e->fd_ipv4 < 0 && e->fd_ipv6 < 0) {
584         catta_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         catta_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, CATTA_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, CATTA_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     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;
612
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);
617
618     return e;
619 }
620
621 void catta_wide_area_engine_free(CattaWideAreaLookupEngine *e) {
622     assert(e);
623
624     catta_wide_area_clear_cache(e);
625
626     while (e->lookups)
627         lookup_destroy(e->lookups);
628
629     catta_hashmap_free(e->cache_by_key);
630     catta_hashmap_free(e->lookups_by_id);
631     catta_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     catta_free(e);
646 }
647
648 void catta_wide_area_clear_cache(CattaWideAreaLookupEngine *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 catta_wide_area_set_servers(CattaWideAreaLookupEngine *e, const CattaAddress *a, unsigned n) {
658     assert(e);
659
660     if (a) {
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;
664     } else {
665         assert(n == 0);
666         e->n_dns_servers = 0;
667     }
668
669     e->current_dns_server = 0;
670
671     catta_wide_area_clear_cache(e);
672 }
673
674 void catta_wide_area_cache_dump(CattaWideAreaLookupEngine *e, CattaDumpCallback callback, void* userdata) {
675     CattaWideAreaCacheEntry *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 = catta_record_to_string(c->record);
684         callback(t, userdata);
685         catta_free(t);
686     }
687 }
688
689 unsigned catta_wide_area_scan_cache(CattaWideAreaLookupEngine *e, CattaKey *key, CattaWideAreaLookupCallback callback, void *userdata) {
690     CattaWideAreaCacheEntry *c;
691     CattaKey *cname_key;
692     unsigned n = 0;
693
694     assert(e);
695     assert(key);
696     assert(callback);
697
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);
700         n++;
701     }
702
703     if ((cname_key = catta_key_new_cname(key))) {
704
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);
707             n++;
708         }
709
710         catta_key_unref(cname_key);
711     }
712
713     return n;
714 }
715
716 int catta_wide_area_has_servers(CattaWideAreaLookupEngine *e) {
717     assert(e);
718
719     return e->n_dns_servers > 0;
720 }
721
722
723