1 /* $NetBSD: res_debug.c,v 1.7 2004/11/07 02:25:01 christos Exp $ */
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
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
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
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
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)
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";
117 __RCSID("$NetBSD: res_debug.c,v 1.7 2004/11/07 02:25:01 christos Exp $");
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"
135 #include "resolv_private.h"
144 # define SPRINTF(x) strlen(sprintf/**/x)
146 # define SPRINTF(x) sprintf x
149 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
150 static const char *precsize_ntoa(u_int32_t
);
153 extern const char * const _res_opcodes
[];
154 extern const char * const _res_sectioncodes
[];
156 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
159 * Print the current options.
162 fp_resstat(const res_state statp
, FILE *file
) {
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
));
175 do_section(const res_state statp
,
176 ns_msg
*handle
, ns_sect section
,
177 int pflag
, FILE *file
)
186 * Print answer records.
188 sflag
= (statp
->pfcode
& pflag
);
189 if (statp
->pfcode
&& !sflag
)
192 buf
= malloc((size_t)buflen
);
194 fprintf(file
, ";; memory allocation failure\n");
198 opcode
= (ns_opcode
) ns_msg_getflag(*handle
, ns_f_opcode
);
201 if (ns_parserr(handle
, section
, rrnum
, &rr
)) {
203 fprintf(file
, ";; ns_parserr: %s\n",
205 else if (rrnum
> 0 && sflag
!= 0 &&
206 (statp
->pfcode
& RES_PRF_HEAD1
))
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",
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
);
221 "; EDNS: version: %u, udp=%u, flags=%04x\n",
222 (ttl
>>16)&0xff, ns_rr_class(rr
), ttl
&0xffff);
224 n
= ns_sprintrr(handle
, &rr
, NULL
, NULL
,
227 if (errno
== ENOSPC
) {
231 buf
= malloc((size_t)(buflen
+= 1024));
234 ";; memory allocation failure\n");
239 fprintf(file
, ";; ns_sprintrr: %s\n",
254 * Print the contents of a query.
255 * This is intended to be primarily a debugging routine.
258 res_pquery(const res_state statp
, const u_char
*msg
, int len
, FILE *file
) {
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
));
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
)
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
))
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
))) {
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)
329 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
331 p_cdnname(const u_char
*cp
, const u_char
*msg
, int len
, FILE *file
) {
335 if ((n
= dn_expand(msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
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
353 p_fqnname(cp
, msg
, msglen
, name
, namelen
)
354 const u_char
*cp
, *msg
;
361 if ((n
= dn_expand(msg
, cp
+ msglen
, cp
, name
, namelen
)) < 0)
363 newlen
= strlen(name
);
364 if (newlen
== 0 || name
[newlen
- 1] != '.') {
365 if (newlen
+ 1 >= namelen
) /* Lack space for final dot */
368 strcpy(name
+ newlen
, ".");
373 /* XXX: the rest of these functions need to become length-limited, too. */
376 p_fqname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
380 n
= p_fqnname(cp
, msg
, MAXCDNAME
, name
, sizeof name
);
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"},
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"},
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\""},
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"},
515 {ns_r_badsig
, "BADSIG", "bad signature"},
516 {ns_r_badkey
, "BADKEY", "bad key"},
517 {ns_r_badtime
, "BADTIME", "bad time"},
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) {
527 return (syms
->number
);
532 return (syms
->number
); /* The default value. */
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
) {
547 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
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
) {
561 return (syms
->humanname
);
564 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
571 * Return a string for the type.
577 static char typebuf
[20];
579 result
= sym_ntos(__p_type_syms
, type
, &success
);
582 if (type
< 0 || type
> 0xffff)
584 sprintf(typebuf
, "TYPE%d", type
);
589 * Return a string for the type.
592 p_section(int section
, int opcode
) {
593 const struct res_sym
*symbols
;
597 symbols
= __p_update_section_syms
;
600 symbols
= __p_default_section_syms
;
603 return (sym_ntos(symbols
, section
, (int *)0));
607 * Return a mnemonic for class.
613 static char classbuf
[20];
615 result
= sym_ntos(__p_class_syms
, class, &success
);
618 if (class < 0 || class > 0xffff)
620 sprintf(classbuf
, "CLASS%d", class);
625 * Return a mnemonic for an option
628 p_option(u_long option
) {
629 static char nbuf
[40];
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";
650 case RES_USE_DNAME
: return "dname";
652 #ifdef RES_USE_DNSSEC
653 case RES_USE_DNSSEC
: return "dnssec";
655 #ifdef RES_NOTLDQUERY
656 case RES_NOTLDQUERY
: return "no-tld-query";
658 #ifdef RES_NO_NIBBLE2
659 case RES_NO_NIBBLE2
: return "no-nibble2";
661 /* XXX nonreentrant */
662 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
667 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
669 * Return a mnemonic for a time to live.
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
);
682 * Return a string for the 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.
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
) {
699 inet_ntop(AF_INET
, &u
.sin
.sin_addr
, ret
, sizeof ret
);
701 #ifdef HAS_INET6_STRUCTS
703 inet_ntop(AF_INET6
, &u
.sin6
.sin6_addr
, ret
, sizeof ret
);
707 sprintf(ret
, "[af%d]", u
.sin
.sin_family
);
711 strncpy(buf
, ret
, size
- 1);
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. */
732 static char retbuf
[sizeof "90000000.00"]; /* XXX nonreentrant */
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);
745 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
746 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
748 precsize_aton(const char **strptr
) {
749 unsigned int mval
= 0, cmval
= 0;
757 while (isdigit((unsigned char)*cp
))
758 mval
= mval
* 10 + (*cp
++ - '0');
760 if (*cp
== '.') { /* centimeters */
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])
775 mantissa
= cmval
/ poweroften
[exponent
];
779 retval
= (mantissa
<< 4) | exponent
;
786 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
788 latlon2ul(const char **latlonstrptr
, int *which
) {
791 int deg
= 0, min
= 0, secs
= 0, secsfrac
= 0;
795 while (isdigit((unsigned char)*cp
))
796 deg
= deg
* 10 + (*cp
++ - '0');
798 while (isspace((unsigned char)*cp
))
801 if (!(isdigit((unsigned char)*cp
)))
804 while (isdigit((unsigned char)*cp
))
805 min
= min
* 10 + (*cp
++ - '0');
807 while (isspace((unsigned char)*cp
))
810 if (!(isdigit((unsigned char)*cp
)))
813 while (isdigit((unsigned char)*cp
))
814 secs
= secs
* 10 + (*cp
++ - '0');
816 if (*cp
== '.') { /* decimal seconds */
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 */
832 while (isspace((unsigned char)*cp
))
839 retval
= ((unsigned)1<<31)
840 + (((((deg
* 60) + min
) * 60) + secs
) * 1000)
845 retval
= ((unsigned)1<<31)
846 - (((((deg
* 60) + min
) * 60) + secs
) * 1000)
850 retval
= 0; /* invalid value -- indicates error */
857 *which
= 1; /* latitude */
861 *which
= 2; /* longitude */
864 *which
= 0; /* error */
868 cp
++; /* skip the hemisphere */
870 while (!isspace((unsigned char)*cp
)) /* if any trailing garbage */
873 while (isspace((unsigned char)*cp
)) /* move to next field */
881 /* converts a zone file representation in a string to an RDATA on-the-wire
884 loc_aton(ascii
, binary
)
888 const char *cp
, *maxcp
;
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;
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 */
911 } else if ((which1
== 2) && (which2
== 1)) { /* reversed */
914 } else { /* some kind of brokenness */
918 default: /* we didn't get one of each */
931 while (isdigit((unsigned char)*cp
))
932 altmeters
= altmeters
* 10 + (*cp
++ - '0');
934 if (*cp
== '.') { /* decimal meters */
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 */
949 while (isspace((unsigned char)*cp
) && (cp
< maxcp
))
955 siz
= precsize_aton(&cp
);
957 while (!isspace((unsigned char)*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
960 while (isspace((unsigned char)*cp
) && (cp
< maxcp
))
966 hp
= precsize_aton(&cp
);
968 while (!isspace((unsigned char)*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
971 while (isspace((unsigned char)*cp
) && (cp
< maxcp
))
977 vp
= precsize_aton(&cp
);
982 *bcp
++ = (u_int8_t
) 0; /* version byte */
990 return (16); /* size of RR in octets */
994 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
996 loc_ntoa(binary
, ascii
)
997 const u_char
*binary
;
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
;
1015 u_int8_t sizeval
, hpval
, vpval
, versionval
;
1017 char *sizestr
, *hpstr
, *vpstr
;
1025 (void) sprintf(ascii
, "; error: unknown LOC RR version");
1035 latval
= (templ
- ((unsigned)1<<31));
1038 longval
= (templ
- ((unsigned)1<<31));
1041 if (templ
< referencealt
) { /* below WGS 84 spheroid */
1042 altval
= referencealt
- templ
;
1045 altval
= templ
- referencealt
;
1055 latsecfrac
= latval
% 1000;
1056 latval
= latval
/ 1000;
1057 latsec
= latval
% 60;
1058 latval
= latval
/ 60;
1059 latmin
= latval
% 60;
1060 latval
= latval
/ 60;
1069 longsecfrac
= longval
% 1000;
1070 longval
= longval
/ 1000;
1071 longsec
= longval
% 60;
1072 longval
= longval
/ 60;
1073 longmin
= longval
% 60;
1074 longval
= longval
/ 60;
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
));
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
)
1104 /* Return the number of DNS hierarchy levels in the name. */
1106 dn_count_labels(const char *name
) {
1110 for (i
= 0, count
= 0; i
< len
; i
++) {
1111 /* XXX need to check for \. or use named's nlabels(). */
1116 /* don't count initial wildcard */
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] != '.')
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.
1135 p_secstodate (u_long secs
) {
1136 /* XXX nonreentrant */
1137 static char output
[15]; /* YYYYMMDDHHMMSS and null */
1138 time_t myclock
= secs
;
1143 mytime
= gmtime_r(&myclock
, &res
);
1145 mytime
= gmtime(&myclock
);
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
);
1155 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
1157 res_nametoclass(const char *buf
, int *successp
) {
1158 unsigned long result
;
1162 result
= sym_ston(__p_class_syms
, buf
, &success
);
1166 if (strncasecmp(buf
, "CLASS", 5) != 0 ||
1167 !isdigit((unsigned char)buf
[5]))
1170 result
= strtoul(buf
+ 5, &endptr
, 10);
1171 if (errno
== 0 && *endptr
== '\0' && result
<= 0xffffU
)
1175 *successp
= success
;
1176 return (u_int16_t
)(result
);
1180 res_nametotype(const char *buf
, int *successp
) {
1181 unsigned long result
;
1185 result
= sym_ston(__p_type_syms
, buf
, &success
);
1189 if (strncasecmp(buf
, "type", 4) != 0 ||
1190 !isdigit((unsigned char)buf
[4]))
1193 result
= strtoul(buf
+ 4, &endptr
, 10);
1194 if (errno
== 0 && *endptr
== '\0' && result
<= 0xffffU
)
1198 *successp
= success
;
1199 return (u_int16_t
)(result
);