Update.
[glibc.git] / resolv / ns_name.c
blobb75f731c442191c71caabdc4f0cc2d7dc6f2693a
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 #ifndef lint
19 static const char rcsid[] = "$Id$";
20 #endif
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
27 #include <errno.h>
28 #include <resolv.h>
29 #include <string.h>
30 #include <ctype.h>
32 /* Data. */
34 static const char digits[] = "0123456789";
36 /* Forward. */
38 static int special(int);
39 static int printable(int);
40 static int dn_find(const u_char *, const u_char *,
41 const u_char * const *,
42 const u_char * const *);
44 /* Public. */
47 * ns_name_ntop(src, dst, dstsiz)
48 * Convert an encoded domain name to printable ascii as per RFC1035.
49 * return:
50 * Number of bytes written to buffer, or -1 (with errno set)
51 * notes:
52 * The root is returned as "."
53 * All other domains are returned in non absolute form
55 int
56 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
57 const u_char *cp;
58 char *dn, *eom;
59 u_char c;
60 u_int n;
62 cp = src;
63 dn = dst;
64 eom = dst + dstsiz;
66 while ((n = *cp++) != 0) {
67 if ((n & NS_CMPRSFLGS) != 0) {
68 /* Some kind of compression pointer. */
69 __set_errno (EMSGSIZE);
70 return (-1);
72 if (dn != dst) {
73 if (dn >= eom) {
74 __set_errno (EMSGSIZE);
75 return (-1);
77 *dn++ = '.';
79 if (dn + n >= eom) {
80 __set_errno (EMSGSIZE);
81 return (-1);
83 for ((void)NULL; n > 0; n--) {
84 c = *cp++;
85 if (special(c)) {
86 if (dn + 1 >= eom) {
87 __set_errno (EMSGSIZE);
88 return (-1);
90 *dn++ = '\\';
91 *dn++ = (char)c;
92 } else if (!printable(c)) {
93 if (dn + 3 >= eom) {
94 __set_errno (EMSGSIZE);
95 return (-1);
97 *dn++ = '\\';
98 *dn++ = digits[c / 100];
99 *dn++ = digits[(c % 100) / 10];
100 *dn++ = digits[c % 10];
101 } else {
102 if (dn >= eom) {
103 __set_errno (EMSGSIZE);
104 return (-1);
106 *dn++ = (char)c;
110 if (dn == dst) {
111 if (dn >= eom) {
112 __set_errno (EMSGSIZE);
113 return (-1);
115 *dn++ = '.';
117 if (dn >= eom) {
118 __set_errno (EMSGSIZE);
119 return (-1);
121 *dn++ = '\0';
122 return (dn - dst);
126 * ns_name_pton(src, dst, dstsiz)
127 * Convert a ascii string into an encoded domain name as per RFC1035.
128 * return:
129 * -1 if it fails
130 * 1 if string was fully qualified
131 * 0 is string was not fully qualified
132 * notes:
133 * Enforces label and domain length limits.
137 ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
138 u_char *label, *bp, *eom;
139 int c, n, escaped;
140 char *cp;
142 escaped = 0;
143 bp = dst;
144 eom = dst + dstsiz;
145 label = bp++;
147 while ((c = *src++) != 0) {
148 if (escaped) {
149 if ((cp = strchr(digits, c)) != NULL) {
150 n = (cp - digits) * 100;
151 if ((c = *src++) == 0 ||
152 (cp = strchr(digits, c)) == NULL) {
153 __set_errno (EMSGSIZE);
154 return (-1);
156 n += (cp - digits) * 10;
157 if ((c = *src++) == 0 ||
158 (cp = strchr(digits, c)) == NULL) {
159 __set_errno (EMSGSIZE);
160 return (-1);
162 n += (cp - digits);
163 if (n > 255) {
164 __set_errno (EMSGSIZE);
165 return (-1);
167 c = n;
169 escaped = 0;
170 } else if (c == '\\') {
171 escaped = 1;
172 continue;
173 } else if (c == '.') {
174 c = (bp - label - 1);
175 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
176 __set_errno (EMSGSIZE);
177 return (-1);
179 if (label >= eom) {
180 __set_errno (EMSGSIZE);
181 return (-1);
183 *label = c;
184 /* Fully qualified ? */
185 if (*src == '\0') {
186 if (c != 0) {
187 if (bp >= eom) {
188 __set_errno (EMSGSIZE);
189 return (-1);
191 *bp++ = '\0';
193 if ((bp - dst) > MAXCDNAME) {
194 __set_errno (EMSGSIZE);
195 return (-1);
197 return (1);
199 if (c == 0 || *src == '.') {
200 __set_errno (EMSGSIZE);
201 return (-1);
203 label = bp++;
204 continue;
206 if (bp >= eom) {
207 __set_errno (EMSGSIZE);
208 return (-1);
210 *bp++ = (u_char)c;
212 c = (bp - label - 1);
213 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
214 __set_errno (EMSGSIZE);
215 return (-1);
217 if (label >= eom) {
218 __set_errno (EMSGSIZE);
219 return (-1);
221 *label = c;
222 if (c != 0) {
223 if (bp >= eom) {
224 __set_errno (EMSGSIZE);
225 return (-1);
227 *bp++ = 0;
229 if ((bp - dst) > MAXCDNAME) { /* src too big */
230 __set_errno (EMSGSIZE);
231 return (-1);
233 return (0);
237 * ns_name_ntol(src, dst, dstsiz)
238 * Convert a network strings labels into all lowercase.
239 * return:
240 * Number of bytes written to buffer, or -1 (with errno set)
241 * notes:
242 * Enforces label and domain length limits.
246 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
247 const u_char *cp;
248 u_char *dn, *eom;
249 u_char c;
250 u_int n;
252 cp = src;
253 dn = dst;
254 eom = dst + dstsiz;
256 while ((n = *cp++) != 0) {
257 if ((n & NS_CMPRSFLGS) != 0) {
258 /* Some kind of compression pointer. */
259 __set_errno (EMSGSIZE);
260 return (-1);
262 *dn++ = n;
263 if (dn + n >= eom) {
264 __set_errno (EMSGSIZE);
265 return (-1);
267 for ((void)NULL; n > 0; n--) {
268 c = *cp++;
269 if (isupper(c))
270 *dn++ = tolower(c);
271 else
272 *dn++ = c;
275 *dn++ = '\0';
276 return (dn - dst);
280 * ns_name_unpack(msg, eom, src, dst, dstsiz)
281 * Unpack a domain name from a message, source may be compressed.
282 * return:
283 * -1 if it fails, or consumed octets if it succeeds.
286 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
287 u_char *dst, size_t dstsiz)
289 const u_char *srcp, *dstlim;
290 u_char *dstp;
291 int n, len, checked;
293 len = -1;
294 checked = 0;
295 dstp = dst;
296 srcp = src;
297 dstlim = dst + dstsiz;
298 if (srcp < msg || srcp >= eom) {
299 __set_errno (EMSGSIZE);
300 return (-1);
302 /* Fetch next label in domain name. */
303 while ((n = *srcp++) != 0) {
304 /* Check for indirection. */
305 switch (n & NS_CMPRSFLGS) {
306 case 0:
307 /* Limit checks. */
308 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
309 __set_errno (EMSGSIZE);
310 return (-1);
312 checked += n + 1;
313 *dstp++ = n;
314 memcpy(dstp, srcp, n);
315 dstp += n;
316 srcp += n;
317 break;
319 case NS_CMPRSFLGS:
320 if (srcp >= eom) {
321 __set_errno (EMSGSIZE);
322 return (-1);
324 if (len < 0)
325 len = srcp - src + 1;
326 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
327 if (srcp < msg || srcp >= eom) { /* Out of range. */
328 __set_errno (EMSGSIZE);
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 __set_errno (EMSGSIZE);
339 return (-1);
341 break;
343 default:
344 __set_errno (EMSGSIZE);
345 return (-1); /* flag error */
348 *dstp = '\0';
349 if (len < 0)
350 len = srcp - src;
351 return (len);
355 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
356 * Pack domain name 'domain' into 'comp_dn'.
357 * return:
358 * Size of the compressed name, or -1.
359 * notes:
360 * 'dnptrs' is an array of pointers to previous compressed names.
361 * dnptrs[0] is a pointer to the beginning of the message. The array
362 * ends with NULL.
363 * 'lastdnptr' is a pointer to the end of the array pointed to
364 * by 'dnptrs'.
365 * Side effects:
366 * The list of pointers in dnptrs is updated for labels inserted into
367 * the message as we compress the name. If 'dnptr' is NULL, we don't
368 * try to compress names. If 'lastdnptr' is NULL, we don't update the
369 * list.
372 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
373 const u_char **dnptrs, const u_char **lastdnptr)
375 u_char *dstp;
376 const u_char **cpp, **lpp, *eob, *msg;
377 const u_char *srcp;
378 int n, l;
380 srcp = src;
381 dstp = dst;
382 eob = dstp + dstsiz;
383 lpp = cpp = NULL;
384 if (dnptrs != NULL) {
385 if ((msg = *dnptrs++) != NULL) {
386 for (cpp = dnptrs; *cpp != NULL; cpp++)
387 (void)NULL;
388 lpp = cpp; /* end of list to search */
390 } else
391 msg = NULL;
393 /* make sure the domain we are about to add is legal */
394 l = 0;
395 do {
396 n = *srcp;
397 if ((n & NS_CMPRSFLGS) != 0) {
398 __set_errno (EMSGSIZE);
399 return (-1);
401 l += n + 1;
402 if (l > MAXCDNAME) {
403 __set_errno (EMSGSIZE);
404 return (-1);
406 srcp += n + 1;
407 } while (n != 0);
409 /* from here on we need to reset compression pointer array on error */
410 srcp = src;
411 do {
412 /* Look to see if we can use pointers. */
413 n = *srcp;
414 if (n != 0 && msg != NULL) {
415 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
416 (const u_char * const *)lpp);
417 if (l >= 0) {
418 if (dstp + 1 >= eob) {
419 goto cleanup;
421 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
422 *dstp++ = l % 256;
423 return (dstp - dst);
425 /* Not found, save it. */
426 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
427 (dstp - msg) < 0x4000) {
428 *cpp++ = dstp;
429 *cpp = NULL;
432 /* copy label to buffer */
433 if (n & NS_CMPRSFLGS) { /* Should not happen. */
434 goto cleanup;
436 if (dstp + 1 + n >= eob) {
437 goto cleanup;
439 memcpy(dstp, srcp, n + 1);
440 srcp += n + 1;
441 dstp += n + 1;
442 } while (n != 0);
444 if (dstp > eob) {
445 cleanup:
446 if (msg != NULL)
447 *lpp = NULL;
448 __set_errno (EMSGSIZE);
449 return (-1);
451 return (dstp - dst);
455 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
456 * Expand compressed domain name to presentation format.
457 * return:
458 * Number of bytes read out of `src', or -1 (with errno set).
459 * note:
460 * Root domain returns as "." not "".
463 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
464 char *dst, size_t dstsiz)
466 u_char tmp[NS_MAXCDNAME];
467 int n;
469 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
470 return (-1);
471 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
472 return (-1);
473 return (n);
477 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
478 * Compress a domain name into wire format, using compression pointers.
479 * return:
480 * Number of bytes consumed in `dst' or -1 (with errno set).
481 * notes:
482 * 'dnptrs' is an array of pointers to previous compressed names.
483 * dnptrs[0] is a pointer to the beginning of the message.
484 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
485 * array pointed to by 'dnptrs'. Side effect is to update the list of
486 * pointers for labels inserted into the message as we compress the name.
487 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
488 * is NULL, we don't update the list.
491 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
492 const u_char **dnptrs, const u_char **lastdnptr)
494 u_char tmp[NS_MAXCDNAME];
496 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
497 return (-1);
498 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
502 * ns_name_skip(ptrptr, eom)
503 * Advance *ptrptr to skip over the compressed name it points at.
504 * return:
505 * 0 on success, -1 (with errno set) on failure.
508 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
509 const u_char *cp;
510 u_int n;
512 cp = *ptrptr;
513 while (cp < eom && (n = *cp++) != 0) {
514 /* Check for indirection. */
515 switch (n & NS_CMPRSFLGS) {
516 case 0: /* normal case, n == len */
517 cp += n;
518 continue;
519 case NS_CMPRSFLGS: /* indirection */
520 cp++;
521 break;
522 default: /* illegal type */
523 __set_errno (EMSGSIZE);
524 return (-1);
526 break;
528 if (cp > eom) {
529 __set_errno (EMSGSIZE);
530 return (-1);
532 *ptrptr = cp;
533 return (0);
536 /* Private. */
539 * special(ch)
540 * Thinking in noninternationalized USASCII (per the DNS spec),
541 * is this characted special ("in need of quoting") ?
542 * return:
543 * boolean.
545 static int
546 special(int ch) {
547 switch (ch) {
548 case 0x22: /* '"' */
549 case 0x2E: /* '.' */
550 case 0x3B: /* ';' */
551 case 0x5C: /* '\\' */
552 /* Special modifiers in zone files. */
553 case 0x40: /* '@' */
554 case 0x24: /* '$' */
555 return (1);
556 default:
557 return (0);
562 * printable(ch)
563 * Thinking in noninternationalized USASCII (per the DNS spec),
564 * is this character visible and not a space when printed ?
565 * return:
566 * boolean.
568 static int
569 printable(int ch) {
570 return (ch > 0x20 && ch < 0x7f);
574 * Thinking in noninternationalized USASCII (per the DNS spec),
575 * convert this character to lower case if it's upper case.
577 static int
578 mklower(int ch) {
579 if (ch >= 0x41 && ch <= 0x5A)
580 return (ch + 0x20);
581 return (ch);
585 * dn_find(domain, msg, dnptrs, lastdnptr)
586 * Search for the counted-label name in an array of compressed names.
587 * return:
588 * offset from msg if found, or -1.
589 * notes:
590 * dnptrs is the pointer to the first name on the list,
591 * not the pointer to the start of the message.
593 static int
594 dn_find(const u_char *domain, const u_char *msg,
595 const u_char * const *dnptrs,
596 const u_char * const *lastdnptr)
598 const u_char *dn, *cp, *sp;
599 const u_char * const *cpp;
600 u_int n;
602 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
603 dn = domain;
604 sp = cp = *cpp;
605 while ((n = *cp++) != 0) {
607 * check for indirection
609 switch (n & NS_CMPRSFLGS) {
610 case 0: /* normal case, n == len */
611 if (n != *dn++)
612 goto next;
613 for ((void)NULL; n > 0; n--)
614 if (mklower(*dn++) != mklower(*cp++))
615 goto next;
616 /* Is next root for both ? */
617 if (*dn == '\0' && *cp == '\0')
618 return (sp - msg);
619 if (*dn)
620 continue;
621 goto next;
623 case NS_CMPRSFLGS: /* indirection */
624 cp = msg + (((n & 0x3f) << 8) | *cp);
625 break;
627 default: /* illegal type */
628 __set_errno (EMSGSIZE);
629 return (-1);
632 next: ;
634 __set_errno (ENOENT);
635 return (-1);