Fix country_name in nds_NL
[glibc.git] / resolv / res_debug.c
blob919b86e2b37dc15055aab98ccba46a4e5da0d593
1 /*
2 * Copyright (c) 1985
3 * The Regents of the University of California. All rights reserved.
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.
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.
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.
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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.
51 * Portions Copyright (c) 1995 by International Business Machines, Inc.
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.
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.
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.
76 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
92 #include <sys/types.h>
93 #include <sys/param.h>
94 #include <sys/socket.h>
96 #include <netinet/in.h>
97 #include <arpa/inet.h>
98 #include <arpa/nameser.h>
100 #include <ctype.h>
101 #include <errno.h>
102 #include <math.h>
103 #include <netdb.h>
104 #include <resolv/resolv-internal.h>
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <string.h>
108 #include <time.h>
109 #include <shlib-compat.h>
111 #ifdef SPRINTF_CHAR
112 # define SPRINTF(x) strlen(sprintf/**/x)
113 #else
114 # define SPRINTF(x) sprintf x
115 #endif
117 extern const char *_res_sectioncodes[] attribute_hidden;
119 /* _res_opcodes was exported by accident as a variable. */
120 #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26)
121 static const char *res_opcodes[] =
122 #else
123 static const char res_opcodes[][9] =
124 #endif
126 "QUERY",
127 "IQUERY",
128 "CQUERYM",
129 "CQUERYU", /* experimental */
130 "NOTIFY", /* experimental */
131 "UPDATE",
132 "6",
133 "7",
134 "8",
135 "9",
136 "10",
137 "11",
138 "12",
139 "13",
140 "ZONEINIT",
141 "ZONEREF",
143 #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26)
144 strong_alias (res_opcodes, _res_opcodes)
145 #endif
147 static const char *p_section(int section, int opcode);
150 * Print the current options.
152 void
153 fp_resstat(const res_state statp, FILE *file) {
154 u_long mask;
156 fprintf(file, ";; res options:");
157 for (mask = 1; mask != 0; mask <<= 1)
158 if (statp->options & mask)
159 fprintf(file, " %s", p_option(mask));
160 putc('\n', file);
163 static void
164 do_section (int pfcode, ns_msg *handle, ns_sect section, int pflag, FILE *file)
166 int n, sflag, rrnum;
167 static int buflen = 2048;
168 char *buf;
169 ns_opcode opcode;
170 ns_rr rr;
173 * Print answer records.
175 sflag = (pfcode & pflag);
176 if (pfcode && !sflag)
177 return;
179 buf = malloc(buflen);
180 if (buf == NULL) {
181 fprintf(file, ";; memory allocation failure\n");
182 return;
185 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
186 rrnum = 0;
187 for (;;) {
188 if (ns_parserr(handle, section, rrnum, &rr)) {
189 if (errno != ENODEV)
190 fprintf(file, ";; ns_parserr: %s\n",
191 strerror(errno));
192 else if (rrnum > 0 && sflag != 0 &&
193 (pfcode & RES_PRF_HEAD1))
194 putc('\n', file);
195 goto cleanup;
197 if (rrnum == 0 && sflag != 0 && (pfcode & RES_PRF_HEAD1))
198 fprintf(file, ";; %s SECTION:\n",
199 p_section(section, opcode));
200 if (section == ns_s_qd)
201 fprintf(file, ";;\t%s, type = %s, class = %s\n",
202 ns_rr_name(rr),
203 p_type(ns_rr_type(rr)),
204 p_class(ns_rr_class(rr)));
205 else {
206 n = ns_sprintrr(handle, &rr, NULL, NULL,
207 buf, buflen);
208 if (n < 0) {
209 if (errno == ENOSPC) {
210 free(buf);
211 buf = NULL;
212 if (buflen < 131072)
213 buf = malloc(buflen += 1024);
214 if (buf == NULL) {
215 fprintf(file,
216 ";; memory allocation failure\n");
217 return;
219 continue;
221 fprintf(file, ";; ns_sprintrr: %s\n",
222 strerror(errno));
223 goto cleanup;
225 fputs(buf, file);
226 fputc('\n', file);
228 rrnum++;
230 cleanup:
231 free(buf);
235 * Print the contents of a query.
236 * This is intended to be primarily a debugging routine.
238 void
239 fp_nquery (const unsigned char *msg, int len, FILE *file)
241 ns_msg handle;
242 int qdcount, ancount, nscount, arcount;
243 u_int opcode, rcode, id;
245 /* There is no need to initialize _res: If _res is not yet
246 initialized, _res.pfcode is zero. But initialization will
247 leave it at zero, too. _res.pfcode is an unsigned long,
248 but the code here assumes that the flags fit into an int,
249 so use that. */
250 int pfcode = _res.pfcode;
252 if (ns_initparse(msg, len, &handle) < 0) {
253 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
254 return;
256 opcode = ns_msg_getflag(handle, ns_f_opcode);
257 rcode = ns_msg_getflag(handle, ns_f_rcode);
258 id = ns_msg_id(handle);
259 qdcount = ns_msg_count(handle, ns_s_qd);
260 ancount = ns_msg_count(handle, ns_s_an);
261 nscount = ns_msg_count(handle, ns_s_ns);
262 arcount = ns_msg_count(handle, ns_s_ar);
265 * Print header fields.
267 if ((!pfcode) || (pfcode & RES_PRF_HEADX) || rcode)
268 fprintf(file,
269 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
270 res_opcodes[opcode], p_rcode(rcode), id);
271 if ((!pfcode) || (pfcode & RES_PRF_HEADX))
272 putc(';', file);
273 if ((!pfcode) || (pfcode & RES_PRF_HEAD2)) {
274 fprintf(file, "; flags:");
275 if (ns_msg_getflag(handle, ns_f_qr))
276 fprintf(file, " qr");
277 if (ns_msg_getflag(handle, ns_f_aa))
278 fprintf(file, " aa");
279 if (ns_msg_getflag(handle, ns_f_tc))
280 fprintf(file, " tc");
281 if (ns_msg_getflag(handle, ns_f_rd))
282 fprintf(file, " rd");
283 if (ns_msg_getflag(handle, ns_f_ra))
284 fprintf(file, " ra");
285 if (ns_msg_getflag(handle, ns_f_z))
286 fprintf(file, " ??");
287 if (ns_msg_getflag(handle, ns_f_ad))
288 fprintf(file, " ad");
289 if (ns_msg_getflag(handle, ns_f_cd))
290 fprintf(file, " cd");
292 if ((!pfcode) || (pfcode & RES_PRF_HEAD1)) {
293 fprintf(file, "; %s: %d",
294 p_section(ns_s_qd, opcode), qdcount);
295 fprintf(file, ", %s: %d",
296 p_section(ns_s_an, opcode), ancount);
297 fprintf(file, ", %s: %d",
298 p_section(ns_s_ns, opcode), nscount);
299 fprintf(file, ", %s: %d",
300 p_section(ns_s_ar, opcode), arcount);
302 if ((!pfcode) || (pfcode &
303 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
304 putc('\n',file);
307 * Print the various sections.
309 do_section (pfcode, &handle, ns_s_qd, RES_PRF_QUES, file);
310 do_section (pfcode, &handle, ns_s_an, RES_PRF_ANS, file);
311 do_section (pfcode, &handle, ns_s_ns, RES_PRF_AUTH, file);
312 do_section (pfcode, &handle, ns_s_ar, RES_PRF_ADD, file);
313 if (qdcount == 0 && ancount == 0 &&
314 nscount == 0 && arcount == 0)
315 putc('\n', file);
317 libresolv_hidden_def (fp_nquery)
319 void
320 fp_query (const unsigned char *msg, FILE *file)
322 fp_nquery (msg, PACKETSZ, file);
324 libresolv_hidden_def (fp_query)
326 void
327 p_query (const unsigned char *msg)
329 fp_query (msg, stdout);
332 const u_char *
333 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
334 char name[MAXDNAME];
335 int n;
337 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
338 return (NULL);
339 if (name[0] == '\0')
340 putc('.', file);
341 else
342 fputs(name, file);
343 return (cp + n);
345 libresolv_hidden_def (p_cdnname)
347 const u_char *
348 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
349 return (p_cdnname(cp, msg, PACKETSZ, file));
352 /* Return a fully-qualified domain name from a compressed name (with
353 length supplied). */
355 const u_char *
356 p_fqnname (const u_char *cp, const u_char *msg, int msglen, char *name,
357 int namelen)
359 int n, newlen;
361 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
362 return (NULL);
363 newlen = strlen(name);
364 if (newlen == 0 || name[newlen - 1] != '.') {
365 if (newlen + 1 >= namelen) /* Lack space for final dot */
366 return (NULL);
367 else
368 strcpy(name + newlen, ".");
370 return (cp + n);
372 libresolv_hidden_def (p_fqnname)
374 /* XXX: the rest of these functions need to become length-limited, too. */
376 const u_char *
377 p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
378 char name[MAXDNAME];
379 const u_char *n;
381 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
382 if (n == NULL)
383 return (NULL);
384 fputs(name, file);
385 return (n);
389 * Names of RR classes and qclasses. Classes and qclasses are the same, except
390 * that C_ANY is a qclass but not a class. (You can ask for records of class
391 * C_ANY, but you can't have any records of that class in the database.)
393 extern const struct res_sym __p_class_syms[];
394 libresolv_hidden_proto (__p_class_syms)
395 const struct res_sym __p_class_syms[] = {
396 {C_IN, (char *) "IN"},
397 {C_CHAOS, (char *) "CHAOS"},
398 {C_HS, (char *) "HS"},
399 {C_HS, (char *) "HESIOD"},
400 {C_ANY, (char *) "ANY"},
401 {C_NONE, (char *) "NONE"},
402 {C_IN, NULL, NULL}
404 libresolv_hidden_data_def (__p_class_syms)
407 * Names of message sections.
409 const struct res_sym __p_default_section_syms[] attribute_hidden = {
410 {ns_s_qd, (char *) "QUERY"},
411 {ns_s_an, (char *) "ANSWER"},
412 {ns_s_ns, (char *) "AUTHORITY"},
413 {ns_s_ar, (char *) "ADDITIONAL"},
414 {0, NULL, NULL}
417 const struct res_sym __p_update_section_syms[] attribute_hidden = {
418 {S_ZONE, (char *) "ZONE"},
419 {S_PREREQ, (char *) "PREREQUISITE"},
420 {S_UPDATE, (char *) "UPDATE"},
421 {S_ADDT, (char *) "ADDITIONAL"},
422 {0, NULL, NULL}
426 * Names of RR types and qtypes. The list is incomplete because its
427 * size is part of the ABI.
429 extern const struct res_sym __p_type_syms[];
430 libresolv_hidden_proto (__p_type_syms)
431 const struct res_sym __p_type_syms[] = {
432 {ns_t_a, (char *) "A", (char *) "address"},
433 {ns_t_ns, (char *) "NS", (char *) "name server"},
434 {ns_t_md, (char *) "MD", (char *) "mail destination (deprecated)"},
435 {ns_t_mf, (char *) "MF", (char *) "mail forwarder (deprecated)"},
436 {ns_t_cname, (char *) "CNAME", (char *) "canonical name"},
437 {ns_t_soa, (char *) "SOA", (char *) "start of authority"},
438 {ns_t_mb, (char *) "MB", (char *) "mailbox"},
439 {ns_t_mg, (char *) "MG", (char *) "mail group member"},
440 {ns_t_mr, (char *) "MR", (char *) "mail rename"},
441 {ns_t_null, (char *) "NULL", (char *) "null"},
442 {ns_t_wks, (char *) "WKS", (char *) "well-known service (deprecated)"},
443 {ns_t_ptr, (char *) "PTR", (char *) "domain name pointer"},
444 {ns_t_hinfo, (char *) "HINFO", (char *) "host information"},
445 {ns_t_minfo, (char *) "MINFO", (char *) "mailbox information"},
446 {ns_t_mx, (char *) "MX", (char *) "mail exchanger"},
447 {ns_t_txt, (char *) "TXT", (char *) "text"},
448 {ns_t_rp, (char *) "RP", (char *) "responsible person"},
449 {ns_t_afsdb, (char *) "AFSDB", (char *) "DCE or AFS server"},
450 {ns_t_x25, (char *) "X25", (char *) "X25 address"},
451 {ns_t_isdn, (char *) "ISDN", (char *) "ISDN address"},
452 {ns_t_rt, (char *) "RT", (char *) "router"},
453 {ns_t_nsap, (char *) "NSAP", (char *) "nsap address"},
454 {ns_t_nsap_ptr, (char *) "NSAP_PTR", (char *) "domain name pointer"},
455 {ns_t_sig, (char *) "SIG", (char *) "signature"},
456 {ns_t_key, (char *) "KEY", (char *) "key"},
457 {ns_t_px, (char *) "PX", (char *) "mapping information"},
458 {ns_t_gpos, (char *) "GPOS",
459 (char *) "geographical position (withdrawn)"},
460 {ns_t_aaaa, (char *) "AAAA", (char *) "IPv6 address"},
461 {ns_t_loc, (char *) "LOC", (char *) "location"},
462 {ns_t_nxt, (char *) "NXT", (char *) "next valid name (unimplemented)"},
463 {ns_t_eid, (char *) "EID", (char *) "endpoint identifier (unimplemented)"},
464 {ns_t_nimloc, (char *) "NIMLOC", (char *) "NIMROD locator (unimplemented)"},
465 {ns_t_srv, (char *) "SRV", (char *) "server selection"},
466 {ns_t_atma, (char *) "ATMA", (char *) "ATM address (unimplemented)"},
467 {ns_t_dname, (char *) "DNAME", (char *) "Non-terminal DNAME (for IPv6)"},
468 {ns_t_tsig, (char *) "TSIG", (char *) "transaction signature"},
469 {ns_t_ixfr, (char *) "IXFR", (char *) "incremental zone transfer"},
470 {ns_t_axfr, (char *) "AXFR", (char *) "zone transfer"},
471 {ns_t_mailb, (char *) "MAILB", (char *) "mailbox-related data (deprecated)"},
472 {ns_t_maila, (char *) "MAILA", (char *) "mail agent (deprecated)"},
473 {ns_t_naptr, (char *) "NAPTR", (char *) "URN Naming Authority"},
474 {ns_t_kx, (char *) "KX", (char *) "Key Exchange"},
475 {ns_t_cert, (char *) "CERT", (char *) "Certificate"},
476 {ns_t_any, (char *) "ANY", (char *) "\"any\""},
477 {0, NULL, NULL}, /* Padding to preserve ABI. */
478 {0, NULL, NULL}
480 libresolv_hidden_data_def (__p_type_syms)
483 * Names of DNS rcodes.
485 const struct res_sym __p_rcode_syms[] attribute_hidden = {
486 {ns_r_noerror, (char *) "NOERROR", (char *) "no error"},
487 {ns_r_formerr, (char *) "FORMERR", (char *) "format error"},
488 {ns_r_servfail, (char *) "SERVFAIL", (char *) "server failed"},
489 {ns_r_nxdomain, (char *) "NXDOMAIN", (char *) "no such domain name"},
490 {ns_r_notimpl, (char *) "NOTIMP", (char *) "not implemented"},
491 {ns_r_refused, (char *) "REFUSED", (char *) "refused"},
492 {ns_r_yxdomain, (char *) "YXDOMAIN", (char *) "domain name exists"},
493 {ns_r_yxrrset, (char *) "YXRRSET", (char *) "rrset exists"},
494 {ns_r_nxrrset, (char *) "NXRRSET", (char *) "rrset doesn't exist"},
495 {ns_r_notauth, (char *) "NOTAUTH", (char *) "not authoritative"},
496 {ns_r_notzone, (char *) "NOTZONE", (char *) "Not in zone"},
497 {ns_r_max, (char *) "", (char *) ""},
498 {ns_r_badsig, (char *) "BADSIG", (char *) "bad signature"},
499 {ns_r_badkey, (char *) "BADKEY", (char *) "bad key"},
500 {ns_r_badtime, (char *) "BADTIME", (char *) "bad time"},
501 {0, NULL, NULL}
505 sym_ston(const struct res_sym *syms, const char *name, int *success) {
506 for ((void)NULL; syms->name != 0; syms++) {
507 if (strcasecmp (name, syms->name) == 0) {
508 if (success)
509 *success = 1;
510 return (syms->number);
513 if (success)
514 *success = 0;
515 return (syms->number); /* The default value. */
518 const char *
519 sym_ntos(const struct res_sym *syms, int number, int *success) {
520 static char unname[20];
522 for ((void)NULL; syms->name != 0; syms++) {
523 if (number == syms->number) {
524 if (success)
525 *success = 1;
526 return (syms->name);
530 sprintf(unname, "%d", number); /* XXX nonreentrant */
531 if (success)
532 *success = 0;
533 return (unname);
535 libresolv_hidden_def (sym_ntos)
537 const char *
538 sym_ntop(const struct res_sym *syms, int number, int *success) {
539 static char unname[20];
541 for ((void)NULL; syms->name != 0; syms++) {
542 if (number == syms->number) {
543 if (success)
544 *success = 1;
545 return (syms->humanname);
548 sprintf(unname, "%d", number); /* XXX nonreentrant */
549 if (success)
550 *success = 0;
551 return (unname);
555 * Return a string for the type.
557 const char *
558 p_type(int type) {
559 return (sym_ntos(__p_type_syms, type, (int *)0));
561 libresolv_hidden_def (p_type)
564 * Return a string for the type.
566 static const char *
567 p_section(int section, int opcode) {
568 const struct res_sym *symbols;
570 switch (opcode) {
571 case ns_o_update:
572 symbols = __p_update_section_syms;
573 break;
574 default:
575 symbols = __p_default_section_syms;
576 break;
578 return (sym_ntos(symbols, section, (int *)0));
582 * Return a mnemonic for class.
584 const char *
585 p_class(int class) {
586 return (sym_ntos(__p_class_syms, class, (int *)0));
588 libresolv_hidden_def (p_class)
591 * Return a mnemonic for an option
593 const char *
594 p_option(u_long option) {
595 static char nbuf[40];
597 switch (option) {
598 case RES_INIT: return "init";
599 case RES_DEBUG: return "debug";
600 case RES_USEVC: return "use-vc";
601 case RES_IGNTC: return "igntc";
602 case RES_RECURSE: return "recurs";
603 case RES_DEFNAMES: return "defnam";
604 case RES_STAYOPEN: return "styopn";
605 case RES_DNSRCH: return "dnsrch";
606 case RES_INSECURE1: return "insecure1";
607 case RES_INSECURE2: return "insecure2";
608 case RES_NOALIASES: return "noaliases";
609 case DEPRECATED_RES_USE_INET6: return "inet6";
610 case RES_ROTATE: return "rotate";
611 case RES_USE_EDNS0: return "edns0";
612 case RES_SNGLKUP: return "single-request";
613 case RES_SNGLKUPREOP: return "single-request-reopen";
614 case RES_USE_DNSSEC: return "dnssec";
615 case RES_NOTLDQUERY: return "no-tld-query";
616 case RES_NORELOAD: return "no-reload";
617 /* XXX nonreentrant */
618 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
619 return (nbuf);
622 libresolv_hidden_def (p_option)
625 * Return a mnemonic for a time to live.
627 const char *
628 p_time(u_int32_t value) {
629 static char nbuf[40]; /* XXX nonreentrant */
631 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
632 sprintf(nbuf, "%u", value);
633 return (nbuf);
637 * Return a string for the rcode.
639 const char *
640 p_rcode(int rcode) {
641 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
643 libresolv_hidden_def (p_rcode)
646 * routines to convert between on-the-wire RR format and zone file format.
647 * Does not contain conversion to/from decimal degrees; divide or multiply
648 * by 60*60*1000 for that.
651 static const unsigned int poweroften[10]=
652 { 1, 10, 100, 1000, 10000, 100000,
653 1000000,10000000,100000000,1000000000};
655 /* takes an XeY precision/size value, returns a string representation. */
656 static const char *
657 precsize_ntoa (u_int8_t prec)
659 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
660 unsigned long val;
661 int mantissa, exponent;
663 mantissa = (int)((prec >> 4) & 0x0f) % 10;
664 exponent = (int)((prec >> 0) & 0x0f) % 10;
666 val = mantissa * poweroften[exponent];
668 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
669 return (retbuf);
672 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
673 static u_int8_t
674 precsize_aton (const char **strptr)
676 unsigned int mval = 0, cmval = 0;
677 u_int8_t retval = 0;
678 const char *cp;
679 int exponent;
680 int mantissa;
682 cp = *strptr;
684 while (isdigit(*cp))
685 mval = mval * 10 + (*cp++ - '0');
687 if (*cp == '.') { /* centimeters */
688 cp++;
689 if (isdigit(*cp)) {
690 cmval = (*cp++ - '0') * 10;
691 if (isdigit(*cp)) {
692 cmval += (*cp++ - '0');
696 cmval = (mval * 100) + cmval;
698 for (exponent = 0; exponent < 9; exponent++)
699 if (cmval < poweroften[exponent+1])
700 break;
702 mantissa = cmval / poweroften[exponent];
703 if (mantissa > 9)
704 mantissa = 9;
706 retval = (mantissa << 4) | exponent;
708 *strptr = cp;
710 return (retval);
713 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
714 static u_int32_t
715 latlon2ul (const char **latlonstrptr, int *which)
717 const char *cp;
718 u_int32_t retval;
719 int deg = 0, min = 0, secs = 0, secsfrac = 0;
721 cp = *latlonstrptr;
723 while (isdigit(*cp))
724 deg = deg * 10 + (*cp++ - '0');
726 while (isspace(*cp))
727 cp++;
729 if (!(isdigit(*cp)))
730 goto fndhemi;
732 while (isdigit(*cp))
733 min = min * 10 + (*cp++ - '0');
735 while (isspace(*cp))
736 cp++;
738 if (!(isdigit(*cp)))
739 goto fndhemi;
741 while (isdigit(*cp))
742 secs = secs * 10 + (*cp++ - '0');
744 if (*cp == '.') { /* decimal seconds */
745 cp++;
746 if (isdigit(*cp)) {
747 secsfrac = (*cp++ - '0') * 100;
748 if (isdigit(*cp)) {
749 secsfrac += (*cp++ - '0') * 10;
750 if (isdigit(*cp)) {
751 secsfrac += (*cp++ - '0');
757 while (!isspace(*cp)) /* if any trailing garbage */
758 cp++;
760 while (isspace(*cp))
761 cp++;
763 fndhemi:
764 switch (*cp) {
765 case 'N': case 'n':
766 case 'E': case 'e':
767 retval = ((unsigned)1<<31)
768 + (((((deg * 60) + min) * 60) + secs) * 1000)
769 + secsfrac;
770 break;
771 case 'S': case 's':
772 case 'W': case 'w':
773 retval = ((unsigned)1<<31)
774 - (((((deg * 60) + min) * 60) + secs) * 1000)
775 - secsfrac;
776 break;
777 default:
778 retval = 0; /* invalid value -- indicates error */
779 break;
782 switch (*cp) {
783 case 'N': case 'n':
784 case 'S': case 's':
785 *which = 1; /* latitude */
786 break;
787 case 'E': case 'e':
788 case 'W': case 'w':
789 *which = 2; /* longitude */
790 break;
791 default:
792 *which = 0; /* error */
793 break;
796 cp++; /* skip the hemisphere */
798 while (!isspace(*cp)) /* if any trailing garbage */
799 cp++;
801 while (isspace(*cp)) /* move to next field */
802 cp++;
804 *latlonstrptr = cp;
806 return (retval);
809 /* converts a zone file representation in a string to an RDATA on-the-wire
810 * representation. */
812 loc_aton (const char *ascii, u_char *binary)
814 const char *cp, *maxcp;
815 u_char *bcp;
817 u_int32_t latit = 0, longit = 0, alt = 0;
818 u_int32_t lltemp1 = 0, lltemp2 = 0;
819 int altmeters = 0, altfrac = 0, altsign = 1;
820 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
821 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
822 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
823 int which1 = 0, which2 = 0;
825 cp = ascii;
826 maxcp = cp + strlen(ascii);
828 lltemp1 = latlon2ul(&cp, &which1);
830 lltemp2 = latlon2ul(&cp, &which2);
832 switch (which1 + which2) {
833 case 3: /* 1 + 2, the only valid combination */
834 if ((which1 == 1) && (which2 == 2)) { /* normal case */
835 latit = lltemp1;
836 longit = lltemp2;
837 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
838 longit = lltemp1;
839 latit = lltemp2;
840 } else { /* some kind of brokenness */
841 return (0);
843 break;
844 default: /* we didn't get one of each */
845 return (0);
848 /* altitude */
849 if (*cp == '-') {
850 altsign = -1;
851 cp++;
854 if (*cp == '+')
855 cp++;
857 while (isdigit(*cp))
858 altmeters = altmeters * 10 + (*cp++ - '0');
860 if (*cp == '.') { /* decimal meters */
861 cp++;
862 if (isdigit(*cp)) {
863 altfrac = (*cp++ - '0') * 10;
864 if (isdigit(*cp)) {
865 altfrac += (*cp++ - '0');
870 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
872 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
873 cp++;
875 while (isspace(*cp) && (cp < maxcp))
876 cp++;
878 if (cp >= maxcp)
879 goto defaults;
881 siz = precsize_aton(&cp);
883 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
884 cp++;
886 while (isspace(*cp) && (cp < maxcp))
887 cp++;
889 if (cp >= maxcp)
890 goto defaults;
892 hp = precsize_aton(&cp);
894 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
895 cp++;
897 while (isspace(*cp) && (cp < maxcp))
898 cp++;
900 if (cp >= maxcp)
901 goto defaults;
903 vp = precsize_aton(&cp);
905 defaults:
907 bcp = binary;
908 *bcp++ = (u_int8_t) 0; /* version byte */
909 *bcp++ = siz;
910 *bcp++ = hp;
911 *bcp++ = vp;
912 PUTLONG(latit,bcp);
913 PUTLONG(longit,bcp);
914 PUTLONG(alt,bcp);
916 return (16); /* size of RR in octets */
919 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
920 const char *
921 loc_ntoa (const u_char *binary, char *ascii)
923 static const char error[] = "?";
924 static char tmpbuf[sizeof
925 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
926 const u_char *cp = binary;
928 int latdeg, latmin, latsec, latsecfrac;
929 int longdeg, longmin, longsec, longsecfrac;
930 char northsouth, eastwest;
931 int altmeters, altfrac, altsign;
933 const u_int32_t referencealt = 100000 * 100;
935 int32_t latval, longval, altval;
936 u_int32_t templ;
937 u_int8_t sizeval, hpval, vpval, versionval;
939 char *sizestr, *hpstr, *vpstr;
941 versionval = *cp++;
943 if (ascii == NULL)
944 ascii = tmpbuf;
946 if (versionval) {
947 (void) sprintf(ascii, "; error: unknown LOC RR version");
948 return (ascii);
951 sizeval = *cp++;
953 hpval = *cp++;
954 vpval = *cp++;
956 GETLONG(templ, cp);
957 latval = (templ - ((unsigned)1<<31));
959 GETLONG(templ, cp);
960 longval = (templ - ((unsigned)1<<31));
962 GETLONG(templ, cp);
963 if (templ < referencealt) { /* below WGS 84 spheroid */
964 altval = referencealt - templ;
965 altsign = -1;
966 } else {
967 altval = templ - referencealt;
968 altsign = 1;
971 if (latval < 0) {
972 northsouth = 'S';
973 latval = -latval;
974 } else
975 northsouth = 'N';
977 latsecfrac = latval % 1000;
978 latval = latval / 1000;
979 latsec = latval % 60;
980 latval = latval / 60;
981 latmin = latval % 60;
982 latval = latval / 60;
983 latdeg = latval;
985 if (longval < 0) {
986 eastwest = 'W';
987 longval = -longval;
988 } else
989 eastwest = 'E';
991 longsecfrac = longval % 1000;
992 longval = longval / 1000;
993 longsec = longval % 60;
994 longval = longval / 60;
995 longmin = longval % 60;
996 longval = longval / 60;
997 longdeg = longval;
999 altfrac = altval % 100;
1000 altmeters = (altval / 100) * altsign;
1002 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
1003 sizestr = (char *) error;
1004 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
1005 hpstr = (char *) error;
1006 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
1007 vpstr = (char *) error;
1009 sprintf(ascii,
1010 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1011 latdeg, latmin, latsec, latsecfrac, northsouth,
1012 longdeg, longmin, longsec, longsecfrac, eastwest,
1013 altmeters, altfrac, sizestr, hpstr, vpstr);
1015 if (sizestr != (char *) error)
1016 free(sizestr);
1017 if (hpstr != (char *) error)
1018 free(hpstr);
1019 if (vpstr != (char *) error)
1020 free(vpstr);
1022 return (ascii);
1024 libresolv_hidden_def (loc_ntoa)
1027 /* Return the number of DNS hierarchy levels in the name. */
1029 dn_count_labels(const char *name) {
1030 int i, len, count;
1032 len = strlen(name);
1033 for (i = 0, count = 0; i < len; i++) {
1034 /* XXX need to check for \. or use named's nlabels(). */
1035 if (name[i] == '.')
1036 count++;
1039 /* don't count initial wildcard */
1040 if (name[0] == '*')
1041 if (count)
1042 count--;
1044 /* don't count the null label for root. */
1045 /* if terminating '.' not found, must adjust */
1046 /* count to include last label */
1047 if (len > 0 && name[len-1] != '.')
1048 count++;
1049 return (count);
1051 libresolv_hidden_def (__dn_count_labels)
1055 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1056 * SIG records are required to be printed like this, by the Secure DNS RFC.
1058 char *
1059 p_secstodate (u_long secs) {
1060 /* XXX nonreentrant */
1061 static char output[15]; /* YYYYMMDDHHMMSS and null */
1062 time_t clock = secs;
1063 struct tm *time;
1065 struct tm timebuf;
1066 time = __gmtime_r(&clock, &timebuf);
1067 time->tm_year += 1900;
1068 time->tm_mon += 1;
1069 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1070 time->tm_year, time->tm_mon, time->tm_mday,
1071 time->tm_hour, time->tm_min, time->tm_sec);
1072 return (output);
1074 libresolv_hidden_def (__p_secstodate)