3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. 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
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * Portions Copyright (c) 1995 by International Business Machines, Inc.
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
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.
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.
76 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
78 * Permission to use, copy, modify, and distribute this software for any
79 * purpose with or without fee is hereby granted, provided that the above
80 * copyright notice and this permission notice appear in all copies.
82 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
83 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
84 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
85 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
86 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
87 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
88 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
92 #include <sys/types.h>
93 #include <sys/param.h>
94 #include <sys/socket.h>
96 #include <netinet/in.h>
97 #include <arpa/inet.h>
98 #include <arpa/nameser.h>
104 #include <resolv/resolv-internal.h>
111 # define SPRINTF(x) strlen(sprintf/**/x)
113 # define SPRINTF(x) sprintf x
116 extern const char *_res_sectioncodes
[] attribute_hidden
;
118 static const char *p_section(int section
, int opcode
);
121 * Print the current options.
124 fp_resstat(const res_state statp
, FILE *file
) {
127 fprintf(file
, ";; res options:");
128 for (mask
= 1; mask
!= 0; mask
<<= 1)
129 if (statp
->options
& mask
)
130 fprintf(file
, " %s", p_option(mask
));
135 do_section(const res_state statp
,
136 ns_msg
*handle
, ns_sect section
,
137 int pflag
, FILE *file
)
140 static int buflen
= 2048;
146 * Print answer records.
148 sflag
= (statp
->pfcode
& pflag
);
149 if (statp
->pfcode
&& !sflag
)
152 buf
= malloc(buflen
);
154 fprintf(file
, ";; memory allocation failure\n");
158 opcode
= (ns_opcode
) ns_msg_getflag(*handle
, ns_f_opcode
);
161 if (ns_parserr(handle
, section
, rrnum
, &rr
)) {
163 fprintf(file
, ";; ns_parserr: %s\n",
165 else if (rrnum
> 0 && sflag
!= 0 &&
166 (statp
->pfcode
& RES_PRF_HEAD1
))
170 if (rrnum
== 0 && sflag
!= 0 && (statp
->pfcode
& RES_PRF_HEAD1
))
171 fprintf(file
, ";; %s SECTION:\n",
172 p_section(section
, opcode
));
173 if (section
== ns_s_qd
)
174 fprintf(file
, ";;\t%s, type = %s, class = %s\n",
176 p_type(ns_rr_type(rr
)),
177 p_class(ns_rr_class(rr
)));
179 n
= ns_sprintrr(handle
, &rr
, NULL
, NULL
,
182 if (errno
== ENOSPC
) {
186 buf
= malloc(buflen
+= 1024);
189 ";; memory allocation failure\n");
194 fprintf(file
, ";; ns_sprintrr: %s\n",
208 * Print the contents of a query.
209 * This is intended to be primarily a debugging routine.
212 res_pquery(const res_state statp
, const u_char
*msg
, int len
, FILE *file
) {
214 int qdcount
, ancount
, nscount
, arcount
;
215 u_int opcode
, rcode
, id
;
217 if (ns_initparse(msg
, len
, &handle
) < 0) {
218 fprintf(file
, ";; ns_initparse: %s\n", strerror(errno
));
221 opcode
= ns_msg_getflag(handle
, ns_f_opcode
);
222 rcode
= ns_msg_getflag(handle
, ns_f_rcode
);
223 id
= ns_msg_id(handle
);
224 qdcount
= ns_msg_count(handle
, ns_s_qd
);
225 ancount
= ns_msg_count(handle
, ns_s_an
);
226 nscount
= ns_msg_count(handle
, ns_s_ns
);
227 arcount
= ns_msg_count(handle
, ns_s_ar
);
230 * Print header fields.
232 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEADX
) || rcode
)
234 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
235 _res_opcodes
[opcode
], p_rcode(rcode
), id
);
236 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEADX
))
238 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEAD2
)) {
239 fprintf(file
, "; flags:");
240 if (ns_msg_getflag(handle
, ns_f_qr
))
241 fprintf(file
, " qr");
242 if (ns_msg_getflag(handle
, ns_f_aa
))
243 fprintf(file
, " aa");
244 if (ns_msg_getflag(handle
, ns_f_tc
))
245 fprintf(file
, " tc");
246 if (ns_msg_getflag(handle
, ns_f_rd
))
247 fprintf(file
, " rd");
248 if (ns_msg_getflag(handle
, ns_f_ra
))
249 fprintf(file
, " ra");
250 if (ns_msg_getflag(handle
, ns_f_z
))
251 fprintf(file
, " ??");
252 if (ns_msg_getflag(handle
, ns_f_ad
))
253 fprintf(file
, " ad");
254 if (ns_msg_getflag(handle
, ns_f_cd
))
255 fprintf(file
, " cd");
257 if ((!statp
->pfcode
) || (statp
->pfcode
& RES_PRF_HEAD1
)) {
258 fprintf(file
, "; %s: %d",
259 p_section(ns_s_qd
, opcode
), qdcount
);
260 fprintf(file
, ", %s: %d",
261 p_section(ns_s_an
, opcode
), ancount
);
262 fprintf(file
, ", %s: %d",
263 p_section(ns_s_ns
, opcode
), nscount
);
264 fprintf(file
, ", %s: %d",
265 p_section(ns_s_ar
, opcode
), arcount
);
267 if ((!statp
->pfcode
) || (statp
->pfcode
&
268 (RES_PRF_HEADX
| RES_PRF_HEAD2
| RES_PRF_HEAD1
))) {
272 * Print the various sections.
274 do_section(statp
, &handle
, ns_s_qd
, RES_PRF_QUES
, file
);
275 do_section(statp
, &handle
, ns_s_an
, RES_PRF_ANS
, file
);
276 do_section(statp
, &handle
, ns_s_ns
, RES_PRF_AUTH
, file
);
277 do_section(statp
, &handle
, ns_s_ar
, RES_PRF_ADD
, file
);
278 if (qdcount
== 0 && ancount
== 0 &&
279 nscount
== 0 && arcount
== 0)
284 p_cdnname(const u_char
*cp
, const u_char
*msg
, int len
, FILE *file
) {
288 if ((n
= dn_expand(msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
296 libresolv_hidden_def (p_cdnname
)
299 p_cdname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
300 return (p_cdnname(cp
, msg
, PACKETSZ
, file
));
303 /* Return a fully-qualified domain name from a compressed name (with
307 p_fqnname (const u_char
*cp
, const u_char
*msg
, int msglen
, char *name
,
312 if ((n
= dn_expand(msg
, cp
+ msglen
, cp
, name
, namelen
)) < 0)
314 newlen
= strlen(name
);
315 if (newlen
== 0 || name
[newlen
- 1] != '.') {
316 if (newlen
+ 1 >= namelen
) /* Lack space for final dot */
319 strcpy(name
+ newlen
, ".");
323 libresolv_hidden_def (p_fqnname
)
325 /* XXX: the rest of these functions need to become length-limited, too. */
328 p_fqname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
332 n
= p_fqnname(cp
, msg
, MAXCDNAME
, name
, sizeof name
);
340 * Names of RR classes and qclasses. Classes and qclasses are the same, except
341 * that C_ANY is a qclass but not a class. (You can ask for records of class
342 * C_ANY, but you can't have any records of that class in the database.)
344 extern const struct res_sym __p_class_syms
[];
345 libresolv_hidden_proto (__p_class_syms
)
346 const struct res_sym __p_class_syms
[] = {
347 {C_IN
, (char *) "IN"},
348 {C_CHAOS
, (char *) "CHAOS"},
349 {C_HS
, (char *) "HS"},
350 {C_HS
, (char *) "HESIOD"},
351 {C_ANY
, (char *) "ANY"},
352 {C_NONE
, (char *) "NONE"},
355 libresolv_hidden_data_def (__p_class_syms
)
358 * Names of message sections.
360 const struct res_sym __p_default_section_syms
[] attribute_hidden
= {
361 {ns_s_qd
, (char *) "QUERY"},
362 {ns_s_an
, (char *) "ANSWER"},
363 {ns_s_ns
, (char *) "AUTHORITY"},
364 {ns_s_ar
, (char *) "ADDITIONAL"},
368 const struct res_sym __p_update_section_syms
[] attribute_hidden
= {
369 {S_ZONE
, (char *) "ZONE"},
370 {S_PREREQ
, (char *) "PREREQUISITE"},
371 {S_UPDATE
, (char *) "UPDATE"},
372 {S_ADDT
, (char *) "ADDITIONAL"},
377 * Names of RR types and qtypes. The list is incomplete because its
378 * size is part of the ABI.
380 extern const struct res_sym __p_type_syms
[];
381 libresolv_hidden_proto (__p_type_syms
)
382 const struct res_sym __p_type_syms
[] = {
383 {ns_t_a
, (char *) "A", (char *) "address"},
384 {ns_t_ns
, (char *) "NS", (char *) "name server"},
385 {ns_t_md
, (char *) "MD", (char *) "mail destination (deprecated)"},
386 {ns_t_mf
, (char *) "MF", (char *) "mail forwarder (deprecated)"},
387 {ns_t_cname
, (char *) "CNAME", (char *) "canonical name"},
388 {ns_t_soa
, (char *) "SOA", (char *) "start of authority"},
389 {ns_t_mb
, (char *) "MB", (char *) "mailbox"},
390 {ns_t_mg
, (char *) "MG", (char *) "mail group member"},
391 {ns_t_mr
, (char *) "MR", (char *) "mail rename"},
392 {ns_t_null
, (char *) "NULL", (char *) "null"},
393 {ns_t_wks
, (char *) "WKS", (char *) "well-known service (deprecated)"},
394 {ns_t_ptr
, (char *) "PTR", (char *) "domain name pointer"},
395 {ns_t_hinfo
, (char *) "HINFO", (char *) "host information"},
396 {ns_t_minfo
, (char *) "MINFO", (char *) "mailbox information"},
397 {ns_t_mx
, (char *) "MX", (char *) "mail exchanger"},
398 {ns_t_txt
, (char *) "TXT", (char *) "text"},
399 {ns_t_rp
, (char *) "RP", (char *) "responsible person"},
400 {ns_t_afsdb
, (char *) "AFSDB", (char *) "DCE or AFS server"},
401 {ns_t_x25
, (char *) "X25", (char *) "X25 address"},
402 {ns_t_isdn
, (char *) "ISDN", (char *) "ISDN address"},
403 {ns_t_rt
, (char *) "RT", (char *) "router"},
404 {ns_t_nsap
, (char *) "NSAP", (char *) "nsap address"},
405 {ns_t_nsap_ptr
, (char *) "NSAP_PTR", (char *) "domain name pointer"},
406 {ns_t_sig
, (char *) "SIG", (char *) "signature"},
407 {ns_t_key
, (char *) "KEY", (char *) "key"},
408 {ns_t_px
, (char *) "PX", (char *) "mapping information"},
409 {ns_t_gpos
, (char *) "GPOS",
410 (char *) "geographical position (withdrawn)"},
411 {ns_t_aaaa
, (char *) "AAAA", (char *) "IPv6 address"},
412 {ns_t_loc
, (char *) "LOC", (char *) "location"},
413 {ns_t_nxt
, (char *) "NXT", (char *) "next valid name (unimplemented)"},
414 {ns_t_eid
, (char *) "EID", (char *) "endpoint identifier (unimplemented)"},
415 {ns_t_nimloc
, (char *) "NIMLOC", (char *) "NIMROD locator (unimplemented)"},
416 {ns_t_srv
, (char *) "SRV", (char *) "server selection"},
417 {ns_t_atma
, (char *) "ATMA", (char *) "ATM address (unimplemented)"},
418 {ns_t_dname
, (char *) "DNAME", (char *) "Non-terminal DNAME (for IPv6)"},
419 {ns_t_tsig
, (char *) "TSIG", (char *) "transaction signature"},
420 {ns_t_ixfr
, (char *) "IXFR", (char *) "incremental zone transfer"},
421 {ns_t_axfr
, (char *) "AXFR", (char *) "zone transfer"},
422 {ns_t_mailb
, (char *) "MAILB", (char *) "mailbox-related data (deprecated)"},
423 {ns_t_maila
, (char *) "MAILA", (char *) "mail agent (deprecated)"},
424 {ns_t_naptr
, (char *) "NAPTR", (char *) "URN Naming Authority"},
425 {ns_t_kx
, (char *) "KX", (char *) "Key Exchange"},
426 {ns_t_cert
, (char *) "CERT", (char *) "Certificate"},
427 {ns_t_any
, (char *) "ANY", (char *) "\"any\""},
428 {0, NULL
, NULL
}, /* Padding to preserve ABI. */
431 libresolv_hidden_data_def (__p_type_syms
)
434 * Names of DNS rcodes.
436 const struct res_sym __p_rcode_syms
[] attribute_hidden
= {
437 {ns_r_noerror
, (char *) "NOERROR", (char *) "no error"},
438 {ns_r_formerr
, (char *) "FORMERR", (char *) "format error"},
439 {ns_r_servfail
, (char *) "SERVFAIL", (char *) "server failed"},
440 {ns_r_nxdomain
, (char *) "NXDOMAIN", (char *) "no such domain name"},
441 {ns_r_notimpl
, (char *) "NOTIMP", (char *) "not implemented"},
442 {ns_r_refused
, (char *) "REFUSED", (char *) "refused"},
443 {ns_r_yxdomain
, (char *) "YXDOMAIN", (char *) "domain name exists"},
444 {ns_r_yxrrset
, (char *) "YXRRSET", (char *) "rrset exists"},
445 {ns_r_nxrrset
, (char *) "NXRRSET", (char *) "rrset doesn't exist"},
446 {ns_r_notauth
, (char *) "NOTAUTH", (char *) "not authoritative"},
447 {ns_r_notzone
, (char *) "NOTZONE", (char *) "Not in zone"},
448 {ns_r_max
, (char *) "", (char *) ""},
449 {ns_r_badsig
, (char *) "BADSIG", (char *) "bad signature"},
450 {ns_r_badkey
, (char *) "BADKEY", (char *) "bad key"},
451 {ns_r_badtime
, (char *) "BADTIME", (char *) "bad time"},
456 sym_ston(const struct res_sym
*syms
, const char *name
, int *success
) {
457 for ((void)NULL
; syms
->name
!= 0; syms
++) {
458 if (strcasecmp (name
, syms
->name
) == 0) {
461 return (syms
->number
);
466 return (syms
->number
); /* The default value. */
470 sym_ntos(const struct res_sym
*syms
, int number
, int *success
) {
471 static char unname
[20];
473 for ((void)NULL
; syms
->name
!= 0; syms
++) {
474 if (number
== syms
->number
) {
481 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
486 libresolv_hidden_def (sym_ntos
)
489 sym_ntop(const struct res_sym
*syms
, int number
, int *success
) {
490 static char unname
[20];
492 for ((void)NULL
; syms
->name
!= 0; syms
++) {
493 if (number
== syms
->number
) {
496 return (syms
->humanname
);
499 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
506 * Return a string for the type.
510 return (sym_ntos(__p_type_syms
, type
, (int *)0));
512 libresolv_hidden_def (p_type
)
515 * Return a string for the type.
518 p_section(int section
, int opcode
) {
519 const struct res_sym
*symbols
;
523 symbols
= __p_update_section_syms
;
526 symbols
= __p_default_section_syms
;
529 return (sym_ntos(symbols
, section
, (int *)0));
533 * Return a mnemonic for class.
537 return (sym_ntos(__p_class_syms
, class, (int *)0));
539 libresolv_hidden_def (p_class
)
542 * Return a mnemonic for an option
545 p_option(u_long option
) {
546 static char nbuf
[40];
549 case RES_INIT
: return "init";
550 case RES_DEBUG
: return "debug";
551 case RES_USEVC
: return "use-vc";
552 case RES_IGNTC
: return "igntc";
553 case RES_RECURSE
: return "recurs";
554 case RES_DEFNAMES
: return "defnam";
555 case RES_STAYOPEN
: return "styopn";
556 case RES_DNSRCH
: return "dnsrch";
557 case RES_INSECURE1
: return "insecure1";
558 case RES_INSECURE2
: return "insecure2";
559 case RES_NOALIASES
: return "noaliases";
560 case DEPRECATED_RES_USE_INET6
: return "inet6";
561 case RES_ROTATE
: return "rotate";
562 case RES_USE_EDNS0
: return "edns0";
563 case RES_SNGLKUP
: return "single-request";
564 case RES_SNGLKUPREOP
: return "single-request-reopen";
565 case RES_USE_DNSSEC
: return "dnssec";
566 case RES_NOTLDQUERY
: return "no-tld-query";
567 /* XXX nonreentrant */
568 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
572 libresolv_hidden_def (p_option
)
575 * Return a mnemonic for a time to live.
578 p_time(u_int32_t value
) {
579 static char nbuf
[40]; /* XXX nonreentrant */
581 if (ns_format_ttl(value
, nbuf
, sizeof nbuf
) < 0)
582 sprintf(nbuf
, "%u", value
);
587 * Return a string for the rcode.
591 return (sym_ntos(__p_rcode_syms
, rcode
, (int *)0));
593 libresolv_hidden_def (p_rcode
)
596 * routines to convert between on-the-wire RR format and zone file format.
597 * Does not contain conversion to/from decimal degrees; divide or multiply
598 * by 60*60*1000 for that.
601 static const unsigned int poweroften
[10]=
602 { 1, 10, 100, 1000, 10000, 100000,
603 1000000,10000000,100000000,1000000000};
605 /* takes an XeY precision/size value, returns a string representation. */
607 precsize_ntoa (u_int8_t prec
)
609 static char retbuf
[sizeof "90000000.00"]; /* XXX nonreentrant */
611 int mantissa
, exponent
;
613 mantissa
= (int)((prec
>> 4) & 0x0f) % 10;
614 exponent
= (int)((prec
>> 0) & 0x0f) % 10;
616 val
= mantissa
* poweroften
[exponent
];
618 (void) sprintf(retbuf
, "%ld.%.2ld", val
/100, val
%100);
622 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
624 precsize_aton (const char **strptr
)
626 unsigned int mval
= 0, cmval
= 0;
635 mval
= mval
* 10 + (*cp
++ - '0');
637 if (*cp
== '.') { /* centimeters */
640 cmval
= (*cp
++ - '0') * 10;
642 cmval
+= (*cp
++ - '0');
646 cmval
= (mval
* 100) + cmval
;
648 for (exponent
= 0; exponent
< 9; exponent
++)
649 if (cmval
< poweroften
[exponent
+1])
652 mantissa
= cmval
/ poweroften
[exponent
];
656 retval
= (mantissa
<< 4) | exponent
;
663 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
665 latlon2ul (const char **latlonstrptr
, int *which
)
669 int deg
= 0, min
= 0, secs
= 0, secsfrac
= 0;
674 deg
= deg
* 10 + (*cp
++ - '0');
683 min
= min
* 10 + (*cp
++ - '0');
692 secs
= secs
* 10 + (*cp
++ - '0');
694 if (*cp
== '.') { /* decimal seconds */
697 secsfrac
= (*cp
++ - '0') * 100;
699 secsfrac
+= (*cp
++ - '0') * 10;
701 secsfrac
+= (*cp
++ - '0');
707 while (!isspace(*cp
)) /* if any trailing garbage */
717 retval
= ((unsigned)1<<31)
718 + (((((deg
* 60) + min
) * 60) + secs
) * 1000)
723 retval
= ((unsigned)1<<31)
724 - (((((deg
* 60) + min
) * 60) + secs
) * 1000)
728 retval
= 0; /* invalid value -- indicates error */
735 *which
= 1; /* latitude */
739 *which
= 2; /* longitude */
742 *which
= 0; /* error */
746 cp
++; /* skip the hemisphere */
748 while (!isspace(*cp
)) /* if any trailing garbage */
751 while (isspace(*cp
)) /* move to next field */
759 /* converts a zone file representation in a string to an RDATA on-the-wire
762 loc_aton (const char *ascii
, u_char
*binary
)
764 const char *cp
, *maxcp
;
767 u_int32_t latit
= 0, longit
= 0, alt
= 0;
768 u_int32_t lltemp1
= 0, lltemp2
= 0;
769 int altmeters
= 0, altfrac
= 0, altsign
= 1;
770 u_int8_t hp
= 0x16; /* default = 1e6 cm = 10000.00m = 10km */
771 u_int8_t vp
= 0x13; /* default = 1e3 cm = 10.00m */
772 u_int8_t siz
= 0x12; /* default = 1e2 cm = 1.00m */
773 int which1
= 0, which2
= 0;
776 maxcp
= cp
+ strlen(ascii
);
778 lltemp1
= latlon2ul(&cp
, &which1
);
780 lltemp2
= latlon2ul(&cp
, &which2
);
782 switch (which1
+ which2
) {
783 case 3: /* 1 + 2, the only valid combination */
784 if ((which1
== 1) && (which2
== 2)) { /* normal case */
787 } else if ((which1
== 2) && (which2
== 1)) { /* reversed */
790 } else { /* some kind of brokenness */
794 default: /* we didn't get one of each */
808 altmeters
= altmeters
* 10 + (*cp
++ - '0');
810 if (*cp
== '.') { /* decimal meters */
813 altfrac
= (*cp
++ - '0') * 10;
815 altfrac
+= (*cp
++ - '0');
820 alt
= (10000000 + (altsign
* (altmeters
* 100 + altfrac
)));
822 while (!isspace(*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
825 while (isspace(*cp
) && (cp
< maxcp
))
831 siz
= precsize_aton(&cp
);
833 while (!isspace(*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
836 while (isspace(*cp
) && (cp
< maxcp
))
842 hp
= precsize_aton(&cp
);
844 while (!isspace(*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
847 while (isspace(*cp
) && (cp
< maxcp
))
853 vp
= precsize_aton(&cp
);
858 *bcp
++ = (u_int8_t
) 0; /* version byte */
866 return (16); /* size of RR in octets */
869 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
871 loc_ntoa (const u_char
*binary
, char *ascii
)
873 static const char error
[] = "?";
874 static char tmpbuf
[sizeof
875 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
876 const u_char
*cp
= binary
;
878 int latdeg
, latmin
, latsec
, latsecfrac
;
879 int longdeg
, longmin
, longsec
, longsecfrac
;
880 char northsouth
, eastwest
;
881 int altmeters
, altfrac
, altsign
;
883 const u_int32_t referencealt
= 100000 * 100;
885 int32_t latval
, longval
, altval
;
887 u_int8_t sizeval
, hpval
, vpval
, versionval
;
889 char *sizestr
, *hpstr
, *vpstr
;
897 (void) sprintf(ascii
, "; error: unknown LOC RR version");
907 latval
= (templ
- ((unsigned)1<<31));
910 longval
= (templ
- ((unsigned)1<<31));
913 if (templ
< referencealt
) { /* below WGS 84 spheroid */
914 altval
= referencealt
- templ
;
917 altval
= templ
- referencealt
;
927 latsecfrac
= latval
% 1000;
928 latval
= latval
/ 1000;
929 latsec
= latval
% 60;
930 latval
= latval
/ 60;
931 latmin
= latval
% 60;
932 latval
= latval
/ 60;
941 longsecfrac
= longval
% 1000;
942 longval
= longval
/ 1000;
943 longsec
= longval
% 60;
944 longval
= longval
/ 60;
945 longmin
= longval
% 60;
946 longval
= longval
/ 60;
949 altfrac
= altval
% 100;
950 altmeters
= (altval
/ 100) * altsign
;
952 if ((sizestr
= strdup(precsize_ntoa(sizeval
))) == NULL
)
953 sizestr
= (char *) error
;
954 if ((hpstr
= strdup(precsize_ntoa(hpval
))) == NULL
)
955 hpstr
= (char *) error
;
956 if ((vpstr
= strdup(precsize_ntoa(vpval
))) == NULL
)
957 vpstr
= (char *) error
;
960 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
961 latdeg
, latmin
, latsec
, latsecfrac
, northsouth
,
962 longdeg
, longmin
, longsec
, longsecfrac
, eastwest
,
963 altmeters
, altfrac
, sizestr
, hpstr
, vpstr
);
965 if (sizestr
!= (char *) error
)
967 if (hpstr
!= (char *) error
)
969 if (vpstr
!= (char *) error
)
974 libresolv_hidden_def (loc_ntoa
)
977 /* Return the number of DNS hierarchy levels in the name. */
979 dn_count_labels(const char *name
) {
983 for (i
= 0, count
= 0; i
< len
; i
++) {
984 /* XXX need to check for \. or use named's nlabels(). */
989 /* don't count initial wildcard */
994 /* don't count the null label for root. */
995 /* if terminating '.' not found, must adjust */
996 /* count to include last label */
997 if (len
> 0 && name
[len
-1] != '.')
1001 libresolv_hidden_def (__dn_count_labels
)
1005 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1006 * SIG records are required to be printed like this, by the Secure DNS RFC.
1009 p_secstodate (u_long secs
) {
1010 /* XXX nonreentrant */
1011 static char output
[15]; /* YYYYMMDDHHMMSS and null */
1012 time_t clock
= secs
;
1016 time
= __gmtime_r(&clock
, &timebuf
);
1017 time
->tm_year
+= 1900;
1019 sprintf(output
, "%04d%02d%02d%02d%02d%02d",
1020 time
->tm_year
, time
->tm_mon
, time
->tm_mday
,
1021 time
->tm_hour
, time
->tm_min
, time
->tm_sec
);
1024 libresolv_hidden_def (__p_secstodate
)