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