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 * Copyright (c) 1995 IBM Corporation
53 * Permission is hereby granted, free of charge, to any person obtaining
54 * a copy of this software and associated documentation files (the
55 * 'Software'), to deal in the Software without restriction, including
56 * without limitation the rights to use, copy, modify, merge, publish,
57 * distribute, sublicense, and/or sell copies of the Software, and to
58 * permit persons to whom the Software is furnished to do so, subject to
59 * the following conditions:
61 * The above copyright notice and this permission notice shall be
62 * included in all copies or substantial portions of the Software.
64 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
65 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
67 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
74 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
76 * Permission to use, copy, modify, and distribute this software for any
77 * purpose with or without fee is hereby granted, provided that the above
78 * copyright notice and this permission notice appear in all copies.
80 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
81 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
82 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
83 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
84 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
85 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
86 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
90 #include <sys/types.h>
91 #include <sys/param.h>
92 #include <sys/socket.h>
94 #include <netinet/in.h>
95 #include <arpa/inet.h>
96 #include <arpa/nameser.h>
102 #include <resolv/resolv-internal.h>
107 #include <shlib-compat.h>
108 #include <libc-diag.h>
111 # define SPRINTF(x) strlen(sprintf/**/x)
113 # define SPRINTF(x) sprintf x
116 extern const char *_res_sectioncodes
[] attribute_hidden
;
118 /* _res_opcodes was exported by accident as a variable. */
119 #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26)
120 static const char *res_opcodes
[] =
122 static const char res_opcodes
[][9] =
128 "CQUERYU", /* experimental */
129 "NOTIFY", /* experimental */
142 #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26)
143 strong_alias (res_opcodes
, _res_opcodes
)
146 static const char *p_section(int section
, int opcode
);
149 * Print the current options.
152 fp_resstat(const res_state statp
, FILE *file
) {
155 fprintf(file
, ";; res options:");
156 for (mask
= 1; mask
!= 0; mask
<<= 1)
157 if (statp
->options
& mask
)
158 fprintf(file
, " %s", p_option(mask
));
163 do_section (int pfcode
, ns_msg
*handle
, ns_sect section
, int pflag
, FILE *file
)
166 static int buflen
= 2048;
172 * Print answer records.
174 sflag
= (pfcode
& pflag
);
175 if (pfcode
&& !sflag
)
178 buf
= malloc(buflen
);
180 fprintf(file
, ";; memory allocation failure\n");
184 opcode
= (ns_opcode
) ns_msg_getflag(*handle
, ns_f_opcode
);
187 if (ns_parserr(handle
, section
, rrnum
, &rr
)) {
189 fprintf(file
, ";; ns_parserr: %s\n",
191 else if (rrnum
> 0 && sflag
!= 0 &&
192 (pfcode
& RES_PRF_HEAD1
))
196 if (rrnum
== 0 && sflag
!= 0 && (pfcode
& RES_PRF_HEAD1
))
197 fprintf(file
, ";; %s SECTION:\n",
198 p_section(section
, opcode
));
199 if (section
== ns_s_qd
)
200 fprintf(file
, ";;\t%s, type = %s, class = %s\n",
202 p_type(ns_rr_type(rr
)),
203 p_class(ns_rr_class(rr
)));
205 n
= ns_sprintrr(handle
, &rr
, NULL
, NULL
,
208 if (errno
== ENOSPC
) {
212 buf
= malloc(buflen
+= 1024);
215 ";; memory allocation failure\n");
220 fprintf(file
, ";; ns_sprintrr: %s\n",
234 * Print the contents of a query.
235 * This is intended to be primarily a debugging routine.
238 fp_nquery (const unsigned char *msg
, int len
, FILE *file
)
241 int qdcount
, ancount
, nscount
, arcount
;
242 u_int opcode
, rcode
, id
;
244 /* There is no need to initialize _res: If _res is not yet
245 initialized, _res.pfcode is zero. But initialization will
246 leave it at zero, too. _res.pfcode is an unsigned long,
247 but the code here assumes that the flags fit into an int,
249 int pfcode
= _res
.pfcode
;
251 if (ns_initparse(msg
, len
, &handle
) < 0) {
252 fprintf(file
, ";; ns_initparse: %s\n", strerror(errno
));
255 opcode
= ns_msg_getflag(handle
, ns_f_opcode
);
256 rcode
= ns_msg_getflag(handle
, ns_f_rcode
);
257 id
= ns_msg_id(handle
);
258 qdcount
= ns_msg_count(handle
, ns_s_qd
);
259 ancount
= ns_msg_count(handle
, ns_s_an
);
260 nscount
= ns_msg_count(handle
, ns_s_ns
);
261 arcount
= ns_msg_count(handle
, ns_s_ar
);
264 * Print header fields.
266 if ((!pfcode
) || (pfcode
& RES_PRF_HEADX
) || rcode
)
268 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
269 res_opcodes
[opcode
], p_rcode(rcode
), id
);
270 if ((!pfcode
) || (pfcode
& RES_PRF_HEADX
))
272 if ((!pfcode
) || (pfcode
& RES_PRF_HEAD2
)) {
273 fprintf(file
, "; flags:");
274 if (ns_msg_getflag(handle
, ns_f_qr
))
275 fprintf(file
, " qr");
276 if (ns_msg_getflag(handle
, ns_f_aa
))
277 fprintf(file
, " aa");
278 if (ns_msg_getflag(handle
, ns_f_tc
))
279 fprintf(file
, " tc");
280 if (ns_msg_getflag(handle
, ns_f_rd
))
281 fprintf(file
, " rd");
282 if (ns_msg_getflag(handle
, ns_f_ra
))
283 fprintf(file
, " ra");
284 if (ns_msg_getflag(handle
, ns_f_z
))
285 fprintf(file
, " ??");
286 if (ns_msg_getflag(handle
, ns_f_ad
))
287 fprintf(file
, " ad");
288 if (ns_msg_getflag(handle
, ns_f_cd
))
289 fprintf(file
, " cd");
291 if ((!pfcode
) || (pfcode
& RES_PRF_HEAD1
)) {
292 fprintf(file
, "; %s: %d",
293 p_section(ns_s_qd
, opcode
), qdcount
);
294 fprintf(file
, ", %s: %d",
295 p_section(ns_s_an
, opcode
), ancount
);
296 fprintf(file
, ", %s: %d",
297 p_section(ns_s_ns
, opcode
), nscount
);
298 fprintf(file
, ", %s: %d",
299 p_section(ns_s_ar
, opcode
), arcount
);
301 if ((!pfcode
) || (pfcode
&
302 (RES_PRF_HEADX
| RES_PRF_HEAD2
| RES_PRF_HEAD1
))) {
306 * Print the various sections.
308 do_section (pfcode
, &handle
, ns_s_qd
, RES_PRF_QUES
, file
);
309 do_section (pfcode
, &handle
, ns_s_an
, RES_PRF_ANS
, file
);
310 do_section (pfcode
, &handle
, ns_s_ns
, RES_PRF_AUTH
, file
);
311 do_section (pfcode
, &handle
, ns_s_ar
, RES_PRF_ADD
, file
);
312 if (qdcount
== 0 && ancount
== 0 &&
313 nscount
== 0 && arcount
== 0)
316 libresolv_hidden_def (fp_nquery
)
319 fp_query (const unsigned char *msg
, FILE *file
)
321 fp_nquery (msg
, PACKETSZ
, file
);
323 libresolv_hidden_def (fp_query
)
326 p_query (const unsigned char *msg
)
328 fp_query (msg
, stdout
);
332 p_cdnname(const u_char
*cp
, const u_char
*msg
, int len
, FILE *file
) {
336 if ((n
= __libc_dn_expand (msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
344 libresolv_hidden_def (p_cdnname
)
347 p_cdname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
348 return (p_cdnname(cp
, msg
, PACKETSZ
, file
));
351 /* Return a fully-qualified domain name from a compressed name (with
355 p_fqnname (const u_char
*cp
, const u_char
*msg
, int msglen
, char *name
,
360 if ((n
= __libc_dn_expand (msg
, cp
+ msglen
, cp
, name
, namelen
)) < 0)
362 newlen
= strlen(name
);
363 if (newlen
== 0 || name
[newlen
- 1] != '.') {
364 if (newlen
+ 1 >= namelen
) /* Lack space for final dot */
367 strcpy(name
+ newlen
, ".");
371 libresolv_hidden_def (p_fqnname
)
373 /* XXX: the rest of these functions need to become length-limited, too. */
376 p_fqname(const u_char
*cp
, const u_char
*msg
, FILE *file
) {
380 n
= p_fqnname(cp
, msg
, MAXCDNAME
, name
, sizeof name
);
388 * Names of RR classes and qclasses. Classes and qclasses are the same, except
389 * that C_ANY is a qclass but not a class. (You can ask for records of class
390 * C_ANY, but you can't have any records of that class in the database.)
392 extern const struct res_sym __p_class_syms
[];
393 libresolv_hidden_proto (__p_class_syms
)
394 const struct res_sym __p_class_syms
[] = {
395 {C_IN
, (char *) "IN"},
396 {C_CHAOS
, (char *) "CHAOS"},
397 {C_HS
, (char *) "HS"},
398 {C_HS
, (char *) "HESIOD"},
399 {C_ANY
, (char *) "ANY"},
400 {C_NONE
, (char *) "NONE"},
403 libresolv_hidden_data_def (__p_class_syms
)
406 * Names of message sections.
408 const struct res_sym __p_default_section_syms
[] attribute_hidden
= {
409 {ns_s_qd
, (char *) "QUERY"},
410 {ns_s_an
, (char *) "ANSWER"},
411 {ns_s_ns
, (char *) "AUTHORITY"},
412 {ns_s_ar
, (char *) "ADDITIONAL"},
416 const struct res_sym __p_update_section_syms
[] attribute_hidden
= {
417 {S_ZONE
, (char *) "ZONE"},
418 {S_PREREQ
, (char *) "PREREQUISITE"},
419 {S_UPDATE
, (char *) "UPDATE"},
420 {S_ADDT
, (char *) "ADDITIONAL"},
425 * Names of RR types and qtypes. The list is incomplete because its
426 * size is part of the ABI.
428 extern const struct res_sym __p_type_syms
[];
429 libresolv_hidden_proto (__p_type_syms
)
430 const struct res_sym __p_type_syms
[] = {
431 {ns_t_a
, (char *) "A", (char *) "address"},
432 {ns_t_ns
, (char *) "NS", (char *) "name server"},
433 {ns_t_md
, (char *) "MD", (char *) "mail destination (deprecated)"},
434 {ns_t_mf
, (char *) "MF", (char *) "mail forwarder (deprecated)"},
435 {ns_t_cname
, (char *) "CNAME", (char *) "canonical name"},
436 {ns_t_soa
, (char *) "SOA", (char *) "start of authority"},
437 {ns_t_mb
, (char *) "MB", (char *) "mailbox"},
438 {ns_t_mg
, (char *) "MG", (char *) "mail group member"},
439 {ns_t_mr
, (char *) "MR", (char *) "mail rename"},
440 {ns_t_null
, (char *) "NULL", (char *) "null"},
441 {ns_t_wks
, (char *) "WKS", (char *) "well-known service (deprecated)"},
442 {ns_t_ptr
, (char *) "PTR", (char *) "domain name pointer"},
443 {ns_t_hinfo
, (char *) "HINFO", (char *) "host information"},
444 {ns_t_minfo
, (char *) "MINFO", (char *) "mailbox information"},
445 {ns_t_mx
, (char *) "MX", (char *) "mail exchanger"},
446 {ns_t_txt
, (char *) "TXT", (char *) "text"},
447 {ns_t_rp
, (char *) "RP", (char *) "responsible person"},
448 {ns_t_afsdb
, (char *) "AFSDB", (char *) "DCE or AFS server"},
449 {ns_t_x25
, (char *) "X25", (char *) "X25 address"},
450 {ns_t_isdn
, (char *) "ISDN", (char *) "ISDN address"},
451 {ns_t_rt
, (char *) "RT", (char *) "router"},
452 {ns_t_nsap
, (char *) "NSAP", (char *) "nsap address"},
453 {ns_t_nsap_ptr
, (char *) "NSAP_PTR", (char *) "domain name pointer"},
454 {ns_t_sig
, (char *) "SIG", (char *) "signature"},
455 {ns_t_key
, (char *) "KEY", (char *) "key"},
456 {ns_t_px
, (char *) "PX", (char *) "mapping information"},
457 {ns_t_gpos
, (char *) "GPOS",
458 (char *) "geographical position (withdrawn)"},
459 {ns_t_aaaa
, (char *) "AAAA", (char *) "IPv6 address"},
460 {ns_t_loc
, (char *) "LOC", (char *) "location"},
461 {ns_t_nxt
, (char *) "NXT", (char *) "next valid name (unimplemented)"},
462 {ns_t_eid
, (char *) "EID", (char *) "endpoint identifier (unimplemented)"},
463 {ns_t_nimloc
, (char *) "NIMLOC", (char *) "NIMROD locator (unimplemented)"},
464 {ns_t_srv
, (char *) "SRV", (char *) "server selection"},
465 {ns_t_atma
, (char *) "ATMA", (char *) "ATM address (unimplemented)"},
466 {ns_t_dname
, (char *) "DNAME", (char *) "Non-terminal DNAME (for IPv6)"},
467 {ns_t_tsig
, (char *) "TSIG", (char *) "transaction signature"},
468 {ns_t_ixfr
, (char *) "IXFR", (char *) "incremental zone transfer"},
469 {ns_t_axfr
, (char *) "AXFR", (char *) "zone transfer"},
470 {ns_t_mailb
, (char *) "MAILB", (char *) "mailbox-related data (deprecated)"},
471 {ns_t_maila
, (char *) "MAILA", (char *) "mail agent (deprecated)"},
472 {ns_t_naptr
, (char *) "NAPTR", (char *) "URN Naming Authority"},
473 {ns_t_kx
, (char *) "KX", (char *) "Key Exchange"},
474 {ns_t_cert
, (char *) "CERT", (char *) "Certificate"},
475 {ns_t_any
, (char *) "ANY", (char *) "\"any\""},
476 {0, NULL
, NULL
}, /* Padding to preserve ABI. */
479 libresolv_hidden_data_def (__p_type_syms
)
482 * Names of DNS rcodes.
484 const struct res_sym __p_rcode_syms
[] attribute_hidden
= {
485 {ns_r_noerror
, (char *) "NOERROR", (char *) "no error"},
486 {ns_r_formerr
, (char *) "FORMERR", (char *) "format error"},
487 {ns_r_servfail
, (char *) "SERVFAIL", (char *) "server failed"},
488 {ns_r_nxdomain
, (char *) "NXDOMAIN", (char *) "no such domain name"},
489 {ns_r_notimpl
, (char *) "NOTIMP", (char *) "not implemented"},
490 {ns_r_refused
, (char *) "REFUSED", (char *) "refused"},
491 {ns_r_yxdomain
, (char *) "YXDOMAIN", (char *) "domain name exists"},
492 {ns_r_yxrrset
, (char *) "YXRRSET", (char *) "rrset exists"},
493 {ns_r_nxrrset
, (char *) "NXRRSET", (char *) "rrset doesn't exist"},
494 {ns_r_notauth
, (char *) "NOTAUTH", (char *) "not authoritative"},
495 {ns_r_notzone
, (char *) "NOTZONE", (char *) "Not in zone"},
496 {ns_r_max
, (char *) "", (char *) ""},
497 {ns_r_badsig
, (char *) "BADSIG", (char *) "bad signature"},
498 {ns_r_badkey
, (char *) "BADKEY", (char *) "bad key"},
499 {ns_r_badtime
, (char *) "BADTIME", (char *) "bad time"},
504 sym_ston(const struct res_sym
*syms
, const char *name
, int *success
) {
505 for ((void)NULL
; syms
->name
!= 0; syms
++) {
506 if (strcasecmp (name
, syms
->name
) == 0) {
509 return (syms
->number
);
514 return (syms
->number
); /* The default value. */
518 sym_ntos(const struct res_sym
*syms
, int number
, int *success
) {
519 static char unname
[20];
521 for ((void)NULL
; syms
->name
!= 0; syms
++) {
522 if (number
== syms
->number
) {
529 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
534 libresolv_hidden_def (sym_ntos
)
537 sym_ntop(const struct res_sym
*syms
, int number
, int *success
) {
538 static char unname
[20];
540 for ((void)NULL
; syms
->name
!= 0; syms
++) {
541 if (number
== syms
->number
) {
544 return (syms
->humanname
);
547 sprintf(unname
, "%d", number
); /* XXX nonreentrant */
554 * Return a string for the type.
558 return (sym_ntos(__p_type_syms
, type
, (int *)0));
560 libresolv_hidden_def (p_type
)
563 * Return a string for the type.
566 p_section(int section
, int opcode
) {
567 const struct res_sym
*symbols
;
571 symbols
= __p_update_section_syms
;
574 symbols
= __p_default_section_syms
;
577 return (sym_ntos(symbols
, section
, (int *)0));
581 * Return a mnemonic for class.
585 return (sym_ntos(__p_class_syms
, class, (int *)0));
587 libresolv_hidden_def (p_class
)
590 * Return a mnemonic for an option
593 p_option(u_long option
) {
594 static char nbuf
[40];
597 case RES_INIT
: return "init";
598 case RES_DEBUG
: return "debug";
599 case RES_USEVC
: return "use-vc";
600 case RES_IGNTC
: return "igntc";
601 case RES_RECURSE
: return "recurs";
602 case RES_DEFNAMES
: return "defnam";
603 case RES_STAYOPEN
: return "styopn";
604 case RES_DNSRCH
: return "dnsrch";
605 case RES_NOALIASES
: return "noaliases";
606 case RES_ROTATE
: return "rotate";
607 case RES_USE_EDNS0
: return "edns0";
608 case RES_SNGLKUP
: return "single-request";
609 case RES_SNGLKUPREOP
: return "single-request-reopen";
610 case RES_USE_DNSSEC
: return "dnssec";
611 case RES_NOTLDQUERY
: return "no-tld-query";
612 case RES_NORELOAD
: return "no-reload";
613 case RES_TRUSTAD
: return "trust-ad";
614 case RES_NOAAAA
: return "no-aaaa";
615 /* XXX nonreentrant */
616 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
620 libresolv_hidden_def (p_option
)
623 * Return a mnemonic for a time to live.
626 p_time(uint32_t value
) {
627 static char nbuf
[40]; /* XXX nonreentrant */
629 if (ns_format_ttl(value
, nbuf
, sizeof nbuf
) < 0)
630 sprintf(nbuf
, "%u", value
);
635 * Return a string for the rcode.
639 return (sym_ntos(__p_rcode_syms
, rcode
, (int *)0));
641 libresolv_hidden_def (p_rcode
)
644 * routines to convert between on-the-wire RR format and zone file format.
645 * Does not contain conversion to/from decimal degrees; divide or multiply
646 * by 60*60*1000 for that.
649 static const unsigned int poweroften
[10]=
650 { 1, 10, 100, 1000, 10000, 100000,
651 1000000,10000000,100000000,1000000000};
653 /* takes an XeY precision/size value, returns a string representation. */
655 precsize_ntoa (uint8_t prec
)
657 static char retbuf
[sizeof "90000000.00"]; /* XXX nonreentrant */
659 int mantissa
, exponent
;
661 mantissa
= (int)((prec
>> 4) & 0x0f) % 10;
662 exponent
= (int)((prec
>> 0) & 0x0f) % 10;
664 val
= mantissa
* poweroften
[exponent
];
666 (void) sprintf(retbuf
, "%ld.%.2ld", val
/100, val
%100);
670 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
672 precsize_aton (const char **strptr
)
674 unsigned int mval
= 0, cmval
= 0;
683 mval
= mval
* 10 + (*cp
++ - '0');
685 if (*cp
== '.') { /* centimeters */
688 cmval
= (*cp
++ - '0') * 10;
690 cmval
+= (*cp
++ - '0');
694 cmval
= (mval
* 100) + cmval
;
696 for (exponent
= 0; exponent
< 9; exponent
++)
697 if (cmval
< poweroften
[exponent
+1])
700 mantissa
= cmval
/ poweroften
[exponent
];
704 retval
= (mantissa
<< 4) | exponent
;
711 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
713 latlon2ul (const char **latlonstrptr
, int *which
)
717 int deg
= 0, min
= 0, secs
= 0, secsfrac
= 0;
722 deg
= deg
* 10 + (*cp
++ - '0');
731 min
= min
* 10 + (*cp
++ - '0');
740 secs
= secs
* 10 + (*cp
++ - '0');
742 if (*cp
== '.') { /* decimal seconds */
745 secsfrac
= (*cp
++ - '0') * 100;
747 secsfrac
+= (*cp
++ - '0') * 10;
749 secsfrac
+= (*cp
++ - '0');
755 while (!isspace(*cp
)) /* if any trailing garbage */
765 retval
= ((unsigned)1<<31)
766 + (((((deg
* 60) + min
) * 60) + secs
) * 1000)
771 retval
= ((unsigned)1<<31)
772 - (((((deg
* 60) + min
) * 60) + secs
) * 1000)
776 retval
= 0; /* invalid value -- indicates error */
783 *which
= 1; /* latitude */
787 *which
= 2; /* longitude */
790 *which
= 0; /* error */
794 cp
++; /* skip the hemisphere */
796 while (!isspace(*cp
)) /* if any trailing garbage */
799 while (isspace(*cp
)) /* move to next field */
807 /* converts a zone file representation in a string to an RDATA on-the-wire
810 loc_aton (const char *ascii
, u_char
*binary
)
812 const char *cp
, *maxcp
;
815 uint32_t latit
= 0, longit
= 0, alt
= 0;
816 uint32_t lltemp1
= 0, lltemp2
= 0;
817 int altmeters
= 0, altfrac
= 0, altsign
= 1;
818 uint8_t hp
= 0x16; /* default = 1e6 cm = 10000.00m = 10km */
819 uint8_t vp
= 0x13; /* default = 1e3 cm = 10.00m */
820 uint8_t siz
= 0x12; /* default = 1e2 cm = 1.00m */
821 int which1
= 0, which2
= 0;
824 maxcp
= cp
+ strlen(ascii
);
826 lltemp1
= latlon2ul(&cp
, &which1
);
828 lltemp2
= latlon2ul(&cp
, &which2
);
830 switch (which1
+ which2
) {
831 case 3: /* 1 + 2, the only valid combination */
832 if ((which1
== 1) && (which2
== 2)) { /* normal case */
835 } else if ((which1
== 2) && (which2
== 1)) { /* reversed */
838 } else { /* some kind of brokenness */
842 default: /* we didn't get one of each */
856 altmeters
= altmeters
* 10 + (*cp
++ - '0');
858 if (*cp
== '.') { /* decimal meters */
861 altfrac
= (*cp
++ - '0') * 10;
863 altfrac
+= (*cp
++ - '0');
868 alt
= (10000000 + (altsign
* (altmeters
* 100 + altfrac
)));
870 while (!isspace(*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
873 while (isspace(*cp
) && (cp
< maxcp
))
879 siz
= precsize_aton(&cp
);
881 while (!isspace(*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
884 while (isspace(*cp
) && (cp
< maxcp
))
890 hp
= precsize_aton(&cp
);
892 while (!isspace(*cp
) && (cp
< maxcp
)) /* if trailing garbage or m */
895 while (isspace(*cp
) && (cp
< maxcp
))
901 vp
= precsize_aton(&cp
);
906 *bcp
++ = (uint8_t) 0; /* version byte */
914 return (16); /* size of RR in octets */
917 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
919 loc_ntoa (const u_char
*binary
, char *ascii
)
921 static const char error
[] = "?";
922 static char tmpbuf
[sizeof
923 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
924 const u_char
*cp
= binary
;
926 int latdeg
, latmin
, latsec
, latsecfrac
;
927 int longdeg
, longmin
, longsec
, longsecfrac
;
928 char northsouth
, eastwest
;
929 int altmeters
, altfrac
, altsign
;
931 const uint32_t referencealt
= 100000 * 100;
933 int32_t latval
, longval
, altval
;
935 uint8_t sizeval
, hpval
, vpval
, versionval
;
937 char *sizestr
, *hpstr
, *vpstr
;
945 (void) sprintf(ascii
, "; error: unknown LOC RR version");
955 latval
= (templ
- ((unsigned)1<<31));
958 longval
= (templ
- ((unsigned)1<<31));
961 if (templ
< referencealt
) { /* below WGS 84 spheroid */
962 altval
= referencealt
- templ
;
965 altval
= templ
- referencealt
;
975 latsecfrac
= latval
% 1000;
976 latval
= latval
/ 1000;
977 latsec
= latval
% 60;
978 latval
= latval
/ 60;
979 latmin
= latval
% 60;
980 latval
= latval
/ 60;
989 longsecfrac
= longval
% 1000;
990 longval
= longval
/ 1000;
991 longsec
= longval
% 60;
992 longval
= longval
/ 60;
993 longmin
= longval
% 60;
994 longval
= longval
/ 60;
997 altfrac
= altval
% 100;
998 altmeters
= (altval
/ 100) * altsign
;
1000 if ((sizestr
= strdup(precsize_ntoa(sizeval
))) == NULL
)
1001 sizestr
= (char *) error
;
1002 if ((hpstr
= strdup(precsize_ntoa(hpval
))) == NULL
)
1003 hpstr
= (char *) error
;
1004 if ((vpstr
= strdup(precsize_ntoa(vpval
))) == NULL
)
1005 vpstr
= (char *) error
;
1008 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1009 latdeg
, latmin
, latsec
, latsecfrac
, northsouth
,
1010 longdeg
, longmin
, longsec
, longsecfrac
, eastwest
,
1011 altmeters
, altfrac
, sizestr
, hpstr
, vpstr
);
1013 if (sizestr
!= (char *) error
)
1015 if (hpstr
!= (char *) error
)
1017 if (vpstr
!= (char *) error
)
1022 libresolv_hidden_def (loc_ntoa
)
1025 /* Return the number of DNS hierarchy levels in the name. */
1027 dn_count_labels(const char *name
) {
1031 for (i
= 0, count
= 0; i
< len
; i
++) {
1032 /* XXX need to check for \. or use named's nlabels(). */
1037 /* don't count initial wildcard */
1042 /* don't count the null label for root. */
1043 /* if terminating '.' not found, must adjust */
1044 /* count to include last label */
1045 if (len
> 0 && name
[len
-1] != '.')
1049 libresolv_hidden_def (__dn_count_labels
)
1052 #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_27)
1054 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1055 * SIG records are required to be printed like this, by the Secure DNS RFC.
1056 * This is an obsolescent function and does not handle dates outside the
1057 * signed 32-bit range.
1060 __p_secstodate (u_long secs
) {
1061 /* XXX nonreentrant */
1062 static char output
[15]; /* YYYYMMDDHHMMSS and null */
1063 time_t clock
= secs
;
1067 /* The call to __gmtime_r can never produce a year overflowing
1068 the range of int, given the check on SECS, but check for a
1069 NULL return anyway to avoid a null pointer dereference in
1070 case there are any other unspecified errors. */
1071 if (secs
> 0x7fffffff
1072 || (time
= __gmtime_r (&clock
, &timebuf
)) == NULL
) {
1073 strcpy (output
, "<overflow>");
1074 __set_errno (EOVERFLOW
);
1077 time
->tm_year
+= 1900;
1079 /* The struct tm fields, given the above range check,
1080 must have values that mean this sprintf exactly fills the
1081 buffer. But as of GCC 8 of 2017-11-21, GCC cannot tell
1082 that, even given range checks on all fields with
1083 __builtin_unreachable called for out-of-range values. */
1084 DIAG_PUSH_NEEDS_COMMENT
;
1085 # if __GNUC_PREREQ (7, 0)
1086 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wformat-overflow=");
1088 sprintf(output
, "%04d%02d%02d%02d%02d%02d",
1089 time
->tm_year
, time
->tm_mon
, time
->tm_mday
,
1090 time
->tm_hour
, time
->tm_min
, time
->tm_sec
);
1091 DIAG_POP_NEEDS_COMMENT
;
1094 compat_symbol (libresolv
, __p_secstodate
, __p_secstodate
, GLIBC_2_0
);