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