2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-2003 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.
17 * Internet Systems Consortium, Inc.
19 * Redwood City, CA 94063
25 static const char rcsid[] = "$Id: ns_name.c,v 1.1.2.5 2004/06/10 17:59:40 dhankins Exp $";
28 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
37 #include "minires/minires.h"
38 #include "arpa/nameser.h"
42 static const char digits[] = "0123456789";
46 static int special(int);
47 static int printable(int);
48 static int dn_find(const u_char *, const u_char *,
49 const u_char * const *,
50 const u_char * const *);
55 * ns_name_ntop(src, dst, dstsiz)
56 * Convert an encoded domain name to printable ascii as per RFC1035.
58 * Number of bytes written to buffer, or -1 (with errno set)
60 * The root is returned as "."
61 * All other domains are returned in non absolute form
64 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
74 while ((n = *cp++) != 0) {
75 if ((n & NS_CMPRSFLGS) != 0) {
76 /* Some kind of compression pointer. */
91 for ((void)NULL; n > 0; n--) {
100 } else if (!printable(c)) {
106 *dn++ = digits[c / 100];
107 *dn++ = digits[(c % 100) / 10];
108 *dn++ = digits[c % 10];
134 * ns_name_pton(src, dst, dstsiz)
135 * Convert a ascii string into an encoded domain name as per RFC1035.
138 * 1 if string was fully qualified
139 * 0 is string was not fully qualified
141 * Enforces label and domain length limits.
145 ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
146 u_char *label, *bp, *eom;
155 while ((c = *src++) != 0) {
157 if ((cp = strchr(digits, c)) != NULL) {
158 n = (cp - digits) * 100;
159 if ((c = *src++) == 0 ||
160 (cp = strchr(digits, c)) == NULL) {
164 n += (cp - digits) * 10;
165 if ((c = *src++) == 0 ||
166 (cp = strchr(digits, c)) == NULL) {
178 } else if (c == '\\') {
181 } else if (c == '.') {
182 c = (bp - label - 1);
183 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
192 /* Fully qualified ? */
201 if ((bp - dst) > MAXCDNAME) {
207 if (c == 0 || *src == '.') {
220 c = (bp - label - 1);
221 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
237 if ((bp - dst) > MAXCDNAME) { /* src too big */
245 * ns_name_ntol(src, dst, dstsiz)
246 * Convert a network strings labels into all lowercase.
248 * Number of bytes written to buffer, or -1 (with errno set)
250 * Enforces label and domain length limits.
254 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
268 while ((n = *cp++) != 0) {
269 if ((n & NS_CMPRSFLGS) != 0) {
270 /* Some kind of compression pointer. */
279 for ((void)NULL; n > 0; n--) {
292 * ns_name_unpack(msg, eom, src, dst, dstsiz)
293 * Unpack a domain name from a message, source may be compressed.
295 * -1 if it fails, or consumed octets if it succeeds.
298 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
299 u_char *dst, size_t dstsiz)
301 const u_char *srcp, *dstlim;
311 dstlim = dst + dstsiz;
312 if (srcp < msg || srcp >= eom) {
316 /* Fetch next label in domain name. */
317 while ((n = *srcp++) != 0) {
318 /* Check for indirection. */
319 switch (n & NS_CMPRSFLGS) {
322 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
328 memcpy(dstp, srcp, n);
339 len = srcp - src + 1;
340 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
341 if (srcp < msg || srcp >= eom) { /* Out of range. */
347 * Check for loops in the compressed name;
348 * if we've looked at the whole message,
349 * there must be a loop.
351 if (checked >= eom - msg) {
359 return (-1); /* flag error */
369 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
370 * Pack domain name 'domain' into 'comp_dn'.
372 * Size of the compressed name, or -1.
374 * 'dnptrs' is an array of pointers to previous compressed names.
375 * dnptrs[0] is a pointer to the beginning of the message. The array
377 * 'lastdnptr' is a pointer to the end of the array pointed to
380 * The list of pointers in dnptrs is updated for labels inserted into
381 * the message as we compress the name. If 'dnptr' is NULL, we don't
382 * try to compress names. If 'lastdnptr' is NULL, we don't update the
386 ns_name_pack(const u_char *src, u_char *dst, unsigned dstsiz,
387 const u_char **dnptrs, const u_char **lastdnptr)
390 const u_char **cpp, **lpp, *eob, *msg;
399 if (dnptrs != NULL) {
400 if ((msg = *dnptrs++) != NULL) {
401 for (cpp = dnptrs; *cpp != NULL; cpp++)
403 lpp = cpp; /* end of list to search */
408 /* make sure the domain we are about to add is legal */
412 if ((n & NS_CMPRSFLGS) != 0) {
424 /* from here on we need to reset compression pointer array on error */
427 /* Look to see if we can use pointers. */
429 if (n != 0 && msg != NULL) {
430 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
431 (const u_char * const *)lpp);
433 if (dstp + 1 >= eob) {
436 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
440 /* Not found, save it. */
441 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
442 (dstp - msg) < 0x4000) {
447 /* copy label to buffer */
448 if (n & NS_CMPRSFLGS) { /* Should not happen. */
451 if (dstp + 1 + n >= eob) {
454 memcpy(dstp, srcp, n + 1);
470 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
471 * Expand compressed domain name to presentation format.
473 * Number of bytes read out of `src', or -1 (with errno set).
475 * Root domain returns as "." not "".
478 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
479 char *dst, size_t dstsiz)
481 u_char tmp[NS_MAXCDNAME];
484 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
486 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
492 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
493 * Compress a domain name into wire format, using compression pointers.
495 * Number of bytes consumed in `dst' or -1 (with errno set).
497 * 'dnptrs' is an array of pointers to previous compressed names.
498 * dnptrs[0] is a pointer to the beginning of the message.
499 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
500 * array pointed to by 'dnptrs'. Side effect is to update the list of
501 * pointers for labels inserted into the message as we compress the name.
502 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
503 * is NULL, we don't update the list.
506 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
507 const u_char **dnptrs, const u_char **lastdnptr)
509 u_char tmp[NS_MAXCDNAME];
511 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
513 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
517 * ns_name_skip(ptrptr, eom)
518 * Advance *ptrptr to skip over the compressed name it points at.
520 * 0 on success, -1 (with errno set) on failure.
523 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
528 while (cp < eom && (n = *cp++) != 0) {
529 /* Check for indirection. */
530 switch (n & NS_CMPRSFLGS) {
531 case 0: /* normal case, n == len */
534 case NS_CMPRSFLGS: /* indirection */
537 default: /* illegal type */
555 * Thinking in noninternationalized USASCII (per the DNS spec),
556 * is this characted special ("in need of quoting") ?
566 case 0x5C: /* '\\' */
567 /* Special modifiers in zone files. */
578 * Thinking in noninternationalized USASCII (per the DNS spec),
579 * is this character visible and not a space when printed ?
585 return (ch > 0x20 && ch < 0x7f);
589 * Thinking in noninternationalized USASCII (per the DNS spec),
590 * convert this character to lower case if it's upper case.
594 if (ch >= 0x41 && ch <= 0x5A)
600 * dn_find(domain, msg, dnptrs, lastdnptr)
601 * Search for the counted-label name in an array of compressed names.
603 * offset from msg if found, or -1.
605 * dnptrs is the pointer to the first name on the list,
606 * not the pointer to the start of the message.
609 dn_find(const u_char *domain, const u_char *msg,
610 const u_char * const *dnptrs,
611 const u_char * const *lastdnptr)
613 const u_char *dn, *cp, *sp;
614 const u_char * const *cpp;
617 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
620 while ((n = *cp++) != 0) {
622 * check for indirection
624 switch (n & NS_CMPRSFLGS) {
625 case 0: /* normal case, n == len */
628 for ((void)NULL; n > 0; n--)
629 if (mklower(*dn++) != mklower(*cp++))
631 /* Is next root for both ? */
632 if (*dn == '\0' && *cp == '\0')
638 case NS_CMPRSFLGS: /* indirection */
639 cp = msg + (((n & 0x3f) << 8) | *cp);
642 default: /* illegal type */