Update.
[glibc.git] / resolv / res_debug.c
blob7887dc26a05665c9c674e1ba8bebeb426b599959
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 #if defined(LIBC_SCCS) && !defined(lint)
93 static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
94 static const char rcsid[] = "$BINDId: res_debug.c,v 8.34 2000/02/29 05:30:55 vixie Exp $";
95 #endif /* LIBC_SCCS and not lint */
97 #include <sys/types.h>
98 #include <sys/param.h>
99 #include <sys/socket.h>
101 #include <netinet/in.h>
102 #include <arpa/inet.h>
103 #include <arpa/nameser.h>
105 #include <ctype.h>
106 #include <errno.h>
107 #include <math.h>
108 #include <netdb.h>
109 #include <resolv.h>
110 #include <stdio.h>
111 #include <stdlib.h>
112 #include <string.h>
113 #include <time.h>
115 #ifdef SPRINTF_CHAR
116 # define SPRINTF(x) strlen(sprintf/**/x)
117 #else
118 # define SPRINTF(x) sprintf x
119 #endif
121 extern const char *_res_opcodes[];
122 extern const char *_res_sectioncodes[];
125 * Print the current options.
127 void
128 fp_resstat(const res_state statp, FILE *file) {
129 u_long mask;
131 fprintf(file, ";; res options:");
132 for (mask = 1; mask != 0; mask <<= 1)
133 if (statp->options & mask)
134 fprintf(file, " %s", p_option(mask));
135 putc('\n', file);
138 static void
139 do_section(const res_state statp,
140 ns_msg *handle, ns_sect section,
141 int pflag, FILE *file)
143 int n, sflag, rrnum;
144 static int buflen = 2048;
145 char *buf;
146 ns_opcode opcode;
147 ns_rr rr;
150 * Print answer records.
152 sflag = (statp->pfcode & pflag);
153 if (statp->pfcode && !sflag)
154 return;
156 buf = malloc(buflen);
157 if (buf == NULL) {
158 fprintf(file, ";; memory allocation failure\n");
159 return;
162 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
163 rrnum = 0;
164 for (;;) {
165 if (ns_parserr(handle, section, rrnum, &rr)) {
166 if (errno != ENODEV)
167 fprintf(file, ";; ns_parserr: %s\n",
168 strerror(errno));
169 else if (rrnum > 0 && sflag != 0 &&
170 (statp->pfcode & RES_PRF_HEAD1))
171 putc('\n', file);
172 goto cleanup;
174 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
175 fprintf(file, ";; %s SECTION:\n",
176 p_section(section, opcode));
177 if (section == ns_s_qd)
178 fprintf(file, ";;\t%s, type = %s, class = %s\n",
179 ns_rr_name(rr),
180 p_type(ns_rr_type(rr)),
181 p_class(ns_rr_class(rr)));
182 else {
183 n = ns_sprintrr(handle, &rr, NULL, NULL,
184 buf, buflen);
185 if (n < 0) {
186 if (errno == ENOSPC) {
187 free(buf);
188 buf = NULL;
189 if (buflen < 131072)
190 buf = malloc(buflen += 1024);
191 if (buf == NULL) {
192 fprintf(file,
193 ";; memory allocation failure\n");
194 return;
196 continue;
198 fprintf(file, ";; ns_sprintrr: %s\n",
199 strerror(errno));
200 goto cleanup;
202 fputs(buf, file);
203 fputc('\n', file);
205 rrnum++;
207 cleanup:
208 if (buf != NULL)
209 free(buf);
213 * Print the contents of a query.
214 * This is intended to be primarily a debugging routine.
216 void
217 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
218 ns_msg handle;
219 int qdcount, ancount, nscount, arcount;
220 u_int opcode, rcode, id;
222 if (ns_initparse(msg, len, &handle) < 0) {
223 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
224 return;
226 opcode = ns_msg_getflag(handle, ns_f_opcode);
227 rcode = ns_msg_getflag(handle, ns_f_rcode);
228 id = ns_msg_id(handle);
229 qdcount = ns_msg_count(handle, ns_s_qd);
230 ancount = ns_msg_count(handle, ns_s_an);
231 nscount = ns_msg_count(handle, ns_s_ns);
232 arcount = ns_msg_count(handle, ns_s_ar);
235 * Print header fields.
237 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
238 fprintf(file,
239 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
240 _res_opcodes[opcode], p_rcode(rcode), id);
241 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
242 putc(';', file);
243 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
244 fprintf(file, "; flags:");
245 if (ns_msg_getflag(handle, ns_f_qr))
246 fprintf(file, " qr");
247 if (ns_msg_getflag(handle, ns_f_aa))
248 fprintf(file, " aa");
249 if (ns_msg_getflag(handle, ns_f_tc))
250 fprintf(file, " tc");
251 if (ns_msg_getflag(handle, ns_f_rd))
252 fprintf(file, " rd");
253 if (ns_msg_getflag(handle, ns_f_ra))
254 fprintf(file, " ra");
255 if (ns_msg_getflag(handle, ns_f_z))
256 fprintf(file, " ??");
257 if (ns_msg_getflag(handle, ns_f_ad))
258 fprintf(file, " ad");
259 if (ns_msg_getflag(handle, ns_f_cd))
260 fprintf(file, " cd");
262 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
263 fprintf(file, "; %s: %d",
264 p_section(ns_s_qd, opcode), qdcount);
265 fprintf(file, ", %s: %d",
266 p_section(ns_s_an, opcode), ancount);
267 fprintf(file, ", %s: %d",
268 p_section(ns_s_ns, opcode), nscount);
269 fprintf(file, ", %s: %d",
270 p_section(ns_s_ar, opcode), arcount);
272 if ((!statp->pfcode) || (statp->pfcode &
273 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
274 putc('\n',file);
277 * Print the various sections.
279 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
280 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
281 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
282 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
283 if (qdcount == 0 && ancount == 0 &&
284 nscount == 0 && arcount == 0)
285 putc('\n', file);
288 const u_char *
289 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
290 char name[MAXDNAME];
291 int n;
293 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
294 return (NULL);
295 if (name[0] == '\0')
296 putc('.', file);
297 else
298 fputs(name, file);
299 return (cp + n);
302 const u_char *
303 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
304 return (p_cdnname(cp, msg, PACKETSZ, file));
307 /* Return a fully-qualified domain name from a compressed name (with
308 length supplied). */
310 const u_char *
311 p_fqnname(cp, msg, msglen, name, namelen)
312 const u_char *cp, *msg;
313 int msglen;
314 char *name;
315 int namelen;
317 int n, newlen;
319 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
320 return (NULL);
321 newlen = strlen(name);
322 if (newlen == 0 || name[newlen - 1] != '.') {
323 if (newlen + 1 >= namelen) /* Lack space for final dot */
324 return (NULL);
325 else
326 strcpy(name + newlen, ".");
328 return (cp + n);
331 /* XXX: the rest of these functions need to become length-limited, too. */
333 const u_char *
334 p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
335 char name[MAXDNAME];
336 const u_char *n;
338 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
339 if (n == NULL)
340 return (NULL);
341 fputs(name, file);
342 return (n);
346 * Names of RR classes and qclasses. Classes and qclasses are the same, except
347 * that C_ANY is a qclass but not a class. (You can ask for records of class
348 * C_ANY, but you can't have any records of that class in the database.)
350 const struct res_sym __p_class_syms[] = {
351 {C_IN, "IN"},
352 {C_CHAOS, "CHAOS"},
353 {C_HS, "HS"},
354 {C_HS, "HESIOD"},
355 {C_ANY, "ANY"},
356 {C_NONE, "NONE"},
357 {C_IN, (char *)0}
361 * Names of message sections.
363 const struct res_sym __p_default_section_syms[] = {
364 {ns_s_qd, "QUERY"},
365 {ns_s_an, "ANSWER"},
366 {ns_s_ns, "AUTHORITY"},
367 {ns_s_ar, "ADDITIONAL"},
368 {0, (char *)0}
371 const struct res_sym __p_update_section_syms[] = {
372 {S_ZONE, "ZONE"},
373 {S_PREREQ, "PREREQUISITE"},
374 {S_UPDATE, "UPDATE"},
375 {S_ADDT, "ADDITIONAL"},
376 {0, (char *)0}
379 const struct res_sym __p_key_syms[] = {
380 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
381 {NS_ALG_DH, "DH", "Diffie Hellman"},
382 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
383 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
384 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
385 {0, NULL, NULL}
388 const struct res_sym __p_cert_syms[] = {
389 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
390 {cert_t_spki, "SPKI", "SPKI certificate"},
391 {cert_t_pgp, "PGP", "PGP certificate"},
392 {cert_t_url, "URL", "URL Private"},
393 {cert_t_oid, "OID", "OID Private"},
394 {0, NULL, NULL}
398 * Names of RR types and qtypes. Types and qtypes are the same, except
399 * that T_ANY is a qtype but not a type. (You can ask for records of type
400 * T_ANY, but you can't have any records of that type in the database.)
402 const struct res_sym __p_type_syms[] = {
403 {ns_t_a, "A", "address"},
404 {ns_t_ns, "NS", "name server"},
405 {ns_t_md, "MD", "mail destination (deprecated)"},
406 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
407 {ns_t_cname, "CNAME", "canonical name"},
408 {ns_t_soa, "SOA", "start of authority"},
409 {ns_t_mb, "MB", "mailbox"},
410 {ns_t_mg, "MG", "mail group member"},
411 {ns_t_mr, "MR", "mail rename"},
412 {ns_t_null, "NULL", "null"},
413 {ns_t_wks, "WKS", "well-known service (deprecated)"},
414 {ns_t_ptr, "PTR", "domain name pointer"},
415 {ns_t_hinfo, "HINFO", "host information"},
416 {ns_t_minfo, "MINFO", "mailbox information"},
417 {ns_t_mx, "MX", "mail exchanger"},
418 {ns_t_txt, "TXT", "text"},
419 {ns_t_rp, "RP", "responsible person"},
420 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
421 {ns_t_x25, "X25", "X25 address"},
422 {ns_t_isdn, "ISDN", "ISDN address"},
423 {ns_t_rt, "RT", "router"},
424 {ns_t_nsap, "NSAP", "nsap address"},
425 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
426 {ns_t_sig, "SIG", "signature"},
427 {ns_t_key, "KEY", "key"},
428 {ns_t_px, "PX", "mapping information"},
429 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
430 {ns_t_aaaa, "AAAA", "IPv6 address"},
431 {ns_t_loc, "LOC", "location"},
432 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
433 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
434 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
435 {ns_t_srv, "SRV", "server selection"},
436 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
437 {ns_t_tsig, "TSIG", "transaction signature"},
438 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
439 {ns_t_axfr, "AXFR", "zone transfer"},
440 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
441 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
442 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
443 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
444 {ns_t_kx, "KX", "Key Exchange"},
445 {ns_t_cert, "CERT", "Certificate"},
446 {ns_t_any, "ANY", "\"any\""},
447 {0, NULL, NULL}
451 * Names of DNS rcodes.
453 const struct res_sym __p_rcode_syms[] = {
454 {ns_r_noerror, "NOERROR", "no error"},
455 {ns_r_formerr, "FORMERR", "format error"},
456 {ns_r_servfail, "SERVFAIL", "server failed"},
457 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
458 {ns_r_notimpl, "NOTIMP", "not implemented"},
459 {ns_r_refused, "REFUSED", "refused"},
460 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
461 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
462 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
463 {ns_r_notauth, "NOTAUTH", "not authoritative"},
464 {ns_r_notzone, "NOTZONE", "Not in zone"},
465 {ns_r_max, "", ""},
466 {ns_r_badsig, "BADSIG", "bad signature"},
467 {ns_r_badkey, "BADKEY", "bad key"},
468 {ns_r_badtime, "BADTIME", "bad time"},
469 {0, NULL, NULL}
473 sym_ston(const struct res_sym *syms, const char *name, int *success) {
474 for ((void)NULL; syms->name != 0; syms++) {
475 if (strcasecmp (name, syms->name) == 0) {
476 if (success)
477 *success = 1;
478 return (syms->number);
481 if (success)
482 *success = 0;
483 return (syms->number); /* The default value. */
486 const char *
487 sym_ntos(const struct res_sym *syms, int number, int *success) {
488 static char unname[20];
490 for ((void)NULL; syms->name != 0; syms++) {
491 if (number == syms->number) {
492 if (success)
493 *success = 1;
494 return (syms->name);
498 sprintf(unname, "%d", number); /* XXX nonreentrant */
499 if (success)
500 *success = 0;
501 return (unname);
504 const char *
505 sym_ntop(const struct res_sym *syms, int number, int *success) {
506 static char unname[20];
508 for ((void)NULL; syms->name != 0; syms++) {
509 if (number == syms->number) {
510 if (success)
511 *success = 1;
512 return (syms->humanname);
515 sprintf(unname, "%d", number); /* XXX nonreentrant */
516 if (success)
517 *success = 0;
518 return (unname);
522 * Return a string for the type.
524 const char *
525 p_type(int type) {
526 return (sym_ntos(__p_type_syms, type, (int *)0));
530 * Return a string for the type.
532 const char *
533 p_section(int section, int opcode) {
534 const struct res_sym *symbols;
536 switch (opcode) {
537 case ns_o_update:
538 symbols = __p_update_section_syms;
539 break;
540 default:
541 symbols = __p_default_section_syms;
542 break;
544 return (sym_ntos(symbols, section, (int *)0));
548 * Return a mnemonic for class.
550 const char *
551 p_class(int class) {
552 return (sym_ntos(__p_class_syms, class, (int *)0));
556 * Return a mnemonic for an option
558 const char *
559 p_option(u_long option) {
560 static char nbuf[40];
562 switch (option) {
563 case RES_INIT: return "init";
564 case RES_DEBUG: return "debug";
565 case RES_AAONLY: return "aaonly(unimpl)";
566 case RES_USEVC: return "usevc";
567 case RES_PRIMARY: return "primry(unimpl)";
568 case RES_IGNTC: return "igntc";
569 case RES_RECURSE: return "recurs";
570 case RES_DEFNAMES: return "defnam";
571 case RES_STAYOPEN: return "styopn";
572 case RES_DNSRCH: return "dnsrch";
573 case RES_INSECURE1: return "insecure1";
574 case RES_INSECURE2: return "insecure2";
575 /* XXX nonreentrant */
576 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
577 return (nbuf);
582 * Return a mnemonic for a time to live.
584 const char *
585 p_time(u_int32_t value) {
586 static char nbuf[40]; /* XXX nonreentrant */
588 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
589 sprintf(nbuf, "%u", value);
590 return (nbuf);
594 * Return a string for the rcode.
596 const char *
597 p_rcode(int rcode) {
598 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
602 * routines to convert between on-the-wire RR format and zone file format.
603 * Does not contain conversion to/from decimal degrees; divide or multiply
604 * by 60*60*1000 for that.
607 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
608 1000000,10000000,100000000,1000000000};
610 /* takes an XeY precision/size value, returns a string representation. */
611 static const char *
612 precsize_ntoa(prec)
613 u_int8_t prec;
615 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
616 unsigned long val;
617 int mantissa, exponent;
619 mantissa = (int)((prec >> 4) & 0x0f) % 10;
620 exponent = (int)((prec >> 0) & 0x0f) % 10;
622 val = mantissa * poweroften[exponent];
624 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
625 return (retbuf);
628 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
629 static u_int8_t
630 precsize_aton(strptr)
631 const char **strptr;
633 unsigned int mval = 0, cmval = 0;
634 u_int8_t retval = 0;
635 const char *cp;
636 int exponent;
637 int mantissa;
639 cp = *strptr;
641 while (isdigit(*cp))
642 mval = mval * 10 + (*cp++ - '0');
644 if (*cp == '.') { /* centimeters */
645 cp++;
646 if (isdigit(*cp)) {
647 cmval = (*cp++ - '0') * 10;
648 if (isdigit(*cp)) {
649 cmval += (*cp++ - '0');
653 cmval = (mval * 100) + cmval;
655 for (exponent = 0; exponent < 9; exponent++)
656 if (cmval < poweroften[exponent+1])
657 break;
659 mantissa = cmval / poweroften[exponent];
660 if (mantissa > 9)
661 mantissa = 9;
663 retval = (mantissa << 4) | exponent;
665 *strptr = cp;
667 return (retval);
670 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
671 static u_int32_t
672 latlon2ul(latlonstrptr,which)
673 const char **latlonstrptr;
674 int *which;
676 const char *cp;
677 u_int32_t retval;
678 int deg = 0, min = 0, secs = 0, secsfrac = 0;
680 cp = *latlonstrptr;
682 while (isdigit(*cp))
683 deg = deg * 10 + (*cp++ - '0');
685 while (isspace(*cp))
686 cp++;
688 if (!(isdigit(*cp)))
689 goto fndhemi;
691 while (isdigit(*cp))
692 min = min * 10 + (*cp++ - '0');
694 while (isspace(*cp))
695 cp++;
697 if (!(isdigit(*cp)))
698 goto fndhemi;
700 while (isdigit(*cp))
701 secs = secs * 10 + (*cp++ - '0');
703 if (*cp == '.') { /* decimal seconds */
704 cp++;
705 if (isdigit(*cp)) {
706 secsfrac = (*cp++ - '0') * 100;
707 if (isdigit(*cp)) {
708 secsfrac += (*cp++ - '0') * 10;
709 if (isdigit(*cp)) {
710 secsfrac += (*cp++ - '0');
716 while (!isspace(*cp)) /* if any trailing garbage */
717 cp++;
719 while (isspace(*cp))
720 cp++;
722 fndhemi:
723 switch (*cp) {
724 case 'N': case 'n':
725 case 'E': case 'e':
726 retval = ((unsigned)1<<31)
727 + (((((deg * 60) + min) * 60) + secs) * 1000)
728 + secsfrac;
729 break;
730 case 'S': case 's':
731 case 'W': case 'w':
732 retval = ((unsigned)1<<31)
733 - (((((deg * 60) + min) * 60) + secs) * 1000)
734 - secsfrac;
735 break;
736 default:
737 retval = 0; /* invalid value -- indicates error */
738 break;
741 switch (*cp) {
742 case 'N': case 'n':
743 case 'S': case 's':
744 *which = 1; /* latitude */
745 break;
746 case 'E': case 'e':
747 case 'W': case 'w':
748 *which = 2; /* longitude */
749 break;
750 default:
751 *which = 0; /* error */
752 break;
755 cp++; /* skip the hemisphere */
757 while (!isspace(*cp)) /* if any trailing garbage */
758 cp++;
760 while (isspace(*cp)) /* move to next field */
761 cp++;
763 *latlonstrptr = cp;
765 return (retval);
768 /* converts a zone file representation in a string to an RDATA on-the-wire
769 * representation. */
771 loc_aton(ascii, binary)
772 const char *ascii;
773 u_char *binary;
775 const char *cp, *maxcp;
776 u_char *bcp;
778 u_int32_t latit = 0, longit = 0, alt = 0;
779 u_int32_t lltemp1 = 0, lltemp2 = 0;
780 int altmeters = 0, altfrac = 0, altsign = 1;
781 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
782 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
783 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
784 int which1 = 0, which2 = 0;
786 cp = ascii;
787 maxcp = cp + strlen(ascii);
789 lltemp1 = latlon2ul(&cp, &which1);
791 lltemp2 = latlon2ul(&cp, &which2);
793 switch (which1 + which2) {
794 case 3: /* 1 + 2, the only valid combination */
795 if ((which1 == 1) && (which2 == 2)) { /* normal case */
796 latit = lltemp1;
797 longit = lltemp2;
798 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
799 longit = lltemp1;
800 latit = lltemp2;
801 } else { /* some kind of brokenness */
802 return (0);
804 break;
805 default: /* we didn't get one of each */
806 return (0);
809 /* altitude */
810 if (*cp == '-') {
811 altsign = -1;
812 cp++;
815 if (*cp == '+')
816 cp++;
818 while (isdigit(*cp))
819 altmeters = altmeters * 10 + (*cp++ - '0');
821 if (*cp == '.') { /* decimal meters */
822 cp++;
823 if (isdigit(*cp)) {
824 altfrac = (*cp++ - '0') * 10;
825 if (isdigit(*cp)) {
826 altfrac += (*cp++ - '0');
831 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
833 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
834 cp++;
836 while (isspace(*cp) && (cp < maxcp))
837 cp++;
839 if (cp >= maxcp)
840 goto defaults;
842 siz = precsize_aton(&cp);
844 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
845 cp++;
847 while (isspace(*cp) && (cp < maxcp))
848 cp++;
850 if (cp >= maxcp)
851 goto defaults;
853 hp = precsize_aton(&cp);
855 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
856 cp++;
858 while (isspace(*cp) && (cp < maxcp))
859 cp++;
861 if (cp >= maxcp)
862 goto defaults;
864 vp = precsize_aton(&cp);
866 defaults:
868 bcp = binary;
869 *bcp++ = (u_int8_t) 0; /* version byte */
870 *bcp++ = siz;
871 *bcp++ = hp;
872 *bcp++ = vp;
873 PUTLONG(latit,bcp);
874 PUTLONG(longit,bcp);
875 PUTLONG(alt,bcp);
877 return (16); /* size of RR in octets */
880 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
881 const char *
882 loc_ntoa(binary, ascii)
883 const u_char *binary;
884 char *ascii;
886 static char *error = "?";
887 static char tmpbuf[sizeof
888 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
889 const u_char *cp = binary;
891 int latdeg, latmin, latsec, latsecfrac;
892 int longdeg, longmin, longsec, longsecfrac;
893 char northsouth, eastwest;
894 int altmeters, altfrac, altsign;
896 const u_int32_t referencealt = 100000 * 100;
898 int32_t latval, longval, altval;
899 u_int32_t templ;
900 u_int8_t sizeval, hpval, vpval, versionval;
902 char *sizestr, *hpstr, *vpstr;
904 versionval = *cp++;
906 if (ascii == NULL)
907 ascii = tmpbuf;
909 if (versionval) {
910 (void) sprintf(ascii, "; error: unknown LOC RR version");
911 return (ascii);
914 sizeval = *cp++;
916 hpval = *cp++;
917 vpval = *cp++;
919 GETLONG(templ, cp);
920 latval = (templ - ((unsigned)1<<31));
922 GETLONG(templ, cp);
923 longval = (templ - ((unsigned)1<<31));
925 GETLONG(templ, cp);
926 if (templ < referencealt) { /* below WGS 84 spheroid */
927 altval = referencealt - templ;
928 altsign = -1;
929 } else {
930 altval = templ - referencealt;
931 altsign = 1;
934 if (latval < 0) {
935 northsouth = 'S';
936 latval = -latval;
937 } else
938 northsouth = 'N';
940 latsecfrac = latval % 1000;
941 latval = latval / 1000;
942 latsec = latval % 60;
943 latval = latval / 60;
944 latmin = latval % 60;
945 latval = latval / 60;
946 latdeg = latval;
948 if (longval < 0) {
949 eastwest = 'W';
950 longval = -longval;
951 } else
952 eastwest = 'E';
954 longsecfrac = longval % 1000;
955 longval = longval / 1000;
956 longsec = longval % 60;
957 longval = longval / 60;
958 longmin = longval % 60;
959 longval = longval / 60;
960 longdeg = longval;
962 altfrac = altval % 100;
963 altmeters = (altval / 100) * altsign;
965 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
966 sizestr = error;
967 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
968 hpstr = error;
969 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
970 vpstr = error;
972 sprintf(ascii,
973 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
974 latdeg, latmin, latsec, latsecfrac, northsouth,
975 longdeg, longmin, longsec, longsecfrac, eastwest,
976 altmeters, altfrac, sizestr, hpstr, vpstr);
978 if (sizestr != error)
979 free(sizestr);
980 if (hpstr != error)
981 free(hpstr);
982 if (vpstr != error)
983 free(vpstr);
985 return (ascii);
989 /* Return the number of DNS hierarchy levels in the name. */
991 dn_count_labels(const char *name) {
992 int i, len, count;
994 len = strlen(name);
995 for (i = 0, count = 0; i < len; i++) {
996 /* XXX need to check for \. or use named's nlabels(). */
997 if (name[i] == '.')
998 count++;
1001 /* don't count initial wildcard */
1002 if (name[0] == '*')
1003 if (count)
1004 count--;
1006 /* don't count the null label for root. */
1007 /* if terminating '.' not found, must adjust */
1008 /* count to include last label */
1009 if (len > 0 && name[len-1] != '.')
1010 count++;
1011 return (count);
1016 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1017 * SIG records are required to be printed like this, by the Secure DNS RFC.
1019 char *
1020 p_secstodate (u_long secs) {
1021 /* XXX nonreentrant */
1022 static char output[15]; /* YYYYMMDDHHMMSS and null */
1023 time_t clock = secs;
1024 struct tm *time;
1026 #ifdef HAVE_TIME_R
1027 struct tm timebuf;
1029 time = gmtime_r(&clock, &timebuf);
1030 #else
1031 time = gmtime(&clock);
1032 #endif
1033 time->tm_year += 1900;
1034 time->tm_mon += 1;
1035 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1036 time->tm_year, time->tm_mon, time->tm_mday,
1037 time->tm_hour, time->tm_min, time->tm_sec);
1038 return (output);