ares_init.c: fix compiler warning on winsock builds
[platform/upstream/c-ares.git] / ares_expand_name.c
1
2 /* Copyright 1998, 2011 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16
17 #include "ares_setup.h"
18
19 #ifdef HAVE_SYS_SOCKET_H
20 #  include <sys/socket.h>
21 #endif
22 #ifdef HAVE_NETINET_IN_H
23 #  include <netinet/in.h>
24 #endif
25 #ifdef HAVE_ARPA_NAMESER_H
26 #  include <arpa/nameser.h>
27 #else
28 #  include "nameser.h"
29 #endif
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #  include <arpa/nameser_compat.h>
32 #endif
33
34 #include <stdlib.h>
35 #include "ares.h"
36 #include "ares_nowarn.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
92     /* indirect root label (like 0xc0 0x0c) is 2 bytes long (stupid, but
93        valid) */
94     if ((*encoded & INDIR_MASK) == INDIR_MASK)
95       *enclen = 2L;
96     else
97       *enclen = 1L;  /* the caller should move one byte to get past this */
98
99     return ARES_SUCCESS;
100   }
101
102   /* No error-checking necessary; it was all done by name_length(). */
103   p = encoded;
104   while (*p)
105     {
106       if ((*p & INDIR_MASK) == INDIR_MASK)
107         {
108           if (!indir)
109             {
110               *enclen = aresx_uztosl(p + 2U - encoded);
111               indir = 1;
112             }
113           p = abuf + ((*p & ~INDIR_MASK) << 8 | *(p + 1));
114         }
115       else
116         {
117           len = *p;
118           p++;
119           while (len--)
120             {
121               if (*p == '.' || *p == '\\')
122                 *q++ = '\\';
123               *q++ = *p;
124               p++;
125             }
126           *q++ = '.';
127         }
128     }
129   if (!indir)
130     *enclen = aresx_uztosl(p + 1U - encoded);
131
132   /* Nuke the trailing period if we wrote one. */
133   if (q > *s)
134     *(q - 1) = 0;
135   else
136     *q = 0; /* zero terminate */
137
138   return ARES_SUCCESS;
139 }
140
141 /* Return the length of the expansion of an encoded domain name, or
142  * -1 if the encoding is invalid.
143  */
144 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
145                        int alen)
146 {
147   int n = 0, offset, indir = 0;
148
149   /* Allow the caller to pass us abuf + alen and have us check for it. */
150   if (encoded == abuf + alen)
151     return -1;
152
153   while (*encoded)
154     {
155       if ((*encoded & INDIR_MASK) == INDIR_MASK)
156         {
157           /* Check the offset and go there. */
158           if (encoded + 1 >= abuf + alen)
159             return -1;
160           offset = (*encoded & ~INDIR_MASK) << 8 | *(encoded + 1);
161           if (offset >= alen)
162             return -1;
163           encoded = abuf + offset;
164
165           /* If we've seen more indirects than the message length,
166            * then there's a loop.
167            */
168           if (++indir > alen)
169             return -1;
170         }
171       else
172         {
173           offset = *encoded;
174           if (encoded + offset + 1 >= abuf + alen)
175             return -1;
176           encoded++;
177           while (offset--)
178             {
179               n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
180               encoded++;
181             }
182           n++;
183         }
184     }
185
186   /* If there were any labels at all, then the number of dots is one
187    * less than the number of labels, so subtract one.
188    */
189   return (n) ? n - 1 : n;
190 }
191
192 /* Like ares_expand_name but returns EBADRESP in case of invalid input. */
193 int ares__expand_name_for_response(const unsigned char *encoded,
194                                    const unsigned char *abuf, int alen,
195                                    char **s, long *enclen)
196 {
197   int status = ares_expand_name(encoded, abuf, alen, s, enclen);
198   if (status == ARES_EBADNAME)
199     status = ARES_EBADRESP;
200   return status;
201 }