xref: /freebsd/contrib/unbound/util/data/msgencode.h (revision ca57a343e86ed3015596db68bda17518ad45436a)
1 /*
2  * util/data/msgencode.h - encode compressed DNS messages.
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 temporary data structures and routines to create
40  * compressed DNS messages.
41  */
42 
43 #ifndef UTIL_DATA_MSGENCODE_H
44 #define UTIL_DATA_MSGENCODE_H
45 struct sldns_buffer;
46 struct query_info;
47 struct reply_info;
48 struct regional;
49 struct edns_data;
50 
51 /**
52  * Generate answer from reply_info.
53  * @param qinf: query information that provides query section in packet.
54  * @param rep: reply to fill in.
55  * @param id: id word from the query.
56  * @param qflags: flags word from the query.
57  * @param dest: buffer to put message into; will truncate if it does not fit.
58  * @param timenow: time to subtract.
59  * @param cached: set true if a cached reply (so no AA bit).
60  *	set false for the first reply.
61  * @param region: where to allocate temp variables (for compression).
62  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
63  * @param edns: EDNS data included in the answer, NULL for none.
64  *	or if edns_present = 0, it is not included.
65  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
66  * @param secure: if 1, the AD bit is set in the reply.
67  * @return: 0 on error (server failure).
68  */
69 int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
70 	uint16_t id, uint16_t qflags, struct sldns_buffer* dest, time_t timenow,
71 	int cached, struct regional* region, uint16_t udpsize,
72 	struct edns_data* edns, int dnssec, int secure);
73 
74 /**
75  * Regenerate the wireformat from the stored msg reply.
76  * If the buffer is too small then the message is truncated at a whole
77  * rrset and the TC bit set, or whole rrsets are left out of the additional
78  * and the TC bit is not set.
79  * @param qinfo: query info to store.
80  * @param rep: reply to store.
81  * @param id: id value to store, network order.
82  * @param flags: flags value to store, host order.
83  * @param buffer: buffer to store the packet into.
84  * @param timenow: time now, to adjust ttl values.
85  * @param region: to store temporary data in.
86  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
87  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
88  * @param minimise: if true, the answer is a minimal response, with
89  *   authority and additional removed if possible.
90  * @return: nonzero is success, or
91  *	0 on error: malloc failure (no log_err has been done).
92  */
93 int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
94 	uint16_t id, uint16_t flags, struct sldns_buffer* buffer, time_t timenow,
95 	struct regional* region, uint16_t udpsize, int dnssec, int minimise);
96 
97 /**
98  * Encode query packet. Assumes the buffer is large enough.
99  * @param pkt: where to store the packet.
100  * @param qinfo: query info.
101  */
102 void qinfo_query_encode(struct sldns_buffer* pkt, struct query_info* qinfo);
103 
104 /**
105  * Estimate size of EDNS record in packet. EDNS record will be no larger.
106  * @param edns: edns data or NULL.
107  * @return octets to reserve for EDNS.
108  */
109 uint16_t calc_edns_field_size(struct edns_data* edns);
110 
111 /**
112  * Calculate the size of a specific EDNS option in packet.
113  * @param edns: edns data or NULL.
114  * @param code: the opt code to get the size of.
115  * @return octets the option will take up.
116  */
117 uint16_t calc_edns_option_size(struct edns_data* edns, uint16_t code);
118 
119 /**
120  * Calculate the size of the EDE option(s) in packet. Also calculate seperately
121  * the size of the EXTRA-TEXT field(s) in case we can trim them to fit.
122  * In this case include any LDNS_EDE_OTHER options in their entirety since they
123  * are useless without extra text.
124  * @param edns: edns data or NULL.
125  * @param txt_size: the size of the EXTRA-TEXT field(s); this includes
126  *	LDNS_EDE_OTHER in their entirety since they are useless without
127  *	extra text.
128  * @return octets the option will take up.
129  */
130 uint16_t calc_ede_option_size(struct edns_data* edns, uint16_t* txt_size);
131 
132 /**
133  * Attach EDNS record to buffer. Buffer has complete packet. There must
134  * be enough room left for the EDNS record.
135  * @param pkt: packet added to.
136  * @param edns: if NULL or present=0, nothing is added to the packet.
137  */
138 void attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns);
139 
140 /**
141  * Encode an error. With QR and RA set.
142  *
143  * @param pkt: where to store the packet.
144  * @param r: RCODE value to encode (may contain extra flags).
145  * @param qinfo: if not NULL, the query is included.
146  * @param qid: query ID to set in packet. network order.
147  * @param qflags: original query flags (to copy RD and CD bits). host order.
148  * @param edns: if not NULL, this is the query edns info,
149  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
150  */
151 void error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo,
152 	uint16_t qid, uint16_t qflags, struct edns_data* edns);
153 
154 /**
155  * Encode an extended error. With QR and RA set.
156  *
157  * @param pkt: where to store the packet.
158  * @param rcode: Extended RCODE value to encode.
159  * @param qinfo: if not NULL, the query is included.
160  * @param qid: query ID to set in packet. network order.
161  * @param qflags: original query flags (to copy RD and CD bits). host order.
162  * @param xflags: extra flags to set (such as for example BIT_AA and/or BIT_TC)
163  * @param edns: if not NULL, this is the query edns info,
164  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
165  * 	Without edns extended errors (i.e. > 15) will not be conveyed.
166  */
167 void extended_error_encode(struct sldns_buffer* pkt, uint16_t rcode,
168 	struct query_info* qinfo, uint16_t qid, uint16_t qflags,
169 	uint16_t xflags, struct edns_data* edns);
170 
171 #endif /* UTIL_DATA_MSGENCODE_H */
172