summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_debug.c')
-rw-r--r--src/lib/libc/net/res_debug.c1509
1 files changed, 1509 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c
new file mode 100644
index 0000000000..82a067c1f0
--- /dev/null
+++ b/src/lib/libc/net/res_debug.c
@@ -0,0 +1,1509 @@
1/* $OpenBSD: res_debug.c,v 1.20 2005/03/30 02:58:28 tedu Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1990, 1993
5 * -
6 * Copyright (c) 1985, 1990, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * Portions Copyright (c) 1995 by International Business Machines, Inc.
52 *
53 * International Business Machines, Inc. (hereinafter called IBM) grants
54 * permission under its copyrights to use, copy, modify, and distribute this
55 * Software with or without fee, provided that the above copyright notice and
56 * all paragraphs of this notice appear in all copies, and that the name of IBM
57 * not be used in connection with the marketing of any product incorporating
58 * the Software or modifications thereof, without specific, written prior
59 * permission.
60 *
61 * To the extent it has a right to do so, IBM grants an immunity from suit
62 * under its patents, if any, for the use, sale or manufacture of products to
63 * the extent that such products are used for performing Domain Name System
64 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
65 * granted for any product per se or for any other function of any product.
66 *
67 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
68 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
69 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
70 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
71 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
72 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
73 * --Copyright--
74 */
75
76#if defined(LIBC_SCCS) && !defined(lint)
77#if 0
78static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
79static char rcsid[] = "$From: res_debug.c,v 8.19 1996/11/26 10:11:23 vixie Exp $";
80#else
81static char rcsid[] = "$OpenBSD: res_debug.c,v 1.20 2005/03/30 02:58:28 tedu Exp $";
82#endif
83#endif /* LIBC_SCCS and not lint */
84
85#include <sys/param.h>
86#include <sys/types.h>
87#include <sys/socket.h>
88#include <netinet/in.h>
89#include <arpa/inet.h>
90#include <arpa/nameser.h>
91
92#include <ctype.h>
93#include <netdb.h>
94#include <resolv.h>
95#include <stdio.h>
96#include <time.h>
97
98#include <stdlib.h>
99#include <string.h>
100
101#include "thread_private.h"
102
103extern const char *_res_opcodes[];
104extern const char *_res_resultcodes[];
105
106static const char *loc_ntoal(const u_char *binary, char *ascii, int ascii_len);
107
108/* XXX: we should use getservbyport() instead. */
109static const char *
110dewks(int wks)
111{
112 static char nbuf[20];
113
114 switch (wks) {
115 case 5: return "rje";
116 case 7: return "echo";
117 case 9: return "discard";
118 case 11: return "systat";
119 case 13: return "daytime";
120 case 15: return "netstat";
121 case 17: return "qotd";
122 case 19: return "chargen";
123 case 20: return "ftp-data";
124 case 21: return "ftp";
125 case 23: return "telnet";
126 case 25: return "smtp";
127 case 37: return "time";
128 case 39: return "rlp";
129 case 42: return "name";
130 case 43: return "whois";
131 case 53: return "domain";
132 case 57: return "apts";
133 case 59: return "apfs";
134 case 67: return "bootps";
135 case 68: return "bootpc";
136 case 69: return "tftp";
137 case 77: return "rje";
138 case 79: return "finger";
139 case 87: return "link";
140 case 95: return "supdup";
141 case 100: return "newacct";
142 case 101: return "hostnames";
143 case 102: return "iso-tsap";
144 case 103: return "x400";
145 case 104: return "x400-snd";
146 case 105: return "csnet-ns";
147 case 109: return "pop-2";
148 case 111: return "sunrpc";
149 case 113: return "auth";
150 case 115: return "sftp";
151 case 117: return "uucp-path";
152 case 119: return "nntp";
153 case 121: return "erpc";
154 case 123: return "ntp";
155 case 133: return "statsrv";
156 case 136: return "profile";
157 case 144: return "NeWS";
158 case 161: return "snmp";
159 case 162: return "snmp-trap";
160 case 170: return "print-srv";
161 default:
162 (void) snprintf(nbuf, sizeof nbuf, "%d", wks);
163 return (nbuf);
164 }
165}
166
167/* XXX: we should use getprotobynumber() instead. */
168static const char *
169deproto(int protonum)
170{
171 static char nbuf[20];
172
173 switch (protonum) {
174 case 1: return "icmp";
175 case 2: return "igmp";
176 case 3: return "ggp";
177 case 5: return "st";
178 case 6: return "tcp";
179 case 7: return "ucl";
180 case 8: return "egp";
181 case 9: return "igp";
182 case 11: return "nvp-II";
183 case 12: return "pup";
184 case 16: return "chaos";
185 case 17: return "udp";
186 default:
187 (void) snprintf(nbuf, sizeof nbuf, "%d", protonum);
188 return (nbuf);
189 }
190}
191
192static const u_char *
193do_rrset(const u_char *msg, int len, const u_char *cp, int cnt, int pflag,
194 FILE *file, const char *hs)
195{
196 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
197 int n;
198 int sflag;
199
200 /*
201 * Print answer records.
202 */
203 sflag = (_resp->pfcode & pflag);
204 if ((n = ntohs(cnt))) {
205 if ((!_resp->pfcode) ||
206 ((sflag) && (_resp->pfcode & RES_PRF_HEAD1)))
207 fprintf(file, "%s", hs);
208 while (--n >= 0) {
209 if ((!_resp->pfcode) || sflag) {
210 cp = p_rr(cp, msg, file);
211 } else {
212 unsigned int dlen;
213 cp += __dn_skipname(cp, cp + MAXCDNAME);
214 cp += INT16SZ;
215 cp += INT16SZ;
216 cp += INT32SZ;
217 dlen = _getshort((u_char*)cp);
218 cp += INT16SZ;
219 cp += dlen;
220 }
221 if ((cp - msg) > len)
222 return (NULL);
223 }
224 if ((!_resp->pfcode) ||
225 ((sflag) && (_resp->pfcode & RES_PRF_HEAD1)))
226 putc('\n', file);
227 }
228 return (cp);
229}
230
231void
232__p_query(const u_char *msg)
233{
234 __fp_query(msg, stdout);
235}
236
237/*
238 * Print the current options.
239 * This is intended to be primarily a debugging routine.
240 */
241void
242__fp_resstat(struct __res_state *statp, FILE *file)
243{
244 u_long mask;
245
246 fprintf(file, ";; res options:");
247 if (!statp)
248 statp = &_res;
249 for (mask = 1; mask != 0; mask <<= 1)
250 if (statp->options & mask)
251 fprintf(file, " %s", p_option(mask));
252 putc('\n', file);
253}
254
255/*
256 * Print the contents of a query.
257 * This is intended to be primarily a debugging routine.
258 */
259void
260__fp_nquery(const u_char *msg, int len, FILE *file)
261{
262 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
263 const u_char *cp, *endMark;
264 const HEADER *hp;
265 int n;
266
267 if (_res_init(0) == -1)
268 return;
269
270#define TruncTest(x) if (x > endMark) goto trunc
271#define ErrorTest(x) if (x == NULL) goto error
272
273 /*
274 * Print header fields.
275 */
276 hp = (HEADER *)msg;
277 cp = msg + HFIXEDSZ;
278 endMark = msg + len;
279 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX) || hp->rcode) {
280 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %u",
281 _res_opcodes[hp->opcode],
282 _res_resultcodes[hp->rcode],
283 ntohs(hp->id));
284 putc('\n', file);
285 }
286 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX))
287 putc(';', file);
288 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD2)) {
289 fprintf(file, "; flags:");
290 if (hp->qr)
291 fprintf(file, " qr");
292 if (hp->aa)
293 fprintf(file, " aa");
294 if (hp->tc)
295 fprintf(file, " tc");
296 if (hp->rd)
297 fprintf(file, " rd");
298 if (hp->ra)
299 fprintf(file, " ra");
300 if (hp->unused)
301 fprintf(file, " UNUSED-BIT-ON");
302 if (hp->ad)
303 fprintf(file, " ad");
304 if (hp->cd)
305 fprintf(file, " cd");
306 }
307 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD1)) {
308 fprintf(file, "; Ques: %u", ntohs(hp->qdcount));
309 fprintf(file, ", Ans: %u", ntohs(hp->ancount));
310 fprintf(file, ", Auth: %u", ntohs(hp->nscount));
311 fprintf(file, ", Addit: %u", ntohs(hp->arcount));
312 }
313 if ((!_resp->pfcode) || (_resp->pfcode &
314 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
315 putc('\n',file);
316 }
317 /*
318 * Print question records.
319 */
320 if ((n = ntohs(hp->qdcount))) {
321 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
322 fprintf(file, ";; QUESTIONS:\n");
323 while (--n >= 0) {
324 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
325 fprintf(file, ";;\t");
326 TruncTest(cp);
327 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
328 cp = p_cdnname(cp, msg, len, file);
329 else {
330 int n;
331 char name[MAXDNAME];
332
333 if ((n = dn_expand(msg, msg+len, cp, name,
334 sizeof name)) < 0)
335 cp = NULL;
336 else
337 cp += n;
338 }
339 ErrorTest(cp);
340 TruncTest(cp);
341 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
342 fprintf(file, ", type = %s",
343 __p_type(_getshort((u_char*)cp)));
344 cp += INT16SZ;
345 TruncTest(cp);
346 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
347 fprintf(file, ", class = %s\n",
348 __p_class(_getshort((u_char*)cp)));
349 cp += INT16SZ;
350 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
351 putc('\n', file);
352 }
353 }
354 /*
355 * Print authoritative answer records
356 */
357 TruncTest(cp);
358 cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file,
359 ";; ANSWERS:\n");
360 ErrorTest(cp);
361
362 /*
363 * print name server records
364 */
365 TruncTest(cp);
366 cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file,
367 ";; AUTHORITY RECORDS:\n");
368 ErrorTest(cp);
369
370 TruncTest(cp);
371 /*
372 * print additional records
373 */
374 cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file,
375 ";; ADDITIONAL RECORDS:\n");
376 ErrorTest(cp);
377 return;
378 trunc:
379 fprintf(file, "\n;; ...truncated\n");
380 return;
381 error:
382 fprintf(file, "\n;; ...malformed\n");
383}
384
385void
386__fp_query(const u_char *msg, FILE *file)
387{
388 fp_nquery(msg, PACKETSZ, file);
389}
390
391const u_char *
392__p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file)
393{
394 char name[MAXDNAME];
395 int n;
396
397 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
398 return (NULL);
399 if (name[0] == '\0')
400 putc('.', file);
401 else
402 fputs(name, file);
403 return (cp + n);
404}
405
406const u_char *
407__p_cdname(const u_char *cp, const u_char *msg, FILE *file)
408{
409 return (p_cdnname(cp, msg, PACKETSZ, file));
410}
411
412
413/* Return a fully-qualified domain name from a compressed name (with
414 length supplied). */
415
416const u_char *
417__p_fqnname(const u_char *cp, const u_char *msg, int msglen, char *name, int namelen)
418{
419 int n, newlen;
420
421 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
422 return (NULL);
423 newlen = strlen(name);
424 if (newlen == 0 || name[newlen - 1] != '.') {
425 if (newlen + 1 >= namelen) /* Lack space for final dot */
426 return (NULL);
427 else
428 strlcpy(name + newlen, ".", namelen - newlen);
429 }
430 return (cp + n);
431}
432
433/* XXX: the rest of these functions need to become length-limited, too. (vix)
434 */
435
436const u_char *
437__p_fqname(const u_char *cp, const u_char *msg, FILE *file)
438{
439 char name[MAXDNAME];
440 const u_char *n;
441
442 n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
443 if (n == NULL)
444 return (NULL);
445 fputs(name, file);
446 return (n);
447}
448
449/*
450 * Print resource record fields in human readable form.
451 */
452const u_char *
453__p_rr(const u_char *cp, const u_char *msg, FILE *file)
454{
455 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
456 int type, class, dlen, n, c;
457 struct in_addr inaddr;
458 const u_char *cp1, *cp2;
459 u_int32_t tmpttl, t;
460 int lcnt;
461 u_int16_t keyflags;
462 char rrname[MAXDNAME]; /* The fqdn of this RR */
463 char base64_key[MAX_KEY_BASE64];
464
465 if (_res_init(0) == -1) {
466 h_errno = NETDB_INTERNAL;
467 return (NULL);
468 }
469 cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname);
470 if (!cp)
471 return (NULL); /* compression error */
472 fputs(rrname, file);
473
474 type = _getshort((u_char*)cp);
475 cp += INT16SZ;
476 class = _getshort((u_char*)cp);
477 cp += INT16SZ;
478 tmpttl = _getlong((u_char*)cp);
479 cp += INT32SZ;
480 dlen = _getshort((u_char*)cp);
481 cp += INT16SZ;
482 cp1 = cp;
483 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_TTLID))
484 fprintf(file, "\t%lu", (u_long)tmpttl);
485 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_CLASS))
486 fprintf(file, "\t%s", __p_class(class));
487 fprintf(file, "\t%s", __p_type(type));
488 /*
489 * Print type specific data, if appropriate
490 */
491 switch (type) {
492 case T_A:
493 switch (class) {
494 case C_IN:
495 case C_HS:
496 bcopy(cp, (char *)&inaddr, INADDRSZ);
497 if (dlen == 4) {
498 fprintf(file, "\t%s", inet_ntoa(inaddr));
499 cp += dlen;
500 } else if (dlen == 7) {
501 char *address;
502 u_char protocol;
503 in_port_t port;
504
505 address = inet_ntoa(inaddr);
506 cp += INADDRSZ;
507 protocol = *(u_char*)cp;
508 cp += sizeof (u_char);
509 port = _getshort((u_char*)cp);
510 cp += INT16SZ;
511 fprintf(file, "\t%s\t; proto %u, port %u",
512 address, protocol, port);
513 }
514 break;
515 default:
516 cp += dlen;
517 }
518 break;
519 case T_CNAME:
520 case T_MB:
521 case T_MG:
522 case T_MR:
523 case T_NS:
524 case T_PTR:
525 putc('\t', file);
526 if ((cp = p_fqname(cp, msg, file)) == NULL)
527 return (NULL);
528 break;
529
530 case T_HINFO:
531 case T_ISDN:
532 cp2 = cp + dlen;
533 (void) fputs("\t\"", file);
534 if ((n = (unsigned char) *cp++) != 0) {
535 for (c = n; c > 0 && cp < cp2; c--) {
536 if (strchr("\n\"\\", *cp))
537 (void) putc('\\', file);
538 (void) putc(*cp++, file);
539 }
540 }
541 putc('"', file);
542 if (cp < cp2 && (n = (unsigned char) *cp++) != 0) {
543 (void) fputs ("\t\"", file);
544 for (c = n; c > 0 && cp < cp2; c--) {
545 if (strchr("\n\"\\", *cp))
546 (void) putc('\\', file);
547 (void) putc(*cp++, file);
548 }
549 putc('"', file);
550 } else if (type == T_HINFO) {
551 (void) fputs("\"?\"", file);
552 fprintf(file, "\n;; *** Warning *** OS-type missing");
553 }
554 break;
555
556 case T_SOA:
557 putc('\t', file);
558 if ((cp = p_fqname(cp, msg, file)) == NULL)
559 return (NULL);
560 putc(' ', file);
561 if ((cp = p_fqname(cp, msg, file)) == NULL)
562 return (NULL);
563 fputs(" (\n", file);
564 t = _getlong((u_char*)cp); cp += INT32SZ;
565 fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t);
566 t = _getlong((u_char*)cp); cp += INT32SZ;
567 fprintf(file, "\t\t\t%lu\t; refresh (%s)\n",
568 (u_long)t, __p_time(t));
569 t = _getlong((u_char*)cp); cp += INT32SZ;
570 fprintf(file, "\t\t\t%lu\t; retry (%s)\n",
571 (u_long)t, __p_time(t));
572 t = _getlong((u_char*)cp); cp += INT32SZ;
573 fprintf(file, "\t\t\t%lu\t; expire (%s)\n",
574 (u_long)t, __p_time(t));
575 t = _getlong((u_char*)cp); cp += INT32SZ;
576 fprintf(file, "\t\t\t%lu )\t; minimum (%s)",
577 (u_long)t, __p_time(t));
578 break;
579
580 case T_MX:
581 case T_AFSDB:
582 case T_RT:
583 fprintf(file, "\t%u ", _getshort((u_char*)cp));
584 cp += INT16SZ;
585 if ((cp = p_fqname(cp, msg, file)) == NULL)
586 return (NULL);
587 break;
588
589 case T_PX:
590 fprintf(file, "\t%u ", _getshort((u_char*)cp));
591 cp += INT16SZ;
592 if ((cp = p_fqname(cp, msg, file)) == NULL)
593 return (NULL);
594 putc(' ', file);
595 if ((cp = p_fqname(cp, msg, file)) == NULL)
596 return (NULL);
597 break;
598
599 case T_X25:
600 cp2 = cp + dlen;
601 (void) fputs("\t\"", file);
602 if ((n = (unsigned char) *cp++) != 0) {
603 for (c = n; c > 0 && cp < cp2; c--) {
604 if (strchr("\n\"\\", *cp))
605 (void) putc('\\', file);
606 (void) putc(*cp++, file);
607 }
608 }
609 putc('"', file);
610 break;
611
612 case T_TXT:
613 (void) putc('\t', file);
614 cp2 = cp1 + dlen;
615 while (cp < cp2) {
616 putc('"', file);
617 if ((n = (unsigned char) *cp++)) {
618 for (c = n; c > 0 && cp < cp2; c--) {
619 if (strchr("\n\"\\", *cp))
620 (void) putc('\\', file);
621 (void) putc(*cp++, file);
622 }
623 }
624 putc('"', file);
625 if (cp < cp2)
626 putc(' ', file);
627 }
628 break;
629
630 case T_NSAP:
631 (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL));
632 cp += dlen;
633 break;
634
635 case T_AAAA: {
636 char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
637
638 fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t));
639 cp += dlen;
640 break;
641 }
642
643 case T_LOC: {
644 char t[255];
645
646 fprintf(file, "\t%s", loc_ntoal(cp, t, sizeof t));
647 cp += dlen;
648 break;
649 }
650
651 case T_NAPTR: {
652 u_int order, preference;
653
654 order = _getshort(cp); cp += INT16SZ;
655 preference = _getshort(cp); cp += INT16SZ;
656 fprintf(file, "\t%u %u ",order, preference);
657 /* Flags */
658 n = *cp++;
659 fprintf(file,"\"%.*s\" ", (int)n, cp);
660 cp += n;
661 /* Service */
662 n = *cp++;
663 fprintf(file,"\"%.*s\" ", (int)n, cp);
664 cp += n;
665 /* Regexp */
666 n = *cp++;
667 fprintf(file,"\"%.*s\" ", (int)n, cp);
668 cp += n;
669 if ((cp = p_fqname(cp, msg, file)) == NULL)
670 return (NULL);
671 break;
672 }
673
674 case T_SRV: {
675 u_int priority, weight, port;
676
677 priority = _getshort(cp); cp += INT16SZ;
678 weight = _getshort(cp); cp += INT16SZ;
679 port = _getshort(cp); cp += INT16SZ;
680 fprintf(file, "\t%u %u %u ", priority, weight, port);
681 if ((cp = p_fqname(cp, msg, file)) == NULL)
682 return (NULL);
683 break;
684 }
685
686 case T_MINFO:
687 case T_RP:
688 putc('\t', file);
689 if ((cp = p_fqname(cp, msg, file)) == NULL)
690 return (NULL);
691 putc(' ', file);
692 if ((cp = p_fqname(cp, msg, file)) == NULL)
693 return (NULL);
694 break;
695
696 case T_UINFO:
697 putc('\t', file);
698 fputs((char *)cp, file);
699 cp += dlen;
700 break;
701
702 case T_UID:
703 case T_GID:
704 if (dlen == 4) {
705 fprintf(file, "\t%u", _getlong((u_char*)cp));
706 cp += INT32SZ;
707 }
708 break;
709
710 case T_WKS:
711 if (dlen < INT32SZ + 1)
712 break;
713 bcopy(cp, (char *)&inaddr, INADDRSZ);
714 cp += INT32SZ;
715 fprintf(file, "\t%s %s ( ",
716 inet_ntoa(inaddr),
717 deproto((int) *cp));
718 cp += sizeof (u_char);
719 n = 0;
720 lcnt = 0;
721 while (cp < cp1 + dlen) {
722 c = *cp++;
723 do {
724 if (c & 0200) {
725 if (lcnt == 0) {
726 fputs("\n\t\t\t", file);
727 lcnt = 5;
728 }
729 fputs(dewks(n), file);
730 putc(' ', file);
731 lcnt--;
732 }
733 c <<= 1;
734 } while (++n & 07);
735 }
736 putc(')', file);
737 break;
738
739 case T_KEY:
740 putc('\t', file);
741 keyflags = _getshort(cp);
742 cp += 2;
743 fprintf(file,"0x%04x", keyflags ); /* flags */
744 fprintf(file," %u", *cp++); /* protocol */
745 fprintf(file," %u (", *cp++); /* algorithm */
746
747 n = b64_ntop(cp, (cp1 + dlen) - cp,
748 base64_key, sizeof base64_key);
749 for (c = 0; c < n; ++c) {
750 if (0 == (c & 0x3F))
751 fprintf(file, "\n\t");
752 putc(base64_key[c], file); /* public key data */
753 }
754
755 fprintf(file, " )");
756 if (n < 0)
757 fprintf(file, "\t; BAD BASE64");
758 fflush(file);
759 cp = cp1 + dlen;
760 break;
761
762 case T_SIG:
763 type = _getshort((u_char*)cp);
764 cp += INT16SZ;
765 fprintf(file, " %s", p_type(type));
766 fprintf(file, "\t%u", *cp++); /* algorithm */
767 /* Check label value and print error if wrong. */
768 n = *cp++;
769 c = dn_count_labels (rrname);
770 if (n != c)
771 fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t",
772 n, c);
773 /* orig ttl */
774 n = _getlong((u_char*)cp);
775 if (n != tmpttl)
776 fprintf(file, " %u", n);
777 cp += INT32SZ;
778 /* sig expire */
779 fprintf(file, " (\n\t%s",
780 __p_secstodate(_getlong((u_char*)cp)));
781 cp += INT32SZ;
782 /* time signed */
783 fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp)));
784 cp += INT32SZ;
785 /* sig footprint */
786 fprintf(file," %u ", _getshort((u_char*)cp));
787 cp += INT16SZ;
788 /* signer's name */
789 cp = p_fqname(cp, msg, file);
790 n = b64_ntop(cp, (cp1 + dlen) - cp,
791 base64_key, sizeof base64_key);
792 for (c = 0; c < n; c++) {
793 if (0 == (c & 0x3F))
794 fprintf (file, "\n\t");
795 putc(base64_key[c], file); /* signature */
796 }
797 /* Clean up... */
798 fprintf(file, " )");
799 if (n < 0)
800 fprintf(file, "\t; BAD BASE64");
801 fflush(file);
802 cp = cp1+dlen;
803 break;
804
805#ifdef ALLOW_T_UNSPEC
806 case T_UNSPEC:
807 {
808 int NumBytes = 8;
809 u_char *DataPtr;
810 int i;
811
812 if (dlen < NumBytes) NumBytes = dlen;
813 fprintf(file, "\tFirst %d bytes of hex data:",
814 NumBytes);
815 for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
816 fprintf(file, " %x", *DataPtr);
817 cp += dlen;
818 }
819 break;
820#endif /* ALLOW_T_UNSPEC */
821
822 default:
823 fprintf(file, "\t?%d?", type);
824 cp += dlen;
825 }
826#if 0
827 fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl));
828#else
829 putc('\n', file);
830#endif
831 if (cp - cp1 != dlen) {
832 fprintf(file, ";; packet size error (found %ld, dlen was %d)\n",
833 (long)(cp - cp1), dlen);
834 cp = NULL;
835 }
836 return (cp);
837}
838
839/*
840 * Names of RR classes and qclasses. Classes and qclasses are the same, except
841 * that C_ANY is a qclass but not a class. (You can ask for records of class
842 * C_ANY, but you can't have any records of that class in the database.)
843 */
844const struct res_sym __p_class_syms[] = {
845 {C_IN, "IN"},
846 {C_CHAOS, "CHAOS"},
847 {C_HS, "HS"},
848 {C_HS, "HESIOD"},
849 {C_ANY, "ANY"},
850 {C_IN, (char *)0}
851};
852
853/*
854 * Names of RR types and qtypes. Types and qtypes are the same, except
855 * that T_ANY is a qtype but not a type. (You can ask for records of type
856 * T_ANY, but you can't have any records of that type in the database.)
857 */
858const struct res_sym __p_type_syms[] = {
859 {T_A, "A", "address"},
860 {T_NS, "NS", "name server"},
861 {T_MD, "MD", "mail destination (deprecated)"},
862 {T_MF, "MF", "mail forwarder (deprecated)"},
863 {T_CNAME, "CNAME", "canonical name"},
864 {T_SOA, "SOA", "start of authority"},
865 {T_MB, "MB", "mailbox"},
866 {T_MG, "MG", "mail group member"},
867 {T_MR, "MR", "mail rename"},
868 {T_NULL, "NULL", "null"},
869 {T_WKS, "WKS", "well-known service (deprecated)"},
870 {T_PTR, "PTR", "domain name pointer"},
871 {T_HINFO, "HINFO", "host information"},
872 {T_MINFO, "MINFO", "mailbox information"},
873 {T_MX, "MX", "mail exchanger"},
874 {T_TXT, "TXT", "text"},
875 {T_RP, "RP", "responsible person"},
876 {T_AFSDB, "AFSDB", "DCE or AFS server"},
877 {T_X25, "X25", "X25 address"},
878 {T_ISDN, "ISDN", "ISDN address"},
879 {T_RT, "RT", "router"},
880 {T_NSAP, "NSAP", "nsap address"},
881 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},
882 {T_SIG, "SIG", "signature"},
883 {T_KEY, "KEY", "key"},
884 {T_PX, "PX", "mapping information"},
885 {T_GPOS, "GPOS", "geographical position (withdrawn)"},
886 {T_AAAA, "AAAA", "IPv6 address"},
887 {T_LOC, "LOC", "location"},
888 {T_NXT, "NXT", "next valid name (unimplemented)"},
889 {T_EID, "EID", "endpoint identifier (unimplemented)"},
890 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},
891 {T_SRV, "SRV", "server selection"},
892 {T_ATMA, "ATMA", "ATM address (unimplemented)"},
893 {T_IXFR, "IXFR", "incremental zone transfer"},
894 {T_AXFR, "AXFR", "zone transfer"},
895 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},
896 {T_MAILA, "MAILA", "mail agent (deprecated)"},
897 {T_UINFO, "UINFO", "user information (nonstandard)"},
898 {T_UID, "UID", "user ID (nonstandard)"},
899 {T_GID, "GID", "group ID (nonstandard)"},
900 {T_NAPTR, "NAPTR", "URN Naming Authority"},
901#ifdef ALLOW_T_UNSPEC
902 {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"},
903#endif /* ALLOW_T_UNSPEC */
904 {T_ANY, "ANY", "\"any\""},
905 {0, NULL, NULL}
906};
907
908int
909__sym_ston(const struct res_sym *syms, char *name, int *success)
910{
911 for (; syms->name != 0; syms++) {
912 if (strcasecmp (name, syms->name) == 0) {
913 if (success)
914 *success = 1;
915 return (syms->number);
916 }
917 }
918 if (success)
919 *success = 0;
920 return (syms->number); /* The default value. */
921}
922
923const char *
924__sym_ntos(const struct res_sym *syms, int number, int *success)
925{
926 static char unname[20];
927
928 for (; syms->name != 0; syms++) {
929 if (number == syms->number) {
930 if (success)
931 *success = 1;
932 return (syms->name);
933 }
934 }
935
936 snprintf(unname, sizeof unname, "%d", number);
937 if (success)
938 *success = 0;
939 return (unname);
940}
941
942
943const char *
944__sym_ntop(const struct res_sym *syms, int number, int *success)
945{
946 static char unname[20];
947
948 for (; syms->name != 0; syms++) {
949 if (number == syms->number) {
950 if (success)
951 *success = 1;
952 return (syms->humanname);
953 }
954 }
955 snprintf(unname, sizeof unname, "%d", number);
956 if (success)
957 *success = 0;
958 return (unname);
959}
960
961/*
962 * Return a string for the type
963 */
964const char *
965__p_type(int type)
966{
967 return (__sym_ntos (__p_type_syms, type, (int *)0));
968}
969
970/*
971 * Return a mnemonic for class
972 */
973const char *
974__p_class(int class)
975{
976 return (__sym_ntos (__p_class_syms, class, (int *)0));
977}
978
979/*
980 * Return a mnemonic for an option
981 */
982const char *
983__p_option(u_long option)
984{
985 static char nbuf[40];
986
987 switch (option) {
988 case RES_INIT: return "init";
989 case RES_DEBUG: return "debug";
990 case RES_AAONLY: return "aaonly(unimpl)";
991 case RES_USEVC: return "usevc";
992 case RES_PRIMARY: return "primry(unimpl)";
993 case RES_IGNTC: return "igntc";
994 case RES_RECURSE: return "recurs";
995 case RES_DEFNAMES: return "defnam";
996 case RES_STAYOPEN: return "styopn";
997 case RES_DNSRCH: return "dnsrch";
998 case RES_INSECURE1: return "insecure1";
999 case RES_INSECURE2: return "insecure2";
1000 case RES_USE_INET6: return "inet6";
1001 case RES_USE_EDNS0: return "edns0";
1002 default:
1003 snprintf(nbuf, sizeof nbuf, "?0x%lx?", (u_long)option);
1004 return (nbuf);
1005 }
1006}
1007
1008/*
1009 * Return a mnemonic for a time to live
1010 */
1011const char *
1012p_time(u_int32_t value)
1013{
1014 static char nbuf[40];
1015 char *ebuf;
1016 int secs, mins, hours, days;
1017 char *p;
1018 int tmp;
1019
1020 if (value == 0) {
1021 strlcpy(nbuf, "0 secs", sizeof nbuf);
1022 return (nbuf);
1023 }
1024
1025 secs = value % 60;
1026 value /= 60;
1027 mins = value % 60;
1028 value /= 60;
1029 hours = value % 24;
1030 value /= 24;
1031 days = value;
1032 value = 0;
1033
1034#define PLURALIZE(x) x, (x == 1) ? "" : "s"
1035 p = nbuf;
1036 ebuf = nbuf + sizeof(nbuf);
1037 if (days) {
1038 if ((tmp = snprintf(p, ebuf - p, "%d day%s",
1039 PLURALIZE(days))) >= ebuf - p || tmp < 0)
1040 goto full;
1041 p += tmp;
1042 }
1043 if (hours) {
1044 if (days)
1045 *p++ = ' ';
1046 if (p >= ebuf)
1047 goto full;
1048 if ((tmp = snprintf(p, ebuf - p, "%d hour%s",
1049 PLURALIZE(hours))) >= ebuf - p || tmp < 0)
1050 goto full;
1051 p += tmp;
1052 }
1053 if (mins) {
1054 if (days || hours)
1055 *p++ = ' ';
1056 if (p >= ebuf)
1057 goto full;
1058 if ((tmp = snprintf(p, ebuf - p, "%d min%s",
1059 PLURALIZE(mins))) >= ebuf - p || tmp < 0)
1060 goto full;
1061 p += tmp;
1062 }
1063 if (secs || ! (days || hours || mins)) {
1064 if (days || hours || mins)
1065 *p++ = ' ';
1066 if (p >= ebuf)
1067 goto full;
1068 if ((tmp = snprintf(p, ebuf - p, "%d sec%s",
1069 PLURALIZE(secs))) >= ebuf - p || tmp < 0)
1070 goto full;
1071 }
1072 return (nbuf);
1073full:
1074 p = nbuf + sizeof(nbuf) - 4;
1075 *p++ = '.';
1076 *p++ = '.';
1077 *p++ = '.';
1078 *p++ = '\0';
1079 return (nbuf);
1080}
1081
1082/*
1083 * routines to convert between on-the-wire RR format and zone file format.
1084 * Does not contain conversion to/from decimal degrees; divide or multiply
1085 * by 60*60*1000 for that.
1086 */
1087
1088static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
1089 1000000,10000000,100000000,1000000000};
1090
1091/* takes an XeY precision/size value, returns a string representation. */
1092static const char *
1093precsize_ntoa(u_int8_t prec)
1094{
1095 static char retbuf[sizeof "90000000.00"];
1096 unsigned long val;
1097 int mantissa, exponent;
1098
1099 mantissa = (int)((prec >> 4) & 0x0f) % 10;
1100 exponent = (int)((prec >> 0) & 0x0f) % 10;
1101
1102 val = mantissa * poweroften[exponent];
1103
1104 (void) snprintf(retbuf, sizeof retbuf, "%ld.%.2ld", val/100, val%100);
1105 return (retbuf);
1106}
1107
1108/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
1109static u_int8_t
1110precsize_aton(char **strptr)
1111{
1112 unsigned int mval = 0, cmval = 0;
1113 u_int8_t retval = 0;
1114 char *cp;
1115 int exponent;
1116 int mantissa;
1117
1118 cp = *strptr;
1119
1120 while (isdigit(*cp))
1121 mval = mval * 10 + (*cp++ - '0');
1122
1123 if (*cp == '.') { /* centimeters */
1124 cp++;
1125 if (isdigit(*cp)) {
1126 cmval = (*cp++ - '0') * 10;
1127 if (isdigit(*cp)) {
1128 cmval += (*cp++ - '0');
1129 }
1130 }
1131 }
1132 cmval = (mval * 100) + cmval;
1133
1134 for (exponent = 0; exponent < 9; exponent++)
1135 if (cmval < poweroften[exponent+1])
1136 break;
1137
1138 mantissa = cmval / poweroften[exponent];
1139 if (mantissa > 9)
1140 mantissa = 9;
1141
1142 retval = (mantissa << 4) | exponent;
1143
1144 *strptr = cp;
1145
1146 return (retval);
1147}
1148
1149/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
1150static u_int32_t
1151latlon2ul(char **latlonstrptr, int *which)
1152{
1153 char *cp;
1154 u_int32_t retval;
1155 int deg = 0, min = 0, secs = 0, secsfrac = 0;
1156
1157 cp = *latlonstrptr;
1158
1159 while (isdigit(*cp))
1160 deg = deg * 10 + (*cp++ - '0');
1161
1162 while (isspace(*cp))
1163 cp++;
1164
1165 if (!(isdigit(*cp)))
1166 goto fndhemi;
1167
1168 while (isdigit(*cp))
1169 min = min * 10 + (*cp++ - '0');
1170
1171 while (isspace(*cp))
1172 cp++;
1173
1174 if (!(isdigit(*cp)))
1175 goto fndhemi;
1176
1177 while (isdigit(*cp))
1178 secs = secs * 10 + (*cp++ - '0');
1179
1180 if (*cp == '.') { /* decimal seconds */
1181 cp++;
1182 if (isdigit(*cp)) {
1183 secsfrac = (*cp++ - '0') * 100;
1184 if (isdigit(*cp)) {
1185 secsfrac += (*cp++ - '0') * 10;
1186 if (isdigit(*cp)) {
1187 secsfrac += (*cp++ - '0');
1188 }
1189 }
1190 }
1191 }
1192
1193 while (!isspace(*cp)) /* if any trailing garbage */
1194 cp++;
1195
1196 while (isspace(*cp))
1197 cp++;
1198
1199 fndhemi:
1200 switch (*cp) {
1201 case 'N': case 'n':
1202 case 'E': case 'e':
1203 retval = ((unsigned)1<<31)
1204 + (((((deg * 60) + min) * 60) + secs) * 1000)
1205 + secsfrac;
1206 break;
1207 case 'S': case 's':
1208 case 'W': case 'w':
1209 retval = ((unsigned)1<<31)
1210 - (((((deg * 60) + min) * 60) + secs) * 1000)
1211 - secsfrac;
1212 break;
1213 default:
1214 retval = 0; /* invalid value -- indicates error */
1215 break;
1216 }
1217
1218 switch (*cp) {
1219 case 'N': case 'n':
1220 case 'S': case 's':
1221 *which = 1; /* latitude */
1222 break;
1223 case 'E': case 'e':
1224 case 'W': case 'w':
1225 *which = 2; /* longitude */
1226 break;
1227 default:
1228 *which = 0; /* error */
1229 break;
1230 }
1231
1232 cp++; /* skip the hemisphere */
1233
1234 while (!isspace(*cp)) /* if any trailing garbage */
1235 cp++;
1236
1237 while (isspace(*cp)) /* move to next field */
1238 cp++;
1239
1240 *latlonstrptr = cp;
1241
1242 return (retval);
1243}
1244
1245/* converts a zone file representation in a string to an RDATA on-the-wire
1246 * representation. */
1247int
1248loc_aton(const char *ascii, u_char *binary)
1249{
1250 const char *maxcp;
1251 u_char *bcp;
1252 char *cp;
1253
1254 u_int32_t latit = 0, longit = 0, alt = 0;
1255 u_int32_t lltemp1 = 0, lltemp2 = 0;
1256 int altmeters = 0, altfrac = 0, altsign = 1;
1257 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
1258 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
1259 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
1260 int which1 = 0, which2 = 0;
1261
1262 cp = (char *)ascii;
1263 maxcp = cp + strlen(ascii);
1264
1265 lltemp1 = latlon2ul(&cp, &which1);
1266
1267 lltemp2 = latlon2ul(&cp, &which2);
1268
1269 switch (which1 + which2) {
1270 case 3: /* 1 + 2, the only valid combination */
1271 if ((which1 == 1) && (which2 == 2)) { /* normal case */
1272 latit = lltemp1;
1273 longit = lltemp2;
1274 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
1275 longit = lltemp1;
1276 latit = lltemp2;
1277 } else { /* some kind of brokenness */
1278 return (0);
1279 }
1280 break;
1281 default: /* we didn't get one of each */
1282 return (0);
1283 }
1284
1285 /* altitude */
1286 if (*cp == '-') {
1287 altsign = -1;
1288 cp++;
1289 }
1290
1291 if (*cp == '+')
1292 cp++;
1293
1294 while (isdigit(*cp))
1295 altmeters = altmeters * 10 + (*cp++ - '0');
1296
1297 if (*cp == '.') { /* decimal meters */
1298 cp++;
1299 if (isdigit(*cp)) {
1300 altfrac = (*cp++ - '0') * 10;
1301 if (isdigit(*cp)) {
1302 altfrac += (*cp++ - '0');
1303 }
1304 }
1305 }
1306
1307 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
1308
1309 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1310 cp++;
1311
1312 while (isspace(*cp) && (cp < maxcp))
1313 cp++;
1314
1315 if (cp >= maxcp)
1316 goto defaults;
1317
1318 siz = precsize_aton(&cp);
1319
1320 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1321 cp++;
1322
1323 while (isspace(*cp) && (cp < maxcp))
1324 cp++;
1325
1326 if (cp >= maxcp)
1327 goto defaults;
1328
1329 hp = precsize_aton(&cp);
1330
1331 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1332 cp++;
1333
1334 while (isspace(*cp) && (cp < maxcp))
1335 cp++;
1336
1337 if (cp >= maxcp)
1338 goto defaults;
1339
1340 vp = precsize_aton(&cp);
1341
1342 defaults:
1343
1344 bcp = binary;
1345 *bcp++ = (u_int8_t) 0; /* version byte */
1346 *bcp++ = siz;
1347 *bcp++ = hp;
1348 *bcp++ = vp;
1349 PUTLONG(latit,bcp);
1350 PUTLONG(longit,bcp);
1351 PUTLONG(alt,bcp);
1352
1353 return (16); /* size of RR in octets */
1354}
1355
1356const char *
1357loc_ntoa(const u_char *binary, char *ascii)
1358{
1359 return loc_ntoal(binary, ascii, 255);
1360}
1361
1362/* takes an on-the-wire LOC RR and formats it in a human readable format. */
1363static const char *
1364loc_ntoal(const u_char *binary, char *ascii, int ascii_len)
1365{
1366 static char *error = "?";
1367 const u_char *cp = binary;
1368
1369 int latdeg, latmin, latsec, latsecfrac;
1370 int longdeg, longmin, longsec, longsecfrac;
1371 char northsouth, eastwest;
1372 int altmeters, altfrac, altsign;
1373
1374 const int referencealt = 100000 * 100;
1375
1376 int32_t latval, longval, altval;
1377 u_int32_t templ;
1378 u_int8_t sizeval, hpval, vpval, versionval;
1379
1380 char *sizestr, *hpstr, *vpstr;
1381
1382 versionval = *cp++;
1383
1384 if (versionval) {
1385 snprintf(ascii, ascii_len, "; error: unknown LOC RR version");
1386 return (ascii);
1387 }
1388
1389 sizeval = *cp++;
1390
1391 hpval = *cp++;
1392 vpval = *cp++;
1393
1394 GETLONG(templ, cp);
1395 latval = (templ - ((unsigned)1<<31));
1396
1397 GETLONG(templ, cp);
1398 longval = (templ - ((unsigned)1<<31));
1399
1400 GETLONG(templ, cp);
1401 if (templ < referencealt) { /* below WGS 84 spheroid */
1402 altval = referencealt - templ;
1403 altsign = -1;
1404 } else {
1405 altval = templ - referencealt;
1406 altsign = 1;
1407 }
1408
1409 if (latval < 0) {
1410 northsouth = 'S';
1411 latval = -latval;
1412 } else
1413 northsouth = 'N';
1414
1415 latsecfrac = latval % 1000;
1416 latval = latval / 1000;
1417 latsec = latval % 60;
1418 latval = latval / 60;
1419 latmin = latval % 60;
1420 latval = latval / 60;
1421 latdeg = latval;
1422
1423 if (longval < 0) {
1424 eastwest = 'W';
1425 longval = -longval;
1426 } else
1427 eastwest = 'E';
1428
1429 longsecfrac = longval % 1000;
1430 longval = longval / 1000;
1431 longsec = longval % 60;
1432 longval = longval / 60;
1433 longmin = longval % 60;
1434 longval = longval / 60;
1435 longdeg = longval;
1436
1437 altfrac = altval % 100;
1438 altmeters = (altval / 100) * altsign;
1439
1440 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
1441 sizestr = error;
1442 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
1443 hpstr = error;
1444 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
1445 vpstr = error;
1446
1447 snprintf(ascii, ascii_len,
1448 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1449 latdeg, latmin, latsec, latsecfrac, northsouth,
1450 longdeg, longmin, longsec, longsecfrac, eastwest,
1451 altmeters, altfrac, sizestr, hpstr, vpstr);
1452
1453 if (sizestr != error)
1454 free(sizestr);
1455 if (hpstr != error)
1456 free(hpstr);
1457 if (vpstr != error)
1458 free(vpstr);
1459
1460 return (ascii);
1461}
1462
1463
1464/* Return the number of DNS hierarchy levels in the name. */
1465int
1466__dn_count_labels(char *name)
1467{
1468 int i, len, count;
1469
1470 len = strlen(name);
1471
1472 for(i = 0, count = 0; i < len; i++) {
1473 if (name[i] == '.')
1474 count++;
1475 }
1476
1477 /* don't count initial wildcard */
1478 if (name[0] == '*')
1479 if (count)
1480 count--;
1481
1482 /* don't count the null label for root. */
1483 /* if terminating '.' not found, must adjust */
1484 /* count to include last label */
1485 if (len > 0 && name[len-1] != '.')
1486 count++;
1487 return (count);
1488}
1489
1490
1491/*
1492 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1493 * SIG records are required to be printed like this, by the Secure DNS RFC.
1494 */
1495char *
1496__p_secstodate (long unsigned int secs)
1497{
1498 static char output[15]; /* YYYYMMDDHHMMSS and null */
1499 time_t clock = secs;
1500 struct tm *time;
1501
1502 time = gmtime(&clock);
1503 time->tm_year += 1900;
1504 time->tm_mon += 1;
1505 snprintf(output, sizeof output, "%04d%02d%02d%02d%02d%02d",
1506 time->tm_year, time->tm_mon, time->tm_mday,
1507 time->tm_hour, time->tm_min, time->tm_sec);
1508 return (output);
1509}