backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / other-licenses / android / res_debug.c
blobacef60f8358e5a6b2bba91b6b9a8416cd9271ef5
1 /* $NetBSD: res_debug.c,v 1.7 2004/11/07 02:25:01 christos Exp $ */
3 /*
4 * Copyright (c) 1985
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
57 * Portions Copyright (c) 1995 by International Business Machines, Inc.
59 * International Business Machines, Inc. (hereinafter called IBM) grants
60 * permission under its copyrights to use, copy, modify, and distribute this
61 * Software with or without fee, provided that the above copyright notice and
62 * all paragraphs of this notice appear in all copies, and that the name of IBM
63 * not be used in connection with the marketing of any product incorporating
64 * the Software or modifications thereof, without specific, written prior
65 * permission.
67 * To the extent it has a right to do so, IBM grants an immunity from suit
68 * under its patents, if any, for the use, sale or manufacture of products to
69 * the extent that such products are used for performing Domain Name System
70 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
71 * granted for any product per se or for any other function of any product.
73 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
74 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
75 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
76 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
77 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
78 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
82 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
83 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
85 * Permission to use, copy, modify, and distribute this software for any
86 * purpose with or without fee is hereby granted, provided that the above
87 * copyright notice and this permission notice appear in all copies.
89 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
90 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
91 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
92 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
93 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
94 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
95 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
99 * This version of this file is derived from Android 2.3 "Gingerbread",
100 * which contains uncredited changes by Android/Google developers. It has
101 * been modified in 2011 for use in the Android build of Mozilla Firefox by
102 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
103 * and Steve Workman <sjhworkman@gmail.com>).
104 * These changes are offered under the same license as the original NetBSD
105 * file, whose copyright and license are unchanged above.
108 #define ANDROID_CHANGES 1
109 #define MOZILLA_NECKO_EXCLUDE_CODE 1
111 #include <sys/cdefs.h>
112 #if defined(LIBC_SCCS) && !defined(lint)
113 #ifdef notdef
114 static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
115 static const char rcsid[] = "Id: res_debug.c,v 1.3.2.5.4.5 2004/07/28 20:16:46 marka Exp";
116 #else
117 __RCSID("$NetBSD: res_debug.c,v 1.7 2004/11/07 02:25:01 christos Exp $");
118 #endif
119 #endif /* LIBC_SCCS and not lint */
123 #include <sys/types.h>
124 #include <sys/param.h>
125 #include <sys/socket.h>
127 #include <netinet/in.h>
128 #include <arpa/inet.h>
129 #include "arpa_nameser.h"
131 #include <ctype.h>
132 #include <errno.h>
133 #include <math.h>
134 #include <netdb.h>
135 #include "resolv_private.h"
136 #include <stdio.h>
137 #include <stdlib.h>
138 #include <string.h>
139 #include <time.h>
143 #ifdef SPRINTF_CHAR
144 # define SPRINTF(x) strlen(sprintf/**/x)
145 #else
146 # define SPRINTF(x) sprintf x
147 #endif
149 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
150 static const char *precsize_ntoa(u_int32_t);
151 #endif
153 extern const char * const _res_opcodes[];
154 extern const char * const _res_sectioncodes[];
156 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
157 #ifndef _LIBC
159 * Print the current options.
161 void
162 fp_resstat(const res_state statp, FILE *file) {
163 u_long mask;
165 fprintf(file, ";; res options:");
166 for (mask = 1; mask != 0U; mask <<= 1)
167 if (statp->options & mask)
168 fprintf(file, " %s", p_option(mask));
169 putc('\n', file);
171 #endif
172 #endif
174 static void
175 do_section(const res_state statp,
176 ns_msg *handle, ns_sect section,
177 int pflag, FILE *file)
179 int n, sflag, rrnum;
180 int buflen = 2048;
181 char *buf;
182 ns_opcode opcode;
183 ns_rr rr;
186 * Print answer records.
188 sflag = (statp->pfcode & pflag);
189 if (statp->pfcode && !sflag)
190 return;
192 buf = malloc((size_t)buflen);
193 if (buf == NULL) {
194 fprintf(file, ";; memory allocation failure\n");
195 return;
198 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
199 rrnum = 0;
200 for (;;) {
201 if (ns_parserr(handle, section, rrnum, &rr)) {
202 if (errno != ENODEV)
203 fprintf(file, ";; ns_parserr: %s\n",
204 strerror(errno));
205 else if (rrnum > 0 && sflag != 0 &&
206 (statp->pfcode & RES_PRF_HEAD1))
207 putc('\n', file);
208 goto cleanup;
210 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
211 fprintf(file, ";; %s SECTION:\n",
212 p_section(section, opcode));
213 if (section == ns_s_qd)
214 fprintf(file, ";;\t%s, type = %s, class = %s\n",
215 ns_rr_name(rr),
216 p_type(ns_rr_type(rr)),
217 p_class(ns_rr_class(rr)));
218 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
219 u_int32_t ttl = ns_rr_ttl(rr);
220 fprintf(file,
221 "; EDNS: version: %u, udp=%u, flags=%04x\n",
222 (ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
223 } else {
224 n = ns_sprintrr(handle, &rr, NULL, NULL,
225 buf, (u_int)buflen);
226 if (n < 0) {
227 if (errno == ENOSPC) {
228 free(buf);
229 buf = NULL;
230 if (buflen < 131072)
231 buf = malloc((size_t)(buflen += 1024));
232 if (buf == NULL) {
233 fprintf(file,
234 ";; memory allocation failure\n");
235 return;
237 continue;
239 fprintf(file, ";; ns_sprintrr: %s\n",
240 strerror(errno));
241 goto cleanup;
243 fputs(buf, file);
244 fputc('\n', file);
246 rrnum++;
248 cleanup:
249 if (buf != NULL)
250 free(buf);
254 * Print the contents of a query.
255 * This is intended to be primarily a debugging routine.
257 void
258 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
259 ns_msg handle;
260 int qdcount, ancount, nscount, arcount;
261 u_int opcode, rcode, id;
263 if (ns_initparse(msg, len, &handle) < 0) {
264 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
265 return;
267 opcode = ns_msg_getflag(handle, ns_f_opcode);
268 rcode = ns_msg_getflag(handle, ns_f_rcode);
269 id = ns_msg_id(handle);
270 qdcount = ns_msg_count(handle, ns_s_qd);
271 ancount = ns_msg_count(handle, ns_s_an);
272 nscount = ns_msg_count(handle, ns_s_ns);
273 arcount = ns_msg_count(handle, ns_s_ar);
276 * Print header fields.
278 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
279 fprintf(file,
280 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
281 _res_opcodes[opcode], p_rcode((int)rcode), id);
282 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
283 putc(';', file);
284 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
285 fprintf(file, "; flags:");
286 if (ns_msg_getflag(handle, ns_f_qr))
287 fprintf(file, " qr");
288 if (ns_msg_getflag(handle, ns_f_aa))
289 fprintf(file, " aa");
290 if (ns_msg_getflag(handle, ns_f_tc))
291 fprintf(file, " tc");
292 if (ns_msg_getflag(handle, ns_f_rd))
293 fprintf(file, " rd");
294 if (ns_msg_getflag(handle, ns_f_ra))
295 fprintf(file, " ra");
296 if (ns_msg_getflag(handle, ns_f_z))
297 fprintf(file, " ??");
298 if (ns_msg_getflag(handle, ns_f_ad))
299 fprintf(file, " ad");
300 if (ns_msg_getflag(handle, ns_f_cd))
301 fprintf(file, " cd");
303 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
304 fprintf(file, "; %s: %d",
305 p_section(ns_s_qd, (int)opcode), qdcount);
306 fprintf(file, ", %s: %d",
307 p_section(ns_s_an, (int)opcode), ancount);
308 fprintf(file, ", %s: %d",
309 p_section(ns_s_ns, (int)opcode), nscount);
310 fprintf(file, ", %s: %d",
311 p_section(ns_s_ar, (int)opcode), arcount);
313 if ((!statp->pfcode) || (statp->pfcode &
314 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
315 putc('\n',file);
318 * Print the various sections.
320 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
321 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
322 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
323 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
324 if (qdcount == 0 && ancount == 0 &&
325 nscount == 0 && arcount == 0)
326 putc('\n', file);
329 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
330 const u_char *
331 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
332 char name[MAXDNAME];
333 int n;
335 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
336 return (NULL);
337 if (name[0] == '\0')
338 putc('.', file);
339 else
340 fputs(name, file);
341 return (cp + n);
344 const u_char *
345 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
346 return (p_cdnname(cp, msg, PACKETSZ, file));
349 /* Return a fully-qualified domain name from a compressed name (with
350 length supplied). */
352 const u_char *
353 p_fqnname(cp, msg, msglen, name, namelen)
354 const u_char *cp, *msg;
355 int msglen;
356 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);
373 /* XXX: the rest of these functions need to become length-limited, too. */
375 const u_char *
376 p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
377 char name[MAXDNAME];
378 const u_char *n;
380 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
381 if (n == NULL)
382 return (NULL);
383 fputs(name, file);
384 return (n);
386 #endif
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 const struct res_sym __p_class_syms[] = {
394 {C_IN, "IN", (char *)0},
395 {C_CHAOS, "CH", (char *)0},
396 {C_CHAOS, "CHAOS", (char *)0},
397 {C_HS, "HS", (char *)0},
398 {C_HS, "HESIOD", (char *)0},
399 {C_ANY, "ANY", (char *)0},
400 {C_NONE, "NONE", (char *)0},
401 {C_IN, (char *)0, (char *)0}
405 * Names of message sections.
407 const struct res_sym __p_default_section_syms[] = {
408 {ns_s_qd, "QUERY", (char *)0},
409 {ns_s_an, "ANSWER", (char *)0},
410 {ns_s_ns, "AUTHORITY", (char *)0},
411 {ns_s_ar, "ADDITIONAL", (char *)0},
412 {0, (char *)0, (char *)0}
415 const struct res_sym __p_update_section_syms[] = {
416 {S_ZONE, "ZONE", (char *)0},
417 {S_PREREQ, "PREREQUISITE", (char *)0},
418 {S_UPDATE, "UPDATE", (char *)0},
419 {S_ADDT, "ADDITIONAL", (char *)0},
420 {0, (char *)0, (char *)0}
423 const struct res_sym __p_key_syms[] = {
424 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
425 {NS_ALG_DH, "DH", "Diffie Hellman"},
426 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
427 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
428 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
429 {0, NULL, NULL}
432 const struct res_sym __p_cert_syms[] = {
433 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
434 {cert_t_spki, "SPKI", "SPKI certificate"},
435 {cert_t_pgp, "PGP", "PGP certificate"},
436 {cert_t_url, "URL", "URL Private"},
437 {cert_t_oid, "OID", "OID Private"},
438 {0, NULL, NULL}
442 * Names of RR types and qtypes. Types and qtypes are the same, except
443 * that T_ANY is a qtype but not a type. (You can ask for records of type
444 * T_ANY, but you can't have any records of that type in the database.)
446 const struct res_sym __p_type_syms[] = {
447 {ns_t_a, "A", "address"},
448 {ns_t_ns, "NS", "name server"},
449 {ns_t_md, "MD", "mail destination (deprecated)"},
450 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
451 {ns_t_cname, "CNAME", "canonical name"},
452 {ns_t_soa, "SOA", "start of authority"},
453 {ns_t_mb, "MB", "mailbox"},
454 {ns_t_mg, "MG", "mail group member"},
455 {ns_t_mr, "MR", "mail rename"},
456 {ns_t_null, "NULL", "null"},
457 {ns_t_wks, "WKS", "well-known service (deprecated)"},
458 {ns_t_ptr, "PTR", "domain name pointer"},
459 {ns_t_hinfo, "HINFO", "host information"},
460 {ns_t_minfo, "MINFO", "mailbox information"},
461 {ns_t_mx, "MX", "mail exchanger"},
462 {ns_t_txt, "TXT", "text"},
463 {ns_t_rp, "RP", "responsible person"},
464 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
465 {ns_t_x25, "X25", "X25 address"},
466 {ns_t_isdn, "ISDN", "ISDN address"},
467 {ns_t_rt, "RT", "router"},
468 {ns_t_nsap, "NSAP", "nsap address"},
469 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
470 {ns_t_sig, "SIG", "signature"},
471 {ns_t_key, "KEY", "key"},
472 {ns_t_px, "PX", "mapping information"},
473 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
474 {ns_t_aaaa, "AAAA", "IPv6 address"},
475 {ns_t_loc, "LOC", "location"},
476 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
477 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
478 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
479 {ns_t_srv, "SRV", "server selection"},
480 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
481 {ns_t_tkey, "TKEY", "tkey"},
482 {ns_t_tsig, "TSIG", "transaction signature"},
483 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
484 {ns_t_axfr, "AXFR", "zone transfer"},
485 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
486 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
487 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
488 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
489 {ns_t_kx, "KX", "Key Exchange"},
490 {ns_t_cert, "CERT", "Certificate"},
491 {ns_t_a6, "A6", "IPv6 Address"},
492 {ns_t_dname, "DNAME", "dname"},
493 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
494 {ns_t_opt, "OPT", "EDNS Options"},
495 {ns_t_any, "ANY", "\"any\""},
496 {0, NULL, NULL}
500 * Names of DNS rcodes.
502 const struct res_sym __p_rcode_syms[] = {
503 {ns_r_noerror, "NOERROR", "no error"},
504 {ns_r_formerr, "FORMERR", "format error"},
505 {ns_r_servfail, "SERVFAIL", "server failed"},
506 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
507 {ns_r_notimpl, "NOTIMP", "not implemented"},
508 {ns_r_refused, "REFUSED", "refused"},
509 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
510 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
511 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
512 {ns_r_notauth, "NOTAUTH", "not authoritative"},
513 {ns_r_notzone, "NOTZONE", "Not in zone"},
514 {ns_r_max, "", ""},
515 {ns_r_badsig, "BADSIG", "bad signature"},
516 {ns_r_badkey, "BADKEY", "bad key"},
517 {ns_r_badtime, "BADTIME", "bad time"},
518 {0, NULL, NULL}
522 sym_ston(const struct res_sym *syms, const char *name, int *success) {
523 for (; syms->name != 0; syms++) {
524 if (strcasecmp (name, syms->name) == 0) {
525 if (success)
526 *success = 1;
527 return (syms->number);
530 if (success)
531 *success = 0;
532 return (syms->number); /* The default value. */
535 const char *
536 sym_ntos(const struct res_sym *syms, int number, int *success) {
537 static char unname[20];
539 for (; syms->name != 0; syms++) {
540 if (number == syms->number) {
541 if (success)
542 *success = 1;
543 return (syms->name);
547 sprintf(unname, "%d", number); /* XXX nonreentrant */
548 if (success)
549 *success = 0;
550 return (unname);
553 const char *
554 sym_ntop(const struct res_sym *syms, int number, int *success) {
555 static char unname[20];
557 for (; syms->name != 0; syms++) {
558 if (number == syms->number) {
559 if (success)
560 *success = 1;
561 return (syms->humanname);
564 sprintf(unname, "%d", number); /* XXX nonreentrant */
565 if (success)
566 *success = 0;
567 return (unname);
571 * Return a string for the type.
573 const char *
574 p_type(int type) {
575 int success;
576 const char *result;
577 static char typebuf[20];
579 result = sym_ntos(__p_type_syms, type, &success);
580 if (success)
581 return (result);
582 if (type < 0 || type > 0xffff)
583 return ("BADTYPE");
584 sprintf(typebuf, "TYPE%d", type);
585 return (typebuf);
589 * Return a string for the type.
591 const char *
592 p_section(int section, int opcode) {
593 const struct res_sym *symbols;
595 switch (opcode) {
596 case ns_o_update:
597 symbols = __p_update_section_syms;
598 break;
599 default:
600 symbols = __p_default_section_syms;
601 break;
603 return (sym_ntos(symbols, section, (int *)0));
607 * Return a mnemonic for class.
609 const char *
610 p_class(int class) {
611 int success;
612 const char *result;
613 static char classbuf[20];
615 result = sym_ntos(__p_class_syms, class, &success);
616 if (success)
617 return (result);
618 if (class < 0 || class > 0xffff)
619 return ("BADCLASS");
620 sprintf(classbuf, "CLASS%d", class);
621 return (classbuf);
625 * Return a mnemonic for an option
627 const char *
628 p_option(u_long option) {
629 static char nbuf[40];
631 switch (option) {
632 case RES_INIT: return "init";
633 case RES_DEBUG: return "debug";
634 case RES_AAONLY: return "aaonly(unimpl)";
635 case RES_USEVC: return "usevc";
636 case RES_PRIMARY: return "primry(unimpl)";
637 case RES_IGNTC: return "igntc";
638 case RES_RECURSE: return "recurs";
639 case RES_DEFNAMES: return "defnam";
640 case RES_STAYOPEN: return "styopn";
641 case RES_DNSRCH: return "dnsrch";
642 case RES_INSECURE1: return "insecure1";
643 case RES_INSECURE2: return "insecure2";
644 case RES_NOALIASES: return "noaliases";
645 case RES_USE_INET6: return "inet6";
646 #ifdef RES_USE_EDNS0 /* KAME extension */
647 case RES_USE_EDNS0: return "edns0";
648 #endif
649 #ifdef RES_USE_DNAME
650 case RES_USE_DNAME: return "dname";
651 #endif
652 #ifdef RES_USE_DNSSEC
653 case RES_USE_DNSSEC: return "dnssec";
654 #endif
655 #ifdef RES_NOTLDQUERY
656 case RES_NOTLDQUERY: return "no-tld-query";
657 #endif
658 #ifdef RES_NO_NIBBLE2
659 case RES_NO_NIBBLE2: return "no-nibble2";
660 #endif
661 /* XXX nonreentrant */
662 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
663 return (nbuf);
667 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
669 * Return a mnemonic for a time to live.
671 const char *
672 p_time(u_int32_t value) {
673 static char nbuf[40]; /* XXX nonreentrant */
675 if (ns_format_ttl((u_long)value, nbuf, sizeof nbuf) < 0)
676 sprintf(nbuf, "%u", value);
677 return (nbuf);
679 #endif
682 * Return a string for the rcode.
684 const char *
685 p_rcode(int rcode) {
686 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
689 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
691 * Return a string for a res_sockaddr_union.
693 const char *
694 p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
695 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
697 switch (u.sin.sin_family) {
698 case AF_INET:
699 inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
700 break;
701 #ifdef HAS_INET6_STRUCTS
702 case AF_INET6:
703 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
704 break;
705 #endif
706 default:
707 sprintf(ret, "[af%d]", u.sin.sin_family);
708 break;
710 if (size > 0U) {
711 strncpy(buf, ret, size - 1);
712 buf[size - 1] = '0';
714 return (buf);
716 #endif
719 * routines to convert between on-the-wire RR format and zone file format.
720 * Does not contain conversion to/from decimal degrees; divide or multiply
721 * by 60*60*1000 for that.
724 static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
725 1000000,10000000,100000000,1000000000};
727 /* takes an XeY precision/size value, returns a string representation. */
728 static const char *
729 precsize_ntoa(prec)
730 u_int32_t prec;
732 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
733 unsigned long val;
734 int mantissa, exponent;
736 mantissa = (int)((prec >> 4) & 0x0f) % 10;
737 exponent = (int)((prec >> 0) & 0x0f) % 10;
739 val = mantissa * poweroften[exponent];
741 (void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
742 return (retbuf);
745 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
746 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
747 static u_int8_t
748 precsize_aton(const char **strptr) {
749 unsigned int mval = 0, cmval = 0;
750 u_int8_t retval = 0;
751 const char *cp;
752 int exponent;
753 int mantissa;
755 cp = *strptr;
757 while (isdigit((unsigned char)*cp))
758 mval = mval * 10 + (*cp++ - '0');
760 if (*cp == '.') { /* centimeters */
761 cp++;
762 if (isdigit((unsigned char)*cp)) {
763 cmval = (*cp++ - '0') * 10;
764 if (isdigit((unsigned char)*cp)) {
765 cmval += (*cp++ - '0');
769 cmval = (mval * 100) + cmval;
771 for (exponent = 0; exponent < 9; exponent++)
772 if (cmval < poweroften[exponent+1])
773 break;
775 mantissa = cmval / poweroften[exponent];
776 if (mantissa > 9)
777 mantissa = 9;
779 retval = (mantissa << 4) | exponent;
781 *strptr = cp;
783 return (retval);
786 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
787 static u_int32_t
788 latlon2ul(const char **latlonstrptr, int *which) {
789 const char *cp;
790 u_int32_t retval;
791 int deg = 0, min = 0, secs = 0, secsfrac = 0;
793 cp = *latlonstrptr;
795 while (isdigit((unsigned char)*cp))
796 deg = deg * 10 + (*cp++ - '0');
798 while (isspace((unsigned char)*cp))
799 cp++;
801 if (!(isdigit((unsigned char)*cp)))
802 goto fndhemi;
804 while (isdigit((unsigned char)*cp))
805 min = min * 10 + (*cp++ - '0');
807 while (isspace((unsigned char)*cp))
808 cp++;
810 if (!(isdigit((unsigned char)*cp)))
811 goto fndhemi;
813 while (isdigit((unsigned char)*cp))
814 secs = secs * 10 + (*cp++ - '0');
816 if (*cp == '.') { /* decimal seconds */
817 cp++;
818 if (isdigit((unsigned char)*cp)) {
819 secsfrac = (*cp++ - '0') * 100;
820 if (isdigit((unsigned char)*cp)) {
821 secsfrac += (*cp++ - '0') * 10;
822 if (isdigit((unsigned char)*cp)) {
823 secsfrac += (*cp++ - '0');
829 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
830 cp++;
832 while (isspace((unsigned char)*cp))
833 cp++;
835 fndhemi:
836 switch (*cp) {
837 case 'N': case 'n':
838 case 'E': case 'e':
839 retval = ((unsigned)1<<31)
840 + (((((deg * 60) + min) * 60) + secs) * 1000)
841 + secsfrac;
842 break;
843 case 'S': case 's':
844 case 'W': case 'w':
845 retval = ((unsigned)1<<31)
846 - (((((deg * 60) + min) * 60) + secs) * 1000)
847 - secsfrac;
848 break;
849 default:
850 retval = 0; /* invalid value -- indicates error */
851 break;
854 switch (*cp) {
855 case 'N': case 'n':
856 case 'S': case 's':
857 *which = 1; /* latitude */
858 break;
859 case 'E': case 'e':
860 case 'W': case 'w':
861 *which = 2; /* longitude */
862 break;
863 default:
864 *which = 0; /* error */
865 break;
868 cp++; /* skip the hemisphere */
870 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
871 cp++;
873 while (isspace((unsigned char)*cp)) /* move to next field */
874 cp++;
876 *latlonstrptr = cp;
878 return (retval);
881 /* converts a zone file representation in a string to an RDATA on-the-wire
882 * representation. */
884 loc_aton(ascii, binary)
885 const char *ascii;
886 u_char *binary;
888 const char *cp, *maxcp;
889 u_char *bcp;
891 u_int32_t latit = 0, longit = 0, alt = 0;
892 u_int32_t lltemp1 = 0, lltemp2 = 0;
893 int altmeters = 0, altfrac = 0, altsign = 1;
894 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
895 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
896 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
897 int which1 = 0, which2 = 0;
899 cp = ascii;
900 maxcp = cp + strlen(ascii);
902 lltemp1 = latlon2ul(&cp, &which1);
904 lltemp2 = latlon2ul(&cp, &which2);
906 switch (which1 + which2) {
907 case 3: /* 1 + 2, the only valid combination */
908 if ((which1 == 1) && (which2 == 2)) { /* normal case */
909 latit = lltemp1;
910 longit = lltemp2;
911 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
912 longit = lltemp1;
913 latit = lltemp2;
914 } else { /* some kind of brokenness */
915 return (0);
917 break;
918 default: /* we didn't get one of each */
919 return (0);
922 /* altitude */
923 if (*cp == '-') {
924 altsign = -1;
925 cp++;
928 if (*cp == '+')
929 cp++;
931 while (isdigit((unsigned char)*cp))
932 altmeters = altmeters * 10 + (*cp++ - '0');
934 if (*cp == '.') { /* decimal meters */
935 cp++;
936 if (isdigit((unsigned char)*cp)) {
937 altfrac = (*cp++ - '0') * 10;
938 if (isdigit((unsigned char)*cp)) {
939 altfrac += (*cp++ - '0');
944 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
946 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
947 cp++;
949 while (isspace((unsigned char)*cp) && (cp < maxcp))
950 cp++;
952 if (cp >= maxcp)
953 goto defaults;
955 siz = precsize_aton(&cp);
957 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
958 cp++;
960 while (isspace((unsigned char)*cp) && (cp < maxcp))
961 cp++;
963 if (cp >= maxcp)
964 goto defaults;
966 hp = precsize_aton(&cp);
968 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
969 cp++;
971 while (isspace((unsigned char)*cp) && (cp < maxcp))
972 cp++;
974 if (cp >= maxcp)
975 goto defaults;
977 vp = precsize_aton(&cp);
979 defaults:
981 bcp = binary;
982 *bcp++ = (u_int8_t) 0; /* version byte */
983 *bcp++ = siz;
984 *bcp++ = hp;
985 *bcp++ = vp;
986 PUTLONG(latit,bcp);
987 PUTLONG(longit,bcp);
988 PUTLONG(alt,bcp);
990 return (16); /* size of RR in octets */
992 #endif
994 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
995 const char *
996 loc_ntoa(binary, ascii)
997 const u_char *binary;
998 char *ascii;
1000 static const char *error = "?";
1001 static char tmpbuf[sizeof
1002 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
1003 const u_char *cp = binary;
1005 int latdeg, latmin, latsec, latsecfrac;
1006 int longdeg, longmin, longsec, longsecfrac;
1007 char northsouth, eastwest;
1008 const char *altsign;
1009 int altmeters, altfrac;
1011 const u_int32_t referencealt = 100000 * 100;
1013 int32_t latval, longval, altval;
1014 u_int32_t templ;
1015 u_int8_t sizeval, hpval, vpval, versionval;
1017 char *sizestr, *hpstr, *vpstr;
1019 versionval = *cp++;
1021 if (ascii == NULL)
1022 ascii = tmpbuf;
1024 if (versionval) {
1025 (void) sprintf(ascii, "; error: unknown LOC RR version");
1026 return (ascii);
1029 sizeval = *cp++;
1031 hpval = *cp++;
1032 vpval = *cp++;
1034 GETLONG(templ, cp);
1035 latval = (templ - ((unsigned)1<<31));
1037 GETLONG(templ, cp);
1038 longval = (templ - ((unsigned)1<<31));
1040 GETLONG(templ, cp);
1041 if (templ < referencealt) { /* below WGS 84 spheroid */
1042 altval = referencealt - templ;
1043 altsign = "-";
1044 } else {
1045 altval = templ - referencealt;
1046 altsign = "";
1049 if (latval < 0) {
1050 northsouth = 'S';
1051 latval = -latval;
1052 } else
1053 northsouth = 'N';
1055 latsecfrac = latval % 1000;
1056 latval = latval / 1000;
1057 latsec = latval % 60;
1058 latval = latval / 60;
1059 latmin = latval % 60;
1060 latval = latval / 60;
1061 latdeg = latval;
1063 if (longval < 0) {
1064 eastwest = 'W';
1065 longval = -longval;
1066 } else
1067 eastwest = 'E';
1069 longsecfrac = longval % 1000;
1070 longval = longval / 1000;
1071 longsec = longval % 60;
1072 longval = longval / 60;
1073 longmin = longval % 60;
1074 longval = longval / 60;
1075 longdeg = longval;
1077 altfrac = altval % 100;
1078 altmeters = (altval / 100);
1080 sizestr = strdup(precsize_ntoa((u_int32_t)sizeval));
1081 hpstr = strdup(precsize_ntoa((u_int32_t)hpval));
1082 vpstr = strdup(precsize_ntoa((u_int32_t)vpval));
1084 sprintf(ascii,
1085 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
1086 latdeg, latmin, latsec, latsecfrac, northsouth,
1087 longdeg, longmin, longsec, longsecfrac, eastwest,
1088 altsign, altmeters, altfrac,
1089 (sizestr != NULL) ? sizestr : error,
1090 (hpstr != NULL) ? hpstr : error,
1091 (vpstr != NULL) ? vpstr : error);
1093 if (sizestr != NULL)
1094 free(sizestr);
1095 if (hpstr != NULL)
1096 free(hpstr);
1097 if (vpstr != NULL)
1098 free(vpstr);
1100 return (ascii);
1104 /* Return the number of DNS hierarchy levels in the name. */
1106 dn_count_labels(const char *name) {
1107 int i, len, count;
1109 len = strlen(name);
1110 for (i = 0, count = 0; i < len; i++) {
1111 /* XXX need to check for \. or use named's nlabels(). */
1112 if (name[i] == '.')
1113 count++;
1116 /* don't count initial wildcard */
1117 if (name[0] == '*')
1118 if (count)
1119 count--;
1121 /* don't count the null label for root. */
1122 /* if terminating '.' not found, must adjust */
1123 /* count to include last label */
1124 if (len > 0 && name[len-1] != '.')
1125 count++;
1126 return (count);
1131 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1132 * SIG records are required to be printed like this, by the Secure DNS RFC.
1134 char *
1135 p_secstodate (u_long secs) {
1136 /* XXX nonreentrant */
1137 static char output[15]; /* YYYYMMDDHHMMSS and null */
1138 time_t myclock = secs;
1139 struct tm *mytime;
1140 #ifdef HAVE_TIME_R
1141 struct tm res;
1143 mytime = gmtime_r(&myclock, &res);
1144 #else
1145 mytime = gmtime(&myclock);
1146 #endif
1147 mytime->tm_year += 1900;
1148 mytime->tm_mon += 1;
1149 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1150 mytime->tm_year, mytime->tm_mon, mytime->tm_mday,
1151 mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
1152 return (output);
1155 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
1156 u_int16_t
1157 res_nametoclass(const char *buf, int *successp) {
1158 unsigned long result;
1159 char *endptr;
1160 int success;
1162 result = sym_ston(__p_class_syms, buf, &success);
1163 if (success)
1164 goto done;
1166 if (strncasecmp(buf, "CLASS", 5) != 0 ||
1167 !isdigit((unsigned char)buf[5]))
1168 goto done;
1169 errno = 0;
1170 result = strtoul(buf + 5, &endptr, 10);
1171 if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
1172 success = 1;
1173 done:
1174 if (successp)
1175 *successp = success;
1176 return (u_int16_t)(result);
1179 u_int16_t
1180 res_nametotype(const char *buf, int *successp) {
1181 unsigned long result;
1182 char *endptr;
1183 int success;
1185 result = sym_ston(__p_type_syms, buf, &success);
1186 if (success)
1187 goto done;
1189 if (strncasecmp(buf, "type", 4) != 0 ||
1190 !isdigit((unsigned char)buf[4]))
1191 goto done;
1192 errno = 0;
1193 result = strtoul(buf + 4, &endptr, 10);
1194 if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
1195 success = 1;
1196 done:
1197 if (successp)
1198 *successp = success;
1199 return (u_int16_t)(result);
1201 #endif