xref: /freebsd/contrib/unbound/util/data/msgencode.h (revision 8f76bb7dad48538c6832c2fb466a433d2a3f8cd5)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * util/data/msgencode.h - encode compressed DNS messages.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains temporary data structures and routines to create
40b7579f77SDag-Erling Smørgrav  * compressed DNS messages.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #ifndef UTIL_DATA_MSGENCODE_H
44b7579f77SDag-Erling Smørgrav #define UTIL_DATA_MSGENCODE_H
4517d15b25SDag-Erling Smørgrav struct sldns_buffer;
46b7579f77SDag-Erling Smørgrav struct query_info;
47b7579f77SDag-Erling Smørgrav struct reply_info;
48b7579f77SDag-Erling Smørgrav struct regional;
49b7579f77SDag-Erling Smørgrav struct edns_data;
50b7579f77SDag-Erling Smørgrav 
51b7579f77SDag-Erling Smørgrav /**
52b7579f77SDag-Erling Smørgrav  * Generate answer from reply_info.
53b7579f77SDag-Erling Smørgrav  * @param qinf: query information that provides query section in packet.
54b7579f77SDag-Erling Smørgrav  * @param rep: reply to fill in.
55b7579f77SDag-Erling Smørgrav  * @param id: id word from the query.
56b7579f77SDag-Erling Smørgrav  * @param qflags: flags word from the query.
57b7579f77SDag-Erling Smørgrav  * @param dest: buffer to put message into; will truncate if it does not fit.
58b7579f77SDag-Erling Smørgrav  * @param timenow: time to subtract.
59b7579f77SDag-Erling Smørgrav  * @param cached: set true if a cached reply (so no AA bit).
60b7579f77SDag-Erling Smørgrav  *	set false for the first reply.
61b7579f77SDag-Erling Smørgrav  * @param region: where to allocate temp variables (for compression).
62b7579f77SDag-Erling Smørgrav  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
63b7579f77SDag-Erling Smørgrav  * @param edns: EDNS data included in the answer, NULL for none.
64b7579f77SDag-Erling Smørgrav  *	or if edns_present = 0, it is not included.
65b7579f77SDag-Erling Smørgrav  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
66b7579f77SDag-Erling Smørgrav  * @param secure: if 1, the AD bit is set in the reply.
67b7579f77SDag-Erling Smørgrav  * @return: 0 on error (server failure).
68b7579f77SDag-Erling Smørgrav  */
69b7579f77SDag-Erling Smørgrav int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
7017d15b25SDag-Erling Smørgrav 	uint16_t id, uint16_t qflags, struct sldns_buffer* dest, time_t timenow,
71b7579f77SDag-Erling Smørgrav 	int cached, struct regional* region, uint16_t udpsize,
72b7579f77SDag-Erling Smørgrav 	struct edns_data* edns, int dnssec, int secure);
73b7579f77SDag-Erling Smørgrav 
74b7579f77SDag-Erling Smørgrav /**
75b7579f77SDag-Erling Smørgrav  * Regenerate the wireformat from the stored msg reply.
76b7579f77SDag-Erling Smørgrav  * If the buffer is too small then the message is truncated at a whole
77b7579f77SDag-Erling Smørgrav  * rrset and the TC bit set, or whole rrsets are left out of the additional
78b7579f77SDag-Erling Smørgrav  * and the TC bit is not set.
79b7579f77SDag-Erling Smørgrav  * @param qinfo: query info to store.
80b7579f77SDag-Erling Smørgrav  * @param rep: reply to store.
81b7579f77SDag-Erling Smørgrav  * @param id: id value to store, network order.
82b7579f77SDag-Erling Smørgrav  * @param flags: flags value to store, host order.
83b7579f77SDag-Erling Smørgrav  * @param buffer: buffer to store the packet into.
84b7579f77SDag-Erling Smørgrav  * @param timenow: time now, to adjust ttl values.
85b7579f77SDag-Erling Smørgrav  * @param region: to store temporary data in.
86b7579f77SDag-Erling Smørgrav  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
87b7579f77SDag-Erling Smørgrav  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
880eefd307SCy Schubert  * @param minimise: if true, the answer is a minimal response, with
890eefd307SCy Schubert  *   authority and additional removed if possible.
90b7579f77SDag-Erling Smørgrav  * @return: nonzero is success, or
91b7579f77SDag-Erling Smørgrav  *	0 on error: malloc failure (no log_err has been done).
92b7579f77SDag-Erling Smørgrav  */
93b7579f77SDag-Erling Smørgrav int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
9417d15b25SDag-Erling Smørgrav 	uint16_t id, uint16_t flags, struct sldns_buffer* buffer, time_t timenow,
950eefd307SCy Schubert 	struct regional* region, uint16_t udpsize, int dnssec, int minimise);
96b7579f77SDag-Erling Smørgrav 
97b7579f77SDag-Erling Smørgrav /**
98b7579f77SDag-Erling Smørgrav  * Encode query packet. Assumes the buffer is large enough.
99b7579f77SDag-Erling Smørgrav  * @param pkt: where to store the packet.
100b7579f77SDag-Erling Smørgrav  * @param qinfo: query info.
101b7579f77SDag-Erling Smørgrav  */
10217d15b25SDag-Erling Smørgrav void qinfo_query_encode(struct sldns_buffer* pkt, struct query_info* qinfo);
103b7579f77SDag-Erling Smørgrav 
104b7579f77SDag-Erling Smørgrav /**
105b7579f77SDag-Erling Smørgrav  * Estimate size of EDNS record in packet. EDNS record will be no larger.
106b7579f77SDag-Erling Smørgrav  * @param edns: edns data or NULL.
107b7579f77SDag-Erling Smørgrav  * @return octets to reserve for EDNS.
108b7579f77SDag-Erling Smørgrav  */
109b7579f77SDag-Erling Smørgrav uint16_t calc_edns_field_size(struct edns_data* edns);
110b7579f77SDag-Erling Smørgrav 
111b7579f77SDag-Erling Smørgrav /**
112*8f76bb7dSCy Schubert  * Calculate the size of a specific EDNS option in packet.
113*8f76bb7dSCy Schubert  * @param edns: edns data or NULL.
114*8f76bb7dSCy Schubert  * @param code: the opt code to get the size of.
115*8f76bb7dSCy Schubert  * @return octets the option will take up.
116*8f76bb7dSCy Schubert  */
117*8f76bb7dSCy Schubert uint16_t calc_edns_option_size(struct edns_data* edns, uint16_t code);
118*8f76bb7dSCy Schubert 
119*8f76bb7dSCy Schubert /**
120*8f76bb7dSCy Schubert  * Calculate the size of the EDE option(s) in packet. Also calculate seperately
121*8f76bb7dSCy Schubert  * the size of the EXTRA-TEXT field(s) in case we can trim them to fit.
122*8f76bb7dSCy Schubert  * In this case include any LDNS_EDE_OTHER options in their entirety since they
123*8f76bb7dSCy Schubert  * are useless without extra text.
124*8f76bb7dSCy Schubert  * @param edns: edns data or NULL.
125*8f76bb7dSCy Schubert  * @param txt_size: the size of the EXTRA-TEXT field(s); this includes
126*8f76bb7dSCy Schubert  *	LDNS_EDE_OTHER in their entirety since they are useless without
127*8f76bb7dSCy Schubert  *	extra text.
128*8f76bb7dSCy Schubert  * @return octets the option will take up.
129*8f76bb7dSCy Schubert  */
130*8f76bb7dSCy Schubert uint16_t calc_ede_option_size(struct edns_data* edns, uint16_t* txt_size);
131*8f76bb7dSCy Schubert 
132*8f76bb7dSCy Schubert /**
133b7579f77SDag-Erling Smørgrav  * Attach EDNS record to buffer. Buffer has complete packet. There must
134b7579f77SDag-Erling Smørgrav  * be enough room left for the EDNS record.
135b7579f77SDag-Erling Smørgrav  * @param pkt: packet added to.
136b7579f77SDag-Erling Smørgrav  * @param edns: if NULL or present=0, nothing is added to the packet.
137b7579f77SDag-Erling Smørgrav  */
13817d15b25SDag-Erling Smørgrav void attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns);
139b7579f77SDag-Erling Smørgrav 
140b7579f77SDag-Erling Smørgrav /**
141b7579f77SDag-Erling Smørgrav  * Encode an error. With QR and RA set.
142b7579f77SDag-Erling Smørgrav  *
143b7579f77SDag-Erling Smørgrav  * @param pkt: where to store the packet.
144*8f76bb7dSCy Schubert  * @param r: RCODE value to encode (may contain extra flags).
145b7579f77SDag-Erling Smørgrav  * @param qinfo: if not NULL, the query is included.
146b7579f77SDag-Erling Smørgrav  * @param qid: query ID to set in packet. network order.
147b7579f77SDag-Erling Smørgrav  * @param qflags: original query flags (to copy RD and CD bits). host order.
148b7579f77SDag-Erling Smørgrav  * @param edns: if not NULL, this is the query edns info,
149b7579f77SDag-Erling Smørgrav  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
150b7579f77SDag-Erling Smørgrav  */
15117d15b25SDag-Erling Smørgrav void error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo,
152b7579f77SDag-Erling Smørgrav 	uint16_t qid, uint16_t qflags, struct edns_data* edns);
153b7579f77SDag-Erling Smørgrav 
154*8f76bb7dSCy Schubert /**
155*8f76bb7dSCy Schubert  * Encode an extended error. With QR and RA set.
156*8f76bb7dSCy Schubert  *
157*8f76bb7dSCy Schubert  * @param pkt: where to store the packet.
158*8f76bb7dSCy Schubert  * @param rcode: Extended RCODE value to encode.
159*8f76bb7dSCy Schubert  * @param qinfo: if not NULL, the query is included.
160*8f76bb7dSCy Schubert  * @param qid: query ID to set in packet. network order.
161*8f76bb7dSCy Schubert  * @param qflags: original query flags (to copy RD and CD bits). host order.
162*8f76bb7dSCy Schubert  * @param xflags: extra flags to set (such as for example BIT_AA and/or BIT_TC)
163*8f76bb7dSCy Schubert  * @param edns: if not NULL, this is the query edns info,
164*8f76bb7dSCy Schubert  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
165*8f76bb7dSCy Schubert  * 	Without edns extended errors (i.e. > 15) will not be conveyed.
166*8f76bb7dSCy Schubert  */
167*8f76bb7dSCy Schubert void extended_error_encode(struct sldns_buffer* pkt, uint16_t rcode,
168*8f76bb7dSCy Schubert 	struct query_info* qinfo, uint16_t qid, uint16_t qflags,
169*8f76bb7dSCy Schubert 	uint16_t xflags, struct edns_data* edns);
170*8f76bb7dSCy Schubert 
171b7579f77SDag-Erling Smørgrav #endif /* UTIL_DATA_MSGENCODE_H */
172