sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libresolv2 / common / resolv / res_debug.c
blobdbad7560b613ce8529ffef47510bf78cbda3da01
1 /*
2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
7 /*
8 * Portions Copyright (C) 2004, 2005, 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
9 * Portions Copyright (C) 1996-2003 Internet Software Consortium.
11 * Permission to use, copy, modify, and/or distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
25 * Copyright (c) 1985
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
74 * SOFTWARE.
78 * Portions Copyright (c) 1995 by International Business Machines, Inc.
80 * International Business Machines, Inc. (hereinafter called IBM) grants
81 * permission under its copyrights to use, copy, modify, and distribute this
82 * Software with or without fee, provided that the above copyright notice and
83 * all paragraphs of this notice appear in all copies, and that the name of IBM
84 * not be used in connection with the marketing of any product incorporating
85 * the Software or modifications thereof, without specific, written prior
86 * permission.
88 * To the extent it has a right to do so, IBM grants an immunity from suit
89 * under its patents, if any, for the use, sale or manufacture of products to
90 * the extent that such products are used for performing Domain Name System
91 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
92 * granted for any product per se or for any other function of any product.
94 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
95 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
96 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
97 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
98 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
99 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
102 #if defined(LIBC_SCCS) && !defined(lint)
103 static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
104 static const char rcsid[] = "$Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp $";
105 #endif /* LIBC_SCCS and not lint */
107 #include "port_before.h"
109 #include <sys/types.h>
110 #include <sys/param.h>
111 #include <sys/socket.h>
113 #include <netinet/in.h>
114 #include <arpa/inet.h>
115 #include <arpa/nameser.h>
117 #include <ctype.h>
118 #include <errno.h>
119 #include <math.h>
120 #include <netdb.h>
121 #include <resolv.h>
122 #include <resolv_mt.h>
123 #include <stdio.h>
124 #include <stdlib.h>
125 #include <string.h>
126 #include <time.h>
128 #include "port_after.h"
130 #ifdef SPRINTF_CHAR
131 # define SPRINTF(x) strlen(sprintf/**/x)
132 #else
133 # define SPRINTF(x) sprintf x
134 #endif
136 extern const char *_res_opcodes[];
137 extern const char *_res_sectioncodes[];
139 #ifndef ORIGINAL_ISC_CODE
140 #pragma weak __dn_count_labels = dn_count_labels
141 #pragma weak __fp_resstat = fp_resstat
142 #pragma weak __loc_aton = loc_aton
143 #pragma weak __loc_ntoa = loc_ntoa
144 #pragma weak __p_cdname = p_cdname
145 #pragma weak __p_class = p_class
146 #pragma weak __p_section = p_section
147 #pragma weak __p_time = p_time
148 #pragma weak __p_type = p_type
149 #pragma weak __sym_ntop = sym_ntop
150 #pragma weak __sym_ntos = sym_ntos
151 #pragma weak __sym_ston = sym_ston
152 #endif /* ORIGINAL_ISC_CODE */
155 * Print the current options.
157 void
158 fp_resstat(const res_state statp, FILE *file) {
159 u_long mask;
161 fprintf(file, ";; res options:");
162 for (mask = 1; mask != 0U; mask <<= 1)
163 if (statp->options & mask)
164 fprintf(file, " %s", p_option(mask));
165 putc('\n', file);
168 static void
169 do_section(const res_state statp,
170 ns_msg *handle, ns_sect section,
171 int pflag, FILE *file)
173 int n, sflag, rrnum;
174 static int buflen = 2048;
175 char *buf;
176 ns_opcode opcode;
177 ns_rr rr;
180 * Print answer records.
182 sflag = (statp->pfcode & pflag);
183 if (statp->pfcode && !sflag)
184 return;
186 buf = malloc(buflen);
187 if (buf == NULL) {
188 fprintf(file, ";; memory allocation failure\n");
189 return;
192 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
193 rrnum = 0;
194 for (;;) {
195 if (ns_parserr(handle, section, rrnum, &rr)) {
196 if (errno != ENODEV)
197 fprintf(file, ";; ns_parserr: %s\n",
198 strerror(errno));
199 else if (rrnum > 0 && sflag != 0 &&
200 (statp->pfcode & RES_PRF_HEAD1))
201 putc('\n', file);
202 goto cleanup;
204 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
205 fprintf(file, ";; %s SECTION:\n",
206 p_section(section, opcode));
207 if (section == ns_s_qd)
208 fprintf(file, ";;\t%s, type = %s, class = %s\n",
209 ns_rr_name(rr),
210 p_type(ns_rr_type(rr)),
211 p_class(ns_rr_class(rr)));
212 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
213 u_int16_t optcode, optlen, rdatalen = ns_rr_rdlen(rr);
214 u_int32_t ttl = ns_rr_ttl(rr);
216 fprintf(file,
217 "; EDNS: version: %u, udp=%u, flags=%04x\n",
218 (ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
220 while (rdatalen >= 4) {
221 const u_char *cp = ns_rr_rdata(rr);
222 int i;
224 GETSHORT(optcode, cp);
225 GETSHORT(optlen, cp);
227 if (optcode == NS_OPT_NSID) {
228 fputs("; NSID: ", file);
229 if (optlen == 0) {
230 fputs("; NSID\n", file);
231 } else {
232 fputs("; NSID: ", file);
233 for (i = 0; i < optlen; i++)
234 fprintf(file, "%02x ",
235 cp[i]);
236 fputs(" (",file);
237 for (i = 0; i < optlen; i++)
238 fprintf(file, "%c",
239 isprint(cp[i])?
240 cp[i] : '.');
241 fputs(")\n", file);
243 } else {
244 if (optlen == 0) {
245 fprintf(file, "; OPT=%u\n",
246 optcode);
247 } else {
248 fprintf(file, "; OPT=%u: ",
249 optcode);
250 for (i = 0; i < optlen; i++)
251 fprintf(file, "%02x ",
252 cp[i]);
253 fputs(" (",file);
254 for (i = 0; i < optlen; i++)
255 fprintf(file, "%c",
256 isprint(cp[i]) ?
257 cp[i] : '.');
258 fputs(")\n", file);
261 rdatalen -= 4 + optlen;
263 } else {
264 n = ns_sprintrr(handle, &rr, NULL, NULL,
265 buf, buflen);
266 if (n < 0) {
267 if (errno == ENOSPC) {
268 free(buf);
269 buf = NULL;
270 if (buflen < 131072)
271 buf = malloc(buflen += 1024);
272 if (buf == NULL) {
273 fprintf(file,
274 ";; memory allocation failure\n");
275 return;
277 continue;
279 fprintf(file, ";; ns_sprintrr: %s\n",
280 strerror(errno));
281 goto cleanup;
283 fputs(buf, file);
284 fputc('\n', file);
286 rrnum++;
288 cleanup:
289 free(buf);
293 * Print the contents of a query.
294 * This is intended to be primarily a debugging routine.
296 void
297 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
298 ns_msg handle;
299 int qdcount, ancount, nscount, arcount;
300 u_int opcode, rcode, id;
302 if (ns_initparse(msg, len, &handle) < 0) {
303 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
304 return;
306 opcode = ns_msg_getflag(handle, ns_f_opcode);
307 rcode = ns_msg_getflag(handle, ns_f_rcode);
308 id = ns_msg_id(handle);
309 qdcount = ns_msg_count(handle, ns_s_qd);
310 ancount = ns_msg_count(handle, ns_s_an);
311 nscount = ns_msg_count(handle, ns_s_ns);
312 arcount = ns_msg_count(handle, ns_s_ar);
315 * Print header fields.
317 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
318 fprintf(file,
319 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
320 _res_opcodes[opcode], p_rcode(rcode), id);
321 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
322 putc(';', file);
323 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
324 fprintf(file, "; flags:");
325 if (ns_msg_getflag(handle, ns_f_qr))
326 fprintf(file, " qr");
327 if (ns_msg_getflag(handle, ns_f_aa))
328 fprintf(file, " aa");
329 if (ns_msg_getflag(handle, ns_f_tc))
330 fprintf(file, " tc");
331 if (ns_msg_getflag(handle, ns_f_rd))
332 fprintf(file, " rd");
333 if (ns_msg_getflag(handle, ns_f_ra))
334 fprintf(file, " ra");
335 if (ns_msg_getflag(handle, ns_f_z))
336 fprintf(file, " ??");
337 if (ns_msg_getflag(handle, ns_f_ad))
338 fprintf(file, " ad");
339 if (ns_msg_getflag(handle, ns_f_cd))
340 fprintf(file, " cd");
342 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
343 fprintf(file, "; %s: %d",
344 p_section(ns_s_qd, opcode), qdcount);
345 fprintf(file, ", %s: %d",
346 p_section(ns_s_an, opcode), ancount);
347 fprintf(file, ", %s: %d",
348 p_section(ns_s_ns, opcode), nscount);
349 fprintf(file, ", %s: %d",
350 p_section(ns_s_ar, opcode), arcount);
352 if ((!statp->pfcode) || (statp->pfcode &
353 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
354 putc('\n',file);
357 * Print the various sections.
359 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
360 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
361 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
362 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
363 if (qdcount == 0 && ancount == 0 &&
364 nscount == 0 && arcount == 0)
365 putc('\n', file);
368 const u_char *
369 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
370 char name[MAXDNAME];
371 int n;
373 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
374 return (NULL);
375 if (name[0] == '\0')
376 putc('.', file);
377 else
378 fputs(name, file);
379 return (cp + n);
382 const u_char *
383 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
384 return (p_cdnname(cp, msg, PACKETSZ, file));
388 * Return a fully-qualified domain name from a compressed name (with
389 length supplied). */
391 const u_char *
392 p_fqnname(cp, msg, msglen, name, namelen)
393 const u_char *cp, *msg;
394 int msglen;
395 char *name;
396 int namelen;
398 int n, newlen;
400 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
401 return (NULL);
402 newlen = strlen(name);
403 if (newlen == 0 || name[newlen - 1] != '.') {
404 if (newlen + 1 >= namelen) /*%< Lack space for final dot */
405 return (NULL);
406 else
407 strcpy(name + newlen, ".");
409 return (cp + n);
412 /* XXX: the rest of these functions need to become length-limited, too. */
414 const u_char *
415 p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
416 char name[MAXDNAME];
417 const u_char *n;
419 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
420 if (n == NULL)
421 return (NULL);
422 fputs(name, file);
423 return (n);
427 * Names of RR classes and qclasses. Classes and qclasses are the same, except
428 * that C_ANY is a qclass but not a class. (You can ask for records of class
429 * C_ANY, but you can't have any records of that class in the database.)
431 const struct res_sym __p_class_syms[] = {
432 {C_IN, "IN", (char *)0},
433 {C_CHAOS, "CH", (char *)0},
434 {C_CHAOS, "CHAOS", (char *)0},
435 {C_HS, "HS", (char *)0},
436 {C_HS, "HESIOD", (char *)0},
437 {C_ANY, "ANY", (char *)0},
438 {C_NONE, "NONE", (char *)0},
439 {C_IN, (char *)0, (char *)0}
443 * Names of message sections.
445 const struct res_sym __p_default_section_syms[] = {
446 {ns_s_qd, "QUERY", (char *)0},
447 {ns_s_an, "ANSWER", (char *)0},
448 {ns_s_ns, "AUTHORITY", (char *)0},
449 {ns_s_ar, "ADDITIONAL", (char *)0},
450 {0, (char *)0, (char *)0}
453 const struct res_sym __p_update_section_syms[] = {
454 {S_ZONE, "ZONE", (char *)0},
455 {S_PREREQ, "PREREQUISITE", (char *)0},
456 {S_UPDATE, "UPDATE", (char *)0},
457 {S_ADDT, "ADDITIONAL", (char *)0},
458 {0, (char *)0, (char *)0}
461 const struct res_sym __p_key_syms[] = {
462 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
463 {NS_ALG_DH, "DH", "Diffie Hellman"},
464 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
465 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
466 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
467 {0, NULL, NULL}
470 const struct res_sym __p_cert_syms[] = {
471 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
472 {cert_t_spki, "SPKI", "SPKI certificate"},
473 {cert_t_pgp, "PGP", "PGP certificate"},
474 {cert_t_url, "URL", "URL Private"},
475 {cert_t_oid, "OID", "OID Private"},
476 {0, NULL, NULL}
480 * Names of RR types and qtypes. Types and qtypes are the same, except
481 * that T_ANY is a qtype but not a type. (You can ask for records of type
482 * T_ANY, but you can't have any records of that type in the database.)
484 const struct res_sym __p_type_syms[] = {
485 {ns_t_a, "A", "address"},
486 {ns_t_ns, "NS", "name server"},
487 {ns_t_md, "MD", "mail destination (deprecated)"},
488 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
489 {ns_t_cname, "CNAME", "canonical name"},
490 {ns_t_soa, "SOA", "start of authority"},
491 {ns_t_mb, "MB", "mailbox"},
492 {ns_t_mg, "MG", "mail group member"},
493 {ns_t_mr, "MR", "mail rename"},
494 {ns_t_null, "NULL", "null"},
495 {ns_t_wks, "WKS", "well-known service (deprecated)"},
496 {ns_t_ptr, "PTR", "domain name pointer"},
497 {ns_t_hinfo, "HINFO", "host information"},
498 {ns_t_minfo, "MINFO", "mailbox information"},
499 {ns_t_mx, "MX", "mail exchanger"},
500 {ns_t_txt, "TXT", "text"},
501 {ns_t_rp, "RP", "responsible person"},
502 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
503 {ns_t_x25, "X25", "X25 address"},
504 {ns_t_isdn, "ISDN", "ISDN address"},
505 {ns_t_rt, "RT", "router"},
506 {ns_t_nsap, "NSAP", "nsap address"},
507 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
508 {ns_t_sig, "SIG", "signature"},
509 {ns_t_key, "KEY", "key"},
510 {ns_t_px, "PX", "mapping information"},
511 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
512 {ns_t_aaaa, "AAAA", "IPv6 address"},
513 {ns_t_loc, "LOC", "location"},
514 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
515 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
516 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
517 {ns_t_srv, "SRV", "server selection"},
518 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
519 {ns_t_naptr, "NAPTR", "naptr"},
520 {ns_t_kx, "KX", "key exchange"},
521 {ns_t_cert, "CERT", "certificate"},
522 {ns_t_a6, "A", "IPv6 address (experminental)"},
523 {ns_t_dname, "DNAME", "non-terminal redirection"},
524 {ns_t_opt, "OPT", "opt"},
525 {ns_t_apl, "apl", "apl"},
526 {ns_t_ds, "DS", "delegation signer"},
527 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
528 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
529 {ns_t_rrsig, "RRSIG", "rrsig"},
530 {ns_t_nsec, "NSEC", "nsec"},
531 {ns_t_dnskey, "DNSKEY", "DNS key"},
532 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
533 {ns_t_nsec3, "NSEC3", "nsec3"},
534 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
535 {ns_t_hip, "HIP", "host identity protocol"},
536 {ns_t_spf, "SPF", "sender policy framework"},
537 {ns_t_tkey, "TKEY", "tkey"},
538 {ns_t_tsig, "TSIG", "transaction signature"},
539 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
540 {ns_t_axfr, "AXFR", "zone transfer"},
541 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
542 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
543 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
544 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
545 {ns_t_kx, "KX", "Key Exchange"},
546 {ns_t_cert, "CERT", "Certificate"},
547 {ns_t_a6, "A6", "IPv6 Address"},
548 {ns_t_dname, "DNAME", "dname"},
549 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
550 {ns_t_opt, "OPT", "EDNS Options"},
551 {ns_t_any, "ANY", "\"any\""},
552 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
553 {0, NULL, NULL}
557 * Names of DNS rcodes.
559 const struct res_sym __p_rcode_syms[] = {
560 {ns_r_noerror, "NOERROR", "no error"},
561 {ns_r_formerr, "FORMERR", "format error"},
562 {ns_r_servfail, "SERVFAIL", "server failed"},
563 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
564 {ns_r_notimpl, "NOTIMP", "not implemented"},
565 {ns_r_refused, "REFUSED", "refused"},
566 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
567 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
568 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
569 {ns_r_notauth, "NOTAUTH", "not authoritative"},
570 {ns_r_notzone, "NOTZONE", "Not in zone"},
571 {ns_r_max, "", ""},
572 {ns_r_badsig, "BADSIG", "bad signature"},
573 {ns_r_badkey, "BADKEY", "bad key"},
574 {ns_r_badtime, "BADTIME", "bad time"},
575 {0, NULL, NULL}
579 sym_ston(const struct res_sym *syms, const char *name, int *success) {
580 for ((void)NULL; syms->name != 0; syms++) {
581 if (strcasecmp (name, syms->name) == 0) {
582 if (success)
583 *success = 1;
584 return (syms->number);
587 if (success)
588 *success = 0;
589 return (syms->number); /*%< The default value. */
592 const char *
593 sym_ntos(const struct res_sym *syms, int number, int *success) {
594 char *unname = sym_ntos_unname;
596 for ((void)NULL; syms->name != 0; syms++) {
597 if (number == syms->number) {
598 if (success)
599 *success = 1;
600 return (syms->name);
604 sprintf(unname, "%d", number); /*%< XXX nonreentrant */
605 if (success)
606 *success = 0;
607 return (unname);
610 const char *
611 sym_ntop(const struct res_sym *syms, int number, int *success) {
612 char *unname = sym_ntop_unname;
614 for ((void)NULL; syms->name != 0; syms++) {
615 if (number == syms->number) {
616 if (success)
617 *success = 1;
618 return (syms->humanname);
621 sprintf(unname, "%d", number); /*%< XXX nonreentrant */
622 if (success)
623 *success = 0;
624 return (unname);
628 * Return a string for the type.
630 const char *
631 p_type(int type) {
632 int success;
633 const char *result;
634 static char typebuf[20];
636 result = sym_ntos(__p_type_syms, type, &success);
637 if (success)
638 return (result);
639 if (type < 0 || type > 0xffff)
640 return ("BADTYPE");
641 sprintf(typebuf, "TYPE%d", type);
642 return (typebuf);
646 * Return a string for the type.
648 const char *
649 p_section(int section, int opcode) {
650 const struct res_sym *symbols;
652 switch (opcode) {
653 case ns_o_update:
654 symbols = __p_update_section_syms;
655 break;
656 default:
657 symbols = __p_default_section_syms;
658 break;
660 return (sym_ntos(symbols, section, (int *)0));
664 * Return a mnemonic for class.
666 const char *
667 p_class(int class) {
668 int success;
669 const char *result;
670 static char classbuf[20];
672 result = sym_ntos(__p_class_syms, class, &success);
673 if (success)
674 return (result);
675 if (class < 0 || class > 0xffff)
676 return ("BADCLASS");
677 sprintf(classbuf, "CLASS%d", class);
678 return (classbuf);
682 * Return a mnemonic for an option
684 const char *
685 p_option(u_long option) {
686 char *nbuf = p_option_nbuf;
688 switch (option) {
689 case RES_INIT: return "init";
690 case RES_DEBUG: return "debug";
691 case RES_AAONLY: return "aaonly(unimpl)";
692 case RES_USEVC: return "usevc";
693 case RES_PRIMARY: return "primry(unimpl)";
694 case RES_IGNTC: return "igntc";
695 case RES_RECURSE: return "recurs";
696 case RES_DEFNAMES: return "defnam";
697 case RES_STAYOPEN: return "styopn";
698 case RES_DNSRCH: return "dnsrch";
699 case RES_INSECURE1: return "insecure1";
700 case RES_INSECURE2: return "insecure2";
701 case RES_NOALIASES: return "noaliases";
702 case RES_USE_INET6: return "inet6";
703 #ifdef RES_USE_EDNS0 /*%< KAME extension */
704 case RES_USE_EDNS0: return "edns0";
705 case RES_NSID: return "nsid";
706 #endif
707 #ifdef RES_USE_DNAME
708 case RES_USE_DNAME: return "dname";
709 #endif
710 #ifdef RES_USE_DNSSEC
711 case RES_USE_DNSSEC: return "dnssec";
712 #endif
713 #ifdef RES_NOTLDQUERY
714 case RES_NOTLDQUERY: return "no-tld-query";
715 #endif
716 #ifdef RES_NO_NIBBLE2
717 case RES_NO_NIBBLE2: return "no-nibble2";
718 #endif
719 /* XXX nonreentrant */
720 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
721 return (nbuf);
726 * Return a mnemonic for a time to live.
728 const char *
729 p_time(u_int32_t value) {
730 char *nbuf = p_time_nbuf;
732 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
733 sprintf(nbuf, "%u", value);
734 return (nbuf);
738 * Return a string for the rcode.
740 const char *
741 p_rcode(int rcode) {
742 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
746 * Return a string for a res_sockaddr_union.
748 const char *
749 p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
750 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
752 switch (u.sin.sin_family) {
753 case AF_INET:
754 inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
755 break;
756 #ifdef HAS_INET6_STRUCTS
757 case AF_INET6:
758 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
759 break;
760 #endif
761 default:
762 sprintf(ret, "[af%d]", u.sin.sin_family);
763 break;
765 if (size > 0U) {
766 strncpy(buf, ret, size - 1);
767 buf[size - 1] = '0';
769 return (buf);
773 * routines to convert between on-the-wire RR format and zone file format.
774 * Does not contain conversion to/from decimal degrees; divide or multiply
775 * by 60*60*1000 for that.
778 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
779 1000000,10000000,100000000,1000000000};
781 /*% takes an XeY precision/size value, returns a string representation. */
782 static const char *
783 precsize_ntoa(prec)
784 u_int8_t prec;
786 char *retbuf = precsize_ntoa_retbuf;
787 unsigned long val;
788 int mantissa, exponent;
790 mantissa = (int)((prec >> 4) & 0x0f) % 10;
791 exponent = (int)((prec >> 0) & 0x0f) % 10;
793 val = mantissa * poweroften[exponent];
795 (void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
796 return (retbuf);
799 /*% converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
800 static u_int8_t
801 precsize_aton(const char **strptr) {
802 unsigned int mval = 0, cmval = 0;
803 u_int8_t retval = 0;
804 const char *cp;
805 int exponent;
806 int mantissa;
808 cp = *strptr;
810 while (isdigit((unsigned char)*cp))
811 mval = mval * 10 + (*cp++ - '0');
813 if (*cp == '.') { /*%< centimeters */
814 cp++;
815 if (isdigit((unsigned char)*cp)) {
816 cmval = (*cp++ - '0') * 10;
817 if (isdigit((unsigned char)*cp)) {
818 cmval += (*cp++ - '0');
822 cmval = (mval * 100) + cmval;
824 for (exponent = 0; exponent < 9; exponent++)
825 if (cmval < poweroften[exponent+1])
826 break;
828 mantissa = cmval / poweroften[exponent];
829 if (mantissa > 9)
830 mantissa = 9;
832 retval = (mantissa << 4) | exponent;
834 *strptr = cp;
836 return (retval);
839 /*% converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
840 static u_int32_t
841 latlon2ul(const char **latlonstrptr, int *which) {
842 const char *cp;
843 u_int32_t retval;
844 int deg = 0, min = 0, secs = 0, secsfrac = 0;
846 cp = *latlonstrptr;
848 while (isdigit((unsigned char)*cp))
849 deg = deg * 10 + (*cp++ - '0');
851 while (isspace((unsigned char)*cp))
852 cp++;
854 if (!(isdigit((unsigned char)*cp)))
855 goto fndhemi;
857 while (isdigit((unsigned char)*cp))
858 min = min * 10 + (*cp++ - '0');
860 while (isspace((unsigned char)*cp))
861 cp++;
863 if (!(isdigit((unsigned char)*cp)))
864 goto fndhemi;
866 while (isdigit((unsigned char)*cp))
867 secs = secs * 10 + (*cp++ - '0');
869 if (*cp == '.') { /*%< decimal seconds */
870 cp++;
871 if (isdigit((unsigned char)*cp)) {
872 secsfrac = (*cp++ - '0') * 100;
873 if (isdigit((unsigned char)*cp)) {
874 secsfrac += (*cp++ - '0') * 10;
875 if (isdigit((unsigned char)*cp)) {
876 secsfrac += (*cp++ - '0');
882 while (!isspace((unsigned char)*cp)) /*%< if any trailing garbage */
883 cp++;
885 while (isspace((unsigned char)*cp))
886 cp++;
888 fndhemi:
889 switch (*cp) {
890 case 'N': case 'n':
891 case 'E': case 'e':
892 retval = ((unsigned)1<<31)
893 + (((((deg * 60) + min) * 60) + secs) * 1000)
894 + secsfrac;
895 break;
896 case 'S': case 's':
897 case 'W': case 'w':
898 retval = ((unsigned)1<<31)
899 - (((((deg * 60) + min) * 60) + secs) * 1000)
900 - secsfrac;
901 break;
902 default:
903 retval = 0; /*%< invalid value -- indicates error */
904 break;
907 switch (*cp) {
908 case 'N': case 'n':
909 case 'S': case 's':
910 *which = 1; /*%< latitude */
911 break;
912 case 'E': case 'e':
913 case 'W': case 'w':
914 *which = 2; /*%< longitude */
915 break;
916 default:
917 *which = 0; /*%< error */
918 break;
921 cp++; /*%< skip the hemisphere */
922 while (!isspace((unsigned char)*cp)) /*%< if any trailing garbage */
923 cp++;
925 while (isspace((unsigned char)*cp)) /*%< move to next field */
926 cp++;
928 *latlonstrptr = cp;
930 return (retval);
934 * converts a zone file representation in a string to an RDATA on-the-wire
935 * representation. */
937 loc_aton(ascii, binary)
938 const char *ascii;
939 u_char *binary;
941 const char *cp, *maxcp;
942 u_char *bcp;
944 u_int32_t latit = 0, longit = 0, alt = 0;
945 u_int32_t lltemp1 = 0, lltemp2 = 0;
946 int altmeters = 0, altfrac = 0, altsign = 1;
947 u_int8_t hp = 0x16; /*%< default = 1e6 cm = 10000.00m = 10km */
948 u_int8_t vp = 0x13; /*%< default = 1e3 cm = 10.00m */
949 u_int8_t siz = 0x12; /*%< default = 1e2 cm = 1.00m */
950 int which1 = 0, which2 = 0;
952 cp = ascii;
953 maxcp = cp + strlen(ascii);
955 lltemp1 = latlon2ul(&cp, &which1);
957 lltemp2 = latlon2ul(&cp, &which2);
959 switch (which1 + which2) {
960 case 3: /*%< 1 + 2, the only valid combination */
961 if ((which1 == 1) && (which2 == 2)) { /*%< normal case */
962 latit = lltemp1;
963 longit = lltemp2;
964 } else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */
965 longit = lltemp1;
966 latit = lltemp2;
967 } else { /*%< some kind of brokenness */
968 return (0);
970 break;
971 default: /*%< we didn't get one of each */
972 return (0);
975 /* altitude */
976 if (*cp == '-') {
977 altsign = -1;
978 cp++;
981 if (*cp == '+')
982 cp++;
984 while (isdigit((unsigned char)*cp))
985 altmeters = altmeters * 10 + (*cp++ - '0');
987 if (*cp == '.') { /*%< decimal meters */
988 cp++;
989 if (isdigit((unsigned char)*cp)) {
990 altfrac = (*cp++ - '0') * 10;
991 if (isdigit((unsigned char)*cp)) {
992 altfrac += (*cp++ - '0');
997 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
999 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
1000 cp++;
1002 while (isspace((unsigned char)*cp) && (cp < maxcp))
1003 cp++;
1005 if (cp >= maxcp)
1006 goto defaults;
1008 siz = precsize_aton(&cp);
1010 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
1011 cp++;
1013 while (isspace((unsigned char)*cp) && (cp < maxcp))
1014 cp++;
1016 if (cp >= maxcp)
1017 goto defaults;
1019 hp = precsize_aton(&cp);
1021 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */
1022 cp++;
1024 while (isspace((unsigned char)*cp) && (cp < maxcp))
1025 cp++;
1027 if (cp >= maxcp)
1028 goto defaults;
1030 vp = precsize_aton(&cp);
1032 defaults:
1034 bcp = binary;
1035 *bcp++ = (u_int8_t) 0; /*%< version byte */
1036 *bcp++ = siz;
1037 *bcp++ = hp;
1038 *bcp++ = vp;
1039 PUTLONG(latit,bcp);
1040 PUTLONG(longit,bcp);
1041 PUTLONG(alt,bcp);
1043 return (16); /*%< size of RR in octets */
1046 /*% takes an on-the-wire LOC RR and formats it in a human readable format. */
1047 const char *
1048 loc_ntoa(binary, ascii)
1049 const u_char *binary;
1050 char *ascii;
1052 static const char *error = "?";
1053 static char tmpbuf[sizeof
1054 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
1055 const u_char *cp = binary;
1057 int latdeg, latmin, latsec, latsecfrac;
1058 int longdeg, longmin, longsec, longsecfrac;
1059 char northsouth, eastwest;
1060 const char *altsign;
1061 int altmeters, altfrac;
1063 const u_int32_t referencealt = 100000 * 100;
1065 int32_t latval, longval, altval;
1066 u_int32_t templ;
1067 u_int8_t sizeval, hpval, vpval, versionval;
1069 char *sizestr, *hpstr, *vpstr;
1071 versionval = *cp++;
1073 if (ascii == NULL)
1074 ascii = tmpbuf;
1076 if (versionval) {
1077 (void) sprintf(ascii, "; error: unknown LOC RR version");
1078 return (ascii);
1081 sizeval = *cp++;
1083 hpval = *cp++;
1084 vpval = *cp++;
1086 GETLONG(templ, cp);
1087 latval = (templ - ((unsigned)1<<31));
1089 GETLONG(templ, cp);
1090 longval = (templ - ((unsigned)1<<31));
1092 GETLONG(templ, cp);
1093 if (templ < referencealt) { /*%< below WGS 84 spheroid */
1094 altval = referencealt - templ;
1095 altsign = "-";
1096 } else {
1097 altval = templ - referencealt;
1098 altsign = "";
1101 if (latval < 0) {
1102 northsouth = 'S';
1103 latval = -latval;
1104 } else
1105 northsouth = 'N';
1107 latsecfrac = latval % 1000;
1108 latval = latval / 1000;
1109 latsec = latval % 60;
1110 latval = latval / 60;
1111 latmin = latval % 60;
1112 latval = latval / 60;
1113 latdeg = latval;
1115 if (longval < 0) {
1116 eastwest = 'W';
1117 longval = -longval;
1118 } else
1119 eastwest = 'E';
1121 longsecfrac = longval % 1000;
1122 longval = longval / 1000;
1123 longsec = longval % 60;
1124 longval = longval / 60;
1125 longmin = longval % 60;
1126 longval = longval / 60;
1127 longdeg = longval;
1129 altfrac = altval % 100;
1130 altmeters = (altval / 100);
1132 sizestr = strdup(precsize_ntoa(sizeval));
1133 hpstr = strdup(precsize_ntoa(hpval));
1134 vpstr = strdup(precsize_ntoa(vpval));
1136 sprintf(ascii,
1137 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
1138 latdeg, latmin, latsec, latsecfrac, northsouth,
1139 longdeg, longmin, longsec, longsecfrac, eastwest,
1140 altsign, altmeters, altfrac,
1141 (sizestr != NULL) ? sizestr : error,
1142 (hpstr != NULL) ? hpstr : error,
1143 (vpstr != NULL) ? vpstr : error);
1145 if (sizestr != NULL)
1146 free(sizestr);
1147 if (hpstr != NULL)
1148 free(hpstr);
1149 if (vpstr != NULL)
1150 free(vpstr);
1152 return (ascii);
1156 /*% Return the number of DNS hierarchy levels in the name. */
1158 dn_count_labels(const char *name) {
1159 int i, len, count;
1161 len = strlen(name);
1162 for (i = 0, count = 0; i < len; i++) {
1163 /* XXX need to check for \. or use named's nlabels(). */
1164 if (name[i] == '.')
1165 count++;
1168 /* don't count initial wildcard */
1169 if (name[0] == '*')
1170 if (count)
1171 count--;
1173 /* don't count the null label for root. */
1174 /* if terminating '.' not found, must adjust */
1175 /* count to include last label */
1176 if (len > 0 && name[len-1] != '.')
1177 count++;
1178 return (count);
1182 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1183 * SIG records are required to be printed like this, by the Secure DNS RFC.
1185 char *
1186 p_secstodate (u_long secs) {
1187 char *output = p_secstodate_output;
1188 time_t clock = secs;
1189 struct tm *time;
1190 #ifdef HAVE_TIME_R
1191 struct tm res;
1193 time = gmtime_r(&clock, &res);
1194 #else
1195 time = gmtime(&clock);
1196 #endif
1197 time->tm_year += 1900;
1198 time->tm_mon += 1;
1199 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1200 time->tm_year, time->tm_mon, time->tm_mday,
1201 time->tm_hour, time->tm_min, time->tm_sec);
1202 return (output);
1205 u_int16_t
1206 res_nametoclass(const char *buf, int *successp) {
1207 unsigned long result;
1208 char *endptr;
1209 int success;
1211 result = sym_ston(__p_class_syms, buf, &success);
1212 if (success)
1213 goto done;
1215 if (strncasecmp(buf, "CLASS", 5) != 0 ||
1216 !isdigit((unsigned char)buf[5]))
1217 goto done;
1218 errno = 0;
1219 result = strtoul(buf + 5, &endptr, 10);
1220 if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
1221 success = 1;
1222 done:
1223 if (successp)
1224 *successp = success;
1225 return (result);
1228 u_int16_t
1229 res_nametotype(const char *buf, int *successp) {
1230 unsigned long result;
1231 char *endptr;
1232 int success;
1234 result = sym_ston(__p_type_syms, buf, &success);
1235 if (success)
1236 goto done;
1238 if (strncasecmp(buf, "type", 4) != 0 ||
1239 !isdigit((unsigned char)buf[4]))
1240 goto done;
1241 errno = 0;
1242 result = strtoul(buf + 4, &endptr, 10);
1243 if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
1244 success = 1;
1245 done:
1246 if (successp)
1247 *successp = success;
1248 return (result);
1251 /*! \file */