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
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 * Portions Copyright (c) 1995 by International Business Machines, Inc.
57 * International Business Machines, Inc. (hereinafter called IBM) grants
58 * permission under its copyrights to use, copy, modify, and distribute this
59 * Software with or without fee, provided that the above copyright notice and
60 * all paragraphs of this notice appear in all copies, and that the name of IBM
61 * not be used in connection with the marketing of any product incorporating
62 * the Software or modifications thereof, without specific, written prior
65 * To the extent it has a right to do so, IBM grants an immunity from suit
66 * under its patents, if any, for the use, sale or manufacture of products to
67 * the extent that such products are used for performing Domain Name System
68 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
69 * granted for any product per se or for any other function of any product.
71 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
72 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
73 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
74 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
75 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
76 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
80 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
82 * Permission to use, copy, modify, and distribute this software for any
83 * purpose with or without fee is hereby granted, provided that the above
84 * copyright notice and this permission notice appear in all copies.
86 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
87 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
88 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
89 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
90 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
91 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
92 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
96 #if defined(LIBC_SCCS) && !defined(lint)
97 static const char sccsid
[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
98 static const char rcsid
[] = "$Id: res_debug.c,v 1.1 2006/03/01 19:01:38 majka Exp $";
99 #endif /* LIBC_SCCS and not lint */
102 #include "port_before.h"
105 #include <sys/types.h>
106 #include <sys/param.h>
107 #include <sys/socket.h>
109 #include <netinet/in.h>
110 #include <arpa/inet.h>
111 #include <arpa/nameser.h>
124 #include "port_after.h"
128 # define SPRINTF(x) strlen(sprintf/**/x)
130 # define SPRINTF(x) sprintf x
133 extern const char * __res_opcodes
[];
134 extern const char *_res_sectioncodes
[];
137 * Print the current options.
140 fp_resstat(const res_state statp
, FILE *file
) {
143 fprintf(file
, ";; res options:");
144 for (mask
= 1; mask
!= 0; mask
<<= 1)
145 if (statp
->options
& mask
)
146 fprintf(file
, " %s", p_option(mask
));
151 do_section(const res_state statp
,
152 ns_msg
*handle
, ns_sect section
,
153 int pflag
, FILE *file
)
156 static int buflen
= 2048;
162 * Print answer records.
164 sflag
= (statp
->pfcode
& pflag
);
165 if (statp
->pfcode
&& !sflag
)
168 buf
= malloc(buflen
);
170 fprintf(file
, ";; memory allocation failure\n");
174 opcode
= (ns_opcode
) ns_msg_getflag(*handle
, ns_f_opcode
);
177 if (ns_parserr(handle
, section
, rrnum
, &rr
)) {
179 fprintf(file
, ";; ns_parserr: %s\n",
181 else if (rrnum
> 0 && sflag
!= 0 &&
182 (statp
->pfcode
& RES_PRF_HEAD1
))
186 if (rrnum
== 0 && sflag
!= 0 && (statp
->pfcode
& RES_PRF_HEAD1
))
187 fprintf(file
, ";; %s SECTION:\n",
188 p_section(section
, opcode
));
189 if (section
== ns_s_qd
)
190 fprintf(file
, ";;\t%s, type = %s, class = %s\n",
192 p_type(ns_rr_type(rr
)),
193 p_class(ns_rr_class(rr
)));
194 else if (section
== ns_s_ar
&& ns_rr_type(rr
) == ns_t_opt
) {
195 u_int32_t ttl
= ns_rr_ttl(rr
);
197 "; EDNS: version: %u, udp=%u, flags=%04x\n",
198 (ttl
>>16)&0xff, ns_rr_class(rr
), ttl
&0xffff);
200 n
= ns_sprintrr(handle
, &rr
, NULL
, NULL
,
203 if (errno
== ENOSPC
) {
207 buf
= malloc(buflen
+= 1024);
210 ";; memory allocation failure\n");
215 fprintf(file
, ";; ns_sprintrr: %s\n",
230 * Print the contents of a query.
231 * This is intended to be primarily a debugging routine.
234 res_pquery(const res_state statp
, const u_char
*msg
, int len
, FILE *file
) {
236 int qdcount
, ancount
, nscount
, arcount
;
237 u_int opcode
, rcode
, id
;
239 if (ns_initparse(msg
, len
, &handle
) < 0) {
240 fprintf(file
, ";; ns_initparse: %s\n", strerror(errno
));
243 opcode
= ns_msg_getflag(handle
, ns_f_opcode
);
244 rcode
= ns_msg_getflag(handle
, ns_f_rcode
);
245 id
= ns_msg_id(handle
);
246 qdcount
= ns_msg_count(handle
, ns_s_qd
);
247 ancount
= ns_msg_count(handle
, ns_s_an
);
248 nscount
= ns_msg_count(handle
, ns_s_ns
);
249 arcount
= ns_msg_count(handle
, ns_s_ar
);
252 * Print header fields.
254 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEADX
) || rcode
)
256 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
257 __res_opcodes
[opcode
], p_rcode(rcode
), id
);
258 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEADX
))
260 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEAD2
)) {
261 fprintf(file
, "; flags:");
262 if (ns_msg_getflag(handle
, ns_f_qr
))
263 fprintf(file
, " qr");
264 if (ns_msg_getflag(handle
, ns_f_aa
))
265 fprintf(file
, " aa");
266 if (ns_msg_getflag(handle
, ns_f_tc
))
267 fprintf(file
, " tc");
268 if (ns_msg_getflag(handle
, ns_f_rd
))
269 fprintf(file
, " rd");
270 if (ns_msg_getflag(handle
, ns_f_ra
))
271 fprintf(file
, " ra");
272 if (ns_msg_getflag(handle
, ns_f_z
))
273 fprintf(file
, " ??");
274 if (ns_msg_getflag(handle
, ns_f_ad
))
275 fprintf(file
, " ad");
276 if (ns_msg_getflag(handle
, ns_f_cd
))
277 fprintf(file
, " cd");
279 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEAD1
)) {
280 fprintf(file
, "; %s: %d",
281 p_section(ns_s_qd
, opcode
), qdcount
);
282 fprintf(file
, ", %s: %d",
283 p_section(ns_s_an
, opcode
), ancount
);
284 fprintf(file
, ", %s: %d",
285 p_section(ns_s_ns
, opcode
), nscount
);
286 fprintf(file
, ", %s: %d",
287 p_section(ns_s_ar
, opcode
), arcount
);
289 if ((!statp
->pfcode
) || (statp
->pfcode
&
290 (RES_PRF_HEADX
| RES_PRF_HEAD2
| RES_PRF_HEAD1
))) {
294 * Print the various sections.
296 do_section(statp
, &handle
, ns_s_qd
, RES_PRF_QUES
, file
);
297 do_section(statp
, &handle
, ns_s_an
, RES_PRF_ANS
, file
);
298 do_section(statp
, &handle
, ns_s_ns
, RES_PRF_AUTH
, file
);
299 do_section(statp
, &handle
, ns_s_ar
, RES_PRF_ADD
, file
);
300 if (qdcount
== 0 && ancount
== 0 &&
301 nscount
== 0 && arcount
== 0)
306 p_cdnname(const u_char
*cp
, const u_char
*msg
, int len
, FILE *file
) {
307 char name
[NS_MAXDNAME
];
310 if ((n
= dn_expand(msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
320 p_cdname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
321 return (p_cdnname(cp
, msg
, NS_PACKETSZ
, file
));
324 /* Return a fully-qualified domain name from a compressed name (with
328 p_fqnname(cp
, msg
, msglen
, name
, namelen
)
329 const u_char
*cp
, *msg
;
336 if ((n
= dn_expand(msg
, cp
+ msglen
, cp
, name
, namelen
)) < 0)
338 newlen
= strlen(name
);
339 if (newlen
== 0 || name
[newlen
- 1] != '.') {
340 if (newlen
+ 1 >= namelen
) /* Lack space for final dot */
343 strcpy(name
+ newlen
, ".");
348 /* XXX: the rest of these functions need to become length-limited, too. */
351 p_fqname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
352 char name
[NS_MAXDNAME
];
355 n
= p_fqnname(cp
, msg
, NS_MAXCDNAME
, name
, sizeof name
);
363 * Names of RR classes and qclasses. Classes and qclasses are the same, except
364 * that ns_c_any is a qclass but not a class. (You can ask for records of class
365 * ns_c_any, but you can't have any records of that class in the database.)
368 const struct res_sym __res_p_class_syms
[] = {
369 {ns_c_in
, "IN", (char *)0},
370 {ns_c_chaos
, "CHAOS", (char *)0},
371 {ns_c_hs
, "HS", (char *)0},
372 {ns_c_hs
, "HESIOD", (char *)0},
373 {ns_c_any
, "ANY", (char *)0},
374 {ns_c_none
, "NONE", (char *)0},
375 {ns_c_in
, (char *)0, (char *)0}
379 * Names of message sections.
384 const struct res_sym __res_p_default_section_syms
[] = {
385 {ns_s_qd
, "QUERY", (char *)0},
386 {ns_s_an
, "ANSWER", (char *)0},
387 {ns_s_ns
, "AUTHORITY", (char *)0},
388 {ns_s_ar
, "ADDITIONAL", (char *)0},
389 {0, (char *)0, (char *)0}
395 const struct res_sym __res_p_update_section_syms
[] = {
396 {ns_s_zn
, "ZONE", (char *)0},
397 {ns_s_pr
, "PREREQUISITE", (char *)0},
398 {ns_s_ud
, "UPDATE", (char *)0},
399 {ns_s_ar
, "ADDITIONAL", (char *)0},
400 {0, (char *)0, (char *)0}
404 * Names of RR types and qtypes. Types and qtypes are the same, except
405 * that T_ANY is a qtype but not a type. (You can ask for records of type
406 * T_ANY, but you can't have any records of that type in the database.)
408 const struct res_sym __p_type_syms
[] = {
409 {ns_t_a
, "A", "address"},
410 {ns_t_ns
, "NS", "name server"},
411 {ns_t_md
, "MD", "mail destination (deprecated)"},
412 {ns_t_mf
, "MF", "mail forwarder (deprecated)"},
413 {ns_t_cname
, "CNAME", "canonical name"},
414 {ns_t_soa
, "SOA", "start of authority"},
415 {ns_t_mb
, "MB", "mailbox"},
416 {ns_t_mg
, "MG", "mail group member"},
417 {ns_t_mr
, "MR", "mail rename"},
418 {ns_t_null
, "NULL", "null"},
419 {ns_t_wks
, "WKS", "well-known service (deprecated)"},
420 {ns_t_ptr
, "PTR", "domain name pointer"},
421 {ns_t_hinfo
, "HINFO", "host information"},
422 {ns_t_minfo
, "MINFO", "mailbox information"},
423 {ns_t_mx
, "MX", "mail exchanger"},
424 {ns_t_txt
, "TXT", "text"},
425 {ns_t_rp
, "RP", "responsible person"},
426 {ns_t_afsdb
, "AFSDB", "DCE or AFS server"},
427 {ns_t_x25
, "X25", "X25 address"},
428 {ns_t_isdn
, "ISDN", "ISDN address"},
429 {ns_t_rt
, "RT", "router"},
430 {ns_t_nsap
, "NSAP", "nsap address"},
431 {ns_t_nsap_ptr
, "NSAP_PTR", "domain name pointer"},
432 {ns_t_sig
, "SIG", "signature"},
433 {ns_t_key
, "KEY", "key"},
434 {ns_t_px
, "PX", "mapping information"},
435 {ns_t_gpos
, "GPOS", "geographical position (withdrawn)"},
436 {ns_t_aaaa
, "AAAA", "IPv6 address"},
437 {ns_t_loc
, "LOC", "location"},
438 {ns_t_nxt
, "NXT", "next valid name (unimplemented)"},
439 {ns_t_eid
, "EID", "endpoint identifier (unimplemented)"},
440 {ns_t_nimloc
, "NIMLOC", "NIMROD locator (unimplemented)"},
441 {ns_t_srv
, "SRV", "server selection"},
442 {ns_t_atma
, "ATMA", "ATM address (unimplemented)"},
443 {ns_t_tkey
, "TKEY", "tkey"},
444 {ns_t_tsig
, "TSIG", "transaction signature"},
445 {ns_t_ixfr
, "IXFR", "incremental zone transfer"},
446 {ns_t_axfr
, "AXFR", "zone transfer"},
447 {ns_t_zxfr
, "ZXFR", "compressed zone transfer"},
448 {ns_t_mailb
, "MAILB", "mailbox-related data (deprecated)"},
449 {ns_t_maila
, "MAILA", "mail agent (deprecated)"},
450 {ns_t_naptr
, "NAPTR", "URN Naming Authority"},
451 {ns_t_kx
, "KX", "Key Exchange"},
452 {ns_t_cert
, "CERT", "Certificate"},
453 {ns_t_a6
, "A6", "IPv6 Address"},
454 {ns_t_dname
, "DNAME", "dname"},
455 {ns_t_sink
, "SINK", "Kitchen Sink (experimental)"},
456 {ns_t_opt
, "OPT", "EDNS Options"},
457 {ns_t_any
, "ANY", "\"any\""},
462 * Names of DNS rcodes.
465 const struct res_sym __res_p_rcode_syms
[] = {
466 {ns_r_noerror
, "NOERROR", "no error"},
467 {ns_r_formerr
, "FORMERR", "format error"},
468 {ns_r_servfail
, "SERVFAIL", "server failed"},
469 {ns_r_nxdomain
, "NXDOMAIN", "no such domain name"},
470 {ns_r_notimpl
, "NOTIMP", "not implemented"},
471 {ns_r_refused
, "REFUSED", "refused"},
472 {ns_r_yxdomain
, "YXDOMAIN", "domain name exists"},
473 {ns_r_yxrrset
, "YXRRSET", "rrset exists"},
474 {ns_r_nxrrset
, "NXRRSET", "rrset doesn't exist"},
475 {ns_r_notauth
, "NOTAUTH", "not authoritative"},
476 {ns_r_notzone
, "NOTZONE", "Not in zone"},
478 {ns_r_badsig
, "BADSIG", "bad signature"},
479 {ns_r_badkey
, "BADKEY", "bad key"},
480 {ns_r_badtime
, "BADTIME", "bad time"},
485 sym_ston(const struct res_sym
*syms
, const char *name
, int *success
) {
486 for ((void)NULL
; syms
->name
!= 0; syms
++) {
487 if (strcasecmp (name
, syms
->name
) == 0) {
490 return (syms
->number
);
495 return (syms
->number
); /* The default value. */
499 sym_ntos(const struct res_sym
*syms
, int number
, int *success
) {
500 static char unname
[20];
502 for ((void)NULL
; syms
->name
!= 0; syms
++) {
503 if (number
== syms
->number
) {
510 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
517 sym_ntop(const struct res_sym
*syms
, int number
, int *success
) {
518 static char unname
[20];
520 for ((void)NULL
; syms
->name
!= 0; syms
++) {
521 if (number
== syms
->number
) {
524 return (syms
->humanname
);
527 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
534 * Return a string for the type.
540 static char typebuf
[20];
542 result
= sym_ntos(__p_type_syms
, type
, &success
);
545 if (type
< 0 || type
> 0xfff)
547 sprintf(typebuf
, "TYPE%d", type
);
552 * Return a string for the type.
555 p_section(int section
, int opcode
) {
556 const struct res_sym
*symbols
;
560 symbols
= __res_p_update_section_syms
;
563 symbols
= __res_p_default_section_syms
;
566 return (sym_ntos(symbols
, section
, (int *)0));
570 * Return a mnemonic for class.
576 static char classbuf
[20];
578 result
= sym_ntos(__res_p_class_syms
, class, &success
);
581 if (class < 0 || class > 0xfff)
583 sprintf(classbuf
, "CLASS%d", class);
588 * Return a mnemonic for an option
591 p_option(u_long option
) {
592 static char nbuf
[40];
595 case RES_INIT
: return "init";
596 case RES_DEBUG
: return "debug";
597 case RES_AAONLY
: return "aaonly(unimpl)";
598 case RES_USEVC
: return "usevc";
599 case RES_PRIMARY
: return "primry(unimpl)";
600 case RES_IGNTC
: return "igntc";
601 case RES_RECURSE
: return "recurs";
602 case RES_DEFNAMES
: return "defnam";
603 case RES_STAYOPEN
: return "styopn";
604 case RES_DNSRCH
: return "dnsrch";
605 case RES_INSECURE1
: return "insecure1";
606 case RES_INSECURE2
: return "insecure2";
607 case RES_NOALIASES
: return "noaliases";
608 case RES_USE_INET6
: return "inet6";
609 #ifdef RES_USE_EDNS0 /* KAME extension */
610 case RES_USE_EDNS0
: return "edns0";
613 case RES_USE_A6
: return "a6";
616 case RES_USE_DNAME
: return "dname";
618 #ifdef RES_USE_DNSSEC
619 case RES_USE_DNSSEC
: return "dnssec";
621 #ifdef RES_NOTLDQUERY
622 case RES_NOTLDQUERY
: return "no-tld-query";
625 /* XXX nonreentrant */
626 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
632 * Return a mnemonic for a time to live.
635 p_time(u_int32_t value
) {
636 static char nbuf
[40]; /* XXX nonreentrant */
638 if (ns_format_ttl(value
, nbuf
, sizeof nbuf
) < 0)
639 sprintf(nbuf
, "%u", value
);
644 * Return a string for the rcode.
648 return (sym_ntos(__res_p_rcode_syms
, rcode
, (int *)0));
652 * routines to convert between on-the-wire RR format and zone file format.
653 * Does not contain conversion to/from decimal degrees; divide or multiply
654 * by 60*60*1000 for that.
657 static unsigned int poweroften
[10] = {1, 10, 100, 1000, 10000, 100000,
658 1000000,10000000,100000000,1000000000};
660 /* takes an XeY precision/size value, returns a string representation. */
665 static char retbuf
[sizeof "90000000.00"]; /* XXX nonreentrant */
667 int mantissa
, exponent
;
669 mantissa
= (int)((prec
>> 4) & 0x0f) % 10;
670 exponent
= (int)((prec
>> 0) & 0x0f) % 10;
672 val
= mantissa
* poweroften
[exponent
];
674 (void) sprintf(retbuf
, "%lu.%.2lu", val
/100, val%100
);
678 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
680 precsize_aton(const char **strptr
) {
681 unsigned int mval
= 0, cmval
= 0;
689 while (isdigit((unsigned char)*cp
))
690 mval
= mval
* 10 + (*cp
++ - '0');
692 if (*cp
== '.') { /* centimeters */
694 if (isdigit((unsigned char)*cp
)) {
695 cmval
= (*cp
++ - '0') * 10;
696 if (isdigit((unsigned char)*cp
)) {
697 cmval
+= (*cp
++ - '0');
701 cmval
= (mval
* 100) + cmval
;
703 for (exponent
= 0; exponent
< 9; exponent
++)
704 if (cmval
< poweroften
[exponent
+1])
707 mantissa
= cmval
/ poweroften
[exponent
];
711 retval
= (mantissa
<< 4) | exponent
;
718 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
720 latlon2ul(const char **latlonstrptr
, int *which
) {
723 int deg
= 0, min
= 0, secs
= 0, secsfrac
= 0;
727 while (isdigit((unsigned char)*cp
))
728 deg
= deg
* 10 + (*cp
++ - '0');
730 while (isspace((unsigned char)*cp
))
733 if (!(isdigit((unsigned char)*cp
)))
736 while (isdigit((unsigned char)*cp
))
737 min
= min
* 10 + (*cp
++ - '0');
739 while (isspace((unsigned char)*cp
))
742 if (!(isdigit((unsigned char)*cp
)))
745 while (isdigit((unsigned char)*cp
))
746 secs
= secs
* 10 + (*cp
++ - '0');
748 if (*cp
== '.') { /* decimal seconds */
750 if (isdigit((unsigned char)*cp
)) {
751 secsfrac
= (*cp
++ - '0') * 100;
752 if (isdigit((unsigned char)*cp
)) {
753 secsfrac
+= (*cp
++ - '0') * 10;
754 if (isdigit((unsigned char)*cp
)) {
755 secsfrac
+= (*cp
++ - '0');
761 while (!isspace((unsigned char)*cp
)) /* if any trailing garbage */
764 while (isspace((unsigned char)*cp
))
771 retval
= ((unsigned)1<<31)
772 + (((((deg
* 60) + min
) * 60) + secs
) * 1000)
777 retval
= ((unsigned)1<<31)
778 - (((((deg
* 60) + min
) * 60) + secs
) * 1000)
782 retval
= 0; /* invalid value -- indicates error */
789 *which
= 1; /* latitude */
793 *which
= 2; /* longitude */
796 *which
= 0; /* error */
800 cp
++; /* skip the hemisphere */
802 while (!isspace((unsigned char)*cp
)) /* if any trailing garbage */
805 while (isspace((unsigned char)*cp
)) /* move to next field */
813 /* converts a zone file representation in a string to an RDATA on-the-wire
816 loc_aton(ascii
, binary
)
820 const char *cp
, *maxcp
;
823 u_int32_t latit
= 0, longit
= 0, alt
= 0;
824 u_int32_t lltemp1
= 0, lltemp2
= 0;
825 int altmeters
= 0, altfrac
= 0, altsign
= 1;
826 u_int8_t hp
= 0x16; /* default = 1e6 cm = 10000.00m = 10km */
827 u_int8_t vp
= 0x13; /* default = 1e3 cm = 10.00m */
828 u_int8_t siz
= 0x12; /* default = 1e2 cm = 1.00m */
829 int which1
= 0, which2
= 0;
832 maxcp
= cp
+ strlen(ascii
);
834 lltemp1
= latlon2ul(&cp
, &which1
);
836 lltemp2
= latlon2ul(&cp
, &which2
);
838 switch (which1
+ which2
) {
839 case 3: /* 1 + 2, the only valid combination */
840 if ((which1
== 1) && (which2
== 2)) { /* normal case */
843 } else if ((which1
== 2) && (which2
== 1)) { /* reversed */
846 } else { /* some kind of brokenness */
850 default: /* we didn't get one of each */
863 while (isdigit((unsigned char)*cp
))
864 altmeters
= altmeters
* 10 + (*cp
++ - '0');
866 if (*cp
== '.') { /* decimal meters */
868 if (isdigit((unsigned char)*cp
)) {
869 altfrac
= (*cp
++ - '0') * 10;
870 if (isdigit((unsigned char)*cp
)) {
871 altfrac
+= (*cp
++ - '0');
876 alt
= (10000000 + (altsign
* (altmeters
* 100 + altfrac
)));
878 while (!isspace((unsigned char)*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
881 while (isspace((unsigned char)*cp
) && (cp
< maxcp
))
887 siz
= precsize_aton(&cp
);
889 while (!isspace((unsigned char)*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
892 while (isspace((unsigned char)*cp
) && (cp
< maxcp
))
898 hp
= precsize_aton(&cp
);
900 while (!isspace((unsigned char)*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
903 while (isspace((unsigned char)*cp
) && (cp
< maxcp
))
909 vp
= precsize_aton(&cp
);
914 *bcp
++ = (u_int8_t
) 0; /* version byte */
919 NS_PUT32(longit
,bcp
);
922 return (16); /* size of RR in octets */
925 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
927 loc_ntoa(binary
, ascii
)
928 const u_char
*binary
;
931 static const char *error
= "?";
932 static char tmpbuf
[sizeof
933 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
934 const u_char
*cp
= binary
;
936 int latdeg
, latmin
, latsec
, latsecfrac
;
937 int longdeg
, longmin
, longsec
, longsecfrac
;
938 char northsouth
, eastwest
;
940 int altmeters
, altfrac
;
942 const u_int32_t referencealt
= 100000 * 100;
944 int32_t latval
, longval
, altval
;
946 u_int8_t sizeval
, hpval
, vpval
, versionval
;
948 char *sizestr
, *hpstr
, *vpstr
;
956 (void) sprintf(ascii
, "; error: unknown LOC RR version");
966 latval
= (templ
- ((unsigned)1<<31));
969 longval
= (templ
- ((unsigned)1<<31));
972 if (templ
< referencealt
) { /* below WGS 84 spheroid */
973 altval
= referencealt
- templ
;
976 altval
= templ
- referencealt
;
986 latsecfrac
= latval
% 1000;
987 latval
= latval
/ 1000;
988 latsec
= latval
% 60;
989 latval
= latval
/ 60;
990 latmin
= latval
% 60;
991 latval
= latval
/ 60;
1000 longsecfrac
= longval
% 1000;
1001 longval
= longval
/ 1000;
1002 longsec
= longval
% 60;
1003 longval
= longval
/ 60;
1004 longmin
= longval
% 60;
1005 longval
= longval
/ 60;
1008 altfrac
= altval
% 100;
1009 altmeters
= (altval
/ 100);
1011 sizestr
= strdup(precsize_ntoa(sizeval
));
1012 hpstr
= strdup(precsize_ntoa(hpval
));
1013 vpstr
= strdup(precsize_ntoa(vpval
));
1016 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
1017 latdeg
, latmin
, latsec
, latsecfrac
, northsouth
,
1018 longdeg
, longmin
, longsec
, longsecfrac
, eastwest
,
1019 altsign
, altmeters
, altfrac
,
1020 (sizestr
!= NULL
) ? sizestr
: error
,
1021 (hpstr
!= NULL
) ? hpstr
: error
,
1022 (vpstr
!= NULL
) ? vpstr
: error
);
1024 if (sizestr
!= NULL
)
1035 /* Return the number of DNS hierarchy levels in the name. */
1037 dn_count_labels(const char *name
) {
1041 for (i
= 0, count
= 0; i
< len
; i
++) {
1042 /* XXX need to check for \. or use named's nlabels(). */
1047 /* don't count initial wildcard */
1052 /* don't count the null label for root. */
1053 /* if terminating '.' not found, must adjust */
1054 /* count to include last label */
1055 if (len
> 0 && name
[len
-1] != '.')
1062 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1063 * SIG records are required to be printed like this, by the Secure DNS RFC.
1066 p_secstodate (u_long secs
) {
1067 /* XXX nonreentrant */
1068 static char output
[15]; /* YYYYMMDDHHMMSS and null */
1069 time_t clock
= secs
;
1073 gmtime_r(&clock
, &time
);
1075 time
= gmtime(&clock
);
1077 time
->tm_year
+= 1900;
1079 sprintf(output
, "%04d%02d%02d%02d%02d%02d",
1080 time
->tm_year
, time
->tm_mon
, time
->tm_mday
,
1081 time
->tm_hour
, time
->tm_min
, time
->tm_sec
);
1086 res_nametoclass(const char *buf
, int *successp
) {
1087 unsigned long result
;
1091 result
= sym_ston(__res_p_class_syms
, buf
, &success
);
1095 if (strncasecmp(buf
, "CLASS", 5) != 0 ||
1096 !isdigit((unsigned char)buf
[5]))
1098 result
= strtoul(buf
+ 5, &endptr
, 10);
1099 if (*endptr
== '\0' && result
<= 0xffff)
1103 *successp
= success
;
1108 res_nametotype(const char *buf
, int *successp
) {
1109 unsigned long result
;
1113 result
= sym_ston(__p_type_syms
, buf
, &success
);
1117 if (strncasecmp(buf
, "type", 4) != 0 ||
1118 !isdigit((unsigned char)buf
[4]))
1120 result
= strtoul(buf
+ 4, &endptr
, 10);
1121 if (*endptr
== '\0' && result
<= 0xffff)
1125 *successp
= success
;