2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by 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
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if !defined(_LIBC) && !defined(lint)
19 static const char rcsid
[] = "$BINDId: ns_name.c,v 8.15 2000/03/30 22:53:46 vixie Exp $";
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
34 # define SPRINTF(x) ((size_t)sprintf x)
36 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
37 #define DNS_LABELTYPE_BITSTRING 0x41
41 static const char digits
[] = "0123456789";
43 static const char digitvalue
[256] = {
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
47 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
48 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
50 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
64 static int special(int);
65 static int printable(int);
66 static int dn_find(const u_char
*, const u_char
*,
67 const u_char
* const *,
68 const u_char
* const *);
69 static int encode_bitstring(const char **, const char *,
70 unsigned char **, unsigned char **,
71 unsigned const char *);
72 static int labellen(const u_char
*);
73 static int decode_bitstring(const unsigned char **,
74 char *, const char *);
79 * Convert an encoded domain name to printable ascii as per RFC1035.
82 *\li Number of bytes written to buffer, or -1 (with errno set)
85 *\li The root is returned as "."
86 *\li All other domains are returned in non absolute form
89 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
101 while ((n
= *cp
++) != 0) {
102 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
103 /* Some kind of compression pointer. */
104 __set_errno (EMSGSIZE
);
109 __set_errno (EMSGSIZE
);
114 if ((l
= labellen(cp
- 1)) < 0) {
115 __set_errno (EMSGSIZE
);
119 __set_errno (EMSGSIZE
);
122 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
125 if (n
!= DNS_LABELTYPE_BITSTRING
) {
126 /* XXX: labellen should reject this case */
127 __set_errno (EINVAL
);
130 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
132 __set_errno (EMSGSIZE
);
138 for ((void)NULL
; l
> 0; l
--) {
142 __set_errno (EMSGSIZE
);
147 } else if (!printable(c
)) {
149 __set_errno (EMSGSIZE
);
153 *dn
++ = digits
[c
/ 100];
154 *dn
++ = digits
[(c
% 100) / 10];
155 *dn
++ = digits
[c
% 10];
158 __set_errno (EMSGSIZE
);
167 __set_errno (EMSGSIZE
);
173 __set_errno (EMSGSIZE
);
179 libresolv_hidden_def (ns_name_ntop
)
180 strong_alias (ns_name_ntop
, __ns_name_ntop
)
183 * Convert a ascii string into an encoded domain name as per RFC1035.
188 *\li 1 if string was fully qualified
189 *\li 0 is string was not fully qualified
192 *\li Enforces label and domain length limits.
196 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
198 u_char
*label
, *bp
, *eom
;
199 int c
, n
, escaped
, e
= 0;
207 while ((c
= *src
++) != 0) {
209 if (c
== '[') { /*%< start a bit string label */
210 if ((cp
= strchr(src
, ']')) == NULL
) {
211 __set_errno (EINVAL
);
214 if ((e
= encode_bitstring(&src
, cp
+ 2,
222 if ((c
= *src
++) == 0)
225 __set_errno (EINVAL
);
230 else if ((cp
= strchr(digits
, c
)) != NULL
) {
231 n
= (cp
- digits
) * 100;
232 if ((c
= *src
++) == 0 ||
233 (cp
= strchr(digits
, c
)) == NULL
) {
234 __set_errno (EMSGSIZE
);
237 n
+= (cp
- digits
) * 10;
238 if ((c
= *src
++) == 0 ||
239 (cp
= strchr(digits
, c
)) == NULL
) {
240 __set_errno (EMSGSIZE
);
245 __set_errno (EMSGSIZE
);
251 } else if (c
== '\\') {
254 } else if (c
== '.') {
255 c
= (bp
- label
- 1);
256 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
257 __set_errno (EMSGSIZE
);
261 __set_errno (EMSGSIZE
);
265 /* Fully qualified ? */
269 __set_errno (EMSGSIZE
);
274 if ((bp
- dst
) > MAXCDNAME
) {
275 __set_errno (EMSGSIZE
);
280 if (c
== 0 || *src
== '.') {
281 __set_errno (EMSGSIZE
);
288 __set_errno (EMSGSIZE
);
293 c
= (bp
- label
- 1);
294 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
295 __set_errno (EMSGSIZE
);
300 __set_errno (EMSGSIZE
);
306 __set_errno (EMSGSIZE
);
311 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
312 __set_errno (EMSGSIZE
);
317 libresolv_hidden_def (ns_name_pton
)
320 * Convert a network strings labels into all lowercase.
323 *\li Number of bytes written to buffer, or -1 (with errno set)
326 *\li Enforces label and domain length limits.
330 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
343 __set_errno (EMSGSIZE
);
346 while ((n
= *cp
++) != 0) {
347 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
348 /* Some kind of compression pointer. */
349 __set_errno (EMSGSIZE
);
353 if ((l
= labellen(cp
- 1)) < 0) {
354 __set_errno (EMSGSIZE
);
358 __set_errno (EMSGSIZE
);
361 for ((void)NULL
; l
> 0; l
--) {
374 * Unpack a domain name from a message, source may be compressed.
377 *\li -1 if it fails, or consumed octets if it succeeds.
380 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
381 u_char
*dst
, size_t dstsiz
)
383 const u_char
*srcp
, *dstlim
;
385 int n
, len
, checked
, l
;
391 dstlim
= dst
+ dstsiz
;
392 if (srcp
< msg
|| srcp
>= eom
) {
393 __set_errno (EMSGSIZE
);
396 /* Fetch next label in domain name. */
397 while ((n
= *srcp
++) != 0) {
398 /* Check for indirection. */
399 switch (n
& NS_CMPRSFLGS
) {
403 if ((l
= labellen(srcp
- 1)) < 0) {
404 __set_errno (EMSGSIZE
);
407 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
408 __set_errno (EMSGSIZE
);
413 memcpy(dstp
, srcp
, l
);
420 __set_errno (EMSGSIZE
);
424 len
= srcp
- src
+ 1;
425 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
426 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
427 __set_errno (EMSGSIZE
);
432 * Check for loops in the compressed name;
433 * if we've looked at the whole message,
434 * there must be a loop.
436 if (checked
>= eom
- msg
) {
437 __set_errno (EMSGSIZE
);
443 __set_errno (EMSGSIZE
);
444 return (-1); /*%< flag error */
452 libresolv_hidden_def (ns_name_unpack
)
453 strong_alias (ns_name_unpack
, __ns_name_unpack
)
456 * Pack domain name 'domain' into 'comp_dn'.
459 *\li Size of the compressed name, or -1.
462 *\li 'dnptrs' is an array of pointers to previous compressed names.
463 *\li dnptrs[0] is a pointer to the beginning of the message. The array
465 *\li 'lastdnptr' is a pointer to the end of the array pointed to
469 *\li The list of pointers in dnptrs is updated for labels inserted into
470 * the message as we compress the name. If 'dnptr' is NULL, we don't
471 * try to compress names. If 'lastdnptr' is NULL, we don't update the
475 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
476 const u_char
**dnptrs
, const u_char
**lastdnptr
)
479 const u_char
**cpp
, **lpp
, *eob
, *msg
;
487 if (dnptrs
!= NULL
) {
488 if ((msg
= *dnptrs
++) != NULL
) {
489 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
491 lpp
= cpp
; /*%< end of list to search */
496 /* make sure the domain we are about to add is legal */
502 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
503 __set_errno (EMSGSIZE
);
506 if ((l0
= labellen(srcp
)) < 0) {
507 __set_errno (EINVAL
);
512 __set_errno (EMSGSIZE
);
518 /* from here on we need to reset compression pointer array on error */
521 /* Look to see if we can use pointers. */
523 if (n
!= 0 && msg
!= NULL
) {
524 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
525 (const u_char
* const *)lpp
);
527 if (dstp
+ 1 >= eob
) {
530 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
534 /* Not found, save it. */
535 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
536 (dstp
- msg
) < 0x4000 && first
) {
542 /* copy label to buffer */
543 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
544 /* Should not happen. */
548 if (dstp
+ 1 + n
>= eob
) {
551 memcpy(dstp
, srcp
, n
+ 1);
560 __set_errno (EMSGSIZE
);
565 libresolv_hidden_def (ns_name_pack
)
568 * Expand compressed domain name to presentation format.
571 *\li Number of bytes read out of `src', or -1 (with errno set).
574 *\li Root domain returns as "." not "".
577 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
578 char *dst
, size_t dstsiz
)
580 u_char tmp
[NS_MAXCDNAME
];
583 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
585 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
589 libresolv_hidden_def (ns_name_uncompress
)
592 * Compress a domain name into wire format, using compression pointers.
595 *\li Number of bytes consumed in `dst' or -1 (with errno set).
598 *\li 'dnptrs' is an array of pointers to previous compressed names.
599 *\li dnptrs[0] is a pointer to the beginning of the message.
600 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
601 * array pointed to by 'dnptrs'. Side effect is to update the list of
602 * pointers for labels inserted into the message as we compress the name.
603 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
604 * is NULL, we don't update the list.
607 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
608 const u_char
**dnptrs
, const u_char
**lastdnptr
)
610 u_char tmp
[NS_MAXCDNAME
];
612 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
614 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
616 libresolv_hidden_def (ns_name_compress
)
619 * Reset dnptrs so that there are no active references to pointers at or
623 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
624 const u_char
**lastdnptr
)
626 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
627 if (*dnptrs
>= src
) {
636 * Advance *ptrptr to skip over the compressed name it points at.
639 *\li 0 on success, -1 (with errno set) on failure.
642 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
649 while (cp
< eom
&& (n
= *cp
++) != 0) {
650 /* Check for indirection. */
651 switch (n
& NS_CMPRSFLGS
) {
652 case 0: /*%< normal case, n == len */
655 case NS_TYPE_ELT
: /*%< EDNS0 extended label */
656 if ((l
= labellen(cp
- 1)) < 0) {
657 __set_errno (EMSGSIZE
);
662 case NS_CMPRSFLGS
: /*%< indirection */
665 default: /*%< illegal type */
666 __set_errno (EMSGSIZE
);
672 __set_errno (EMSGSIZE
);
678 libresolv_hidden_def (ns_name_skip
)
683 * Thinking in noninternationalized USASCII (per the DNS spec),
684 * is this characted special ("in need of quoting") ?
692 case 0x22: /*%< '"' */
693 case 0x2E: /*%< '.' */
694 case 0x3B: /*%< ';' */
695 case 0x5C: /*%< '\\' */
696 case 0x28: /*%< '(' */
697 case 0x29: /*%< ')' */
698 /* Special modifiers in zone files. */
699 case 0x40: /*%< '@' */
700 case 0x24: /*%< '$' */
708 * Thinking in noninternationalized USASCII (per the DNS spec),
709 * is this character visible and not a space when printed ?
716 return (ch
> 0x20 && ch
< 0x7f);
720 * Thinking in noninternationalized USASCII (per the DNS spec),
721 * convert this character to lower case if it's upper case.
725 if (ch
>= 0x41 && ch
<= 0x5A)
731 * Search for the counted-label name in an array of compressed names.
734 *\li offset from msg if found, or -1.
737 *\li dnptrs is the pointer to the first name on the list,
738 *\li not the pointer to the start of the message.
741 dn_find(const u_char
*domain
, const u_char
*msg
,
742 const u_char
* const *dnptrs
,
743 const u_char
* const *lastdnptr
)
745 const u_char
*dn
, *cp
, *sp
;
746 const u_char
* const *cpp
;
749 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
752 * terminate search on:
754 * compression pointer
757 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
758 (sp
- msg
) < 0x4000) {
761 while ((n
= *cp
++) != 0) {
763 * check for indirection
765 switch (n
& NS_CMPRSFLGS
) {
766 case 0: /*%< normal case, n == len */
767 n
= labellen(cp
- 1); /*%< XXX */
771 for ((void)NULL
; n
> 0; n
--)
772 if (mklower(*dn
++) !=
775 /* Is next root for both ? */
776 if (*dn
== '\0' && *cp
== '\0')
781 case NS_CMPRSFLGS
: /*%< indirection */
782 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
785 default: /*%< illegal type */
786 __set_errno (EMSGSIZE
);
794 __set_errno (ENOENT
);
799 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
801 const unsigned char *cp
= *cpp
;
803 int b
, blen
, plen
, i
;
805 if ((blen
= (*cp
& 0xff)) == 0)
807 plen
= (blen
+ 3) / 4;
808 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
809 if (dn
+ plen
>= eom
)
813 i
= SPRINTF((dn
, "\\[x"));
817 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
818 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
825 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
831 i
= SPRINTF((dn
, "%1x",
832 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
837 i
= SPRINTF((dn
, "/%d]", blen
));
847 encode_bitstring(const char **bp
, const char *end
, unsigned char **labelp
,
848 unsigned char ** dst
, unsigned const char *eom
)
851 const char *cp
= *bp
;
854 const char *beg_blen
;
855 char *end_blen
= NULL
;
856 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
858 beg_blen
= end_blen
= NULL
;
860 /* a bitstring must contain at least 2 characters */
864 /* XXX: currently, only hex strings are supported */
867 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
870 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
872 case ']': /*%< end of the bitstring */
874 if (beg_blen
== NULL
)
876 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
877 if (*end_blen
!= ']')
881 *tp
++ = ((value
<< 4) & 0xff);
882 cp
++; /*%< skip ']' */
889 if (!isdigit(c
&0xff))
891 if (beg_blen
== NULL
) {
894 /* blen never begings with 0 */
900 if (!isxdigit(c
&0xff))
903 value
+= digitvalue
[(int)c
];
917 if (cp
>= end
|| tp
>= eom
)
921 * bit length validation:
922 * If a <length> is present, the number of digits in the <bit-data>
923 * MUST be just sufficient to contain the number of bits specified
924 * by the <length>. If there are insignificant bits in a final
925 * hexadecimal or octal digit, they MUST be zero.
926 * RFC2673, Section 3.2.
931 if (((blen
+ 3) & ~3) != tbcount
)
933 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
934 if (((value
<< (8 - traillen
)) & 0xff) != 0)
942 /* encode the type and the significant bit fields */
943 **labelp
= DNS_LABELTYPE_BITSTRING
;
953 labellen(const u_char
*lp
)
958 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
959 /* should be avoided by the caller */
963 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
964 if (l
== DNS_LABELTYPE_BITSTRING
) {
965 if ((bitlen
= *(lp
+ 1)) == 0)
967 return((bitlen
+ 7 ) / 8 + 1);
969 return(-1); /*%< unknwon ELT */