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