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