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 * Attach EDNS record to buffer. Buffer has complete packet. There must 113 * be enough room left for the EDNS record. 114 * @param pkt: packet added to. 115 * @param edns: if NULL or present=0, nothing is added to the packet. 116 */ 117 void attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns); 118 119 /** 120 * Encode an error. With QR and RA set. 121 * 122 * @param pkt: where to store the packet. 123 * @param r: RCODE value to encode. 124 * @param qinfo: if not NULL, the query is included. 125 * @param qid: query ID to set in packet. network order. 126 * @param qflags: original query flags (to copy RD and CD bits). host order. 127 * @param edns: if not NULL, this is the query edns info, 128 * and an edns reply is attached. Only attached if EDNS record fits reply. 129 */ 130 void error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo, 131 uint16_t qid, uint16_t qflags, struct edns_data* edns); 132 133 #endif /* UTIL_DATA_MSGENCODE_H */ 134