Update.
[glibc.git] / resolv / res_debug.c
blob2df1c5bbdcdc4a60bf4178a77bb22b582b6578ff
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[] = "$Id$";
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 char buf[2048]; /* XXX need to malloc */
145 ns_opcode opcode;
146 ns_rr rr;
149 * Print answer records.
151 sflag = (statp->pfcode & pflag);
152 if (statp->pfcode && !sflag)
153 return;
155 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
156 rrnum = 0;
157 for (;;) {
158 if (ns_parserr(handle, section, rrnum, &rr)) {
159 if (errno != ENODEV)
160 fprintf(file, ";; ns_parserr: %s\n",
161 strerror(errno));
162 else if (rrnum > 0 && sflag != 0 &&
163 (statp->pfcode & RES_PRF_HEAD1))
164 putc('\n', file);
165 return;
167 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
168 fprintf(file, ";; %s SECTION:\n",
169 p_section(section, opcode));
170 if (section == ns_s_qd)
171 fprintf(file, ";;\t%s, type = %s, class = %s\n",
172 ns_rr_name(rr),
173 p_type(ns_rr_type(rr)),
174 p_class(ns_rr_class(rr)));
175 else {
176 n = ns_sprintrr(handle, &rr, NULL, NULL,
177 buf, sizeof buf);
178 if (n < 0) {
179 fprintf(file, ";; ns_sprintrr: %s\n",
180 strerror(errno));
181 return;
183 fputs(buf, file);
184 fputc('\n', file);
186 rrnum++;
191 * Print the contents of a query.
192 * This is intended to be primarily a debugging routine.
194 void
195 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
196 ns_msg handle;
197 int qdcount, ancount, nscount, arcount;
198 u_int opcode, rcode, id;
200 if (ns_initparse(msg, len, &handle) < 0) {
201 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
202 return;
204 opcode = ns_msg_getflag(handle, ns_f_opcode);
205 rcode = ns_msg_getflag(handle, ns_f_rcode);
206 id = ns_msg_id(handle);
207 qdcount = ns_msg_count(handle, ns_s_qd);
208 ancount = ns_msg_count(handle, ns_s_an);
209 nscount = ns_msg_count(handle, ns_s_ns);
210 arcount = ns_msg_count(handle, ns_s_ar);
213 * Print header fields.
215 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
216 fprintf(file,
217 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
218 _res_opcodes[opcode], p_rcode(rcode), id);
219 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
220 putc(';', file);
221 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
222 fprintf(file, "; flags:");
223 if (ns_msg_getflag(handle, ns_f_qr))
224 fprintf(file, " qr");
225 if (ns_msg_getflag(handle, ns_f_aa))
226 fprintf(file, " aa");
227 if (ns_msg_getflag(handle, ns_f_tc))
228 fprintf(file, " tc");
229 if (ns_msg_getflag(handle, ns_f_rd))
230 fprintf(file, " rd");
231 if (ns_msg_getflag(handle, ns_f_ra))
232 fprintf(file, " ra");
233 if (ns_msg_getflag(handle, ns_f_z))
234 fprintf(file, " ??");
235 if (ns_msg_getflag(handle, ns_f_ad))
236 fprintf(file, " ad");
237 if (ns_msg_getflag(handle, ns_f_cd))
238 fprintf(file, " cd");
240 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
241 fprintf(file, "; %s: %d",
242 p_section(ns_s_qd, opcode), qdcount);
243 fprintf(file, ", %s: %d",
244 p_section(ns_s_an, opcode), ancount);
245 fprintf(file, ", %s: %d",
246 p_section(ns_s_ns, opcode), nscount);
247 fprintf(file, ", %s: %d",
248 p_section(ns_s_ar, opcode), arcount);
250 if ((!statp->pfcode) || (statp->pfcode &
251 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
252 putc('\n',file);
255 * Print the various sections.
257 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
258 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
259 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
260 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
261 if (qdcount == 0 && ancount == 0 &&
262 nscount == 0 && arcount == 0)
263 putc('\n', file);
266 const u_char *
267 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
268 char name[MAXDNAME];
269 int n;
271 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
272 return (NULL);
273 if (name[0] == '\0')
274 putc('.', file);
275 else
276 fputs(name, file);
277 return (cp + n);
280 const u_char *
281 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
282 return (p_cdnname(cp, msg, PACKETSZ, file));
285 /* Return a fully-qualified domain name from a compressed name (with
286 length supplied). */
288 const u_char *
289 p_fqnname(cp, msg, msglen, name, namelen)
290 const u_char *cp, *msg;
291 int msglen;
292 char *name;
293 int namelen;
295 int n, newlen;
297 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
298 return (NULL);
299 newlen = strlen(name);
300 if (newlen == 0 || name[newlen - 1] != '.') {
301 if (newlen + 1 >= namelen) /* Lack space for final dot */
302 return (NULL);
303 else
304 strcpy(name + newlen, ".");
306 return (cp + n);
309 /* XXX: the rest of these functions need to become length-limited, too. */
311 const u_char *
312 p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
313 char name[MAXDNAME];
314 const u_char *n;
316 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
317 if (n == NULL)
318 return (NULL);
319 fputs(name, file);
320 return (n);
324 * Names of RR classes and qclasses. Classes and qclasses are the same, except
325 * that C_ANY is a qclass but not a class. (You can ask for records of class
326 * C_ANY, but you can't have any records of that class in the database.)
328 const struct res_sym __p_class_syms[] = {
329 {C_IN, "IN"},
330 {C_CHAOS, "CHAOS"},
331 {C_HS, "HS"},
332 {C_HS, "HESIOD"},
333 {C_ANY, "ANY"},
334 {C_NONE, "NONE"},
335 {C_IN, (char *)0}
339 * Names of message sections.
341 const struct res_sym __p_default_section_syms[] = {
342 {ns_s_qd, "QUERY"},
343 {ns_s_an, "ANSWER"},
344 {ns_s_ns, "AUTHORITY"},
345 {ns_s_ar, "ADDITIONAL"},
346 {0, (char *)0}
349 const struct res_sym __p_update_section_syms[] = {
350 {S_ZONE, "ZONE"},
351 {S_PREREQ, "PREREQUISITE"},
352 {S_UPDATE, "UPDATE"},
353 {S_ADDT, "ADDITIONAL"},
354 {0, (char *)0}
357 const struct res_sym __p_key_syms[] = {
358 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
359 {NS_ALG_DH, "DH", "Diffie Hellman"},
360 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
361 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
362 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
363 {0, NULL, NULL}
366 const struct res_sym __p_cert_syms[] = {
367 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
368 {cert_t_spki, "SPKI", "SPKI certificate"},
369 {cert_t_pgp, "PGP", "PGP certificate"},
370 {cert_t_url, "URL", "URL Private"},
371 {cert_t_oid, "OID", "OID Private"},
372 {0, NULL, NULL}
376 * Names of RR types and qtypes. Types and qtypes are the same, except
377 * that T_ANY is a qtype but not a type. (You can ask for records of type
378 * T_ANY, but you can't have any records of that type in the database.)
380 const struct res_sym __p_type_syms[] = {
381 {ns_t_a, "A", "address"},
382 {ns_t_ns, "NS", "name server"},
383 {ns_t_md, "MD", "mail destination (deprecated)"},
384 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
385 {ns_t_cname, "CNAME", "canonical name"},
386 {ns_t_soa, "SOA", "start of authority"},
387 {ns_t_mb, "MB", "mailbox"},
388 {ns_t_mg, "MG", "mail group member"},
389 {ns_t_mr, "MR", "mail rename"},
390 {ns_t_null, "NULL", "null"},
391 {ns_t_wks, "WKS", "well-known service (deprecated)"},
392 {ns_t_ptr, "PTR", "domain name pointer"},
393 {ns_t_hinfo, "HINFO", "host information"},
394 {ns_t_minfo, "MINFO", "mailbox information"},
395 {ns_t_mx, "MX", "mail exchanger"},
396 {ns_t_txt, "TXT", "text"},
397 {ns_t_rp, "RP", "responsible person"},
398 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
399 {ns_t_x25, "X25", "X25 address"},
400 {ns_t_isdn, "ISDN", "ISDN address"},
401 {ns_t_rt, "RT", "router"},
402 {ns_t_nsap, "NSAP", "nsap address"},
403 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
404 {ns_t_sig, "SIG", "signature"},
405 {ns_t_key, "KEY", "key"},
406 {ns_t_px, "PX", "mapping information"},
407 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
408 {ns_t_aaaa, "AAAA", "IPv6 address"},
409 {ns_t_loc, "LOC", "location"},
410 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
411 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
412 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
413 {ns_t_srv, "SRV", "server selection"},
414 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
415 {ns_t_tsig, "TSIG", "transaction signature"},
416 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
417 {ns_t_axfr, "AXFR", "zone transfer"},
418 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
419 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
420 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
421 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
422 {ns_t_kx, "KX", "Key Exchange"},
423 {ns_t_cert, "CERT", "Certificate"},
424 {ns_t_any, "ANY", "\"any\""},
425 {0, NULL, NULL}
429 * Names of DNS rcodes.
431 const struct res_sym __p_rcode_syms[] = {
432 {ns_r_noerror, "NOERROR", "no error"},
433 {ns_r_formerr, "FORMERR", "format error"},
434 {ns_r_servfail, "SERVFAIL", "server failed"},
435 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
436 {ns_r_notimpl, "NOTIMP", "not implemented"},
437 {ns_r_refused, "REFUSED", "refused"},
438 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
439 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
440 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
441 {ns_r_notauth, "NOTAUTH", "not authoritative"},
442 {ns_r_notzone, "NOTZONE", "Not in zone"},
443 {ns_r_max, "", ""},
444 {ns_r_badsig, "BADSIG", "bad signature"},
445 {ns_r_badkey, "BADKEY", "bad key"},
446 {ns_r_badtime, "BADTIME", "bad time"},
447 {0, NULL, NULL}
451 sym_ston(const struct res_sym *syms, const char *name, int *success) {
452 for ((void)NULL; syms->name != 0; syms++) {
453 if (strcasecmp (name, syms->name) == 0) {
454 if (success)
455 *success = 1;
456 return (syms->number);
459 if (success)
460 *success = 0;
461 return (syms->number); /* The default value. */
464 const char *
465 sym_ntos(const struct res_sym *syms, int number, int *success) {
466 static char unname[20];
468 #ifdef _LIBC
469 /* Changed to prevent warning. --drepper@gnu */
470 for (; syms->name != 0; syms++) {
471 #else
472 for ((void)NULL; syms->name != 0; syms++) {
473 #endif
474 if (number == syms->number) {
475 if (success)
476 *success = 1;
477 return (syms->name);
481 sprintf(unname, "%d", number); /* XXX nonreentrant */
482 if (success)
483 *success = 0;
484 return (unname);
487 const char *
488 sym_ntop(const struct res_sym *syms, int number, int *success) {
489 static char unname[20];
491 #ifdef _LIBC
492 /* Changed to prevent warning. --drepper@gnu */
493 for (; syms->name != 0; syms++) {
494 #else
495 for ((void)NULL; syms->name != 0; syms++) {
496 #endif
497 if (number == syms->number) {
498 if (success)
499 *success = 1;
500 return (syms->humanname);
503 sprintf(unname, "%d", number); /* XXX nonreentrant */
504 if (success)
505 *success = 0;
506 return (unname);
510 * Return a string for the type.
512 const char *
513 p_type(int type) {
514 return (sym_ntos(__p_type_syms, type, (int *)0));
518 * Return a string for the type.
520 const char *
521 p_section(int section, int opcode) {
522 const struct res_sym *symbols;
524 switch (opcode) {
525 case ns_o_update:
526 symbols = __p_update_section_syms;
527 break;
528 default:
529 symbols = __p_default_section_syms;
530 break;
532 return (sym_ntos(symbols, section, (int *)0));
536 * Return a mnemonic for class.
538 const char *
539 p_class(int class) {
540 return (sym_ntos(__p_class_syms, class, (int *)0));
544 * Return a mnemonic for an option
546 const char *
547 p_option(u_long option) {
548 static char nbuf[40];
550 switch (option) {
551 case RES_INIT: return "init";
552 case RES_DEBUG: return "debug";
553 case RES_AAONLY: return "aaonly(unimpl)";
554 case RES_USEVC: return "usevc";
555 case RES_PRIMARY: return "primry(unimpl)";
556 case RES_IGNTC: return "igntc";
557 case RES_RECURSE: return "recurs";
558 case RES_DEFNAMES: return "defnam";
559 case RES_STAYOPEN: return "styopn";
560 case RES_DNSRCH: return "dnsrch";
561 case RES_INSECURE1: return "insecure1";
562 case RES_INSECURE2: return "insecure2";
563 /* XXX nonreentrant */
564 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
565 return (nbuf);
570 * Return a mnemonic for a time to live.
572 const char *
573 p_time(u_int32_t value) {
574 static char nbuf[40]; /* XXX nonreentrant */
576 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
577 sprintf(nbuf, "%u", value);
578 return (nbuf);
582 * Return a string for the rcode.
584 const char *
585 p_rcode(int rcode) {
586 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
590 * routines to convert between on-the-wire RR format and zone file format.
591 * Does not contain conversion to/from decimal degrees; divide or multiply
592 * by 60*60*1000 for that.
595 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
596 1000000,10000000,100000000,1000000000};
598 /* takes an XeY precision/size value, returns a string representation. */
599 static const char *
600 precsize_ntoa(prec)
601 u_int8_t prec;
603 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
604 unsigned long val;
605 int mantissa, exponent;
607 mantissa = (int)((prec >> 4) & 0x0f) % 10;
608 exponent = (int)((prec >> 0) & 0x0f) % 10;
610 val = mantissa * poweroften[exponent];
612 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
613 return (retbuf);
616 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
617 static u_int8_t
618 precsize_aton(strptr)
619 char **strptr;
621 unsigned int mval = 0, cmval = 0;
622 u_int8_t retval = 0;
623 char *cp;
624 int exponent;
625 int mantissa;
627 cp = *strptr;
629 while (isdigit(*cp))
630 mval = mval * 10 + (*cp++ - '0');
632 if (*cp == '.') { /* centimeters */
633 cp++;
634 if (isdigit(*cp)) {
635 cmval = (*cp++ - '0') * 10;
636 if (isdigit(*cp)) {
637 cmval += (*cp++ - '0');
641 cmval = (mval * 100) + cmval;
643 for (exponent = 0; exponent < 9; exponent++)
644 if (cmval < poweroften[exponent+1])
645 break;
647 mantissa = cmval / poweroften[exponent];
648 if (mantissa > 9)
649 mantissa = 9;
651 retval = (mantissa << 4) | exponent;
653 *strptr = cp;
655 return (retval);
658 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
659 static u_int32_t
660 latlon2ul(latlonstrptr,which)
661 char **latlonstrptr;
662 int *which;
664 char *cp;
665 u_int32_t retval;
666 int deg = 0, min = 0, secs = 0, secsfrac = 0;
668 cp = *latlonstrptr;
670 while (isdigit(*cp))
671 deg = deg * 10 + (*cp++ - '0');
673 while (isspace(*cp))
674 cp++;
676 if (!(isdigit(*cp)))
677 goto fndhemi;
679 while (isdigit(*cp))
680 min = min * 10 + (*cp++ - '0');
682 while (isspace(*cp))
683 cp++;
685 if (!(isdigit(*cp)))
686 goto fndhemi;
688 while (isdigit(*cp))
689 secs = secs * 10 + (*cp++ - '0');
691 if (*cp == '.') { /* decimal seconds */
692 cp++;
693 if (isdigit(*cp)) {
694 secsfrac = (*cp++ - '0') * 100;
695 if (isdigit(*cp)) {
696 secsfrac += (*cp++ - '0') * 10;
697 if (isdigit(*cp)) {
698 secsfrac += (*cp++ - '0');
704 while (!isspace(*cp)) /* if any trailing garbage */
705 cp++;
707 while (isspace(*cp))
708 cp++;
710 fndhemi:
711 switch (*cp) {
712 case 'N': case 'n':
713 case 'E': case 'e':
714 retval = ((unsigned)1<<31)
715 + (((((deg * 60) + min) * 60) + secs) * 1000)
716 + secsfrac;
717 break;
718 case 'S': case 's':
719 case 'W': case 'w':
720 retval = ((unsigned)1<<31)
721 - (((((deg * 60) + min) * 60) + secs) * 1000)
722 - secsfrac;
723 break;
724 default:
725 retval = 0; /* invalid value -- indicates error */
726 break;
729 switch (*cp) {
730 case 'N': case 'n':
731 case 'S': case 's':
732 *which = 1; /* latitude */
733 break;
734 case 'E': case 'e':
735 case 'W': case 'w':
736 *which = 2; /* longitude */
737 break;
738 default:
739 *which = 0; /* error */
740 break;
743 cp++; /* skip the hemisphere */
745 while (!isspace(*cp)) /* if any trailing garbage */
746 cp++;
748 while (isspace(*cp)) /* move to next field */
749 cp++;
751 *latlonstrptr = cp;
753 return (retval);
756 /* converts a zone file representation in a string to an RDATA on-the-wire
757 * representation. */
759 loc_aton(ascii, binary)
760 const char *ascii;
761 u_char *binary;
763 const char *cp, *maxcp;
764 u_char *bcp;
766 u_int32_t latit = 0, longit = 0, alt = 0;
767 u_int32_t lltemp1 = 0, lltemp2 = 0;
768 int altmeters = 0, altfrac = 0, altsign = 1;
769 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
770 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
771 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
772 int which1 = 0, which2 = 0;
774 cp = ascii;
775 maxcp = cp + strlen(ascii);
777 lltemp1 = latlon2ul(&cp, &which1);
779 lltemp2 = latlon2ul(&cp, &which2);
781 switch (which1 + which2) {
782 case 3: /* 1 + 2, the only valid combination */
783 if ((which1 == 1) && (which2 == 2)) { /* normal case */
784 latit = lltemp1;
785 longit = lltemp2;
786 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
787 longit = lltemp1;
788 latit = lltemp2;
789 } else { /* some kind of brokenness */
790 return (0);
792 break;
793 default: /* we didn't get one of each */
794 return (0);
797 /* altitude */
798 if (*cp == '-') {
799 altsign = -1;
800 cp++;
803 if (*cp == '+')
804 cp++;
806 while (isdigit(*cp))
807 altmeters = altmeters * 10 + (*cp++ - '0');
809 if (*cp == '.') { /* decimal meters */
810 cp++;
811 if (isdigit(*cp)) {
812 altfrac = (*cp++ - '0') * 10;
813 if (isdigit(*cp)) {
814 altfrac += (*cp++ - '0');
819 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
821 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
822 cp++;
824 while (isspace(*cp) && (cp < maxcp))
825 cp++;
827 if (cp >= maxcp)
828 goto defaults;
830 siz = precsize_aton(&cp);
832 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
833 cp++;
835 while (isspace(*cp) && (cp < maxcp))
836 cp++;
838 if (cp >= maxcp)
839 goto defaults;
841 hp = precsize_aton(&cp);
843 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
844 cp++;
846 while (isspace(*cp) && (cp < maxcp))
847 cp++;
849 if (cp >= maxcp)
850 goto defaults;
852 vp = precsize_aton(&cp);
854 defaults:
856 bcp = binary;
857 *bcp++ = (u_int8_t) 0; /* version byte */
858 *bcp++ = siz;
859 *bcp++ = hp;
860 *bcp++ = vp;
861 PUTLONG(latit,bcp);
862 PUTLONG(longit,bcp);
863 PUTLONG(alt,bcp);
865 return (16); /* size of RR in octets */
868 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
869 const char *
870 loc_ntoa(binary, ascii)
871 const u_char *binary;
872 char *ascii;
874 static char *error = "?";
875 static char tmpbuf[sizeof
876 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
877 const u_char *cp = binary;
879 int latdeg, latmin, latsec, latsecfrac;
880 int longdeg, longmin, longsec, longsecfrac;
881 char northsouth, eastwest;
882 int altmeters, altfrac, altsign;
884 const u_int32_t referencealt = 100000 * 100;
886 int32_t latval, longval, altval;
887 u_int32_t templ;
888 u_int8_t sizeval, hpval, vpval, versionval;
890 char *sizestr, *hpstr, *vpstr;
892 versionval = *cp++;
894 if (ascii == NULL)
895 ascii = tmpbuf;
897 if (versionval) {
898 (void) sprintf(ascii, "; error: unknown LOC RR version");
899 return (ascii);
902 sizeval = *cp++;
904 hpval = *cp++;
905 vpval = *cp++;
907 GETLONG(templ, cp);
908 latval = (templ - ((unsigned)1<<31));
910 GETLONG(templ, cp);
911 longval = (templ - ((unsigned)1<<31));
913 GETLONG(templ, cp);
914 if (templ < referencealt) { /* below WGS 84 spheroid */
915 altval = referencealt - templ;
916 altsign = -1;
917 } else {
918 altval = templ - referencealt;
919 altsign = 1;
922 if (latval < 0) {
923 northsouth = 'S';
924 latval = -latval;
925 } else
926 northsouth = 'N';
928 latsecfrac = latval % 1000;
929 latval = latval / 1000;
930 latsec = latval % 60;
931 latval = latval / 60;
932 latmin = latval % 60;
933 latval = latval / 60;
934 latdeg = latval;
936 if (longval < 0) {
937 eastwest = 'W';
938 longval = -longval;
939 } else
940 eastwest = 'E';
942 longsecfrac = longval % 1000;
943 longval = longval / 1000;
944 longsec = longval % 60;
945 longval = longval / 60;
946 longmin = longval % 60;
947 longval = longval / 60;
948 longdeg = longval;
950 altfrac = altval % 100;
951 altmeters = (altval / 100) * altsign;
953 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
954 sizestr = error;
955 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
956 hpstr = error;
957 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
958 vpstr = error;
960 sprintf(ascii,
961 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
962 latdeg, latmin, latsec, latsecfrac, northsouth,
963 longdeg, longmin, longsec, longsecfrac, eastwest,
964 altmeters, altfrac, sizestr, hpstr, vpstr);
966 if (sizestr != error)
967 free(sizestr);
968 if (hpstr != error)
969 free(hpstr);
970 if (vpstr != error)
971 free(vpstr);
973 return (ascii);
977 /* Return the number of DNS hierarchy levels in the name. */
979 dn_count_labels(const char *name) {
980 int i, len, count;
982 len = strlen(name);
983 for (i = 0, count = 0; i < len; i++) {
984 /* XXX need to check for \. or use named's nlabels(). */
985 if (name[i] == '.')
986 count++;
989 /* don't count initial wildcard */
990 if (name[0] == '*')
991 if (count)
992 count--;
994 /* don't count the null label for root. */
995 /* if terminating '.' not found, must adjust */
996 /* count to include last label */
997 if (len > 0 && name[len-1] != '.')
998 count++;
999 return (count);
1004 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1005 * SIG records are required to be printed like this, by the Secure DNS RFC.
1007 char *
1008 p_secstodate (u_long secs) {
1009 /* XXX nonreentrant */
1010 static char output[15]; /* YYYYMMDDHHMMSS and null */
1011 time_t clock = secs;
1012 struct tm *time;
1014 #ifdef HAVE_TIME_R
1015 struct time timebuf;
1017 time = gmtime_r(&clock, &timebuf);
1018 #else
1019 time = gmtime(&clock);
1020 #endif
1021 time->tm_year += 1900;
1022 time->tm_mon += 1;
1023 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1024 time->tm_year, time->tm_mon, time->tm_mday,
1025 time->tm_hour, time->tm_min, time->tm_sec);
1026 return (output);