4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static const char rcsid
[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp";
24 #include "port_before.h"
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
38 #include "port_after.h"
41 # define SPRINTF(x) strlen(sprintf/**/x)
43 # define SPRINTF(x) ((size_t)sprintf x)
46 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
47 #define DNS_LABELTYPE_BITSTRING 0x41
51 static const char digits
[] = "0123456789";
53 static const char digitvalue
[256] = {
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
57 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
58 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
60 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
74 static int special(int);
75 static int printable(int);
76 static int dn_find(const u_char
*, const u_char
*,
77 const u_char
* const *,
78 const u_char
* const *);
79 static int encode_bitsring(const char **, const char *,
80 unsigned char **, unsigned char **,
81 unsigned const char *);
82 static int labellen(const u_char
*);
83 static int decode_bitstring(const unsigned char **,
84 char *, const char *);
89 * Convert an encoded domain name to printable ascii as per RFC1035.
92 *\li Number of bytes written to buffer, or -1 (with errno set)
95 *\li The root is returned as "."
96 *\li All other domains are returned in non absolute form
99 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
111 while ((n
= *cp
++) != 0) {
112 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
113 /* Some kind of compression pointer. */
124 if ((l
= labellen(cp
- 1)) < 0) {
125 errno
= EMSGSIZE
; /*%< XXX */
132 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
135 if (n
!= DNS_LABELTYPE_BITSTRING
) {
136 /* XXX: labellen should reject this case */
140 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
148 for ((void)NULL
; l
> 0; l
--) {
157 } else if (!printable(c
)) {
163 *dn
++ = digits
[c
/ 100];
164 *dn
++ = digits
[(c
% 100) / 10];
165 *dn
++ = digits
[c
% 10];
191 * Convert a ascii string into an encoded domain name as per RFC1035.
196 *\li 1 if string was fully qualified
197 *\li 0 is string was not fully qualified
200 *\li Enforces label and domain length limits.
203 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
204 return (ns_name_pton2(src
, dst
, dstsiz
, NULL
));
208 * ns_name_pton2(src, dst, dstsiz, *dstlen)
209 * Convert a ascii string into an encoded domain name as per RFC1035.
212 * 1 if string was fully qualified
213 * 0 is string was not fully qualified
215 * fills in *dstlen (if non-NULL)
217 * Enforces label and domain length limits.
220 ns_name_pton2(const char *src
, u_char
*dst
, size_t dstsiz
, size_t *dstlen
) {
221 u_char
*label
, *bp
, *eom
;
222 int c
, n
, escaped
, e
= 0;
230 while ((c
= *src
++) != 0) {
232 if (c
== '[') { /*%< start a bit string label */
233 if ((cp
= strchr(src
, ']')) == NULL
) {
234 errno
= EINVAL
; /*%< ??? */
237 if ((e
= encode_bitsring(&src
, cp
+ 2,
245 if ((c
= *src
++) == 0)
253 else if ((cp
= strchr(digits
, c
)) != NULL
) {
254 n
= (cp
- digits
) * 100;
255 if ((c
= *src
++) == 0 ||
256 (cp
= strchr(digits
, c
)) == NULL
) {
260 n
+= (cp
- digits
) * 10;
261 if ((c
= *src
++) == 0 ||
262 (cp
= strchr(digits
, c
)) == NULL
) {
274 } else if (c
== '\\') {
277 } else if (c
== '.') {
278 c
= (bp
- label
- 1);
279 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
288 /* Fully qualified ? */
297 if ((bp
- dst
) > MAXCDNAME
) {
302 *dstlen
= (bp
- dst
);
305 if (c
== 0 || *src
== '.') {
318 c
= (bp
- label
- 1);
319 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
336 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
341 *dstlen
= (bp
- dst
);
346 * Convert a network strings labels into all lowercase.
349 *\li Number of bytes written to buffer, or -1 (with errno set)
352 *\li Enforces label and domain length limits.
356 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
372 while ((n
= *cp
++) != 0) {
373 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
374 /* Some kind of compression pointer. */
379 if ((l
= labellen(cp
- 1)) < 0) {
387 for ((void)NULL
; l
> 0; l
--) {
389 if (isascii(c
) && isupper(c
))
400 * Unpack a domain name from a message, source may be compressed.
403 *\li -1 if it fails, or consumed octets if it succeeds.
406 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
407 u_char
*dst
, size_t dstsiz
)
409 return (ns_name_unpack2(msg
, eom
, src
, dst
, dstsiz
, NULL
));
413 * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
414 * Unpack a domain name from a message, source may be compressed.
416 * -1 if it fails, or consumed octets if it succeeds.
418 * fills in *dstlen (if non-NULL).
421 ns_name_unpack2(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
422 u_char
*dst
, size_t dstsiz
, size_t *dstlen
)
424 const u_char
*srcp
, *dstlim
;
426 int n
, len
, checked
, l
;
432 dstlim
= dst
+ dstsiz
;
433 if (srcp
< msg
|| srcp
>= eom
) {
437 /* Fetch next label in domain name. */
438 while ((n
= *srcp
++) != 0) {
439 /* Check for indirection. */
440 switch (n
& NS_CMPRSFLGS
) {
444 if ((l
= labellen(srcp
- 1)) < 0) {
448 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
454 memcpy(dstp
, srcp
, l
);
465 len
= srcp
- src
+ 1;
466 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
467 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
473 * Check for loops in the compressed name;
474 * if we've looked at the whole message,
475 * there must be a loop.
477 if (checked
>= eom
- msg
) {
485 return (-1); /*%< flag error */
490 *dstlen
= dstp
- dst
;
497 * Pack domain name 'domain' into 'comp_dn'.
500 *\li Size of the compressed name, or -1.
503 *\li 'dnptrs' is an array of pointers to previous compressed names.
504 *\li dnptrs[0] is a pointer to the beginning of the message. The array
506 *\li 'lastdnptr' is a pointer to the end of the array pointed to
510 *\li The list of pointers in dnptrs is updated for labels inserted into
511 * the message as we compress the name. If 'dnptr' is NULL, we don't
512 * try to compress names. If 'lastdnptr' is NULL, we don't update the
516 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
517 const u_char
**dnptrs
, const u_char
**lastdnptr
)
520 const u_char
**cpp
, **lpp
, *eob
, *msg
;
528 if (dnptrs
!= NULL
) {
529 if ((msg
= *dnptrs
++) != NULL
) {
530 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
532 lpp
= cpp
; /*%< end of list to search */
537 /* make sure the domain we are about to add is legal */
543 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
547 if ((l0
= labellen(srcp
)) < 0) {
559 /* from here on we need to reset compression pointer array on error */
562 /* Look to see if we can use pointers. */
564 if (n
!= 0 && msg
!= NULL
) {
565 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
566 (const u_char
* const *)lpp
);
568 if (dstp
+ 1 >= eob
) {
571 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
575 /* Not found, save it. */
576 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
577 (dstp
- msg
) < 0x4000 && first
) {
583 /* copy label to buffer */
584 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
585 /* Should not happen. */
589 if (dstp
+ 1 + n
>= eob
) {
592 memcpy(dstp
, srcp
, n
+ 1);
608 * Expand compressed domain name to presentation format.
611 *\li Number of bytes read out of `src', or -1 (with errno set).
614 *\li Root domain returns as "." not "".
617 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
618 char *dst
, size_t dstsiz
)
620 u_char tmp
[NS_MAXCDNAME
];
623 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
625 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
631 * Compress a domain name into wire format, using compression pointers.
634 *\li Number of bytes consumed in `dst' or -1 (with errno set).
637 *\li 'dnptrs' is an array of pointers to previous compressed names.
638 *\li dnptrs[0] is a pointer to the beginning of the message.
639 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
640 * array pointed to by 'dnptrs'. Side effect is to update the list of
641 * pointers for labels inserted into the message as we compress the name.
642 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
643 * is NULL, we don't update the list.
646 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
647 const u_char
**dnptrs
, const u_char
**lastdnptr
)
649 u_char tmp
[NS_MAXCDNAME
];
651 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
653 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
657 * Reset dnptrs so that there are no active references to pointers at or
661 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
662 const u_char
**lastdnptr
)
664 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
665 if (*dnptrs
>= src
) {
674 * Advance *ptrptr to skip over the compressed name it points at.
677 *\li 0 on success, -1 (with errno set) on failure.
680 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
687 while (cp
< eom
&& (n
= *cp
++) != 0) {
688 /* Check for indirection. */
689 switch (n
& NS_CMPRSFLGS
) {
690 case 0: /*%< normal case, n == len */
693 case NS_TYPE_ELT
: /*%< EDNS0 extended label */
694 if ((l
= labellen(cp
- 1)) < 0) {
695 errno
= EMSGSIZE
; /*%< XXX */
700 case NS_CMPRSFLGS
: /*%< indirection */
703 default: /*%< illegal type */
717 /* Find the number of octets an nname takes up, including the root label.
718 * (This is basically ns_name_skip() without compression-pointer support.)
719 * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
722 ns_name_length(ns_nname_ct nname
, size_t namesiz
) {
723 ns_nname_ct orig
= nname
;
726 while (namesiz
-- > 0 && (n
= *nname
++) != 0) {
727 if ((n
& NS_CMPRSFLGS
) != 0) {
738 return (nname
- orig
);
741 /* Compare two nname's for equality. Return -1 on error (setting errno).
744 ns_name_eq(ns_nname_ct a
, size_t as
, ns_nname_ct b
, size_t bs
) {
745 ns_nname_ct ae
= a
+ as
, be
= b
+ bs
;
748 while (ac
= *a
, bc
= *b
, ac
!= 0 && bc
!= 0) {
749 if ((ac
& NS_CMPRSFLGS
) != 0 || (bc
& NS_CMPRSFLGS
) != 0) {
753 if (a
+ ac
>= ae
|| b
+ bc
>= be
) {
757 if (ac
!= bc
|| strncasecmp((const char *) ++a
,
758 (const char *) ++b
, ac
) != 0)
762 return (ac
== 0 && bc
== 0);
765 /* Is domain "A" owned by (at or below) domain "B"?
768 ns_name_owned(ns_namemap_ct a
, int an
, ns_namemap_ct b
, int bn
) {
769 /* If A is shorter, it cannot be owned by B. */
773 /* If they are unequal before the length of the shorter, A cannot... */
775 if (a
->len
!= b
->len
||
776 strncasecmp((const char *) a
->base
,
777 (const char *) b
->base
, a
->len
) != 0)
783 /* A might be longer or not, but either way, B owns it. */
787 /* Build an array of <base,len> tuples from an nname, top-down order.
788 * Return the number of tuples (labels) thus discovered.
791 ns_name_map(ns_nname_ct nname
, size_t namelen
, ns_namemap_t map
, int mapsize
) {
800 /* Extra data follows name? */
808 /* Compression pointer? */
809 if ((n
& NS_CMPRSFLGS
) != 0) {
814 /* Label too long? */
820 /* Recurse to get rest of name done first. */
821 l
= ns_name_map(nname
+ n
, namelen
- n
, map
, mapsize
);
825 /* Too many labels? */
827 errno
= ENAMETOOLONG
;
831 /* We're on our way back up-stack, store current map data. */
837 /* Count the labels in a domain name. Root counts, so COM. has two. This
838 * is to make the result comparable to the result of ns_name_map().
841 ns_name_labels(ns_nname_ct nname
, size_t namesiz
) {
845 while (namesiz
-- > 0 && (n
= *nname
++) != 0) {
846 if ((n
& NS_CMPRSFLGS
) != 0) {
864 * Thinking in noninternationalized USASCII (per the DNS spec),
865 * is this characted special ("in need of quoting") ?
873 case 0x22: /*%< '"' */
874 case 0x2E: /*%< '.' */
875 case 0x3B: /*%< ';' */
876 case 0x5C: /*%< '\\' */
877 case 0x28: /*%< '(' */
878 case 0x29: /*%< ')' */
879 /* Special modifiers in zone files. */
880 case 0x40: /*%< '@' */
881 case 0x24: /*%< '$' */
889 * Thinking in noninternationalized USASCII (per the DNS spec),
890 * is this character visible and not a space when printed ?
897 return (ch
> 0x20 && ch
< 0x7f);
901 * Thinking in noninternationalized USASCII (per the DNS spec),
902 * convert this character to lower case if it's upper case.
906 if (ch
>= 0x41 && ch
<= 0x5A)
912 * Search for the counted-label name in an array of compressed names.
915 *\li offset from msg if found, or -1.
918 *\li dnptrs is the pointer to the first name on the list,
919 *\li not the pointer to the start of the message.
922 dn_find(const u_char
*domain
, const u_char
*msg
,
923 const u_char
* const *dnptrs
,
924 const u_char
* const *lastdnptr
)
926 const u_char
*dn
, *cp
, *sp
;
927 const u_char
* const *cpp
;
930 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
933 * terminate search on:
935 * compression pointer
938 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
939 (sp
- msg
) < 0x4000) {
942 while ((n
= *cp
++) != 0) {
944 * check for indirection
946 switch (n
& NS_CMPRSFLGS
) {
947 case 0: /*%< normal case, n == len */
948 n
= labellen(cp
- 1); /*%< XXX */
952 for ((void)NULL
; n
> 0; n
--)
953 if (mklower(*dn
++) !=
956 /* Is next root for both ? */
957 if (*dn
== '\0' && *cp
== '\0')
962 case NS_CMPRSFLGS
: /*%< indirection */
963 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
966 default: /*%< illegal type */
980 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
982 const unsigned char *cp
= *cpp
;
984 int b
, blen
, plen
, i
;
986 if ((blen
= (*cp
& 0xff)) == 0)
988 plen
= (blen
+ 3) / 4;
989 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
990 if (dn
+ plen
>= eom
)
994 i
= SPRINTF((dn
, "\\[x"));
998 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
999 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
1006 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
1012 i
= SPRINTF((dn
, "%1x",
1013 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
1018 i
= SPRINTF((dn
, "/%d]", blen
));
1028 encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
1029 unsigned char ** dst
, unsigned const char *eom
)
1032 const char *cp
= *bp
;
1035 const char *beg_blen
;
1036 char *end_blen
= NULL
;
1037 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
1039 beg_blen
= end_blen
= NULL
;
1041 /* a bitstring must contain at least 2 characters */
1045 /* XXX: currently, only hex strings are supported */
1048 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
1051 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
1053 case ']': /*%< end of the bitstring */
1055 if (beg_blen
== NULL
)
1057 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
1058 if (*end_blen
!= ']')
1062 *tp
++ = ((value
<< 4) & 0xff);
1063 cp
++; /*%< skip ']' */
1070 if (!isdigit(c
&0xff))
1072 if (beg_blen
== NULL
) {
1075 /* blen never begings with 0 */
1081 if (!isxdigit(c
&0xff))
1084 value
+= digitvalue
[(int)c
];
1098 if (cp
>= end
|| tp
>= eom
)
1102 * bit length validation:
1103 * If a <length> is present, the number of digits in the <bit-data>
1104 * MUST be just sufficient to contain the number of bits specified
1105 * by the <length>. If there are insignificant bits in a final
1106 * hexadecimal or octal digit, they MUST be zero.
1107 * RFC2673, Section 3.2.
1112 if (((blen
+ 3) & ~3) != tbcount
)
1114 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
1115 if (((value
<< (8 - traillen
)) & 0xff) != 0)
1123 /* encode the type and the significant bit fields */
1124 **labelp
= DNS_LABELTYPE_BITSTRING
;
1134 labellen(const u_char
*lp
)
1139 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
1140 /* should be avoided by the caller */
1144 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
1145 if (l
== DNS_LABELTYPE_BITSTRING
) {
1146 if ((bitlen
= *(lp
+ 1)) == 0)
1148 return ((bitlen
+ 7 ) / 8 + 1);
1150 return (-1); /*%< unknwon ELT */