xref: /titanic_41/usr/src/lib/libresolv2/common/resolv/res_debug.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*9525b14bSRao Shoaib 
7*9525b14bSRao Shoaib /*
8*9525b14bSRao Shoaib  * Portions Copyright (C) 2004, 2005, 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
9*9525b14bSRao Shoaib  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
10*9525b14bSRao Shoaib  *
11*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
12*9525b14bSRao Shoaib  * purpose with or without fee is hereby granted, provided that the above
13*9525b14bSRao Shoaib  * copyright notice and this permission notice appear in all copies.
14*9525b14bSRao Shoaib  *
15*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
22*9525b14bSRao Shoaib  */
23*9525b14bSRao Shoaib 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * Copyright (c) 1985
267c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
297c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
307c478bd9Sstevel@tonic-gate  * are met:
317c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
327c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
337c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
347c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
357c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
367c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
377c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
387c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
397c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
407c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
417c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
427c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
457c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
467c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
477c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
487c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
497c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
507c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
517c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
527c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
537c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
547c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
617c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
627c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
637c478bd9Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
647c478bd9Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
657c478bd9Sstevel@tonic-gate  * specific, written prior permission.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
687c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
697c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
707c478bd9Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
717c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
727c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
737c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
747c478bd9Sstevel@tonic-gate  * SOFTWARE.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1995 by International Business Machines, Inc.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * International Business Machines, Inc. (hereinafter called IBM) grants
817c478bd9Sstevel@tonic-gate  * permission under its copyrights to use, copy, modify, and distribute this
827c478bd9Sstevel@tonic-gate  * Software with or without fee, provided that the above copyright notice and
837c478bd9Sstevel@tonic-gate  * all paragraphs of this notice appear in all copies, and that the name of IBM
847c478bd9Sstevel@tonic-gate  * not be used in connection with the marketing of any product incorporating
857c478bd9Sstevel@tonic-gate  * the Software or modifications thereof, without specific, written prior
867c478bd9Sstevel@tonic-gate  * permission.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * To the extent it has a right to do so, IBM grants an immunity from suit
897c478bd9Sstevel@tonic-gate  * under its patents, if any, for the use, sale or manufacture of products to
907c478bd9Sstevel@tonic-gate  * the extent that such products are used for performing Domain Name System
917c478bd9Sstevel@tonic-gate  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
927c478bd9Sstevel@tonic-gate  * granted for any product per se or for any other function of any product.
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
957c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
967c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
977c478bd9Sstevel@tonic-gate  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
987c478bd9Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
997c478bd9Sstevel@tonic-gate  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
1037c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_debug.c	8.1 (Berkeley) 6/4/93";
104*9525b14bSRao Shoaib static const char rcsid[] = "$Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp $";
1057c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #include "port_before.h"
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #include <sys/types.h>
1107c478bd9Sstevel@tonic-gate #include <sys/param.h>
1117c478bd9Sstevel@tonic-gate #include <sys/socket.h>
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #include <netinet/in.h>
1147c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
1157c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #include <ctype.h>
1187c478bd9Sstevel@tonic-gate #include <errno.h>
1197c478bd9Sstevel@tonic-gate #include <math.h>
1207c478bd9Sstevel@tonic-gate #include <netdb.h>
1217c478bd9Sstevel@tonic-gate #include <resolv.h>
122*9525b14bSRao Shoaib #include <resolv_mt.h>
1237c478bd9Sstevel@tonic-gate #include <stdio.h>
1247c478bd9Sstevel@tonic-gate #include <stdlib.h>
1257c478bd9Sstevel@tonic-gate #include <string.h>
1267c478bd9Sstevel@tonic-gate #include <time.h>
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #include "port_after.h"
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
1317c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
1327c478bd9Sstevel@tonic-gate #else
1337c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate extern const char *_res_opcodes[];
1377c478bd9Sstevel@tonic-gate extern const char *_res_sectioncodes[];
1387c478bd9Sstevel@tonic-gate 
139*9525b14bSRao Shoaib #ifndef	ORIGINAL_ISC_CODE
1407c478bd9Sstevel@tonic-gate #pragma weak	__dn_count_labels	=	dn_count_labels
1417c478bd9Sstevel@tonic-gate #pragma weak	__fp_resstat		=	fp_resstat
1427c478bd9Sstevel@tonic-gate #pragma weak	__loc_aton		=	loc_aton
1437c478bd9Sstevel@tonic-gate #pragma weak	__loc_ntoa		=	loc_ntoa
1447c478bd9Sstevel@tonic-gate #pragma weak	__p_cdname		=	p_cdname
1457c478bd9Sstevel@tonic-gate #pragma weak	__p_class		=	p_class
1467c478bd9Sstevel@tonic-gate #pragma weak	__p_section		=	p_section
1477c478bd9Sstevel@tonic-gate #pragma weak	__p_time		=	p_time
1487c478bd9Sstevel@tonic-gate #pragma weak	__p_type		=	p_type
1497c478bd9Sstevel@tonic-gate #pragma weak	__sym_ntop		=	sym_ntop
1507c478bd9Sstevel@tonic-gate #pragma weak	__sym_ntos		=	sym_ntos
1517c478bd9Sstevel@tonic-gate #pragma weak	__sym_ston		=	sym_ston
1527c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
1537c478bd9Sstevel@tonic-gate 
154*9525b14bSRao Shoaib /*%
1557c478bd9Sstevel@tonic-gate  * Print the current options.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate void
fp_resstat(const res_state statp,FILE * file)1587c478bd9Sstevel@tonic-gate fp_resstat(const res_state statp, FILE *file) {
1597c478bd9Sstevel@tonic-gate 	u_long mask;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	fprintf(file, ";; res options:");
162*9525b14bSRao Shoaib 	for (mask = 1;  mask != 0U;  mask <<= 1)
1637c478bd9Sstevel@tonic-gate 		if (statp->options & mask)
1647c478bd9Sstevel@tonic-gate 			fprintf(file, " %s", p_option(mask));
1657c478bd9Sstevel@tonic-gate 	putc('\n', file);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static void
do_section(const res_state statp,ns_msg * handle,ns_sect section,int pflag,FILE * file)1697c478bd9Sstevel@tonic-gate do_section(const res_state statp,
1707c478bd9Sstevel@tonic-gate 	   ns_msg *handle, ns_sect section,
1717c478bd9Sstevel@tonic-gate 	   int pflag, FILE *file)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	int n, sflag, rrnum;
1747c478bd9Sstevel@tonic-gate 	static int buflen = 2048;
1757c478bd9Sstevel@tonic-gate 	char *buf;
1767c478bd9Sstevel@tonic-gate 	ns_opcode opcode;
1777c478bd9Sstevel@tonic-gate 	ns_rr rr;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * Print answer records.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	sflag = (statp->pfcode & pflag);
1837c478bd9Sstevel@tonic-gate 	if (statp->pfcode && !sflag)
1847c478bd9Sstevel@tonic-gate 		return;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	buf = malloc(buflen);
1877c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
1887c478bd9Sstevel@tonic-gate 		fprintf(file, ";; memory allocation failure\n");
1897c478bd9Sstevel@tonic-gate 		return;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
1937c478bd9Sstevel@tonic-gate 	rrnum = 0;
1947c478bd9Sstevel@tonic-gate 	for (;;) {
1957c478bd9Sstevel@tonic-gate 		if (ns_parserr(handle, section, rrnum, &rr)) {
1967c478bd9Sstevel@tonic-gate 			if (errno != ENODEV)
1977c478bd9Sstevel@tonic-gate 				fprintf(file, ";; ns_parserr: %s\n",
1987c478bd9Sstevel@tonic-gate 					strerror(errno));
1997c478bd9Sstevel@tonic-gate 			else if (rrnum > 0 && sflag != 0 &&
2007c478bd9Sstevel@tonic-gate 				 (statp->pfcode & RES_PRF_HEAD1))
2017c478bd9Sstevel@tonic-gate 				putc('\n', file);
2027c478bd9Sstevel@tonic-gate 			goto cleanup;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
2057c478bd9Sstevel@tonic-gate 			fprintf(file, ";; %s SECTION:\n",
2067c478bd9Sstevel@tonic-gate 				p_section(section, opcode));
2077c478bd9Sstevel@tonic-gate 		if (section == ns_s_qd)
2087c478bd9Sstevel@tonic-gate 			fprintf(file, ";;\t%s, type = %s, class = %s\n",
2097c478bd9Sstevel@tonic-gate 				ns_rr_name(rr),
2107c478bd9Sstevel@tonic-gate 				p_type(ns_rr_type(rr)),
2117c478bd9Sstevel@tonic-gate 				p_class(ns_rr_class(rr)));
2127c478bd9Sstevel@tonic-gate 		else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
213*9525b14bSRao Shoaib 			u_int16_t optcode, optlen, rdatalen = ns_rr_rdlen(rr);
2147c478bd9Sstevel@tonic-gate 			u_int32_t ttl = ns_rr_ttl(rr);
215*9525b14bSRao Shoaib 
2167c478bd9Sstevel@tonic-gate 			fprintf(file,
2177c478bd9Sstevel@tonic-gate 				"; EDNS: version: %u, udp=%u, flags=%04x\n",
2187c478bd9Sstevel@tonic-gate 				(ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
219*9525b14bSRao Shoaib 
220*9525b14bSRao Shoaib 			while (rdatalen >= 4) {
221*9525b14bSRao Shoaib 				const u_char *cp = ns_rr_rdata(rr);
222*9525b14bSRao Shoaib 				int i;
223*9525b14bSRao Shoaib 
224*9525b14bSRao Shoaib 				GETSHORT(optcode, cp);
225*9525b14bSRao Shoaib 				GETSHORT(optlen, cp);
226*9525b14bSRao Shoaib 
227*9525b14bSRao Shoaib 				if (optcode == NS_OPT_NSID) {
228*9525b14bSRao Shoaib 					fputs("; NSID: ", file);
229*9525b14bSRao Shoaib 					if (optlen == 0) {
230*9525b14bSRao Shoaib 						fputs("; NSID\n", file);
231*9525b14bSRao Shoaib 					} else {
232*9525b14bSRao Shoaib 						fputs("; NSID: ", file);
233*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
234*9525b14bSRao Shoaib 							fprintf(file, "%02x ",
235*9525b14bSRao Shoaib 								cp[i]);
236*9525b14bSRao Shoaib 						fputs(" (",file);
237*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
238*9525b14bSRao Shoaib 							fprintf(file, "%c",
239*9525b14bSRao Shoaib 								isprint(cp[i])?
240*9525b14bSRao Shoaib 								cp[i] : '.');
241*9525b14bSRao Shoaib 						fputs(")\n", file);
242*9525b14bSRao Shoaib 					}
243*9525b14bSRao Shoaib 				} else {
244*9525b14bSRao Shoaib 					if (optlen == 0) {
245*9525b14bSRao Shoaib 						fprintf(file, "; OPT=%u\n",
246*9525b14bSRao Shoaib 							optcode);
247*9525b14bSRao Shoaib 					} else {
248*9525b14bSRao Shoaib 						fprintf(file, "; OPT=%u: ",
249*9525b14bSRao Shoaib 							optcode);
250*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
251*9525b14bSRao Shoaib 							fprintf(file, "%02x ",
252*9525b14bSRao Shoaib 								cp[i]);
253*9525b14bSRao Shoaib 						fputs(" (",file);
254*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
255*9525b14bSRao Shoaib 							fprintf(file, "%c",
256*9525b14bSRao Shoaib 								isprint(cp[i]) ?
257*9525b14bSRao Shoaib 									cp[i] : '.');
258*9525b14bSRao Shoaib 						fputs(")\n", file);
259*9525b14bSRao Shoaib 					}
260*9525b14bSRao Shoaib 				}
261*9525b14bSRao Shoaib 				rdatalen -= 4 + optlen;
262*9525b14bSRao Shoaib 			}
2637c478bd9Sstevel@tonic-gate 		} else {
2647c478bd9Sstevel@tonic-gate 			n = ns_sprintrr(handle, &rr, NULL, NULL,
2657c478bd9Sstevel@tonic-gate 					buf, buflen);
2667c478bd9Sstevel@tonic-gate 			if (n < 0) {
2677c478bd9Sstevel@tonic-gate 				if (errno == ENOSPC) {
2687c478bd9Sstevel@tonic-gate 					free(buf);
2697c478bd9Sstevel@tonic-gate 					buf = NULL;
2707c478bd9Sstevel@tonic-gate 					if (buflen < 131072)
2717c478bd9Sstevel@tonic-gate 						buf = malloc(buflen += 1024);
2727c478bd9Sstevel@tonic-gate 					if (buf == NULL) {
2737c478bd9Sstevel@tonic-gate 						fprintf(file,
2747c478bd9Sstevel@tonic-gate 					      ";; memory allocation failure\n");
2757c478bd9Sstevel@tonic-gate 					      return;
2767c478bd9Sstevel@tonic-gate 					}
2777c478bd9Sstevel@tonic-gate 					continue;
2787c478bd9Sstevel@tonic-gate 				}
2797c478bd9Sstevel@tonic-gate 				fprintf(file, ";; ns_sprintrr: %s\n",
2807c478bd9Sstevel@tonic-gate 					strerror(errno));
2817c478bd9Sstevel@tonic-gate 				goto cleanup;
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 			fputs(buf, file);
2847c478bd9Sstevel@tonic-gate 			fputc('\n', file);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		rrnum++;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate  cleanup:
2897c478bd9Sstevel@tonic-gate 	if (buf != NULL)
2907c478bd9Sstevel@tonic-gate 		free(buf);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
293*9525b14bSRao Shoaib /*%
2947c478bd9Sstevel@tonic-gate  * Print the contents of a query.
2957c478bd9Sstevel@tonic-gate  * This is intended to be primarily a debugging routine.
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate void
res_pquery(const res_state statp,const u_char * msg,int len,FILE * file)2987c478bd9Sstevel@tonic-gate res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
2997c478bd9Sstevel@tonic-gate 	ns_msg handle;
3007c478bd9Sstevel@tonic-gate 	int qdcount, ancount, nscount, arcount;
3017c478bd9Sstevel@tonic-gate 	u_int opcode, rcode, id;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (ns_initparse(msg, len, &handle) < 0) {
3047c478bd9Sstevel@tonic-gate 		fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
3057c478bd9Sstevel@tonic-gate 		return;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	opcode = ns_msg_getflag(handle, ns_f_opcode);
3087c478bd9Sstevel@tonic-gate 	rcode = ns_msg_getflag(handle, ns_f_rcode);
3097c478bd9Sstevel@tonic-gate 	id = ns_msg_id(handle);
3107c478bd9Sstevel@tonic-gate 	qdcount = ns_msg_count(handle, ns_s_qd);
3117c478bd9Sstevel@tonic-gate 	ancount = ns_msg_count(handle, ns_s_an);
3127c478bd9Sstevel@tonic-gate 	nscount = ns_msg_count(handle, ns_s_ns);
3137c478bd9Sstevel@tonic-gate 	arcount = ns_msg_count(handle, ns_s_ar);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Print header fields.
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
3197c478bd9Sstevel@tonic-gate 		fprintf(file,
3207c478bd9Sstevel@tonic-gate 			";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
3217c478bd9Sstevel@tonic-gate 			_res_opcodes[opcode], p_rcode(rcode), id);
3227c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
3237c478bd9Sstevel@tonic-gate 		putc(';', file);
3247c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
3257c478bd9Sstevel@tonic-gate 		fprintf(file, "; flags:");
3267c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_qr))
3277c478bd9Sstevel@tonic-gate 			fprintf(file, " qr");
3287c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_aa))
3297c478bd9Sstevel@tonic-gate 			fprintf(file, " aa");
3307c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_tc))
3317c478bd9Sstevel@tonic-gate 			fprintf(file, " tc");
3327c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_rd))
3337c478bd9Sstevel@tonic-gate 			fprintf(file, " rd");
3347c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ra))
3357c478bd9Sstevel@tonic-gate 			fprintf(file, " ra");
3367c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_z))
3377c478bd9Sstevel@tonic-gate 			fprintf(file, " ??");
3387c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ad))
3397c478bd9Sstevel@tonic-gate 			fprintf(file, " ad");
3407c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_cd))
3417c478bd9Sstevel@tonic-gate 			fprintf(file, " cd");
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
3447c478bd9Sstevel@tonic-gate 		fprintf(file, "; %s: %d",
3457c478bd9Sstevel@tonic-gate 			p_section(ns_s_qd, opcode), qdcount);
3467c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3477c478bd9Sstevel@tonic-gate 			p_section(ns_s_an, opcode), ancount);
3487c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3497c478bd9Sstevel@tonic-gate 			p_section(ns_s_ns, opcode), nscount);
3507c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3517c478bd9Sstevel@tonic-gate 			p_section(ns_s_ar, opcode), arcount);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode &
3547c478bd9Sstevel@tonic-gate 		(RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
3557c478bd9Sstevel@tonic-gate 		putc('\n',file);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * Print the various sections.
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
3617c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
3627c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
3637c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
3647c478bd9Sstevel@tonic-gate 	if (qdcount == 0 && ancount == 0 &&
3657c478bd9Sstevel@tonic-gate 	    nscount == 0 && arcount == 0)
3667c478bd9Sstevel@tonic-gate 		putc('\n', file);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate const u_char *
p_cdnname(const u_char * cp,const u_char * msg,int len,FILE * file)3707c478bd9Sstevel@tonic-gate p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
3717c478bd9Sstevel@tonic-gate 	char name[MAXDNAME];
3727c478bd9Sstevel@tonic-gate 	int n;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
3757c478bd9Sstevel@tonic-gate 		return (NULL);
3767c478bd9Sstevel@tonic-gate 	if (name[0] == '\0')
3777c478bd9Sstevel@tonic-gate 		putc('.', file);
3787c478bd9Sstevel@tonic-gate 	else
3797c478bd9Sstevel@tonic-gate 		fputs(name, file);
3807c478bd9Sstevel@tonic-gate 	return (cp + n);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate const u_char *
p_cdname(const u_char * cp,const u_char * msg,FILE * file)3847c478bd9Sstevel@tonic-gate p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
3857c478bd9Sstevel@tonic-gate 	return (p_cdnname(cp, msg, PACKETSZ, file));
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
388*9525b14bSRao Shoaib /*%
389*9525b14bSRao Shoaib  * Return a fully-qualified domain name from a compressed name (with
3907c478bd9Sstevel@tonic-gate    length supplied).  */
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate const u_char *
p_fqnname(cp,msg,msglen,name,namelen)3937c478bd9Sstevel@tonic-gate p_fqnname(cp, msg, msglen, name, namelen)
3947c478bd9Sstevel@tonic-gate 	const u_char *cp, *msg;
3957c478bd9Sstevel@tonic-gate 	int msglen;
3967c478bd9Sstevel@tonic-gate 	char *name;
3977c478bd9Sstevel@tonic-gate 	int namelen;
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	int n, newlen;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
4027c478bd9Sstevel@tonic-gate 		return (NULL);
4037c478bd9Sstevel@tonic-gate 	newlen = strlen(name);
4047c478bd9Sstevel@tonic-gate 	if (newlen == 0 || name[newlen - 1] != '.') {
405*9525b14bSRao Shoaib 		if (newlen + 1 >= namelen)	/*%< Lack space for final dot */
4067c478bd9Sstevel@tonic-gate 			return (NULL);
4077c478bd9Sstevel@tonic-gate 		else
4087c478bd9Sstevel@tonic-gate 			strcpy(name + newlen, ".");
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (cp + n);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /* XXX:	the rest of these functions need to become length-limited, too. */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate const u_char *
p_fqname(const u_char * cp,const u_char * msg,FILE * file)4167c478bd9Sstevel@tonic-gate p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
4177c478bd9Sstevel@tonic-gate 	char name[MAXDNAME];
4187c478bd9Sstevel@tonic-gate 	const u_char *n;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
4217c478bd9Sstevel@tonic-gate 	if (n == NULL)
4227c478bd9Sstevel@tonic-gate 		return (NULL);
4237c478bd9Sstevel@tonic-gate 	fputs(name, file);
4247c478bd9Sstevel@tonic-gate 	return (n);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
427*9525b14bSRao Shoaib /*%
4287c478bd9Sstevel@tonic-gate  * Names of RR classes and qclasses.  Classes and qclasses are the same, except
4297c478bd9Sstevel@tonic-gate  * that C_ANY is a qclass but not a class.  (You can ask for records of class
4307c478bd9Sstevel@tonic-gate  * C_ANY, but you can't have any records of that class in the database.)
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@tonic-gate const struct res_sym __p_class_syms[] = {
4337c478bd9Sstevel@tonic-gate 	{C_IN,		"IN",		(char *)0},
434*9525b14bSRao Shoaib 	{C_CHAOS,	"CH",		(char *)0},
4357c478bd9Sstevel@tonic-gate 	{C_CHAOS,	"CHAOS",	(char *)0},
4367c478bd9Sstevel@tonic-gate 	{C_HS,		"HS",		(char *)0},
4377c478bd9Sstevel@tonic-gate 	{C_HS,		"HESIOD",	(char *)0},
4387c478bd9Sstevel@tonic-gate 	{C_ANY,		"ANY",		(char *)0},
4397c478bd9Sstevel@tonic-gate 	{C_NONE,	"NONE",		(char *)0},
4407c478bd9Sstevel@tonic-gate 	{C_IN, 		(char *)0,	(char *)0}
4417c478bd9Sstevel@tonic-gate };
4427c478bd9Sstevel@tonic-gate 
443*9525b14bSRao Shoaib /*%
4447c478bd9Sstevel@tonic-gate  * Names of message sections.
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate const struct res_sym __p_default_section_syms[] = {
4477c478bd9Sstevel@tonic-gate 	{ns_s_qd,	"QUERY",	(char *)0},
4487c478bd9Sstevel@tonic-gate 	{ns_s_an,	"ANSWER",	(char *)0},
4497c478bd9Sstevel@tonic-gate 	{ns_s_ns,	"AUTHORITY",	(char *)0},
4507c478bd9Sstevel@tonic-gate 	{ns_s_ar,	"ADDITIONAL",	(char *)0},
4517c478bd9Sstevel@tonic-gate 	{0,		(char *)0,	(char *)0}
4527c478bd9Sstevel@tonic-gate };
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate const struct res_sym __p_update_section_syms[] = {
4557c478bd9Sstevel@tonic-gate 	{S_ZONE,	"ZONE",		(char *)0},
4567c478bd9Sstevel@tonic-gate 	{S_PREREQ,	"PREREQUISITE",	(char *)0},
4577c478bd9Sstevel@tonic-gate 	{S_UPDATE,	"UPDATE",	(char *)0},
4587c478bd9Sstevel@tonic-gate 	{S_ADDT,	"ADDITIONAL",	(char *)0},
4597c478bd9Sstevel@tonic-gate 	{0,		(char *)0,	(char *)0}
4607c478bd9Sstevel@tonic-gate };
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate const struct res_sym __p_key_syms[] = {
4637c478bd9Sstevel@tonic-gate 	{NS_ALG_MD5RSA,		"RSA",		"RSA KEY with MD5 hash"},
4647c478bd9Sstevel@tonic-gate 	{NS_ALG_DH,		"DH",		"Diffie Hellman"},
4657c478bd9Sstevel@tonic-gate 	{NS_ALG_DSA,		"DSA",		"Digital Signature Algorithm"},
4667c478bd9Sstevel@tonic-gate 	{NS_ALG_EXPIRE_ONLY,	"EXPIREONLY",	"No algorithm"},
4677c478bd9Sstevel@tonic-gate 	{NS_ALG_PRIVATE_OID,	"PRIVATE",	"Algorithm obtained from OID"},
4687c478bd9Sstevel@tonic-gate 	{0,			NULL,		NULL}
4697c478bd9Sstevel@tonic-gate };
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate const struct res_sym __p_cert_syms[] = {
4727c478bd9Sstevel@tonic-gate 	{cert_t_pkix,	"PKIX",		"PKIX (X.509v3) Certificate"},
4737c478bd9Sstevel@tonic-gate 	{cert_t_spki,	"SPKI",		"SPKI certificate"},
4747c478bd9Sstevel@tonic-gate 	{cert_t_pgp,	"PGP",		"PGP certificate"},
4757c478bd9Sstevel@tonic-gate 	{cert_t_url,	"URL",		"URL Private"},
4767c478bd9Sstevel@tonic-gate 	{cert_t_oid,	"OID",		"OID Private"},
4777c478bd9Sstevel@tonic-gate 	{0,		NULL,		NULL}
4787c478bd9Sstevel@tonic-gate };
4797c478bd9Sstevel@tonic-gate 
480*9525b14bSRao Shoaib /*%
4817c478bd9Sstevel@tonic-gate  * Names of RR types and qtypes.  Types and qtypes are the same, except
4827c478bd9Sstevel@tonic-gate  * that T_ANY is a qtype but not a type.  (You can ask for records of type
4837c478bd9Sstevel@tonic-gate  * T_ANY, but you can't have any records of that type in the database.)
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate const struct res_sym __p_type_syms[] = {
4867c478bd9Sstevel@tonic-gate 	{ns_t_a,	"A",		"address"},
4877c478bd9Sstevel@tonic-gate 	{ns_t_ns,	"NS",		"name server"},
4887c478bd9Sstevel@tonic-gate 	{ns_t_md,	"MD",		"mail destination (deprecated)"},
4897c478bd9Sstevel@tonic-gate 	{ns_t_mf,	"MF",		"mail forwarder (deprecated)"},
4907c478bd9Sstevel@tonic-gate 	{ns_t_cname,	"CNAME",	"canonical name"},
4917c478bd9Sstevel@tonic-gate 	{ns_t_soa,	"SOA",		"start of authority"},
4927c478bd9Sstevel@tonic-gate 	{ns_t_mb,	"MB",		"mailbox"},
4937c478bd9Sstevel@tonic-gate 	{ns_t_mg,	"MG",		"mail group member"},
4947c478bd9Sstevel@tonic-gate 	{ns_t_mr,	"MR",		"mail rename"},
4957c478bd9Sstevel@tonic-gate 	{ns_t_null,	"NULL",		"null"},
4967c478bd9Sstevel@tonic-gate 	{ns_t_wks,	"WKS",		"well-known service (deprecated)"},
4977c478bd9Sstevel@tonic-gate 	{ns_t_ptr,	"PTR",		"domain name pointer"},
4987c478bd9Sstevel@tonic-gate 	{ns_t_hinfo,	"HINFO",	"host information"},
4997c478bd9Sstevel@tonic-gate 	{ns_t_minfo,	"MINFO",	"mailbox information"},
5007c478bd9Sstevel@tonic-gate 	{ns_t_mx,	"MX",		"mail exchanger"},
5017c478bd9Sstevel@tonic-gate 	{ns_t_txt,	"TXT",		"text"},
5027c478bd9Sstevel@tonic-gate 	{ns_t_rp,	"RP",		"responsible person"},
5037c478bd9Sstevel@tonic-gate 	{ns_t_afsdb,	"AFSDB",	"DCE or AFS server"},
5047c478bd9Sstevel@tonic-gate 	{ns_t_x25,	"X25",		"X25 address"},
5057c478bd9Sstevel@tonic-gate 	{ns_t_isdn,	"ISDN",		"ISDN address"},
5067c478bd9Sstevel@tonic-gate 	{ns_t_rt,	"RT",		"router"},
5077c478bd9Sstevel@tonic-gate 	{ns_t_nsap,	"NSAP",		"nsap address"},
5087c478bd9Sstevel@tonic-gate 	{ns_t_nsap_ptr,	"NSAP_PTR",	"domain name pointer"},
5097c478bd9Sstevel@tonic-gate 	{ns_t_sig,	"SIG",		"signature"},
5107c478bd9Sstevel@tonic-gate 	{ns_t_key,	"KEY",		"key"},
5117c478bd9Sstevel@tonic-gate 	{ns_t_px,	"PX",		"mapping information"},
5127c478bd9Sstevel@tonic-gate 	{ns_t_gpos,	"GPOS",		"geographical position (withdrawn)"},
5137c478bd9Sstevel@tonic-gate 	{ns_t_aaaa,	"AAAA",		"IPv6 address"},
5147c478bd9Sstevel@tonic-gate 	{ns_t_loc,	"LOC",		"location"},
5157c478bd9Sstevel@tonic-gate 	{ns_t_nxt,	"NXT",		"next valid name (unimplemented)"},
5167c478bd9Sstevel@tonic-gate 	{ns_t_eid,	"EID",		"endpoint identifier (unimplemented)"},
5177c478bd9Sstevel@tonic-gate 	{ns_t_nimloc,	"NIMLOC",	"NIMROD locator (unimplemented)"},
5187c478bd9Sstevel@tonic-gate 	{ns_t_srv,	"SRV",		"server selection"},
5197c478bd9Sstevel@tonic-gate 	{ns_t_atma,	"ATMA",		"ATM address (unimplemented)"},
520*9525b14bSRao Shoaib 	{ns_t_naptr,	"NAPTR",	"naptr"},
521*9525b14bSRao Shoaib 	{ns_t_kx,	"KX",		"key exchange"},
522*9525b14bSRao Shoaib 	{ns_t_cert,	"CERT",		"certificate"},
523*9525b14bSRao Shoaib 	{ns_t_a6,	"A",		"IPv6 address (experminental)"},
524*9525b14bSRao Shoaib 	{ns_t_dname,	"DNAME",	"non-terminal redirection"},
525*9525b14bSRao Shoaib 	{ns_t_opt,	"OPT",		"opt"},
526*9525b14bSRao Shoaib 	{ns_t_apl,	"apl",		"apl"},
527*9525b14bSRao Shoaib 	{ns_t_ds,	"DS",		"delegation signer"},
528*9525b14bSRao Shoaib 	{ns_t_sshfp,	"SSFP",		"SSH fingerprint"},
529*9525b14bSRao Shoaib 	{ns_t_ipseckey,	"IPSECKEY",	"IPSEC key"},
530*9525b14bSRao Shoaib 	{ns_t_rrsig,	"RRSIG",	"rrsig"},
531*9525b14bSRao Shoaib 	{ns_t_nsec,	"NSEC",		"nsec"},
532*9525b14bSRao Shoaib 	{ns_t_dnskey,	"DNSKEY",	"DNS key"},
533*9525b14bSRao Shoaib 	{ns_t_dhcid,	"DHCID",       "dynamic host configuration identifier"},
534*9525b14bSRao Shoaib 	{ns_t_nsec3,	"NSEC3",	"nsec3"},
535*9525b14bSRao Shoaib 	{ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
536*9525b14bSRao Shoaib 	{ns_t_hip,	"HIP",		"host identity protocol"},
537*9525b14bSRao Shoaib 	{ns_t_spf,	"SPF",		"sender policy framework"},
5387c478bd9Sstevel@tonic-gate 	{ns_t_tkey,	"TKEY",		"tkey"},
5397c478bd9Sstevel@tonic-gate 	{ns_t_tsig,	"TSIG",		"transaction signature"},
5407c478bd9Sstevel@tonic-gate 	{ns_t_ixfr,	"IXFR",		"incremental zone transfer"},
5417c478bd9Sstevel@tonic-gate 	{ns_t_axfr,	"AXFR",		"zone transfer"},
5427c478bd9Sstevel@tonic-gate 	{ns_t_zxfr,	"ZXFR",		"compressed zone transfer"},
5437c478bd9Sstevel@tonic-gate 	{ns_t_mailb,	"MAILB",	"mailbox-related data (deprecated)"},
5447c478bd9Sstevel@tonic-gate 	{ns_t_maila,	"MAILA",	"mail agent (deprecated)"},
5457c478bd9Sstevel@tonic-gate 	{ns_t_naptr,	"NAPTR",	"URN Naming Authority"},
5467c478bd9Sstevel@tonic-gate 	{ns_t_kx,	"KX",		"Key Exchange"},
5477c478bd9Sstevel@tonic-gate 	{ns_t_cert,	"CERT",		"Certificate"},
5487c478bd9Sstevel@tonic-gate 	{ns_t_a6,	"A6",		"IPv6 Address"},
5497c478bd9Sstevel@tonic-gate 	{ns_t_dname,	"DNAME",	"dname"},
5507c478bd9Sstevel@tonic-gate 	{ns_t_sink,	"SINK",		"Kitchen Sink (experimental)"},
5517c478bd9Sstevel@tonic-gate 	{ns_t_opt,	"OPT",		"EDNS Options"},
5527c478bd9Sstevel@tonic-gate 	{ns_t_any,	"ANY",		"\"any\""},
553*9525b14bSRao Shoaib 	{ns_t_dlv,	"DLV",		"DNSSEC look-aside validation"},
5547c478bd9Sstevel@tonic-gate 	{0, 		NULL,		NULL}
5557c478bd9Sstevel@tonic-gate };
5567c478bd9Sstevel@tonic-gate 
557*9525b14bSRao Shoaib /*%
5587c478bd9Sstevel@tonic-gate  * Names of DNS rcodes.
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate const struct res_sym __p_rcode_syms[] = {
5617c478bd9Sstevel@tonic-gate 	{ns_r_noerror,	"NOERROR",		"no error"},
5627c478bd9Sstevel@tonic-gate 	{ns_r_formerr,	"FORMERR",		"format error"},
5637c478bd9Sstevel@tonic-gate 	{ns_r_servfail,	"SERVFAIL",		"server failed"},
5647c478bd9Sstevel@tonic-gate 	{ns_r_nxdomain,	"NXDOMAIN",		"no such domain name"},
5657c478bd9Sstevel@tonic-gate 	{ns_r_notimpl,	"NOTIMP",		"not implemented"},
5667c478bd9Sstevel@tonic-gate 	{ns_r_refused,	"REFUSED",		"refused"},
5677c478bd9Sstevel@tonic-gate 	{ns_r_yxdomain,	"YXDOMAIN",		"domain name exists"},
5687c478bd9Sstevel@tonic-gate 	{ns_r_yxrrset,	"YXRRSET",		"rrset exists"},
5697c478bd9Sstevel@tonic-gate 	{ns_r_nxrrset,	"NXRRSET",		"rrset doesn't exist"},
5707c478bd9Sstevel@tonic-gate 	{ns_r_notauth,	"NOTAUTH",		"not authoritative"},
5717c478bd9Sstevel@tonic-gate 	{ns_r_notzone,	"NOTZONE",		"Not in zone"},
5727c478bd9Sstevel@tonic-gate 	{ns_r_max,	"",			""},
5737c478bd9Sstevel@tonic-gate 	{ns_r_badsig,	"BADSIG",		"bad signature"},
5747c478bd9Sstevel@tonic-gate 	{ns_r_badkey,	"BADKEY",		"bad key"},
5757c478bd9Sstevel@tonic-gate 	{ns_r_badtime,	"BADTIME",		"bad time"},
5767c478bd9Sstevel@tonic-gate 	{0, 		NULL,			NULL}
5777c478bd9Sstevel@tonic-gate };
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate int
sym_ston(const struct res_sym * syms,const char * name,int * success)5807c478bd9Sstevel@tonic-gate sym_ston(const struct res_sym *syms, const char *name, int *success) {
5817c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
5827c478bd9Sstevel@tonic-gate 		if (strcasecmp (name, syms->name) == 0) {
5837c478bd9Sstevel@tonic-gate 			if (success)
5847c478bd9Sstevel@tonic-gate 				*success = 1;
5857c478bd9Sstevel@tonic-gate 			return (syms->number);
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	if (success)
5897c478bd9Sstevel@tonic-gate 		*success = 0;
590*9525b14bSRao Shoaib 	return (syms->number);		/*%< The default value. */
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate const char *
sym_ntos(const struct res_sym * syms,int number,int * success)5947c478bd9Sstevel@tonic-gate sym_ntos(const struct res_sym *syms, int number, int *success) {
5957c478bd9Sstevel@tonic-gate 	char *unname = sym_ntos_unname;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
5987c478bd9Sstevel@tonic-gate 		if (number == syms->number) {
5997c478bd9Sstevel@tonic-gate 			if (success)
6007c478bd9Sstevel@tonic-gate 				*success = 1;
6017c478bd9Sstevel@tonic-gate 			return (syms->name);
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
605*9525b14bSRao Shoaib 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6067c478bd9Sstevel@tonic-gate 	if (success)
6077c478bd9Sstevel@tonic-gate 		*success = 0;
6087c478bd9Sstevel@tonic-gate 	return (unname);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate const char *
sym_ntop(const struct res_sym * syms,int number,int * success)6127c478bd9Sstevel@tonic-gate sym_ntop(const struct res_sym *syms, int number, int *success) {
6137c478bd9Sstevel@tonic-gate 	char *unname = sym_ntop_unname;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
6167c478bd9Sstevel@tonic-gate 		if (number == syms->number) {
6177c478bd9Sstevel@tonic-gate 			if (success)
6187c478bd9Sstevel@tonic-gate 				*success = 1;
6197c478bd9Sstevel@tonic-gate 			return (syms->humanname);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 	}
622*9525b14bSRao Shoaib 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6237c478bd9Sstevel@tonic-gate 	if (success)
6247c478bd9Sstevel@tonic-gate 		*success = 0;
6257c478bd9Sstevel@tonic-gate 	return (unname);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
628*9525b14bSRao Shoaib /*%
6297c478bd9Sstevel@tonic-gate  * Return a string for the type.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate const char *
p_type(int type)6327c478bd9Sstevel@tonic-gate p_type(int type) {
6337c478bd9Sstevel@tonic-gate 	int success;
6347c478bd9Sstevel@tonic-gate 	const char *result;
6357c478bd9Sstevel@tonic-gate 	static char typebuf[20];
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	result = sym_ntos(__p_type_syms, type, &success);
6387c478bd9Sstevel@tonic-gate 	if (success)
6397c478bd9Sstevel@tonic-gate 		return (result);
640*9525b14bSRao Shoaib 	if (type < 0 || type > 0xffff)
6417c478bd9Sstevel@tonic-gate 		return ("BADTYPE");
6427c478bd9Sstevel@tonic-gate 	sprintf(typebuf, "TYPE%d", type);
6437c478bd9Sstevel@tonic-gate 	return (typebuf);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
646*9525b14bSRao Shoaib /*%
6477c478bd9Sstevel@tonic-gate  * Return a string for the type.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate const char *
p_section(int section,int opcode)6507c478bd9Sstevel@tonic-gate p_section(int section, int opcode) {
6517c478bd9Sstevel@tonic-gate 	const struct res_sym *symbols;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	switch (opcode) {
6547c478bd9Sstevel@tonic-gate 	case ns_o_update:
6557c478bd9Sstevel@tonic-gate 		symbols = __p_update_section_syms;
6567c478bd9Sstevel@tonic-gate 		break;
6577c478bd9Sstevel@tonic-gate 	default:
6587c478bd9Sstevel@tonic-gate 		symbols = __p_default_section_syms;
6597c478bd9Sstevel@tonic-gate 		break;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 	return (sym_ntos(symbols, section, (int *)0));
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
664*9525b14bSRao Shoaib /*%
6657c478bd9Sstevel@tonic-gate  * Return a mnemonic for class.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate const char *
p_class(int class)6687c478bd9Sstevel@tonic-gate p_class(int class) {
6697c478bd9Sstevel@tonic-gate 	int success;
6707c478bd9Sstevel@tonic-gate 	const char *result;
6717c478bd9Sstevel@tonic-gate 	static char classbuf[20];
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	result = sym_ntos(__p_class_syms, class, &success);
6747c478bd9Sstevel@tonic-gate 	if (success)
6757c478bd9Sstevel@tonic-gate 		return (result);
676*9525b14bSRao Shoaib 	if (class < 0 || class > 0xffff)
6777c478bd9Sstevel@tonic-gate 		return ("BADCLASS");
6787c478bd9Sstevel@tonic-gate 	sprintf(classbuf, "CLASS%d", class);
6797c478bd9Sstevel@tonic-gate 	return (classbuf);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
682*9525b14bSRao Shoaib /*%
6837c478bd9Sstevel@tonic-gate  * Return a mnemonic for an option
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate const char *
p_option(u_long option)6867c478bd9Sstevel@tonic-gate p_option(u_long option) {
6877c478bd9Sstevel@tonic-gate 	char *nbuf = p_option_nbuf;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	switch (option) {
6907c478bd9Sstevel@tonic-gate 	case RES_INIT:		return "init";
6917c478bd9Sstevel@tonic-gate 	case RES_DEBUG:		return "debug";
6927c478bd9Sstevel@tonic-gate 	case RES_AAONLY:	return "aaonly(unimpl)";
6937c478bd9Sstevel@tonic-gate 	case RES_USEVC:		return "usevc";
6947c478bd9Sstevel@tonic-gate 	case RES_PRIMARY:	return "primry(unimpl)";
6957c478bd9Sstevel@tonic-gate 	case RES_IGNTC:		return "igntc";
6967c478bd9Sstevel@tonic-gate 	case RES_RECURSE:	return "recurs";
6977c478bd9Sstevel@tonic-gate 	case RES_DEFNAMES:	return "defnam";
6987c478bd9Sstevel@tonic-gate 	case RES_STAYOPEN:	return "styopn";
6997c478bd9Sstevel@tonic-gate 	case RES_DNSRCH:	return "dnsrch";
7007c478bd9Sstevel@tonic-gate 	case RES_INSECURE1:	return "insecure1";
7017c478bd9Sstevel@tonic-gate 	case RES_INSECURE2:	return "insecure2";
7027c478bd9Sstevel@tonic-gate 	case RES_NOALIASES:	return "noaliases";
7037c478bd9Sstevel@tonic-gate 	case RES_USE_INET6:	return "inet6";
704*9525b14bSRao Shoaib #ifdef RES_USE_EDNS0	/*%< KAME extension */
7057c478bd9Sstevel@tonic-gate 	case RES_USE_EDNS0:	return "edns0";
706*9525b14bSRao Shoaib 	case RES_NSID:		return "nsid";
7077c478bd9Sstevel@tonic-gate #endif
7087c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNAME
7097c478bd9Sstevel@tonic-gate 	case RES_USE_DNAME:	return "dname";
7107c478bd9Sstevel@tonic-gate #endif
7117c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNSSEC
7127c478bd9Sstevel@tonic-gate 	case RES_USE_DNSSEC:	return "dnssec";
7137c478bd9Sstevel@tonic-gate #endif
7147c478bd9Sstevel@tonic-gate #ifdef RES_NOTLDQUERY
7157c478bd9Sstevel@tonic-gate 	case RES_NOTLDQUERY:	return "no-tld-query";
7167c478bd9Sstevel@tonic-gate #endif
717*9525b14bSRao Shoaib #ifdef RES_NO_NIBBLE2
718*9525b14bSRao Shoaib 	case RES_NO_NIBBLE2:	return "no-nibble2";
719*9525b14bSRao Shoaib #endif
7207c478bd9Sstevel@tonic-gate 				/* XXX nonreentrant */
7217c478bd9Sstevel@tonic-gate 	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
7227c478bd9Sstevel@tonic-gate 				return (nbuf);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
726*9525b14bSRao Shoaib /*%
7277c478bd9Sstevel@tonic-gate  * Return a mnemonic for a time to live.
7287c478bd9Sstevel@tonic-gate  */
7297c478bd9Sstevel@tonic-gate const char *
p_time(u_int32_t value)7307c478bd9Sstevel@tonic-gate p_time(u_int32_t value) {
7317c478bd9Sstevel@tonic-gate 	char *nbuf = p_time_nbuf;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
7347c478bd9Sstevel@tonic-gate 		sprintf(nbuf, "%u", value);
7357c478bd9Sstevel@tonic-gate 	return (nbuf);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
738*9525b14bSRao Shoaib /*%
7397c478bd9Sstevel@tonic-gate  * Return a string for the rcode.
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate const char *
p_rcode(int rcode)7427c478bd9Sstevel@tonic-gate p_rcode(int rcode) {
7437c478bd9Sstevel@tonic-gate 	return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
746*9525b14bSRao Shoaib /*%
7477c478bd9Sstevel@tonic-gate  * Return a string for a res_sockaddr_union.
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate const char *
p_sockun(union res_sockaddr_union u,char * buf,size_t size)7507c478bd9Sstevel@tonic-gate p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
7517c478bd9Sstevel@tonic-gate 	char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	switch (u.sin.sin_family) {
7547c478bd9Sstevel@tonic-gate 	case AF_INET:
7557c478bd9Sstevel@tonic-gate 		inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
7567c478bd9Sstevel@tonic-gate 		break;
7577c478bd9Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
7587c478bd9Sstevel@tonic-gate 	case AF_INET6:
7597c478bd9Sstevel@tonic-gate 		inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
7607c478bd9Sstevel@tonic-gate 		break;
7617c478bd9Sstevel@tonic-gate #endif
7627c478bd9Sstevel@tonic-gate 	default:
7637c478bd9Sstevel@tonic-gate 		sprintf(ret, "[af%d]", u.sin.sin_family);
7647c478bd9Sstevel@tonic-gate 		break;
7657c478bd9Sstevel@tonic-gate 	}
766*9525b14bSRao Shoaib 	if (size > 0U) {
7677c478bd9Sstevel@tonic-gate 		strncpy(buf, ret, size - 1);
7687c478bd9Sstevel@tonic-gate 		buf[size - 1] = '0';
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	return (buf);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
773*9525b14bSRao Shoaib /*%
7747c478bd9Sstevel@tonic-gate  * routines to convert between on-the-wire RR format and zone file format.
7757c478bd9Sstevel@tonic-gate  * Does not contain conversion to/from decimal degrees; divide or multiply
7767c478bd9Sstevel@tonic-gate  * by 60*60*1000 for that.
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
7807c478bd9Sstevel@tonic-gate 				      1000000,10000000,100000000,1000000000};
7817c478bd9Sstevel@tonic-gate 
782*9525b14bSRao Shoaib /*% takes an XeY precision/size value, returns a string representation. */
7837c478bd9Sstevel@tonic-gate static const char *
precsize_ntoa(prec)7847c478bd9Sstevel@tonic-gate precsize_ntoa(prec)
7857c478bd9Sstevel@tonic-gate 	u_int8_t prec;
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	char *retbuf = precsize_ntoa_retbuf;
7887c478bd9Sstevel@tonic-gate 	unsigned long val;
7897c478bd9Sstevel@tonic-gate 	int mantissa, exponent;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	mantissa = (int)((prec >> 4) & 0x0f) % 10;
7927c478bd9Sstevel@tonic-gate 	exponent = (int)((prec >> 0) & 0x0f) % 10;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	val = mantissa * poweroften[exponent];
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	(void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
7977c478bd9Sstevel@tonic-gate 	return (retbuf);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
800*9525b14bSRao Shoaib /*% converts ascii size/precision X * 10**Y(cm) to 0xXY.  moves pointer.  */
8017c478bd9Sstevel@tonic-gate static u_int8_t
precsize_aton(const char ** strptr)8027c478bd9Sstevel@tonic-gate precsize_aton(const char **strptr) {
8037c478bd9Sstevel@tonic-gate 	unsigned int mval = 0, cmval = 0;
8047c478bd9Sstevel@tonic-gate 	u_int8_t retval = 0;
8057c478bd9Sstevel@tonic-gate 	const char *cp;
8067c478bd9Sstevel@tonic-gate 	int exponent;
8077c478bd9Sstevel@tonic-gate 	int mantissa;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	cp = *strptr;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8127c478bd9Sstevel@tonic-gate 		mval = mval * 10 + (*cp++ - '0');
8137c478bd9Sstevel@tonic-gate 
814*9525b14bSRao Shoaib 	if (*cp == '.') {		/*%< centimeters */
8157c478bd9Sstevel@tonic-gate 		cp++;
8167c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
8177c478bd9Sstevel@tonic-gate 			cmval = (*cp++ - '0') * 10;
8187c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
8197c478bd9Sstevel@tonic-gate 				cmval += (*cp++ - '0');
8207c478bd9Sstevel@tonic-gate 			}
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	cmval = (mval * 100) + cmval;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	for (exponent = 0; exponent < 9; exponent++)
8267c478bd9Sstevel@tonic-gate 		if (cmval < poweroften[exponent+1])
8277c478bd9Sstevel@tonic-gate 			break;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	mantissa = cmval / poweroften[exponent];
8307c478bd9Sstevel@tonic-gate 	if (mantissa > 9)
8317c478bd9Sstevel@tonic-gate 		mantissa = 9;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	retval = (mantissa << 4) | exponent;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	*strptr = cp;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	return (retval);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
840*9525b14bSRao Shoaib /*% converts ascii lat/lon to unsigned encoded 32-bit number.  moves pointer. */
8417c478bd9Sstevel@tonic-gate static u_int32_t
latlon2ul(const char ** latlonstrptr,int * which)8427c478bd9Sstevel@tonic-gate latlon2ul(const char **latlonstrptr, int *which) {
8437c478bd9Sstevel@tonic-gate 	const char *cp;
8447c478bd9Sstevel@tonic-gate 	u_int32_t retval;
8457c478bd9Sstevel@tonic-gate 	int deg = 0, min = 0, secs = 0, secsfrac = 0;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	cp = *latlonstrptr;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8507c478bd9Sstevel@tonic-gate 		deg = deg * 10 + (*cp++ - '0');
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8537c478bd9Sstevel@tonic-gate 		cp++;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
8567c478bd9Sstevel@tonic-gate 		goto fndhemi;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8597c478bd9Sstevel@tonic-gate 		min = min * 10 + (*cp++ - '0');
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8627c478bd9Sstevel@tonic-gate 		cp++;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
8657c478bd9Sstevel@tonic-gate 		goto fndhemi;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8687c478bd9Sstevel@tonic-gate 		secs = secs * 10 + (*cp++ - '0');
8697c478bd9Sstevel@tonic-gate 
870*9525b14bSRao Shoaib 	if (*cp == '.') {		/*%< decimal seconds */
8717c478bd9Sstevel@tonic-gate 		cp++;
8727c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
8737c478bd9Sstevel@tonic-gate 			secsfrac = (*cp++ - '0') * 100;
8747c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
8757c478bd9Sstevel@tonic-gate 				secsfrac += (*cp++ - '0') * 10;
8767c478bd9Sstevel@tonic-gate 				if (isdigit((unsigned char)*cp)) {
8777c478bd9Sstevel@tonic-gate 					secsfrac += (*cp++ - '0');
8787c478bd9Sstevel@tonic-gate 				}
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
883*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
8847c478bd9Sstevel@tonic-gate 		cp++;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8877c478bd9Sstevel@tonic-gate 		cp++;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate  fndhemi:
8907c478bd9Sstevel@tonic-gate 	switch (*cp) {
8917c478bd9Sstevel@tonic-gate 	case 'N': case 'n':
8927c478bd9Sstevel@tonic-gate 	case 'E': case 'e':
8937c478bd9Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
8947c478bd9Sstevel@tonic-gate 			+ (((((deg * 60) + min) * 60) + secs) * 1000)
8957c478bd9Sstevel@tonic-gate 			+ secsfrac;
8967c478bd9Sstevel@tonic-gate 		break;
8977c478bd9Sstevel@tonic-gate 	case 'S': case 's':
8987c478bd9Sstevel@tonic-gate 	case 'W': case 'w':
8997c478bd9Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
9007c478bd9Sstevel@tonic-gate 			- (((((deg * 60) + min) * 60) + secs) * 1000)
9017c478bd9Sstevel@tonic-gate 			- secsfrac;
9027c478bd9Sstevel@tonic-gate 		break;
9037c478bd9Sstevel@tonic-gate 	default:
904*9525b14bSRao Shoaib 		retval = 0;	/*%< invalid value -- indicates error */
9057c478bd9Sstevel@tonic-gate 		break;
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	switch (*cp) {
9097c478bd9Sstevel@tonic-gate 	case 'N': case 'n':
9107c478bd9Sstevel@tonic-gate 	case 'S': case 's':
911*9525b14bSRao Shoaib 		*which = 1;	/*%< latitude */
9127c478bd9Sstevel@tonic-gate 		break;
9137c478bd9Sstevel@tonic-gate 	case 'E': case 'e':
9147c478bd9Sstevel@tonic-gate 	case 'W': case 'w':
915*9525b14bSRao Shoaib 		*which = 2;	/*%< longitude */
9167c478bd9Sstevel@tonic-gate 		break;
9177c478bd9Sstevel@tonic-gate 	default:
918*9525b14bSRao Shoaib 		*which = 0;	/*%< error */
9197c478bd9Sstevel@tonic-gate 		break;
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
922*9525b14bSRao Shoaib 	cp++;			/*%< skip the hemisphere */
923*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
9247c478bd9Sstevel@tonic-gate 		cp++;
9257c478bd9Sstevel@tonic-gate 
926*9525b14bSRao Shoaib 	while (isspace((unsigned char)*cp))	/*%< move to next field */
9277c478bd9Sstevel@tonic-gate 		cp++;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	*latlonstrptr = cp;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	return (retval);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
934*9525b14bSRao Shoaib /*%
935*9525b14bSRao Shoaib  * converts a zone file representation in a string to an RDATA on-the-wire
9367c478bd9Sstevel@tonic-gate  * representation. */
9377c478bd9Sstevel@tonic-gate int
loc_aton(ascii,binary)9387c478bd9Sstevel@tonic-gate loc_aton(ascii, binary)
9397c478bd9Sstevel@tonic-gate 	const char *ascii;
9407c478bd9Sstevel@tonic-gate 	u_char *binary;
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	const char *cp, *maxcp;
9437c478bd9Sstevel@tonic-gate 	u_char *bcp;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	u_int32_t latit = 0, longit = 0, alt = 0;
9467c478bd9Sstevel@tonic-gate 	u_int32_t lltemp1 = 0, lltemp2 = 0;
9477c478bd9Sstevel@tonic-gate 	int altmeters = 0, altfrac = 0, altsign = 1;
948*9525b14bSRao Shoaib 	u_int8_t hp = 0x16;	/*%< default = 1e6 cm = 10000.00m = 10km */
949*9525b14bSRao Shoaib 	u_int8_t vp = 0x13;	/*%< default = 1e3 cm = 10.00m */
950*9525b14bSRao Shoaib 	u_int8_t siz = 0x12;	/*%< default = 1e2 cm = 1.00m */
9517c478bd9Sstevel@tonic-gate 	int which1 = 0, which2 = 0;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	cp = ascii;
9547c478bd9Sstevel@tonic-gate 	maxcp = cp + strlen(ascii);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	lltemp1 = latlon2ul(&cp, &which1);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	lltemp2 = latlon2ul(&cp, &which2);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	switch (which1 + which2) {
961*9525b14bSRao Shoaib 	case 3:			/*%< 1 + 2, the only valid combination */
962*9525b14bSRao Shoaib 		if ((which1 == 1) && (which2 == 2)) { /*%< normal case */
9637c478bd9Sstevel@tonic-gate 			latit = lltemp1;
9647c478bd9Sstevel@tonic-gate 			longit = lltemp2;
965*9525b14bSRao Shoaib 		} else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */
9667c478bd9Sstevel@tonic-gate 			longit = lltemp1;
9677c478bd9Sstevel@tonic-gate 			latit = lltemp2;
968*9525b14bSRao Shoaib 		} else {	/*%< some kind of brokenness */
9697c478bd9Sstevel@tonic-gate 			return (0);
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 		break;
972*9525b14bSRao Shoaib 	default:		/*%< we didn't get one of each */
9737c478bd9Sstevel@tonic-gate 		return (0);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	/* altitude */
9777c478bd9Sstevel@tonic-gate 	if (*cp == '-') {
9787c478bd9Sstevel@tonic-gate 		altsign = -1;
9797c478bd9Sstevel@tonic-gate 		cp++;
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (*cp == '+')
9837c478bd9Sstevel@tonic-gate 		cp++;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
9867c478bd9Sstevel@tonic-gate 		altmeters = altmeters * 10 + (*cp++ - '0');
9877c478bd9Sstevel@tonic-gate 
988*9525b14bSRao Shoaib 	if (*cp == '.') {		/*%< decimal meters */
9897c478bd9Sstevel@tonic-gate 		cp++;
9907c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
9917c478bd9Sstevel@tonic-gate 			altfrac = (*cp++ - '0') * 10;
9927c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
9937c478bd9Sstevel@tonic-gate 				altfrac += (*cp++ - '0');
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
9997c478bd9Sstevel@tonic-gate 
1000*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
10017c478bd9Sstevel@tonic-gate 		cp++;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10047c478bd9Sstevel@tonic-gate 		cp++;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
10077c478bd9Sstevel@tonic-gate 		goto defaults;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	siz = precsize_aton(&cp);
10107c478bd9Sstevel@tonic-gate 
1011*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10127c478bd9Sstevel@tonic-gate 		cp++;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10157c478bd9Sstevel@tonic-gate 		cp++;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
10187c478bd9Sstevel@tonic-gate 		goto defaults;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	hp = precsize_aton(&cp);
10217c478bd9Sstevel@tonic-gate 
1022*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10237c478bd9Sstevel@tonic-gate 		cp++;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10267c478bd9Sstevel@tonic-gate 		cp++;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
10297c478bd9Sstevel@tonic-gate 		goto defaults;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	vp = precsize_aton(&cp);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate  defaults:
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	bcp = binary;
1036*9525b14bSRao Shoaib 	*bcp++ = (u_int8_t) 0;	/*%< version byte */
10377c478bd9Sstevel@tonic-gate 	*bcp++ = siz;
10387c478bd9Sstevel@tonic-gate 	*bcp++ = hp;
10397c478bd9Sstevel@tonic-gate 	*bcp++ = vp;
10407c478bd9Sstevel@tonic-gate 	PUTLONG(latit,bcp);
10417c478bd9Sstevel@tonic-gate 	PUTLONG(longit,bcp);
10427c478bd9Sstevel@tonic-gate 	PUTLONG(alt,bcp);
10437c478bd9Sstevel@tonic-gate 
1044*9525b14bSRao Shoaib 	return (16);		/*%< size of RR in octets */
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
1047*9525b14bSRao Shoaib /*% takes an on-the-wire LOC RR and formats it in a human readable format. */
10487c478bd9Sstevel@tonic-gate const char *
loc_ntoa(binary,ascii)10497c478bd9Sstevel@tonic-gate loc_ntoa(binary, ascii)
10507c478bd9Sstevel@tonic-gate 	const u_char *binary;
10517c478bd9Sstevel@tonic-gate 	char *ascii;
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	static const char *error = "?";
10547c478bd9Sstevel@tonic-gate 	static char tmpbuf[sizeof
10557c478bd9Sstevel@tonic-gate "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
10567c478bd9Sstevel@tonic-gate 	const u_char *cp = binary;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	int latdeg, latmin, latsec, latsecfrac;
10597c478bd9Sstevel@tonic-gate 	int longdeg, longmin, longsec, longsecfrac;
10607c478bd9Sstevel@tonic-gate 	char northsouth, eastwest;
10617c478bd9Sstevel@tonic-gate 	const char *altsign;
10627c478bd9Sstevel@tonic-gate 	int altmeters, altfrac;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	const u_int32_t referencealt = 100000 * 100;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	int32_t latval, longval, altval;
10677c478bd9Sstevel@tonic-gate 	u_int32_t templ;
10687c478bd9Sstevel@tonic-gate 	u_int8_t sizeval, hpval, vpval, versionval;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	char *sizestr, *hpstr, *vpstr;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	versionval = *cp++;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (ascii == NULL)
10757c478bd9Sstevel@tonic-gate 		ascii = tmpbuf;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (versionval) {
10787c478bd9Sstevel@tonic-gate 		(void) sprintf(ascii, "; error: unknown LOC RR version");
10797c478bd9Sstevel@tonic-gate 		return (ascii);
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	sizeval = *cp++;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	hpval = *cp++;
10857c478bd9Sstevel@tonic-gate 	vpval = *cp++;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
10887c478bd9Sstevel@tonic-gate 	latval = (templ - ((unsigned)1<<31));
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
10917c478bd9Sstevel@tonic-gate 	longval = (templ - ((unsigned)1<<31));
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
1094*9525b14bSRao Shoaib 	if (templ < referencealt) { /*%< below WGS 84 spheroid */
10957c478bd9Sstevel@tonic-gate 		altval = referencealt - templ;
10967c478bd9Sstevel@tonic-gate 		altsign = "-";
10977c478bd9Sstevel@tonic-gate 	} else {
10987c478bd9Sstevel@tonic-gate 		altval = templ - referencealt;
10997c478bd9Sstevel@tonic-gate 		altsign = "";
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	if (latval < 0) {
11037c478bd9Sstevel@tonic-gate 		northsouth = 'S';
11047c478bd9Sstevel@tonic-gate 		latval = -latval;
11057c478bd9Sstevel@tonic-gate 	} else
11067c478bd9Sstevel@tonic-gate 		northsouth = 'N';
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	latsecfrac = latval % 1000;
11097c478bd9Sstevel@tonic-gate 	latval = latval / 1000;
11107c478bd9Sstevel@tonic-gate 	latsec = latval % 60;
11117c478bd9Sstevel@tonic-gate 	latval = latval / 60;
11127c478bd9Sstevel@tonic-gate 	latmin = latval % 60;
11137c478bd9Sstevel@tonic-gate 	latval = latval / 60;
11147c478bd9Sstevel@tonic-gate 	latdeg = latval;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	if (longval < 0) {
11177c478bd9Sstevel@tonic-gate 		eastwest = 'W';
11187c478bd9Sstevel@tonic-gate 		longval = -longval;
11197c478bd9Sstevel@tonic-gate 	} else
11207c478bd9Sstevel@tonic-gate 		eastwest = 'E';
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	longsecfrac = longval % 1000;
11237c478bd9Sstevel@tonic-gate 	longval = longval / 1000;
11247c478bd9Sstevel@tonic-gate 	longsec = longval % 60;
11257c478bd9Sstevel@tonic-gate 	longval = longval / 60;
11267c478bd9Sstevel@tonic-gate 	longmin = longval % 60;
11277c478bd9Sstevel@tonic-gate 	longval = longval / 60;
11287c478bd9Sstevel@tonic-gate 	longdeg = longval;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	altfrac = altval % 100;
11317c478bd9Sstevel@tonic-gate 	altmeters = (altval / 100);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	sizestr = strdup(precsize_ntoa(sizeval));
11347c478bd9Sstevel@tonic-gate 	hpstr = strdup(precsize_ntoa(hpval));
11357c478bd9Sstevel@tonic-gate 	vpstr = strdup(precsize_ntoa(vpval));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	sprintf(ascii,
11387c478bd9Sstevel@tonic-gate 	    "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
11397c478bd9Sstevel@tonic-gate 		latdeg, latmin, latsec, latsecfrac, northsouth,
11407c478bd9Sstevel@tonic-gate 		longdeg, longmin, longsec, longsecfrac, eastwest,
11417c478bd9Sstevel@tonic-gate 		altsign, altmeters, altfrac,
11427c478bd9Sstevel@tonic-gate 		(sizestr != NULL) ? sizestr : error,
11437c478bd9Sstevel@tonic-gate 		(hpstr != NULL) ? hpstr : error,
11447c478bd9Sstevel@tonic-gate 		(vpstr != NULL) ? vpstr : error);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (sizestr != NULL)
11477c478bd9Sstevel@tonic-gate 		free(sizestr);
11487c478bd9Sstevel@tonic-gate 	if (hpstr != NULL)
11497c478bd9Sstevel@tonic-gate 		free(hpstr);
11507c478bd9Sstevel@tonic-gate 	if (vpstr != NULL)
11517c478bd9Sstevel@tonic-gate 		free(vpstr);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	return (ascii);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 
1157*9525b14bSRao Shoaib /*% Return the number of DNS hierarchy levels in the name. */
11587c478bd9Sstevel@tonic-gate int
dn_count_labels(const char * name)11597c478bd9Sstevel@tonic-gate dn_count_labels(const char *name) {
11607c478bd9Sstevel@tonic-gate 	int i, len, count;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	len = strlen(name);
11637c478bd9Sstevel@tonic-gate 	for (i = 0, count = 0; i < len; i++) {
11647c478bd9Sstevel@tonic-gate 		/* XXX need to check for \. or use named's nlabels(). */
11657c478bd9Sstevel@tonic-gate 		if (name[i] == '.')
11667c478bd9Sstevel@tonic-gate 			count++;
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	/* don't count initial wildcard */
11707c478bd9Sstevel@tonic-gate 	if (name[0] == '*')
11717c478bd9Sstevel@tonic-gate 		if (count)
11727c478bd9Sstevel@tonic-gate 			count--;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/* don't count the null label for root. */
11757c478bd9Sstevel@tonic-gate 	/* if terminating '.' not found, must adjust */
11767c478bd9Sstevel@tonic-gate 	/* count to include last label */
11777c478bd9Sstevel@tonic-gate 	if (len > 0 && name[len-1] != '.')
11787c478bd9Sstevel@tonic-gate 		count++;
11797c478bd9Sstevel@tonic-gate 	return (count);
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
1182*9525b14bSRao Shoaib /*%
11837c478bd9Sstevel@tonic-gate  * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
11847c478bd9Sstevel@tonic-gate  * SIG records are required to be printed like this, by the Secure DNS RFC.
11857c478bd9Sstevel@tonic-gate  */
11867c478bd9Sstevel@tonic-gate char *
p_secstodate(u_long secs)11877c478bd9Sstevel@tonic-gate p_secstodate (u_long secs) {
11887c478bd9Sstevel@tonic-gate 	char *output = p_secstodate_output;
11897c478bd9Sstevel@tonic-gate 	time_t clock = secs;
11907c478bd9Sstevel@tonic-gate 	struct tm *time;
11917c478bd9Sstevel@tonic-gate #ifdef HAVE_TIME_R
1192*9525b14bSRao Shoaib 	struct tm res;
1193*9525b14bSRao Shoaib 
1194*9525b14bSRao Shoaib 	time = gmtime_r(&clock, &res);
11957c478bd9Sstevel@tonic-gate #else
11967c478bd9Sstevel@tonic-gate 	time = gmtime(&clock);
11977c478bd9Sstevel@tonic-gate #endif
11987c478bd9Sstevel@tonic-gate 	time->tm_year += 1900;
11997c478bd9Sstevel@tonic-gate 	time->tm_mon += 1;
12007c478bd9Sstevel@tonic-gate 	sprintf(output, "%04d%02d%02d%02d%02d%02d",
12017c478bd9Sstevel@tonic-gate 		time->tm_year, time->tm_mon, time->tm_mday,
12027c478bd9Sstevel@tonic-gate 		time->tm_hour, time->tm_min, time->tm_sec);
12037c478bd9Sstevel@tonic-gate 	return (output);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate u_int16_t
res_nametoclass(const char * buf,int * successp)12077c478bd9Sstevel@tonic-gate res_nametoclass(const char *buf, int *successp) {
12087c478bd9Sstevel@tonic-gate 	unsigned long result;
12097c478bd9Sstevel@tonic-gate 	char *endptr;
12107c478bd9Sstevel@tonic-gate 	int success;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	result = sym_ston(__p_class_syms, buf, &success);
12137c478bd9Sstevel@tonic-gate 	if (success)
12147c478bd9Sstevel@tonic-gate 		goto done;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (strncasecmp(buf, "CLASS", 5) != 0 ||
12177c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[5]))
12187c478bd9Sstevel@tonic-gate 		goto done;
12197c478bd9Sstevel@tonic-gate 	errno = 0;
12207c478bd9Sstevel@tonic-gate 	result = strtoul(buf + 5, &endptr, 10);
1221*9525b14bSRao Shoaib 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12227c478bd9Sstevel@tonic-gate 		success = 1;
12237c478bd9Sstevel@tonic-gate  done:
12247c478bd9Sstevel@tonic-gate 	if (successp)
12257c478bd9Sstevel@tonic-gate 		*successp = success;
12267c478bd9Sstevel@tonic-gate 	return (result);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate u_int16_t
res_nametotype(const char * buf,int * successp)12307c478bd9Sstevel@tonic-gate res_nametotype(const char *buf, int *successp) {
12317c478bd9Sstevel@tonic-gate 	unsigned long result;
12327c478bd9Sstevel@tonic-gate 	char *endptr;
12337c478bd9Sstevel@tonic-gate 	int success;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	result = sym_ston(__p_type_syms, buf, &success);
12367c478bd9Sstevel@tonic-gate 	if (success)
12377c478bd9Sstevel@tonic-gate 		goto done;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	if (strncasecmp(buf, "type", 4) != 0 ||
12407c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[4]))
12417c478bd9Sstevel@tonic-gate 		goto done;
12427c478bd9Sstevel@tonic-gate 	errno = 0;
12437c478bd9Sstevel@tonic-gate 	result = strtoul(buf + 4, &endptr, 10);
1244*9525b14bSRao Shoaib 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12457c478bd9Sstevel@tonic-gate 		success = 1;
12467c478bd9Sstevel@tonic-gate  done:
12477c478bd9Sstevel@tonic-gate 	if (successp)
12487c478bd9Sstevel@tonic-gate 		*successp = success;
12497c478bd9Sstevel@tonic-gate 	return (result);
12507c478bd9Sstevel@tonic-gate }
1251*9525b14bSRao Shoaib 
1252*9525b14bSRao Shoaib /*! \file */
1253