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.
27*9525b14bSRao Shoaib  *
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.
43*9525b14bSRao Shoaib  *
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.
59*9525b14bSRao Shoaib  *
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.
66*9525b14bSRao Shoaib  *
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 #include "port_before.h"
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #include <sys/types.h>
1057c478bd9Sstevel@tonic-gate #include <sys/param.h>
1067c478bd9Sstevel@tonic-gate #include <sys/socket.h>
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #include <netinet/in.h>
1097c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
1107c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #include <ctype.h>
1137c478bd9Sstevel@tonic-gate #include <errno.h>
1147c478bd9Sstevel@tonic-gate #include <math.h>
1157c478bd9Sstevel@tonic-gate #include <netdb.h>
1167c478bd9Sstevel@tonic-gate #include <resolv.h>
117*9525b14bSRao Shoaib #include <resolv_mt.h>
1187c478bd9Sstevel@tonic-gate #include <stdio.h>
1197c478bd9Sstevel@tonic-gate #include <stdlib.h>
1207c478bd9Sstevel@tonic-gate #include <string.h>
1217c478bd9Sstevel@tonic-gate #include <time.h>
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #include "port_after.h"
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
1267c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
1277c478bd9Sstevel@tonic-gate #else
1287c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate extern const char *_res_opcodes[];
1327c478bd9Sstevel@tonic-gate extern const char *_res_sectioncodes[];
1337c478bd9Sstevel@tonic-gate 
134*9525b14bSRao Shoaib #ifndef	ORIGINAL_ISC_CODE
1357c478bd9Sstevel@tonic-gate #pragma weak	__dn_count_labels	=	dn_count_labels
1367c478bd9Sstevel@tonic-gate #pragma weak	__fp_resstat		=	fp_resstat
1377c478bd9Sstevel@tonic-gate #pragma weak	__loc_aton		=	loc_aton
1387c478bd9Sstevel@tonic-gate #pragma weak	__loc_ntoa		=	loc_ntoa
1397c478bd9Sstevel@tonic-gate #pragma weak	__p_cdname		=	p_cdname
1407c478bd9Sstevel@tonic-gate #pragma weak	__p_class		=	p_class
1417c478bd9Sstevel@tonic-gate #pragma weak	__p_section		=	p_section
1427c478bd9Sstevel@tonic-gate #pragma weak	__p_time		=	p_time
1437c478bd9Sstevel@tonic-gate #pragma weak	__p_type		=	p_type
1447c478bd9Sstevel@tonic-gate #pragma weak	__sym_ntop		=	sym_ntop
1457c478bd9Sstevel@tonic-gate #pragma weak	__sym_ntos		=	sym_ntos
1467c478bd9Sstevel@tonic-gate #pragma weak	__sym_ston		=	sym_ston
1477c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
1487c478bd9Sstevel@tonic-gate 
149*9525b14bSRao Shoaib /*%
1507c478bd9Sstevel@tonic-gate  * Print the current options.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate void
fp_resstat(const res_state statp,FILE * file)1537c478bd9Sstevel@tonic-gate fp_resstat(const res_state statp, FILE *file) {
1547c478bd9Sstevel@tonic-gate 	u_long mask;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	fprintf(file, ";; res options:");
157*9525b14bSRao Shoaib 	for (mask = 1;  mask != 0U;  mask <<= 1)
1587c478bd9Sstevel@tonic-gate 		if (statp->options & mask)
1597c478bd9Sstevel@tonic-gate 			fprintf(file, " %s", p_option(mask));
1607c478bd9Sstevel@tonic-gate 	putc('\n', file);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static void
do_section(const res_state statp,ns_msg * handle,ns_sect section,int pflag,FILE * file)1647c478bd9Sstevel@tonic-gate do_section(const res_state statp,
1657c478bd9Sstevel@tonic-gate 	   ns_msg *handle, ns_sect section,
1667c478bd9Sstevel@tonic-gate 	   int pflag, FILE *file)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int n, sflag, rrnum;
1697c478bd9Sstevel@tonic-gate 	static int buflen = 2048;
1707c478bd9Sstevel@tonic-gate 	char *buf;
1717c478bd9Sstevel@tonic-gate 	ns_opcode opcode;
1727c478bd9Sstevel@tonic-gate 	ns_rr rr;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Print answer records.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	sflag = (statp->pfcode & pflag);
1787c478bd9Sstevel@tonic-gate 	if (statp->pfcode && !sflag)
1797c478bd9Sstevel@tonic-gate 		return;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	buf = malloc(buflen);
1827c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
1837c478bd9Sstevel@tonic-gate 		fprintf(file, ";; memory allocation failure\n");
1847c478bd9Sstevel@tonic-gate 		return;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
1887c478bd9Sstevel@tonic-gate 	rrnum = 0;
1897c478bd9Sstevel@tonic-gate 	for (;;) {
1907c478bd9Sstevel@tonic-gate 		if (ns_parserr(handle, section, rrnum, &rr)) {
1917c478bd9Sstevel@tonic-gate 			if (errno != ENODEV)
1927c478bd9Sstevel@tonic-gate 				fprintf(file, ";; ns_parserr: %s\n",
1937c478bd9Sstevel@tonic-gate 					strerror(errno));
1947c478bd9Sstevel@tonic-gate 			else if (rrnum > 0 && sflag != 0 &&
1957c478bd9Sstevel@tonic-gate 				 (statp->pfcode & RES_PRF_HEAD1))
1967c478bd9Sstevel@tonic-gate 				putc('\n', file);
1977c478bd9Sstevel@tonic-gate 			goto cleanup;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
2007c478bd9Sstevel@tonic-gate 			fprintf(file, ";; %s SECTION:\n",
2017c478bd9Sstevel@tonic-gate 				p_section(section, opcode));
2027c478bd9Sstevel@tonic-gate 		if (section == ns_s_qd)
2037c478bd9Sstevel@tonic-gate 			fprintf(file, ";;\t%s, type = %s, class = %s\n",
2047c478bd9Sstevel@tonic-gate 				ns_rr_name(rr),
2057c478bd9Sstevel@tonic-gate 				p_type(ns_rr_type(rr)),
2067c478bd9Sstevel@tonic-gate 				p_class(ns_rr_class(rr)));
2077c478bd9Sstevel@tonic-gate 		else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
208*9525b14bSRao Shoaib 			u_int16_t optcode, optlen, rdatalen = ns_rr_rdlen(rr);
2097c478bd9Sstevel@tonic-gate 			u_int32_t ttl = ns_rr_ttl(rr);
210*9525b14bSRao Shoaib 
2117c478bd9Sstevel@tonic-gate 			fprintf(file,
2127c478bd9Sstevel@tonic-gate 				"; EDNS: version: %u, udp=%u, flags=%04x\n",
2137c478bd9Sstevel@tonic-gate 				(ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
214*9525b14bSRao Shoaib 
215*9525b14bSRao Shoaib 			while (rdatalen >= 4) {
216*9525b14bSRao Shoaib 				const u_char *cp = ns_rr_rdata(rr);
217*9525b14bSRao Shoaib 				int i;
218*9525b14bSRao Shoaib 
219*9525b14bSRao Shoaib 				GETSHORT(optcode, cp);
220*9525b14bSRao Shoaib 				GETSHORT(optlen, cp);
221*9525b14bSRao Shoaib 
222*9525b14bSRao Shoaib 				if (optcode == NS_OPT_NSID) {
223*9525b14bSRao Shoaib 					fputs("; NSID: ", file);
224*9525b14bSRao Shoaib 					if (optlen == 0) {
225*9525b14bSRao Shoaib 						fputs("; NSID\n", file);
226*9525b14bSRao Shoaib 					} else {
227*9525b14bSRao Shoaib 						fputs("; NSID: ", file);
228*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
229*9525b14bSRao Shoaib 							fprintf(file, "%02x ",
230*9525b14bSRao Shoaib 								cp[i]);
231*9525b14bSRao Shoaib 						fputs(" (",file);
232*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
233*9525b14bSRao Shoaib 							fprintf(file, "%c",
234*9525b14bSRao Shoaib 								isprint(cp[i])?
235*9525b14bSRao Shoaib 								cp[i] : '.');
236*9525b14bSRao Shoaib 						fputs(")\n", file);
237*9525b14bSRao Shoaib 					}
238*9525b14bSRao Shoaib 				} else {
239*9525b14bSRao Shoaib 					if (optlen == 0) {
240*9525b14bSRao Shoaib 						fprintf(file, "; OPT=%u\n",
241*9525b14bSRao Shoaib 							optcode);
242*9525b14bSRao Shoaib 					} else {
243*9525b14bSRao Shoaib 						fprintf(file, "; OPT=%u: ",
244*9525b14bSRao Shoaib 							optcode);
245*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
246*9525b14bSRao Shoaib 							fprintf(file, "%02x ",
247*9525b14bSRao Shoaib 								cp[i]);
248*9525b14bSRao Shoaib 						fputs(" (",file);
249*9525b14bSRao Shoaib 						for (i = 0; i < optlen; i++)
250*9525b14bSRao Shoaib 							fprintf(file, "%c",
251*9525b14bSRao Shoaib 								isprint(cp[i]) ?
252*9525b14bSRao Shoaib 									cp[i] : '.');
253*9525b14bSRao Shoaib 						fputs(")\n", file);
254*9525b14bSRao Shoaib 					}
255*9525b14bSRao Shoaib 				}
256*9525b14bSRao Shoaib 				rdatalen -= 4 + optlen;
257*9525b14bSRao Shoaib 			}
2587c478bd9Sstevel@tonic-gate 		} else {
2597c478bd9Sstevel@tonic-gate 			n = ns_sprintrr(handle, &rr, NULL, NULL,
2607c478bd9Sstevel@tonic-gate 					buf, buflen);
2617c478bd9Sstevel@tonic-gate 			if (n < 0) {
2627c478bd9Sstevel@tonic-gate 				if (errno == ENOSPC) {
2637c478bd9Sstevel@tonic-gate 					free(buf);
2647c478bd9Sstevel@tonic-gate 					buf = NULL;
2657c478bd9Sstevel@tonic-gate 					if (buflen < 131072)
2667c478bd9Sstevel@tonic-gate 						buf = malloc(buflen += 1024);
2677c478bd9Sstevel@tonic-gate 					if (buf == NULL) {
2687c478bd9Sstevel@tonic-gate 						fprintf(file,
269*9525b14bSRao Shoaib 					      ";; memory allocation failure\n");
2707c478bd9Sstevel@tonic-gate 					      return;
2717c478bd9Sstevel@tonic-gate 					}
2727c478bd9Sstevel@tonic-gate 					continue;
2737c478bd9Sstevel@tonic-gate 				}
2747c478bd9Sstevel@tonic-gate 				fprintf(file, ";; ns_sprintrr: %s\n",
2757c478bd9Sstevel@tonic-gate 					strerror(errno));
2767c478bd9Sstevel@tonic-gate 				goto cleanup;
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 			fputs(buf, file);
2797c478bd9Sstevel@tonic-gate 			fputc('\n', file);
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 		rrnum++;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate  cleanup:
2847c478bd9Sstevel@tonic-gate 	if (buf != NULL)
2857c478bd9Sstevel@tonic-gate 		free(buf);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
288*9525b14bSRao Shoaib /*%
2897c478bd9Sstevel@tonic-gate  * Print the contents of a query.
2907c478bd9Sstevel@tonic-gate  * This is intended to be primarily a debugging routine.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate void
res_pquery(const res_state statp,const u_char * msg,int len,FILE * file)2937c478bd9Sstevel@tonic-gate res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
2947c478bd9Sstevel@tonic-gate 	ns_msg handle;
2957c478bd9Sstevel@tonic-gate 	int qdcount, ancount, nscount, arcount;
2967c478bd9Sstevel@tonic-gate 	u_int opcode, rcode, id;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (ns_initparse(msg, len, &handle) < 0) {
2997c478bd9Sstevel@tonic-gate 		fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
3007c478bd9Sstevel@tonic-gate 		return;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	opcode = ns_msg_getflag(handle, ns_f_opcode);
3037c478bd9Sstevel@tonic-gate 	rcode = ns_msg_getflag(handle, ns_f_rcode);
3047c478bd9Sstevel@tonic-gate 	id = ns_msg_id(handle);
3057c478bd9Sstevel@tonic-gate 	qdcount = ns_msg_count(handle, ns_s_qd);
3067c478bd9Sstevel@tonic-gate 	ancount = ns_msg_count(handle, ns_s_an);
3077c478bd9Sstevel@tonic-gate 	nscount = ns_msg_count(handle, ns_s_ns);
3087c478bd9Sstevel@tonic-gate 	arcount = ns_msg_count(handle, ns_s_ar);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Print header fields.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
3147c478bd9Sstevel@tonic-gate 		fprintf(file,
3157c478bd9Sstevel@tonic-gate 			";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
3167c478bd9Sstevel@tonic-gate 			_res_opcodes[opcode], p_rcode(rcode), id);
3177c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
3187c478bd9Sstevel@tonic-gate 		putc(';', file);
3197c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
3207c478bd9Sstevel@tonic-gate 		fprintf(file, "; flags:");
3217c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_qr))
3227c478bd9Sstevel@tonic-gate 			fprintf(file, " qr");
3237c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_aa))
3247c478bd9Sstevel@tonic-gate 			fprintf(file, " aa");
3257c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_tc))
3267c478bd9Sstevel@tonic-gate 			fprintf(file, " tc");
3277c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_rd))
3287c478bd9Sstevel@tonic-gate 			fprintf(file, " rd");
3297c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ra))
3307c478bd9Sstevel@tonic-gate 			fprintf(file, " ra");
3317c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_z))
3327c478bd9Sstevel@tonic-gate 			fprintf(file, " ??");
3337c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ad))
3347c478bd9Sstevel@tonic-gate 			fprintf(file, " ad");
3357c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_cd))
3367c478bd9Sstevel@tonic-gate 			fprintf(file, " cd");
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
3397c478bd9Sstevel@tonic-gate 		fprintf(file, "; %s: %d",
3407c478bd9Sstevel@tonic-gate 			p_section(ns_s_qd, opcode), qdcount);
3417c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3427c478bd9Sstevel@tonic-gate 			p_section(ns_s_an, opcode), ancount);
3437c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3447c478bd9Sstevel@tonic-gate 			p_section(ns_s_ns, opcode), nscount);
3457c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
3467c478bd9Sstevel@tonic-gate 			p_section(ns_s_ar, opcode), arcount);
3477c478bd9Sstevel@tonic-gate 	}
348*9525b14bSRao Shoaib 	if ((!statp->pfcode) || (statp->pfcode &
3497c478bd9Sstevel@tonic-gate 		(RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
3507c478bd9Sstevel@tonic-gate 		putc('\n',file);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Print the various sections.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
3567c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
3577c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
3587c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
3597c478bd9Sstevel@tonic-gate 	if (qdcount == 0 && ancount == 0 &&
3607c478bd9Sstevel@tonic-gate 	    nscount == 0 && arcount == 0)
3617c478bd9Sstevel@tonic-gate 		putc('\n', file);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate const u_char *
p_cdnname(const u_char * cp,const u_char * msg,int len,FILE * file)3657c478bd9Sstevel@tonic-gate p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
3667c478bd9Sstevel@tonic-gate 	char name[MAXDNAME];
3677c478bd9Sstevel@tonic-gate 	int n;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
3707c478bd9Sstevel@tonic-gate 		return (NULL);
3717c478bd9Sstevel@tonic-gate 	if (name[0] == '\0')
3727c478bd9Sstevel@tonic-gate 		putc('.', file);
3737c478bd9Sstevel@tonic-gate 	else
3747c478bd9Sstevel@tonic-gate 		fputs(name, file);
3757c478bd9Sstevel@tonic-gate 	return (cp + n);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate const u_char *
p_cdname(const u_char * cp,const u_char * msg,FILE * file)3797c478bd9Sstevel@tonic-gate p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
3807c478bd9Sstevel@tonic-gate 	return (p_cdnname(cp, msg, PACKETSZ, file));
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
383*9525b14bSRao Shoaib /*%
384*9525b14bSRao Shoaib  * Return a fully-qualified domain name from a compressed name (with
3857c478bd9Sstevel@tonic-gate    length supplied).  */
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate const u_char *
p_fqnname(cp,msg,msglen,name,namelen)3887c478bd9Sstevel@tonic-gate p_fqnname(cp, msg, msglen, name, namelen)
3897c478bd9Sstevel@tonic-gate 	const u_char *cp, *msg;
3907c478bd9Sstevel@tonic-gate 	int msglen;
3917c478bd9Sstevel@tonic-gate 	char *name;
3927c478bd9Sstevel@tonic-gate 	int namelen;
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	int n, newlen;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
3977c478bd9Sstevel@tonic-gate 		return (NULL);
3987c478bd9Sstevel@tonic-gate 	newlen = strlen(name);
3997c478bd9Sstevel@tonic-gate 	if (newlen == 0 || name[newlen - 1] != '.') {
400*9525b14bSRao Shoaib 		if (newlen + 1 >= namelen)	/*%< Lack space for final dot */
4017c478bd9Sstevel@tonic-gate 			return (NULL);
4027c478bd9Sstevel@tonic-gate 		else
4037c478bd9Sstevel@tonic-gate 			strcpy(name + newlen, ".");
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 	return (cp + n);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /* XXX:	the rest of these functions need to become length-limited, too. */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate const u_char *
p_fqname(const u_char * cp,const u_char * msg,FILE * file)4117c478bd9Sstevel@tonic-gate p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
4127c478bd9Sstevel@tonic-gate 	char name[MAXDNAME];
4137c478bd9Sstevel@tonic-gate 	const u_char *n;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
4167c478bd9Sstevel@tonic-gate 	if (n == NULL)
4177c478bd9Sstevel@tonic-gate 		return (NULL);
4187c478bd9Sstevel@tonic-gate 	fputs(name, file);
4197c478bd9Sstevel@tonic-gate 	return (n);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
422*9525b14bSRao Shoaib /*%
4237c478bd9Sstevel@tonic-gate  * Names of RR classes and qclasses.  Classes and qclasses are the same, except
4247c478bd9Sstevel@tonic-gate  * that C_ANY is a qclass but not a class.  (You can ask for records of class
4257c478bd9Sstevel@tonic-gate  * C_ANY, but you can't have any records of that class in the database.)
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate const struct res_sym __p_class_syms[] = {
4287c478bd9Sstevel@tonic-gate 	{C_IN,		"IN",		(char *)0},
429*9525b14bSRao Shoaib 	{C_CHAOS,	"CH",		(char *)0},
4307c478bd9Sstevel@tonic-gate 	{C_CHAOS,	"CHAOS",	(char *)0},
4317c478bd9Sstevel@tonic-gate 	{C_HS,		"HS",		(char *)0},
4327c478bd9Sstevel@tonic-gate 	{C_HS,		"HESIOD",	(char *)0},
4337c478bd9Sstevel@tonic-gate 	{C_ANY,		"ANY",		(char *)0},
4347c478bd9Sstevel@tonic-gate 	{C_NONE,	"NONE",		(char *)0},
4357c478bd9Sstevel@tonic-gate 	{C_IN, 		(char *)0,	(char *)0}
4367c478bd9Sstevel@tonic-gate };
4377c478bd9Sstevel@tonic-gate 
438*9525b14bSRao Shoaib /*%
4397c478bd9Sstevel@tonic-gate  * Names of message sections.
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate const struct res_sym __p_default_section_syms[] = {
4427c478bd9Sstevel@tonic-gate 	{ns_s_qd,	"QUERY",	(char *)0},
4437c478bd9Sstevel@tonic-gate 	{ns_s_an,	"ANSWER",	(char *)0},
4447c478bd9Sstevel@tonic-gate 	{ns_s_ns,	"AUTHORITY",	(char *)0},
4457c478bd9Sstevel@tonic-gate 	{ns_s_ar,	"ADDITIONAL",	(char *)0},
446*9525b14bSRao Shoaib 	{0,		(char *)0,	(char *)0}
4477c478bd9Sstevel@tonic-gate };
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate const struct res_sym __p_update_section_syms[] = {
4507c478bd9Sstevel@tonic-gate 	{S_ZONE,	"ZONE",		(char *)0},
4517c478bd9Sstevel@tonic-gate 	{S_PREREQ,	"PREREQUISITE",	(char *)0},
4527c478bd9Sstevel@tonic-gate 	{S_UPDATE,	"UPDATE",	(char *)0},
4537c478bd9Sstevel@tonic-gate 	{S_ADDT,	"ADDITIONAL",	(char *)0},
454*9525b14bSRao Shoaib 	{0,		(char *)0,	(char *)0}
4557c478bd9Sstevel@tonic-gate };
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate const struct res_sym __p_key_syms[] = {
4587c478bd9Sstevel@tonic-gate 	{NS_ALG_MD5RSA,		"RSA",		"RSA KEY with MD5 hash"},
4597c478bd9Sstevel@tonic-gate 	{NS_ALG_DH,		"DH",		"Diffie Hellman"},
4607c478bd9Sstevel@tonic-gate 	{NS_ALG_DSA,		"DSA",		"Digital Signature Algorithm"},
4617c478bd9Sstevel@tonic-gate 	{NS_ALG_EXPIRE_ONLY,	"EXPIREONLY",	"No algorithm"},
4627c478bd9Sstevel@tonic-gate 	{NS_ALG_PRIVATE_OID,	"PRIVATE",	"Algorithm obtained from OID"},
4637c478bd9Sstevel@tonic-gate 	{0,			NULL,		NULL}
4647c478bd9Sstevel@tonic-gate };
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate const struct res_sym __p_cert_syms[] = {
4677c478bd9Sstevel@tonic-gate 	{cert_t_pkix,	"PKIX",		"PKIX (X.509v3) Certificate"},
4687c478bd9Sstevel@tonic-gate 	{cert_t_spki,	"SPKI",		"SPKI certificate"},
4697c478bd9Sstevel@tonic-gate 	{cert_t_pgp,	"PGP",		"PGP certificate"},
4707c478bd9Sstevel@tonic-gate 	{cert_t_url,	"URL",		"URL Private"},
4717c478bd9Sstevel@tonic-gate 	{cert_t_oid,	"OID",		"OID Private"},
4727c478bd9Sstevel@tonic-gate 	{0,		NULL,		NULL}
4737c478bd9Sstevel@tonic-gate };
4747c478bd9Sstevel@tonic-gate 
475*9525b14bSRao Shoaib /*%
4767c478bd9Sstevel@tonic-gate  * Names of RR types and qtypes.  Types and qtypes are the same, except
4777c478bd9Sstevel@tonic-gate  * that T_ANY is a qtype but not a type.  (You can ask for records of type
4787c478bd9Sstevel@tonic-gate  * T_ANY, but you can't have any records of that type in the database.)
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate const struct res_sym __p_type_syms[] = {
4817c478bd9Sstevel@tonic-gate 	{ns_t_a,	"A",		"address"},
4827c478bd9Sstevel@tonic-gate 	{ns_t_ns,	"NS",		"name server"},
4837c478bd9Sstevel@tonic-gate 	{ns_t_md,	"MD",		"mail destination (deprecated)"},
4847c478bd9Sstevel@tonic-gate 	{ns_t_mf,	"MF",		"mail forwarder (deprecated)"},
4857c478bd9Sstevel@tonic-gate 	{ns_t_cname,	"CNAME",	"canonical name"},
4867c478bd9Sstevel@tonic-gate 	{ns_t_soa,	"SOA",		"start of authority"},
4877c478bd9Sstevel@tonic-gate 	{ns_t_mb,	"MB",		"mailbox"},
4887c478bd9Sstevel@tonic-gate 	{ns_t_mg,	"MG",		"mail group member"},
4897c478bd9Sstevel@tonic-gate 	{ns_t_mr,	"MR",		"mail rename"},
4907c478bd9Sstevel@tonic-gate 	{ns_t_null,	"NULL",		"null"},
4917c478bd9Sstevel@tonic-gate 	{ns_t_wks,	"WKS",		"well-known service (deprecated)"},
4927c478bd9Sstevel@tonic-gate 	{ns_t_ptr,	"PTR",		"domain name pointer"},
4937c478bd9Sstevel@tonic-gate 	{ns_t_hinfo,	"HINFO",	"host information"},
4947c478bd9Sstevel@tonic-gate 	{ns_t_minfo,	"MINFO",	"mailbox information"},
4957c478bd9Sstevel@tonic-gate 	{ns_t_mx,	"MX",		"mail exchanger"},
4967c478bd9Sstevel@tonic-gate 	{ns_t_txt,	"TXT",		"text"},
4977c478bd9Sstevel@tonic-gate 	{ns_t_rp,	"RP",		"responsible person"},
4987c478bd9Sstevel@tonic-gate 	{ns_t_afsdb,	"AFSDB",	"DCE or AFS server"},
4997c478bd9Sstevel@tonic-gate 	{ns_t_x25,	"X25",		"X25 address"},
5007c478bd9Sstevel@tonic-gate 	{ns_t_isdn,	"ISDN",		"ISDN address"},
5017c478bd9Sstevel@tonic-gate 	{ns_t_rt,	"RT",		"router"},
5027c478bd9Sstevel@tonic-gate 	{ns_t_nsap,	"NSAP",		"nsap address"},
5037c478bd9Sstevel@tonic-gate 	{ns_t_nsap_ptr,	"NSAP_PTR",	"domain name pointer"},
5047c478bd9Sstevel@tonic-gate 	{ns_t_sig,	"SIG",		"signature"},
5057c478bd9Sstevel@tonic-gate 	{ns_t_key,	"KEY",		"key"},
5067c478bd9Sstevel@tonic-gate 	{ns_t_px,	"PX",		"mapping information"},
5077c478bd9Sstevel@tonic-gate 	{ns_t_gpos,	"GPOS",		"geographical position (withdrawn)"},
5087c478bd9Sstevel@tonic-gate 	{ns_t_aaaa,	"AAAA",		"IPv6 address"},
5097c478bd9Sstevel@tonic-gate 	{ns_t_loc,	"LOC",		"location"},
5107c478bd9Sstevel@tonic-gate 	{ns_t_nxt,	"NXT",		"next valid name (unimplemented)"},
5117c478bd9Sstevel@tonic-gate 	{ns_t_eid,	"EID",		"endpoint identifier (unimplemented)"},
5127c478bd9Sstevel@tonic-gate 	{ns_t_nimloc,	"NIMLOC",	"NIMROD locator (unimplemented)"},
5137c478bd9Sstevel@tonic-gate 	{ns_t_srv,	"SRV",		"server selection"},
5147c478bd9Sstevel@tonic-gate 	{ns_t_atma,	"ATMA",		"ATM address (unimplemented)"},
515*9525b14bSRao Shoaib 	{ns_t_naptr,	"NAPTR",	"naptr"},
516*9525b14bSRao Shoaib 	{ns_t_kx,	"KX",		"key exchange"},
517*9525b14bSRao Shoaib 	{ns_t_cert,	"CERT",		"certificate"},
518*9525b14bSRao Shoaib 	{ns_t_a6,	"A",		"IPv6 address (experminental)"},
519*9525b14bSRao Shoaib 	{ns_t_dname,	"DNAME",	"non-terminal redirection"},
520*9525b14bSRao Shoaib 	{ns_t_opt,	"OPT",		"opt"},
521*9525b14bSRao Shoaib 	{ns_t_apl,	"apl",		"apl"},
522*9525b14bSRao Shoaib 	{ns_t_ds,	"DS",		"delegation signer"},
523*9525b14bSRao Shoaib 	{ns_t_sshfp,	"SSFP",		"SSH fingerprint"},
524*9525b14bSRao Shoaib 	{ns_t_ipseckey,	"IPSECKEY",	"IPSEC key"},
525*9525b14bSRao Shoaib 	{ns_t_rrsig,	"RRSIG",	"rrsig"},
526*9525b14bSRao Shoaib 	{ns_t_nsec,	"NSEC",		"nsec"},
527*9525b14bSRao Shoaib 	{ns_t_dnskey,	"DNSKEY",	"DNS key"},
528*9525b14bSRao Shoaib 	{ns_t_dhcid,	"DHCID",       "dynamic host configuration identifier"},
529*9525b14bSRao Shoaib 	{ns_t_nsec3,	"NSEC3",	"nsec3"},
530*9525b14bSRao Shoaib 	{ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
531*9525b14bSRao Shoaib 	{ns_t_hip,	"HIP",		"host identity protocol"},
532*9525b14bSRao Shoaib 	{ns_t_spf,	"SPF",		"sender policy framework"},
5337c478bd9Sstevel@tonic-gate 	{ns_t_tkey,	"TKEY",		"tkey"},
5347c478bd9Sstevel@tonic-gate 	{ns_t_tsig,	"TSIG",		"transaction signature"},
5357c478bd9Sstevel@tonic-gate 	{ns_t_ixfr,	"IXFR",		"incremental zone transfer"},
5367c478bd9Sstevel@tonic-gate 	{ns_t_axfr,	"AXFR",		"zone transfer"},
5377c478bd9Sstevel@tonic-gate 	{ns_t_zxfr,	"ZXFR",		"compressed zone transfer"},
5387c478bd9Sstevel@tonic-gate 	{ns_t_mailb,	"MAILB",	"mailbox-related data (deprecated)"},
5397c478bd9Sstevel@tonic-gate 	{ns_t_maila,	"MAILA",	"mail agent (deprecated)"},
5407c478bd9Sstevel@tonic-gate 	{ns_t_naptr,	"NAPTR",	"URN Naming Authority"},
5417c478bd9Sstevel@tonic-gate 	{ns_t_kx,	"KX",		"Key Exchange"},
5427c478bd9Sstevel@tonic-gate 	{ns_t_cert,	"CERT",		"Certificate"},
5437c478bd9Sstevel@tonic-gate 	{ns_t_a6,	"A6",		"IPv6 Address"},
5447c478bd9Sstevel@tonic-gate 	{ns_t_dname,	"DNAME",	"dname"},
5457c478bd9Sstevel@tonic-gate 	{ns_t_sink,	"SINK",		"Kitchen Sink (experimental)"},
5467c478bd9Sstevel@tonic-gate 	{ns_t_opt,	"OPT",		"EDNS Options"},
5477c478bd9Sstevel@tonic-gate 	{ns_t_any,	"ANY",		"\"any\""},
548*9525b14bSRao Shoaib 	{ns_t_dlv,	"DLV",		"DNSSEC look-aside validation"},
5497c478bd9Sstevel@tonic-gate 	{0, 		NULL,		NULL}
5507c478bd9Sstevel@tonic-gate };
5517c478bd9Sstevel@tonic-gate 
552*9525b14bSRao Shoaib /*%
5537c478bd9Sstevel@tonic-gate  * Names of DNS rcodes.
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate const struct res_sym __p_rcode_syms[] = {
5567c478bd9Sstevel@tonic-gate 	{ns_r_noerror,	"NOERROR",		"no error"},
5577c478bd9Sstevel@tonic-gate 	{ns_r_formerr,	"FORMERR",		"format error"},
5587c478bd9Sstevel@tonic-gate 	{ns_r_servfail,	"SERVFAIL",		"server failed"},
5597c478bd9Sstevel@tonic-gate 	{ns_r_nxdomain,	"NXDOMAIN",		"no such domain name"},
5607c478bd9Sstevel@tonic-gate 	{ns_r_notimpl,	"NOTIMP",		"not implemented"},
5617c478bd9Sstevel@tonic-gate 	{ns_r_refused,	"REFUSED",		"refused"},
5627c478bd9Sstevel@tonic-gate 	{ns_r_yxdomain,	"YXDOMAIN",		"domain name exists"},
5637c478bd9Sstevel@tonic-gate 	{ns_r_yxrrset,	"YXRRSET",		"rrset exists"},
5647c478bd9Sstevel@tonic-gate 	{ns_r_nxrrset,	"NXRRSET",		"rrset doesn't exist"},
5657c478bd9Sstevel@tonic-gate 	{ns_r_notauth,	"NOTAUTH",		"not authoritative"},
5667c478bd9Sstevel@tonic-gate 	{ns_r_notzone,	"NOTZONE",		"Not in zone"},
5677c478bd9Sstevel@tonic-gate 	{ns_r_max,	"",			""},
5687c478bd9Sstevel@tonic-gate 	{ns_r_badsig,	"BADSIG",		"bad signature"},
5697c478bd9Sstevel@tonic-gate 	{ns_r_badkey,	"BADKEY",		"bad key"},
5707c478bd9Sstevel@tonic-gate 	{ns_r_badtime,	"BADTIME",		"bad time"},
5717c478bd9Sstevel@tonic-gate 	{0, 		NULL,			NULL}
5727c478bd9Sstevel@tonic-gate };
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate int
sym_ston(const struct res_sym * syms,const char * name,int * success)5757c478bd9Sstevel@tonic-gate sym_ston(const struct res_sym *syms, const char *name, int *success) {
5767c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
5777c478bd9Sstevel@tonic-gate 		if (strcasecmp (name, syms->name) == 0) {
5787c478bd9Sstevel@tonic-gate 			if (success)
5797c478bd9Sstevel@tonic-gate 				*success = 1;
5807c478bd9Sstevel@tonic-gate 			return (syms->number);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	if (success)
5847c478bd9Sstevel@tonic-gate 		*success = 0;
585*9525b14bSRao Shoaib 	return (syms->number);		/*%< The default value. */
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate const char *
sym_ntos(const struct res_sym * syms,int number,int * success)5897c478bd9Sstevel@tonic-gate sym_ntos(const struct res_sym *syms, int number, int *success) {
590*9525b14bSRao Shoaib 	char *unname = sym_ntos_unname;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
5937c478bd9Sstevel@tonic-gate 		if (number == syms->number) {
5947c478bd9Sstevel@tonic-gate 			if (success)
5957c478bd9Sstevel@tonic-gate 				*success = 1;
5967c478bd9Sstevel@tonic-gate 			return (syms->name);
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
600*9525b14bSRao Shoaib 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6017c478bd9Sstevel@tonic-gate 	if (success)
6027c478bd9Sstevel@tonic-gate 		*success = 0;
6037c478bd9Sstevel@tonic-gate 	return (unname);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate const char *
sym_ntop(const struct res_sym * syms,int number,int * success)6077c478bd9Sstevel@tonic-gate sym_ntop(const struct res_sym *syms, int number, int *success) {
608*9525b14bSRao Shoaib 	char *unname = sym_ntop_unname;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
6117c478bd9Sstevel@tonic-gate 		if (number == syms->number) {
6127c478bd9Sstevel@tonic-gate 			if (success)
6137c478bd9Sstevel@tonic-gate 				*success = 1;
6147c478bd9Sstevel@tonic-gate 			return (syms->humanname);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	}
617*9525b14bSRao Shoaib 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6187c478bd9Sstevel@tonic-gate 	if (success)
6197c478bd9Sstevel@tonic-gate 		*success = 0;
6207c478bd9Sstevel@tonic-gate 	return (unname);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
623*9525b14bSRao Shoaib /*%
6247c478bd9Sstevel@tonic-gate  * Return a string for the type.
6257c478bd9Sstevel@tonic-gate  */
6267c478bd9Sstevel@tonic-gate const char *
p_type(int type)6277c478bd9Sstevel@tonic-gate p_type(int type) {
6287c478bd9Sstevel@tonic-gate 	int success;
6297c478bd9Sstevel@tonic-gate 	const char *result;
6307c478bd9Sstevel@tonic-gate 	static char typebuf[20];
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	result = sym_ntos(__p_type_syms, type, &success);
6337c478bd9Sstevel@tonic-gate 	if (success)
6347c478bd9Sstevel@tonic-gate 		return (result);
635*9525b14bSRao Shoaib 	if (type < 0 || type > 0xffff)
6367c478bd9Sstevel@tonic-gate 		return ("BADTYPE");
6377c478bd9Sstevel@tonic-gate 	sprintf(typebuf, "TYPE%d", type);
6387c478bd9Sstevel@tonic-gate 	return (typebuf);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
641*9525b14bSRao Shoaib /*%
6427c478bd9Sstevel@tonic-gate  * Return a string for the type.
6437c478bd9Sstevel@tonic-gate  */
6447c478bd9Sstevel@tonic-gate const char *
p_section(int section,int opcode)6457c478bd9Sstevel@tonic-gate p_section(int section, int opcode) {
6467c478bd9Sstevel@tonic-gate 	const struct res_sym *symbols;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	switch (opcode) {
6497c478bd9Sstevel@tonic-gate 	case ns_o_update:
6507c478bd9Sstevel@tonic-gate 		symbols = __p_update_section_syms;
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 	default:
6537c478bd9Sstevel@tonic-gate 		symbols = __p_default_section_syms;
6547c478bd9Sstevel@tonic-gate 		break;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	return (sym_ntos(symbols, section, (int *)0));
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
659*9525b14bSRao Shoaib /*%
6607c478bd9Sstevel@tonic-gate  * Return a mnemonic for class.
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate const char *
p_class(int class)6637c478bd9Sstevel@tonic-gate p_class(int class) {
6647c478bd9Sstevel@tonic-gate 	int success;
6657c478bd9Sstevel@tonic-gate 	const char *result;
6667c478bd9Sstevel@tonic-gate 	static char classbuf[20];
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	result = sym_ntos(__p_class_syms, class, &success);
6697c478bd9Sstevel@tonic-gate 	if (success)
6707c478bd9Sstevel@tonic-gate 		return (result);
671*9525b14bSRao Shoaib 	if (class < 0 || class > 0xffff)
6727c478bd9Sstevel@tonic-gate 		return ("BADCLASS");
6737c478bd9Sstevel@tonic-gate 	sprintf(classbuf, "CLASS%d", class);
6747c478bd9Sstevel@tonic-gate 	return (classbuf);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
677*9525b14bSRao Shoaib /*%
6787c478bd9Sstevel@tonic-gate  * Return a mnemonic for an option
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate const char *
p_option(u_long option)6817c478bd9Sstevel@tonic-gate p_option(u_long option) {
682*9525b14bSRao Shoaib 	char *nbuf = p_option_nbuf;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	switch (option) {
6857c478bd9Sstevel@tonic-gate 	case RES_INIT:		return "init";
6867c478bd9Sstevel@tonic-gate 	case RES_DEBUG:		return "debug";
6877c478bd9Sstevel@tonic-gate 	case RES_AAONLY:	return "aaonly(unimpl)";
6887c478bd9Sstevel@tonic-gate 	case RES_USEVC:		return "usevc";
6897c478bd9Sstevel@tonic-gate 	case RES_PRIMARY:	return "primry(unimpl)";
6907c478bd9Sstevel@tonic-gate 	case RES_IGNTC:		return "igntc";
6917c478bd9Sstevel@tonic-gate 	case RES_RECURSE:	return "recurs";
6927c478bd9Sstevel@tonic-gate 	case RES_DEFNAMES:	return "defnam";
6937c478bd9Sstevel@tonic-gate 	case RES_STAYOPEN:	return "styopn";
6947c478bd9Sstevel@tonic-gate 	case RES_DNSRCH:	return "dnsrch";
6957c478bd9Sstevel@tonic-gate 	case RES_INSECURE1:	return "insecure1";
6967c478bd9Sstevel@tonic-gate 	case RES_INSECURE2:	return "insecure2";
6977c478bd9Sstevel@tonic-gate 	case RES_NOALIASES:	return "noaliases";
6987c478bd9Sstevel@tonic-gate 	case RES_USE_INET6:	return "inet6";
699*9525b14bSRao Shoaib #ifdef RES_USE_EDNS0	/*%< KAME extension */
7007c478bd9Sstevel@tonic-gate 	case RES_USE_EDNS0:	return "edns0";
701*9525b14bSRao Shoaib 	case RES_NSID:		return "nsid";
7027c478bd9Sstevel@tonic-gate #endif
7037c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNAME
7047c478bd9Sstevel@tonic-gate 	case RES_USE_DNAME:	return "dname";
7057c478bd9Sstevel@tonic-gate #endif
7067c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNSSEC
7077c478bd9Sstevel@tonic-gate 	case RES_USE_DNSSEC:	return "dnssec";
7087c478bd9Sstevel@tonic-gate #endif
7097c478bd9Sstevel@tonic-gate #ifdef RES_NOTLDQUERY
7107c478bd9Sstevel@tonic-gate 	case RES_NOTLDQUERY:	return "no-tld-query";
7117c478bd9Sstevel@tonic-gate #endif
712*9525b14bSRao Shoaib #ifdef RES_NO_NIBBLE2
713*9525b14bSRao Shoaib 	case RES_NO_NIBBLE2:	return "no-nibble2";
714*9525b14bSRao Shoaib #endif
7157c478bd9Sstevel@tonic-gate 				/* XXX nonreentrant */
7167c478bd9Sstevel@tonic-gate 	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
7177c478bd9Sstevel@tonic-gate 				return (nbuf);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
721*9525b14bSRao Shoaib /*%
7227c478bd9Sstevel@tonic-gate  * Return a mnemonic for a time to live.
7237c478bd9Sstevel@tonic-gate  */
7247c478bd9Sstevel@tonic-gate const char *
p_time(u_int32_t value)7257c478bd9Sstevel@tonic-gate p_time(u_int32_t value) {
726*9525b14bSRao Shoaib 	char *nbuf = p_time_nbuf;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
7297c478bd9Sstevel@tonic-gate 		sprintf(nbuf, "%u", value);
7307c478bd9Sstevel@tonic-gate 	return (nbuf);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
733*9525b14bSRao Shoaib /*%
7347c478bd9Sstevel@tonic-gate  * Return a string for the rcode.
7357c478bd9Sstevel@tonic-gate  */
7367c478bd9Sstevel@tonic-gate const char *
p_rcode(int rcode)7377c478bd9Sstevel@tonic-gate p_rcode(int rcode) {
7387c478bd9Sstevel@tonic-gate 	return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
741*9525b14bSRao Shoaib /*%
7427c478bd9Sstevel@tonic-gate  * Return a string for a res_sockaddr_union.
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate const char *
p_sockun(union res_sockaddr_union u,char * buf,size_t size)7457c478bd9Sstevel@tonic-gate p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
7467c478bd9Sstevel@tonic-gate 	char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	switch (u.sin.sin_family) {
7497c478bd9Sstevel@tonic-gate 	case AF_INET:
7507c478bd9Sstevel@tonic-gate 		inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
7517c478bd9Sstevel@tonic-gate 		break;
7527c478bd9Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
7537c478bd9Sstevel@tonic-gate 	case AF_INET6:
7547c478bd9Sstevel@tonic-gate 		inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
7557c478bd9Sstevel@tonic-gate 		break;
7567c478bd9Sstevel@tonic-gate #endif
7577c478bd9Sstevel@tonic-gate 	default:
7587c478bd9Sstevel@tonic-gate 		sprintf(ret, "[af%d]", u.sin.sin_family);
7597c478bd9Sstevel@tonic-gate 		break;
7607c478bd9Sstevel@tonic-gate 	}
761*9525b14bSRao Shoaib 	if (size > 0U) {
7627c478bd9Sstevel@tonic-gate 		strncpy(buf, ret, size - 1);
7637c478bd9Sstevel@tonic-gate 		buf[size - 1] = '0';
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 	return (buf);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
768*9525b14bSRao Shoaib /*%
7697c478bd9Sstevel@tonic-gate  * routines to convert between on-the-wire RR format and zone file format.
7707c478bd9Sstevel@tonic-gate  * Does not contain conversion to/from decimal degrees; divide or multiply
7717c478bd9Sstevel@tonic-gate  * by 60*60*1000 for that.
7727c478bd9Sstevel@tonic-gate  */
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
7757c478bd9Sstevel@tonic-gate 				      1000000,10000000,100000000,1000000000};
7767c478bd9Sstevel@tonic-gate 
777*9525b14bSRao Shoaib /*% takes an XeY precision/size value, returns a string representation. */
7787c478bd9Sstevel@tonic-gate static const char *
precsize_ntoa(prec)7797c478bd9Sstevel@tonic-gate precsize_ntoa(prec)
7807c478bd9Sstevel@tonic-gate 	u_int8_t prec;
7817c478bd9Sstevel@tonic-gate {
782*9525b14bSRao Shoaib 	char *retbuf = precsize_ntoa_retbuf;
7837c478bd9Sstevel@tonic-gate 	unsigned long val;
7847c478bd9Sstevel@tonic-gate 	int mantissa, exponent;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	mantissa = (int)((prec >> 4) & 0x0f) % 10;
7877c478bd9Sstevel@tonic-gate 	exponent = (int)((prec >> 0) & 0x0f) % 10;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	val = mantissa * poweroften[exponent];
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	(void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
7927c478bd9Sstevel@tonic-gate 	return (retbuf);
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
795*9525b14bSRao Shoaib /*% converts ascii size/precision X * 10**Y(cm) to 0xXY.  moves pointer.  */
7967c478bd9Sstevel@tonic-gate static u_int8_t
precsize_aton(const char ** strptr)7977c478bd9Sstevel@tonic-gate precsize_aton(const char **strptr) {
7987c478bd9Sstevel@tonic-gate 	unsigned int mval = 0, cmval = 0;
7997c478bd9Sstevel@tonic-gate 	u_int8_t retval = 0;
8007c478bd9Sstevel@tonic-gate 	const char *cp;
8017c478bd9Sstevel@tonic-gate 	int exponent;
8027c478bd9Sstevel@tonic-gate 	int mantissa;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	cp = *strptr;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8077c478bd9Sstevel@tonic-gate 		mval = mval * 10 + (*cp++ - '0');
8087c478bd9Sstevel@tonic-gate 
809*9525b14bSRao Shoaib 	if (*cp == '.') {		/*%< centimeters */
8107c478bd9Sstevel@tonic-gate 		cp++;
8117c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
8127c478bd9Sstevel@tonic-gate 			cmval = (*cp++ - '0') * 10;
8137c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
8147c478bd9Sstevel@tonic-gate 				cmval += (*cp++ - '0');
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	cmval = (mval * 100) + cmval;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	for (exponent = 0; exponent < 9; exponent++)
8217c478bd9Sstevel@tonic-gate 		if (cmval < poweroften[exponent+1])
8227c478bd9Sstevel@tonic-gate 			break;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	mantissa = cmval / poweroften[exponent];
8257c478bd9Sstevel@tonic-gate 	if (mantissa > 9)
8267c478bd9Sstevel@tonic-gate 		mantissa = 9;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	retval = (mantissa << 4) | exponent;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	*strptr = cp;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	return (retval);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
835*9525b14bSRao Shoaib /*% converts ascii lat/lon to unsigned encoded 32-bit number.  moves pointer. */
8367c478bd9Sstevel@tonic-gate static u_int32_t
latlon2ul(const char ** latlonstrptr,int * which)8377c478bd9Sstevel@tonic-gate latlon2ul(const char **latlonstrptr, int *which) {
8387c478bd9Sstevel@tonic-gate 	const char *cp;
8397c478bd9Sstevel@tonic-gate 	u_int32_t retval;
8407c478bd9Sstevel@tonic-gate 	int deg = 0, min = 0, secs = 0, secsfrac = 0;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	cp = *latlonstrptr;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8457c478bd9Sstevel@tonic-gate 		deg = deg * 10 + (*cp++ - '0');
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8487c478bd9Sstevel@tonic-gate 		cp++;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
8517c478bd9Sstevel@tonic-gate 		goto fndhemi;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8547c478bd9Sstevel@tonic-gate 		min = min * 10 + (*cp++ - '0');
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8577c478bd9Sstevel@tonic-gate 		cp++;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
8607c478bd9Sstevel@tonic-gate 		goto fndhemi;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
8637c478bd9Sstevel@tonic-gate 		secs = secs * 10 + (*cp++ - '0');
8647c478bd9Sstevel@tonic-gate 
865*9525b14bSRao Shoaib 	if (*cp == '.') {		/*%< decimal seconds */
8667c478bd9Sstevel@tonic-gate 		cp++;
8677c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
8687c478bd9Sstevel@tonic-gate 			secsfrac = (*cp++ - '0') * 100;
8697c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
8707c478bd9Sstevel@tonic-gate 				secsfrac += (*cp++ - '0') * 10;
8717c478bd9Sstevel@tonic-gate 				if (isdigit((unsigned char)*cp)) {
8727c478bd9Sstevel@tonic-gate 					secsfrac += (*cp++ - '0');
8737c478bd9Sstevel@tonic-gate 				}
8747c478bd9Sstevel@tonic-gate 			}
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
878*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
8797c478bd9Sstevel@tonic-gate 		cp++;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
8827c478bd9Sstevel@tonic-gate 		cp++;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate  fndhemi:
8857c478bd9Sstevel@tonic-gate 	switch (*cp) {
8867c478bd9Sstevel@tonic-gate 	case 'N': case 'n':
8877c478bd9Sstevel@tonic-gate 	case 'E': case 'e':
8887c478bd9Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
8897c478bd9Sstevel@tonic-gate 			+ (((((deg * 60) + min) * 60) + secs) * 1000)
8907c478bd9Sstevel@tonic-gate 			+ secsfrac;
8917c478bd9Sstevel@tonic-gate 		break;
8927c478bd9Sstevel@tonic-gate 	case 'S': case 's':
8937c478bd9Sstevel@tonic-gate 	case 'W': case 'w':
8947c478bd9Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
8957c478bd9Sstevel@tonic-gate 			- (((((deg * 60) + min) * 60) + secs) * 1000)
8967c478bd9Sstevel@tonic-gate 			- secsfrac;
8977c478bd9Sstevel@tonic-gate 		break;
8987c478bd9Sstevel@tonic-gate 	default:
899*9525b14bSRao Shoaib 		retval = 0;	/*%< invalid value -- indicates error */
9007c478bd9Sstevel@tonic-gate 		break;
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	switch (*cp) {
9047c478bd9Sstevel@tonic-gate 	case 'N': case 'n':
9057c478bd9Sstevel@tonic-gate 	case 'S': case 's':
906*9525b14bSRao Shoaib 		*which = 1;	/*%< latitude */
9077c478bd9Sstevel@tonic-gate 		break;
9087c478bd9Sstevel@tonic-gate 	case 'E': case 'e':
9097c478bd9Sstevel@tonic-gate 	case 'W': case 'w':
910*9525b14bSRao Shoaib 		*which = 2;	/*%< longitude */
9117c478bd9Sstevel@tonic-gate 		break;
9127c478bd9Sstevel@tonic-gate 	default:
913*9525b14bSRao Shoaib 		*which = 0;	/*%< error */
9147c478bd9Sstevel@tonic-gate 		break;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
917*9525b14bSRao Shoaib 	cp++;			/*%< skip the hemisphere */
918*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
9197c478bd9Sstevel@tonic-gate 		cp++;
9207c478bd9Sstevel@tonic-gate 
921*9525b14bSRao Shoaib 	while (isspace((unsigned char)*cp))	/*%< move to next field */
9227c478bd9Sstevel@tonic-gate 		cp++;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	*latlonstrptr = cp;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	return (retval);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
929*9525b14bSRao Shoaib /*%
930*9525b14bSRao Shoaib  * converts a zone file representation in a string to an RDATA on-the-wire
9317c478bd9Sstevel@tonic-gate  * representation. */
9327c478bd9Sstevel@tonic-gate int
loc_aton(ascii,binary)9337c478bd9Sstevel@tonic-gate loc_aton(ascii, binary)
9347c478bd9Sstevel@tonic-gate 	const char *ascii;
9357c478bd9Sstevel@tonic-gate 	u_char *binary;
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	const char *cp, *maxcp;
9387c478bd9Sstevel@tonic-gate 	u_char *bcp;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	u_int32_t latit = 0, longit = 0, alt = 0;
9417c478bd9Sstevel@tonic-gate 	u_int32_t lltemp1 = 0, lltemp2 = 0;
9427c478bd9Sstevel@tonic-gate 	int altmeters = 0, altfrac = 0, altsign = 1;
943*9525b14bSRao Shoaib 	u_int8_t hp = 0x16;	/*%< default = 1e6 cm = 10000.00m = 10km */
944*9525b14bSRao Shoaib 	u_int8_t vp = 0x13;	/*%< default = 1e3 cm = 10.00m */
945*9525b14bSRao Shoaib 	u_int8_t siz = 0x12;	/*%< default = 1e2 cm = 1.00m */
9467c478bd9Sstevel@tonic-gate 	int which1 = 0, which2 = 0;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	cp = ascii;
9497c478bd9Sstevel@tonic-gate 	maxcp = cp + strlen(ascii);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	lltemp1 = latlon2ul(&cp, &which1);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	lltemp2 = latlon2ul(&cp, &which2);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	switch (which1 + which2) {
956*9525b14bSRao Shoaib 	case 3:			/*%< 1 + 2, the only valid combination */
957*9525b14bSRao Shoaib 		if ((which1 == 1) && (which2 == 2)) { /*%< normal case */
9587c478bd9Sstevel@tonic-gate 			latit = lltemp1;
9597c478bd9Sstevel@tonic-gate 			longit = lltemp2;
960*9525b14bSRao Shoaib 		} else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */
9617c478bd9Sstevel@tonic-gate 			longit = lltemp1;
9627c478bd9Sstevel@tonic-gate 			latit = lltemp2;
963*9525b14bSRao Shoaib 		} else {	/*%< some kind of brokenness */
9647c478bd9Sstevel@tonic-gate 			return (0);
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		break;
967*9525b14bSRao Shoaib 	default:		/*%< we didn't get one of each */
9687c478bd9Sstevel@tonic-gate 		return (0);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/* altitude */
9727c478bd9Sstevel@tonic-gate 	if (*cp == '-') {
9737c478bd9Sstevel@tonic-gate 		altsign = -1;
9747c478bd9Sstevel@tonic-gate 		cp++;
9757c478bd9Sstevel@tonic-gate 	}
976*9525b14bSRao Shoaib 
9777c478bd9Sstevel@tonic-gate 	if (*cp == '+')
9787c478bd9Sstevel@tonic-gate 		cp++;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
9817c478bd9Sstevel@tonic-gate 		altmeters = altmeters * 10 + (*cp++ - '0');
9827c478bd9Sstevel@tonic-gate 
983*9525b14bSRao Shoaib 	if (*cp == '.') {		/*%< decimal meters */
9847c478bd9Sstevel@tonic-gate 		cp++;
9857c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
9867c478bd9Sstevel@tonic-gate 			altfrac = (*cp++ - '0') * 10;
9877c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
9887c478bd9Sstevel@tonic-gate 				altfrac += (*cp++ - '0');
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
9947c478bd9Sstevel@tonic-gate 
995*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
9967c478bd9Sstevel@tonic-gate 		cp++;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
9997c478bd9Sstevel@tonic-gate 		cp++;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
10027c478bd9Sstevel@tonic-gate 		goto defaults;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	siz = precsize_aton(&cp);
1005*9525b14bSRao Shoaib 
1006*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10077c478bd9Sstevel@tonic-gate 		cp++;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10107c478bd9Sstevel@tonic-gate 		cp++;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
10137c478bd9Sstevel@tonic-gate 		goto defaults;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	hp = precsize_aton(&cp);
10167c478bd9Sstevel@tonic-gate 
1017*9525b14bSRao Shoaib 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10187c478bd9Sstevel@tonic-gate 		cp++;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10217c478bd9Sstevel@tonic-gate 		cp++;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
10247c478bd9Sstevel@tonic-gate 		goto defaults;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	vp = precsize_aton(&cp);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate  defaults:
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	bcp = binary;
1031*9525b14bSRao Shoaib 	*bcp++ = (u_int8_t) 0;	/*%< version byte */
10327c478bd9Sstevel@tonic-gate 	*bcp++ = siz;
10337c478bd9Sstevel@tonic-gate 	*bcp++ = hp;
10347c478bd9Sstevel@tonic-gate 	*bcp++ = vp;
10357c478bd9Sstevel@tonic-gate 	PUTLONG(latit,bcp);
10367c478bd9Sstevel@tonic-gate 	PUTLONG(longit,bcp);
10377c478bd9Sstevel@tonic-gate 	PUTLONG(alt,bcp);
1038*9525b14bSRao Shoaib 
1039*9525b14bSRao Shoaib 	return (16);		/*%< size of RR in octets */
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
1042*9525b14bSRao Shoaib /*% takes an on-the-wire LOC RR and formats it in a human readable format. */
10437c478bd9Sstevel@tonic-gate const char *
loc_ntoa(binary,ascii)10447c478bd9Sstevel@tonic-gate loc_ntoa(binary, ascii)
10457c478bd9Sstevel@tonic-gate 	const u_char *binary;
10467c478bd9Sstevel@tonic-gate 	char *ascii;
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	static const char *error = "?";
10497c478bd9Sstevel@tonic-gate 	static char tmpbuf[sizeof
10507c478bd9Sstevel@tonic-gate "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
10517c478bd9Sstevel@tonic-gate 	const u_char *cp = binary;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	int latdeg, latmin, latsec, latsecfrac;
10547c478bd9Sstevel@tonic-gate 	int longdeg, longmin, longsec, longsecfrac;
10557c478bd9Sstevel@tonic-gate 	char northsouth, eastwest;
10567c478bd9Sstevel@tonic-gate 	const char *altsign;
10577c478bd9Sstevel@tonic-gate 	int altmeters, altfrac;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	const u_int32_t referencealt = 100000 * 100;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	int32_t latval, longval, altval;
10627c478bd9Sstevel@tonic-gate 	u_int32_t templ;
10637c478bd9Sstevel@tonic-gate 	u_int8_t sizeval, hpval, vpval, versionval;
1064*9525b14bSRao Shoaib 
10657c478bd9Sstevel@tonic-gate 	char *sizestr, *hpstr, *vpstr;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	versionval = *cp++;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (ascii == NULL)
10707c478bd9Sstevel@tonic-gate 		ascii = tmpbuf;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if (versionval) {
10737c478bd9Sstevel@tonic-gate 		(void) sprintf(ascii, "; error: unknown LOC RR version");
10747c478bd9Sstevel@tonic-gate 		return (ascii);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	sizeval = *cp++;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	hpval = *cp++;
10807c478bd9Sstevel@tonic-gate 	vpval = *cp++;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
10837c478bd9Sstevel@tonic-gate 	latval = (templ - ((unsigned)1<<31));
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
10867c478bd9Sstevel@tonic-gate 	longval = (templ - ((unsigned)1<<31));
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
1089*9525b14bSRao Shoaib 	if (templ < referencealt) { /*%< below WGS 84 spheroid */
10907c478bd9Sstevel@tonic-gate 		altval = referencealt - templ;
10917c478bd9Sstevel@tonic-gate 		altsign = "-";
10927c478bd9Sstevel@tonic-gate 	} else {
10937c478bd9Sstevel@tonic-gate 		altval = templ - referencealt;
10947c478bd9Sstevel@tonic-gate 		altsign = "";
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (latval < 0) {
10987c478bd9Sstevel@tonic-gate 		northsouth = 'S';
10997c478bd9Sstevel@tonic-gate 		latval = -latval;
11007c478bd9Sstevel@tonic-gate 	} else
11017c478bd9Sstevel@tonic-gate 		northsouth = 'N';
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	latsecfrac = latval % 1000;
11047c478bd9Sstevel@tonic-gate 	latval = latval / 1000;
11057c478bd9Sstevel@tonic-gate 	latsec = latval % 60;
11067c478bd9Sstevel@tonic-gate 	latval = latval / 60;
11077c478bd9Sstevel@tonic-gate 	latmin = latval % 60;
11087c478bd9Sstevel@tonic-gate 	latval = latval / 60;
11097c478bd9Sstevel@tonic-gate 	latdeg = latval;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	if (longval < 0) {
11127c478bd9Sstevel@tonic-gate 		eastwest = 'W';
11137c478bd9Sstevel@tonic-gate 		longval = -longval;
11147c478bd9Sstevel@tonic-gate 	} else
11157c478bd9Sstevel@tonic-gate 		eastwest = 'E';
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	longsecfrac = longval % 1000;
11187c478bd9Sstevel@tonic-gate 	longval = longval / 1000;
11197c478bd9Sstevel@tonic-gate 	longsec = longval % 60;
11207c478bd9Sstevel@tonic-gate 	longval = longval / 60;
11217c478bd9Sstevel@tonic-gate 	longmin = longval % 60;
11227c478bd9Sstevel@tonic-gate 	longval = longval / 60;
11237c478bd9Sstevel@tonic-gate 	longdeg = longval;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	altfrac = altval % 100;
11267c478bd9Sstevel@tonic-gate 	altmeters = (altval / 100);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	sizestr = strdup(precsize_ntoa(sizeval));
11297c478bd9Sstevel@tonic-gate 	hpstr = strdup(precsize_ntoa(hpval));
11307c478bd9Sstevel@tonic-gate 	vpstr = strdup(precsize_ntoa(vpval));
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	sprintf(ascii,
11337c478bd9Sstevel@tonic-gate 	    "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
11347c478bd9Sstevel@tonic-gate 		latdeg, latmin, latsec, latsecfrac, northsouth,
11357c478bd9Sstevel@tonic-gate 		longdeg, longmin, longsec, longsecfrac, eastwest,
11367c478bd9Sstevel@tonic-gate 		altsign, altmeters, altfrac,
11377c478bd9Sstevel@tonic-gate 		(sizestr != NULL) ? sizestr : error,
11387c478bd9Sstevel@tonic-gate 		(hpstr != NULL) ? hpstr : error,
11397c478bd9Sstevel@tonic-gate 		(vpstr != NULL) ? vpstr : error);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	if (sizestr != NULL)
11427c478bd9Sstevel@tonic-gate 		free(sizestr);
11437c478bd9Sstevel@tonic-gate 	if (hpstr != NULL)
11447c478bd9Sstevel@tonic-gate 		free(hpstr);
11457c478bd9Sstevel@tonic-gate 	if (vpstr != NULL)
11467c478bd9Sstevel@tonic-gate 		free(vpstr);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	return (ascii);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 
1152*9525b14bSRao Shoaib /*% Return the number of DNS hierarchy levels in the name. */
11537c478bd9Sstevel@tonic-gate int
dn_count_labels(const char * name)11547c478bd9Sstevel@tonic-gate dn_count_labels(const char *name) {
11557c478bd9Sstevel@tonic-gate 	int i, len, count;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	len = strlen(name);
11587c478bd9Sstevel@tonic-gate 	for (i = 0, count = 0; i < len; i++) {
11597c478bd9Sstevel@tonic-gate 		/* XXX need to check for \. or use named's nlabels(). */
11607c478bd9Sstevel@tonic-gate 		if (name[i] == '.')
11617c478bd9Sstevel@tonic-gate 			count++;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	/* don't count initial wildcard */
11657c478bd9Sstevel@tonic-gate 	if (name[0] == '*')
11667c478bd9Sstevel@tonic-gate 		if (count)
11677c478bd9Sstevel@tonic-gate 			count--;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	/* don't count the null label for root. */
11707c478bd9Sstevel@tonic-gate 	/* if terminating '.' not found, must adjust */
11717c478bd9Sstevel@tonic-gate 	/* count to include last label */
11727c478bd9Sstevel@tonic-gate 	if (len > 0 && name[len-1] != '.')
11737c478bd9Sstevel@tonic-gate 		count++;
11747c478bd9Sstevel@tonic-gate 	return (count);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
1177*9525b14bSRao Shoaib /*%
1178*9525b14bSRao Shoaib  * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
11797c478bd9Sstevel@tonic-gate  * SIG records are required to be printed like this, by the Secure DNS RFC.
11807c478bd9Sstevel@tonic-gate  */
11817c478bd9Sstevel@tonic-gate char *
p_secstodate(u_long secs)11827c478bd9Sstevel@tonic-gate p_secstodate (u_long secs) {
1183*9525b14bSRao Shoaib 	char *output = p_secstodate_output;
11847c478bd9Sstevel@tonic-gate 	time_t clock = secs;
11857c478bd9Sstevel@tonic-gate 	struct tm *time;
11867c478bd9Sstevel@tonic-gate #ifdef HAVE_TIME_R
1187*9525b14bSRao Shoaib 	struct tm res;
1188*9525b14bSRao Shoaib 
1189*9525b14bSRao Shoaib 	time = gmtime_r(&clock, &res);
11907c478bd9Sstevel@tonic-gate #else
11917c478bd9Sstevel@tonic-gate 	time = gmtime(&clock);
11927c478bd9Sstevel@tonic-gate #endif
11937c478bd9Sstevel@tonic-gate 	time->tm_year += 1900;
11947c478bd9Sstevel@tonic-gate 	time->tm_mon += 1;
11957c478bd9Sstevel@tonic-gate 	sprintf(output, "%04d%02d%02d%02d%02d%02d",
11967c478bd9Sstevel@tonic-gate 		time->tm_year, time->tm_mon, time->tm_mday,
11977c478bd9Sstevel@tonic-gate 		time->tm_hour, time->tm_min, time->tm_sec);
11987c478bd9Sstevel@tonic-gate 	return (output);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate u_int16_t
res_nametoclass(const char * buf,int * successp)12027c478bd9Sstevel@tonic-gate res_nametoclass(const char *buf, int *successp) {
12037c478bd9Sstevel@tonic-gate 	unsigned long result;
12047c478bd9Sstevel@tonic-gate 	char *endptr;
12057c478bd9Sstevel@tonic-gate 	int success;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	result = sym_ston(__p_class_syms, buf, &success);
12087c478bd9Sstevel@tonic-gate 	if (success)
12097c478bd9Sstevel@tonic-gate 		goto done;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	if (strncasecmp(buf, "CLASS", 5) != 0 ||
12127c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[5]))
12137c478bd9Sstevel@tonic-gate 		goto done;
12147c478bd9Sstevel@tonic-gate 	errno = 0;
12157c478bd9Sstevel@tonic-gate 	result = strtoul(buf + 5, &endptr, 10);
1216*9525b14bSRao Shoaib 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12177c478bd9Sstevel@tonic-gate 		success = 1;
12187c478bd9Sstevel@tonic-gate  done:
12197c478bd9Sstevel@tonic-gate 	if (successp)
12207c478bd9Sstevel@tonic-gate 		*successp = success;
12217c478bd9Sstevel@tonic-gate 	return (result);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate u_int16_t
res_nametotype(const char * buf,int * successp)12257c478bd9Sstevel@tonic-gate res_nametotype(const char *buf, int *successp) {
12267c478bd9Sstevel@tonic-gate 	unsigned long result;
12277c478bd9Sstevel@tonic-gate 	char *endptr;
12287c478bd9Sstevel@tonic-gate 	int success;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	result = sym_ston(__p_type_syms, buf, &success);
12317c478bd9Sstevel@tonic-gate 	if (success)
12327c478bd9Sstevel@tonic-gate 		goto done;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	if (strncasecmp(buf, "type", 4) != 0 ||
12357c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[4]))
12367c478bd9Sstevel@tonic-gate 		goto done;
12377c478bd9Sstevel@tonic-gate 	errno = 0;
12387c478bd9Sstevel@tonic-gate 	result = strtoul(buf + 4, &endptr, 10);
1239*9525b14bSRao Shoaib 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12407c478bd9Sstevel@tonic-gate 		success = 1;
12417c478bd9Sstevel@tonic-gate  done:
12427c478bd9Sstevel@tonic-gate 	if (successp)
12437c478bd9Sstevel@tonic-gate 		*successp = success;
12447c478bd9Sstevel@tonic-gate 	return (result);
12457c478bd9Sstevel@tonic-gate }
1246*9525b14bSRao Shoaib 
1247*9525b14bSRao Shoaib /*! \file */
1248