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