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