push b5232b2081a0e20e4bf07d6ded424d0101e4a589
[wine/hacks.git] / dlls / dnsapi / ns_name.c
blobde5fbdb14f63d29c85e72e0ce8435d4b545da813
1 /*
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 * SOFTWARE.
18 #include "config.h"
20 #include <sys/types.h>
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 #endif
29 #include <ctype.h>
30 #include <errno.h>
31 #ifdef HAVE_RESOLV_H
32 # include <resolv.h>
33 #endif
34 #include <string.h>
35 #include <ctype.h>
37 /* Data. */
39 static const char digits[] = "0123456789";
41 /* Forward. */
43 static int special(int);
44 static int printable(int);
46 /* Public. */
49 * dns_ns_name_ntop(src, dst, dstsiz)
50 * Convert an encoded domain name to printable ascii as per RFC1035.
51 * return:
52 * Number of bytes written to buffer, or -1 (with errno set)
53 * notes:
54 * The root is returned as "."
55 * All other domains are returned in non absolute form
57 static int
58 dns_ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
59 const u_char *cp;
60 char *dn, *eom;
61 u_char c;
62 u_int n;
64 cp = src;
65 dn = dst;
66 eom = dst + dstsiz;
68 while ((n = *cp++) != 0) {
69 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
70 /* Some kind of compression pointer. */
71 return (-1);
73 if (dn != dst) {
74 if (dn >= eom) {
75 return (-1);
77 *dn++ = '.';
80 if (n == 0x41) {
81 n = *cp++ / 8;
82 if (dn + n * 2 + 4 >= eom) {
83 return (-1);
85 *dn++ = '\\';
86 *dn++ = '[';
87 *dn++ = 'x';
89 while (n-- > 0) {
90 unsigned u;
91 c = *cp++;
92 u = c >> 4;
93 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
94 u = c & 0xf;
95 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
98 *dn++ = ']';
99 continue;
102 if (dn + n >= eom) {
103 return (-1);
105 for ((void)NULL; n > 0; n--) {
106 c = *cp++;
107 if (special(c)) {
108 if (dn + 1 >= eom) {
109 return (-1);
111 *dn++ = '\\';
112 *dn++ = (char)c;
113 } else if (!printable(c)) {
114 if (dn + 3 >= eom) {
115 return (-1);
117 *dn++ = '\\';
118 *dn++ = digits[c / 100];
119 *dn++ = digits[(c % 100) / 10];
120 *dn++ = digits[c % 10];
121 } else {
122 if (dn >= eom) {
123 return (-1);
125 *dn++ = (char)c;
129 if (dn == dst) {
130 if (dn >= eom) {
131 return (-1);
133 *dn++ = '.';
135 if (dn >= eom) {
136 return (-1);
138 *dn++ = '\0';
139 return (dn - dst);
143 * dns_ns_name_pton(src, dst, dstsiz)
144 * Convert a ascii string into an encoded domain name as per RFC1035.
145 * return:
146 * -1 if it fails
147 * 1 if string was fully qualified
148 * 0 is string was not fully qualified
149 * notes:
150 * Enforces label and domain length limits.
154 dns_ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
155 u_char *label, *bp, *eom;
156 int c, n, escaped;
157 char *cp;
159 escaped = 0;
160 bp = dst;
161 eom = dst + dstsiz;
162 label = bp++;
164 while ((c = *src++) != 0) {
165 if (escaped) {
166 if ((cp = strchr(digits, c)) != NULL) {
167 n = (cp - digits) * 100;
168 if ((c = *src++) == 0 ||
169 (cp = strchr(digits, c)) == NULL) {
170 return (-1);
172 n += (cp - digits) * 10;
173 if ((c = *src++) == 0 ||
174 (cp = strchr(digits, c)) == NULL) {
175 return (-1);
177 n += (cp - digits);
178 if (n > 255) {
179 return (-1);
181 c = n;
182 } else if (c == '[' && label == bp - 1 && *src == 'x') {
183 /* Theoretically we would have to handle \[o
184 as well but we do not since we do not need
185 it internally. */
186 *label = 0x41;
187 label = bp++;
188 ++src;
189 while (isxdigit (*src)) {
190 n = *src > '9' ? *src - 'a' + 10 : *src - '0';
191 ++src;
192 if (! isxdigit(*src)) {
193 return (-1);
195 n <<= 4;
196 n += *src > '9' ? *src - 'a' + 10 : *src - '0';
197 if (bp + 1 >= eom) {
198 return (-1);
200 *bp++ = n;
201 ++src;
203 *label = (bp - label - 1) * 8;
204 if (*src++ != ']' || *src++ != '.') {
205 return (-1);
207 escaped = 0;
208 label = bp++;
209 if (bp >= eom) {
210 return (-1);
212 continue;
214 escaped = 0;
215 } else if (c == '\\') {
216 escaped = 1;
217 continue;
218 } else if (c == '.') {
219 c = (bp - label - 1);
220 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
221 return (-1);
223 if (label >= eom) {
224 return (-1);
226 *label = c;
227 /* Fully qualified ? */
228 if (*src == '\0') {
229 if (c != 0) {
230 if (bp >= eom) {
231 return (-1);
233 *bp++ = '\0';
235 if ((bp - dst) > NS_MAXCDNAME) {
236 return (-1);
238 return (1);
240 if (c == 0 || *src == '.') {
241 return (-1);
243 label = bp++;
244 continue;
246 if (bp >= eom) {
247 return (-1);
249 *bp++ = (u_char)c;
251 c = (bp - label - 1);
252 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
253 return (-1);
255 if (label >= eom) {
256 return (-1);
258 *label = c;
259 if (c != 0) {
260 if (bp >= eom) {
261 return (-1);
263 *bp++ = 0;
265 if ((bp - dst) > NS_MAXCDNAME) { /* src too big */
266 return (-1);
268 return (0);
273 * dns_ns_name_unpack(msg, eom, src, dst, dstsiz)
274 * Unpack a domain name from a message, source may be compressed.
275 * return:
276 * -1 if it fails, or consumed octets if it succeeds.
278 static int
279 dns_ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
280 u_char *dst, size_t dstsiz)
282 const u_char *srcp, *dstlim;
283 u_char *dstp;
284 int n, len, checked;
286 len = -1;
287 checked = 0;
288 dstp = dst;
289 srcp = src;
290 dstlim = dst + dstsiz;
291 if (srcp < msg || srcp >= eom) {
292 return (-1);
294 /* Fetch next label in domain name. */
295 while ((n = *srcp++) != 0) {
296 /* Check for indirection. */
297 switch (n & NS_CMPRSFLGS) {
298 case 0x40:
299 if (n == 0x41) {
300 if (dstp + 1 >= dstlim) {
301 return (-1);
303 *dstp++ = 0x41;
304 n = *srcp++ / 8;
305 ++checked;
306 } else {
307 return (-1); /* flag error */
309 /* FALLTHROUGH */
310 case 0:
311 /* Limit checks. */
312 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
313 return (-1);
315 checked += n + 1;
316 dstp = memcpy(dstp, srcp - 1, n + 1);
317 dstp += n + 1;
318 srcp += n;
319 break;
321 case NS_CMPRSFLGS:
322 if (srcp >= eom) {
323 return (-1);
325 if (len < 0)
326 len = srcp - src + 1;
327 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
328 if (srcp < msg || srcp >= eom) { /* Out of range. */
329 return (-1);
331 checked += 2;
333 * Check for loops in the compressed name;
334 * if we've looked at the whole message,
335 * there must be a loop.
337 if (checked >= eom - msg) {
338 return (-1);
340 break;
342 default:
343 return (-1); /* flag error */
346 *dstp = '\0';
347 if (len < 0)
348 len = srcp - src;
349 return (len);
354 * dns_ns_name_uncompress(msg, eom, src, dst, dstsiz)
355 * Expand compressed domain name to presentation format.
356 * return:
357 * Number of bytes read out of `src', or -1 (with errno set).
358 * note:
359 * Root domain returns as "." not "".
362 dns_ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
363 char *dst, size_t dstsiz)
365 u_char tmp[NS_MAXCDNAME];
366 int n;
368 if ((n = dns_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
369 return (-1);
370 if (dns_ns_name_ntop(tmp, dst, dstsiz) == -1)
371 return (-1);
372 return (n);
377 * dns_ns_name_skip(ptrptr, eom)
378 * Advance *ptrptr to skip over the compressed name it points at.
379 * return:
380 * 0 on success, -1 (with errno set) on failure.
383 dns_ns_name_skip(const u_char **ptrptr, const u_char *eom) {
384 const u_char *cp;
385 u_int n;
387 cp = *ptrptr;
388 while (cp < eom && (n = *cp++) != 0) {
389 /* Check for indirection. */
390 switch (n & NS_CMPRSFLGS) {
391 case 0: /* normal case, n == len */
392 cp += n;
393 continue;
394 case NS_CMPRSFLGS: /* indirection */
395 cp++;
396 break;
397 default: /* illegal type */
398 return (-1);
400 break;
402 if (cp > eom) {
403 return (-1);
405 *ptrptr = cp;
406 return (0);
409 /* Private. */
412 * special(ch)
413 * Thinking in noninternationalized USASCII (per the DNS spec),
414 * is this characted special ("in need of quoting") ?
415 * return:
416 * boolean.
418 static int
419 special(int ch) {
420 switch (ch) {
421 case 0x22: /* '"' */
422 case 0x2E: /* '.' */
423 case 0x3B: /* ';' */
424 case 0x5C: /* '\\' */
425 /* Special modifiers in zone files. */
426 case 0x40: /* '@' */
427 case 0x24: /* '$' */
428 return (1);
429 default:
430 return (0);
435 * printable(ch)
436 * Thinking in noninternationalized USASCII (per the DNS spec),
437 * is this character visible and not a space when printed ?
438 * return:
439 * boolean.
441 static int
442 printable(int ch) {
443 return (ch > 0x20 && ch < 0x7f);