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