2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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: name.c,v 1.127.2.9 2004/03/09 06:11:03 marka Exp $ */
24 #include <isc/buffer.h>
27 #include <isc/print.h>
28 #include <isc/string.h>
31 #include <dns/compress.h>
33 #include <dns/result.h>
35 #define VALID_NAME(n) ISC_MAGIC_VALID(n, DNS_NAME_MAGIC)
65 static char digitvalue
[256] = {
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
69 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
70 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
72 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
80 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
81 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
84 static char hexdigits
[16] = {
85 '0', '1', '2', '3', '4', '5', '6', '7',
86 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
89 static unsigned char maptolower
[] = {
90 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
91 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
92 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
93 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
94 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
95 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
96 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
97 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
98 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
99 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
100 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
101 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
102 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
103 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
104 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
105 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
106 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
107 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
108 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
109 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
110 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
111 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
112 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
113 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
114 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
115 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
116 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
117 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
118 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
119 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
120 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
121 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
124 #define CONVERTTOASCII(c)
125 #define CONVERTFROMASCII(c)
127 #define INIT_OFFSETS(name, var, default) \
128 if (name->offsets != NULL) \
129 var = name->offsets; \
133 #define SETUP_OFFSETS(name, var, default) \
134 if (name->offsets != NULL) \
135 var = name->offsets; \
138 set_offsets(name, var, NULL); \
142 * Note: If additional attributes are added that should not be set for
143 * empty names, MAKE_EMPTY() must be changed so it clears them.
145 #define MAKE_EMPTY(name) \
147 name->ndata = NULL; \
150 name->attributes &= ~DNS_NAMEATTR_ABSOLUTE; \
154 * A name is "bindable" if it can be set to point to a new value, i.e.
155 * name->ndata and name->length may be changed.
157 #define BINDABLE(name) \
158 ((name->attributes & (DNS_NAMEATTR_READONLY|DNS_NAMEATTR_DYNAMIC)) \
162 * Note that the name data must be a char array, not a string
163 * literal, to avoid compiler warnings about discarding
164 * the const attribute of a string.
166 static unsigned char root_ndata
[] = { '\0' };
167 static unsigned char root_offsets
[] = { 0 };
169 static dns_name_t root
=
173 DNS_NAMEATTR_READONLY
| DNS_NAMEATTR_ABSOLUTE
,
175 {(void *)-1, (void *)-1},
179 /* XXXDCL make const? */
180 dns_name_t
*dns_rootname
= &root
;
182 static unsigned char wild_ndata
[] = { '\001', '*' };
183 static unsigned char wild_offsets
[] = { 0 };
185 static dns_name_t wild
=
189 DNS_NAMEATTR_READONLY
,
191 {(void *)-1, (void *)-1},
195 /* XXXDCL make const? */
196 dns_name_t
*dns_wildcardname
= &wild
;
199 set_offsets(const dns_name_t
*name
, unsigned char *offsets
,
200 dns_name_t
*set_name
);
203 compact(dns_name_t
*name
, unsigned char *offsets
);
206 * Yes, get_bit and set_bit are lame. We define them here so they can
207 * be inlined by smart compilers.
210 static inline unsigned int
211 get_bit(unsigned char *array
, unsigned int idx
) {
212 unsigned int byte
, shift
;
214 byte
= array
[idx
/ 8];
215 shift
= 7 - (idx
% 8);
217 return ((byte
>> shift
) & 0x01);
221 set_bit(unsigned char *array
, unsigned int idx
, unsigned int bit
) {
222 unsigned int shift
, mask
;
224 shift
= 7 - (idx
% 8);
228 array
[idx
/ 8] |= mask
;
230 array
[idx
/ 8] &= (~mask
& 0xFF);
234 dns_label_type(dns_label_t
*label
) {
236 * Get the type of 'label'.
239 REQUIRE(label
!= NULL
);
240 REQUIRE(label
->length
> 0);
241 REQUIRE(label
->base
[0] <= 63 ||
242 label
->base
[0] == DNS_LABELTYPE_BITSTRING
);
244 if (label
->base
[0] <= 63)
245 return (dns_labeltype_ordinary
);
247 return (dns_labeltype_bitstring
);
251 dns_label_countbits(dns_label_t
*label
) {
255 * The number of bits in a bitstring label.
258 REQUIRE(label
!= NULL
);
259 REQUIRE(label
->length
> 2);
260 REQUIRE(label
->base
[0] == DNS_LABELTYPE_BITSTRING
);
262 count
= label
->base
[1];
270 dns_label_getbit(dns_label_t
*label
, unsigned int n
) {
271 unsigned int count
, bit
;
274 * The 'n'th most significant bit of 'label'.
277 * Numbering starts at 0.
280 REQUIRE(label
!= NULL
);
281 REQUIRE(label
->length
> 2);
282 REQUIRE(label
->base
[0] == DNS_LABELTYPE_BITSTRING
);
284 count
= label
->base
[1];
290 bit
= get_bit(&label
->base
[2], n
);
292 return (dns_bitlabel_0
);
293 return (dns_bitlabel_1
);
297 dns_name_init(dns_name_t
*name
, unsigned char *offsets
) {
301 DNS_NAME_INIT(name
, offsets
);
305 dns_name_reset(dns_name_t
*name
) {
306 REQUIRE(VALID_NAME(name
));
307 REQUIRE(BINDABLE(name
));
309 DNS_NAME_RESET(name
);
313 dns_name_invalidate(dns_name_t
*name
) {
315 * Make 'name' invalid.
318 REQUIRE(VALID_NAME(name
));
324 name
->attributes
= 0;
325 name
->offsets
= NULL
;
327 ISC_LINK_INIT(name
, link
);
331 dns_name_setbuffer(dns_name_t
*name
, isc_buffer_t
*buffer
) {
333 * Dedicate a buffer for use with 'name'.
336 REQUIRE(VALID_NAME(name
));
337 REQUIRE((buffer
!= NULL
&& name
->buffer
== NULL
) ||
340 name
->buffer
= buffer
;
344 dns_name_hasbuffer(const dns_name_t
*name
) {
346 * Does 'name' have a dedicated buffer?
349 REQUIRE(VALID_NAME(name
));
351 if (name
->buffer
!= NULL
)
358 dns_name_isabsolute(const dns_name_t
*name
) {
361 * Does 'name' end in the root label?
364 REQUIRE(VALID_NAME(name
));
366 if ((name
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
372 dns_name_iswildcard(const dns_name_t
*name
) {
373 unsigned char *ndata
;
376 * Is 'name' a wildcard name?
379 REQUIRE(VALID_NAME(name
));
380 REQUIRE(name
->labels
> 0);
382 if (name
->length
>= 2) {
384 if (ndata
[0] == 1 && ndata
[1] == '*')
392 dns_name_requiresedns(const dns_name_t
*name
) {
393 unsigned int count
, nrem
;
394 unsigned char *ndata
;
395 isc_boolean_t requiresedns
= ISC_FALSE
;
398 * Does 'name' require EDNS for transmission?
401 REQUIRE(VALID_NAME(name
));
402 REQUIRE(name
->labels
> 0);
412 INSIST(count
== DNS_LABELTYPE_BITSTRING
);
413 requiresedns
= ISC_TRUE
;
416 INSIST(nrem
>= count
);
421 return (requiresedns
);
425 dns_name_hash(dns_name_t
*name
, isc_boolean_t case_sensitive
) {
427 const unsigned char *s
;
432 * Provide a hash value for 'name'.
434 REQUIRE(VALID_NAME(name
));
436 if (name
->labels
== 0)
438 length
= name
->length
;
443 * This hash function is similar to the one Ousterhout
447 if (case_sensitive
) {
449 h
+= ( h
<< 3 ) + *s
;
466 dns_fullname_hash(dns_name_t
*name
, isc_boolean_t case_sensitive
) {
468 * Provide a hash value for 'name'.
470 REQUIRE(VALID_NAME(name
));
472 if (name
->labels
== 0)
475 return (isc_hash_calc((const unsigned char *)name
->ndata
,
476 name
->length
, case_sensitive
));
480 dns_name_fullcompare(const dns_name_t
*name1
, const dns_name_t
*name2
,
482 unsigned int *nlabelsp
, unsigned int *nbitsp
)
484 unsigned int l1
, l2
, l
, count1
, count2
, count
;
485 unsigned int b1
, b2
, n
, nlabels
, nbits
;
486 int cdiff
, ldiff
, chdiff
;
487 unsigned char *label1
, *label2
;
488 unsigned char *offsets1
, *offsets2
;
489 dns_offsets_t odata1
, odata2
;
490 dns_namereln_t namereln
= dns_namereln_none
;
493 * Determine the relative ordering under the DNSSEC order relation of
494 * 'name1' and 'name2', and also determine the hierarchical
495 * relationship of the names.
497 * Note: It makes no sense for one of the names to be relative and the
498 * other absolute. If both names are relative, then to be meaningfully
499 * compared the caller must ensure that they are both relative to the
503 REQUIRE(VALID_NAME(name1
));
504 REQUIRE(VALID_NAME(name2
));
505 REQUIRE(orderp
!= NULL
);
506 REQUIRE(nlabelsp
!= NULL
);
507 REQUIRE(nbitsp
!= NULL
);
509 * Either name1 is absolute and name2 is absolute, or neither is.
511 REQUIRE((name1
->attributes
& DNS_NAMEATTR_ABSOLUTE
) ==
512 (name2
->attributes
& DNS_NAMEATTR_ABSOLUTE
));
514 SETUP_OFFSETS(name1
, offsets1
, odata1
);
515 SETUP_OFFSETS(name2
, offsets2
, odata2
);
521 ldiff
= (int)l1
- (int)l2
;
531 label1
= &name1
->ndata
[offsets1
[l1
]];
532 label2
= &name2
->ndata
[offsets2
[l2
]];
535 if (count1
<= 63 && count2
<= 63) {
536 cdiff
= (int)count1
- (int)count2
;
543 chdiff
= (int)maptolower
[*label1
] -
544 (int)maptolower
[*label2
];
558 } else if (count1
== DNS_LABELTYPE_BITSTRING
&& count2
<= 63) {
564 } else if (count2
== DNS_LABELTYPE_BITSTRING
&& count1
<= 63) {
571 INSIST(count1
== DNS_LABELTYPE_BITSTRING
&&
572 count2
== DNS_LABELTYPE_BITSTRING
);
579 if (count1
< count2
) {
589 /* Yes, this loop is really slow! */
590 for (n
= 0; n
< count
; n
++) {
591 b1
= get_bit(label1
, n
);
592 b2
= get_bit(label2
, n
);
596 } else if (b1
> b2
) {
606 * If we're here, then we have two bitstrings
607 * of differing length.
609 * If the name with the shorter bitstring
610 * has any labels, then it must be greater
611 * than the longer bitstring. This is a bit
612 * counterintuitive. If the name with the
613 * shorter bitstring has any more labels, then
614 * the next label must be an ordinary label.
615 * It can't be a bitstring label because if it
616 * were, then there would be room for it in
617 * the current bitstring label (since all
618 * bitstrings are canonicalized). Since
619 * there's at least one more bit in the
620 * name with the longer bitstring, and since
621 * a bitlabel sorts before any ordinary label,
622 * the name with the longer bitstring must
623 * be lexically before the one with the shorter
626 * On the other hand, if there are no more
627 * labels in the name with the shorter
628 * bitstring, then that name contains the
631 namereln
= dns_namereln_commonancestor
;
638 dns_namereln_contains
;
646 dns_namereln_subdomain
;
657 namereln
= dns_namereln_contains
;
659 namereln
= dns_namereln_subdomain
;
661 namereln
= dns_namereln_equal
;
667 if (nlabels
> 0 && namereln
== dns_namereln_none
)
668 namereln
= dns_namereln_commonancestor
;
674 dns_name_compare(const dns_name_t
*name1
, const dns_name_t
*name2
) {
676 unsigned int nlabels
, nbits
;
679 * Determine the relative ordering under the DNSSEC order relation of
680 * 'name1' and 'name2'.
682 * Note: It makes no sense for one of the names to be relative and the
683 * other absolute. If both names are relative, then to be meaningfully
684 * compared the caller must ensure that they are both relative to the
688 (void)dns_name_fullcompare(name1
, name2
, &order
, &nlabels
, &nbits
);
694 dns_name_equal(const dns_name_t
*name1
, const dns_name_t
*name2
) {
695 unsigned int l
, count
;
697 unsigned char *label1
, *label2
;
700 * Are 'name1' and 'name2' equal?
702 * Note: It makes no sense for one of the names to be relative and the
703 * other absolute. If both names are relative, then to be meaningfully
704 * compared the caller must ensure that they are both relative to the
708 REQUIRE(VALID_NAME(name1
));
709 REQUIRE(VALID_NAME(name2
));
711 * Either name1 is absolute and name2 is absolute, or neither is.
713 REQUIRE((name1
->attributes
& DNS_NAMEATTR_ABSOLUTE
) ==
714 (name2
->attributes
& DNS_NAMEATTR_ABSOLUTE
));
716 if (name1
->length
!= name2
->length
)
721 if (l
!= name2
->labels
)
724 label1
= name1
->ndata
;
725 label2
= name2
->ndata
;
729 if (count
!= *label2
++)
734 c
= maptolower
[*label1
++];
735 if (c
!= maptolower
[*label2
++])
739 INSIST(count
== DNS_LABELTYPE_BITSTRING
);
741 if (count
!= *label2
++)
748 count
= (count
+ 7) / 8;
762 dns_name_rdatacompare(const dns_name_t
*name1
, const dns_name_t
*name2
) {
763 unsigned int l1
, l2
, l
, count1
, count2
, count
;
764 unsigned char c1
, c2
;
765 unsigned char *label1
, *label2
;
768 * Compare two absolute names as rdata.
771 REQUIRE(VALID_NAME(name1
));
772 REQUIRE(name1
->labels
> 0);
773 REQUIRE((name1
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0);
774 REQUIRE(VALID_NAME(name2
));
775 REQUIRE(name2
->labels
> 0);
776 REQUIRE((name2
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0);
781 l
= (l1
< l2
) ? l1
: l2
;
783 label1
= name1
->ndata
;
784 label2
= name2
->ndata
;
789 if (count1
<= 63 && count2
<= 63) {
790 if (count1
!= count2
)
791 return ((count1
< count2
) ? -1 : 1);
795 c1
= maptolower
[*label1
++];
796 c2
= maptolower
[*label2
++];
802 } else if (count1
== DNS_LABELTYPE_BITSTRING
&& count2
<= 63) {
804 } else if (count2
== DNS_LABELTYPE_BITSTRING
&& count1
<= 63) {
807 INSIST(count1
== DNS_LABELTYPE_BITSTRING
&&
808 count2
== DNS_LABELTYPE_BITSTRING
);
811 if (count1
!= count2
)
812 return ((count1
< count2
) ? -1 : 1);
817 /* number of bytes */
818 count
= (count1
+ 7) / 8;
824 return ((c1
< c2
) ? -1 : 1);
830 * If one name had more labels than the other, their common
831 * prefix must have been different because the shorter name
832 * ended with the root label and the longer one can't have
833 * a root label in the middle of it. Therefore, if we get
834 * to this point, the lengths must be equal.
842 dns_name_issubdomain(const dns_name_t
*name1
, const dns_name_t
*name2
) {
844 unsigned int nlabels
, nbits
;
845 dns_namereln_t namereln
;
848 * Is 'name1' a subdomain of 'name2'?
850 * Note: It makes no sense for one of the names to be relative and the
851 * other absolute. If both names are relative, then to be meaningfully
852 * compared the caller must ensure that they are both relative to the
856 namereln
= dns_name_fullcompare(name1
, name2
, &order
, &nlabels
,
858 if (namereln
== dns_namereln_subdomain
||
859 namereln
== dns_namereln_equal
)
866 dns_name_matcheswildcard(const dns_name_t
*name
, const dns_name_t
*wname
) {
868 unsigned int nlabels
, nbits
, labels
;
871 REQUIRE(VALID_NAME(name
));
872 REQUIRE(name
->labels
> 0);
873 REQUIRE(VALID_NAME(wname
));
874 labels
= wname
->labels
;
876 REQUIRE(dns_name_iswildcard(wname
));
878 DNS_NAME_INIT(&tname
, NULL
);
879 dns_name_getlabelsequence(wname
, 1, labels
- 1, &tname
);
880 if (dns_name_fullcompare(name
, &tname
, &order
, &nlabels
, &nbits
) ==
881 dns_namereln_subdomain
)
887 dns_name_depth(const dns_name_t
*name
) {
888 unsigned int depth
, count
, nrem
, n
;
889 unsigned char *ndata
;
892 * The depth of 'name'.
895 REQUIRE(VALID_NAME(name
));
897 if (name
->labels
== 0)
907 INSIST(count
== DNS_LABELTYPE_BITSTRING
);
922 INSIST(nrem
>= count
);
931 dns_name_countlabels(const dns_name_t
*name
) {
933 * How many labels does 'name' have?
936 REQUIRE(VALID_NAME(name
));
938 ENSURE(name
->labels
<= 128);
940 return (name
->labels
);
944 dns_name_getlabel(const dns_name_t
*name
, unsigned int n
, dns_label_t
*label
) {
945 unsigned char *offsets
;
949 * Make 'label' refer to the 'n'th least significant label of 'name'.
952 REQUIRE(VALID_NAME(name
));
953 REQUIRE(name
->labels
> 0);
954 REQUIRE(n
< name
->labels
);
955 REQUIRE(label
!= NULL
);
957 SETUP_OFFSETS(name
, offsets
, odata
);
959 label
->base
= &name
->ndata
[offsets
[n
]];
960 if (n
== name
->labels
- 1)
961 label
->length
= name
->length
- offsets
[n
];
963 label
->length
= offsets
[n
+ 1] - offsets
[n
];
967 dns_name_getlabelsequence(const dns_name_t
*source
,
968 unsigned int first
, unsigned int n
,
971 unsigned char *offsets
;
973 unsigned int firstoffset
, endoffset
;
976 * Make 'target' refer to the 'n' labels including and following
977 * 'first' in 'source'.
980 REQUIRE(VALID_NAME(source
));
981 REQUIRE(VALID_NAME(target
));
982 REQUIRE(first
<= source
->labels
);
983 REQUIRE(first
+ n
<= source
->labels
);
984 REQUIRE(BINDABLE(target
));
986 SETUP_OFFSETS(source
, offsets
, odata
);
988 if (first
== source
->labels
)
989 firstoffset
= source
->length
;
991 firstoffset
= offsets
[first
];
993 if (first
+ n
== source
->labels
)
994 endoffset
= source
->length
;
996 endoffset
= offsets
[first
+ n
];
998 target
->ndata
= &source
->ndata
[firstoffset
];
999 target
->length
= endoffset
- firstoffset
;
1001 if (first
+ n
== source
->labels
&& n
> 0 &&
1002 (source
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
1003 target
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
1005 target
->attributes
&= ~DNS_NAMEATTR_ABSOLUTE
;
1010 * If source and target are the same, and we're making target
1011 * a prefix of source, the offsets table is correct already
1012 * so we don't need to call set_offsets().
1014 if (target
->offsets
!= NULL
&&
1015 (target
!= source
|| first
!= 0))
1016 set_offsets(target
, target
->offsets
, NULL
);
1020 dns_name_clone(dns_name_t
*source
, dns_name_t
*target
) {
1023 * Make 'target' refer to the same name as 'source'.
1026 REQUIRE(VALID_NAME(source
));
1027 REQUIRE(VALID_NAME(target
));
1028 REQUIRE(BINDABLE(target
));
1030 target
->ndata
= source
->ndata
;
1031 target
->length
= source
->length
;
1032 target
->labels
= source
->labels
;
1033 target
->attributes
= source
->attributes
&
1034 (unsigned int)~(DNS_NAMEATTR_READONLY
| DNS_NAMEATTR_DYNAMIC
|
1035 DNS_NAMEATTR_DYNOFFSETS
);
1036 if (target
->offsets
!= NULL
&& source
->labels
> 0) {
1037 if (source
->offsets
!= NULL
)
1038 memcpy(target
->offsets
, source
->offsets
,
1041 set_offsets(target
, target
->offsets
, NULL
);
1046 dns_name_fromregion(dns_name_t
*name
, isc_region_t
*r
) {
1047 unsigned char *offsets
;
1048 dns_offsets_t odata
;
1053 * Make 'name' refer to region 'r'.
1056 REQUIRE(VALID_NAME(name
));
1058 REQUIRE(BINDABLE(name
));
1060 INIT_OFFSETS(name
, offsets
, odata
);
1062 if (name
->buffer
!= NULL
) {
1063 isc_buffer_clear(name
->buffer
);
1064 isc_buffer_availableregion(name
->buffer
, &r2
);
1065 len
= (r
->length
< r2
.length
) ? r
->length
: r2
.length
;
1066 if (len
> DNS_NAME_MAXWIRE
)
1067 len
= DNS_NAME_MAXWIRE
;
1068 memcpy(r2
.base
, r
->base
, len
);
1069 name
->ndata
= r2
.base
;
1072 name
->ndata
= r
->base
;
1073 name
->length
= (r
->length
<= DNS_NAME_MAXWIRE
) ?
1074 r
->length
: DNS_NAME_MAXWIRE
;
1078 set_offsets(name
, offsets
, name
);
1081 name
->attributes
&= ~DNS_NAMEATTR_ABSOLUTE
;
1084 if (name
->buffer
!= NULL
)
1085 isc_buffer_add(name
->buffer
, name
->length
);
1089 dns_name_toregion(dns_name_t
*name
, isc_region_t
*r
) {
1091 * Make 'r' refer to 'name'.
1094 REQUIRE(VALID_NAME(name
));
1097 DNS_NAME_TOREGION(name
, r
);
1102 dns_name_fromtext(dns_name_t
*name
, isc_buffer_t
*source
,
1103 dns_name_t
*origin
, isc_boolean_t downcase
,
1104 isc_buffer_t
*target
)
1106 unsigned char *ndata
, *label
;
1109 ft_state state
, kind
;
1110 unsigned int value
, count
, tbcount
, bitlength
, maxlength
;
1111 unsigned int n1
, n2
, vlen
, tlen
, nrem
, nused
, digits
, labels
, tused
;
1112 isc_boolean_t done
, saw_bitstring
;
1113 unsigned char dqchars
[4];
1114 unsigned char *offsets
;
1115 dns_offsets_t odata
;
1118 * Convert the textual representation of a DNS name at source
1119 * into uncompressed wire form stored in target.
1122 * Relative domain names will have 'origin' appended to them
1123 * unless 'origin' is NULL, in which case relative domain names
1124 * will remain relative.
1127 REQUIRE(VALID_NAME(name
));
1128 REQUIRE(ISC_BUFFER_VALID(source
));
1129 REQUIRE((target
!= NULL
&& ISC_BUFFER_VALID(target
)) ||
1130 (target
== NULL
&& ISC_BUFFER_VALID(name
->buffer
)));
1132 if (target
== NULL
&& name
->buffer
!= NULL
) {
1133 target
= name
->buffer
;
1134 isc_buffer_clear(target
);
1137 REQUIRE(BINDABLE(name
));
1139 INIT_OFFSETS(name
, offsets
, odata
);
1143 * Initialize things to make the compiler happy; they're not required.
1158 * Make 'name' empty in case of failure.
1163 * Set up the state machine.
1165 tdata
= (char *)source
->base
+ source
->current
;
1166 tlen
= isc_buffer_remaininglength(source
);
1168 ndata
= isc_buffer_used(target
);
1169 nrem
= isc_buffer_availablelength(target
);
1175 saw_bitstring
= ISC_FALSE
;
1178 while (nrem
> 0 && tlen
> 0 && !done
) {
1187 * Is this the root name?
1191 return (DNS_R_EMPTYLABEL
);
1199 if (c
== '@' && tlen
== 0) {
1212 state
= ft_initialescape
;
1216 state
= ft_ordinary
;
1218 return (ISC_R_NOSPACE
);
1223 return (DNS_R_EMPTYLABEL
);
1226 INSIST(labels
<= 127);
1227 offsets
[labels
] = nused
;
1236 } else if (c
== '\\') {
1240 return (DNS_R_LABELTOOLONG
);
1244 c
= maptolower
[(int)c
];
1250 case ft_initialescape
:
1252 saw_bitstring
= ISC_TRUE
;
1253 kind
= ft_bitstring
;
1254 state
= ft_bitstring
;
1255 *label
= DNS_LABELTYPE_BITSTRING
;
1266 if (!isdigit(c
& 0xff)) {
1268 return (DNS_R_LABELTOOLONG
);
1272 c
= maptolower
[(int)c
];
1276 state
= ft_ordinary
;
1281 state
= ft_escdecimal
;
1284 if (!isdigit(c
& 0xff))
1285 return (DNS_R_BADESCAPE
);
1287 value
+= digitvalue
[(int)c
];
1291 return (DNS_R_BADESCAPE
);
1293 return (DNS_R_LABELTOOLONG
);
1296 value
= maptolower
[value
];
1300 state
= ft_ordinary
;
1312 } else if (c
== 'o') {
1317 } else if (c
== 'x') {
1322 } else if (isdigit(c
& 0xff)) {
1328 kind
= ft_dottedquad
;
1329 state
= ft_dqdecimal
;
1332 return (DNS_R_BADBITSTRING
);
1335 if (c
!= '0' && c
!= '1') {
1336 state
= ft_maybeslash
;
1345 return (DNS_R_BITSTRINGTOOLONG
);
1354 if (!isdigit(c
& 0xff) || c
== '9' || c
== '8') {
1355 state
= ft_maybeslash
;
1359 value
+= digitvalue
[(int)c
];
1363 * The total bit count is tested against 258 instead
1364 * of 256 because of the possibility that the bitstring
1365 * label is exactly 256 bits long; on the last octal
1366 * digit (which must be 4) tbcount is incremented
1367 * from 255 to 258. This case is adequately handled
1371 return (DNS_R_BITSTRINGTOOLONG
);
1377 } else if (count
== 9) {
1378 *ndata
++ = (value
>> 1);
1383 } else if (count
== 10) {
1384 *ndata
++ = (value
>> 2);
1392 if (!isxdigit(c
& 0xff)) {
1393 state
= ft_maybeslash
;
1397 value
+= digitvalue
[(int)c
];
1401 return (DNS_R_BITSTRINGTOOLONG
);
1410 if (c
!= '.' && n1
< 3)
1411 return (DNS_R_BADDOTTEDQUAD
);
1412 dqchars
[n1
] = value
;
1419 state
= ft_maybeslash
;
1424 state
= ft_dqdecimal
;
1427 if (!isdigit(c
& 0xff)) {
1428 if (digits
== 0 || value
> 255)
1429 return (DNS_R_BADDOTTEDQUAD
);
1430 state
= ft_dottedquad
;
1435 return (DNS_R_BADDOTTEDQUAD
);
1437 value
+= digitvalue
[(int)c
];
1442 state
= ft_bitlength
;
1446 case ft_finishbitstring
:
1449 return (DNS_R_BADBITSTRING
);
1457 if (bitlength
!= 0) {
1458 if (bitlength
> tbcount
)
1459 return (DNS_R_BADBITSTRING
);
1460 if (kind
== ft_binary
&&
1461 bitlength
!= tbcount
) {
1462 return (DNS_R_BADBITSTRING
);
1463 } else if (kind
== ft_octal
) {
1465 * Figure out correct number
1466 * of octal digits for the
1467 * bitlength, and compare to
1471 if (bitlength
% 3 != 0)
1474 /* tbcount % 3 == 0 */
1476 return (DNS_R_BADBITSTRING
);
1479 * Check that no bits extend
1480 * past the end of the last
1481 * byte that is included in
1482 * the bitlength. Example:
1483 * \[o036/8] == \[b00001111],
1484 * which fits into just one
1485 * byte, but the three octal
1486 * digits actually specified
1487 * two bytes worth of data,
1488 * 9 bits, before the bitlength
1489 * limited it back to one byte.
1491 * n1 is the number of bytes
1492 * necessary for the bitlength.
1493 * n2 is the number of bytes
1494 * encompassed by the octal
1495 * digits. If they are not
1496 * equal, then "value" holds
1497 * the excess bits, which
1498 * must be zero. If the bits
1499 * are zero, then "count" is
1500 * zero'ed to prevent the
1501 * addition of another byte
1504 n1
= (bitlength
- 1) / 8;
1505 n2
= (tbcount
- 1) / 8;
1509 (DNS_R_BADBITSTRING
);
1513 } else if (kind
== ft_hex
) {
1515 * Figure out correct number
1516 * of hex digits for the
1517 * bitlength, and compare to
1521 if (bitlength
% 4 != 0)
1524 /* tbcount % 4 == 0 */
1526 return (DNS_R_BADBITSTRING
);
1528 n1
= bitlength
% vlen
;
1531 * Are the pad bits in the
1532 * last 'vlen' bits zero?
1535 ~((~0) << (vlen
-n1
))) != 0)
1536 return (DNS_R_BADBITSTRING
);
1538 } else if (kind
== ft_dottedquad
)
1540 else if (tbcount
> 256)
1542 * This can happen when an octal
1543 * bitstring label of 86 octal digits
1544 * is specified; tbcount will be 258.
1545 * This is not trapped above because
1546 * the bitstring label might be limited
1547 * by a "/256" modifier.
1549 return (DNS_R_BADBITSTRING
);
1551 bitlength
= tbcount
;
1559 if (kind
== ft_dottedquad
) {
1561 if (bitlength
% 8 != 0)
1564 return (ISC_R_NOSPACE
);
1565 for (n2
= 0; n2
< n1
; n2
++) {
1566 *ndata
++ = dqchars
[n2
];
1571 if (bitlength
== 256)
1576 INSIST(labels
<= 127);
1577 offsets
[labels
] = nused
;
1579 return (DNS_R_BADBITSTRING
);
1583 if (!isdigit(c
& 0xff)) {
1585 return (DNS_R_BADBITSTRING
);
1586 state
= ft_finishbitstring
;
1590 bitlength
+= digitvalue
[(int)c
];
1591 if (bitlength
> maxlength
)
1592 return (DNS_R_BADBITSTRING
);
1596 return (DNS_R_BADBITSTRING
);
1607 FATAL_ERROR(__FILE__
, __LINE__
,
1608 "Unexpected state %d", state
);
1609 /* Does not return. */
1615 return (ISC_R_NOSPACE
);
1617 if (state
!= ft_ordinary
&& state
!= ft_eatdot
&&
1619 return (ISC_R_UNEXPECTEDEND
);
1620 if (state
== ft_ordinary
) {
1624 INSIST(labels
<= 127);
1625 offsets
[labels
] = nused
;
1627 if (origin
!= NULL
) {
1628 if (nrem
< origin
->length
)
1629 return (ISC_R_NOSPACE
);
1630 label
= origin
->ndata
;
1631 n1
= origin
->length
;
1642 c
= maptolower
[(int)c
];
1647 INSIST(n2
== DNS_LABELTYPE_BITSTRING
);
1649 bitlength
= *label
++;
1650 *ndata
++ = bitlength
;
1654 if (bitlength
% 8 != 0)
1659 *ndata
++ = *label
++;
1665 INSIST(labels
<= 127);
1666 offsets
[labels
] = nused
;
1669 if ((origin
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
1670 name
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
1673 name
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
1675 name
->ndata
= (unsigned char *)target
->base
+ target
->used
;
1676 name
->labels
= labels
;
1677 name
->length
= nused
;
1680 compact(name
, offsets
);
1682 isc_buffer_forward(source
, tused
);
1683 isc_buffer_add(target
, name
->length
);
1685 return (ISC_R_SUCCESS
);
1689 dns_name_totext(dns_name_t
*name
, isc_boolean_t omit_final_dot
,
1690 isc_buffer_t
*target
)
1692 unsigned char *ndata
;
1694 unsigned int nlen
, tlen
;
1696 unsigned int trem
, count
;
1697 unsigned int bytes
, nibbles
;
1699 unsigned int labels
;
1700 isc_boolean_t saw_root
= ISC_FALSE
;
1704 * This function assumes the name is in proper uncompressed
1707 REQUIRE(VALID_NAME(name
));
1708 REQUIRE(ISC_BUFFER_VALID(target
));
1710 ndata
= name
->ndata
;
1711 nlen
= name
->length
;
1712 labels
= name
->labels
;
1713 tdata
= isc_buffer_used(target
);
1714 tlen
= isc_buffer_availablelength(target
);
1718 if (labels
== 0 && nlen
== 0) {
1720 * Special handling for an empty name.
1723 return (ISC_R_NOSPACE
);
1726 * The names of these booleans are misleading in this case.
1727 * This empty name is not necessarily from the root node of
1728 * the DNS root zone, nor is a final dot going to be included.
1729 * They need to be set this way, though, to keep the "@"
1730 * from being trounced.
1732 saw_root
= ISC_TRUE
;
1733 omit_final_dot
= ISC_FALSE
;
1738 * Skip the while() loop.
1741 } else if (nlen
== 1 && labels
== 1 && *ndata
== '\0') {
1743 * Special handling for the root label.
1746 return (ISC_R_NOSPACE
);
1748 saw_root
= ISC_TRUE
;
1749 omit_final_dot
= ISC_FALSE
;
1754 * Skip the while() loop.
1759 while (labels
> 0 && nlen
> 0 && trem
> 0) {
1764 saw_root
= ISC_TRUE
;
1768 INSIST(nlen
>= count
);
1772 case 0x22: /* '"' */
1773 case 0x28: /* '(' */
1774 case 0x29: /* ')' */
1775 case 0x2E: /* '.' */
1776 case 0x3B: /* ';' */
1777 case 0x5C: /* '\\' */
1778 /* Special modifiers in zone files. */
1779 case 0x40: /* '@' */
1780 case 0x24: /* '$' */
1782 return (ISC_R_NOSPACE
);
1784 CONVERTFROMASCII(c
);
1791 if (c
> 0x20 && c
< 0x7f) {
1793 return (ISC_R_NOSPACE
);
1794 CONVERTFROMASCII(c
);
1801 return (ISC_R_NOSPACE
);
1802 sprintf(tdata
, "\\%03u",
1812 } else if (count
== DNS_LABELTYPE_BITSTRING
) {
1814 return (ISC_R_NOSPACE
);
1824 len
= sprintf(num
, "%u", count
); /* XXX */
1829 INSIST(nlen
>= bytes
);
1830 nibbles
= count
/ 4;
1834 return (ISC_R_NOSPACE
);
1837 while (nibbles
> 0) {
1839 *tdata
++ = hexdigits
[(c
>> 4)];
1842 *tdata
++ = hexdigits
[c
& 0xf];
1847 return (ISC_R_NOSPACE
);
1849 for (i
= 0; i
< len
; i
++)
1854 FATAL_ERROR(__FILE__
, __LINE__
,
1855 "Unexpected label type %02x", count
);
1860 * The following assumes names are absolute. If not, we
1861 * fix things up later. Note that this means that in some
1862 * cases one more byte of text buffer is required than is
1863 * needed in the final output.
1866 return (ISC_R_NOSPACE
);
1871 if (nlen
!= 0 && trem
== 0)
1872 return (ISC_R_NOSPACE
);
1874 if (!saw_root
|| omit_final_dot
)
1877 isc_buffer_add(target
, tlen
- trem
);
1879 return (ISC_R_SUCCESS
);
1883 dns_name_tofilenametext(dns_name_t
*name
, isc_boolean_t omit_final_dot
,
1884 isc_buffer_t
*target
)
1886 unsigned char *ndata
;
1888 unsigned int nlen
, tlen
;
1890 unsigned int trem
, count
;
1891 unsigned int bytes
, nibbles
;
1893 unsigned int labels
;
1897 * This function assumes the name is in proper uncompressed
1900 REQUIRE(VALID_NAME(name
));
1901 REQUIRE((name
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0);
1902 REQUIRE(ISC_BUFFER_VALID(target
));
1904 ndata
= name
->ndata
;
1905 nlen
= name
->length
;
1906 labels
= name
->labels
;
1907 tdata
= isc_buffer_used(target
);
1908 tlen
= isc_buffer_availablelength(target
);
1912 if (nlen
== 1 && labels
== 1 && *ndata
== '\0') {
1914 * Special handling for the root label.
1917 return (ISC_R_NOSPACE
);
1919 omit_final_dot
= ISC_FALSE
;
1924 * Skip the while() loop.
1929 while (labels
> 0 && nlen
> 0 && trem
> 0) {
1936 INSIST(nlen
>= count
);
1939 if ((c
>= 0x30 && c
<= 0x39) || /* digit */
1940 (c
>= 0x41 && c
<= 0x5A) || /* uppercase */
1941 (c
>= 0x61 && c
<= 0x7A) || /* lowercase */
1942 c
== 0x2D || /* hyphen */
1943 c
== 0x5F) /* underscore */
1946 return (ISC_R_NOSPACE
);
1948 if (c
>= 0x41 && c
<= 0x5A)
1950 CONVERTFROMASCII(c
);
1957 return (ISC_R_NOSPACE
);
1958 sprintf(tdata
, "%%%02X", c
);
1966 } else if (count
== DNS_LABELTYPE_BITSTRING
) {
1968 return (ISC_R_NOSPACE
);
1977 len
= sprintf(num
, "%u", count
); /* XXX */
1982 INSIST(nlen
>= bytes
);
1983 nibbles
= count
/ 4;
1987 return (ISC_R_NOSPACE
);
1990 while (nibbles
> 0) {
1992 *tdata
++ = hexdigits
[(c
>> 4)];
1995 *tdata
++ = hexdigits
[c
& 0xf];
2001 return (ISC_R_NOSPACE
);
2003 for (i
= 0; i
< len
; i
++)
2008 FATAL_ERROR(__FILE__
, __LINE__
,
2009 "Unexpected label type %02x", count
);
2014 * The following assumes names are absolute. If not, we
2015 * fix things up later. Note that this means that in some
2016 * cases one more byte of text buffer is required than is
2017 * needed in the final output.
2020 return (ISC_R_NOSPACE
);
2025 if (nlen
!= 0 && trem
== 0)
2026 return (ISC_R_NOSPACE
);
2031 isc_buffer_add(target
, tlen
- trem
);
2033 return (ISC_R_SUCCESS
);
2037 dns_name_downcase(dns_name_t
*source
, dns_name_t
*name
, isc_buffer_t
*target
) {
2038 unsigned char *sndata
, *ndata
;
2039 unsigned int nlen
, count
, bytes
, labels
;
2040 isc_buffer_t buffer
;
2043 * Downcase 'source'.
2046 REQUIRE(VALID_NAME(source
));
2047 REQUIRE(VALID_NAME(name
));
2048 if (source
== name
) {
2049 REQUIRE((name
->attributes
& DNS_NAMEATTR_READONLY
) == 0);
2050 isc_buffer_init(&buffer
, source
->ndata
, source
->length
);
2052 ndata
= source
->ndata
;
2054 REQUIRE(BINDABLE(name
));
2055 REQUIRE((target
!= NULL
&& ISC_BUFFER_VALID(target
)) ||
2056 (target
== NULL
&& ISC_BUFFER_VALID(name
->buffer
)));
2057 if (target
== NULL
) {
2058 target
= name
->buffer
;
2059 isc_buffer_clear(name
->buffer
);
2061 ndata
= (unsigned char *)target
->base
+ target
->used
;
2062 name
->ndata
= ndata
;
2065 sndata
= source
->ndata
;
2066 nlen
= source
->length
;
2067 labels
= source
->labels
;
2069 if (nlen
> (target
->length
- target
->used
)) {
2071 return (ISC_R_NOSPACE
);
2074 while (labels
> 0 && nlen
> 0) {
2080 INSIST(nlen
>= count
);
2082 *ndata
++ = maptolower
[(*sndata
++)];
2086 } else if (count
== DNS_LABELTYPE_BITSTRING
) {
2098 INSIST(nlen
>= bytes
);
2101 *ndata
++ = *sndata
++;
2105 FATAL_ERROR(__FILE__
, __LINE__
,
2106 "Unexpected label type %02x", count
);
2107 /* Does not return. */
2111 if (source
!= name
) {
2112 name
->labels
= source
->labels
;
2113 name
->length
= source
->length
;
2114 if ((source
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
2115 name
->attributes
= DNS_NAMEATTR_ABSOLUTE
;
2117 name
->attributes
= 0;
2118 if (name
->labels
> 0 && name
->offsets
!= NULL
)
2119 set_offsets(name
, name
->offsets
, NULL
);
2122 isc_buffer_add(target
, name
->length
);
2124 return (ISC_R_SUCCESS
);
2128 set_offsets(const dns_name_t
*name
, unsigned char *offsets
,
2129 dns_name_t
*set_name
)
2131 unsigned int offset
, count
, length
, nlabels
, n
;
2132 unsigned char *ndata
;
2133 isc_boolean_t absolute
;
2135 ndata
= name
->ndata
;
2136 length
= name
->length
;
2139 absolute
= ISC_FALSE
;
2140 while (offset
!= length
) {
2141 INSIST(nlabels
< 128);
2142 offsets
[nlabels
++] = offset
;
2148 INSIST(offset
<= length
);
2150 absolute
= ISC_TRUE
;
2154 INSIST(count
== DNS_LABELTYPE_BITSTRING
);
2164 INSIST(offset
<= length
);
2167 if (set_name
!= NULL
) {
2168 INSIST(set_name
== name
);
2170 set_name
->labels
= nlabels
;
2171 set_name
->length
= offset
;
2173 set_name
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
2175 set_name
->attributes
&= ~DNS_NAMEATTR_ABSOLUTE
;
2177 INSIST(nlabels
== name
->labels
);
2178 INSIST(offset
== name
->length
);
2182 compact(dns_name_t
*name
, unsigned char *offsets
) {
2183 unsigned char *head
, *curr
, *last
;
2184 unsigned int count
, n
, bit
;
2185 unsigned int headbits
, currbits
, tailbits
, newbits
;
2186 unsigned int headrem
, newrem
;
2187 unsigned int headindex
, currindex
, tailindex
, newindex
;
2188 unsigned char tail
[32];
2191 * The caller MUST ensure that all bitstrings are correctly formatted
2192 * and that the offsets table is valid.
2196 memset(tail
, 0, sizeof tail
);
2197 INSIST(name
->labels
!= 0);
2198 n
= name
->labels
- 1;
2201 head
= &name
->ndata
[offsets
[n
]];
2202 if (head
[0] == DNS_LABELTYPE_BITSTRING
&& head
[1] != 0) {
2205 curr
= &name
->ndata
[offsets
[n
]];
2206 if (curr
[0] != DNS_LABELTYPE_BITSTRING
)
2209 * We have consecutive bitstrings labels, and
2210 * the more significant label ('head') has
2220 headindex
= headbits
;
2221 count
= 256 - headbits
;
2222 if (count
> currbits
)
2224 headrem
= headbits
% 8;
2226 headrem
= 8 - headrem
;
2228 if (headrem
> count
)
2231 bit
= get_bit(&curr
[2],
2233 set_bit(&head
[2], headindex
,
2240 } while (headrem
!= 0);
2245 bit
= get_bit(&curr
[2], currindex
);
2246 set_bit(tail
, tailindex
, bit
);
2254 if (currindex
< currbits
) {
2255 while (currindex
< currbits
) {
2256 bit
= get_bit(&curr
[2],
2258 set_bit(&curr
[2], newindex
,
2264 INSIST(newbits
< 256);
2266 count
= newbits
/ 8;
2267 newrem
= newbits
% 8;
2268 /* Zero remaining pad bits, if any. */
2271 newrem
= 8 - newrem
;
2272 while (newrem
> 0) {
2282 /* We got rid of curr. */
2285 /* copy head, then tail, then rest to curr. */
2286 count
= headbits
+ tailbits
;
2287 INSIST(count
<= 256);
2288 curr
[0] = DNS_LABELTYPE_BITSTRING
;
2295 count
= headbits
/ 8;
2296 if (headbits
% 8 != 0)
2302 count
= tailbits
/ 8;
2303 if (tailbits
% 8 != 0)
2310 last
= name
->ndata
+ name
->length
;
2311 while (head
!= last
)
2313 name
->length
= (curr
- name
->ndata
);
2315 * The offsets table may now be invalid.
2317 set_offsets(name
, offsets
, NULL
);
2326 dns_name_fromwire(dns_name_t
*name
, isc_buffer_t
*source
,
2327 dns_decompress_t
*dctx
, isc_boolean_t downcase
,
2328 isc_buffer_t
*target
)
2330 unsigned char *cdata
, *ndata
;
2331 unsigned int cused
; /* Bytes of compressed name data used */
2332 unsigned int hops
, nused
, labels
, n
, nmax
;
2333 unsigned int current
, new_current
, biggest_pointer
;
2334 isc_boolean_t saw_bitstring
, done
;
2335 fw_state state
= fw_start
;
2337 unsigned char *offsets
;
2338 dns_offsets_t odata
;
2341 * Copy the possibly-compressed name at source into target,
2345 REQUIRE(VALID_NAME(name
));
2346 REQUIRE((target
!= NULL
&& ISC_BUFFER_VALID(target
)) ||
2347 (target
== NULL
&& ISC_BUFFER_VALID(name
->buffer
)));
2349 if (target
== NULL
&& name
->buffer
!= NULL
) {
2350 target
= name
->buffer
;
2351 isc_buffer_clear(target
);
2354 REQUIRE(dctx
!= NULL
);
2355 REQUIRE(BINDABLE(name
));
2357 INIT_OFFSETS(name
, offsets
, odata
);
2360 * Make 'name' empty in case of failure.
2365 * Initialize things to make the compiler happy; they're not required.
2375 saw_bitstring
= ISC_FALSE
;
2378 ndata
= isc_buffer_used(target
);
2382 * Find the maximum number of uncompressed target name
2383 * bytes we are willing to generate. This is the smaller
2384 * of the available target buffer length and the
2385 * maximum legal domain name length (255).
2387 nmax
= isc_buffer_availablelength(target
);
2388 if (nmax
> DNS_NAME_MAXWIRE
)
2389 nmax
= DNS_NAME_MAXWIRE
;
2391 cdata
= isc_buffer_current(source
);
2394 current
= source
->current
;
2395 biggest_pointer
= current
;
2398 * Note: The following code is not optimized for speed, but
2399 * rather for correctness. Speed will be addressed in the future.
2402 while (current
< source
->active
&& !done
) {
2411 offsets
[labels
] = nused
;
2413 if (nused
+ c
+ 1 > nmax
)
2420 state
= fw_ordinary
;
2421 } else if (c
>= 128 && c
< 192) {
2423 * 14 bit local compression pointer.
2424 * Local compression is no longer an
2427 return (DNS_R_BADLABELTYPE
);
2428 } else if (c
>= 192) {
2430 * Ordinary 14-bit pointer.
2432 if ((dctx
->allowed
& DNS_COMPRESS_GLOBAL14
) ==
2434 return (DNS_R_DISALLOWED
);
2435 new_current
= c
& 0x3F;
2437 state
= fw_newcurrent
;
2438 } else if (c
== DNS_LABELTYPE_BITSTRING
) {
2439 offsets
[labels
] = nused
;
2445 saw_bitstring
= ISC_TRUE
;
2446 state
= fw_bitstring
;
2448 return (DNS_R_BADLABELTYPE
);
2467 if (nused
+ n
+ 1 > nmax
)
2479 if (new_current
>= biggest_pointer
)
2480 return (DNS_R_BADPOINTER
);
2481 biggest_pointer
= new_current
;
2482 current
= new_current
;
2483 cdata
= (unsigned char *)source
->base
+
2486 if (hops
> DNS_POINTER_MAXHOPS
)
2487 return (DNS_R_TOOMANYHOPS
);
2491 FATAL_ERROR(__FILE__
, __LINE__
,
2492 "Unknown state %d", state
);
2493 /* Does not return. */
2498 return (ISC_R_UNEXPECTEDEND
);
2500 name
->ndata
= (unsigned char *)target
->base
+ target
->used
;
2501 name
->labels
= labels
;
2502 name
->length
= nused
;
2503 name
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
2506 compact(name
, offsets
);
2508 isc_buffer_forward(source
, cused
);
2509 isc_buffer_add(target
, name
->length
);
2511 return (ISC_R_SUCCESS
);
2514 if (nmax
== DNS_NAME_MAXWIRE
)
2516 * The name did not fit even though we had a buffer
2517 * big enough to fit a maximum-length name.
2519 return (DNS_R_NAMETOOLONG
);
2522 * The name might fit if only the caller could give us a
2523 * big enough buffer.
2525 return (ISC_R_NOSPACE
);
2530 dns_name_towire(dns_name_t
*name
, dns_compress_t
*cctx
, isc_buffer_t
*target
) {
2531 unsigned int methods
;
2532 isc_uint16_t offset
;
2533 dns_name_t gp
; /* Global compression prefix */
2534 isc_boolean_t gf
; /* Global compression target found */
2535 isc_uint16_t go
; /* Global compression offset */
2540 * Convert 'name' into wire format, compressing it as specified by the
2541 * compression context 'cctx', and storing the result in 'target'.
2544 REQUIRE(VALID_NAME(name
));
2545 REQUIRE(cctx
!= NULL
);
2546 REQUIRE(ISC_BUFFER_VALID(target
));
2549 * If 'name' doesn't have an offsets table, make a clone which
2552 if (name
->offsets
== NULL
) {
2553 DNS_NAME_INIT(&clname
, clo
);
2554 dns_name_clone(name
, &clname
);
2557 DNS_NAME_INIT(&gp
, NULL
);
2559 offset
= target
->used
; /*XXX*/
2561 methods
= dns_compress_getmethods(cctx
);
2563 if ((methods
& DNS_COMPRESS_GLOBAL14
) != 0)
2564 gf
= dns_compress_findglobal(cctx
, name
, &gp
, &go
);
2569 * If the offset is too high for 14 bit global compression, we're
2572 if (gf
&& go
>= 0x4000)
2576 * Will the compression pointer reduce the message size?
2578 if (gf
&& (gp
.length
+ 2) >= name
->length
)
2582 if (target
->length
- target
->used
< gp
.length
)
2583 return (ISC_R_NOSPACE
);
2584 (void)memcpy((unsigned char *)target
->base
+ target
->used
,
2585 gp
.ndata
, (size_t)gp
.length
);
2586 isc_buffer_add(target
, gp
.length
);
2588 if (target
->length
- target
->used
< 2)
2589 return (ISC_R_NOSPACE
);
2590 isc_buffer_putuint16(target
, go
);
2592 dns_compress_add(cctx
, name
, &gp
, offset
);
2594 if (target
->length
- target
->used
< name
->length
)
2595 return (ISC_R_NOSPACE
);
2596 (void)memcpy((unsigned char *)target
->base
+ target
->used
,
2597 name
->ndata
, (size_t)name
->length
);
2598 isc_buffer_add(target
, name
->length
);
2599 dns_compress_add(cctx
, name
, name
, offset
);
2601 return (ISC_R_SUCCESS
);
2605 dns_name_concatenate(dns_name_t
*prefix
, dns_name_t
*suffix
, dns_name_t
*name
,
2606 isc_buffer_t
*target
)
2608 unsigned char *ndata
, *offsets
;
2609 unsigned int nrem
, labels
, prefix_length
, length
, offset
;
2610 isc_boolean_t copy_prefix
= ISC_TRUE
;
2611 isc_boolean_t copy_suffix
= ISC_TRUE
;
2612 isc_boolean_t saw_bitstring
= ISC_FALSE
;
2613 isc_boolean_t absolute
= ISC_FALSE
;
2614 dns_name_t tmp_name
;
2615 dns_offsets_t odata
;
2618 * Concatenate 'prefix' and 'suffix'.
2621 REQUIRE(prefix
== NULL
|| VALID_NAME(prefix
));
2622 REQUIRE(suffix
== NULL
|| VALID_NAME(suffix
));
2623 REQUIRE(name
== NULL
|| VALID_NAME(name
));
2624 REQUIRE((target
!= NULL
&& ISC_BUFFER_VALID(target
)) ||
2625 (target
== NULL
&& name
!= NULL
&& ISC_BUFFER_VALID(name
->buffer
)));
2626 if (prefix
== NULL
|| prefix
->labels
== 0)
2627 copy_prefix
= ISC_FALSE
;
2628 if (suffix
== NULL
|| suffix
->labels
== 0)
2629 copy_suffix
= ISC_FALSE
;
2631 (prefix
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0) {
2632 absolute
= ISC_TRUE
;
2633 REQUIRE(!copy_suffix
);
2636 DNS_NAME_INIT(&tmp_name
, odata
);
2639 if (target
== NULL
) {
2640 INSIST(name
->buffer
!= NULL
);
2641 target
= name
->buffer
;
2642 isc_buffer_clear(name
->buffer
);
2645 REQUIRE(BINDABLE(name
));
2648 * XXX IMPORTANT NOTE
2650 * If the most-signficant label in prefix is a bitstring,
2651 * and the least-signficant label in suffix is a bitstring,
2652 * it's possible that compaction could convert them into
2653 * one label. If this happens, then the final size will
2654 * be three bytes less than nrem.
2656 * We do not check for this special case, and handling it is
2657 * a little messy; we can't just concatenate and compact,
2658 * because we may only have 255 bytes but might need 258 bytes
2659 * temporarily. There are ways to do this with only 255 bytes,
2660 * which will be implemented later.
2662 * For now, we simply reject these few cases as being too
2669 nrem
= target
->length
- target
->used
;
2670 ndata
= (unsigned char *)target
->base
+ target
->used
;
2671 if (nrem
> DNS_NAME_MAXWIRE
)
2672 nrem
= DNS_NAME_MAXWIRE
;
2677 prefix_length
= prefix
->length
;
2678 length
+= prefix_length
;
2679 labels
+= prefix
->labels
;
2682 length
+= suffix
->length
;
2683 labels
+= suffix
->labels
;
2685 if (length
> DNS_NAME_MAXWIRE
) {
2687 return (DNS_R_NAMETOOLONG
);
2689 if (length
> nrem
) {
2691 return (ISC_R_NOSPACE
);
2695 if ((suffix
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
2696 absolute
= ISC_TRUE
;
2698 suffix
->ndata
[0] == DNS_LABELTYPE_BITSTRING
) {
2700 * We only need to call compact() if both the
2701 * least-significant label of the suffix and the
2702 * most-significant label of the prefix are both
2705 * A further possible optimization, which we don't do,
2706 * is to not compact() if the suffix bitstring is
2707 * full. It will usually not be full, so I don't
2708 * think this is worth it.
2710 if (prefix
->offsets
!= NULL
) {
2711 offset
= prefix
->offsets
[prefix
->labels
- 1];
2712 if (prefix
->ndata
[offset
] ==
2713 DNS_LABELTYPE_BITSTRING
)
2714 saw_bitstring
= ISC_TRUE
;
2717 * We don't have an offsets table for prefix,
2718 * and rather than spend the effort to make it
2719 * we'll just compact(), which doesn't cost
2720 * more than computing the offsets table if
2721 * there is no bitstring in prefix.
2723 saw_bitstring
= ISC_TRUE
;
2726 if (suffix
== name
&& suffix
->buffer
== target
)
2727 memmove(ndata
+ prefix_length
, suffix
->ndata
,
2730 memcpy(ndata
+ prefix_length
, suffix
->ndata
,
2735 * If 'prefix' and 'name' are the same object, and the object has
2736 * a dedicated buffer, and we're using it, then we don't have to
2739 if (copy_prefix
&& (prefix
!= name
|| prefix
->buffer
!= target
))
2740 memcpy(ndata
, prefix
->ndata
, prefix_length
);
2742 name
->ndata
= ndata
;
2743 name
->labels
= labels
;
2744 name
->length
= length
;
2746 name
->attributes
= DNS_NAMEATTR_ABSOLUTE
;
2748 name
->attributes
= 0;
2750 if (name
->labels
> 0 && (name
->offsets
!= NULL
|| saw_bitstring
)) {
2751 INIT_OFFSETS(name
, offsets
, odata
);
2752 set_offsets(name
, offsets
, NULL
);
2754 compact(name
, offsets
);
2757 isc_buffer_add(target
, name
->length
);
2759 return (ISC_R_SUCCESS
);
2763 dns_name_split(dns_name_t
*name
,
2764 unsigned int suffixlabels
, unsigned int nbits
,
2765 dns_name_t
*prefix
, dns_name_t
*suffix
)
2768 dns_offsets_t name_odata
, prefix_odata
, suffix_odata
;
2769 unsigned char *offsets
, *prefix_offsets
= NULL
, *suffix_offsets
;
2770 isc_result_t result
= ISC_R_SUCCESS
;
2771 unsigned int splitlabel
, bitbytes
, mod
, len
;
2772 unsigned char *p
, *src
, *dst
;
2773 isc_boolean_t maybe_compact_prefix
= ISC_FALSE
;
2775 REQUIRE(VALID_NAME(name
));
2776 REQUIRE(suffixlabels
> 0);
2777 REQUIRE((nbits
== 0 && suffixlabels
< name
->labels
) ||
2778 (nbits
!= 0 && suffixlabels
<= name
->labels
));
2779 REQUIRE(prefix
!= NULL
|| suffix
!= NULL
);
2780 REQUIRE(prefix
== NULL
||
2781 (VALID_NAME(prefix
) &&
2782 prefix
->buffer
!= NULL
&&
2784 REQUIRE(suffix
== NULL
||
2785 (VALID_NAME(suffix
) &&
2786 suffix
->buffer
!= NULL
&&
2790 * When splitting bitstring labels, if prefix and suffix have the same
2791 * buffer, suffix will overwrite the ndata of prefix, corrupting it.
2792 * If prefix has the ndata of name, then it modifies the bitstring
2793 * label and suffix doesn't have the original available. This latter
2794 * problem could be worked around if it is ever deemed desirable.
2796 REQUIRE(nbits
== 0 || prefix
== NULL
|| suffix
== NULL
||
2797 (prefix
->buffer
->base
!= suffix
->buffer
->base
&&
2798 prefix
->buffer
->base
!= name
->ndata
));
2800 SETUP_OFFSETS(name
, offsets
, name_odata
);
2802 splitlabel
= name
->labels
- suffixlabels
;
2805 * Make p point at the count byte of the bitstring label,
2806 * if there is one (p will not be used if we are not
2809 p
= &name
->ndata
[offsets
[splitlabel
] + 1];
2812 * When a bit count is specified, ensure that the label is a bitstring
2813 * label and it has more bits than the requested slice.
2815 REQUIRE(nbits
== 0 ||
2816 (*(p
- 1) == DNS_LABELTYPE_BITSTRING
&& nbits
< 256 &&
2817 (*p
== 0 || *p
> nbits
)));
2821 if (prefix
!= NULL
) {
2823 isc_buffer_clear(prefix
->buffer
);
2826 * '2' is for the DNS_LABELTYPE_BITSTRING id
2827 * plus the existing number of bits byte.
2829 len
= offsets
[splitlabel
] + 2;
2831 dst
= prefix
->buffer
->base
;
2835 * If these are overlapping names ...
2836 * wow. How bizarre could that be?
2838 INSIST(! (src
<= dst
&& src
+ len
> dst
) ||
2839 (dst
<= src
&& dst
+ len
> src
));
2841 memcpy(dst
, src
, len
);
2847 * Set the new bit count. Also, when a bitstring
2848 * label being split is maximal length, compaction
2849 * might be necessary on the prefix.
2852 maybe_compact_prefix
= ISC_TRUE
;
2858 * Calculate the number of bytes necessary to hold
2859 * all of the bits left in the prefix.
2861 bitbytes
= (*p
- 1) / 8 + 1;
2863 prefix
->length
= len
+ bitbytes
;
2865 if (prefix
->length
> prefix
->buffer
->length
) {
2866 dns_name_invalidate(prefix
);
2867 return (ISC_R_NOSPACE
);
2871 * All of the bits now need to be shifted to the left
2872 * to fill in the space taken by the removed bits.
2873 * This is wonderfully easy when the number of removed
2874 * bits is an integral multiple of 8, but of course
2875 * life isn't always that easy.
2877 src
+= len
+ nbits
/ 8;
2882 memmove(dst
, src
, len
);
2885 * p is adjusted to point to the last byte of
2886 * the starting bitstring label to make it
2887 * cheap to determine when bits from the next
2888 * byte should be shifted into the low order
2889 * bits of the current byte.
2891 p
= src
+ (mod
+ *p
- 1) / 8;
2894 *dst
= *src
++ << mod
;
2896 * The 0xff subexpression guards
2897 * against arithmetic sign extension
2898 * by the right shift.
2902 (*src
>> (8 - mod
)) &
2907 * Et voila, the very last byte has
2908 * automatically already had its padding
2909 * fixed by the left shift.
2913 prefix
->buffer
->used
= prefix
->length
;
2914 prefix
->ndata
= prefix
->buffer
->base
;
2917 * Yes, = is meant here, not ==. The intent is
2918 * to have it set only when INSISTs are turned on,
2919 * to doublecheck the result of set_offsets.
2921 INSIST(len
= prefix
->length
);
2923 INIT_OFFSETS(prefix
, prefix_offsets
, prefix_odata
);
2924 set_offsets(prefix
, prefix_offsets
, prefix
);
2926 INSIST(prefix
->labels
== splitlabel
+ 1 &&
2927 prefix
->length
== len
);
2930 dns_name_getlabelsequence(name
, 0, splitlabel
,
2935 if (suffix
!= NULL
&& result
== ISC_R_SUCCESS
) {
2937 bitbytes
= (nbits
- 1) / 8 + 1;
2939 isc_buffer_clear(suffix
->buffer
);
2942 * The existing bitcount is in src.
2943 * Set len to the number of bytes to be removed,
2944 * and the suffix length to the number of bytes in
2947 src
= &name
->ndata
[offsets
[splitlabel
] + 1];
2948 len
= ((*src
== 0 ? 256 : *src
) - 1) / 8;
2949 len
-= (bitbytes
- 1);
2952 suffix
->length
= name
->length
-
2953 offsets
[splitlabel
] - len
;
2955 INSIST(suffix
->length
> 0);
2956 if (suffix
->length
> suffix
->buffer
->length
) {
2957 dns_name_invalidate(suffix
);
2958 return (ISC_R_NOSPACE
);
2962 * First set up the bitstring label.
2964 dst
= suffix
->buffer
->base
;
2965 *dst
++ = DNS_LABELTYPE_BITSTRING
;
2970 * Remember where the next label starts.
2972 p
= src
+ bitbytes
+ len
;
2975 * Some bytes are being removed from the
2976 * middle of the name because of the truncation
2977 * of bits in the bitstring label. Copy
2978 * the bytes (whether full with 8 bits or not)
2979 * that are being kept.
2981 for (len
= bitbytes
; len
> 0; len
--)
2985 * Now just copy the rest of the labels of
2986 * the name by adjusting src to point to
2989 * 2 == label type byte + bitcount byte.
2991 len
= suffix
->length
- bitbytes
- 2;
2994 len
= suffix
->length
- 2;
2997 memmove(dst
, src
, len
);
2999 suffix
->buffer
->used
= suffix
->length
;
3000 suffix
->ndata
= suffix
->buffer
->base
;
3003 * The byte that contains the end of the
3004 * bitstring has its pad bits (if any) masked
3008 suffix
->ndata
[bitbytes
+ 1] &=
3012 * Yes, = is meant here, not ==. The intent is
3013 * to have it set only when INSISTs are turned on,
3014 * to doublecheck the result of set_offsets.
3016 INSIST(len
= suffix
->length
);
3018 INIT_OFFSETS(suffix
, suffix_offsets
, suffix_odata
);
3019 set_offsets(suffix
, suffix_offsets
, suffix
);
3021 INSIST(suffix
->labels
== suffixlabels
&&
3022 suffix
->length
== len
);
3025 dns_name_getlabelsequence(name
, splitlabel
,
3026 suffixlabels
, suffix
);
3031 * Compacting the prefix can't be done until after the suffix is
3032 * set, because it would screw up the offsets table of 'name'
3033 * when 'name' == 'prefix'.
3035 if (maybe_compact_prefix
&& splitlabel
> 0 &&
3036 prefix
->ndata
[prefix_offsets
[splitlabel
- 1]] ==
3037 DNS_LABELTYPE_BITSTRING
)
3038 compact(prefix
, prefix_offsets
);
3044 dns_name_splitatdepth(dns_name_t
*name
, unsigned int depth
,
3045 dns_name_t
*prefix
, dns_name_t
*suffix
)
3047 unsigned int suffixlabels
, nbits
, label
, count
, n
;
3048 unsigned char *offsets
, *ndata
;
3049 dns_offsets_t odata
;
3052 * Split 'name' into two pieces at a certain depth.
3055 REQUIRE(VALID_NAME(name
));
3056 REQUIRE(name
->labels
> 0);
3059 SETUP_OFFSETS(name
, offsets
, odata
);
3063 label
= name
->labels
;
3066 ndata
= &name
->ndata
[offsets
[label
]];
3069 INSIST(count
== DNS_LABELTYPE_BITSTRING
);
3071 * Get the number of bits in the bitstring label.
3079 * This entire bitstring is in the suffix.
3084 * Only the first 'depth' bits of this
3085 * bitstring are in the suffix.
3094 } while (depth
!= 0 && label
!= 0);
3097 * If depth is not zero, then the caller violated the requirement
3098 * that depth <= dns_name_depth(name).
3101 REQUIRE(depth
<= dns_name_depth(name
));
3103 * We should never get here!
3108 return (dns_name_split(name
, suffixlabels
, nbits
, prefix
, suffix
));
3112 dns_name_dup(dns_name_t
*source
, isc_mem_t
*mctx
, dns_name_t
*target
) {
3114 * Make 'target' a dynamically allocated copy of 'source'.
3117 REQUIRE(VALID_NAME(source
));
3118 REQUIRE(source
->length
> 0);
3119 REQUIRE(VALID_NAME(target
));
3120 REQUIRE(BINDABLE(target
));
3123 * Make 'target' empty in case of failure.
3127 target
->ndata
= isc_mem_get(mctx
, source
->length
);
3128 if (target
->ndata
== NULL
)
3129 return (ISC_R_NOMEMORY
);
3131 memcpy(target
->ndata
, source
->ndata
, source
->length
);
3133 target
->length
= source
->length
;
3134 target
->labels
= source
->labels
;
3135 target
->attributes
= DNS_NAMEATTR_DYNAMIC
;
3136 if ((source
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
3137 target
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
3138 if (target
->offsets
!= NULL
) {
3139 if (source
->offsets
!= NULL
)
3140 memcpy(target
->offsets
, source
->offsets
,
3143 set_offsets(target
, target
->offsets
, NULL
);
3146 return (ISC_R_SUCCESS
);
3150 dns_name_dupwithoffsets(dns_name_t
*source
, isc_mem_t
*mctx
,
3154 * Make 'target' a read-only dynamically allocated copy of 'source'.
3155 * 'target' will also have a dynamically allocated offsets table.
3158 REQUIRE(VALID_NAME(source
));
3159 REQUIRE(source
->length
> 0);
3160 REQUIRE(VALID_NAME(target
));
3161 REQUIRE(BINDABLE(target
));
3162 REQUIRE(target
->offsets
== NULL
);
3165 * Make 'target' empty in case of failure.
3169 target
->ndata
= isc_mem_get(mctx
, source
->length
+ source
->labels
);
3170 if (target
->ndata
== NULL
)
3171 return (ISC_R_NOMEMORY
);
3173 memcpy(target
->ndata
, source
->ndata
, source
->length
);
3175 target
->length
= source
->length
;
3176 target
->labels
= source
->labels
;
3177 target
->attributes
= DNS_NAMEATTR_DYNAMIC
| DNS_NAMEATTR_DYNOFFSETS
|
3178 DNS_NAMEATTR_READONLY
;
3179 if ((source
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
3180 target
->attributes
|= DNS_NAMEATTR_ABSOLUTE
;
3181 target
->offsets
= target
->ndata
+ source
->length
;
3182 if (source
->offsets
!= NULL
)
3183 memcpy(target
->offsets
, source
->offsets
, source
->labels
);
3185 set_offsets(target
, target
->offsets
, NULL
);
3187 return (ISC_R_SUCCESS
);
3191 dns_name_free(dns_name_t
*name
, isc_mem_t
*mctx
) {
3198 REQUIRE(VALID_NAME(name
));
3199 REQUIRE((name
->attributes
& DNS_NAMEATTR_DYNAMIC
) != 0);
3201 size
= name
->length
;
3202 if ((name
->attributes
& DNS_NAMEATTR_DYNOFFSETS
) != 0)
3203 size
+= name
->labels
;
3204 isc_mem_put(mctx
, name
->ndata
, size
);
3205 dns_name_invalidate(name
);
3209 dns_name_digest(dns_name_t
*name
, dns_digestfunc_t digest
, void *arg
) {
3210 dns_name_t downname
;
3211 unsigned char data
[256];
3212 isc_buffer_t buffer
;
3213 isc_result_t result
;
3217 * Send 'name' in DNSSEC canonical form to 'digest'.
3220 REQUIRE(VALID_NAME(name
));
3221 REQUIRE(digest
!= NULL
);
3223 DNS_NAME_INIT(&downname
, NULL
);
3224 isc_buffer_init(&buffer
, data
, sizeof(data
));
3226 result
= dns_name_downcase(name
, &downname
, &buffer
);
3227 if (result
!= ISC_R_SUCCESS
)
3230 isc_buffer_usedregion(&buffer
, &r
);
3232 return ((digest
)(arg
, &r
));
3236 dns_name_dynamic(dns_name_t
*name
) {
3237 REQUIRE(VALID_NAME(name
));
3240 * Returns whether there is dynamic memory associated with this name.
3243 return ((name
->attributes
& DNS_NAMEATTR_DYNAMIC
) != 0 ?
3244 ISC_TRUE
: ISC_FALSE
);
3248 dns_name_print(dns_name_t
*name
, FILE *stream
) {
3249 isc_result_t result
;
3255 * Print 'name' on 'stream'.
3258 REQUIRE(VALID_NAME(name
));
3260 isc_buffer_init(&b
, t
, sizeof(t
));
3261 result
= dns_name_totext(name
, ISC_FALSE
, &b
);
3262 if (result
!= ISC_R_SUCCESS
)
3264 isc_buffer_usedregion(&b
, &r
);
3265 fprintf(stream
, "%.*s", (int)r
.length
, (char *)r
.base
);
3267 return (ISC_R_SUCCESS
);
3271 dns_name_format(dns_name_t
*name
, char *cp
, unsigned int size
) {
3272 isc_result_t result
;
3278 * Leave room for null termination after buffer.
3280 isc_buffer_init(&buf
, cp
, size
- 1);
3281 result
= dns_name_totext(name
, ISC_TRUE
, &buf
);
3282 if (result
== ISC_R_SUCCESS
) {
3287 isc_buffer_usedregion(&buf
, &r
);
3288 ((char *) r
.base
)[r
.length
] = '\0';
3291 snprintf(cp
, size
, "<unknown>");
3295 dns_name_copy(dns_name_t
*source
, dns_name_t
*dest
, isc_buffer_t
*target
) {
3296 unsigned char *ndata
, *offsets
;
3297 dns_offsets_t odata
;
3300 * Make dest a copy of source.
3303 REQUIRE(VALID_NAME(source
));
3304 REQUIRE(VALID_NAME(dest
));
3305 REQUIRE(target
!= NULL
|| dest
->buffer
!= NULL
);
3307 if (target
== NULL
) {
3308 target
= dest
->buffer
;
3309 isc_buffer_clear(dest
->buffer
);
3312 REQUIRE(BINDABLE(dest
));
3317 if (target
->length
- target
->used
< source
->length
)
3318 return (ISC_R_NOSPACE
);
3320 ndata
= (unsigned char *)target
->base
+ target
->used
;
3321 dest
->ndata
= target
->base
;
3323 memcpy(ndata
, source
->ndata
, source
->length
);
3325 dest
->ndata
= ndata
;
3326 dest
->labels
= source
->labels
;
3327 dest
->length
= source
->length
;
3328 if ((source
->attributes
& DNS_NAMEATTR_ABSOLUTE
) != 0)
3329 dest
->attributes
= DNS_NAMEATTR_ABSOLUTE
;
3331 dest
->attributes
= 0;
3333 if (dest
->labels
> 0 && dest
->offsets
!= NULL
) {
3334 INIT_OFFSETS(dest
, offsets
, odata
);
3335 set_offsets(dest
, offsets
, NULL
);
3338 isc_buffer_add(target
, dest
->length
);
3340 return (ISC_R_SUCCESS
);