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