xref: /openbsd-src/usr.sbin/unbound/util/data/msgencode.h (revision 437d28603d0290bfd9d9c531721c9139b03d4aa6)
1933707f3Ssthen /*
2933707f3Ssthen  * util/data/msgencode.h - encode compressed DNS messages.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains temporary data structures and routines to create
40933707f3Ssthen  * compressed DNS messages.
41933707f3Ssthen  */
42933707f3Ssthen 
43933707f3Ssthen #ifndef UTIL_DATA_MSGENCODE_H
44933707f3Ssthen #define UTIL_DATA_MSGENCODE_H
455d76a658Ssthen struct sldns_buffer;
46933707f3Ssthen struct query_info;
47933707f3Ssthen struct reply_info;
48933707f3Ssthen struct regional;
49933707f3Ssthen struct edns_data;
50933707f3Ssthen 
51933707f3Ssthen /**
52933707f3Ssthen  * Generate answer from reply_info.
53933707f3Ssthen  * @param qinf: query information that provides query section in packet.
54933707f3Ssthen  * @param rep: reply to fill in.
55933707f3Ssthen  * @param id: id word from the query.
56933707f3Ssthen  * @param qflags: flags word from the query.
57933707f3Ssthen  * @param dest: buffer to put message into; will truncate if it does not fit.
58933707f3Ssthen  * @param timenow: time to subtract.
59933707f3Ssthen  * @param cached: set true if a cached reply (so no AA bit).
60933707f3Ssthen  *	set false for the first reply.
61933707f3Ssthen  * @param region: where to allocate temp variables (for compression).
62933707f3Ssthen  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
63933707f3Ssthen  * @param edns: EDNS data included in the answer, NULL for none.
64933707f3Ssthen  *	or if edns_present = 0, it is not included.
65933707f3Ssthen  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
66933707f3Ssthen  * @param secure: if 1, the AD bit is set in the reply.
67933707f3Ssthen  * @return: 0 on error (server failure).
68933707f3Ssthen  */
69933707f3Ssthen int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
705d76a658Ssthen 	uint16_t id, uint16_t qflags, struct sldns_buffer* dest, time_t timenow,
71933707f3Ssthen 	int cached, struct regional* region, uint16_t udpsize,
72933707f3Ssthen 	struct edns_data* edns, int dnssec, int secure);
73933707f3Ssthen 
74933707f3Ssthen /**
75933707f3Ssthen  * Regenerate the wireformat from the stored msg reply.
76933707f3Ssthen  * If the buffer is too small then the message is truncated at a whole
77933707f3Ssthen  * rrset and the TC bit set, or whole rrsets are left out of the additional
78933707f3Ssthen  * and the TC bit is not set.
79933707f3Ssthen  * @param qinfo: query info to store.
80933707f3Ssthen  * @param rep: reply to store.
81933707f3Ssthen  * @param id: id value to store, network order.
82933707f3Ssthen  * @param flags: flags value to store, host order.
83933707f3Ssthen  * @param buffer: buffer to store the packet into.
84933707f3Ssthen  * @param timenow: time now, to adjust ttl values.
85933707f3Ssthen  * @param region: to store temporary data in.
86933707f3Ssthen  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
87933707f3Ssthen  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
88c981f76fSsthen  * @param minimise: if true, the answer is a minimal response, with
89c981f76fSsthen  *   authority and additional removed if possible.
90933707f3Ssthen  * @return: nonzero is success, or
91933707f3Ssthen  *	0 on error: malloc failure (no log_err has been done).
92933707f3Ssthen  */
93933707f3Ssthen int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
945d76a658Ssthen 	uint16_t id, uint16_t flags, struct sldns_buffer* buffer, time_t timenow,
95c981f76fSsthen 	struct regional* region, uint16_t udpsize, int dnssec, int minimise);
96933707f3Ssthen 
97933707f3Ssthen /**
98933707f3Ssthen  * Encode query packet. Assumes the buffer is large enough.
99933707f3Ssthen  * @param pkt: where to store the packet.
100933707f3Ssthen  * @param qinfo: query info.
101933707f3Ssthen  */
1025d76a658Ssthen void qinfo_query_encode(struct sldns_buffer* pkt, struct query_info* qinfo);
103933707f3Ssthen 
104933707f3Ssthen /**
105933707f3Ssthen  * Estimate size of EDNS record in packet. EDNS record will be no larger.
106933707f3Ssthen  * @param edns: edns data or NULL.
107933707f3Ssthen  * @return octets to reserve for EDNS.
108933707f3Ssthen  */
109933707f3Ssthen uint16_t calc_edns_field_size(struct edns_data* edns);
110933707f3Ssthen 
111933707f3Ssthen /**
112*437d2860Ssthen  * Calculate the size of a specific EDNS option in packet.
113*437d2860Ssthen  * @param edns: edns data or NULL.
114*437d2860Ssthen  * @param code: the opt code to get the size of.
115*437d2860Ssthen  * @return octets the option will take up.
116*437d2860Ssthen  */
117*437d2860Ssthen uint16_t calc_edns_option_size(struct edns_data* edns, uint16_t code);
118*437d2860Ssthen 
119*437d2860Ssthen /**
120*437d2860Ssthen  * Calculate the size of the EDE option(s) in packet. Also calculate seperately
121*437d2860Ssthen  * the size of the EXTRA-TEXT field(s) in case we can trim them to fit.
122*437d2860Ssthen  * In this case include any LDNS_EDE_OTHER options in their entirety since they
123*437d2860Ssthen  * are useless without extra text.
124*437d2860Ssthen  * @param edns: edns data or NULL.
125*437d2860Ssthen  * @param txt_size: the size of the EXTRA-TEXT field(s); this includes
126*437d2860Ssthen  *	LDNS_EDE_OTHER in their entirety since they are useless without
127*437d2860Ssthen  *	extra text.
128*437d2860Ssthen  * @return octets the option will take up.
129*437d2860Ssthen  */
130*437d2860Ssthen uint16_t calc_ede_option_size(struct edns_data* edns, uint16_t* txt_size);
131*437d2860Ssthen 
132*437d2860Ssthen /**
133933707f3Ssthen  * Attach EDNS record to buffer. Buffer has complete packet. There must
134933707f3Ssthen  * be enough room left for the EDNS record.
135933707f3Ssthen  * @param pkt: packet added to.
136933707f3Ssthen  * @param edns: if NULL or present=0, nothing is added to the packet.
137933707f3Ssthen  */
1385d76a658Ssthen void attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns);
139933707f3Ssthen 
140933707f3Ssthen /**
141933707f3Ssthen  * Encode an error. With QR and RA set.
142933707f3Ssthen  *
143933707f3Ssthen  * @param pkt: where to store the packet.
144*437d2860Ssthen  * @param r: RCODE value to encode (may contain extra flags).
145933707f3Ssthen  * @param qinfo: if not NULL, the query is included.
146933707f3Ssthen  * @param qid: query ID to set in packet. network order.
147933707f3Ssthen  * @param qflags: original query flags (to copy RD and CD bits). host order.
148933707f3Ssthen  * @param edns: if not NULL, this is the query edns info,
149933707f3Ssthen  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
150933707f3Ssthen  */
1515d76a658Ssthen void error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo,
152933707f3Ssthen 	uint16_t qid, uint16_t qflags, struct edns_data* edns);
153933707f3Ssthen 
154*437d2860Ssthen /**
155*437d2860Ssthen  * Encode an extended error. With QR and RA set.
156*437d2860Ssthen  *
157*437d2860Ssthen  * @param pkt: where to store the packet.
158*437d2860Ssthen  * @param rcode: Extended RCODE value to encode.
159*437d2860Ssthen  * @param qinfo: if not NULL, the query is included.
160*437d2860Ssthen  * @param qid: query ID to set in packet. network order.
161*437d2860Ssthen  * @param qflags: original query flags (to copy RD and CD bits). host order.
162*437d2860Ssthen  * @param xflags: extra flags to set (such as for example BIT_AA and/or BIT_TC)
163*437d2860Ssthen  * @param edns: if not NULL, this is the query edns info,
164*437d2860Ssthen  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
165*437d2860Ssthen  * 	Without edns extended errors (i.e. > 15) will not be conveyed.
166*437d2860Ssthen  */
167*437d2860Ssthen void extended_error_encode(struct sldns_buffer* pkt, uint16_t rcode,
168*437d2860Ssthen 	struct query_info* qinfo, uint16_t qid, uint16_t qflags,
169*437d2860Ssthen 	uint16_t xflags, struct edns_data* edns);
170*437d2860Ssthen 
171933707f3Ssthen #endif /* UTIL_DATA_MSGENCODE_H */
172