Added CVS id.
[platform/upstream/c-ares.git] / ares_expand_name.c
1 /* $Id: */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17
18 #include "setup.h"
19 #include <sys/types.h>
20
21 #if defined(WIN32) && !defined(WATT32)
22 #include "nameser.h"
23 #else
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
27 #include <arpa/nameser_compat.h>
28 #endif
29 #endif
30
31 #include <stdlib.h>
32 #include "ares.h"
33 #include "ares_private.h" /* for the memdebug */
34
35 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
36                        int alen);
37
38 /* Expand an RFC1035-encoded domain name given by encoded.  The
39  * containing message is given by abuf and alen.  The result given by
40  * *s, which is set to a NUL-terminated allocated buffer.  *enclen is
41  * set to the length of the encoded name (not the length of the
42  * expanded name; the goal is to tell the caller how many bytes to
43  * move forward to get past the encoded name).
44  *
45  * In the simple case, an encoded name is a series of labels, each
46  * composed of a one-byte length (limited to values between 0 and 63
47  * inclusive) followed by the label contents.  The name is terminated
48  * by a zero-length label.
49  *
50  * In the more complicated case, a label may be terminated by an
51  * indirection pointer, specified by two bytes with the high bits of
52  * the first byte (corresponding to INDIR_MASK) set to 11.  With the
53  * two high bits of the first byte stripped off, the indirection
54  * pointer gives an offset from the beginning of the containing
55  * message with more labels to decode.  Indirection can happen an
56  * arbitrary number of times, so we have to detect loops.
57  *
58  * Since the expanded name uses '.' as a label separator, we use
59  * backslashes to escape periods or backslashes in the expanded name.
60  */
61
62 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
63                      int alen, char **s, long *enclen)
64 {
65   int len, indir = 0;
66   char *q;
67   const unsigned char *p;
68
69   len = name_length(encoded, abuf, alen);
70   if (len == -1)
71     return ARES_EBADNAME;
72
73   *s = malloc(len + 1);
74   if (!*s)
75     return ARES_ENOMEM;
76   q = *s;
77
78   /* No error-checking necessary; it was all done by name_length(). */
79   p = encoded;
80   while (*p)
81     {
82       if ((*p & INDIR_MASK) == INDIR_MASK)
83         {
84           if (!indir)
85             {
86               *enclen = p + 2 - encoded;
87               indir = 1;
88             }
89           p = abuf + ((*p & ~INDIR_MASK) << 8 | *(p + 1));
90         }
91       else
92         {
93           len = *p;
94           p++;
95           while (len--)
96             {
97               if (*p == '.' || *p == '\\')
98                 *q++ = '\\';
99               *q++ = *p;
100               p++;
101             }
102           *q++ = '.';
103         }
104     }
105   if (!indir)
106     *enclen = p + 1 - encoded;
107
108   /* Nuke the trailing period if we wrote one. */
109   if (q > *s)
110     *(q - 1) = 0;
111   else
112     *q = 0; /* zero terminate */
113
114   return ARES_SUCCESS;
115 }
116
117 /* Return the length of the expansion of an encoded domain name, or
118  * -1 if the encoding is invalid.
119  */
120 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
121                        int alen)
122 {
123   int n = 0, offset, indir = 0;
124
125   /* Allow the caller to pass us abuf + alen and have us check for it. */
126   if (encoded == abuf + alen)
127     return -1;
128
129   while (*encoded)
130     {
131       if ((*encoded & INDIR_MASK) == INDIR_MASK)
132         {
133           /* Check the offset and go there. */
134           if (encoded + 1 >= abuf + alen)
135             return -1;
136           offset = (*encoded & ~INDIR_MASK) << 8 | *(encoded + 1);
137           if (offset >= alen)
138             return -1;
139           encoded = abuf + offset;
140
141           /* If we've seen more indirects than the message length,
142            * then there's a loop.
143            */
144           if (++indir > alen)
145             return -1;
146         }
147       else
148         {
149           offset = *encoded;
150           if (encoded + offset + 1 >= abuf + alen)
151             return -1;
152           encoded++;
153           while (offset--)
154             {
155               n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
156               encoded++;
157             }
158           n++;
159         }
160     }
161
162   /* If there were any labels at all, then the number of dots is one
163    * less than the number of labels, so subtract one.
164    */
165   return (n) ? n - 1 : n;
166 }