1 /* $NetBSD: ns_name.c,v 1.8 2009/04/12 19:43:37 christos Exp $ */
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.
20 #include <sys/cdefs.h>
23 static const char rcsid
[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp";
25 __RCSID("$NetBSD: ns_name.c,v 1.8 2009/04/12 19:43:37 christos Exp $");
29 #include "port_before.h"
31 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
43 #include "port_after.h"
46 # define SPRINTF(x) strlen(sprintf/**/x)
48 # define SPRINTF(x) ((size_t)sprintf x)
51 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
52 #define DNS_LABELTYPE_BITSTRING 0x41
56 static const char digits
[] = "0123456789";
58 static const char digitvalue
[256] = {
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
62 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
63 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
65 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
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,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
79 static int special(int);
80 static int printable(int);
81 static int dn_find(const u_char
*, const u_char
*,
82 const u_char
* const *,
83 const u_char
* const *);
84 static int encode_bitsring(const char **, const char *,
85 unsigned char **, unsigned char **,
86 unsigned const char *);
87 static int labellen(const u_char
*);
88 static int decode_bitstring(const unsigned char **,
89 char *, const char *);
94 * Convert an encoded domain name to printable ascii as per RFC1035.
97 *\li Number of bytes written to buffer, or -1 (with errno set)
100 *\li The root is returned as "."
101 *\li All other domains are returned in non absolute form
104 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
116 while ((n
= *cp
++) != 0) {
117 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
118 /* Some kind of compression pointer. */
129 if ((l
= labellen(cp
- 1)) < 0) {
130 errno
= EMSGSIZE
; /*%< XXX */
137 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
140 if (n
!= DNS_LABELTYPE_BITSTRING
) {
141 /* XXX: labellen should reject this case */
145 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
162 } else if (!printable(c
)) {
168 *dn
++ = digits
[c
/ 100];
169 *dn
++ = digits
[(c
% 100) / 10];
170 *dn
++ = digits
[c
% 10];
196 * Convert a ascii string into an encoded domain name as per RFC1035.
201 *\li 1 if string was fully qualified
202 *\li 0 is string was not fully qualified
205 *\li Enforces label and domain length limits.
208 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
209 return (ns_name_pton2(src
, dst
, dstsiz
, NULL
));
213 * ns_name_pton2(src, dst, dstsiz, *dstlen)
214 * Convert a ascii string into an encoded domain name as per RFC1035.
217 * 1 if string was fully qualified
218 * 0 is string was not fully qualified
220 * fills in *dstlen (if non-NULL)
222 * Enforces label and domain length limits.
225 ns_name_pton2(const char *src
, u_char
*dst
, size_t dstsiz
, size_t *dstlen
) {
226 u_char
*label
, *bp
, *eom
;
227 int c
, n
, escaped
, e
= 0;
235 while ((c
= *src
++) != 0) {
237 if (c
== '[') { /*%< start a bit string label */
238 if ((cp
= strchr(src
, ']')) == NULL
) {
239 errno
= EINVAL
; /*%< ??? */
242 if ((e
= encode_bitsring(&src
, cp
+ 2,
250 if ((c
= *src
++) == 0)
258 else if ((cp
= strchr(digits
, c
)) != NULL
) {
259 n
= (cp
- digits
) * 100;
260 if ((c
= *src
++) == 0 ||
261 (cp
= strchr(digits
, c
)) == NULL
) {
265 n
+= (cp
- digits
) * 10;
266 if ((c
= *src
++) == 0 ||
267 (cp
= strchr(digits
, c
)) == NULL
) {
279 } else if (c
== '\\') {
282 } else if (c
== '.') {
283 c
= (bp
- label
- 1);
284 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
293 /* Fully qualified ? */
302 if ((bp
- dst
) > MAXCDNAME
) {
307 *dstlen
= (bp
- dst
);
310 if (c
== 0 || *src
== '.') {
323 c
= (bp
- label
- 1);
324 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
341 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
346 *dstlen
= (bp
- dst
);
351 * Convert a network strings labels into all lowercase.
354 *\li Number of bytes written to buffer, or -1 (with errno set)
357 *\li Enforces label and domain length limits.
361 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
377 while ((n
= *cp
++) != 0) {
378 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
379 /* Some kind of compression pointer. */
384 if ((l
= labellen(cp
- 1)) < 0) {
394 if (isascii(c
) && isupper(c
))
405 * Unpack a domain name from a message, source may be compressed.
408 *\li -1 if it fails, or consumed octets if it succeeds.
411 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
412 u_char
*dst
, size_t dstsiz
)
414 return (ns_name_unpack2(msg
, eom
, src
, dst
, dstsiz
, NULL
));
418 * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
419 * Unpack a domain name from a message, source may be compressed.
421 * -1 if it fails, or consumed octets if it succeeds.
423 * fills in *dstlen (if non-NULL).
426 ns_name_unpack2(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
427 u_char
*dst
, size_t dstsiz
, size_t *dstlen
)
429 const u_char
*srcp
, *dstlim
;
431 int n
, len
, checked
, l
;
437 dstlim
= dst
+ dstsiz
;
438 if (srcp
< msg
|| srcp
>= eom
) {
442 /* Fetch next label in domain name. */
443 while ((n
= *srcp
++) != 0) {
444 /* Check for indirection. */
445 switch (n
& NS_CMPRSFLGS
) {
449 if ((l
= labellen(srcp
- 1)) < 0) {
453 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
459 memcpy(dstp
, srcp
, (size_t)l
);
470 len
= srcp
- src
+ 1;
471 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
472 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
478 * Check for loops in the compressed name;
479 * if we've looked at the whole message,
480 * there must be a loop.
482 if (checked
>= eom
- msg
) {
490 return (-1); /*%< flag error */
495 *dstlen
= dstp
- dst
;
502 * Pack domain name 'domain' into 'comp_dn'.
505 *\li Size of the compressed name, or -1.
508 *\li 'dnptrs' is an array of pointers to previous compressed names.
509 *\li dnptrs[0] is a pointer to the beginning of the message. The array
511 *\li 'lastdnptr' is a pointer to the end of the array pointed to
515 *\li The list of pointers in dnptrs is updated for labels inserted into
516 * the message as we compress the name. If 'dnptr' is NULL, we don't
517 * try to compress names. If 'lastdnptr' is NULL, we don't update the
521 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
522 const u_char
**dnptrs
, const u_char
**lastdnptr
)
525 const u_char
**cpp
, **lpp
, *eob
, *msg
;
533 if (dnptrs
!= NULL
) {
534 if ((msg
= *dnptrs
++) != NULL
) {
535 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
537 lpp
= cpp
; /*%< end of list to search */
542 /* make sure the domain we are about to add is legal */
548 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
552 if ((l0
= labellen(srcp
)) < 0) {
564 /* from here on we need to reset compression pointer array on error */
567 /* Look to see if we can use pointers. */
569 if (n
!= 0 && msg
!= NULL
) {
570 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
571 (const u_char
* const *)lpp
);
573 if (dstp
+ 1 >= eob
) {
576 *dstp
++ = ((u_int32_t
)l
>> 8) | NS_CMPRSFLGS
;
580 /* Not found, save it. */
581 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
582 (dstp
- msg
) < 0x4000 && first
) {
588 /* copy label to buffer */
589 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
590 /* Should not happen. */
594 if (dstp
+ 1 + n
>= eob
) {
597 memcpy(dstp
, srcp
, (size_t)(n
+ 1));
613 * Expand compressed domain name to presentation format.
616 *\li Number of bytes read out of `src', or -1 (with errno set).
619 *\li Root domain returns as "." not "".
622 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
623 char *dst
, size_t dstsiz
)
625 u_char tmp
[NS_MAXCDNAME
];
628 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
630 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
636 * Compress a domain name into wire format, using compression pointers.
639 *\li Number of bytes consumed in `dst' or -1 (with errno set).
642 *\li 'dnptrs' is an array of pointers to previous compressed names.
643 *\li dnptrs[0] is a pointer to the beginning of the message.
644 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
645 * array pointed to by 'dnptrs'. Side effect is to update the list of
646 * pointers for labels inserted into the message as we compress the name.
647 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
648 * is NULL, we don't update the list.
651 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
652 const u_char
**dnptrs
, const u_char
**lastdnptr
)
654 u_char tmp
[NS_MAXCDNAME
];
656 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
658 return (ns_name_pack(tmp
, dst
, (int)dstsiz
, dnptrs
, lastdnptr
));
662 * Reset dnptrs so that there are no active references to pointers at or
666 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
667 const u_char
**lastdnptr
)
669 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
670 if (*dnptrs
>= src
) {
679 * Advance *ptrptr to skip over the compressed name it points at.
682 *\li 0 on success, -1 (with errno set) on failure.
685 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
692 while (cp
< eom
&& (n
= *cp
++) != 0) {
693 /* Check for indirection. */
694 switch (n
& NS_CMPRSFLGS
) {
695 case 0: /*%< normal case, n == len */
698 case NS_TYPE_ELT
: /*%< EDNS0 extended label */
699 if ((l
= labellen(cp
- 1)) < 0) {
700 errno
= EMSGSIZE
; /*%< XXX */
705 case NS_CMPRSFLGS
: /*%< indirection */
708 default: /*%< illegal type */
722 /* Find the number of octets an nname takes up, including the root label.
723 * (This is basically ns_name_skip() without compression-pointer support.)
724 * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
727 ns_name_length(ns_nname_ct nname
, size_t namesiz
) {
728 ns_nname_ct orig
= nname
;
731 while (namesiz
-- > 0 && (n
= *nname
++) != 0) {
732 if ((n
& NS_CMPRSFLGS
) != 0) {
743 return (nname
- orig
);
746 /* Compare two nname's for equality. Return -1 on error (setting errno).
749 ns_name_eq(ns_nname_ct a
, size_t as
, ns_nname_ct b
, size_t bs
) {
750 ns_nname_ct ae
= a
+ as
, be
= b
+ bs
;
753 while (ac
= *a
, bc
= *b
, ac
!= 0 && bc
!= 0) {
754 if ((ac
& NS_CMPRSFLGS
) != 0 || (bc
& NS_CMPRSFLGS
) != 0) {
758 if (a
+ ac
>= ae
|| b
+ bc
>= be
) {
762 if (ac
!= bc
|| strncasecmp((const char *) ++a
,
768 return (ac
== 0 && bc
== 0);
771 /* Is domain "A" owned by (at or below) domain "B"?
774 ns_name_owned(ns_namemap_ct a
, int an
, ns_namemap_ct b
, int bn
) {
775 /* If A is shorter, it cannot be owned by B. */
779 /* If they are unequal before the length of the shorter, A cannot... */
781 if (a
->len
!= b
->len
||
782 strncasecmp((const char *) a
->base
,
783 (const char *) b
->base
, (size_t)a
->len
) != 0)
789 /* A might be longer or not, but either way, B owns it. */
793 /* Build an array of <base,len> tuples from an nname, top-down order.
794 * Return the number of tuples (labels) thus discovered.
797 ns_name_map(ns_nname_ct nname
, size_t namelen
, ns_namemap_t map
, int mapsize
) {
806 /* Extra data follows name? */
814 /* Compression pointer? */
815 if ((n
& NS_CMPRSFLGS
) != 0) {
820 /* Label too long? */
826 /* Recurse to get rest of name done first. */
827 l
= ns_name_map(nname
+ n
, namelen
- n
, map
, mapsize
);
831 /* Too many labels? */
833 errno
= ENAMETOOLONG
;
837 /* We're on our way back up-stack, store current map data. */
843 /* Count the labels in a domain name. Root counts, so COM. has two. This
844 * is to make the result comparable to the result of ns_name_map().
847 ns_name_labels(ns_nname_ct nname
, size_t namesiz
) {
851 while (namesiz
-- > 0 && (n
= *nname
++) != 0) {
852 if ((n
& NS_CMPRSFLGS
) != 0) {
870 * Thinking in noninternationalized USASCII (per the DNS spec),
871 * is this characted special ("in need of quoting") ?
879 case 0x22: /*%< '"' */
880 case 0x2E: /*%< '.' */
881 case 0x3B: /*%< ';' */
882 case 0x5C: /*%< '\\' */
883 case 0x28: /*%< '(' */
884 case 0x29: /*%< ')' */
885 /* Special modifiers in zone files. */
886 case 0x40: /*%< '@' */
887 case 0x24: /*%< '$' */
895 * Thinking in noninternationalized USASCII (per the DNS spec),
896 * is this character visible and not a space when printed ?
903 return (ch
> 0x20 && ch
< 0x7f);
907 * Thinking in noninternationalized USASCII (per the DNS spec),
908 * convert this character to lower case if it's upper case.
912 if (ch
>= 0x41 && ch
<= 0x5A)
918 * Search for the counted-label name in an array of compressed names.
921 *\li offset from msg if found, or -1.
924 *\li dnptrs is the pointer to the first name on the list,
925 *\li not the pointer to the start of the message.
928 dn_find(const u_char
*domain
, const u_char
*msg
,
929 const u_char
* const *dnptrs
,
930 const u_char
* const *lastdnptr
)
932 const u_char
*dn
, *cp
, *sp
;
933 const u_char
* const *cpp
;
936 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
939 * terminate search on:
941 * compression pointer
944 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
945 (sp
- msg
) < 0x4000) {
948 while ((n
= *cp
++) != 0) {
950 * check for indirection
952 switch (n
& NS_CMPRSFLGS
) {
953 case 0: /*%< normal case, n == len */
954 n
= labellen(cp
- 1); /*%< XXX */
959 if (mklower(*dn
++) !=
962 /* Is next root for both ? */
963 if (*dn
== '\0' && *cp
== '\0')
968 case NS_CMPRSFLGS
: /*%< indirection */
969 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
972 default: /*%< illegal type */
986 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
988 const unsigned char *cp
= *cpp
;
990 int b
, blen
, plen
, i
;
992 if ((blen
= (*cp
& 0xff)) == 0)
994 plen
= (blen
+ 3) / 4;
995 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
996 if (dn
+ plen
>= eom
)
1000 i
= SPRINTF((dn
, "\\[x"));
1004 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
1005 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
1012 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
1018 i
= SPRINTF((dn
, "%1x",
1019 (((u_int32_t
)tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
1024 i
= SPRINTF((dn
, "/%d]", blen
));
1034 encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
1035 unsigned char ** dst
, unsigned const char *eom
)
1038 const char *cp
= *bp
;
1041 const char *beg_blen
;
1042 char *end_blen
= NULL
;
1043 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
1045 beg_blen
= end_blen
= NULL
;
1047 /* a bitstring must contain at least 2 characters */
1051 /* XXX: currently, only hex strings are supported */
1054 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
1057 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
1059 case ']': /*%< end of the bitstring */
1061 if (beg_blen
== NULL
)
1063 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
1064 if (*end_blen
!= ']')
1068 *tp
++ = ((value
<< 4) & 0xff);
1069 cp
++; /*%< skip ']' */
1076 if (!isdigit(c
&0xff))
1078 if (beg_blen
== NULL
) {
1081 /* blen never begings with 0 */
1087 if (!isxdigit(c
&0xff))
1090 value
+= digitvalue
[(int)c
];
1104 if (cp
>= end
|| tp
>= eom
)
1108 * bit length validation:
1109 * If a <length> is present, the number of digits in the <bit-data>
1110 * MUST be just sufficient to contain the number of bits specified
1111 * by the <length>. If there are insignificant bits in a final
1112 * hexadecimal or octal digit, they MUST be zero.
1113 * RFC2673, Section 3.2.
1118 if (((blen
+ 3) & ~3) != tbcount
)
1120 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
1121 if (((value
<< (8 - traillen
)) & 0xff) != 0)
1129 /* encode the type and the significant bit fields */
1130 **labelp
= DNS_LABELTYPE_BITSTRING
;
1140 labellen(const u_char
*lp
)
1145 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
1146 /* should be avoided by the caller */
1150 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
1151 if (l
== DNS_LABELTYPE_BITSTRING
) {
1152 if ((bitlen
= *(lp
+ 1)) == 0)
1154 return ((bitlen
+ 7 ) / 8 + 1);
1156 return (-1); /*%< unknwon ELT */