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