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.
17 * $Id: ns_name.c,v 1.10 2005/04/27 04:56:40 sra Exp $
20 #include "port_before.h"
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
34 #include "port_after.h"
37 # define SPRINTF(x) strlen(sprintf/**/x)
39 # define SPRINTF(x) ((size_t)sprintf x)
42 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
43 #define DNS_LABELTYPE_BITSTRING 0x41
47 static const char digits
[] = "0123456789";
49 static const char digitvalue
[256] = {
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
53 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
54 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
56 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
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,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, /*256*/
70 static int special(int);
71 static int printable(int);
72 static int dn_find(const u_char
*, const u_char
*,
73 const u_char
* const *,
74 const u_char
* const *);
75 static int encode_bitsring(const char **, const char *,
76 unsigned char **, unsigned char **,
77 unsigned const char *);
78 static int labellen(const u_char
*);
79 static int decode_bitstring(const unsigned char **,
80 char *, const char *);
85 * Convert an encoded domain name to printable ascii as per RFC1035.
88 *\li Number of bytes written to buffer, or -1 (with errno set)
91 *\li The root is returned as "."
92 *\li All other domains are returned in non absolute form
95 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
107 while ((n
= *cp
++) != 0) {
108 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
109 /* Some kind of compression pointer. */
120 if ((l
= labellen(cp
- 1)) < 0) {
121 errno
= EMSGSIZE
; /*%< XXX */
128 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
131 if (n
!= DNS_LABELTYPE_BITSTRING
) {
132 /* XXX: labellen should reject this case */
136 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
144 for ((void)NULL
; l
> 0; l
--) {
153 } else if (!printable(c
)) {
159 *dn
++ = digits
[c
/ 100];
160 *dn
++ = digits
[(c
% 100) / 10];
161 *dn
++ = digits
[c
% 10];
187 * Convert a ascii string into an encoded domain name as per RFC1035.
192 *\li 1 if string was fully qualified
193 *\li 0 is string was not fully qualified
196 *\li Enforces label and domain length limits.
200 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
202 u_char
*label
, *bp
, *eom
;
203 int c
, n
, escaped
, e
= 0;
211 while ((c
= *src
++) != 0) {
213 if (c
== '[') { /*%< start a bit string label */
214 if ((cp
= strchr(src
, ']')) == NULL
) {
215 errno
= EINVAL
; /*%< ??? */
218 if ((e
= encode_bitsring(&src
, cp
+ 2,
226 if ((c
= *src
++) == 0)
234 else if ((cp
= strchr(digits
, c
)) != NULL
) {
235 n
= (cp
- digits
) * 100;
236 if ((c
= *src
++) == 0 ||
237 (cp
= strchr(digits
, c
)) == NULL
) {
241 n
+= (cp
- digits
) * 10;
242 if ((c
= *src
++) == 0 ||
243 (cp
= strchr(digits
, c
)) == NULL
) {
255 } else if (c
== '\\') {
258 } else if (c
== '.') {
259 c
= (bp
- label
- 1);
260 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
269 /* Fully qualified ? */
278 if ((bp
- dst
) > MAXCDNAME
) {
284 if (c
== 0 || *src
== '.') {
297 c
= (bp
- label
- 1);
298 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
315 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
323 * Convert a network strings labels into all lowercase.
326 *\li Number of bytes written to buffer, or -1 (with errno set)
329 *\li Enforces label and domain length limits.
333 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
349 while ((n
= *cp
++) != 0) {
350 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
351 /* Some kind of compression pointer. */
356 if ((l
= labellen(cp
- 1)) < 0) {
364 for ((void)NULL
; l
> 0; l
--) {
377 * Unpack a domain name from a message, source may be compressed.
380 *\li -1 if it fails, or consumed octets if it succeeds.
383 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
384 u_char
*dst
, size_t dstsiz
)
386 const u_char
*srcp
, *dstlim
;
388 int n
, len
, checked
, l
;
394 dstlim
= dst
+ dstsiz
;
395 if (srcp
< msg
|| srcp
>= eom
) {
399 /* Fetch next label in domain name. */
400 while ((n
= *srcp
++) != 0) {
401 /* Check for indirection. */
402 switch (n
& NS_CMPRSFLGS
) {
406 if ((l
= labellen(srcp
- 1)) < 0) {
410 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
416 memcpy(dstp
, srcp
, l
);
427 len
= srcp
- src
+ 1;
428 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
429 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
435 * Check for loops in the compressed name;
436 * if we've looked at the whole message,
437 * there must be a loop.
439 if (checked
>= eom
- msg
) {
447 return (-1); /*%< flag error */
457 * Pack domain name 'domain' into 'comp_dn'.
460 *\li Size of the compressed name, or -1.
463 *\li 'dnptrs' is an array of pointers to previous compressed names.
464 *\li dnptrs[0] is a pointer to the beginning of the message. The array
466 *\li 'lastdnptr' is a pointer to the end of the array pointed to
470 *\li The list of pointers in dnptrs is updated for labels inserted into
471 * the message as we compress the name. If 'dnptr' is NULL, we don't
472 * try to compress names. If 'lastdnptr' is NULL, we don't update the
476 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
477 const u_char
**dnptrs
, const u_char
**lastdnptr
)
480 const u_char
**cpp
, **lpp
, *eob
, *msg
;
488 if (dnptrs
!= NULL
) {
489 if ((msg
= *dnptrs
++) != NULL
) {
490 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
492 lpp
= cpp
; /*%< end of list to search */
497 /* make sure the domain we are about to add is legal */
503 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
507 if ((l0
= labellen(srcp
)) < 0) {
519 /* from here on we need to reset compression pointer array on error */
522 /* Look to see if we can use pointers. */
524 if (n
!= 0 && msg
!= NULL
) {
525 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
526 (const u_char
* const *)lpp
);
528 if (dstp
+ 1 >= eob
) {
531 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
535 /* Not found, save it. */
536 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
537 (dstp
- msg
) < 0x4000 && first
) {
543 /* copy label to buffer */
544 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
545 /* Should not happen. */
549 if (dstp
+ 1 + n
>= eob
) {
552 memcpy(dstp
, srcp
, n
+ 1);
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)
591 * Compress a domain name into wire format, using compression pointers.
594 *\li Number of bytes consumed in `dst' or -1 (with errno set).
597 *\li 'dnptrs' is an array of pointers to previous compressed names.
598 *\li dnptrs[0] is a pointer to the beginning of the message.
599 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
600 * array pointed to by 'dnptrs'. Side effect is to update the list of
601 * pointers for labels inserted into the message as we compress the name.
602 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
603 * is NULL, we don't update the list.
606 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
607 const u_char
**dnptrs
, const u_char
**lastdnptr
)
609 u_char tmp
[NS_MAXCDNAME
];
611 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
613 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
617 * Reset dnptrs so that there are no active references to pointers at or
621 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
622 const u_char
**lastdnptr
)
624 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
625 if (*dnptrs
>= src
) {
634 * Advance *ptrptr to skip over the compressed name it points at.
637 *\li 0 on success, -1 (with errno set) on failure.
640 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
647 while (cp
< eom
&& (n
= *cp
++) != 0) {
648 /* Check for indirection. */
649 switch (n
& NS_CMPRSFLGS
) {
650 case 0: /*%< normal case, n == len */
653 case NS_TYPE_ELT
: /*%< EDNS0 extended label */
654 if ((l
= labellen(cp
- 1)) < 0) {
655 errno
= EMSGSIZE
; /*%< XXX */
660 case NS_CMPRSFLGS
: /*%< indirection */
663 default: /*%< illegal type */
680 * Thinking in noninternationalized USASCII (per the DNS spec),
681 * is this characted special ("in need of quoting") ?
689 case 0x22: /*%< '"' */
690 case 0x2E: /*%< '.' */
691 case 0x3B: /*%< ';' */
692 case 0x5C: /*%< '\\' */
693 case 0x28: /*%< '(' */
694 case 0x29: /*%< ')' */
695 /* Special modifiers in zone files. */
696 case 0x40: /*%< '@' */
697 case 0x24: /*%< '$' */
705 * Thinking in noninternationalized USASCII (per the DNS spec),
706 * is this character visible and not a space when printed ?
713 return (ch
> 0x20 && ch
< 0x7f);
717 * Thinking in noninternationalized USASCII (per the DNS spec),
718 * convert this character to lower case if it's upper case.
722 if (ch
>= 0x41 && ch
<= 0x5A)
728 * Search for the counted-label name in an array of compressed names.
731 *\li offset from msg if found, or -1.
734 *\li dnptrs is the pointer to the first name on the list,
735 *\li not the pointer to the start of the message.
738 dn_find(const u_char
*domain
, const u_char
*msg
,
739 const u_char
* const *dnptrs
,
740 const u_char
* const *lastdnptr
)
742 const u_char
*dn
, *cp
, *sp
;
743 const u_char
* const *cpp
;
746 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
749 * terminate search on:
751 * compression pointer
754 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
755 (sp
- msg
) < 0x4000) {
758 while ((n
= *cp
++) != 0) {
760 * check for indirection
762 switch (n
& NS_CMPRSFLGS
) {
763 case 0: /*%< normal case, n == len */
764 n
= labellen(cp
- 1); /*%< XXX */
768 for ((void)NULL
; n
> 0; n
--)
769 if (mklower(*dn
++) !=
772 /* Is next root for both ? */
773 if (*dn
== '\0' && *cp
== '\0')
778 case NS_CMPRSFLGS
: /*%< indirection */
779 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
782 default: /*%< illegal type */
796 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
798 const unsigned char *cp
= *cpp
;
800 int b
, blen
, plen
, i
;
802 if ((blen
= (*cp
& 0xff)) == 0)
804 plen
= (blen
+ 3) / 4;
805 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
806 if (dn
+ plen
>= eom
)
810 i
= SPRINTF((dn
, "\\[x"));
814 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
815 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
822 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
828 i
= SPRINTF((dn
, "%1x",
829 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
834 i
= SPRINTF((dn
, "/%d]", blen
));
844 encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
845 unsigned char ** dst
, unsigned const char *eom
)
848 const char *cp
= *bp
;
851 const char *beg_blen
;
852 char *end_blen
= NULL
;
853 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
855 beg_blen
= end_blen
= NULL
;
857 /* a bitstring must contain at least 2 characters */
861 /* XXX: currently, only hex strings are supported */
864 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
867 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
869 case ']': /*%< end of the bitstring */
871 if (beg_blen
== NULL
)
873 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
874 if (*end_blen
!= ']')
878 *tp
++ = ((value
<< 4) & 0xff);
879 cp
++; /*%< skip ']' */
886 if (!isdigit(c
&0xff))
888 if (beg_blen
== NULL
) {
891 /* blen never begings with 0 */
897 if (!isxdigit(c
&0xff))
900 value
+= digitvalue
[(int)c
];
914 if (cp
>= end
|| tp
>= eom
)
918 * bit length validation:
919 * If a <length> is present, the number of digits in the <bit-data>
920 * MUST be just sufficient to contain the number of bits specified
921 * by the <length>. If there are insignificant bits in a final
922 * hexadecimal or octal digit, they MUST be zero.
923 * RFC2673, Section 3.2.
928 if (((blen
+ 3) & ~3) != tbcount
)
930 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
931 if (((value
<< (8 - traillen
)) & 0xff) != 0)
939 /* encode the type and the significant bit fields */
940 **labelp
= DNS_LABELTYPE_BITSTRING
;
950 labellen(const u_char
*lp
)
955 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
956 /* should be avoided by the caller */
960 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
961 if (l
== DNS_LABELTYPE_BITSTRING
) {
962 if ((bitlen
= *(lp
+ 1)) == 0)
964 return((bitlen
+ 7 ) / 8 + 1);
966 return(-1); /*%< unknwon ELT */