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