blob: f5a0aff6f0198a0d6d58861ccfd8d325088ea00a [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $ */
2
3/*
4 * Portions Copyright (C) 2004, 2005, 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 1996-2003 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/*
21 * Copyright (c) 1985
22 * The Regents of the University of California. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52
53/*
54 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies, and that
59 * the name of Digital Equipment Corporation not be used in advertising or
60 * publicity pertaining to distribution of the document or software without
61 * specific, written prior permission.
62 *
63 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
64 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
65 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
66 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
67 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
68 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
69 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
70 * SOFTWARE.
71 */
72
73/*
74 * Portions Copyright (c) 1995 by International Business Machines, Inc.
75 *
76 * International Business Machines, Inc. (hereinafter called IBM) grants
77 * permission under its copyrights to use, copy, modify, and distribute this
78 * Software with or without fee, provided that the above copyright notice and
79 * all paragraphs of this notice appear in all copies, and that the name of IBM
80 * not be used in connection with the marketing of any product incorporating
81 * the Software or modifications thereof, without specific, written prior
82 * permission.
83 *
84 * To the extent it has a right to do so, IBM grants an immunity from suit
85 * under its patents, if any, for the use, sale or manufacture of products to
86 * the extent that such products are used for performing Domain Name System
87 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
88 * granted for any product per se or for any other function of any product.
89 *
90 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
91 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
92 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
93 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
94 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
95 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
96 */
97
Bernie Innocenti55864192018-08-30 04:05:20 +090098#include <sys/param.h>
99#include <sys/socket.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900100#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900101
Bernie Innocenti55864192018-08-30 04:05:20 +0900102#include <arpa/inet.h>
103#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900104#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900105
106#include <ctype.h>
107#include <errno.h>
108#include <math.h>
109#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900110#include <stdio.h>
111#include <stdlib.h>
112#include <string.h>
113#include <strings.h>
114#include <time.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +0900115
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900117
Bernie Innocenti189eb502018-10-01 23:10:18 +0900118struct res_sym {
119 int number; /* Identifying number, like T_MX */
120 const char* name; /* Its symbolic name, like "MX" */
121 const char* humanname; /* Its fun name, like "mail exchanger" */
122};
Bernie Innocentif89b3512018-08-30 07:34:37 +0900123
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag,
125 FILE* file) {
126 int n, sflag, rrnum;
127 int buflen = 2048;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900128 ns_opcode opcode;
129 ns_rr rr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900130
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900131 /*
132 * Print answer records.
133 */
134 sflag = (int) (statp->pfcode & pflag);
135 if (statp->pfcode && !sflag) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900137 char* buf = (char*) malloc((size_t) buflen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 if (buf == NULL) {
139 fprintf(file, ";; memory allocation failure\n");
140 return;
141 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900142
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900143 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
144 rrnum = 0;
145 for (;;) {
146 if (ns_parserr(handle, section, rrnum, &rr)) {
147 if (errno != ENODEV)
148 fprintf(file, ";; ns_parserr: %s\n", strerror(errno));
149 else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
150 putc('\n', file);
151 goto cleanup;
152 }
153 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
154 fprintf(file, ";; %s SECTION:\n", p_section(section, opcode));
155 if (section == ns_s_qd)
156 fprintf(file, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), p_type(ns_rr_type(rr)),
157 p_class(ns_rr_class(rr)));
158 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
159 size_t rdatalen, ttl;
160 uint16_t optcode, optlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900161
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 rdatalen = ns_rr_rdlen(rr);
163 ttl = ns_rr_ttl(rr);
164 fprintf(file, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
165 ns_rr_class(rr), ttl & 0xffff);
166 while (rdatalen >= 4) {
167 const u_char* cp = ns_rr_rdata(rr);
168 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900169
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170 GETSHORT(optcode, cp);
171 GETSHORT(optlen, cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900172
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173 if (optcode == NS_OPT_NSID) {
174 fputs("; NSID: ", file);
175 if (optlen == 0) {
176 fputs("; NSID\n", file);
177 } else {
178 fputs("; NSID: ", file);
179 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
180 fputs(" (", file);
181 for (i = 0; i < optlen; i++)
182 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
183 fputs(")\n", file);
184 }
185 } else {
186 if (optlen == 0) {
187 fprintf(file, "; OPT=%u\n", optcode);
188 } else {
189 fprintf(file, "; OPT=%u: ", optcode);
190 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
191 fputs(" (", file);
192 for (i = 0; i < optlen; i++)
193 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
194 fputs(")\n", file);
195 }
196 }
197 rdatalen -= 4 + optlen;
198 }
199 } else {
200 n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
201 if (n < 0) {
202 if (errno == ENOSPC) {
203 free(buf);
204 buf = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900205 if (buflen < 131072) {
206 buf = (char*) malloc((size_t)(buflen += 1024));
207 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 if (buf == NULL) {
209 fprintf(file, ";; memory allocation failure\n");
210 return;
211 }
212 continue;
213 }
214 fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno));
215 goto cleanup;
216 }
217 fputs(buf, file);
218 fputc('\n', file);
219 }
220 rrnum++;
221 }
222cleanup:
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900223 free(buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900224}
225
226/*
227 * Print the contents of a query.
228 * This is intended to be primarily a debugging routine.
229 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900230void res_pquery(const res_state statp, const u_char* msg, int len, FILE* file) {
231 ns_msg handle;
232 int qdcount, ancount, nscount, arcount;
233 u_int opcode, rcode, id;
Bernie Innocenti55864192018-08-30 04:05:20 +0900234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235 if (ns_initparse(msg, len, &handle) < 0) {
236 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
237 return;
238 }
239 opcode = ns_msg_getflag(handle, ns_f_opcode);
240 rcode = ns_msg_getflag(handle, ns_f_rcode);
241 id = ns_msg_id(handle);
242 qdcount = ns_msg_count(handle, ns_s_qd);
243 ancount = ns_msg_count(handle, ns_s_an);
244 nscount = ns_msg_count(handle, ns_s_ns);
245 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti55864192018-08-30 04:05:20 +0900246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 /*
248 * Print header fields.
249 */
250 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
251 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
252 p_rcode((int) rcode), id);
253 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) putc(';', file);
254 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
255 fprintf(file, "; flags:");
256 if (ns_msg_getflag(handle, ns_f_qr)) fprintf(file, " qr");
257 if (ns_msg_getflag(handle, ns_f_aa)) fprintf(file, " aa");
258 if (ns_msg_getflag(handle, ns_f_tc)) fprintf(file, " tc");
259 if (ns_msg_getflag(handle, ns_f_rd)) fprintf(file, " rd");
260 if (ns_msg_getflag(handle, ns_f_ra)) fprintf(file, " ra");
261 if (ns_msg_getflag(handle, ns_f_z)) fprintf(file, " ??");
262 if (ns_msg_getflag(handle, ns_f_ad)) fprintf(file, " ad");
263 if (ns_msg_getflag(handle, ns_f_cd)) fprintf(file, " cd");
264 }
265 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
266 fprintf(file, "; %s: %d", p_section(ns_s_qd, (int) opcode), qdcount);
267 fprintf(file, ", %s: %d", p_section(ns_s_an, (int) opcode), ancount);
268 fprintf(file, ", %s: %d", p_section(ns_s_ns, (int) opcode), nscount);
269 fprintf(file, ", %s: %d", p_section(ns_s_ar, (int) opcode), arcount);
270 }
271 if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
272 putc('\n', file);
273 }
274 /*
275 * Print the various sections.
276 */
277 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
278 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
279 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
280 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
281 if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) putc('\n', file);
Bernie Innocenti55864192018-08-30 04:05:20 +0900282}
283
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900284const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) {
285 char name[MAXDNAME];
286 int n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900287
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900288 if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL);
289 if (name[0] == '\0')
290 putc('.', file);
291 else
292 fputs(name, file);
293 return (cp + n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900294}
295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) {
297 return (p_cdnname(cp, msg, PACKETSZ, file));
Bernie Innocenti55864192018-08-30 04:05:20 +0900298}
299
300/* Return a fully-qualified domain name from a compressed name (with
301 length supplied). */
302
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900303const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) {
304 int n;
305 size_t newlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL);
308 newlen = strlen(name);
309 if (newlen == 0 || name[newlen - 1] != '.') {
310 if ((int) newlen + 1 >= namelen) /* Lack space for final dot */
311 return (NULL);
312 else
313 strcpy(name + newlen, ".");
314 }
315 return (cp + n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900316}
317
318/* XXX: the rest of these functions need to become length-limited, too. */
319
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900320const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) {
321 char name[MAXDNAME];
322 const u_char* n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900324 n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name);
325 if (n == NULL) return (NULL);
326 fputs(name, file);
327 return (n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900328}
329
330/*
331 * Names of RR classes and qclasses. Classes and qclasses are the same, except
332 * that C_ANY is a qclass but not a class. (You can ask for records of class
333 * C_ANY, but you can't have any records of that class in the database.)
334 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900335static const struct res_sym p_class_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
337 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
338 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900339
340/*
341 * Names of message sections.
342 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900343static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
344 {ns_s_an, "ANSWER", (char*) 0},
345 {ns_s_ns, "AUTHORITY", (char*) 0},
346 {ns_s_ar, "ADDITIONAL", (char*) 0},
347 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900348
Bernie Innocenti189eb502018-10-01 23:10:18 +0900349static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
350 {S_PREREQ, "PREREQUISITE", (char*) 0},
351 {S_UPDATE, "UPDATE", (char*) 0},
352 {S_ADDT, "ADDITIONAL", (char*) 0},
353 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
Bernie Innocenti55864192018-08-30 04:05:20 +0900355/*
356 * Names of RR types and qtypes. Types and qtypes are the same, except
357 * that T_ANY is a qtype but not a type. (You can ask for records of type
358 * T_ANY, but you can't have any records of that type in the database.)
359 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900360const struct res_sym p_type_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900361 {ns_t_a, "A", "address"},
362 {ns_t_ns, "NS", "name server"},
363 {ns_t_md, "MD", "mail destination (deprecated)"},
364 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
365 {ns_t_cname, "CNAME", "canonical name"},
366 {ns_t_soa, "SOA", "start of authority"},
367 {ns_t_mb, "MB", "mailbox"},
368 {ns_t_mg, "MG", "mail group member"},
369 {ns_t_mr, "MR", "mail rename"},
370 {ns_t_null, "NULL", "null"},
371 {ns_t_wks, "WKS", "well-known service (deprecated)"},
372 {ns_t_ptr, "PTR", "domain name pointer"},
373 {ns_t_hinfo, "HINFO", "host information"},
374 {ns_t_minfo, "MINFO", "mailbox information"},
375 {ns_t_mx, "MX", "mail exchanger"},
376 {ns_t_txt, "TXT", "text"},
377 {ns_t_rp, "RP", "responsible person"},
378 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
379 {ns_t_x25, "X25", "X25 address"},
380 {ns_t_isdn, "ISDN", "ISDN address"},
381 {ns_t_rt, "RT", "router"},
382 {ns_t_nsap, "NSAP", "nsap address"},
383 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
384 {ns_t_sig, "SIG", "signature"},
385 {ns_t_key, "KEY", "key"},
386 {ns_t_px, "PX", "mapping information"},
387 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
388 {ns_t_aaaa, "AAAA", "IPv6 address"},
389 {ns_t_loc, "LOC", "location"},
390 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
391 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
392 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
393 {ns_t_srv, "SRV", "server selection"},
394 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
395 {ns_t_naptr, "NAPTR", "naptr"},
396 {ns_t_kx, "KX", "key exchange"},
397 {ns_t_cert, "CERT", "certificate"},
398 {ns_t_a6, "A", "IPv6 address (experminental)"},
399 {ns_t_dname, "DNAME", "non-terminal redirection"},
400 {ns_t_opt, "OPT", "opt"},
401 {ns_t_apl, "apl", "apl"},
402 {ns_t_ds, "DS", "delegation signer"},
403 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
404 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
405 {ns_t_rrsig, "RRSIG", "rrsig"},
406 {ns_t_nsec, "NSEC", "nsec"},
407 {ns_t_dnskey, "DNSKEY", "DNS key"},
408 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
409 {ns_t_nsec3, "NSEC3", "nsec3"},
410 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
411 {ns_t_hip, "HIP", "host identity protocol"},
412 {ns_t_spf, "SPF", "sender policy framework"},
413 {ns_t_tkey, "TKEY", "tkey"},
414 {ns_t_tsig, "TSIG", "transaction signature"},
415 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
416 {ns_t_axfr, "AXFR", "zone transfer"},
417 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
418 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
419 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
420 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
421 {ns_t_kx, "KX", "Key Exchange"},
422 {ns_t_cert, "CERT", "Certificate"},
423 {ns_t_a6, "A6", "IPv6 Address"},
424 {ns_t_dname, "DNAME", "dname"},
425 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
426 {ns_t_opt, "OPT", "EDNS Options"},
427 {ns_t_any, "ANY", "\"any\""},
428 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
429 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900430
431/*
432 * Names of DNS rcodes.
433 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900434static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
435 {ns_r_formerr, "FORMERR", "format error"},
436 {ns_r_servfail, "SERVFAIL", "server failed"},
437 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
438 {ns_r_notimpl, "NOTIMP", "not implemented"},
439 {ns_r_refused, "REFUSED", "refused"},
440 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
441 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
442 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
443 {ns_r_notauth, "NOTAUTH", "not authoritative"},
444 {ns_r_notzone, "NOTZONE", "Not in zone"},
445 {ns_r_max, "", ""},
446 {ns_r_badsig, "BADSIG", "bad signature"},
447 {ns_r_badkey, "BADKEY", "bad key"},
448 {ns_r_badtime, "BADTIME", "bad time"},
449 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900450
Bernie Innocenti0d180fc2018-09-11 18:18:22 +0900451static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900452 static char unname[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900453
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 for (; syms->name != 0; syms++) {
455 if (number == syms->number) {
456 if (success) *success = 1;
457 return (syms->name);
458 }
459 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900460
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900461 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
462 if (success) *success = 0;
463 return (unname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900464}
465
Bernie Innocenti55864192018-08-30 04:05:20 +0900466/*
467 * Return a string for the type.
468 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900469const char* p_type(int type) {
470 int success;
471 const char* result;
472 static char typebuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900473
Bernie Innocenti189eb502018-10-01 23:10:18 +0900474 result = sym_ntos(p_type_syms, type, &success);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 if (success) return (result);
476 if (type < 0 || type > 0xffff) return ("BADTYPE");
477 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
478 return (typebuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900479}
480
481/*
482 * Return a string for the type.
483 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900484const char* p_section(int section, int opcode) {
485 const struct res_sym* symbols;
Bernie Innocenti55864192018-08-30 04:05:20 +0900486
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900487 switch (opcode) {
488 case ns_o_update:
Bernie Innocenti189eb502018-10-01 23:10:18 +0900489 symbols = p_update_section_syms;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 break;
491 default:
Bernie Innocenti189eb502018-10-01 23:10:18 +0900492 symbols = p_default_section_syms;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 break;
494 }
495 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900496}
497
498/*
499 * Return a mnemonic for class.
500 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900501const char* p_class(int cl) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900502 int success;
503 const char* result;
504 static char classbuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900505
Bernie Innocenti189eb502018-10-01 23:10:18 +0900506 result = sym_ntos(p_class_syms, cl, &success);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507 if (success) return (result);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900508 if (cl < 0 || cl > 0xffff) return ("BADCLASS");
509 snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 return (classbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900511}
512
513/*
Bernie Innocenti55864192018-08-30 04:05:20 +0900514 * Return a string for the rcode.
515 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516const char* p_rcode(int rcode) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900517 return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900518}