xref: /freebsd/contrib/unbound/util/data/msgencode.h (revision b7579f77d18196a58ff700756c84dc9a302a7f67)
1*b7579f77SDag-Erling Smørgrav /*
2*b7579f77SDag-Erling Smørgrav  * util/data/msgencode.h - encode compressed DNS messages.
3*b7579f77SDag-Erling Smørgrav  *
4*b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*b7579f77SDag-Erling Smørgrav  *
6*b7579f77SDag-Erling Smørgrav  * This software is open source.
7*b7579f77SDag-Erling Smørgrav  *
8*b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9*b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10*b7579f77SDag-Erling Smørgrav  * are met:
11*b7579f77SDag-Erling Smørgrav  *
12*b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13*b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14*b7579f77SDag-Erling Smørgrav  *
15*b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16*b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17*b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18*b7579f77SDag-Erling Smørgrav  *
19*b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20*b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21*b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22*b7579f77SDag-Erling Smørgrav  *
23*b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*b7579f77SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25*b7579f77SDag-Erling Smørgrav  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*b7579f77SDag-Erling Smørgrav  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27*b7579f77SDag-Erling Smørgrav  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*b7579f77SDag-Erling Smørgrav  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*b7579f77SDag-Erling Smørgrav  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30*b7579f77SDag-Erling Smørgrav  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31*b7579f77SDag-Erling Smørgrav  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32*b7579f77SDag-Erling Smørgrav  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33*b7579f77SDag-Erling Smørgrav  * POSSIBILITY OF SUCH DAMAGE.
34*b7579f77SDag-Erling Smørgrav  */
35*b7579f77SDag-Erling Smørgrav 
36*b7579f77SDag-Erling Smørgrav /**
37*b7579f77SDag-Erling Smørgrav  * \file
38*b7579f77SDag-Erling Smørgrav  *
39*b7579f77SDag-Erling Smørgrav  * This file contains temporary data structures and routines to create
40*b7579f77SDag-Erling Smørgrav  * compressed DNS messages.
41*b7579f77SDag-Erling Smørgrav  */
42*b7579f77SDag-Erling Smørgrav 
43*b7579f77SDag-Erling Smørgrav #ifndef UTIL_DATA_MSGENCODE_H
44*b7579f77SDag-Erling Smørgrav #define UTIL_DATA_MSGENCODE_H
45*b7579f77SDag-Erling Smørgrav struct query_info;
46*b7579f77SDag-Erling Smørgrav struct reply_info;
47*b7579f77SDag-Erling Smørgrav struct regional;
48*b7579f77SDag-Erling Smørgrav struct edns_data;
49*b7579f77SDag-Erling Smørgrav 
50*b7579f77SDag-Erling Smørgrav /**
51*b7579f77SDag-Erling Smørgrav  * Generate answer from reply_info.
52*b7579f77SDag-Erling Smørgrav  * @param qinf: query information that provides query section in packet.
53*b7579f77SDag-Erling Smørgrav  * @param rep: reply to fill in.
54*b7579f77SDag-Erling Smørgrav  * @param id: id word from the query.
55*b7579f77SDag-Erling Smørgrav  * @param qflags: flags word from the query.
56*b7579f77SDag-Erling Smørgrav  * @param dest: buffer to put message into; will truncate if it does not fit.
57*b7579f77SDag-Erling Smørgrav  * @param timenow: time to subtract.
58*b7579f77SDag-Erling Smørgrav  * @param cached: set true if a cached reply (so no AA bit).
59*b7579f77SDag-Erling Smørgrav  *	set false for the first reply.
60*b7579f77SDag-Erling Smørgrav  * @param region: where to allocate temp variables (for compression).
61*b7579f77SDag-Erling Smørgrav  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
62*b7579f77SDag-Erling Smørgrav  * @param edns: EDNS data included in the answer, NULL for none.
63*b7579f77SDag-Erling Smørgrav  *	or if edns_present = 0, it is not included.
64*b7579f77SDag-Erling Smørgrav  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
65*b7579f77SDag-Erling Smørgrav  * @param secure: if 1, the AD bit is set in the reply.
66*b7579f77SDag-Erling Smørgrav  * @return: 0 on error (server failure).
67*b7579f77SDag-Erling Smørgrav  */
68*b7579f77SDag-Erling Smørgrav int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
69*b7579f77SDag-Erling Smørgrav 	uint16_t id, uint16_t qflags, ldns_buffer* dest, uint32_t timenow,
70*b7579f77SDag-Erling Smørgrav 	int cached, struct regional* region, uint16_t udpsize,
71*b7579f77SDag-Erling Smørgrav 	struct edns_data* edns, int dnssec, int secure);
72*b7579f77SDag-Erling Smørgrav 
73*b7579f77SDag-Erling Smørgrav /**
74*b7579f77SDag-Erling Smørgrav  * Regenerate the wireformat from the stored msg reply.
75*b7579f77SDag-Erling Smørgrav  * If the buffer is too small then the message is truncated at a whole
76*b7579f77SDag-Erling Smørgrav  * rrset and the TC bit set, or whole rrsets are left out of the additional
77*b7579f77SDag-Erling Smørgrav  * and the TC bit is not set.
78*b7579f77SDag-Erling Smørgrav  * @param qinfo: query info to store.
79*b7579f77SDag-Erling Smørgrav  * @param rep: reply to store.
80*b7579f77SDag-Erling Smørgrav  * @param id: id value to store, network order.
81*b7579f77SDag-Erling Smørgrav  * @param flags: flags value to store, host order.
82*b7579f77SDag-Erling Smørgrav  * @param buffer: buffer to store the packet into.
83*b7579f77SDag-Erling Smørgrav  * @param timenow: time now, to adjust ttl values.
84*b7579f77SDag-Erling Smørgrav  * @param region: to store temporary data in.
85*b7579f77SDag-Erling Smørgrav  * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
86*b7579f77SDag-Erling Smørgrav  * @param dnssec: if 0 DNSSEC records are omitted from the answer.
87*b7579f77SDag-Erling Smørgrav  * @return: nonzero is success, or
88*b7579f77SDag-Erling Smørgrav  *	0 on error: malloc failure (no log_err has been done).
89*b7579f77SDag-Erling Smørgrav  */
90*b7579f77SDag-Erling Smørgrav int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
91*b7579f77SDag-Erling Smørgrav 	uint16_t id, uint16_t flags, ldns_buffer* buffer, uint32_t timenow,
92*b7579f77SDag-Erling Smørgrav 	struct regional* region, uint16_t udpsize, int dnssec);
93*b7579f77SDag-Erling Smørgrav 
94*b7579f77SDag-Erling Smørgrav /**
95*b7579f77SDag-Erling Smørgrav  * Encode query packet. Assumes the buffer is large enough.
96*b7579f77SDag-Erling Smørgrav  * @param pkt: where to store the packet.
97*b7579f77SDag-Erling Smørgrav  * @param qinfo: query info.
98*b7579f77SDag-Erling Smørgrav  */
99*b7579f77SDag-Erling Smørgrav void qinfo_query_encode(ldns_buffer* pkt, struct query_info* qinfo);
100*b7579f77SDag-Erling Smørgrav 
101*b7579f77SDag-Erling Smørgrav /**
102*b7579f77SDag-Erling Smørgrav  * Estimate size of EDNS record in packet. EDNS record will be no larger.
103*b7579f77SDag-Erling Smørgrav  * @param edns: edns data or NULL.
104*b7579f77SDag-Erling Smørgrav  * @return octets to reserve for EDNS.
105*b7579f77SDag-Erling Smørgrav  */
106*b7579f77SDag-Erling Smørgrav uint16_t calc_edns_field_size(struct edns_data* edns);
107*b7579f77SDag-Erling Smørgrav 
108*b7579f77SDag-Erling Smørgrav /**
109*b7579f77SDag-Erling Smørgrav  * Attach EDNS record to buffer. Buffer has complete packet. There must
110*b7579f77SDag-Erling Smørgrav  * be enough room left for the EDNS record.
111*b7579f77SDag-Erling Smørgrav  * @param pkt: packet added to.
112*b7579f77SDag-Erling Smørgrav  * @param edns: if NULL or present=0, nothing is added to the packet.
113*b7579f77SDag-Erling Smørgrav  */
114*b7579f77SDag-Erling Smørgrav void attach_edns_record(ldns_buffer* pkt, struct edns_data* edns);
115*b7579f77SDag-Erling Smørgrav 
116*b7579f77SDag-Erling Smørgrav /**
117*b7579f77SDag-Erling Smørgrav  * Encode an error. With QR and RA set.
118*b7579f77SDag-Erling Smørgrav  *
119*b7579f77SDag-Erling Smørgrav  * @param pkt: where to store the packet.
120*b7579f77SDag-Erling Smørgrav  * @param r: RCODE value to encode.
121*b7579f77SDag-Erling Smørgrav  * @param qinfo: if not NULL, the query is included.
122*b7579f77SDag-Erling Smørgrav  * @param qid: query ID to set in packet. network order.
123*b7579f77SDag-Erling Smørgrav  * @param qflags: original query flags (to copy RD and CD bits). host order.
124*b7579f77SDag-Erling Smørgrav  * @param edns: if not NULL, this is the query edns info,
125*b7579f77SDag-Erling Smørgrav  * 	and an edns reply is attached. Only attached if EDNS record fits reply.
126*b7579f77SDag-Erling Smørgrav  */
127*b7579f77SDag-Erling Smørgrav void error_encode(ldns_buffer* pkt, int r, struct query_info* qinfo,
128*b7579f77SDag-Erling Smørgrav 	uint16_t qid, uint16_t qflags, struct edns_data* edns);
129*b7579f77SDag-Erling Smørgrav 
130*b7579f77SDag-Erling Smørgrav #endif /* UTIL_DATA_MSGENCODE_H */
131