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.3.2.4.4.2 2004/05/04 03:27:47 marka 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 * ns_name_ntop(src, dst, dstsiz)
88 * Convert an encoded domain name to printable ascii as per RFC1035.
90 * Number of bytes written to buffer, or -1 (with errno set)
92 * The root is returned as "."
93 * All other domains are returned in non absolute form
96 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
108 while ((n = *cp++) != 0) {
109 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
110 /* Some kind of compression pointer. */
121 if ((l = labellen(cp - 1)) < 0) {
122 errno = EMSGSIZE; /* XXX */
129 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
132 if (n != DNS_LABELTYPE_BITSTRING) {
133 /* XXX: labellen should reject this case */
137 if ((m = decode_bitstring(&cp, dn, eom)) < 0)
145 for ((void)NULL; l > 0; l--) {
154 } else if (!printable(c)) {
160 *dn++ = digits[c / 100];
161 *dn++ = digits[(c % 100) / 10];
162 *dn++ = digits[c % 10];
188 * ns_name_pton(src, dst, dstsiz)
189 * Convert a ascii string into an encoded domain name as per RFC1035.
192 * 1 if string was fully qualified
193 * 0 is string was not fully qualified
195 * Enforces label and domain length limits.
199 ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
201 u_char *label, *bp, *eom;
202 int c, n, escaped, e = 0;
210 while ((c = *src++) != 0) {
212 if (c == '[') { /* start a bit string label */
213 if ((cp = strchr(src, ']')) == NULL) {
214 errno = EINVAL; /* ??? */
217 if ((e = encode_bitsring(&src, cp + 2,
225 if ((c = *src++) == 0)
233 else if ((cp = strchr(digits, c)) != NULL) {
234 n = (cp - digits) * 100;
235 if ((c = *src++) == 0 ||
236 (cp = strchr(digits, c)) == NULL) {
240 n += (cp - digits) * 10;
241 if ((c = *src++) == 0 ||
242 (cp = strchr(digits, c)) == NULL) {
254 } else if (c == '\\') {
257 } else if (c == '.') {
258 c = (bp - label - 1);
259 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
268 /* Fully qualified ? */
277 if ((bp - dst) > MAXCDNAME) {
283 if (c == 0 || *src == '.') {
296 c = (bp - label - 1);
297 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
314 if ((bp - dst) > MAXCDNAME) { /* src too big */
322 * ns_name_ntol(src, dst, dstsiz)
323 * Convert a network strings labels into all lowercase.
325 * Number of bytes written to buffer, or -1 (with errno set)
327 * Enforces label and domain length limits.
331 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
347 while ((n = *cp++) != 0) {
348 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
349 /* Some kind of compression pointer. */
354 if ((l = labellen(cp - 1)) < 0) {
362 for ((void)NULL; l > 0; l--) {
375 * ns_name_unpack(msg, eom, src, dst, dstsiz)
376 * Unpack a domain name from a message, source may be compressed.
378 * -1 if it fails, or consumed octets if it succeeds.
381 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
382 u_char *dst, size_t dstsiz)
384 const u_char *srcp, *dstlim;
386 int n, len, checked, l;
392 dstlim = dst + dstsiz;
393 if (srcp < msg || srcp >= eom) {
397 /* Fetch next label in domain name. */
398 while ((n = *srcp++) != 0) {
399 /* Check for indirection. */
400 switch (n & NS_CMPRSFLGS) {
404 if ((l = labellen(srcp - 1)) < 0) {
408 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
414 memcpy(dstp, srcp, l);
425 len = srcp - src + 1;
426 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
427 if (srcp < msg || srcp >= eom) { /* Out of range. */
433 * Check for loops in the compressed name;
434 * if we've looked at the whole message,
435 * there must be a loop.
437 if (checked >= eom - msg) {
445 return (-1); /* flag error */
455 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
456 * Pack domain name 'domain' into 'comp_dn'.
458 * Size of the compressed name, or -1.
460 * 'dnptrs' is an array of pointers to previous compressed names.
461 * dnptrs[0] is a pointer to the beginning of the message. The array
463 * 'lastdnptr' is a pointer to the end of the array pointed to
466 * The list of pointers in dnptrs is updated for labels inserted into
467 * the message as we compress the name. If 'dnptr' is NULL, we don't
468 * try to compress names. If 'lastdnptr' is NULL, we don't update the
472 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
473 const u_char **dnptrs, const u_char **lastdnptr)
476 const u_char **cpp, **lpp, *eob, *msg;
484 if (dnptrs != NULL) {
485 if ((msg = *dnptrs++) != NULL) {
486 for (cpp = dnptrs; *cpp != NULL; cpp++)
488 lpp = cpp; /* end of list to search */
493 /* make sure the domain we are about to add is legal */
499 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
503 if ((l0 = labellen(srcp)) < 0) {
515 /* from here on we need to reset compression pointer array on error */
518 /* Look to see if we can use pointers. */
520 if (n != 0 && msg != NULL) {
521 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
522 (const u_char * const *)lpp);
524 if (dstp + 1 >= eob) {
527 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
531 /* Not found, save it. */
532 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
533 (dstp - msg) < 0x4000 && first) {
539 /* copy label to buffer */
540 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
541 /* Should not happen. */
545 if (dstp + 1 + n >= eob) {
548 memcpy(dstp, srcp, n + 1);
564 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
565 * Expand compressed domain name to presentation format.
567 * Number of bytes read out of `src', or -1 (with errno set).
569 * Root domain returns as "." not "".
572 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
573 char *dst, size_t dstsiz)
575 u_char tmp[NS_MAXCDNAME];
578 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
580 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
586 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
587 * Compress a domain name into wire format, using compression pointers.
589 * Number of bytes consumed in `dst' or -1 (with errno set).
591 * 'dnptrs' is an array of pointers to previous compressed names.
592 * dnptrs[0] is a pointer to the beginning of the message.
593 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
594 * array pointed to by 'dnptrs'. Side effect is to update the list of
595 * pointers for labels inserted into the message as we compress the name.
596 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
597 * is NULL, we don't update the list.
600 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
601 const u_char **dnptrs, const u_char **lastdnptr)
603 u_char tmp[NS_MAXCDNAME];
605 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
607 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
611 * Reset dnptrs so that there are no active references to pointers at or
615 ns_name_rollback(const u_char *src, const u_char **dnptrs,
616 const u_char **lastdnptr)
618 while (dnptrs < lastdnptr && *dnptrs != NULL) {
619 if (*dnptrs >= src) {
628 * ns_name_skip(ptrptr, eom)
629 * Advance *ptrptr to skip over the compressed name it points at.
631 * 0 on success, -1 (with errno set) on failure.
634 ns_name_skip(const u_char **ptrptr, const u_char *eom)
641 while (cp < eom && (n = *cp++) != 0) {
642 /* Check for indirection. */
643 switch (n & NS_CMPRSFLGS) {
644 case 0: /* normal case, n == len */
647 case NS_TYPE_ELT: /* EDNS0 extended label */
648 if ((l = labellen(cp - 1)) < 0) {
649 errno = EMSGSIZE; /* XXX */
654 case NS_CMPRSFLGS: /* indirection */
657 default: /* illegal type */
675 * Thinking in noninternationalized USASCII (per the DNS spec),
676 * is this characted special ("in need of quoting") ?
686 case 0x5C: /* '\\' */
689 /* Special modifiers in zone files. */
700 * Thinking in noninternationalized USASCII (per the DNS spec),
701 * is this character visible and not a space when printed ?
707 return (ch > 0x20 && ch < 0x7f);
711 * Thinking in noninternationalized USASCII (per the DNS spec),
712 * convert this character to lower case if it's upper case.
716 if (ch >= 0x41 && ch <= 0x5A)
722 * dn_find(domain, msg, dnptrs, lastdnptr)
723 * Search for the counted-label name in an array of compressed names.
725 * offset from msg if found, or -1.
727 * dnptrs is the pointer to the first name on the list,
728 * not the pointer to the start of the message.
731 dn_find(const u_char *domain, const u_char *msg,
732 const u_char * const *dnptrs,
733 const u_char * const *lastdnptr)
735 const u_char *dn, *cp, *sp;
736 const u_char * const *cpp;
739 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
742 * terminate search on:
744 * compression pointer
747 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
748 (sp - msg) < 0x4000) {
751 while ((n = *cp++) != 0) {
753 * check for indirection
755 switch (n & NS_CMPRSFLGS) {
756 case 0: /* normal case, n == len */
757 n = labellen(cp - 1); /* XXX */
762 for ((void)NULL; n > 0; n--)
763 if (mklower(*dn++) !=
766 /* Is next root for both ? */
767 if (*dn == '\0' && *cp == '\0')
772 case NS_CMPRSFLGS: /* indirection */
773 cp = msg + (((n & 0x3f) << 8) | *cp);
776 default: /* illegal type */
790 decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
792 const unsigned char *cp = *cpp;
794 int b, blen, plen, i;
796 if ((blen = (*cp & 0xff)) == 0)
798 plen = (blen + 3) / 4;
799 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
800 if (dn + plen >= eom)
804 i = SPRINTF((dn, "\\[x"));
808 for (b = blen; b > 7; b -= 8, cp++) {
809 i = SPRINTF((dn, "%02x", *cp & 0xff));
816 i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
822 i = SPRINTF((dn, "%1x",
823 ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
828 i = SPRINTF((dn, "/%d]", blen));
838 encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
839 unsigned char ** dst, unsigned const char *eom)
842 const char *cp = *bp;
845 const char *beg_blen;
846 char *end_blen = NULL;
847 int value = 0, count = 0, tbcount = 0, blen = 0;
849 beg_blen = end_blen = NULL;
851 /* a bitstring must contain at least 2 characters */
855 /* XXX: currently, only hex strings are supported */
858 if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */
861 for (tp = *dst + 1; cp < end && tp < eom; cp++) {
863 case ']': /* end of the bitstring */
865 if (beg_blen == NULL)
867 blen = (int)strtol(beg_blen, &end_blen, 10);
868 if (*end_blen != ']')
872 *tp++ = ((value << 4) & 0xff);
880 if (!isdigit(c&0xff))
882 if (beg_blen == NULL) {
885 /* blen never begings with 0 */
891 if (!isxdigit(c&0xff))
894 value += digitvalue[(int)c];
908 if (cp >= end || tp >= eom)
912 * bit length validation:
913 * If a <length> is present, the number of digits in the <bit-data>
914 * MUST be just sufficient to contain the number of bits specified
915 * by the <length>. If there are insignificant bits in a final
916 * hexadecimal or octal digit, they MUST be zero.
917 * RFC 2673, Section 3.2.
922 if (((blen + 3) & ~3) != tbcount)
924 traillen = tbcount - blen; /* between 0 and 3 */
925 if (((value << (8 - traillen)) & 0xff) != 0)
933 /* encode the type and the significant bit fields */
934 **labelp = DNS_LABELTYPE_BITSTRING;
944 labellen(const u_char *lp)
949 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
950 /* should be avoided by the caller */
954 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
955 if (l == DNS_LABELTYPE_BITSTRING) {
956 if ((bitlen = *(lp + 1)) == 0)
958 return((bitlen + 7 ) / 8 + 1);
960 return(-1); /* unknwon ELT */