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