* sysdeps/powerpc/powerpc32/power5/fpu/s_isnan.S: New file.
[glibc.git] / resolv / res_debug.c
blobcd69d4f4d448b72e76a84485d8c495970faafb28
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_sectioncodes[] attribute_hidden;
124 * Print the current options.
126 void
127 fp_resstat(const res_state statp, FILE *file) {
128 u_long mask;
130 fprintf(file, ";; res options:");
131 for (mask = 1; mask != 0; mask <<= 1)
132 if (statp->options & mask)
133 fprintf(file, " %s", p_option(mask));
134 putc('\n', file);
137 static void
138 do_section(const res_state statp,
139 ns_msg *handle, ns_sect section,
140 int pflag, FILE *file)
142 int n, sflag, rrnum;
143 static int buflen = 2048;
144 char *buf;
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 buf = malloc(buflen);
156 if (buf == NULL) {
157 fprintf(file, ";; memory allocation failure\n");
158 return;
161 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
162 rrnum = 0;
163 for (;;) {
164 if (ns_parserr(handle, section, rrnum, &rr)) {
165 if (errno != ENODEV)
166 fprintf(file, ";; ns_parserr: %s\n",
167 strerror(errno));
168 else if (rrnum > 0 && sflag != 0 &&
169 (statp->pfcode & RES_PRF_HEAD1))
170 putc('\n', file);
171 goto cleanup;
173 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
174 fprintf(file, ";; %s SECTION:\n",
175 p_section(section, opcode));
176 if (section == ns_s_qd)
177 fprintf(file, ";;\t%s, type = %s, class = %s\n",
178 ns_rr_name(rr),
179 p_type(ns_rr_type(rr)),
180 p_class(ns_rr_class(rr)));
181 else {
182 n = ns_sprintrr(handle, &rr, NULL, NULL,
183 buf, buflen);
184 if (n < 0) {
185 if (errno == ENOSPC) {
186 free(buf);
187 buf = NULL;
188 if (buflen < 131072)
189 buf = malloc(buflen += 1024);
190 if (buf == NULL) {
191 fprintf(file,
192 ";; memory allocation failure\n");
193 return;
195 continue;
197 fprintf(file, ";; ns_sprintrr: %s\n",
198 strerror(errno));
199 goto cleanup;
201 fputs(buf, file);
202 fputc('\n', file);
204 rrnum++;
206 cleanup:
207 free(buf);
211 * Print the contents of a query.
212 * This is intended to be primarily a debugging routine.
214 void
215 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
216 ns_msg handle;
217 int qdcount, ancount, nscount, arcount;
218 u_int opcode, rcode, id;
220 if (ns_initparse(msg, len, &handle) < 0) {
221 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
222 return;
224 opcode = ns_msg_getflag(handle, ns_f_opcode);
225 rcode = ns_msg_getflag(handle, ns_f_rcode);
226 id = ns_msg_id(handle);
227 qdcount = ns_msg_count(handle, ns_s_qd);
228 ancount = ns_msg_count(handle, ns_s_an);
229 nscount = ns_msg_count(handle, ns_s_ns);
230 arcount = ns_msg_count(handle, ns_s_ar);
233 * Print header fields.
235 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
236 fprintf(file,
237 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
238 _res_opcodes[opcode], p_rcode(rcode), id);
239 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
240 putc(';', file);
241 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
242 fprintf(file, "; flags:");
243 if (ns_msg_getflag(handle, ns_f_qr))
244 fprintf(file, " qr");
245 if (ns_msg_getflag(handle, ns_f_aa))
246 fprintf(file, " aa");
247 if (ns_msg_getflag(handle, ns_f_tc))
248 fprintf(file, " tc");
249 if (ns_msg_getflag(handle, ns_f_rd))
250 fprintf(file, " rd");
251 if (ns_msg_getflag(handle, ns_f_ra))
252 fprintf(file, " ra");
253 if (ns_msg_getflag(handle, ns_f_z))
254 fprintf(file, " ??");
255 if (ns_msg_getflag(handle, ns_f_ad))
256 fprintf(file, " ad");
257 if (ns_msg_getflag(handle, ns_f_cd))
258 fprintf(file, " cd");
260 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
261 fprintf(file, "; %s: %d",
262 p_section(ns_s_qd, opcode), qdcount);
263 fprintf(file, ", %s: %d",
264 p_section(ns_s_an, opcode), ancount);
265 fprintf(file, ", %s: %d",
266 p_section(ns_s_ns, opcode), nscount);
267 fprintf(file, ", %s: %d",
268 p_section(ns_s_ar, opcode), arcount);
270 if ((!statp->pfcode) || (statp->pfcode &
271 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
272 putc('\n',file);
275 * Print the various sections.
277 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
278 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
279 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
280 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
281 if (qdcount == 0 && ancount == 0 &&
282 nscount == 0 && arcount == 0)
283 putc('\n', file);
286 const u_char *
287 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
288 char name[MAXDNAME];
289 int n;
291 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
292 return (NULL);
293 if (name[0] == '\0')
294 putc('.', file);
295 else
296 fputs(name, file);
297 return (cp + n);
299 libresolv_hidden_def (p_cdnname)
301 const u_char *
302 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
303 return (p_cdnname(cp, msg, PACKETSZ, file));
306 /* Return a fully-qualified domain name from a compressed name (with
307 length supplied). */
309 const u_char *
310 p_fqnname(cp, msg, msglen, name, namelen)
311 const u_char *cp, *msg;
312 int msglen;
313 char *name;
314 int namelen;
316 int n, newlen;
318 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
319 return (NULL);
320 newlen = strlen(name);
321 if (newlen == 0 || name[newlen - 1] != '.') {
322 if (newlen + 1 >= namelen) /* Lack space for final dot */
323 return (NULL);
324 else
325 strcpy(name + newlen, ".");
327 return (cp + n);
329 libresolv_hidden_def (p_fqnname)
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 extern const struct res_sym __p_class_syms[];
351 libresolv_hidden_proto (__p_class_syms)
352 const struct res_sym __p_class_syms[] = {
353 {C_IN, "IN"},
354 {C_CHAOS, "CHAOS"},
355 {C_HS, "HS"},
356 {C_HS, "HESIOD"},
357 {C_ANY, "ANY"},
358 {C_NONE, "NONE"},
359 {C_IN, (char *)0}
361 libresolv_hidden_data_def (__p_class_syms)
364 * Names of message sections.
366 const struct res_sym __p_default_section_syms[] attribute_hidden = {
367 {ns_s_qd, "QUERY"},
368 {ns_s_an, "ANSWER"},
369 {ns_s_ns, "AUTHORITY"},
370 {ns_s_ar, "ADDITIONAL"},
371 {0, (char *)0}
374 const struct res_sym __p_update_section_syms[] attribute_hidden = {
375 {S_ZONE, "ZONE"},
376 {S_PREREQ, "PREREQUISITE"},
377 {S_UPDATE, "UPDATE"},
378 {S_ADDT, "ADDITIONAL"},
379 {0, (char *)0}
382 const struct res_sym __p_key_syms[] attribute_hidden = {
383 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
384 {NS_ALG_DH, "DH", "Diffie Hellman"},
385 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
386 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
387 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
388 {0, NULL, NULL}
391 const struct res_sym __p_cert_syms[] attribute_hidden = {
392 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
393 {cert_t_spki, "SPKI", "SPKI certificate"},
394 {cert_t_pgp, "PGP", "PGP certificate"},
395 {cert_t_url, "URL", "URL Private"},
396 {cert_t_oid, "OID", "OID Private"},
397 {0, NULL, NULL}
401 * Names of RR types and qtypes. Types and qtypes are the same, except
402 * that T_ANY is a qtype but not a type. (You can ask for records of type
403 * T_ANY, but you can't have any records of that type in the database.)
405 extern const struct res_sym __p_type_syms[];
406 libresolv_hidden_proto (__p_type_syms)
407 const struct res_sym __p_type_syms[] = {
408 {ns_t_a, "A", "address"},
409 {ns_t_ns, "NS", "name server"},
410 {ns_t_md, "MD", "mail destination (deprecated)"},
411 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
412 {ns_t_cname, "CNAME", "canonical name"},
413 {ns_t_soa, "SOA", "start of authority"},
414 {ns_t_mb, "MB", "mailbox"},
415 {ns_t_mg, "MG", "mail group member"},
416 {ns_t_mr, "MR", "mail rename"},
417 {ns_t_null, "NULL", "null"},
418 {ns_t_wks, "WKS", "well-known service (deprecated)"},
419 {ns_t_ptr, "PTR", "domain name pointer"},
420 {ns_t_hinfo, "HINFO", "host information"},
421 {ns_t_minfo, "MINFO", "mailbox information"},
422 {ns_t_mx, "MX", "mail exchanger"},
423 {ns_t_txt, "TXT", "text"},
424 {ns_t_rp, "RP", "responsible person"},
425 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
426 {ns_t_x25, "X25", "X25 address"},
427 {ns_t_isdn, "ISDN", "ISDN address"},
428 {ns_t_rt, "RT", "router"},
429 {ns_t_nsap, "NSAP", "nsap address"},
430 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
431 {ns_t_sig, "SIG", "signature"},
432 {ns_t_key, "KEY", "key"},
433 {ns_t_px, "PX", "mapping information"},
434 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
435 {ns_t_aaaa, "AAAA", "IPv6 address"},
436 {ns_t_loc, "LOC", "location"},
437 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
438 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
439 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
440 {ns_t_srv, "SRV", "server selection"},
441 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
442 {ns_t_tsig, "TSIG", "transaction signature"},
443 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
444 {ns_t_axfr, "AXFR", "zone transfer"},
445 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
446 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
447 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
448 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
449 {ns_t_kx, "KX", "Key Exchange"},
450 {ns_t_cert, "CERT", "Certificate"},
451 {ns_t_any, "ANY", "\"any\""},
452 {0, NULL, NULL}
454 libresolv_hidden_data_def (__p_type_syms)
457 * Names of DNS rcodes.
459 const struct res_sym __p_rcode_syms[] attribute_hidden = {
460 {ns_r_noerror, "NOERROR", "no error"},
461 {ns_r_formerr, "FORMERR", "format error"},
462 {ns_r_servfail, "SERVFAIL", "server failed"},
463 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
464 {ns_r_notimpl, "NOTIMP", "not implemented"},
465 {ns_r_refused, "REFUSED", "refused"},
466 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
467 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
468 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
469 {ns_r_notauth, "NOTAUTH", "not authoritative"},
470 {ns_r_notzone, "NOTZONE", "Not in zone"},
471 {ns_r_max, "", ""},
472 {ns_r_badsig, "BADSIG", "bad signature"},
473 {ns_r_badkey, "BADKEY", "bad key"},
474 {ns_r_badtime, "BADTIME", "bad time"},
475 {0, NULL, NULL}
479 sym_ston(const struct res_sym *syms, const char *name, int *success) {
480 for ((void)NULL; syms->name != 0; syms++) {
481 if (strcasecmp (name, syms->name) == 0) {
482 if (success)
483 *success = 1;
484 return (syms->number);
487 if (success)
488 *success = 0;
489 return (syms->number); /* The default value. */
492 const char *
493 sym_ntos(const struct res_sym *syms, int number, int *success) {
494 static char unname[20];
496 for ((void)NULL; syms->name != 0; syms++) {
497 if (number == syms->number) {
498 if (success)
499 *success = 1;
500 return (syms->name);
504 sprintf(unname, "%d", number); /* XXX nonreentrant */
505 if (success)
506 *success = 0;
507 return (unname);
509 libresolv_hidden_def (sym_ntos)
511 const char *
512 sym_ntop(const struct res_sym *syms, int number, int *success) {
513 static char unname[20];
515 for ((void)NULL; syms->name != 0; syms++) {
516 if (number == syms->number) {
517 if (success)
518 *success = 1;
519 return (syms->humanname);
522 sprintf(unname, "%d", number); /* XXX nonreentrant */
523 if (success)
524 *success = 0;
525 return (unname);
529 * Return a string for the type.
531 const char *
532 p_type(int type) {
533 return (sym_ntos(__p_type_syms, type, (int *)0));
535 libresolv_hidden_def (p_type)
538 * Return a string for the type.
540 const char *
541 p_section(int section, int opcode) {
542 const struct res_sym *symbols;
544 switch (opcode) {
545 case ns_o_update:
546 symbols = __p_update_section_syms;
547 break;
548 default:
549 symbols = __p_default_section_syms;
550 break;
552 return (sym_ntos(symbols, section, (int *)0));
556 * Return a mnemonic for class.
558 const char *
559 p_class(int class) {
560 return (sym_ntos(__p_class_syms, class, (int *)0));
562 libresolv_hidden_def (p_class)
565 * Return a mnemonic for an option
567 const char *
568 p_option(u_long option) {
569 static char nbuf[40];
571 switch (option) {
572 case RES_INIT: return "init";
573 case RES_DEBUG: return "debug";
574 case RES_AAONLY: return "aaonly(unimpl)";
575 case RES_USEVC: return "usevc";
576 case RES_PRIMARY: return "primry(unimpl)";
577 case RES_IGNTC: return "igntc";
578 case RES_RECURSE: return "recurs";
579 case RES_DEFNAMES: return "defnam";
580 case RES_STAYOPEN: return "styopn";
581 case RES_DNSRCH: return "dnsrch";
582 case RES_INSECURE1: return "insecure1";
583 case RES_INSECURE2: return "insecure2";
584 case RES_USE_INET6: return "inet6";
585 case RES_ROTATE: return "rotate";
586 case RES_NOCHECKNAME: return "no-check-names";
587 case RES_USEBSTRING: return "ip6-bytstring";
588 /* XXX nonreentrant */
589 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
590 return (nbuf);
593 libresolv_hidden_def (p_option)
596 * Return a mnemonic for a time to live.
598 const char *
599 p_time(u_int32_t value) {
600 static char nbuf[40]; /* XXX nonreentrant */
602 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
603 sprintf(nbuf, "%u", value);
604 return (nbuf);
608 * Return a string for the rcode.
610 const char *
611 p_rcode(int rcode) {
612 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
614 libresolv_hidden_def (p_rcode)
617 * routines to convert between on-the-wire RR format and zone file format.
618 * Does not contain conversion to/from decimal degrees; divide or multiply
619 * by 60*60*1000 for that.
622 static const unsigned int poweroften[10]=
623 { 1, 10, 100, 1000, 10000, 100000,
624 1000000,10000000,100000000,1000000000};
626 /* takes an XeY precision/size value, returns a string representation. */
627 static const char *
628 precsize_ntoa (u_int8_t prec)
630 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
631 unsigned long val;
632 int mantissa, exponent;
634 mantissa = (int)((prec >> 4) & 0x0f) % 10;
635 exponent = (int)((prec >> 0) & 0x0f) % 10;
637 val = mantissa * poweroften[exponent];
639 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
640 return (retbuf);
643 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
644 static u_int8_t
645 precsize_aton (const char **strptr)
647 unsigned int mval = 0, cmval = 0;
648 u_int8_t retval = 0;
649 const char *cp;
650 int exponent;
651 int mantissa;
653 cp = *strptr;
655 while (isdigit(*cp))
656 mval = mval * 10 + (*cp++ - '0');
658 if (*cp == '.') { /* centimeters */
659 cp++;
660 if (isdigit(*cp)) {
661 cmval = (*cp++ - '0') * 10;
662 if (isdigit(*cp)) {
663 cmval += (*cp++ - '0');
667 cmval = (mval * 100) + cmval;
669 for (exponent = 0; exponent < 9; exponent++)
670 if (cmval < poweroften[exponent+1])
671 break;
673 mantissa = cmval / poweroften[exponent];
674 if (mantissa > 9)
675 mantissa = 9;
677 retval = (mantissa << 4) | exponent;
679 *strptr = cp;
681 return (retval);
684 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
685 static u_int32_t
686 latlon2ul (const char **latlonstrptr, int *which)
688 const char *cp;
689 u_int32_t retval;
690 int deg = 0, min = 0, secs = 0, secsfrac = 0;
692 cp = *latlonstrptr;
694 while (isdigit(*cp))
695 deg = deg * 10 + (*cp++ - '0');
697 while (isspace(*cp))
698 cp++;
700 if (!(isdigit(*cp)))
701 goto fndhemi;
703 while (isdigit(*cp))
704 min = min * 10 + (*cp++ - '0');
706 while (isspace(*cp))
707 cp++;
709 if (!(isdigit(*cp)))
710 goto fndhemi;
712 while (isdigit(*cp))
713 secs = secs * 10 + (*cp++ - '0');
715 if (*cp == '.') { /* decimal seconds */
716 cp++;
717 if (isdigit(*cp)) {
718 secsfrac = (*cp++ - '0') * 100;
719 if (isdigit(*cp)) {
720 secsfrac += (*cp++ - '0') * 10;
721 if (isdigit(*cp)) {
722 secsfrac += (*cp++ - '0');
728 while (!isspace(*cp)) /* if any trailing garbage */
729 cp++;
731 while (isspace(*cp))
732 cp++;
734 fndhemi:
735 switch (*cp) {
736 case 'N': case 'n':
737 case 'E': case 'e':
738 retval = ((unsigned)1<<31)
739 + (((((deg * 60) + min) * 60) + secs) * 1000)
740 + secsfrac;
741 break;
742 case 'S': case 's':
743 case 'W': case 'w':
744 retval = ((unsigned)1<<31)
745 - (((((deg * 60) + min) * 60) + secs) * 1000)
746 - secsfrac;
747 break;
748 default:
749 retval = 0; /* invalid value -- indicates error */
750 break;
753 switch (*cp) {
754 case 'N': case 'n':
755 case 'S': case 's':
756 *which = 1; /* latitude */
757 break;
758 case 'E': case 'e':
759 case 'W': case 'w':
760 *which = 2; /* longitude */
761 break;
762 default:
763 *which = 0; /* error */
764 break;
767 cp++; /* skip the hemisphere */
769 while (!isspace(*cp)) /* if any trailing garbage */
770 cp++;
772 while (isspace(*cp)) /* move to next field */
773 cp++;
775 *latlonstrptr = cp;
777 return (retval);
780 /* converts a zone file representation in a string to an RDATA on-the-wire
781 * representation. */
783 loc_aton(ascii, binary)
784 const char *ascii;
785 u_char *binary;
787 const char *cp, *maxcp;
788 u_char *bcp;
790 u_int32_t latit = 0, longit = 0, alt = 0;
791 u_int32_t lltemp1 = 0, lltemp2 = 0;
792 int altmeters = 0, altfrac = 0, altsign = 1;
793 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
794 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
795 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
796 int which1 = 0, which2 = 0;
798 cp = ascii;
799 maxcp = cp + strlen(ascii);
801 lltemp1 = latlon2ul(&cp, &which1);
803 lltemp2 = latlon2ul(&cp, &which2);
805 switch (which1 + which2) {
806 case 3: /* 1 + 2, the only valid combination */
807 if ((which1 == 1) && (which2 == 2)) { /* normal case */
808 latit = lltemp1;
809 longit = lltemp2;
810 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
811 longit = lltemp1;
812 latit = lltemp2;
813 } else { /* some kind of brokenness */
814 return (0);
816 break;
817 default: /* we didn't get one of each */
818 return (0);
821 /* altitude */
822 if (*cp == '-') {
823 altsign = -1;
824 cp++;
827 if (*cp == '+')
828 cp++;
830 while (isdigit(*cp))
831 altmeters = altmeters * 10 + (*cp++ - '0');
833 if (*cp == '.') { /* decimal meters */
834 cp++;
835 if (isdigit(*cp)) {
836 altfrac = (*cp++ - '0') * 10;
837 if (isdigit(*cp)) {
838 altfrac += (*cp++ - '0');
843 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
845 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
846 cp++;
848 while (isspace(*cp) && (cp < maxcp))
849 cp++;
851 if (cp >= maxcp)
852 goto defaults;
854 siz = precsize_aton(&cp);
856 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
857 cp++;
859 while (isspace(*cp) && (cp < maxcp))
860 cp++;
862 if (cp >= maxcp)
863 goto defaults;
865 hp = precsize_aton(&cp);
867 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
868 cp++;
870 while (isspace(*cp) && (cp < maxcp))
871 cp++;
873 if (cp >= maxcp)
874 goto defaults;
876 vp = precsize_aton(&cp);
878 defaults:
880 bcp = binary;
881 *bcp++ = (u_int8_t) 0; /* version byte */
882 *bcp++ = siz;
883 *bcp++ = hp;
884 *bcp++ = vp;
885 PUTLONG(latit,bcp);
886 PUTLONG(longit,bcp);
887 PUTLONG(alt,bcp);
889 return (16); /* size of RR in octets */
892 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
893 const char *
894 loc_ntoa(binary, ascii)
895 const u_char *binary;
896 char *ascii;
898 static const char error[] = "?";
899 static char tmpbuf[sizeof
900 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
901 const u_char *cp = binary;
903 int latdeg, latmin, latsec, latsecfrac;
904 int longdeg, longmin, longsec, longsecfrac;
905 char northsouth, eastwest;
906 int altmeters, altfrac, altsign;
908 const u_int32_t referencealt = 100000 * 100;
910 int32_t latval, longval, altval;
911 u_int32_t templ;
912 u_int8_t sizeval, hpval, vpval, versionval;
914 char *sizestr, *hpstr, *vpstr;
916 versionval = *cp++;
918 if (ascii == NULL)
919 ascii = tmpbuf;
921 if (versionval) {
922 (void) sprintf(ascii, "; error: unknown LOC RR version");
923 return (ascii);
926 sizeval = *cp++;
928 hpval = *cp++;
929 vpval = *cp++;
931 GETLONG(templ, cp);
932 latval = (templ - ((unsigned)1<<31));
934 GETLONG(templ, cp);
935 longval = (templ - ((unsigned)1<<31));
937 GETLONG(templ, cp);
938 if (templ < referencealt) { /* below WGS 84 spheroid */
939 altval = referencealt - templ;
940 altsign = -1;
941 } else {
942 altval = templ - referencealt;
943 altsign = 1;
946 if (latval < 0) {
947 northsouth = 'S';
948 latval = -latval;
949 } else
950 northsouth = 'N';
952 latsecfrac = latval % 1000;
953 latval = latval / 1000;
954 latsec = latval % 60;
955 latval = latval / 60;
956 latmin = latval % 60;
957 latval = latval / 60;
958 latdeg = latval;
960 if (longval < 0) {
961 eastwest = 'W';
962 longval = -longval;
963 } else
964 eastwest = 'E';
966 longsecfrac = longval % 1000;
967 longval = longval / 1000;
968 longsec = longval % 60;
969 longval = longval / 60;
970 longmin = longval % 60;
971 longval = longval / 60;
972 longdeg = longval;
974 altfrac = altval % 100;
975 altmeters = (altval / 100) * altsign;
977 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
978 sizestr = (char *) error;
979 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
980 hpstr = (char *) error;
981 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
982 vpstr = (char *) error;
984 sprintf(ascii,
985 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
986 latdeg, latmin, latsec, latsecfrac, northsouth,
987 longdeg, longmin, longsec, longsecfrac, eastwest,
988 altmeters, altfrac, sizestr, hpstr, vpstr);
990 if (sizestr != (char *) error)
991 free(sizestr);
992 if (hpstr != (char *) error)
993 free(hpstr);
994 if (vpstr != (char *) error)
995 free(vpstr);
997 return (ascii);
999 libresolv_hidden_def (loc_ntoa)
1002 /* Return the number of DNS hierarchy levels in the name. */
1004 dn_count_labels(const char *name) {
1005 int i, len, count;
1007 len = strlen(name);
1008 for (i = 0, count = 0; i < len; i++) {
1009 /* XXX need to check for \. or use named's nlabels(). */
1010 if (name[i] == '.')
1011 count++;
1014 /* don't count initial wildcard */
1015 if (name[0] == '*')
1016 if (count)
1017 count--;
1019 /* don't count the null label for root. */
1020 /* if terminating '.' not found, must adjust */
1021 /* count to include last label */
1022 if (len > 0 && name[len-1] != '.')
1023 count++;
1024 return (count);
1029 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1030 * SIG records are required to be printed like this, by the Secure DNS RFC.
1032 char *
1033 p_secstodate (u_long secs) {
1034 /* XXX nonreentrant */
1035 static char output[15]; /* YYYYMMDDHHMMSS and null */
1036 time_t clock = secs;
1037 struct tm *time;
1039 #ifdef HAVE_TIME_R
1040 struct tm timebuf;
1042 time = gmtime_r(&clock, &timebuf);
1043 #else
1044 time = gmtime(&clock);
1045 #endif
1046 time->tm_year += 1900;
1047 time->tm_mon += 1;
1048 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1049 time->tm_year, time->tm_mon, time->tm_mday,
1050 time->tm_hour, time->tm_min, time->tm_sec);
1051 return (output);