xref: /onnv-gate/usr/src/lib/libresolv2/common/resolv/res_debug.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
6*11038SRao.Shoaib@Sun.COM 
7*11038SRao.Shoaib@Sun.COM /*
8*11038SRao.Shoaib@Sun.COM  * Portions Copyright (C) 2004, 2005, 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
9*11038SRao.Shoaib@Sun.COM  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
10*11038SRao.Shoaib@Sun.COM  *
11*11038SRao.Shoaib@Sun.COM  * Permission to use, copy, modify, and/or distribute this software for any
12*11038SRao.Shoaib@Sun.COM  * purpose with or without fee is hereby granted, provided that the above
13*11038SRao.Shoaib@Sun.COM  * copyright notice and this permission notice appear in all copies.
14*11038SRao.Shoaib@Sun.COM  *
15*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16*11038SRao.Shoaib@Sun.COM  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17*11038SRao.Shoaib@Sun.COM  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18*11038SRao.Shoaib@Sun.COM  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19*11038SRao.Shoaib@Sun.COM  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20*11038SRao.Shoaib@Sun.COM  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*11038SRao.Shoaib@Sun.COM  * PERFORMANCE OF THIS SOFTWARE.
22*11038SRao.Shoaib@Sun.COM  */
23*11038SRao.Shoaib@Sun.COM 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * Copyright (c) 1985
260Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
27*11038SRao.Shoaib@Sun.COM  *
280Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
290Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
300Sstevel@tonic-gate  * are met:
310Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
320Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
330Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
340Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
350Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
360Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
370Sstevel@tonic-gate  *    must display the following acknowledgement:
380Sstevel@tonic-gate  * 	This product includes software developed by the University of
390Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
400Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
410Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
420Sstevel@tonic-gate  *    without specific prior written permission.
43*11038SRao.Shoaib@Sun.COM  *
440Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
450Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
470Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
480Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
490Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
500Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
510Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
520Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
530Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
540Sstevel@tonic-gate  * SUCH DAMAGE.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59*11038SRao.Shoaib@Sun.COM  *
600Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
610Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
620Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
630Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
640Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
650Sstevel@tonic-gate  * specific, written prior permission.
66*11038SRao.Shoaib@Sun.COM  *
670Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
680Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
690Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
700Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
710Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
720Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
730Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
740Sstevel@tonic-gate  * SOFTWARE.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Portions Copyright (c) 1995 by International Business Machines, Inc.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * International Business Machines, Inc. (hereinafter called IBM) grants
810Sstevel@tonic-gate  * permission under its copyrights to use, copy, modify, and distribute this
820Sstevel@tonic-gate  * Software with or without fee, provided that the above copyright notice and
830Sstevel@tonic-gate  * all paragraphs of this notice appear in all copies, and that the name of IBM
840Sstevel@tonic-gate  * not be used in connection with the marketing of any product incorporating
850Sstevel@tonic-gate  * the Software or modifications thereof, without specific, written prior
860Sstevel@tonic-gate  * permission.
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * To the extent it has a right to do so, IBM grants an immunity from suit
890Sstevel@tonic-gate  * under its patents, if any, for the use, sale or manufacture of products to
900Sstevel@tonic-gate  * the extent that such products are used for performing Domain Name System
910Sstevel@tonic-gate  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
920Sstevel@tonic-gate  * granted for any product per se or for any other function of any product.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
950Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
960Sstevel@tonic-gate  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
970Sstevel@tonic-gate  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
980Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
990Sstevel@tonic-gate  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
1030Sstevel@tonic-gate static const char sccsid[] = "@(#)res_debug.c	8.1 (Berkeley) 6/4/93";
104*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp $";
1050Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #include "port_before.h"
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #include <sys/types.h>
1100Sstevel@tonic-gate #include <sys/param.h>
1110Sstevel@tonic-gate #include <sys/socket.h>
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #include <netinet/in.h>
1140Sstevel@tonic-gate #include <arpa/inet.h>
1150Sstevel@tonic-gate #include <arpa/nameser.h>
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #include <ctype.h>
1180Sstevel@tonic-gate #include <errno.h>
1190Sstevel@tonic-gate #include <math.h>
1200Sstevel@tonic-gate #include <netdb.h>
1210Sstevel@tonic-gate #include <resolv.h>
122*11038SRao.Shoaib@Sun.COM #include <resolv_mt.h>
1230Sstevel@tonic-gate #include <stdio.h>
1240Sstevel@tonic-gate #include <stdlib.h>
1250Sstevel@tonic-gate #include <string.h>
1260Sstevel@tonic-gate #include <time.h>
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #include "port_after.h"
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #ifdef SPRINTF_CHAR
1310Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
1320Sstevel@tonic-gate #else
1330Sstevel@tonic-gate # define SPRINTF(x) sprintf x
1340Sstevel@tonic-gate #endif
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate extern const char *_res_opcodes[];
1370Sstevel@tonic-gate extern const char *_res_sectioncodes[];
1380Sstevel@tonic-gate 
139*11038SRao.Shoaib@Sun.COM #ifndef	ORIGINAL_ISC_CODE
1400Sstevel@tonic-gate #pragma weak	__dn_count_labels	=	dn_count_labels
1410Sstevel@tonic-gate #pragma weak	__fp_resstat		=	fp_resstat
1420Sstevel@tonic-gate #pragma weak	__loc_aton		=	loc_aton
1430Sstevel@tonic-gate #pragma weak	__loc_ntoa		=	loc_ntoa
1440Sstevel@tonic-gate #pragma weak	__p_cdname		=	p_cdname
1450Sstevel@tonic-gate #pragma weak	__p_class		=	p_class
1460Sstevel@tonic-gate #pragma weak	__p_section		=	p_section
1470Sstevel@tonic-gate #pragma weak	__p_time		=	p_time
1480Sstevel@tonic-gate #pragma weak	__p_type		=	p_type
1490Sstevel@tonic-gate #pragma weak	__sym_ntop		=	sym_ntop
1500Sstevel@tonic-gate #pragma weak	__sym_ntos		=	sym_ntos
1510Sstevel@tonic-gate #pragma weak	__sym_ston		=	sym_ston
1520Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
1530Sstevel@tonic-gate 
154*11038SRao.Shoaib@Sun.COM /*%
1550Sstevel@tonic-gate  * Print the current options.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate void
fp_resstat(const res_state statp,FILE * file)1580Sstevel@tonic-gate fp_resstat(const res_state statp, FILE *file) {
1590Sstevel@tonic-gate 	u_long mask;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	fprintf(file, ";; res options:");
162*11038SRao.Shoaib@Sun.COM 	for (mask = 1;  mask != 0U;  mask <<= 1)
1630Sstevel@tonic-gate 		if (statp->options & mask)
1640Sstevel@tonic-gate 			fprintf(file, " %s", p_option(mask));
1650Sstevel@tonic-gate 	putc('\n', file);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static void
do_section(const res_state statp,ns_msg * handle,ns_sect section,int pflag,FILE * file)1690Sstevel@tonic-gate do_section(const res_state statp,
1700Sstevel@tonic-gate 	   ns_msg *handle, ns_sect section,
1710Sstevel@tonic-gate 	   int pflag, FILE *file)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	int n, sflag, rrnum;
1740Sstevel@tonic-gate 	static int buflen = 2048;
1750Sstevel@tonic-gate 	char *buf;
1760Sstevel@tonic-gate 	ns_opcode opcode;
1770Sstevel@tonic-gate 	ns_rr rr;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Print answer records.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	sflag = (statp->pfcode & pflag);
1830Sstevel@tonic-gate 	if (statp->pfcode && !sflag)
1840Sstevel@tonic-gate 		return;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	buf = malloc(buflen);
1870Sstevel@tonic-gate 	if (buf == NULL) {
1880Sstevel@tonic-gate 		fprintf(file, ";; memory allocation failure\n");
1890Sstevel@tonic-gate 		return;
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
1930Sstevel@tonic-gate 	rrnum = 0;
1940Sstevel@tonic-gate 	for (;;) {
1950Sstevel@tonic-gate 		if (ns_parserr(handle, section, rrnum, &rr)) {
1960Sstevel@tonic-gate 			if (errno != ENODEV)
1970Sstevel@tonic-gate 				fprintf(file, ";; ns_parserr: %s\n",
1980Sstevel@tonic-gate 					strerror(errno));
1990Sstevel@tonic-gate 			else if (rrnum > 0 && sflag != 0 &&
2000Sstevel@tonic-gate 				 (statp->pfcode & RES_PRF_HEAD1))
2010Sstevel@tonic-gate 				putc('\n', file);
2020Sstevel@tonic-gate 			goto cleanup;
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
2050Sstevel@tonic-gate 			fprintf(file, ";; %s SECTION:\n",
2060Sstevel@tonic-gate 				p_section(section, opcode));
2070Sstevel@tonic-gate 		if (section == ns_s_qd)
2080Sstevel@tonic-gate 			fprintf(file, ";;\t%s, type = %s, class = %s\n",
2090Sstevel@tonic-gate 				ns_rr_name(rr),
2100Sstevel@tonic-gate 				p_type(ns_rr_type(rr)),
2110Sstevel@tonic-gate 				p_class(ns_rr_class(rr)));
2120Sstevel@tonic-gate 		else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
213*11038SRao.Shoaib@Sun.COM 			u_int16_t optcode, optlen, rdatalen = ns_rr_rdlen(rr);
2140Sstevel@tonic-gate 			u_int32_t ttl = ns_rr_ttl(rr);
215*11038SRao.Shoaib@Sun.COM 
2160Sstevel@tonic-gate 			fprintf(file,
2170Sstevel@tonic-gate 				"; EDNS: version: %u, udp=%u, flags=%04x\n",
2180Sstevel@tonic-gate 				(ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
219*11038SRao.Shoaib@Sun.COM 
220*11038SRao.Shoaib@Sun.COM 			while (rdatalen >= 4) {
221*11038SRao.Shoaib@Sun.COM 				const u_char *cp = ns_rr_rdata(rr);
222*11038SRao.Shoaib@Sun.COM 				int i;
223*11038SRao.Shoaib@Sun.COM 
224*11038SRao.Shoaib@Sun.COM 				GETSHORT(optcode, cp);
225*11038SRao.Shoaib@Sun.COM 				GETSHORT(optlen, cp);
226*11038SRao.Shoaib@Sun.COM 
227*11038SRao.Shoaib@Sun.COM 				if (optcode == NS_OPT_NSID) {
228*11038SRao.Shoaib@Sun.COM 					fputs("; NSID: ", file);
229*11038SRao.Shoaib@Sun.COM 					if (optlen == 0) {
230*11038SRao.Shoaib@Sun.COM 						fputs("; NSID\n", file);
231*11038SRao.Shoaib@Sun.COM 					} else {
232*11038SRao.Shoaib@Sun.COM 						fputs("; NSID: ", file);
233*11038SRao.Shoaib@Sun.COM 						for (i = 0; i < optlen; i++)
234*11038SRao.Shoaib@Sun.COM 							fprintf(file, "%02x ",
235*11038SRao.Shoaib@Sun.COM 								cp[i]);
236*11038SRao.Shoaib@Sun.COM 						fputs(" (",file);
237*11038SRao.Shoaib@Sun.COM 						for (i = 0; i < optlen; i++)
238*11038SRao.Shoaib@Sun.COM 							fprintf(file, "%c",
239*11038SRao.Shoaib@Sun.COM 								isprint(cp[i])?
240*11038SRao.Shoaib@Sun.COM 								cp[i] : '.');
241*11038SRao.Shoaib@Sun.COM 						fputs(")\n", file);
242*11038SRao.Shoaib@Sun.COM 					}
243*11038SRao.Shoaib@Sun.COM 				} else {
244*11038SRao.Shoaib@Sun.COM 					if (optlen == 0) {
245*11038SRao.Shoaib@Sun.COM 						fprintf(file, "; OPT=%u\n",
246*11038SRao.Shoaib@Sun.COM 							optcode);
247*11038SRao.Shoaib@Sun.COM 					} else {
248*11038SRao.Shoaib@Sun.COM 						fprintf(file, "; OPT=%u: ",
249*11038SRao.Shoaib@Sun.COM 							optcode);
250*11038SRao.Shoaib@Sun.COM 						for (i = 0; i < optlen; i++)
251*11038SRao.Shoaib@Sun.COM 							fprintf(file, "%02x ",
252*11038SRao.Shoaib@Sun.COM 								cp[i]);
253*11038SRao.Shoaib@Sun.COM 						fputs(" (",file);
254*11038SRao.Shoaib@Sun.COM 						for (i = 0; i < optlen; i++)
255*11038SRao.Shoaib@Sun.COM 							fprintf(file, "%c",
256*11038SRao.Shoaib@Sun.COM 								isprint(cp[i]) ?
257*11038SRao.Shoaib@Sun.COM 									cp[i] : '.');
258*11038SRao.Shoaib@Sun.COM 						fputs(")\n", file);
259*11038SRao.Shoaib@Sun.COM 					}
260*11038SRao.Shoaib@Sun.COM 				}
261*11038SRao.Shoaib@Sun.COM 				rdatalen -= 4 + optlen;
262*11038SRao.Shoaib@Sun.COM 			}
2630Sstevel@tonic-gate 		} else {
2640Sstevel@tonic-gate 			n = ns_sprintrr(handle, &rr, NULL, NULL,
2650Sstevel@tonic-gate 					buf, buflen);
2660Sstevel@tonic-gate 			if (n < 0) {
2670Sstevel@tonic-gate 				if (errno == ENOSPC) {
2680Sstevel@tonic-gate 					free(buf);
2690Sstevel@tonic-gate 					buf = NULL;
2700Sstevel@tonic-gate 					if (buflen < 131072)
2710Sstevel@tonic-gate 						buf = malloc(buflen += 1024);
2720Sstevel@tonic-gate 					if (buf == NULL) {
2730Sstevel@tonic-gate 						fprintf(file,
274*11038SRao.Shoaib@Sun.COM 					      ";; memory allocation failure\n");
2750Sstevel@tonic-gate 					      return;
2760Sstevel@tonic-gate 					}
2770Sstevel@tonic-gate 					continue;
2780Sstevel@tonic-gate 				}
2790Sstevel@tonic-gate 				fprintf(file, ";; ns_sprintrr: %s\n",
2800Sstevel@tonic-gate 					strerror(errno));
2810Sstevel@tonic-gate 				goto cleanup;
2820Sstevel@tonic-gate 			}
2830Sstevel@tonic-gate 			fputs(buf, file);
2840Sstevel@tonic-gate 			fputc('\n', file);
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		rrnum++;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate  cleanup:
2890Sstevel@tonic-gate 	if (buf != NULL)
2900Sstevel@tonic-gate 		free(buf);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
293*11038SRao.Shoaib@Sun.COM /*%
2940Sstevel@tonic-gate  * Print the contents of a query.
2950Sstevel@tonic-gate  * This is intended to be primarily a debugging routine.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate void
res_pquery(const res_state statp,const u_char * msg,int len,FILE * file)2980Sstevel@tonic-gate res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
2990Sstevel@tonic-gate 	ns_msg handle;
3000Sstevel@tonic-gate 	int qdcount, ancount, nscount, arcount;
3010Sstevel@tonic-gate 	u_int opcode, rcode, id;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (ns_initparse(msg, len, &handle) < 0) {
3040Sstevel@tonic-gate 		fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
3050Sstevel@tonic-gate 		return;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	opcode = ns_msg_getflag(handle, ns_f_opcode);
3080Sstevel@tonic-gate 	rcode = ns_msg_getflag(handle, ns_f_rcode);
3090Sstevel@tonic-gate 	id = ns_msg_id(handle);
3100Sstevel@tonic-gate 	qdcount = ns_msg_count(handle, ns_s_qd);
3110Sstevel@tonic-gate 	ancount = ns_msg_count(handle, ns_s_an);
3120Sstevel@tonic-gate 	nscount = ns_msg_count(handle, ns_s_ns);
3130Sstevel@tonic-gate 	arcount = ns_msg_count(handle, ns_s_ar);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * Print header fields.
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
3190Sstevel@tonic-gate 		fprintf(file,
3200Sstevel@tonic-gate 			";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
3210Sstevel@tonic-gate 			_res_opcodes[opcode], p_rcode(rcode), id);
3220Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
3230Sstevel@tonic-gate 		putc(';', file);
3240Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
3250Sstevel@tonic-gate 		fprintf(file, "; flags:");
3260Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_qr))
3270Sstevel@tonic-gate 			fprintf(file, " qr");
3280Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_aa))
3290Sstevel@tonic-gate 			fprintf(file, " aa");
3300Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_tc))
3310Sstevel@tonic-gate 			fprintf(file, " tc");
3320Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_rd))
3330Sstevel@tonic-gate 			fprintf(file, " rd");
3340Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ra))
3350Sstevel@tonic-gate 			fprintf(file, " ra");
3360Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_z))
3370Sstevel@tonic-gate 			fprintf(file, " ??");
3380Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ad))
3390Sstevel@tonic-gate 			fprintf(file, " ad");
3400Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_cd))
3410Sstevel@tonic-gate 			fprintf(file, " cd");
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
3440Sstevel@tonic-gate 		fprintf(file, "; %s: %d",
3450Sstevel@tonic-gate 			p_section(ns_s_qd, opcode), qdcount);
3460Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3470Sstevel@tonic-gate 			p_section(ns_s_an, opcode), ancount);
3480Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3490Sstevel@tonic-gate 			p_section(ns_s_ns, opcode), nscount);
3500Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3510Sstevel@tonic-gate 			p_section(ns_s_ar, opcode), arcount);
3520Sstevel@tonic-gate 	}
353*11038SRao.Shoaib@Sun.COM 	if ((!statp->pfcode) || (statp->pfcode &
3540Sstevel@tonic-gate 		(RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
3550Sstevel@tonic-gate 		putc('\n',file);
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * Print the various sections.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
3610Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
3620Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
3630Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
3640Sstevel@tonic-gate 	if (qdcount == 0 && ancount == 0 &&
3650Sstevel@tonic-gate 	    nscount == 0 && arcount == 0)
3660Sstevel@tonic-gate 		putc('\n', file);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate const u_char *
p_cdnname(const u_char * cp,const u_char * msg,int len,FILE * file)3700Sstevel@tonic-gate p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
3710Sstevel@tonic-gate 	char name[MAXDNAME];
3720Sstevel@tonic-gate 	int n;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
3750Sstevel@tonic-gate 		return (NULL);
3760Sstevel@tonic-gate 	if (name[0] == '\0')
3770Sstevel@tonic-gate 		putc('.', file);
3780Sstevel@tonic-gate 	else
3790Sstevel@tonic-gate 		fputs(name, file);
3800Sstevel@tonic-gate 	return (cp + n);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate const u_char *
p_cdname(const u_char * cp,const u_char * msg,FILE * file)3840Sstevel@tonic-gate p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
3850Sstevel@tonic-gate 	return (p_cdnname(cp, msg, PACKETSZ, file));
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
388*11038SRao.Shoaib@Sun.COM /*%
389*11038SRao.Shoaib@Sun.COM  * Return a fully-qualified domain name from a compressed name (with
3900Sstevel@tonic-gate    length supplied).  */
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate const u_char *
p_fqnname(cp,msg,msglen,name,namelen)3930Sstevel@tonic-gate p_fqnname(cp, msg, msglen, name, namelen)
3940Sstevel@tonic-gate 	const u_char *cp, *msg;
3950Sstevel@tonic-gate 	int msglen;
3960Sstevel@tonic-gate 	char *name;
3970Sstevel@tonic-gate 	int namelen;
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	int n, newlen;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
4020Sstevel@tonic-gate 		return (NULL);
4030Sstevel@tonic-gate 	newlen = strlen(name);
4040Sstevel@tonic-gate 	if (newlen == 0 || name[newlen - 1] != '.') {
405*11038SRao.Shoaib@Sun.COM 		if (newlen + 1 >= namelen)	/*%< Lack space for final dot */
4060Sstevel@tonic-gate 			return (NULL);
4070Sstevel@tonic-gate 		else
4080Sstevel@tonic-gate 			strcpy(name + newlen, ".");
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	return (cp + n);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /* XXX:	the rest of these functions need to become length-limited, too. */
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate const u_char *
p_fqname(const u_char * cp,const u_char * msg,FILE * file)4160Sstevel@tonic-gate p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
4170Sstevel@tonic-gate 	char name[MAXDNAME];
4180Sstevel@tonic-gate 	const u_char *n;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
4210Sstevel@tonic-gate 	if (n == NULL)
4220Sstevel@tonic-gate 		return (NULL);
4230Sstevel@tonic-gate 	fputs(name, file);
4240Sstevel@tonic-gate 	return (n);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
427*11038SRao.Shoaib@Sun.COM /*%
4280Sstevel@tonic-gate  * Names of RR classes and qclasses.  Classes and qclasses are the same, except
4290Sstevel@tonic-gate  * that C_ANY is a qclass but not a class.  (You can ask for records of class
4300Sstevel@tonic-gate  * C_ANY, but you can't have any records of that class in the database.)
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate const struct res_sym __p_class_syms[] = {
4330Sstevel@tonic-gate 	{C_IN,		"IN",		(char *)0},
434*11038SRao.Shoaib@Sun.COM 	{C_CHAOS,	"CH",		(char *)0},
4350Sstevel@tonic-gate 	{C_CHAOS,	"CHAOS",	(char *)0},
4360Sstevel@tonic-gate 	{C_HS,		"HS",		(char *)0},
4370Sstevel@tonic-gate 	{C_HS,		"HESIOD",	(char *)0},
4380Sstevel@tonic-gate 	{C_ANY,		"ANY",		(char *)0},
4390Sstevel@tonic-gate 	{C_NONE,	"NONE",		(char *)0},
4400Sstevel@tonic-gate 	{C_IN, 		(char *)0,	(char *)0}
4410Sstevel@tonic-gate };
4420Sstevel@tonic-gate 
443*11038SRao.Shoaib@Sun.COM /*%
4440Sstevel@tonic-gate  * Names of message sections.
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate const struct res_sym __p_default_section_syms[] = {
4470Sstevel@tonic-gate 	{ns_s_qd,	"QUERY",	(char *)0},
4480Sstevel@tonic-gate 	{ns_s_an,	"ANSWER",	(char *)0},
4490Sstevel@tonic-gate 	{ns_s_ns,	"AUTHORITY",	(char *)0},
4500Sstevel@tonic-gate 	{ns_s_ar,	"ADDITIONAL",	(char *)0},
451*11038SRao.Shoaib@Sun.COM 	{0,		(char *)0,	(char *)0}
4520Sstevel@tonic-gate };
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate const struct res_sym __p_update_section_syms[] = {
4550Sstevel@tonic-gate 	{S_ZONE,	"ZONE",		(char *)0},
4560Sstevel@tonic-gate 	{S_PREREQ,	"PREREQUISITE",	(char *)0},
4570Sstevel@tonic-gate 	{S_UPDATE,	"UPDATE",	(char *)0},
4580Sstevel@tonic-gate 	{S_ADDT,	"ADDITIONAL",	(char *)0},
459*11038SRao.Shoaib@Sun.COM 	{0,		(char *)0,	(char *)0}
4600Sstevel@tonic-gate };
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate const struct res_sym __p_key_syms[] = {
4630Sstevel@tonic-gate 	{NS_ALG_MD5RSA,		"RSA",		"RSA KEY with MD5 hash"},
4640Sstevel@tonic-gate 	{NS_ALG_DH,		"DH",		"Diffie Hellman"},
4650Sstevel@tonic-gate 	{NS_ALG_DSA,		"DSA",		"Digital Signature Algorithm"},
4660Sstevel@tonic-gate 	{NS_ALG_EXPIRE_ONLY,	"EXPIREONLY",	"No algorithm"},
4670Sstevel@tonic-gate 	{NS_ALG_PRIVATE_OID,	"PRIVATE",	"Algorithm obtained from OID"},
4680Sstevel@tonic-gate 	{0,			NULL,		NULL}
4690Sstevel@tonic-gate };
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate const struct res_sym __p_cert_syms[] = {
4720Sstevel@tonic-gate 	{cert_t_pkix,	"PKIX",		"PKIX (X.509v3) Certificate"},
4730Sstevel@tonic-gate 	{cert_t_spki,	"SPKI",		"SPKI certificate"},
4740Sstevel@tonic-gate 	{cert_t_pgp,	"PGP",		"PGP certificate"},
4750Sstevel@tonic-gate 	{cert_t_url,	"URL",		"URL Private"},
4760Sstevel@tonic-gate 	{cert_t_oid,	"OID",		"OID Private"},
4770Sstevel@tonic-gate 	{0,		NULL,		NULL}
4780Sstevel@tonic-gate };
4790Sstevel@tonic-gate 
480*11038SRao.Shoaib@Sun.COM /*%
4810Sstevel@tonic-gate  * Names of RR types and qtypes.  Types and qtypes are the same, except
4820Sstevel@tonic-gate  * that T_ANY is a qtype but not a type.  (You can ask for records of type
4830Sstevel@tonic-gate  * T_ANY, but you can't have any records of that type in the database.)
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate const struct res_sym __p_type_syms[] = {
4860Sstevel@tonic-gate 	{ns_t_a,	"A",		"address"},
4870Sstevel@tonic-gate 	{ns_t_ns,	"NS",		"name server"},
4880Sstevel@tonic-gate 	{ns_t_md,	"MD",		"mail destination (deprecated)"},
4890Sstevel@tonic-gate 	{ns_t_mf,	"MF",		"mail forwarder (deprecated)"},
4900Sstevel@tonic-gate 	{ns_t_cname,	"CNAME",	"canonical name"},
4910Sstevel@tonic-gate 	{ns_t_soa,	"SOA",		"start of authority"},
4920Sstevel@tonic-gate 	{ns_t_mb,	"MB",		"mailbox"},
4930Sstevel@tonic-gate 	{ns_t_mg,	"MG",		"mail group member"},
4940Sstevel@tonic-gate 	{ns_t_mr,	"MR",		"mail rename"},
4950Sstevel@tonic-gate 	{ns_t_null,	"NULL",		"null"},
4960Sstevel@tonic-gate 	{ns_t_wks,	"WKS",		"well-known service (deprecated)"},
4970Sstevel@tonic-gate 	{ns_t_ptr,	"PTR",		"domain name pointer"},
4980Sstevel@tonic-gate 	{ns_t_hinfo,	"HINFO",	"host information"},
4990Sstevel@tonic-gate 	{ns_t_minfo,	"MINFO",	"mailbox information"},
5000Sstevel@tonic-gate 	{ns_t_mx,	"MX",		"mail exchanger"},
5010Sstevel@tonic-gate 	{ns_t_txt,	"TXT",		"text"},
5020Sstevel@tonic-gate 	{ns_t_rp,	"RP",		"responsible person"},
5030Sstevel@tonic-gate 	{ns_t_afsdb,	"AFSDB",	"DCE or AFS server"},
5040Sstevel@tonic-gate 	{ns_t_x25,	"X25",		"X25 address"},
5050Sstevel@tonic-gate 	{ns_t_isdn,	"ISDN",		"ISDN address"},
5060Sstevel@tonic-gate 	{ns_t_rt,	"RT",		"router"},
5070Sstevel@tonic-gate 	{ns_t_nsap,	"NSAP",		"nsap address"},
5080Sstevel@tonic-gate 	{ns_t_nsap_ptr,	"NSAP_PTR",	"domain name pointer"},
5090Sstevel@tonic-gate 	{ns_t_sig,	"SIG",		"signature"},
5100Sstevel@tonic-gate 	{ns_t_key,	"KEY",		"key"},
5110Sstevel@tonic-gate 	{ns_t_px,	"PX",		"mapping information"},
5120Sstevel@tonic-gate 	{ns_t_gpos,	"GPOS",		"geographical position (withdrawn)"},
5130Sstevel@tonic-gate 	{ns_t_aaaa,	"AAAA",		"IPv6 address"},
5140Sstevel@tonic-gate 	{ns_t_loc,	"LOC",		"location"},
5150Sstevel@tonic-gate 	{ns_t_nxt,	"NXT",		"next valid name (unimplemented)"},
5160Sstevel@tonic-gate 	{ns_t_eid,	"EID",		"endpoint identifier (unimplemented)"},
5170Sstevel@tonic-gate 	{ns_t_nimloc,	"NIMLOC",	"NIMROD locator (unimplemented)"},
5180Sstevel@tonic-gate 	{ns_t_srv,	"SRV",		"server selection"},
5190Sstevel@tonic-gate 	{ns_t_atma,	"ATMA",		"ATM address (unimplemented)"},
520*11038SRao.Shoaib@Sun.COM 	{ns_t_naptr,	"NAPTR",	"naptr"},
521*11038SRao.Shoaib@Sun.COM 	{ns_t_kx,	"KX",		"key exchange"},
522*11038SRao.Shoaib@Sun.COM 	{ns_t_cert,	"CERT",		"certificate"},
523*11038SRao.Shoaib@Sun.COM 	{ns_t_a6,	"A",		"IPv6 address (experminental)"},
524*11038SRao.Shoaib@Sun.COM 	{ns_t_dname,	"DNAME",	"non-terminal redirection"},
525*11038SRao.Shoaib@Sun.COM 	{ns_t_opt,	"OPT",		"opt"},
526*11038SRao.Shoaib@Sun.COM 	{ns_t_apl,	"apl",		"apl"},
527*11038SRao.Shoaib@Sun.COM 	{ns_t_ds,	"DS",		"delegation signer"},
528*11038SRao.Shoaib@Sun.COM 	{ns_t_sshfp,	"SSFP",		"SSH fingerprint"},
529*11038SRao.Shoaib@Sun.COM 	{ns_t_ipseckey,	"IPSECKEY",	"IPSEC key"},
530*11038SRao.Shoaib@Sun.COM 	{ns_t_rrsig,	"RRSIG",	"rrsig"},
531*11038SRao.Shoaib@Sun.COM 	{ns_t_nsec,	"NSEC",		"nsec"},
532*11038SRao.Shoaib@Sun.COM 	{ns_t_dnskey,	"DNSKEY",	"DNS key"},
533*11038SRao.Shoaib@Sun.COM 	{ns_t_dhcid,	"DHCID",       "dynamic host configuration identifier"},
534*11038SRao.Shoaib@Sun.COM 	{ns_t_nsec3,	"NSEC3",	"nsec3"},
535*11038SRao.Shoaib@Sun.COM 	{ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
536*11038SRao.Shoaib@Sun.COM 	{ns_t_hip,	"HIP",		"host identity protocol"},
537*11038SRao.Shoaib@Sun.COM 	{ns_t_spf,	"SPF",		"sender policy framework"},
5380Sstevel@tonic-gate 	{ns_t_tkey,	"TKEY",		"tkey"},
5390Sstevel@tonic-gate 	{ns_t_tsig,	"TSIG",		"transaction signature"},
5400Sstevel@tonic-gate 	{ns_t_ixfr,	"IXFR",		"incremental zone transfer"},
5410Sstevel@tonic-gate 	{ns_t_axfr,	"AXFR",		"zone transfer"},
5420Sstevel@tonic-gate 	{ns_t_zxfr,	"ZXFR",		"compressed zone transfer"},
5430Sstevel@tonic-gate 	{ns_t_mailb,	"MAILB",	"mailbox-related data (deprecated)"},
5440Sstevel@tonic-gate 	{ns_t_maila,	"MAILA",	"mail agent (deprecated)"},
5450Sstevel@tonic-gate 	{ns_t_naptr,	"NAPTR",	"URN Naming Authority"},
5460Sstevel@tonic-gate 	{ns_t_kx,	"KX",		"Key Exchange"},
5470Sstevel@tonic-gate 	{ns_t_cert,	"CERT",		"Certificate"},
5480Sstevel@tonic-gate 	{ns_t_a6,	"A6",		"IPv6 Address"},
5490Sstevel@tonic-gate 	{ns_t_dname,	"DNAME",	"dname"},
5500Sstevel@tonic-gate 	{ns_t_sink,	"SINK",		"Kitchen Sink (experimental)"},
5510Sstevel@tonic-gate 	{ns_t_opt,	"OPT",		"EDNS Options"},
5520Sstevel@tonic-gate 	{ns_t_any,	"ANY",		"\"any\""},
553*11038SRao.Shoaib@Sun.COM 	{ns_t_dlv,	"DLV",		"DNSSEC look-aside validation"},
5540Sstevel@tonic-gate 	{0, 		NULL,		NULL}
5550Sstevel@tonic-gate };
5560Sstevel@tonic-gate 
557*11038SRao.Shoaib@Sun.COM /*%
5580Sstevel@tonic-gate  * Names of DNS rcodes.
5590Sstevel@tonic-gate  */
5600Sstevel@tonic-gate const struct res_sym __p_rcode_syms[] = {
5610Sstevel@tonic-gate 	{ns_r_noerror,	"NOERROR",		"no error"},
5620Sstevel@tonic-gate 	{ns_r_formerr,	"FORMERR",		"format error"},
5630Sstevel@tonic-gate 	{ns_r_servfail,	"SERVFAIL",		"server failed"},
5640Sstevel@tonic-gate 	{ns_r_nxdomain,	"NXDOMAIN",		"no such domain name"},
5650Sstevel@tonic-gate 	{ns_r_notimpl,	"NOTIMP",		"not implemented"},
5660Sstevel@tonic-gate 	{ns_r_refused,	"REFUSED",		"refused"},
5670Sstevel@tonic-gate 	{ns_r_yxdomain,	"YXDOMAIN",		"domain name exists"},
5680Sstevel@tonic-gate 	{ns_r_yxrrset,	"YXRRSET",		"rrset exists"},
5690Sstevel@tonic-gate 	{ns_r_nxrrset,	"NXRRSET",		"rrset doesn't exist"},
5700Sstevel@tonic-gate 	{ns_r_notauth,	"NOTAUTH",		"not authoritative"},
5710Sstevel@tonic-gate 	{ns_r_notzone,	"NOTZONE",		"Not in zone"},
5720Sstevel@tonic-gate 	{ns_r_max,	"",			""},
5730Sstevel@tonic-gate 	{ns_r_badsig,	"BADSIG",		"bad signature"},
5740Sstevel@tonic-gate 	{ns_r_badkey,	"BADKEY",		"bad key"},
5750Sstevel@tonic-gate 	{ns_r_badtime,	"BADTIME",		"bad time"},
5760Sstevel@tonic-gate 	{0, 		NULL,			NULL}
5770Sstevel@tonic-gate };
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate int
sym_ston(const struct res_sym * syms,const char * name,int * success)5800Sstevel@tonic-gate sym_ston(const struct res_sym *syms, const char *name, int *success) {
5810Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
5820Sstevel@tonic-gate 		if (strcasecmp (name, syms->name) == 0) {
5830Sstevel@tonic-gate 			if (success)
5840Sstevel@tonic-gate 				*success = 1;
5850Sstevel@tonic-gate 			return (syms->number);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	if (success)
5890Sstevel@tonic-gate 		*success = 0;
590*11038SRao.Shoaib@Sun.COM 	return (syms->number);		/*%< The default value. */
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate const char *
sym_ntos(const struct res_sym * syms,int number,int * success)5940Sstevel@tonic-gate sym_ntos(const struct res_sym *syms, int number, int *success) {
595*11038SRao.Shoaib@Sun.COM 	char *unname = sym_ntos_unname;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
5980Sstevel@tonic-gate 		if (number == syms->number) {
5990Sstevel@tonic-gate 			if (success)
6000Sstevel@tonic-gate 				*success = 1;
6010Sstevel@tonic-gate 			return (syms->name);
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
605*11038SRao.Shoaib@Sun.COM 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6060Sstevel@tonic-gate 	if (success)
6070Sstevel@tonic-gate 		*success = 0;
6080Sstevel@tonic-gate 	return (unname);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate const char *
sym_ntop(const struct res_sym * syms,int number,int * success)6120Sstevel@tonic-gate sym_ntop(const struct res_sym *syms, int number, int *success) {
613*11038SRao.Shoaib@Sun.COM 	char *unname = sym_ntop_unname;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
6160Sstevel@tonic-gate 		if (number == syms->number) {
6170Sstevel@tonic-gate 			if (success)
6180Sstevel@tonic-gate 				*success = 1;
6190Sstevel@tonic-gate 			return (syms->humanname);
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 	}
622*11038SRao.Shoaib@Sun.COM 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6230Sstevel@tonic-gate 	if (success)
6240Sstevel@tonic-gate 		*success = 0;
6250Sstevel@tonic-gate 	return (unname);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
628*11038SRao.Shoaib@Sun.COM /*%
6290Sstevel@tonic-gate  * Return a string for the type.
6300Sstevel@tonic-gate  */
6310Sstevel@tonic-gate const char *
p_type(int type)6320Sstevel@tonic-gate p_type(int type) {
6330Sstevel@tonic-gate 	int success;
6340Sstevel@tonic-gate 	const char *result;
6350Sstevel@tonic-gate 	static char typebuf[20];
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	result = sym_ntos(__p_type_syms, type, &success);
6380Sstevel@tonic-gate 	if (success)
6390Sstevel@tonic-gate 		return (result);
640*11038SRao.Shoaib@Sun.COM 	if (type < 0 || type > 0xffff)
6410Sstevel@tonic-gate 		return ("BADTYPE");
6420Sstevel@tonic-gate 	sprintf(typebuf, "TYPE%d", type);
6430Sstevel@tonic-gate 	return (typebuf);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
646*11038SRao.Shoaib@Sun.COM /*%
6470Sstevel@tonic-gate  * Return a string for the type.
6480Sstevel@tonic-gate  */
6490Sstevel@tonic-gate const char *
p_section(int section,int opcode)6500Sstevel@tonic-gate p_section(int section, int opcode) {
6510Sstevel@tonic-gate 	const struct res_sym *symbols;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	switch (opcode) {
6540Sstevel@tonic-gate 	case ns_o_update:
6550Sstevel@tonic-gate 		symbols = __p_update_section_syms;
6560Sstevel@tonic-gate 		break;
6570Sstevel@tonic-gate 	default:
6580Sstevel@tonic-gate 		symbols = __p_default_section_syms;
6590Sstevel@tonic-gate 		break;
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 	return (sym_ntos(symbols, section, (int *)0));
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
664*11038SRao.Shoaib@Sun.COM /*%
6650Sstevel@tonic-gate  * Return a mnemonic for class.
6660Sstevel@tonic-gate  */
6670Sstevel@tonic-gate const char *
p_class(int class)6680Sstevel@tonic-gate p_class(int class) {
6690Sstevel@tonic-gate 	int success;
6700Sstevel@tonic-gate 	const char *result;
6710Sstevel@tonic-gate 	static char classbuf[20];
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	result = sym_ntos(__p_class_syms, class, &success);
6740Sstevel@tonic-gate 	if (success)
6750Sstevel@tonic-gate 		return (result);
676*11038SRao.Shoaib@Sun.COM 	if (class < 0 || class > 0xffff)
6770Sstevel@tonic-gate 		return ("BADCLASS");
6780Sstevel@tonic-gate 	sprintf(classbuf, "CLASS%d", class);
6790Sstevel@tonic-gate 	return (classbuf);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
682*11038SRao.Shoaib@Sun.COM /*%
6830Sstevel@tonic-gate  * Return a mnemonic for an option
6840Sstevel@tonic-gate  */
6850Sstevel@tonic-gate const char *
p_option(u_long option)6860Sstevel@tonic-gate p_option(u_long option) {
687*11038SRao.Shoaib@Sun.COM 	char *nbuf = p_option_nbuf;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	switch (option) {
6900Sstevel@tonic-gate 	case RES_INIT:		return "init";
6910Sstevel@tonic-gate 	case RES_DEBUG:		return "debug";
6920Sstevel@tonic-gate 	case RES_AAONLY:	return "aaonly(unimpl)";
6930Sstevel@tonic-gate 	case RES_USEVC:		return "usevc";
6940Sstevel@tonic-gate 	case RES_PRIMARY:	return "primry(unimpl)";
6950Sstevel@tonic-gate 	case RES_IGNTC:		return "igntc";
6960Sstevel@tonic-gate 	case RES_RECURSE:	return "recurs";
6970Sstevel@tonic-gate 	case RES_DEFNAMES:	return "defnam";
6980Sstevel@tonic-gate 	case RES_STAYOPEN:	return "styopn";
6990Sstevel@tonic-gate 	case RES_DNSRCH:	return "dnsrch";
7000Sstevel@tonic-gate 	case RES_INSECURE1:	return "insecure1";
7010Sstevel@tonic-gate 	case RES_INSECURE2:	return "insecure2";
7020Sstevel@tonic-gate 	case RES_NOALIASES:	return "noaliases";
7030Sstevel@tonic-gate 	case RES_USE_INET6:	return "inet6";
704*11038SRao.Shoaib@Sun.COM #ifdef RES_USE_EDNS0	/*%< KAME extension */
7050Sstevel@tonic-gate 	case RES_USE_EDNS0:	return "edns0";
706*11038SRao.Shoaib@Sun.COM 	case RES_NSID:		return "nsid";
7070Sstevel@tonic-gate #endif
7080Sstevel@tonic-gate #ifdef RES_USE_DNAME
7090Sstevel@tonic-gate 	case RES_USE_DNAME:	return "dname";
7100Sstevel@tonic-gate #endif
7110Sstevel@tonic-gate #ifdef RES_USE_DNSSEC
7120Sstevel@tonic-gate 	case RES_USE_DNSSEC:	return "dnssec";
7130Sstevel@tonic-gate #endif
7140Sstevel@tonic-gate #ifdef RES_NOTLDQUERY
7150Sstevel@tonic-gate 	case RES_NOTLDQUERY:	return "no-tld-query";
7160Sstevel@tonic-gate #endif
717*11038SRao.Shoaib@Sun.COM #ifdef RES_NO_NIBBLE2
718*11038SRao.Shoaib@Sun.COM 	case RES_NO_NIBBLE2:	return "no-nibble2";
719*11038SRao.Shoaib@Sun.COM #endif
7200Sstevel@tonic-gate 				/* XXX nonreentrant */
7210Sstevel@tonic-gate 	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
7220Sstevel@tonic-gate 				return (nbuf);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
726*11038SRao.Shoaib@Sun.COM /*%
7270Sstevel@tonic-gate  * Return a mnemonic for a time to live.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate const char *
p_time(u_int32_t value)7300Sstevel@tonic-gate p_time(u_int32_t value) {
731*11038SRao.Shoaib@Sun.COM 	char *nbuf = p_time_nbuf;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
7340Sstevel@tonic-gate 		sprintf(nbuf, "%u", value);
7350Sstevel@tonic-gate 	return (nbuf);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
738*11038SRao.Shoaib@Sun.COM /*%
7390Sstevel@tonic-gate  * Return a string for the rcode.
7400Sstevel@tonic-gate  */
7410Sstevel@tonic-gate const char *
p_rcode(int rcode)7420Sstevel@tonic-gate p_rcode(int rcode) {
7430Sstevel@tonic-gate 	return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
746*11038SRao.Shoaib@Sun.COM /*%
7470Sstevel@tonic-gate  * Return a string for a res_sockaddr_union.
7480Sstevel@tonic-gate  */
7490Sstevel@tonic-gate const char *
p_sockun(union res_sockaddr_union u,char * buf,size_t size)7500Sstevel@tonic-gate p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
7510Sstevel@tonic-gate 	char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	switch (u.sin.sin_family) {
7540Sstevel@tonic-gate 	case AF_INET:
7550Sstevel@tonic-gate 		inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
7560Sstevel@tonic-gate 		break;
7570Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
7580Sstevel@tonic-gate 	case AF_INET6:
7590Sstevel@tonic-gate 		inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
7600Sstevel@tonic-gate 		break;
7610Sstevel@tonic-gate #endif
7620Sstevel@tonic-gate 	default:
7630Sstevel@tonic-gate 		sprintf(ret, "[af%d]", u.sin.sin_family);
7640Sstevel@tonic-gate 		break;
7650Sstevel@tonic-gate 	}
766*11038SRao.Shoaib@Sun.COM 	if (size > 0U) {
7670Sstevel@tonic-gate 		strncpy(buf, ret, size - 1);
7680Sstevel@tonic-gate 		buf[size - 1] = '0';
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	return (buf);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
773*11038SRao.Shoaib@Sun.COM /*%
7740Sstevel@tonic-gate  * routines to convert between on-the-wire RR format and zone file format.
7750Sstevel@tonic-gate  * Does not contain conversion to/from decimal degrees; divide or multiply
7760Sstevel@tonic-gate  * by 60*60*1000 for that.
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
7800Sstevel@tonic-gate 				      1000000,10000000,100000000,1000000000};
7810Sstevel@tonic-gate 
782*11038SRao.Shoaib@Sun.COM /*% takes an XeY precision/size value, returns a string representation. */
7830Sstevel@tonic-gate static const char *
precsize_ntoa(prec)7840Sstevel@tonic-gate precsize_ntoa(prec)
7850Sstevel@tonic-gate 	u_int8_t prec;
7860Sstevel@tonic-gate {
787*11038SRao.Shoaib@Sun.COM 	char *retbuf = precsize_ntoa_retbuf;
7880Sstevel@tonic-gate 	unsigned long val;
7890Sstevel@tonic-gate 	int mantissa, exponent;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	mantissa = (int)((prec >> 4) & 0x0f) % 10;
7920Sstevel@tonic-gate 	exponent = (int)((prec >> 0) & 0x0f) % 10;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	val = mantissa * poweroften[exponent];
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	(void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
7970Sstevel@tonic-gate 	return (retbuf);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
800*11038SRao.Shoaib@Sun.COM /*% converts ascii size/precision X * 10**Y(cm) to 0xXY.  moves pointer.  */
8010Sstevel@tonic-gate static u_int8_t
precsize_aton(const char ** strptr)8020Sstevel@tonic-gate precsize_aton(const char **strptr) {
8030Sstevel@tonic-gate 	unsigned int mval = 0, cmval = 0;
8040Sstevel@tonic-gate 	u_int8_t retval = 0;
8050Sstevel@tonic-gate 	const char *cp;
8060Sstevel@tonic-gate 	int exponent;
8070Sstevel@tonic-gate 	int mantissa;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	cp = *strptr;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8120Sstevel@tonic-gate 		mval = mval * 10 + (*cp++ - '0');
8130Sstevel@tonic-gate 
814*11038SRao.Shoaib@Sun.COM 	if (*cp == '.') {		/*%< centimeters */
8150Sstevel@tonic-gate 		cp++;
8160Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
8170Sstevel@tonic-gate 			cmval = (*cp++ - '0') * 10;
8180Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
8190Sstevel@tonic-gate 				cmval += (*cp++ - '0');
8200Sstevel@tonic-gate 			}
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 	cmval = (mval * 100) + cmval;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	for (exponent = 0; exponent < 9; exponent++)
8260Sstevel@tonic-gate 		if (cmval < poweroften[exponent+1])
8270Sstevel@tonic-gate 			break;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	mantissa = cmval / poweroften[exponent];
8300Sstevel@tonic-gate 	if (mantissa > 9)
8310Sstevel@tonic-gate 		mantissa = 9;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	retval = (mantissa << 4) | exponent;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	*strptr = cp;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	return (retval);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
840*11038SRao.Shoaib@Sun.COM /*% converts ascii lat/lon to unsigned encoded 32-bit number.  moves pointer. */
8410Sstevel@tonic-gate static u_int32_t
latlon2ul(const char ** latlonstrptr,int * which)8420Sstevel@tonic-gate latlon2ul(const char **latlonstrptr, int *which) {
8430Sstevel@tonic-gate 	const char *cp;
8440Sstevel@tonic-gate 	u_int32_t retval;
8450Sstevel@tonic-gate 	int deg = 0, min = 0, secs = 0, secsfrac = 0;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	cp = *latlonstrptr;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8500Sstevel@tonic-gate 		deg = deg * 10 + (*cp++ - '0');
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8530Sstevel@tonic-gate 		cp++;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
8560Sstevel@tonic-gate 		goto fndhemi;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8590Sstevel@tonic-gate 		min = min * 10 + (*cp++ - '0');
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8620Sstevel@tonic-gate 		cp++;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
8650Sstevel@tonic-gate 		goto fndhemi;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8680Sstevel@tonic-gate 		secs = secs * 10 + (*cp++ - '0');
8690Sstevel@tonic-gate 
870*11038SRao.Shoaib@Sun.COM 	if (*cp == '.') {		/*%< decimal seconds */
8710Sstevel@tonic-gate 		cp++;
8720Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
8730Sstevel@tonic-gate 			secsfrac = (*cp++ - '0') * 100;
8740Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
8750Sstevel@tonic-gate 				secsfrac += (*cp++ - '0') * 10;
8760Sstevel@tonic-gate 				if (isdigit((unsigned char)*cp)) {
8770Sstevel@tonic-gate 					secsfrac += (*cp++ - '0');
8780Sstevel@tonic-gate 				}
8790Sstevel@tonic-gate 			}
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
883*11038SRao.Shoaib@Sun.COM 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
8840Sstevel@tonic-gate 		cp++;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8870Sstevel@tonic-gate 		cp++;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate  fndhemi:
8900Sstevel@tonic-gate 	switch (*cp) {
8910Sstevel@tonic-gate 	case 'N': case 'n':
8920Sstevel@tonic-gate 	case 'E': case 'e':
8930Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
8940Sstevel@tonic-gate 			+ (((((deg * 60) + min) * 60) + secs) * 1000)
8950Sstevel@tonic-gate 			+ secsfrac;
8960Sstevel@tonic-gate 		break;
8970Sstevel@tonic-gate 	case 'S': case 's':
8980Sstevel@tonic-gate 	case 'W': case 'w':
8990Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
9000Sstevel@tonic-gate 			- (((((deg * 60) + min) * 60) + secs) * 1000)
9010Sstevel@tonic-gate 			- secsfrac;
9020Sstevel@tonic-gate 		break;
9030Sstevel@tonic-gate 	default:
904*11038SRao.Shoaib@Sun.COM 		retval = 0;	/*%< invalid value -- indicates error */
9050Sstevel@tonic-gate 		break;
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	switch (*cp) {
9090Sstevel@tonic-gate 	case 'N': case 'n':
9100Sstevel@tonic-gate 	case 'S': case 's':
911*11038SRao.Shoaib@Sun.COM 		*which = 1;	/*%< latitude */
9120Sstevel@tonic-gate 		break;
9130Sstevel@tonic-gate 	case 'E': case 'e':
9140Sstevel@tonic-gate 	case 'W': case 'w':
915*11038SRao.Shoaib@Sun.COM 		*which = 2;	/*%< longitude */
9160Sstevel@tonic-gate 		break;
9170Sstevel@tonic-gate 	default:
918*11038SRao.Shoaib@Sun.COM 		*which = 0;	/*%< error */
9190Sstevel@tonic-gate 		break;
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
922*11038SRao.Shoaib@Sun.COM 	cp++;			/*%< skip the hemisphere */
923*11038SRao.Shoaib@Sun.COM 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
9240Sstevel@tonic-gate 		cp++;
9250Sstevel@tonic-gate 
926*11038SRao.Shoaib@Sun.COM 	while (isspace((unsigned char)*cp))	/*%< move to next field */
9270Sstevel@tonic-gate 		cp++;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	*latlonstrptr = cp;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	return (retval);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate 
934*11038SRao.Shoaib@Sun.COM /*%
935*11038SRao.Shoaib@Sun.COM  * converts a zone file representation in a string to an RDATA on-the-wire
9360Sstevel@tonic-gate  * representation. */
9370Sstevel@tonic-gate int
loc_aton(ascii,binary)9380Sstevel@tonic-gate loc_aton(ascii, binary)
9390Sstevel@tonic-gate 	const char *ascii;
9400Sstevel@tonic-gate 	u_char *binary;
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	const char *cp, *maxcp;
9430Sstevel@tonic-gate 	u_char *bcp;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	u_int32_t latit = 0, longit = 0, alt = 0;
9460Sstevel@tonic-gate 	u_int32_t lltemp1 = 0, lltemp2 = 0;
9470Sstevel@tonic-gate 	int altmeters = 0, altfrac = 0, altsign = 1;
948*11038SRao.Shoaib@Sun.COM 	u_int8_t hp = 0x16;	/*%< default = 1e6 cm = 10000.00m = 10km */
949*11038SRao.Shoaib@Sun.COM 	u_int8_t vp = 0x13;	/*%< default = 1e3 cm = 10.00m */
950*11038SRao.Shoaib@Sun.COM 	u_int8_t siz = 0x12;	/*%< default = 1e2 cm = 1.00m */
9510Sstevel@tonic-gate 	int which1 = 0, which2 = 0;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	cp = ascii;
9540Sstevel@tonic-gate 	maxcp = cp + strlen(ascii);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	lltemp1 = latlon2ul(&cp, &which1);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	lltemp2 = latlon2ul(&cp, &which2);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	switch (which1 + which2) {
961*11038SRao.Shoaib@Sun.COM 	case 3:			/*%< 1 + 2, the only valid combination */
962*11038SRao.Shoaib@Sun.COM 		if ((which1 == 1) && (which2 == 2)) { /*%< normal case */
9630Sstevel@tonic-gate 			latit = lltemp1;
9640Sstevel@tonic-gate 			longit = lltemp2;
965*11038SRao.Shoaib@Sun.COM 		} else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */
9660Sstevel@tonic-gate 			longit = lltemp1;
9670Sstevel@tonic-gate 			latit = lltemp2;
968*11038SRao.Shoaib@Sun.COM 		} else {	/*%< some kind of brokenness */
9690Sstevel@tonic-gate 			return (0);
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 		break;
972*11038SRao.Shoaib@Sun.COM 	default:		/*%< we didn't get one of each */
9730Sstevel@tonic-gate 		return (0);
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/* altitude */
9770Sstevel@tonic-gate 	if (*cp == '-') {
9780Sstevel@tonic-gate 		altsign = -1;
9790Sstevel@tonic-gate 		cp++;
9800Sstevel@tonic-gate 	}
981*11038SRao.Shoaib@Sun.COM 
9820Sstevel@tonic-gate 	if (*cp == '+')
9830Sstevel@tonic-gate 		cp++;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
9860Sstevel@tonic-gate 		altmeters = altmeters * 10 + (*cp++ - '0');
9870Sstevel@tonic-gate 
988*11038SRao.Shoaib@Sun.COM 	if (*cp == '.') {		/*%< decimal meters */
9890Sstevel@tonic-gate 		cp++;
9900Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
9910Sstevel@tonic-gate 			altfrac = (*cp++ - '0') * 10;
9920Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
9930Sstevel@tonic-gate 				altfrac += (*cp++ - '0');
9940Sstevel@tonic-gate 			}
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
9990Sstevel@tonic-gate 
1000*11038SRao.Shoaib@Sun.COM 	while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
10010Sstevel@tonic-gate 		cp++;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10040Sstevel@tonic-gate 		cp++;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (cp >= maxcp)
10070Sstevel@tonic-gate 		goto defaults;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	siz = precsize_aton(&cp);
1010*11038SRao.Shoaib@Sun.COM 
1011*11038SRao.Shoaib@Sun.COM 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10120Sstevel@tonic-gate 		cp++;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10150Sstevel@tonic-gate 		cp++;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	if (cp >= maxcp)
10180Sstevel@tonic-gate 		goto defaults;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	hp = precsize_aton(&cp);
10210Sstevel@tonic-gate 
1022*11038SRao.Shoaib@Sun.COM 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10230Sstevel@tonic-gate 		cp++;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10260Sstevel@tonic-gate 		cp++;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (cp >= maxcp)
10290Sstevel@tonic-gate 		goto defaults;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	vp = precsize_aton(&cp);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate  defaults:
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	bcp = binary;
1036*11038SRao.Shoaib@Sun.COM 	*bcp++ = (u_int8_t) 0;	/*%< version byte */
10370Sstevel@tonic-gate 	*bcp++ = siz;
10380Sstevel@tonic-gate 	*bcp++ = hp;
10390Sstevel@tonic-gate 	*bcp++ = vp;
10400Sstevel@tonic-gate 	PUTLONG(latit,bcp);
10410Sstevel@tonic-gate 	PUTLONG(longit,bcp);
10420Sstevel@tonic-gate 	PUTLONG(alt,bcp);
1043*11038SRao.Shoaib@Sun.COM 
1044*11038SRao.Shoaib@Sun.COM 	return (16);		/*%< size of RR in octets */
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate 
1047*11038SRao.Shoaib@Sun.COM /*% takes an on-the-wire LOC RR and formats it in a human readable format. */
10480Sstevel@tonic-gate const char *
loc_ntoa(binary,ascii)10490Sstevel@tonic-gate loc_ntoa(binary, ascii)
10500Sstevel@tonic-gate 	const u_char *binary;
10510Sstevel@tonic-gate 	char *ascii;
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate 	static const char *error = "?";
10540Sstevel@tonic-gate 	static char tmpbuf[sizeof
10550Sstevel@tonic-gate "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
10560Sstevel@tonic-gate 	const u_char *cp = binary;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	int latdeg, latmin, latsec, latsecfrac;
10590Sstevel@tonic-gate 	int longdeg, longmin, longsec, longsecfrac;
10600Sstevel@tonic-gate 	char northsouth, eastwest;
10610Sstevel@tonic-gate 	const char *altsign;
10620Sstevel@tonic-gate 	int altmeters, altfrac;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	const u_int32_t referencealt = 100000 * 100;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	int32_t latval, longval, altval;
10670Sstevel@tonic-gate 	u_int32_t templ;
10680Sstevel@tonic-gate 	u_int8_t sizeval, hpval, vpval, versionval;
1069*11038SRao.Shoaib@Sun.COM 
10700Sstevel@tonic-gate 	char *sizestr, *hpstr, *vpstr;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	versionval = *cp++;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (ascii == NULL)
10750Sstevel@tonic-gate 		ascii = tmpbuf;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (versionval) {
10780Sstevel@tonic-gate 		(void) sprintf(ascii, "; error: unknown LOC RR version");
10790Sstevel@tonic-gate 		return (ascii);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	sizeval = *cp++;
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	hpval = *cp++;
10850Sstevel@tonic-gate 	vpval = *cp++;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	GETLONG(templ, cp);
10880Sstevel@tonic-gate 	latval = (templ - ((unsigned)1<<31));
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	GETLONG(templ, cp);
10910Sstevel@tonic-gate 	longval = (templ - ((unsigned)1<<31));
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	GETLONG(templ, cp);
1094*11038SRao.Shoaib@Sun.COM 	if (templ < referencealt) { /*%< below WGS 84 spheroid */
10950Sstevel@tonic-gate 		altval = referencealt - templ;
10960Sstevel@tonic-gate 		altsign = "-";
10970Sstevel@tonic-gate 	} else {
10980Sstevel@tonic-gate 		altval = templ - referencealt;
10990Sstevel@tonic-gate 		altsign = "";
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	if (latval < 0) {
11030Sstevel@tonic-gate 		northsouth = 'S';
11040Sstevel@tonic-gate 		latval = -latval;
11050Sstevel@tonic-gate 	} else
11060Sstevel@tonic-gate 		northsouth = 'N';
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	latsecfrac = latval % 1000;
11090Sstevel@tonic-gate 	latval = latval / 1000;
11100Sstevel@tonic-gate 	latsec = latval % 60;
11110Sstevel@tonic-gate 	latval = latval / 60;
11120Sstevel@tonic-gate 	latmin = latval % 60;
11130Sstevel@tonic-gate 	latval = latval / 60;
11140Sstevel@tonic-gate 	latdeg = latval;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (longval < 0) {
11170Sstevel@tonic-gate 		eastwest = 'W';
11180Sstevel@tonic-gate 		longval = -longval;
11190Sstevel@tonic-gate 	} else
11200Sstevel@tonic-gate 		eastwest = 'E';
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	longsecfrac = longval % 1000;
11230Sstevel@tonic-gate 	longval = longval / 1000;
11240Sstevel@tonic-gate 	longsec = longval % 60;
11250Sstevel@tonic-gate 	longval = longval / 60;
11260Sstevel@tonic-gate 	longmin = longval % 60;
11270Sstevel@tonic-gate 	longval = longval / 60;
11280Sstevel@tonic-gate 	longdeg = longval;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	altfrac = altval % 100;
11310Sstevel@tonic-gate 	altmeters = (altval / 100);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	sizestr = strdup(precsize_ntoa(sizeval));
11340Sstevel@tonic-gate 	hpstr = strdup(precsize_ntoa(hpval));
11350Sstevel@tonic-gate 	vpstr = strdup(precsize_ntoa(vpval));
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	sprintf(ascii,
11380Sstevel@tonic-gate 	    "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
11390Sstevel@tonic-gate 		latdeg, latmin, latsec, latsecfrac, northsouth,
11400Sstevel@tonic-gate 		longdeg, longmin, longsec, longsecfrac, eastwest,
11410Sstevel@tonic-gate 		altsign, altmeters, altfrac,
11420Sstevel@tonic-gate 		(sizestr != NULL) ? sizestr : error,
11430Sstevel@tonic-gate 		(hpstr != NULL) ? hpstr : error,
11440Sstevel@tonic-gate 		(vpstr != NULL) ? vpstr : error);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	if (sizestr != NULL)
11470Sstevel@tonic-gate 		free(sizestr);
11480Sstevel@tonic-gate 	if (hpstr != NULL)
11490Sstevel@tonic-gate 		free(hpstr);
11500Sstevel@tonic-gate 	if (vpstr != NULL)
11510Sstevel@tonic-gate 		free(vpstr);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	return (ascii);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 
1157*11038SRao.Shoaib@Sun.COM /*% Return the number of DNS hierarchy levels in the name. */
11580Sstevel@tonic-gate int
dn_count_labels(const char * name)11590Sstevel@tonic-gate dn_count_labels(const char *name) {
11600Sstevel@tonic-gate 	int i, len, count;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	len = strlen(name);
11630Sstevel@tonic-gate 	for (i = 0, count = 0; i < len; i++) {
11640Sstevel@tonic-gate 		/* XXX need to check for \. or use named's nlabels(). */
11650Sstevel@tonic-gate 		if (name[i] == '.')
11660Sstevel@tonic-gate 			count++;
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/* don't count initial wildcard */
11700Sstevel@tonic-gate 	if (name[0] == '*')
11710Sstevel@tonic-gate 		if (count)
11720Sstevel@tonic-gate 			count--;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/* don't count the null label for root. */
11750Sstevel@tonic-gate 	/* if terminating '.' not found, must adjust */
11760Sstevel@tonic-gate 	/* count to include last label */
11770Sstevel@tonic-gate 	if (len > 0 && name[len-1] != '.')
11780Sstevel@tonic-gate 		count++;
11790Sstevel@tonic-gate 	return (count);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate 
1182*11038SRao.Shoaib@Sun.COM /*%
1183*11038SRao.Shoaib@Sun.COM  * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
11840Sstevel@tonic-gate  * SIG records are required to be printed like this, by the Secure DNS RFC.
11850Sstevel@tonic-gate  */
11860Sstevel@tonic-gate char *
p_secstodate(u_long secs)11870Sstevel@tonic-gate p_secstodate (u_long secs) {
1188*11038SRao.Shoaib@Sun.COM 	char *output = p_secstodate_output;
11890Sstevel@tonic-gate 	time_t clock = secs;
11900Sstevel@tonic-gate 	struct tm *time;
11910Sstevel@tonic-gate #ifdef HAVE_TIME_R
1192*11038SRao.Shoaib@Sun.COM 	struct tm res;
1193*11038SRao.Shoaib@Sun.COM 
1194*11038SRao.Shoaib@Sun.COM 	time = gmtime_r(&clock, &res);
11950Sstevel@tonic-gate #else
11960Sstevel@tonic-gate 	time = gmtime(&clock);
11970Sstevel@tonic-gate #endif
11980Sstevel@tonic-gate 	time->tm_year += 1900;
11990Sstevel@tonic-gate 	time->tm_mon += 1;
12000Sstevel@tonic-gate 	sprintf(output, "%04d%02d%02d%02d%02d%02d",
12010Sstevel@tonic-gate 		time->tm_year, time->tm_mon, time->tm_mday,
12020Sstevel@tonic-gate 		time->tm_hour, time->tm_min, time->tm_sec);
12030Sstevel@tonic-gate 	return (output);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate u_int16_t
res_nametoclass(const char * buf,int * successp)12070Sstevel@tonic-gate res_nametoclass(const char *buf, int *successp) {
12080Sstevel@tonic-gate 	unsigned long result;
12090Sstevel@tonic-gate 	char *endptr;
12100Sstevel@tonic-gate 	int success;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	result = sym_ston(__p_class_syms, buf, &success);
12130Sstevel@tonic-gate 	if (success)
12140Sstevel@tonic-gate 		goto done;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if (strncasecmp(buf, "CLASS", 5) != 0 ||
12170Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[5]))
12180Sstevel@tonic-gate 		goto done;
12190Sstevel@tonic-gate 	errno = 0;
12200Sstevel@tonic-gate 	result = strtoul(buf + 5, &endptr, 10);
1221*11038SRao.Shoaib@Sun.COM 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12220Sstevel@tonic-gate 		success = 1;
12230Sstevel@tonic-gate  done:
12240Sstevel@tonic-gate 	if (successp)
12250Sstevel@tonic-gate 		*successp = success;
12260Sstevel@tonic-gate 	return (result);
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate u_int16_t
res_nametotype(const char * buf,int * successp)12300Sstevel@tonic-gate res_nametotype(const char *buf, int *successp) {
12310Sstevel@tonic-gate 	unsigned long result;
12320Sstevel@tonic-gate 	char *endptr;
12330Sstevel@tonic-gate 	int success;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	result = sym_ston(__p_type_syms, buf, &success);
12360Sstevel@tonic-gate 	if (success)
12370Sstevel@tonic-gate 		goto done;
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	if (strncasecmp(buf, "type", 4) != 0 ||
12400Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[4]))
12410Sstevel@tonic-gate 		goto done;
12420Sstevel@tonic-gate 	errno = 0;
12430Sstevel@tonic-gate 	result = strtoul(buf + 4, &endptr, 10);
1244*11038SRao.Shoaib@Sun.COM 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12450Sstevel@tonic-gate 		success = 1;
12460Sstevel@tonic-gate  done:
12470Sstevel@tonic-gate 	if (successp)
12480Sstevel@tonic-gate 		*successp = success;
12490Sstevel@tonic-gate 	return (result);
12500Sstevel@tonic-gate }
1251*11038SRao.Shoaib@Sun.COM 
1252*11038SRao.Shoaib@Sun.COM /*! \file */
1253