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