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.
19 static const char rcsid
[] = "$Id: ns_name.c,v 1.10 2005/04/27 04:56:40 sra Exp $";
22 #include "port_before.h"
24 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
36 #include "port_after.h"
39 # define SPRINTF(x) strlen(sprintf/**/x)
41 # define SPRINTF(x) ((size_t)sprintf x)
44 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
45 #define DNS_LABELTYPE_BITSTRING 0x41
49 static const char digits
[] = "0123456789";
51 static const char digitvalue
[256] = {
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
55 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
56 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
58 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
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,
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, /*256*/
72 static int special(int);
73 static int printable(int);
74 static int dn_find(const u_char
*, const u_char
*,
75 const u_char
* const *,
76 const u_char
* const *);
77 static int encode_bitsring(const char **, const char *,
78 unsigned char **, unsigned char **,
79 unsigned const char *);
80 static int labellen(const u_char
*);
81 static int decode_bitstring(const unsigned char **,
82 char *, const char *);
87 * Convert an encoded domain name to printable ascii as per RFC1035.
90 *\li Number of bytes written to buffer, or -1 (with errno set)
93 *\li The root is returned as "."
94 *\li All other domains are returned in non absolute form
97 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
109 while ((n
= *cp
++) != 0) {
110 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
111 /* Some kind of compression pointer. */
122 if ((l
= labellen(cp
- 1)) < 0) {
123 errno
= EMSGSIZE
; /*%< XXX */
130 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
133 if (n
!= DNS_LABELTYPE_BITSTRING
) {
134 /* XXX: labellen should reject this case */
138 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
146 for ((void)NULL
; l
> 0; l
--) {
155 } else if (!printable(c
)) {
161 *dn
++ = digits
[c
/ 100];
162 *dn
++ = digits
[(c
% 100) / 10];
163 *dn
++ = digits
[c
% 10];
189 * Convert a ascii string into an encoded domain name as per RFC1035.
194 *\li 1 if string was fully qualified
195 *\li 0 is string was not fully qualified
198 *\li Enforces label and domain length limits.
202 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
204 u_char
*label
, *bp
, *eom
;
205 int c
, n
, escaped
, e
= 0;
213 while ((c
= *src
++) != 0) {
215 if (c
== '[') { /*%< start a bit string label */
216 if ((cp
= strchr(src
, ']')) == NULL
) {
217 errno
= EINVAL
; /*%< ??? */
220 if ((e
= encode_bitsring(&src
, cp
+ 2,
228 if ((c
= *src
++) == 0)
236 else if ((cp
= strchr(digits
, c
)) != NULL
) {
237 n
= (cp
- digits
) * 100;
238 if ((c
= *src
++) == 0 ||
239 (cp
= strchr(digits
, c
)) == NULL
) {
243 n
+= (cp
- digits
) * 10;
244 if ((c
= *src
++) == 0 ||
245 (cp
= strchr(digits
, c
)) == NULL
) {
257 } else if (c
== '\\') {
260 } else if (c
== '.') {
261 c
= (bp
- label
- 1);
262 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
271 /* Fully qualified ? */
280 if ((bp
- dst
) > MAXCDNAME
) {
286 if (c
== 0 || *src
== '.') {
299 c
= (bp
- label
- 1);
300 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
317 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
325 * Convert a network strings labels into all lowercase.
328 *\li Number of bytes written to buffer, or -1 (with errno set)
331 *\li Enforces label and domain length limits.
335 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
351 while ((n
= *cp
++) != 0) {
352 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
353 /* Some kind of compression pointer. */
358 if ((l
= labellen(cp
- 1)) < 0) {
366 for ((void)NULL
; l
> 0; l
--) {
379 * Unpack a domain name from a message, source may be compressed.
382 *\li -1 if it fails, or consumed octets if it succeeds.
385 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
386 u_char
*dst
, size_t dstsiz
)
388 const u_char
*srcp
, *dstlim
;
390 int n
, len
, checked
, l
;
396 dstlim
= dst
+ dstsiz
;
397 if (srcp
< msg
|| srcp
>= eom
) {
401 /* Fetch next label in domain name. */
402 while ((n
= *srcp
++) != 0) {
403 /* Check for indirection. */
404 switch (n
& NS_CMPRSFLGS
) {
408 if ((l
= labellen(srcp
- 1)) < 0) {
412 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
418 memcpy(dstp
, srcp
, l
);
429 len
= srcp
- src
+ 1;
430 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
431 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
437 * Check for loops in the compressed name;
438 * if we've looked at the whole message,
439 * there must be a loop.
441 if (checked
>= eom
- msg
) {
449 return (-1); /*%< flag error */
459 * Pack domain name 'domain' into 'comp_dn'.
462 *\li Size of the compressed name, or -1.
465 *\li 'dnptrs' is an array of pointers to previous compressed names.
466 *\li dnptrs[0] is a pointer to the beginning of the message. The array
468 *\li 'lastdnptr' is a pointer to the end of the array pointed to
472 *\li The list of pointers in dnptrs is updated for labels inserted into
473 * the message as we compress the name. If 'dnptr' is NULL, we don't
474 * try to compress names. If 'lastdnptr' is NULL, we don't update the
478 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
479 const u_char
**dnptrs
, const u_char
**lastdnptr
)
482 const u_char
**cpp
, **lpp
, *eob
, *msg
;
490 if (dnptrs
!= NULL
) {
491 if ((msg
= *dnptrs
++) != NULL
) {
492 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
494 lpp
= cpp
; /*%< end of list to search */
499 /* make sure the domain we are about to add is legal */
505 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
509 if ((l0
= labellen(srcp
)) < 0) {
521 /* from here on we need to reset compression pointer array on error */
524 /* Look to see if we can use pointers. */
526 if (n
!= 0 && msg
!= NULL
) {
527 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
528 (const u_char
* const *)lpp
);
530 if (dstp
+ 1 >= eob
) {
533 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
537 /* Not found, save it. */
538 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
539 (dstp
- msg
) < 0x4000 && first
) {
545 /* copy label to buffer */
546 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
547 /* Should not happen. */
551 if (dstp
+ 1 + n
>= eob
) {
554 memcpy(dstp
, srcp
, n
+ 1);
570 * Expand compressed domain name to presentation format.
573 *\li Number of bytes read out of `src', or -1 (with errno set).
576 *\li Root domain returns as "." not "".
579 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
580 char *dst
, size_t dstsiz
)
582 u_char tmp
[NS_MAXCDNAME
];
585 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
587 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
593 * Compress a domain name into wire format, using compression pointers.
596 *\li Number of bytes consumed in `dst' or -1 (with errno set).
599 *\li 'dnptrs' is an array of pointers to previous compressed names.
600 *\li dnptrs[0] is a pointer to the beginning of the message.
601 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
602 * array pointed to by 'dnptrs'. Side effect is to update the list of
603 * pointers for labels inserted into the message as we compress the name.
604 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
605 * is NULL, we don't update the list.
608 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
609 const u_char
**dnptrs
, const u_char
**lastdnptr
)
611 u_char tmp
[NS_MAXCDNAME
];
613 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
615 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
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 errno
= EMSGSIZE
; /*%< XXX */
662 case NS_CMPRSFLGS
: /*%< indirection */
665 default: /*%< illegal type */
682 * Thinking in noninternationalized USASCII (per the DNS spec),
683 * is this characted special ("in need of quoting") ?
691 case 0x22: /*%< '"' */
692 case 0x2E: /*%< '.' */
693 case 0x3B: /*%< ';' */
694 case 0x5C: /*%< '\\' */
695 case 0x28: /*%< '(' */
696 case 0x29: /*%< ')' */
697 /* Special modifiers in zone files. */
698 case 0x40: /*%< '@' */
699 case 0x24: /*%< '$' */
707 * Thinking in noninternationalized USASCII (per the DNS spec),
708 * is this character visible and not a space when printed ?
715 return (ch
> 0x20 && ch
< 0x7f);
719 * Thinking in noninternationalized USASCII (per the DNS spec),
720 * convert this character to lower case if it's upper case.
724 if (ch
>= 0x41 && ch
<= 0x5A)
730 * Search for the counted-label name in an array of compressed names.
733 *\li offset from msg if found, or -1.
736 *\li dnptrs is the pointer to the first name on the list,
737 *\li not the pointer to the start of the message.
740 dn_find(const u_char
*domain
, const u_char
*msg
,
741 const u_char
* const *dnptrs
,
742 const u_char
* const *lastdnptr
)
744 const u_char
*dn
, *cp
, *sp
;
745 const u_char
* const *cpp
;
748 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
751 * terminate search on:
753 * compression pointer
756 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
757 (sp
- msg
) < 0x4000) {
760 while ((n
= *cp
++) != 0) {
762 * check for indirection
764 switch (n
& NS_CMPRSFLGS
) {
765 case 0: /*%< normal case, n == len */
766 n
= labellen(cp
- 1); /*%< XXX */
770 for ((void)NULL
; n
> 0; n
--)
771 if (mklower(*dn
++) !=
774 /* Is next root for both ? */
775 if (*dn
== '\0' && *cp
== '\0')
780 case NS_CMPRSFLGS
: /*%< indirection */
781 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
784 default: /*%< illegal type */
798 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
800 const unsigned char *cp
= *cpp
;
802 int b
, blen
, plen
, i
;
804 if ((blen
= (*cp
& 0xff)) == 0)
806 plen
= (blen
+ 3) / 4;
807 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
808 if (dn
+ plen
>= eom
)
812 i
= SPRINTF((dn
, "\\[x"));
816 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
817 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
824 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
830 i
= SPRINTF((dn
, "%1x",
831 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
836 i
= SPRINTF((dn
, "/%d]", blen
));
846 encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
847 unsigned char ** dst
, unsigned const char *eom
)
850 const char *cp
= *bp
;
853 const char *beg_blen
;
854 char *end_blen
= NULL
;
855 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
857 beg_blen
= end_blen
= NULL
;
859 /* a bitstring must contain at least 2 characters */
863 /* XXX: currently, only hex strings are supported */
866 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
869 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
871 case ']': /*%< end of the bitstring */
873 if (beg_blen
== NULL
)
875 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
876 if (*end_blen
!= ']')
880 *tp
++ = ((value
<< 4) & 0xff);
881 cp
++; /*%< skip ']' */
888 if (!isdigit(c
&0xff))
890 if (beg_blen
== NULL
) {
893 /* blen never begings with 0 */
899 if (!isxdigit(c
&0xff))
902 value
+= digitvalue
[(int)c
];
916 if (cp
>= end
|| tp
>= eom
)
920 * bit length validation:
921 * If a <length> is present, the number of digits in the <bit-data>
922 * MUST be just sufficient to contain the number of bits specified
923 * by the <length>. If there are insignificant bits in a final
924 * hexadecimal or octal digit, they MUST be zero.
925 * RFC2673, Section 3.2.
930 if (((blen
+ 3) & ~3) != tbcount
)
932 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
933 if (((value
<< (8 - traillen
)) & 0xff) != 0)
941 /* encode the type and the significant bit fields */
942 **labelp
= DNS_LABELTYPE_BITSTRING
;
952 labellen(const u_char
*lp
)
957 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
958 /* should be avoided by the caller */
962 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
963 if (l
== DNS_LABELTYPE_BITSTRING
) {
964 if ((bitlen
= *(lp
+ 1)) == 0)
966 return((bitlen
+ 7 ) / 8 + 1);
968 return(-1); /*%< unknwon ELT */