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