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