2 * Copyright (c) 1996,1999-2003 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
19 static const char rcsid[] = "$Id: ns_name.c,v 1.1.2.4 2003/01/14 23:15:06 dhankins Exp $";
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
31 #include "minires/minires.h"
32 #include "arpa/nameser.h"
36 static const char digits[] = "0123456789";
40 static int special(int);
41 static int printable(int);
42 static int dn_find(const u_char *, const u_char *,
43 const u_char * const *,
44 const u_char * const *);
49 * ns_name_ntop(src, dst, dstsiz)
50 * Convert an encoded domain name to printable ascii as per RFC1035.
52 * Number of bytes written to buffer, or -1 (with errno set)
54 * The root is returned as "."
55 * All other domains are returned in non absolute form
58 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
68 while ((n = *cp++) != 0) {
69 if ((n & NS_CMPRSFLGS) != 0) {
70 /* Some kind of compression pointer. */
85 for ((void)NULL; n > 0; n--) {
94 } else if (!printable(c)) {
100 *dn++ = digits[c / 100];
101 *dn++ = digits[(c % 100) / 10];
102 *dn++ = digits[c % 10];
128 * ns_name_pton(src, dst, dstsiz)
129 * Convert a ascii string into an encoded domain name as per RFC1035.
132 * 1 if string was fully qualified
133 * 0 is string was not fully qualified
135 * Enforces label and domain length limits.
139 ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
140 u_char *label, *bp, *eom;
149 while ((c = *src++) != 0) {
151 if ((cp = strchr(digits, c)) != NULL) {
152 n = (cp - digits) * 100;
153 if ((c = *src++) == 0 ||
154 (cp = strchr(digits, c)) == NULL) {
158 n += (cp - digits) * 10;
159 if ((c = *src++) == 0 ||
160 (cp = strchr(digits, c)) == NULL) {
172 } else if (c == '\\') {
175 } else if (c == '.') {
176 c = (bp - label - 1);
177 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
186 /* Fully qualified ? */
195 if ((bp - dst) > MAXCDNAME) {
201 if (c == 0 || *src == '.') {
214 c = (bp - label - 1);
215 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
231 if ((bp - dst) > MAXCDNAME) { /* src too big */
239 * ns_name_ntol(src, dst, dstsiz)
240 * Convert a network strings labels into all lowercase.
242 * Number of bytes written to buffer, or -1 (with errno set)
244 * Enforces label and domain length limits.
248 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
262 while ((n = *cp++) != 0) {
263 if ((n & NS_CMPRSFLGS) != 0) {
264 /* Some kind of compression pointer. */
273 for ((void)NULL; n > 0; n--) {
286 * ns_name_unpack(msg, eom, src, dst, dstsiz)
287 * Unpack a domain name from a message, source may be compressed.
289 * -1 if it fails, or consumed octets if it succeeds.
292 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
293 u_char *dst, size_t dstsiz)
295 const u_char *srcp, *dstlim;
305 dstlim = dst + dstsiz;
306 if (srcp < msg || srcp >= eom) {
310 /* Fetch next label in domain name. */
311 while ((n = *srcp++) != 0) {
312 /* Check for indirection. */
313 switch (n & NS_CMPRSFLGS) {
316 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
322 memcpy(dstp, srcp, n);
333 len = srcp - src + 1;
334 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
335 if (srcp < msg || srcp >= eom) { /* Out of range. */
341 * Check for loops in the compressed name;
342 * if we've looked at the whole message,
343 * there must be a loop.
345 if (checked >= eom - msg) {
353 return (-1); /* flag error */
363 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
364 * Pack domain name 'domain' into 'comp_dn'.
366 * Size of the compressed name, or -1.
368 * 'dnptrs' is an array of pointers to previous compressed names.
369 * dnptrs[0] is a pointer to the beginning of the message. The array
371 * 'lastdnptr' is a pointer to the end of the array pointed to
374 * The list of pointers in dnptrs is updated for labels inserted into
375 * the message as we compress the name. If 'dnptr' is NULL, we don't
376 * try to compress names. If 'lastdnptr' is NULL, we don't update the
380 ns_name_pack(const u_char *src, u_char *dst, unsigned dstsiz,
381 const u_char **dnptrs, const u_char **lastdnptr)
384 const u_char **cpp, **lpp, *eob, *msg;
393 if (dnptrs != NULL) {
394 if ((msg = *dnptrs++) != NULL) {
395 for (cpp = dnptrs; *cpp != NULL; cpp++)
397 lpp = cpp; /* end of list to search */
402 /* make sure the domain we are about to add is legal */
406 if ((n & NS_CMPRSFLGS) != 0) {
418 /* from here on we need to reset compression pointer array on error */
421 /* Look to see if we can use pointers. */
423 if (n != 0 && msg != NULL) {
424 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
425 (const u_char * const *)lpp);
427 if (dstp + 1 >= eob) {
430 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
434 /* Not found, save it. */
435 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
436 (dstp - msg) < 0x4000) {
441 /* copy label to buffer */
442 if (n & NS_CMPRSFLGS) { /* Should not happen. */
445 if (dstp + 1 + n >= eob) {
448 memcpy(dstp, srcp, n + 1);
464 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
465 * Expand compressed domain name to presentation format.
467 * Number of bytes read out of `src', or -1 (with errno set).
469 * Root domain returns as "." not "".
472 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
473 char *dst, size_t dstsiz)
475 u_char tmp[NS_MAXCDNAME];
478 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
480 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
486 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
487 * Compress a domain name into wire format, using compression pointers.
489 * Number of bytes consumed in `dst' or -1 (with errno set).
491 * 'dnptrs' is an array of pointers to previous compressed names.
492 * dnptrs[0] is a pointer to the beginning of the message.
493 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
494 * array pointed to by 'dnptrs'. Side effect is to update the list of
495 * pointers for labels inserted into the message as we compress the name.
496 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
497 * is NULL, we don't update the list.
500 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
501 const u_char **dnptrs, const u_char **lastdnptr)
503 u_char tmp[NS_MAXCDNAME];
505 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
507 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
511 * ns_name_skip(ptrptr, eom)
512 * Advance *ptrptr to skip over the compressed name it points at.
514 * 0 on success, -1 (with errno set) on failure.
517 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
522 while (cp < eom && (n = *cp++) != 0) {
523 /* Check for indirection. */
524 switch (n & NS_CMPRSFLGS) {
525 case 0: /* normal case, n == len */
528 case NS_CMPRSFLGS: /* indirection */
531 default: /* illegal type */
549 * Thinking in noninternationalized USASCII (per the DNS spec),
550 * is this characted special ("in need of quoting") ?
560 case 0x5C: /* '\\' */
561 /* Special modifiers in zone files. */
572 * Thinking in noninternationalized USASCII (per the DNS spec),
573 * is this character visible and not a space when printed ?
579 return (ch > 0x20 && ch < 0x7f);
583 * Thinking in noninternationalized USASCII (per the DNS spec),
584 * convert this character to lower case if it's upper case.
588 if (ch >= 0x41 && ch <= 0x5A)
594 * dn_find(domain, msg, dnptrs, lastdnptr)
595 * Search for the counted-label name in an array of compressed names.
597 * offset from msg if found, or -1.
599 * dnptrs is the pointer to the first name on the list,
600 * not the pointer to the start of the message.
603 dn_find(const u_char *domain, const u_char *msg,
604 const u_char * const *dnptrs,
605 const u_char * const *lastdnptr)
607 const u_char *dn, *cp, *sp;
608 const u_char * const *cpp;
611 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
614 while ((n = *cp++) != 0) {
616 * check for indirection
618 switch (n & NS_CMPRSFLGS) {
619 case 0: /* normal case, n == len */
622 for ((void)NULL; n > 0; n--)
623 if (mklower(*dn++) != mklower(*cp++))
625 /* Is next root for both ? */
626 if (*dn == '\0' && *cp == '\0')
632 case NS_CMPRSFLGS: /* indirection */
633 cp = msg + (((n & 0x3f) << 8) | *cp);
636 default: /* illegal type */