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.
18 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
30 # define SPRINTF(x) ((size_t)sprintf x)
32 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
33 #define DNS_LABELTYPE_BITSTRING 0x41
37 static const char digits
[] = "0123456789";
39 static const char digitvalue
[256] = {
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
43 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
44 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
46 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
60 static int special(int);
61 static int printable(int);
62 static int dn_find(const u_char
*, const u_char
*,
63 const u_char
* const *,
64 const u_char
* const *);
65 static int encode_bitstring(const char **, const char *,
66 unsigned char **, unsigned char **,
67 unsigned const char *);
68 static int labellen(const u_char
*);
69 static int decode_bitstring(const unsigned char **,
70 char *, const char *);
75 * Convert an encoded domain name to printable ascii as per RFC1035.
78 *\li Number of bytes written to buffer, or -1 (with errno set)
81 *\li The root is returned as "."
82 *\li All other domains are returned in non absolute form
85 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
97 while ((n
= *cp
++) != 0) {
98 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
99 /* Some kind of compression pointer. */
100 __set_errno (EMSGSIZE
);
105 __set_errno (EMSGSIZE
);
110 if ((l
= labellen(cp
- 1)) < 0) {
111 __set_errno (EMSGSIZE
);
115 __set_errno (EMSGSIZE
);
118 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
121 if (n
!= DNS_LABELTYPE_BITSTRING
) {
122 /* XXX: labellen should reject this case */
123 __set_errno (EINVAL
);
126 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
128 __set_errno (EMSGSIZE
);
134 for ((void)NULL
; l
> 0; l
--) {
138 __set_errno (EMSGSIZE
);
143 } else if (!printable(c
)) {
145 __set_errno (EMSGSIZE
);
149 *dn
++ = digits
[c
/ 100];
150 *dn
++ = digits
[(c
% 100) / 10];
151 *dn
++ = digits
[c
% 10];
154 __set_errno (EMSGSIZE
);
163 __set_errno (EMSGSIZE
);
169 __set_errno (EMSGSIZE
);
175 libresolv_hidden_def (ns_name_ntop
)
176 strong_alias (ns_name_ntop
, __ns_name_ntop
)
179 * Convert an ascii string into an encoded domain name as per RFC1035.
184 *\li 1 if string was fully qualified
185 *\li 0 is string was not fully qualified
188 *\li Enforces label and domain length limits.
192 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
194 u_char
*label
, *bp
, *eom
;
195 int c
, n
, escaped
, e
= 0;
203 while ((c
= *src
++) != 0) {
205 if (c
== '[') { /*%< start a bit string label */
206 if ((cp
= strchr(src
, ']')) == NULL
) {
207 __set_errno (EINVAL
);
210 if ((e
= encode_bitstring(&src
, cp
+ 2,
218 if ((c
= *src
++) == 0)
221 __set_errno (EINVAL
);
226 else if ((cp
= strchr(digits
, c
)) != NULL
) {
227 n
= (cp
- digits
) * 100;
228 if ((c
= *src
++) == 0 ||
229 (cp
= strchr(digits
, c
)) == NULL
) {
230 __set_errno (EMSGSIZE
);
233 n
+= (cp
- digits
) * 10;
234 if ((c
= *src
++) == 0 ||
235 (cp
= strchr(digits
, c
)) == NULL
) {
236 __set_errno (EMSGSIZE
);
241 __set_errno (EMSGSIZE
);
247 } else if (c
== '\\') {
250 } else if (c
== '.') {
251 c
= (bp
- label
- 1);
252 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
253 __set_errno (EMSGSIZE
);
257 __set_errno (EMSGSIZE
);
261 /* Fully qualified ? */
265 __set_errno (EMSGSIZE
);
270 if ((bp
- dst
) > MAXCDNAME
) {
271 __set_errno (EMSGSIZE
);
276 if (c
== 0 || *src
== '.') {
277 __set_errno (EMSGSIZE
);
284 __set_errno (EMSGSIZE
);
289 c
= (bp
- label
- 1);
290 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
291 __set_errno (EMSGSIZE
);
296 __set_errno (EMSGSIZE
);
302 __set_errno (EMSGSIZE
);
307 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
308 __set_errno (EMSGSIZE
);
313 libresolv_hidden_def (ns_name_pton
)
316 * Convert a network strings labels into all lowercase.
319 *\li Number of bytes written to buffer, or -1 (with errno set)
322 *\li Enforces label and domain length limits.
326 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
339 __set_errno (EMSGSIZE
);
342 while ((n
= *cp
++) != 0) {
343 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
344 /* Some kind of compression pointer. */
345 __set_errno (EMSGSIZE
);
349 if ((l
= labellen(cp
- 1)) < 0) {
350 __set_errno (EMSGSIZE
);
354 __set_errno (EMSGSIZE
);
357 for ((void)NULL
; l
> 0; l
--) {
370 * Unpack a domain name from a message, source may be compressed.
373 *\li -1 if it fails, or consumed octets if it succeeds.
376 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
377 u_char
*dst
, size_t dstsiz
)
379 const u_char
*srcp
, *dstlim
;
381 int n
, len
, checked
, l
;
387 dstlim
= dst
+ dstsiz
;
388 if (srcp
< msg
|| srcp
>= eom
) {
389 __set_errno (EMSGSIZE
);
392 /* Fetch next label in domain name. */
393 while ((n
= *srcp
++) != 0) {
394 /* Check for indirection. */
395 switch (n
& NS_CMPRSFLGS
) {
399 if ((l
= labellen(srcp
- 1)) < 0) {
400 __set_errno (EMSGSIZE
);
403 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
404 __set_errno (EMSGSIZE
);
409 memcpy(dstp
, srcp
, l
);
416 __set_errno (EMSGSIZE
);
420 len
= srcp
- src
+ 1;
421 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
422 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
423 __set_errno (EMSGSIZE
);
428 * Check for loops in the compressed name;
429 * if we've looked at the whole message,
430 * there must be a loop.
432 if (checked
>= eom
- msg
) {
433 __set_errno (EMSGSIZE
);
439 __set_errno (EMSGSIZE
);
440 return (-1); /*%< flag error */
448 libresolv_hidden_def (ns_name_unpack
)
449 strong_alias (ns_name_unpack
, __ns_name_unpack
)
452 * Pack domain name 'domain' into 'comp_dn'.
455 *\li Size of the compressed name, or -1.
458 *\li 'dnptrs' is an array of pointers to previous compressed names.
459 *\li dnptrs[0] is a pointer to the beginning of the message. The array
461 *\li 'lastdnptr' is a pointer to the end of the array pointed to
465 *\li The list of pointers in dnptrs is updated for labels inserted into
466 * the message as we compress the name. If 'dnptr' is NULL, we don't
467 * try to compress names. If 'lastdnptr' is NULL, we don't update the
471 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
472 const u_char
**dnptrs
, const u_char
**lastdnptr
)
475 const u_char
**cpp
, **lpp
, *eob
, *msg
;
483 if (dnptrs
!= NULL
) {
484 if ((msg
= *dnptrs
++) != NULL
) {
485 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
487 lpp
= cpp
; /*%< end of list to search */
492 /* make sure the domain we are about to add is legal */
498 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
499 __set_errno (EMSGSIZE
);
502 if ((l0
= labellen(srcp
)) < 0) {
503 __set_errno (EINVAL
);
508 __set_errno (EMSGSIZE
);
514 /* from here on we need to reset compression pointer array on error */
517 /* Look to see if we can use pointers. */
519 if (n
!= 0 && msg
!= NULL
) {
520 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
521 (const u_char
* const *)lpp
);
523 if (dstp
+ 1 >= eob
) {
526 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
530 /* Not found, save it. */
531 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
532 (dstp
- msg
) < 0x4000 && first
) {
538 /* copy label to buffer */
539 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
540 /* Should not happen. */
544 if (dstp
+ 1 + n
>= eob
) {
547 memcpy(dstp
, srcp
, n
+ 1);
556 __set_errno (EMSGSIZE
);
561 libresolv_hidden_def (ns_name_pack
)
564 * Expand compressed domain name to presentation format.
567 *\li Number of bytes read out of `src', or -1 (with errno set).
570 *\li Root domain returns as "." not "".
573 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
574 char *dst
, size_t dstsiz
)
576 u_char tmp
[NS_MAXCDNAME
];
579 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
581 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
585 libresolv_hidden_def (ns_name_uncompress
)
588 * Compress a domain name into wire format, using compression pointers.
591 *\li Number of bytes consumed in `dst' or -1 (with errno set).
594 *\li 'dnptrs' is an array of pointers to previous compressed names.
595 *\li dnptrs[0] is a pointer to the beginning of the message.
596 *\li 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 *\li 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
));
612 libresolv_hidden_def (ns_name_compress
)
615 * Reset dnptrs so that there are no active references to pointers at or
619 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
620 const u_char
**lastdnptr
)
622 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
623 if (*dnptrs
>= src
) {
632 * Advance *ptrptr to skip over the compressed name it points at.
635 *\li 0 on success, -1 (with errno set) on failure.
638 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
645 while (cp
< eom
&& (n
= *cp
++) != 0) {
646 /* Check for indirection. */
647 switch (n
& NS_CMPRSFLGS
) {
648 case 0: /*%< normal case, n == len */
651 case NS_TYPE_ELT
: /*%< EDNS0 extended label */
652 if ((l
= labellen(cp
- 1)) < 0) {
653 __set_errno (EMSGSIZE
);
658 case NS_CMPRSFLGS
: /*%< indirection */
661 default: /*%< illegal type */
662 __set_errno (EMSGSIZE
);
668 __set_errno (EMSGSIZE
);
674 libresolv_hidden_def (ns_name_skip
)
679 * Thinking in noninternationalized USASCII (per the DNS spec),
680 * is this character special ("in need of quoting") ?
688 case 0x22: /*%< '"' */
689 case 0x2E: /*%< '.' */
690 case 0x3B: /*%< ';' */
691 case 0x5C: /*%< '\\' */
692 case 0x28: /*%< '(' */
693 case 0x29: /*%< ')' */
694 /* Special modifiers in zone files. */
695 case 0x40: /*%< '@' */
696 case 0x24: /*%< '$' */
704 * Thinking in noninternationalized USASCII (per the DNS spec),
705 * is this character visible and not a space when printed ?
712 return (ch
> 0x20 && ch
< 0x7f);
716 * Thinking in noninternationalized USASCII (per the DNS spec),
717 * convert this character to lower case if it's upper case.
721 if (ch
>= 0x41 && ch
<= 0x5A)
727 * Search for the counted-label name in an array of compressed names.
730 *\li offset from msg if found, or -1.
733 *\li dnptrs is the pointer to the first name on the list,
734 *\li not the pointer to the start of the message.
737 dn_find(const u_char
*domain
, const u_char
*msg
,
738 const u_char
* const *dnptrs
,
739 const u_char
* const *lastdnptr
)
741 const u_char
*dn
, *cp
, *sp
;
742 const u_char
* const *cpp
;
745 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
748 * terminate search on:
750 * compression pointer
753 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
754 (sp
- msg
) < 0x4000) {
757 while ((n
= *cp
++) != 0) {
759 * check for indirection
761 switch (n
& NS_CMPRSFLGS
) {
762 case 0: /*%< normal case, n == len */
763 n
= labellen(cp
- 1); /*%< XXX */
767 for ((void)NULL
; n
> 0; n
--)
768 if (mklower(*dn
++) !=
771 /* Is next root for both ? */
772 if (*dn
== '\0' && *cp
== '\0')
777 case NS_CMPRSFLGS
: /*%< indirection */
778 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
781 default: /*%< illegal type */
782 __set_errno (EMSGSIZE
);
790 __set_errno (ENOENT
);
795 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
797 const unsigned char *cp
= *cpp
;
799 int b
, blen
, plen
, i
;
801 if ((blen
= (*cp
& 0xff)) == 0)
803 plen
= (blen
+ 3) / 4;
804 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
805 if (dn
+ plen
>= eom
)
809 i
= SPRINTF((dn
, "\\[x"));
813 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
814 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
821 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
827 i
= SPRINTF((dn
, "%1x",
828 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
833 i
= SPRINTF((dn
, "/%d]", blen
));
843 encode_bitstring(const char **bp
, const char *end
, unsigned char **labelp
,
844 unsigned char ** dst
, unsigned const char *eom
)
847 const char *cp
= *bp
;
850 const char *beg_blen
;
851 char *end_blen
= NULL
;
852 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
854 beg_blen
= end_blen
= NULL
;
856 /* a bitstring must contain at least 2 characters */
860 /* XXX: currently, only hex strings are supported */
863 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
866 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
868 case ']': /*%< end of the bitstring */
870 if (beg_blen
== NULL
)
872 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
873 if (*end_blen
!= ']')
877 *tp
++ = ((value
<< 4) & 0xff);
878 cp
++; /*%< skip ']' */
885 if (!isdigit(c
&0xff))
887 if (beg_blen
== NULL
) {
890 /* blen never begings with 0 */
896 if (!isxdigit(c
&0xff))
899 value
+= digitvalue
[(int)c
];
913 if (cp
>= end
|| tp
>= eom
)
917 * bit length validation:
918 * If a <length> is present, the number of digits in the <bit-data>
919 * MUST be just sufficient to contain the number of bits specified
920 * by the <length>. If there are insignificant bits in a final
921 * hexadecimal or octal digit, they MUST be zero.
922 * RFC2673, Section 3.2.
927 if (((blen
+ 3) & ~3) != tbcount
)
929 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
930 if (((value
<< (8 - traillen
)) & 0xff) != 0)
938 /* encode the type and the significant bit fields */
939 **labelp
= DNS_LABELTYPE_BITSTRING
;
949 labellen(const u_char
*lp
)
954 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
955 /* should be avoided by the caller */
959 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
960 if (l
== DNS_LABELTYPE_BITSTRING
) {
961 if ((bitlen
= *(lp
+ 1)) == 0)
963 return((bitlen
+ 7 ) / 8 + 1);
965 return(-1); /*%< unknwon ELT */