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