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>
28 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
31 /* Expand an RFC1035-encoded domain name given by encoded. The
32 * containing message is given by abuf and alen. The result given by
33 * *s, which is set to a NUL-terminated allocated buffer. *enclen is
34 * set to the length of the encoded name (not the length of the
35 * expanded name; the goal is to tell the caller how many bytes to
36 * move forward to get past the encoded name).
38 * In the simple case, an encoded name is a series of labels, each
39 * composed of a one-byte length (limited to values between 0 and 63
40 * inclusive) followed by the label contents. The name is terminated
41 * by a zero-length label.
43 * In the more complicated case, a label may be terminated by an
44 * indirection pointer, specified by two bytes with the high bits of
45 * the first byte (corresponding to INDIR_MASK) set to 11. With the
46 * two high bits of the first byte stripped off, the indirection
47 * pointer gives an offset from the beginning of the containing
48 * message with more labels to decode. Indirection can happen an
49 * arbitrary number of times, so we have to detect loops.
51 * Since the expanded name uses '.' as a label separator, we use
52 * backslashes to escape periods or backslashes in the expanded name.
55 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
56 int alen, char **s, int *enclen)
60 const unsigned char *p;
62 len = name_length(encoded, abuf, alen);
71 /* No error-checking necessary; it was all done by name_length(). */
75 if ((*p & INDIR_MASK) == INDIR_MASK)
79 *enclen = p + 2 - encoded;
82 p = abuf + ((*p & ~INDIR_MASK) << 8 | *(p + 1));
90 if (*p == '.' || *p == '\\')
99 *enclen = p + 1 - encoded;
101 /* Nuke the trailing period if we wrote one. */
108 /* Return the length of the expansion of an encoded domain name, or
109 * -1 if the encoding is invalid.
111 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
114 int n = 0, offset, indir = 0;
116 /* Allow the caller to pass us abuf + alen and have us check for it. */
117 if (encoded == abuf + alen)
122 if ((*encoded & INDIR_MASK) == INDIR_MASK)
124 /* Check the offset and go there. */
125 if (encoded + 1 >= abuf + alen)
127 offset = (*encoded & ~INDIR_MASK) << 8 | *(encoded + 1);
130 encoded = abuf + offset;
132 /* If we've seen more indirects than the message length,
133 * then there's a loop.
141 if (encoded + offset + 1 >= abuf + alen)
146 n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
153 /* If there were any labels at all, then the number of dots is one
154 * less than the number of labels, so subtract one.
156 return (n) ? n - 1 : n;