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