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