2 * Copyright (c) 1996 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
17 * $FreeBSD: src/lib/libc/net/ns_name.c,v 1.2 1999/08/28 00:00:14 peter Exp $
18 * $DragonFly: src/lib/libcr/net/Attic/ns_name.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
21 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
32 static char digits[] = "0123456789";
36 static int special(int);
37 static int printable(int);
38 static int dn_find(const u_char *, const u_char *,
39 const u_char * const *,
40 const u_char * const *);
45 * ns_name_ntop(src, dst, dstsiz)
46 * Convert an encoded domain name to printable ascii as per RFC1035.
48 * Number of bytes written to buffer, or -1 (with errno set)
50 * The root is returned as "."
51 * All other domains are returned in non absolute form
54 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
64 while ((n = *cp++) != 0) {
65 if ((n & NS_CMPRSFLGS) != 0) {
66 /* Some kind of compression pointer. */
81 for ((void)NULL; n > 0; n--) {
90 } else if (!printable(c)) {
96 *dn++ = digits[c / 100];
97 *dn++ = digits[(c % 100) / 10];
98 *dn++ = digits[c % 10];
124 * ns_name_pton(src, dst, dstsiz)
125 * Convert a ascii string into an encoded domain name as per RFC1035.
128 * 1 if string was fully qualified
129 * 0 is string was not fully qualified
131 * Enforces label and domain length limits.
135 ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
136 u_char *label, *bp, *eom;
145 while ((c = *src++) != 0) {
147 if ((cp = strchr(digits, c)) != NULL) {
148 n = (cp - digits) * 100;
149 if ((c = *src++) == 0 ||
150 (cp = strchr(digits, c)) == NULL) {
154 n += (cp - digits) * 10;
155 if ((c = *src++) == 0 ||
156 (cp = strchr(digits, c)) == NULL) {
168 } else if (c == '\\') {
171 } else if (c == '.') {
172 c = (bp - label - 1);
173 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
182 /* Fully qualified ? */
191 if ((bp - dst) > MAXCDNAME) {
210 c = (bp - label - 1);
211 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
227 if ((bp - dst) > MAXCDNAME) { /* src too big */
235 * ns_name_unpack(msg, eom, src, dst, dstsiz)
236 * Unpack a domain name from a message, source may be compressed.
238 * -1 if it fails, or consumed octets if it succeeds.
241 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
242 u_char *dst, size_t dstsiz)
244 const u_char *srcp, *dstlim;
246 int n, c, len, checked;
252 dstlim = dst + dstsiz;
253 if (srcp < msg || srcp >= eom) {
257 /* Fetch next label in domain name. */
258 while ((n = *srcp++) != 0) {
259 /* Check for indirection. */
260 switch (n & NS_CMPRSFLGS) {
263 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
269 memcpy(dstp, srcp, n);
280 len = srcp - src + 1;
281 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
282 if (srcp < msg || srcp >= eom) { /* Out of range. */
288 * Check for loops in the compressed name;
289 * if we've looked at the whole message,
290 * there must be a loop.
292 if (checked >= eom - msg) {
300 return (-1); /* flag error */
310 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
311 * Pack domain name 'domain' into 'comp_dn'.
313 * Size of the compressed name, or -1.
315 * 'dnptrs' is an array of pointers to previous compressed names.
316 * dnptrs[0] is a pointer to the beginning of the message. The array
318 * 'lastdnptr' is a pointer to the end of the array pointed to
321 * The list of pointers in dnptrs is updated for labels inserted into
322 * the message as we compress the name. If 'dnptr' is NULL, we don't
323 * try to compress names. If 'lastdnptr' is NULL, we don't update the
327 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
328 const u_char **dnptrs, const u_char **lastdnptr)
331 const u_char **cpp, **lpp, *eob, *msg;
339 if (dnptrs != NULL) {
340 if ((msg = *dnptrs++) != NULL) {
341 for (cpp = dnptrs; *cpp != NULL; cpp++)
343 lpp = cpp; /* end of list to search */
348 /* make sure the domain we are about to add is legal */
352 if ((n & NS_CMPRSFLGS) != 0) {
366 /* Look to see if we can use pointers. */
368 if (n != 0 && msg != NULL) {
369 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
370 (const u_char * const *)lpp);
372 if (dstp + 1 >= eob) {
376 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
380 /* Not found, save it. */
381 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
382 (dstp - msg) < 0x4000) {
387 /* copy label to buffer */
388 if (n & NS_CMPRSFLGS) { /* Should not happen. */
392 if (dstp + 1 + n >= eob) {
396 memcpy(dstp, srcp, n + 1);
411 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
412 * Expand compressed domain name to presentation format.
414 * Number of bytes read out of `src', or -1 (with errno set).
416 * Root domain returns as "." not "".
419 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
420 char *dst, size_t dstsiz)
422 u_char tmp[NS_MAXCDNAME];
425 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
427 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
433 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
434 * Compress a domain name into wire format, using compression pointers.
436 * Number of bytes consumed in `dst' or -1 (with errno set).
438 * 'dnptrs' is an array of pointers to previous compressed names.
439 * dnptrs[0] is a pointer to the beginning of the message.
440 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
441 * array pointed to by 'dnptrs'. Side effect is to update the list of
442 * pointers for labels inserted into the message as we compress the name.
443 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
444 * is NULL, we don't update the list.
447 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
448 const u_char **dnptrs, const u_char **lastdnptr)
450 u_char tmp[NS_MAXCDNAME];
452 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
454 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
458 * ns_name_skip(ptrptr, eom)
459 * Advance *ptrptr to skip over the compressed name it points at.
461 * 0 on success, -1 (with errno set) on failure.
464 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
469 while (cp < eom && (n = *cp++) != 0) {
470 /* Check for indirection. */
471 switch (n & NS_CMPRSFLGS) {
472 case 0: /* normal case, n == len */
475 case NS_CMPRSFLGS: /* indirection */
478 default: /* illegal type */
496 * Thinking in noninternationalized USASCII (per the DNS spec),
497 * is this characted special ("in need of quoting") ?
507 case 0x5C: /* '\\' */
508 /* Special modifiers in zone files. */
519 * Thinking in noninternationalized USASCII (per the DNS spec),
520 * is this character visible and not a space when printed ?
526 return (ch > 0x20 && ch < 0x7f);
530 * Thinking in noninternationalized USASCII (per the DNS spec),
531 * convert this character to lower case if it's upper case.
535 if (ch >= 0x41 && ch <= 0x5A)
541 * dn_find(domain, msg, dnptrs, lastdnptr)
542 * Search for the counted-label name in an array of compressed names.
544 * offset from msg if found, or -1.
546 * dnptrs is the pointer to the first name on the list,
547 * not the pointer to the start of the message.
550 dn_find(const u_char *domain, const u_char *msg,
551 const u_char * const *dnptrs,
552 const u_char * const *lastdnptr)
554 const u_char *dn, *cp, *sp;
555 const u_char * const *cpp;
558 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
561 while ((n = *cp++) != 0) {
563 * check for indirection
565 switch (n & NS_CMPRSFLGS) {
566 case 0: /* normal case, n == len */
569 for ((void)NULL; n > 0; n--)
570 if (mklower(*dn++) != mklower(*cp++))
572 /* Is next root for both ? */
573 if (*dn == '\0' && *cp == '\0')
579 case NS_CMPRSFLGS: /* indirection */
580 cp = msg + (((n & 0x3f) << 8) | *cp);
583 default: /* illegal type */