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";
46 * Thinking in noninternationalized USASCII (per the DNS spec),
47 * is this character special ("in need of quoting") ?
58 /* Special modifiers in zone files. */
69 * Thinking in noninternationalized USASCII (per the DNS spec),
70 * is this character visible and not a space when printed ?
76 return (ch > 0x20 && ch < 0x7f);
82 * dns_ns_name_ntop(src, dst, dstsiz)
83 * Convert an encoded domain name to printable ascii as per RFC1035.
85 * Number of bytes written to buffer, or -1 (with errno set)
87 * The root is returned as "."
88 * All other domains are returned in non absolute form
91 dns_ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
101 while ((n = *cp++) != 0) {
102 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
103 /* Some kind of compression pointer. */
115 if (dn + n * 2 + 4 >= eom) {
126 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
128 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
146 } else if (!printable(c)) {
151 *dn++ = digits[c / 100];
152 *dn++ = digits[(c % 100) / 10];
153 *dn++ = digits[c % 10];
177 * dns_ns_name_unpack(msg, eom, src, dst, dstsiz)
178 * Unpack a domain name from a message, source may be compressed.
180 * -1 if it fails, or consumed octets if it succeeds.
183 dns_ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
184 u_char *dst, size_t dstsiz)
186 const u_char *srcp, *dstlim;
194 dstlim = dst + dstsiz;
195 if (srcp < msg || srcp >= eom) {
198 /* Fetch next label in domain name. */
199 while ((n = *srcp++) != 0) {
200 /* Check for indirection. */
201 switch (n & NS_CMPRSFLGS) {
204 if (dstp + 1 >= dstlim) {
211 return (-1); /* flag error */
216 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
220 dstp = memcpy(dstp, srcp - 1, n + 1);
230 len = srcp - src + 1;
231 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
232 if (srcp < msg || srcp >= eom) { /* Out of range. */
237 * Check for loops in the compressed name;
238 * if we've looked at the whole message,
239 * there must be a loop.
241 if (checked >= eom - msg) {
247 return (-1); /* flag error */
258 * dns_ns_name_uncompress(msg, eom, src, dst, dstsiz)
259 * Expand compressed domain name to presentation format.
261 * Number of bytes read out of `src', or -1 (with errno set).
263 * Root domain returns as "." not "".
266 dns_ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
267 char *dst, size_t dstsiz)
269 u_char tmp[NS_MAXCDNAME];
272 if ((n = dns_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
274 if (dns_ns_name_ntop(tmp, dst, dstsiz) == -1)
281 * dns_ns_name_skip(ptrptr, eom)
282 * Advance *ptrptr to skip over the compressed name it points at.
284 * 0 on success, -1 (with errno set) on failure.
287 dns_ns_name_skip(const u_char **ptrptr, const u_char *eom) {
292 while (cp < eom && (n = *cp++) != 0) {
293 /* Check for indirection. */
294 switch (n & NS_CMPRSFLGS) {
295 case 0: /* normal case, n == len */
298 case NS_CMPRSFLGS: /* indirection */
301 default: /* illegal type */
313 #endif /* HAVE_RESOLV */