/[resiprocate]/main/contrib/ares/ares_expand_name.c
ViewVC logotype

Contents of /main/contrib/ares/ares_expand_name.c

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27