1 /* 2 * iterator/iter_delegpt.c - delegation point with NS and address information. 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 implements the Delegation Point. It contains a list of name servers 40 * and their addresses if known. 41 */ 42 #include "config.h" 43 #include "iterator/iter_delegpt.h" 44 #include "services/cache/dns.h" 45 #include "util/regional.h" 46 #include "util/data/dname.h" 47 #include "util/data/packed_rrset.h" 48 #include "util/data/msgreply.h" 49 #include "util/net_help.h" 50 #include "sldns/rrdef.h" 51 #include "sldns/sbuffer.h" 52 53 struct delegpt* 54 delegpt_create(struct regional* region) 55 { 56 struct delegpt* dp=(struct delegpt*)regional_alloc( 57 region, sizeof(*dp)); 58 if(!dp) 59 return NULL; 60 memset(dp, 0, sizeof(*dp)); 61 return dp; 62 } 63 64 struct delegpt* delegpt_copy(struct delegpt* dp, struct regional* region) 65 { 66 struct delegpt* copy = delegpt_create(region); 67 struct delegpt_ns* ns; 68 struct delegpt_addr* a; 69 if(!copy) 70 return NULL; 71 if(!delegpt_set_name(copy, region, dp->name)) 72 return NULL; 73 copy->bogus = dp->bogus; 74 copy->has_parent_side_NS = dp->has_parent_side_NS; 75 copy->ssl_upstream = dp->ssl_upstream; 76 copy->tcp_upstream = dp->tcp_upstream; 77 for(ns = dp->nslist; ns; ns = ns->next) { 78 if(!delegpt_add_ns(copy, region, ns->name, ns->lame, 79 ns->tls_auth_name, ns->port)) 80 return NULL; 81 copy->nslist->resolved = ns->resolved; 82 copy->nslist->got4 = ns->got4; 83 copy->nslist->got6 = ns->got6; 84 copy->nslist->done_pside4 = ns->done_pside4; 85 copy->nslist->done_pside6 = ns->done_pside6; 86 } 87 for(a = dp->target_list; a; a = a->next_target) { 88 if(!delegpt_add_addr(copy, region, &a->addr, a->addrlen, 89 a->bogus, a->lame, a->tls_auth_name, -1, NULL)) 90 return NULL; 91 } 92 return copy; 93 } 94 95 int 96 delegpt_set_name(struct delegpt* dp, struct regional* region, uint8_t* name) 97 { 98 log_assert(!dp->dp_type_mlc); 99 dp->namelabs = dname_count_size_labels(name, &dp->namelen); 100 dp->name = regional_alloc_init(region, name, dp->namelen); 101 return dp->name != 0; 102 } 103 104 int 105 delegpt_add_ns(struct delegpt* dp, struct regional* region, uint8_t* name, 106 uint8_t lame, char* tls_auth_name, int port) 107 { 108 struct delegpt_ns* ns; 109 size_t len; 110 (void)dname_count_size_labels(name, &len); 111 log_assert(!dp->dp_type_mlc); 112 /* slow check for duplicates to avoid counting failures when 113 * adding the same server as a dependency twice */ 114 if(delegpt_find_ns(dp, name, len)) 115 return 1; 116 ns = (struct delegpt_ns*)regional_alloc(region, 117 sizeof(struct delegpt_ns)); 118 if(!ns) 119 return 0; 120 ns->next = dp->nslist; 121 ns->namelen = len; 122 dp->nslist = ns; 123 ns->name = regional_alloc_init(region, name, ns->namelen); 124 ns->resolved = 0; 125 ns->got4 = 0; 126 ns->got6 = 0; 127 ns->lame = lame; 128 ns->done_pside4 = 0; 129 ns->done_pside6 = 0; 130 ns->port = port; 131 if(tls_auth_name) { 132 ns->tls_auth_name = regional_strdup(region, tls_auth_name); 133 if(!ns->tls_auth_name) 134 return 0; 135 } else { 136 ns->tls_auth_name = NULL; 137 } 138 return ns->name != 0; 139 } 140 141 struct delegpt_ns* 142 delegpt_find_ns(struct delegpt* dp, uint8_t* name, size_t namelen) 143 { 144 struct delegpt_ns* p = dp->nslist; 145 while(p) { 146 if(namelen == p->namelen && 147 query_dname_compare(name, p->name) == 0) { 148 return p; 149 } 150 p = p->next; 151 } 152 return NULL; 153 } 154 155 struct delegpt_addr* 156 delegpt_find_addr(struct delegpt* dp, struct sockaddr_storage* addr, 157 socklen_t addrlen) 158 { 159 struct delegpt_addr* p = dp->target_list; 160 while(p) { 161 if(sockaddr_cmp_addr(addr, addrlen, &p->addr, p->addrlen)==0 162 && ((struct sockaddr_in*)addr)->sin_port == 163 ((struct sockaddr_in*)&p->addr)->sin_port) { 164 return p; 165 } 166 p = p->next_target; 167 } 168 return NULL; 169 } 170 171 int 172 delegpt_add_target(struct delegpt* dp, struct regional* region, 173 uint8_t* name, size_t namelen, struct sockaddr_storage* addr, 174 socklen_t addrlen, uint8_t bogus, uint8_t lame, int* additions) 175 { 176 struct delegpt_ns* ns = delegpt_find_ns(dp, name, namelen); 177 log_assert(!dp->dp_type_mlc); 178 if(!ns) { 179 /* ignore it */ 180 return 1; 181 } 182 if(!lame) { 183 if(addr_is_ip6(addr, addrlen)) 184 ns->got6 = 1; 185 else ns->got4 = 1; 186 if(ns->got4 && ns->got6) 187 ns->resolved = 1; 188 } 189 log_assert(ns->port>0); 190 return delegpt_add_addr(dp, region, addr, addrlen, bogus, lame, 191 ns->tls_auth_name, ns->port, additions); 192 } 193 194 int 195 delegpt_add_addr(struct delegpt* dp, struct regional* region, 196 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus, 197 uint8_t lame, char* tls_auth_name, int port, int* additions) 198 { 199 struct delegpt_addr* a; 200 log_assert(!dp->dp_type_mlc); 201 if(port != -1) { 202 log_assert(port>0); 203 sockaddr_store_port(addr, addrlen, port); 204 } 205 /* check for duplicates */ 206 if((a = delegpt_find_addr(dp, addr, addrlen))) { 207 if(bogus) 208 a->bogus = bogus; 209 if(!lame) 210 a->lame = 0; 211 return 1; 212 } 213 if(additions) 214 *additions = 1; 215 216 a = (struct delegpt_addr*)regional_alloc(region, 217 sizeof(struct delegpt_addr)); 218 if(!a) 219 return 0; 220 a->next_target = dp->target_list; 221 dp->target_list = a; 222 a->next_result = 0; 223 a->next_usable = dp->usable_list; 224 dp->usable_list = a; 225 memcpy(&a->addr, addr, addrlen); 226 a->addrlen = addrlen; 227 a->attempts = 0; 228 a->bogus = bogus; 229 a->lame = lame; 230 a->dnsseclame = 0; 231 if(tls_auth_name) { 232 a->tls_auth_name = regional_strdup(region, tls_auth_name); 233 if(!a->tls_auth_name) 234 return 0; 235 } else { 236 a->tls_auth_name = NULL; 237 } 238 return 1; 239 } 240 241 void 242 delegpt_count_ns(struct delegpt* dp, size_t* numns, size_t* missing) 243 { 244 struct delegpt_ns* ns; 245 *numns = 0; 246 *missing = 0; 247 for(ns = dp->nslist; ns; ns = ns->next) { 248 (*numns)++; 249 if(!ns->resolved) 250 (*missing)++; 251 } 252 } 253 254 void 255 delegpt_count_addr(struct delegpt* dp, size_t* numaddr, size_t* numres, 256 size_t* numavail) 257 { 258 struct delegpt_addr* a; 259 *numaddr = 0; 260 *numres = 0; 261 *numavail = 0; 262 for(a = dp->target_list; a; a = a->next_target) { 263 (*numaddr)++; 264 } 265 for(a = dp->result_list; a; a = a->next_result) { 266 (*numres)++; 267 } 268 for(a = dp->usable_list; a; a = a->next_usable) { 269 (*numavail)++; 270 } 271 } 272 273 void delegpt_log(enum verbosity_value v, struct delegpt* dp) 274 { 275 char buf[LDNS_MAX_DOMAINLEN+1]; 276 struct delegpt_ns* ns; 277 struct delegpt_addr* a; 278 size_t missing=0, numns=0, numaddr=0, numres=0, numavail=0; 279 if(verbosity < v) 280 return; 281 dname_str(dp->name, buf); 282 if(dp->nslist == NULL && dp->target_list == NULL) { 283 log_info("DelegationPoint<%s>: empty", buf); 284 return; 285 } 286 delegpt_count_ns(dp, &numns, &missing); 287 delegpt_count_addr(dp, &numaddr, &numres, &numavail); 288 log_info("DelegationPoint<%s>: %u names (%u missing), " 289 "%u addrs (%u result, %u avail)%s", 290 buf, (unsigned)numns, (unsigned)missing, 291 (unsigned)numaddr, (unsigned)numres, (unsigned)numavail, 292 (dp->has_parent_side_NS?" parentNS":" cacheNS")); 293 if(verbosity >= VERB_ALGO) { 294 for(ns = dp->nslist; ns; ns = ns->next) { 295 dname_str(ns->name, buf); 296 log_info(" %s %s%s%s%s%s%s%s", buf, 297 (ns->resolved?"*":""), 298 (ns->got4?" A":""), (ns->got6?" AAAA":""), 299 (dp->bogus?" BOGUS":""), (ns->lame?" PARENTSIDE":""), 300 (ns->done_pside4?" PSIDE_A":""), 301 (ns->done_pside6?" PSIDE_AAAA":"")); 302 } 303 for(a = dp->target_list; a; a = a->next_target) { 304 char s[128]; 305 const char* str = " "; 306 if(a->bogus && a->lame) str = " BOGUS ADDR_LAME "; 307 else if(a->bogus) str = " BOGUS "; 308 else if(a->lame) str = " ADDR_LAME "; 309 if(a->tls_auth_name) 310 snprintf(s, sizeof(s), "%s[%s]", str, 311 a->tls_auth_name); 312 else snprintf(s, sizeof(s), "%s", str); 313 log_addr(VERB_ALGO, s, &a->addr, a->addrlen); 314 } 315 } 316 } 317 318 void 319 delegpt_add_unused_targets(struct delegpt* dp) 320 { 321 struct delegpt_addr* usa = dp->usable_list; 322 dp->usable_list = NULL; 323 while(usa) { 324 usa->next_result = dp->result_list; 325 dp->result_list = usa; 326 usa = usa->next_usable; 327 } 328 } 329 330 size_t 331 delegpt_count_targets(struct delegpt* dp) 332 { 333 struct delegpt_addr* a; 334 size_t n = 0; 335 for(a = dp->target_list; a; a = a->next_target) 336 n++; 337 return n; 338 } 339 340 size_t 341 delegpt_count_missing_targets(struct delegpt* dp) 342 { 343 struct delegpt_ns* ns; 344 size_t n = 0; 345 for(ns = dp->nslist; ns; ns = ns->next) 346 if(!ns->resolved) 347 n++; 348 return n; 349 } 350 351 /** find NS rrset in given list */ 352 static struct ub_packed_rrset_key* 353 find_NS(struct reply_info* rep, size_t from, size_t to) 354 { 355 size_t i; 356 for(i=from; i<to; i++) { 357 if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS) 358 return rep->rrsets[i]; 359 } 360 return NULL; 361 } 362 363 struct delegpt* 364 delegpt_from_message(struct dns_msg* msg, struct regional* region) 365 { 366 struct ub_packed_rrset_key* ns_rrset = NULL; 367 struct delegpt* dp; 368 size_t i; 369 /* look for NS records in the authority section... */ 370 ns_rrset = find_NS(msg->rep, msg->rep->an_numrrsets, 371 msg->rep->an_numrrsets+msg->rep->ns_numrrsets); 372 373 /* In some cases (even legitimate, perfectly legal cases), the 374 * NS set for the "referral" might be in the answer section. */ 375 if(!ns_rrset) 376 ns_rrset = find_NS(msg->rep, 0, msg->rep->an_numrrsets); 377 378 /* If there was no NS rrset in the authority section, then this 379 * wasn't a referral message. (It might not actually be a 380 * referral message anyway) */ 381 if(!ns_rrset) 382 return NULL; 383 384 /* If we found any, then Yay! we have a delegation point. */ 385 dp = delegpt_create(region); 386 if(!dp) 387 return NULL; 388 dp->has_parent_side_NS = 1; /* created from message */ 389 if(!delegpt_set_name(dp, region, ns_rrset->rk.dname)) 390 return NULL; 391 if(!delegpt_rrset_add_ns(dp, region, ns_rrset, 0)) 392 return NULL; 393 394 /* add glue, A and AAAA in answer and additional section */ 395 for(i=0; i<msg->rep->rrset_count; i++) { 396 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 397 /* skip auth section. FIXME really needed?*/ 398 if(msg->rep->an_numrrsets <= i && 399 i < (msg->rep->an_numrrsets+msg->rep->ns_numrrsets)) 400 continue; 401 402 if(ntohs(s->rk.type) == LDNS_RR_TYPE_A) { 403 if(!delegpt_add_rrset_A(dp, region, s, 0, NULL)) 404 return NULL; 405 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_AAAA) { 406 if(!delegpt_add_rrset_AAAA(dp, region, s, 0, NULL)) 407 return NULL; 408 } 409 } 410 return dp; 411 } 412 413 int 414 delegpt_rrset_add_ns(struct delegpt* dp, struct regional* region, 415 struct ub_packed_rrset_key* ns_rrset, uint8_t lame) 416 { 417 struct packed_rrset_data* nsdata = (struct packed_rrset_data*) 418 ns_rrset->entry.data; 419 size_t i; 420 log_assert(!dp->dp_type_mlc); 421 if(nsdata->security == sec_status_bogus) 422 dp->bogus = 1; 423 for(i=0; i<nsdata->count; i++) { 424 if(nsdata->rr_len[i] < 2+1) continue; /* len + root label */ 425 if(dname_valid(nsdata->rr_data[i]+2, nsdata->rr_len[i]-2) != 426 (size_t)sldns_read_uint16(nsdata->rr_data[i])) 427 continue; /* bad format */ 428 /* add rdata of NS (= wirefmt dname), skip rdatalen bytes */ 429 if(!delegpt_add_ns(dp, region, nsdata->rr_data[i]+2, lame, 430 NULL, UNBOUND_DNS_PORT)) 431 return 0; 432 } 433 return 1; 434 } 435 436 int 437 delegpt_add_rrset_A(struct delegpt* dp, struct regional* region, 438 struct ub_packed_rrset_key* ak, uint8_t lame, int* additions) 439 { 440 struct packed_rrset_data* d=(struct packed_rrset_data*)ak->entry.data; 441 size_t i; 442 struct sockaddr_in sa; 443 socklen_t len = (socklen_t)sizeof(sa); 444 log_assert(!dp->dp_type_mlc); 445 memset(&sa, 0, len); 446 sa.sin_family = AF_INET; 447 for(i=0; i<d->count; i++) { 448 if(d->rr_len[i] != 2 + INET_SIZE) 449 continue; 450 memmove(&sa.sin_addr, d->rr_data[i]+2, INET_SIZE); 451 if(!delegpt_add_target(dp, region, ak->rk.dname, 452 ak->rk.dname_len, (struct sockaddr_storage*)&sa, 453 len, (d->security==sec_status_bogus), lame, additions)) 454 return 0; 455 } 456 return 1; 457 } 458 459 int 460 delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* region, 461 struct ub_packed_rrset_key* ak, uint8_t lame, int* additions) 462 { 463 struct packed_rrset_data* d=(struct packed_rrset_data*)ak->entry.data; 464 size_t i; 465 struct sockaddr_in6 sa; 466 socklen_t len = (socklen_t)sizeof(sa); 467 log_assert(!dp->dp_type_mlc); 468 memset(&sa, 0, len); 469 sa.sin6_family = AF_INET6; 470 for(i=0; i<d->count; i++) { 471 if(d->rr_len[i] != 2 + INET6_SIZE) /* rdatalen + len of IP6 */ 472 continue; 473 memmove(&sa.sin6_addr, d->rr_data[i]+2, INET6_SIZE); 474 if(!delegpt_add_target(dp, region, ak->rk.dname, 475 ak->rk.dname_len, (struct sockaddr_storage*)&sa, 476 len, (d->security==sec_status_bogus), lame, additions)) 477 return 0; 478 } 479 return 1; 480 } 481 482 int 483 delegpt_add_rrset(struct delegpt* dp, struct regional* region, 484 struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions) 485 { 486 if(!rrset) 487 return 1; 488 if(ntohs(rrset->rk.type) == LDNS_RR_TYPE_NS) 489 return delegpt_rrset_add_ns(dp, region, rrset, lame); 490 else if(ntohs(rrset->rk.type) == LDNS_RR_TYPE_A) 491 return delegpt_add_rrset_A(dp, region, rrset, lame, additions); 492 else if(ntohs(rrset->rk.type) == LDNS_RR_TYPE_AAAA) 493 return delegpt_add_rrset_AAAA(dp, region, rrset, lame, 494 additions); 495 log_warn("Unknown rrset type added to delegpt"); 496 return 1; 497 } 498 499 void delegpt_mark_neg(struct delegpt_ns* ns, uint16_t qtype) 500 { 501 if(ns) { 502 if(qtype == LDNS_RR_TYPE_A) 503 ns->got4 = 2; 504 else if(qtype == LDNS_RR_TYPE_AAAA) 505 ns->got6 = 2; 506 if(ns->got4 && ns->got6) 507 ns->resolved = 1; 508 } 509 } 510 511 void delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg) 512 { 513 struct reply_info* rep = (struct reply_info*)msg->entry.data; 514 if(!rep) return; 515 516 /* if error or no answers */ 517 if(FLAGS_GET_RCODE(rep->flags) != 0 || rep->an_numrrsets == 0) { 518 struct delegpt_ns* ns = delegpt_find_ns(dp, msg->key.qname, 519 msg->key.qname_len); 520 delegpt_mark_neg(ns, msg->key.qtype); 521 } 522 } 523 524 void delegpt_no_ipv6(struct delegpt* dp) 525 { 526 struct delegpt_ns* ns; 527 for(ns = dp->nslist; ns; ns = ns->next) { 528 /* no ipv6, so only ipv4 is enough to resolve a nameserver */ 529 if(ns->got4) 530 ns->resolved = 1; 531 } 532 } 533 534 void delegpt_no_ipv4(struct delegpt* dp) 535 { 536 struct delegpt_ns* ns; 537 for(ns = dp->nslist; ns; ns = ns->next) { 538 /* no ipv4, so only ipv6 is enough to resolve a nameserver */ 539 if(ns->got6) 540 ns->resolved = 1; 541 } 542 } 543 544 struct delegpt* delegpt_create_mlc(uint8_t* name) 545 { 546 struct delegpt* dp=(struct delegpt*)calloc(1, sizeof(*dp)); 547 if(!dp) 548 return NULL; 549 dp->dp_type_mlc = 1; 550 if(name) { 551 dp->namelabs = dname_count_size_labels(name, &dp->namelen); 552 dp->name = memdup(name, dp->namelen); 553 if(!dp->name) { 554 free(dp); 555 return NULL; 556 } 557 } 558 return dp; 559 } 560 561 void delegpt_free_mlc(struct delegpt* dp) 562 { 563 struct delegpt_ns* n, *nn; 564 struct delegpt_addr* a, *na; 565 if(!dp) return; 566 log_assert(dp->dp_type_mlc); 567 n = dp->nslist; 568 while(n) { 569 nn = n->next; 570 free(n->name); 571 free(n->tls_auth_name); 572 free(n); 573 n = nn; 574 } 575 a = dp->target_list; 576 while(a) { 577 na = a->next_target; 578 free(a->tls_auth_name); 579 free(a); 580 a = na; 581 } 582 free(dp->name); 583 free(dp); 584 } 585 586 int delegpt_set_name_mlc(struct delegpt* dp, uint8_t* name) 587 { 588 log_assert(dp->dp_type_mlc); 589 dp->namelabs = dname_count_size_labels(name, &dp->namelen); 590 dp->name = memdup(name, dp->namelen); 591 return (dp->name != NULL); 592 } 593 594 int delegpt_add_ns_mlc(struct delegpt* dp, uint8_t* name, uint8_t lame, 595 char* tls_auth_name, int port) 596 { 597 struct delegpt_ns* ns; 598 size_t len; 599 (void)dname_count_size_labels(name, &len); 600 log_assert(dp->dp_type_mlc); 601 /* slow check for duplicates to avoid counting failures when 602 * adding the same server as a dependency twice */ 603 if(delegpt_find_ns(dp, name, len)) 604 return 1; 605 ns = (struct delegpt_ns*)malloc(sizeof(struct delegpt_ns)); 606 if(!ns) 607 return 0; 608 ns->namelen = len; 609 ns->name = memdup(name, ns->namelen); 610 if(!ns->name) { 611 free(ns); 612 return 0; 613 } 614 ns->next = dp->nslist; 615 dp->nslist = ns; 616 ns->resolved = 0; 617 ns->got4 = 0; 618 ns->got6 = 0; 619 ns->lame = (uint8_t)lame; 620 ns->done_pside4 = 0; 621 ns->done_pside6 = 0; 622 ns->port = port; 623 if(tls_auth_name) { 624 ns->tls_auth_name = strdup(tls_auth_name); 625 if(!ns->tls_auth_name) { 626 free(ns->name); 627 free(ns); 628 return 0; 629 } 630 } else { 631 ns->tls_auth_name = NULL; 632 } 633 return 1; 634 } 635 636 int delegpt_add_addr_mlc(struct delegpt* dp, struct sockaddr_storage* addr, 637 socklen_t addrlen, uint8_t bogus, uint8_t lame, char* tls_auth_name, 638 int port) 639 { 640 struct delegpt_addr* a; 641 log_assert(dp->dp_type_mlc); 642 if(port != -1) { 643 log_assert(port>0); 644 sockaddr_store_port(addr, addrlen, port); 645 } 646 /* check for duplicates */ 647 if((a = delegpt_find_addr(dp, addr, addrlen))) { 648 if(bogus) 649 a->bogus = bogus; 650 if(!lame) 651 a->lame = 0; 652 return 1; 653 } 654 655 a = (struct delegpt_addr*)malloc(sizeof(struct delegpt_addr)); 656 if(!a) 657 return 0; 658 a->next_target = dp->target_list; 659 dp->target_list = a; 660 a->next_result = 0; 661 a->next_usable = dp->usable_list; 662 dp->usable_list = a; 663 memcpy(&a->addr, addr, addrlen); 664 a->addrlen = addrlen; 665 a->attempts = 0; 666 a->bogus = bogus; 667 a->lame = lame; 668 a->dnsseclame = 0; 669 if(tls_auth_name) { 670 a->tls_auth_name = strdup(tls_auth_name); 671 if(!a->tls_auth_name) { 672 free(a); 673 return 0; 674 } 675 } else { 676 a->tls_auth_name = NULL; 677 } 678 return 1; 679 } 680 681 int delegpt_add_target_mlc(struct delegpt* dp, uint8_t* name, size_t namelen, 682 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus, 683 uint8_t lame) 684 { 685 struct delegpt_ns* ns = delegpt_find_ns(dp, name, namelen); 686 log_assert(dp->dp_type_mlc); 687 if(!ns) { 688 /* ignore it */ 689 return 1; 690 } 691 if(!lame) { 692 if(addr_is_ip6(addr, addrlen)) 693 ns->got6 = 1; 694 else ns->got4 = 1; 695 if(ns->got4 && ns->got6) 696 ns->resolved = 1; 697 } 698 log_assert(ns->port>0); 699 return delegpt_add_addr_mlc(dp, addr, addrlen, bogus, lame, 700 ns->tls_auth_name, ns->port); 701 } 702 703 size_t delegpt_get_mem(struct delegpt* dp) 704 { 705 struct delegpt_ns* ns; 706 size_t s; 707 if(!dp) return 0; 708 s = sizeof(*dp) + dp->namelen + 709 delegpt_count_targets(dp)*sizeof(struct delegpt_addr); 710 for(ns=dp->nslist; ns; ns=ns->next) 711 s += sizeof(*ns)+ns->namelen; 712 return s; 713 } 714