00001 /* 00002 * Copyright (C) 2004-2007, 2009-2013, 2015 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 1998-2003 Internet Software Consortium. 00004 * 00005 * Permission to use, copy, modify, and/or distribute this software for any 00006 * purpose with or without fee is hereby granted, provided that the above 00007 * copyright notice and this permission notice appear in all copies. 00008 * 00009 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00010 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00011 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00012 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00013 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00014 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00015 * PERFORMANCE OF THIS SOFTWARE. 00016 */ 00017 00018 /* $Id: name.h,v 1.137 2011/01/13 04:59:26 tbox Exp $ */ 00019 00020 #ifndef DNS_NAME_H 00021 #define DNS_NAME_H 1 00022 00023 /***** 00024 ***** Module Info 00025 *****/ 00026 00027 /*! \file dns/name.h 00028 * \brief 00029 * Provides facilities for manipulating DNS names and labels, including 00030 * conversions to and from wire format and text format. 00031 * 00032 * Given the large number of names possible in a nameserver, and because 00033 * names occur in rdata, it was important to come up with a very efficient 00034 * way of storing name data, but at the same time allow names to be 00035 * manipulated. The decision was to store names in uncompressed wire format, 00036 * and not to make them fully abstracted objects; i.e. certain parts of the 00037 * server know names are stored that way. This saves a lot of memory, and 00038 * makes adding names to messages easy. Having much of the server know 00039 * the representation would be perilous, and we certainly don't want each 00040 * user of names to be manipulating such a low-level structure. This is 00041 * where the Names and Labels module comes in. The module allows name or 00042 * label handles to be created and attached to uncompressed wire format 00043 * regions. All name operations and conversions are done through these 00044 * handles. 00045 * 00046 * MP: 00047 *\li Clients of this module must impose any required synchronization. 00048 * 00049 * Reliability: 00050 *\li This module deals with low-level byte streams. Errors in any of 00051 * the functions are likely to crash the server or corrupt memory. 00052 * 00053 * Resources: 00054 *\li None. 00055 * 00056 * Security: 00057 * 00058 *\li *** WARNING *** 00059 * 00060 *\li dns_name_fromwire() deals with raw network data. An error in 00061 * this routine could result in the failure or hijacking of the server. 00062 * 00063 * Standards: 00064 *\li RFC1035 00065 *\li Draft EDNS0 (0) 00066 *\li Draft Binary Labels (2) 00067 * 00068 */ 00069 00070 /*** 00071 *** Imports 00072 ***/ 00073 00074 #include <stdio.h> 00075 00076 #include <isc/boolean.h> 00077 #include <isc/lang.h> 00078 #include <isc/magic.h> 00079 #include <isc/region.h> /* Required for storage size of dns_label_t. */ 00080 00081 #include <dns/types.h> 00082 00083 ISC_LANG_BEGINDECLS 00084 00085 /***** 00086 ***** Labels 00087 ***** 00088 ***** A 'label' is basically a region. It contains one DNS wire format 00089 ***** label of type 00 (ordinary). 00090 *****/ 00091 00092 /***** 00093 ***** Names 00094 ***** 00095 ***** A 'name' is a handle to a binary region. It contains a sequence of one 00096 ***** or more DNS wire format labels of type 00 (ordinary). 00097 ***** Note that all names are not required to end with the root label, 00098 ***** as they are in the actual DNS wire protocol. 00099 *****/ 00100 00101 /*** 00102 *** Types 00103 ***/ 00104 00105 /*% 00106 * Clients are strongly discouraged from using this type directly, with 00107 * the exception of the 'link' and 'list' fields which may be used directly 00108 * for whatever purpose the client desires. 00109 */ 00110 struct dns_name { 00111 unsigned int magic; 00112 unsigned char * ndata; 00113 unsigned int length; 00114 unsigned int labels; 00115 unsigned int attributes; 00116 unsigned char * offsets; 00117 isc_buffer_t * buffer; 00118 ISC_LINK(dns_name_t) link; 00119 ISC_LIST(dns_rdataset_t) list; 00120 }; 00121 00122 #define DNS_NAME_MAGIC ISC_MAGIC('D','N','S','n') 00123 00124 #define DNS_NAMEATTR_ABSOLUTE 0x00000001 00125 #define DNS_NAMEATTR_READONLY 0x00000002 00126 #define DNS_NAMEATTR_DYNAMIC 0x00000004 00127 #define DNS_NAMEATTR_DYNOFFSETS 0x00000008 00128 #define DNS_NAMEATTR_NOCOMPRESS 0x00000010 00129 /* 00130 * Attributes below 0x0100 reserved for name.c usage. 00131 */ 00132 #define DNS_NAMEATTR_CACHE 0x00000100 /*%< Used by resolver. */ 00133 #define DNS_NAMEATTR_ANSWER 0x00000200 /*%< Used by resolver. */ 00134 #define DNS_NAMEATTR_NCACHE 0x00000400 /*%< Used by resolver. */ 00135 #define DNS_NAMEATTR_CHAINING 0x00000800 /*%< Used by resolver. */ 00136 #define DNS_NAMEATTR_CHASE 0x00001000 /*%< Used by resolver. */ 00137 #define DNS_NAMEATTR_WILDCARD 0x00002000 /*%< Used by server. */ 00138 #define DNS_NAMEATTR_PREREQUISITE 0x00004000 /*%< Used by client. */ 00139 #define DNS_NAMEATTR_UPDATE 0x00008000 /*%< Used by client. */ 00140 #define DNS_NAMEATTR_HASUPDATEREC 0x00010000 /*%< Used by client. */ 00141 00142 /* 00143 * Various flags. 00144 */ 00145 #define DNS_NAME_DOWNCASE 0x0001 00146 #define DNS_NAME_CHECKNAMES 0x0002 /*%< Used by rdata. */ 00147 #define DNS_NAME_CHECKNAMESFAIL 0x0004 /*%< Used by rdata. */ 00148 #define DNS_NAME_CHECKREVERSE 0x0008 /*%< Used by rdata. */ 00149 #define DNS_NAME_CHECKMX 0x0010 /*%< Used by rdata. */ 00150 #define DNS_NAME_CHECKMXFAIL 0x0020 /*%< Used by rdata. */ 00151 00152 LIBDNS_EXTERNAL_DATA extern dns_name_t *dns_rootname; 00153 LIBDNS_EXTERNAL_DATA extern dns_name_t *dns_wildcardname; 00154 00155 /*% 00156 * Standard size of a wire format name 00157 */ 00158 #define DNS_NAME_MAXWIRE 255 00159 00160 /* 00161 * Text output filter procedure. 00162 * 'target' is the buffer to be converted. The region to be converted 00163 * is from 'buffer'->base + 'used_org' to the end of the used region. 00164 */ 00165 typedef isc_result_t (*dns_name_totextfilter_t)(isc_buffer_t *target, 00166 unsigned int used_org, 00167 isc_boolean_t absolute); 00168 00169 /*** 00170 *** Initialization 00171 ***/ 00172 00173 void 00174 dns_name_init(dns_name_t *name, unsigned char *offsets); 00175 /*%< 00176 * Initialize 'name'. 00177 * 00178 * Notes: 00179 * \li 'offsets' is never required to be non-NULL, but specifying a 00180 * dns_offsets_t for 'offsets' will improve the performance of most 00181 * name operations if the name is used more than once. 00182 * 00183 * Requires: 00184 * \li 'name' is not NULL and points to a struct dns_name. 00185 * 00186 * \li offsets == NULL or offsets is a dns_offsets_t. 00187 * 00188 * Ensures: 00189 * \li 'name' is a valid name. 00190 * \li dns_name_countlabels(name) == 0 00191 * \li dns_name_isabsolute(name) == ISC_FALSE 00192 */ 00193 00194 void 00195 dns_name_reset(dns_name_t *name); 00196 /*%< 00197 * Reinitialize 'name'. 00198 * 00199 * Notes: 00200 * \li This function distinguishes itself from dns_name_init() in two 00201 * key ways: 00202 * 00203 * \li + If any buffer is associated with 'name' (via dns_name_setbuffer() 00204 * or by being part of a dns_fixedname_t) the link to the buffer 00205 * is retained but the buffer itself is cleared. 00206 * 00207 * \li + Of the attributes associated with 'name', all are retained except 00208 * DNS_NAMEATTR_ABSOLUTE. 00209 * 00210 * Requires: 00211 * \li 'name' is a valid name. 00212 * 00213 * Ensures: 00214 * \li 'name' is a valid name. 00215 * \li dns_name_countlabels(name) == 0 00216 * \li dns_name_isabsolute(name) == ISC_FALSE 00217 */ 00218 00219 void 00220 dns_name_invalidate(dns_name_t *name); 00221 /*%< 00222 * Make 'name' invalid. 00223 * 00224 * Requires: 00225 * \li 'name' is a valid name. 00226 * 00227 * Ensures: 00228 * \li If assertion checking is enabled, future attempts to use 'name' 00229 * without initializing it will cause an assertion failure. 00230 * 00231 * \li If the name had a dedicated buffer, that association is ended. 00232 */ 00233 00234 isc_boolean_t 00235 dns_name_isvalid(const dns_name_t *name); 00236 /*%< 00237 * Check whether 'name' points to a valid dns_name 00238 */ 00239 00240 /*** 00241 *** Dedicated Buffers 00242 ***/ 00243 00244 void 00245 dns_name_setbuffer(dns_name_t *name, isc_buffer_t *buffer); 00246 /*%< 00247 * Dedicate a buffer for use with 'name'. 00248 * 00249 * Notes: 00250 * \li Specification of a target buffer in dns_name_fromwire(), 00251 * dns_name_fromtext(), and dns_name_concatenate() is optional if 00252 * 'name' has a dedicated buffer. 00253 * 00254 * \li The caller must not write to buffer until the name has been 00255 * invalidated or is otherwise known not to be in use. 00256 * 00257 * \li If buffer is NULL and the name previously had a dedicated buffer, 00258 * than that buffer is no longer dedicated to use with this name. 00259 * The caller is responsible for ensuring that the storage used by 00260 * the name remains valid. 00261 * 00262 * Requires: 00263 * \li 'name' is a valid name. 00264 * 00265 * \li 'buffer' is a valid binary buffer and 'name' doesn't have a 00266 * dedicated buffer already, or 'buffer' is NULL. 00267 */ 00268 00269 isc_boolean_t 00270 dns_name_hasbuffer(const dns_name_t *name); 00271 /*%< 00272 * Does 'name' have a dedicated buffer? 00273 * 00274 * Requires: 00275 * \li 'name' is a valid name. 00276 * 00277 * Returns: 00278 * \li ISC_TRUE 'name' has a dedicated buffer. 00279 * \li ISC_FALSE 'name' does not have a dedicated buffer. 00280 */ 00281 00282 /*** 00283 *** Properties 00284 ***/ 00285 00286 isc_boolean_t 00287 dns_name_isabsolute(const dns_name_t *name); 00288 /*%< 00289 * Does 'name' end in the root label? 00290 * 00291 * Requires: 00292 * \li 'name' is a valid name 00293 * 00294 * Returns: 00295 * \li TRUE The last label in 'name' is the root label. 00296 * \li FALSE The last label in 'name' is not the root label. 00297 */ 00298 00299 isc_boolean_t 00300 dns_name_iswildcard(const dns_name_t *name); 00301 /*%< 00302 * Is 'name' a wildcard name? 00303 * 00304 * Requires: 00305 * \li 'name' is a valid name 00306 * 00307 * \li dns_name_countlabels(name) > 0 00308 * 00309 * Returns: 00310 * \li TRUE The least significant label of 'name' is '*'. 00311 * \li FALSE The least significant label of 'name' is not '*'. 00312 */ 00313 00314 unsigned int 00315 dns_name_hash(dns_name_t *name, isc_boolean_t case_sensitive); 00316 /*%< 00317 * Provide a hash value for 'name'. 00318 * 00319 * Note: if 'case_sensitive' is ISC_FALSE, then names which differ only in 00320 * case will have the same hash value. 00321 * 00322 * Requires: 00323 * \li 'name' is a valid name 00324 * 00325 * Returns: 00326 * \li A hash value 00327 */ 00328 00329 unsigned int 00330 dns_name_fullhash(dns_name_t *name, isc_boolean_t case_sensitive); 00331 /*%< 00332 * Provide a hash value for 'name'. Unlike dns_name_hash(), this function 00333 * always takes into account of the entire name to calculate the hash value. 00334 * 00335 * Note: if 'case_sensitive' is ISC_FALSE, then names which differ only in 00336 * case will have the same hash value. 00337 * 00338 * Requires: 00339 *\li 'name' is a valid name 00340 * 00341 * Returns: 00342 *\li A hash value 00343 */ 00344 00345 unsigned int 00346 dns_name_hashbylabel(dns_name_t *name, isc_boolean_t case_sensitive); 00347 /*%< 00348 * Provide a hash value for 'name', where the hash value is the sum 00349 * of the hash values of each label. 00350 * 00351 * Note: if 'case_sensitive' is ISC_FALSE, then names which differ only in 00352 * case will have the same hash value. 00353 * 00354 * Requires: 00355 *\li 'name' is a valid name 00356 * 00357 * Returns: 00358 *\li A hash value 00359 */ 00360 00361 /* 00362 *** Comparisons 00363 ***/ 00364 00365 dns_namereln_t 00366 dns_name_fullcompare(const dns_name_t *name1, const dns_name_t *name2, 00367 int *orderp, unsigned int *nlabelsp); 00368 /*%< 00369 * Determine the relative ordering under the DNSSEC order relation of 00370 * 'name1' and 'name2', and also determine the hierarchical 00371 * relationship of the names. 00372 * 00373 * Note: It makes no sense for one of the names to be relative and the 00374 * other absolute. If both names are relative, then to be meaningfully 00375 * compared the caller must ensure that they are both relative to the 00376 * same domain. 00377 * 00378 * Requires: 00379 *\li 'name1' is a valid name 00380 * 00381 *\li dns_name_countlabels(name1) > 0 00382 * 00383 *\li 'name2' is a valid name 00384 * 00385 *\li dns_name_countlabels(name2) > 0 00386 * 00387 *\li orderp and nlabelsp are valid pointers. 00388 * 00389 *\li Either name1 is absolute and name2 is absolute, or neither is. 00390 * 00391 * Ensures: 00392 * 00393 *\li *orderp is < 0 if name1 < name2, 0 if name1 = name2, > 0 if 00394 * name1 > name2. 00395 * 00396 *\li *nlabelsp is the number of common significant labels. 00397 * 00398 * Returns: 00399 *\li dns_namereln_none There's no hierarchical relationship 00400 * between name1 and name2. 00401 *\li dns_namereln_contains name1 properly contains name2; i.e. 00402 * name2 is a proper subdomain of name1. 00403 *\li dns_namereln_subdomain name1 is a proper subdomain of name2. 00404 *\li dns_namereln_equal name1 and name2 are equal. 00405 *\li dns_namereln_commonancestor name1 and name2 share a common 00406 * ancestor. 00407 */ 00408 00409 int 00410 dns_name_compare(const dns_name_t *name1, const dns_name_t *name2); 00411 /*%< 00412 * Determine the relative ordering under the DNSSEC order relation of 00413 * 'name1' and 'name2'. 00414 * 00415 * Note: It makes no sense for one of the names to be relative and the 00416 * other absolute. If both names are relative, then to be meaningfully 00417 * compared the caller must ensure that they are both relative to the 00418 * same domain. 00419 * 00420 * Requires: 00421 * \li 'name1' is a valid name 00422 * 00423 * \li 'name2' is a valid name 00424 * 00425 * \li Either name1 is absolute and name2 is absolute, or neither is. 00426 * 00427 * Returns: 00428 * \li < 0 'name1' is less than 'name2' 00429 * \li 0 'name1' is equal to 'name2' 00430 * \li > 0 'name1' is greater than 'name2' 00431 */ 00432 00433 isc_boolean_t 00434 dns_name_equal(const dns_name_t *name1, const dns_name_t *name2); 00435 /*%< 00436 * Are 'name1' and 'name2' equal? 00437 * 00438 * Notes: 00439 * \li Because it only needs to test for equality, dns_name_equal() can be 00440 * significantly faster than dns_name_fullcompare() or dns_name_compare(). 00441 * 00442 * \li Offsets tables are not used in the comparision. 00443 * 00444 * \li It makes no sense for one of the names to be relative and the 00445 * other absolute. If both names are relative, then to be meaningfully 00446 * compared the caller must ensure that they are both relative to the 00447 * same domain. 00448 * 00449 * Requires: 00450 * \li 'name1' is a valid name 00451 * 00452 * \li 'name2' is a valid name 00453 * 00454 * \li Either name1 is absolute and name2 is absolute, or neither is. 00455 * 00456 * Returns: 00457 * \li ISC_TRUE 'name1' and 'name2' are equal 00458 * \li ISC_FALSE 'name1' and 'name2' are not equal 00459 */ 00460 00461 isc_boolean_t 00462 dns_name_caseequal(const dns_name_t *name1, const dns_name_t *name2); 00463 /*%< 00464 * Case sensitive version of dns_name_equal(). 00465 */ 00466 00467 int 00468 dns_name_rdatacompare(const dns_name_t *name1, const dns_name_t *name2); 00469 /*%< 00470 * Compare two names as if they are part of rdata in DNSSEC canonical 00471 * form. 00472 * 00473 * Requires: 00474 * \li 'name1' is a valid absolute name 00475 * 00476 * \li dns_name_countlabels(name1) > 0 00477 * 00478 * \li 'name2' is a valid absolute name 00479 * 00480 * \li dns_name_countlabels(name2) > 0 00481 * 00482 * Returns: 00483 * \li < 0 'name1' is less than 'name2' 00484 * \li 0 'name1' is equal to 'name2' 00485 * \li > 0 'name1' is greater than 'name2' 00486 */ 00487 00488 isc_boolean_t 00489 dns_name_issubdomain(const dns_name_t *name1, const dns_name_t *name2); 00490 /*%< 00491 * Is 'name1' a subdomain of 'name2'? 00492 * 00493 * Notes: 00494 * \li name1 is a subdomain of name2 if name1 is contained in name2, or 00495 * name1 equals name2. 00496 * 00497 * \li It makes no sense for one of the names to be relative and the 00498 * other absolute. If both names are relative, then to be meaningfully 00499 * compared the caller must ensure that they are both relative to the 00500 * same domain. 00501 * 00502 * Requires: 00503 * \li 'name1' is a valid name 00504 * 00505 * \li 'name2' is a valid name 00506 * 00507 * \li Either name1 is absolute and name2 is absolute, or neither is. 00508 * 00509 * Returns: 00510 * \li TRUE 'name1' is a subdomain of 'name2' 00511 * \li FALSE 'name1' is not a subdomain of 'name2' 00512 */ 00513 00514 isc_boolean_t 00515 dns_name_matcheswildcard(const dns_name_t *name, const dns_name_t *wname); 00516 /*%< 00517 * Does 'name' match the wildcard specified in 'wname'? 00518 * 00519 * Notes: 00520 * \li name matches the wildcard specified in wname if all labels 00521 * following the wildcard in wname are identical to the same number 00522 * of labels at the end of name. 00523 * 00524 * \li It makes no sense for one of the names to be relative and the 00525 * other absolute. If both names are relative, then to be meaningfully 00526 * compared the caller must ensure that they are both relative to the 00527 * same domain. 00528 * 00529 * Requires: 00530 * \li 'name' is a valid name 00531 * 00532 * \li dns_name_countlabels(name) > 0 00533 * 00534 * \li 'wname' is a valid name 00535 * 00536 * \li dns_name_countlabels(wname) > 0 00537 * 00538 * \li dns_name_iswildcard(wname) is true 00539 * 00540 * \li Either name is absolute and wname is absolute, or neither is. 00541 * 00542 * Returns: 00543 * \li TRUE 'name' matches the wildcard specified in 'wname' 00544 * \li FALSE 'name' does not match the wildcard specified in 'wname' 00545 */ 00546 00547 /*** 00548 *** Labels 00549 ***/ 00550 00551 unsigned int 00552 dns_name_countlabels(const dns_name_t *name); 00553 /*%< 00554 * How many labels does 'name' have? 00555 * 00556 * Notes: 00557 * \li In this case, as in other places, a 'label' is an ordinary label. 00558 * 00559 * Requires: 00560 * \li 'name' is a valid name 00561 * 00562 * Ensures: 00563 * \li The result is <= 128. 00564 * 00565 * Returns: 00566 * \li The number of labels in 'name'. 00567 */ 00568 00569 void 00570 dns_name_getlabel(const dns_name_t *name, unsigned int n, dns_label_t *label); 00571 /*%< 00572 * Make 'label' refer to the 'n'th least significant label of 'name'. 00573 * 00574 * Notes: 00575 * \li Numbering starts at 0. 00576 * 00577 * \li Given "rc.vix.com.", the label 0 is "rc", and label 3 is the 00578 * root label. 00579 * 00580 * \li 'label' refers to the same memory as 'name', so 'name' must not 00581 * be changed while 'label' is still in use. 00582 * 00583 * Requires: 00584 * \li n < dns_name_countlabels(name) 00585 */ 00586 00587 void 00588 dns_name_getlabelsequence(const dns_name_t *source, unsigned int first, 00589 unsigned int n, dns_name_t *target); 00590 /*%< 00591 * Make 'target' refer to the 'n' labels including and following 'first' 00592 * in 'source'. 00593 * 00594 * Notes: 00595 * \li Numbering starts at 0. 00596 * 00597 * \li Given "rc.vix.com.", the label 0 is "rc", and label 3 is the 00598 * root label. 00599 * 00600 * \li 'target' refers to the same memory as 'source', so 'source' 00601 * must not be changed while 'target' is still in use. 00602 * 00603 * Requires: 00604 * \li 'source' and 'target' are valid names. 00605 * 00606 * \li first < dns_name_countlabels(name) 00607 * 00608 * \li first + n <= dns_name_countlabels(name) 00609 */ 00610 00611 00612 void 00613 dns_name_clone(const dns_name_t *source, dns_name_t *target); 00614 /*%< 00615 * Make 'target' refer to the same name as 'source'. 00616 * 00617 * Notes: 00618 * 00619 * \li 'target' refers to the same memory as 'source', so 'source' 00620 * must not be changed while 'target' is still in use. 00621 * 00622 * \li This call is functionally equivalent to: 00623 * 00624 * \code 00625 * dns_name_getlabelsequence(source, 0, 00626 * dns_name_countlabels(source), 00627 * target); 00628 * \endcode 00629 * 00630 * but is more efficient. Also, dns_name_clone() works even if 'source' 00631 * is empty. 00632 * 00633 * Requires: 00634 * 00635 * \li 'source' is a valid name. 00636 * 00637 * \li 'target' is a valid name that is not read-only. 00638 */ 00639 00640 /*** 00641 *** Conversions 00642 ***/ 00643 00644 void 00645 dns_name_fromregion(dns_name_t *name, const isc_region_t *r); 00646 /*%< 00647 * Make 'name' refer to region 'r'. 00648 * 00649 * Note: 00650 * \li If the conversion encounters a root label before the end of the 00651 * region the conversion stops and the length is set to the length 00652 * so far converted. A maximum of 255 bytes is converted. 00653 * 00654 * Requires: 00655 * \li The data in 'r' is a sequence of one or more type 00 or type 01000001 00656 * labels. 00657 */ 00658 00659 void 00660 dns_name_toregion(dns_name_t *name, isc_region_t *r); 00661 /*%< 00662 * Make 'r' refer to 'name'. 00663 * 00664 * Requires: 00665 * 00666 * \li 'name' is a valid name. 00667 * 00668 * \li 'r' is a valid region. 00669 */ 00670 00671 isc_result_t 00672 dns_name_fromwire(dns_name_t *name, isc_buffer_t *source, 00673 dns_decompress_t *dctx, unsigned int options, 00674 isc_buffer_t *target); 00675 /*%< 00676 * Copy the possibly-compressed name at source (active region) into target, 00677 * decompressing it. 00678 * 00679 * Notes: 00680 * \li Decompression policy is controlled by 'dctx'. 00681 * 00682 * \li If DNS_NAME_DOWNCASE is set, any uppercase letters in 'source' will be 00683 * downcased when they are copied into 'target'. 00684 * 00685 * Security: 00686 * 00687 * \li *** WARNING *** 00688 * 00689 * \li This routine will often be used when 'source' contains raw network 00690 * data. A programming error in this routine could result in a denial 00691 * of service, or in the hijacking of the server. 00692 * 00693 * Requires: 00694 * 00695 * \li 'name' is a valid name. 00696 * 00697 * \li 'source' is a valid buffer and the first byte of the active 00698 * region should be the first byte of a DNS wire format domain name. 00699 * 00700 * \li 'target' is a valid buffer or 'target' is NULL and 'name' has 00701 * a dedicated buffer. 00702 * 00703 * \li 'dctx' is a valid decompression context. 00704 * 00705 * Ensures: 00706 * 00707 * If result is success: 00708 * \li If 'target' is not NULL, 'name' is attached to it. 00709 * 00710 * \li Uppercase letters are downcased in the copy iff 00711 * DNS_NAME_DOWNCASE is set in options. 00712 * 00713 * \li The current location in source is advanced, and the used space 00714 * in target is updated. 00715 * 00716 * Result: 00717 * \li Success 00718 * \li Bad Form: Label Length 00719 * \li Bad Form: Unknown Label Type 00720 * \li Bad Form: Name Length 00721 * \li Bad Form: Compression type not allowed 00722 * \li Bad Form: Bad compression pointer 00723 * \li Bad Form: Input too short 00724 * \li Resource Limit: Too many compression pointers 00725 * \li Resource Limit: Not enough space in buffer 00726 */ 00727 00728 isc_result_t 00729 dns_name_towire(const dns_name_t *name, dns_compress_t *cctx, 00730 isc_buffer_t *target); 00731 /*%< 00732 * Convert 'name' into wire format, compressing it as specified by the 00733 * compression context 'cctx', and storing the result in 'target'. 00734 * 00735 * Notes: 00736 * \li If the compression context allows global compression, then the 00737 * global compression table may be updated. 00738 * 00739 * Requires: 00740 * \li 'name' is a valid name 00741 * 00742 * \li dns_name_countlabels(name) > 0 00743 * 00744 * \li dns_name_isabsolute(name) == TRUE 00745 * 00746 * \li target is a valid buffer. 00747 * 00748 * \li Any offsets specified in a global compression table are valid 00749 * for buffer. 00750 * 00751 * Ensures: 00752 * 00753 * If the result is success: 00754 * 00755 * \li The used space in target is updated. 00756 * 00757 * Returns: 00758 * \li Success 00759 * \li Resource Limit: Not enough space in buffer 00760 */ 00761 00762 isc_result_t 00763 dns_name_fromtext(dns_name_t *name, isc_buffer_t *source, 00764 const dns_name_t *origin, unsigned int options, 00765 isc_buffer_t *target); 00766 /*%< 00767 * Convert the textual representation of a DNS name at source 00768 * into uncompressed wire form stored in target. 00769 * 00770 * Notes: 00771 * \li Relative domain names will have 'origin' appended to them 00772 * unless 'origin' is NULL, in which case relative domain names 00773 * will remain relative. 00774 * 00775 * \li If DNS_NAME_DOWNCASE is set in 'options', any uppercase letters 00776 * in 'source' will be downcased when they are copied into 'target'. 00777 * 00778 * Requires: 00779 * 00780 * \li 'name' is a valid name. 00781 * 00782 * \li 'source' is a valid buffer. 00783 * 00784 * \li 'target' is a valid buffer or 'target' is NULL and 'name' has 00785 * a dedicated buffer. 00786 * 00787 * Ensures: 00788 * 00789 * If result is success: 00790 * \li If 'target' is not NULL, 'name' is attached to it. 00791 * 00792 * \li Uppercase letters are downcased in the copy iff 00793 * DNS_NAME_DOWNCASE is set in 'options'. 00794 * 00795 * \li The current location in source is advanced, and the used space 00796 * in target is updated. 00797 * 00798 * Result: 00799 *\li #ISC_R_SUCCESS 00800 *\li #DNS_R_EMPTYLABEL 00801 *\li #DNS_R_LABELTOOLONG 00802 *\li #DNS_R_BADESCAPE 00803 *\li (#DNS_R_BADBITSTRING: should not be returned) 00804 *\li (#DNS_R_BITSTRINGTOOLONG: should not be returned) 00805 *\li #DNS_R_BADDOTTEDQUAD 00806 *\li #ISC_R_NOSPACE 00807 *\li #ISC_R_UNEXPECTEDEND 00808 */ 00809 00810 #define DNS_NAME_OMITFINALDOT 0x01U 00811 #define DNS_NAME_MASTERFILE 0x02U /* escape $ and @ */ 00812 00813 isc_result_t 00814 dns_name_toprincipal(const dns_name_t *name, isc_buffer_t *target); 00815 00816 isc_result_t 00817 dns_name_totext(const dns_name_t *name, isc_boolean_t omit_final_dot, 00818 isc_buffer_t *target); 00819 00820 isc_result_t 00821 dns_name_totext2(const dns_name_t *name, unsigned int options, 00822 isc_buffer_t *target); 00823 /*%< 00824 * Convert 'name' into text format, storing the result in 'target'. 00825 * 00826 * Notes: 00827 *\li If 'omit_final_dot' is true, then the final '.' in absolute 00828 * names other than the root name will be omitted. 00829 * 00830 *\li If DNS_NAME_OMITFINALDOT is set in options, then the final '.' 00831 * in absolute names other than the root name will be omitted. 00832 * 00833 *\li If DNS_NAME_MASTERFILE is set in options, '$' and '@' will also 00834 * be escaped. 00835 * 00836 *\li If dns_name_countlabels == 0, the name will be "@", representing the 00837 * current origin as described by RFC1035. 00838 * 00839 *\li The name is not NUL terminated. 00840 * 00841 * Requires: 00842 * 00843 *\li 'name' is a valid name 00844 * 00845 *\li 'target' is a valid buffer. 00846 * 00847 *\li if dns_name_isabsolute == FALSE, then omit_final_dot == FALSE 00848 * 00849 * Ensures: 00850 * 00851 *\li If the result is success: 00852 * the used space in target is updated. 00853 * 00854 * Returns: 00855 *\li #ISC_R_SUCCESS 00856 *\li #ISC_R_NOSPACE 00857 */ 00858 00859 #define DNS_NAME_MAXTEXT 1023 00860 /*%< 00861 * The maximum length of the text representation of a domain 00862 * name as generated by dns_name_totext(). This does not 00863 * include space for a terminating NULL. 00864 * 00865 * This definition is conservative - the actual maximum 00866 * is 1004, derived as follows: 00867 * 00868 * A backslash-decimal escaped character takes 4 bytes. 00869 * A wire-encoded name can be up to 255 bytes and each 00870 * label is one length byte + at most 63 bytes of data. 00871 * Maximizing the label lengths gives us a name of 00872 * three 63-octet labels, one 61-octet label, and the 00873 * root label: 00874 * 00875 * 1 + 63 + 1 + 63 + 1 + 63 + 1 + 61 + 1 = 255 00876 * 00877 * When printed, this is (3 * 63 + 61) * 4 00878 * bytes for the escaped label data + 4 bytes for the 00879 * dot terminating each label = 1004 bytes total. 00880 */ 00881 00882 isc_result_t 00883 dns_name_tofilenametext(dns_name_t *name, isc_boolean_t omit_final_dot, 00884 isc_buffer_t *target); 00885 /*%< 00886 * Convert 'name' into an alternate text format appropriate for filenames, 00887 * storing the result in 'target'. The name data is downcased, guaranteeing 00888 * that the filename does not depend on the case of the converted name. 00889 * 00890 * Notes: 00891 *\li If 'omit_final_dot' is true, then the final '.' in absolute 00892 * names other than the root name will be omitted. 00893 * 00894 *\li The name is not NUL terminated. 00895 * 00896 * Requires: 00897 * 00898 *\li 'name' is a valid absolute name 00899 * 00900 *\li 'target' is a valid buffer. 00901 * 00902 * Ensures: 00903 * 00904 *\li If the result is success: 00905 * the used space in target is updated. 00906 * 00907 * Returns: 00908 *\li #ISC_R_SUCCESS 00909 *\li #ISC_R_NOSPACE 00910 */ 00911 00912 isc_result_t 00913 dns_name_downcase(dns_name_t *source, dns_name_t *name, 00914 isc_buffer_t *target); 00915 /*%< 00916 * Downcase 'source'. 00917 * 00918 * Requires: 00919 * 00920 *\li 'source' and 'name' are valid names. 00921 * 00922 *\li If source == name, then 00923 * 'source' must not be read-only 00924 * 00925 *\li Otherwise, 00926 * 'target' is a valid buffer or 'target' is NULL and 00927 * 'name' has a dedicated buffer. 00928 * 00929 * Returns: 00930 *\li #ISC_R_SUCCESS 00931 *\li #ISC_R_NOSPACE 00932 * 00933 * Note: if source == name, then the result will always be ISC_R_SUCCESS. 00934 */ 00935 00936 isc_result_t 00937 dns_name_concatenate(dns_name_t *prefix, dns_name_t *suffix, 00938 dns_name_t *name, isc_buffer_t *target); 00939 /*%< 00940 * Concatenate 'prefix' and 'suffix'. 00941 * 00942 * Requires: 00943 * 00944 *\li 'prefix' is a valid name or NULL. 00945 * 00946 *\li 'suffix' is a valid name or NULL. 00947 * 00948 *\li 'name' is a valid name or NULL. 00949 * 00950 *\li 'target' is a valid buffer or 'target' is NULL and 'name' has 00951 * a dedicated buffer. 00952 * 00953 *\li If 'prefix' is absolute, 'suffix' must be NULL or the empty name. 00954 * 00955 * Ensures: 00956 * 00957 *\li On success, 00958 * If 'target' is not NULL and 'name' is not NULL, then 'name' 00959 * is attached to it. 00960 * The used space in target is updated. 00961 * 00962 * Returns: 00963 *\li #ISC_R_SUCCESS 00964 *\li #ISC_R_NOSPACE 00965 *\li #DNS_R_NAMETOOLONG 00966 */ 00967 00968 void 00969 dns_name_split(dns_name_t *name, unsigned int suffixlabels, 00970 dns_name_t *prefix, dns_name_t *suffix); 00971 /*%< 00972 * 00973 * Split 'name' into two pieces on a label boundary. 00974 * 00975 * Notes: 00976 * \li 'name' is split such that 'suffix' holds the most significant 00977 * 'suffixlabels' labels. All other labels are stored in 'prefix'. 00978 * 00979 *\li Copying name data is avoided as much as possible, so 'prefix' 00980 * and 'suffix' will end up pointing at the data for 'name'. 00981 * 00982 *\li It is legitimate to pass a 'prefix' or 'suffix' that has 00983 * its name data stored someplace other than the dedicated buffer. 00984 * This is useful to avoid name copying in the calling function. 00985 * 00986 *\li It is also legitimate to pass a 'prefix' or 'suffix' that is 00987 * the same dns_name_t as 'name'. 00988 * 00989 * Requires: 00990 *\li 'name' is a valid name. 00991 * 00992 *\li 'suffixlabels' cannot exceed the number of labels in 'name'. 00993 * 00994 * \li 'prefix' is a valid name or NULL, and cannot be read-only. 00995 * 00996 *\li 'suffix' is a valid name or NULL, and cannot be read-only. 00997 * 00998 *\li If non-NULL, 'prefix' and 'suffix' must have dedicated buffers. 00999 * 01000 *\li 'prefix' and 'suffix' cannot point to the same buffer. 01001 * 01002 * Ensures: 01003 * 01004 *\li On success: 01005 * If 'prefix' is not NULL it will contain the least significant 01006 * labels. 01007 * If 'suffix' is not NULL it will contain the most significant 01008 * labels. dns_name_countlabels(suffix) will be equal to 01009 * suffixlabels. 01010 * 01011 *\li On failure: 01012 * Either 'prefix' or 'suffix' is invalidated (depending 01013 * on which one the problem was encountered with). 01014 * 01015 * Returns: 01016 *\li #ISC_R_SUCCESS No worries. (This function should always success). 01017 */ 01018 01019 isc_result_t 01020 dns_name_dup(const dns_name_t *source, isc_mem_t *mctx, 01021 dns_name_t *target); 01022 /*%< 01023 * Make 'target' a dynamically allocated copy of 'source'. 01024 * 01025 * Requires: 01026 * 01027 *\li 'source' is a valid non-empty name. 01028 * 01029 *\li 'target' is a valid name that is not read-only. 01030 * 01031 *\li 'mctx' is a valid memory context. 01032 */ 01033 01034 isc_result_t 01035 dns_name_dupwithoffsets(dns_name_t *source, isc_mem_t *mctx, 01036 dns_name_t *target); 01037 /*%< 01038 * Make 'target' a read-only dynamically allocated copy of 'source'. 01039 * 'target' will also have a dynamically allocated offsets table. 01040 * 01041 * Requires: 01042 * 01043 *\li 'source' is a valid non-empty name. 01044 * 01045 *\li 'target' is a valid name that is not read-only. 01046 * 01047 *\li 'target' has no offsets table. 01048 * 01049 *\li 'mctx' is a valid memory context. 01050 */ 01051 01052 void 01053 dns_name_free(dns_name_t *name, isc_mem_t *mctx); 01054 /*%< 01055 * Free 'name'. 01056 * 01057 * Requires: 01058 * 01059 *\li 'name' is a valid name created previously in 'mctx' by dns_name_dup(). 01060 * 01061 *\li 'mctx' is a valid memory context. 01062 * 01063 * Ensures: 01064 * 01065 *\li All dynamic resources used by 'name' are freed and the name is 01066 * invalidated. 01067 */ 01068 01069 isc_result_t 01070 dns_name_digest(dns_name_t *name, dns_digestfunc_t digest, void *arg); 01071 /*%< 01072 * Send 'name' in DNSSEC canonical form to 'digest'. 01073 * 01074 * Requires: 01075 * 01076 *\li 'name' is a valid name. 01077 * 01078 *\li 'digest' is a valid dns_digestfunc_t. 01079 * 01080 * Ensures: 01081 * 01082 *\li If successful, the DNSSEC canonical form of 'name' will have been 01083 * sent to 'digest'. 01084 * 01085 *\li If digest() returns something other than ISC_R_SUCCESS, that result 01086 * will be returned as the result of dns_name_digest(). 01087 * 01088 * Returns: 01089 * 01090 *\li #ISC_R_SUCCESS 01091 * 01092 *\li Many other results are possible if not successful. 01093 * 01094 */ 01095 01096 isc_boolean_t 01097 dns_name_dynamic(dns_name_t *name); 01098 /*%< 01099 * Returns whether there is dynamic memory associated with this name. 01100 * 01101 * Requires: 01102 * 01103 *\li 'name' is a valid name. 01104 * 01105 * Returns: 01106 * 01107 *\li 'ISC_TRUE' if the name is dynamic otherwise 'ISC_FALSE'. 01108 */ 01109 01110 isc_result_t 01111 dns_name_print(dns_name_t *name, FILE *stream); 01112 /*%< 01113 * Print 'name' on 'stream'. 01114 * 01115 * Requires: 01116 * 01117 *\li 'name' is a valid name. 01118 * 01119 *\li 'stream' is a valid stream. 01120 * 01121 * Returns: 01122 * 01123 *\li #ISC_R_SUCCESS 01124 * 01125 *\li Any error that dns_name_totext() can return. 01126 */ 01127 01128 void 01129 dns_name_format(const dns_name_t *name, char *cp, unsigned int size); 01130 /*%< 01131 * Format 'name' as text appropriate for use in log messages. 01132 * 01133 * Store the formatted name at 'cp', writing no more than 01134 * 'size' bytes. The resulting string is guaranteed to be 01135 * null terminated. 01136 * 01137 * The formatted name will have a terminating dot only if it is 01138 * the root. 01139 * 01140 * This function cannot fail, instead any errors are indicated 01141 * in the returned text. 01142 * 01143 * Requires: 01144 * 01145 *\li 'name' is a valid name. 01146 * 01147 *\li 'cp' points a valid character array of size 'size'. 01148 * 01149 *\li 'size' > 0. 01150 * 01151 */ 01152 01153 isc_result_t 01154 dns_name_tostring(dns_name_t *source, char **target, isc_mem_t *mctx); 01155 /*%< 01156 * Convert 'name' to string format, allocating sufficient memory to 01157 * hold it (free with isc_mem_free()). 01158 * 01159 * Differs from dns_name_format in that it allocates its own memory. 01160 * 01161 * Requires: 01162 * 01163 *\li 'name' is a valid name. 01164 *\li 'target' is not NULL. 01165 *\li '*target' is NULL. 01166 * 01167 * Returns: 01168 * 01169 *\li ISC_R_SUCCESS 01170 *\li ISC_R_NOMEMORY 01171 * 01172 *\li Any error that dns_name_totext() can return. 01173 */ 01174 01175 isc_result_t 01176 dns_name_fromstring(dns_name_t *target, const char *src, unsigned int options, 01177 isc_mem_t *mctx); 01178 isc_result_t 01179 dns_name_fromstring2(dns_name_t *target, const char *src, 01180 const dns_name_t *origin, unsigned int options, 01181 isc_mem_t *mctx); 01182 /*%< 01183 * Convert a string to a name and place it in target, allocating memory 01184 * as necessary. 'options' has the same semantics as that of 01185 * dns_name_fromtext(). 01186 * 01187 * If 'target' has a buffer then the name will be copied into it rather than 01188 * memory being allocated. 01189 * 01190 * Requires: 01191 * 01192 * \li 'target' is a valid name that is not read-only. 01193 * \li 'src' is not NULL. 01194 * 01195 * Returns: 01196 * 01197 *\li #ISC_R_SUCCESS 01198 * 01199 *\li Any error that dns_name_fromtext() can return. 01200 * 01201 *\li Any error that dns_name_dup() can return. 01202 */ 01203 01204 isc_result_t 01205 dns_name_settotextfilter(dns_name_totextfilter_t proc); 01206 /*%< 01207 * Set / clear a thread specific function 'proc' to be called at the 01208 * end of dns_name_totext(). 01209 * 01210 * Note: Under Windows you need to call "dns_name_settotextfilter(NULL);" 01211 * prior to exiting the thread otherwise memory will be leaked. 01212 * For other platforms, which are pthreads based, this is still a good 01213 * idea but not required. 01214 * 01215 * Returns 01216 *\li #ISC_R_SUCCESS 01217 *\li #ISC_R_UNEXPECTED 01218 */ 01219 01220 #define DNS_NAME_FORMATSIZE (DNS_NAME_MAXTEXT + 1) 01221 /*%< 01222 * Suggested size of buffer passed to dns_name_format(). 01223 * Includes space for the terminating NULL. 01224 */ 01225 01226 isc_result_t 01227 dns_name_copy(const dns_name_t *source, dns_name_t *dest, isc_buffer_t *target); 01228 /*%< 01229 * Makes 'dest' refer to a copy of the name in 'source'. The data are 01230 * either copied to 'target' or the dedicated buffer in 'dest'. 01231 * 01232 * Requires: 01233 * \li 'source' is a valid name. 01234 * 01235 * \li 'dest' is an initialized name with a dedicated buffer. 01236 * 01237 * \li 'target' is NULL or an initialized buffer. 01238 * 01239 * \li Either dest has a dedicated buffer or target != NULL. 01240 * 01241 * Ensures: 01242 * 01243 *\li On success, the used space in target is updated. 01244 * 01245 * Returns: 01246 *\li #ISC_R_SUCCESS 01247 *\li #ISC_R_NOSPACE 01248 */ 01249 01250 isc_boolean_t 01251 dns_name_ishostname(const dns_name_t *name, isc_boolean_t wildcard); 01252 /*%< 01253 * Return if 'name' is a valid hostname. RFC 952 / RFC 1123. 01254 * If 'wildcard' is ISC_TRUE then allow the first label of name to 01255 * be a wildcard. 01256 * The root is also accepted. 01257 * 01258 * Requires: 01259 * 'name' to be valid. 01260 */ 01261 01262 01263 isc_boolean_t 01264 dns_name_ismailbox(const dns_name_t *name); 01265 /*%< 01266 * Return if 'name' is a valid mailbox. RFC 821. 01267 * 01268 * Requires: 01269 * \li 'name' to be valid. 01270 */ 01271 01272 isc_boolean_t 01273 dns_name_internalwildcard(const dns_name_t *name); 01274 /*%< 01275 * Return if 'name' contains a internal wildcard name. 01276 * 01277 * Requires: 01278 * \li 'name' to be valid. 01279 */ 01280 01281 void 01282 dns_name_destroy(void); 01283 /*%< 01284 * Cleanup dns_name_settotextfilter() / dns_name_totext() state. 01285 * 01286 * This should be called as part of the final cleanup process. 01287 * 01288 * Note: dns_name_settotextfilter(NULL); should be called for all 01289 * threads which have called dns_name_settotextfilter() with a 01290 * non-NULL argument prior to calling dns_name_destroy(); 01291 */ 01292 01293 ISC_LANG_ENDDECLS 01294 01295 /* 01296 *** High Performance Macros 01297 ***/ 01298 01299 /* 01300 * WARNING: Use of these macros by applications may require recompilation 01301 * of the application in some situations where calling the function 01302 * would not. 01303 * 01304 * WARNING: No assertion checking is done for these macros. 01305 */ 01306 01307 #define DNS_NAME_INIT(n, o) \ 01308 do { \ 01309 dns_name_t *_n = (n); \ 01310 /* memset(_n, 0, sizeof(*_n)); */ \ 01311 _n->magic = DNS_NAME_MAGIC; \ 01312 _n->ndata = NULL; \ 01313 _n->length = 0; \ 01314 _n->labels = 0; \ 01315 _n->attributes = 0; \ 01316 _n->offsets = (o); \ 01317 _n->buffer = NULL; \ 01318 ISC_LINK_INIT(_n, link); \ 01319 ISC_LIST_INIT(_n->list); \ 01320 } while (0) 01321 01322 #define DNS_NAME_RESET(n) \ 01323 do { \ 01324 (n)->ndata = NULL; \ 01325 (n)->length = 0; \ 01326 (n)->labels = 0; \ 01327 (n)->attributes &= ~DNS_NAMEATTR_ABSOLUTE; \ 01328 if ((n)->buffer != NULL) \ 01329 isc_buffer_clear((n)->buffer); \ 01330 } while (0) 01331 01332 #define DNS_NAME_SETBUFFER(n, b) \ 01333 (n)->buffer = (b) 01334 01335 #define DNS_NAME_ISABSOLUTE(n) \ 01336 (((n)->attributes & DNS_NAMEATTR_ABSOLUTE) != 0 ? ISC_TRUE : ISC_FALSE) 01337 01338 #define DNS_NAME_COUNTLABELS(n) \ 01339 ((n)->labels) 01340 01341 #define DNS_NAME_TOREGION(n, r) \ 01342 do { \ 01343 (r)->base = (n)->ndata; \ 01344 (r)->length = (n)->length; \ 01345 } while (0) 01346 01347 #define DNS_NAME_SPLIT(n, l, p, s) \ 01348 do { \ 01349 dns_name_t *_n = (n); \ 01350 dns_name_t *_p = (p); \ 01351 dns_name_t *_s = (s); \ 01352 unsigned int _l = (l); \ 01353 if (_p != NULL) \ 01354 dns_name_getlabelsequence(_n, 0, _n->labels - _l, _p); \ 01355 if (_s != NULL) \ 01356 dns_name_getlabelsequence(_n, _n->labels - _l, _l, _s); \ 01357 } while (0) 01358 01359 #ifdef DNS_NAME_USEINLINE 01360 01361 #define dns_name_init(n, o) DNS_NAME_INIT(n, o) 01362 #define dns_name_reset(n) DNS_NAME_RESET(n) 01363 #define dns_name_setbuffer(n, b) DNS_NAME_SETBUFFER(n, b) 01364 #define dns_name_countlabels(n) DNS_NAME_COUNTLABELS(n) 01365 #define dns_name_isabsolute(n) DNS_NAME_ISABSOLUTE(n) 01366 #define dns_name_toregion(n, r) DNS_NAME_TOREGION(n, r) 01367 #define dns_name_split(n, l, p, s) DNS_NAME_SPLIT(n, l, p, s) 01368 01369 #endif /* DNS_NAME_USEINLINE */ 01370 01371 #endif /* DNS_NAME_H */