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