xref: /freebsd/contrib/unbound/util/data/msgreply.c (revision 681ce946)
1 /*
2  * util/data/msgreply.c - store message and reply data.
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 a data structure to store a message and its reply.
40  */
41 
42 #include "config.h"
43 #include "util/data/msgreply.h"
44 #include "util/storage/lookup3.h"
45 #include "util/log.h"
46 #include "util/alloc.h"
47 #include "util/netevent.h"
48 #include "util/net_help.h"
49 #include "util/data/dname.h"
50 #include "util/regional.h"
51 #include "util/data/msgparse.h"
52 #include "util/data/msgencode.h"
53 #include "sldns/sbuffer.h"
54 #include "sldns/wire2str.h"
55 #include "util/module.h"
56 #include "util/fptr_wlist.h"
57 
58 /** MAX TTL default for messages and rrsets */
59 time_t MAX_TTL = 3600 * 24 * 10; /* ten days */
60 /** MIN TTL default for messages and rrsets */
61 time_t MIN_TTL = 0;
62 /** MAX Negative TTL, for SOA records in authority section */
63 time_t MAX_NEG_TTL = 3600; /* one hour */
64 /** If we serve expired entries and prefetch them */
65 int SERVE_EXPIRED = 0;
66 /** Time to serve records after expiration */
67 time_t SERVE_EXPIRED_TTL = 0;
68 /** TTL to use for expired records */
69 time_t SERVE_EXPIRED_REPLY_TTL = 30;
70 /** If we serve the original TTL or decrementing TTLs */
71 int SERVE_ORIGINAL_TTL = 0;
72 
73 /** allocate qinfo, return 0 on error */
74 static int
75 parse_create_qinfo(sldns_buffer* pkt, struct msg_parse* msg,
76 	struct query_info* qinf, struct regional* region)
77 {
78 	if(msg->qname) {
79 		if(region)
80 			qinf->qname = (uint8_t*)regional_alloc(region,
81 				msg->qname_len);
82 		else	qinf->qname = (uint8_t*)malloc(msg->qname_len);
83 		if(!qinf->qname) return 0;
84 		dname_pkt_copy(pkt, qinf->qname, msg->qname);
85 	} else	qinf->qname = 0;
86 	qinf->qname_len = msg->qname_len;
87 	qinf->qtype = msg->qtype;
88 	qinf->qclass = msg->qclass;
89 	qinf->local_alias = NULL;
90 	return 1;
91 }
92 
93 /** constructor for replyinfo */
94 struct reply_info*
95 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd,
96 	time_t ttl, time_t prettl, time_t expttl, size_t an, size_t ns,
97 	size_t ar, size_t total, enum sec_status sec)
98 {
99 	struct reply_info* rep;
100 	/* rrset_count-1 because the first ref is part of the struct. */
101 	size_t s = sizeof(struct reply_info) - sizeof(struct rrset_ref) +
102 		sizeof(struct ub_packed_rrset_key*) * total;
103 	if(total >= RR_COUNT_MAX) return NULL; /* sanity check on numRRS*/
104 	if(region)
105 		rep = (struct reply_info*)regional_alloc(region, s);
106 	else	rep = (struct reply_info*)malloc(s +
107 			sizeof(struct rrset_ref) * (total));
108 	if(!rep)
109 		return NULL;
110 	rep->flags = flags;
111 	rep->qdcount = qd;
112 	rep->ttl = ttl;
113 	rep->prefetch_ttl = prettl;
114 	rep->serve_expired_ttl = expttl;
115 	rep->an_numrrsets = an;
116 	rep->ns_numrrsets = ns;
117 	rep->ar_numrrsets = ar;
118 	rep->rrset_count = total;
119 	rep->security = sec;
120 	rep->authoritative = 0;
121 	/* array starts after the refs */
122 	if(region)
123 		rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[0]);
124 	else	rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[total]);
125 	/* zero the arrays to assist cleanup in case of malloc failure */
126 	memset( rep->rrsets, 0, sizeof(struct ub_packed_rrset_key*) * total);
127 	if(!region)
128 		memset( &rep->ref[0], 0, sizeof(struct rrset_ref) * total);
129 	return rep;
130 }
131 
132 /** allocate replyinfo, return 0 on error */
133 static int
134 parse_create_repinfo(struct msg_parse* msg, struct reply_info** rep,
135 	struct regional* region)
136 {
137 	*rep = construct_reply_info_base(region, msg->flags, msg->qdcount, 0,
138 		0, 0, msg->an_rrsets, msg->ns_rrsets, msg->ar_rrsets,
139 		msg->rrset_count, sec_status_unchecked);
140 	if(!*rep)
141 		return 0;
142 	return 1;
143 }
144 
145 int
146 reply_info_alloc_rrset_keys(struct reply_info* rep, struct alloc_cache* alloc,
147 	struct regional* region)
148 {
149 	size_t i;
150 	for(i=0; i<rep->rrset_count; i++) {
151 		if(region) {
152 			rep->rrsets[i] = (struct ub_packed_rrset_key*)
153 				regional_alloc(region,
154 				sizeof(struct ub_packed_rrset_key));
155 			if(rep->rrsets[i]) {
156 				memset(rep->rrsets[i], 0,
157 					sizeof(struct ub_packed_rrset_key));
158 				rep->rrsets[i]->entry.key = rep->rrsets[i];
159 			}
160 		}
161 		else	rep->rrsets[i] = alloc_special_obtain(alloc);
162 		if(!rep->rrsets[i])
163 			return 0;
164 		rep->rrsets[i]->entry.data = NULL;
165 	}
166 	return 1;
167 }
168 
169 struct reply_info *
170 make_new_reply_info(const struct reply_info* rep, struct regional* region,
171 	size_t an_numrrsets, size_t copy_rrsets)
172 {
173 	struct reply_info* new_rep;
174 	size_t i;
175 
176 	/* create a base struct.  we specify 'insecure' security status as
177 	 * the modified response won't be DNSSEC-valid.  In our faked response
178 	 * the authority and additional sections will be empty (except possible
179 	 * EDNS0 OPT RR in the additional section appended on sending it out),
180 	 * so the total number of RRsets is an_numrrsets. */
181 	new_rep = construct_reply_info_base(region, rep->flags,
182 		rep->qdcount, rep->ttl, rep->prefetch_ttl,
183 		rep->serve_expired_ttl, an_numrrsets, 0, 0, an_numrrsets,
184 		sec_status_insecure);
185 	if(!new_rep)
186 		return NULL;
187 	if(!reply_info_alloc_rrset_keys(new_rep, NULL, region))
188 		return NULL;
189 	for(i=0; i<copy_rrsets; i++)
190 		new_rep->rrsets[i] = rep->rrsets[i];
191 
192 	return new_rep;
193 }
194 
195 /** find the minimumttl in the rdata of SOA record */
196 static time_t
197 soa_find_minttl(struct rr_parse* rr)
198 {
199 	uint16_t rlen = sldns_read_uint16(rr->ttl_data+4);
200 	if(rlen < 20)
201 		return 0; /* rdata too small for SOA (dname, dname, 5*32bit) */
202 	/* minimum TTL is the last 32bit value in the rdata of the record */
203 	/* at position ttl_data + 4(ttl) + 2(rdatalen) + rdatalen - 4(timeval)*/
204 	return (time_t)sldns_read_uint32(rr->ttl_data+6+rlen-4);
205 }
206 
207 /** do the rdata copy */
208 static int
209 rdata_copy(sldns_buffer* pkt, struct packed_rrset_data* data, uint8_t* to,
210 	struct rr_parse* rr, time_t* rr_ttl, uint16_t type,
211 	sldns_pkt_section section)
212 {
213 	uint16_t pkt_len;
214 	const sldns_rr_descriptor* desc;
215 
216 	*rr_ttl = sldns_read_uint32(rr->ttl_data);
217 	/* RFC 2181 Section 8. if msb of ttl is set treat as if zero. */
218 	if(*rr_ttl & 0x80000000U)
219 		*rr_ttl = 0;
220 	if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) {
221 		/* negative response. see if TTL of SOA record larger than the
222 		 * minimum-ttl in the rdata of the SOA record */
223 		if(*rr_ttl > soa_find_minttl(rr))
224 			*rr_ttl = soa_find_minttl(rr);
225 	}
226 	if(!SERVE_ORIGINAL_TTL && (*rr_ttl < MIN_TTL))
227 		*rr_ttl = MIN_TTL;
228 	if(!SERVE_ORIGINAL_TTL && (*rr_ttl > MAX_TTL))
229 		*rr_ttl = MAX_TTL;
230 	if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) {
231 		/* max neg ttl overrides the min and max ttl of everything
232 		 * else, it is for a more specific record */
233 		if(*rr_ttl > MAX_NEG_TTL)
234 			*rr_ttl = MAX_NEG_TTL;
235 	}
236 	if(*rr_ttl < data->ttl)
237 		data->ttl = *rr_ttl;
238 
239 	if(rr->outside_packet) {
240 		/* uncompressed already, only needs copy */
241 		memmove(to, rr->ttl_data+sizeof(uint32_t), rr->size);
242 		return 1;
243 	}
244 
245 	sldns_buffer_set_position(pkt, (size_t)
246 		(rr->ttl_data - sldns_buffer_begin(pkt) + sizeof(uint32_t)));
247 	/* insert decompressed size into rdata len stored in memory */
248 	/* -2 because rdatalen bytes are not included. */
249 	pkt_len = htons(rr->size - 2);
250 	memmove(to, &pkt_len, sizeof(uint16_t));
251 	to += 2;
252 	/* read packet rdata len */
253 	pkt_len = sldns_buffer_read_u16(pkt);
254 	if(sldns_buffer_remaining(pkt) < pkt_len)
255 		return 0;
256 	desc = sldns_rr_descript(type);
257 	if(pkt_len > 0 && desc && desc->_dname_count > 0) {
258 		int count = (int)desc->_dname_count;
259 		int rdf = 0;
260 		size_t len;
261 		size_t oldpos;
262 		/* decompress dnames. */
263 		while(pkt_len > 0 && count) {
264 			switch(desc->_wireformat[rdf]) {
265 			case LDNS_RDF_TYPE_DNAME:
266 				oldpos = sldns_buffer_position(pkt);
267 				dname_pkt_copy(pkt, to,
268 					sldns_buffer_current(pkt));
269 				to += pkt_dname_len(pkt);
270 				pkt_len -= sldns_buffer_position(pkt)-oldpos;
271 				count--;
272 				len = 0;
273 				break;
274 			case LDNS_RDF_TYPE_STR:
275 				len = sldns_buffer_current(pkt)[0] + 1;
276 				break;
277 			default:
278 				len = get_rdf_size(desc->_wireformat[rdf]);
279 				break;
280 			}
281 			if(len) {
282 				log_assert(len <= pkt_len);
283 				memmove(to, sldns_buffer_current(pkt), len);
284 				to += len;
285 				sldns_buffer_skip(pkt, (ssize_t)len);
286 				pkt_len -= len;
287 			}
288 			rdf++;
289 		}
290 	}
291 	/* copy remaining rdata */
292 	if(pkt_len >  0)
293 		memmove(to, sldns_buffer_current(pkt), pkt_len);
294 
295 	return 1;
296 }
297 
298 /** copy over the data into packed rrset */
299 static int
300 parse_rr_copy(sldns_buffer* pkt, struct rrset_parse* pset,
301 	struct packed_rrset_data* data)
302 {
303 	size_t i;
304 	struct rr_parse* rr = pset->rr_first;
305 	uint8_t* nextrdata;
306 	size_t total = pset->rr_count + pset->rrsig_count;
307 	data->ttl = MAX_TTL;
308 	data->count = pset->rr_count;
309 	data->rrsig_count = pset->rrsig_count;
310 	data->trust = rrset_trust_none;
311 	data->security = sec_status_unchecked;
312 	/* layout: struct - rr_len - rr_data - rr_ttl - rdata - rrsig */
313 	data->rr_len = (size_t*)((uint8_t*)data +
314 		sizeof(struct packed_rrset_data));
315 	data->rr_data = (uint8_t**)&(data->rr_len[total]);
316 	data->rr_ttl = (time_t*)&(data->rr_data[total]);
317 	nextrdata = (uint8_t*)&(data->rr_ttl[total]);
318 	for(i=0; i<data->count; i++) {
319 		data->rr_len[i] = rr->size;
320 		data->rr_data[i] = nextrdata;
321 		nextrdata += rr->size;
322 		if(!rdata_copy(pkt, data, data->rr_data[i], rr,
323 			&data->rr_ttl[i], pset->type, pset->section))
324 			return 0;
325 		rr = rr->next;
326 	}
327 	/* if rrsig, its rdata is at nextrdata */
328 	rr = pset->rrsig_first;
329 	for(i=data->count; i<total; i++) {
330 		data->rr_len[i] = rr->size;
331 		data->rr_data[i] = nextrdata;
332 		nextrdata += rr->size;
333 		if(!rdata_copy(pkt, data, data->rr_data[i], rr,
334 			&data->rr_ttl[i], LDNS_RR_TYPE_RRSIG, pset->section))
335 			return 0;
336 		rr = rr->next;
337 	}
338 	return 1;
339 }
340 
341 /** create rrset return 0 on failure */
342 static int
343 parse_create_rrset(sldns_buffer* pkt, struct rrset_parse* pset,
344 	struct packed_rrset_data** data, struct regional* region)
345 {
346 	/* allocate */
347 	size_t s;
348 	if(pset->rr_count > RR_COUNT_MAX || pset->rrsig_count > RR_COUNT_MAX ||
349 		pset->size > RR_COUNT_MAX)
350 		return 0; /* protect against integer overflow */
351 	s = sizeof(struct packed_rrset_data) +
352 		(pset->rr_count + pset->rrsig_count) *
353 		(sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t)) +
354 		pset->size;
355 	if(region)
356 		*data = regional_alloc_zero(region, s);
357 	else	*data = calloc(1, s);
358 	if(!*data)
359 		return 0;
360 	/* copy & decompress */
361 	if(!parse_rr_copy(pkt, pset, *data)) {
362 		if(!region) {
363 			free(*data);
364 			*data = NULL;
365 		}
366 		return 0;
367 	}
368 	return 1;
369 }
370 
371 /** get trust value for rrset */
372 static enum rrset_trust
373 get_rrset_trust(struct msg_parse* msg, struct rrset_parse* rrset)
374 {
375 	uint16_t AA = msg->flags & BIT_AA;
376 	if(rrset->section == LDNS_SECTION_ANSWER) {
377 		if(AA) {
378 			/* RFC2181 says remainder of CNAME chain is nonauth*/
379 			if(msg->rrset_first &&
380 				msg->rrset_first->section==LDNS_SECTION_ANSWER
381 				&& msg->rrset_first->type==LDNS_RR_TYPE_CNAME){
382 				if(rrset == msg->rrset_first)
383 					return rrset_trust_ans_AA;
384 				else 	return rrset_trust_ans_noAA;
385 			}
386 			if(msg->rrset_first &&
387 				msg->rrset_first->section==LDNS_SECTION_ANSWER
388 				&& msg->rrset_first->type==LDNS_RR_TYPE_DNAME){
389 				if(rrset == msg->rrset_first ||
390 				   rrset == msg->rrset_first->rrset_all_next)
391 					return rrset_trust_ans_AA;
392 				else 	return rrset_trust_ans_noAA;
393 			}
394 			return rrset_trust_ans_AA;
395 		}
396 		else	return rrset_trust_ans_noAA;
397 	} else if(rrset->section == LDNS_SECTION_AUTHORITY) {
398 		if(AA)	return rrset_trust_auth_AA;
399 		else	return rrset_trust_auth_noAA;
400 	} else {
401 		/* addit section */
402 		if(AA)	return rrset_trust_add_AA;
403 		else	return rrset_trust_add_noAA;
404 	}
405 	/* NOTREACHED */
406 	return rrset_trust_none;
407 }
408 
409 int
410 parse_copy_decompress_rrset(sldns_buffer* pkt, struct msg_parse* msg,
411 	struct rrset_parse *pset, struct regional* region,
412 	struct ub_packed_rrset_key* pk)
413 {
414 	struct packed_rrset_data* data;
415 	pk->rk.flags = pset->flags;
416 	pk->rk.dname_len = pset->dname_len;
417 	if(region)
418 		pk->rk.dname = (uint8_t*)regional_alloc(
419 			region, pset->dname_len);
420 	else	pk->rk.dname =
421 			(uint8_t*)malloc(pset->dname_len);
422 	if(!pk->rk.dname)
423 		return 0;
424 	/** copy & decompress dname */
425 	dname_pkt_copy(pkt, pk->rk.dname, pset->dname);
426 	/** copy over type and class */
427 	pk->rk.type = htons(pset->type);
428 	pk->rk.rrset_class = pset->rrset_class;
429 	/** read data part. */
430 	if(!parse_create_rrset(pkt, pset, &data, region)) {
431 		if(!region) {
432 			free(pk->rk.dname);
433 			pk->rk.dname = NULL;
434 		}
435 		return 0;
436 	}
437 	pk->entry.data = (void*)data;
438 	pk->entry.key = (void*)pk;
439 	pk->entry.hash = pset->hash;
440 	data->trust = get_rrset_trust(msg, pset);
441 	return 1;
442 }
443 
444 /**
445  * Copy and decompress rrs
446  * @param pkt: the packet for compression pointer resolution.
447  * @param msg: the parsed message
448  * @param rep: reply info to put rrs into.
449  * @param region: if not NULL, used for allocation.
450  * @return 0 on failure.
451  */
452 static int
453 parse_copy_decompress(sldns_buffer* pkt, struct msg_parse* msg,
454 	struct reply_info* rep, struct regional* region)
455 {
456 	size_t i;
457 	struct rrset_parse *pset = msg->rrset_first;
458 	struct packed_rrset_data* data;
459 	log_assert(rep);
460 	rep->ttl = MAX_TTL;
461 	rep->security = sec_status_unchecked;
462 	if(rep->rrset_count == 0)
463 		rep->ttl = NORR_TTL;
464 
465 	for(i=0; i<rep->rrset_count; i++) {
466 		if(!parse_copy_decompress_rrset(pkt, msg, pset, region,
467 			rep->rrsets[i]))
468 			return 0;
469 		data = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
470 		if(data->ttl < rep->ttl)
471 			rep->ttl = data->ttl;
472 
473 		pset = pset->rrset_all_next;
474 	}
475 	rep->prefetch_ttl = PREFETCH_TTL_CALC(rep->ttl);
476 	rep->serve_expired_ttl = rep->ttl + SERVE_EXPIRED_TTL;
477 	return 1;
478 }
479 
480 int
481 parse_create_msg(sldns_buffer* pkt, struct msg_parse* msg,
482 	struct alloc_cache* alloc, struct query_info* qinf,
483 	struct reply_info** rep, struct regional* region)
484 {
485 	log_assert(pkt && msg);
486 	if(!parse_create_qinfo(pkt, msg, qinf, region))
487 		return 0;
488 	if(!parse_create_repinfo(msg, rep, region))
489 		return 0;
490 	if(!reply_info_alloc_rrset_keys(*rep, alloc, region)) {
491 		if(!region) reply_info_parsedelete(*rep, alloc);
492 		return 0;
493 	}
494 	if(!parse_copy_decompress(pkt, msg, *rep, region)) {
495 		if(!region) reply_info_parsedelete(*rep, alloc);
496 		return 0;
497 	}
498 	return 1;
499 }
500 
501 int reply_info_parse(sldns_buffer* pkt, struct alloc_cache* alloc,
502         struct query_info* qinf, struct reply_info** rep,
503 	struct regional* region, struct edns_data* edns)
504 {
505 	/* use scratch pad region-allocator during parsing. */
506 	struct msg_parse* msg;
507 	int ret;
508 
509 	qinf->qname = NULL;
510 	qinf->local_alias = NULL;
511 	*rep = NULL;
512 	if(!(msg = regional_alloc(region, sizeof(*msg)))) {
513 		return LDNS_RCODE_SERVFAIL;
514 	}
515 	memset(msg, 0, sizeof(*msg));
516 
517 	sldns_buffer_set_position(pkt, 0);
518 	if((ret = parse_packet(pkt, msg, region)) != 0) {
519 		return ret;
520 	}
521 	if((ret = parse_extract_edns_from_response_msg(msg, edns, region)) != 0)
522 		return ret;
523 
524 	/* parse OK, allocate return structures */
525 	/* this also performs dname decompression */
526 	if(!parse_create_msg(pkt, msg, alloc, qinf, rep, NULL)) {
527 		query_info_clear(qinf);
528 		*rep = NULL;
529 		return LDNS_RCODE_SERVFAIL;
530 	}
531 	return 0;
532 }
533 
534 /** helper compare function to sort in lock order */
535 static int
536 reply_info_sortref_cmp(const void* a, const void* b)
537 {
538 	struct rrset_ref* x = (struct rrset_ref*)a;
539 	struct rrset_ref* y = (struct rrset_ref*)b;
540 	if(x->key < y->key) return -1;
541 	if(x->key > y->key) return 1;
542 	return 0;
543 }
544 
545 void
546 reply_info_sortref(struct reply_info* rep)
547 {
548 	qsort(&rep->ref[0], rep->rrset_count, sizeof(struct rrset_ref),
549 		reply_info_sortref_cmp);
550 }
551 
552 void
553 reply_info_set_ttls(struct reply_info* rep, time_t timenow)
554 {
555 	size_t i, j;
556 	rep->ttl += timenow;
557 	rep->prefetch_ttl += timenow;
558 	rep->serve_expired_ttl += timenow;
559 	for(i=0; i<rep->rrset_count; i++) {
560 		struct packed_rrset_data* data = (struct packed_rrset_data*)
561 			rep->ref[i].key->entry.data;
562 		if(i>0 && rep->ref[i].key == rep->ref[i-1].key)
563 			continue;
564 		data->ttl += timenow;
565 		for(j=0; j<data->count + data->rrsig_count; j++) {
566 			data->rr_ttl[j] += timenow;
567 		}
568 		data->ttl_add = timenow;
569 	}
570 }
571 
572 void
573 reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc)
574 {
575 	size_t i;
576 	if(!rep)
577 		return;
578 	/* no need to lock, since not shared in hashtables. */
579 	for(i=0; i<rep->rrset_count; i++) {
580 		ub_packed_rrset_parsedelete(rep->rrsets[i], alloc);
581 	}
582 	free(rep);
583 }
584 
585 int
586 query_info_parse(struct query_info* m, sldns_buffer* query)
587 {
588 	uint8_t* q = sldns_buffer_begin(query);
589 	/* minimum size: header + \0 + qtype + qclass */
590 	if(sldns_buffer_limit(query) < LDNS_HEADER_SIZE + 5)
591 		return 0;
592 	if((LDNS_OPCODE_WIRE(q) != LDNS_PACKET_QUERY && LDNS_OPCODE_WIRE(q) !=
593 		LDNS_PACKET_NOTIFY) || LDNS_QDCOUNT(q) != 1 ||
594 		sldns_buffer_position(query) != 0)
595 		return 0;
596 	sldns_buffer_skip(query, LDNS_HEADER_SIZE);
597 	m->qname = sldns_buffer_current(query);
598 	if((m->qname_len = query_dname_len(query)) == 0)
599 		return 0; /* parse error */
600 	if(sldns_buffer_remaining(query) < 4)
601 		return 0; /* need qtype, qclass */
602 	m->qtype = sldns_buffer_read_u16(query);
603 	m->qclass = sldns_buffer_read_u16(query);
604 	m->local_alias = NULL;
605 	return 1;
606 }
607 
608 /** tiny subroutine for msgreply_compare */
609 #define COMPARE_IT(x, y) \
610 	if( (x) < (y) ) return -1; \
611 	else if( (x) > (y) ) return +1; \
612 	log_assert( (x) == (y) );
613 
614 int
615 query_info_compare(void* m1, void* m2)
616 {
617 	struct query_info* msg1 = (struct query_info*)m1;
618 	struct query_info* msg2 = (struct query_info*)m2;
619 	int mc;
620 	/* from most different to least different for speed */
621 	COMPARE_IT(msg1->qtype, msg2->qtype);
622 	if((mc = query_dname_compare(msg1->qname, msg2->qname)) != 0)
623 		return mc;
624 	log_assert(msg1->qname_len == msg2->qname_len);
625 	COMPARE_IT(msg1->qclass, msg2->qclass);
626 	return 0;
627 #undef COMPARE_IT
628 }
629 
630 void
631 query_info_clear(struct query_info* m)
632 {
633 	free(m->qname);
634 	m->qname = NULL;
635 }
636 
637 size_t
638 msgreply_sizefunc(void* k, void* d)
639 {
640 	struct msgreply_entry* q = (struct msgreply_entry*)k;
641 	struct reply_info* r = (struct reply_info*)d;
642 	size_t s = sizeof(struct msgreply_entry) + sizeof(struct reply_info)
643 		+ q->key.qname_len + lock_get_mem(&q->entry.lock)
644 		- sizeof(struct rrset_ref);
645 	s += r->rrset_count * sizeof(struct rrset_ref);
646 	s += r->rrset_count * sizeof(struct ub_packed_rrset_key*);
647 	return s;
648 }
649 
650 void
651 query_entry_delete(void *k, void* ATTR_UNUSED(arg))
652 {
653 	struct msgreply_entry* q = (struct msgreply_entry*)k;
654 	lock_rw_destroy(&q->entry.lock);
655 	query_info_clear(&q->key);
656 	free(q);
657 }
658 
659 void
660 reply_info_delete(void* d, void* ATTR_UNUSED(arg))
661 {
662 	struct reply_info* r = (struct reply_info*)d;
663 	free(r);
664 }
665 
666 hashvalue_type
667 query_info_hash(struct query_info *q, uint16_t flags)
668 {
669 	hashvalue_type h = 0xab;
670 	h = hashlittle(&q->qtype, sizeof(q->qtype), h);
671 	if(q->qtype == LDNS_RR_TYPE_AAAA && (flags&BIT_CD))
672 		h++;
673 	h = hashlittle(&q->qclass, sizeof(q->qclass), h);
674 	h = dname_query_hash(q->qname, h);
675 	return h;
676 }
677 
678 struct msgreply_entry*
679 query_info_entrysetup(struct query_info* q, struct reply_info* r,
680 	hashvalue_type h)
681 {
682 	struct msgreply_entry* e = (struct msgreply_entry*)malloc(
683 		sizeof(struct msgreply_entry));
684 	if(!e) return NULL;
685 	memcpy(&e->key, q, sizeof(*q));
686 	e->entry.hash = h;
687 	e->entry.key = e;
688 	e->entry.data = r;
689 	lock_rw_init(&e->entry.lock);
690 	lock_protect(&e->entry.lock, &e->key.qname, sizeof(e->key.qname));
691 	lock_protect(&e->entry.lock, &e->key.qname_len, sizeof(e->key.qname_len));
692 	lock_protect(&e->entry.lock, &e->key.qtype, sizeof(e->key.qtype));
693 	lock_protect(&e->entry.lock, &e->key.qclass, sizeof(e->key.qclass));
694 	lock_protect(&e->entry.lock, &e->key.local_alias, sizeof(e->key.local_alias));
695 	lock_protect(&e->entry.lock, &e->entry.hash, sizeof(e->entry.hash));
696 	lock_protect(&e->entry.lock, &e->entry.key, sizeof(e->entry.key));
697 	lock_protect(&e->entry.lock, &e->entry.data, sizeof(e->entry.data));
698 	lock_protect(&e->entry.lock, e->key.qname, e->key.qname_len);
699 	q->qname = NULL;
700 	return e;
701 }
702 
703 /** copy rrsets from replyinfo to dest replyinfo */
704 static int
705 repinfo_copy_rrsets(struct reply_info* dest, struct reply_info* from,
706 	struct regional* region)
707 {
708 	size_t i, s;
709 	struct packed_rrset_data* fd, *dd;
710 	struct ub_packed_rrset_key* fk, *dk;
711 	for(i=0; i<dest->rrset_count; i++) {
712 		fk = from->rrsets[i];
713 		dk = dest->rrsets[i];
714 		fd = (struct packed_rrset_data*)fk->entry.data;
715 		dk->entry.hash = fk->entry.hash;
716 		dk->rk = fk->rk;
717 		if(region) {
718 			dk->id = fk->id;
719 			dk->rk.dname = (uint8_t*)regional_alloc_init(region,
720 				fk->rk.dname, fk->rk.dname_len);
721 		} else
722 			dk->rk.dname = (uint8_t*)memdup(fk->rk.dname,
723 				fk->rk.dname_len);
724 		if(!dk->rk.dname)
725 			return 0;
726 		s = packed_rrset_sizeof(fd);
727 		if(region)
728 			dd = (struct packed_rrset_data*)regional_alloc_init(
729 				region, fd, s);
730 		else	dd = (struct packed_rrset_data*)memdup(fd, s);
731 		if(!dd)
732 			return 0;
733 		packed_rrset_ptr_fixup(dd);
734 		dk->entry.data = (void*)dd;
735 	}
736 	return 1;
737 }
738 
739 struct reply_info*
740 reply_info_copy(struct reply_info* rep, struct alloc_cache* alloc,
741 	struct regional* region)
742 {
743 	struct reply_info* cp;
744 	cp = construct_reply_info_base(region, rep->flags, rep->qdcount,
745 		rep->ttl, rep->prefetch_ttl, rep->serve_expired_ttl,
746 		rep->an_numrrsets, rep->ns_numrrsets, rep->ar_numrrsets,
747 		rep->rrset_count, rep->security);
748 	if(!cp)
749 		return NULL;
750 	/* allocate ub_key structures special or not */
751 	if(!reply_info_alloc_rrset_keys(cp, alloc, region)) {
752 		if(!region)
753 			reply_info_parsedelete(cp, alloc);
754 		return NULL;
755 	}
756 	if(!repinfo_copy_rrsets(cp, rep, region)) {
757 		if(!region)
758 			reply_info_parsedelete(cp, alloc);
759 		return NULL;
760 	}
761 	return cp;
762 }
763 
764 uint8_t*
765 reply_find_final_cname_target(struct query_info* qinfo, struct reply_info* rep)
766 {
767 	uint8_t* sname = qinfo->qname;
768 	size_t snamelen = qinfo->qname_len;
769 	size_t i;
770 	for(i=0; i<rep->an_numrrsets; i++) {
771 		struct ub_packed_rrset_key* s = rep->rrsets[i];
772 		/* follow CNAME chain (if any) */
773 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
774 			ntohs(s->rk.rrset_class) == qinfo->qclass &&
775 			snamelen == s->rk.dname_len &&
776 			query_dname_compare(sname, s->rk.dname) == 0) {
777 			get_cname_target(s, &sname, &snamelen);
778 		}
779 	}
780 	if(sname != qinfo->qname)
781 		return sname;
782 	return NULL;
783 }
784 
785 struct ub_packed_rrset_key*
786 reply_find_answer_rrset(struct query_info* qinfo, struct reply_info* rep)
787 {
788 	uint8_t* sname = qinfo->qname;
789 	size_t snamelen = qinfo->qname_len;
790 	size_t i;
791 	for(i=0; i<rep->an_numrrsets; i++) {
792 		struct ub_packed_rrset_key* s = rep->rrsets[i];
793 		/* first match type, for query of qtype cname */
794 		if(ntohs(s->rk.type) == qinfo->qtype &&
795 			ntohs(s->rk.rrset_class) == qinfo->qclass &&
796 			snamelen == s->rk.dname_len &&
797 			query_dname_compare(sname, s->rk.dname) == 0) {
798 			return s;
799 		}
800 		/* follow CNAME chain (if any) */
801 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
802 			ntohs(s->rk.rrset_class) == qinfo->qclass &&
803 			snamelen == s->rk.dname_len &&
804 			query_dname_compare(sname, s->rk.dname) == 0) {
805 			get_cname_target(s, &sname, &snamelen);
806 		}
807 	}
808 	return NULL;
809 }
810 
811 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
812 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
813 {
814 	size_t i;
815 	for(i=0; i<rep->an_numrrsets; i++) {
816 		struct ub_packed_rrset_key* s = rep->rrsets[i];
817 		if(ntohs(s->rk.type) == type &&
818 			ntohs(s->rk.rrset_class) == dclass &&
819 			namelen == s->rk.dname_len &&
820 			query_dname_compare(name, s->rk.dname) == 0) {
821 			return s;
822 		}
823 	}
824 	return NULL;
825 }
826 
827 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
828 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
829 {
830 	size_t i;
831 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
832 		struct ub_packed_rrset_key* s = rep->rrsets[i];
833 		if(ntohs(s->rk.type) == type &&
834 			ntohs(s->rk.rrset_class) == dclass &&
835 			namelen == s->rk.dname_len &&
836 			query_dname_compare(name, s->rk.dname) == 0) {
837 			return s;
838 		}
839 	}
840 	return NULL;
841 }
842 
843 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
844 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
845 {
846 	size_t i;
847 	for(i=0; i<rep->rrset_count; i++) {
848 		struct ub_packed_rrset_key* s = rep->rrsets[i];
849 		if(ntohs(s->rk.type) == type &&
850 			ntohs(s->rk.rrset_class) == dclass &&
851 			namelen == s->rk.dname_len &&
852 			query_dname_compare(name, s->rk.dname) == 0) {
853 			return s;
854 		}
855 	}
856 	return NULL;
857 }
858 
859 void
860 log_dns_msg(const char* str, struct query_info* qinfo, struct reply_info* rep)
861 {
862 	/* not particularly fast but flexible, make wireformat and print */
863 	sldns_buffer* buf = sldns_buffer_new(65535);
864 	struct regional* region = regional_create();
865 	if(!(buf && region)) {
866 		log_err("%s: log_dns_msg: out of memory", str);
867 		sldns_buffer_free(buf);
868 		regional_destroy(region);
869 		return;
870 	}
871 	if(!reply_info_encode(qinfo, rep, 0, rep->flags, buf, 0,
872 		region, 65535, 1, 0)) {
873 		log_err("%s: log_dns_msg: out of memory", str);
874 	} else {
875 		char* s = sldns_wire2str_pkt(sldns_buffer_begin(buf),
876 			sldns_buffer_limit(buf));
877 		if(!s) {
878 			log_info("%s: log_dns_msg: ldns tostr failed", str);
879 		} else {
880 			log_info("%s %s", str, s);
881 		}
882 		free(s);
883 	}
884 	sldns_buffer_free(buf);
885 	regional_destroy(region);
886 }
887 
888 void
889 log_reply_info(enum verbosity_value v, struct query_info *qinf,
890 	struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
891 	int cached, struct sldns_buffer *rmsg)
892 {
893 	char qname_buf[LDNS_MAX_DOMAINLEN+1];
894 	char clientip_buf[128];
895 	char rcode_buf[16];
896 	char type_buf[16];
897 	char class_buf[16];
898 	size_t pktlen;
899 	uint16_t rcode = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(rmsg, 2));
900 
901 	if(verbosity < v)
902 	  return;
903 
904 	sldns_wire2str_rcode_buf((int)rcode, rcode_buf, sizeof(rcode_buf));
905 	addr_to_str(addr, addrlen, clientip_buf, sizeof(clientip_buf));
906 	if(rcode == LDNS_RCODE_FORMERR)
907 	{
908 		if(LOG_TAG_QUERYREPLY)
909 			log_reply("%s - - - %s - - - ", clientip_buf, rcode_buf);
910 		else	log_info("%s - - - %s - - - ", clientip_buf, rcode_buf);
911 	} else {
912 		if(qinf->qname)
913 			dname_str(qinf->qname, qname_buf);
914 		else	snprintf(qname_buf, sizeof(qname_buf), "null");
915 		pktlen = sldns_buffer_limit(rmsg);
916 		sldns_wire2str_type_buf(qinf->qtype, type_buf, sizeof(type_buf));
917 		sldns_wire2str_class_buf(qinf->qclass, class_buf, sizeof(class_buf));
918 		if(LOG_TAG_QUERYREPLY)
919 		     log_reply("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d",
920 			clientip_buf, qname_buf, type_buf, class_buf,
921 			rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen);
922 		else log_info("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d",
923 			clientip_buf, qname_buf, type_buf, class_buf,
924 			rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen);
925 	}
926 }
927 
928 void
929 log_query_info(enum verbosity_value v, const char* str,
930 	struct query_info* qinf)
931 {
932 	log_nametypeclass(v, str, qinf->qname, qinf->qtype, qinf->qclass);
933 }
934 
935 int
936 reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep)
937 {
938 	/* check only answer section rrs for matching cname chain.
939 	 * the cache may return changed rdata, but owner names are untouched.*/
940 	size_t i;
941 	uint8_t* sname = qinfo->qname;
942 	size_t snamelen = qinfo->qname_len;
943 	for(i=0; i<rep->an_numrrsets; i++) {
944 		uint16_t t = ntohs(rep->rrsets[i]->rk.type);
945 		if(t == LDNS_RR_TYPE_DNAME)
946 			continue; /* skip dnames; note TTL 0 not cached */
947 		/* verify that owner matches current sname */
948 		if(query_dname_compare(sname, rep->rrsets[i]->rk.dname) != 0){
949 			/* cname chain broken */
950 			return 0;
951 		}
952 		/* if this is a cname; move on */
953 		if(t == LDNS_RR_TYPE_CNAME) {
954 			get_cname_target(rep->rrsets[i], &sname, &snamelen);
955 		}
956 	}
957 	return 1;
958 }
959 
960 int
961 reply_all_rrsets_secure(struct reply_info* rep)
962 {
963 	size_t i;
964 	for(i=0; i<rep->rrset_count; i++) {
965 		if( ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
966 			->security != sec_status_secure )
967 		return 0;
968 	}
969 	return 1;
970 }
971 
972 struct reply_info*
973 parse_reply_in_temp_region(sldns_buffer* pkt, struct regional* region,
974 	struct query_info* qi)
975 {
976 	struct reply_info* rep;
977 	struct msg_parse* msg;
978 	if(!(msg = regional_alloc(region, sizeof(*msg)))) {
979 		return NULL;
980 	}
981 	memset(msg, 0, sizeof(*msg));
982 	sldns_buffer_set_position(pkt, 0);
983 	if(parse_packet(pkt, msg, region) != 0){
984 		return 0;
985 	}
986 	if(!parse_create_msg(pkt, msg, NULL, qi, &rep, region)) {
987 		return 0;
988 	}
989 	return rep;
990 }
991 
992 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
993 	uint8_t* data, struct regional* region)
994 {
995 	struct edns_option** prevp;
996 	struct edns_option* opt;
997 
998 	/* allocate new element */
999 	opt = (struct edns_option*)regional_alloc(region, sizeof(*opt));
1000 	if(!opt)
1001 		return 0;
1002 	opt->next = NULL;
1003 	opt->opt_code = code;
1004 	opt->opt_len = len;
1005 	opt->opt_data = NULL;
1006 	if(len > 0) {
1007 		opt->opt_data = regional_alloc_init(region, data, len);
1008 		if(!opt->opt_data)
1009 			return 0;
1010 	}
1011 
1012 	/* append at end of list */
1013 	prevp = list;
1014 	while(*prevp != NULL) {
1015 		prevp = &((*prevp)->next);
1016 	}
1017 	*prevp = opt;
1018 	return 1;
1019 }
1020 
1021 int edns_opt_list_remove(struct edns_option** list, uint16_t code)
1022 {
1023 	/* The list should already be allocated in a region. Freeing the
1024 	 * allocated space in a region is not possible. We just unlink the
1025 	 * required elements and they will be freed together with the region. */
1026 
1027 	struct edns_option* prev;
1028 	struct edns_option* curr;
1029 	if(!list || !(*list)) return 0;
1030 
1031 	/* Unlink and repoint if the element(s) are first in list */
1032 	while(list && *list && (*list)->opt_code == code) {
1033 		*list = (*list)->next;
1034 	}
1035 
1036 	if(!list || !(*list)) return 1;
1037 	/* Unlink elements and reattach the list */
1038 	prev = *list;
1039 	curr = (*list)->next;
1040 	while(curr != NULL) {
1041 		if(curr->opt_code == code) {
1042 			prev->next = curr->next;
1043 			curr = curr->next;
1044 		} else {
1045 			prev = curr;
1046 			curr = curr->next;
1047 		}
1048 	}
1049 	return 1;
1050 }
1051 
1052 static int inplace_cb_reply_call_generic(
1053     struct inplace_cb* callback_list, enum inplace_cb_list_type type,
1054 	struct query_info* qinfo, struct module_qstate* qstate,
1055 	struct reply_info* rep, int rcode, struct edns_data* edns,
1056 	struct comm_reply* repinfo, struct regional* region,
1057 	struct timeval* start_time)
1058 {
1059 	struct inplace_cb* cb;
1060 	struct edns_option* opt_list_out = NULL;
1061 #if defined(EXPORT_ALL_SYMBOLS)
1062 	(void)type; /* param not used when fptr_ok disabled */
1063 #endif
1064 	if(qstate)
1065 		opt_list_out = qstate->edns_opts_front_out;
1066 	for(cb=callback_list; cb; cb=cb->next) {
1067 		fptr_ok(fptr_whitelist_inplace_cb_reply_generic(
1068 			(inplace_cb_reply_func_type*)cb->cb, type));
1069 		(void)(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep,
1070 			rcode, edns, &opt_list_out, repinfo, region, start_time, cb->id, cb->cb_arg);
1071 	}
1072 	edns->opt_list_inplace_cb_out = opt_list_out;
1073 	return 1;
1074 }
1075 
1076 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
1077 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
1078 	struct edns_data* edns, struct comm_reply* repinfo, struct regional* region,
1079 	struct timeval* start_time)
1080 {
1081 	return inplace_cb_reply_call_generic(
1082 		env->inplace_cb_lists[inplace_cb_reply], inplace_cb_reply, qinfo,
1083 		qstate, rep, rcode, edns, repinfo, region, start_time);
1084 }
1085 
1086 int inplace_cb_reply_cache_call(struct module_env* env,
1087 	struct query_info* qinfo, struct module_qstate* qstate,
1088 	struct reply_info* rep, int rcode, struct edns_data* edns,
1089 	struct comm_reply* repinfo, struct regional* region,
1090 	struct timeval* start_time)
1091 {
1092 	return inplace_cb_reply_call_generic(
1093 		env->inplace_cb_lists[inplace_cb_reply_cache], inplace_cb_reply_cache,
1094 		qinfo, qstate, rep, rcode, edns, repinfo, region, start_time);
1095 }
1096 
1097 int inplace_cb_reply_local_call(struct module_env* env,
1098 	struct query_info* qinfo, struct module_qstate* qstate,
1099 	struct reply_info* rep, int rcode, struct edns_data* edns,
1100 	struct comm_reply* repinfo, struct regional* region,
1101 	struct timeval* start_time)
1102 {
1103 	return inplace_cb_reply_call_generic(
1104 		env->inplace_cb_lists[inplace_cb_reply_local], inplace_cb_reply_local,
1105 		qinfo, qstate, rep, rcode, edns, repinfo, region, start_time);
1106 }
1107 
1108 int inplace_cb_reply_servfail_call(struct module_env* env,
1109 	struct query_info* qinfo, struct module_qstate* qstate,
1110 	struct reply_info* rep, int rcode, struct edns_data* edns,
1111 	struct comm_reply* repinfo, struct regional* region,
1112 	struct timeval* start_time)
1113 {
1114 	/* We are going to servfail. Remove any potential edns options. */
1115 	if(qstate)
1116 		qstate->edns_opts_front_out = NULL;
1117 	return inplace_cb_reply_call_generic(
1118 		env->inplace_cb_lists[inplace_cb_reply_servfail],
1119 		inplace_cb_reply_servfail, qinfo, qstate, rep, rcode, edns, repinfo,
1120 		region, start_time);
1121 }
1122 
1123 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
1124 	uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
1125 	uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
1126 	struct regional* region)
1127 {
1128 	struct inplace_cb* cb = env->inplace_cb_lists[inplace_cb_query];
1129 	for(; cb; cb=cb->next) {
1130 		fptr_ok(fptr_whitelist_inplace_cb_query(
1131 			(inplace_cb_query_func_type*)cb->cb));
1132 		(void)(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags,
1133 			qstate, addr, addrlen, zone, zonelen, region,
1134 			cb->id, cb->cb_arg);
1135 	}
1136 	return 1;
1137 }
1138 
1139 int inplace_cb_edns_back_parsed_call(struct module_env* env,
1140 	struct module_qstate* qstate)
1141 {
1142 	struct inplace_cb* cb =
1143 		env->inplace_cb_lists[inplace_cb_edns_back_parsed];
1144 	for(; cb; cb=cb->next) {
1145 		fptr_ok(fptr_whitelist_inplace_cb_edns_back_parsed(
1146 			(inplace_cb_edns_back_parsed_func_type*)cb->cb));
1147 		(void)(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate,
1148 			cb->id, cb->cb_arg);
1149 	}
1150 	return 1;
1151 }
1152 
1153 int inplace_cb_query_response_call(struct module_env* env,
1154 	struct module_qstate* qstate, struct dns_msg* response) {
1155 	struct inplace_cb* cb =
1156 		env->inplace_cb_lists[inplace_cb_query_response];
1157 	for(; cb; cb=cb->next) {
1158 		fptr_ok(fptr_whitelist_inplace_cb_query_response(
1159 			(inplace_cb_query_response_func_type*)cb->cb));
1160 		(void)(*(inplace_cb_query_response_func_type*)cb->cb)(qstate,
1161 			response, cb->id, cb->cb_arg);
1162 	}
1163 	return 1;
1164 }
1165 
1166 struct edns_option* edns_opt_copy_region(struct edns_option* list,
1167         struct regional* region)
1168 {
1169 	struct edns_option* result = NULL, *cur = NULL, *s;
1170 	while(list) {
1171 		/* copy edns option structure */
1172 		s = regional_alloc_init(region, list, sizeof(*list));
1173 		if(!s) return NULL;
1174 		s->next = NULL;
1175 
1176 		/* copy option data */
1177 		if(s->opt_data) {
1178 			s->opt_data = regional_alloc_init(region, s->opt_data,
1179 				s->opt_len);
1180 			if(!s->opt_data)
1181 				return NULL;
1182 		}
1183 
1184 		/* link into list */
1185 		if(cur)
1186 			cur->next = s;
1187 		else	result = s;
1188 		cur = s;
1189 
1190 		/* examine next element */
1191 		list = list->next;
1192 	}
1193 	return result;
1194 }
1195 
1196 int edns_opt_compare(struct edns_option* p, struct edns_option* q)
1197 {
1198 	if(!p && !q) return 0;
1199 	if(!p) return -1;
1200 	if(!q) return 1;
1201 	log_assert(p && q);
1202 	if(p->opt_code != q->opt_code)
1203 		return (int)q->opt_code - (int)p->opt_code;
1204 	if(p->opt_len != q->opt_len)
1205 		return (int)q->opt_len - (int)p->opt_len;
1206 	if(p->opt_len != 0)
1207 		return memcmp(p->opt_data, q->opt_data, p->opt_len);
1208 	return 0;
1209 }
1210 
1211 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q)
1212 {
1213 	int r;
1214 	while(p && q) {
1215 		r = edns_opt_compare(p, q);
1216 		if(r != 0)
1217 			return r;
1218 		p = p->next;
1219 		q = q->next;
1220 	}
1221 	if(p || q) {
1222 		/* uneven length lists */
1223 		if(p) return 1;
1224 		if(q) return -1;
1225 	}
1226 	return 0;
1227 }
1228 
1229 void edns_opt_list_free(struct edns_option* list)
1230 {
1231 	struct edns_option* n;
1232 	while(list) {
1233 		free(list->opt_data);
1234 		n = list->next;
1235 		free(list);
1236 		list = n;
1237 	}
1238 }
1239 
1240 struct edns_option* edns_opt_copy_alloc(struct edns_option* list)
1241 {
1242 	struct edns_option* result = NULL, *cur = NULL, *s;
1243 	while(list) {
1244 		/* copy edns option structure */
1245 		s = memdup(list, sizeof(*list));
1246 		if(!s) {
1247 			edns_opt_list_free(result);
1248 			return NULL;
1249 		}
1250 		s->next = NULL;
1251 
1252 		/* copy option data */
1253 		if(s->opt_data) {
1254 			s->opt_data = memdup(s->opt_data, s->opt_len);
1255 			if(!s->opt_data) {
1256 				free(s);
1257 				edns_opt_list_free(result);
1258 				return NULL;
1259 			}
1260 		}
1261 
1262 		/* link into list */
1263 		if(cur)
1264 			cur->next = s;
1265 		else	result = s;
1266 		cur = s;
1267 
1268 		/* examine next element */
1269 		list = list->next;
1270 	}
1271 	return result;
1272 }
1273 
1274 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code)
1275 {
1276 	struct edns_option* p;
1277 	for(p=list; p; p=p->next) {
1278 		if(p->opt_code == code)
1279 			return p;
1280 	}
1281 	return NULL;
1282 }
1283