1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/nameser.h>
27 #include "ares_private.h" /* for the memdebug */
29 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
32 /* Expand an RFC1035-encoded domain name given by encoded. The
33 * containing message is given by abuf and alen. The result given by
34 * *s, which is set to a NUL-terminated allocated buffer. *enclen is
35 * set to the length of the encoded name (not the length of the
36 * expanded name; the goal is to tell the caller how many bytes to
37 * move forward to get past the encoded name).
39 * In the simple case, an encoded name is a series of labels, each
40 * composed of a one-byte length (limited to values between 0 and 63
41 * inclusive) followed by the label contents. The name is terminated
42 * by a zero-length label.
44 * In the more complicated case, a label may be terminated by an
45 * indirection pointer, specified by two bytes with the high bits of
46 * the first byte (corresponding to INDIR_MASK) set to 11. With the
47 * two high bits of the first byte stripped off, the indirection
48 * pointer gives an offset from the beginning of the containing
49 * message with more labels to decode. Indirection can happen an
50 * arbitrary number of times, so we have to detect loops.
52 * Since the expanded name uses '.' as a label separator, we use
53 * backslashes to escape periods or backslashes in the expanded name.
56 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
57 int alen, char **s, long *enclen)
61 const unsigned char *p;
63 len = name_length(encoded, abuf, alen);
72 /* No error-checking necessary; it was all done by name_length(). */
76 if ((*p & INDIR_MASK) == INDIR_MASK)
80 *enclen = p + 2 - encoded;
83 p = abuf + ((*p & ~INDIR_MASK) << 8 | *(p + 1));
91 if (*p == '.' || *p == '\\')
100 *enclen = p + 1 - encoded;
102 /* Nuke the trailing period if we wrote one. */
109 /* Return the length of the expansion of an encoded domain name, or
110 * -1 if the encoding is invalid.
112 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
115 int n = 0, offset, indir = 0;
117 /* Allow the caller to pass us abuf + alen and have us check for it. */
118 if (encoded == abuf + alen)
123 if ((*encoded & INDIR_MASK) == INDIR_MASK)
125 /* Check the offset and go there. */
126 if (encoded + 1 >= abuf + alen)
128 offset = (*encoded & ~INDIR_MASK) << 8 | *(encoded + 1);
131 encoded = abuf + offset;
133 /* If we've seen more indirects than the message length,
134 * then there's a loop.
142 if (encoded + offset + 1 >= abuf + alen)
147 n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
154 /* If there were any labels at all, then the number of dots is one
155 * less than the number of labels, so subtract one.
157 return (n) ? n - 1 : n;