xref: /freebsd/contrib/unbound/util/data/msgencode.h (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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  * @return: nonzero is success, or
89  *	0 on error: malloc failure (no log_err has been done).
90  */
91 int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
92 	uint16_t id, uint16_t flags, struct sldns_buffer* buffer, time_t timenow,
93 	struct regional* region, uint16_t udpsize, int dnssec);
94 
95 /**
96  * Encode query packet. Assumes the buffer is large enough.
97  * @param pkt: where to store the packet.
98  * @param qinfo: query info.
99  */
100 void qinfo_query_encode(struct sldns_buffer* pkt, struct query_info* qinfo);
101 
102 /**
103  * Estimate size of EDNS record in packet. EDNS record will be no larger.
104  * @param edns: edns data or NULL.
105  * @return octets to reserve for EDNS.
106  */
107 uint16_t calc_edns_field_size(struct edns_data* edns);
108 
109 /**
110  * Attach EDNS record to buffer. Buffer has complete packet. There must
111  * be enough room left for the EDNS record.
112  * @param pkt: packet added to.
113  * @param edns: if NULL or present=0, nothing is added to the packet.
114  */
115 void attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns);
116 
117 /**
118  * Encode an error. With QR and RA set.
119  *
120  * @param pkt: where to store the packet.
121  * @param r: RCODE value to encode.
122  * @param qinfo: if not NULL, the query is included.
123  * @param qid: query ID to set in packet. network order.
124  * @param qflags: original query flags (to copy RD and CD bits). host order.
125  * @param edns: if not NULL, this is the query edns info,
126  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
127  */
128 void error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo,
129 	uint16_t qid, uint16_t qflags, struct edns_data* edns);
130 
131 #endif /* UTIL_DATA_MSGENCODE_H */
132