xref: /minix3/lib/libc/resolv/res_debug.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: res_debug.c,v 1.14 2015/02/24 17:56:20 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Portions Copyright (C) 2004, 2005, 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Permission to use, copy, modify, and/or distribute this software for any
82fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras  *
112fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
122fe8fb19SBen Gras  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
132fe8fb19SBen Gras  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
142fe8fb19SBen Gras  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
152fe8fb19SBen Gras  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
162fe8fb19SBen Gras  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
172fe8fb19SBen Gras  * PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras  */
192fe8fb19SBen Gras 
202fe8fb19SBen Gras /*
212fe8fb19SBen Gras  * Copyright (c) 1985
222fe8fb19SBen Gras  *    The Regents of the University of California.  All rights reserved.
232fe8fb19SBen Gras  *
242fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
252fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
262fe8fb19SBen Gras  * are met:
272fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
282fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
292fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
302fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
312fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
32*0a6a1f1dSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
332fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
342fe8fb19SBen Gras  *    without specific prior written permission.
352fe8fb19SBen Gras  *
362fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
372fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
382fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
392fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
402fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
412fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
422fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
432fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
442fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
452fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
462fe8fb19SBen Gras  * SUCH DAMAGE.
472fe8fb19SBen Gras  */
482fe8fb19SBen Gras 
492fe8fb19SBen Gras /*
502fe8fb19SBen Gras  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
512fe8fb19SBen Gras  *
522fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
532fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
542fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies, and that
552fe8fb19SBen Gras  * the name of Digital Equipment Corporation not be used in advertising or
562fe8fb19SBen Gras  * publicity pertaining to distribution of the document or software without
572fe8fb19SBen Gras  * specific, written prior permission.
582fe8fb19SBen Gras  *
592fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
602fe8fb19SBen Gras  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
612fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
622fe8fb19SBen Gras  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
632fe8fb19SBen Gras  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
642fe8fb19SBen Gras  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
652fe8fb19SBen Gras  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
662fe8fb19SBen Gras  * SOFTWARE.
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras 
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras  * Portions Copyright (c) 1995 by International Business Machines, Inc.
712fe8fb19SBen Gras  *
722fe8fb19SBen Gras  * International Business Machines, Inc. (hereinafter called IBM) grants
732fe8fb19SBen Gras  * permission under its copyrights to use, copy, modify, and distribute this
742fe8fb19SBen Gras  * Software with or without fee, provided that the above copyright notice and
752fe8fb19SBen Gras  * all paragraphs of this notice appear in all copies, and that the name of IBM
762fe8fb19SBen Gras  * not be used in connection with the marketing of any product incorporating
772fe8fb19SBen Gras  * the Software or modifications thereof, without specific, written prior
782fe8fb19SBen Gras  * permission.
792fe8fb19SBen Gras  *
802fe8fb19SBen Gras  * To the extent it has a right to do so, IBM grants an immunity from suit
812fe8fb19SBen Gras  * under its patents, if any, for the use, sale or manufacture of products to
822fe8fb19SBen Gras  * the extent that such products are used for performing Domain Name System
832fe8fb19SBen Gras  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
842fe8fb19SBen Gras  * granted for any product per se or for any other function of any product.
852fe8fb19SBen Gras  *
862fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
872fe8fb19SBen Gras  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
882fe8fb19SBen Gras  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
892fe8fb19SBen Gras  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
902fe8fb19SBen Gras  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
912fe8fb19SBen Gras  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
922fe8fb19SBen Gras  */
932fe8fb19SBen Gras 
942fe8fb19SBen Gras #include <sys/cdefs.h>
952fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
962fe8fb19SBen Gras #ifdef notdef
972fe8fb19SBen Gras static const char sccsid[] = "@(#)res_debug.c	8.1 (Berkeley) 6/4/93";
982fe8fb19SBen Gras static const char rcsid[] = "Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp";
992fe8fb19SBen Gras #else
100*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: res_debug.c,v 1.14 2015/02/24 17:56:20 christos Exp $");
1012fe8fb19SBen Gras #endif
1022fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
1032fe8fb19SBen Gras 
1042fe8fb19SBen Gras #include "port_before.h"
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras #include "namespace.h"
1072fe8fb19SBen Gras #include <sys/types.h>
1082fe8fb19SBen Gras #include <sys/param.h>
1092fe8fb19SBen Gras #include <sys/socket.h>
1102fe8fb19SBen Gras 
1112fe8fb19SBen Gras #include <netinet/in.h>
1122fe8fb19SBen Gras #include <arpa/inet.h>
1132fe8fb19SBen Gras #include <arpa/nameser.h>
1142fe8fb19SBen Gras 
115f14fb602SLionel Sambuc #include <assert.h>
1162fe8fb19SBen Gras #include <ctype.h>
1172fe8fb19SBen Gras #include <errno.h>
1182fe8fb19SBen Gras #include <math.h>
1192fe8fb19SBen Gras #include <netdb.h>
1202fe8fb19SBen Gras #include <resolv.h>
1212fe8fb19SBen Gras #include <resolv_mt.h>
1222fe8fb19SBen Gras #include <stdio.h>
1232fe8fb19SBen Gras #include <stdlib.h>
1242fe8fb19SBen Gras #include <string.h>
1252fe8fb19SBen Gras #include <time.h>
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras #include "port_after.h"
1282fe8fb19SBen Gras 
1292fe8fb19SBen Gras #ifdef SPRINTF_CHAR
1302fe8fb19SBen Gras # define SPRINTF(x) strlen(sprintf/**/x)
1312fe8fb19SBen Gras #else
1322fe8fb19SBen Gras # define SPRINTF(x) sprintf x
1332fe8fb19SBen Gras #endif
1342fe8fb19SBen Gras 
1352fe8fb19SBen Gras extern const char *_res_opcodes[];
1362fe8fb19SBen Gras extern const char *_res_sectioncodes[];
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras #if 0
1392fe8fb19SBen Gras #ifdef __weak_alias
1402fe8fb19SBen Gras __weak_alias(res_pquery,__res_pquery)
1412fe8fb19SBen Gras __weak_alias(res_nametoclass,__res_nametoclass)
1422fe8fb19SBen Gras __weak_alias(res_nametotype,__res_nametotype)
1432fe8fb19SBen Gras #endif
1442fe8fb19SBen Gras #endif
1452fe8fb19SBen Gras 
1462fe8fb19SBen Gras #ifndef _LIBC
1472fe8fb19SBen Gras /*%
1482fe8fb19SBen Gras  * Print the current options.
1492fe8fb19SBen Gras  */
1502fe8fb19SBen Gras void
fp_resstat(const res_state statp,FILE * file)1512fe8fb19SBen Gras fp_resstat(const res_state statp, FILE *file) {
1522fe8fb19SBen Gras 	u_long mask;
1532fe8fb19SBen Gras 
1542fe8fb19SBen Gras 	fprintf(file, ";; res options:");
1552fe8fb19SBen Gras 	for (mask = 1;  mask != 0U;  mask <<= 1)
1562fe8fb19SBen Gras 		if (statp->options & mask)
1572fe8fb19SBen Gras 			fprintf(file, " %s", p_option(mask));
1582fe8fb19SBen Gras 	putc('\n', file);
1592fe8fb19SBen Gras }
1602fe8fb19SBen Gras #endif
1612fe8fb19SBen Gras 
1622fe8fb19SBen Gras static void
do_section(const res_state statp,ns_msg * handle,ns_sect section,int pflag,FILE * file)1632fe8fb19SBen Gras do_section(const res_state statp,
1642fe8fb19SBen Gras 	   ns_msg *handle, ns_sect section,
1652fe8fb19SBen Gras 	   int pflag, FILE *file)
1662fe8fb19SBen Gras {
1672fe8fb19SBen Gras 	int n, sflag, rrnum;
1682fe8fb19SBen Gras 	static int buflen = 2048;
1692fe8fb19SBen Gras 	char *buf;
1702fe8fb19SBen Gras 	ns_opcode opcode;
1712fe8fb19SBen Gras 	ns_rr rr;
1722fe8fb19SBen Gras 
1732fe8fb19SBen Gras 	/*
1742fe8fb19SBen Gras 	 * Print answer records.
1752fe8fb19SBen Gras 	 */
176f14fb602SLionel Sambuc 	sflag = (int)(statp->pfcode & pflag);
1772fe8fb19SBen Gras 	if (statp->pfcode && !sflag)
1782fe8fb19SBen Gras 		return;
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras 	buf = malloc((size_t)buflen);
1812fe8fb19SBen Gras 	if (buf == NULL) {
1822fe8fb19SBen Gras 		fprintf(file, ";; memory allocation failure\n");
1832fe8fb19SBen Gras 		return;
1842fe8fb19SBen Gras 	}
1852fe8fb19SBen Gras 
1862fe8fb19SBen Gras 	opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
1872fe8fb19SBen Gras 	rrnum = 0;
1882fe8fb19SBen Gras 	for (;;) {
1892fe8fb19SBen Gras 		if (ns_parserr(handle, section, rrnum, &rr)) {
1902fe8fb19SBen Gras 			if (errno != ENODEV)
1912fe8fb19SBen Gras 				fprintf(file, ";; ns_parserr: %s\n",
1922fe8fb19SBen Gras 					strerror(errno));
1932fe8fb19SBen Gras 			else if (rrnum > 0 && sflag != 0 &&
1942fe8fb19SBen Gras 				 (statp->pfcode & RES_PRF_HEAD1))
1952fe8fb19SBen Gras 				putc('\n', file);
1962fe8fb19SBen Gras 			goto cleanup;
1972fe8fb19SBen Gras 		}
1982fe8fb19SBen Gras 		if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
1992fe8fb19SBen Gras 			fprintf(file, ";; %s SECTION:\n",
2002fe8fb19SBen Gras 				p_section(section, opcode));
2012fe8fb19SBen Gras 		if (section == ns_s_qd)
2022fe8fb19SBen Gras 			fprintf(file, ";;\t%s, type = %s, class = %s\n",
2032fe8fb19SBen Gras 				ns_rr_name(rr),
2042fe8fb19SBen Gras 				p_type(ns_rr_type(rr)),
2052fe8fb19SBen Gras 				p_class(ns_rr_class(rr)));
2062fe8fb19SBen Gras 		else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
207f14fb602SLionel Sambuc 			size_t rdatalen, ttl;
208f14fb602SLionel Sambuc 			uint16_t optcode, optlen;
209f14fb602SLionel Sambuc 
210f14fb602SLionel Sambuc 			rdatalen = ns_rr_rdlen(rr);
211f14fb602SLionel Sambuc 			ttl = ns_rr_ttl(rr);
2122fe8fb19SBen Gras 
2132fe8fb19SBen Gras 			fprintf(file,
214f14fb602SLionel Sambuc 				"; EDNS: version: %zu, udp=%u, flags=%04zx\n",
2152fe8fb19SBen Gras 				(ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
2162fe8fb19SBen Gras 
2172fe8fb19SBen Gras 			while (rdatalen >= 4) {
2182fe8fb19SBen Gras 				const u_char *cp = ns_rr_rdata(rr);
2192fe8fb19SBen Gras 				int i;
2202fe8fb19SBen Gras 
2212fe8fb19SBen Gras 				GETSHORT(optcode, cp);
2222fe8fb19SBen Gras 				GETSHORT(optlen, cp);
2232fe8fb19SBen Gras 
2242fe8fb19SBen Gras 				if (optcode == NS_OPT_NSID) {
2252fe8fb19SBen Gras 					fputs("; NSID: ", file);
2262fe8fb19SBen Gras 					if (optlen == 0) {
2272fe8fb19SBen Gras 						fputs("; NSID\n", file);
2282fe8fb19SBen Gras 					} else {
2292fe8fb19SBen Gras 						fputs("; NSID: ", file);
2302fe8fb19SBen Gras 						for (i = 0; i < optlen; i++)
2312fe8fb19SBen Gras 							fprintf(file, "%02x ",
2322fe8fb19SBen Gras 								cp[i]);
2332fe8fb19SBen Gras 						fputs(" (",file);
2342fe8fb19SBen Gras 						for (i = 0; i < optlen; i++)
2352fe8fb19SBen Gras 							fprintf(file, "%c",
2362fe8fb19SBen Gras 								isprint(cp[i])?
2372fe8fb19SBen Gras 								cp[i] : '.');
2382fe8fb19SBen Gras 						fputs(")\n", file);
2392fe8fb19SBen Gras 					}
2402fe8fb19SBen Gras 				} else {
2412fe8fb19SBen Gras 					if (optlen == 0) {
2422fe8fb19SBen Gras 						fprintf(file, "; OPT=%u\n",
2432fe8fb19SBen Gras 							optcode);
2442fe8fb19SBen Gras 					} else {
2452fe8fb19SBen Gras 						fprintf(file, "; OPT=%u: ",
2462fe8fb19SBen Gras 							optcode);
2472fe8fb19SBen Gras 						for (i = 0; i < optlen; i++)
2482fe8fb19SBen Gras 							fprintf(file, "%02x ",
2492fe8fb19SBen Gras 								cp[i]);
2502fe8fb19SBen Gras 						fputs(" (",file);
2512fe8fb19SBen Gras 						for (i = 0; i < optlen; i++)
2522fe8fb19SBen Gras 							fprintf(file, "%c",
2532fe8fb19SBen Gras 								isprint(cp[i]) ?
2542fe8fb19SBen Gras 									cp[i] : '.');
2552fe8fb19SBen Gras 						fputs(")\n", file);
2562fe8fb19SBen Gras 					}
2572fe8fb19SBen Gras 				}
2582fe8fb19SBen Gras 				rdatalen -= 4 + optlen;
2592fe8fb19SBen Gras 			}
2602fe8fb19SBen Gras 		} else {
2612fe8fb19SBen Gras 			n = ns_sprintrr(handle, &rr, NULL, NULL,
2622fe8fb19SBen Gras 					buf, (u_int)buflen);
2632fe8fb19SBen Gras 			if (n < 0) {
2642fe8fb19SBen Gras 				if (errno == ENOSPC) {
2652fe8fb19SBen Gras 					free(buf);
2662fe8fb19SBen Gras 					buf = NULL;
2672fe8fb19SBen Gras 					if (buflen < 131072)
2682fe8fb19SBen Gras 						buf = malloc((size_t)(buflen += 1024));
2692fe8fb19SBen Gras 					if (buf == NULL) {
2702fe8fb19SBen Gras 						fprintf(file,
2712fe8fb19SBen Gras 					      ";; memory allocation failure\n");
2722fe8fb19SBen Gras 					      return;
2732fe8fb19SBen Gras 					}
2742fe8fb19SBen Gras 					continue;
2752fe8fb19SBen Gras 				}
2762fe8fb19SBen Gras 				fprintf(file, ";; ns_sprintrr: %s\n",
2772fe8fb19SBen Gras 					strerror(errno));
2782fe8fb19SBen Gras 				goto cleanup;
2792fe8fb19SBen Gras 			}
2802fe8fb19SBen Gras 			fputs(buf, file);
2812fe8fb19SBen Gras 			fputc('\n', file);
2822fe8fb19SBen Gras 		}
2832fe8fb19SBen Gras 		rrnum++;
2842fe8fb19SBen Gras 	}
2852fe8fb19SBen Gras  cleanup:
2862fe8fb19SBen Gras 	if (buf != NULL)
2872fe8fb19SBen Gras 		free(buf);
2882fe8fb19SBen Gras }
2892fe8fb19SBen Gras 
2902fe8fb19SBen Gras /*%
2912fe8fb19SBen Gras  * Print the contents of a query.
2922fe8fb19SBen Gras  * This is intended to be primarily a debugging routine.
2932fe8fb19SBen Gras  */
2942fe8fb19SBen Gras void
res_pquery(const res_state statp,const u_char * msg,int len,FILE * file)2952fe8fb19SBen Gras res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
2962fe8fb19SBen Gras 	ns_msg handle;
2972fe8fb19SBen Gras 	int qdcount, ancount, nscount, arcount;
2982fe8fb19SBen Gras 	u_int opcode, rcode, id;
2992fe8fb19SBen Gras 
3002fe8fb19SBen Gras 	if (ns_initparse(msg, len, &handle) < 0) {
3012fe8fb19SBen Gras 		fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
3022fe8fb19SBen Gras 		return;
3032fe8fb19SBen Gras 	}
3042fe8fb19SBen Gras 	opcode = ns_msg_getflag(handle, ns_f_opcode);
3052fe8fb19SBen Gras 	rcode = ns_msg_getflag(handle, ns_f_rcode);
3062fe8fb19SBen Gras 	id = ns_msg_id(handle);
3072fe8fb19SBen Gras 	qdcount = ns_msg_count(handle, ns_s_qd);
3082fe8fb19SBen Gras 	ancount = ns_msg_count(handle, ns_s_an);
3092fe8fb19SBen Gras 	nscount = ns_msg_count(handle, ns_s_ns);
3102fe8fb19SBen Gras 	arcount = ns_msg_count(handle, ns_s_ar);
3112fe8fb19SBen Gras 
3122fe8fb19SBen Gras 	/*
3132fe8fb19SBen Gras 	 * Print header fields.
3142fe8fb19SBen Gras 	 */
3152fe8fb19SBen Gras 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
3162fe8fb19SBen Gras 		fprintf(file,
3172fe8fb19SBen Gras 			";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
3182fe8fb19SBen Gras 			_res_opcodes[opcode], p_rcode((int)rcode), id);
3192fe8fb19SBen Gras 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
3202fe8fb19SBen Gras 		putc(';', file);
3212fe8fb19SBen Gras 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
3222fe8fb19SBen Gras 		fprintf(file, "; flags:");
3232fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_qr))
3242fe8fb19SBen Gras 			fprintf(file, " qr");
3252fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_aa))
3262fe8fb19SBen Gras 			fprintf(file, " aa");
3272fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_tc))
3282fe8fb19SBen Gras 			fprintf(file, " tc");
3292fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_rd))
3302fe8fb19SBen Gras 			fprintf(file, " rd");
3312fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_ra))
3322fe8fb19SBen Gras 			fprintf(file, " ra");
3332fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_z))
3342fe8fb19SBen Gras 			fprintf(file, " ??");
3352fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_ad))
3362fe8fb19SBen Gras 			fprintf(file, " ad");
3372fe8fb19SBen Gras 		if (ns_msg_getflag(handle, ns_f_cd))
3382fe8fb19SBen Gras 			fprintf(file, " cd");
3392fe8fb19SBen Gras 	}
3402fe8fb19SBen Gras 	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
3412fe8fb19SBen Gras 		fprintf(file, "; %s: %d",
3422fe8fb19SBen Gras 			p_section(ns_s_qd, (int)opcode), qdcount);
3432fe8fb19SBen Gras 		fprintf(file, ", %s: %d",
3442fe8fb19SBen Gras 			p_section(ns_s_an, (int)opcode), ancount);
3452fe8fb19SBen Gras 		fprintf(file, ", %s: %d",
3462fe8fb19SBen Gras 			p_section(ns_s_ns, (int)opcode), nscount);
3472fe8fb19SBen Gras 		fprintf(file, ", %s: %d",
3482fe8fb19SBen Gras 			p_section(ns_s_ar, (int)opcode), arcount);
3492fe8fb19SBen Gras 	}
3502fe8fb19SBen Gras 	if ((!statp->pfcode) || (statp->pfcode &
3512fe8fb19SBen Gras 		(RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
3522fe8fb19SBen Gras 		putc('\n',file);
3532fe8fb19SBen Gras 	}
3542fe8fb19SBen Gras 	/*
3552fe8fb19SBen Gras 	 * Print the various sections.
3562fe8fb19SBen Gras 	 */
3572fe8fb19SBen Gras 	do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
3582fe8fb19SBen Gras 	do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
3592fe8fb19SBen Gras 	do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
3602fe8fb19SBen Gras 	do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
3612fe8fb19SBen Gras 	if (qdcount == 0 && ancount == 0 &&
3622fe8fb19SBen Gras 	    nscount == 0 && arcount == 0)
3632fe8fb19SBen Gras 		putc('\n', file);
3642fe8fb19SBen Gras }
3652fe8fb19SBen Gras 
3662fe8fb19SBen Gras const u_char *
p_cdnname(const u_char * cp,const u_char * msg,int len,FILE * file)3672fe8fb19SBen Gras p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
3682fe8fb19SBen Gras 	char name[MAXDNAME];
3692fe8fb19SBen Gras 	int n;
3702fe8fb19SBen Gras 
371f14fb602SLionel Sambuc 	if ((n = dn_expand(msg, msg + len, cp, name, (int)sizeof name)) < 0)
3722fe8fb19SBen Gras 		return (NULL);
3732fe8fb19SBen Gras 	if (name[0] == '\0')
3742fe8fb19SBen Gras 		putc('.', file);
3752fe8fb19SBen Gras 	else
3762fe8fb19SBen Gras 		fputs(name, file);
3772fe8fb19SBen Gras 	return (cp + n);
3782fe8fb19SBen Gras }
3792fe8fb19SBen Gras 
3802fe8fb19SBen Gras const u_char *
p_cdname(const u_char * cp,const u_char * msg,FILE * file)3812fe8fb19SBen Gras p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
3822fe8fb19SBen Gras 	return (p_cdnname(cp, msg, PACKETSZ, file));
3832fe8fb19SBen Gras }
3842fe8fb19SBen Gras 
3852fe8fb19SBen Gras /*%
3862fe8fb19SBen Gras  * Return a fully-qualified domain name from a compressed name (with
3872fe8fb19SBen Gras    length supplied).  */
3882fe8fb19SBen Gras 
3892fe8fb19SBen Gras const u_char *
p_fqnname(const u_char * cp,const u_char * msg,int msglen,char * name,int namelen)390f14fb602SLionel Sambuc p_fqnname(const u_char *cp, const u_char *msg, int msglen, char *name,
391f14fb602SLionel Sambuc     int namelen)
3922fe8fb19SBen Gras {
393f14fb602SLionel Sambuc 	int n;
394f14fb602SLionel Sambuc 	size_t newlen;
3952fe8fb19SBen Gras 
3962fe8fb19SBen Gras 	if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
3972fe8fb19SBen Gras 		return (NULL);
3982fe8fb19SBen Gras 	newlen = strlen(name);
3992fe8fb19SBen Gras 	if (newlen == 0 || name[newlen - 1] != '.') {
400f14fb602SLionel Sambuc 		if ((int)newlen + 1 >= namelen)	/*%< Lack space for final dot */
4012fe8fb19SBen Gras 			return (NULL);
4022fe8fb19SBen Gras 		else
4032fe8fb19SBen Gras 			strcpy(name + newlen, ".");
4042fe8fb19SBen Gras 	}
4052fe8fb19SBen Gras 	return (cp + n);
4062fe8fb19SBen Gras }
4072fe8fb19SBen Gras 
4082fe8fb19SBen Gras /* XXX:	the rest of these functions need to become length-limited, too. */
4092fe8fb19SBen Gras 
4102fe8fb19SBen Gras const u_char *
p_fqname(const u_char * cp,const u_char * msg,FILE * file)4112fe8fb19SBen Gras p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
4122fe8fb19SBen Gras 	char name[MAXDNAME];
4132fe8fb19SBen Gras 	const u_char *n;
4142fe8fb19SBen Gras 
415f14fb602SLionel Sambuc 	n = p_fqnname(cp, msg, MAXCDNAME, name, (int)sizeof name);
4162fe8fb19SBen Gras 	if (n == NULL)
4172fe8fb19SBen Gras 		return (NULL);
4182fe8fb19SBen Gras 	fputs(name, file);
4192fe8fb19SBen Gras 	return (n);
4202fe8fb19SBen Gras }
4212fe8fb19SBen Gras 
4222fe8fb19SBen Gras /*%
4232fe8fb19SBen Gras  * Names of RR classes and qclasses.  Classes and qclasses are the same, except
4242fe8fb19SBen Gras  * that C_ANY is a qclass but not a class.  (You can ask for records of class
4252fe8fb19SBen Gras  * C_ANY, but you can't have any records of that class in the database.)
4262fe8fb19SBen Gras  */
4272fe8fb19SBen Gras const struct res_sym __p_class_syms[] = {
4282fe8fb19SBen Gras 	{C_IN,		"IN",		(char *)0},
4292fe8fb19SBen Gras 	{C_CHAOS,	"CH",		(char *)0},
4302fe8fb19SBen Gras 	{C_CHAOS,	"CHAOS",	(char *)0},
4312fe8fb19SBen Gras 	{C_HS,		"HS",		(char *)0},
4322fe8fb19SBen Gras 	{C_HS,		"HESIOD",	(char *)0},
4332fe8fb19SBen Gras 	{C_ANY,		"ANY",		(char *)0},
4342fe8fb19SBen Gras 	{C_NONE,	"NONE",		(char *)0},
4352fe8fb19SBen Gras 	{C_IN, 		(char *)0,	(char *)0}
4362fe8fb19SBen Gras };
4372fe8fb19SBen Gras 
4382fe8fb19SBen Gras /*%
4392fe8fb19SBen Gras  * Names of message sections.
4402fe8fb19SBen Gras  */
4412fe8fb19SBen Gras const struct res_sym __p_default_section_syms[] = {
4422fe8fb19SBen Gras 	{ns_s_qd,	"QUERY",	(char *)0},
4432fe8fb19SBen Gras 	{ns_s_an,	"ANSWER",	(char *)0},
4442fe8fb19SBen Gras 	{ns_s_ns,	"AUTHORITY",	(char *)0},
4452fe8fb19SBen Gras 	{ns_s_ar,	"ADDITIONAL",	(char *)0},
4462fe8fb19SBen Gras 	{0,		(char *)0,	(char *)0}
4472fe8fb19SBen Gras };
4482fe8fb19SBen Gras 
4492fe8fb19SBen Gras const struct res_sym __p_update_section_syms[] = {
4502fe8fb19SBen Gras 	{S_ZONE,	"ZONE",		(char *)0},
4512fe8fb19SBen Gras 	{S_PREREQ,	"PREREQUISITE",	(char *)0},
4522fe8fb19SBen Gras 	{S_UPDATE,	"UPDATE",	(char *)0},
4532fe8fb19SBen Gras 	{S_ADDT,	"ADDITIONAL",	(char *)0},
4542fe8fb19SBen Gras 	{0,		(char *)0,	(char *)0}
4552fe8fb19SBen Gras };
4562fe8fb19SBen Gras 
4572fe8fb19SBen Gras const struct res_sym __p_key_syms[] = {
4582fe8fb19SBen Gras 	{NS_ALG_MD5RSA,		"RSA",		"RSA KEY with MD5 hash"},
4592fe8fb19SBen Gras 	{NS_ALG_DH,		"DH",		"Diffie Hellman"},
4602fe8fb19SBen Gras 	{NS_ALG_DSA,		"DSA",		"Digital Signature Algorithm"},
4612fe8fb19SBen Gras 	{NS_ALG_EXPIRE_ONLY,	"EXPIREONLY",	"No algorithm"},
4622fe8fb19SBen Gras 	{NS_ALG_PRIVATE_OID,	"PRIVATE",	"Algorithm obtained from OID"},
4632fe8fb19SBen Gras 	{0,			NULL,		NULL}
4642fe8fb19SBen Gras };
4652fe8fb19SBen Gras 
4662fe8fb19SBen Gras const struct res_sym __p_cert_syms[] = {
4672fe8fb19SBen Gras 	{cert_t_pkix,	"PKIX",		"PKIX (X.509v3) Certificate"},
4682fe8fb19SBen Gras 	{cert_t_spki,	"SPKI",		"SPKI certificate"},
4692fe8fb19SBen Gras 	{cert_t_pgp,	"PGP",		"PGP certificate"},
4702fe8fb19SBen Gras 	{cert_t_url,	"URL",		"URL Private"},
4712fe8fb19SBen Gras 	{cert_t_oid,	"OID",		"OID Private"},
4722fe8fb19SBen Gras 	{0,		NULL,		NULL}
4732fe8fb19SBen Gras };
4742fe8fb19SBen Gras 
4752fe8fb19SBen Gras /*%
4762fe8fb19SBen Gras  * Names of RR types and qtypes.  Types and qtypes are the same, except
4772fe8fb19SBen Gras  * that T_ANY is a qtype but not a type.  (You can ask for records of type
4782fe8fb19SBen Gras  * T_ANY, but you can't have any records of that type in the database.)
4792fe8fb19SBen Gras  */
4802fe8fb19SBen Gras const struct res_sym __p_type_syms[] = {
4812fe8fb19SBen Gras 	{ns_t_a,	"A",		"address"},
4822fe8fb19SBen Gras 	{ns_t_ns,	"NS",		"name server"},
4832fe8fb19SBen Gras 	{ns_t_md,	"MD",		"mail destination (deprecated)"},
4842fe8fb19SBen Gras 	{ns_t_mf,	"MF",		"mail forwarder (deprecated)"},
4852fe8fb19SBen Gras 	{ns_t_cname,	"CNAME",	"canonical name"},
4862fe8fb19SBen Gras 	{ns_t_soa,	"SOA",		"start of authority"},
4872fe8fb19SBen Gras 	{ns_t_mb,	"MB",		"mailbox"},
4882fe8fb19SBen Gras 	{ns_t_mg,	"MG",		"mail group member"},
4892fe8fb19SBen Gras 	{ns_t_mr,	"MR",		"mail rename"},
4902fe8fb19SBen Gras 	{ns_t_null,	"NULL",		"null"},
4912fe8fb19SBen Gras 	{ns_t_wks,	"WKS",		"well-known service (deprecated)"},
4922fe8fb19SBen Gras 	{ns_t_ptr,	"PTR",		"domain name pointer"},
4932fe8fb19SBen Gras 	{ns_t_hinfo,	"HINFO",	"host information"},
4942fe8fb19SBen Gras 	{ns_t_minfo,	"MINFO",	"mailbox information"},
4952fe8fb19SBen Gras 	{ns_t_mx,	"MX",		"mail exchanger"},
4962fe8fb19SBen Gras 	{ns_t_txt,	"TXT",		"text"},
4972fe8fb19SBen Gras 	{ns_t_rp,	"RP",		"responsible person"},
4982fe8fb19SBen Gras 	{ns_t_afsdb,	"AFSDB",	"DCE or AFS server"},
4992fe8fb19SBen Gras 	{ns_t_x25,	"X25",		"X25 address"},
5002fe8fb19SBen Gras 	{ns_t_isdn,	"ISDN",		"ISDN address"},
5012fe8fb19SBen Gras 	{ns_t_rt,	"RT",		"router"},
5022fe8fb19SBen Gras 	{ns_t_nsap,	"NSAP",		"nsap address"},
5032fe8fb19SBen Gras 	{ns_t_nsap_ptr,	"NSAP_PTR",	"domain name pointer"},
5042fe8fb19SBen Gras 	{ns_t_sig,	"SIG",		"signature"},
5052fe8fb19SBen Gras 	{ns_t_key,	"KEY",		"key"},
5062fe8fb19SBen Gras 	{ns_t_px,	"PX",		"mapping information"},
5072fe8fb19SBen Gras 	{ns_t_gpos,	"GPOS",		"geographical position (withdrawn)"},
5082fe8fb19SBen Gras 	{ns_t_aaaa,	"AAAA",		"IPv6 address"},
5092fe8fb19SBen Gras 	{ns_t_loc,	"LOC",		"location"},
5102fe8fb19SBen Gras 	{ns_t_nxt,	"NXT",		"next valid name (unimplemented)"},
5112fe8fb19SBen Gras 	{ns_t_eid,	"EID",		"endpoint identifier (unimplemented)"},
5122fe8fb19SBen Gras 	{ns_t_nimloc,	"NIMLOC",	"NIMROD locator (unimplemented)"},
5132fe8fb19SBen Gras 	{ns_t_srv,	"SRV",		"server selection"},
5142fe8fb19SBen Gras 	{ns_t_atma,	"ATMA",		"ATM address (unimplemented)"},
5152fe8fb19SBen Gras 	{ns_t_naptr,	"NAPTR",	"naptr"},
5162fe8fb19SBen Gras 	{ns_t_kx,	"KX",		"key exchange"},
5172fe8fb19SBen Gras 	{ns_t_cert,	"CERT",		"certificate"},
5182fe8fb19SBen Gras 	{ns_t_a6,	"A",		"IPv6 address (experminental)"},
5192fe8fb19SBen Gras 	{ns_t_dname,	"DNAME",	"non-terminal redirection"},
5202fe8fb19SBen Gras 	{ns_t_opt,	"OPT",		"opt"},
5212fe8fb19SBen Gras 	{ns_t_apl,	"apl",		"apl"},
5222fe8fb19SBen Gras 	{ns_t_ds,	"DS",		"delegation signer"},
5232fe8fb19SBen Gras 	{ns_t_sshfp,	"SSFP",		"SSH fingerprint"},
5242fe8fb19SBen Gras 	{ns_t_ipseckey,	"IPSECKEY",	"IPSEC key"},
5252fe8fb19SBen Gras 	{ns_t_rrsig,	"RRSIG",	"rrsig"},
5262fe8fb19SBen Gras 	{ns_t_nsec,	"NSEC",		"nsec"},
5272fe8fb19SBen Gras 	{ns_t_dnskey,	"DNSKEY",	"DNS key"},
5282fe8fb19SBen Gras 	{ns_t_dhcid,	"DHCID",       "dynamic host configuration identifier"},
5292fe8fb19SBen Gras 	{ns_t_nsec3,	"NSEC3",	"nsec3"},
5302fe8fb19SBen Gras 	{ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
5312fe8fb19SBen Gras 	{ns_t_hip,	"HIP",		"host identity protocol"},
5322fe8fb19SBen Gras 	{ns_t_spf,	"SPF",		"sender policy framework"},
5332fe8fb19SBen Gras 	{ns_t_tkey,	"TKEY",		"tkey"},
5342fe8fb19SBen Gras 	{ns_t_tsig,	"TSIG",		"transaction signature"},
5352fe8fb19SBen Gras 	{ns_t_ixfr,	"IXFR",		"incremental zone transfer"},
5362fe8fb19SBen Gras 	{ns_t_axfr,	"AXFR",		"zone transfer"},
5372fe8fb19SBen Gras 	{ns_t_zxfr,	"ZXFR",		"compressed zone transfer"},
5382fe8fb19SBen Gras 	{ns_t_mailb,	"MAILB",	"mailbox-related data (deprecated)"},
5392fe8fb19SBen Gras 	{ns_t_maila,	"MAILA",	"mail agent (deprecated)"},
5402fe8fb19SBen Gras 	{ns_t_naptr,	"NAPTR",	"URN Naming Authority"},
5412fe8fb19SBen Gras 	{ns_t_kx,	"KX",		"Key Exchange"},
5422fe8fb19SBen Gras 	{ns_t_cert,	"CERT",		"Certificate"},
5432fe8fb19SBen Gras 	{ns_t_a6,	"A6",		"IPv6 Address"},
5442fe8fb19SBen Gras 	{ns_t_dname,	"DNAME",	"dname"},
5452fe8fb19SBen Gras 	{ns_t_sink,	"SINK",		"Kitchen Sink (experimental)"},
5462fe8fb19SBen Gras 	{ns_t_opt,	"OPT",		"EDNS Options"},
5472fe8fb19SBen Gras 	{ns_t_any,	"ANY",		"\"any\""},
5482fe8fb19SBen Gras 	{ns_t_dlv,	"DLV",		"DNSSEC look-aside validation"},
5492fe8fb19SBen Gras 	{0, 		NULL,		NULL}
5502fe8fb19SBen Gras };
5512fe8fb19SBen Gras 
5522fe8fb19SBen Gras /*%
5532fe8fb19SBen Gras  * Names of DNS rcodes.
5542fe8fb19SBen Gras  */
5552fe8fb19SBen Gras const struct res_sym __p_rcode_syms[] = {
5562fe8fb19SBen Gras 	{ns_r_noerror,	"NOERROR",		"no error"},
5572fe8fb19SBen Gras 	{ns_r_formerr,	"FORMERR",		"format error"},
5582fe8fb19SBen Gras 	{ns_r_servfail,	"SERVFAIL",		"server failed"},
5592fe8fb19SBen Gras 	{ns_r_nxdomain,	"NXDOMAIN",		"no such domain name"},
5602fe8fb19SBen Gras 	{ns_r_notimpl,	"NOTIMP",		"not implemented"},
5612fe8fb19SBen Gras 	{ns_r_refused,	"REFUSED",		"refused"},
5622fe8fb19SBen Gras 	{ns_r_yxdomain,	"YXDOMAIN",		"domain name exists"},
5632fe8fb19SBen Gras 	{ns_r_yxrrset,	"YXRRSET",		"rrset exists"},
5642fe8fb19SBen Gras 	{ns_r_nxrrset,	"NXRRSET",		"rrset doesn't exist"},
5652fe8fb19SBen Gras 	{ns_r_notauth,	"NOTAUTH",		"not authoritative"},
5662fe8fb19SBen Gras 	{ns_r_notzone,	"NOTZONE",		"Not in zone"},
5672fe8fb19SBen Gras 	{ns_r_max,	"",			""},
5682fe8fb19SBen Gras 	{ns_r_badsig,	"BADSIG",		"bad signature"},
5692fe8fb19SBen Gras 	{ns_r_badkey,	"BADKEY",		"bad key"},
5702fe8fb19SBen Gras 	{ns_r_badtime,	"BADTIME",		"bad time"},
5712fe8fb19SBen Gras 	{0, 		NULL,			NULL}
5722fe8fb19SBen Gras };
5732fe8fb19SBen Gras 
5742fe8fb19SBen Gras int
sym_ston(const struct res_sym * syms,const char * name,int * success)5752fe8fb19SBen Gras sym_ston(const struct res_sym *syms, const char *name, int *success) {
5762fe8fb19SBen Gras 	for (; syms->name != 0; syms++) {
5772fe8fb19SBen Gras 		if (strcasecmp (name, syms->name) == 0) {
5782fe8fb19SBen Gras 			if (success)
5792fe8fb19SBen Gras 				*success = 1;
5802fe8fb19SBen Gras 			return (syms->number);
5812fe8fb19SBen Gras 		}
5822fe8fb19SBen Gras 	}
5832fe8fb19SBen Gras 	if (success)
5842fe8fb19SBen Gras 		*success = 0;
5852fe8fb19SBen Gras 	return (syms->number);		/*%< The default value. */
5862fe8fb19SBen Gras }
5872fe8fb19SBen Gras 
5882fe8fb19SBen Gras const char *
sym_ntos(const struct res_sym * syms,int number,int * success)5892fe8fb19SBen Gras sym_ntos(const struct res_sym *syms, int number, int *success) {
5902fe8fb19SBen Gras 	char *unname = sym_ntos_unname;
5912fe8fb19SBen Gras 
5922fe8fb19SBen Gras 	for (; syms->name != 0; syms++) {
5932fe8fb19SBen Gras 		if (number == syms->number) {
5942fe8fb19SBen Gras 			if (success)
5952fe8fb19SBen Gras 				*success = 1;
5962fe8fb19SBen Gras 			return (syms->name);
5972fe8fb19SBen Gras 		}
5982fe8fb19SBen Gras 	}
5992fe8fb19SBen Gras 
6002fe8fb19SBen Gras 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6012fe8fb19SBen Gras 	if (success)
6022fe8fb19SBen Gras 		*success = 0;
6032fe8fb19SBen Gras 	return (unname);
6042fe8fb19SBen Gras }
6052fe8fb19SBen Gras 
6062fe8fb19SBen Gras const char *
sym_ntop(const struct res_sym * syms,int number,int * success)6072fe8fb19SBen Gras sym_ntop(const struct res_sym *syms, int number, int *success) {
6082fe8fb19SBen Gras 	char *unname = sym_ntop_unname;
6092fe8fb19SBen Gras 
6102fe8fb19SBen Gras 	for (; syms->name != 0; syms++) {
6112fe8fb19SBen Gras 		if (number == syms->number) {
6122fe8fb19SBen Gras 			if (success)
6132fe8fb19SBen Gras 				*success = 1;
6142fe8fb19SBen Gras 			return (syms->humanname);
6152fe8fb19SBen Gras 		}
6162fe8fb19SBen Gras 	}
6172fe8fb19SBen Gras 	sprintf(unname, "%d", number);		/*%< XXX nonreentrant */
6182fe8fb19SBen Gras 	if (success)
6192fe8fb19SBen Gras 		*success = 0;
6202fe8fb19SBen Gras 	return (unname);
6212fe8fb19SBen Gras }
6222fe8fb19SBen Gras 
6232fe8fb19SBen Gras /*%
6242fe8fb19SBen Gras  * Return a string for the type.
6252fe8fb19SBen Gras  */
6262fe8fb19SBen Gras const char *
p_type(int type)6272fe8fb19SBen Gras p_type(int type) {
6282fe8fb19SBen Gras 	int success;
6292fe8fb19SBen Gras 	const char *result;
6302fe8fb19SBen Gras 	static char typebuf[20];
6312fe8fb19SBen Gras 
6322fe8fb19SBen Gras 	result = sym_ntos(__p_type_syms, type, &success);
6332fe8fb19SBen Gras 	if (success)
6342fe8fb19SBen Gras 		return (result);
6352fe8fb19SBen Gras 	if (type < 0 || type > 0xffff)
6362fe8fb19SBen Gras 		return ("BADTYPE");
6372fe8fb19SBen Gras 	sprintf(typebuf, "TYPE%d", type);
6382fe8fb19SBen Gras 	return (typebuf);
6392fe8fb19SBen Gras }
6402fe8fb19SBen Gras 
6412fe8fb19SBen Gras /*%
6422fe8fb19SBen Gras  * Return a string for the type.
6432fe8fb19SBen Gras  */
6442fe8fb19SBen Gras const char *
p_section(int section,int opcode)6452fe8fb19SBen Gras p_section(int section, int opcode) {
6462fe8fb19SBen Gras 	const struct res_sym *symbols;
6472fe8fb19SBen Gras 
6482fe8fb19SBen Gras 	switch (opcode) {
6492fe8fb19SBen Gras 	case ns_o_update:
6502fe8fb19SBen Gras 		symbols = __p_update_section_syms;
6512fe8fb19SBen Gras 		break;
6522fe8fb19SBen Gras 	default:
6532fe8fb19SBen Gras 		symbols = __p_default_section_syms;
6542fe8fb19SBen Gras 		break;
6552fe8fb19SBen Gras 	}
6562fe8fb19SBen Gras 	return (sym_ntos(symbols, section, (int *)0));
6572fe8fb19SBen Gras }
6582fe8fb19SBen Gras 
6592fe8fb19SBen Gras /*%
6602fe8fb19SBen Gras  * Return a mnemonic for class.
6612fe8fb19SBen Gras  */
6622fe8fb19SBen Gras const char *
p_class(int class)6632fe8fb19SBen Gras p_class(int class) {
6642fe8fb19SBen Gras 	int success;
6652fe8fb19SBen Gras 	const char *result;
6662fe8fb19SBen Gras 	static char classbuf[20];
6672fe8fb19SBen Gras 
6682fe8fb19SBen Gras 	result = sym_ntos(__p_class_syms, class, &success);
6692fe8fb19SBen Gras 	if (success)
6702fe8fb19SBen Gras 		return (result);
6712fe8fb19SBen Gras 	if (class < 0 || class > 0xffff)
6722fe8fb19SBen Gras 		return ("BADCLASS");
6732fe8fb19SBen Gras 	sprintf(classbuf, "CLASS%d", class);
6742fe8fb19SBen Gras 	return (classbuf);
6752fe8fb19SBen Gras }
6762fe8fb19SBen Gras 
6772fe8fb19SBen Gras /*%
6782fe8fb19SBen Gras  * Return a mnemonic for an option
6792fe8fb19SBen Gras  */
6802fe8fb19SBen Gras const char *
p_option(u_long option)6812fe8fb19SBen Gras p_option(u_long option) {
6822fe8fb19SBen Gras 	char *nbuf = p_option_nbuf;
6832fe8fb19SBen Gras 
6842fe8fb19SBen Gras 	switch (option) {
6852fe8fb19SBen Gras 	case RES_INIT:		return "init";
6862fe8fb19SBen Gras 	case RES_DEBUG:		return "debug";
6872fe8fb19SBen Gras 	case RES_AAONLY:	return "aaonly(unimpl)";
6882fe8fb19SBen Gras 	case RES_USEVC:		return "usevc";
6892fe8fb19SBen Gras 	case RES_PRIMARY:	return "primry(unimpl)";
6902fe8fb19SBen Gras 	case RES_IGNTC:		return "igntc";
6912fe8fb19SBen Gras 	case RES_RECURSE:	return "recurs";
6922fe8fb19SBen Gras 	case RES_DEFNAMES:	return "defnam";
6932fe8fb19SBen Gras 	case RES_STAYOPEN:	return "styopn";
6942fe8fb19SBen Gras 	case RES_DNSRCH:	return "dnsrch";
6952fe8fb19SBen Gras 	case RES_INSECURE1:	return "insecure1";
6962fe8fb19SBen Gras 	case RES_INSECURE2:	return "insecure2";
6972fe8fb19SBen Gras 	case RES_NOALIASES:	return "noaliases";
6982fe8fb19SBen Gras 	case RES_USE_INET6:	return "inet6";
6992fe8fb19SBen Gras #ifdef RES_USE_EDNS0	/*%< KAME extension */
7002fe8fb19SBen Gras 	case RES_USE_EDNS0:	return "edns0";
7012fe8fb19SBen Gras 	case RES_NSID:		return "nsid";
7022fe8fb19SBen Gras #endif
7032fe8fb19SBen Gras #ifdef RES_USE_DNAME
7042fe8fb19SBen Gras 	case RES_USE_DNAME:	return "dname";
7052fe8fb19SBen Gras #endif
7062fe8fb19SBen Gras #ifdef RES_USE_DNSSEC
7072fe8fb19SBen Gras 	case RES_USE_DNSSEC:	return "dnssec";
7082fe8fb19SBen Gras #endif
7092fe8fb19SBen Gras #ifdef RES_NOTLDQUERY
7102fe8fb19SBen Gras 	case RES_NOTLDQUERY:	return "no-tld-query";
7112fe8fb19SBen Gras #endif
7122fe8fb19SBen Gras #ifdef RES_NO_NIBBLE2
7132fe8fb19SBen Gras 	case RES_NO_NIBBLE2:	return "no-nibble2";
7142fe8fb19SBen Gras #endif
7152fe8fb19SBen Gras 				/* XXX nonreentrant */
7162fe8fb19SBen Gras 	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
7172fe8fb19SBen Gras 				return (nbuf);
7182fe8fb19SBen Gras 	}
7192fe8fb19SBen Gras }
7202fe8fb19SBen Gras 
7212fe8fb19SBen Gras /*%
7222fe8fb19SBen Gras  * Return a mnemonic for a time to live.
7232fe8fb19SBen Gras  */
7242fe8fb19SBen Gras const char *
p_time(u_int32_t value)7252fe8fb19SBen Gras p_time(u_int32_t value) {
7262fe8fb19SBen Gras 	char *nbuf = p_time_nbuf;
7272fe8fb19SBen Gras 
7282fe8fb19SBen Gras 	if (ns_format_ttl((u_long)value, nbuf, sizeof nbuf) < 0)
7292fe8fb19SBen Gras 		sprintf(nbuf, "%u", value);
7302fe8fb19SBen Gras 	return (nbuf);
7312fe8fb19SBen Gras }
7322fe8fb19SBen Gras 
7332fe8fb19SBen Gras /*%
7342fe8fb19SBen Gras  * Return a string for the rcode.
7352fe8fb19SBen Gras  */
7362fe8fb19SBen Gras const char *
p_rcode(int rcode)7372fe8fb19SBen Gras p_rcode(int rcode) {
7382fe8fb19SBen Gras 	return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
7392fe8fb19SBen Gras }
7402fe8fb19SBen Gras 
7412fe8fb19SBen Gras /*%
7422fe8fb19SBen Gras  * Return a string for a res_sockaddr_union.
7432fe8fb19SBen Gras  */
7442fe8fb19SBen Gras const char *
p_sockun(union res_sockaddr_union u,char * buf,size_t size)7452fe8fb19SBen Gras p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
7462fe8fb19SBen Gras 	char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
7472fe8fb19SBen Gras 
7482fe8fb19SBen Gras 	switch (u.sin.sin_family) {
7492fe8fb19SBen Gras 	case AF_INET:
750f14fb602SLionel Sambuc 		inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t)sizeof ret);
7512fe8fb19SBen Gras 		break;
7522fe8fb19SBen Gras #ifdef HAS_INET6_STRUCTS
7532fe8fb19SBen Gras 	case AF_INET6:
7542fe8fb19SBen Gras 		inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
7552fe8fb19SBen Gras 		break;
7562fe8fb19SBen Gras #endif
7572fe8fb19SBen Gras 	default:
7582fe8fb19SBen Gras 		sprintf(ret, "[af%d]", u.sin.sin_family);
7592fe8fb19SBen Gras 		break;
7602fe8fb19SBen Gras 	}
7612fe8fb19SBen Gras 	if (size > 0U) {
7622fe8fb19SBen Gras 		strncpy(buf, ret, size - 1);
7632fe8fb19SBen Gras 		buf[size - 1] = '0';
7642fe8fb19SBen Gras 	}
7652fe8fb19SBen Gras 	return (buf);
7662fe8fb19SBen Gras }
7672fe8fb19SBen Gras 
7682fe8fb19SBen Gras /*%
7692fe8fb19SBen Gras  * routines to convert between on-the-wire RR format and zone file format.
7702fe8fb19SBen Gras  * Does not contain conversion to/from decimal degrees; divide or multiply
7712fe8fb19SBen Gras  * by 60*60*1000 for that.
7722fe8fb19SBen Gras  */
7732fe8fb19SBen Gras 
7742fe8fb19SBen Gras static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
7752fe8fb19SBen Gras 				      1000000,10000000,100000000,1000000000};
7762fe8fb19SBen Gras 
7772fe8fb19SBen Gras /*% takes an XeY precision/size value, returns a string representation. */
7782fe8fb19SBen Gras static const char *
precsize_ntoa(u_int32_t prec)7792fe8fb19SBen Gras precsize_ntoa(u_int32_t prec)
7802fe8fb19SBen Gras {
7812fe8fb19SBen Gras 	char *retbuf = precsize_ntoa_retbuf;
7822fe8fb19SBen Gras 	unsigned long val;
7832fe8fb19SBen Gras 	int mantissa, exponent;
7842fe8fb19SBen Gras 
7852fe8fb19SBen Gras 	mantissa = (int)((prec >> 4) & 0x0f) % 10;
7862fe8fb19SBen Gras 	exponent = (int)((prec >> 0) & 0x0f) % 10;
7872fe8fb19SBen Gras 
7882fe8fb19SBen Gras 	val = mantissa * poweroften[exponent];
7892fe8fb19SBen Gras 
7902fe8fb19SBen Gras 	(void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
7912fe8fb19SBen Gras 	return (retbuf);
7922fe8fb19SBen Gras }
7932fe8fb19SBen Gras 
7942fe8fb19SBen Gras /*% converts ascii size/precision X * 10**Y(cm) to 0xXY.  moves pointer.  */
7952fe8fb19SBen Gras static u_int8_t
precsize_aton(const char ** strptr)7962fe8fb19SBen Gras precsize_aton(const char **strptr) {
7972fe8fb19SBen Gras 	unsigned int mval = 0, cmval = 0;
7982fe8fb19SBen Gras 	u_int8_t retval = 0;
7992fe8fb19SBen Gras 	const char *cp;
8002fe8fb19SBen Gras 	int exponent;
8012fe8fb19SBen Gras 	int mantissa;
8022fe8fb19SBen Gras 
8032fe8fb19SBen Gras 	cp = *strptr;
8042fe8fb19SBen Gras 
8052fe8fb19SBen Gras 	while (isdigit((unsigned char)*cp))
8062fe8fb19SBen Gras 		mval = mval * 10 + (*cp++ - '0');
8072fe8fb19SBen Gras 
8082fe8fb19SBen Gras 	if (*cp == '.') {		/*%< centimeters */
8092fe8fb19SBen Gras 		cp++;
8102fe8fb19SBen Gras 		if (isdigit((unsigned char)*cp)) {
8112fe8fb19SBen Gras 			cmval = (*cp++ - '0') * 10;
8122fe8fb19SBen Gras 			if (isdigit((unsigned char)*cp)) {
8132fe8fb19SBen Gras 				cmval += (*cp++ - '0');
8142fe8fb19SBen Gras 			}
8152fe8fb19SBen Gras 		}
8162fe8fb19SBen Gras 	}
8172fe8fb19SBen Gras 	cmval = (mval * 100) + cmval;
8182fe8fb19SBen Gras 
8192fe8fb19SBen Gras 	for (exponent = 0; exponent < 9; exponent++)
8202fe8fb19SBen Gras 		if (cmval < poweroften[exponent+1])
8212fe8fb19SBen Gras 			break;
8222fe8fb19SBen Gras 
8232fe8fb19SBen Gras 	mantissa = cmval / poweroften[exponent];
8242fe8fb19SBen Gras 	if (mantissa > 9)
8252fe8fb19SBen Gras 		mantissa = 9;
8262fe8fb19SBen Gras 
8272fe8fb19SBen Gras 	retval = (mantissa << 4) | exponent;
8282fe8fb19SBen Gras 
8292fe8fb19SBen Gras 	*strptr = cp;
8302fe8fb19SBen Gras 
8312fe8fb19SBen Gras 	return (retval);
8322fe8fb19SBen Gras }
8332fe8fb19SBen Gras 
8342fe8fb19SBen Gras /*% converts ascii lat/lon to unsigned encoded 32-bit number.  moves pointer. */
8352fe8fb19SBen Gras static u_int32_t
latlon2ul(const char ** latlonstrptr,int * which)8362fe8fb19SBen Gras latlon2ul(const char **latlonstrptr, int *which) {
8372fe8fb19SBen Gras 	const char *cp;
8382fe8fb19SBen Gras 	u_int32_t retval;
8392fe8fb19SBen Gras 	int deg = 0, min = 0, secs = 0, secsfrac = 0;
8402fe8fb19SBen Gras 
8412fe8fb19SBen Gras 	cp = *latlonstrptr;
8422fe8fb19SBen Gras 
8432fe8fb19SBen Gras 	while (isdigit((unsigned char)*cp))
8442fe8fb19SBen Gras 		deg = deg * 10 + (*cp++ - '0');
8452fe8fb19SBen Gras 
8462fe8fb19SBen Gras 	while (isspace((unsigned char)*cp))
8472fe8fb19SBen Gras 		cp++;
8482fe8fb19SBen Gras 
8492fe8fb19SBen Gras 	if (!(isdigit((unsigned char)*cp)))
8502fe8fb19SBen Gras 		goto fndhemi;
8512fe8fb19SBen Gras 
8522fe8fb19SBen Gras 	while (isdigit((unsigned char)*cp))
8532fe8fb19SBen Gras 		min = min * 10 + (*cp++ - '0');
8542fe8fb19SBen Gras 
8552fe8fb19SBen Gras 	while (isspace((unsigned char)*cp))
8562fe8fb19SBen Gras 		cp++;
8572fe8fb19SBen Gras 
8582fe8fb19SBen Gras 	if (!(isdigit((unsigned char)*cp)))
8592fe8fb19SBen Gras 		goto fndhemi;
8602fe8fb19SBen Gras 
8612fe8fb19SBen Gras 	while (isdigit((unsigned char)*cp))
8622fe8fb19SBen Gras 		secs = secs * 10 + (*cp++ - '0');
8632fe8fb19SBen Gras 
8642fe8fb19SBen Gras 	if (*cp == '.') {		/*%< decimal seconds */
8652fe8fb19SBen Gras 		cp++;
8662fe8fb19SBen Gras 		if (isdigit((unsigned char)*cp)) {
8672fe8fb19SBen Gras 			secsfrac = (*cp++ - '0') * 100;
8682fe8fb19SBen Gras 			if (isdigit((unsigned char)*cp)) {
8692fe8fb19SBen Gras 				secsfrac += (*cp++ - '0') * 10;
8702fe8fb19SBen Gras 				if (isdigit((unsigned char)*cp)) {
8712fe8fb19SBen Gras 					secsfrac += (*cp++ - '0');
8722fe8fb19SBen Gras 				}
8732fe8fb19SBen Gras 			}
8742fe8fb19SBen Gras 		}
8752fe8fb19SBen Gras 	}
8762fe8fb19SBen Gras 
8772fe8fb19SBen Gras 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
8782fe8fb19SBen Gras 		cp++;
8792fe8fb19SBen Gras 
8802fe8fb19SBen Gras 	while (isspace((unsigned char)*cp))
8812fe8fb19SBen Gras 		cp++;
8822fe8fb19SBen Gras 
8832fe8fb19SBen Gras  fndhemi:
8842fe8fb19SBen Gras 	switch (*cp) {
8852fe8fb19SBen Gras 	case 'N': case 'n':
8862fe8fb19SBen Gras 	case 'E': case 'e':
8872fe8fb19SBen Gras 		retval = ((unsigned)1<<31)
8882fe8fb19SBen Gras 			+ (((((deg * 60) + min) * 60) + secs) * 1000)
8892fe8fb19SBen Gras 			+ secsfrac;
8902fe8fb19SBen Gras 		break;
8912fe8fb19SBen Gras 	case 'S': case 's':
8922fe8fb19SBen Gras 	case 'W': case 'w':
8932fe8fb19SBen Gras 		retval = ((unsigned)1<<31)
8942fe8fb19SBen Gras 			- (((((deg * 60) + min) * 60) + secs) * 1000)
8952fe8fb19SBen Gras 			- secsfrac;
8962fe8fb19SBen Gras 		break;
8972fe8fb19SBen Gras 	default:
8982fe8fb19SBen Gras 		retval = 0;	/*%< invalid value -- indicates error */
8992fe8fb19SBen Gras 		break;
9002fe8fb19SBen Gras 	}
9012fe8fb19SBen Gras 
9022fe8fb19SBen Gras 	switch (*cp) {
9032fe8fb19SBen Gras 	case 'N': case 'n':
9042fe8fb19SBen Gras 	case 'S': case 's':
9052fe8fb19SBen Gras 		*which = 1;	/*%< latitude */
9062fe8fb19SBen Gras 		break;
9072fe8fb19SBen Gras 	case 'E': case 'e':
9082fe8fb19SBen Gras 	case 'W': case 'w':
9092fe8fb19SBen Gras 		*which = 2;	/*%< longitude */
9102fe8fb19SBen Gras 		break;
9112fe8fb19SBen Gras 	default:
9122fe8fb19SBen Gras 		*which = 0;	/*%< error */
9132fe8fb19SBen Gras 		break;
9142fe8fb19SBen Gras 	}
9152fe8fb19SBen Gras 
9162fe8fb19SBen Gras 	cp++;			/*%< skip the hemisphere */
9172fe8fb19SBen Gras 	while (!isspace((unsigned char)*cp))	/*%< if any trailing garbage */
9182fe8fb19SBen Gras 		cp++;
9192fe8fb19SBen Gras 
9202fe8fb19SBen Gras 	while (isspace((unsigned char)*cp))	/*%< move to next field */
9212fe8fb19SBen Gras 		cp++;
9222fe8fb19SBen Gras 
9232fe8fb19SBen Gras 	*latlonstrptr = cp;
9242fe8fb19SBen Gras 
9252fe8fb19SBen Gras 	return (retval);
9262fe8fb19SBen Gras }
9272fe8fb19SBen Gras 
9282fe8fb19SBen Gras /*%
9292fe8fb19SBen Gras  * converts a zone file representation in a string to an RDATA on-the-wire
9302fe8fb19SBen Gras  * representation. */
9312fe8fb19SBen Gras int
loc_aton(const char * ascii,u_char * binary)932f14fb602SLionel Sambuc loc_aton(const char *ascii, u_char *binary)
9332fe8fb19SBen Gras {
9342fe8fb19SBen Gras 	const char *cp, *maxcp;
9352fe8fb19SBen Gras 	u_char *bcp;
9362fe8fb19SBen Gras 
9372fe8fb19SBen Gras 	u_int32_t latit = 0, longit = 0, alt = 0;
9382fe8fb19SBen Gras 	u_int32_t lltemp1 = 0, lltemp2 = 0;
9392fe8fb19SBen Gras 	int altmeters = 0, altfrac = 0, altsign = 1;
9402fe8fb19SBen Gras 	u_int8_t hp = 0x16;	/*%< default = 1e6 cm = 10000.00m = 10km */
9412fe8fb19SBen Gras 	u_int8_t vp = 0x13;	/*%< default = 1e3 cm = 10.00m */
9422fe8fb19SBen Gras 	u_int8_t siz = 0x12;	/*%< default = 1e2 cm = 1.00m */
9432fe8fb19SBen Gras 	int which1 = 0, which2 = 0;
9442fe8fb19SBen Gras 
9452fe8fb19SBen Gras 	cp = ascii;
9462fe8fb19SBen Gras 	maxcp = cp + strlen(ascii);
9472fe8fb19SBen Gras 
9482fe8fb19SBen Gras 	lltemp1 = latlon2ul(&cp, &which1);
9492fe8fb19SBen Gras 
9502fe8fb19SBen Gras 	lltemp2 = latlon2ul(&cp, &which2);
9512fe8fb19SBen Gras 
9522fe8fb19SBen Gras 	switch (which1 + which2) {
9532fe8fb19SBen Gras 	case 3:			/*%< 1 + 2, the only valid combination */
9542fe8fb19SBen Gras 		if ((which1 == 1) && (which2 == 2)) { /*%< normal case */
9552fe8fb19SBen Gras 			latit = lltemp1;
9562fe8fb19SBen Gras 			longit = lltemp2;
9572fe8fb19SBen Gras 		} else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */
9582fe8fb19SBen Gras 			longit = lltemp1;
9592fe8fb19SBen Gras 			latit = lltemp2;
9602fe8fb19SBen Gras 		} else {	/*%< some kind of brokenness */
9612fe8fb19SBen Gras 			return (0);
9622fe8fb19SBen Gras 		}
9632fe8fb19SBen Gras 		break;
9642fe8fb19SBen Gras 	default:		/*%< we didn't get one of each */
9652fe8fb19SBen Gras 		return (0);
9662fe8fb19SBen Gras 	}
9672fe8fb19SBen Gras 
9682fe8fb19SBen Gras 	/* altitude */
9692fe8fb19SBen Gras 	if (*cp == '-') {
9702fe8fb19SBen Gras 		altsign = -1;
9712fe8fb19SBen Gras 		cp++;
9722fe8fb19SBen Gras 	}
9732fe8fb19SBen Gras 
9742fe8fb19SBen Gras 	if (*cp == '+')
9752fe8fb19SBen Gras 		cp++;
9762fe8fb19SBen Gras 
9772fe8fb19SBen Gras 	while (isdigit((unsigned char)*cp))
9782fe8fb19SBen Gras 		altmeters = altmeters * 10 + (*cp++ - '0');
9792fe8fb19SBen Gras 
9802fe8fb19SBen Gras 	if (*cp == '.') {		/*%< decimal meters */
9812fe8fb19SBen Gras 		cp++;
9822fe8fb19SBen Gras 		if (isdigit((unsigned char)*cp)) {
9832fe8fb19SBen Gras 			altfrac = (*cp++ - '0') * 10;
9842fe8fb19SBen Gras 			if (isdigit((unsigned char)*cp)) {
9852fe8fb19SBen Gras 				altfrac += (*cp++ - '0');
9862fe8fb19SBen Gras 			}
9872fe8fb19SBen Gras 		}
9882fe8fb19SBen Gras 	}
9892fe8fb19SBen Gras 
9902fe8fb19SBen Gras 	alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
9912fe8fb19SBen Gras 
9922fe8fb19SBen Gras 	while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
9932fe8fb19SBen Gras 		cp++;
9942fe8fb19SBen Gras 
9952fe8fb19SBen Gras 	while (isspace((unsigned char)*cp) && (cp < maxcp))
9962fe8fb19SBen Gras 		cp++;
9972fe8fb19SBen Gras 
9982fe8fb19SBen Gras 	if (cp >= maxcp)
9992fe8fb19SBen Gras 		goto defaults;
10002fe8fb19SBen Gras 
10012fe8fb19SBen Gras 	siz = precsize_aton(&cp);
10022fe8fb19SBen Gras 
10032fe8fb19SBen Gras 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10042fe8fb19SBen Gras 		cp++;
10052fe8fb19SBen Gras 
10062fe8fb19SBen Gras 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10072fe8fb19SBen Gras 		cp++;
10082fe8fb19SBen Gras 
10092fe8fb19SBen Gras 	if (cp >= maxcp)
10102fe8fb19SBen Gras 		goto defaults;
10112fe8fb19SBen Gras 
10122fe8fb19SBen Gras 	hp = precsize_aton(&cp);
10132fe8fb19SBen Gras 
10142fe8fb19SBen Gras 	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/*%< if trailing garbage or m */
10152fe8fb19SBen Gras 		cp++;
10162fe8fb19SBen Gras 
10172fe8fb19SBen Gras 	while (isspace((unsigned char)*cp) && (cp < maxcp))
10182fe8fb19SBen Gras 		cp++;
10192fe8fb19SBen Gras 
10202fe8fb19SBen Gras 	if (cp >= maxcp)
10212fe8fb19SBen Gras 		goto defaults;
10222fe8fb19SBen Gras 
10232fe8fb19SBen Gras 	vp = precsize_aton(&cp);
10242fe8fb19SBen Gras 
10252fe8fb19SBen Gras  defaults:
10262fe8fb19SBen Gras 
10272fe8fb19SBen Gras 	bcp = binary;
10282fe8fb19SBen Gras 	*bcp++ = (u_int8_t) 0;	/*%< version byte */
10292fe8fb19SBen Gras 	*bcp++ = siz;
10302fe8fb19SBen Gras 	*bcp++ = hp;
10312fe8fb19SBen Gras 	*bcp++ = vp;
10322fe8fb19SBen Gras 	PUTLONG(latit,bcp);
10332fe8fb19SBen Gras 	PUTLONG(longit,bcp);
10342fe8fb19SBen Gras 	PUTLONG(alt,bcp);
10352fe8fb19SBen Gras 
10362fe8fb19SBen Gras 	return (16);		/*%< size of RR in octets */
10372fe8fb19SBen Gras }
10382fe8fb19SBen Gras 
10392fe8fb19SBen Gras /*% takes an on-the-wire LOC RR and formats it in a human readable format. */
10402fe8fb19SBen Gras const char *
loc_ntoa(const u_char * binary,char * ascii)1041f14fb602SLionel Sambuc loc_ntoa(const u_char *binary, char *ascii)
10422fe8fb19SBen Gras {
10432fe8fb19SBen Gras 	static const char *error = "?";
10442fe8fb19SBen Gras 	static char tmpbuf[sizeof
10452fe8fb19SBen Gras "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
10462fe8fb19SBen Gras 	const u_char *cp = binary;
10472fe8fb19SBen Gras 
10482fe8fb19SBen Gras 	int latdeg, latmin, latsec, latsecfrac;
10492fe8fb19SBen Gras 	int longdeg, longmin, longsec, longsecfrac;
10502fe8fb19SBen Gras 	char northsouth, eastwest;
10512fe8fb19SBen Gras 	const char *altsign;
10522fe8fb19SBen Gras 	int altmeters, altfrac;
10532fe8fb19SBen Gras 
10542fe8fb19SBen Gras 	const u_int32_t referencealt = 100000 * 100;
10552fe8fb19SBen Gras 
10562fe8fb19SBen Gras 	int32_t latval, longval, altval;
10572fe8fb19SBen Gras 	u_int32_t templ;
10582fe8fb19SBen Gras 	u_int8_t sizeval, hpval, vpval, versionval;
10592fe8fb19SBen Gras 
10602fe8fb19SBen Gras 	char *sizestr, *hpstr, *vpstr;
10612fe8fb19SBen Gras 
10622fe8fb19SBen Gras 	versionval = *cp++;
10632fe8fb19SBen Gras 
10642fe8fb19SBen Gras 	if (ascii == NULL)
10652fe8fb19SBen Gras 		ascii = tmpbuf;
10662fe8fb19SBen Gras 
10672fe8fb19SBen Gras 	if (versionval) {
10682fe8fb19SBen Gras 		(void) sprintf(ascii, "; error: unknown LOC RR version");
10692fe8fb19SBen Gras 		return (ascii);
10702fe8fb19SBen Gras 	}
10712fe8fb19SBen Gras 
10722fe8fb19SBen Gras 	sizeval = *cp++;
10732fe8fb19SBen Gras 
10742fe8fb19SBen Gras 	hpval = *cp++;
10752fe8fb19SBen Gras 	vpval = *cp++;
10762fe8fb19SBen Gras 
10772fe8fb19SBen Gras 	GETLONG(templ, cp);
10782fe8fb19SBen Gras 	latval = (templ - ((unsigned)1<<31));
10792fe8fb19SBen Gras 
10802fe8fb19SBen Gras 	GETLONG(templ, cp);
10812fe8fb19SBen Gras 	longval = (templ - ((unsigned)1<<31));
10822fe8fb19SBen Gras 
10832fe8fb19SBen Gras 	GETLONG(templ, cp);
10842fe8fb19SBen Gras 	if (templ < referencealt) { /*%< below WGS 84 spheroid */
10852fe8fb19SBen Gras 		altval = referencealt - templ;
10862fe8fb19SBen Gras 		altsign = "-";
10872fe8fb19SBen Gras 	} else {
10882fe8fb19SBen Gras 		altval = templ - referencealt;
10892fe8fb19SBen Gras 		altsign = "";
10902fe8fb19SBen Gras 	}
10912fe8fb19SBen Gras 
10922fe8fb19SBen Gras 	if (latval < 0) {
10932fe8fb19SBen Gras 		northsouth = 'S';
10942fe8fb19SBen Gras 		latval = -latval;
10952fe8fb19SBen Gras 	} else
10962fe8fb19SBen Gras 		northsouth = 'N';
10972fe8fb19SBen Gras 
10982fe8fb19SBen Gras 	latsecfrac = latval % 1000;
10992fe8fb19SBen Gras 	latval = latval / 1000;
11002fe8fb19SBen Gras 	latsec = latval % 60;
11012fe8fb19SBen Gras 	latval = latval / 60;
11022fe8fb19SBen Gras 	latmin = latval % 60;
11032fe8fb19SBen Gras 	latval = latval / 60;
11042fe8fb19SBen Gras 	latdeg = latval;
11052fe8fb19SBen Gras 
11062fe8fb19SBen Gras 	if (longval < 0) {
11072fe8fb19SBen Gras 		eastwest = 'W';
11082fe8fb19SBen Gras 		longval = -longval;
11092fe8fb19SBen Gras 	} else
11102fe8fb19SBen Gras 		eastwest = 'E';
11112fe8fb19SBen Gras 
11122fe8fb19SBen Gras 	longsecfrac = longval % 1000;
11132fe8fb19SBen Gras 	longval = longval / 1000;
11142fe8fb19SBen Gras 	longsec = longval % 60;
11152fe8fb19SBen Gras 	longval = longval / 60;
11162fe8fb19SBen Gras 	longmin = longval % 60;
11172fe8fb19SBen Gras 	longval = longval / 60;
11182fe8fb19SBen Gras 	longdeg = longval;
11192fe8fb19SBen Gras 
11202fe8fb19SBen Gras 	altfrac = altval % 100;
11212fe8fb19SBen Gras 	altmeters = (altval / 100);
11222fe8fb19SBen Gras 
11232fe8fb19SBen Gras 	sizestr = strdup(precsize_ntoa((u_int32_t)sizeval));
11242fe8fb19SBen Gras 	hpstr = strdup(precsize_ntoa((u_int32_t)hpval));
11252fe8fb19SBen Gras 	vpstr = strdup(precsize_ntoa((u_int32_t)vpval));
11262fe8fb19SBen Gras 
11272fe8fb19SBen Gras 	sprintf(ascii,
11282fe8fb19SBen Gras 	    "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
11292fe8fb19SBen Gras 		latdeg, latmin, latsec, latsecfrac, northsouth,
11302fe8fb19SBen Gras 		longdeg, longmin, longsec, longsecfrac, eastwest,
11312fe8fb19SBen Gras 		altsign, altmeters, altfrac,
11322fe8fb19SBen Gras 		(sizestr != NULL) ? sizestr : error,
11332fe8fb19SBen Gras 		(hpstr != NULL) ? hpstr : error,
11342fe8fb19SBen Gras 		(vpstr != NULL) ? vpstr : error);
11352fe8fb19SBen Gras 
11362fe8fb19SBen Gras 	if (sizestr != NULL)
11372fe8fb19SBen Gras 		free(sizestr);
11382fe8fb19SBen Gras 	if (hpstr != NULL)
11392fe8fb19SBen Gras 		free(hpstr);
11402fe8fb19SBen Gras 	if (vpstr != NULL)
11412fe8fb19SBen Gras 		free(vpstr);
11422fe8fb19SBen Gras 
11432fe8fb19SBen Gras 	return (ascii);
11442fe8fb19SBen Gras }
11452fe8fb19SBen Gras 
11462fe8fb19SBen Gras 
11472fe8fb19SBen Gras /*% Return the number of DNS hierarchy levels in the name. */
11482fe8fb19SBen Gras int
dn_count_labels(const char * name)11492fe8fb19SBen Gras dn_count_labels(const char *name) {
1150f14fb602SLionel Sambuc 	size_t len, i, count;
11512fe8fb19SBen Gras 
11522fe8fb19SBen Gras 	len = strlen(name);
11532fe8fb19SBen Gras 	for (i = 0, count = 0; i < len; i++) {
11542fe8fb19SBen Gras 		/* XXX need to check for \. or use named's nlabels(). */
11552fe8fb19SBen Gras 		if (name[i] == '.')
11562fe8fb19SBen Gras 			count++;
11572fe8fb19SBen Gras 	}
11582fe8fb19SBen Gras 
11592fe8fb19SBen Gras 	/* don't count initial wildcard */
11602fe8fb19SBen Gras 	if (name[0] == '*')
11612fe8fb19SBen Gras 		if (count)
11622fe8fb19SBen Gras 			count--;
11632fe8fb19SBen Gras 
11642fe8fb19SBen Gras 	/* don't count the null label for root. */
11652fe8fb19SBen Gras 	/* if terminating '.' not found, must adjust */
11662fe8fb19SBen Gras 	/* count to include last label */
11672fe8fb19SBen Gras 	if (len > 0 && name[len-1] != '.')
11682fe8fb19SBen Gras 		count++;
1169f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, count));
1170f14fb602SLionel Sambuc 	return (int)count;
11712fe8fb19SBen Gras }
11722fe8fb19SBen Gras 
11732fe8fb19SBen Gras /*%
11742fe8fb19SBen Gras  * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
11752fe8fb19SBen Gras  * SIG records are required to be printed like this, by the Secure DNS RFC.
11762fe8fb19SBen Gras  */
11772fe8fb19SBen Gras char *
p_secstodate(u_long secs)11782fe8fb19SBen Gras p_secstodate (u_long secs) {
11792fe8fb19SBen Gras 	/* XXX nonreentrant */
11802fe8fb19SBen Gras 	char *output = p_secstodate_output;
11812fe8fb19SBen Gras 	time_t myclock = secs;
11822fe8fb19SBen Gras 	struct tm *mytime;
11832fe8fb19SBen Gras #ifdef HAVE_TIME_R
11842fe8fb19SBen Gras 	struct tm res;
11852fe8fb19SBen Gras 
11862fe8fb19SBen Gras 	mytime = gmtime_r(&myclock, &res);
11872fe8fb19SBen Gras #else
11882fe8fb19SBen Gras 	mytime = gmtime(&myclock);
11892fe8fb19SBen Gras #endif
11902fe8fb19SBen Gras 	mytime->tm_year += 1900;
11912fe8fb19SBen Gras 	mytime->tm_mon += 1;
11922fe8fb19SBen Gras 	sprintf(output, "%04d%02d%02d%02d%02d%02d",
11932fe8fb19SBen Gras 		mytime->tm_year, mytime->tm_mon, mytime->tm_mday,
11942fe8fb19SBen Gras 		mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
11952fe8fb19SBen Gras 	return (output);
11962fe8fb19SBen Gras }
11972fe8fb19SBen Gras 
11982fe8fb19SBen Gras u_int16_t
res_nametoclass(const char * buf,int * successp)11992fe8fb19SBen Gras res_nametoclass(const char *buf, int *successp) {
12002fe8fb19SBen Gras 	unsigned long result;
12012fe8fb19SBen Gras 	char *endptr;
12022fe8fb19SBen Gras 	int success;
12032fe8fb19SBen Gras 
12042fe8fb19SBen Gras 	result = sym_ston(__p_class_syms, buf, &success);
12052fe8fb19SBen Gras 	if (success)
12062fe8fb19SBen Gras 		goto done;
12072fe8fb19SBen Gras 
12082fe8fb19SBen Gras 	if (strncasecmp(buf, "CLASS", 5) != 0 ||
12092fe8fb19SBen Gras 	    !isdigit((unsigned char)buf[5]))
12102fe8fb19SBen Gras 		goto done;
12112fe8fb19SBen Gras 	errno = 0;
12122fe8fb19SBen Gras 	result = strtoul(buf + 5, &endptr, 10);
12132fe8fb19SBen Gras 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12142fe8fb19SBen Gras 		success = 1;
12152fe8fb19SBen Gras  done:
12162fe8fb19SBen Gras 	if (successp)
12172fe8fb19SBen Gras 		*successp = success;
12182fe8fb19SBen Gras 	return (u_int16_t)(result);
12192fe8fb19SBen Gras }
12202fe8fb19SBen Gras 
12212fe8fb19SBen Gras u_int16_t
res_nametotype(const char * buf,int * successp)12222fe8fb19SBen Gras res_nametotype(const char *buf, int *successp) {
12232fe8fb19SBen Gras 	unsigned long result;
12242fe8fb19SBen Gras 	char *endptr;
12252fe8fb19SBen Gras 	int success;
12262fe8fb19SBen Gras 
12272fe8fb19SBen Gras 	result = sym_ston(__p_type_syms, buf, &success);
12282fe8fb19SBen Gras 	if (success)
12292fe8fb19SBen Gras 		goto done;
12302fe8fb19SBen Gras 
12312fe8fb19SBen Gras 	if (strncasecmp(buf, "type", 4) != 0 ||
12322fe8fb19SBen Gras 	    !isdigit((unsigned char)buf[4]))
12332fe8fb19SBen Gras 		goto done;
12342fe8fb19SBen Gras 	errno = 0;
12352fe8fb19SBen Gras 	result = strtoul(buf + 4, &endptr, 10);
12362fe8fb19SBen Gras 	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
12372fe8fb19SBen Gras 		success = 1;
12382fe8fb19SBen Gras  done:
12392fe8fb19SBen Gras 	if (successp)
12402fe8fb19SBen Gras 		*successp = success;
12412fe8fb19SBen Gras 	return (u_int16_t)(result);
12422fe8fb19SBen Gras }
12432fe8fb19SBen Gras 
12442fe8fb19SBen Gras /*! \file */
1245