1 /* 2 * services/cache/dns.c - Cache services for DNS using msg and rrset caches. 3 * 4 * Copyright (c) 2007, 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 the DNS cache. 40 */ 41 #include "config.h" 42 #include "iterator/iter_delegpt.h" 43 #include "iterator/iter_utils.h" 44 #include "validator/val_nsec.h" 45 #include "validator/val_utils.h" 46 #include "services/cache/dns.h" 47 #include "services/cache/rrset.h" 48 #include "util/data/msgparse.h" 49 #include "util/data/msgreply.h" 50 #include "util/data/packed_rrset.h" 51 #include "util/data/dname.h" 52 #include "util/module.h" 53 #include "util/net_help.h" 54 #include "util/regional.h" 55 #include "util/config_file.h" 56 #include "sldns/sbuffer.h" 57 58 /** store rrsets in the rrset cache. 59 * @param env: module environment with caches. 60 * @param rep: contains list of rrsets to store. 61 * @param now: current time. 62 * @param leeway: during prefetch how much leeway to update TTLs. 63 * This makes rrsets (other than type NS) timeout sooner so they get 64 * updated with a new full TTL. 65 * Type NS does not get this, because it must not be refreshed from the 66 * child domain, but keep counting down properly. 67 * @param pside: if from parentside discovered NS, so that its NS is okay 68 * in a prefetch situation to be updated (without becoming sticky). 69 * @param qrep: update rrsets here if cache is better 70 * @param region: for qrep allocs. 71 * @param qstarttime: time when delegations were looked up, this is perhaps 72 * earlier than the time in now. The time is used to determine if RRsets 73 * of type NS have expired, so that they can only be updated using 74 * lookups of delegation points that did not use them, since they had 75 * expired then. 76 */ 77 static void 78 store_rrsets(struct module_env* env, struct reply_info* rep, time_t now, 79 time_t leeway, int pside, struct reply_info* qrep, 80 struct regional* region, time_t qstarttime) 81 { 82 size_t i; 83 time_t ttl, min_ttl = rep->ttl; 84 /* see if rrset already exists in cache, if not insert it. */ 85 for(i=0; i<rep->rrset_count; i++) { 86 rep->ref[i].key = rep->rrsets[i]; 87 rep->ref[i].id = rep->rrsets[i]->id; 88 /* update ref if it was in the cache */ 89 switch(rrset_cache_update(env->rrset_cache, &rep->ref[i], 90 env->alloc, ((ntohs(rep->ref[i].key->rk.type)== 91 LDNS_RR_TYPE_NS && !pside)?qstarttime:now + leeway))) { 92 case 0: /* ref unchanged, item inserted */ 93 break; 94 case 2: /* ref updated, cache is superior */ 95 if(region) { 96 struct ub_packed_rrset_key* ck; 97 lock_rw_rdlock(&rep->ref[i].key->entry.lock); 98 /* if deleted rrset, do not copy it */ 99 if(rep->ref[i].key->id == 0 || 100 rep->ref[i].id != rep->ref[i].key->id) 101 ck = NULL; 102 else ck = packed_rrset_copy_region( 103 rep->ref[i].key, region, now); 104 lock_rw_unlock(&rep->ref[i].key->entry.lock); 105 if(ck) { 106 /* use cached copy if memory allows */ 107 qrep->rrsets[i] = ck; 108 } 109 } 110 /* no break: also copy key item */ 111 /* the line below is matched by gcc regex and silences 112 * the fallthrough warning */ 113 ATTR_FALLTHROUGH 114 /* fallthrough */ 115 case 1: /* ref updated, item inserted */ 116 rep->rrsets[i] = rep->ref[i].key; 117 /* ref was updated; make sure the message ttl is 118 * updated to the minimum of the current rrsets. */ 119 lock_rw_rdlock(&rep->ref[i].key->entry.lock); 120 /* if deleted, skip ttl update. */ 121 if(rep->ref[i].key->id != 0 && 122 rep->ref[i].id == rep->ref[i].key->id) { 123 ttl = ((struct packed_rrset_data*) 124 rep->rrsets[i]->entry.data)->ttl; 125 if(ttl < min_ttl) min_ttl = ttl; 126 } 127 lock_rw_unlock(&rep->ref[i].key->entry.lock); 128 } 129 } 130 if(min_ttl < rep->ttl) { 131 rep->ttl = min_ttl; 132 rep->prefetch_ttl = PREFETCH_TTL_CALC(rep->ttl); 133 rep->serve_expired_ttl = rep->ttl + SERVE_EXPIRED_TTL; 134 } 135 } 136 137 /** delete message from message cache */ 138 void 139 msg_cache_remove(struct module_env* env, uint8_t* qname, size_t qnamelen, 140 uint16_t qtype, uint16_t qclass, uint16_t flags) 141 { 142 struct query_info k; 143 hashvalue_type h; 144 145 k.qname = qname; 146 k.qname_len = qnamelen; 147 k.qtype = qtype; 148 k.qclass = qclass; 149 k.local_alias = NULL; 150 h = query_info_hash(&k, flags); 151 slabhash_remove(env->msg_cache, h, &k); 152 } 153 154 void 155 dns_cache_store_msg(struct module_env* env, struct query_info* qinfo, 156 hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside, 157 struct reply_info* qrep, uint32_t flags, struct regional* region, 158 time_t qstarttime) 159 { 160 struct msgreply_entry* e; 161 time_t ttl = rep->ttl; 162 size_t i; 163 164 /* store RRsets */ 165 for(i=0; i<rep->rrset_count; i++) { 166 rep->ref[i].key = rep->rrsets[i]; 167 rep->ref[i].id = rep->rrsets[i]->id; 168 } 169 170 /* there was a reply_info_sortref(rep) here but it seems to be 171 * unnecessary, because the cache gets locked per rrset. */ 172 reply_info_set_ttls(rep, *env->now); 173 store_rrsets(env, rep, *env->now, leeway, pside, qrep, region, 174 qstarttime); 175 if(ttl == 0 && !(flags & DNSCACHE_STORE_ZEROTTL)) { 176 /* we do not store the message, but we did store the RRs, 177 * which could be useful for delegation information */ 178 verbose(VERB_ALGO, "TTL 0: dropped msg from cache"); 179 reply_info_delete(rep, NULL); 180 /* if the message is in the cache, remove that msg, 181 * so that the TTL 0 response can be returned for future 182 * responses (i.e. don't get answered from 183 * cache, but instead go to recursion to get this TTL0 184 * response). 185 * Possible messages that could be in the cache: 186 * - SERVFAIL 187 * - NXDOMAIN 188 * - NODATA 189 * - an older record that is expired 190 * - an older record that did not yet expire */ 191 msg_cache_remove(env, qinfo->qname, qinfo->qname_len, 192 qinfo->qtype, qinfo->qclass, flags); 193 return; 194 } 195 196 /* store msg in the cache */ 197 reply_info_sortref(rep); 198 if(!(e = query_info_entrysetup(qinfo, rep, hash))) { 199 log_err("store_msg: malloc failed"); 200 return; 201 } 202 slabhash_insert(env->msg_cache, hash, &e->entry, rep, env->alloc); 203 } 204 205 /** find closest NS or DNAME and returns the rrset (locked) */ 206 static struct ub_packed_rrset_key* 207 find_closest_of_type(struct module_env* env, uint8_t* qname, size_t qnamelen, 208 uint16_t qclass, time_t now, uint16_t searchtype, int stripfront, 209 int noexpiredabove, uint8_t* expiretop, size_t expiretoplen) 210 { 211 struct ub_packed_rrset_key *rrset; 212 uint8_t lablen; 213 214 if(stripfront) { 215 /* strip off so that DNAMEs have strict subdomain match */ 216 lablen = *qname; 217 qname += lablen + 1; 218 qnamelen -= lablen + 1; 219 } 220 221 /* snip off front part of qname until the type is found */ 222 while(qnamelen > 0) { 223 if((rrset = rrset_cache_lookup(env->rrset_cache, qname, 224 qnamelen, searchtype, qclass, 0, now, 0))) { 225 uint8_t* origqname = qname; 226 size_t origqnamelen = qnamelen; 227 if(!noexpiredabove) 228 return rrset; 229 /* if expiretop set, do not look above it, but 230 * qname is equal, so the just found result is also 231 * the nonexpired above part. */ 232 if(expiretop && qnamelen == expiretoplen && 233 query_dname_compare(qname, expiretop)==0) 234 return rrset; 235 /* check for expiry, but we have to let go of the rrset 236 * for the lock ordering */ 237 lock_rw_unlock(&rrset->entry.lock); 238 /* the rrset_cache_expired_above function always takes 239 * off one label (if qnamelen>0) and returns the final 240 * qname where it searched, so we can continue from 241 * there turning the O N*N search into O N. */ 242 if(!rrset_cache_expired_above(env->rrset_cache, &qname, 243 &qnamelen, searchtype, qclass, now, expiretop, 244 expiretoplen)) { 245 /* we want to return rrset, but it may be 246 * gone from cache, if so, just loop like 247 * it was not in the cache in the first place. 248 */ 249 if((rrset = rrset_cache_lookup(env-> 250 rrset_cache, origqname, origqnamelen, 251 searchtype, qclass, 0, now, 0))) { 252 return rrset; 253 } 254 } 255 log_nametypeclass(VERB_ALGO, "ignoring rrset because expired rrsets exist above it", origqname, searchtype, qclass); 256 continue; 257 } 258 259 /* snip off front label */ 260 lablen = *qname; 261 qname += lablen + 1; 262 qnamelen -= lablen + 1; 263 } 264 return NULL; 265 } 266 267 /** add addr to additional section */ 268 static void 269 addr_to_additional(struct ub_packed_rrset_key* rrset, struct regional* region, 270 struct dns_msg* msg, time_t now) 271 { 272 if((msg->rep->rrsets[msg->rep->rrset_count] = 273 packed_rrset_copy_region(rrset, region, now))) { 274 msg->rep->ar_numrrsets++; 275 msg->rep->rrset_count++; 276 } 277 } 278 279 /** lookup message in message cache */ 280 struct msgreply_entry* 281 msg_cache_lookup(struct module_env* env, uint8_t* qname, size_t qnamelen, 282 uint16_t qtype, uint16_t qclass, uint16_t flags, time_t now, int wr) 283 { 284 struct lruhash_entry* e; 285 struct query_info k; 286 hashvalue_type h; 287 288 k.qname = qname; 289 k.qname_len = qnamelen; 290 k.qtype = qtype; 291 k.qclass = qclass; 292 k.local_alias = NULL; 293 h = query_info_hash(&k, flags); 294 e = slabhash_lookup(env->msg_cache, h, &k, wr); 295 296 if(!e) return NULL; 297 if( now > ((struct reply_info*)e->data)->ttl ) { 298 lock_rw_unlock(&e->lock); 299 return NULL; 300 } 301 return (struct msgreply_entry*)e->key; 302 } 303 304 /** find and add A and AAAA records for nameservers in delegpt */ 305 static int 306 find_add_addrs(struct module_env* env, uint16_t qclass, 307 struct regional* region, struct delegpt* dp, time_t now, 308 struct dns_msg** msg) 309 { 310 struct delegpt_ns* ns; 311 struct msgreply_entry* neg; 312 struct ub_packed_rrset_key* akey; 313 for(ns = dp->nslist; ns; ns = ns->next) { 314 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 315 ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0); 316 if(akey) { 317 if(!delegpt_add_rrset_A(dp, region, akey, 0, NULL)) { 318 lock_rw_unlock(&akey->entry.lock); 319 return 0; 320 } 321 if(msg) 322 addr_to_additional(akey, region, *msg, now); 323 lock_rw_unlock(&akey->entry.lock); 324 } else { 325 /* BIT_CD on false because delegpt lookup does 326 * not use dns64 translation */ 327 neg = msg_cache_lookup(env, ns->name, ns->namelen, 328 LDNS_RR_TYPE_A, qclass, 0, now, 0); 329 if(neg) { 330 delegpt_add_neg_msg(dp, neg); 331 lock_rw_unlock(&neg->entry.lock); 332 } 333 } 334 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 335 ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0); 336 if(akey) { 337 if(!delegpt_add_rrset_AAAA(dp, region, akey, 0, NULL)) { 338 lock_rw_unlock(&akey->entry.lock); 339 return 0; 340 } 341 if(msg) 342 addr_to_additional(akey, region, *msg, now); 343 lock_rw_unlock(&akey->entry.lock); 344 } else { 345 /* BIT_CD on false because delegpt lookup does 346 * not use dns64 translation */ 347 neg = msg_cache_lookup(env, ns->name, ns->namelen, 348 LDNS_RR_TYPE_AAAA, qclass, 0, now, 0); 349 /* Because recursion for lookup uses BIT_CD, check 350 * for that so it stops the recursion lookup, if a 351 * negative answer is cached. Because the cache uses 352 * the CD flag for type AAAA. */ 353 if(!neg) 354 neg = msg_cache_lookup(env, ns->name, ns->namelen, 355 LDNS_RR_TYPE_AAAA, qclass, BIT_CD, now, 0); 356 if(neg) { 357 delegpt_add_neg_msg(dp, neg); 358 lock_rw_unlock(&neg->entry.lock); 359 } 360 } 361 } 362 return 1; 363 } 364 365 /** find and add A and AAAA records for missing nameservers in delegpt */ 366 int 367 cache_fill_missing(struct module_env* env, uint16_t qclass, 368 struct regional* region, struct delegpt* dp) 369 { 370 struct delegpt_ns* ns; 371 struct msgreply_entry* neg; 372 struct ub_packed_rrset_key* akey; 373 time_t now = *env->now; 374 for(ns = dp->nslist; ns; ns = ns->next) { 375 if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX) 376 continue; 377 ns->cache_lookup_count++; 378 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 379 ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0); 380 if(akey) { 381 if(!delegpt_add_rrset_A(dp, region, akey, ns->lame, 382 NULL)) { 383 lock_rw_unlock(&akey->entry.lock); 384 return 0; 385 } 386 log_nametypeclass(VERB_ALGO, "found in cache", 387 ns->name, LDNS_RR_TYPE_A, qclass); 388 lock_rw_unlock(&akey->entry.lock); 389 } else { 390 /* BIT_CD on false because delegpt lookup does 391 * not use dns64 translation */ 392 neg = msg_cache_lookup(env, ns->name, ns->namelen, 393 LDNS_RR_TYPE_A, qclass, 0, now, 0); 394 if(neg) { 395 delegpt_add_neg_msg(dp, neg); 396 lock_rw_unlock(&neg->entry.lock); 397 } 398 } 399 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 400 ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0); 401 if(akey) { 402 if(!delegpt_add_rrset_AAAA(dp, region, akey, ns->lame, 403 NULL)) { 404 lock_rw_unlock(&akey->entry.lock); 405 return 0; 406 } 407 log_nametypeclass(VERB_ALGO, "found in cache", 408 ns->name, LDNS_RR_TYPE_AAAA, qclass); 409 lock_rw_unlock(&akey->entry.lock); 410 } else { 411 /* BIT_CD on false because delegpt lookup does 412 * not use dns64 translation */ 413 neg = msg_cache_lookup(env, ns->name, ns->namelen, 414 LDNS_RR_TYPE_AAAA, qclass, 0, now, 0); 415 /* Because recursion for lookup uses BIT_CD, check 416 * for that so it stops the recursion lookup, if a 417 * negative answer is cached. Because the cache uses 418 * the CD flag for type AAAA. */ 419 if(!neg) 420 neg = msg_cache_lookup(env, ns->name, ns->namelen, 421 LDNS_RR_TYPE_AAAA, qclass, BIT_CD, now, 0); 422 if(neg) { 423 delegpt_add_neg_msg(dp, neg); 424 lock_rw_unlock(&neg->entry.lock); 425 } 426 } 427 } 428 return 1; 429 } 430 431 /** find and add DS or NSEC to delegation msg */ 432 static void 433 find_add_ds(struct module_env* env, struct regional* region, 434 struct dns_msg* msg, struct delegpt* dp, time_t now) 435 { 436 /* Lookup the DS or NSEC at the delegation point. */ 437 struct ub_packed_rrset_key* rrset = rrset_cache_lookup( 438 env->rrset_cache, dp->name, dp->namelen, LDNS_RR_TYPE_DS, 439 msg->qinfo.qclass, 0, now, 0); 440 if(!rrset) { 441 /* NOTE: this won't work for alternate NSEC schemes 442 * (opt-in, NSEC3) */ 443 rrset = rrset_cache_lookup(env->rrset_cache, dp->name, 444 dp->namelen, LDNS_RR_TYPE_NSEC, msg->qinfo.qclass, 445 0, now, 0); 446 /* Note: the PACKED_RRSET_NSEC_AT_APEX flag is not used. 447 * since this is a referral, we need the NSEC at the parent 448 * side of the zone cut, not the NSEC at apex side. */ 449 if(rrset && nsec_has_type(rrset, LDNS_RR_TYPE_DS)) { 450 lock_rw_unlock(&rrset->entry.lock); 451 rrset = NULL; /* discard wrong NSEC */ 452 } 453 } 454 if(rrset) { 455 /* add it to auth section. This is the second rrset. */ 456 if((msg->rep->rrsets[msg->rep->rrset_count] = 457 packed_rrset_copy_region(rrset, region, now))) { 458 msg->rep->ns_numrrsets++; 459 msg->rep->rrset_count++; 460 } 461 lock_rw_unlock(&rrset->entry.lock); 462 } 463 } 464 465 struct dns_msg* 466 dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 467 uint16_t qclass, struct regional* region, size_t capacity) 468 { 469 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region, 470 sizeof(struct dns_msg)); 471 if(!msg) 472 return NULL; 473 msg->qinfo.qname = regional_alloc_init(region, qname, qnamelen); 474 if(!msg->qinfo.qname) 475 return NULL; 476 msg->qinfo.qname_len = qnamelen; 477 msg->qinfo.qtype = qtype; 478 msg->qinfo.qclass = qclass; 479 msg->qinfo.local_alias = NULL; 480 /* non-packed reply_info, because it needs to grow the array */ 481 msg->rep = (struct reply_info*)regional_alloc_zero(region, 482 sizeof(struct reply_info)-sizeof(struct rrset_ref)); 483 if(!msg->rep) 484 return NULL; 485 if(capacity > RR_COUNT_MAX) 486 return NULL; /* integer overflow protection */ 487 msg->rep->flags = BIT_QR; /* with QR, no AA */ 488 msg->rep->qdcount = 1; 489 msg->rep->reason_bogus = LDNS_EDE_NONE; 490 msg->rep->rrsets = (struct ub_packed_rrset_key**) 491 regional_alloc(region, 492 capacity*sizeof(struct ub_packed_rrset_key*)); 493 if(!msg->rep->rrsets) 494 return NULL; 495 return msg; 496 } 497 498 int 499 dns_msg_authadd(struct dns_msg* msg, struct regional* region, 500 struct ub_packed_rrset_key* rrset, time_t now) 501 { 502 if(!(msg->rep->rrsets[msg->rep->rrset_count++] = 503 packed_rrset_copy_region(rrset, region, now))) 504 return 0; 505 msg->rep->ns_numrrsets++; 506 return 1; 507 } 508 509 int 510 dns_msg_ansadd(struct dns_msg* msg, struct regional* region, 511 struct ub_packed_rrset_key* rrset, time_t now) 512 { 513 if(!(msg->rep->rrsets[msg->rep->rrset_count++] = 514 packed_rrset_copy_region(rrset, region, now))) 515 return 0; 516 msg->rep->an_numrrsets++; 517 return 1; 518 } 519 520 struct delegpt* 521 dns_cache_find_delegation(struct module_env* env, uint8_t* qname, 522 size_t qnamelen, uint16_t qtype, uint16_t qclass, 523 struct regional* region, struct dns_msg** msg, time_t now, 524 int noexpiredabove, uint8_t* expiretop, size_t expiretoplen) 525 { 526 /* try to find closest NS rrset */ 527 struct ub_packed_rrset_key* nskey; 528 struct packed_rrset_data* nsdata; 529 struct delegpt* dp; 530 531 nskey = find_closest_of_type(env, qname, qnamelen, qclass, now, 532 LDNS_RR_TYPE_NS, 0, noexpiredabove, expiretop, expiretoplen); 533 if(!nskey) /* hope the caller has hints to prime or something */ 534 return NULL; 535 nsdata = (struct packed_rrset_data*)nskey->entry.data; 536 /* got the NS key, create delegation point */ 537 dp = delegpt_create(region); 538 if(!dp || !delegpt_set_name(dp, region, nskey->rk.dname)) { 539 lock_rw_unlock(&nskey->entry.lock); 540 log_err("find_delegation: out of memory"); 541 return NULL; 542 } 543 /* create referral message */ 544 if(msg) { 545 /* allocate the array to as much as we could need: 546 * NS rrset + DS/NSEC rrset + 547 * A rrset for every NS RR 548 * AAAA rrset for every NS RR 549 */ 550 *msg = dns_msg_create(qname, qnamelen, qtype, qclass, region, 551 2 + nsdata->count*2); 552 if(!*msg || !dns_msg_authadd(*msg, region, nskey, now)) { 553 lock_rw_unlock(&nskey->entry.lock); 554 log_err("find_delegation: out of memory"); 555 return NULL; 556 } 557 } 558 if(!delegpt_rrset_add_ns(dp, region, nskey, 0)) 559 log_err("find_delegation: addns out of memory"); 560 lock_rw_unlock(&nskey->entry.lock); /* first unlock before next lookup*/ 561 /* find and add DS/NSEC (if any) */ 562 if(msg) 563 find_add_ds(env, region, *msg, dp, now); 564 /* find and add A entries */ 565 if(!find_add_addrs(env, qclass, region, dp, now, msg)) 566 log_err("find_delegation: addrs out of memory"); 567 return dp; 568 } 569 570 /** allocate dns_msg from query_info and reply_info */ 571 static struct dns_msg* 572 gen_dns_msg(struct regional* region, struct query_info* q, size_t num) 573 { 574 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region, 575 sizeof(struct dns_msg)); 576 if(!msg) 577 return NULL; 578 memcpy(&msg->qinfo, q, sizeof(struct query_info)); 579 msg->qinfo.qname = regional_alloc_init(region, q->qname, q->qname_len); 580 if(!msg->qinfo.qname) 581 return NULL; 582 /* allocate replyinfo struct and rrset key array separately */ 583 msg->rep = (struct reply_info*)regional_alloc(region, 584 sizeof(struct reply_info) - sizeof(struct rrset_ref)); 585 if(!msg->rep) 586 return NULL; 587 msg->rep->reason_bogus = LDNS_EDE_NONE; 588 msg->rep->reason_bogus_str = NULL; 589 if(num > RR_COUNT_MAX) 590 return NULL; /* integer overflow protection */ 591 msg->rep->rrsets = (struct ub_packed_rrset_key**) 592 regional_alloc(region, 593 num * sizeof(struct ub_packed_rrset_key*)); 594 if(!msg->rep->rrsets) 595 return NULL; 596 return msg; 597 } 598 599 struct dns_msg* 600 tomsg(struct module_env* env, struct query_info* q, struct reply_info* r, 601 struct regional* region, time_t now, int allow_expired, 602 struct regional* scratch) 603 { 604 struct dns_msg* msg; 605 size_t i; 606 int is_expired = 0; 607 time_t now_control = now; 608 if(now > r->ttl) { 609 /* Check if we are allowed to serve expired */ 610 if(allow_expired) { 611 if(env->cfg->serve_expired_ttl && 612 r->serve_expired_ttl < now) { 613 return NULL; 614 } 615 /* Ignore expired failure answers */ 616 if(FLAGS_GET_RCODE(r->flags) != 617 LDNS_RCODE_NOERROR && 618 FLAGS_GET_RCODE(r->flags) != 619 LDNS_RCODE_NXDOMAIN && 620 FLAGS_GET_RCODE(r->flags) != 621 LDNS_RCODE_YXDOMAIN) 622 return 0; 623 } else { 624 return NULL; 625 } 626 /* Change the current time so we can pass the below TTL checks when 627 * serving expired data. */ 628 now_control = r->ttl - env->cfg->serve_expired_reply_ttl; 629 is_expired = 1; 630 } 631 632 msg = gen_dns_msg(region, q, r->rrset_count); 633 if(!msg) return NULL; 634 msg->rep->flags = r->flags; 635 msg->rep->qdcount = r->qdcount; 636 msg->rep->ttl = is_expired 637 ?SERVE_EXPIRED_REPLY_TTL 638 :r->ttl - now; 639 if(r->prefetch_ttl > now) 640 msg->rep->prefetch_ttl = r->prefetch_ttl - now; 641 else 642 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl); 643 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL; 644 msg->rep->security = r->security; 645 msg->rep->an_numrrsets = r->an_numrrsets; 646 msg->rep->ns_numrrsets = r->ns_numrrsets; 647 msg->rep->ar_numrrsets = r->ar_numrrsets; 648 msg->rep->rrset_count = r->rrset_count; 649 msg->rep->authoritative = r->authoritative; 650 msg->rep->reason_bogus = r->reason_bogus; 651 if(r->reason_bogus_str) { 652 msg->rep->reason_bogus_str = regional_strdup(region, r->reason_bogus_str); 653 } 654 655 if(!rrset_array_lock(r->ref, r->rrset_count, now_control)) { 656 return NULL; 657 } 658 if(r->an_numrrsets > 0 && (r->rrsets[0]->rk.type == htons( 659 LDNS_RR_TYPE_CNAME) || r->rrsets[0]->rk.type == htons( 660 LDNS_RR_TYPE_DNAME)) && !reply_check_cname_chain(q, r)) { 661 /* cname chain is now invalid, reconstruct msg */ 662 rrset_array_unlock(r->ref, r->rrset_count); 663 return NULL; 664 } 665 if(r->security == sec_status_secure && !reply_all_rrsets_secure(r)) { 666 /* message rrsets have changed status, revalidate */ 667 rrset_array_unlock(r->ref, r->rrset_count); 668 return NULL; 669 } 670 for(i=0; i<msg->rep->rrset_count; i++) { 671 msg->rep->rrsets[i] = packed_rrset_copy_region(r->rrsets[i], 672 region, now); 673 if(!msg->rep->rrsets[i]) { 674 rrset_array_unlock(r->ref, r->rrset_count); 675 return NULL; 676 } 677 } 678 if(env) 679 rrset_array_unlock_touch(env->rrset_cache, scratch, r->ref, 680 r->rrset_count); 681 else 682 rrset_array_unlock(r->ref, r->rrset_count); 683 return msg; 684 } 685 686 struct dns_msg* 687 dns_msg_deepcopy_region(struct dns_msg* origin, struct regional* region) 688 { 689 size_t i; 690 struct dns_msg* res = NULL; 691 res = gen_dns_msg(region, &origin->qinfo, origin->rep->rrset_count); 692 if(!res) return NULL; 693 *res->rep = *origin->rep; 694 if(origin->rep->reason_bogus_str) { 695 res->rep->reason_bogus_str = regional_strdup(region, 696 origin->rep->reason_bogus_str); 697 } 698 for(i=0; i<res->rep->rrset_count; i++) { 699 res->rep->rrsets[i] = packed_rrset_copy_region( 700 origin->rep->rrsets[i], region, 0); 701 if(!res->rep->rrsets[i]) { 702 return NULL; 703 } 704 } 705 return res; 706 } 707 708 /** synthesize RRset-only response from cached RRset item */ 709 static struct dns_msg* 710 rrset_msg(struct ub_packed_rrset_key* rrset, struct regional* region, 711 time_t now, struct query_info* q) 712 { 713 struct dns_msg* msg; 714 struct packed_rrset_data* d = (struct packed_rrset_data*) 715 rrset->entry.data; 716 if(now > d->ttl) 717 return NULL; 718 msg = gen_dns_msg(region, q, 1); /* only the CNAME (or other) RRset */ 719 if(!msg) 720 return NULL; 721 msg->rep->flags = BIT_QR; /* reply, no AA, no error */ 722 msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */ 723 msg->rep->qdcount = 1; 724 msg->rep->ttl = d->ttl - now; 725 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl); 726 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL; 727 msg->rep->security = sec_status_unchecked; 728 msg->rep->an_numrrsets = 1; 729 msg->rep->ns_numrrsets = 0; 730 msg->rep->ar_numrrsets = 0; 731 msg->rep->rrset_count = 1; 732 msg->rep->reason_bogus = LDNS_EDE_NONE; 733 msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now); 734 if(!msg->rep->rrsets[0]) /* copy CNAME */ 735 return NULL; 736 return msg; 737 } 738 739 /** synthesize DNAME+CNAME response from cached DNAME item */ 740 static struct dns_msg* 741 synth_dname_msg(struct ub_packed_rrset_key* rrset, struct regional* region, 742 time_t now, struct query_info* q, enum sec_status* sec_status) 743 { 744 struct dns_msg* msg; 745 struct ub_packed_rrset_key* ck; 746 struct packed_rrset_data* newd, *d = (struct packed_rrset_data*) 747 rrset->entry.data; 748 uint8_t* newname, *dtarg = NULL; 749 size_t newlen, dtarglen; 750 if(now > d->ttl) 751 return NULL; 752 /* only allow validated (with DNSSEC) DNAMEs used from cache 753 * for insecure DNAMEs, query again. */ 754 *sec_status = d->security; 755 /* return sec status, so the status of the CNAME can be checked 756 * by the calling routine. */ 757 msg = gen_dns_msg(region, q, 2); /* DNAME + CNAME RRset */ 758 if(!msg) 759 return NULL; 760 msg->rep->flags = BIT_QR; /* reply, no AA, no error */ 761 msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */ 762 msg->rep->qdcount = 1; 763 msg->rep->ttl = d->ttl - now; 764 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl); 765 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL; 766 msg->rep->security = sec_status_unchecked; 767 msg->rep->an_numrrsets = 1; 768 msg->rep->ns_numrrsets = 0; 769 msg->rep->ar_numrrsets = 0; 770 msg->rep->rrset_count = 1; 771 msg->rep->reason_bogus = LDNS_EDE_NONE; 772 msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now); 773 if(!msg->rep->rrsets[0]) /* copy DNAME */ 774 return NULL; 775 /* synth CNAME rrset */ 776 get_cname_target(rrset, &dtarg, &dtarglen); 777 if(!dtarg) 778 return NULL; 779 newlen = q->qname_len + dtarglen - rrset->rk.dname_len; 780 if(newlen > LDNS_MAX_DOMAINLEN) { 781 msg->rep->flags |= LDNS_RCODE_YXDOMAIN; 782 return msg; 783 } 784 newname = (uint8_t*)regional_alloc(region, newlen); 785 if(!newname) 786 return NULL; 787 /* new name is concatenation of qname front (without DNAME owner) 788 * and DNAME target name */ 789 memcpy(newname, q->qname, q->qname_len-rrset->rk.dname_len); 790 memmove(newname+(q->qname_len-rrset->rk.dname_len), dtarg, dtarglen); 791 /* create rest of CNAME rrset */ 792 ck = (struct ub_packed_rrset_key*)regional_alloc(region, 793 sizeof(struct ub_packed_rrset_key)); 794 if(!ck) 795 return NULL; 796 memset(&ck->entry, 0, sizeof(ck->entry)); 797 msg->rep->rrsets[1] = ck; 798 ck->entry.key = ck; 799 ck->rk.type = htons(LDNS_RR_TYPE_CNAME); 800 ck->rk.rrset_class = rrset->rk.rrset_class; 801 ck->rk.flags = 0; 802 ck->rk.dname = regional_alloc_init(region, q->qname, q->qname_len); 803 if(!ck->rk.dname) 804 return NULL; 805 ck->rk.dname_len = q->qname_len; 806 ck->entry.hash = rrset_key_hash(&ck->rk); 807 newd = (struct packed_rrset_data*)regional_alloc_zero(region, 808 sizeof(struct packed_rrset_data) + sizeof(size_t) + 809 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t) 810 + newlen); 811 if(!newd) 812 return NULL; 813 ck->entry.data = newd; 814 newd->ttl = d->ttl - now; /* RFC6672: synth CNAME TTL == DNAME TTL */ 815 newd->count = 1; 816 newd->rrsig_count = 0; 817 newd->trust = rrset_trust_ans_noAA; 818 newd->rr_len = (size_t*)((uint8_t*)newd + 819 sizeof(struct packed_rrset_data)); 820 newd->rr_len[0] = newlen + sizeof(uint16_t); 821 packed_rrset_ptr_fixup(newd); 822 newd->rr_ttl[0] = newd->ttl; 823 msg->rep->ttl = newd->ttl; 824 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(newd->ttl); 825 msg->rep->serve_expired_ttl = newd->ttl + SERVE_EXPIRED_TTL; 826 sldns_write_uint16(newd->rr_data[0], newlen); 827 memmove(newd->rr_data[0] + sizeof(uint16_t), newname, newlen); 828 msg->rep->an_numrrsets ++; 829 msg->rep->rrset_count ++; 830 return msg; 831 } 832 833 /** Fill TYPE_ANY response with some data from cache */ 834 static struct dns_msg* 835 fill_any(struct module_env* env, 836 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 837 struct regional* region) 838 { 839 time_t now = *env->now; 840 struct dns_msg* msg = NULL; 841 uint16_t lookup[] = {LDNS_RR_TYPE_A, LDNS_RR_TYPE_AAAA, 842 LDNS_RR_TYPE_MX, LDNS_RR_TYPE_SOA, LDNS_RR_TYPE_NS, 843 LDNS_RR_TYPE_DNAME, 0}; 844 int i, num=6; /* number of RR types to look up */ 845 log_assert(lookup[num] == 0); 846 847 if(env->cfg->deny_any) { 848 /* return empty message */ 849 msg = dns_msg_create(qname, qnamelen, qtype, qclass, 850 region, 0); 851 if(!msg) { 852 return NULL; 853 } 854 /* set NOTIMPL for RFC 8482 */ 855 msg->rep->flags |= LDNS_RCODE_NOTIMPL; 856 msg->rep->security = sec_status_indeterminate; 857 return msg; 858 } 859 860 for(i=0; i<num; i++) { 861 /* look up this RR for inclusion in type ANY response */ 862 struct ub_packed_rrset_key* rrset = rrset_cache_lookup( 863 env->rrset_cache, qname, qnamelen, lookup[i], 864 qclass, 0, now, 0); 865 struct packed_rrset_data *d; 866 if(!rrset) 867 continue; 868 869 /* only if rrset from answer section */ 870 d = (struct packed_rrset_data*)rrset->entry.data; 871 if(d->trust == rrset_trust_add_noAA || 872 d->trust == rrset_trust_auth_noAA || 873 d->trust == rrset_trust_add_AA || 874 d->trust == rrset_trust_auth_AA) { 875 lock_rw_unlock(&rrset->entry.lock); 876 continue; 877 } 878 879 /* create msg if none */ 880 if(!msg) { 881 msg = dns_msg_create(qname, qnamelen, qtype, qclass, 882 region, (size_t)(num-i)); 883 if(!msg) { 884 lock_rw_unlock(&rrset->entry.lock); 885 return NULL; 886 } 887 } 888 889 /* add RRset to response */ 890 if(!dns_msg_ansadd(msg, region, rrset, now)) { 891 lock_rw_unlock(&rrset->entry.lock); 892 return NULL; 893 } 894 lock_rw_unlock(&rrset->entry.lock); 895 } 896 return msg; 897 } 898 899 struct dns_msg* 900 dns_cache_lookup(struct module_env* env, 901 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 902 uint16_t flags, struct regional* region, struct regional* scratch, 903 int no_partial, uint8_t* dpname, size_t dpnamelen) 904 { 905 struct lruhash_entry* e; 906 struct query_info k; 907 hashvalue_type h; 908 time_t now = *env->now; 909 struct ub_packed_rrset_key* rrset; 910 911 /* lookup first, this has both NXdomains and ANSWER responses */ 912 k.qname = qname; 913 k.qname_len = qnamelen; 914 k.qtype = qtype; 915 k.qclass = qclass; 916 k.local_alias = NULL; 917 h = query_info_hash(&k, flags); 918 e = slabhash_lookup(env->msg_cache, h, &k, 0); 919 if(e) { 920 struct msgreply_entry* key = (struct msgreply_entry*)e->key; 921 struct reply_info* data = (struct reply_info*)e->data; 922 struct dns_msg* msg = tomsg(env, &key->key, data, region, now, 0, 923 scratch); 924 if(msg) { 925 lock_rw_unlock(&e->lock); 926 return msg; 927 } 928 /* could be msg==NULL; due to TTL or not all rrsets available */ 929 lock_rw_unlock(&e->lock); 930 } 931 932 /* see if a DNAME exists. Checked for first, to enforce that DNAMEs 933 * are more important, the CNAME is resynthesized and thus 934 * consistent with the DNAME */ 935 if(!no_partial && 936 (rrset=find_closest_of_type(env, qname, qnamelen, qclass, now, 937 LDNS_RR_TYPE_DNAME, 1, 0, NULL, 0))) { 938 /* synthesize a DNAME+CNAME message based on this */ 939 enum sec_status sec_status = sec_status_unchecked; 940 struct dns_msg* msg = synth_dname_msg(rrset, region, now, &k, 941 &sec_status); 942 if(msg) { 943 struct ub_packed_rrset_key* cname_rrset; 944 lock_rw_unlock(&rrset->entry.lock); 945 /* now, after unlocking the DNAME rrset lock, 946 * check the sec_status, and see if we need to look 947 * up the CNAME record associated before it can 948 * be used */ 949 /* normally, only secure DNAMEs allowed from cache*/ 950 if(sec_status == sec_status_secure) 951 return msg; 952 /* but if we have a CNAME cached with this name, then we 953 * have previously already allowed this name to pass. 954 * the next cache lookup is going to fetch that CNAME itself, 955 * but it is better to have the (unsigned)DNAME + CNAME in 956 * that case */ 957 cname_rrset = rrset_cache_lookup( 958 env->rrset_cache, qname, qnamelen, 959 LDNS_RR_TYPE_CNAME, qclass, 0, now, 0); 960 if(cname_rrset) { 961 /* CNAME already synthesized by 962 * synth_dname_msg routine, so we can 963 * straight up return the msg */ 964 lock_rw_unlock(&cname_rrset->entry.lock); 965 return msg; 966 } 967 } else { 968 lock_rw_unlock(&rrset->entry.lock); 969 } 970 } 971 972 /* see if we have CNAME for this domain, 973 * but not for DS records (which are part of the parent) */ 974 if(!no_partial && qtype != LDNS_RR_TYPE_DS && 975 (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen, 976 LDNS_RR_TYPE_CNAME, qclass, 0, now, 0))) { 977 uint8_t* wc = NULL; 978 size_t wl; 979 /* if the rrset is not a wildcard expansion, with wcname */ 980 /* because, if we return that CNAME rrset on its own, it is 981 * missing the NSEC or NSEC3 proof */ 982 if(!(val_rrset_wildcard(rrset, &wc, &wl) && wc != NULL)) { 983 struct dns_msg* msg = rrset_msg(rrset, region, now, &k); 984 if(msg) { 985 lock_rw_unlock(&rrset->entry.lock); 986 return msg; 987 } 988 } 989 lock_rw_unlock(&rrset->entry.lock); 990 } 991 992 /* construct DS, DNSKEY messages from rrset cache. */ 993 if((qtype == LDNS_RR_TYPE_DS || qtype == LDNS_RR_TYPE_DNSKEY) && 994 (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen, 995 qtype, qclass, 0, now, 0))) { 996 /* if the rrset is from the additional section, and the 997 * signatures have fallen off, then do not synthesize a msg 998 * instead, allow a full query for signed results to happen. 999 * Forego all rrset data from additional section, because 1000 * some signatures may not be present and cause validation 1001 * failure. 1002 */ 1003 struct packed_rrset_data *d = (struct packed_rrset_data*) 1004 rrset->entry.data; 1005 if(d->trust != rrset_trust_add_noAA && 1006 d->trust != rrset_trust_add_AA && 1007 (qtype == LDNS_RR_TYPE_DS || 1008 (d->trust != rrset_trust_auth_noAA 1009 && d->trust != rrset_trust_auth_AA) )) { 1010 struct dns_msg* msg = rrset_msg(rrset, region, now, &k); 1011 if(msg) { 1012 lock_rw_unlock(&rrset->entry.lock); 1013 return msg; 1014 } 1015 } 1016 lock_rw_unlock(&rrset->entry.lock); 1017 } 1018 1019 /* stop downwards cache search on NXDOMAIN. 1020 * Empty nonterminals are NOERROR, so an NXDOMAIN for foo 1021 * means bla.foo also does not exist. The DNSSEC proofs are 1022 * the same. We search upwards for NXDOMAINs. */ 1023 if(env->cfg->harden_below_nxdomain) { 1024 while(!dname_is_root(k.qname)) { 1025 if(dpname && dpnamelen 1026 && !dname_subdomain_c(k.qname, dpname)) 1027 break; /* no synth nxdomain above the stub */ 1028 dname_remove_label(&k.qname, &k.qname_len); 1029 h = query_info_hash(&k, flags); 1030 e = slabhash_lookup(env->msg_cache, h, &k, 0); 1031 if(!e && k.qtype != LDNS_RR_TYPE_A && 1032 env->cfg->qname_minimisation) { 1033 k.qtype = LDNS_RR_TYPE_A; 1034 h = query_info_hash(&k, flags); 1035 e = slabhash_lookup(env->msg_cache, h, &k, 0); 1036 } 1037 if(e) { 1038 struct reply_info* data = (struct reply_info*)e->data; 1039 struct dns_msg* msg; 1040 if(FLAGS_GET_RCODE(data->flags) == LDNS_RCODE_NXDOMAIN 1041 && data->security == sec_status_secure 1042 && (data->an_numrrsets == 0 || 1043 ntohs(data->rrsets[0]->rk.type) != LDNS_RR_TYPE_CNAME) 1044 && (msg=tomsg(env, &k, data, region, now, 0, scratch))) { 1045 lock_rw_unlock(&e->lock); 1046 msg->qinfo.qname=qname; 1047 msg->qinfo.qname_len=qnamelen; 1048 /* check that DNSSEC really works out */ 1049 msg->rep->security = sec_status_unchecked; 1050 iter_scrub_nxdomain(msg); 1051 return msg; 1052 } 1053 lock_rw_unlock(&e->lock); 1054 } 1055 k.qtype = qtype; 1056 } 1057 } 1058 1059 /* fill common RR types for ANY response to avoid requery */ 1060 if(qtype == LDNS_RR_TYPE_ANY) { 1061 return fill_any(env, qname, qnamelen, qtype, qclass, region); 1062 } 1063 1064 return NULL; 1065 } 1066 1067 int 1068 dns_cache_store(struct module_env* env, struct query_info* msgqinf, 1069 struct reply_info* msgrep, int is_referral, time_t leeway, int pside, 1070 struct regional* region, uint32_t flags, time_t qstarttime) 1071 { 1072 struct reply_info* rep = NULL; 1073 /* alloc, malloc properly (not in region, like msg is) */ 1074 rep = reply_info_copy(msgrep, env->alloc, NULL); 1075 if(!rep) 1076 return 0; 1077 /* ttl must be relative ;i.e. 0..86400 not time(0)+86400. 1078 * the env->now is added to message and RRsets in this routine. */ 1079 /* the leeway is used to invalidate other rrsets earlier */ 1080 if(is_referral) { 1081 /* store rrsets */ 1082 struct rrset_ref ref; 1083 size_t i; 1084 for(i=0; i<rep->rrset_count; i++) { 1085 packed_rrset_ttl_add((struct packed_rrset_data*) 1086 rep->rrsets[i]->entry.data, *env->now); 1087 ref.key = rep->rrsets[i]; 1088 ref.id = rep->rrsets[i]->id; 1089 /*ignore ret: it was in the cache, ref updated */ 1090 /* no leeway for typeNS */ 1091 (void)rrset_cache_update(env->rrset_cache, &ref, 1092 env->alloc, 1093 ((ntohs(ref.key->rk.type)==LDNS_RR_TYPE_NS 1094 && !pside) ? qstarttime:*env->now + leeway)); 1095 } 1096 reply_info_delete(rep, NULL); 1097 return 1; 1098 } else { 1099 /* store msg, and rrsets */ 1100 struct query_info qinf; 1101 hashvalue_type h; 1102 1103 qinf = *msgqinf; 1104 qinf.qname = memdup(msgqinf->qname, msgqinf->qname_len); 1105 if(!qinf.qname) { 1106 reply_info_parsedelete(rep, env->alloc); 1107 return 0; 1108 } 1109 /* fixup flags to be sensible for a reply based on the cache */ 1110 /* this module means that RA is available. It is an answer QR. 1111 * Not AA from cache. Not CD in cache (depends on client bit). */ 1112 rep->flags |= (BIT_RA | BIT_QR); 1113 rep->flags &= ~(BIT_AA | BIT_CD); 1114 h = query_info_hash(&qinf, (uint16_t)flags); 1115 dns_cache_store_msg(env, &qinf, h, rep, leeway, pside, msgrep, 1116 flags, region, qstarttime); 1117 /* qname is used inside query_info_entrysetup, and set to 1118 * NULL. If it has not been used, free it. free(0) is safe. */ 1119 free(qinf.qname); 1120 } 1121 return 1; 1122 } 1123 1124 int 1125 dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo, 1126 time_t adjust, uint16_t flags) 1127 { 1128 struct msgreply_entry* msg; 1129 msg = msg_cache_lookup(env, qinfo->qname, qinfo->qname_len, 1130 qinfo->qtype, qinfo->qclass, flags, *env->now, 1); 1131 if(msg) { 1132 struct reply_info* rep = (struct reply_info*)msg->entry.data; 1133 if(rep) { 1134 rep->prefetch_ttl += adjust; 1135 lock_rw_unlock(&msg->entry.lock); 1136 return 1; 1137 } 1138 lock_rw_unlock(&msg->entry.lock); 1139 } 1140 return 0; 1141 } 1142