1238106Sdes/* 2238106Sdes * util/data/msgencode.h - encode compressed DNS messages. 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24266114Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25266114Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26266114Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27266114Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28266114Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29266114Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30266114Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31266114Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32266114Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33266114Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * This file contains temporary data structures and routines to create 40238106Sdes * compressed DNS messages. 41238106Sdes */ 42238106Sdes 43238106Sdes#ifndef UTIL_DATA_MSGENCODE_H 44238106Sdes#define UTIL_DATA_MSGENCODE_H 45266114Sdesstruct sldns_buffer; 46238106Sdesstruct query_info; 47238106Sdesstruct reply_info; 48238106Sdesstruct regional; 49238106Sdesstruct edns_data; 50238106Sdes 51238106Sdes/** 52238106Sdes * Generate answer from reply_info. 53238106Sdes * @param qinf: query information that provides query section in packet. 54238106Sdes * @param rep: reply to fill in. 55238106Sdes * @param id: id word from the query. 56238106Sdes * @param qflags: flags word from the query. 57238106Sdes * @param dest: buffer to put message into; will truncate if it does not fit. 58238106Sdes * @param timenow: time to subtract. 59238106Sdes * @param cached: set true if a cached reply (so no AA bit). 60238106Sdes * set false for the first reply. 61238106Sdes * @param region: where to allocate temp variables (for compression). 62238106Sdes * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP. 63238106Sdes * @param edns: EDNS data included in the answer, NULL for none. 64238106Sdes * or if edns_present = 0, it is not included. 65238106Sdes * @param dnssec: if 0 DNSSEC records are omitted from the answer. 66238106Sdes * @param secure: if 1, the AD bit is set in the reply. 67238106Sdes * @return: 0 on error (server failure). 68238106Sdes */ 69238106Sdesint reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep, 70266114Sdes uint16_t id, uint16_t qflags, struct sldns_buffer* dest, time_t timenow, 71238106Sdes int cached, struct regional* region, uint16_t udpsize, 72238106Sdes struct edns_data* edns, int dnssec, int secure); 73238106Sdes 74238106Sdes/** 75238106Sdes * Regenerate the wireformat from the stored msg reply. 76238106Sdes * If the buffer is too small then the message is truncated at a whole 77238106Sdes * rrset and the TC bit set, or whole rrsets are left out of the additional 78238106Sdes * and the TC bit is not set. 79238106Sdes * @param qinfo: query info to store. 80238106Sdes * @param rep: reply to store. 81238106Sdes * @param id: id value to store, network order. 82238106Sdes * @param flags: flags value to store, host order. 83238106Sdes * @param buffer: buffer to store the packet into. 84238106Sdes * @param timenow: time now, to adjust ttl values. 85238106Sdes * @param region: to store temporary data in. 86238106Sdes * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP. 87238106Sdes * @param dnssec: if 0 DNSSEC records are omitted from the answer. 88356345Scy * @param minimise: if true, the answer is a minimal response, with 89356345Scy * authority and additional removed if possible. 90238106Sdes * @return: nonzero is success, or 91238106Sdes * 0 on error: malloc failure (no log_err has been done). 92238106Sdes */ 93238106Sdesint reply_info_encode(struct query_info* qinfo, struct reply_info* rep, 94266114Sdes uint16_t id, uint16_t flags, struct sldns_buffer* buffer, time_t timenow, 95356345Scy struct regional* region, uint16_t udpsize, int dnssec, int minimise); 96238106Sdes 97238106Sdes/** 98238106Sdes * Encode query packet. Assumes the buffer is large enough. 99238106Sdes * @param pkt: where to store the packet. 100238106Sdes * @param qinfo: query info. 101238106Sdes */ 102266114Sdesvoid qinfo_query_encode(struct sldns_buffer* pkt, struct query_info* qinfo); 103238106Sdes 104238106Sdes/** 105238106Sdes * Estimate size of EDNS record in packet. EDNS record will be no larger. 106238106Sdes * @param edns: edns data or NULL. 107238106Sdes * @return octets to reserve for EDNS. 108238106Sdes */ 109238106Sdesuint16_t calc_edns_field_size(struct edns_data* edns); 110238106Sdes 111238106Sdes/** 112238106Sdes * Attach EDNS record to buffer. Buffer has complete packet. There must 113238106Sdes * be enough room left for the EDNS record. 114238106Sdes * @param pkt: packet added to. 115238106Sdes * @param edns: if NULL or present=0, nothing is added to the packet. 116238106Sdes */ 117266114Sdesvoid attach_edns_record(struct sldns_buffer* pkt, struct edns_data* edns); 118238106Sdes 119238106Sdes/** 120238106Sdes * Encode an error. With QR and RA set. 121238106Sdes * 122238106Sdes * @param pkt: where to store the packet. 123238106Sdes * @param r: RCODE value to encode. 124238106Sdes * @param qinfo: if not NULL, the query is included. 125238106Sdes * @param qid: query ID to set in packet. network order. 126238106Sdes * @param qflags: original query flags (to copy RD and CD bits). host order. 127238106Sdes * @param edns: if not NULL, this is the query edns info, 128238106Sdes * and an edns reply is attached. Only attached if EDNS record fits reply. 129238106Sdes */ 130266114Sdesvoid error_encode(struct sldns_buffer* pkt, int r, struct query_info* qinfo, 131238106Sdes uint16_t qid, uint16_t qflags, struct edns_data* edns); 132238106Sdes 133238106Sdes#endif /* UTIL_DATA_MSGENCODE_H */ 134