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
19 static const char rcsid[] = "$Id: ns_name.c,v 1.1 2000/02/02 07:28:14 mellon 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) {
258 while ((n = *cp++) != 0) {
259 if ((n & NS_CMPRSFLGS) != 0) {
260 /* Some kind of compression pointer. */
269 for ((void)NULL; n > 0; n--) {
282 * ns_name_unpack(msg, eom, src, dst, dstsiz)
283 * Unpack a domain name from a message, source may be compressed.
285 * -1 if it fails, or consumed octets if it succeeds.
288 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
289 u_char *dst, size_t dstsiz)
291 const u_char *srcp, *dstlim;
301 dstlim = dst + dstsiz;
302 if (srcp < msg || srcp >= eom) {
306 /* Fetch next label in domain name. */
307 while ((n = *srcp++) != 0) {
308 /* Check for indirection. */
309 switch (n & NS_CMPRSFLGS) {
312 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
318 memcpy(dstp, srcp, n);
329 len = srcp - src + 1;
330 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
331 if (srcp < msg || srcp >= eom) { /* Out of range. */
337 * Check for loops in the compressed name;
338 * if we've looked at the whole message,
339 * there must be a loop.
341 if (checked >= eom - msg) {
349 return (-1); /* flag error */
359 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
360 * Pack domain name 'domain' into 'comp_dn'.
362 * Size of the compressed name, or -1.
364 * 'dnptrs' is an array of pointers to previous compressed names.
365 * dnptrs[0] is a pointer to the beginning of the message. The array
367 * 'lastdnptr' is a pointer to the end of the array pointed to
370 * The list of pointers in dnptrs is updated for labels inserted into
371 * the message as we compress the name. If 'dnptr' is NULL, we don't
372 * try to compress names. If 'lastdnptr' is NULL, we don't update the
376 ns_name_pack(const u_char *src, u_char *dst, unsigned dstsiz,
377 const u_char **dnptrs, const u_char **lastdnptr)
380 const u_char **cpp, **lpp, *eob, *msg;
389 if (dnptrs != NULL) {
390 if ((msg = *dnptrs++) != NULL) {
391 for (cpp = dnptrs; *cpp != NULL; cpp++)
393 lpp = cpp; /* end of list to search */
398 /* make sure the domain we are about to add is legal */
402 if ((n & NS_CMPRSFLGS) != 0) {
414 /* from here on we need to reset compression pointer array on error */
417 /* Look to see if we can use pointers. */
419 if (n != 0 && msg != NULL) {
420 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
421 (const u_char * const *)lpp);
423 if (dstp + 1 >= eob) {
426 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
430 /* Not found, save it. */
431 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
432 (dstp - msg) < 0x4000) {
437 /* copy label to buffer */
438 if (n & NS_CMPRSFLGS) { /* Should not happen. */
441 if (dstp + 1 + n >= eob) {
444 memcpy(dstp, srcp, n + 1);
460 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
461 * Expand compressed domain name to presentation format.
463 * Number of bytes read out of `src', or -1 (with errno set).
465 * Root domain returns as "." not "".
468 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
469 char *dst, size_t dstsiz)
471 u_char tmp[NS_MAXCDNAME];
474 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
476 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
482 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
483 * Compress a domain name into wire format, using compression pointers.
485 * Number of bytes consumed in `dst' or -1 (with errno set).
487 * 'dnptrs' is an array of pointers to previous compressed names.
488 * dnptrs[0] is a pointer to the beginning of the message.
489 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
490 * array pointed to by 'dnptrs'. Side effect is to update the list of
491 * pointers for labels inserted into the message as we compress the name.
492 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
493 * is NULL, we don't update the list.
496 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
497 const u_char **dnptrs, const u_char **lastdnptr)
499 u_char tmp[NS_MAXCDNAME];
501 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
503 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
507 * ns_name_skip(ptrptr, eom)
508 * Advance *ptrptr to skip over the compressed name it points at.
510 * 0 on success, -1 (with errno set) on failure.
513 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
518 while (cp < eom && (n = *cp++) != 0) {
519 /* Check for indirection. */
520 switch (n & NS_CMPRSFLGS) {
521 case 0: /* normal case, n == len */
524 case NS_CMPRSFLGS: /* indirection */
527 default: /* illegal type */
545 * Thinking in noninternationalized USASCII (per the DNS spec),
546 * is this characted special ("in need of quoting") ?
556 case 0x5C: /* '\\' */
557 /* Special modifiers in zone files. */
568 * Thinking in noninternationalized USASCII (per the DNS spec),
569 * is this character visible and not a space when printed ?
575 return (ch > 0x20 && ch < 0x7f);
579 * Thinking in noninternationalized USASCII (per the DNS spec),
580 * convert this character to lower case if it's upper case.
584 if (ch >= 0x41 && ch <= 0x5A)
590 * dn_find(domain, msg, dnptrs, lastdnptr)
591 * Search for the counted-label name in an array of compressed names.
593 * offset from msg if found, or -1.
595 * dnptrs is the pointer to the first name on the list,
596 * not the pointer to the start of the message.
599 dn_find(const u_char *domain, const u_char *msg,
600 const u_char * const *dnptrs,
601 const u_char * const *lastdnptr)
603 const u_char *dn, *cp, *sp;
604 const u_char * const *cpp;
607 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
610 while ((n = *cp++) != 0) {
612 * check for indirection
614 switch (n & NS_CMPRSFLGS) {
615 case 0: /* normal case, n == len */
618 for ((void)NULL; n > 0; n--)
619 if (mklower(*dn++) != mklower(*cp++))
621 /* Is next root for both ? */
622 if (*dn == '\0' && *cp == '\0')
628 case NS_CMPRSFLGS: /* indirection */
629 cp = msg + (((n & 0x3f) << 8) | *cp);
632 default: /* illegal type */