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