]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - daemon/cachedump.c
import unbound 1.4.21
[FreeBSD/FreeBSD.git] / daemon / cachedump.c
1 /*
2  * daemon/cachedump.c - dump the cache to text format.
3  *
4  * Copyright (c) 2008, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /**
37  * \file
38  *
39  * This file contains functions to read and write the cache(s)
40  * to text format.
41  */
42 #include "config.h"
43 #include <ldns/ldns.h>
44 #include "daemon/cachedump.h"
45 #include "daemon/remote.h"
46 #include "daemon/worker.h"
47 #include "services/cache/rrset.h"
48 #include "services/cache/dns.h"
49 #include "services/cache/infra.h"
50 #include "util/data/msgreply.h"
51 #include "util/regional.h"
52 #include "util/net_help.h"
53 #include "util/data/dname.h"
54 #include "iterator/iterator.h"
55 #include "iterator/iter_delegpt.h"
56 #include "iterator/iter_utils.h"
57 #include "iterator/iter_fwd.h"
58 #include "iterator/iter_hints.h"
59
60 /** convert to ldns rr */
61 static ldns_rr*
62 to_rr(struct ub_packed_rrset_key* k, struct packed_rrset_data* d, 
63         time_t now, size_t i, uint16_t type)
64 {
65         ldns_rr* rr = ldns_rr_new();
66         ldns_rdf* rdf;
67         ldns_status status;
68         size_t pos;
69         log_assert(i < d->count + d->rrsig_count);
70         if(!rr) {
71                 return NULL;
72         }
73         ldns_rr_set_type(rr, type);
74         ldns_rr_set_class(rr, ntohs(k->rk.rrset_class));
75         if(d->rr_ttl[i] < now)
76                 ldns_rr_set_ttl(rr, 0);
77         else    ldns_rr_set_ttl(rr, d->rr_ttl[i] - now);
78         pos = 0;
79         status = ldns_wire2dname(&rdf, k->rk.dname, k->rk.dname_len, &pos);
80         if(status != LDNS_STATUS_OK) {
81                 /* we drop detailed error in status */
82                 ldns_rr_free(rr);
83                 return NULL;
84         }
85         ldns_rr_set_owner(rr, rdf);
86         pos = 0;
87         status = ldns_wire2rdf(rr, d->rr_data[i], d->rr_len[i], &pos);
88         if(status != LDNS_STATUS_OK) {
89                 /* we drop detailed error in status */
90                 ldns_rr_free(rr);
91                 return NULL;
92         }
93         return rr;
94 }
95
96 /** dump one rrset zonefile line */
97 static int
98 dump_rrset_line(SSL* ssl, struct ub_packed_rrset_key* k,
99         struct packed_rrset_data* d, time_t now, size_t i, uint16_t type)
100 {
101         char* s;
102         ldns_rr* rr = to_rr(k, d, now, i, type);
103         if(!rr) {
104                 return ssl_printf(ssl, "BADRR\n");
105         }
106         s = ldns_rr2str(rr);
107         ldns_rr_free(rr);
108         if(!s) {
109                 return ssl_printf(ssl, "BADRR\n");
110         }
111         if(!ssl_printf(ssl, "%s", s)) {
112                 free(s);
113                 return 0;
114         }
115         free(s);
116         return 1;
117 }
118
119 /** dump rrset key and data info */
120 static int
121 dump_rrset(SSL* ssl, struct ub_packed_rrset_key* k, 
122         struct packed_rrset_data* d, time_t now)
123 {
124         size_t i;
125         /* rd lock held by caller */
126         if(!k || !d) return 1;
127         if(d->ttl < now) return 1; /* expired */
128
129         /* meta line */
130         if(!ssl_printf(ssl, ";rrset%s %lld %u %u %d %d\n",
131                 (k->rk.flags & PACKED_RRSET_NSEC_AT_APEX)?" nsec_apex":"",
132                 (long long)(d->ttl - now),
133                 (unsigned)d->count, (unsigned)d->rrsig_count,
134                 (int)d->trust, (int)d->security
135                 )) 
136                 return 0;
137         for(i=0; i<d->count; i++) {
138                 if(!dump_rrset_line(ssl, k, d, now, i, ntohs(k->rk.type)))
139                         return 0;
140         }
141         for(i=0; i<d->rrsig_count; i++) {
142                 if(!dump_rrset_line(ssl, k, d, now, i+d->count, 
143                         LDNS_RR_TYPE_RRSIG))
144                         return 0;
145         }
146         
147         return 1;
148 }
149
150 /** dump lruhash rrset cache */
151 static int
152 dump_rrset_lruhash(SSL* ssl, struct lruhash* h, time_t now)
153 {
154         struct lruhash_entry* e;
155         /* lruhash already locked by caller */
156         /* walk in order of lru; best first */
157         for(e=h->lru_start; e; e = e->lru_next) {
158                 lock_rw_rdlock(&e->lock);
159                 if(!dump_rrset(ssl, (struct ub_packed_rrset_key*)e->key,
160                         (struct packed_rrset_data*)e->data, now)) {
161                         lock_rw_unlock(&e->lock);
162                         return 0;
163                 }
164                 lock_rw_unlock(&e->lock);
165         }
166         return 1;
167 }
168
169 /** dump rrset cache */
170 static int
171 dump_rrset_cache(SSL* ssl, struct worker* worker)
172 {
173         struct rrset_cache* r = worker->env.rrset_cache;
174         size_t slab;
175         if(!ssl_printf(ssl, "START_RRSET_CACHE\n")) return 0;
176         for(slab=0; slab<r->table.size; slab++) {
177                 lock_quick_lock(&r->table.array[slab]->lock);
178                 if(!dump_rrset_lruhash(ssl, r->table.array[slab],
179                         *worker->env.now)) {
180                         lock_quick_unlock(&r->table.array[slab]->lock);
181                         return 0;
182                 }
183                 lock_quick_unlock(&r->table.array[slab]->lock);
184         }
185         return ssl_printf(ssl, "END_RRSET_CACHE\n");
186 }
187
188 /** dump message to rrset reference */
189 static int
190 dump_msg_ref(SSL* ssl, struct ub_packed_rrset_key* k)
191 {
192         ldns_rdf* rdf;
193         ldns_status status;
194         size_t pos;
195         char* nm, *tp, *cl;
196
197         pos = 0;
198         status = ldns_wire2dname(&rdf, k->rk.dname, k->rk.dname_len, &pos);
199         if(status != LDNS_STATUS_OK) {
200                 return ssl_printf(ssl, "BADREF\n");
201         }
202         nm = ldns_rdf2str(rdf);
203         ldns_rdf_deep_free(rdf);
204         tp = ldns_rr_type2str(ntohs(k->rk.type));
205         cl = ldns_rr_class2str(ntohs(k->rk.rrset_class));
206         if(!nm || !cl || !tp) {
207                 free(nm);
208                 free(tp);
209                 free(cl);
210                 return ssl_printf(ssl, "BADREF\n");
211         }
212         if(!ssl_printf(ssl, "%s %s %s %d\n", nm, cl, tp, (int)k->rk.flags)) {
213                 free(nm);
214                 free(tp);
215                 free(cl);
216                 return 0;
217         }
218         free(nm);
219         free(tp);
220         free(cl);
221
222         return 1;
223 }
224
225 /** dump message entry */
226 static int
227 dump_msg(SSL* ssl, struct query_info* k, struct reply_info* d, 
228         time_t now)
229 {
230         size_t i;
231         char* nm, *tp, *cl;
232         ldns_rdf* rdf;
233         ldns_status status;
234         size_t pos;
235         if(!k || !d) return 1;
236         if(d->ttl < now) return 1; /* expired */
237         
238         pos = 0;
239         status = ldns_wire2dname(&rdf, k->qname, k->qname_len, &pos);
240         if(status != LDNS_STATUS_OK) {
241                 return 1; /* skip this entry */
242         }
243         nm = ldns_rdf2str(rdf);
244         ldns_rdf_deep_free(rdf);
245         tp = ldns_rr_type2str(k->qtype);
246         cl = ldns_rr_class2str(k->qclass);
247         if(!nm || !tp || !cl) {
248                 free(nm);
249                 free(tp);
250                 free(cl);
251                 return 1; /* skip this entry */
252         }
253         if(!rrset_array_lock(d->ref, d->rrset_count, now)) {
254                 /* rrsets have timed out or do not exist */
255                 free(nm);
256                 free(tp);
257                 free(cl);
258                 return 1; /* skip this entry */
259         }
260         
261         /* meta line */
262         if(!ssl_printf(ssl, "msg %s %s %s %d %d %lld %d %u %u %u\n",
263                         nm, cl, tp,
264                         (int)d->flags, (int)d->qdcount, 
265                         (long long)(d->ttl-now), (int)d->security,
266                         (unsigned)d->an_numrrsets, 
267                         (unsigned)d->ns_numrrsets,
268                         (unsigned)d->ar_numrrsets)) {
269                 free(nm);
270                 free(tp);
271                 free(cl);
272                 rrset_array_unlock(d->ref, d->rrset_count);
273                 return 0;
274         }
275         free(nm);
276         free(tp);
277         free(cl);
278         
279         for(i=0; i<d->rrset_count; i++) {
280                 if(!dump_msg_ref(ssl, d->rrsets[i])) {
281                         rrset_array_unlock(d->ref, d->rrset_count);
282                         return 0;
283                 }
284         }
285         rrset_array_unlock(d->ref, d->rrset_count);
286
287         return 1;
288 }
289
290 /** copy msg to worker pad */
291 static int
292 copy_msg(struct regional* region, struct lruhash_entry* e, 
293         struct query_info** k, struct reply_info** d)
294 {
295         struct reply_info* rep = (struct reply_info*)e->data;
296         *d = (struct reply_info*)regional_alloc_init(region, e->data,
297                 sizeof(struct reply_info) + 
298                 sizeof(struct rrset_ref) * (rep->rrset_count-1) +
299                 sizeof(struct ub_packed_rrset_key*) * rep->rrset_count);
300         if(!*d)
301                 return 0;
302         (*d)->rrsets = (struct ub_packed_rrset_key**)(
303                 (uint8_t*)(&((*d)->ref[0])) + 
304                 sizeof(struct rrset_ref) * rep->rrset_count);
305         *k = (struct query_info*)regional_alloc_init(region, 
306                 e->key, sizeof(struct query_info));
307         if(!*k)
308                 return 0;
309         (*k)->qname = regional_alloc_init(region, 
310                 (*k)->qname, (*k)->qname_len);
311         return (*k)->qname != NULL;
312 }
313
314 /** dump lruhash msg cache */
315 static int
316 dump_msg_lruhash(SSL* ssl, struct worker* worker, struct lruhash* h)
317 {
318         struct lruhash_entry* e;
319         struct query_info* k;
320         struct reply_info* d;
321
322         /* lruhash already locked by caller */
323         /* walk in order of lru; best first */
324         for(e=h->lru_start; e; e = e->lru_next) {
325                 regional_free_all(worker->scratchpad);
326                 lock_rw_rdlock(&e->lock);
327                 /* make copy of rrset in worker buffer */
328                 if(!copy_msg(worker->scratchpad, e, &k, &d)) {
329                         lock_rw_unlock(&e->lock);
330                         return 0;
331                 }
332                 lock_rw_unlock(&e->lock);
333                 /* release lock so we can lookup the rrset references 
334                  * in the rrset cache */
335                 if(!dump_msg(ssl, k, d, *worker->env.now)) {
336                         return 0;
337                 }
338         }
339         return 1;
340 }
341
342 /** dump msg cache */
343 static int
344 dump_msg_cache(SSL* ssl, struct worker* worker)
345 {
346         struct slabhash* sh = worker->env.msg_cache;
347         size_t slab;
348         if(!ssl_printf(ssl, "START_MSG_CACHE\n")) return 0;
349         for(slab=0; slab<sh->size; slab++) {
350                 lock_quick_lock(&sh->array[slab]->lock);
351                 if(!dump_msg_lruhash(ssl, worker, sh->array[slab])) {
352                         lock_quick_unlock(&sh->array[slab]->lock);
353                         return 0;
354                 }
355                 lock_quick_unlock(&sh->array[slab]->lock);
356         }
357         return ssl_printf(ssl, "END_MSG_CACHE\n");
358 }
359
360 int
361 dump_cache(SSL* ssl, struct worker* worker)
362 {
363         if(!dump_rrset_cache(ssl, worker))
364                 return 0;
365         if(!dump_msg_cache(ssl, worker))
366                 return 0;
367         return ssl_printf(ssl, "EOF\n");
368 }
369
370 /** read a line from ssl into buffer */
371 static int
372 ssl_read_buf(SSL* ssl, ldns_buffer* buf)
373 {
374         return ssl_read_line(ssl, (char*)ldns_buffer_begin(buf), 
375                 ldns_buffer_capacity(buf));
376 }
377
378 /** check fixed text on line */
379 static int
380 read_fixed(SSL* ssl, ldns_buffer* buf, const char* str)
381 {
382         if(!ssl_read_buf(ssl, buf)) return 0;
383         return (strcmp((char*)ldns_buffer_begin(buf), str) == 0);
384 }
385
386 /** load an RR into rrset */
387 static int
388 load_rr(SSL* ssl, ldns_buffer* buf, struct regional* region,
389         struct ub_packed_rrset_key* rk, struct packed_rrset_data* d,
390         unsigned int i, int is_rrsig, int* go_on, time_t now)
391 {
392         ldns_rr* rr;
393         ldns_status status;
394
395         /* read the line */
396         if(!ssl_read_buf(ssl, buf))
397                 return 0;
398         if(strncmp((char*)ldns_buffer_begin(buf), "BADRR\n", 6) == 0) {
399                 *go_on = 0;
400                 return 1;
401         }
402         status = ldns_rr_new_frm_str(&rr, (char*)ldns_buffer_begin(buf),
403                 LDNS_DEFAULT_TTL, NULL, NULL);
404         if(status != LDNS_STATUS_OK) {
405                 log_warn("error cannot parse rr: %s: %s",
406                         ldns_get_errorstr_by_id(status),
407                         (char*)ldns_buffer_begin(buf));
408                 return 0;
409         }
410         if(is_rrsig && ldns_rr_get_type(rr) != LDNS_RR_TYPE_RRSIG) {
411                 log_warn("error expected rrsig but got %s",
412                         (char*)ldns_buffer_begin(buf));
413                 return 0;
414         }
415
416         /* convert ldns rr into packed_rr */
417         d->rr_ttl[i] = ldns_rr_ttl(rr) + now;
418         ldns_buffer_clear(buf);
419         ldns_buffer_skip(buf, 2);
420         status = ldns_rr_rdata2buffer_wire(buf, rr);
421         if(status != LDNS_STATUS_OK) {
422                 log_warn("error cannot rr2wire: %s",
423                         ldns_get_errorstr_by_id(status));
424                 ldns_rr_free(rr);
425                 return 0;
426         }
427         ldns_buffer_flip(buf);
428         ldns_buffer_write_u16_at(buf, 0, ldns_buffer_limit(buf) - 2);
429
430         d->rr_len[i] = ldns_buffer_limit(buf);
431         d->rr_data[i] = (uint8_t*)regional_alloc_init(region, 
432                 ldns_buffer_begin(buf), ldns_buffer_limit(buf));
433         if(!d->rr_data[i]) {
434                 ldns_rr_free(rr);
435                 log_warn("error out of memory");
436                 return 0;
437         }
438
439         /* if first entry, fill the key structure */
440         if(i==0) {
441                 rk->rk.type = htons(ldns_rr_get_type(rr));
442                 rk->rk.rrset_class = htons(ldns_rr_get_class(rr));
443                 ldns_buffer_clear(buf);
444                 status = ldns_dname2buffer_wire(buf, ldns_rr_owner(rr));
445                 if(status != LDNS_STATUS_OK) {
446                         log_warn("error cannot dname2buffer: %s",
447                                 ldns_get_errorstr_by_id(status));
448                         ldns_rr_free(rr);
449                         return 0;
450                 }
451                 ldns_buffer_flip(buf);
452                 rk->rk.dname_len = ldns_buffer_limit(buf);
453                 rk->rk.dname = regional_alloc_init(region, 
454                         ldns_buffer_begin(buf), ldns_buffer_limit(buf));
455                 if(!rk->rk.dname) {
456                         log_warn("error out of memory");
457                         ldns_rr_free(rr);
458                         return 0;
459                 }
460         }
461         ldns_rr_free(rr);
462
463         return 1;
464 }
465
466 /** move entry into cache */
467 static int
468 move_into_cache(struct ub_packed_rrset_key* k, 
469         struct packed_rrset_data* d, struct worker* worker)
470 {
471         struct ub_packed_rrset_key* ak;
472         struct packed_rrset_data* ad;
473         size_t s, i, num = d->count + d->rrsig_count;
474         struct rrset_ref ref;
475         uint8_t* p;
476
477         ak = alloc_special_obtain(&worker->alloc);
478         if(!ak) {
479                 log_warn("error out of memory");
480                 return 0;
481         }
482         ak->entry.data = NULL;
483         ak->rk = k->rk;
484         ak->entry.hash = rrset_key_hash(&k->rk);
485         ak->rk.dname = (uint8_t*)memdup(k->rk.dname, k->rk.dname_len);
486         if(!ak->rk.dname) {
487                 log_warn("error out of memory");
488                 ub_packed_rrset_parsedelete(ak, &worker->alloc);
489                 return 0;
490         }
491         s = sizeof(*ad) + (sizeof(size_t) + sizeof(uint8_t*) + 
492                 sizeof(time_t))* num;
493         for(i=0; i<num; i++)
494                 s += d->rr_len[i];
495         ad = (struct packed_rrset_data*)malloc(s);
496         if(!ad) {
497                 log_warn("error out of memory");
498                 ub_packed_rrset_parsedelete(ak, &worker->alloc);
499                 return 0;
500         }
501         p = (uint8_t*)ad;
502         memmove(p, d, sizeof(*ad));
503         p += sizeof(*ad);
504         memmove(p, &d->rr_len[0], sizeof(size_t)*num);
505         p += sizeof(size_t)*num;
506         memmove(p, &d->rr_data[0], sizeof(uint8_t*)*num);
507         p += sizeof(uint8_t*)*num;
508         memmove(p, &d->rr_ttl[0], sizeof(time_t)*num);
509         p += sizeof(time_t)*num;
510         for(i=0; i<num; i++) {
511                 memmove(p, d->rr_data[i], d->rr_len[i]);
512                 p += d->rr_len[i];
513         }
514         packed_rrset_ptr_fixup(ad);
515
516         ak->entry.data = ad;
517
518         ref.key = ak;
519         ref.id = ak->id;
520         (void)rrset_cache_update(worker->env.rrset_cache, &ref,
521                 &worker->alloc, *worker->env.now);
522         return 1;
523 }
524
525 /** load an rrset entry */
526 static int
527 load_rrset(SSL* ssl, ldns_buffer* buf, struct worker* worker)
528 {
529         char* s = (char*)ldns_buffer_begin(buf);
530         struct regional* region = worker->scratchpad;
531         struct ub_packed_rrset_key* rk;
532         struct packed_rrset_data* d;
533         unsigned int rr_count, rrsig_count, trust, security;
534         long long ttl;
535         unsigned int i;
536         int go_on = 1;
537         regional_free_all(region);
538
539         rk = (struct ub_packed_rrset_key*)regional_alloc_zero(region, 
540                 sizeof(*rk));
541         d = (struct packed_rrset_data*)regional_alloc_zero(region, sizeof(*d));
542         if(!rk || !d) {
543                 log_warn("error out of memory");
544                 return 0;
545         }
546
547         if(strncmp(s, ";rrset", 6) != 0) {
548                 log_warn("error expected ';rrset' but got %s", s);
549                 return 0;
550         }
551         s += 6;
552         if(strncmp(s, " nsec_apex", 10) == 0) {
553                 s += 10;
554                 rk->rk.flags |= PACKED_RRSET_NSEC_AT_APEX;
555         }
556         if(sscanf(s, " %lld %u %u %u %u", &ttl, &rr_count, &rrsig_count,
557                 &trust, &security) != 5) {
558                 log_warn("error bad rrset spec %s", s);
559                 return 0;
560         }
561         if(rr_count == 0 && rrsig_count == 0) {
562                 log_warn("bad rrset without contents");
563                 return 0;
564         }
565         d->count = (size_t)rr_count;
566         d->rrsig_count = (size_t)rrsig_count;
567         d->security = (enum sec_status)security;
568         d->trust = (enum rrset_trust)trust;
569         d->ttl = (time_t)ttl + *worker->env.now;
570
571         d->rr_len = regional_alloc_zero(region, 
572                 sizeof(size_t)*(d->count+d->rrsig_count));
573         d->rr_ttl = regional_alloc_zero(region, 
574                 sizeof(time_t)*(d->count+d->rrsig_count));
575         d->rr_data = regional_alloc_zero(region, 
576                 sizeof(uint8_t*)*(d->count+d->rrsig_count));
577         if(!d->rr_len || !d->rr_ttl || !d->rr_data) {
578                 log_warn("error out of memory");
579                 return 0;
580         }
581         
582         /* read the rr's themselves */
583         for(i=0; i<rr_count; i++) {
584                 if(!load_rr(ssl, buf, region, rk, d, i, 0, 
585                         &go_on, *worker->env.now)) {
586                         log_warn("could not read rr %u", i);
587                         return 0;
588                 }
589         }
590         for(i=0; i<rrsig_count; i++) {
591                 if(!load_rr(ssl, buf, region, rk, d, i+rr_count, 1, 
592                         &go_on, *worker->env.now)) {
593                         log_warn("could not read rrsig %u", i);
594                         return 0;
595                 }
596         }
597         if(!go_on) {
598                 /* skip this entry */
599                 return 1;
600         }
601
602         return move_into_cache(rk, d, worker);
603 }
604
605 /** load rrset cache */
606 static int
607 load_rrset_cache(SSL* ssl, struct worker* worker)
608 {
609         ldns_buffer* buf = worker->env.scratch_buffer;
610         if(!read_fixed(ssl, buf, "START_RRSET_CACHE")) return 0;
611         while(ssl_read_buf(ssl, buf) && 
612                 strcmp((char*)ldns_buffer_begin(buf), "END_RRSET_CACHE")!=0) {
613                 if(!load_rrset(ssl, buf, worker))
614                         return 0;
615         }
616         return 1;
617 }
618
619 /** read qinfo from next three words */
620 static char*
621 load_qinfo(char* str, struct query_info* qinfo, ldns_buffer* buf, 
622         struct regional* region)
623 {
624         /* s is part of the buf */
625         char* s = str;
626         ldns_rr* rr;
627         ldns_status status;
628
629         /* skip three words */
630         s = strchr(str, ' ');
631         if(s) s = strchr(s+1, ' ');
632         if(s) s = strchr(s+1, ' ');
633         if(!s) {
634                 log_warn("error line too short, %s", str);
635                 return NULL;
636         }
637         s[0] = 0;
638         s++;
639
640         /* parse them */
641         status = ldns_rr_new_question_frm_str(&rr, str, NULL, NULL);
642         if(status != LDNS_STATUS_OK) {
643                 log_warn("error cannot parse: %s %s",
644                         ldns_get_errorstr_by_id(status), str);
645                 return NULL;
646         }
647         qinfo->qtype = ldns_rr_get_type(rr);
648         qinfo->qclass = ldns_rr_get_class(rr);
649         ldns_buffer_clear(buf);
650         status = ldns_dname2buffer_wire(buf, ldns_rr_owner(rr));
651         ldns_rr_free(rr);
652         if(status != LDNS_STATUS_OK) {
653                 log_warn("error cannot dname2wire: %s", 
654                         ldns_get_errorstr_by_id(status));
655                 return NULL;
656         }
657         ldns_buffer_flip(buf);
658         qinfo->qname_len = ldns_buffer_limit(buf);
659         qinfo->qname = (uint8_t*)regional_alloc_init(region, 
660                 ldns_buffer_begin(buf), ldns_buffer_limit(buf));
661         if(!qinfo->qname) {
662                 log_warn("error out of memory");
663                 return NULL;
664         }
665
666         return s;
667 }
668
669 /** load a msg rrset reference */
670 static int
671 load_ref(SSL* ssl, ldns_buffer* buf, struct worker* worker, 
672         struct regional *region, struct ub_packed_rrset_key** rrset, 
673         int* go_on)
674 {
675         char* s = (char*)ldns_buffer_begin(buf);
676         struct query_info qinfo;
677         unsigned int flags;
678         struct ub_packed_rrset_key* k;
679
680         /* read line */
681         if(!ssl_read_buf(ssl, buf))
682                 return 0;
683         if(strncmp(s, "BADREF", 6) == 0) {
684                 *go_on = 0; /* its bad, skip it and skip message */
685                 return 1;
686         }
687
688         s = load_qinfo(s, &qinfo, buf, region);
689         if(!s) {
690                 return 0;
691         }
692         if(sscanf(s, " %u", &flags) != 1) {
693                 log_warn("error cannot parse flags: %s", s);
694                 return 0;
695         }
696
697         /* lookup in cache */
698         k = rrset_cache_lookup(worker->env.rrset_cache, qinfo.qname,
699                 qinfo.qname_len, qinfo.qtype, qinfo.qclass,
700                 (uint32_t)flags, *worker->env.now, 0);
701         if(!k) {
702                 /* not found or expired */
703                 *go_on = 0;
704                 return 1;
705         }
706
707         /* store in result */
708         *rrset = packed_rrset_copy_region(k, region, *worker->env.now);
709         lock_rw_unlock(&k->entry.lock);
710
711         return (*rrset != NULL);
712 }
713
714 /** load a msg entry */
715 static int
716 load_msg(SSL* ssl, ldns_buffer* buf, struct worker* worker)
717 {
718         struct regional* region = worker->scratchpad;
719         struct query_info qinf;
720         struct reply_info rep;
721         char* s = (char*)ldns_buffer_begin(buf);
722         unsigned int flags, qdcount, security, an, ns, ar;
723         long long ttl;
724         size_t i;
725         int go_on = 1;
726
727         regional_free_all(region);
728
729         if(strncmp(s, "msg ", 4) != 0) {
730                 log_warn("error expected msg but got %s", s);
731                 return 0;
732         }
733         s += 4;
734         s = load_qinfo(s, &qinf, buf, region);
735         if(!s) {
736                 return 0;
737         }
738
739         /* read remainder of line */
740         if(sscanf(s, " %u %u %lld %u %u %u %u", &flags, &qdcount, &ttl, 
741                 &security, &an, &ns, &ar) != 7) {
742                 log_warn("error cannot parse numbers: %s", s);
743                 return 0;
744         }
745         rep.flags = (uint16_t)flags;
746         rep.qdcount = (uint16_t)qdcount;
747         rep.ttl = (time_t)ttl;
748         rep.prefetch_ttl = PREFETCH_TTL_CALC(rep.ttl);
749         rep.security = (enum sec_status)security;
750         rep.an_numrrsets = (size_t)an;
751         rep.ns_numrrsets = (size_t)ns;
752         rep.ar_numrrsets = (size_t)ar;
753         rep.rrset_count = (size_t)an+(size_t)ns+(size_t)ar;
754         rep.rrsets = (struct ub_packed_rrset_key**)regional_alloc_zero(
755                 region, sizeof(struct ub_packed_rrset_key*)*rep.rrset_count);
756
757         /* fill repinfo with references */
758         for(i=0; i<rep.rrset_count; i++) {
759                 if(!load_ref(ssl, buf, worker, region, &rep.rrsets[i], 
760                         &go_on)) {
761                         return 0;
762                 }
763         }
764
765         if(!go_on) 
766                 return 1; /* skip this one, not all references satisfied */
767
768         if(!dns_cache_store(&worker->env, &qinf, &rep, 0, 0, 0, NULL)) {
769                 log_warn("error out of memory");
770                 return 0;
771         }
772         return 1;
773 }
774
775 /** load msg cache */
776 static int
777 load_msg_cache(SSL* ssl, struct worker* worker)
778 {
779         ldns_buffer* buf = worker->env.scratch_buffer;
780         if(!read_fixed(ssl, buf, "START_MSG_CACHE")) return 0;
781         while(ssl_read_buf(ssl, buf) && 
782                 strcmp((char*)ldns_buffer_begin(buf), "END_MSG_CACHE")!=0) {
783                 if(!load_msg(ssl, buf, worker))
784                         return 0;
785         }
786         return 1;
787 }
788
789 int
790 load_cache(SSL* ssl, struct worker* worker)
791 {
792         if(!load_rrset_cache(ssl, worker))
793                 return 0;
794         if(!load_msg_cache(ssl, worker))
795                 return 0;
796         return read_fixed(ssl, worker->env.scratch_buffer, "EOF");
797 }
798
799 /** print details on a delegation point */
800 static void
801 print_dp_details(SSL* ssl, struct worker* worker, struct delegpt* dp)
802 {
803         char buf[257];
804         struct delegpt_addr* a;
805         int lame, dlame, rlame, rto, edns_vs, to, delay,
806                 tA = 0, tAAAA = 0, tother = 0;
807         long long entry_ttl;
808         struct rtt_info ri;
809         uint8_t edns_lame_known;
810         for(a = dp->target_list; a; a = a->next_target) {
811                 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
812                 if(!ssl_printf(ssl, "%-16s\t", buf))
813                         return;
814                 if(a->bogus) {
815                         if(!ssl_printf(ssl, "Address is BOGUS. ")) 
816                                 return;
817                 }
818                 /* lookup in infra cache */
819                 delay=0;
820                 entry_ttl = infra_get_host_rto(worker->env.infra_cache,
821                         &a->addr, a->addrlen, dp->name, dp->namelen,
822                         &ri, &delay, *worker->env.now, &tA, &tAAAA, &tother);
823                 if(entry_ttl == -2 && ri.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
824                         if(!ssl_printf(ssl, "expired, rto %d msec, tA %d "
825                                 "tAAAA %d tother %d.\n", ri.rto, tA, tAAAA,
826                                 tother))
827                                 return;
828                         continue;
829                 }
830                 if(entry_ttl == -1 || entry_ttl == -2) {
831                         if(!ssl_printf(ssl, "not in infra cache.\n"))
832                                 return;
833                         continue; /* skip stuff not in infra cache */
834                 }
835
836                 /* uses type_A because most often looked up, but other
837                  * lameness won't be reported then */
838                 if(!infra_get_lame_rtt(worker->env.infra_cache, 
839                         &a->addr, a->addrlen, dp->name, dp->namelen,
840                         LDNS_RR_TYPE_A, &lame, &dlame, &rlame, &rto,
841                         *worker->env.now)) {
842                         if(!ssl_printf(ssl, "not in infra cache.\n"))
843                                 return;
844                         continue; /* skip stuff not in infra cache */
845                 }
846                 if(!ssl_printf(ssl, "%s%s%s%srto %d msec, ttl %lld, ping %d "
847                         "var %d rtt %d, tA %d, tAAAA %d, tother %d",
848                         lame?"LAME ":"", dlame?"NoDNSSEC ":"",
849                         a->lame?"AddrWasParentSide ":"",
850                         rlame?"NoAuthButRecursive ":"", rto, entry_ttl,
851                         ri.srtt, ri.rttvar, rtt_notimeout(&ri),
852                         tA, tAAAA, tother))
853                         return;
854                 if(delay)
855                         if(!ssl_printf(ssl, ", probedelay %d", delay))
856                                 return;
857                 if(infra_host(worker->env.infra_cache, &a->addr, a->addrlen,
858                         dp->name, dp->namelen, *worker->env.now, &edns_vs,
859                         &edns_lame_known, &to)) {
860                         if(edns_vs == -1) {
861                                 if(!ssl_printf(ssl, ", noEDNS%s.",
862                                         edns_lame_known?" probed":" assumed"))
863                                         return;
864                         } else {
865                                 if(!ssl_printf(ssl, ", EDNS %d%s.", edns_vs,
866                                         edns_lame_known?" probed":" assumed"))
867                                         return;
868                         }
869                 }
870                 if(!ssl_printf(ssl, "\n"))
871                         return;
872         }
873 }
874
875 /** print main dp info */
876 static void
877 print_dp_main(SSL* ssl, struct delegpt* dp, struct dns_msg* msg)
878 {
879         size_t i, n_ns, n_miss, n_addr, n_res, n_avail;
880
881         /* print the dp */
882         if(msg)
883             for(i=0; i<msg->rep->rrset_count; i++) {
884                 struct ub_packed_rrset_key* k = msg->rep->rrsets[i];
885                 struct packed_rrset_data* d = 
886                         (struct packed_rrset_data*)k->entry.data;
887                 if(d->security == sec_status_bogus) {
888                         if(!ssl_printf(ssl, "Address is BOGUS:\n"))
889                                 return;
890                 }
891                 if(!dump_rrset(ssl, k, d, 0))
892                         return;
893             }
894         delegpt_count_ns(dp, &n_ns, &n_miss);
895         delegpt_count_addr(dp, &n_addr, &n_res, &n_avail);
896         /* since dp has not been used by iterator, all are available*/
897         if(!ssl_printf(ssl, "Delegation with %d names, of which %d "
898                 "can be examined to query further addresses.\n"
899                 "%sIt provides %d IP addresses.\n", 
900                 (int)n_ns, (int)n_miss, (dp->bogus?"It is BOGUS. ":""),
901                 (int)n_addr))
902                 return;
903 }
904
905 int print_deleg_lookup(SSL* ssl, struct worker* worker, uint8_t* nm,
906         size_t nmlen, int ATTR_UNUSED(nmlabs))
907 {
908         /* deep links into the iterator module */
909         struct delegpt* dp;
910         struct dns_msg* msg;
911         struct regional* region = worker->scratchpad;
912         char b[260];
913         struct query_info qinfo;
914         struct iter_hints_stub* stub;
915         regional_free_all(region);
916         qinfo.qname = nm;
917         qinfo.qname_len = nmlen;
918         qinfo.qtype = LDNS_RR_TYPE_A;
919         qinfo.qclass = LDNS_RR_CLASS_IN;
920
921         dname_str(nm, b);
922         if(!ssl_printf(ssl, "The following name servers are used for lookup "
923                 "of %s\n", b)) 
924                 return 0;
925         
926         dp = forwards_lookup(worker->env.fwds, nm, qinfo.qclass);
927         if(dp) {
928                 if(!ssl_printf(ssl, "forwarding request:\n"))
929                         return 0;
930                 print_dp_main(ssl, dp, NULL);
931                 print_dp_details(ssl, worker, dp);
932                 return 1;
933         }
934         
935         while(1) {
936                 dp = dns_cache_find_delegation(&worker->env, nm, nmlen, 
937                         qinfo.qtype, qinfo.qclass, region, &msg, 
938                         *worker->env.now);
939                 if(!dp) {
940                         return ssl_printf(ssl, "no delegation from "
941                                 "cache; goes to configured roots\n");
942                 }
943                 /* go up? */
944                 if(iter_dp_is_useless(&qinfo, BIT_RD, dp)) {
945                         print_dp_main(ssl, dp, msg);
946                         print_dp_details(ssl, worker, dp);
947                         if(!ssl_printf(ssl, "cache delegation was "
948                                 "useless (no IP addresses)\n"))
949                                 return 0;
950                         if(dname_is_root(nm)) {
951                                 /* goes to root config */
952                                 return ssl_printf(ssl, "no delegation from "
953                                         "cache; goes to configured roots\n");
954                         } else {
955                                 /* useless, goes up */
956                                 nm = dp->name;
957                                 nmlen = dp->namelen;
958                                 dname_remove_label(&nm, &nmlen);
959                                 dname_str(nm, b);
960                                 if(!ssl_printf(ssl, "going up, lookup %s\n", b))
961                                         return 0;
962                                 continue;
963                         }
964                 } 
965                 stub = hints_lookup_stub(worker->env.hints, nm, qinfo.qclass,
966                         dp);
967                 if(stub) {
968                         if(stub->noprime) {
969                                 if(!ssl_printf(ssl, "The noprime stub servers "
970                                         "are used:\n"))
971                                         return 0;
972                         } else {
973                                 if(!ssl_printf(ssl, "The stub is primed "
974                                                 "with servers:\n"))
975                                         return 0;
976                         }
977                         print_dp_main(ssl, stub->dp, NULL);
978                         print_dp_details(ssl, worker, stub->dp);
979                 } else {
980                         print_dp_main(ssl, dp, msg);
981                         print_dp_details(ssl, worker, dp);
982                 }
983                 break;
984         }
985
986         return 1;
987 }