hr_HR locale: Don’t use single code points for the digraphs in LC_TIME
[glibc.git] / resolv / ns_name.c
blob73213fee2dca530baf1abcb6dc52024db9d40060
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
23 #include <errno.h>
24 #include <resolv.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <limits.h>
30 # define SPRINTF(x) ((size_t)sprintf x)
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 *);
43 static int labellen(const u_char *);
45 /* Public. */
47 /*%
48 * Convert an encoded domain name to printable ascii as per RFC1035.
50 * return:
51 *\li Number of bytes written to buffer, or -1 (with errno set)
53 * notes:
54 *\li The root is returned as "."
55 *\li All other domains are returned in non absolute form
57 int
58 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
60 const u_char *cp;
61 char *dn, *eom;
62 u_char c;
63 u_int n;
64 int l;
66 cp = src;
67 dn = dst;
68 eom = dst + dstsiz;
70 while ((n = *cp++) != 0) {
71 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
72 /* Some kind of compression pointer. */
73 __set_errno (EMSGSIZE);
74 return (-1);
76 if (dn != dst) {
77 if (dn >= eom) {
78 __set_errno (EMSGSIZE);
79 return (-1);
81 *dn++ = '.';
83 if ((l = labellen(cp - 1)) < 0) {
84 __set_errno (EMSGSIZE);
85 return(-1);
87 if (dn + l >= eom) {
88 __set_errno (EMSGSIZE);
89 return (-1);
91 for ((void)NULL; l > 0; l--) {
92 c = *cp++;
93 if (special(c)) {
94 if (dn + 1 >= eom) {
95 __set_errno (EMSGSIZE);
96 return (-1);
98 *dn++ = '\\';
99 *dn++ = (char)c;
100 } else if (!printable(c)) {
101 if (dn + 3 >= eom) {
102 __set_errno (EMSGSIZE);
103 return (-1);
105 *dn++ = '\\';
106 *dn++ = digits[c / 100];
107 *dn++ = digits[(c % 100) / 10];
108 *dn++ = digits[c % 10];
109 } else {
110 if (dn >= eom) {
111 __set_errno (EMSGSIZE);
112 return (-1);
114 *dn++ = (char)c;
118 if (dn == dst) {
119 if (dn >= eom) {
120 __set_errno (EMSGSIZE);
121 return (-1);
123 *dn++ = '.';
125 if (dn >= eom) {
126 __set_errno (EMSGSIZE);
127 return (-1);
129 *dn++ = '\0';
130 return (dn - dst);
132 libresolv_hidden_def (ns_name_ntop)
133 strong_alias (ns_name_ntop, __ns_name_ntop)
136 * Convert an ascii string into an encoded domain name as per RFC1035.
138 * return:
140 *\li -1 if it fails
141 *\li 1 if string was fully qualified
142 *\li 0 is string was not fully qualified
144 * notes:
145 *\li Enforces label and domain length limits.
149 ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
151 u_char *label, *bp, *eom;
152 int c, n, escaped;
153 char *cp;
155 escaped = 0;
156 bp = dst;
157 eom = dst + dstsiz;
158 label = bp++;
160 while ((c = *src++) != 0) {
161 if (escaped) {
162 if ((cp = strchr(digits, c)) != NULL) {
163 n = (cp - digits) * 100;
164 if ((c = *src++) == 0 ||
165 (cp = strchr(digits, c)) == NULL) {
166 __set_errno (EMSGSIZE);
167 return (-1);
169 n += (cp - digits) * 10;
170 if ((c = *src++) == 0 ||
171 (cp = strchr(digits, c)) == NULL) {
172 __set_errno (EMSGSIZE);
173 return (-1);
175 n += (cp - digits);
176 if (n > 255) {
177 __set_errno (EMSGSIZE);
178 return (-1);
180 c = n;
182 escaped = 0;
183 } else if (c == '\\') {
184 escaped = 1;
185 continue;
186 } else if (c == '.') {
187 c = (bp - label - 1);
188 if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
189 __set_errno (EMSGSIZE);
190 return (-1);
192 if (label >= eom) {
193 __set_errno (EMSGSIZE);
194 return (-1);
196 *label = c;
197 /* Fully qualified ? */
198 if (*src == '\0') {
199 if (c != 0) {
200 if (bp >= eom) {
201 __set_errno (EMSGSIZE);
202 return (-1);
204 *bp++ = '\0';
206 if ((bp - dst) > MAXCDNAME) {
207 __set_errno (EMSGSIZE);
208 return (-1);
210 return (1);
212 if (c == 0 || *src == '.') {
213 __set_errno (EMSGSIZE);
214 return (-1);
216 label = bp++;
217 continue;
219 if (bp >= eom) {
220 __set_errno (EMSGSIZE);
221 return (-1);
223 *bp++ = (u_char)c;
225 if (escaped) {
226 /* Trailing backslash. */
227 __set_errno (EMSGSIZE);
228 return -1;
230 c = (bp - label - 1);
231 if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
232 __set_errno (EMSGSIZE);
233 return (-1);
235 if (label >= eom) {
236 __set_errno (EMSGSIZE);
237 return (-1);
239 *label = c;
240 if (c != 0) {
241 if (bp >= eom) {
242 __set_errno (EMSGSIZE);
243 return (-1);
245 *bp++ = 0;
247 if ((bp - dst) > MAXCDNAME) { /*%< src too big */
248 __set_errno (EMSGSIZE);
249 return (-1);
251 return (0);
253 libresolv_hidden_def (ns_name_pton)
256 * Convert a network strings labels into all lowercase.
258 * return:
259 *\li Number of bytes written to buffer, or -1 (with errno set)
261 * notes:
262 *\li Enforces label and domain length limits.
266 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
268 const u_char *cp;
269 u_char *dn, *eom;
270 u_char c;
271 u_int n;
272 int l;
274 cp = src;
275 dn = dst;
276 eom = dst + dstsiz;
278 if (dn >= eom) {
279 __set_errno (EMSGSIZE);
280 return (-1);
282 while ((n = *cp++) != 0) {
283 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
284 /* Some kind of compression pointer. */
285 __set_errno (EMSGSIZE);
286 return (-1);
288 *dn++ = n;
289 if ((l = labellen(cp - 1)) < 0) {
290 __set_errno (EMSGSIZE);
291 return (-1);
293 if (dn + l >= eom) {
294 __set_errno (EMSGSIZE);
295 return (-1);
297 for ((void)NULL; l > 0; l--) {
298 c = *cp++;
299 if (isupper(c))
300 *dn++ = tolower(c);
301 else
302 *dn++ = c;
305 *dn++ = '\0';
306 return (dn - dst);
310 * Unpack a domain name from a message, source may be compressed.
312 * return:
313 *\li -1 if it fails, or consumed octets if it succeeds.
316 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
317 u_char *dst, size_t dstsiz)
319 const u_char *srcp, *dstlim;
320 u_char *dstp;
321 int n, len, checked, l;
323 len = -1;
324 checked = 0;
325 dstp = dst;
326 srcp = src;
327 dstlim = dst + dstsiz;
328 if (srcp < msg || srcp >= eom) {
329 __set_errno (EMSGSIZE);
330 return (-1);
332 /* Fetch next label in domain name. */
333 while ((n = *srcp++) != 0) {
334 /* Check for indirection. */
335 switch (n & NS_CMPRSFLGS) {
336 case 0:
337 /* Limit checks. */
338 if ((l = labellen(srcp - 1)) < 0) {
339 __set_errno (EMSGSIZE);
340 return(-1);
342 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
343 __set_errno (EMSGSIZE);
344 return (-1);
346 checked += l + 1;
347 *dstp++ = n;
348 memcpy(dstp, srcp, l);
349 dstp += l;
350 srcp += l;
351 break;
353 case NS_CMPRSFLGS:
354 if (srcp >= eom) {
355 __set_errno (EMSGSIZE);
356 return (-1);
358 if (len < 0)
359 len = srcp - src + 1;
360 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
361 if (srcp < msg || srcp >= eom) { /*%< Out of range. */
362 __set_errno (EMSGSIZE);
363 return (-1);
365 checked += 2;
367 * Check for loops in the compressed name;
368 * if we've looked at the whole message,
369 * there must be a loop.
371 if (checked >= eom - msg) {
372 __set_errno (EMSGSIZE);
373 return (-1);
375 break;
377 default:
378 __set_errno (EMSGSIZE);
379 return (-1); /*%< flag error */
382 *dstp = '\0';
383 if (len < 0)
384 len = srcp - src;
385 return (len);
387 libresolv_hidden_def (ns_name_unpack)
388 strong_alias (ns_name_unpack, __ns_name_unpack)
391 * Pack domain name 'domain' into 'comp_dn'.
393 * return:
394 *\li Size of the compressed name, or -1.
396 * notes:
397 *\li 'dnptrs' is an array of pointers to previous compressed names.
398 *\li dnptrs[0] is a pointer to the beginning of the message. The array
399 * ends with NULL.
400 *\li 'lastdnptr' is a pointer to the end of the array pointed to
401 * by 'dnptrs'.
403 * Side effects:
404 *\li The list of pointers in dnptrs is updated for labels inserted into
405 * the message as we compress the name. If 'dnptr' is NULL, we don't
406 * try to compress names. If 'lastdnptr' is NULL, we don't update the
407 * list.
410 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
411 const u_char **dnptrs, const u_char **lastdnptr)
413 u_char *dstp;
414 const u_char **cpp, **lpp, *eob, *msg;
415 const u_char *srcp;
416 int n, l, first = 1;
418 srcp = src;
419 dstp = dst;
420 eob = dstp + dstsiz;
421 lpp = cpp = NULL;
422 if (dnptrs != NULL) {
423 if ((msg = *dnptrs++) != NULL) {
424 for (cpp = dnptrs; *cpp != NULL; cpp++)
425 (void)NULL;
426 lpp = cpp; /*%< end of list to search */
428 } else
429 msg = NULL;
431 /* make sure the domain we are about to add is legal */
432 l = 0;
433 do {
434 int l0;
436 n = *srcp;
437 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
438 __set_errno (EMSGSIZE);
439 return (-1);
441 if ((l0 = labellen(srcp)) < 0) {
442 __set_errno (EINVAL);
443 return(-1);
445 l += l0 + 1;
446 if (l > MAXCDNAME) {
447 __set_errno (EMSGSIZE);
448 return (-1);
450 srcp += l0 + 1;
451 } while (n != 0);
453 /* from here on we need to reset compression pointer array on error */
454 srcp = src;
455 do {
456 /* Look to see if we can use pointers. */
457 n = *srcp;
458 if (n != 0 && msg != NULL) {
459 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
460 (const u_char * const *)lpp);
461 if (l >= 0) {
462 if (dstp + 1 >= eob) {
463 goto cleanup;
465 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
466 *dstp++ = l % 256;
467 return (dstp - dst);
469 /* Not found, save it. */
470 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
471 (dstp - msg) < 0x4000 && first) {
472 *cpp++ = dstp;
473 *cpp = NULL;
474 first = 0;
477 /* copy label to buffer */
478 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
479 /* Should not happen. */
480 goto cleanup;
482 n = labellen(srcp);
483 if (n + 1 > eob - dstp) {
484 goto cleanup;
486 memcpy(dstp, srcp, n + 1);
487 srcp += n + 1;
488 dstp += n + 1;
489 } while (n != 0);
491 if (dstp > eob) {
492 cleanup:
493 if (msg != NULL)
494 *lpp = NULL;
495 __set_errno (EMSGSIZE);
496 return (-1);
498 return (dstp - dst);
500 libresolv_hidden_def (ns_name_pack)
503 * Expand compressed domain name to presentation format.
505 * return:
506 *\li Number of bytes read out of `src', or -1 (with errno set).
508 * note:
509 *\li Root domain returns as "." not "".
512 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
513 char *dst, size_t dstsiz)
515 u_char tmp[NS_MAXCDNAME];
516 int n;
518 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
519 return (-1);
520 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
521 return (-1);
522 return (n);
524 libresolv_hidden_def (ns_name_uncompress)
527 * Compress a domain name into wire format, using compression pointers.
529 * return:
530 *\li Number of bytes consumed in `dst' or -1 (with errno set).
532 * notes:
533 *\li 'dnptrs' is an array of pointers to previous compressed names.
534 *\li dnptrs[0] is a pointer to the beginning of the message.
535 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
536 * array pointed to by 'dnptrs'. Side effect is to update the list of
537 * pointers for labels inserted into the message as we compress the name.
538 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
539 * is NULL, we don't update the list.
542 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
543 const u_char **dnptrs, const u_char **lastdnptr)
545 u_char tmp[NS_MAXCDNAME];
547 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
548 return (-1);
549 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
551 libresolv_hidden_def (ns_name_compress)
554 * Reset dnptrs so that there are no active references to pointers at or
555 * after src.
557 void
558 ns_name_rollback(const u_char *src, const u_char **dnptrs,
559 const u_char **lastdnptr)
561 while (dnptrs < lastdnptr && *dnptrs != NULL) {
562 if (*dnptrs >= src) {
563 *dnptrs = NULL;
564 break;
566 dnptrs++;
571 * Advance *ptrptr to skip over the compressed name it points at.
573 * return:
574 *\li 0 on success, -1 (with errno set) on failure.
577 ns_name_skip(const u_char **ptrptr, const u_char *eom)
579 const u_char *cp;
580 u_int n;
582 cp = *ptrptr;
583 while (cp < eom && (n = *cp++) != 0) {
584 /* Check for indirection. */
585 switch (n & NS_CMPRSFLGS) {
586 case 0: /*%< normal case, n == len */
587 cp += n;
588 continue;
589 case NS_CMPRSFLGS: /*%< indirection */
590 cp++;
591 break;
592 default: /*%< illegal type */
593 __set_errno (EMSGSIZE);
594 return (-1);
596 break;
598 if (cp > eom) {
599 __set_errno (EMSGSIZE);
600 return (-1);
602 *ptrptr = cp;
603 return (0);
605 libresolv_hidden_def (ns_name_skip)
607 /* Private. */
610 * Thinking in noninternationalized USASCII (per the DNS spec),
611 * is this character special ("in need of quoting") ?
613 * return:
614 *\li boolean.
616 static int
617 special(int ch) {
618 switch (ch) {
619 case 0x22: /*%< '"' */
620 case 0x2E: /*%< '.' */
621 case 0x3B: /*%< ';' */
622 case 0x5C: /*%< '\\' */
623 case 0x28: /*%< '(' */
624 case 0x29: /*%< ')' */
625 /* Special modifiers in zone files. */
626 case 0x40: /*%< '@' */
627 case 0x24: /*%< '$' */
628 return (1);
629 default:
630 return (0);
635 * Thinking in noninternationalized USASCII (per the DNS spec),
636 * is this character visible and not a space when printed ?
638 * return:
639 *\li boolean.
641 static int
642 printable(int ch) {
643 return (ch > 0x20 && ch < 0x7f);
647 * Thinking in noninternationalized USASCII (per the DNS spec),
648 * convert this character to lower case if it's upper case.
650 static int
651 mklower(int ch) {
652 if (ch >= 0x41 && ch <= 0x5A)
653 return (ch + 0x20);
654 return (ch);
658 * Search for the counted-label name in an array of compressed names.
660 * return:
661 *\li offset from msg if found, or -1.
663 * notes:
664 *\li dnptrs is the pointer to the first name on the list,
665 *\li not the pointer to the start of the message.
667 static int
668 dn_find(const u_char *domain, const u_char *msg,
669 const u_char * const *dnptrs,
670 const u_char * const *lastdnptr)
672 const u_char *dn, *cp, *sp;
673 const u_char * const *cpp;
674 u_int n;
676 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
677 sp = *cpp;
679 * terminate search on:
680 * root label
681 * compression pointer
682 * unusable offset
684 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
685 (sp - msg) < 0x4000) {
686 dn = domain;
687 cp = sp;
688 while ((n = *cp++) != 0) {
690 * check for indirection
692 switch (n & NS_CMPRSFLGS) {
693 case 0: /*%< normal case, n == len */
694 n = labellen(cp - 1); /*%< XXX */
695 if (n != *dn++)
696 goto next;
698 for ((void)NULL; n > 0; n--)
699 if (mklower(*dn++) !=
700 mklower(*cp++))
701 goto next;
702 /* Is next root for both ? */
703 if (*dn == '\0' && *cp == '\0')
704 return (sp - msg);
705 if (*dn)
706 continue;
707 goto next;
708 case NS_CMPRSFLGS: /*%< indirection */
709 cp = msg + (((n & 0x3f) << 8) | *cp);
710 break;
712 default: /*%< illegal type */
713 __set_errno (EMSGSIZE);
714 return (-1);
717 next: ;
718 sp += *sp + 1;
721 __set_errno (ENOENT);
722 return (-1);
725 /* Return the length of the encoded label starting at LP, or -1 for
726 compression references and extended label types. */
727 static int
728 labellen (const unsigned char *lp)
730 if (*lp <= 63)
731 return *lp;
732 return -1;
735 /*! \file */