res_debug.c revision 156956
1156952Sume/*
2156952Sume * Copyright (c) 1985
3156952Sume *    The Regents of the University of California.  All rights reserved.
4156952Sume *
5156952Sume * Redistribution and use in source and binary forms, with or without
6156952Sume * modification, are permitted provided that the following conditions
7156952Sume * are met:
8156952Sume * 1. Redistributions of source code must retain the above copyright
9156952Sume *    notice, this list of conditions and the following disclaimer.
10156952Sume * 2. Redistributions in binary form must reproduce the above copyright
11156952Sume *    notice, this list of conditions and the following disclaimer in the
12156952Sume *    documentation and/or other materials provided with the distribution.
13156952Sume * 3. All advertising materials mentioning features or use of this software
14156952Sume *    must display the following acknowledgement:
15156952Sume * 	This product includes software developed by the University of
16156952Sume * 	California, Berkeley and its contributors.
17156952Sume * 4. Neither the name of the University nor the names of its contributors
18156952Sume *    may be used to endorse or promote products derived from this software
19156952Sume *    without specific prior written permission.
20156952Sume *
21156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31156952Sume * SUCH DAMAGE.
32156952Sume */
33156952Sume
34156952Sume/*
35156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36156952Sume *
37156952Sume * Permission to use, copy, modify, and distribute this software for any
38156952Sume * purpose with or without fee is hereby granted, provided that the above
39156952Sume * copyright notice and this permission notice appear in all copies, and that
40156952Sume * the name of Digital Equipment Corporation not be used in advertising or
41156952Sume * publicity pertaining to distribution of the document or software without
42156952Sume * specific, written prior permission.
43156952Sume *
44156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46156952Sume * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
47156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51156952Sume * SOFTWARE.
52156952Sume */
53156952Sume
54156952Sume/*
55156952Sume * Portions Copyright (c) 1995 by International Business Machines, Inc.
56156952Sume *
57156952Sume * International Business Machines, Inc. (hereinafter called IBM) grants
58156952Sume * permission under its copyrights to use, copy, modify, and distribute this
59156952Sume * Software with or without fee, provided that the above copyright notice and
60156952Sume * all paragraphs of this notice appear in all copies, and that the name of IBM
61156952Sume * not be used in connection with the marketing of any product incorporating
62156952Sume * the Software or modifications thereof, without specific, written prior
63156952Sume * permission.
64156952Sume *
65156952Sume * To the extent it has a right to do so, IBM grants an immunity from suit
66156952Sume * under its patents, if any, for the use, sale or manufacture of products to
67156952Sume * the extent that such products are used for performing Domain Name System
68156952Sume * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
69156952Sume * granted for any product per se or for any other function of any product.
70156952Sume *
71156952Sume * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
72156952Sume * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
73156952Sume * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
74156952Sume * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
75156952Sume * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
76156952Sume * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
77156952Sume */
78156952Sume
79156952Sume/*
80156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
81156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
82156952Sume *
83156952Sume * Permission to use, copy, modify, and distribute this software for any
84156952Sume * purpose with or without fee is hereby granted, provided that the above
85156952Sume * copyright notice and this permission notice appear in all copies.
86156952Sume *
87156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
88156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
89156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
90156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
91156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
92156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
93156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
94156952Sume */
95156952Sume
96156952Sume#if defined(LIBC_SCCS) && !defined(lint)
97156952Sumestatic const char sccsid[] = "@(#)res_debug.c	8.1 (Berkeley) 6/4/93";
98156952Sumestatic const char rcsid[] = "$Id: res_debug.c,v 1.3.2.5.4.6 2005/07/28 07:43:22 marka Exp $";
99156952Sume#endif /* LIBC_SCCS and not lint */
100156956Sume#include <sys/cdefs.h>
101156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/res_debug.c 156956 2006-03-21 15:37:16Z ume $");
102156952Sume
103156952Sume#include "port_before.h"
104156952Sume
105156952Sume#include <sys/types.h>
106156952Sume#include <sys/param.h>
107156952Sume#include <sys/socket.h>
108156952Sume
109156952Sume#include <netinet/in.h>
110156952Sume#include <arpa/inet.h>
111156952Sume#include <arpa/nameser.h>
112156952Sume
113156952Sume#include <ctype.h>
114156952Sume#include <errno.h>
115156952Sume#include <math.h>
116156952Sume#include <netdb.h>
117156952Sume#include <resolv.h>
118156952Sume#include <resolv_mt.h>
119156952Sume#include <stdio.h>
120156952Sume#include <stdlib.h>
121156952Sume#include <string.h>
122156952Sume#include <time.h>
123156952Sume
124156952Sume#include "port_after.h"
125156952Sume
126156952Sume#ifdef SPRINTF_CHAR
127156952Sume# define SPRINTF(x) strlen(sprintf/**/x)
128156952Sume#else
129156952Sume# define SPRINTF(x) sprintf x
130156952Sume#endif
131156952Sume
132156952Sumeextern const char *_res_opcodes[];
133156952Sumeextern const char *_res_sectioncodes[];
134156952Sume
135156952Sume/*
136156952Sume * Print the current options.
137156952Sume */
138156952Sumevoid
139156952Sumefp_resstat(const res_state statp, FILE *file) {
140156952Sume	u_long mask;
141156952Sume
142156952Sume	fprintf(file, ";; res options:");
143156952Sume	for (mask = 1;  mask != 0U;  mask <<= 1)
144156952Sume		if (statp->options & mask)
145156952Sume			fprintf(file, " %s", p_option(mask));
146156952Sume	putc('\n', file);
147156952Sume}
148156952Sume
149156952Sumestatic void
150156952Sumedo_section(const res_state statp,
151156952Sume	   ns_msg *handle, ns_sect section,
152156952Sume	   int pflag, FILE *file)
153156952Sume{
154156952Sume	int n, sflag, rrnum;
155156952Sume	static int buflen = 2048;
156156952Sume	char *buf;
157156952Sume	ns_opcode opcode;
158156952Sume	ns_rr rr;
159156952Sume
160156952Sume	/*
161156952Sume	 * Print answer records.
162156952Sume	 */
163156952Sume	sflag = (statp->pfcode & pflag);
164156952Sume	if (statp->pfcode && !sflag)
165156952Sume		return;
166156952Sume
167156952Sume	buf = malloc(buflen);
168156952Sume	if (buf == NULL) {
169156952Sume		fprintf(file, ";; memory allocation failure\n");
170156952Sume		return;
171156952Sume	}
172156952Sume
173156952Sume	opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
174156952Sume	rrnum = 0;
175156952Sume	for (;;) {
176156952Sume		if (ns_parserr(handle, section, rrnum, &rr)) {
177156952Sume			if (errno != ENODEV)
178156952Sume				fprintf(file, ";; ns_parserr: %s\n",
179156952Sume					strerror(errno));
180156952Sume			else if (rrnum > 0 && sflag != 0 &&
181156952Sume				 (statp->pfcode & RES_PRF_HEAD1))
182156952Sume				putc('\n', file);
183156952Sume			goto cleanup;
184156952Sume		}
185156952Sume		if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
186156952Sume			fprintf(file, ";; %s SECTION:\n",
187156952Sume				p_section(section, opcode));
188156952Sume		if (section == ns_s_qd)
189156952Sume			fprintf(file, ";;\t%s, type = %s, class = %s\n",
190156952Sume				ns_rr_name(rr),
191156952Sume				p_type(ns_rr_type(rr)),
192156952Sume				p_class(ns_rr_class(rr)));
193156952Sume		else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
194156952Sume			u_int32_t ttl = ns_rr_ttl(rr);
195156952Sume			fprintf(file,
196156952Sume				"; EDNS: version: %u, udp=%u, flags=%04x\n",
197156952Sume				(ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
198156952Sume		} else {
199156952Sume			n = ns_sprintrr(handle, &rr, NULL, NULL,
200156952Sume					buf, buflen);
201156952Sume			if (n < 0) {
202156952Sume				if (errno == ENOSPC) {
203156952Sume					free(buf);
204156952Sume					buf = NULL;
205156952Sume					if (buflen < 131072)
206156952Sume						buf = malloc(buflen += 1024);
207156952Sume					if (buf == NULL) {
208156952Sume						fprintf(file,
209156952Sume				              ";; memory allocation failure\n");
210156952Sume					      return;
211156952Sume					}
212156952Sume					continue;
213156952Sume				}
214156952Sume				fprintf(file, ";; ns_sprintrr: %s\n",
215156952Sume					strerror(errno));
216156952Sume				goto cleanup;
217156952Sume			}
218156952Sume			fputs(buf, file);
219156952Sume			fputc('\n', file);
220156952Sume		}
221156952Sume		rrnum++;
222156952Sume	}
223156952Sume cleanup:
224156952Sume	if (buf != NULL)
225156952Sume		free(buf);
226156952Sume}
227156952Sume
228156952Sume/*
229156952Sume * Print the contents of a query.
230156952Sume * This is intended to be primarily a debugging routine.
231156952Sume */
232156952Sumevoid
233156952Sumeres_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
234156952Sume	ns_msg handle;
235156952Sume	int qdcount, ancount, nscount, arcount;
236156952Sume	u_int opcode, rcode, id;
237156952Sume
238156952Sume	if (ns_initparse(msg, len, &handle) < 0) {
239156952Sume		fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
240156952Sume		return;
241156952Sume	}
242156952Sume	opcode = ns_msg_getflag(handle, ns_f_opcode);
243156952Sume	rcode = ns_msg_getflag(handle, ns_f_rcode);
244156952Sume	id = ns_msg_id(handle);
245156952Sume	qdcount = ns_msg_count(handle, ns_s_qd);
246156952Sume	ancount = ns_msg_count(handle, ns_s_an);
247156952Sume	nscount = ns_msg_count(handle, ns_s_ns);
248156952Sume	arcount = ns_msg_count(handle, ns_s_ar);
249156952Sume
250156952Sume	/*
251156952Sume	 * Print header fields.
252156952Sume	 */
253156952Sume	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
254156952Sume		fprintf(file,
255156952Sume			";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
256156952Sume			_res_opcodes[opcode], p_rcode(rcode), id);
257156952Sume	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
258156952Sume		putc(';', file);
259156952Sume	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
260156952Sume		fprintf(file, "; flags:");
261156952Sume		if (ns_msg_getflag(handle, ns_f_qr))
262156952Sume			fprintf(file, " qr");
263156952Sume		if (ns_msg_getflag(handle, ns_f_aa))
264156952Sume			fprintf(file, " aa");
265156952Sume		if (ns_msg_getflag(handle, ns_f_tc))
266156952Sume			fprintf(file, " tc");
267156952Sume		if (ns_msg_getflag(handle, ns_f_rd))
268156952Sume			fprintf(file, " rd");
269156952Sume		if (ns_msg_getflag(handle, ns_f_ra))
270156952Sume			fprintf(file, " ra");
271156952Sume		if (ns_msg_getflag(handle, ns_f_z))
272156952Sume			fprintf(file, " ??");
273156952Sume		if (ns_msg_getflag(handle, ns_f_ad))
274156952Sume			fprintf(file, " ad");
275156952Sume		if (ns_msg_getflag(handle, ns_f_cd))
276156952Sume			fprintf(file, " cd");
277156952Sume	}
278156952Sume	if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
279156952Sume		fprintf(file, "; %s: %d",
280156952Sume			p_section(ns_s_qd, opcode), qdcount);
281156952Sume		fprintf(file, ", %s: %d",
282156952Sume			p_section(ns_s_an, opcode), ancount);
283156952Sume		fprintf(file, ", %s: %d",
284156952Sume			p_section(ns_s_ns, opcode), nscount);
285156952Sume		fprintf(file, ", %s: %d",
286156952Sume			p_section(ns_s_ar, opcode), arcount);
287156952Sume	}
288156952Sume	if ((!statp->pfcode) || (statp->pfcode &
289156952Sume		(RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
290156952Sume		putc('\n',file);
291156952Sume	}
292156952Sume	/*
293156952Sume	 * Print the various sections.
294156952Sume	 */
295156952Sume	do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
296156952Sume	do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
297156952Sume	do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
298156952Sume	do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
299156952Sume	if (qdcount == 0 && ancount == 0 &&
300156952Sume	    nscount == 0 && arcount == 0)
301156952Sume		putc('\n', file);
302156952Sume}
303156952Sume
304156952Sumeconst u_char *
305156952Sumep_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
306156952Sume	char name[MAXDNAME];
307156952Sume	int n;
308156952Sume
309156952Sume	if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
310156952Sume		return (NULL);
311156952Sume	if (name[0] == '\0')
312156952Sume		putc('.', file);
313156952Sume	else
314156952Sume		fputs(name, file);
315156952Sume	return (cp + n);
316156952Sume}
317156952Sume
318156952Sumeconst u_char *
319156952Sumep_cdname(const u_char *cp, const u_char *msg, FILE *file) {
320156952Sume	return (p_cdnname(cp, msg, PACKETSZ, file));
321156952Sume}
322156952Sume
323156952Sume/* Return a fully-qualified domain name from a compressed name (with
324156952Sume   length supplied).  */
325156952Sume
326156952Sumeconst u_char *
327156952Sumep_fqnname(cp, msg, msglen, name, namelen)
328156952Sume	const u_char *cp, *msg;
329156952Sume	int msglen;
330156952Sume	char *name;
331156952Sume	int namelen;
332156952Sume{
333156952Sume	int n, newlen;
334156952Sume
335156952Sume	if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
336156952Sume		return (NULL);
337156952Sume	newlen = strlen(name);
338156952Sume	if (newlen == 0 || name[newlen - 1] != '.') {
339156952Sume		if (newlen + 1 >= namelen)	/* Lack space for final dot */
340156952Sume			return (NULL);
341156952Sume		else
342156952Sume			strcpy(name + newlen, ".");
343156952Sume	}
344156952Sume	return (cp + n);
345156952Sume}
346156952Sume
347156952Sume/* XXX:	the rest of these functions need to become length-limited, too. */
348156952Sume
349156952Sumeconst u_char *
350156952Sumep_fqname(const u_char *cp, const u_char *msg, FILE *file) {
351156952Sume	char name[MAXDNAME];
352156952Sume	const u_char *n;
353156952Sume
354156952Sume	n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
355156952Sume	if (n == NULL)
356156952Sume		return (NULL);
357156952Sume	fputs(name, file);
358156952Sume	return (n);
359156952Sume}
360156952Sume
361156952Sume/*
362156952Sume * Names of RR classes and qclasses.  Classes and qclasses are the same, except
363156952Sume * that C_ANY is a qclass but not a class.  (You can ask for records of class
364156952Sume * C_ANY, but you can't have any records of that class in the database.)
365156952Sume */
366156952Sumeconst struct res_sym __p_class_syms[] = {
367156952Sume	{C_IN,		"IN",		(char *)0},
368156952Sume	{C_CHAOS,	"CH",		(char *)0},
369156952Sume	{C_CHAOS,	"CHAOS",	(char *)0},
370156952Sume	{C_HS,		"HS",		(char *)0},
371156952Sume	{C_HS,		"HESIOD",	(char *)0},
372156952Sume	{C_ANY,		"ANY",		(char *)0},
373156952Sume	{C_NONE,	"NONE",		(char *)0},
374156952Sume	{C_IN, 		(char *)0,	(char *)0}
375156952Sume};
376156952Sume
377156952Sume/*
378156952Sume * Names of message sections.
379156952Sume */
380156956Sumestatic const struct res_sym __p_default_section_syms[] = {
381156952Sume	{ns_s_qd,	"QUERY",	(char *)0},
382156952Sume	{ns_s_an,	"ANSWER",	(char *)0},
383156952Sume	{ns_s_ns,	"AUTHORITY",	(char *)0},
384156952Sume	{ns_s_ar,	"ADDITIONAL",	(char *)0},
385156952Sume	{0,             (char *)0,	(char *)0}
386156952Sume};
387156952Sume
388156956Sumestatic const struct res_sym __p_update_section_syms[] = {
389156952Sume	{S_ZONE,	"ZONE",		(char *)0},
390156952Sume	{S_PREREQ,	"PREREQUISITE",	(char *)0},
391156952Sume	{S_UPDATE,	"UPDATE",	(char *)0},
392156952Sume	{S_ADDT,	"ADDITIONAL",	(char *)0},
393156952Sume	{0,             (char *)0,	(char *)0}
394156952Sume};
395156952Sume
396156952Sumeconst struct res_sym __p_key_syms[] = {
397156952Sume	{NS_ALG_MD5RSA,		"RSA",		"RSA KEY with MD5 hash"},
398156952Sume	{NS_ALG_DH,		"DH",		"Diffie Hellman"},
399156952Sume	{NS_ALG_DSA,		"DSA",		"Digital Signature Algorithm"},
400156952Sume	{NS_ALG_EXPIRE_ONLY,	"EXPIREONLY",	"No algorithm"},
401156952Sume	{NS_ALG_PRIVATE_OID,	"PRIVATE",	"Algorithm obtained from OID"},
402156952Sume	{0,			NULL,		NULL}
403156952Sume};
404156952Sume
405156952Sumeconst struct res_sym __p_cert_syms[] = {
406156952Sume	{cert_t_pkix,	"PKIX",		"PKIX (X.509v3) Certificate"},
407156952Sume	{cert_t_spki,	"SPKI",		"SPKI certificate"},
408156952Sume	{cert_t_pgp,	"PGP",		"PGP certificate"},
409156952Sume	{cert_t_url,	"URL",		"URL Private"},
410156952Sume	{cert_t_oid,	"OID",		"OID Private"},
411156952Sume	{0,		NULL,		NULL}
412156952Sume};
413156952Sume
414156952Sume/*
415156952Sume * Names of RR types and qtypes.  Types and qtypes are the same, except
416156952Sume * that T_ANY is a qtype but not a type.  (You can ask for records of type
417156952Sume * T_ANY, but you can't have any records of that type in the database.)
418156952Sume */
419156952Sumeconst struct res_sym __p_type_syms[] = {
420156952Sume	{ns_t_a,	"A",		"address"},
421156952Sume	{ns_t_ns,	"NS",		"name server"},
422156952Sume	{ns_t_md,	"MD",		"mail destination (deprecated)"},
423156952Sume	{ns_t_mf,	"MF",		"mail forwarder (deprecated)"},
424156952Sume	{ns_t_cname,	"CNAME",	"canonical name"},
425156952Sume	{ns_t_soa,	"SOA",		"start of authority"},
426156952Sume	{ns_t_mb,	"MB",		"mailbox"},
427156952Sume	{ns_t_mg,	"MG",		"mail group member"},
428156952Sume	{ns_t_mr,	"MR",		"mail rename"},
429156952Sume	{ns_t_null,	"NULL",		"null"},
430156952Sume	{ns_t_wks,	"WKS",		"well-known service (deprecated)"},
431156952Sume	{ns_t_ptr,	"PTR",		"domain name pointer"},
432156952Sume	{ns_t_hinfo,	"HINFO",	"host information"},
433156952Sume	{ns_t_minfo,	"MINFO",	"mailbox information"},
434156952Sume	{ns_t_mx,	"MX",		"mail exchanger"},
435156952Sume	{ns_t_txt,	"TXT",		"text"},
436156952Sume	{ns_t_rp,	"RP",		"responsible person"},
437156952Sume	{ns_t_afsdb,	"AFSDB",	"DCE or AFS server"},
438156952Sume	{ns_t_x25,	"X25",		"X25 address"},
439156952Sume	{ns_t_isdn,	"ISDN",		"ISDN address"},
440156952Sume	{ns_t_rt,	"RT",		"router"},
441156952Sume	{ns_t_nsap,	"NSAP",		"nsap address"},
442156952Sume	{ns_t_nsap_ptr,	"NSAP_PTR",	"domain name pointer"},
443156952Sume	{ns_t_sig,	"SIG",		"signature"},
444156952Sume	{ns_t_key,	"KEY",		"key"},
445156952Sume	{ns_t_px,	"PX",		"mapping information"},
446156952Sume	{ns_t_gpos,	"GPOS",		"geographical position (withdrawn)"},
447156952Sume	{ns_t_aaaa,	"AAAA",		"IPv6 address"},
448156952Sume	{ns_t_loc,	"LOC",		"location"},
449156952Sume	{ns_t_nxt,	"NXT",		"next valid name (unimplemented)"},
450156952Sume	{ns_t_eid,	"EID",		"endpoint identifier (unimplemented)"},
451156952Sume	{ns_t_nimloc,	"NIMLOC",	"NIMROD locator (unimplemented)"},
452156952Sume	{ns_t_srv,	"SRV",		"server selection"},
453156952Sume	{ns_t_atma,	"ATMA",		"ATM address (unimplemented)"},
454156952Sume	{ns_t_tkey,	"TKEY",		"tkey"},
455156952Sume	{ns_t_tsig,	"TSIG",		"transaction signature"},
456156952Sume	{ns_t_ixfr,	"IXFR",		"incremental zone transfer"},
457156952Sume	{ns_t_axfr,	"AXFR",		"zone transfer"},
458156952Sume	{ns_t_zxfr,	"ZXFR",		"compressed zone transfer"},
459156952Sume	{ns_t_mailb,	"MAILB",	"mailbox-related data (deprecated)"},
460156952Sume	{ns_t_maila,	"MAILA",	"mail agent (deprecated)"},
461156952Sume	{ns_t_naptr,	"NAPTR",	"URN Naming Authority"},
462156952Sume	{ns_t_kx,	"KX",		"Key Exchange"},
463156952Sume	{ns_t_cert,	"CERT",		"Certificate"},
464156952Sume	{ns_t_a6,	"A6",		"IPv6 Address"},
465156952Sume	{ns_t_dname,	"DNAME",	"dname"},
466156952Sume	{ns_t_sink,	"SINK",		"Kitchen Sink (experimental)"},
467156952Sume	{ns_t_opt,	"OPT",		"EDNS Options"},
468156952Sume	{ns_t_any,	"ANY",		"\"any\""},
469156952Sume	{0, 		NULL,		NULL}
470156952Sume};
471156952Sume
472156952Sume/*
473156952Sume * Names of DNS rcodes.
474156952Sume */
475156956Sumestatic const struct res_sym __p_rcode_syms[] = {
476156952Sume	{ns_r_noerror,	"NOERROR",		"no error"},
477156952Sume	{ns_r_formerr,	"FORMERR",		"format error"},
478156952Sume	{ns_r_servfail,	"SERVFAIL",		"server failed"},
479156952Sume	{ns_r_nxdomain,	"NXDOMAIN",		"no such domain name"},
480156952Sume	{ns_r_notimpl,	"NOTIMP",		"not implemented"},
481156952Sume	{ns_r_refused,	"REFUSED",		"refused"},
482156952Sume	{ns_r_yxdomain,	"YXDOMAIN",		"domain name exists"},
483156952Sume	{ns_r_yxrrset,	"YXRRSET",		"rrset exists"},
484156952Sume	{ns_r_nxrrset,	"NXRRSET",		"rrset doesn't exist"},
485156952Sume	{ns_r_notauth,	"NOTAUTH",		"not authoritative"},
486156952Sume	{ns_r_notzone,	"NOTZONE",		"Not in zone"},
487156952Sume	{ns_r_max,	"",			""},
488156952Sume	{ns_r_badsig,	"BADSIG",		"bad signature"},
489156952Sume	{ns_r_badkey,	"BADKEY",		"bad key"},
490156952Sume	{ns_r_badtime,	"BADTIME",		"bad time"},
491156952Sume	{0, 		NULL,			NULL}
492156952Sume};
493156952Sume
494156952Sumeint
495156952Sumesym_ston(const struct res_sym *syms, const char *name, int *success) {
496156952Sume	for ((void)NULL; syms->name != 0; syms++) {
497156952Sume		if (strcasecmp (name, syms->name) == 0) {
498156952Sume			if (success)
499156952Sume				*success = 1;
500156952Sume			return (syms->number);
501156952Sume		}
502156952Sume	}
503156952Sume	if (success)
504156952Sume		*success = 0;
505156952Sume	return (syms->number);		/* The default value. */
506156952Sume}
507156952Sume
508156952Sumeconst char *
509156952Sumesym_ntos(const struct res_sym *syms, int number, int *success) {
510156952Sume	char *unname = sym_ntos_unname;
511156952Sume
512156952Sume	for ((void)NULL; syms->name != 0; syms++) {
513156952Sume		if (number == syms->number) {
514156952Sume			if (success)
515156952Sume				*success = 1;
516156952Sume			return (syms->name);
517156952Sume		}
518156952Sume	}
519156952Sume
520156952Sume	sprintf(unname, "%d", number);		/* XXX nonreentrant */
521156952Sume	if (success)
522156952Sume		*success = 0;
523156952Sume	return (unname);
524156952Sume}
525156952Sume
526156952Sumeconst char *
527156952Sumesym_ntop(const struct res_sym *syms, int number, int *success) {
528156952Sume	char *unname = sym_ntop_unname;
529156952Sume
530156952Sume	for ((void)NULL; syms->name != 0; syms++) {
531156952Sume		if (number == syms->number) {
532156952Sume			if (success)
533156952Sume				*success = 1;
534156952Sume			return (syms->humanname);
535156952Sume		}
536156952Sume	}
537156952Sume	sprintf(unname, "%d", number);		/* XXX nonreentrant */
538156952Sume	if (success)
539156952Sume		*success = 0;
540156952Sume	return (unname);
541156952Sume}
542156952Sume
543156952Sume/*
544156952Sume * Return a string for the type.
545156952Sume */
546156952Sumeconst char *
547156952Sumep_type(int type) {
548156952Sume	int success;
549156952Sume	const char *result;
550156952Sume	static char typebuf[20];
551156952Sume
552156952Sume	result = sym_ntos(__p_type_syms, type, &success);
553156952Sume	if (success)
554156952Sume		return (result);
555156952Sume	if (type < 0 || type > 0xffff)
556156952Sume		return ("BADTYPE");
557156952Sume	sprintf(typebuf, "TYPE%d", type);
558156952Sume	return (typebuf);
559156952Sume}
560156952Sume
561156952Sume/*
562156952Sume * Return a string for the type.
563156952Sume */
564156952Sumeconst char *
565156952Sumep_section(int section, int opcode) {
566156952Sume	const struct res_sym *symbols;
567156952Sume
568156952Sume	switch (opcode) {
569156952Sume	case ns_o_update:
570156952Sume		symbols = __p_update_section_syms;
571156952Sume		break;
572156952Sume	default:
573156952Sume		symbols = __p_default_section_syms;
574156952Sume		break;
575156952Sume	}
576156952Sume	return (sym_ntos(symbols, section, (int *)0));
577156952Sume}
578156952Sume
579156952Sume/*
580156952Sume * Return a mnemonic for class.
581156952Sume */
582156952Sumeconst char *
583156952Sumep_class(int class) {
584156952Sume	int success;
585156952Sume	const char *result;
586156952Sume	static char classbuf[20];
587156952Sume
588156952Sume	result = sym_ntos(__p_class_syms, class, &success);
589156952Sume	if (success)
590156952Sume		return (result);
591156952Sume	if (class < 0 || class > 0xffff)
592156952Sume		return ("BADCLASS");
593156952Sume	sprintf(classbuf, "CLASS%d", class);
594156952Sume	return (classbuf);
595156952Sume}
596156952Sume
597156952Sume/*
598156952Sume * Return a mnemonic for an option
599156952Sume */
600156952Sumeconst char *
601156952Sumep_option(u_long option) {
602156952Sume	char *nbuf = p_option_nbuf;
603156952Sume
604156952Sume	switch (option) {
605156952Sume	case RES_INIT:		return "init";
606156952Sume	case RES_DEBUG:		return "debug";
607156952Sume	case RES_AAONLY:	return "aaonly(unimpl)";
608156952Sume	case RES_USEVC:		return "usevc";
609156952Sume	case RES_PRIMARY:	return "primry(unimpl)";
610156952Sume	case RES_IGNTC:		return "igntc";
611156952Sume	case RES_RECURSE:	return "recurs";
612156952Sume	case RES_DEFNAMES:	return "defnam";
613156952Sume	case RES_STAYOPEN:	return "styopn";
614156952Sume	case RES_DNSRCH:	return "dnsrch";
615156952Sume	case RES_INSECURE1:	return "insecure1";
616156952Sume	case RES_INSECURE2:	return "insecure2";
617156952Sume	case RES_NOALIASES:	return "noaliases";
618156952Sume	case RES_USE_INET6:	return "inet6";
619156952Sume#ifdef RES_USE_EDNS0	/* KAME extension */
620156952Sume	case RES_USE_EDNS0:	return "edns0";
621156952Sume#endif
622156952Sume#ifdef RES_USE_DNAME
623156952Sume	case RES_USE_DNAME:	return "dname";
624156952Sume#endif
625156952Sume#ifdef RES_USE_DNSSEC
626156952Sume	case RES_USE_DNSSEC:	return "dnssec";
627156952Sume#endif
628156952Sume#ifdef RES_NOTLDQUERY
629156952Sume	case RES_NOTLDQUERY:	return "no-tld-query";
630156952Sume#endif
631156952Sume#ifdef RES_NO_NIBBLE2
632156952Sume	case RES_NO_NIBBLE2:	return "no-nibble2";
633156952Sume#endif
634156952Sume				/* XXX nonreentrant */
635156952Sume	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
636156952Sume				return (nbuf);
637156952Sume	}
638156952Sume}
639156952Sume
640156952Sume/*
641156952Sume * Return a mnemonic for a time to live.
642156952Sume */
643156952Sumeconst char *
644156952Sumep_time(u_int32_t value) {
645156952Sume	char *nbuf = p_time_nbuf;
646156952Sume
647156952Sume	if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
648156952Sume		sprintf(nbuf, "%u", value);
649156952Sume	return (nbuf);
650156952Sume}
651156952Sume
652156952Sume/*
653156952Sume * Return a string for the rcode.
654156952Sume */
655156952Sumeconst char *
656156952Sumep_rcode(int rcode) {
657156952Sume	return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
658156952Sume}
659156952Sume
660156952Sume/*
661156952Sume * Return a string for a res_sockaddr_union.
662156952Sume */
663156952Sumeconst char *
664156952Sumep_sockun(union res_sockaddr_union u, char *buf, size_t size) {
665156952Sume	char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
666156952Sume
667156952Sume	switch (u.sin.sin_family) {
668156952Sume	case AF_INET:
669156952Sume		inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
670156952Sume		break;
671156952Sume#ifdef HAS_INET6_STRUCTS
672156952Sume	case AF_INET6:
673156952Sume		inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
674156952Sume		break;
675156952Sume#endif
676156952Sume	default:
677156952Sume		sprintf(ret, "[af%d]", u.sin.sin_family);
678156952Sume		break;
679156952Sume	}
680156952Sume	if (size > 0U) {
681156952Sume		strncpy(buf, ret, size - 1);
682156952Sume		buf[size - 1] = '0';
683156952Sume	}
684156952Sume	return (buf);
685156952Sume}
686156952Sume
687156952Sume/*
688156952Sume * routines to convert between on-the-wire RR format and zone file format.
689156952Sume * Does not contain conversion to/from decimal degrees; divide or multiply
690156952Sume * by 60*60*1000 for that.
691156952Sume */
692156952Sume
693156952Sumestatic unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
694156952Sume				      1000000,10000000,100000000,1000000000};
695156952Sume
696156952Sume/* takes an XeY precision/size value, returns a string representation. */
697156952Sumestatic const char *
698156952Sumeprecsize_ntoa(prec)
699156952Sume	u_int8_t prec;
700156952Sume{
701156952Sume	char *retbuf = precsize_ntoa_retbuf;
702156952Sume	unsigned long val;
703156952Sume	int mantissa, exponent;
704156952Sume
705156952Sume	mantissa = (int)((prec >> 4) & 0x0f) % 10;
706156952Sume	exponent = (int)((prec >> 0) & 0x0f) % 10;
707156952Sume
708156952Sume	val = mantissa * poweroften[exponent];
709156952Sume
710156952Sume	(void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
711156952Sume	return (retbuf);
712156952Sume}
713156952Sume
714156952Sume/* converts ascii size/precision X * 10**Y(cm) to 0xXY.  moves pointer. */
715156952Sumestatic u_int8_t
716156952Sumeprecsize_aton(const char **strptr) {
717156952Sume	unsigned int mval = 0, cmval = 0;
718156952Sume	u_int8_t retval = 0;
719156952Sume	const char *cp;
720156952Sume	int exponent;
721156952Sume	int mantissa;
722156952Sume
723156952Sume	cp = *strptr;
724156952Sume
725156952Sume	while (isdigit((unsigned char)*cp))
726156952Sume		mval = mval * 10 + (*cp++ - '0');
727156952Sume
728156952Sume	if (*cp == '.') {		/* centimeters */
729156952Sume		cp++;
730156952Sume		if (isdigit((unsigned char)*cp)) {
731156952Sume			cmval = (*cp++ - '0') * 10;
732156952Sume			if (isdigit((unsigned char)*cp)) {
733156952Sume				cmval += (*cp++ - '0');
734156952Sume			}
735156952Sume		}
736156952Sume	}
737156952Sume	cmval = (mval * 100) + cmval;
738156952Sume
739156952Sume	for (exponent = 0; exponent < 9; exponent++)
740156952Sume		if (cmval < poweroften[exponent+1])
741156952Sume			break;
742156952Sume
743156952Sume	mantissa = cmval / poweroften[exponent];
744156952Sume	if (mantissa > 9)
745156952Sume		mantissa = 9;
746156952Sume
747156952Sume	retval = (mantissa << 4) | exponent;
748156952Sume
749156952Sume	*strptr = cp;
750156952Sume
751156952Sume	return (retval);
752156952Sume}
753156952Sume
754156952Sume/* converts ascii lat/lon to unsigned encoded 32-bit number.  moves pointer. */
755156952Sumestatic u_int32_t
756156952Sumelatlon2ul(const char **latlonstrptr, int *which) {
757156952Sume	const char *cp;
758156952Sume	u_int32_t retval;
759156952Sume	int deg = 0, min = 0, secs = 0, secsfrac = 0;
760156952Sume
761156952Sume	cp = *latlonstrptr;
762156952Sume
763156952Sume	while (isdigit((unsigned char)*cp))
764156952Sume		deg = deg * 10 + (*cp++ - '0');
765156952Sume
766156952Sume	while (isspace((unsigned char)*cp))
767156952Sume		cp++;
768156952Sume
769156952Sume	if (!(isdigit((unsigned char)*cp)))
770156952Sume		goto fndhemi;
771156952Sume
772156952Sume	while (isdigit((unsigned char)*cp))
773156952Sume		min = min * 10 + (*cp++ - '0');
774156952Sume
775156952Sume	while (isspace((unsigned char)*cp))
776156952Sume		cp++;
777156952Sume
778156952Sume	if (!(isdigit((unsigned char)*cp)))
779156952Sume		goto fndhemi;
780156952Sume
781156952Sume	while (isdigit((unsigned char)*cp))
782156952Sume		secs = secs * 10 + (*cp++ - '0');
783156952Sume
784156952Sume	if (*cp == '.') {		/* decimal seconds */
785156952Sume		cp++;
786156952Sume		if (isdigit((unsigned char)*cp)) {
787156952Sume			secsfrac = (*cp++ - '0') * 100;
788156952Sume			if (isdigit((unsigned char)*cp)) {
789156952Sume				secsfrac += (*cp++ - '0') * 10;
790156952Sume				if (isdigit((unsigned char)*cp)) {
791156952Sume					secsfrac += (*cp++ - '0');
792156952Sume				}
793156952Sume			}
794156952Sume		}
795156952Sume	}
796156952Sume
797156952Sume	while (!isspace((unsigned char)*cp))	/* if any trailing garbage */
798156952Sume		cp++;
799156952Sume
800156952Sume	while (isspace((unsigned char)*cp))
801156952Sume		cp++;
802156952Sume
803156952Sume fndhemi:
804156952Sume	switch (*cp) {
805156952Sume	case 'N': case 'n':
806156952Sume	case 'E': case 'e':
807156952Sume		retval = ((unsigned)1<<31)
808156952Sume			+ (((((deg * 60) + min) * 60) + secs) * 1000)
809156952Sume			+ secsfrac;
810156952Sume		break;
811156952Sume	case 'S': case 's':
812156952Sume	case 'W': case 'w':
813156952Sume		retval = ((unsigned)1<<31)
814156952Sume			- (((((deg * 60) + min) * 60) + secs) * 1000)
815156952Sume			- secsfrac;
816156952Sume		break;
817156952Sume	default:
818156952Sume		retval = 0;	/* invalid value -- indicates error */
819156952Sume		break;
820156952Sume	}
821156952Sume
822156952Sume	switch (*cp) {
823156952Sume	case 'N': case 'n':
824156952Sume	case 'S': case 's':
825156952Sume		*which = 1;	/* latitude */
826156952Sume		break;
827156952Sume	case 'E': case 'e':
828156952Sume	case 'W': case 'w':
829156952Sume		*which = 2;	/* longitude */
830156952Sume		break;
831156952Sume	default:
832156952Sume		*which = 0;	/* error */
833156952Sume		break;
834156952Sume	}
835156952Sume
836156952Sume	cp++;			/* skip the hemisphere */
837156952Sume
838156952Sume	while (!isspace((unsigned char)*cp))	/* if any trailing garbage */
839156952Sume		cp++;
840156952Sume
841156952Sume	while (isspace((unsigned char)*cp))	/* move to next field */
842156952Sume		cp++;
843156952Sume
844156952Sume	*latlonstrptr = cp;
845156952Sume
846156952Sume	return (retval);
847156952Sume}
848156952Sume
849156952Sume/* converts a zone file representation in a string to an RDATA on-the-wire
850156952Sume * representation. */
851156952Sumeint
852156952Sumeloc_aton(ascii, binary)
853156952Sume	const char *ascii;
854156952Sume	u_char *binary;
855156952Sume{
856156952Sume	const char *cp, *maxcp;
857156952Sume	u_char *bcp;
858156952Sume
859156952Sume	u_int32_t latit = 0, longit = 0, alt = 0;
860156952Sume	u_int32_t lltemp1 = 0, lltemp2 = 0;
861156952Sume	int altmeters = 0, altfrac = 0, altsign = 1;
862156952Sume	u_int8_t hp = 0x16;	/* default = 1e6 cm = 10000.00m = 10km */
863156952Sume	u_int8_t vp = 0x13;	/* default = 1e3 cm = 10.00m */
864156952Sume	u_int8_t siz = 0x12;	/* default = 1e2 cm = 1.00m */
865156952Sume	int which1 = 0, which2 = 0;
866156952Sume
867156952Sume	cp = ascii;
868156952Sume	maxcp = cp + strlen(ascii);
869156952Sume
870156952Sume	lltemp1 = latlon2ul(&cp, &which1);
871156952Sume
872156952Sume	lltemp2 = latlon2ul(&cp, &which2);
873156952Sume
874156952Sume	switch (which1 + which2) {
875156952Sume	case 3:			/* 1 + 2, the only valid combination */
876156952Sume		if ((which1 == 1) && (which2 == 2)) { /* normal case */
877156952Sume			latit = lltemp1;
878156952Sume			longit = lltemp2;
879156952Sume		} else if ((which1 == 2) && (which2 == 1)) { /* reversed */
880156952Sume			longit = lltemp1;
881156952Sume			latit = lltemp2;
882156952Sume		} else {	/* some kind of brokenness */
883156952Sume			return (0);
884156952Sume		}
885156952Sume		break;
886156952Sume	default:		/* we didn't get one of each */
887156952Sume		return (0);
888156952Sume	}
889156952Sume
890156952Sume	/* altitude */
891156952Sume	if (*cp == '-') {
892156952Sume		altsign = -1;
893156952Sume		cp++;
894156952Sume	}
895156952Sume
896156952Sume	if (*cp == '+')
897156952Sume		cp++;
898156952Sume
899156952Sume	while (isdigit((unsigned char)*cp))
900156952Sume		altmeters = altmeters * 10 + (*cp++ - '0');
901156952Sume
902156952Sume	if (*cp == '.') {		/* decimal meters */
903156952Sume		cp++;
904156952Sume		if (isdigit((unsigned char)*cp)) {
905156952Sume			altfrac = (*cp++ - '0') * 10;
906156952Sume			if (isdigit((unsigned char)*cp)) {
907156952Sume				altfrac += (*cp++ - '0');
908156952Sume			}
909156952Sume		}
910156952Sume	}
911156952Sume
912156952Sume	alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
913156952Sume
914156952Sume	while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
915156952Sume		cp++;
916156952Sume
917156952Sume	while (isspace((unsigned char)*cp) && (cp < maxcp))
918156952Sume		cp++;
919156952Sume
920156952Sume	if (cp >= maxcp)
921156952Sume		goto defaults;
922156952Sume
923156952Sume	siz = precsize_aton(&cp);
924156952Sume
925156952Sume	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/* if trailing garbage or m */
926156952Sume		cp++;
927156952Sume
928156952Sume	while (isspace((unsigned char)*cp) && (cp < maxcp))
929156952Sume		cp++;
930156952Sume
931156952Sume	if (cp >= maxcp)
932156952Sume		goto defaults;
933156952Sume
934156952Sume	hp = precsize_aton(&cp);
935156952Sume
936156952Sume	while (!isspace((unsigned char)*cp) && (cp < maxcp))	/* if trailing garbage or m */
937156952Sume		cp++;
938156952Sume
939156952Sume	while (isspace((unsigned char)*cp) && (cp < maxcp))
940156952Sume		cp++;
941156952Sume
942156952Sume	if (cp >= maxcp)
943156952Sume		goto defaults;
944156952Sume
945156952Sume	vp = precsize_aton(&cp);
946156952Sume
947156952Sume defaults:
948156952Sume
949156952Sume	bcp = binary;
950156952Sume	*bcp++ = (u_int8_t) 0;	/* version byte */
951156952Sume	*bcp++ = siz;
952156952Sume	*bcp++ = hp;
953156952Sume	*bcp++ = vp;
954156952Sume	PUTLONG(latit,bcp);
955156952Sume	PUTLONG(longit,bcp);
956156952Sume	PUTLONG(alt,bcp);
957156952Sume
958156952Sume	return (16);		/* size of RR in octets */
959156952Sume}
960156952Sume
961156952Sume/* takes an on-the-wire LOC RR and formats it in a human readable format. */
962156952Sumeconst char *
963156952Sumeloc_ntoa(binary, ascii)
964156952Sume	const u_char *binary;
965156952Sume	char *ascii;
966156952Sume{
967156952Sume	static const char *error = "?";
968156952Sume	static char tmpbuf[sizeof
969156952Sume"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
970156952Sume	const u_char *cp = binary;
971156952Sume
972156952Sume	int latdeg, latmin, latsec, latsecfrac;
973156952Sume	int longdeg, longmin, longsec, longsecfrac;
974156952Sume	char northsouth, eastwest;
975156952Sume	const char *altsign;
976156952Sume	int altmeters, altfrac;
977156952Sume
978156952Sume	const u_int32_t referencealt = 100000 * 100;
979156952Sume
980156952Sume	int32_t latval, longval, altval;
981156952Sume	u_int32_t templ;
982156952Sume	u_int8_t sizeval, hpval, vpval, versionval;
983156952Sume
984156952Sume	char *sizestr, *hpstr, *vpstr;
985156952Sume
986156952Sume	versionval = *cp++;
987156952Sume
988156952Sume	if (ascii == NULL)
989156952Sume		ascii = tmpbuf;
990156952Sume
991156952Sume	if (versionval) {
992156952Sume		(void) sprintf(ascii, "; error: unknown LOC RR version");
993156952Sume		return (ascii);
994156952Sume	}
995156952Sume
996156952Sume	sizeval = *cp++;
997156952Sume
998156952Sume	hpval = *cp++;
999156952Sume	vpval = *cp++;
1000156952Sume
1001156952Sume	GETLONG(templ, cp);
1002156952Sume	latval = (templ - ((unsigned)1<<31));
1003156952Sume
1004156952Sume	GETLONG(templ, cp);
1005156952Sume	longval = (templ - ((unsigned)1<<31));
1006156952Sume
1007156952Sume	GETLONG(templ, cp);
1008156952Sume	if (templ < referencealt) { /* below WGS 84 spheroid */
1009156952Sume		altval = referencealt - templ;
1010156952Sume		altsign = "-";
1011156952Sume	} else {
1012156952Sume		altval = templ - referencealt;
1013156952Sume		altsign = "";
1014156952Sume	}
1015156952Sume
1016156952Sume	if (latval < 0) {
1017156952Sume		northsouth = 'S';
1018156952Sume		latval = -latval;
1019156952Sume	} else
1020156952Sume		northsouth = 'N';
1021156952Sume
1022156952Sume	latsecfrac = latval % 1000;
1023156952Sume	latval = latval / 1000;
1024156952Sume	latsec = latval % 60;
1025156952Sume	latval = latval / 60;
1026156952Sume	latmin = latval % 60;
1027156952Sume	latval = latval / 60;
1028156952Sume	latdeg = latval;
1029156952Sume
1030156952Sume	if (longval < 0) {
1031156952Sume		eastwest = 'W';
1032156952Sume		longval = -longval;
1033156952Sume	} else
1034156952Sume		eastwest = 'E';
1035156952Sume
1036156952Sume	longsecfrac = longval % 1000;
1037156952Sume	longval = longval / 1000;
1038156952Sume	longsec = longval % 60;
1039156952Sume	longval = longval / 60;
1040156952Sume	longmin = longval % 60;
1041156952Sume	longval = longval / 60;
1042156952Sume	longdeg = longval;
1043156952Sume
1044156952Sume	altfrac = altval % 100;
1045156952Sume	altmeters = (altval / 100);
1046156952Sume
1047156952Sume	sizestr = strdup(precsize_ntoa(sizeval));
1048156952Sume	hpstr = strdup(precsize_ntoa(hpval));
1049156952Sume	vpstr = strdup(precsize_ntoa(vpval));
1050156952Sume
1051156952Sume	sprintf(ascii,
1052156952Sume	    "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
1053156952Sume		latdeg, latmin, latsec, latsecfrac, northsouth,
1054156952Sume		longdeg, longmin, longsec, longsecfrac, eastwest,
1055156952Sume		altsign, altmeters, altfrac,
1056156952Sume		(sizestr != NULL) ? sizestr : error,
1057156952Sume		(hpstr != NULL) ? hpstr : error,
1058156952Sume		(vpstr != NULL) ? vpstr : error);
1059156952Sume
1060156952Sume	if (sizestr != NULL)
1061156952Sume		free(sizestr);
1062156952Sume	if (hpstr != NULL)
1063156952Sume		free(hpstr);
1064156952Sume	if (vpstr != NULL)
1065156952Sume		free(vpstr);
1066156952Sume
1067156952Sume	return (ascii);
1068156952Sume}
1069156952Sume
1070156952Sume
1071156952Sume/* Return the number of DNS hierarchy levels in the name. */
1072156952Sumeint
1073156952Sumedn_count_labels(const char *name) {
1074156952Sume	int i, len, count;
1075156952Sume
1076156952Sume	len = strlen(name);
1077156952Sume	for (i = 0, count = 0; i < len; i++) {
1078156952Sume		/* XXX need to check for \. or use named's nlabels(). */
1079156952Sume		if (name[i] == '.')
1080156952Sume			count++;
1081156952Sume	}
1082156952Sume
1083156952Sume	/* don't count initial wildcard */
1084156952Sume	if (name[0] == '*')
1085156952Sume		if (count)
1086156952Sume			count--;
1087156952Sume
1088156952Sume	/* don't count the null label for root. */
1089156952Sume	/* if terminating '.' not found, must adjust */
1090156952Sume	/* count to include last label */
1091156952Sume	if (len > 0 && name[len-1] != '.')
1092156952Sume		count++;
1093156952Sume	return (count);
1094156952Sume}
1095156952Sume
1096156952Sume
1097156952Sume/*
1098156952Sume * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1099156952Sume * SIG records are required to be printed like this, by the Secure DNS RFC.
1100156952Sume */
1101156952Sumechar *
1102156952Sumep_secstodate (u_long secs) {
1103156952Sume	char *output = p_secstodate_output;
1104156952Sume	time_t clock = secs;
1105156952Sume	struct tm *time;
1106156952Sume#ifdef HAVE_TIME_R
1107156952Sume	struct tm res;
1108156952Sume
1109156952Sume	time = gmtime_r(&clock, &res);
1110156952Sume#else
1111156952Sume	time = gmtime(&clock);
1112156952Sume#endif
1113156952Sume	time->tm_year += 1900;
1114156952Sume	time->tm_mon += 1;
1115156952Sume	sprintf(output, "%04d%02d%02d%02d%02d%02d",
1116156952Sume		time->tm_year, time->tm_mon, time->tm_mday,
1117156952Sume		time->tm_hour, time->tm_min, time->tm_sec);
1118156952Sume	return (output);
1119156952Sume}
1120156952Sume
1121156952Sumeu_int16_t
1122156952Sumeres_nametoclass(const char *buf, int *successp) {
1123156952Sume	unsigned long result;
1124156952Sume	char *endptr;
1125156952Sume	int success;
1126156952Sume
1127156952Sume	result = sym_ston(__p_class_syms, buf, &success);
1128156952Sume	if (success)
1129156952Sume		goto done;
1130156952Sume
1131156952Sume	if (strncasecmp(buf, "CLASS", 5) != 0 ||
1132156952Sume	    !isdigit((unsigned char)buf[5]))
1133156952Sume		goto done;
1134156952Sume	errno = 0;
1135156952Sume	result = strtoul(buf + 5, &endptr, 10);
1136156952Sume	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
1137156952Sume		success = 1;
1138156952Sume done:
1139156952Sume	if (successp)
1140156952Sume		*successp = success;
1141156952Sume	return (result);
1142156952Sume}
1143156952Sume
1144156952Sumeu_int16_t
1145156952Sumeres_nametotype(const char *buf, int *successp) {
1146156952Sume	unsigned long result;
1147156952Sume	char *endptr;
1148156952Sume	int success;
1149156952Sume
1150156952Sume	result = sym_ston(__p_type_syms, buf, &success);
1151156952Sume	if (success)
1152156952Sume		goto done;
1153156952Sume
1154156952Sume	if (strncasecmp(buf, "type", 4) != 0 ||
1155156952Sume	    !isdigit((unsigned char)buf[4]))
1156156952Sume		goto done;
1157156952Sume	errno = 0;
1158156952Sume	result = strtoul(buf + 4, &endptr, 10);
1159156952Sume	if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
1160156952Sume		success = 1;
1161156952Sume done:
1162156952Sume	if (successp)
1163156952Sume		*successp = success;
1164156952Sume	return (result);
1165156952Sume}
1166156956Sume
1167156956Sume/*
1168156956Sume * Weak aliases for applications that use certain private entry points,
1169156956Sume * and fail to include <resolv.h>.
1170156956Sume */
1171156956Sume#undef fp_resstat
1172156956Sume__weak_reference(__fp_resstat, fp_resstat);
1173156956Sume#undef p_fqnname
1174156956Sume__weak_reference(__p_fqnname, p_fqnname);
1175156956Sume#undef sym_ston
1176156956Sume__weak_reference(__sym_ston, sym_ston);
1177156956Sume#undef sym_ntos
1178156956Sume__weak_reference(__sym_ntos, sym_ntos);
1179156956Sume#undef sym_ntop
1180156956Sume__weak_reference(__sym_ntop, sym_ntop);
1181156956Sume#undef dn_count_labels
1182156956Sume__weak_reference(__dn_count_labels, dn_count_labels);
1183156956Sume#undef p_secstodate
1184156956Sume__weak_reference(__p_secstodate, p_secstodate);
1185