Use pkgsrc packages from a custom location.
[dragonfly.git] / lib / libc / net / res_debug.c
blob26179508db33552a54aa8b5f6dbdbe5d773ec8dd
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)res_debug.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/lib/libc/net/res_debug.c,v 1.18.2.1 2001/06/15 22:08:28 ume Exp $
31 * $DragonFly: src/lib/libc/net/res_debug.c,v 1.4 2005/11/13 02:04:47 swildner Exp $
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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.
55 * Portions Copyright (c) 1995 by International Business Machines, Inc.
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.
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.
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.
80 * Portions Copyright (c) 1996 by Internet Software Consortium.
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.
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.
96 #include <sys/types.h>
97 #include <sys/param.h>
98 #include <sys/socket.h>
100 #include <netinet/in.h>
101 #include <arpa/inet.h>
102 #include <arpa/nameser.h>
104 #include <ctype.h>
105 #include <errno.h>
106 #include <math.h>
107 #include <netdb.h>
108 #include <resolv.h>
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <string.h>
112 #include <time.h>
114 #define SPRINTF(x) sprintf x
116 extern const char *_res_opcodes[];
117 extern const char *_res_resultcodes[];
118 extern const char *_res_sectioncodes[];
121 * Print the current options.
123 void
124 fp_resstat(struct __res_state *statp, FILE *file)
126 u_long mask;
128 fprintf(file, ";; res options:");
129 if (!statp)
130 statp = &_res;
131 for (mask = 1; mask != 0; mask <<= 1)
132 if (statp->options & mask)
133 fprintf(file, " %s", p_option(mask));
134 putc('\n', file);
137 static void
138 do_section(ns_msg *handle, ns_sect section, int pflag, FILE *file)
140 int n, sflag, rrnum;
141 char buf[2048]; /* XXX need to malloc */
142 ns_opcode opcode;
143 ns_rr rr;
146 * Print answer records.
148 sflag = (_res.pfcode & pflag);
149 if (_res.pfcode && !sflag)
150 return;
152 opcode = ns_msg_getflag(*handle, ns_f_opcode);
153 rrnum = 0;
154 for (;;) {
155 if (ns_parserr(handle, section, rrnum, &rr)) {
156 if (errno != ENODEV)
157 fprintf(file, ";; ns_parserr: %s\n",
158 strerror(errno));
159 else if (rrnum > 0 && sflag != 0 &&
160 (_res.pfcode & RES_PRF_HEAD1))
161 putc('\n', file);
162 return;
164 if (rrnum == 0 && sflag != 0 && (_res.pfcode & RES_PRF_HEAD1))
165 fprintf(file, ";; %s SECTION:\n",
166 p_section(section, opcode));
167 if (section == ns_s_qd)
168 fprintf(file, ";;\t%s, type = %s, class = %s\n",
169 ns_rr_name(rr),
170 p_type(ns_rr_type(rr)),
171 p_class(ns_rr_class(rr)));
172 else {
173 n = ns_sprintrr(handle, &rr, NULL, NULL,
174 buf, sizeof buf);
175 if (n < 0) {
176 fprintf(file, ";; ns_sprintrr: %s\n",
177 strerror(errno));
178 return;
180 fputs(buf, file);
181 fputc('\n', file);
183 rrnum++;
187 void
188 p_query(const u_char *msg)
190 fp_query(msg, stdout);
193 void
194 fp_query(const u_char *msg, FILE *file)
196 fp_nquery(msg, PACKETSZ, file);
200 * Print the contents of a query.
201 * This is intended to be primarily a debugging routine.
203 void
204 fp_nquery(const u_char *msg, int len, FILE *file)
206 ns_msg handle;
207 int qdcount, ancount, nscount, arcount;
208 u_int opcode, rcode, id;
210 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
211 return;
213 if (ns_initparse(msg, len, &handle) < 0) {
214 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
215 return;
217 opcode = ns_msg_getflag(handle, ns_f_opcode);
218 rcode = ns_msg_getflag(handle, ns_f_rcode);
219 id = ns_msg_id(handle);
220 qdcount = ns_msg_count(handle, ns_s_qd);
221 ancount = ns_msg_count(handle, ns_s_an);
222 nscount = ns_msg_count(handle, ns_s_ns);
223 arcount = ns_msg_count(handle, ns_s_ar);
226 * Print header fields.
228 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || rcode)
229 fprintf(file,
230 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
231 _res_opcodes[opcode], _res_resultcodes[rcode], id);
232 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX))
233 putc(';', file);
234 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {
235 fprintf(file, "; flags:");
236 if (ns_msg_getflag(handle, ns_f_qr))
237 fprintf(file, " qr");
238 if (ns_msg_getflag(handle, ns_f_aa))
239 fprintf(file, " aa");
240 if (ns_msg_getflag(handle, ns_f_tc))
241 fprintf(file, " tc");
242 if (ns_msg_getflag(handle, ns_f_rd))
243 fprintf(file, " rd");
244 if (ns_msg_getflag(handle, ns_f_ra))
245 fprintf(file, " ra");
246 if (ns_msg_getflag(handle, ns_f_z))
247 fprintf(file, " ??");
248 if (ns_msg_getflag(handle, ns_f_ad))
249 fprintf(file, " ad");
250 if (ns_msg_getflag(handle, ns_f_cd))
251 fprintf(file, " cd");
253 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) {
254 fprintf(file, "; %s: %d",
255 p_section(ns_s_qd, opcode), qdcount);
256 fprintf(file, ", %s: %d",
257 p_section(ns_s_an, opcode), ancount);
258 fprintf(file, ", %s: %d",
259 p_section(ns_s_ns, opcode), nscount);
260 fprintf(file, ", %s: %d",
261 p_section(ns_s_ar, opcode), arcount);
263 if ((!_res.pfcode) || (_res.pfcode &
264 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
265 putc('\n',file);
268 * Print the various sections.
270 do_section(&handle, ns_s_qd, RES_PRF_QUES, file);
271 do_section(&handle, ns_s_an, RES_PRF_ANS, file);
272 do_section(&handle, ns_s_ns, RES_PRF_AUTH, file);
273 do_section(&handle, ns_s_ar, RES_PRF_ADD, file);
274 if (qdcount == 0 && ancount == 0 &&
275 nscount == 0 && arcount == 0)
276 putc('\n', file);
279 const u_char *
280 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file)
282 char name[MAXDNAME];
283 int n;
285 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
286 return (NULL);
287 if (name[0] == '\0')
288 putc('.', file);
289 else
290 fputs(name, file);
291 return (cp + n);
294 const u_char *
295 p_cdname(const u_char *cp, const u_char *msg, FILE *file)
297 return (p_cdnname(cp, msg, PACKETSZ, file));
300 /* Return a fully-qualified domain name from a compressed name (with
301 length supplied). */
303 const u_char *
304 p_fqnname(const u_char *cp, const u_char *msg, int msglen, char *name,
305 int namelen)
307 int n, newlen;
309 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
310 return (NULL);
311 newlen = strlen(name);
312 if (newlen == 0 || name[newlen - 1] != '.') {
313 if (newlen + 1 >= namelen) /* Lack space for final dot */
314 return (NULL);
315 else
316 strcpy(name + newlen, ".");
318 return (cp + n);
321 /* XXX: the rest of these functions need to become length-limited, too. */
323 const u_char *
324 p_fqname(const u_char *cp, const u_char *msg, FILE *file)
326 char name[MAXDNAME];
327 const u_char *n;
329 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
330 if (n == NULL)
331 return (NULL);
332 fputs(name, file);
333 return (n);
337 * Names of RR classes and qclasses. Classes and qclasses are the same, except
338 * that C_ANY is a qclass but not a class. (You can ask for records of class
339 * C_ANY, but you can't have any records of that class in the database.)
341 const struct res_sym __p_class_syms[] = {
342 {C_IN, "IN"},
343 {C_CHAOS, "CHAOS"},
344 {C_HS, "HS"},
345 {C_HS, "HESIOD"},
346 {C_ANY, "ANY"},
347 {C_NONE, "NONE"},
348 {C_IN, (char *)0}
352 * Names of message sections.
354 const struct res_sym __p_default_section_syms[] = {
355 {ns_s_qd, "QUERY"},
356 {ns_s_an, "ANSWER"},
357 {ns_s_ns, "AUTHORITY"},
358 {ns_s_ar, "ADDITIONAL"},
359 {0, (char *)0}
362 const struct res_sym __p_update_section_syms[] = {
363 {S_ZONE, "ZONE"},
364 {S_PREREQ, "PREREQUISITE"},
365 {S_UPDATE, "UPDATE"},
366 {S_ADDT, "ADDITIONAL"},
367 {0, (char *)0}
371 * Names of RR types and qtypes. Types and qtypes are the same, except
372 * that T_ANY is a qtype but not a type. (You can ask for records of type
373 * T_ANY, but you can't have any records of that type in the database.)
375 const struct res_sym __p_type_syms[] = {
376 {T_A, "A", "address"},
377 {T_NS, "NS", "name server"},
378 {T_MD, "MD", "mail destination (deprecated)"},
379 {T_MF, "MF", "mail forwarder (deprecated)"},
380 {T_CNAME, "CNAME", "canonical name"},
381 {T_SOA, "SOA", "start of authority"},
382 {T_MB, "MB", "mailbox"},
383 {T_MG, "MG", "mail group member"},
384 {T_MR, "MR", "mail rename"},
385 {T_NULL, "NULL", "null"},
386 {T_WKS, "WKS", "well-known service (deprecated)"},
387 {T_PTR, "PTR", "domain name pointer"},
388 {T_HINFO, "HINFO", "host information"},
389 {T_MINFO, "MINFO", "mailbox information"},
390 {T_MX, "MX", "mail exchanger"},
391 {T_TXT, "TXT", "text"},
392 {T_RP, "RP", "responsible person"},
393 {T_AFSDB, "AFSDB", "DCE or AFS server"},
394 {T_X25, "X25", "X25 address"},
395 {T_ISDN, "ISDN", "ISDN address"},
396 {T_RT, "RT", "router"},
397 {T_NSAP, "NSAP", "nsap address"},
398 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},
399 {T_SIG, "SIG", "signature"},
400 {T_KEY, "KEY", "key"},
401 {T_PX, "PX", "mapping information"},
402 {T_GPOS, "GPOS", "geographical position (withdrawn)"},
403 {T_AAAA, "AAAA", "IPv6 address"},
404 {T_LOC, "LOC", "location"},
405 {T_NXT, "NXT", "next valid name (unimplemented)"},
406 {T_EID, "EID", "endpoint identifier (unimplemented)"},
407 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},
408 {T_SRV, "SRV", "server selection"},
409 {T_ATMA, "ATMA", "ATM address (unimplemented)"},
410 {T_IXFR, "IXFR", "incremental zone transfer"},
411 {T_AXFR, "AXFR", "zone transfer"},
412 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},
413 {T_MAILA, "MAILA", "mail agent (deprecated)"},
414 {T_NAPTR, "NAPTR", "URN Naming Authority"},
415 {T_ANY, "ANY", "\"any\""},
416 {0, NULL, NULL}
420 sym_ston(const struct res_sym *syms, const char *name, int *success)
422 for (; syms->name != 0; syms++) {
423 if (strcasecmp (name, syms->name) == 0) {
424 if (success)
425 *success = 1;
426 return (syms->number);
429 if (success)
430 *success = 0;
431 return (syms->number); /* The default value. */
434 const char *
435 sym_ntos(const struct res_sym *syms, int number, int *success)
437 static char unname[20];
439 for (; syms->name != 0; syms++) {
440 if (number == syms->number) {
441 if (success)
442 *success = 1;
443 return (syms->name);
447 sprintf(unname, "%d", number);
448 if (success)
449 *success = 0;
450 return (unname);
453 const char *
454 sym_ntop(const struct res_sym *syms, int number, int *success)
456 static char unname[20];
458 for (; syms->name != 0; syms++) {
459 if (number == syms->number) {
460 if (success)
461 *success = 1;
462 return (syms->humanname);
465 sprintf(unname, "%d", number);
466 if (success)
467 *success = 0;
468 return (unname);
472 * Return a string for the type.
474 const char *
475 p_type(int type)
477 return (sym_ntos(__p_type_syms, type, (int *)0));
481 * Return a string for the type.
483 const char *
484 p_section(int section, int opcode)
486 const struct res_sym *symbols;
488 switch (opcode) {
489 case ns_o_update:
490 symbols = __p_update_section_syms;
491 break;
492 default:
493 symbols = __p_default_section_syms;
494 break;
496 return (sym_ntos(symbols, section, (int *)0));
500 * Return a mnemonic for class.
502 const char *
503 p_class(int class)
505 return (sym_ntos(__p_class_syms, class, (int *)0));
509 * Return a mnemonic for an option
511 const char *
512 p_option(u_long option) {
513 static char nbuf[40];
515 switch (option) {
516 case RES_INIT: return "init";
517 case RES_DEBUG: return "debug";
518 case RES_AAONLY: return "aaonly(unimpl)";
519 case RES_USEVC: return "usevc";
520 case RES_PRIMARY: return "primry(unimpl)";
521 case RES_IGNTC: return "igntc";
522 case RES_RECURSE: return "recurs";
523 case RES_DEFNAMES: return "defnam";
524 case RES_STAYOPEN: return "styopn";
525 case RES_DNSRCH: return "dnsrch";
526 case RES_INSECURE1: return "insecure1";
527 case RES_INSECURE2: return "insecure2";
528 case RES_NOALIASES: return "noaliases";
529 case RES_USE_INET6: return "inet6";
530 case RES_NOTLDQUERY: return "no-tld-query";
531 case RES_USE_EDNS0: return "edns0";
532 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
533 return (nbuf);
538 * Return a mnemonic for a time to live.
540 const char *
541 p_time(u_int32_t value) {
542 static char nbuf[40];
544 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
545 sprintf(nbuf, "%u", value);
546 return (nbuf);
551 * routines to convert between on-the-wire RR format and zone file format.
552 * Does not contain conversion to/from decimal degrees; divide or multiply
553 * by 60*60*1000 for that.
556 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
557 1000000,10000000,100000000,1000000000};
559 /* takes an XeY precision/size value, returns a string representation. */
560 static const char *
561 precsize_ntoa(u_int8_t prec)
563 static char retbuf[sizeof "90000000.00"];
564 unsigned long val;
565 int mantissa, exponent;
567 mantissa = (int)((prec >> 4) & 0x0f) % 10;
568 exponent = (int)((prec >> 0) & 0x0f) % 10;
570 val = mantissa * poweroften[exponent];
572 sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
573 return (retbuf);
576 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
577 static u_int8_t
578 precsize_aton(char **strptr)
580 unsigned int mval = 0, cmval = 0;
581 u_int8_t retval = 0;
582 char *cp;
583 int exponent;
584 int mantissa;
586 cp = *strptr;
588 while (isdigit((unsigned char)*cp))
589 mval = mval * 10 + (*cp++ - '0');
591 if (*cp == '.') { /* centimeters */
592 cp++;
593 if (isdigit((unsigned char)*cp)) {
594 cmval = (*cp++ - '0') * 10;
595 if (isdigit((unsigned char)*cp)) {
596 cmval += (*cp++ - '0');
600 cmval = (mval * 100) + cmval;
602 for (exponent = 0; exponent < 9; exponent++)
603 if (cmval < poweroften[exponent+1])
604 break;
606 mantissa = cmval / poweroften[exponent];
607 if (mantissa > 9)
608 mantissa = 9;
610 retval = (mantissa << 4) | exponent;
612 *strptr = cp;
614 return (retval);
617 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
618 static u_int32_t
619 latlon2ul(char **latlonstrptr, int *which)
621 char *cp;
622 u_int32_t retval;
623 int deg = 0, min = 0, secs = 0, secsfrac = 0;
625 cp = *latlonstrptr;
627 while (isdigit((unsigned char)*cp))
628 deg = deg * 10 + (*cp++ - '0');
630 while (isspace((unsigned char)*cp))
631 cp++;
633 if (!(isdigit((unsigned char)*cp)))
634 goto fndhemi;
636 while (isdigit((unsigned char)*cp))
637 min = min * 10 + (*cp++ - '0');
639 while (isspace((unsigned char)*cp))
640 cp++;
642 if (!(isdigit((unsigned char)*cp)))
643 goto fndhemi;
645 while (isdigit((unsigned char)*cp))
646 secs = secs * 10 + (*cp++ - '0');
648 if (*cp == '.') { /* decimal seconds */
649 cp++;
650 if (isdigit((unsigned char)*cp)) {
651 secsfrac = (*cp++ - '0') * 100;
652 if (isdigit((unsigned char)*cp)) {
653 secsfrac += (*cp++ - '0') * 10;
654 if (isdigit((unsigned char)*cp)) {
655 secsfrac += (*cp++ - '0');
661 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
662 cp++;
664 while (isspace((unsigned char)*cp))
665 cp++;
667 fndhemi:
668 switch (*cp) {
669 case 'N': case 'n':
670 case 'E': case 'e':
671 retval = ((unsigned)1<<31)
672 + (((((deg * 60) + min) * 60) + secs) * 1000)
673 + secsfrac;
674 break;
675 case 'S': case 's':
676 case 'W': case 'w':
677 retval = ((unsigned)1<<31)
678 - (((((deg * 60) + min) * 60) + secs) * 1000)
679 - secsfrac;
680 break;
681 default:
682 retval = 0; /* invalid value -- indicates error */
683 break;
686 switch (*cp) {
687 case 'N': case 'n':
688 case 'S': case 's':
689 *which = 1; /* latitude */
690 break;
691 case 'E': case 'e':
692 case 'W': case 'w':
693 *which = 2; /* longitude */
694 break;
695 default:
696 *which = 0; /* error */
697 break;
700 cp++; /* skip the hemisphere */
702 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
703 cp++;
705 while (isspace((unsigned char)*cp)) /* move to next field */
706 cp++;
708 *latlonstrptr = cp;
710 return (retval);
713 /* converts a zone file representation in a string to an RDATA on-the-wire
714 * representation. */
716 loc_aton(const char *ascii, u_char *binary)
718 const char *cp, *maxcp;
719 u_char *bcp;
721 u_int32_t latit = 0, longit = 0, alt = 0;
722 u_int32_t lltemp1 = 0, lltemp2 = 0;
723 int altmeters = 0, altfrac = 0, altsign = 1;
724 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
725 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
726 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
727 int which1 = 0, which2 = 0;
729 cp = ascii;
730 maxcp = cp + strlen(ascii);
732 lltemp1 = latlon2ul(&cp, &which1);
734 lltemp2 = latlon2ul(&cp, &which2);
736 switch (which1 + which2) {
737 case 3: /* 1 + 2, the only valid combination */
738 if ((which1 == 1) && (which2 == 2)) { /* normal case */
739 latit = lltemp1;
740 longit = lltemp2;
741 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
742 longit = lltemp1;
743 latit = lltemp2;
744 } else { /* some kind of brokenness */
745 return (0);
747 break;
748 default: /* we didn't get one of each */
749 return (0);
752 /* altitude */
753 if (*cp == '-') {
754 altsign = -1;
755 cp++;
758 if (*cp == '+')
759 cp++;
761 while (isdigit((unsigned char)*cp))
762 altmeters = altmeters * 10 + (*cp++ - '0');
764 if (*cp == '.') { /* decimal meters */
765 cp++;
766 if (isdigit((unsigned char)*cp)) {
767 altfrac = (*cp++ - '0') * 10;
768 if (isdigit((unsigned char)*cp)) {
769 altfrac += (*cp++ - '0');
774 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
776 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
777 cp++;
779 while (isspace((unsigned char)*cp) && (cp < maxcp))
780 cp++;
782 if (cp >= maxcp)
783 goto defaults;
785 siz = precsize_aton(&cp);
787 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
788 cp++;
790 while (isspace((unsigned char)*cp) && (cp < maxcp))
791 cp++;
793 if (cp >= maxcp)
794 goto defaults;
796 hp = precsize_aton(&cp);
798 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
799 cp++;
801 while (isspace((unsigned char)*cp) && (cp < maxcp))
802 cp++;
804 if (cp >= maxcp)
805 goto defaults;
807 vp = precsize_aton(&cp);
809 defaults:
811 bcp = binary;
812 *bcp++ = (u_int8_t) 0; /* version byte */
813 *bcp++ = siz;
814 *bcp++ = hp;
815 *bcp++ = vp;
816 PUTLONG(latit,bcp);
817 PUTLONG(longit,bcp);
818 PUTLONG(alt,bcp);
820 return (16); /* size of RR in octets */
823 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
824 const char *
825 loc_ntoa(const u_char *binary, char *ascii)
827 static char *error = "?";
828 const u_char *cp = binary;
830 int latdeg, latmin, latsec, latsecfrac;
831 int longdeg, longmin, longsec, longsecfrac;
832 char northsouth, eastwest;
833 int altmeters, altfrac, altsign;
835 const u_int32_t referencealt = 100000 * 100;
837 int32_t latval, longval, altval;
838 u_int32_t templ;
839 u_int8_t sizeval, hpval, vpval, versionval;
841 char *sizestr, *hpstr, *vpstr;
843 versionval = *cp++;
845 if (versionval) {
846 sprintf(ascii, "; error: unknown LOC RR version");
847 return (ascii);
850 sizeval = *cp++;
852 hpval = *cp++;
853 vpval = *cp++;
855 GETLONG(templ, cp);
856 latval = (templ - ((unsigned)1<<31));
858 GETLONG(templ, cp);
859 longval = (templ - ((unsigned)1<<31));
861 GETLONG(templ, cp);
862 if (templ < referencealt) { /* below WGS 84 spheroid */
863 altval = referencealt - templ;
864 altsign = -1;
865 } else {
866 altval = templ - referencealt;
867 altsign = 1;
870 if (latval < 0) {
871 northsouth = 'S';
872 latval = -latval;
873 } else
874 northsouth = 'N';
876 latsecfrac = latval % 1000;
877 latval = latval / 1000;
878 latsec = latval % 60;
879 latval = latval / 60;
880 latmin = latval % 60;
881 latval = latval / 60;
882 latdeg = latval;
884 if (longval < 0) {
885 eastwest = 'W';
886 longval = -longval;
887 } else
888 eastwest = 'E';
890 longsecfrac = longval % 1000;
891 longval = longval / 1000;
892 longsec = longval % 60;
893 longval = longval / 60;
894 longmin = longval % 60;
895 longval = longval / 60;
896 longdeg = longval;
898 altfrac = altval % 100;
899 altmeters = (altval / 100) * altsign;
901 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
902 sizestr = error;
903 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
904 hpstr = error;
905 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
906 vpstr = error;
908 sprintf(ascii,
909 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
910 latdeg, latmin, latsec, latsecfrac, northsouth,
911 longdeg, longmin, longsec, longsecfrac, eastwest,
912 altmeters, altfrac, sizestr, hpstr, vpstr);
914 if (sizestr != error)
915 free(sizestr);
916 if (hpstr != error)
917 free(hpstr);
918 if (vpstr != error)
919 free(vpstr);
921 return (ascii);
925 /* Return the number of DNS hierarchy levels in the name. */
927 dn_count_labels(const char *name)
929 int i, len, count;
931 len = strlen(name);
932 for (i = 0, count = 0; i < len; i++) {
933 /* XXX need to check for \. or use named's nlabels(). */
934 if (name[i] == '.')
935 count++;
938 /* don't count initial wildcard */
939 if (name[0] == '*')
940 if (count)
941 count--;
943 /* don't count the null label for root. */
944 /* if terminating '.' not found, must adjust */
945 /* count to include last label */
946 if (len > 0 && name[len-1] != '.')
947 count++;
948 return (count);
953 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
954 * SIG records are required to be printed like this, by the Secure DNS RFC.
956 char *
957 p_secstodate (u_long secs)
959 static char output[15]; /* YYYYMMDDHHMMSS and null */
960 time_t clock = secs;
961 struct tm *time;
963 time = gmtime(&clock);
964 time->tm_year += 1900;
965 time->tm_mon += 1;
966 sprintf(output, "%04d%02d%02d%02d%02d%02d",
967 time->tm_year, time->tm_mon, time->tm_mday,
968 time->tm_hour, time->tm_min, time->tm_sec);
969 return (output);
973 * Weak aliases for applications that use certain private entry points,
974 * and fail to include <resolv.h>.
976 #undef fp_resstat
977 __weak_reference(__fp_resstat, fp_resstat);
978 #undef p_query
979 __weak_reference(__p_query, p_query);
980 #undef p_fqnname
981 __weak_reference(__p_fqnname, p_fqnname);
982 #undef sym_ston
983 __weak_reference(__sym_ston, sym_ston);
984 #undef sym_ntos
985 __weak_reference(__sym_ntos, sym_ntos);
986 #undef sym_ntop
987 __weak_reference(__sym_ntop, sym_ntop);
988 #undef dn_count_labels
989 __weak_reference(__dn_count_labels, dn_count_labels);
990 #undef p_secstodate
991 __weak_reference(__p_secstodate, p_secstodate);