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