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