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 LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * 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 query_info; 46 struct reply_info; 47 struct regional; 48 struct edns_data; 49 50 /** 51 * Generate answer from reply_info. 52 * @param qinf: query information that provides query section in packet. 53 * @param rep: reply to fill in. 54 * @param id: id word from the query. 55 * @param qflags: flags word from the query. 56 * @param dest: buffer to put message into; will truncate if it does not fit. 57 * @param timenow: time to subtract. 58 * @param cached: set true if a cached reply (so no AA bit). 59 * set false for the first reply. 60 * @param region: where to allocate temp variables (for compression). 61 * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP. 62 * @param edns: EDNS data included in the answer, NULL for none. 63 * or if edns_present = 0, it is not included. 64 * @param dnssec: if 0 DNSSEC records are omitted from the answer. 65 * @param secure: if 1, the AD bit is set in the reply. 66 * @return: 0 on error (server failure). 67 */ 68 int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep, 69 uint16_t id, uint16_t qflags, ldns_buffer* dest, uint32_t timenow, 70 int cached, struct regional* region, uint16_t udpsize, 71 struct edns_data* edns, int dnssec, int secure); 72 73 /** 74 * Regenerate the wireformat from the stored msg reply. 75 * If the buffer is too small then the message is truncated at a whole 76 * rrset and the TC bit set, or whole rrsets are left out of the additional 77 * and the TC bit is not set. 78 * @param qinfo: query info to store. 79 * @param rep: reply to store. 80 * @param id: id value to store, network order. 81 * @param flags: flags value to store, host order. 82 * @param buffer: buffer to store the packet into. 83 * @param timenow: time now, to adjust ttl values. 84 * @param region: to store temporary data in. 85 * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP. 86 * @param dnssec: if 0 DNSSEC records are omitted from the answer. 87 * @return: nonzero is success, or 88 * 0 on error: malloc failure (no log_err has been done). 89 */ 90 int reply_info_encode(struct query_info* qinfo, struct reply_info* rep, 91 uint16_t id, uint16_t flags, ldns_buffer* buffer, uint32_t timenow, 92 struct regional* region, uint16_t udpsize, int dnssec); 93 94 /** 95 * Encode query packet. Assumes the buffer is large enough. 96 * @param pkt: where to store the packet. 97 * @param qinfo: query info. 98 */ 99 void qinfo_query_encode(ldns_buffer* pkt, struct query_info* qinfo); 100 101 /** 102 * Estimate size of EDNS record in packet. EDNS record will be no larger. 103 * @param edns: edns data or NULL. 104 * @return octets to reserve for EDNS. 105 */ 106 uint16_t calc_edns_field_size(struct edns_data* edns); 107 108 /** 109 * Attach EDNS record to buffer. Buffer has complete packet. There must 110 * be enough room left for the EDNS record. 111 * @param pkt: packet added to. 112 * @param edns: if NULL or present=0, nothing is added to the packet. 113 */ 114 void attach_edns_record(ldns_buffer* pkt, struct edns_data* edns); 115 116 /** 117 * Encode an error. With QR and RA set. 118 * 119 * @param pkt: where to store the packet. 120 * @param r: RCODE value to encode. 121 * @param qinfo: if not NULL, the query is included. 122 * @param qid: query ID to set in packet. network order. 123 * @param qflags: original query flags (to copy RD and CD bits). host order. 124 * @param edns: if not NULL, this is the query edns info, 125 * and an edns reply is attached. Only attached if EDNS record fits reply. 126 */ 127 void error_encode(ldns_buffer* pkt, int r, struct query_info* qinfo, 128 uint16_t qid, uint16_t qflags, struct edns_data* edns); 129 130 #endif /* UTIL_DATA_MSGENCODE_H */ 131