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
22 #include <sys/types.h>
24 #ifdef HAVE_NETINET_IN_H
25 # include <netinet/in.h>
27 #ifdef HAVE_ARPA_NAMESER_H
28 # include <arpa/nameser.h>
40 static const char digits[] = "0123456789";
44 static int special(int);
45 static int printable(int);
50 * dns_ns_name_ntop(src, dst, dstsiz)
51 * Convert an encoded domain name to printable ascii as per RFC1035.
53 * Number of bytes written to buffer, or -1 (with errno set)
55 * The root is returned as "."
56 * All other domains are returned in non absolute form
59 dns_ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
69 while ((n = *cp++) != 0) {
70 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
71 /* Some kind of compression pointer. */
83 if (dn + n * 2 + 4 >= eom) {
94 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
96 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
106 for ((void)NULL; n > 0; n--) {
114 } else if (!printable(c)) {
119 *dn++ = digits[c / 100];
120 *dn++ = digits[(c % 100) / 10];
121 *dn++ = digits[c % 10];
144 * dns_ns_name_pton(src, dst, dstsiz)
145 * Convert a ascii string into an encoded domain name as per RFC1035.
148 * 1 if string was fully qualified
149 * 0 is string was not fully qualified
151 * Enforces label and domain length limits.
155 dns_ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
156 u_char *label, *bp, *eom;
165 while ((c = *src++) != 0) {
167 if ((cp = strchr(digits, c)) != NULL) {
168 n = (cp - digits) * 100;
169 if ((c = *src++) == 0 ||
170 (cp = strchr(digits, c)) == NULL) {
173 n += (cp - digits) * 10;
174 if ((c = *src++) == 0 ||
175 (cp = strchr(digits, c)) == NULL) {
183 } else if (c == '[' && label == bp - 1 && *src == 'x') {
184 /* Theoretically we would have to handle \[o
185 as well but we do not since we do not need
190 while (isxdigit (*src)) {
191 n = *src > '9' ? *src - 'a' + 10 : *src - '0';
193 if (! isxdigit(*src)) {
197 n += *src > '9' ? *src - 'a' + 10 : *src - '0';
204 *label = (bp - label - 1) * 8;
205 if (*src++ != ']' || *src++ != '.') {
216 } else if (c == '\\') {
219 } else if (c == '.') {
220 c = (bp - label - 1);
221 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
228 /* Fully qualified ? */
236 if ((bp - dst) > NS_MAXCDNAME) {
241 if (c == 0 || *src == '.') {
252 c = (bp - label - 1);
253 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
266 if ((bp - dst) > NS_MAXCDNAME) { /* src too big */
274 * dns_ns_name_unpack(msg, eom, src, dst, dstsiz)
275 * Unpack a domain name from a message, source may be compressed.
277 * -1 if it fails, or consumed octets if it succeeds.
280 dns_ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
281 u_char *dst, size_t dstsiz)
283 const u_char *srcp, *dstlim;
291 dstlim = dst + dstsiz;
292 if (srcp < msg || srcp >= eom) {
295 /* Fetch next label in domain name. */
296 while ((n = *srcp++) != 0) {
297 /* Check for indirection. */
298 switch (n & NS_CMPRSFLGS) {
301 if (dstp + 1 >= dstlim) {
308 return (-1); /* flag error */
313 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
317 dstp = memcpy(dstp, srcp - 1, n + 1);
327 len = srcp - src + 1;
328 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
329 if (srcp < msg || srcp >= eom) { /* Out of range. */
334 * Check for loops in the compressed name;
335 * if we've looked at the whole message,
336 * there must be a loop.
338 if (checked >= eom - msg) {
344 return (-1); /* flag error */
355 * dns_ns_name_uncompress(msg, eom, src, dst, dstsiz)
356 * Expand compressed domain name to presentation format.
358 * Number of bytes read out of `src', or -1 (with errno set).
360 * Root domain returns as "." not "".
363 dns_ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
364 char *dst, size_t dstsiz)
366 u_char tmp[NS_MAXCDNAME];
369 if ((n = dns_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
371 if (dns_ns_name_ntop(tmp, dst, dstsiz) == -1)
378 * dns_ns_name_skip(ptrptr, eom)
379 * Advance *ptrptr to skip over the compressed name it points at.
381 * 0 on success, -1 (with errno set) on failure.
384 dns_ns_name_skip(const u_char **ptrptr, const u_char *eom) {
389 while (cp < eom && (n = *cp++) != 0) {
390 /* Check for indirection. */
391 switch (n & NS_CMPRSFLGS) {
392 case 0: /* normal case, n == len */
395 case NS_CMPRSFLGS: /* indirection */
398 default: /* illegal type */
414 * Thinking in noninternationalized USASCII (per the DNS spec),
415 * is this character special ("in need of quoting") ?
425 case 0x5C: /* '\\' */
426 /* Special modifiers in zone files. */
437 * Thinking in noninternationalized USASCII (per the DNS spec),
438 * is this character visible and not a space when printed ?
444 return (ch > 0x20 && ch < 0x7f);
447 #endif /* HAVE_RESOLV */