]>
git.saurik.com Git - apple/libresolv.git/blob - ns_name.c
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 static const char rcsid
[] = "$Id: ns_name.c,v 1.1 2006/03/01 19:01:36 majka Exp $";
25 #include "port_before.h"
28 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
41 #include "port_after.h"
45 # define SPRINTF(x) strlen(sprintf/**/x)
47 # define SPRINTF(x) ((size_t)sprintf x)
50 #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
51 #define DNS_LABELTYPE_BITSTRING 0x41
55 static const char digits
[] = "0123456789";
57 static const char digitvalue
[256] = {
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
61 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
62 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
64 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
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,
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, /*256*/
78 static int special(int);
79 static int printable(int);
80 static int dn_find(const u_char
*, const u_char
*,
81 const u_char
* const *,
82 const u_char
* const *);
83 static int encode_bitsring(const char **, const char *,
84 char **, char **, const char *);
85 static int labellen(const u_char
*);
86 static int decode_bitstring(const char **, char *, const char *);
91 * ns_name_ntop(src, dst, dstsiz)
92 * Convert an encoded domain name to printable ascii as per RFC1035.
94 * Number of bytes written to buffer, or -1 (with errno set)
96 * The root is returned as "."
97 * All other domains are returned in non absolute form
100 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
112 while ((n
= *cp
++) != 0) {
113 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
114 /* Some kind of compression pointer. */
125 if ((l
= labellen(cp
- 1)) < 0) {
126 errno
= EMSGSIZE
; /* XXX */
133 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
136 if (n
!= DNS_LABELTYPE_BITSTRING
) {
137 /* XXX: labellen should reject this case */
141 if ((m
= decode_bitstring((const char **)&cp
, dn
, eom
)) < 0)
149 for ((void)NULL
; l
> 0; l
--) {
158 } else if (!printable(c
)) {
164 *dn
++ = digits
[c
/ 100];
165 *dn
++ = digits
[(c
% 100) / 10];
166 *dn
++ = digits
[c
% 10];
192 * ns_name_pton(src, dst, dstsiz)
193 * Convert a ascii string into an encoded domain name as per RFC1035.
196 * 1 if string was fully qualified
197 * 0 is string was not fully qualified
199 * Enforces label and domain length limits.
203 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
205 u_char
*label
, *bp
, *eom
;
206 int c
, n
, escaped
, e
= 0;
214 while ((c
= *src
++) != 0) {
216 if (c
== '[') { /* start a bit string label */
217 if ((cp
= strchr(src
, ']')) == NULL
) {
218 errno
= EINVAL
; /* ??? */
221 if ((e
= encode_bitsring(&src
,
232 if ((c
= *src
++) == 0)
240 else if ((cp
= strchr(digits
, c
)) != NULL
) {
241 n
= (cp
- digits
) * 100;
242 if ((c
= *src
++) == 0 ||
243 (cp
= strchr(digits
, c
)) == NULL
) {
247 n
+= (cp
- digits
) * 10;
248 if ((c
= *src
++) == 0 ||
249 (cp
= strchr(digits
, c
)) == NULL
) {
261 } else if (c
== '\\') {
264 } else if (c
== '.') {
265 c
= (bp
- label
- 1);
266 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
275 /* Fully qualified ? */
284 if ((bp
- dst
) > NS_MAXCDNAME
) {
290 if (c
== 0 || *src
== '.') {
303 c
= (bp
- label
- 1);
304 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
321 if ((bp
- dst
) > NS_MAXCDNAME
) { /* src too big */
329 * ns_name_ntol(src, dst, dstsiz)
330 * Convert a network strings labels into all lowercase.
332 * Number of bytes written to buffer, or -1 (with errno set)
334 * Enforces label and domain length limits.
338 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
350 while ((n
= *cp
++) != 0) {
351 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
352 /* Some kind of compression pointer. */
357 if ((l
= labellen(cp
- 1)) < 0) {
365 for ((void)NULL
; l
> 0; l
--) {
378 * ns_name_unpack(msg, eom, src, dst, dstsiz)
379 * Unpack a domain name from a message, source may be compressed.
381 * -1 if it fails, or consumed octets if it succeeds.
384 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
385 u_char
*dst
, size_t dstsiz
)
387 const u_char
*srcp
, *dstlim
;
389 int n
, len
, checked
, l
;
395 dstlim
= dst
+ dstsiz
;
396 if (srcp
< msg
|| srcp
>= eom
) {
400 /* Fetch next label in domain name. */
401 while ((n
= *srcp
++) != 0) {
402 /* Check for indirection. */
403 switch (n
& NS_CMPRSFLGS
) {
407 if ((l
= labellen(srcp
- 1)) < 0) {
411 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
417 memcpy(dstp
, srcp
, l
);
428 len
= srcp
- src
+ 1;
429 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
430 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
436 * Check for loops in the compressed name;
437 * if we've looked at the whole message,
438 * there must be a loop.
440 if (checked
>= eom
- msg
) {
448 return (-1); /* flag error */
458 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
459 * Pack domain name 'domain' into 'comp_dn'.
461 * Size of the compressed name, or -1.
463 * 'dnptrs' is an array of pointers to previous compressed names.
464 * dnptrs[0] is a pointer to the beginning of the message. The array
466 * 'lastdnptr' is a pointer to the end of the array pointed to
469 * 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
) {
506 if ((l0
= labellen(srcp
)) < 0) {
511 if (l
> NS_MAXCDNAME
) {
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);
567 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
568 * Expand compressed domain name to presentation format.
570 * Number of bytes read out of `src', or -1 (with errno set).
572 * Root domain returns as "." not "".
575 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
576 char *dst
, size_t dstsiz
)
578 u_char tmp
[NS_MAXCDNAME
];
581 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
583 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
589 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
590 * Compress a domain name into wire format, using compression pointers.
592 * Number of bytes consumed in `dst' or -1 (with errno set).
594 * 'dnptrs' is an array of pointers to previous compressed names.
595 * dnptrs[0] is a pointer to the beginning of the message.
596 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
597 * array pointed to by 'dnptrs'. Side effect is to update the list of
598 * pointers for labels inserted into the message as we compress the name.
599 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
600 * is NULL, we don't update the list.
603 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
604 const u_char
**dnptrs
, const u_char
**lastdnptr
)
606 u_char tmp
[NS_MAXCDNAME
];
608 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
610 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
614 * Reset dnptrs so that there are no active references to pointers at or
618 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
619 const u_char
**lastdnptr
)
621 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
622 if (*dnptrs
>= src
) {
631 * ns_name_skip(ptrptr, eom)
632 * Advance *ptrptr to skip over the compressed name it points at.
634 * 0 on success, -1 (with errno set) on failure.
637 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
644 while (cp
< eom
&& (n
= *cp
++) != 0) {
645 /* Check for indirection. */
646 switch (n
& NS_CMPRSFLGS
) {
647 case 0: /* normal case, n == len */
650 case NS_TYPE_ELT
: /* EDNS0 extended label */
651 if ((l
= labellen(cp
- 1)) < 0) {
652 errno
= EMSGSIZE
; /* XXX */
657 case NS_CMPRSFLGS
: /* indirection */
660 default: /* illegal type */
678 * Thinking in noninternationalized USASCII (per the DNS spec),
679 * is this characted special ("in need of quoting") ?
689 case 0x5C: /* '\\' */
692 /* Special modifiers in zone files. */
703 * Thinking in noninternationalized USASCII (per the DNS spec),
704 * is this character visible and not a space when printed ?
710 return (ch
> 0x20 && ch
< 0x7f);
714 * Thinking in noninternationalized USASCII (per the DNS spec),
715 * convert this character to lower case if it's upper case.
719 if (ch
>= 0x41 && ch
<= 0x5A)
725 * dn_find(domain, msg, dnptrs, lastdnptr)
726 * Search for the counted-label name in an array of compressed names.
728 * offset from msg if found, or -1.
730 * dnptrs is the pointer to the first name on the list,
731 * not the pointer to the start of the message.
734 dn_find(const u_char
*domain
, const u_char
*msg
,
735 const u_char
* const *dnptrs
,
736 const u_char
* const *lastdnptr
)
738 const u_char
*dn
, *cp
, *sp
;
739 const u_char
* const *cpp
;
742 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
745 * terminate search on:
747 * compression pointer
750 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
751 (sp
- msg
) < 0x4000) {
754 while ((n
= *cp
++) != 0) {
756 * check for indirection
758 switch (n
& NS_CMPRSFLGS
) {
759 case 0: /* normal case, n == len */
760 n
= labellen(cp
- 1); /* XXX */
765 for ((void)NULL
; n
> 0; n
--)
766 if (mklower(*dn
++) !=
769 /* Is next root for both ? */
770 if (*dn
== '\0' && *cp
== '\0')
775 case NS_CMPRSFLGS
: /* indirection */
776 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
779 default: /* illegal type */
793 decode_bitstring(const char **cpp
, char *dn
, const char *eom
)
795 const char *cp
= *cpp
;
799 if ((blen
= (*cp
& 0xff)) == 0)
801 plen
= (blen
+ 3) / 4;
802 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
803 if (dn
+ plen
>= eom
)
807 dn
+= SPRINTF((dn
, "\\[x"));
808 for (b
= blen
; b
> 7; b
-= 8, cp
++)
809 dn
+= SPRINTF((dn
, "%02x", *cp
& 0xff));
812 dn
+= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
815 dn
+= SPRINTF((dn
, "%1x",
816 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
818 dn
+= SPRINTF((dn
, "/%d]", blen
));
825 encode_bitsring(const char **bp
, const char *end
, char **labelp
,
826 char ** dst
, const char *eom
)
829 const char *cp
= *bp
;
831 const char *beg_blen
;
832 char *end_blen
= NULL
;
833 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
835 beg_blen
= end_blen
= NULL
;
837 /* a bitstring must contain at least 2 characters */
841 /* XXX: currently, only hex strings are supported */
844 if (!isxdigit((*cp
) & 0xff)) /* reject '\[x/BLEN]' */
847 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
849 case ']': /* end of the bitstring */
851 if (beg_blen
== NULL
)
853 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
854 if (*end_blen
!= ']')
858 *tp
++ = ((value
<< 4) & 0xff);
866 if (!isdigit(c
&0xff))
868 if (beg_blen
== NULL
) {
871 /* blen never begings with 0 */
877 if (!isxdigit(c
&0xff))
880 value
+= digitvalue
[(int)c
];
894 if (cp
>= end
|| tp
>= eom
)
898 * bit length validation:
899 * If a <length> is present, the number of digits in the <bit-data>
900 * MUST be just sufficient to contain the number of bits specified
901 * by the <length>. If there are insignificant bits in a final
902 * hexadecimal or octal digit, they MUST be zero.
903 * RFC 2673, Section 3.2.
908 if (((blen
+ 3) & ~3) != tbcount
)
910 traillen
= tbcount
- blen
; /* between 0 and 3 */
911 if (((value
<< (8 - traillen
)) & 0xff) != 0)
919 /* encode the type and the significant bit fields */
920 **labelp
= DNS_LABELTYPE_BITSTRING
;
930 labellen(const u_char
*lp
)
935 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
936 /* should be avoided by the caller */
940 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
941 if (l
== DNS_LABELTYPE_BITSTRING
) {
942 if ((bitlen
= *(lp
+ 1)) == 0)
944 return((bitlen
+ 7 ) / 8 + 1);
946 return(-1); /* unknwon ELT */