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