📄 message.h
字号:
/* * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1999-2003 Internet Software Consortium. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. *//* $Id: message.h,v 1.100.2.3.8.7 2004/03/08 02:08:00 marka Exp $ */#ifndef DNS_MESSAGE_H#define DNS_MESSAGE_H 1/*** *** Imports ***/#include <isc/lang.h>#include <isc/magic.h>#include <dns/compress.h>#include <dns/masterdump.h>#include <dns/types.h>#include <dst/dst.h>/* * How this beast works: * * When a dns message is received in a buffer, dns_message_fromwire() is called * on the memory region. Various items are checked including the format * of the message (if counts are right, if counts consume the entire sections, * and if sections consume the entire message) and known pseudo-RRs in the * additional data section are analyzed and removed. * * TSIG checking is also done at this layer, and any DNSSEC transaction * signatures should also be checked here. * * Notes on using the gettemp*() and puttemp*() functions: * * These functions return items (names, rdatasets, etc) allocated from some * internal state of the dns_message_t. * * Names and rdatasets must be put back into the dns_message_t in * one of two ways. Assume a name was allocated via * dns_message_gettempname(): * * (1) insert it into a section, using dns_message_addname(). * * (2) return it to the message using dns_message_puttempname(). * * The same applies to rdatasets. * * On the other hand, offsets, rdatalists and rdatas allocated using * dns_message_gettemp*() will always be freed automatically * when the message is reset or destroyed; calling dns_message_puttemp*() * on rdatalists and rdatas is optional and serves only to enable the item * to be reused multiple times during the lifetime of the message; offsets * cannot be reused. * * Buffers allocated using isc_buffer_allocate() can be automatically freed * as well by giving the buffer to the message using dns_message_takebuffer(). * Doing this will cause the buffer to be freed using isc_buffer_free() * when the section lists are cleared, such as in a reset or in a destroy. * Since the buffer itself exists until the message is destroyed, this sort * of code can be written: * * buffer = isc_buffer_allocate(mctx, 512); * name = NULL; * name = dns_message_gettempname(message, &name); * dns_name_init(name, NULL); * result = dns_name_fromtext(name, &source, dns_rootname, ISC_FALSE, * buffer); * dns_message_takebuffer(message, &buffer); * * * TODO: * * XXX Needed: ways to set and retrieve EDNS information, add rdata to a * section, move rdata from one section to another, remove rdata, etc. */#define DNS_MESSAGEFLAG_QR 0x8000U#define DNS_MESSAGEFLAG_AA 0x0400U#define DNS_MESSAGEFLAG_TC 0x0200U#define DNS_MESSAGEFLAG_RD 0x0100U#define DNS_MESSAGEFLAG_RA 0x0080U#define DNS_MESSAGEFLAG_AD 0x0020U#define DNS_MESSAGEFLAG_CD 0x0010U#define DNS_MESSAGEEXTFLAG_DO 0x8000U#define DNS_MESSAGE_REPLYPRESERVE (DNS_MESSAGEFLAG_RD|DNS_MESSAGEFLAG_CD)#define DNS_MESSAGEEXTFLAG_REPLYPRESERVE (DNS_MESSAGEEXTFLAG_DO)#define DNS_MESSAGE_HEADERLEN 12 /* 6 isc_uint16_t's */#define DNS_MESSAGE_MAGIC ISC_MAGIC('M','S','G','@')#define DNS_MESSAGE_VALID(msg) ISC_MAGIC_VALID(msg, DNS_MESSAGE_MAGIC)/* * Ordering here matters. DNS_SECTION_ANY must be the lowest and negative, * and DNS_SECTION_MAX must be one greater than the last used section. */typedef int dns_section_t;#define DNS_SECTION_ANY (-1)#define DNS_SECTION_QUESTION 0#define DNS_SECTION_ANSWER 1#define DNS_SECTION_AUTHORITY 2#define DNS_SECTION_ADDITIONAL 3#define DNS_SECTION_MAX 4typedef int dns_pseudosection_t;#define DNS_PSEUDOSECTION_ANY (-1)#define DNS_PSEUDOSECTION_OPT 0#define DNS_PSEUDOSECTION_TSIG 1#define DNS_PSEUDOSECTION_SIG0 2#define DNS_PSEUDOSECTION_MAX 3typedef int dns_messagetextflag_t;#define DNS_MESSAGETEXTFLAG_NOCOMMENTS 0x0001#define DNS_MESSAGETEXTFLAG_NOHEADERS 0x0002/* * Dynamic update names for these sections. */#define DNS_SECTION_ZONE DNS_SECTION_QUESTION#define DNS_SECTION_PREREQUISITE DNS_SECTION_ANSWER#define DNS_SECTION_UPDATE DNS_SECTION_AUTHORITY/* * These tell the message library how the created dns_message_t will be used. */#define DNS_MESSAGE_INTENTUNKNOWN 0 /* internal use only */#define DNS_MESSAGE_INTENTPARSE 1 /* parsing messages */#define DNS_MESSAGE_INTENTRENDER 2 /* rendering *//* * Control behavior of parsing */#define DNS_MESSAGEPARSE_PRESERVEORDER 0x0001 /* preserve rdata order */#define DNS_MESSAGEPARSE_BESTEFFORT 0x0002 /* return a message if a recoverable parse error occurs */#define DNS_MESSAGEPARSE_CLONEBUFFER 0x0004 /* save a copy of the source buffer */#define DNS_MESSAGEPARSE_IGNORETRUNCATION 0x0008 /* trucation errors are * not fatal. *//* * Control behavior of rendering */#define DNS_MESSAGERENDER_ORDERED 0x0001 /* don't change order */#define DNS_MESSAGERENDER_PARTIAL 0x0002 /* allow a partial rdataset */#define DNS_MESSAGERENDER_OMITDNSSEC 0x0004 /* omit DNSSEC records */#define DNS_MESSAGERENDER_PREFER_A 0x0008 /* prefer A records in * additional section. */#define DNS_MESSAGERENDER_PREFER_AAAA 0x0010 /* prefer AAAA records in * additional section. */typedef struct dns_msgblock dns_msgblock_t;struct dns_message { /* public from here down */ unsigned int magic; dns_messageid_t id; unsigned int flags; dns_rcode_t rcode; unsigned int opcode; dns_rdataclass_t rdclass; /* 4 real, 1 pseudo */ unsigned int counts[DNS_SECTION_MAX]; /* private from here down */ dns_namelist_t sections[DNS_SECTION_MAX]; dns_name_t *cursors[DNS_SECTION_MAX]; dns_rdataset_t *opt; dns_rdataset_t *sig0; dns_rdataset_t *tsig; int state; unsigned int from_to_wire : 2; unsigned int header_ok : 1; unsigned int question_ok : 1; unsigned int tcp_continuation : 1; unsigned int verified_sig : 1; unsigned int verify_attempted : 1; unsigned int free_query : 1; unsigned int free_saved : 1; unsigned int opt_reserved; unsigned int sig_reserved; unsigned int reserved; /* reserved space (render) */ isc_buffer_t *buffer; dns_compress_t *cctx; isc_mem_t *mctx; isc_mempool_t *namepool; isc_mempool_t *rdspool; isc_bufferlist_t scratchpad; isc_bufferlist_t cleanup; ISC_LIST(dns_msgblock_t) rdatas; ISC_LIST(dns_msgblock_t) rdatalists; ISC_LIST(dns_msgblock_t) offsets; ISC_LIST(dns_rdata_t) freerdata; ISC_LIST(dns_rdatalist_t) freerdatalist; dns_rcode_t tsigstatus; dns_rcode_t querytsigstatus; dns_name_t *tsigname; /* Owner name of TSIG, if any */ dns_rdataset_t *querytsig; dns_tsigkey_t *tsigkey; dst_context_t *tsigctx; int sigstart; int timeadjust; dns_name_t *sig0name; /* Owner name of SIG0, if any */ dst_key_t *sig0key; dns_rcode_t sig0status; isc_region_t query; isc_region_t saved; dns_rdatasetorderfunc_t order; void * order_arg;};/*** *** Functions ***/ISC_LANG_BEGINDECLSisc_result_tdns_message_create(isc_mem_t *mctx, unsigned int intent, dns_message_t **msgp);/* * Create msg structure. * * This function will allocate some internal blocks of memory that are * expected to be needed for parsing or rendering nearly any type of message. * * Requires: * 'mctx' be a valid memory context. * * 'msgp' be non-null and '*msg' be NULL. * * 'intent' must be one of DNS_MESSAGE_INTENTPARSE or * DNS_MESSAGE_INTENTRENDER. * * Ensures: * The data in "*msg" is set to indicate an unused and empty msg * structure. * * Returns: * ISC_R_NOMEMORY -- out of memory * ISC_R_SUCCESS -- success */voiddns_message_reset(dns_message_t *msg, unsigned int intent);/* * Reset a message structure to default state. All internal lists are freed * or reset to a default state as well. This is simply a more efficient * way to call dns_message_destroy() followed by dns_message_allocate(), * since it avoid many memory allocations. * * If any data loanouts (buffers, names, rdatas, etc) were requested, * the caller must no longer use them after this call. * * The intended next use of the message will be 'intent'. * * Requires: * * 'msg' be valid. * * 'intent' is DNS_MESSAGE_INTENTPARSE or DNS_MESSAGE_INTENTRENDER */voiddns_message_destroy(dns_message_t **msgp);/* * Destroy all state in the message. * * Requires: * * 'msgp' be valid. * * Ensures: * '*msgp' == NULL */isc_result_tdns_message_sectiontotext(dns_message_t *msg, dns_section_t section, const dns_master_style_t *style, dns_messagetextflag_t flags, isc_buffer_t *target);isc_result_tdns_message_pseudosectiontotext(dns_message_t *msg, dns_pseudosection_t section, const dns_master_style_t *style, dns_messagetextflag_t flags, isc_buffer_t *target);/* * Convert section 'section' or 'pseudosection' of message 'msg' to * a cleartext representation * * Notes: * See dns_message_totext for meanings of flags. * * Requires: * * 'msg' is a valid message. * * 'style' is a valid master dump style. * * 'target' is a valid buffer. * * 'section' is a valid section label. * * Ensures: * * If the result is success: * * The used space in 'target' is updated. * * Returns: * * ISC_R_SUCCESS * ISC_R_NOSPACE * ISC_R_NOMORE * * Note: On error return, *target may be partially filled with data.*/isc_result_tdns_message_totext(dns_message_t *msg, const dns_master_style_t *style, dns_messagetextflag_t flags, isc_buffer_t *target);/* * Convert all sections of message 'msg' to a cleartext representation * * Notes: * In flags, If DNS_MESSAGETEXTFLAG_OMITDOT is set, then the * final '.' in absolute names will not be emitted. If * DNS_MESSAGETEXTFLAG_NOCOMMENTS is cleared, lines beginning * with ";;" will be emitted indicating section name. If * DNS_MESSAGETEXTFLAG_NOHEADERS is cleared, header lines will * be emitted. * * Requires: * * 'msg' is a valid message. * * 'style' is a valid master dump style. * * 'target' is a valid buffer. * * Ensures: * * If the result is success: * * The used space in 'target' is updated. * * Returns: * * ISC_R_SUCCESS * ISC_R_NOSPACE * ISC_R_NOMORE * * Note: On error return, *target may be partially filled with data. */isc_result_tdns_message_parse(dns_message_t *msg, isc_buffer_t *source, unsigned int options);/* * Parse raw wire data in 'source' as a DNS message. * * OPT records are detected and stored in the pseudo-section "opt". * TSIGs are detected and stored in the pseudo-section "tsig". * * If DNS_MESSAGEPARSE_PRESERVEORDER is set, or if the opcode of the message * is UPDATE, a separate dns_name_t object will be created for each RR in the * message. Each such dns_name_t will have a single rdataset containing the * single RR, and the order of the RRs in the message is preserved. * Otherwise, only one dns_name_t object will be created for each unique * owner name in the section, and each such dns_name_t will have a list * of rdatasets. To access the names and their data, use * dns_message_firstname() and dns_message_nextname(). * * If DNS_MESSAGEPARSE_BESTEFFORT is set, errors in message content will * not be considered FORMERRs. If the entire message can be parsed, it * will be returned and DNS_R_RECOVERABLE will be returned. * * If DNS_MESSAGEPARSE_IGNORETRUNCATION is set then return as many complete * RR's as possible, DNS_R_RECOVERABLE will be returned. * * OPT and TSIG records are always handled specially, regardless of the * 'preserve_order' setting. * * Requires: * "msg" be valid. * * "buffer" be a wire format buffer. * * Ensures: * The buffer's data format is correct. * * The buffer's contents verify as correct regarding header bits, buffer * and rdata sizes, etc. * * Returns: * ISC_R_SUCCESS -- all is well * ISC_R_NOMEMORY -- no memory * DNS_R_RECOVERABLE -- the message parsed properly, but contained * errors. * Many other errors possible XXXMLG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -