1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1985
8*7c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
11*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
12*7c478bd9Sstevel@tonic-gate  * are met:
13*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
14*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
15*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
17*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
18*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
19*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
20*7c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
21*7c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
22*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
23*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
24*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
43*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
44*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
45*7c478bd9Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
46*7c478bd9Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
47*7c478bd9Sstevel@tonic-gate  * specific, written prior permission.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
50*7c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
51*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
52*7c478bd9Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
53*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
54*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
55*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1995 by International Business Machines, Inc.
61*7c478bd9Sstevel@tonic-gate  *
62*7c478bd9Sstevel@tonic-gate  * International Business Machines, Inc. (hereinafter called IBM) grants
63*7c478bd9Sstevel@tonic-gate  * permission under its copyrights to use, copy, modify, and distribute this
64*7c478bd9Sstevel@tonic-gate  * Software with or without fee, provided that the above copyright notice and
65*7c478bd9Sstevel@tonic-gate  * all paragraphs of this notice appear in all copies, and that the name of IBM
66*7c478bd9Sstevel@tonic-gate  * not be used in connection with the marketing of any product incorporating
67*7c478bd9Sstevel@tonic-gate  * the Software or modifications thereof, without specific, written prior
68*7c478bd9Sstevel@tonic-gate  * permission.
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * To the extent it has a right to do so, IBM grants an immunity from suit
71*7c478bd9Sstevel@tonic-gate  * under its patents, if any, for the use, sale or manufacture of products to
72*7c478bd9Sstevel@tonic-gate  * the extent that such products are used for performing Domain Name System
73*7c478bd9Sstevel@tonic-gate  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
74*7c478bd9Sstevel@tonic-gate  * granted for any product per se or for any other function of any product.
75*7c478bd9Sstevel@tonic-gate  *
76*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
77*7c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
78*7c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
79*7c478bd9Sstevel@tonic-gate  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
80*7c478bd9Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
81*7c478bd9Sstevel@tonic-gate  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
88*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
89*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
90*7c478bd9Sstevel@tonic-gate  *
91*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
92*7c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
93*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
94*7c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
95*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
96*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
97*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
98*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
104*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_debug.c	8.1 (Berkeley) 6/4/93";
105*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: res_debug.c,v 8.50 2003/04/16 04:55:40 marka Exp $";
106*7c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate #include "port_before.h"
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
111*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
112*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
115*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
116*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate #include <ctype.h>
119*7c478bd9Sstevel@tonic-gate #include <errno.h>
120*7c478bd9Sstevel@tonic-gate #include <math.h>
121*7c478bd9Sstevel@tonic-gate #include <netdb.h>
122*7c478bd9Sstevel@tonic-gate #include <resolv.h>
123*7c478bd9Sstevel@tonic-gate #include <stdio.h>
124*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
125*7c478bd9Sstevel@tonic-gate #include <string.h>
126*7c478bd9Sstevel@tonic-gate #include <time.h>
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate #include "port_after.h"
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
131*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
132*7c478bd9Sstevel@tonic-gate #else
133*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
134*7c478bd9Sstevel@tonic-gate #endif
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate extern const char *_res_opcodes[];
137*7c478bd9Sstevel@tonic-gate extern const char *_res_sectioncodes[];
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
140*7c478bd9Sstevel@tonic-gate #else
141*7c478bd9Sstevel@tonic-gate #pragma weak	__dn_count_labels	=	dn_count_labels
142*7c478bd9Sstevel@tonic-gate #pragma weak	__fp_resstat		=	fp_resstat
143*7c478bd9Sstevel@tonic-gate #pragma weak	__loc_aton		=	loc_aton
144*7c478bd9Sstevel@tonic-gate #pragma weak	__loc_ntoa		=	loc_ntoa
145*7c478bd9Sstevel@tonic-gate #pragma weak	__p_cdname		=	p_cdname
146*7c478bd9Sstevel@tonic-gate #pragma weak	__p_class		=	p_class
147*7c478bd9Sstevel@tonic-gate #pragma weak	__p_secstodate		=	p_secstodate
148*7c478bd9Sstevel@tonic-gate #pragma weak	__p_section		=	p_section
149*7c478bd9Sstevel@tonic-gate #pragma weak	__p_time		=	p_time
150*7c478bd9Sstevel@tonic-gate #pragma weak	__p_type		=	p_type
151*7c478bd9Sstevel@tonic-gate #pragma weak	__sym_ntop		=	sym_ntop
152*7c478bd9Sstevel@tonic-gate #pragma weak	__sym_ntos		=	sym_ntos
153*7c478bd9Sstevel@tonic-gate #pragma weak	__sym_ston		=	sym_ston
154*7c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate /*
157*7c478bd9Sstevel@tonic-gate  * Print the current options.
158*7c478bd9Sstevel@tonic-gate  */
159*7c478bd9Sstevel@tonic-gate void
160*7c478bd9Sstevel@tonic-gate fp_resstat(const res_state statp, FILE *file) {
161*7c478bd9Sstevel@tonic-gate 	u_long mask;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	fprintf(file, ";; res options:");
164*7c478bd9Sstevel@tonic-gate 	for (mask = 1;  mask != 0;  mask <<= 1)
165*7c478bd9Sstevel@tonic-gate 		if (statp->options & mask)
166*7c478bd9Sstevel@tonic-gate 			fprintf(file, " %s", p_option(mask));
167*7c478bd9Sstevel@tonic-gate 	putc('\n', file);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate static void
171*7c478bd9Sstevel@tonic-gate do_section(const res_state statp,
172*7c478bd9Sstevel@tonic-gate 	   ns_msg *handle, ns_sect section,
173*7c478bd9Sstevel@tonic-gate 	   int pflag, FILE *file)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	int n, sflag, rrnum;
176*7c478bd9Sstevel@tonic-gate 	static int buflen = 2048;
177*7c478bd9Sstevel@tonic-gate 	char *buf;
178*7c478bd9Sstevel@tonic-gate 	ns_opcode opcode;
179*7c478bd9Sstevel@tonic-gate 	ns_rr rr;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/*
182*7c478bd9Sstevel@tonic-gate 	 * Print answer records.
183*7c478bd9Sstevel@tonic-gate 	 */
184*7c478bd9Sstevel@tonic-gate 	sflag = (statp->pfcode & pflag);
185*7c478bd9Sstevel@tonic-gate 	if (statp->pfcode && !sflag)
186*7c478bd9Sstevel@tonic-gate 		return;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	buf = malloc(buflen);
189*7c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
190*7c478bd9Sstevel@tonic-gate 		fprintf(file, ";; memory allocation failure\n");
191*7c478bd9Sstevel@tonic-gate 		return;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
195*7c478bd9Sstevel@tonic-gate 	rrnum = 0;
196*7c478bd9Sstevel@tonic-gate 	for (;;) {
197*7c478bd9Sstevel@tonic-gate 		if (ns_parserr(handle, section, rrnum, &rr)) {
198*7c478bd9Sstevel@tonic-gate 			if (errno != ENODEV)
199*7c478bd9Sstevel@tonic-gate 				fprintf(file, ";; ns_parserr: %s\n",
200*7c478bd9Sstevel@tonic-gate 					strerror(errno));
201*7c478bd9Sstevel@tonic-gate 			else if (rrnum > 0 && sflag != 0 &&
202*7c478bd9Sstevel@tonic-gate 				 (statp->pfcode & RES_PRF_HEAD1))
203*7c478bd9Sstevel@tonic-gate 				putc('\n', file);
204*7c478bd9Sstevel@tonic-gate 			goto cleanup;
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 		if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
207*7c478bd9Sstevel@tonic-gate 			fprintf(file, ";; %s SECTION:\n",
208*7c478bd9Sstevel@tonic-gate 				p_section(section, opcode));
209*7c478bd9Sstevel@tonic-gate 		if (section == ns_s_qd)
210*7c478bd9Sstevel@tonic-gate 			fprintf(file, ";;\t%s, type = %s, class = %s\n",
211*7c478bd9Sstevel@tonic-gate 				ns_rr_name(rr),
212*7c478bd9Sstevel@tonic-gate 				p_type(ns_rr_type(rr)),
213*7c478bd9Sstevel@tonic-gate 				p_class(ns_rr_class(rr)));
214*7c478bd9Sstevel@tonic-gate 		else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
215*7c478bd9Sstevel@tonic-gate 			u_int32_t ttl = ns_rr_ttl(rr);
216*7c478bd9Sstevel@tonic-gate 			fprintf(file,
217*7c478bd9Sstevel@tonic-gate 				"; EDNS: version: %u, udp=%u, flags=%04x\n",
218*7c478bd9Sstevel@tonic-gate 				(ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
219*7c478bd9Sstevel@tonic-gate 		} else {
220*7c478bd9Sstevel@tonic-gate 			n = ns_sprintrr(handle, &rr, NULL, NULL,
221*7c478bd9Sstevel@tonic-gate 					buf, buflen);
222*7c478bd9Sstevel@tonic-gate 			if (n < 0) {
223*7c478bd9Sstevel@tonic-gate 				if (errno == ENOSPC) {
224*7c478bd9Sstevel@tonic-gate 					free(buf);
225*7c478bd9Sstevel@tonic-gate 					buf = NULL;
226*7c478bd9Sstevel@tonic-gate 					if (buflen < 131072)
227*7c478bd9Sstevel@tonic-gate 						buf = malloc(buflen += 1024);
228*7c478bd9Sstevel@tonic-gate 					if (buf == NULL) {
229*7c478bd9Sstevel@tonic-gate 						fprintf(file,
230*7c478bd9Sstevel@tonic-gate 				              ";; memory allocation failure\n");
231*7c478bd9Sstevel@tonic-gate 					      return;
232*7c478bd9Sstevel@tonic-gate 					}
233*7c478bd9Sstevel@tonic-gate 					continue;
234*7c478bd9Sstevel@tonic-gate 				}
235*7c478bd9Sstevel@tonic-gate 				fprintf(file, ";; ns_sprintrr: %s\n",
236*7c478bd9Sstevel@tonic-gate 					strerror(errno));
237*7c478bd9Sstevel@tonic-gate 				goto cleanup;
238*7c478bd9Sstevel@tonic-gate 			}
239*7c478bd9Sstevel@tonic-gate 			fputs(buf, file);
240*7c478bd9Sstevel@tonic-gate 			fputc('\n', file);
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate 		rrnum++;
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate  cleanup:
245*7c478bd9Sstevel@tonic-gate 	if (buf != NULL)
246*7c478bd9Sstevel@tonic-gate 		free(buf);
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * Print the contents of a query.
251*7c478bd9Sstevel@tonic-gate  * This is intended to be primarily a debugging routine.
252*7c478bd9Sstevel@tonic-gate  */
253*7c478bd9Sstevel@tonic-gate void
254*7c478bd9Sstevel@tonic-gate res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
255*7c478bd9Sstevel@tonic-gate 	ns_msg handle;
256*7c478bd9Sstevel@tonic-gate 	int qdcount, ancount, nscount, arcount;
257*7c478bd9Sstevel@tonic-gate 	u_int opcode, rcode, id;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (ns_initparse(msg, len, &handle) < 0) {
260*7c478bd9Sstevel@tonic-gate 		fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
261*7c478bd9Sstevel@tonic-gate 		return;
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 	opcode = ns_msg_getflag(handle, ns_f_opcode);
264*7c478bd9Sstevel@tonic-gate 	rcode = ns_msg_getflag(handle, ns_f_rcode);
265*7c478bd9Sstevel@tonic-gate 	id = ns_msg_id(handle);
266*7c478bd9Sstevel@tonic-gate 	qdcount = ns_msg_count(handle, ns_s_qd);
267*7c478bd9Sstevel@tonic-gate 	ancount = ns_msg_count(handle, ns_s_an);
268*7c478bd9Sstevel@tonic-gate 	nscount = ns_msg_count(handle, ns_s_ns);
269*7c478bd9Sstevel@tonic-gate 	arcount = ns_msg_count(handle, ns_s_ar);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	/*
272*7c478bd9Sstevel@tonic-gate 	 * Print header fields.
273*7c478bd9Sstevel@tonic-gate 	 */
274*7c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
275*7c478bd9Sstevel@tonic-gate 		fprintf(file,
276*7c478bd9Sstevel@tonic-gate 			";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
277*7c478bd9Sstevel@tonic-gate 			_res_opcodes[opcode], p_rcode(rcode), id);
278*7c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
279*7c478bd9Sstevel@tonic-gate 		putc(';', file);
280*7c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
281*7c478bd9Sstevel@tonic-gate 		fprintf(file, "; flags:");
282*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_qr))
283*7c478bd9Sstevel@tonic-gate 			fprintf(file, " qr");
284*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_aa))
285*7c478bd9Sstevel@tonic-gate 			fprintf(file, " aa");
286*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_tc))
287*7c478bd9Sstevel@tonic-gate 			fprintf(file, " tc");
288*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_rd))
289*7c478bd9Sstevel@tonic-gate 			fprintf(file, " rd");
290*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ra))
291*7c478bd9Sstevel@tonic-gate 			fprintf(file, " ra");
292*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_z))
293*7c478bd9Sstevel@tonic-gate 			fprintf(file, " ??");
294*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_ad))
295*7c478bd9Sstevel@tonic-gate 			fprintf(file, " ad");
296*7c478bd9Sstevel@tonic-gate 		if (ns_msg_getflag(handle, ns_f_cd))
297*7c478bd9Sstevel@tonic-gate 			fprintf(file, " cd");
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
300*7c478bd9Sstevel@tonic-gate 		fprintf(file, "; %s: %d",
301*7c478bd9Sstevel@tonic-gate 			p_section(ns_s_qd, opcode), qdcount);
302*7c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
303*7c478bd9Sstevel@tonic-gate 			p_section(ns_s_an, opcode), ancount);
304*7c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
305*7c478bd9Sstevel@tonic-gate 			p_section(ns_s_ns, opcode), nscount);
306*7c478bd9Sstevel@tonic-gate 		fprintf(file, ", %s: %d",
307*7c478bd9Sstevel@tonic-gate 			p_section(ns_s_ar, opcode), arcount);
308*7c478bd9Sstevel@tonic-gate 	}
309*7c478bd9Sstevel@tonic-gate 	if ((!statp->pfcode) || (statp->pfcode &
310*7c478bd9Sstevel@tonic-gate 		(RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
311*7c478bd9Sstevel@tonic-gate 		putc('\n',file);
312*7c478bd9Sstevel@tonic-gate 	}
313*7c478bd9Sstevel@tonic-gate 	/*
314*7c478bd9Sstevel@tonic-gate 	 * Print the various sections.
315*7c478bd9Sstevel@tonic-gate 	 */
316*7c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
317*7c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
318*7c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
319*7c478bd9Sstevel@tonic-gate 	do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
320*7c478bd9Sstevel@tonic-gate 	if (qdcount == 0 && ancount == 0 &&
321*7c478bd9Sstevel@tonic-gate 	    nscount == 0 && arcount == 0)
322*7c478bd9Sstevel@tonic-gate 		putc('\n', file);
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate const u_char *
326*7c478bd9Sstevel@tonic-gate p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
327*7c478bd9Sstevel@tonic-gate 	char name[MAXDNAME];
328*7c478bd9Sstevel@tonic-gate 	int n;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
331*7c478bd9Sstevel@tonic-gate 		return (NULL);
332*7c478bd9Sstevel@tonic-gate 	if (name[0] == '\0')
333*7c478bd9Sstevel@tonic-gate 		putc('.', file);
334*7c478bd9Sstevel@tonic-gate 	else
335*7c478bd9Sstevel@tonic-gate 		fputs(name, file);
336*7c478bd9Sstevel@tonic-gate 	return (cp + n);
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate const u_char *
340*7c478bd9Sstevel@tonic-gate p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
341*7c478bd9Sstevel@tonic-gate 	return (p_cdnname(cp, msg, PACKETSZ, file));
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate /* Return a fully-qualified domain name from a compressed name (with
345*7c478bd9Sstevel@tonic-gate    length supplied).  */
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate const u_char *
348*7c478bd9Sstevel@tonic-gate p_fqnname(cp, msg, msglen, name, namelen)
349*7c478bd9Sstevel@tonic-gate 	const u_char *cp, *msg;
350*7c478bd9Sstevel@tonic-gate 	int msglen;
351*7c478bd9Sstevel@tonic-gate 	char *name;
352*7c478bd9Sstevel@tonic-gate 	int namelen;
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	int n, newlen;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
357*7c478bd9Sstevel@tonic-gate 		return (NULL);
358*7c478bd9Sstevel@tonic-gate 	newlen = strlen(name);
359*7c478bd9Sstevel@tonic-gate 	if (newlen == 0 || name[newlen - 1] != '.') {
360*7c478bd9Sstevel@tonic-gate 		if (newlen + 1 >= namelen)	/* Lack space for final dot */
361*7c478bd9Sstevel@tonic-gate 			return (NULL);
362*7c478bd9Sstevel@tonic-gate 		else
363*7c478bd9Sstevel@tonic-gate 			strcpy(name + newlen, ".");
364*7c478bd9Sstevel@tonic-gate 	}
365*7c478bd9Sstevel@tonic-gate 	return (cp + n);
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate /* XXX:	the rest of these functions need to become length-limited, too. */
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate const u_char *
371*7c478bd9Sstevel@tonic-gate p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
372*7c478bd9Sstevel@tonic-gate 	char name[MAXDNAME];
373*7c478bd9Sstevel@tonic-gate 	const u_char *n;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
376*7c478bd9Sstevel@tonic-gate 	if (n == NULL)
377*7c478bd9Sstevel@tonic-gate 		return (NULL);
378*7c478bd9Sstevel@tonic-gate 	fputs(name, file);
379*7c478bd9Sstevel@tonic-gate 	return (n);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate /*
383*7c478bd9Sstevel@tonic-gate  * Names of RR classes and qclasses.  Classes and qclasses are the same, except
384*7c478bd9Sstevel@tonic-gate  * that C_ANY is a qclass but not a class.  (You can ask for records of class
385*7c478bd9Sstevel@tonic-gate  * C_ANY, but you can't have any records of that class in the database.)
386*7c478bd9Sstevel@tonic-gate  */
387*7c478bd9Sstevel@tonic-gate const struct res_sym __p_class_syms[] = {
388*7c478bd9Sstevel@tonic-gate 	{C_IN,		"IN",		(char *)0},
389*7c478bd9Sstevel@tonic-gate 	{C_CHAOS,	"CHAOS",	(char *)0},
390*7c478bd9Sstevel@tonic-gate 	{C_HS,		"HS",		(char *)0},
391*7c478bd9Sstevel@tonic-gate 	{C_HS,		"HESIOD",	(char *)0},
392*7c478bd9Sstevel@tonic-gate 	{C_ANY,		"ANY",		(char *)0},
393*7c478bd9Sstevel@tonic-gate 	{C_NONE,	"NONE",		(char *)0},
394*7c478bd9Sstevel@tonic-gate 	{C_IN, 		(char *)0,	(char *)0}
395*7c478bd9Sstevel@tonic-gate };
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate /*
398*7c478bd9Sstevel@tonic-gate  * Names of message sections.
399*7c478bd9Sstevel@tonic-gate  */
400*7c478bd9Sstevel@tonic-gate const struct res_sym __p_default_section_syms[] = {
401*7c478bd9Sstevel@tonic-gate 	{ns_s_qd,	"QUERY",	(char *)0},
402*7c478bd9Sstevel@tonic-gate 	{ns_s_an,	"ANSWER",	(char *)0},
403*7c478bd9Sstevel@tonic-gate 	{ns_s_ns,	"AUTHORITY",	(char *)0},
404*7c478bd9Sstevel@tonic-gate 	{ns_s_ar,	"ADDITIONAL",	(char *)0},
405*7c478bd9Sstevel@tonic-gate 	{0,             (char *)0,	(char *)0}
406*7c478bd9Sstevel@tonic-gate };
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate const struct res_sym __p_update_section_syms[] = {
409*7c478bd9Sstevel@tonic-gate 	{S_ZONE,	"ZONE",		(char *)0},
410*7c478bd9Sstevel@tonic-gate 	{S_PREREQ,	"PREREQUISITE",	(char *)0},
411*7c478bd9Sstevel@tonic-gate 	{S_UPDATE,	"UPDATE",	(char *)0},
412*7c478bd9Sstevel@tonic-gate 	{S_ADDT,	"ADDITIONAL",	(char *)0},
413*7c478bd9Sstevel@tonic-gate 	{0,             (char *)0,	(char *)0}
414*7c478bd9Sstevel@tonic-gate };
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate const struct res_sym __p_key_syms[] = {
417*7c478bd9Sstevel@tonic-gate 	{NS_ALG_MD5RSA,		"RSA",		"RSA KEY with MD5 hash"},
418*7c478bd9Sstevel@tonic-gate 	{NS_ALG_DH,		"DH",		"Diffie Hellman"},
419*7c478bd9Sstevel@tonic-gate 	{NS_ALG_DSA,		"DSA",		"Digital Signature Algorithm"},
420*7c478bd9Sstevel@tonic-gate 	{NS_ALG_EXPIRE_ONLY,	"EXPIREONLY",	"No algorithm"},
421*7c478bd9Sstevel@tonic-gate 	{NS_ALG_PRIVATE_OID,	"PRIVATE",	"Algorithm obtained from OID"},
422*7c478bd9Sstevel@tonic-gate 	{0,			NULL,		NULL}
423*7c478bd9Sstevel@tonic-gate };
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate const struct res_sym __p_cert_syms[] = {
426*7c478bd9Sstevel@tonic-gate 	{cert_t_pkix,	"PKIX",		"PKIX (X.509v3) Certificate"},
427*7c478bd9Sstevel@tonic-gate 	{cert_t_spki,	"SPKI",		"SPKI certificate"},
428*7c478bd9Sstevel@tonic-gate 	{cert_t_pgp,	"PGP",		"PGP certificate"},
429*7c478bd9Sstevel@tonic-gate 	{cert_t_url,	"URL",		"URL Private"},
430*7c478bd9Sstevel@tonic-gate 	{cert_t_oid,	"OID",		"OID Private"},
431*7c478bd9Sstevel@tonic-gate 	{0,		NULL,		NULL}
432*7c478bd9Sstevel@tonic-gate };
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate /*
435*7c478bd9Sstevel@tonic-gate  * Names of RR types and qtypes.  Types and qtypes are the same, except
436*7c478bd9Sstevel@tonic-gate  * that T_ANY is a qtype but not a type.  (You can ask for records of type
437*7c478bd9Sstevel@tonic-gate  * T_ANY, but you can't have any records of that type in the database.)
438*7c478bd9Sstevel@tonic-gate  */
439*7c478bd9Sstevel@tonic-gate const struct res_sym __p_type_syms[] = {
440*7c478bd9Sstevel@tonic-gate 	{ns_t_a,	"A",		"address"},
441*7c478bd9Sstevel@tonic-gate 	{ns_t_ns,	"NS",		"name server"},
442*7c478bd9Sstevel@tonic-gate 	{ns_t_md,	"MD",		"mail destination (deprecated)"},
443*7c478bd9Sstevel@tonic-gate 	{ns_t_mf,	"MF",		"mail forwarder (deprecated)"},
444*7c478bd9Sstevel@tonic-gate 	{ns_t_cname,	"CNAME",	"canonical name"},
445*7c478bd9Sstevel@tonic-gate 	{ns_t_soa,	"SOA",		"start of authority"},
446*7c478bd9Sstevel@tonic-gate 	{ns_t_mb,	"MB",		"mailbox"},
447*7c478bd9Sstevel@tonic-gate 	{ns_t_mg,	"MG",		"mail group member"},
448*7c478bd9Sstevel@tonic-gate 	{ns_t_mr,	"MR",		"mail rename"},
449*7c478bd9Sstevel@tonic-gate 	{ns_t_null,	"NULL",		"null"},
450*7c478bd9Sstevel@tonic-gate 	{ns_t_wks,	"WKS",		"well-known service (deprecated)"},
451*7c478bd9Sstevel@tonic-gate 	{ns_t_ptr,	"PTR",		"domain name pointer"},
452*7c478bd9Sstevel@tonic-gate 	{ns_t_hinfo,	"HINFO",	"host information"},
453*7c478bd9Sstevel@tonic-gate 	{ns_t_minfo,	"MINFO",	"mailbox information"},
454*7c478bd9Sstevel@tonic-gate 	{ns_t_mx,	"MX",		"mail exchanger"},
455*7c478bd9Sstevel@tonic-gate 	{ns_t_txt,	"TXT",		"text"},
456*7c478bd9Sstevel@tonic-gate 	{ns_t_rp,	"RP",		"responsible person"},
457*7c478bd9Sstevel@tonic-gate 	{ns_t_afsdb,	"AFSDB",	"DCE or AFS server"},
458*7c478bd9Sstevel@tonic-gate 	{ns_t_x25,	"X25",		"X25 address"},
459*7c478bd9Sstevel@tonic-gate 	{ns_t_isdn,	"ISDN",		"ISDN address"},
460*7c478bd9Sstevel@tonic-gate 	{ns_t_rt,	"RT",		"router"},
461*7c478bd9Sstevel@tonic-gate 	{ns_t_nsap,	"NSAP",		"nsap address"},
462*7c478bd9Sstevel@tonic-gate 	{ns_t_nsap_ptr,	"NSAP_PTR",	"domain name pointer"},
463*7c478bd9Sstevel@tonic-gate 	{ns_t_sig,	"SIG",		"signature"},
464*7c478bd9Sstevel@tonic-gate 	{ns_t_key,	"KEY",		"key"},
465*7c478bd9Sstevel@tonic-gate 	{ns_t_px,	"PX",		"mapping information"},
466*7c478bd9Sstevel@tonic-gate 	{ns_t_gpos,	"GPOS",		"geographical position (withdrawn)"},
467*7c478bd9Sstevel@tonic-gate 	{ns_t_aaaa,	"AAAA",		"IPv6 address"},
468*7c478bd9Sstevel@tonic-gate 	{ns_t_loc,	"LOC",		"location"},
469*7c478bd9Sstevel@tonic-gate 	{ns_t_nxt,	"NXT",		"next valid name (unimplemented)"},
470*7c478bd9Sstevel@tonic-gate 	{ns_t_eid,	"EID",		"endpoint identifier (unimplemented)"},
471*7c478bd9Sstevel@tonic-gate 	{ns_t_nimloc,	"NIMLOC",	"NIMROD locator (unimplemented)"},
472*7c478bd9Sstevel@tonic-gate 	{ns_t_srv,	"SRV",		"server selection"},
473*7c478bd9Sstevel@tonic-gate 	{ns_t_atma,	"ATMA",		"ATM address (unimplemented)"},
474*7c478bd9Sstevel@tonic-gate 	{ns_t_tkey,	"TKEY",		"tkey"},
475*7c478bd9Sstevel@tonic-gate 	{ns_t_tsig,	"TSIG",		"transaction signature"},
476*7c478bd9Sstevel@tonic-gate 	{ns_t_ixfr,	"IXFR",		"incremental zone transfer"},
477*7c478bd9Sstevel@tonic-gate 	{ns_t_axfr,	"AXFR",		"zone transfer"},
478*7c478bd9Sstevel@tonic-gate 	{ns_t_zxfr,	"ZXFR",		"compressed zone transfer"},
479*7c478bd9Sstevel@tonic-gate 	{ns_t_mailb,	"MAILB",	"mailbox-related data (deprecated)"},
480*7c478bd9Sstevel@tonic-gate 	{ns_t_maila,	"MAILA",	"mail agent (deprecated)"},
481*7c478bd9Sstevel@tonic-gate 	{ns_t_naptr,	"NAPTR",	"URN Naming Authority"},
482*7c478bd9Sstevel@tonic-gate 	{ns_t_kx,	"KX",		"Key Exchange"},
483*7c478bd9Sstevel@tonic-gate 	{ns_t_cert,	"CERT",		"Certificate"},
484*7c478bd9Sstevel@tonic-gate 	{ns_t_a6,	"A6",		"IPv6 Address"},
485*7c478bd9Sstevel@tonic-gate 	{ns_t_dname,	"DNAME",	"dname"},
486*7c478bd9Sstevel@tonic-gate 	{ns_t_sink,	"SINK",		"Kitchen Sink (experimental)"},
487*7c478bd9Sstevel@tonic-gate 	{ns_t_opt,	"OPT",		"EDNS Options"},
488*7c478bd9Sstevel@tonic-gate 	{ns_t_any,	"ANY",		"\"any\""},
489*7c478bd9Sstevel@tonic-gate 	{0, 		NULL,		NULL}
490*7c478bd9Sstevel@tonic-gate };
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate /*
493*7c478bd9Sstevel@tonic-gate  * Names of DNS rcodes.
494*7c478bd9Sstevel@tonic-gate  */
495*7c478bd9Sstevel@tonic-gate const struct res_sym __p_rcode_syms[] = {
496*7c478bd9Sstevel@tonic-gate 	{ns_r_noerror,	"NOERROR",		"no error"},
497*7c478bd9Sstevel@tonic-gate 	{ns_r_formerr,	"FORMERR",		"format error"},
498*7c478bd9Sstevel@tonic-gate 	{ns_r_servfail,	"SERVFAIL",		"server failed"},
499*7c478bd9Sstevel@tonic-gate 	{ns_r_nxdomain,	"NXDOMAIN",		"no such domain name"},
500*7c478bd9Sstevel@tonic-gate 	{ns_r_notimpl,	"NOTIMP",		"not implemented"},
501*7c478bd9Sstevel@tonic-gate 	{ns_r_refused,	"REFUSED",		"refused"},
502*7c478bd9Sstevel@tonic-gate 	{ns_r_yxdomain,	"YXDOMAIN",		"domain name exists"},
503*7c478bd9Sstevel@tonic-gate 	{ns_r_yxrrset,	"YXRRSET",		"rrset exists"},
504*7c478bd9Sstevel@tonic-gate 	{ns_r_nxrrset,	"NXRRSET",		"rrset doesn't exist"},
505*7c478bd9Sstevel@tonic-gate 	{ns_r_notauth,	"NOTAUTH",		"not authoritative"},
506*7c478bd9Sstevel@tonic-gate 	{ns_r_notzone,	"NOTZONE",		"Not in zone"},
507*7c478bd9Sstevel@tonic-gate 	{ns_r_max,	"",			""},
508*7c478bd9Sstevel@tonic-gate 	{ns_r_badsig,	"BADSIG",		"bad signature"},
509*7c478bd9Sstevel@tonic-gate 	{ns_r_badkey,	"BADKEY",		"bad key"},
510*7c478bd9Sstevel@tonic-gate 	{ns_r_badtime,	"BADTIME",		"bad time"},
511*7c478bd9Sstevel@tonic-gate 	{0, 		NULL,			NULL}
512*7c478bd9Sstevel@tonic-gate };
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate int
515*7c478bd9Sstevel@tonic-gate sym_ston(const struct res_sym *syms, const char *name, int *success) {
516*7c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
517*7c478bd9Sstevel@tonic-gate 		if (strcasecmp (name, syms->name) == 0) {
518*7c478bd9Sstevel@tonic-gate 			if (success)
519*7c478bd9Sstevel@tonic-gate 				*success = 1;
520*7c478bd9Sstevel@tonic-gate 			return (syms->number);
521*7c478bd9Sstevel@tonic-gate 		}
522*7c478bd9Sstevel@tonic-gate 	}
523*7c478bd9Sstevel@tonic-gate 	if (success)
524*7c478bd9Sstevel@tonic-gate 		*success = 0;
525*7c478bd9Sstevel@tonic-gate 	return (syms->number);		/* The default value. */
526*7c478bd9Sstevel@tonic-gate }
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate const char *
529*7c478bd9Sstevel@tonic-gate sym_ntos(const struct res_sym *syms, int number, int *success) {
530*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
531*7c478bd9Sstevel@tonic-gate 	char	*unname = sym_ntos_unname;
532*7c478bd9Sstevel@tonic-gate #else
533*7c478bd9Sstevel@tonic-gate 	static char unname[20];
534*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
537*7c478bd9Sstevel@tonic-gate 		if (number == syms->number) {
538*7c478bd9Sstevel@tonic-gate 			if (success)
539*7c478bd9Sstevel@tonic-gate 				*success = 1;
540*7c478bd9Sstevel@tonic-gate 			return (syms->name);
541*7c478bd9Sstevel@tonic-gate 		}
542*7c478bd9Sstevel@tonic-gate 	}
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	sprintf(unname, "%d", number);		/* XXX nonreentrant */
545*7c478bd9Sstevel@tonic-gate 	if (success)
546*7c478bd9Sstevel@tonic-gate 		*success = 0;
547*7c478bd9Sstevel@tonic-gate 	return (unname);
548*7c478bd9Sstevel@tonic-gate }
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate const char *
551*7c478bd9Sstevel@tonic-gate sym_ntop(const struct res_sym *syms, int number, int *success) {
552*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
553*7c478bd9Sstevel@tonic-gate 	char	*unname = sym_ntop_unname;
554*7c478bd9Sstevel@tonic-gate #else
555*7c478bd9Sstevel@tonic-gate 	static char unname[20];
556*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	for ((void)NULL; syms->name != 0; syms++) {
559*7c478bd9Sstevel@tonic-gate 		if (number == syms->number) {
560*7c478bd9Sstevel@tonic-gate 			if (success)
561*7c478bd9Sstevel@tonic-gate 				*success = 1;
562*7c478bd9Sstevel@tonic-gate 			return (syms->humanname);
563*7c478bd9Sstevel@tonic-gate 		}
564*7c478bd9Sstevel@tonic-gate 	}
565*7c478bd9Sstevel@tonic-gate 	sprintf(unname, "%d", number);		/* XXX nonreentrant */
566*7c478bd9Sstevel@tonic-gate 	if (success)
567*7c478bd9Sstevel@tonic-gate 		*success = 0;
568*7c478bd9Sstevel@tonic-gate 	return (unname);
569*7c478bd9Sstevel@tonic-gate }
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate /*
572*7c478bd9Sstevel@tonic-gate  * Return a string for the type.
573*7c478bd9Sstevel@tonic-gate  */
574*7c478bd9Sstevel@tonic-gate const char *
575*7c478bd9Sstevel@tonic-gate p_type(int type) {
576*7c478bd9Sstevel@tonic-gate 	int success;
577*7c478bd9Sstevel@tonic-gate 	const char *result;
578*7c478bd9Sstevel@tonic-gate 	static char typebuf[20];
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	result = sym_ntos(__p_type_syms, type, &success);
581*7c478bd9Sstevel@tonic-gate 	if (success)
582*7c478bd9Sstevel@tonic-gate 		return (result);
583*7c478bd9Sstevel@tonic-gate 	if (type < 0 || type > 0xfff)
584*7c478bd9Sstevel@tonic-gate 		return ("BADTYPE");
585*7c478bd9Sstevel@tonic-gate 	sprintf(typebuf, "TYPE%d", type);
586*7c478bd9Sstevel@tonic-gate 	return (typebuf);
587*7c478bd9Sstevel@tonic-gate }
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate /*
590*7c478bd9Sstevel@tonic-gate  * Return a string for the type.
591*7c478bd9Sstevel@tonic-gate  */
592*7c478bd9Sstevel@tonic-gate const char *
593*7c478bd9Sstevel@tonic-gate p_section(int section, int opcode) {
594*7c478bd9Sstevel@tonic-gate 	const struct res_sym *symbols;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	switch (opcode) {
597*7c478bd9Sstevel@tonic-gate 	case ns_o_update:
598*7c478bd9Sstevel@tonic-gate 		symbols = __p_update_section_syms;
599*7c478bd9Sstevel@tonic-gate 		break;
600*7c478bd9Sstevel@tonic-gate 	default:
601*7c478bd9Sstevel@tonic-gate 		symbols = __p_default_section_syms;
602*7c478bd9Sstevel@tonic-gate 		break;
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate 	return (sym_ntos(symbols, section, (int *)0));
605*7c478bd9Sstevel@tonic-gate }
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate /*
608*7c478bd9Sstevel@tonic-gate  * Return a mnemonic for class.
609*7c478bd9Sstevel@tonic-gate  */
610*7c478bd9Sstevel@tonic-gate const char *
611*7c478bd9Sstevel@tonic-gate p_class(int class) {
612*7c478bd9Sstevel@tonic-gate 	int success;
613*7c478bd9Sstevel@tonic-gate 	const char *result;
614*7c478bd9Sstevel@tonic-gate 	static char classbuf[20];
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate 	result = sym_ntos(__p_class_syms, class, &success);
617*7c478bd9Sstevel@tonic-gate 	if (success)
618*7c478bd9Sstevel@tonic-gate 		return (result);
619*7c478bd9Sstevel@tonic-gate 	if (class < 0 || class > 0xfff)
620*7c478bd9Sstevel@tonic-gate 		return ("BADCLASS");
621*7c478bd9Sstevel@tonic-gate 	sprintf(classbuf, "CLASS%d", class);
622*7c478bd9Sstevel@tonic-gate 	return (classbuf);
623*7c478bd9Sstevel@tonic-gate }
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate /*
626*7c478bd9Sstevel@tonic-gate  * Return a mnemonic for an option
627*7c478bd9Sstevel@tonic-gate  */
628*7c478bd9Sstevel@tonic-gate const char *
629*7c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
630*7c478bd9Sstevel@tonic-gate p_option(u_long option) {
631*7c478bd9Sstevel@tonic-gate #else
632*7c478bd9Sstevel@tonic-gate p_option(u_int option) {
633*7c478bd9Sstevel@tonic-gate #endif
634*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
635*7c478bd9Sstevel@tonic-gate 	char	*nbuf = p_option_nbuf;
636*7c478bd9Sstevel@tonic-gate #else
637*7c478bd9Sstevel@tonic-gate 	static char nbuf[40];
638*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	switch (option) {
641*7c478bd9Sstevel@tonic-gate 	case RES_INIT:		return "init";
642*7c478bd9Sstevel@tonic-gate 	case RES_DEBUG:		return "debug";
643*7c478bd9Sstevel@tonic-gate 	case RES_AAONLY:	return "aaonly(unimpl)";
644*7c478bd9Sstevel@tonic-gate 	case RES_USEVC:		return "usevc";
645*7c478bd9Sstevel@tonic-gate 	case RES_PRIMARY:	return "primry(unimpl)";
646*7c478bd9Sstevel@tonic-gate 	case RES_IGNTC:		return "igntc";
647*7c478bd9Sstevel@tonic-gate 	case RES_RECURSE:	return "recurs";
648*7c478bd9Sstevel@tonic-gate 	case RES_DEFNAMES:	return "defnam";
649*7c478bd9Sstevel@tonic-gate 	case RES_STAYOPEN:	return "styopn";
650*7c478bd9Sstevel@tonic-gate 	case RES_DNSRCH:	return "dnsrch";
651*7c478bd9Sstevel@tonic-gate 	case RES_INSECURE1:	return "insecure1";
652*7c478bd9Sstevel@tonic-gate 	case RES_INSECURE2:	return "insecure2";
653*7c478bd9Sstevel@tonic-gate 	case RES_NOALIASES:	return "noaliases";
654*7c478bd9Sstevel@tonic-gate 	case RES_USE_INET6:	return "inet6";
655*7c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0	/* KAME extension */
656*7c478bd9Sstevel@tonic-gate 	case RES_USE_EDNS0:	return "edns0";
657*7c478bd9Sstevel@tonic-gate #endif
658*7c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNAME
659*7c478bd9Sstevel@tonic-gate 	case RES_USE_DNAME:	return "dname";
660*7c478bd9Sstevel@tonic-gate #endif
661*7c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNSSEC
662*7c478bd9Sstevel@tonic-gate 	case RES_USE_DNSSEC:	return "dnssec";
663*7c478bd9Sstevel@tonic-gate #endif
664*7c478bd9Sstevel@tonic-gate #ifdef RES_NOTLDQUERY
665*7c478bd9Sstevel@tonic-gate 	case RES_NOTLDQUERY:	return "no-tld-query";
666*7c478bd9Sstevel@tonic-gate #endif
667*7c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
668*7c478bd9Sstevel@tonic-gate 				/* XXX nonreentrant */
669*7c478bd9Sstevel@tonic-gate 	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
670*7c478bd9Sstevel@tonic-gate #else
671*7c478bd9Sstevel@tonic-gate 	default:		sprintf(nbuf, "?0x%x?", (u_int)option);
672*7c478bd9Sstevel@tonic-gate #endif
673*7c478bd9Sstevel@tonic-gate 				return (nbuf);
674*7c478bd9Sstevel@tonic-gate 	}
675*7c478bd9Sstevel@tonic-gate }
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate /*
678*7c478bd9Sstevel@tonic-gate  * Return a mnemonic for a time to live.
679*7c478bd9Sstevel@tonic-gate  */
680*7c478bd9Sstevel@tonic-gate const char *
681*7c478bd9Sstevel@tonic-gate p_time(u_int32_t value) {
682*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
683*7c478bd9Sstevel@tonic-gate 	char	*nbuf = p_time_nbuf;
684*7c478bd9Sstevel@tonic-gate #else
685*7c478bd9Sstevel@tonic-gate 	static char nbuf[40];		/* XXX nonreentrant */
686*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 	if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
689*7c478bd9Sstevel@tonic-gate 		sprintf(nbuf, "%u", value);
690*7c478bd9Sstevel@tonic-gate 	return (nbuf);
691*7c478bd9Sstevel@tonic-gate }
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate /*
694*7c478bd9Sstevel@tonic-gate  * Return a string for the rcode.
695*7c478bd9Sstevel@tonic-gate  */
696*7c478bd9Sstevel@tonic-gate const char *
697*7c478bd9Sstevel@tonic-gate p_rcode(int rcode) {
698*7c478bd9Sstevel@tonic-gate 	return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate /*
702*7c478bd9Sstevel@tonic-gate  * Return a string for a res_sockaddr_union.
703*7c478bd9Sstevel@tonic-gate  */
704*7c478bd9Sstevel@tonic-gate const char *
705*7c478bd9Sstevel@tonic-gate p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
706*7c478bd9Sstevel@tonic-gate 	char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	switch (u.sin.sin_family) {
709*7c478bd9Sstevel@tonic-gate 	case AF_INET:
710*7c478bd9Sstevel@tonic-gate 		inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
711*7c478bd9Sstevel@tonic-gate 		break;
712*7c478bd9Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
713*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
714*7c478bd9Sstevel@tonic-gate 		inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
715*7c478bd9Sstevel@tonic-gate 		break;
716*7c478bd9Sstevel@tonic-gate #endif
717*7c478bd9Sstevel@tonic-gate 	default:
718*7c478bd9Sstevel@tonic-gate 		sprintf(ret, "[af%d]", u.sin.sin_family);
719*7c478bd9Sstevel@tonic-gate 		break;
720*7c478bd9Sstevel@tonic-gate 	}
721*7c478bd9Sstevel@tonic-gate 	if (size > 0) {
722*7c478bd9Sstevel@tonic-gate 		strncpy(buf, ret, size - 1);
723*7c478bd9Sstevel@tonic-gate 		buf[size - 1] = '0';
724*7c478bd9Sstevel@tonic-gate 	}
725*7c478bd9Sstevel@tonic-gate 	return (buf);
726*7c478bd9Sstevel@tonic-gate }
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate /*
729*7c478bd9Sstevel@tonic-gate  * routines to convert between on-the-wire RR format and zone file format.
730*7c478bd9Sstevel@tonic-gate  * Does not contain conversion to/from decimal degrees; divide or multiply
731*7c478bd9Sstevel@tonic-gate  * by 60*60*1000 for that.
732*7c478bd9Sstevel@tonic-gate  */
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
735*7c478bd9Sstevel@tonic-gate 				      1000000,10000000,100000000,1000000000};
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate /* takes an XeY precision/size value, returns a string representation. */
738*7c478bd9Sstevel@tonic-gate static const char *
739*7c478bd9Sstevel@tonic-gate precsize_ntoa(prec)
740*7c478bd9Sstevel@tonic-gate 	u_int8_t prec;
741*7c478bd9Sstevel@tonic-gate {
742*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
743*7c478bd9Sstevel@tonic-gate 	char	*retbuf = precsize_ntoa_retbuf;
744*7c478bd9Sstevel@tonic-gate #else
745*7c478bd9Sstevel@tonic-gate 	static char retbuf[sizeof "90000000.00"];	/* XXX nonreentrant */
746*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
747*7c478bd9Sstevel@tonic-gate 	unsigned long val;
748*7c478bd9Sstevel@tonic-gate 	int mantissa, exponent;
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 	mantissa = (int)((prec >> 4) & 0x0f) % 10;
751*7c478bd9Sstevel@tonic-gate 	exponent = (int)((prec >> 0) & 0x0f) % 10;
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate 	val = mantissa * poweroften[exponent];
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate 	(void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
756*7c478bd9Sstevel@tonic-gate 	return (retbuf);
757*7c478bd9Sstevel@tonic-gate }
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate /* converts ascii size/precision X * 10**Y(cm) to 0xXY.  moves pointer. */
760*7c478bd9Sstevel@tonic-gate static u_int8_t
761*7c478bd9Sstevel@tonic-gate precsize_aton(const char **strptr) {
762*7c478bd9Sstevel@tonic-gate 	unsigned int mval = 0, cmval = 0;
763*7c478bd9Sstevel@tonic-gate 	u_int8_t retval = 0;
764*7c478bd9Sstevel@tonic-gate 	const char *cp;
765*7c478bd9Sstevel@tonic-gate 	int exponent;
766*7c478bd9Sstevel@tonic-gate 	int mantissa;
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 	cp = *strptr;
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
771*7c478bd9Sstevel@tonic-gate 		mval = mval * 10 + (*cp++ - '0');
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate 	if (*cp == '.') {		/* centimeters */
774*7c478bd9Sstevel@tonic-gate 		cp++;
775*7c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
776*7c478bd9Sstevel@tonic-gate 			cmval = (*cp++ - '0') * 10;
777*7c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
778*7c478bd9Sstevel@tonic-gate 				cmval += (*cp++ - '0');
779*7c478bd9Sstevel@tonic-gate 			}
780*7c478bd9Sstevel@tonic-gate 		}
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 	cmval = (mval * 100) + cmval;
783*7c478bd9Sstevel@tonic-gate 
784*7c478bd9Sstevel@tonic-gate 	for (exponent = 0; exponent < 9; exponent++)
785*7c478bd9Sstevel@tonic-gate 		if (cmval < poweroften[exponent+1])
786*7c478bd9Sstevel@tonic-gate 			break;
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	mantissa = cmval / poweroften[exponent];
789*7c478bd9Sstevel@tonic-gate 	if (mantissa > 9)
790*7c478bd9Sstevel@tonic-gate 		mantissa = 9;
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	retval = (mantissa << 4) | exponent;
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate 	*strptr = cp;
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 	return (retval);
797*7c478bd9Sstevel@tonic-gate }
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate /* converts ascii lat/lon to unsigned encoded 32-bit number.  moves pointer. */
800*7c478bd9Sstevel@tonic-gate static u_int32_t
801*7c478bd9Sstevel@tonic-gate latlon2ul(const char **latlonstrptr, int *which) {
802*7c478bd9Sstevel@tonic-gate 	const char *cp;
803*7c478bd9Sstevel@tonic-gate 	u_int32_t retval;
804*7c478bd9Sstevel@tonic-gate 	int deg = 0, min = 0, secs = 0, secsfrac = 0;
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate 	cp = *latlonstrptr;
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
809*7c478bd9Sstevel@tonic-gate 		deg = deg * 10 + (*cp++ - '0');
810*7c478bd9Sstevel@tonic-gate 
811*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
812*7c478bd9Sstevel@tonic-gate 		cp++;
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
815*7c478bd9Sstevel@tonic-gate 		goto fndhemi;
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
818*7c478bd9Sstevel@tonic-gate 		min = min * 10 + (*cp++ - '0');
819*7c478bd9Sstevel@tonic-gate 
820*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
821*7c478bd9Sstevel@tonic-gate 		cp++;
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 	if (!(isdigit((unsigned char)*cp)))
824*7c478bd9Sstevel@tonic-gate 		goto fndhemi;
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
827*7c478bd9Sstevel@tonic-gate 		secs = secs * 10 + (*cp++ - '0');
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 	if (*cp == '.') {		/* decimal seconds */
830*7c478bd9Sstevel@tonic-gate 		cp++;
831*7c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
832*7c478bd9Sstevel@tonic-gate 			secsfrac = (*cp++ - '0') * 100;
833*7c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
834*7c478bd9Sstevel@tonic-gate 				secsfrac += (*cp++ - '0') * 10;
835*7c478bd9Sstevel@tonic-gate 				if (isdigit((unsigned char)*cp)) {
836*7c478bd9Sstevel@tonic-gate 					secsfrac += (*cp++ - '0');
837*7c478bd9Sstevel@tonic-gate 				}
838*7c478bd9Sstevel@tonic-gate 			}
839*7c478bd9Sstevel@tonic-gate 		}
840*7c478bd9Sstevel@tonic-gate 	}
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 	while (!isspace((unsigned char)*cp))	/* if any trailing garbage */
843*7c478bd9Sstevel@tonic-gate 		cp++;
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))
846*7c478bd9Sstevel@tonic-gate 		cp++;
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate  fndhemi:
849*7c478bd9Sstevel@tonic-gate 	switch (*cp) {
850*7c478bd9Sstevel@tonic-gate 	case 'N': case 'n':
851*7c478bd9Sstevel@tonic-gate 	case 'E': case 'e':
852*7c478bd9Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
853*7c478bd9Sstevel@tonic-gate 			+ (((((deg * 60) + min) * 60) + secs) * 1000)
854*7c478bd9Sstevel@tonic-gate 			+ secsfrac;
855*7c478bd9Sstevel@tonic-gate 		break;
856*7c478bd9Sstevel@tonic-gate 	case 'S': case 's':
857*7c478bd9Sstevel@tonic-gate 	case 'W': case 'w':
858*7c478bd9Sstevel@tonic-gate 		retval = ((unsigned)1<<31)
859*7c478bd9Sstevel@tonic-gate 			- (((((deg * 60) + min) * 60) + secs) * 1000)
860*7c478bd9Sstevel@tonic-gate 			- secsfrac;
861*7c478bd9Sstevel@tonic-gate 		break;
862*7c478bd9Sstevel@tonic-gate 	default:
863*7c478bd9Sstevel@tonic-gate 		retval = 0;	/* invalid value -- indicates error */
864*7c478bd9Sstevel@tonic-gate 		break;
865*7c478bd9Sstevel@tonic-gate 	}
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	switch (*cp) {
868*7c478bd9Sstevel@tonic-gate 	case 'N': case 'n':
869*7c478bd9Sstevel@tonic-gate 	case 'S': case 's':
870*7c478bd9Sstevel@tonic-gate 		*which = 1;	/* latitude */
871*7c478bd9Sstevel@tonic-gate 		break;
872*7c478bd9Sstevel@tonic-gate 	case 'E': case 'e':
873*7c478bd9Sstevel@tonic-gate 	case 'W': case 'w':
874*7c478bd9Sstevel@tonic-gate 		*which = 2;	/* longitude */
875*7c478bd9Sstevel@tonic-gate 		break;
876*7c478bd9Sstevel@tonic-gate 	default:
877*7c478bd9Sstevel@tonic-gate 		*which = 0;	/* error */
878*7c478bd9Sstevel@tonic-gate 		break;
879*7c478bd9Sstevel@tonic-gate 	}
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	cp++;			/* skip the hemisphere */
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate 	while (!isspace((unsigned char)*cp))	/* if any trailing garbage */
884*7c478bd9Sstevel@tonic-gate 		cp++;
885*7c478bd9Sstevel@tonic-gate 
886*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp))	/* move to next field */
887*7c478bd9Sstevel@tonic-gate 		cp++;
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate 	*latlonstrptr = cp;
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 	return (retval);
892*7c478bd9Sstevel@tonic-gate }
893*7c478bd9Sstevel@tonic-gate 
894*7c478bd9Sstevel@tonic-gate /* converts a zone file representation in a string to an RDATA on-the-wire
895*7c478bd9Sstevel@tonic-gate  * representation. */
896*7c478bd9Sstevel@tonic-gate int
897*7c478bd9Sstevel@tonic-gate loc_aton(ascii, binary)
898*7c478bd9Sstevel@tonic-gate 	const char *ascii;
899*7c478bd9Sstevel@tonic-gate 	u_char *binary;
900*7c478bd9Sstevel@tonic-gate {
901*7c478bd9Sstevel@tonic-gate 	const char *cp, *maxcp;
902*7c478bd9Sstevel@tonic-gate 	u_char *bcp;
903*7c478bd9Sstevel@tonic-gate 
904*7c478bd9Sstevel@tonic-gate 	u_int32_t latit = 0, longit = 0, alt = 0;
905*7c478bd9Sstevel@tonic-gate 	u_int32_t lltemp1 = 0, lltemp2 = 0;
906*7c478bd9Sstevel@tonic-gate 	int altmeters = 0, altfrac = 0, altsign = 1;
907*7c478bd9Sstevel@tonic-gate 	u_int8_t hp = 0x16;	/* default = 1e6 cm = 10000.00m = 10km */
908*7c478bd9Sstevel@tonic-gate 	u_int8_t vp = 0x13;	/* default = 1e3 cm = 10.00m */
909*7c478bd9Sstevel@tonic-gate 	u_int8_t siz = 0x12;	/* default = 1e2 cm = 1.00m */
910*7c478bd9Sstevel@tonic-gate 	int which1 = 0, which2 = 0;
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate 	cp = ascii;
913*7c478bd9Sstevel@tonic-gate 	maxcp = cp + strlen(ascii);
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 	lltemp1 = latlon2ul(&cp, &which1);
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate 	lltemp2 = latlon2ul(&cp, &which2);
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	switch (which1 + which2) {
920*7c478bd9Sstevel@tonic-gate 	case 3:			/* 1 + 2, the only valid combination */
921*7c478bd9Sstevel@tonic-gate 		if ((which1 == 1) && (which2 == 2)) { /* normal case */
922*7c478bd9Sstevel@tonic-gate 			latit = lltemp1;
923*7c478bd9Sstevel@tonic-gate 			longit = lltemp2;
924*7c478bd9Sstevel@tonic-gate 		} else if ((which1 == 2) && (which2 == 1)) { /* reversed */
925*7c478bd9Sstevel@tonic-gate 			longit = lltemp1;
926*7c478bd9Sstevel@tonic-gate 			latit = lltemp2;
927*7c478bd9Sstevel@tonic-gate 		} else {	/* some kind of brokenness */
928*7c478bd9Sstevel@tonic-gate 			return (0);
929*7c478bd9Sstevel@tonic-gate 		}
930*7c478bd9Sstevel@tonic-gate 		break;
931*7c478bd9Sstevel@tonic-gate 	default:		/* we didn't get one of each */
932*7c478bd9Sstevel@tonic-gate 		return (0);
933*7c478bd9Sstevel@tonic-gate 	}
934*7c478bd9Sstevel@tonic-gate 
935*7c478bd9Sstevel@tonic-gate 	/* altitude */
936*7c478bd9Sstevel@tonic-gate 	if (*cp == '-') {
937*7c478bd9Sstevel@tonic-gate 		altsign = -1;
938*7c478bd9Sstevel@tonic-gate 		cp++;
939*7c478bd9Sstevel@tonic-gate 	}
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 	if (*cp == '+')
942*7c478bd9Sstevel@tonic-gate 		cp++;
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*cp))
945*7c478bd9Sstevel@tonic-gate 		altmeters = altmeters * 10 + (*cp++ - '0');
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate 	if (*cp == '.') {		/* decimal meters */
948*7c478bd9Sstevel@tonic-gate 		cp++;
949*7c478bd9Sstevel@tonic-gate 		if (isdigit((unsigned char)*cp)) {
950*7c478bd9Sstevel@tonic-gate 			altfrac = (*cp++ - '0') * 10;
951*7c478bd9Sstevel@tonic-gate 			if (isdigit((unsigned char)*cp)) {
952*7c478bd9Sstevel@tonic-gate 				altfrac += (*cp++ - '0');
953*7c478bd9Sstevel@tonic-gate 			}
954*7c478bd9Sstevel@tonic-gate 		}
955*7c478bd9Sstevel@tonic-gate 	}
956*7c478bd9Sstevel@tonic-gate 
957*7c478bd9Sstevel@tonic-gate 	alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
958*7c478bd9Sstevel@tonic-gate 
959*7c478bd9Sstevel@tonic-gate 	while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
960*7c478bd9Sstevel@tonic-gate 		cp++;
961*7c478bd9Sstevel@tonic-gate 
962*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
963*7c478bd9Sstevel@tonic-gate 		cp++;
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
966*7c478bd9Sstevel@tonic-gate 		goto defaults;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	siz = precsize_aton(&cp);
969*7c478bd9Sstevel@tonic-gate 
970*7c478bd9Sstevel@tonic-gate 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/* if trailing garbage or m */
971*7c478bd9Sstevel@tonic-gate 		cp++;
972*7c478bd9Sstevel@tonic-gate 
973*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
974*7c478bd9Sstevel@tonic-gate 		cp++;
975*7c478bd9Sstevel@tonic-gate 
976*7c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
977*7c478bd9Sstevel@tonic-gate 		goto defaults;
978*7c478bd9Sstevel@tonic-gate 
979*7c478bd9Sstevel@tonic-gate 	hp = precsize_aton(&cp);
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/* if trailing garbage or m */
982*7c478bd9Sstevel@tonic-gate 		cp++;
983*7c478bd9Sstevel@tonic-gate 
984*7c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*cp) && (cp < maxcp))
985*7c478bd9Sstevel@tonic-gate 		cp++;
986*7c478bd9Sstevel@tonic-gate 
987*7c478bd9Sstevel@tonic-gate 	if (cp >= maxcp)
988*7c478bd9Sstevel@tonic-gate 		goto defaults;
989*7c478bd9Sstevel@tonic-gate 
990*7c478bd9Sstevel@tonic-gate 	vp = precsize_aton(&cp);
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate  defaults:
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 	bcp = binary;
995*7c478bd9Sstevel@tonic-gate 	*bcp++ = (u_int8_t) 0;	/* version byte */
996*7c478bd9Sstevel@tonic-gate 	*bcp++ = siz;
997*7c478bd9Sstevel@tonic-gate 	*bcp++ = hp;
998*7c478bd9Sstevel@tonic-gate 	*bcp++ = vp;
999*7c478bd9Sstevel@tonic-gate 	PUTLONG(latit,bcp);
1000*7c478bd9Sstevel@tonic-gate 	PUTLONG(longit,bcp);
1001*7c478bd9Sstevel@tonic-gate 	PUTLONG(alt,bcp);
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate 	return (16);		/* size of RR in octets */
1004*7c478bd9Sstevel@tonic-gate }
1005*7c478bd9Sstevel@tonic-gate 
1006*7c478bd9Sstevel@tonic-gate /* takes an on-the-wire LOC RR and formats it in a human readable format. */
1007*7c478bd9Sstevel@tonic-gate const char *
1008*7c478bd9Sstevel@tonic-gate loc_ntoa(binary, ascii)
1009*7c478bd9Sstevel@tonic-gate 	const u_char *binary;
1010*7c478bd9Sstevel@tonic-gate 	char *ascii;
1011*7c478bd9Sstevel@tonic-gate {
1012*7c478bd9Sstevel@tonic-gate 	static const char *error = "?";
1013*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
1014*7c478bd9Sstevel@tonic-gate 	char	*tmpbuf = loc_ntoa_tmpbuf;
1015*7c478bd9Sstevel@tonic-gate #else
1016*7c478bd9Sstevel@tonic-gate 	static char tmpbuf[sizeof
1017*7c478bd9Sstevel@tonic-gate "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
1018*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
1019*7c478bd9Sstevel@tonic-gate 	const u_char *cp = binary;
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate 	int latdeg, latmin, latsec, latsecfrac;
1022*7c478bd9Sstevel@tonic-gate 	int longdeg, longmin, longsec, longsecfrac;
1023*7c478bd9Sstevel@tonic-gate 	char northsouth, eastwest;
1024*7c478bd9Sstevel@tonic-gate 	const char *altsign;
1025*7c478bd9Sstevel@tonic-gate 	int altmeters, altfrac;
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate 	const u_int32_t referencealt = 100000 * 100;
1028*7c478bd9Sstevel@tonic-gate 
1029*7c478bd9Sstevel@tonic-gate 	int32_t latval, longval, altval;
1030*7c478bd9Sstevel@tonic-gate 	u_int32_t templ;
1031*7c478bd9Sstevel@tonic-gate 	u_int8_t sizeval, hpval, vpval, versionval;
1032*7c478bd9Sstevel@tonic-gate 
1033*7c478bd9Sstevel@tonic-gate 	char *sizestr, *hpstr, *vpstr;
1034*7c478bd9Sstevel@tonic-gate 
1035*7c478bd9Sstevel@tonic-gate 	versionval = *cp++;
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate 	if (ascii == NULL)
1038*7c478bd9Sstevel@tonic-gate 		ascii = tmpbuf;
1039*7c478bd9Sstevel@tonic-gate 
1040*7c478bd9Sstevel@tonic-gate 	if (versionval) {
1041*7c478bd9Sstevel@tonic-gate 		(void) sprintf(ascii, "; error: unknown LOC RR version");
1042*7c478bd9Sstevel@tonic-gate 		return (ascii);
1043*7c478bd9Sstevel@tonic-gate 	}
1044*7c478bd9Sstevel@tonic-gate 
1045*7c478bd9Sstevel@tonic-gate 	sizeval = *cp++;
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 	hpval = *cp++;
1048*7c478bd9Sstevel@tonic-gate 	vpval = *cp++;
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
1051*7c478bd9Sstevel@tonic-gate 	latval = (templ - ((unsigned)1<<31));
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
1054*7c478bd9Sstevel@tonic-gate 	longval = (templ - ((unsigned)1<<31));
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate 	GETLONG(templ, cp);
1057*7c478bd9Sstevel@tonic-gate 	if (templ < referencealt) { /* below WGS 84 spheroid */
1058*7c478bd9Sstevel@tonic-gate 		altval = referencealt - templ;
1059*7c478bd9Sstevel@tonic-gate 		altsign = "-";
1060*7c478bd9Sstevel@tonic-gate 	} else {
1061*7c478bd9Sstevel@tonic-gate 		altval = templ - referencealt;
1062*7c478bd9Sstevel@tonic-gate 		altsign = "";
1063*7c478bd9Sstevel@tonic-gate 	}
1064*7c478bd9Sstevel@tonic-gate 
1065*7c478bd9Sstevel@tonic-gate 	if (latval < 0) {
1066*7c478bd9Sstevel@tonic-gate 		northsouth = 'S';
1067*7c478bd9Sstevel@tonic-gate 		latval = -latval;
1068*7c478bd9Sstevel@tonic-gate 	} else
1069*7c478bd9Sstevel@tonic-gate 		northsouth = 'N';
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate 	latsecfrac = latval % 1000;
1072*7c478bd9Sstevel@tonic-gate 	latval = latval / 1000;
1073*7c478bd9Sstevel@tonic-gate 	latsec = latval % 60;
1074*7c478bd9Sstevel@tonic-gate 	latval = latval / 60;
1075*7c478bd9Sstevel@tonic-gate 	latmin = latval % 60;
1076*7c478bd9Sstevel@tonic-gate 	latval = latval / 60;
1077*7c478bd9Sstevel@tonic-gate 	latdeg = latval;
1078*7c478bd9Sstevel@tonic-gate 
1079*7c478bd9Sstevel@tonic-gate 	if (longval < 0) {
1080*7c478bd9Sstevel@tonic-gate 		eastwest = 'W';
1081*7c478bd9Sstevel@tonic-gate 		longval = -longval;
1082*7c478bd9Sstevel@tonic-gate 	} else
1083*7c478bd9Sstevel@tonic-gate 		eastwest = 'E';
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate 	longsecfrac = longval % 1000;
1086*7c478bd9Sstevel@tonic-gate 	longval = longval / 1000;
1087*7c478bd9Sstevel@tonic-gate 	longsec = longval % 60;
1088*7c478bd9Sstevel@tonic-gate 	longval = longval / 60;
1089*7c478bd9Sstevel@tonic-gate 	longmin = longval % 60;
1090*7c478bd9Sstevel@tonic-gate 	longval = longval / 60;
1091*7c478bd9Sstevel@tonic-gate 	longdeg = longval;
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 	altfrac = altval % 100;
1094*7c478bd9Sstevel@tonic-gate 	altmeters = (altval / 100);
1095*7c478bd9Sstevel@tonic-gate 
1096*7c478bd9Sstevel@tonic-gate 	sizestr = strdup(precsize_ntoa(sizeval));
1097*7c478bd9Sstevel@tonic-gate 	hpstr = strdup(precsize_ntoa(hpval));
1098*7c478bd9Sstevel@tonic-gate 	vpstr = strdup(precsize_ntoa(vpval));
1099*7c478bd9Sstevel@tonic-gate 
1100*7c478bd9Sstevel@tonic-gate 	sprintf(ascii,
1101*7c478bd9Sstevel@tonic-gate 	    "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
1102*7c478bd9Sstevel@tonic-gate 		latdeg, latmin, latsec, latsecfrac, northsouth,
1103*7c478bd9Sstevel@tonic-gate 		longdeg, longmin, longsec, longsecfrac, eastwest,
1104*7c478bd9Sstevel@tonic-gate 		altsign, altmeters, altfrac,
1105*7c478bd9Sstevel@tonic-gate 		(sizestr != NULL) ? sizestr : error,
1106*7c478bd9Sstevel@tonic-gate 		(hpstr != NULL) ? hpstr : error,
1107*7c478bd9Sstevel@tonic-gate 		(vpstr != NULL) ? vpstr : error);
1108*7c478bd9Sstevel@tonic-gate 
1109*7c478bd9Sstevel@tonic-gate 	if (sizestr != NULL)
1110*7c478bd9Sstevel@tonic-gate 		free(sizestr);
1111*7c478bd9Sstevel@tonic-gate 	if (hpstr != NULL)
1112*7c478bd9Sstevel@tonic-gate 		free(hpstr);
1113*7c478bd9Sstevel@tonic-gate 	if (vpstr != NULL)
1114*7c478bd9Sstevel@tonic-gate 		free(vpstr);
1115*7c478bd9Sstevel@tonic-gate 
1116*7c478bd9Sstevel@tonic-gate 	return (ascii);
1117*7c478bd9Sstevel@tonic-gate }
1118*7c478bd9Sstevel@tonic-gate 
1119*7c478bd9Sstevel@tonic-gate 
1120*7c478bd9Sstevel@tonic-gate /* Return the number of DNS hierarchy levels in the name. */
1121*7c478bd9Sstevel@tonic-gate int
1122*7c478bd9Sstevel@tonic-gate dn_count_labels(const char *name) {
1123*7c478bd9Sstevel@tonic-gate 	int i, len, count;
1124*7c478bd9Sstevel@tonic-gate 
1125*7c478bd9Sstevel@tonic-gate 	len = strlen(name);
1126*7c478bd9Sstevel@tonic-gate 	for (i = 0, count = 0; i < len; i++) {
1127*7c478bd9Sstevel@tonic-gate 		/* XXX need to check for \. or use named's nlabels(). */
1128*7c478bd9Sstevel@tonic-gate 		if (name[i] == '.')
1129*7c478bd9Sstevel@tonic-gate 			count++;
1130*7c478bd9Sstevel@tonic-gate 	}
1131*7c478bd9Sstevel@tonic-gate 
1132*7c478bd9Sstevel@tonic-gate 	/* don't count initial wildcard */
1133*7c478bd9Sstevel@tonic-gate 	if (name[0] == '*')
1134*7c478bd9Sstevel@tonic-gate 		if (count)
1135*7c478bd9Sstevel@tonic-gate 			count--;
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate 	/* don't count the null label for root. */
1138*7c478bd9Sstevel@tonic-gate 	/* if terminating '.' not found, must adjust */
1139*7c478bd9Sstevel@tonic-gate 	/* count to include last label */
1140*7c478bd9Sstevel@tonic-gate 	if (len > 0 && name[len-1] != '.')
1141*7c478bd9Sstevel@tonic-gate 		count++;
1142*7c478bd9Sstevel@tonic-gate 	return (count);
1143*7c478bd9Sstevel@tonic-gate }
1144*7c478bd9Sstevel@tonic-gate 
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate /*
1147*7c478bd9Sstevel@tonic-gate  * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1148*7c478bd9Sstevel@tonic-gate  * SIG records are required to be printed like this, by the Secure DNS RFC.
1149*7c478bd9Sstevel@tonic-gate  */
1150*7c478bd9Sstevel@tonic-gate char *
1151*7c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
1152*7c478bd9Sstevel@tonic-gate p_secstodate (u_long secs) {
1153*7c478bd9Sstevel@tonic-gate #else
1154*7c478bd9Sstevel@tonic-gate p_secstodate (u_int secs) {
1155*7c478bd9Sstevel@tonic-gate #endif
1156*7c478bd9Sstevel@tonic-gate #ifdef	SUNW_MT_RESOLVER
1157*7c478bd9Sstevel@tonic-gate 	char	*output = p_secstodate_output;
1158*7c478bd9Sstevel@tonic-gate #else
1159*7c478bd9Sstevel@tonic-gate 	/* XXX nonreentrant */
1160*7c478bd9Sstevel@tonic-gate 	static char output[15];		/* YYYYMMDDHHMMSS and null */
1161*7c478bd9Sstevel@tonic-gate #endif	/* SUNW_MT_RESOLVER */
1162*7c478bd9Sstevel@tonic-gate 	time_t clock = secs;
1163*7c478bd9Sstevel@tonic-gate 	struct tm *time;
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate #ifdef HAVE_TIME_R
1166*7c478bd9Sstevel@tonic-gate 	gmtime_r(&clock, &time);
1167*7c478bd9Sstevel@tonic-gate #else
1168*7c478bd9Sstevel@tonic-gate 	time = gmtime(&clock);
1169*7c478bd9Sstevel@tonic-gate #endif
1170*7c478bd9Sstevel@tonic-gate 	time->tm_year += 1900;
1171*7c478bd9Sstevel@tonic-gate 	time->tm_mon += 1;
1172*7c478bd9Sstevel@tonic-gate 	sprintf(output, "%04d%02d%02d%02d%02d%02d",
1173*7c478bd9Sstevel@tonic-gate 		time->tm_year, time->tm_mon, time->tm_mday,
1174*7c478bd9Sstevel@tonic-gate 		time->tm_hour, time->tm_min, time->tm_sec);
1175*7c478bd9Sstevel@tonic-gate 	return (output);
1176*7c478bd9Sstevel@tonic-gate }
1177*7c478bd9Sstevel@tonic-gate 
1178*7c478bd9Sstevel@tonic-gate u_int16_t
1179*7c478bd9Sstevel@tonic-gate res_nametoclass(const char *buf, int *successp) {
1180*7c478bd9Sstevel@tonic-gate 	unsigned long result;
1181*7c478bd9Sstevel@tonic-gate 	char *endptr;
1182*7c478bd9Sstevel@tonic-gate 	int success;
1183*7c478bd9Sstevel@tonic-gate 
1184*7c478bd9Sstevel@tonic-gate 	result = sym_ston(__p_class_syms, buf, &success);
1185*7c478bd9Sstevel@tonic-gate 	if (success)
1186*7c478bd9Sstevel@tonic-gate 		goto done;
1187*7c478bd9Sstevel@tonic-gate 
1188*7c478bd9Sstevel@tonic-gate 	if (strncasecmp(buf, "CLASS", 5) != 0 ||
1189*7c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[5]))
1190*7c478bd9Sstevel@tonic-gate 		goto done;
1191*7c478bd9Sstevel@tonic-gate 	errno = 0;
1192*7c478bd9Sstevel@tonic-gate 	result = strtoul(buf + 5, &endptr, 10);
1193*7c478bd9Sstevel@tonic-gate 	if (errno == 0 && *endptr == '\0' && result <= 0xffff)
1194*7c478bd9Sstevel@tonic-gate 		success = 1;
1195*7c478bd9Sstevel@tonic-gate  done:
1196*7c478bd9Sstevel@tonic-gate 	if (successp)
1197*7c478bd9Sstevel@tonic-gate 		*successp = success;
1198*7c478bd9Sstevel@tonic-gate 	return (result);
1199*7c478bd9Sstevel@tonic-gate }
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate u_int16_t
1202*7c478bd9Sstevel@tonic-gate res_nametotype(const char *buf, int *successp) {
1203*7c478bd9Sstevel@tonic-gate 	unsigned long result;
1204*7c478bd9Sstevel@tonic-gate 	char *endptr;
1205*7c478bd9Sstevel@tonic-gate 	int success;
1206*7c478bd9Sstevel@tonic-gate 
1207*7c478bd9Sstevel@tonic-gate 	result = sym_ston(__p_type_syms, buf, &success);
1208*7c478bd9Sstevel@tonic-gate 	if (success)
1209*7c478bd9Sstevel@tonic-gate 		goto done;
1210*7c478bd9Sstevel@tonic-gate 
1211*7c478bd9Sstevel@tonic-gate 	if (strncasecmp(buf, "type", 4) != 0 ||
1212*7c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)buf[4]))
1213*7c478bd9Sstevel@tonic-gate 		goto done;
1214*7c478bd9Sstevel@tonic-gate 	errno = 0;
1215*7c478bd9Sstevel@tonic-gate 	result = strtoul(buf + 4, &endptr, 10);
1216*7c478bd9Sstevel@tonic-gate 	if (errno == 0 && *endptr == '\0' && result <= 0xffff)
1217*7c478bd9Sstevel@tonic-gate 		success = 1;
1218*7c478bd9Sstevel@tonic-gate  done:
1219*7c478bd9Sstevel@tonic-gate 	if (successp)
1220*7c478bd9Sstevel@tonic-gate 		*successp = success;
1221*7c478bd9Sstevel@tonic-gate 	return (result);
1222*7c478bd9Sstevel@tonic-gate }
1223