]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* |
b43b13ac | 2 | * Copyright (c) 1985 |
28f540f4 | 3 | * The Regents of the University of California. All rights reserved. |
0a463fa9 | 4 | * |
28f540f4 RM |
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. | |
28f540f4 RM |
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. | |
0a463fa9 | 16 | * |
28f540f4 RM |
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. | |
b43b13ac UD |
28 | */ |
29 | ||
30 | /* | |
28f540f4 | 31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
0a463fa9 | 32 | * |
28f540f4 RM |
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. | |
0a463fa9 | 39 | * |
28f540f4 RM |
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 | |
47 | * SOFTWARE. | |
b43b13ac UD |
48 | */ |
49 | ||
50 | /* | |
df21c858 UD |
51 | * Portions Copyright (c) 1995 by International Business Machines, Inc. |
52 | * | |
53 | * International Business Machines, Inc. (hereinafter called IBM) grants | |
54 | * permission under its copyrights to use, copy, modify, and distribute this | |
55 | * Software with or without fee, provided that the above copyright notice and | |
56 | * all paragraphs of this notice appear in all copies, and that the name of IBM | |
57 | * not be used in connection with the marketing of any product incorporating | |
58 | * the Software or modifications thereof, without specific, written prior | |
59 | * permission. | |
60 | * | |
61 | * To the extent it has a right to do so, IBM grants an immunity from suit | |
62 | * under its patents, if any, for the use, sale or manufacture of products to | |
63 | * the extent that such products are used for performing Domain Name System | |
64 | * dynamic updates in TCP/IP networks by means of the Software. No immunity is | |
65 | * granted for any product per se or for any other function of any product. | |
66 | * | |
67 | * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, | |
68 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
69 | * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, | |
70 | * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING | |
71 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN | |
72 | * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
b43b13ac UD |
73 | */ |
74 | ||
75 | /* | |
76 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. | |
77 | * | |
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. | |
81 | * | |
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 | |
89 | * SOFTWARE. | |
28f540f4 RM |
90 | */ |
91 | ||
fa0bc87c | 92 | #include <sys/types.h> |
b43b13ac | 93 | #include <sys/param.h> |
fa0bc87c | 94 | #include <sys/socket.h> |
b43b13ac | 95 | |
28f540f4 RM |
96 | #include <netinet/in.h> |
97 | #include <arpa/inet.h> | |
98 | #include <arpa/nameser.h> | |
99 | ||
58e07151 | 100 | #include <ctype.h> |
b43b13ac UD |
101 | #include <errno.h> |
102 | #include <math.h> | |
3d61b63c | 103 | #include <netdb.h> |
b76e0659 | 104 | #include <resolv/resolv-internal.h> |
df21c858 | 105 | #include <stdio.h> |
b43b13ac UD |
106 | #include <stdlib.h> |
107 | #include <string.h> | |
df21c858 | 108 | #include <time.h> |
6781d8e6 | 109 | #include <shlib-compat.h> |
f120cda6 | 110 | #include <libc-diag.h> |
df21c858 | 111 | |
b43b13ac UD |
112 | #ifdef SPRINTF_CHAR |
113 | # define SPRINTF(x) strlen(sprintf/**/x) | |
28f540f4 | 114 | #else |
b43b13ac | 115 | # define SPRINTF(x) sprintf x |
28f540f4 RM |
116 | #endif |
117 | ||
6f9d8e68 | 118 | extern const char *_res_sectioncodes[] attribute_hidden; |
28f540f4 | 119 | |
6781d8e6 FW |
120 | /* _res_opcodes was exported by accident as a variable. */ |
121 | #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26) | |
122 | static const char *res_opcodes[] = | |
123 | #else | |
124 | static const char res_opcodes[][9] = | |
125 | #endif | |
d85f9967 FW |
126 | { |
127 | "QUERY", | |
128 | "IQUERY", | |
129 | "CQUERYM", | |
130 | "CQUERYU", /* experimental */ | |
131 | "NOTIFY", /* experimental */ | |
132 | "UPDATE", | |
133 | "6", | |
134 | "7", | |
135 | "8", | |
136 | "9", | |
137 | "10", | |
138 | "11", | |
139 | "12", | |
140 | "13", | |
141 | "ZONEINIT", | |
142 | "ZONEREF", | |
143 | }; | |
6781d8e6 FW |
144 | #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26) |
145 | strong_alias (res_opcodes, _res_opcodes) | |
146 | #endif | |
d85f9967 | 147 | |
a7ff1da8 FW |
148 | static const char *p_section(int section, int opcode); |
149 | ||
b43b13ac UD |
150 | /* |
151 | * Print the current options. | |
152 | */ | |
153 | void | |
154 | fp_resstat(const res_state statp, FILE *file) { | |
155 | u_long mask; | |
28f540f4 | 156 | |
b43b13ac UD |
157 | fprintf(file, ";; res options:"); |
158 | for (mask = 1; mask != 0; mask <<= 1) | |
159 | if (statp->options & mask) | |
160 | fprintf(file, " %s", p_option(mask)); | |
161 | putc('\n', file); | |
28f540f4 RM |
162 | } |
163 | ||
b43b13ac | 164 | static void |
d85f9967 | 165 | do_section (int pfcode, ns_msg *handle, ns_sect section, int pflag, FILE *file) |
28f540f4 | 166 | { |
b43b13ac | 167 | int n, sflag, rrnum; |
e685e07d UD |
168 | static int buflen = 2048; |
169 | char *buf; | |
b43b13ac UD |
170 | ns_opcode opcode; |
171 | ns_rr rr; | |
28f540f4 RM |
172 | |
173 | /* | |
174 | * Print answer records. | |
175 | */ | |
d85f9967 FW |
176 | sflag = (pfcode & pflag); |
177 | if (pfcode && !sflag) | |
b43b13ac UD |
178 | return; |
179 | ||
e685e07d UD |
180 | buf = malloc(buflen); |
181 | if (buf == NULL) { | |
182 | fprintf(file, ";; memory allocation failure\n"); | |
183 | return; | |
184 | } | |
185 | ||
b43b13ac UD |
186 | opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode); |
187 | rrnum = 0; | |
188 | for (;;) { | |
189 | if (ns_parserr(handle, section, rrnum, &rr)) { | |
190 | if (errno != ENODEV) | |
191 | fprintf(file, ";; ns_parserr: %s\n", | |
192 | strerror(errno)); | |
193 | else if (rrnum > 0 && sflag != 0 && | |
d85f9967 | 194 | (pfcode & RES_PRF_HEAD1)) |
b43b13ac | 195 | putc('\n', file); |
e685e07d | 196 | goto cleanup; |
b43b13ac | 197 | } |
d85f9967 | 198 | if (rrnum == 0 && sflag != 0 && (pfcode & RES_PRF_HEAD1)) |
b43b13ac UD |
199 | fprintf(file, ";; %s SECTION:\n", |
200 | p_section(section, opcode)); | |
201 | if (section == ns_s_qd) | |
202 | fprintf(file, ";;\t%s, type = %s, class = %s\n", | |
203 | ns_rr_name(rr), | |
204 | p_type(ns_rr_type(rr)), | |
205 | p_class(ns_rr_class(rr))); | |
206 | else { | |
207 | n = ns_sprintrr(handle, &rr, NULL, NULL, | |
e685e07d | 208 | buf, buflen); |
b43b13ac | 209 | if (n < 0) { |
e685e07d UD |
210 | if (errno == ENOSPC) { |
211 | free(buf); | |
212 | buf = NULL; | |
213 | if (buflen < 131072) | |
214 | buf = malloc(buflen += 1024); | |
215 | if (buf == NULL) { | |
216 | fprintf(file, | |
f87dfb1f | 217 | ";; memory allocation failure\n"); |
e685e07d UD |
218 | return; |
219 | } | |
220 | continue; | |
221 | } | |
b43b13ac UD |
222 | fprintf(file, ";; ns_sprintrr: %s\n", |
223 | strerror(errno)); | |
e685e07d | 224 | goto cleanup; |
28f540f4 | 225 | } |
b43b13ac UD |
226 | fputs(buf, file); |
227 | fputc('\n', file); | |
28f540f4 | 228 | } |
b43b13ac | 229 | rrnum++; |
28f540f4 | 230 | } |
e685e07d | 231 | cleanup: |
72e6cdfa | 232 | free(buf); |
28f540f4 RM |
233 | } |
234 | ||
235 | /* | |
236 | * Print the contents of a query. | |
237 | * This is intended to be primarily a debugging routine. | |
238 | */ | |
239 | void | |
d85f9967 FW |
240 | fp_nquery (const unsigned char *msg, int len, FILE *file) |
241 | { | |
b43b13ac UD |
242 | ns_msg handle; |
243 | int qdcount, ancount, nscount, arcount; | |
244 | u_int opcode, rcode, id; | |
28f540f4 | 245 | |
d85f9967 FW |
246 | /* There is no need to initialize _res: If _res is not yet |
247 | initialized, _res.pfcode is zero. But initialization will | |
248 | leave it at zero, too. _res.pfcode is an unsigned long, | |
249 | but the code here assumes that the flags fit into an int, | |
250 | so use that. */ | |
251 | int pfcode = _res.pfcode; | |
252 | ||
b43b13ac UD |
253 | if (ns_initparse(msg, len, &handle) < 0) { |
254 | fprintf(file, ";; ns_initparse: %s\n", strerror(errno)); | |
3d61b63c | 255 | return; |
b43b13ac UD |
256 | } |
257 | opcode = ns_msg_getflag(handle, ns_f_opcode); | |
258 | rcode = ns_msg_getflag(handle, ns_f_rcode); | |
259 | id = ns_msg_id(handle); | |
260 | qdcount = ns_msg_count(handle, ns_s_qd); | |
261 | ancount = ns_msg_count(handle, ns_s_an); | |
262 | nscount = ns_msg_count(handle, ns_s_ns); | |
263 | arcount = ns_msg_count(handle, ns_s_ar); | |
28f540f4 RM |
264 | |
265 | /* | |
266 | * Print header fields. | |
267 | */ | |
d85f9967 | 268 | if ((!pfcode) || (pfcode & RES_PRF_HEADX) || rcode) |
b43b13ac UD |
269 | fprintf(file, |
270 | ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", | |
6781d8e6 | 271 | res_opcodes[opcode], p_rcode(rcode), id); |
d85f9967 | 272 | if ((!pfcode) || (pfcode & RES_PRF_HEADX)) |
a2e1b046 | 273 | putc(';', file); |
d85f9967 | 274 | if ((!pfcode) || (pfcode & RES_PRF_HEAD2)) { |
28f540f4 | 275 | fprintf(file, "; flags:"); |
b43b13ac | 276 | if (ns_msg_getflag(handle, ns_f_qr)) |
28f540f4 | 277 | fprintf(file, " qr"); |
b43b13ac | 278 | if (ns_msg_getflag(handle, ns_f_aa)) |
28f540f4 | 279 | fprintf(file, " aa"); |
b43b13ac | 280 | if (ns_msg_getflag(handle, ns_f_tc)) |
28f540f4 | 281 | fprintf(file, " tc"); |
b43b13ac | 282 | if (ns_msg_getflag(handle, ns_f_rd)) |
28f540f4 | 283 | fprintf(file, " rd"); |
b43b13ac | 284 | if (ns_msg_getflag(handle, ns_f_ra)) |
28f540f4 | 285 | fprintf(file, " ra"); |
b43b13ac UD |
286 | if (ns_msg_getflag(handle, ns_f_z)) |
287 | fprintf(file, " ??"); | |
288 | if (ns_msg_getflag(handle, ns_f_ad)) | |
df21c858 | 289 | fprintf(file, " ad"); |
b43b13ac | 290 | if (ns_msg_getflag(handle, ns_f_cd)) |
df21c858 | 291 | fprintf(file, " cd"); |
28f540f4 | 292 | } |
d85f9967 | 293 | if ((!pfcode) || (pfcode & RES_PRF_HEAD1)) { |
b43b13ac UD |
294 | fprintf(file, "; %s: %d", |
295 | p_section(ns_s_qd, opcode), qdcount); | |
296 | fprintf(file, ", %s: %d", | |
297 | p_section(ns_s_an, opcode), ancount); | |
298 | fprintf(file, ", %s: %d", | |
299 | p_section(ns_s_ns, opcode), nscount); | |
300 | fprintf(file, ", %s: %d", | |
301 | p_section(ns_s_ar, opcode), arcount); | |
28f540f4 | 302 | } |
d85f9967 | 303 | if ((!pfcode) || (pfcode & |
28f540f4 RM |
304 | (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) { |
305 | putc('\n',file); | |
306 | } | |
307 | /* | |
b43b13ac | 308 | * Print the various sections. |
28f540f4 | 309 | */ |
d85f9967 FW |
310 | do_section (pfcode, &handle, ns_s_qd, RES_PRF_QUES, file); |
311 | do_section (pfcode, &handle, ns_s_an, RES_PRF_ANS, file); | |
312 | do_section (pfcode, &handle, ns_s_ns, RES_PRF_AUTH, file); | |
313 | do_section (pfcode, &handle, ns_s_ar, RES_PRF_ADD, file); | |
b43b13ac UD |
314 | if (qdcount == 0 && ancount == 0 && |
315 | nscount == 0 && arcount == 0) | |
316 | putc('\n', file); | |
28f540f4 | 317 | } |
d85f9967 FW |
318 | libresolv_hidden_def (fp_nquery) |
319 | ||
320 | void | |
321 | fp_query (const unsigned char *msg, FILE *file) | |
322 | { | |
323 | fp_nquery (msg, PACKETSZ, file); | |
324 | } | |
325 | libresolv_hidden_def (fp_query) | |
326 | ||
327 | void | |
328 | p_query (const unsigned char *msg) | |
329 | { | |
330 | fp_query (msg, stdout); | |
331 | } | |
28f540f4 RM |
332 | |
333 | const u_char * | |
b43b13ac | 334 | p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) { |
28f540f4 RM |
335 | char name[MAXDNAME]; |
336 | int n; | |
337 | ||
338 | if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0) | |
339 | return (NULL); | |
340 | if (name[0] == '\0') | |
341 | putc('.', file); | |
342 | else | |
343 | fputs(name, file); | |
344 | return (cp + n); | |
345 | } | |
6f9d8e68 | 346 | libresolv_hidden_def (p_cdnname) |
28f540f4 RM |
347 | |
348 | const u_char * | |
b43b13ac | 349 | p_cdname(const u_char *cp, const u_char *msg, FILE *file) { |
28f540f4 RM |
350 | return (p_cdnname(cp, msg, PACKETSZ, file)); |
351 | } | |
352 | ||
df21c858 UD |
353 | /* Return a fully-qualified domain name from a compressed name (with |
354 | length supplied). */ | |
355 | ||
356 | const u_char * | |
41075ae3 JM |
357 | p_fqnname (const u_char *cp, const u_char *msg, int msglen, char *name, |
358 | int namelen) | |
df21c858 UD |
359 | { |
360 | int n, newlen; | |
361 | ||
362 | if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) | |
363 | return (NULL); | |
b43b13ac | 364 | newlen = strlen(name); |
3121cc04 | 365 | if (newlen == 0 || name[newlen - 1] != '.') { |
b43b13ac | 366 | if (newlen + 1 >= namelen) /* Lack space for final dot */ |
df21c858 UD |
367 | return (NULL); |
368 | else | |
369 | strcpy(name + newlen, "."); | |
3121cc04 | 370 | } |
df21c858 UD |
371 | return (cp + n); |
372 | } | |
6f9d8e68 | 373 | libresolv_hidden_def (p_fqnname) |
df21c858 | 374 | |
b43b13ac | 375 | /* XXX: the rest of these functions need to become length-limited, too. */ |
28f540f4 RM |
376 | |
377 | const u_char * | |
b43b13ac | 378 | p_fqname(const u_char *cp, const u_char *msg, FILE *file) { |
28f540f4 | 379 | char name[MAXDNAME]; |
df21c858 | 380 | const u_char *n; |
28f540f4 | 381 | |
b43b13ac | 382 | n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name); |
df21c858 | 383 | if (n == NULL) |
28f540f4 | 384 | return (NULL); |
df21c858 UD |
385 | fputs(name, file); |
386 | return (n); | |
28f540f4 RM |
387 | } |
388 | ||
df21c858 UD |
389 | /* |
390 | * Names of RR classes and qclasses. Classes and qclasses are the same, except | |
391 | * that C_ANY is a qclass but not a class. (You can ask for records of class | |
392 | * C_ANY, but you can't have any records of that class in the database.) | |
393 | */ | |
32738a22 UD |
394 | extern const struct res_sym __p_class_syms[]; |
395 | libresolv_hidden_proto (__p_class_syms) | |
396 | const struct res_sym __p_class_syms[] = { | |
bf8c5424 FW |
397 | {C_IN, (char *) "IN"}, |
398 | {C_CHAOS, (char *) "CHAOS"}, | |
399 | {C_HS, (char *) "HS"}, | |
400 | {C_HS, (char *) "HESIOD"}, | |
401 | {C_ANY, (char *) "ANY"}, | |
402 | {C_NONE, (char *) "NONE"}, | |
403 | {C_IN, NULL, NULL} | |
b43b13ac | 404 | }; |
32738a22 | 405 | libresolv_hidden_data_def (__p_class_syms) |
b43b13ac UD |
406 | |
407 | /* | |
408 | * Names of message sections. | |
409 | */ | |
6f9d8e68 | 410 | const struct res_sym __p_default_section_syms[] attribute_hidden = { |
bf8c5424 FW |
411 | {ns_s_qd, (char *) "QUERY"}, |
412 | {ns_s_an, (char *) "ANSWER"}, | |
413 | {ns_s_ns, (char *) "AUTHORITY"}, | |
414 | {ns_s_ar, (char *) "ADDITIONAL"}, | |
415 | {0, NULL, NULL} | |
b43b13ac UD |
416 | }; |
417 | ||
6f9d8e68 | 418 | const struct res_sym __p_update_section_syms[] attribute_hidden = { |
bf8c5424 FW |
419 | {S_ZONE, (char *) "ZONE"}, |
420 | {S_PREREQ, (char *) "PREREQUISITE"}, | |
421 | {S_UPDATE, (char *) "UPDATE"}, | |
422 | {S_ADDT, (char *) "ADDITIONAL"}, | |
423 | {0, NULL, NULL} | |
b43b13ac UD |
424 | }; |
425 | ||
df21c858 | 426 | /* |
4f157746 FW |
427 | * Names of RR types and qtypes. The list is incomplete because its |
428 | * size is part of the ABI. | |
df21c858 | 429 | */ |
32738a22 UD |
430 | extern const struct res_sym __p_type_syms[]; |
431 | libresolv_hidden_proto (__p_type_syms) | |
432 | const struct res_sym __p_type_syms[] = { | |
bf8c5424 FW |
433 | {ns_t_a, (char *) "A", (char *) "address"}, |
434 | {ns_t_ns, (char *) "NS", (char *) "name server"}, | |
435 | {ns_t_md, (char *) "MD", (char *) "mail destination (deprecated)"}, | |
436 | {ns_t_mf, (char *) "MF", (char *) "mail forwarder (deprecated)"}, | |
437 | {ns_t_cname, (char *) "CNAME", (char *) "canonical name"}, | |
438 | {ns_t_soa, (char *) "SOA", (char *) "start of authority"}, | |
439 | {ns_t_mb, (char *) "MB", (char *) "mailbox"}, | |
440 | {ns_t_mg, (char *) "MG", (char *) "mail group member"}, | |
441 | {ns_t_mr, (char *) "MR", (char *) "mail rename"}, | |
442 | {ns_t_null, (char *) "NULL", (char *) "null"}, | |
443 | {ns_t_wks, (char *) "WKS", (char *) "well-known service (deprecated)"}, | |
444 | {ns_t_ptr, (char *) "PTR", (char *) "domain name pointer"}, | |
445 | {ns_t_hinfo, (char *) "HINFO", (char *) "host information"}, | |
446 | {ns_t_minfo, (char *) "MINFO", (char *) "mailbox information"}, | |
447 | {ns_t_mx, (char *) "MX", (char *) "mail exchanger"}, | |
448 | {ns_t_txt, (char *) "TXT", (char *) "text"}, | |
449 | {ns_t_rp, (char *) "RP", (char *) "responsible person"}, | |
450 | {ns_t_afsdb, (char *) "AFSDB", (char *) "DCE or AFS server"}, | |
451 | {ns_t_x25, (char *) "X25", (char *) "X25 address"}, | |
452 | {ns_t_isdn, (char *) "ISDN", (char *) "ISDN address"}, | |
453 | {ns_t_rt, (char *) "RT", (char *) "router"}, | |
454 | {ns_t_nsap, (char *) "NSAP", (char *) "nsap address"}, | |
455 | {ns_t_nsap_ptr, (char *) "NSAP_PTR", (char *) "domain name pointer"}, | |
456 | {ns_t_sig, (char *) "SIG", (char *) "signature"}, | |
457 | {ns_t_key, (char *) "KEY", (char *) "key"}, | |
458 | {ns_t_px, (char *) "PX", (char *) "mapping information"}, | |
459 | {ns_t_gpos, (char *) "GPOS", | |
460 | (char *) "geographical position (withdrawn)"}, | |
461 | {ns_t_aaaa, (char *) "AAAA", (char *) "IPv6 address"}, | |
462 | {ns_t_loc, (char *) "LOC", (char *) "location"}, | |
463 | {ns_t_nxt, (char *) "NXT", (char *) "next valid name (unimplemented)"}, | |
464 | {ns_t_eid, (char *) "EID", (char *) "endpoint identifier (unimplemented)"}, | |
465 | {ns_t_nimloc, (char *) "NIMLOC", (char *) "NIMROD locator (unimplemented)"}, | |
466 | {ns_t_srv, (char *) "SRV", (char *) "server selection"}, | |
467 | {ns_t_atma, (char *) "ATMA", (char *) "ATM address (unimplemented)"}, | |
468 | {ns_t_dname, (char *) "DNAME", (char *) "Non-terminal DNAME (for IPv6)"}, | |
469 | {ns_t_tsig, (char *) "TSIG", (char *) "transaction signature"}, | |
470 | {ns_t_ixfr, (char *) "IXFR", (char *) "incremental zone transfer"}, | |
471 | {ns_t_axfr, (char *) "AXFR", (char *) "zone transfer"}, | |
bf8c5424 FW |
472 | {ns_t_mailb, (char *) "MAILB", (char *) "mailbox-related data (deprecated)"}, |
473 | {ns_t_maila, (char *) "MAILA", (char *) "mail agent (deprecated)"}, | |
474 | {ns_t_naptr, (char *) "NAPTR", (char *) "URN Naming Authority"}, | |
475 | {ns_t_kx, (char *) "KX", (char *) "Key Exchange"}, | |
476 | {ns_t_cert, (char *) "CERT", (char *) "Certificate"}, | |
477 | {ns_t_any, (char *) "ANY", (char *) "\"any\""}, | |
4f157746 | 478 | {0, NULL, NULL}, /* Padding to preserve ABI. */ |
bf8c5424 | 479 | {0, NULL, NULL} |
b43b13ac | 480 | }; |
32738a22 | 481 | libresolv_hidden_data_def (__p_type_syms) |
b43b13ac UD |
482 | |
483 | /* | |
484 | * Names of DNS rcodes. | |
485 | */ | |
6f9d8e68 | 486 | const struct res_sym __p_rcode_syms[] attribute_hidden = { |
bf8c5424 FW |
487 | {ns_r_noerror, (char *) "NOERROR", (char *) "no error"}, |
488 | {ns_r_formerr, (char *) "FORMERR", (char *) "format error"}, | |
489 | {ns_r_servfail, (char *) "SERVFAIL", (char *) "server failed"}, | |
490 | {ns_r_nxdomain, (char *) "NXDOMAIN", (char *) "no such domain name"}, | |
491 | {ns_r_notimpl, (char *) "NOTIMP", (char *) "not implemented"}, | |
492 | {ns_r_refused, (char *) "REFUSED", (char *) "refused"}, | |
493 | {ns_r_yxdomain, (char *) "YXDOMAIN", (char *) "domain name exists"}, | |
494 | {ns_r_yxrrset, (char *) "YXRRSET", (char *) "rrset exists"}, | |
495 | {ns_r_nxrrset, (char *) "NXRRSET", (char *) "rrset doesn't exist"}, | |
496 | {ns_r_notauth, (char *) "NOTAUTH", (char *) "not authoritative"}, | |
497 | {ns_r_notzone, (char *) "NOTZONE", (char *) "Not in zone"}, | |
498 | {ns_r_max, (char *) "", (char *) ""}, | |
499 | {ns_r_badsig, (char *) "BADSIG", (char *) "bad signature"}, | |
500 | {ns_r_badkey, (char *) "BADKEY", (char *) "bad key"}, | |
501 | {ns_r_badtime, (char *) "BADTIME", (char *) "bad time"}, | |
502 | {0, NULL, NULL} | |
df21c858 UD |
503 | }; |
504 | ||
505 | int | |
b43b13ac UD |
506 | sym_ston(const struct res_sym *syms, const char *name, int *success) { |
507 | for ((void)NULL; syms->name != 0; syms++) { | |
df21c858 UD |
508 | if (strcasecmp (name, syms->name) == 0) { |
509 | if (success) | |
510 | *success = 1; | |
511 | return (syms->number); | |
512 | } | |
513 | } | |
514 | if (success) | |
515 | *success = 0; | |
b43b13ac | 516 | return (syms->number); /* The default value. */ |
df21c858 UD |
517 | } |
518 | ||
519 | const char * | |
b43b13ac | 520 | sym_ntos(const struct res_sym *syms, int number, int *success) { |
df21c858 UD |
521 | static char unname[20]; |
522 | ||
b43b13ac | 523 | for ((void)NULL; syms->name != 0; syms++) { |
e685e07d | 524 | if (number == syms->number) { |
df21c858 UD |
525 | if (success) |
526 | *success = 1; | |
527 | return (syms->name); | |
528 | } | |
529 | } | |
530 | ||
b43b13ac | 531 | sprintf(unname, "%d", number); /* XXX nonreentrant */ |
df21c858 UD |
532 | if (success) |
533 | *success = 0; | |
534 | return (unname); | |
535 | } | |
6f9d8e68 | 536 | libresolv_hidden_def (sym_ntos) |
df21c858 | 537 | |
df21c858 | 538 | const char * |
b43b13ac | 539 | sym_ntop(const struct res_sym *syms, int number, int *success) { |
df21c858 UD |
540 | static char unname[20]; |
541 | ||
b43b13ac | 542 | for ((void)NULL; syms->name != 0; syms++) { |
df21c858 UD |
543 | if (number == syms->number) { |
544 | if (success) | |
545 | *success = 1; | |
546 | return (syms->humanname); | |
547 | } | |
548 | } | |
b43b13ac | 549 | sprintf(unname, "%d", number); /* XXX nonreentrant */ |
df21c858 UD |
550 | if (success) |
551 | *success = 0; | |
552 | return (unname); | |
553 | } | |
554 | ||
28f540f4 | 555 | /* |
b43b13ac | 556 | * Return a string for the type. |
28f540f4 RM |
557 | */ |
558 | const char * | |
b43b13ac UD |
559 | p_type(int type) { |
560 | return (sym_ntos(__p_type_syms, type, (int *)0)); | |
28f540f4 | 561 | } |
6f9d8e68 | 562 | libresolv_hidden_def (p_type) |
28f540f4 RM |
563 | |
564 | /* | |
b43b13ac | 565 | * Return a string for the type. |
28f540f4 | 566 | */ |
a7ff1da8 | 567 | static const char * |
b43b13ac UD |
568 | p_section(int section, int opcode) { |
569 | const struct res_sym *symbols; | |
570 | ||
571 | switch (opcode) { | |
572 | case ns_o_update: | |
573 | symbols = __p_update_section_syms; | |
574 | break; | |
575 | default: | |
576 | symbols = __p_default_section_syms; | |
577 | break; | |
578 | } | |
579 | return (sym_ntos(symbols, section, (int *)0)); | |
580 | } | |
581 | ||
582 | /* | |
583 | * Return a mnemonic for class. | |
584 | */ | |
585 | const char * | |
586 | p_class(int class) { | |
587 | return (sym_ntos(__p_class_syms, class, (int *)0)); | |
28f540f4 | 588 | } |
6f9d8e68 | 589 | libresolv_hidden_def (p_class) |
28f540f4 RM |
590 | |
591 | /* | |
592 | * Return a mnemonic for an option | |
593 | */ | |
594 | const char * | |
b43b13ac | 595 | p_option(u_long option) { |
3d61b63c RM |
596 | static char nbuf[40]; |
597 | ||
28f540f4 RM |
598 | switch (option) { |
599 | case RES_INIT: return "init"; | |
600 | case RES_DEBUG: return "debug"; | |
c3ed8088 | 601 | case RES_USEVC: return "use-vc"; |
28f540f4 RM |
602 | case RES_IGNTC: return "igntc"; |
603 | case RES_RECURSE: return "recurs"; | |
604 | case RES_DEFNAMES: return "defnam"; | |
605 | case RES_STAYOPEN: return "styopn"; | |
606 | case RES_DNSRCH: return "dnsrch"; | |
607 | case RES_INSECURE1: return "insecure1"; | |
608 | case RES_INSECURE2: return "insecure2"; | |
b1a36ceb | 609 | case RES_NOALIASES: return "noaliases"; |
b76e0659 | 610 | case DEPRECATED_RES_USE_INET6: return "inet6"; |
ee778b56 | 611 | case RES_ROTATE: return "rotate"; |
2d0671cb | 612 | case RES_USE_EDNS0: return "edns0"; |
b1a36ceb AJ |
613 | case RES_SNGLKUP: return "single-request"; |
614 | case RES_SNGLKUPREOP: return "single-request-reopen"; | |
2d0671cb | 615 | case RES_USE_DNSSEC: return "dnssec"; |
f87dfb1f | 616 | case RES_NOTLDQUERY: return "no-tld-query"; |
aef16cc8 | 617 | case RES_NORELOAD: return "no-reload"; |
b43b13ac | 618 | /* XXX nonreentrant */ |
3d61b63c RM |
619 | default: sprintf(nbuf, "?0x%lx?", (u_long)option); |
620 | return (nbuf); | |
28f540f4 RM |
621 | } |
622 | } | |
6f9d8e68 | 623 | libresolv_hidden_def (p_option) |
28f540f4 RM |
624 | |
625 | /* | |
b43b13ac | 626 | * Return a mnemonic for a time to live. |
28f540f4 | 627 | */ |
845dcb57 | 628 | const char * |
d9fee042 | 629 | p_time(uint32_t value) { |
b43b13ac | 630 | static char nbuf[40]; /* XXX nonreentrant */ |
28f540f4 | 631 | |
b43b13ac UD |
632 | if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0) |
633 | sprintf(nbuf, "%u", value); | |
28f540f4 RM |
634 | return (nbuf); |
635 | } | |
ee188d55 | 636 | |
b43b13ac UD |
637 | /* |
638 | * Return a string for the rcode. | |
639 | */ | |
640 | const char * | |
641 | p_rcode(int rcode) { | |
642 | return (sym_ntos(__p_rcode_syms, rcode, (int *)0)); | |
643 | } | |
6f9d8e68 | 644 | libresolv_hidden_def (p_rcode) |
b43b13ac | 645 | |
ee188d55 RM |
646 | /* |
647 | * routines to convert between on-the-wire RR format and zone file format. | |
648 | * Does not contain conversion to/from decimal degrees; divide or multiply | |
649 | * by 60*60*1000 for that. | |
650 | */ | |
651 | ||
6f9d8e68 UD |
652 | static const unsigned int poweroften[10]= |
653 | { 1, 10, 100, 1000, 10000, 100000, | |
654 | 1000000,10000000,100000000,1000000000}; | |
ee188d55 RM |
655 | |
656 | /* takes an XeY precision/size value, returns a string representation. */ | |
657 | static const char * | |
d9fee042 | 658 | precsize_ntoa (uint8_t prec) |
ee188d55 | 659 | { |
b43b13ac | 660 | static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */ |
ee188d55 RM |
661 | unsigned long val; |
662 | int mantissa, exponent; | |
663 | ||
664 | mantissa = (int)((prec >> 4) & 0x0f) % 10; | |
665 | exponent = (int)((prec >> 0) & 0x0f) % 10; | |
666 | ||
667 | val = mantissa * poweroften[exponent]; | |
668 | ||
669 | (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100); | |
670 | return (retbuf); | |
671 | } | |
672 | ||
673 | /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ | |
d9fee042 | 674 | static uint8_t |
db0a00d3 | 675 | precsize_aton (const char **strptr) |
ee188d55 | 676 | { |
b43b13ac | 677 | unsigned int mval = 0, cmval = 0; |
d9fee042 | 678 | uint8_t retval = 0; |
0a463fa9 | 679 | const char *cp; |
b43b13ac UD |
680 | int exponent; |
681 | int mantissa; | |
ee188d55 RM |
682 | |
683 | cp = *strptr; | |
684 | ||
b43b13ac UD |
685 | while (isdigit(*cp)) |
686 | mval = mval * 10 + (*cp++ - '0'); | |
687 | ||
688 | if (*cp == '.') { /* centimeters */ | |
ee188d55 RM |
689 | cp++; |
690 | if (isdigit(*cp)) { | |
b43b13ac | 691 | cmval = (*cp++ - '0') * 10; |
ee188d55 | 692 | if (isdigit(*cp)) { |
b43b13ac | 693 | cmval += (*cp++ - '0'); |
ee188d55 RM |
694 | } |
695 | } | |
696 | } | |
b43b13ac UD |
697 | cmval = (mval * 100) + cmval; |
698 | ||
699 | for (exponent = 0; exponent < 9; exponent++) | |
700 | if (cmval < poweroften[exponent+1]) | |
701 | break; | |
702 | ||
703 | mantissa = cmval / poweroften[exponent]; | |
704 | if (mantissa > 9) | |
705 | mantissa = 9; | |
ee188d55 RM |
706 | |
707 | retval = (mantissa << 4) | exponent; | |
b43b13ac | 708 | |
ee188d55 | 709 | *strptr = cp; |
b43b13ac | 710 | |
ee188d55 RM |
711 | return (retval); |
712 | } | |
713 | ||
714 | /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ | |
d9fee042 | 715 | static uint32_t |
db0a00d3 | 716 | latlon2ul (const char **latlonstrptr, int *which) |
ee188d55 | 717 | { |
0a463fa9 | 718 | const char *cp; |
d9fee042 | 719 | uint32_t retval; |
ee188d55 RM |
720 | int deg = 0, min = 0, secs = 0, secsfrac = 0; |
721 | ||
722 | cp = *latlonstrptr; | |
723 | ||
724 | while (isdigit(*cp)) | |
725 | deg = deg * 10 + (*cp++ - '0'); | |
726 | ||
727 | while (isspace(*cp)) | |
728 | cp++; | |
729 | ||
730 | if (!(isdigit(*cp))) | |
731 | goto fndhemi; | |
732 | ||
733 | while (isdigit(*cp)) | |
734 | min = min * 10 + (*cp++ - '0'); | |
735 | ||
736 | while (isspace(*cp)) | |
737 | cp++; | |
738 | ||
739 | if (!(isdigit(*cp))) | |
740 | goto fndhemi; | |
741 | ||
742 | while (isdigit(*cp)) | |
743 | secs = secs * 10 + (*cp++ - '0'); | |
744 | ||
745 | if (*cp == '.') { /* decimal seconds */ | |
746 | cp++; | |
747 | if (isdigit(*cp)) { | |
748 | secsfrac = (*cp++ - '0') * 100; | |
749 | if (isdigit(*cp)) { | |
750 | secsfrac += (*cp++ - '0') * 10; | |
751 | if (isdigit(*cp)) { | |
752 | secsfrac += (*cp++ - '0'); | |
753 | } | |
754 | } | |
755 | } | |
756 | } | |
757 | ||
758 | while (!isspace(*cp)) /* if any trailing garbage */ | |
759 | cp++; | |
760 | ||
761 | while (isspace(*cp)) | |
762 | cp++; | |
763 | ||
764 | fndhemi: | |
765 | switch (*cp) { | |
766 | case 'N': case 'n': | |
767 | case 'E': case 'e': | |
768 | retval = ((unsigned)1<<31) | |
769 | + (((((deg * 60) + min) * 60) + secs) * 1000) | |
770 | + secsfrac; | |
771 | break; | |
772 | case 'S': case 's': | |
773 | case 'W': case 'w': | |
774 | retval = ((unsigned)1<<31) | |
775 | - (((((deg * 60) + min) * 60) + secs) * 1000) | |
776 | - secsfrac; | |
777 | break; | |
778 | default: | |
779 | retval = 0; /* invalid value -- indicates error */ | |
780 | break; | |
781 | } | |
782 | ||
783 | switch (*cp) { | |
784 | case 'N': case 'n': | |
785 | case 'S': case 's': | |
786 | *which = 1; /* latitude */ | |
787 | break; | |
788 | case 'E': case 'e': | |
789 | case 'W': case 'w': | |
790 | *which = 2; /* longitude */ | |
791 | break; | |
792 | default: | |
793 | *which = 0; /* error */ | |
794 | break; | |
795 | } | |
796 | ||
797 | cp++; /* skip the hemisphere */ | |
798 | ||
799 | while (!isspace(*cp)) /* if any trailing garbage */ | |
800 | cp++; | |
801 | ||
802 | while (isspace(*cp)) /* move to next field */ | |
803 | cp++; | |
804 | ||
805 | *latlonstrptr = cp; | |
806 | ||
807 | return (retval); | |
808 | } | |
809 | ||
810 | /* converts a zone file representation in a string to an RDATA on-the-wire | |
811 | * representation. */ | |
812 | int | |
9d46370c | 813 | loc_aton (const char *ascii, u_char *binary) |
ee188d55 RM |
814 | { |
815 | const char *cp, *maxcp; | |
816 | u_char *bcp; | |
817 | ||
d9fee042 JM |
818 | uint32_t latit = 0, longit = 0, alt = 0; |
819 | uint32_t lltemp1 = 0, lltemp2 = 0; | |
ee188d55 | 820 | int altmeters = 0, altfrac = 0, altsign = 1; |
d9fee042 JM |
821 | uint8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */ |
822 | uint8_t vp = 0x13; /* default = 1e3 cm = 10.00m */ | |
823 | uint8_t siz = 0x12; /* default = 1e2 cm = 1.00m */ | |
ee188d55 RM |
824 | int which1 = 0, which2 = 0; |
825 | ||
826 | cp = ascii; | |
827 | maxcp = cp + strlen(ascii); | |
828 | ||
829 | lltemp1 = latlon2ul(&cp, &which1); | |
830 | ||
831 | lltemp2 = latlon2ul(&cp, &which2); | |
832 | ||
833 | switch (which1 + which2) { | |
834 | case 3: /* 1 + 2, the only valid combination */ | |
835 | if ((which1 == 1) && (which2 == 2)) { /* normal case */ | |
836 | latit = lltemp1; | |
837 | longit = lltemp2; | |
838 | } else if ((which1 == 2) && (which2 == 1)) { /* reversed */ | |
839 | longit = lltemp1; | |
840 | latit = lltemp2; | |
841 | } else { /* some kind of brokenness */ | |
df21c858 | 842 | return (0); |
ee188d55 RM |
843 | } |
844 | break; | |
845 | default: /* we didn't get one of each */ | |
df21c858 | 846 | return (0); |
ee188d55 RM |
847 | } |
848 | ||
849 | /* altitude */ | |
850 | if (*cp == '-') { | |
851 | altsign = -1; | |
852 | cp++; | |
853 | } | |
0a463fa9 | 854 | |
ee188d55 RM |
855 | if (*cp == '+') |
856 | cp++; | |
857 | ||
858 | while (isdigit(*cp)) | |
859 | altmeters = altmeters * 10 + (*cp++ - '0'); | |
860 | ||
861 | if (*cp == '.') { /* decimal meters */ | |
862 | cp++; | |
863 | if (isdigit(*cp)) { | |
864 | altfrac = (*cp++ - '0') * 10; | |
865 | if (isdigit(*cp)) { | |
866 | altfrac += (*cp++ - '0'); | |
867 | } | |
868 | } | |
869 | } | |
870 | ||
871 | alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); | |
872 | ||
873 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | |
874 | cp++; | |
875 | ||
876 | while (isspace(*cp) && (cp < maxcp)) | |
877 | cp++; | |
878 | ||
879 | if (cp >= maxcp) | |
880 | goto defaults; | |
881 | ||
882 | siz = precsize_aton(&cp); | |
0a463fa9 | 883 | |
ee188d55 RM |
884 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ |
885 | cp++; | |
886 | ||
887 | while (isspace(*cp) && (cp < maxcp)) | |
888 | cp++; | |
889 | ||
890 | if (cp >= maxcp) | |
891 | goto defaults; | |
892 | ||
893 | hp = precsize_aton(&cp); | |
894 | ||
895 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | |
896 | cp++; | |
897 | ||
898 | while (isspace(*cp) && (cp < maxcp)) | |
899 | cp++; | |
900 | ||
901 | if (cp >= maxcp) | |
902 | goto defaults; | |
903 | ||
904 | vp = precsize_aton(&cp); | |
905 | ||
906 | defaults: | |
907 | ||
908 | bcp = binary; | |
d9fee042 | 909 | *bcp++ = (uint8_t) 0; /* version byte */ |
ee188d55 RM |
910 | *bcp++ = siz; |
911 | *bcp++ = hp; | |
912 | *bcp++ = vp; | |
913 | PUTLONG(latit,bcp); | |
914 | PUTLONG(longit,bcp); | |
915 | PUTLONG(alt,bcp); | |
0a463fa9 | 916 | |
ee188d55 RM |
917 | return (16); /* size of RR in octets */ |
918 | } | |
919 | ||
920 | /* takes an on-the-wire LOC RR and formats it in a human readable format. */ | |
845dcb57 | 921 | const char * |
9d46370c | 922 | loc_ntoa (const u_char *binary, char *ascii) |
ee188d55 | 923 | { |
4442d58f | 924 | static const char error[] = "?"; |
b43b13ac UD |
925 | static char tmpbuf[sizeof |
926 | "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; | |
927 | const u_char *cp = binary; | |
ee188d55 RM |
928 | |
929 | int latdeg, latmin, latsec, latsecfrac; | |
930 | int longdeg, longmin, longsec, longsecfrac; | |
931 | char northsouth, eastwest; | |
932 | int altmeters, altfrac, altsign; | |
933 | ||
d9fee042 | 934 | const uint32_t referencealt = 100000 * 100; |
ee188d55 RM |
935 | |
936 | int32_t latval, longval, altval; | |
d9fee042 JM |
937 | uint32_t templ; |
938 | uint8_t sizeval, hpval, vpval, versionval; | |
0a463fa9 | 939 | |
ee188d55 RM |
940 | char *sizestr, *hpstr, *vpstr; |
941 | ||
942 | versionval = *cp++; | |
943 | ||
b43b13ac UD |
944 | if (ascii == NULL) |
945 | ascii = tmpbuf; | |
946 | ||
ee188d55 | 947 | if (versionval) { |
b43b13ac | 948 | (void) sprintf(ascii, "; error: unknown LOC RR version"); |
ee188d55 RM |
949 | return (ascii); |
950 | } | |
951 | ||
952 | sizeval = *cp++; | |
953 | ||
954 | hpval = *cp++; | |
955 | vpval = *cp++; | |
956 | ||
957 | GETLONG(templ, cp); | |
958 | latval = (templ - ((unsigned)1<<31)); | |
959 | ||
960 | GETLONG(templ, cp); | |
961 | longval = (templ - ((unsigned)1<<31)); | |
962 | ||
963 | GETLONG(templ, cp); | |
b43b13ac | 964 | if (templ < referencealt) { /* below WGS 84 spheroid */ |
ee188d55 RM |
965 | altval = referencealt - templ; |
966 | altsign = -1; | |
967 | } else { | |
968 | altval = templ - referencealt; | |
969 | altsign = 1; | |
970 | } | |
971 | ||
972 | if (latval < 0) { | |
973 | northsouth = 'S'; | |
974 | latval = -latval; | |
975 | } else | |
976 | northsouth = 'N'; | |
977 | ||
978 | latsecfrac = latval % 1000; | |
979 | latval = latval / 1000; | |
980 | latsec = latval % 60; | |
981 | latval = latval / 60; | |
982 | latmin = latval % 60; | |
983 | latval = latval / 60; | |
984 | latdeg = latval; | |
985 | ||
986 | if (longval < 0) { | |
987 | eastwest = 'W'; | |
988 | longval = -longval; | |
989 | } else | |
990 | eastwest = 'E'; | |
991 | ||
992 | longsecfrac = longval % 1000; | |
993 | longval = longval / 1000; | |
994 | longsec = longval % 60; | |
995 | longval = longval / 60; | |
996 | longmin = longval % 60; | |
997 | longval = longval / 60; | |
998 | longdeg = longval; | |
999 | ||
1000 | altfrac = altval % 100; | |
1001 | altmeters = (altval / 100) * altsign; | |
1002 | ||
1003 | if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL) | |
4442d58f | 1004 | sizestr = (char *) error; |
ee188d55 | 1005 | if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL) |
4442d58f | 1006 | hpstr = (char *) error; |
ee188d55 | 1007 | if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL) |
4442d58f | 1008 | vpstr = (char *) error; |
ee188d55 RM |
1009 | |
1010 | sprintf(ascii, | |
1011 | "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm", | |
1012 | latdeg, latmin, latsec, latsecfrac, northsouth, | |
1013 | longdeg, longmin, longsec, longsecfrac, eastwest, | |
1014 | altmeters, altfrac, sizestr, hpstr, vpstr); | |
1015 | ||
4442d58f | 1016 | if (sizestr != (char *) error) |
ee188d55 | 1017 | free(sizestr); |
4442d58f | 1018 | if (hpstr != (char *) error) |
ee188d55 | 1019 | free(hpstr); |
4442d58f | 1020 | if (vpstr != (char *) error) |
ee188d55 RM |
1021 | free(vpstr); |
1022 | ||
1023 | return (ascii); | |
1024 | } | |
6f9d8e68 | 1025 | libresolv_hidden_def (loc_ntoa) |
df21c858 UD |
1026 | |
1027 | ||
1028 | /* Return the number of DNS hierarchy levels in the name. */ | |
1029 | int | |
b43b13ac | 1030 | dn_count_labels(const char *name) { |
df21c858 UD |
1031 | int i, len, count; |
1032 | ||
1033 | len = strlen(name); | |
b43b13ac UD |
1034 | for (i = 0, count = 0; i < len; i++) { |
1035 | /* XXX need to check for \. or use named's nlabels(). */ | |
df21c858 UD |
1036 | if (name[i] == '.') |
1037 | count++; | |
1038 | } | |
1039 | ||
1040 | /* don't count initial wildcard */ | |
1041 | if (name[0] == '*') | |
1042 | if (count) | |
1043 | count--; | |
1044 | ||
1045 | /* don't count the null label for root. */ | |
1046 | /* if terminating '.' not found, must adjust */ | |
1047 | /* count to include last label */ | |
1048 | if (len > 0 && name[len-1] != '.') | |
1049 | count++; | |
1050 | return (count); | |
1051 | } | |
cd5743fd | 1052 | libresolv_hidden_def (__dn_count_labels) |
df21c858 UD |
1053 | |
1054 | ||
754034c4 | 1055 | #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_27) |
0a463fa9 RM |
1056 | /* |
1057 | * Make dates expressed in seconds-since-Jan-1-1970 easy to read. | |
df21c858 | 1058 | * SIG records are required to be printed like this, by the Secure DNS RFC. |
f120cda6 JM |
1059 | * This is an obsolescent function and does not handle dates outside the |
1060 | * signed 32-bit range. | |
df21c858 UD |
1061 | */ |
1062 | char * | |
754034c4 | 1063 | __p_secstodate (u_long secs) { |
b43b13ac UD |
1064 | /* XXX nonreentrant */ |
1065 | static char output[15]; /* YYYYMMDDHHMMSS and null */ | |
df21c858 | 1066 | time_t clock = secs; |
b43b13ac | 1067 | struct tm *time; |
0a463fa9 | 1068 | |
e685e07d | 1069 | struct tm timebuf; |
f120cda6 JM |
1070 | /* The call to __gmtime_r can never produce a year overflowing |
1071 | the range of int, given the check on SECS, but check for a | |
1072 | NULL return anyway to avoid a null pointer dereference in | |
1073 | case there are any other unspecified errors. */ | |
1074 | if (secs > 0x7fffffff | |
1075 | || (time = __gmtime_r (&clock, &timebuf)) == NULL) { | |
1076 | strcpy (output, "<overflow>"); | |
1077 | __set_errno (EOVERFLOW); | |
1078 | return output; | |
1079 | } | |
b43b13ac UD |
1080 | time->tm_year += 1900; |
1081 | time->tm_mon += 1; | |
f120cda6 JM |
1082 | /* The struct tm fields, given the above range check, |
1083 | must have values that mean this sprintf exactly fills the | |
1084 | buffer. But as of GCC 8 of 2017-11-21, GCC cannot tell | |
1085 | that, even given range checks on all fields with | |
1086 | __builtin_unreachable called for out-of-range values. */ | |
1087 | DIAG_PUSH_NEEDS_COMMENT; | |
754034c4 | 1088 | # if __GNUC_PREREQ (7, 0) |
f120cda6 | 1089 | DIAG_IGNORE_NEEDS_COMMENT (8, "-Wformat-overflow="); |
754034c4 | 1090 | # endif |
df21c858 | 1091 | sprintf(output, "%04d%02d%02d%02d%02d%02d", |
b43b13ac UD |
1092 | time->tm_year, time->tm_mon, time->tm_mday, |
1093 | time->tm_hour, time->tm_min, time->tm_sec); | |
f120cda6 | 1094 | DIAG_POP_NEEDS_COMMENT; |
df21c858 UD |
1095 | return (output); |
1096 | } | |
754034c4 JM |
1097 | compat_symbol (libresolv, __p_secstodate, __p_secstodate, GLIBC_2_0); |
1098 | #endif |