1 /* $NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 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.
21 * This version of this file is derived from Android 2.3 "Gingerbread",
22 * which contains uncredited changes by Android/Google developers. It has
23 * been modified in 2011 for use in the Android build of Mozilla Firefox by
24 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
25 * and Steve Workman <sjhworkman@gmail.com>).
26 * These changes are offered under the same license as the original NetBSD
27 * file, whose copyright and license are unchanged above.
30 #define ANDROID_CHANGES 1
31 #define MOZILLA_NECKO_EXCLUDE_CODE 1
33 #include <sys/cdefs.h>
36 static const char rcsid
[] = "Id: ns_name.c,v 1.3.2.4.4.2 2004/05/04 03:27:47 marka Exp";
38 __RCSID("$NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $");
42 #include <sys/types.h>
44 #include <netinet/in.h>
45 #include "arpa_nameser.h"
48 #ifdef ANDROID_CHANGES
49 #include "resolv_private.h"
59 # define SPRINTF(x) strlen(sprintf/**/x)
61 # define SPRINTF(x) ((size_t)sprintf x)
64 #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
65 #define DNS_LABELTYPE_BITSTRING 0x41
69 static const char digits
[] = "0123456789";
71 static const char digitvalue
[256] = {
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
75 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
76 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
77 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
78 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
80 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
81 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
82 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
83 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
84 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
85 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
86 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
87 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
92 static int special(int);
93 static int printable(int);
94 static int dn_find(const u_char
*, const u_char
*,
95 const u_char
* const *,
96 const u_char
* const *);
97 static int encode_bitsring(const char **, const char *,
98 unsigned char **, unsigned char **,
99 unsigned const char *);
100 static int labellen(const u_char
*);
101 static int decode_bitstring(const unsigned char **,
102 char *, const char *);
107 * ns_name_ntop(src, dst, dstsiz)
108 * Convert an encoded domain name to printable ascii as per RFC1035.
110 * Number of bytes written to buffer, or -1 (with errno set)
112 * The root is returned as "."
113 * All other domains are returned in non absolute form
116 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
128 while ((n
= *cp
++) != 0) {
129 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
130 /* Some kind of compression pointer. */
141 if ((l
= labellen(cp
- 1)) < 0) {
142 errno
= EMSGSIZE
; /* XXX */
149 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
152 if (n
!= DNS_LABELTYPE_BITSTRING
) {
153 /* XXX: labellen should reject this case */
157 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
174 } else if (!printable(c
)) {
180 *dn
++ = digits
[c
/ 100];
181 *dn
++ = digits
[(c
% 100) / 10];
182 *dn
++ = digits
[c
% 10];
208 * ns_name_pton(src, dst, dstsiz)
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 * Enforces label and domain length limits.
219 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
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
) {
303 if (c
== 0 || *src
== '.') {
316 c
= (bp
- label
- 1);
317 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
334 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
341 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
343 * ns_name_ntol(src, dst, dstsiz)
344 * Convert a network strings labels into all lowercase.
346 * Number of bytes written to buffer, or -1 (with errno set)
348 * Enforces label and domain length limits.
352 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
368 while ((n
= *cp
++) != 0) {
369 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
370 /* Some kind of compression pointer. */
375 if ((l
= labellen(cp
- 1)) < 0) {
397 * ns_name_unpack(msg, eom, src, dst, dstsiz)
398 * Unpack a domain name from a message, source may be compressed.
400 * -1 if it fails, or consumed octets if it succeeds.
403 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
404 u_char
*dst
, size_t dstsiz
)
406 const u_char
*srcp
, *dstlim
;
408 int n
, len
, checked
, l
;
414 dstlim
= dst
+ dstsiz
;
415 if (srcp
< msg
|| srcp
>= eom
) {
419 /* Fetch next label in domain name. */
420 while ((n
= *srcp
++) != 0) {
421 /* Check for indirection. */
422 switch (n
& NS_CMPRSFLGS
) {
426 if ((l
= labellen(srcp
- 1)) < 0) {
430 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
436 memcpy(dstp
, srcp
, (size_t)l
);
447 len
= srcp
- src
+ 1;
448 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
449 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
455 * Check for loops in the compressed name;
456 * if we've looked at the whole message,
457 * there must be a loop.
459 if (checked
>= eom
- msg
) {
467 return (-1); /* flag error */
477 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
478 * Pack domain name 'domain' into 'comp_dn'.
480 * Size of the compressed name, or -1.
482 * 'dnptrs' is an array of pointers to previous compressed names.
483 * dnptrs[0] is a pointer to the beginning of the message. The array
485 * 'lastdnptr' is a pointer to the end of the array pointed to
488 * The list of pointers in dnptrs is updated for labels inserted into
489 * the message as we compress the name. If 'dnptr' is NULL, we don't
490 * try to compress names. If 'lastdnptr' is NULL, we don't update the
494 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
495 const u_char
**dnptrs
, const u_char
**lastdnptr
)
498 const u_char
**cpp
, **lpp
, *eob
, *msg
;
506 if (dnptrs
!= NULL
) {
507 if ((msg
= *dnptrs
++) != NULL
) {
508 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
510 lpp
= cpp
; /* end of list to search */
515 /* make sure the domain we are about to add is legal */
521 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
525 if ((l0
= labellen(srcp
)) < 0) {
537 /* from here on we need to reset compression pointer array on error */
540 /* Look to see if we can use pointers. */
542 if (n
!= 0 && msg
!= NULL
) {
543 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
544 (const u_char
* const *)lpp
);
546 if (dstp
+ 1 >= eob
) {
549 *dstp
++ = ((u_int32_t
)l
>> 8) | NS_CMPRSFLGS
;
553 /* Not found, save it. */
554 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
555 (dstp
- msg
) < 0x4000 && first
) {
561 /* copy label to buffer */
562 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
563 /* Should not happen. */
567 if (dstp
+ 1 + n
>= eob
) {
570 memcpy(dstp
, srcp
, (size_t)(n
+ 1));
586 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
587 * Expand compressed domain name to presentation format.
589 * Number of bytes read out of `src', or -1 (with errno set).
591 * Root domain returns as "." not "".
594 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
595 char *dst
, size_t dstsiz
)
597 u_char tmp
[NS_MAXCDNAME
];
600 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
602 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
608 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
609 * Compress a domain name into wire format, using compression pointers.
611 * Number of bytes consumed in `dst' or -1 (with errno set).
613 * 'dnptrs' is an array of pointers to previous compressed names.
614 * dnptrs[0] is a pointer to the beginning of the message.
615 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
616 * array pointed to by 'dnptrs'. Side effect is to update the list of
617 * pointers for labels inserted into the message as we compress the name.
618 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
619 * is NULL, we don't update the list.
622 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
623 const u_char
**dnptrs
, const u_char
**lastdnptr
)
625 u_char tmp
[NS_MAXCDNAME
];
627 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
629 return (ns_name_pack(tmp
, dst
, (int)dstsiz
, dnptrs
, lastdnptr
));
632 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
634 * Reset dnptrs so that there are no active references to pointers at or
638 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
639 const u_char
**lastdnptr
)
641 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
642 if (*dnptrs
>= src
) {
652 * ns_name_skip(ptrptr, eom)
653 * Advance *ptrptr to skip over the compressed name it points at.
655 * 0 on success, -1 (with errno set) on failure.
658 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
665 while (cp
< eom
&& (n
= *cp
++) != 0) {
666 /* Check for indirection. */
667 switch (n
& NS_CMPRSFLGS
) {
668 case 0: /* normal case, n == len */
671 case NS_TYPE_ELT
: /* EDNS0 extended label */
672 if ((l
= labellen(cp
- 1)) < 0) {
673 errno
= EMSGSIZE
; /* XXX */
678 case NS_CMPRSFLGS
: /* indirection */
681 default: /* illegal type */
699 * Thinking in noninternationalized USASCII (per the DNS spec),
700 * is this characted special ("in need of quoting") ?
710 case 0x5C: /* '\\' */
713 /* Special modifiers in zone files. */
724 * Thinking in noninternationalized USASCII (per the DNS spec),
725 * is this character visible and not a space when printed ?
731 return (ch
> 0x20 && ch
< 0x7f);
735 * Thinking in noninternationalized USASCII (per the DNS spec),
736 * convert this character to lower case if it's upper case.
740 if (ch
>= 0x41 && ch
<= 0x5A)
746 * dn_find(domain, msg, dnptrs, lastdnptr)
747 * Search for the counted-label name in an array of compressed names.
749 * offset from msg if found, or -1.
751 * dnptrs is the pointer to the first name on the list,
752 * not the pointer to the start of the message.
755 dn_find(const u_char
*domain
, const u_char
*msg
,
756 const u_char
* const *dnptrs
,
757 const u_char
* const *lastdnptr
)
759 const u_char
*dn
, *cp
, *sp
;
760 const u_char
* const *cpp
;
763 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
766 * terminate search on:
768 * compression pointer
771 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
772 (sp
- msg
) < 0x4000) {
775 while ((n
= *cp
++) != 0) {
777 * check for indirection
779 switch (n
& NS_CMPRSFLGS
) {
780 case 0: /* normal case, n == len */
781 n
= labellen(cp
- 1); /* XXX */
787 if (mklower(*dn
++) !=
790 /* Is next root for both ? */
791 if (*dn
== '\0' && *cp
== '\0')
796 case NS_CMPRSFLGS
: /* indirection */
797 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
800 default: /* illegal type */
814 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
816 const unsigned char *cp
= *cpp
;
818 int b
, blen
, plen
, i
;
820 if ((blen
= (*cp
& 0xff)) == 0)
822 plen
= (blen
+ 3) / 4;
823 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
824 if (dn
+ plen
>= eom
)
828 i
= SPRINTF((dn
, "\\[x"));
832 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
833 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
840 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
846 i
= SPRINTF((dn
, "%1x",
847 (((u_int32_t
)tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
852 i
= SPRINTF((dn
, "/%d]", blen
));
862 encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
863 unsigned char ** dst
, unsigned const char *eom
)
866 const char *cp
= *bp
;
869 const char *beg_blen
;
870 char *end_blen
= NULL
;
871 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
873 beg_blen
= end_blen
= NULL
;
875 /* a bitstring must contain at least 2 characters */
879 /* XXX: currently, only hex strings are supported */
882 if (!isxdigit((*cp
) & 0xff)) /* reject '\[x/BLEN]' */
885 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
887 case ']': /* end of the bitstring */
889 if (beg_blen
== NULL
)
891 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
892 if (*end_blen
!= ']')
896 *tp
++ = ((value
<< 4) & 0xff);
904 if (!isdigit(c
&0xff))
906 if (beg_blen
== NULL
) {
909 /* blen never begings with 0 */
915 if (!isxdigit(c
&0xff))
918 value
+= digitvalue
[(int)c
];
932 if (cp
>= end
|| tp
>= eom
)
936 * bit length validation:
937 * If a <length> is present, the number of digits in the <bit-data>
938 * MUST be just sufficient to contain the number of bits specified
939 * by the <length>. If there are insignificant bits in a final
940 * hexadecimal or octal digit, they MUST be zero.
941 * RFC 2673, Section 3.2.
946 if (((blen
+ 3) & ~3) != tbcount
)
948 traillen
= tbcount
- blen
; /* between 0 and 3 */
949 if (((value
<< (8 - traillen
)) & 0xff) != 0)
957 /* encode the type and the significant bit fields */
958 **labelp
= DNS_LABELTYPE_BITSTRING
;
968 labellen(const u_char
*lp
)
973 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
974 /* should be avoided by the caller */
978 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
979 if (l
== DNS_LABELTYPE_BITSTRING
) {
980 if ((bitlen
= *(lp
+ 1)) == 0)
982 return((bitlen
+ 7 ) / 8 + 1);
984 return(-1); /* unknwon ELT */