]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/util/data/msgencode.c
2 * util/data/msgencode.c - Encode DNS messages, queries and replies.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains a routines to encode DNS messages.
43 #include "util/data/msgencode.h"
44 #include "util/data/msgreply.h"
45 #include "util/data/msgparse.h"
46 #include "util/data/dname.h"
48 #include "util/regional.h"
49 #include "util/net_help.h"
50 #include "ldns/sbuffer.h"
52 /** return code that means the function ran out of memory. negative so it does
53 * not conflict with DNS rcodes. */
54 #define RETVAL_OUTMEM -2
55 /** return code that means the data did not fit (completely) in the packet */
56 #define RETVAL_TRUNC -4
57 /** return code that means all is peachy keen. Equal to DNS rcode NOERROR */
61 * Data structure to help domain name compression in outgoing messages.
62 * A tree of dnames and their offsets in the packet is kept.
63 * It is kept sorted, not canonical, but by label at least, so that after
64 * a lookup of a name you know its closest match, and the parent from that
65 * closest match. These are possible compression targets.
67 * It is a binary tree, not a rbtree or balanced tree, as the effort
68 * of keeping it balanced probably outweighs usefulness (given typical
71 struct compress_tree_node
{
72 /** left node in tree, all smaller to this */
73 struct compress_tree_node
* left
;
74 /** right node in tree, all larger than this */
75 struct compress_tree_node
* right
;
77 /** the parent node - not for tree, but zone parent. One less label */
78 struct compress_tree_node
* parent
;
79 /** the domain name for this node. Pointer to uncompressed memory. */
81 /** number of labels in domain name, kept to help compare func. */
83 /** offset in packet that points to this dname */
88 * Find domain name in tree, returns exact and closest match.
89 * @param tree: root of tree.
90 * @param dname: pointer to uncompressed dname.
91 * @param labs: number of labels in domain name.
92 * @param match: closest or exact match.
93 * guaranteed to be smaller or equal to the sought dname.
94 * can be null if the tree is empty.
95 * @param matchlabels: number of labels that match with closest match.
96 * can be zero is there is no match.
97 * @param insertpt: insert location for dname, if not found.
98 * @return: 0 if no exact match.
101 compress_tree_search(struct compress_tree_node
** tree
, uint8_t* dname
,
102 int labs
, struct compress_tree_node
** match
, int* matchlabels
,
103 struct compress_tree_node
*** insertpt
)
106 struct compress_tree_node
* p
= *tree
;
107 struct compress_tree_node
* close
= 0;
108 struct compress_tree_node
** prev
= tree
;
110 if((c
= dname_lab_cmp(dname
, labs
, p
->dname
, p
->labs
, &n
))
121 close
= p
; /* p->dname is smaller than dname */
127 *matchlabels
= closen
;
133 * Lookup a domain name in compression tree.
134 * @param tree: root of tree (not the node with '.').
135 * @param dname: pointer to uncompressed dname.
136 * @param labs: number of labels in domain name.
137 * @param insertpt: insert location for dname, if not found.
138 * @return: 0 if not found or compress treenode with best compression.
140 static struct compress_tree_node
*
141 compress_tree_lookup(struct compress_tree_node
** tree
, uint8_t* dname
,
142 int labs
, struct compress_tree_node
*** insertpt
)
144 struct compress_tree_node
* p
;
147 return 0; /* do not compress root node */
148 if(compress_tree_search(tree
, dname
, labs
, &p
, &m
, insertpt
)) {
152 /* return some ancestor of p that compresses well. */
154 /* www.example.com. (labs=4) matched foo.example.com.(labs=4)
155 * then matchcount = 3. need to go up. */
156 while(p
&& p
->labs
> m
)
164 * Create node for domain name compression tree.
165 * @param dname: pointer to uncompressed dname (stored in tree).
166 * @param labs: number of labels in dname.
167 * @param offset: offset into packet for dname.
168 * @param region: how to allocate memory for new node.
169 * @return new node or 0 on malloc failure.
171 static struct compress_tree_node
*
172 compress_tree_newnode(uint8_t* dname
, int labs
, size_t offset
,
173 struct regional
* region
)
175 struct compress_tree_node
* n
= (struct compress_tree_node
*)
176 regional_alloc(region
, sizeof(struct compress_tree_node
));
188 * Store domain name and ancestors into compression tree.
189 * @param dname: pointer to uncompressed dname (stored in tree).
190 * @param labs: number of labels in dname.
191 * @param offset: offset into packet for dname.
192 * @param region: how to allocate memory for new node.
193 * @param closest: match from previous lookup, used to compress dname.
194 * may be NULL if no previous match.
195 * if the tree has an ancestor of dname already, this must be it.
196 * @param insertpt: where to insert the dname in tree.
197 * @return: 0 on memory error.
200 compress_tree_store(uint8_t* dname
, int labs
, size_t offset
,
201 struct regional
* region
, struct compress_tree_node
* closest
,
202 struct compress_tree_node
** insertpt
)
205 struct compress_tree_node
* newnode
;
206 struct compress_tree_node
* prevnode
= NULL
;
207 int uplabs
= labs
-1; /* does not store root in tree */
208 if(closest
) uplabs
= labs
- closest
->labs
;
209 log_assert(uplabs
>= 0);
210 /* algorithms builds up a vine of dname-labels to hang into tree */
212 if(offset
> PTR_MAX_OFFSET
) {
213 /* insertion failed, drop vine */
214 return 1; /* compression pointer no longer useful */
216 if(!(newnode
= compress_tree_newnode(dname
, labs
, offset
,
218 /* insertion failed, drop vine */
223 /* chain nodes together, last one has one label more,
224 * so is larger than newnode, thus goes right. */
225 newnode
->right
= prevnode
;
226 prevnode
->parent
= newnode
;
236 /* if we have a vine, hang the vine into the tree */
238 *insertpt
= prevnode
;
239 prevnode
->parent
= closest
;
244 /** compress a domain name */
246 write_compressed_dname(sldns_buffer
* pkt
, uint8_t* dname
, int labs
,
247 struct compress_tree_node
* p
)
250 int labcopy
= labs
- p
->labs
;
255 /* write root label */
256 if(sldns_buffer_remaining(pkt
) < 1)
258 sldns_buffer_write_u8(pkt
, 0);
262 /* copy the first couple of labels */
265 if(sldns_buffer_remaining(pkt
) < (size_t)lablen
+1)
267 sldns_buffer_write_u8(pkt
, lablen
);
268 sldns_buffer_write(pkt
, dname
, lablen
);
271 /* insert compression ptr */
272 if(sldns_buffer_remaining(pkt
) < 2)
274 ptr
= PTR_CREATE(p
->offset
);
275 sldns_buffer_write_u16(pkt
, ptr
);
279 /** compress owner name of RR, return RETVAL_OUTMEM RETVAL_TRUNC */
281 compress_owner(struct ub_packed_rrset_key
* key
, sldns_buffer
* pkt
,
282 struct regional
* region
, struct compress_tree_node
** tree
,
283 size_t owner_pos
, uint16_t* owner_ptr
, int owner_labs
)
285 struct compress_tree_node
* p
;
286 struct compress_tree_node
** insertpt
;
288 /* compress first time dname */
289 if((p
= compress_tree_lookup(tree
, key
->rk
.dname
,
290 owner_labs
, &insertpt
))) {
291 if(p
->labs
== owner_labs
)
292 /* avoid ptr chains, since some software is
293 * not capable of decoding ptr after a ptr. */
294 *owner_ptr
= htons(PTR_CREATE(p
->offset
));
295 if(!write_compressed_dname(pkt
, key
->rk
.dname
,
298 /* check if typeclass+4 ttl + rdatalen is available */
299 if(sldns_buffer_remaining(pkt
) < 4+4+2)
303 if(sldns_buffer_remaining(pkt
) < key
->rk
.dname_len
+4+4+2)
305 sldns_buffer_write(pkt
, key
->rk
.dname
,
307 if(owner_pos
<= PTR_MAX_OFFSET
)
308 *owner_ptr
= htons(PTR_CREATE(owner_pos
));
310 if(!compress_tree_store(key
->rk
.dname
, owner_labs
,
311 owner_pos
, region
, p
, insertpt
))
312 return RETVAL_OUTMEM
;
314 /* always compress 2nd-further RRs in RRset */
315 if(owner_labs
== 1) {
316 if(sldns_buffer_remaining(pkt
) < 1+4+4+2)
318 sldns_buffer_write_u8(pkt
, 0);
320 if(sldns_buffer_remaining(pkt
) < 2+4+4+2)
322 sldns_buffer_write(pkt
, owner_ptr
, 2);
328 /** compress any domain name to the packet, return RETVAL_* */
330 compress_any_dname(uint8_t* dname
, sldns_buffer
* pkt
, int labs
,
331 struct regional
* region
, struct compress_tree_node
** tree
)
333 struct compress_tree_node
* p
;
334 struct compress_tree_node
** insertpt
= NULL
;
335 size_t pos
= sldns_buffer_position(pkt
);
336 if((p
= compress_tree_lookup(tree
, dname
, labs
, &insertpt
))) {
337 if(!write_compressed_dname(pkt
, dname
, labs
, p
))
340 if(!dname_buffer_write(pkt
, dname
))
343 if(!compress_tree_store(dname
, labs
, pos
, region
, p
, insertpt
))
344 return RETVAL_OUTMEM
;
348 /** return true if type needs domain name compression in rdata */
349 static const sldns_rr_descriptor
*
350 type_rdata_compressable(struct ub_packed_rrset_key
* key
)
352 uint16_t t
= ntohs(key
->rk
.type
);
353 if(sldns_rr_descript(t
) &&
354 sldns_rr_descript(t
)->_compress
== LDNS_RR_COMPRESS
)
355 return sldns_rr_descript(t
);
359 /** compress domain names in rdata, return RETVAL_* */
361 compress_rdata(sldns_buffer
* pkt
, uint8_t* rdata
, size_t todolen
,
362 struct regional
* region
, struct compress_tree_node
** tree
,
363 const sldns_rr_descriptor
* desc
)
365 int labs
, r
, rdf
= 0;
366 size_t dname_len
, len
, pos
= sldns_buffer_position(pkt
);
367 uint8_t count
= desc
->_dname_count
;
369 sldns_buffer_skip(pkt
, 2); /* rdata len fill in later */
370 /* space for rdatalen checked for already */
373 while(todolen
> 0 && count
) {
374 switch(desc
->_wireformat
[rdf
]) {
375 case LDNS_RDF_TYPE_DNAME
:
376 labs
= dname_count_size_labels(rdata
, &dname_len
);
377 if((r
=compress_any_dname(rdata
, pkt
, labs
, region
,
381 todolen
-= dname_len
;
385 case LDNS_RDF_TYPE_STR
:
389 len
= get_rdf_size(desc
->_wireformat
[rdf
]);
393 if(sldns_buffer_remaining(pkt
) < len
)
395 sldns_buffer_write(pkt
, rdata
, len
);
403 if(sldns_buffer_remaining(pkt
) < todolen
)
405 sldns_buffer_write(pkt
, rdata
, todolen
);
409 sldns_buffer_write_u16_at(pkt
, pos
, sldns_buffer_position(pkt
)-pos
-2);
413 /** Returns true if RR type should be included */
415 rrset_belongs_in_reply(sldns_pkt_section s
, uint16_t rrtype
, uint16_t qtype
,
420 /* skip non DNSSEC types, except if directly queried for */
421 if(s
== LDNS_SECTION_ANSWER
) {
422 if(qtype
== LDNS_RR_TYPE_ANY
|| qtype
== rrtype
)
425 /* check DNSSEC-ness */
427 case LDNS_RR_TYPE_SIG
:
428 case LDNS_RR_TYPE_KEY
:
429 case LDNS_RR_TYPE_NXT
:
430 case LDNS_RR_TYPE_DS
:
431 case LDNS_RR_TYPE_RRSIG
:
432 case LDNS_RR_TYPE_NSEC
:
433 case LDNS_RR_TYPE_DNSKEY
:
434 case LDNS_RR_TYPE_NSEC3
:
435 case LDNS_RR_TYPE_NSEC3PARAMS
:
441 /** store rrset in buffer in wireformat, return RETVAL_* */
443 packed_rrset_encode(struct ub_packed_rrset_key
* key
, sldns_buffer
* pkt
,
444 uint16_t* num_rrs
, time_t timenow
, struct regional
* region
,
445 int do_data
, int do_sig
, struct compress_tree_node
** tree
,
446 sldns_pkt_section s
, uint16_t qtype
, int dnssec
, size_t rr_offset
)
448 size_t i
, j
, owner_pos
;
450 uint16_t owner_ptr
= 0;
451 struct packed_rrset_data
* data
= (struct packed_rrset_data
*)
454 /* does this RR type belong in the answer? */
455 if(!rrset_belongs_in_reply(s
, ntohs(key
->rk
.type
), qtype
, dnssec
))
458 owner_labs
= dname_count_labels(key
->rk
.dname
);
459 owner_pos
= sldns_buffer_position(pkt
);
462 const sldns_rr_descriptor
* c
= type_rdata_compressable(key
);
463 for(i
=0; i
<data
->count
; i
++) {
464 /* rrset roundrobin */
465 j
= (i
+ rr_offset
) % data
->count
;
466 if((r
=compress_owner(key
, pkt
, region
, tree
,
467 owner_pos
, &owner_ptr
, owner_labs
))
470 sldns_buffer_write(pkt
, &key
->rk
.type
, 2);
471 sldns_buffer_write(pkt
, &key
->rk
.rrset_class
, 2);
472 if(data
->rr_ttl
[j
] < timenow
)
473 sldns_buffer_write_u32(pkt
, 0);
474 else sldns_buffer_write_u32(pkt
,
475 data
->rr_ttl
[j
]-timenow
);
477 if((r
=compress_rdata(pkt
, data
->rr_data
[j
],
478 data
->rr_len
[j
], region
, tree
, c
))
482 if(sldns_buffer_remaining(pkt
) < data
->rr_len
[j
])
484 sldns_buffer_write(pkt
, data
->rr_data
[j
],
490 if(do_sig
&& dnssec
) {
491 size_t total
= data
->count
+data
->rrsig_count
;
492 for(i
=data
->count
; i
<total
; i
++) {
493 if(owner_ptr
&& owner_labs
!= 1) {
494 if(sldns_buffer_remaining(pkt
) <
495 2+4+4+data
->rr_len
[i
])
497 sldns_buffer_write(pkt
, &owner_ptr
, 2);
499 if((r
=compress_any_dname(key
->rk
.dname
,
500 pkt
, owner_labs
, region
, tree
))
503 if(sldns_buffer_remaining(pkt
) <
507 sldns_buffer_write_u16(pkt
, LDNS_RR_TYPE_RRSIG
);
508 sldns_buffer_write(pkt
, &key
->rk
.rrset_class
, 2);
509 if(data
->rr_ttl
[i
] < timenow
)
510 sldns_buffer_write_u32(pkt
, 0);
511 else sldns_buffer_write_u32(pkt
,
512 data
->rr_ttl
[i
]-timenow
);
513 /* rrsig rdata cannot be compressed, perform 100+ byte
515 sldns_buffer_write(pkt
, data
->rr_data
[i
],
519 /* change rrnum only after we are sure it fits */
521 *num_rrs
+= data
->count
;
523 *num_rrs
+= data
->rrsig_count
;
528 /** store msg section in wireformat buffer, return RETVAL_* */
530 insert_section(struct reply_info
* rep
, size_t num_rrsets
, uint16_t* num_rrs
,
531 sldns_buffer
* pkt
, size_t rrsets_before
, time_t timenow
,
532 struct regional
* region
, struct compress_tree_node
** tree
,
533 sldns_pkt_section s
, uint16_t qtype
, int dnssec
, size_t rr_offset
)
538 if(s
!= LDNS_SECTION_ADDITIONAL
) {
539 if(s
== LDNS_SECTION_ANSWER
&& qtype
== LDNS_RR_TYPE_ANY
)
540 dnssec
= 1; /* include all types in ANY answer */
541 for(i
=0; i
<num_rrsets
; i
++) {
542 setstart
= sldns_buffer_position(pkt
);
543 if((r
=packed_rrset_encode(rep
->rrsets
[rrsets_before
+i
],
544 pkt
, num_rrs
, timenow
, region
, 1, 1, tree
,
545 s
, qtype
, dnssec
, rr_offset
))
547 /* Bad, but if due to size must set TC bit */
548 /* trim off the rrset neatly. */
549 sldns_buffer_set_position(pkt
, setstart
);
554 for(i
=0; i
<num_rrsets
; i
++) {
555 setstart
= sldns_buffer_position(pkt
);
556 if((r
=packed_rrset_encode(rep
->rrsets
[rrsets_before
+i
],
557 pkt
, num_rrs
, timenow
, region
, 1, 0, tree
,
558 s
, qtype
, dnssec
, rr_offset
))
560 sldns_buffer_set_position(pkt
, setstart
);
565 for(i
=0; i
<num_rrsets
; i
++) {
566 setstart
= sldns_buffer_position(pkt
);
567 if((r
=packed_rrset_encode(rep
->rrsets
[rrsets_before
+i
],
568 pkt
, num_rrs
, timenow
, region
, 0, 1, tree
,
569 s
, qtype
, dnssec
, rr_offset
))
571 sldns_buffer_set_position(pkt
, setstart
);
579 /** store query section in wireformat buffer, return RETVAL */
581 insert_query(struct query_info
* qinfo
, struct compress_tree_node
** tree
,
582 sldns_buffer
* buffer
, struct regional
* region
)
584 if(sldns_buffer_remaining(buffer
) <
585 qinfo
->qname_len
+sizeof(uint16_t)*2)
586 return RETVAL_TRUNC
; /* buffer too small */
587 /* the query is the first name inserted into the tree */
588 if(!compress_tree_store(qinfo
->qname
,
589 dname_count_labels(qinfo
->qname
),
590 sldns_buffer_position(buffer
), region
, NULL
, tree
))
591 return RETVAL_OUTMEM
;
592 if(sldns_buffer_current(buffer
) == qinfo
->qname
)
593 sldns_buffer_skip(buffer
, (ssize_t
)qinfo
->qname_len
);
594 else sldns_buffer_write(buffer
, qinfo
->qname
, qinfo
->qname_len
);
595 sldns_buffer_write_u16(buffer
, qinfo
->qtype
);
596 sldns_buffer_write_u16(buffer
, qinfo
->qclass
);
601 positive_answer(struct reply_info
* rep
, uint16_t qtype
) {
603 if (FLAGS_GET_RCODE(rep
->flags
) != LDNS_RCODE_NOERROR
)
606 for(i
=0;i
<rep
->an_numrrsets
; i
++) {
607 if(ntohs(rep
->rrsets
[i
]->rk
.type
) == qtype
) {
608 /* in case it is a wildcard with DNSSEC, there will
609 * be NSEC/NSEC3 records in the authority section
610 * that we cannot remove */
611 for(i
=rep
->an_numrrsets
; i
<rep
->an_numrrsets
+
612 rep
->ns_numrrsets
; i
++) {
613 if(ntohs(rep
->rrsets
[i
]->rk
.type
) ==
615 ntohs(rep
->rrsets
[i
]->rk
.type
) ==
626 reply_info_encode(struct query_info
* qinfo
, struct reply_info
* rep
,
627 uint16_t id
, uint16_t flags
, sldns_buffer
* buffer
, time_t timenow
,
628 struct regional
* region
, uint16_t udpsize
, int dnssec
)
630 uint16_t ancount
=0, nscount
=0, arcount
=0;
631 struct compress_tree_node
* tree
= 0;
635 sldns_buffer_clear(buffer
);
636 if(udpsize
< sldns_buffer_limit(buffer
))
637 sldns_buffer_set_limit(buffer
, udpsize
);
638 if(sldns_buffer_remaining(buffer
) < LDNS_HEADER_SIZE
)
641 sldns_buffer_write(buffer
, &id
, sizeof(uint16_t));
642 sldns_buffer_write_u16(buffer
, flags
);
643 sldns_buffer_write_u16(buffer
, rep
->qdcount
);
644 /* set an, ns, ar counts to zero in case of small packets */
645 sldns_buffer_write(buffer
, "\000\000\000\000\000\000", 6);
647 /* insert query section */
649 if((r
=insert_query(qinfo
, &tree
, buffer
, region
)) !=
651 if(r
== RETVAL_TRUNC
) {
652 /* create truncated message */
653 sldns_buffer_write_u16_at(buffer
, 4, 0);
654 LDNS_TC_SET(sldns_buffer_begin(buffer
));
655 sldns_buffer_flip(buffer
);
661 /* roundrobin offset. using query id for random number. With ntohs
662 * for different roundrobins for sequential id client senders. */
663 rr_offset
= RRSET_ROUNDROBIN
?ntohs(id
):0;
665 /* insert answer section */
666 if((r
=insert_section(rep
, rep
->an_numrrsets
, &ancount
, buffer
,
667 0, timenow
, region
, &tree
, LDNS_SECTION_ANSWER
, qinfo
->qtype
,
668 dnssec
, rr_offset
)) != RETVAL_OK
) {
669 if(r
== RETVAL_TRUNC
) {
670 /* create truncated message */
671 sldns_buffer_write_u16_at(buffer
, 6, ancount
);
672 LDNS_TC_SET(sldns_buffer_begin(buffer
));
673 sldns_buffer_flip(buffer
);
678 sldns_buffer_write_u16_at(buffer
, 6, ancount
);
680 /* if response is positive answer, auth/add sections are not required */
681 if( ! (MINIMAL_RESPONSES
&& positive_answer(rep
, qinfo
->qtype
)) ) {
682 /* insert auth section */
683 if((r
=insert_section(rep
, rep
->ns_numrrsets
, &nscount
, buffer
,
684 rep
->an_numrrsets
, timenow
, region
, &tree
,
685 LDNS_SECTION_AUTHORITY
, qinfo
->qtype
,
686 dnssec
, rr_offset
)) != RETVAL_OK
) {
687 if(r
== RETVAL_TRUNC
) {
688 /* create truncated message */
689 sldns_buffer_write_u16_at(buffer
, 8, nscount
);
690 LDNS_TC_SET(sldns_buffer_begin(buffer
));
691 sldns_buffer_flip(buffer
);
696 sldns_buffer_write_u16_at(buffer
, 8, nscount
);
698 /* insert add section */
699 if((r
=insert_section(rep
, rep
->ar_numrrsets
, &arcount
, buffer
,
700 rep
->an_numrrsets
+ rep
->ns_numrrsets
, timenow
, region
,
701 &tree
, LDNS_SECTION_ADDITIONAL
, qinfo
->qtype
,
702 dnssec
, rr_offset
)) != RETVAL_OK
) {
703 if(r
== RETVAL_TRUNC
) {
704 /* no need to set TC bit, this is the additional */
705 sldns_buffer_write_u16_at(buffer
, 10, arcount
);
706 sldns_buffer_flip(buffer
);
711 sldns_buffer_write_u16_at(buffer
, 10, arcount
);
713 sldns_buffer_flip(buffer
);
718 calc_edns_field_size(struct edns_data
* edns
)
720 if(!edns
|| !edns
->edns_present
)
722 /* domain root '.' + type + class + ttl + rdatalen(=0) */
723 return 1 + 2 + 2 + 4 + 2;
727 attach_edns_record(sldns_buffer
* pkt
, struct edns_data
* edns
)
730 if(!edns
|| !edns
->edns_present
)
732 /* inc additional count */
733 sldns_buffer_write_u16_at(pkt
, 10,
734 sldns_buffer_read_u16_at(pkt
, 10) + 1);
735 len
= sldns_buffer_limit(pkt
);
736 sldns_buffer_clear(pkt
);
737 sldns_buffer_set_position(pkt
, len
);
738 /* write EDNS record */
739 sldns_buffer_write_u8(pkt
, 0); /* '.' label */
740 sldns_buffer_write_u16(pkt
, LDNS_RR_TYPE_OPT
); /* type */
741 sldns_buffer_write_u16(pkt
, edns
->udp_size
); /* class */
742 sldns_buffer_write_u8(pkt
, edns
->ext_rcode
); /* ttl */
743 sldns_buffer_write_u8(pkt
, edns
->edns_version
);
744 sldns_buffer_write_u16(pkt
, edns
->bits
);
745 sldns_buffer_write_u16(pkt
, 0); /* rdatalen */
746 sldns_buffer_flip(pkt
);
750 reply_info_answer_encode(struct query_info
* qinf
, struct reply_info
* rep
,
751 uint16_t id
, uint16_t qflags
, sldns_buffer
* pkt
, time_t timenow
,
752 int cached
, struct regional
* region
, uint16_t udpsize
,
753 struct edns_data
* edns
, int dnssec
, int secure
)
758 if(!cached
|| rep
->authoritative
) {
759 /* original flags, copy RD and CD bits from query. */
760 flags
= rep
->flags
| (qflags
& (BIT_RD
|BIT_CD
));
762 /* remove AA bit, copy RD and CD bits from query. */
763 flags
= (rep
->flags
& ~BIT_AA
) | (qflags
& (BIT_RD
|BIT_CD
));
765 if(secure
&& (dnssec
|| (qflags
&BIT_AD
)))
767 log_assert(flags
& BIT_QR
); /* QR bit must be on in our replies */
768 if(udpsize
< LDNS_HEADER_SIZE
)
770 if(udpsize
< LDNS_HEADER_SIZE
+ calc_edns_field_size(edns
)) {
771 /* packet too small to contain edns, omit it. */
774 /* reserve space for edns record */
775 udpsize
-= calc_edns_field_size(edns
);
778 if(!reply_info_encode(qinf
, rep
, id
, flags
, pkt
, timenow
, region
,
780 log_err("reply encode: out of memory");
784 attach_edns_record(pkt
, edns
);
789 qinfo_query_encode(sldns_buffer
* pkt
, struct query_info
* qinfo
)
791 uint16_t flags
= 0; /* QUERY, NOERROR */
792 sldns_buffer_clear(pkt
);
793 log_assert(sldns_buffer_remaining(pkt
) >= 12+255+4/*max query*/);
794 sldns_buffer_skip(pkt
, 2); /* id done later */
795 sldns_buffer_write_u16(pkt
, flags
);
796 sldns_buffer_write_u16(pkt
, 1); /* query count */
797 sldns_buffer_write(pkt
, "\000\000\000\000\000\000", 6); /* counts */
798 sldns_buffer_write(pkt
, qinfo
->qname
, qinfo
->qname_len
);
799 sldns_buffer_write_u16(pkt
, qinfo
->qtype
);
800 sldns_buffer_write_u16(pkt
, qinfo
->qclass
);
801 sldns_buffer_flip(pkt
);
805 error_encode(sldns_buffer
* buf
, int r
, struct query_info
* qinfo
,
806 uint16_t qid
, uint16_t qflags
, struct edns_data
* edns
)
810 sldns_buffer_clear(buf
);
811 sldns_buffer_write(buf
, &qid
, sizeof(uint16_t));
812 flags
= (uint16_t)(BIT_QR
| BIT_RA
| r
); /* QR and retcode*/
813 flags
|= (qflags
& (BIT_RD
|BIT_CD
)); /* copy RD and CD bit */
814 sldns_buffer_write_u16(buf
, flags
);
817 sldns_buffer_write_u16(buf
, flags
);
819 sldns_buffer_write(buf
, &flags
, sizeof(uint16_t));
820 sldns_buffer_write(buf
, &flags
, sizeof(uint16_t));
821 sldns_buffer_write(buf
, &flags
, sizeof(uint16_t));
823 if(sldns_buffer_current(buf
) == qinfo
->qname
)
824 sldns_buffer_skip(buf
, (ssize_t
)qinfo
->qname_len
);
825 else sldns_buffer_write(buf
, qinfo
->qname
, qinfo
->qname_len
);
826 sldns_buffer_write_u16(buf
, qinfo
->qtype
);
827 sldns_buffer_write_u16(buf
, qinfo
->qclass
);
829 sldns_buffer_flip(buf
);
831 struct edns_data es
= *edns
;
832 es
.edns_version
= EDNS_ADVERTISED_VERSION
;
833 es
.udp_size
= EDNS_ADVERTISED_SIZE
;
836 if(sldns_buffer_limit(buf
) + calc_edns_field_size(&es
) >
839 attach_edns_record(buf
, &es
);