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