msgencode.h revision 238106
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 24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33238106Sdes * 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 45238106Sdesstruct query_info; 46238106Sdesstruct reply_info; 47238106Sdesstruct regional; 48238106Sdesstruct edns_data; 49238106Sdes 50238106Sdes/** 51238106Sdes * Generate answer from reply_info. 52238106Sdes * @param qinf: query information that provides query section in packet. 53238106Sdes * @param rep: reply to fill in. 54238106Sdes * @param id: id word from the query. 55238106Sdes * @param qflags: flags word from the query. 56238106Sdes * @param dest: buffer to put message into; will truncate if it does not fit. 57238106Sdes * @param timenow: time to subtract. 58238106Sdes * @param cached: set true if a cached reply (so no AA bit). 59238106Sdes * set false for the first reply. 60238106Sdes * @param region: where to allocate temp variables (for compression). 61238106Sdes * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP. 62238106Sdes * @param edns: EDNS data included in the answer, NULL for none. 63238106Sdes * or if edns_present = 0, it is not included. 64238106Sdes * @param dnssec: if 0 DNSSEC records are omitted from the answer. 65238106Sdes * @param secure: if 1, the AD bit is set in the reply. 66238106Sdes * @return: 0 on error (server failure). 67238106Sdes */ 68238106Sdesint reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep, 69238106Sdes uint16_t id, uint16_t qflags, ldns_buffer* dest, uint32_t timenow, 70238106Sdes int cached, struct regional* region, uint16_t udpsize, 71238106Sdes struct edns_data* edns, int dnssec, int secure); 72238106Sdes 73238106Sdes/** 74238106Sdes * Regenerate the wireformat from the stored msg reply. 75238106Sdes * If the buffer is too small then the message is truncated at a whole 76238106Sdes * rrset and the TC bit set, or whole rrsets are left out of the additional 77238106Sdes * and the TC bit is not set. 78238106Sdes * @param qinfo: query info to store. 79238106Sdes * @param rep: reply to store. 80238106Sdes * @param id: id value to store, network order. 81238106Sdes * @param flags: flags value to store, host order. 82238106Sdes * @param buffer: buffer to store the packet into. 83238106Sdes * @param timenow: time now, to adjust ttl values. 84238106Sdes * @param region: to store temporary data in. 85238106Sdes * @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP. 86238106Sdes * @param dnssec: if 0 DNSSEC records are omitted from the answer. 87238106Sdes * @return: nonzero is success, or 88238106Sdes * 0 on error: malloc failure (no log_err has been done). 89238106Sdes */ 90238106Sdesint reply_info_encode(struct query_info* qinfo, struct reply_info* rep, 91238106Sdes uint16_t id, uint16_t flags, ldns_buffer* buffer, uint32_t timenow, 92238106Sdes struct regional* region, uint16_t udpsize, int dnssec); 93238106Sdes 94238106Sdes/** 95238106Sdes * Encode query packet. Assumes the buffer is large enough. 96238106Sdes * @param pkt: where to store the packet. 97238106Sdes * @param qinfo: query info. 98238106Sdes */ 99238106Sdesvoid qinfo_query_encode(ldns_buffer* pkt, struct query_info* qinfo); 100238106Sdes 101238106Sdes/** 102238106Sdes * Estimate size of EDNS record in packet. EDNS record will be no larger. 103238106Sdes * @param edns: edns data or NULL. 104238106Sdes * @return octets to reserve for EDNS. 105238106Sdes */ 106238106Sdesuint16_t calc_edns_field_size(struct edns_data* edns); 107238106Sdes 108238106Sdes/** 109238106Sdes * Attach EDNS record to buffer. Buffer has complete packet. There must 110238106Sdes * be enough room left for the EDNS record. 111238106Sdes * @param pkt: packet added to. 112238106Sdes * @param edns: if NULL or present=0, nothing is added to the packet. 113238106Sdes */ 114238106Sdesvoid attach_edns_record(ldns_buffer* pkt, struct edns_data* edns); 115238106Sdes 116238106Sdes/** 117238106Sdes * Encode an error. With QR and RA set. 118238106Sdes * 119238106Sdes * @param pkt: where to store the packet. 120238106Sdes * @param r: RCODE value to encode. 121238106Sdes * @param qinfo: if not NULL, the query is included. 122238106Sdes * @param qid: query ID to set in packet. network order. 123238106Sdes * @param qflags: original query flags (to copy RD and CD bits). host order. 124238106Sdes * @param edns: if not NULL, this is the query edns info, 125238106Sdes * and an edns reply is attached. Only attached if EDNS record fits reply. 126238106Sdes */ 127238106Sdesvoid error_encode(ldns_buffer* pkt, int r, struct query_info* qinfo, 128238106Sdes uint16_t qid, uint16_t qflags, struct edns_data* edns); 129238106Sdes 130238106Sdes#endif /* UTIL_DATA_MSGENCODE_H */ 131