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