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
19 static char rcsid[] = "$FreeBSD: src/lib/libc/net/ns_name.c,v 1.2 1999/08/28 00:00:14 peter Exp $";
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
33 static char digits[] = "0123456789";
37 static int special(int);
38 static int printable(int);
39 static int dn_find(const u_char *, const u_char *,
40 const u_char * const *,
41 const u_char * const *);
46 * ns_name_ntop(src, dst, dstsiz)
47 * Convert an encoded domain name to printable ascii as per RFC1035.
49 * Number of bytes written to buffer, or -1 (with errno set)
51 * The root is returned as "."
52 * All other domains are returned in non absolute form
55 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
65 while ((n = *cp++) != 0) {
66 if ((n & NS_CMPRSFLGS) != 0) {
67 /* Some kind of compression pointer. */
82 for ((void)NULL; n > 0; n--) {
91 } else if (!printable(c)) {
97 *dn++ = digits[c / 100];
98 *dn++ = digits[(c % 100) / 10];
99 *dn++ = digits[c % 10];
125 * ns_name_pton(src, dst, dstsiz)
126 * Convert a ascii string into an encoded domain name as per RFC1035.
129 * 1 if string was fully qualified
130 * 0 is string was not fully qualified
132 * Enforces label and domain length limits.
136 ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
137 u_char *label, *bp, *eom;
146 while ((c = *src++) != 0) {
148 if ((cp = strchr(digits, c)) != NULL) {
149 n = (cp - digits) * 100;
150 if ((c = *src++) == 0 ||
151 (cp = strchr(digits, c)) == NULL) {
155 n += (cp - digits) * 10;
156 if ((c = *src++) == 0 ||
157 (cp = strchr(digits, c)) == NULL) {
169 } else if (c == '\\') {
172 } else if (c == '.') {
173 c = (bp - label - 1);
174 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
183 /* Fully qualified ? */
192 if ((bp - dst) > MAXCDNAME) {
211 c = (bp - label - 1);
212 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
228 if ((bp - dst) > MAXCDNAME) { /* src too big */
236 * ns_name_unpack(msg, eom, src, dst, dstsiz)
237 * Unpack a domain name from a message, source may be compressed.
239 * -1 if it fails, or consumed octets if it succeeds.
242 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
243 u_char *dst, size_t dstsiz)
245 const u_char *srcp, *dstlim;
247 int n, c, len, checked;
253 dstlim = dst + dstsiz;
254 if (srcp < msg || srcp >= eom) {
258 /* Fetch next label in domain name. */
259 while ((n = *srcp++) != 0) {
260 /* Check for indirection. */
261 switch (n & NS_CMPRSFLGS) {
264 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
270 memcpy(dstp, srcp, n);
281 len = srcp - src + 1;
282 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
283 if (srcp < msg || srcp >= eom) { /* Out of range. */
289 * Check for loops in the compressed name;
290 * if we've looked at the whole message,
291 * there must be a loop.
293 if (checked >= eom - msg) {
301 return (-1); /* flag error */
311 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
312 * Pack domain name 'domain' into 'comp_dn'.
314 * Size of the compressed name, or -1.
316 * 'dnptrs' is an array of pointers to previous compressed names.
317 * dnptrs[0] is a pointer to the beginning of the message. The array
319 * 'lastdnptr' is a pointer to the end of the array pointed to
322 * The list of pointers in dnptrs is updated for labels inserted into
323 * the message as we compress the name. If 'dnptr' is NULL, we don't
324 * try to compress names. If 'lastdnptr' is NULL, we don't update the
328 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
329 const u_char **dnptrs, const u_char **lastdnptr)
332 const u_char **cpp, **lpp, *eob, *msg;
340 if (dnptrs != NULL) {
341 if ((msg = *dnptrs++) != NULL) {
342 for (cpp = dnptrs; *cpp != NULL; cpp++)
344 lpp = cpp; /* end of list to search */
349 /* make sure the domain we are about to add is legal */
353 if ((n & NS_CMPRSFLGS) != 0) {
367 /* Look to see if we can use pointers. */
369 if (n != 0 && msg != NULL) {
370 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
371 (const u_char * const *)lpp);
373 if (dstp + 1 >= eob) {
377 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
381 /* Not found, save it. */
382 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
383 (dstp - msg) < 0x4000) {
388 /* copy label to buffer */
389 if (n & NS_CMPRSFLGS) { /* Should not happen. */
393 if (dstp + 1 + n >= eob) {
397 memcpy(dstp, srcp, n + 1);
412 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
413 * Expand compressed domain name to presentation format.
415 * Number of bytes read out of `src', or -1 (with errno set).
417 * Root domain returns as "." not "".
420 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
421 char *dst, size_t dstsiz)
423 u_char tmp[NS_MAXCDNAME];
426 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
428 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
434 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
435 * Compress a domain name into wire format, using compression pointers.
437 * Number of bytes consumed in `dst' or -1 (with errno set).
439 * 'dnptrs' is an array of pointers to previous compressed names.
440 * dnptrs[0] is a pointer to the beginning of the message.
441 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
442 * array pointed to by 'dnptrs'. Side effect is to update the list of
443 * pointers for labels inserted into the message as we compress the name.
444 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
445 * is NULL, we don't update the list.
448 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
449 const u_char **dnptrs, const u_char **lastdnptr)
451 u_char tmp[NS_MAXCDNAME];
453 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
455 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
459 * ns_name_skip(ptrptr, eom)
460 * Advance *ptrptr to skip over the compressed name it points at.
462 * 0 on success, -1 (with errno set) on failure.
465 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
470 while (cp < eom && (n = *cp++) != 0) {
471 /* Check for indirection. */
472 switch (n & NS_CMPRSFLGS) {
473 case 0: /* normal case, n == len */
476 case NS_CMPRSFLGS: /* indirection */
479 default: /* illegal type */
497 * Thinking in noninternationalized USASCII (per the DNS spec),
498 * is this characted special ("in need of quoting") ?
508 case 0x5C: /* '\\' */
509 /* Special modifiers in zone files. */
520 * Thinking in noninternationalized USASCII (per the DNS spec),
521 * is this character visible and not a space when printed ?
527 return (ch > 0x20 && ch < 0x7f);
531 * Thinking in noninternationalized USASCII (per the DNS spec),
532 * convert this character to lower case if it's upper case.
536 if (ch >= 0x41 && ch <= 0x5A)
542 * dn_find(domain, msg, dnptrs, lastdnptr)
543 * Search for the counted-label name in an array of compressed names.
545 * offset from msg if found, or -1.
547 * dnptrs is the pointer to the first name on the list,
548 * not the pointer to the start of the message.
551 dn_find(const u_char *domain, const u_char *msg,
552 const u_char * const *dnptrs,
553 const u_char * const *lastdnptr)
555 const u_char *dn, *cp, *sp;
556 const u_char * const *cpp;
559 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
562 while ((n = *cp++) != 0) {
564 * check for indirection
566 switch (n & NS_CMPRSFLGS) {
567 case 0: /* normal case, n == len */
570 for ((void)NULL; n > 0; n--)
571 if (mklower(*dn++) != mklower(*cp++))
573 /* Is next root for both ? */
574 if (*dn == '\0' && *cp == '\0')
580 case NS_CMPRSFLGS: /* indirection */
581 cp = msg + (((n & 0x3f) << 8) | *cp);
584 default: /* illegal type */