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