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