xref: /freebsd/contrib/unbound/util/data/msgencode.c (revision 5ab1c5846ff41be24b1f6beb0317bf8258cd4409)
1 /*
2  * util/data/msgencode.c - Encode DNS messages, queries and replies.
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 routines to encode DNS messages.
40  */
41 
42 #include "config.h"
43 #include "util/data/msgencode.h"
44 #include "util/data/msgreply.h"
45 #include "util/data/msgparse.h"
46 #include "util/data/dname.h"
47 #include "util/log.h"
48 #include "util/regional.h"
49 #include "util/net_help.h"
50 #include "sldns/sbuffer.h"
51 #include "services/localzone.h"
52 
53 #ifdef HAVE_TIME_H
54 #include <time.h>
55 #endif
56 #include <sys/time.h>
57 
58 /** return code that means the function ran out of memory. negative so it does
59  * not conflict with DNS rcodes. */
60 #define RETVAL_OUTMEM	-2
61 /** return code that means the data did not fit (completely) in the packet */
62 #define RETVAL_TRUNC	-4
63 /** return code that means all is peachy keen. Equal to DNS rcode NOERROR */
64 #define RETVAL_OK	0
65 
66 /**
67  * Data structure to help domain name compression in outgoing messages.
68  * A tree of dnames and their offsets in the packet is kept.
69  * It is kept sorted, not canonical, but by label at least, so that after
70  * a lookup of a name you know its closest match, and the parent from that
71  * closest match. These are possible compression targets.
72  *
73  * It is a binary tree, not a rbtree or balanced tree, as the effort
74  * of keeping it balanced probably outweighs usefulness (given typical
75  * DNS packet size).
76  */
77 struct compress_tree_node {
78 	/** left node in tree, all smaller to this */
79 	struct compress_tree_node* left;
80 	/** right node in tree, all larger than this */
81 	struct compress_tree_node* right;
82 
83 	/** the parent node - not for tree, but zone parent. One less label */
84 	struct compress_tree_node* parent;
85 	/** the domain name for this node. Pointer to uncompressed memory. */
86 	uint8_t* dname;
87 	/** number of labels in domain name, kept to help compare func. */
88 	int labs;
89 	/** offset in packet that points to this dname */
90 	size_t offset;
91 };
92 
93 /**
94  * Find domain name in tree, returns exact and closest match.
95  * @param tree: root of tree.
96  * @param dname: pointer to uncompressed dname.
97  * @param labs: number of labels in domain name.
98  * @param match: closest or exact match.
99  *	guaranteed to be smaller or equal to the sought dname.
100  *	can be null if the tree is empty.
101  * @param matchlabels: number of labels that match with closest match.
102  *	can be zero is there is no match.
103  * @param insertpt: insert location for dname, if not found.
104  * @return: 0 if no exact match.
105  */
106 static int
107 compress_tree_search(struct compress_tree_node** tree, uint8_t* dname,
108 	int labs, struct compress_tree_node** match, int* matchlabels,
109 	struct compress_tree_node*** insertpt)
110 {
111 	int c, n, closen=0;
112 	struct compress_tree_node* p = *tree;
113 	struct compress_tree_node* close = 0;
114 	struct compress_tree_node** prev = tree;
115 	while(p) {
116 		if((c = dname_lab_cmp(dname, labs, p->dname, p->labs, &n))
117 			== 0) {
118 			*matchlabels = n;
119 			*match = p;
120 			return 1;
121 		}
122 		if(c<0) {
123 			prev = &p->left;
124 			p = p->left;
125 		} else	{
126 			closen = n;
127 			close = p; /* p->dname is smaller than dname */
128 			prev = &p->right;
129 			p = p->right;
130 		}
131 	}
132 	*insertpt = prev;
133 	*matchlabels = closen;
134 	*match = close;
135 	return 0;
136 }
137 
138 /**
139  * Lookup a domain name in compression tree.
140  * @param tree: root of tree (not the node with '.').
141  * @param dname: pointer to uncompressed dname.
142  * @param labs: number of labels in domain name.
143  * @param insertpt: insert location for dname, if not found.
144  * @return: 0 if not found or compress treenode with best compression.
145  */
146 static struct compress_tree_node*
147 compress_tree_lookup(struct compress_tree_node** tree, uint8_t* dname,
148 	int labs, struct compress_tree_node*** insertpt)
149 {
150 	struct compress_tree_node* p;
151 	int m;
152 	if(labs <= 1)
153 		return 0; /* do not compress root node */
154 	if(compress_tree_search(tree, dname, labs, &p, &m, insertpt)) {
155 		/* exact match */
156 		return p;
157 	}
158 	/* return some ancestor of p that compresses well. */
159 	if(m>1) {
160 		/* www.example.com. (labs=4) matched foo.example.com.(labs=4)
161 		 * then matchcount = 3. need to go up. */
162 		while(p && p->labs > m)
163 			p = p->parent;
164 		return p;
165 	}
166 	return 0;
167 }
168 
169 /**
170  * Create node for domain name compression tree.
171  * @param dname: pointer to uncompressed dname (stored in tree).
172  * @param labs: number of labels in dname.
173  * @param offset: offset into packet for dname.
174  * @param region: how to allocate memory for new node.
175  * @return new node or 0 on malloc failure.
176  */
177 static struct compress_tree_node*
178 compress_tree_newnode(uint8_t* dname, int labs, size_t offset,
179 	struct regional* region)
180 {
181 	struct compress_tree_node* n = (struct compress_tree_node*)
182 		regional_alloc(region, sizeof(struct compress_tree_node));
183 	if(!n) return 0;
184 	n->left = 0;
185 	n->right = 0;
186 	n->parent = 0;
187 	n->dname = dname;
188 	n->labs = labs;
189 	n->offset = offset;
190 	return n;
191 }
192 
193 /**
194  * Store domain name and ancestors into compression tree.
195  * @param dname: pointer to uncompressed dname (stored in tree).
196  * @param labs: number of labels in dname.
197  * @param offset: offset into packet for dname.
198  * @param region: how to allocate memory for new node.
199  * @param closest: match from previous lookup, used to compress dname.
200  *	may be NULL if no previous match.
201  *	if the tree has an ancestor of dname already, this must be it.
202  * @param insertpt: where to insert the dname in tree.
203  * @return: 0 on memory error.
204  */
205 static int
206 compress_tree_store(uint8_t* dname, int labs, size_t offset,
207 	struct regional* region, struct compress_tree_node* closest,
208 	struct compress_tree_node** insertpt)
209 {
210 	uint8_t lablen;
211 	struct compress_tree_node* newnode;
212 	struct compress_tree_node* prevnode = NULL;
213 	int uplabs = labs-1; /* does not store root in tree */
214 	if(closest) uplabs = labs - closest->labs;
215 	log_assert(uplabs >= 0);
216 	/* algorithms builds up a vine of dname-labels to hang into tree */
217 	while(uplabs--) {
218 		if(offset > PTR_MAX_OFFSET) {
219 			/* insertion failed, drop vine */
220 			return 1; /* compression pointer no longer useful */
221 		}
222 		if(!(newnode = compress_tree_newnode(dname, labs, offset,
223 			region))) {
224 			/* insertion failed, drop vine */
225 			return 0;
226 		}
227 
228 		if(prevnode) {
229 			/* chain nodes together, last one has one label more,
230 			 * so is larger than newnode, thus goes right. */
231 			newnode->right = prevnode;
232 			prevnode->parent = newnode;
233 		}
234 
235 		/* next label */
236 		lablen = *dname++;
237 		dname += lablen;
238 		offset += lablen+1;
239 		prevnode = newnode;
240 		labs--;
241 	}
242 	/* if we have a vine, hang the vine into the tree */
243 	if(prevnode) {
244 		*insertpt = prevnode;
245 		prevnode->parent = closest;
246 	}
247 	return 1;
248 }
249 
250 /** compress a domain name */
251 static int
252 write_compressed_dname(sldns_buffer* pkt, uint8_t* dname, int labs,
253 	struct compress_tree_node* p)
254 {
255 	/* compress it */
256 	int labcopy = labs - p->labs;
257 	uint8_t lablen;
258 	uint16_t ptr;
259 
260 	if(labs == 1) {
261 		/* write root label */
262 		if(sldns_buffer_remaining(pkt) < 1)
263 			return 0;
264 		sldns_buffer_write_u8(pkt, 0);
265 		return 1;
266 	}
267 
268 	/* copy the first couple of labels */
269 	while(labcopy--) {
270 		lablen = *dname++;
271 		if(sldns_buffer_remaining(pkt) < (size_t)lablen+1)
272 			return 0;
273 		sldns_buffer_write_u8(pkt, lablen);
274 		sldns_buffer_write(pkt, dname, lablen);
275 		dname += lablen;
276 	}
277 	/* insert compression ptr */
278 	if(sldns_buffer_remaining(pkt) < 2)
279 		return 0;
280 	ptr = PTR_CREATE(p->offset);
281 	sldns_buffer_write_u16(pkt, ptr);
282 	return 1;
283 }
284 
285 /** compress owner name of RR, return RETVAL_OUTMEM RETVAL_TRUNC */
286 static int
287 compress_owner(struct ub_packed_rrset_key* key, sldns_buffer* pkt,
288 	struct regional* region, struct compress_tree_node** tree,
289 	size_t owner_pos, uint16_t* owner_ptr, int owner_labs)
290 {
291 	struct compress_tree_node* p;
292 	struct compress_tree_node** insertpt = NULL;
293 	if(!*owner_ptr) {
294 		/* compress first time dname */
295 		if((p = compress_tree_lookup(tree, key->rk.dname,
296 			owner_labs, &insertpt))) {
297 			if(p->labs == owner_labs)
298 				/* avoid ptr chains, since some software is
299 				 * not capable of decoding ptr after a ptr. */
300 				*owner_ptr = htons(PTR_CREATE(p->offset));
301 			if(!write_compressed_dname(pkt, key->rk.dname,
302 				owner_labs, p))
303 				return RETVAL_TRUNC;
304 			/* check if typeclass+4 ttl + rdatalen is available */
305 			if(sldns_buffer_remaining(pkt) < 4+4+2)
306 				return RETVAL_TRUNC;
307 		} else {
308 			/* no compress */
309 			if(sldns_buffer_remaining(pkt) < key->rk.dname_len+4+4+2)
310 				return RETVAL_TRUNC;
311 			sldns_buffer_write(pkt, key->rk.dname,
312 				key->rk.dname_len);
313 			if(owner_pos <= PTR_MAX_OFFSET)
314 				*owner_ptr = htons(PTR_CREATE(owner_pos));
315 		}
316 		if(!compress_tree_store(key->rk.dname, owner_labs,
317 			owner_pos, region, p, insertpt))
318 			return RETVAL_OUTMEM;
319 	} else {
320 		/* always compress 2nd-further RRs in RRset */
321 		if(owner_labs == 1) {
322 			if(sldns_buffer_remaining(pkt) < 1+4+4+2)
323 				return RETVAL_TRUNC;
324 			sldns_buffer_write_u8(pkt, 0);
325 		} else {
326 			if(sldns_buffer_remaining(pkt) < 2+4+4+2)
327 				return RETVAL_TRUNC;
328 			sldns_buffer_write(pkt, owner_ptr, 2);
329 		}
330 	}
331 	return RETVAL_OK;
332 }
333 
334 /** compress any domain name to the packet, return RETVAL_* */
335 static int
336 compress_any_dname(uint8_t* dname, sldns_buffer* pkt, int labs,
337 	struct regional* region, struct compress_tree_node** tree)
338 {
339 	struct compress_tree_node* p;
340 	struct compress_tree_node** insertpt = NULL;
341 	size_t pos = sldns_buffer_position(pkt);
342 	if((p = compress_tree_lookup(tree, dname, labs, &insertpt))) {
343 		if(!write_compressed_dname(pkt, dname, labs, p))
344 			return RETVAL_TRUNC;
345 	} else {
346 		if(!dname_buffer_write(pkt, dname))
347 			return RETVAL_TRUNC;
348 	}
349 	if(!compress_tree_store(dname, labs, pos, region, p, insertpt))
350 		return RETVAL_OUTMEM;
351 	return RETVAL_OK;
352 }
353 
354 /** return true if type needs domain name compression in rdata */
355 static const sldns_rr_descriptor*
356 type_rdata_compressable(struct ub_packed_rrset_key* key)
357 {
358 	uint16_t t = ntohs(key->rk.type);
359 	if(sldns_rr_descript(t) &&
360 		sldns_rr_descript(t)->_compress == LDNS_RR_COMPRESS)
361 		return sldns_rr_descript(t);
362 	return 0;
363 }
364 
365 /** compress domain names in rdata, return RETVAL_* */
366 static int
367 compress_rdata(sldns_buffer* pkt, uint8_t* rdata, size_t todolen,
368 	struct regional* region, struct compress_tree_node** tree,
369 	const sldns_rr_descriptor* desc)
370 {
371 	int labs, r, rdf = 0;
372 	size_t dname_len, len, pos = sldns_buffer_position(pkt);
373 	uint8_t count = desc->_dname_count;
374 
375 	sldns_buffer_skip(pkt, 2); /* rdata len fill in later */
376 	/* space for rdatalen checked for already */
377 	rdata += 2;
378 	todolen -= 2;
379 	while(todolen > 0 && count) {
380 		switch(desc->_wireformat[rdf]) {
381 		case LDNS_RDF_TYPE_DNAME:
382 			labs = dname_count_size_labels(rdata, &dname_len);
383 			if((r=compress_any_dname(rdata, pkt, labs, region,
384 				tree)) != RETVAL_OK)
385 				return r;
386 			rdata += dname_len;
387 			todolen -= dname_len;
388 			count--;
389 			len = 0;
390 			break;
391 		case LDNS_RDF_TYPE_STR:
392 			len = *rdata + 1;
393 			break;
394 		default:
395 			len = get_rdf_size(desc->_wireformat[rdf]);
396 		}
397 		if(len) {
398 			/* copy over */
399 			if(sldns_buffer_remaining(pkt) < len)
400 				return RETVAL_TRUNC;
401 			sldns_buffer_write(pkt, rdata, len);
402 			todolen -= len;
403 			rdata += len;
404 		}
405 		rdf++;
406 	}
407 	/* copy remainder */
408 	if(todolen > 0) {
409 		if(sldns_buffer_remaining(pkt) < todolen)
410 			return RETVAL_TRUNC;
411 		sldns_buffer_write(pkt, rdata, todolen);
412 	}
413 
414 	/* set rdata len */
415 	sldns_buffer_write_u16_at(pkt, pos, sldns_buffer_position(pkt)-pos-2);
416 	return RETVAL_OK;
417 }
418 
419 /** Returns true if RR type should be included */
420 static int
421 rrset_belongs_in_reply(sldns_pkt_section s, uint16_t rrtype, uint16_t qtype,
422 	int dnssec)
423 {
424 	if(dnssec)
425 		return 1;
426 	/* skip non DNSSEC types, except if directly queried for */
427 	if(s == LDNS_SECTION_ANSWER) {
428 		if(qtype == LDNS_RR_TYPE_ANY || qtype == rrtype)
429 			return 1;
430 	}
431 	/* check DNSSEC-ness */
432 	switch(rrtype) {
433 		case LDNS_RR_TYPE_SIG:
434 		case LDNS_RR_TYPE_KEY:
435 		case LDNS_RR_TYPE_NXT:
436 		case LDNS_RR_TYPE_DS:
437 		case LDNS_RR_TYPE_RRSIG:
438 		case LDNS_RR_TYPE_NSEC:
439 		case LDNS_RR_TYPE_DNSKEY:
440 		case LDNS_RR_TYPE_NSEC3:
441 		case LDNS_RR_TYPE_NSEC3PARAMS:
442 			return 0;
443 	}
444 	return 1;
445 }
446 
447 /** store rrset in buffer in wireformat, return RETVAL_* */
448 static int
449 packed_rrset_encode(struct ub_packed_rrset_key* key, sldns_buffer* pkt,
450 	uint16_t* num_rrs, time_t timenow, struct regional* region,
451 	int do_data, int do_sig, struct compress_tree_node** tree,
452 	sldns_pkt_section s, uint16_t qtype, int dnssec, size_t rr_offset)
453 {
454 	size_t i, j, owner_pos;
455 	int r, owner_labs;
456 	uint16_t owner_ptr = 0;
457 	struct packed_rrset_data* data = (struct packed_rrset_data*)
458 		key->entry.data;
459 
460 	/* does this RR type belong in the answer? */
461 	if(!rrset_belongs_in_reply(s, ntohs(key->rk.type), qtype, dnssec))
462 		return RETVAL_OK;
463 
464 	owner_labs = dname_count_labels(key->rk.dname);
465 	owner_pos = sldns_buffer_position(pkt);
466 
467 	/* For an rrset with a fixed TTL, use the rrset's TTL as given */
468 	if((key->rk.flags & PACKED_RRSET_FIXEDTTL) != 0)
469 		timenow = 0;
470 
471 	if(do_data) {
472 		const sldns_rr_descriptor* c = type_rdata_compressable(key);
473 		for(i=0; i<data->count; i++) {
474 			/* rrset roundrobin */
475 			j = (i + rr_offset) % data->count;
476 			if((r=compress_owner(key, pkt, region, tree,
477 				owner_pos, &owner_ptr, owner_labs))
478 				!= RETVAL_OK)
479 				return r;
480 			sldns_buffer_write(pkt, &key->rk.type, 2);
481 			sldns_buffer_write(pkt, &key->rk.rrset_class, 2);
482 			if(data->rr_ttl[j] < timenow)
483 				sldns_buffer_write_u32(pkt, 0);
484 			else 	sldns_buffer_write_u32(pkt,
485 					data->rr_ttl[j]-timenow);
486 			if(c) {
487 				if((r=compress_rdata(pkt, data->rr_data[j],
488 					data->rr_len[j], region, tree, c))
489 					!= RETVAL_OK)
490 					return r;
491 			} else {
492 				if(sldns_buffer_remaining(pkt) < data->rr_len[j])
493 					return RETVAL_TRUNC;
494 				sldns_buffer_write(pkt, data->rr_data[j],
495 					data->rr_len[j]);
496 			}
497 		}
498 	}
499 	/* insert rrsigs */
500 	if(do_sig && dnssec) {
501 		size_t total = data->count+data->rrsig_count;
502 		for(i=data->count; i<total; i++) {
503 			if(owner_ptr && owner_labs != 1) {
504 				if(sldns_buffer_remaining(pkt) <
505 					2+4+4+data->rr_len[i])
506 					return RETVAL_TRUNC;
507 				sldns_buffer_write(pkt, &owner_ptr, 2);
508 			} else {
509 				if((r=compress_any_dname(key->rk.dname,
510 					pkt, owner_labs, region, tree))
511 					!= RETVAL_OK)
512 					return r;
513 				if(sldns_buffer_remaining(pkt) <
514 					4+4+data->rr_len[i])
515 					return RETVAL_TRUNC;
516 			}
517 			sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_RRSIG);
518 			sldns_buffer_write(pkt, &key->rk.rrset_class, 2);
519 			if(data->rr_ttl[i] < timenow)
520 				sldns_buffer_write_u32(pkt, 0);
521 			else 	sldns_buffer_write_u32(pkt,
522 					data->rr_ttl[i]-timenow);
523 			/* rrsig rdata cannot be compressed, perform 100+ byte
524 			 * memcopy. */
525 			sldns_buffer_write(pkt, data->rr_data[i],
526 				data->rr_len[i]);
527 		}
528 	}
529 	/* change rrnum only after we are sure it fits */
530 	if(do_data)
531 		*num_rrs += data->count;
532 	if(do_sig && dnssec)
533 		*num_rrs += data->rrsig_count;
534 
535 	return RETVAL_OK;
536 }
537 
538 /** store msg section in wireformat buffer, return RETVAL_* */
539 static int
540 insert_section(struct reply_info* rep, size_t num_rrsets, uint16_t* num_rrs,
541 	sldns_buffer* pkt, size_t rrsets_before, time_t timenow,
542 	struct regional* region, struct compress_tree_node** tree,
543 	sldns_pkt_section s, uint16_t qtype, int dnssec, size_t rr_offset)
544 {
545 	int r;
546 	size_t i, setstart;
547 	/* we now allow this function to be called multiple times for the
548 	 * same section, incrementally updating num_rrs.  The caller is
549 	 * responsible for initializing it (which is the case in the current
550 	 * implementation). */
551 
552 	if(s != LDNS_SECTION_ADDITIONAL) {
553 		if(s == LDNS_SECTION_ANSWER && qtype == LDNS_RR_TYPE_ANY)
554 			dnssec = 1; /* include all types in ANY answer */
555 	  	for(i=0; i<num_rrsets; i++) {
556 			setstart = sldns_buffer_position(pkt);
557 			if((r=packed_rrset_encode(rep->rrsets[rrsets_before+i],
558 				pkt, num_rrs, timenow, region, 1, 1, tree,
559 				s, qtype, dnssec, rr_offset))
560 				!= RETVAL_OK) {
561 				/* Bad, but if due to size must set TC bit */
562 				/* trim off the rrset neatly. */
563 				sldns_buffer_set_position(pkt, setstart);
564 				return r;
565 			}
566 		}
567 	} else {
568 	  	for(i=0; i<num_rrsets; i++) {
569 			setstart = sldns_buffer_position(pkt);
570 			if((r=packed_rrset_encode(rep->rrsets[rrsets_before+i],
571 				pkt, num_rrs, timenow, region, 1, 0, tree,
572 				s, qtype, dnssec, rr_offset))
573 				!= RETVAL_OK) {
574 				sldns_buffer_set_position(pkt, setstart);
575 				return r;
576 			}
577 		}
578 		if(dnssec)
579 	  	  for(i=0; i<num_rrsets; i++) {
580 			setstart = sldns_buffer_position(pkt);
581 			if((r=packed_rrset_encode(rep->rrsets[rrsets_before+i],
582 				pkt, num_rrs, timenow, region, 0, 1, tree,
583 				s, qtype, dnssec, rr_offset))
584 				!= RETVAL_OK) {
585 				sldns_buffer_set_position(pkt, setstart);
586 				return r;
587 			}
588 		  }
589 	}
590 	return RETVAL_OK;
591 }
592 
593 /** store query section in wireformat buffer, return RETVAL */
594 static int
595 insert_query(struct query_info* qinfo, struct compress_tree_node** tree,
596 	sldns_buffer* buffer, struct regional* region)
597 {
598 	uint8_t* qname = qinfo->local_alias ?
599 		qinfo->local_alias->rrset->rk.dname : qinfo->qname;
600 	size_t qname_len = qinfo->local_alias ?
601 		qinfo->local_alias->rrset->rk.dname_len : qinfo->qname_len;
602 	if(sldns_buffer_remaining(buffer) <
603 		qinfo->qname_len+sizeof(uint16_t)*2)
604 		return RETVAL_TRUNC; /* buffer too small */
605 	/* the query is the first name inserted into the tree */
606 	if(!compress_tree_store(qname, dname_count_labels(qname),
607 		sldns_buffer_position(buffer), region, NULL, tree))
608 		return RETVAL_OUTMEM;
609 	if(sldns_buffer_current(buffer) == qname)
610 		sldns_buffer_skip(buffer, (ssize_t)qname_len);
611 	else	sldns_buffer_write(buffer, qname, qname_len);
612 	sldns_buffer_write_u16(buffer, qinfo->qtype);
613 	sldns_buffer_write_u16(buffer, qinfo->qclass);
614 	return RETVAL_OK;
615 }
616 
617 static int
618 positive_answer(struct reply_info* rep, uint16_t qtype) {
619 	size_t i;
620 	if (FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_NOERROR)
621 		return 0;
622 
623 	for(i=0;i<rep->an_numrrsets; i++) {
624 		if(ntohs(rep->rrsets[i]->rk.type) == qtype) {
625 			/* in case it is a wildcard with DNSSEC, there will
626 			 * be NSEC/NSEC3 records in the authority section
627 			 * that we cannot remove */
628 			for(i=rep->an_numrrsets; i<rep->an_numrrsets+
629 				rep->ns_numrrsets; i++) {
630 				if(ntohs(rep->rrsets[i]->rk.type) ==
631 					LDNS_RR_TYPE_NSEC ||
632 				   ntohs(rep->rrsets[i]->rk.type) ==
633 				   	LDNS_RR_TYPE_NSEC3)
634 					return 0;
635 			}
636 			return 1;
637 		}
638 	}
639 	return 0;
640 }
641 
642 int
643 reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
644 	uint16_t id, uint16_t flags, sldns_buffer* buffer, time_t timenow,
645 	struct regional* region, uint16_t udpsize, int dnssec)
646 {
647 	uint16_t ancount=0, nscount=0, arcount=0;
648 	struct compress_tree_node* tree = 0;
649 	int r;
650 	size_t rr_offset;
651 
652 	sldns_buffer_clear(buffer);
653 	if(udpsize < sldns_buffer_limit(buffer))
654 		sldns_buffer_set_limit(buffer, udpsize);
655 	if(sldns_buffer_remaining(buffer) < LDNS_HEADER_SIZE)
656 		return 0;
657 
658 	sldns_buffer_write(buffer, &id, sizeof(uint16_t));
659 	sldns_buffer_write_u16(buffer, flags);
660 	sldns_buffer_write_u16(buffer, rep->qdcount);
661 	/* set an, ns, ar counts to zero in case of small packets */
662 	sldns_buffer_write(buffer, "\000\000\000\000\000\000", 6);
663 
664 	/* insert query section */
665 	if(rep->qdcount) {
666 		if((r=insert_query(qinfo, &tree, buffer, region)) !=
667 			RETVAL_OK) {
668 			if(r == RETVAL_TRUNC) {
669 				/* create truncated message */
670 				sldns_buffer_write_u16_at(buffer, 4, 0);
671 				LDNS_TC_SET(sldns_buffer_begin(buffer));
672 				sldns_buffer_flip(buffer);
673 				return 1;
674 			}
675 			return 0;
676 		}
677 	}
678 	/* roundrobin offset. using query id for random number.  With ntohs
679 	 * for different roundrobins for sequential id client senders. */
680 	rr_offset = RRSET_ROUNDROBIN?ntohs(id)+(timenow?timenow:time(NULL)):0;
681 
682 	/* "prepend" any local alias records in the answer section if this
683 	 * response is supposed to be authoritative.  Currently it should
684 	 * be a single CNAME record (sanity-checked in worker_handle_request())
685 	 * but it can be extended if and when we support more variations of
686 	 * aliases. */
687 	if(qinfo->local_alias && (flags & BIT_AA)) {
688 		struct reply_info arep;
689 		time_t timezero = 0; /* to use the 'authoritative' TTL */
690 		memset(&arep, 0, sizeof(arep));
691 		arep.flags = rep->flags;
692 		arep.an_numrrsets = 1;
693 		arep.rrset_count = 1;
694 		arep.rrsets = &qinfo->local_alias->rrset;
695 		if((r=insert_section(&arep, 1, &ancount, buffer, 0,
696 			timezero, region, &tree, LDNS_SECTION_ANSWER,
697 			qinfo->qtype, dnssec, rr_offset)) != RETVAL_OK) {
698 			if(r == RETVAL_TRUNC) {
699 				/* create truncated message */
700 				sldns_buffer_write_u16_at(buffer, 6, ancount);
701 				LDNS_TC_SET(sldns_buffer_begin(buffer));
702 				sldns_buffer_flip(buffer);
703 				return 1;
704 			}
705 			return 0;
706 		}
707 	}
708 
709 	/* insert answer section */
710 	if((r=insert_section(rep, rep->an_numrrsets, &ancount, buffer,
711 		0, timenow, region, &tree, LDNS_SECTION_ANSWER, qinfo->qtype,
712 		dnssec, rr_offset)) != RETVAL_OK) {
713 		if(r == RETVAL_TRUNC) {
714 			/* create truncated message */
715 			sldns_buffer_write_u16_at(buffer, 6, ancount);
716 			LDNS_TC_SET(sldns_buffer_begin(buffer));
717 			sldns_buffer_flip(buffer);
718 			return 1;
719 		}
720 		return 0;
721 	}
722 	sldns_buffer_write_u16_at(buffer, 6, ancount);
723 
724 	/* if response is positive answer, auth/add sections are not required */
725 	if( ! (MINIMAL_RESPONSES && positive_answer(rep, qinfo->qtype)) ) {
726 		/* insert auth section */
727 		if((r=insert_section(rep, rep->ns_numrrsets, &nscount, buffer,
728 			rep->an_numrrsets, timenow, region, &tree,
729 			LDNS_SECTION_AUTHORITY, qinfo->qtype,
730 			dnssec, rr_offset)) != RETVAL_OK) {
731 			if(r == RETVAL_TRUNC) {
732 				/* create truncated message */
733 				sldns_buffer_write_u16_at(buffer, 8, nscount);
734 				LDNS_TC_SET(sldns_buffer_begin(buffer));
735 				sldns_buffer_flip(buffer);
736 				return 1;
737 			}
738 			return 0;
739 		}
740 		sldns_buffer_write_u16_at(buffer, 8, nscount);
741 
742 		/* insert add section */
743 		if((r=insert_section(rep, rep->ar_numrrsets, &arcount, buffer,
744 			rep->an_numrrsets + rep->ns_numrrsets, timenow, region,
745 			&tree, LDNS_SECTION_ADDITIONAL, qinfo->qtype,
746 			dnssec, rr_offset)) != RETVAL_OK) {
747 			if(r == RETVAL_TRUNC) {
748 				/* no need to set TC bit, this is the additional */
749 				sldns_buffer_write_u16_at(buffer, 10, arcount);
750 				sldns_buffer_flip(buffer);
751 				return 1;
752 			}
753 			return 0;
754 		}
755 		sldns_buffer_write_u16_at(buffer, 10, arcount);
756 	}
757 	sldns_buffer_flip(buffer);
758 	return 1;
759 }
760 
761 uint16_t
762 calc_edns_field_size(struct edns_data* edns)
763 {
764 	size_t rdatalen = 0;
765 	struct edns_option* opt;
766 	if(!edns || !edns->edns_present)
767 		return 0;
768 	for(opt = edns->opt_list; opt; opt = opt->next) {
769 		rdatalen += 4 + opt->opt_len;
770 	}
771 	/* domain root '.' + type + class + ttl + rdatalen */
772 	return 1 + 2 + 2 + 4 + 2 + rdatalen;
773 }
774 
775 void
776 attach_edns_record(sldns_buffer* pkt, struct edns_data* edns)
777 {
778 	size_t len;
779 	size_t rdatapos;
780 	struct edns_option* opt;
781 	if(!edns || !edns->edns_present)
782 		return;
783 	/* inc additional count */
784 	sldns_buffer_write_u16_at(pkt, 10,
785 		sldns_buffer_read_u16_at(pkt, 10) + 1);
786 	len = sldns_buffer_limit(pkt);
787 	sldns_buffer_clear(pkt);
788 	sldns_buffer_set_position(pkt, len);
789 	/* write EDNS record */
790 	sldns_buffer_write_u8(pkt, 0); /* '.' label */
791 	sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_OPT); /* type */
792 	sldns_buffer_write_u16(pkt, edns->udp_size); /* class */
793 	sldns_buffer_write_u8(pkt, edns->ext_rcode); /* ttl */
794 	sldns_buffer_write_u8(pkt, edns->edns_version);
795 	sldns_buffer_write_u16(pkt, edns->bits);
796 	rdatapos = sldns_buffer_position(pkt);
797 	sldns_buffer_write_u16(pkt, 0); /* rdatalen */
798 	/* write rdata */
799 	for(opt=edns->opt_list; opt; opt=opt->next) {
800 		sldns_buffer_write_u16(pkt, opt->opt_code);
801 		sldns_buffer_write_u16(pkt, opt->opt_len);
802 		if(opt->opt_len != 0)
803 			sldns_buffer_write(pkt, opt->opt_data, opt->opt_len);
804 	}
805 	if(edns->opt_list)
806 		sldns_buffer_write_u16_at(pkt, rdatapos,
807 			sldns_buffer_position(pkt)-rdatapos-2);
808 	sldns_buffer_flip(pkt);
809 }
810 
811 int
812 reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
813 	uint16_t id, uint16_t qflags, sldns_buffer* pkt, time_t timenow,
814 	int cached, struct regional* region, uint16_t udpsize,
815 	struct edns_data* edns, int dnssec, int secure)
816 {
817 	uint16_t flags;
818 	unsigned int attach_edns = 0;
819 
820 	if(!cached || rep->authoritative) {
821 		/* original flags, copy RD and CD bits from query. */
822 		flags = rep->flags | (qflags & (BIT_RD|BIT_CD));
823 	} else {
824 		/* remove AA bit, copy RD and CD bits from query. */
825 		flags = (rep->flags & ~BIT_AA) | (qflags & (BIT_RD|BIT_CD));
826 	}
827 	if(secure && (dnssec || (qflags&BIT_AD)))
828 		flags |= BIT_AD;
829 	/* restore AA bit if we have a local alias and the response can be
830 	 * authoritative.  Also clear AD bit if set as the local data is the
831 	 * primary answer. */
832 	if(qinf->local_alias &&
833 		(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
834 		FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN)) {
835 		flags |= BIT_AA;
836 		flags &= ~BIT_AD;
837 	}
838 	log_assert(flags & BIT_QR); /* QR bit must be on in our replies */
839 	if(udpsize < LDNS_HEADER_SIZE)
840 		return 0;
841 	if(sldns_buffer_capacity(pkt) < udpsize)
842 		udpsize = sldns_buffer_capacity(pkt);
843 	if(udpsize < LDNS_HEADER_SIZE + calc_edns_field_size(edns)) {
844 		/* packet too small to contain edns, omit it. */
845 		attach_edns = 0;
846 	} else {
847 		/* reserve space for edns record */
848 		attach_edns = (unsigned int)calc_edns_field_size(edns);
849 		udpsize -= attach_edns;
850 	}
851 
852 	if(!reply_info_encode(qinf, rep, id, flags, pkt, timenow, region,
853 		udpsize, dnssec)) {
854 		log_err("reply encode: out of memory");
855 		return 0;
856 	}
857 	if(attach_edns && sldns_buffer_capacity(pkt) >=
858 		sldns_buffer_limit(pkt)+attach_edns)
859 		attach_edns_record(pkt, edns);
860 	return 1;
861 }
862 
863 void
864 qinfo_query_encode(sldns_buffer* pkt, struct query_info* qinfo)
865 {
866 	uint16_t flags = 0; /* QUERY, NOERROR */
867 	const uint8_t* qname = qinfo->local_alias ?
868 		qinfo->local_alias->rrset->rk.dname : qinfo->qname;
869 	size_t qname_len = qinfo->local_alias ?
870 		qinfo->local_alias->rrset->rk.dname_len : qinfo->qname_len;
871 	sldns_buffer_clear(pkt);
872 	log_assert(sldns_buffer_remaining(pkt) >= 12+255+4/*max query*/);
873 	sldns_buffer_skip(pkt, 2); /* id done later */
874 	sldns_buffer_write_u16(pkt, flags);
875 	sldns_buffer_write_u16(pkt, 1); /* query count */
876 	sldns_buffer_write(pkt, "\000\000\000\000\000\000", 6); /* counts */
877 	sldns_buffer_write(pkt, qname, qname_len);
878 	sldns_buffer_write_u16(pkt, qinfo->qtype);
879 	sldns_buffer_write_u16(pkt, qinfo->qclass);
880 	sldns_buffer_flip(pkt);
881 }
882 
883 void
884 error_encode(sldns_buffer* buf, int r, struct query_info* qinfo,
885 	uint16_t qid, uint16_t qflags, struct edns_data* edns)
886 {
887 	uint16_t flags;
888 
889 	sldns_buffer_clear(buf);
890 	sldns_buffer_write(buf, &qid, sizeof(uint16_t));
891 	flags = (uint16_t)(BIT_QR | BIT_RA | r); /* QR and retcode*/
892 	flags |= (qflags & (BIT_RD|BIT_CD)); /* copy RD and CD bit */
893 	sldns_buffer_write_u16(buf, flags);
894 	if(qinfo) flags = 1;
895 	else	flags = 0;
896 	sldns_buffer_write_u16(buf, flags);
897 	flags = 0;
898 	sldns_buffer_write(buf, &flags, sizeof(uint16_t));
899 	sldns_buffer_write(buf, &flags, sizeof(uint16_t));
900 	sldns_buffer_write(buf, &flags, sizeof(uint16_t));
901 	if(qinfo) {
902 		const uint8_t* qname = qinfo->local_alias ?
903 			qinfo->local_alias->rrset->rk.dname : qinfo->qname;
904 		size_t qname_len = qinfo->local_alias ?
905 			qinfo->local_alias->rrset->rk.dname_len :
906 			qinfo->qname_len;
907 		if(sldns_buffer_current(buf) == qname)
908 			sldns_buffer_skip(buf, (ssize_t)qname_len);
909 		else	sldns_buffer_write(buf, qname, qname_len);
910 		sldns_buffer_write_u16(buf, qinfo->qtype);
911 		sldns_buffer_write_u16(buf, qinfo->qclass);
912 	}
913 	sldns_buffer_flip(buf);
914 	if(edns) {
915 		struct edns_data es = *edns;
916 		es.edns_version = EDNS_ADVERTISED_VERSION;
917 		es.udp_size = EDNS_ADVERTISED_SIZE;
918 		es.ext_rcode = 0;
919 		es.bits &= EDNS_DO;
920 		if(sldns_buffer_limit(buf) + calc_edns_field_size(&es) >
921 			edns->udp_size)
922 			return;
923 		attach_edns_record(buf, &es);
924 	}
925 }
926