2 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: rdata.h,v 1.77 2009-12-04 21:09:33 marka Exp $ */
29 * Provides facilities for manipulating DNS rdata, including conversions to
30 * and from wire format and text format.
32 * Given the large amount of rdata possible in a nameserver, it was important
33 * to come up with a very efficient way of storing rdata, but at the same
34 * time allow it to be manipulated.
36 * The decision was to store rdata in uncompressed wire format,
37 * and not to make it a fully abstracted object; i.e. certain parts of the
38 * server know rdata is stored that way. This saves a lot of memory, and
39 * makes adding rdata to messages easy. Having much of the server know
40 * the representation would be perilous, and we certainly don't want each
41 * user of rdata to be manipulating such a low-level structure. This is
42 * where the rdata module comes in. The module allows rdata handles to be
43 * created and attached to uncompressed wire format regions. All rdata
44 * operations and conversions are done through these handles.
46 * Implementation Notes:
48 *\li The routines in this module are expected to be synthesized by the
49 * build process from a set of source files, one per rdata type. For
50 * portability, it's probably best that the building be done by a C
51 * program. Adding a new rdata type will be a simple matter of adding
52 * a file to a directory and rebuilding the server. *All* knowledge of
53 * the format of a particular rdata type is in this file.
56 *\li Clients of this module must impose any required synchronization.
59 *\li This module deals with low-level byte streams. Errors in any of
60 * the functions are likely to crash the server or corrupt memory.
62 *\li Rdata is typed, and the caller must know what type of rdata it has.
63 * A caller that gets this wrong could crash the server.
65 *\li The fromstruct() and tostruct() routines use a void * pointer to
66 * represent the structure. The caller must ensure that it passes a
67 * pointer to the appropriate type, or the server could crash or memory
76 * dns_rdata_fromwire() deals with raw network data. An error in
77 * this routine could result in the failure or hijacking of the server.
83 *\li Draft Binary Labels (2)
84 *\li Draft Local Compression (1)
85 *\li Various RFCs for particular types; these will be documented in the
86 * sources files of the types.
96 #include <dns/types.h>
98 #include <dns/message.h>
108 ***** An 'rdata' is a handle to a binary region. The handle has an RR
109 ***** class and type, and the data in the binary region is in the format
110 ***** of the given class and type.
113 * Clients are strongly discouraged from using this type directly, with
114 * the exception of the 'link' field which may be used directly for whatever
115 * purpose the client desires.
118 unsigned char * data;
120 dns_rdataclass_t rdclass;
121 dns_rdatatype_t type;
123 ISC_LINK(dns_rdata_t) link;
126 #define DNS_RDATA_INIT { NULL, 0, 0, 0, 0, {(void*)(-1), (void *)(-1)}}
128 #define DNS_RDATA_CHECKINITIALIZED
129 #ifdef DNS_RDATA_CHECKINITIALIZED
130 #define DNS_RDATA_INITIALIZED(rdata) \
131 ((rdata)->data == NULL && (rdata)->length == 0 && \
132 (rdata)->rdclass == 0 && (rdata)->type == 0 && (rdata)->flags == 0 && \
133 !ISC_LINK_LINKED((rdata), link))
135 #ifdef ISC_LIST_CHECKINIT
136 #define DNS_RDATA_INITIALIZED(rdata) \
137 (!ISC_LINK_LINKED((rdata), link))
139 #define DNS_RDATA_INITIALIZED(rdata) ISC_TRUE
143 #define DNS_RDATA_UPDATE 0x0001 /*%< update pseudo record. */
144 #define DNS_RDATA_OFFLINE 0x0002 /*%< RRSIG has a offline key. */
146 #define DNS_RDATA_VALIDFLAGS(rdata) \
147 (((rdata)->flags & ~(DNS_RDATA_UPDATE|DNS_RDATA_OFFLINE)) == 0)
150 * The maximum length of a RDATA that can be sent on the wire.
151 * Max packet size (65535) less header (12), less name (1), type (2),
152 * class (2), ttl(4), length (2).
154 * None of the defined types that support name compression can exceed
155 * this and all new types are to be sent uncompressed.
158 #define DNS_RDATA_MAXLENGTH 65512U
161 * Flags affecting rdata formatting style. Flags 0xFFFF0000
162 * are used by masterfile-level formatting and defined elsewhere.
163 * See additional comments at dns_rdata_tofmttext().
166 /*% Split the rdata into multiple lines to try to keep it
167 within the "width". */
168 #define DNS_STYLEFLAG_MULTILINE 0x00000001U
170 /*% Output explanatory comments. */
171 #define DNS_STYLEFLAG_COMMENT 0x00000002U
173 #define DNS_RDATA_DOWNCASE DNS_NAME_DOWNCASE
174 #define DNS_RDATA_CHECKNAMES DNS_NAME_CHECKNAMES
175 #define DNS_RDATA_CHECKNAMESFAIL DNS_NAME_CHECKNAMESFAIL
176 #define DNS_RDATA_CHECKREVERSE DNS_NAME_CHECKREVERSE
177 #define DNS_RDATA_CHECKMX DNS_NAME_CHECKMX
178 #define DNS_RDATA_CHECKMXFAIL DNS_NAME_CHECKMXFAIL
185 dns_rdata_init(dns_rdata_t *rdata);
187 * Make 'rdata' empty.
190 * 'rdata' is a valid rdata (i.e. not NULL, points to a struct dns_rdata)
194 dns_rdata_reset(dns_rdata_t *rdata);
196 * Make 'rdata' empty.
199 *\li 'rdata' is a previously initialized rdata and is not linked.
203 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target);
205 * Clone 'target' from 'src'.
208 *\li 'src' to be initialized.
209 *\li 'target' to be initialized.
217 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2);
219 * Determine the relative ordering under the DNSSEC order relation of
220 * 'rdata1' and 'rdata2'.
224 *\li 'rdata1' is a valid, non-empty rdata
226 *\li 'rdata2' is a valid, non-empty rdata
229 *\li < 0 'rdata1' is less than 'rdata2'
230 *\li 0 'rdata1' is equal to 'rdata2'
231 *\li > 0 'rdata1' is greater than 'rdata2'
235 dns_rdata_casecompare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2);
237 * dns_rdata_casecompare() is similar to dns_rdata_compare() but also
238 * compares domain names case insensitively in known rdata types that
239 * are treated as opaque data by dns_rdata_compare().
243 *\li 'rdata1' is a valid, non-empty rdata
245 *\li 'rdata2' is a valid, non-empty rdata
248 *\li < 0 'rdata1' is less than 'rdata2'
249 *\li 0 'rdata1' is equal to 'rdata2'
250 *\li > 0 'rdata1' is greater than 'rdata2'
258 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
259 dns_rdatatype_t type, isc_region_t *r);
261 * Make 'rdata' refer to region 'r'.
265 *\li The data in 'r' is properly formatted for whatever type it is.
269 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r);
271 * Make 'r' refer to 'rdata'.
275 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
276 dns_rdatatype_t type, isc_buffer_t *source,
277 dns_decompress_t *dctx, unsigned int options,
278 isc_buffer_t *target);
280 * Copy the possibly-compressed rdata at source into the target region.
283 *\li Name decompression policy is controlled by 'dctx'.
286 *\li DNS_RDATA_DOWNCASE downcase domain names when they are copied
291 *\li 'rdclass' and 'type' are valid.
293 *\li 'source' is a valid buffer, and the active region of 'source'
294 * references the rdata to be processed.
296 *\li 'target' is a valid buffer.
298 *\li 'dctx' is a valid decompression context.
301 * if result is success:
302 * \li If 'rdata' is not NULL, it is attached to the target.
303 * \li The conditions dns_name_fromwire() ensures for names hold
304 * for all names in the rdata.
305 * \li The current location in source is advanced, and the used space
306 * in target is updated.
310 *\li Any non-success status from dns_name_fromwire()
311 *\li Various 'Bad Form' class failures depending on class and type
312 *\li Bad Form: Input too short
313 *\li Resource Limit: Not enough space
317 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
318 isc_buffer_t *target);
320 * Convert 'rdata' into wire format, compressing it as specified by the
321 * compression context 'cctx', and storing the result in 'target'.
324 *\li If the compression context allows global compression, then the
325 * global compression table may be updated.
328 *\li 'rdata' is a valid, non-empty rdata
330 *\li target is a valid buffer
332 *\li Any offsets specified in a global compression table are valid
336 * if the result is success:
337 * \li The used space in target is updated.
341 *\li Any non-success status from dns_name_towire()
342 *\li Resource Limit: Not enough space
346 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
347 dns_rdatatype_t type, isc_lex_t *lexer, dns_name_t *origin,
348 unsigned int options, isc_mem_t *mctx,
349 isc_buffer_t *target, dns_rdatacallbacks_t *callbacks);
351 * Convert the textual representation of a DNS rdata into uncompressed wire
352 * form stored in the target region. Tokens constituting the text of the rdata
353 * are taken from 'lexer'.
356 *\li Relative domain names in the rdata will have 'origin' appended to them.
357 * A NULL origin implies "origin == dns_rootname".
361 *\li DNS_RDATA_DOWNCASE downcase domain names when they are copied
363 *\li DNS_RDATA_CHECKNAMES perform checknames checks.
364 *\li DNS_RDATA_CHECKNAMESFAIL fail if the checknames check fail. If
365 * not set a warning will be issued.
366 *\li DNS_RDATA_CHECKREVERSE this should set if the owner name ends
367 * in IP6.ARPA, IP6.INT or IN-ADDR.ARPA.
371 *\li 'rdclass' and 'type' are valid.
373 *\li 'lexer' is a valid isc_lex_t.
375 *\li 'mctx' is a valid isc_mem_t.
377 *\li 'target' is a valid region.
379 *\li 'origin' if non NULL it must be absolute.
381 *\li 'callbacks' to be NULL or callbacks->warn and callbacks->error be
385 * if result is success:
386 *\li If 'rdata' is not NULL, it is attached to the target.
388 *\li The conditions dns_name_fromtext() ensures for names hold
389 * for all names in the rdata.
391 *\li The used space in target is updated.
395 *\li Translated result codes from isc_lex_gettoken
396 *\li Various 'Bad Form' class failures depending on class and type
397 *\li Bad Form: Input too short
398 *\li Resource Limit: Not enough space
399 *\li Resource Limit: Not enough memory
403 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target);
405 * Convert 'rdata' into text format, storing the result in 'target'.
406 * The text will consist of a single line, with fields separated by
410 *\li If 'origin' is not NULL, then any names in the rdata that are
411 * subdomains of 'origin' will be made relative it.
413 *\li XXX Do we *really* want to support 'origin'? I'm inclined towards "no"
418 *\li 'rdata' is a valid, non-empty rdata
420 *\li 'origin' is NULL, or is a valid name
422 *\li 'target' is a valid text buffer
425 * if the result is success:
427 * \li The used space in target is updated.
431 *\li Any non-success status from dns_name_totext()
432 *\li Resource Limit: Not enough space
436 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin, unsigned int flags,
437 unsigned int width, const char *linebreak,
438 isc_buffer_t *target);
440 * Like dns_rdata_totext, but do formatted output suitable for
441 * database dumps. This is intended for use by dns_db_dump();
442 * library users are discouraged from calling it directly.
444 * If (flags & #DNS_STYLEFLAG_MULTILINE) != 0, attempt to stay
445 * within 'width' by breaking the text into multiple lines.
446 * The string 'linebreak' is inserted between lines, and parentheses
447 * are added when necessary. Because RRs contain unbreakable elements
448 * such as domain names whose length is variable, unpredictable, and
449 * potentially large, there is no guarantee that the lines will
450 * not exceed 'width' anyway.
452 * If (flags & #DNS_STYLEFLAG_MULTILINE) == 0, the rdata is always
453 * printed as a single line, and no parentheses are used.
454 * The 'width' and 'linebreak' arguments are ignored.
456 * If (flags & #DNS_STYLEFLAG_COMMENT) != 0, output explanatory
457 * comments next to things like the SOA timer fields. Some
458 * comments (e.g., the SOA ones) are only printed when multiline
459 * output is selected.
463 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
464 dns_rdatatype_t type, void *source, isc_buffer_t *target);
466 * Convert the C structure representation of an rdata into uncompressed wire
467 * format in 'target'.
469 * XXX Should we have a 'size' parameter as a sanity check on target?
473 *\li 'rdclass' and 'type' are valid.
475 *\li 'source' points to a valid C struct for the class and type.
477 *\li 'target' is a valid buffer.
479 *\li All structure pointers to memory blocks should be NULL if their
480 * corresponding length values are zero.
483 * if result is success:
484 * \li If 'rdata' is not NULL, it is attached to the target.
486 * \li The used space in 'target' is updated.
490 *\li Various 'Bad Form' class failures depending on class and type
491 *\li Resource Limit: Not enough space
495 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx);
497 * Convert an rdata into its C structure representation.
499 * If 'mctx' is NULL then 'rdata' must persist while 'target' is being used.
501 * If 'mctx' is non NULL then memory will be allocated if required.
505 *\li 'rdata' is a valid, non-empty rdata.
507 *\li 'target' to point to a valid pointer for the type and class.
511 *\li Resource Limit: Not enough memory
515 dns_rdata_freestruct(void *source);
517 * Free dynamic memory attached to 'source' (if any).
521 *\li 'source' to point to the structure previously filled in by
522 * dns_rdata_tostruct().
526 dns_rdatatype_ismeta(dns_rdatatype_t type);
528 * Return true iff the rdata type 'type' is a meta-type
533 dns_rdatatype_issingleton(dns_rdatatype_t type);
535 * Return true iff the rdata type 'type' is a singleton type,
539 * \li 'type' is a valid rdata type.
544 dns_rdataclass_ismeta(dns_rdataclass_t rdclass);
546 * Return true iff the rdata class 'rdclass' is a meta-class
551 dns_rdatatype_isdnssec(dns_rdatatype_t type);
553 * Return true iff 'type' is one of the DNSSEC
554 * rdata types that may exist alongside a CNAME record.
557 * \li 'type' is a valid rdata type.
561 dns_rdatatype_iszonecutauth(dns_rdatatype_t type);
563 * Return true iff rdata of type 'type' is considered authoritative
564 * data (not glue) in the NSEC chain when it occurs in the parent zone
568 * \li 'type' is a valid rdata type.
573 dns_rdatatype_isknown(dns_rdatatype_t type);
575 * Return true iff the rdata type 'type' is known.
578 * \li 'type' is a valid rdata type.
584 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
587 * Call 'add' for each name and type from 'rdata' which is subject to
588 * additional section processing.
592 *\li 'rdata' is a valid, non-empty rdata.
594 *\li 'add' is a valid dns_additionalfunc_t.
598 *\li If successful, then add() will have been called for each name
599 * and type subject to additional section processing.
601 *\li If add() returns something other than #ISC_R_SUCCESS, that result
602 * will be returned as the result of dns_rdata_additionaldata().
608 *\li Many other results are possible if not successful.
612 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg);
614 * Send 'rdata' in DNSSEC canonical form to 'digest'.
617 *\li 'digest' may be called more than once by dns_rdata_digest(). The
618 * concatenation of all the regions, in the order they were given
619 * to 'digest', will be the DNSSEC canonical form of 'rdata'.
623 *\li 'rdata' is a valid, non-empty rdata.
625 *\li 'digest' is a valid dns_digestfunc_t.
629 *\li If successful, then all of the rdata's data has been sent, in
630 * DNSSEC canonical form, to 'digest'.
632 *\li If digest() returns something other than ISC_R_SUCCESS, that result
633 * will be returned as the result of dns_rdata_digest().
639 *\li Many other results are possible if not successful.
643 dns_rdatatype_questiononly(dns_rdatatype_t type);
645 * Return true iff rdata of type 'type' can only appear in the question
646 * section of a properly formatted message.
649 * \li 'type' is a valid rdata type.
654 dns_rdatatype_notquestion(dns_rdatatype_t type);
656 * Return true iff rdata of type 'type' can not appear in the question
657 * section of a properly formatted message.
660 * \li 'type' is a valid rdata type.
665 dns_rdatatype_atparent(dns_rdatatype_t type);
667 * Return true iff rdata of type 'type' should appear at the parent of
671 * \li 'type' is a valid rdata type.
676 dns_rdatatype_attributes(dns_rdatatype_t rdtype);
678 * Return attributes for the given type.
681 *\li 'rdtype' are known.
684 *\li a bitmask consisting of the following flags.
687 /*% only one may exist for a name */
688 #define DNS_RDATATYPEATTR_SINGLETON 0x00000001U
689 /*% requires no other data be present */
690 #define DNS_RDATATYPEATTR_EXCLUSIVE 0x00000002U
691 /*% Is a meta type */
692 #define DNS_RDATATYPEATTR_META 0x00000004U
693 /*% Is a DNSSEC type, like RRSIG or NSEC */
694 #define DNS_RDATATYPEATTR_DNSSEC 0x00000008U
695 /*% Is a zone cut authority type */
696 #define DNS_RDATATYPEATTR_ZONECUTAUTH 0x00000010U
697 /*% Is reserved (unusable) */
698 #define DNS_RDATATYPEATTR_RESERVED 0x00000020U
699 /*% Is an unknown type */
700 #define DNS_RDATATYPEATTR_UNKNOWN 0x00000040U
701 /*% Is META, and can only be in a question section */
702 #define DNS_RDATATYPEATTR_QUESTIONONLY 0x00000080U
703 /*% is META, and can NOT be in a question section */
704 #define DNS_RDATATYPEATTR_NOTQUESTION 0x00000100U
705 /*% Is present at zone cuts in the parent, not the child */
706 #define DNS_RDATATYPEATTR_ATPARENT 0x00000200U
709 dns_rdata_covers(dns_rdata_t *rdata);
711 * Return the rdatatype that this type covers.
714 *\li 'rdata' is a valid, non-empty rdata.
716 *\li 'rdata' is a type that covers other rdata types.
719 *\li The type covered.
723 dns_rdata_checkowner(dns_name_t* name, dns_rdataclass_t rdclass,
724 dns_rdatatype_t type, isc_boolean_t wildcard);
726 * Returns whether this is a valid ownername for this <type,class>.
727 * If wildcard is true allow the first label to be a wildcard if
731 * 'name' is a valid name.
735 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad);
737 * Returns whether 'rdata' contains valid domain names. The checks are
738 * sensitive to the owner name.
740 * If 'bad' is non-NULL and a domain name fails the check the
741 * the offending name will be return in 'bad' by cloning from
742 * the 'rdata' contents.
745 * 'rdata' to be valid.
746 * 'owner' to be valid.
747 * 'bad' to be NULL or valid.
751 dns_rdata_exists(dns_rdata_t *rdata, dns_rdatatype_t type);
754 dns_rdata_notexist(dns_rdata_t *rdata, dns_rdatatype_t type);
757 dns_rdata_deleterrset(dns_rdata_t *rdata, dns_rdatatype_t type);
760 dns_rdata_makedelete(dns_rdata_t *rdata);
763 dns_rdata_updateop(dns_rdata_t *rdata, dns_section_t section);
767 #endif /* DNS_RDATA_H */