blob: 55a919fae4447ea12e96503800bfe805a94e5f43 [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 Innocentid017e972019-03-03 19:39:53 +090098#define LOG_TAG "res_debug"
99
Bernie Innocenti55864192018-08-30 04:05:20 +0900100#include <sys/param.h>
101#include <sys/socket.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900102#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900103
Bernie Innocenti55864192018-08-30 04:05:20 +0900104#include <arpa/inet.h>
105#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900107
chenbruce16adee42019-02-20 19:45:50 +0800108#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900109#include <ctype.h>
110#include <errno.h>
111#include <math.h>
112#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900113#include <stdlib.h>
114#include <string.h>
115#include <strings.h>
116#include <time.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +0900117
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900118#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900119
Bernie Innocenti189eb502018-10-01 23:10:18 +0900120struct res_sym {
121 int number; /* Identifying number, like T_MX */
122 const char* name; /* Its symbolic name, like "MX" */
123 const char* humanname; /* Its fun name, like "mail exchanger" */
124};
Bernie Innocentif89b3512018-08-30 07:34:37 +0900125
Bernie Innocentid017e972019-03-03 19:39:53 +0900126// add a formatted string to a bounded buffer
127// TODO: convert to std::string
chenbruce16adee42019-02-20 19:45:50 +0800128static char* dbprint(char* p, char* end, const char* format, ...) {
129 int avail, n;
130 va_list args;
131
132 avail = end - p;
133
134 if (avail <= 0) return p;
135
136 va_start(args, format);
137 n = vsnprintf(p, avail, format, args);
138 va_end(args);
139
140 /* certain C libraries return -1 in case of truncation */
141 if (n < 0 || n > avail) n = avail;
142
143 p += n;
144 /* certain C libraries do not zero-terminate in case of truncation */
145 if (p == end) p[-1] = 0;
146
147 return p;
148}
Bernie Innocentid017e972019-03-03 19:39:53 +0900149
chenbruce16adee42019-02-20 19:45:50 +0800150static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151 int n, sflag, rrnum;
152 int buflen = 2048;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 ns_opcode opcode;
154 ns_rr rr;
chenbruce16adee42019-02-20 19:45:50 +0800155 char temp[2048], *p = temp, *end = p + sizeof(temp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900156
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 /*
158 * Print answer records.
159 */
160 sflag = (int) (statp->pfcode & pflag);
161 if (statp->pfcode && !sflag) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900162
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900163 char* buf = (char*) malloc((size_t) buflen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 if (buf == NULL) {
chenbruce16adee42019-02-20 19:45:50 +0800165 dbprint(p, end, ";; memory allocation failure\n");
166 LOG(VERBOSE) << temp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900167 return;
168 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900169
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
171 rrnum = 0;
172 for (;;) {
173 if (ns_parserr(handle, section, rrnum, &rr)) {
174 if (errno != ENODEV)
chenbruce16adee42019-02-20 19:45:50 +0800175 dbprint(p, end, ";; ns_parserr: %s", strerror(errno));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176 else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
chenbruce16adee42019-02-20 19:45:50 +0800177 dbprint(p, end, "\n");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178 goto cleanup;
179 }
180 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
chenbruce16adee42019-02-20 19:45:50 +0800181 dbprint(p, end, ";; %s SECTION:\n", p_section(section, opcode));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182 if (section == ns_s_qd)
chenbruce16adee42019-02-20 19:45:50 +0800183 dbprint(p, end, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr),
184 p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr)));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
186 size_t rdatalen, ttl;
187 uint16_t optcode, optlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189 rdatalen = ns_rr_rdlen(rr);
190 ttl = ns_rr_ttl(rr);
chenbruce16adee42019-02-20 19:45:50 +0800191 dbprint(p, end, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192 ns_rr_class(rr), ttl & 0xffff);
193 while (rdatalen >= 4) {
194 const u_char* cp = ns_rr_rdata(rr);
195 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197 GETSHORT(optcode, cp);
198 GETSHORT(optlen, cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900199
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200 if (optcode == NS_OPT_NSID) {
chenbruce16adee42019-02-20 19:45:50 +0800201 p = dbprint(p, end, "; NSID: ");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202 if (optlen == 0) {
chenbruce16adee42019-02-20 19:45:50 +0800203 p = dbprint(p, end, "; NSID\n");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 } else {
chenbruce16adee42019-02-20 19:45:50 +0800205 p = dbprint(p, end, "; NSID: ");
206 for (i = 0; i < optlen; i++) {
207 p = dbprint(p, end, "%02x ", cp[i]);
208 }
209 p = dbprint(p, end, " (");
210 for (i = 0; i < optlen; i++) {
211 p = dbprint(p, end, "%c", isprint(cp[i]) ? cp[i] : '.');
212 }
213 p = dbprint(p, end, ")\n");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 }
215 } else {
216 if (optlen == 0) {
chenbruce16adee42019-02-20 19:45:50 +0800217 p = dbprint(p, end, "; OPT=%u\n", optcode);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900218 } else {
chenbruce16adee42019-02-20 19:45:50 +0800219 p = dbprint(p, end, "; OPT=%u: ", optcode);
220 for (i = 0; i < optlen; i++) {
221 p = dbprint(p, end, "%02x ", cp[i]);
222 }
223 p = dbprint(p, end, " (");
224 for (i = 0; i < optlen; i++) {
225 p = dbprint(p, end, "%c", isprint(cp[i]) ? cp[i] : '.');
226 }
227 p = dbprint(p, end, ")\n");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228 }
229 }
230 rdatalen -= 4 + optlen;
231 }
232 } else {
233 n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
234 if (n < 0) {
235 if (errno == ENOSPC) {
236 free(buf);
237 buf = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900238 if (buflen < 131072) {
239 buf = (char*) malloc((size_t)(buflen += 1024));
240 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900241 if (buf == NULL) {
chenbruce16adee42019-02-20 19:45:50 +0800242 p = dbprint(p, end, ";; memory allocation failure\n");
243 LOG(VERBOSE) << temp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900244 return;
245 }
246 continue;
247 }
chenbruce16adee42019-02-20 19:45:50 +0800248 p = dbprint(p, end, ";; ns_sprintrr: %s\n", strerror(errno));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900249 goto cleanup;
250 }
chenbruce16adee42019-02-20 19:45:50 +0800251 p = dbprint(p, end, ";; %s\n", buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252 }
253 rrnum++;
254 }
255cleanup:
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900256 free(buf);
chenbruce16adee42019-02-20 19:45:50 +0800257 LOG(VERBOSE) << temp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900258}
259
260/*
261 * Print the contents of a query.
262 * This is intended to be primarily a debugging routine.
263 */
chenbruce16adee42019-02-20 19:45:50 +0800264void res_pquery(const res_state statp, const u_char* msg, int len) {
Bernie Innocentid017e972019-03-03 19:39:53 +0900265 if (!WOULD_LOG(VERBOSE)) return;
266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267 ns_msg handle;
268 int qdcount, ancount, nscount, arcount;
269 u_int opcode, rcode, id;
chenbruce16adee42019-02-20 19:45:50 +0800270 char temp[2048], *p = temp, *end = p + sizeof(temp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900271
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900272 if (ns_initparse(msg, len, &handle) < 0) {
chenbruce16adee42019-02-20 19:45:50 +0800273 dbprint(p, end, ";; ns_initparse: %s\n", strerror(errno));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900274 return;
275 }
276 opcode = ns_msg_getflag(handle, ns_f_opcode);
277 rcode = ns_msg_getflag(handle, ns_f_rcode);
278 id = ns_msg_id(handle);
279 qdcount = ns_msg_count(handle, ns_s_qd);
280 ancount = ns_msg_count(handle, ns_s_an);
281 nscount = ns_msg_count(handle, ns_s_ns);
282 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti55864192018-08-30 04:05:20 +0900283
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900284 /*
285 * Print header fields.
286 */
287 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
chenbruce16adee42019-02-20 19:45:50 +0800288 dbprint(p, end, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
289 p_rcode((int)rcode), id);
290 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) p = dbprint(p, end, ";");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900291 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
chenbruce16adee42019-02-20 19:45:50 +0800292 p = dbprint(p, end, "; flags:");
293 if (ns_msg_getflag(handle, ns_f_qr)) p = dbprint(p, end, " qr");
294 if (ns_msg_getflag(handle, ns_f_aa)) p = dbprint(p, end, " aa");
295 if (ns_msg_getflag(handle, ns_f_tc)) p = dbprint(p, end, " tc");
296 if (ns_msg_getflag(handle, ns_f_rd)) p = dbprint(p, end, " rd");
297 if (ns_msg_getflag(handle, ns_f_ra)) p = dbprint(p, end, " ra");
298 if (ns_msg_getflag(handle, ns_f_z)) p = dbprint(p, end, " ??");
299 if (ns_msg_getflag(handle, ns_f_ad)) p = dbprint(p, end, " ad");
300 if (ns_msg_getflag(handle, ns_f_cd)) p = dbprint(p, end, " cd");
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900301 }
302 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
chenbruce16adee42019-02-20 19:45:50 +0800303 p = dbprint(p, end, "; %s: %d", p_section(ns_s_qd, (int)opcode), qdcount);
304 p = dbprint(p, end, ", %s: %d", p_section(ns_s_an, (int)opcode), ancount);
305 p = dbprint(p, end, ", %s: %d", p_section(ns_s_ns, (int)opcode), nscount);
306 p = dbprint(p, end, ", %s: %d", p_section(ns_s_ar, (int)opcode), arcount);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307 }
Bernie Innocentid017e972019-03-03 19:39:53 +0900308
chenbruce16adee42019-02-20 19:45:50 +0800309 LOG(VERBOSE) << temp;
310
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900311 /*
312 * Print the various sections.
313 */
chenbruce16adee42019-02-20 19:45:50 +0800314 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES);
315 do_section(statp, &handle, ns_s_an, RES_PRF_ANS);
316 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH);
317 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD);
318 if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) LOG(VERBOSE) << ";;";
Bernie Innocenti55864192018-08-30 04:05:20 +0900319}
320
Bernie Innocenti55864192018-08-30 04:05:20 +0900321/*
322 * Names of RR classes and qclasses. Classes and qclasses are the same, except
323 * that C_ANY is a qclass but not a class. (You can ask for records of class
324 * C_ANY, but you can't have any records of that class in the database.)
325 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900326static const struct res_sym p_class_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900327 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
328 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
329 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900330
331/*
332 * Names of message sections.
333 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900334static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
335 {ns_s_an, "ANSWER", (char*) 0},
336 {ns_s_ns, "AUTHORITY", (char*) 0},
337 {ns_s_ar, "ADDITIONAL", (char*) 0},
338 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900339
Bernie Innocenti189eb502018-10-01 23:10:18 +0900340static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
341 {S_PREREQ, "PREREQUISITE", (char*) 0},
342 {S_UPDATE, "UPDATE", (char*) 0},
343 {S_ADDT, "ADDITIONAL", (char*) 0},
344 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900345
Bernie Innocenti55864192018-08-30 04:05:20 +0900346/*
347 * Names of RR types and qtypes. Types and qtypes are the same, except
348 * that T_ANY is a qtype but not a type. (You can ask for records of type
349 * T_ANY, but you can't have any records of that type in the database.)
350 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900351const struct res_sym p_type_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352 {ns_t_a, "A", "address"},
353 {ns_t_ns, "NS", "name server"},
354 {ns_t_md, "MD", "mail destination (deprecated)"},
355 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
356 {ns_t_cname, "CNAME", "canonical name"},
357 {ns_t_soa, "SOA", "start of authority"},
358 {ns_t_mb, "MB", "mailbox"},
359 {ns_t_mg, "MG", "mail group member"},
360 {ns_t_mr, "MR", "mail rename"},
361 {ns_t_null, "NULL", "null"},
362 {ns_t_wks, "WKS", "well-known service (deprecated)"},
363 {ns_t_ptr, "PTR", "domain name pointer"},
364 {ns_t_hinfo, "HINFO", "host information"},
365 {ns_t_minfo, "MINFO", "mailbox information"},
366 {ns_t_mx, "MX", "mail exchanger"},
367 {ns_t_txt, "TXT", "text"},
368 {ns_t_rp, "RP", "responsible person"},
369 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
370 {ns_t_x25, "X25", "X25 address"},
371 {ns_t_isdn, "ISDN", "ISDN address"},
372 {ns_t_rt, "RT", "router"},
373 {ns_t_nsap, "NSAP", "nsap address"},
374 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
375 {ns_t_sig, "SIG", "signature"},
376 {ns_t_key, "KEY", "key"},
377 {ns_t_px, "PX", "mapping information"},
378 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
379 {ns_t_aaaa, "AAAA", "IPv6 address"},
380 {ns_t_loc, "LOC", "location"},
381 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
382 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
383 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
384 {ns_t_srv, "SRV", "server selection"},
385 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
386 {ns_t_naptr, "NAPTR", "naptr"},
387 {ns_t_kx, "KX", "key exchange"},
388 {ns_t_cert, "CERT", "certificate"},
389 {ns_t_a6, "A", "IPv6 address (experminental)"},
390 {ns_t_dname, "DNAME", "non-terminal redirection"},
391 {ns_t_opt, "OPT", "opt"},
392 {ns_t_apl, "apl", "apl"},
393 {ns_t_ds, "DS", "delegation signer"},
394 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
395 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
396 {ns_t_rrsig, "RRSIG", "rrsig"},
397 {ns_t_nsec, "NSEC", "nsec"},
398 {ns_t_dnskey, "DNSKEY", "DNS key"},
399 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
400 {ns_t_nsec3, "NSEC3", "nsec3"},
401 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
402 {ns_t_hip, "HIP", "host identity protocol"},
403 {ns_t_spf, "SPF", "sender policy framework"},
404 {ns_t_tkey, "TKEY", "tkey"},
405 {ns_t_tsig, "TSIG", "transaction signature"},
406 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
407 {ns_t_axfr, "AXFR", "zone transfer"},
408 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
409 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
410 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
411 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
412 {ns_t_kx, "KX", "Key Exchange"},
413 {ns_t_cert, "CERT", "Certificate"},
414 {ns_t_a6, "A6", "IPv6 Address"},
415 {ns_t_dname, "DNAME", "dname"},
416 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
417 {ns_t_opt, "OPT", "EDNS Options"},
418 {ns_t_any, "ANY", "\"any\""},
419 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
420 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900421
422/*
423 * Names of DNS rcodes.
424 */
Bernie Innocenti189eb502018-10-01 23:10:18 +0900425static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
426 {ns_r_formerr, "FORMERR", "format error"},
427 {ns_r_servfail, "SERVFAIL", "server failed"},
428 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
429 {ns_r_notimpl, "NOTIMP", "not implemented"},
430 {ns_r_refused, "REFUSED", "refused"},
431 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
432 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
433 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
434 {ns_r_notauth, "NOTAUTH", "not authoritative"},
435 {ns_r_notzone, "NOTZONE", "Not in zone"},
436 {ns_r_max, "", ""},
437 {ns_r_badsig, "BADSIG", "bad signature"},
438 {ns_r_badkey, "BADKEY", "bad key"},
439 {ns_r_badtime, "BADTIME", "bad time"},
440 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocenti0d180fc2018-09-11 18:18:22 +0900442static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900443 static char unname[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900444
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 for (; syms->name != 0; syms++) {
446 if (number == syms->number) {
447 if (success) *success = 1;
448 return (syms->name);
449 }
450 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900451
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900452 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
453 if (success) *success = 0;
454 return (unname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900455}
456
Bernie Innocenti55864192018-08-30 04:05:20 +0900457/*
458 * Return a string for the type.
459 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900460const char* p_type(int type) {
461 int success;
462 const char* result;
463 static char typebuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900464
Bernie Innocenti189eb502018-10-01 23:10:18 +0900465 result = sym_ntos(p_type_syms, type, &success);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 if (success) return (result);
467 if (type < 0 || type > 0xffff) return ("BADTYPE");
468 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
469 return (typebuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900470}
471
472/*
473 * Return a string for the type.
474 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475const char* p_section(int section, int opcode) {
476 const struct res_sym* symbols;
Bernie Innocenti55864192018-08-30 04:05:20 +0900477
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900478 switch (opcode) {
479 case ns_o_update:
Bernie Innocenti189eb502018-10-01 23:10:18 +0900480 symbols = p_update_section_syms;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481 break;
482 default:
Bernie Innocenti189eb502018-10-01 23:10:18 +0900483 symbols = p_default_section_syms;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900484 break;
485 }
486 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900487}
488
489/*
490 * Return a mnemonic for class.
491 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900492const char* p_class(int cl) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 int success;
494 const char* result;
495 static char classbuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900496
Bernie Innocenti189eb502018-10-01 23:10:18 +0900497 result = sym_ntos(p_class_syms, cl, &success);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 if (success) return (result);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900499 if (cl < 0 || cl > 0xffff) return ("BADCLASS");
500 snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 return (classbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502}
503
504/*
Bernie Innocenti55864192018-08-30 04:05:20 +0900505 * Return a string for the rcode.
506 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507const char* p_rcode(int rcode) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900508 return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900509}
chenbruce16adee42019-02-20 19:45:50 +0800510
511android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr) {
512 android::base::LogSeverity logSeverityEnum;
513 if (logSeverityStr == "VERBOSE") {
514 logSeverityEnum = android::base::VERBOSE;
515 } else if (logSeverityStr == "DEBUG") {
516 logSeverityEnum = android::base::DEBUG;
517 } else if (logSeverityStr == "INFO") {
518 logSeverityEnum = android::base::INFO;
519 } else if (logSeverityStr == "WARNING") {
520 logSeverityEnum = android::base::WARNING;
521 } else if (logSeverityStr == "ERROR") {
522 logSeverityEnum = android::base::ERROR;
523 } else if (logSeverityStr == "FATAL_WITHOUT_ABORT") {
524 logSeverityEnum = android::base::FATAL_WITHOUT_ABORT;
525 } else if (logSeverityStr == "FATAL") {
526 logSeverityEnum = android::base::FATAL;
527 } else {
528 // Invalid parameter is treated as WARNING (default setting)
529 logSeverityEnum = android::base::WARNING;
530 }
531 LOG(INFO) << "logSeverityEnum " << logSeverityEnum;
532 return logSeverityEnum;
533}