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