msgencode.h revision 269257
1116742Ssam/*
2116904Ssam * util/data/msgencode.h - encode compressed DNS messages.
3178354Ssam *
4116742Ssam * Copyright (c) 2007, NLnet Labs. All rights reserved.
5116742Ssam *
6116742Ssam * This software is open source.
7116742Ssam *
8116742Ssam * Redistribution and use in source and binary forms, with or without
9116742Ssam * modification, are permitted provided that the following conditions
10116904Ssam * are met:
11116904Ssam *
12116904Ssam * Redistributions of source code must retain the above copyright notice,
13116904Ssam * this list of conditions and the following disclaimer.
14116742Ssam *
15116904Ssam * Redistributions in binary form must reproduce the above copyright notice,
16116904Ssam * this list of conditions and the following disclaimer in the documentation
17116904Ssam * and/or other materials provided with the distribution.
18116904Ssam *
19116904Ssam * Neither the name of the NLNET LABS nor the names of its contributors may
20116904Ssam * be used to endorse or promote products derived from this software without
21116904Ssam * specific prior written permission.
22116904Ssam *
23116904Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24116904Ssam * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25116742Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26116742Ssam * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27116742Ssam * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28116742Ssam * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29116742Ssam * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30116742Ssam * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31116742Ssam * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32116742Ssam * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33116742Ssam * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34116742Ssam */
35178354Ssam
36116742Ssam/**
37116742Ssam * \file
38138568Ssam *
39170530Ssam * This file contains temporary data structures and routines to create
40170530Ssam * compressed DNS messages.
41116742Ssam */
42178354Ssam
43116742Ssam#ifndef UTIL_DATA_MSGENCODE_H
44116742Ssam#define UTIL_DATA_MSGENCODE_H
45116742Ssamstruct sldns_buffer;
46138568Ssamstruct query_info;
47116742Ssamstruct reply_info;
48116742Ssamstruct regional;
49178354Ssamstruct edns_data;
50178354Ssam
51178354Ssam/**
52178354Ssam * Generate answer from reply_info.
53195618Srpaulo * @param qinf: query information that provides query section in packet.
54195618Srpaulo * @param rep: reply to fill in.
55195618Srpaulo * @param id: id word from the query.
56178354Ssam * @param qflags: flags word from the query.
57178354Ssam * @param dest: buffer to put message into; will truncate if it does not fit.
58116742Ssam * @param timenow: time to subtract.
59138568Ssam * @param cached: set true if a cached reply (so no AA bit).
60138568Ssam *	set false for the first reply.
61138568Ssam * @param region: where to allocate temp variables (for compression).
62116742Ssam * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
63116742Ssam * @param edns: EDNS data included in the answer, NULL for none.
64116742Ssam *	or if edns_present = 0, it is not included.
65116742Ssam * @param dnssec: if 0 DNSSEC records are omitted from the answer.
66116742Ssam * @param secure: if 1, the AD bit is set in the reply.
67218927Sbschmidt * @return: 0 on error (server failure).
68116742Ssam */
69138568Ssamint reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
70138568Ssam	uint16_t id, uint16_t qflags, struct sldns_buffer* dest, time_t timenow,
71138568Ssam	int cached, struct regional* region, uint16_t udpsize,
72138568Ssam	struct edns_data* edns, int dnssec, int secure);
73138568Ssam
74138568Ssam/**
75167283Ssam * Regenerate the wireformat from the stored msg reply.
76167283Ssam * If the buffer is too small then the message is truncated at a whole
77167283Ssam * rrset and the TC bit set, or whole rrsets are left out of the additional
78178354Ssam * and the TC bit is not set.
79167283Ssam * @param qinfo: query info to store.
80167283Ssam * @param rep: reply to store.
81195618Srpaulo * @param id: id value to store, network order.
82195618Srpaulo * @param flags: flags value to store, host order.
83167283Ssam * @param buffer: buffer to store the packet into.
84117811Ssam * @param timenow: time now, to adjust ttl values.
85117811Ssam * @param region: to store temporary data in.
86117811Ssam * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
87117811Ssam * @param dnssec: if 0 DNSSEC records are omitted from the answer.
88117811Ssam * @return: nonzero is success, or
89172058Ssam *	0 on error: malloc failure (no log_err has been done).
90172058Ssam */
91172058Ssamint reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
92172058Ssam	uint16_t id, uint16_t flags, struct sldns_buffer* buffer, time_t timenow,
93117811Ssam	struct regional* region, uint16_t udpsize, int dnssec);
94138568Ssam
95138568Ssam/**
96138568Ssam * Encode query packet. Assumes the buffer is large enough.
97138568Ssam * @param pkt: where to store the packet.
98138568Ssam * @param qinfo: query info.
99138568Ssam */
100138568Ssamvoid qinfo_query_encode(struct sldns_buffer* pkt, struct query_info* qinfo);
101116742Ssam
102191746Sthompsa/**
103191746Sthompsa * Estimate size of EDNS record in packet. EDNS record will be no larger.
104178354Ssam * @param edns: edns data or NULL.
105191746Sthompsa * @return octets to reserve for EDNS.
106191746Sthompsa */
107191746Sthompsauint16_t calc_edns_field_size(struct edns_data* edns);
108233452Sadrian
109191746Sthompsa/**
110117811Ssam * Attach EDNS record to buffer. Buffer has complete packet. There must
111178354Ssam * be enough room left for the EDNS record.
112178354Ssam * @param pkt: packet added to.
113178354Ssam * @param edns: if NULL or present=0, nothing is added to the packet.
114172211Ssam */
115178354Ssamvoid attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns);
116178354Ssam
117178354Ssam/**
118178354Ssam * Encode an error. With QR and RA set.
119178354Ssam *
120172211Ssam * @param pkt: where to store the packet.
121172211Ssam * @param r: RCODE value to encode.
122116742Ssam * @param qinfo: if not NULL, the query is included.
123138568Ssam * @param qid: query ID to set in packet. network order.
124116742Ssam * @param qflags: original query flags (to copy RD and CD bits). host order.
125138568Ssam * @param edns: if not NULL, this is the query edns info,
126116742Ssam * 	and an edns reply is attached. Only attached if EDNS record fits reply.
127178354Ssam */
128178354Ssamvoid error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo,
129178354Ssam	uint16_t qid, uint16_t qflags, struct edns_data* edns);
130178354Ssam
131178354Ssam#endif /* UTIL_DATA_MSGENCODE_H */
132178354Ssam