blob: e0ada474b3145325f1f1d4d21ea66125b5a1d59d [file] [log] [blame]
Bernie Innocenti318ed2d2018-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
Ken Chen5471dca2019-04-15 15:25:35 +080098#define LOG_TAG "resolv"
Bernie Innocenti3952ccc2019-03-03 19:39:53 +090099
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900100#include <sys/param.h>
101#include <sys/socket.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900102#include <sys/types.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900103
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900104#include <arpa/inet.h>
105#include <arpa/nameser.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900106#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900107
chenbruceacb832c2019-02-20 19:45:50 +0800108#include <android-base/logging.h>
chenbruce5b24e212019-03-12 13:45:38 +0800109#include <android-base/stringprintf.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900110#include <ctype.h>
111#include <errno.h>
112#include <math.h>
113#include <netdb.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900114#include <stdlib.h>
115#include <string.h>
116#include <strings.h>
117#include <time.h>
Bernie Innocentiac18b122018-10-01 23:10:18 +0900118
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900119#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900120
Bernie Innocentiedf67682019-03-29 17:04:48 +0900121// Default to disabling verbose logging unless overridden by Android.bp
122// for debuggable builds.
123//
124// NOTE: Verbose resolver logs could contain PII -- do NOT enable in production builds
125#ifndef RESOLV_ALLOW_VERBOSE_LOGGING
126#define RESOLV_ALLOW_VERBOSE_LOGGING 0
127#endif
128
chenbruce5b24e212019-03-12 13:45:38 +0800129using android::base::StringAppendF;
130
Bernie Innocentiac18b122018-10-01 23:10:18 +0900131struct res_sym {
132 int number; /* Identifying number, like T_MX */
133 const char* name; /* Its symbolic name, like "MX" */
134 const char* humanname; /* Its fun name, like "mail exchanger" */
135};
Bernie Innocentiafaacf72018-08-30 07:34:37 +0900136
chenbrucedd210722019-03-06 13:52:43 +0800137static void do_section(ns_msg* handle, ns_sect section) {
chenbruce5b24e212019-03-12 13:45:38 +0800138 int n, rrnum = 0;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900139 int buflen = 2048;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900140 ns_rr rr;
chenbruce5b24e212019-03-12 13:45:38 +0800141 std::string s;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900142
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900143 /*
144 * Print answer records.
145 */
chenbruce5b24e212019-03-12 13:45:38 +0800146 auto buf = std::make_unique<char[]>(buflen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900147 for (;;) {
148 if (ns_parserr(handle, section, rrnum, &rr)) {
chenbruce5b24e212019-03-12 13:45:38 +0800149 if (errno != ENODEV) StringAppendF(&s, "ns_parserr: %s", strerror(errno));
150 LOG(VERBOSE) << s;
151 return;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900152 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900153 if (section == ns_s_qd)
chenbruce5b24e212019-03-12 13:45:38 +0800154 StringAppendF(&s, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr),
155 p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr)));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900156 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
157 size_t rdatalen, ttl;
158 uint16_t optcode, optlen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900159
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900160 rdatalen = ns_rr_rdlen(rr);
161 ttl = ns_rr_ttl(rr);
chenbruce5b24e212019-03-12 13:45:38 +0800162 StringAppendF(&s, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
163 ns_rr_class(rr), ttl & 0xffff);
chenbrucef3cc6e82019-03-27 15:06:41 +0800164 const u_char* cp = ns_rr_rdata(rr);
165 while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900166 int i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900167
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900168 GETSHORT(optcode, cp);
169 GETSHORT(optlen, cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900170
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900171 if (optcode == NS_OPT_NSID) {
chenbruce5b24e212019-03-12 13:45:38 +0800172 StringAppendF(&s, "; NSID: ");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900173 if (optlen == 0) {
chenbruce5b24e212019-03-12 13:45:38 +0800174 StringAppendF(&s, "; NSID\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900175 } else {
chenbruce5b24e212019-03-12 13:45:38 +0800176 StringAppendF(&s, "; NSID: ");
chenbruceacb832c2019-02-20 19:45:50 +0800177 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800178 StringAppendF(&s, "%02x ", cp[i]);
chenbruceacb832c2019-02-20 19:45:50 +0800179 }
chenbruce5b24e212019-03-12 13:45:38 +0800180 StringAppendF(&s, " (");
chenbruceacb832c2019-02-20 19:45:50 +0800181 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800182 StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.');
chenbruceacb832c2019-02-20 19:45:50 +0800183 }
chenbruce5b24e212019-03-12 13:45:38 +0800184 StringAppendF(&s, ")\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900185 }
186 } else {
187 if (optlen == 0) {
chenbruce5b24e212019-03-12 13:45:38 +0800188 StringAppendF(&s, "; OPT=%u\n", optcode);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900189 } else {
chenbruce5b24e212019-03-12 13:45:38 +0800190 StringAppendF(&s, "; OPT=%u: ", optcode);
chenbruceacb832c2019-02-20 19:45:50 +0800191 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800192 StringAppendF(&s, "%02x ", cp[i]);
chenbruceacb832c2019-02-20 19:45:50 +0800193 }
chenbruce5b24e212019-03-12 13:45:38 +0800194 StringAppendF(&s, " (");
chenbruceacb832c2019-02-20 19:45:50 +0800195 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800196 StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.');
chenbruceacb832c2019-02-20 19:45:50 +0800197 }
chenbruce5b24e212019-03-12 13:45:38 +0800198 StringAppendF(&s, ")\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900199 }
200 }
201 rdatalen -= 4 + optlen;
chenbrucef3cc6e82019-03-27 15:06:41 +0800202 cp += optlen;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900203 }
204 } else {
chenbruce5b24e212019-03-12 13:45:38 +0800205 n = ns_sprintrr(handle, &rr, NULL, NULL, buf.get(), (u_int)buflen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900206 if (n < 0) {
207 if (errno == ENOSPC) {
Bernie Innocenti9c575932018-09-07 21:10:25 +0900208 if (buflen < 131072) {
chenbruce5b24e212019-03-12 13:45:38 +0800209 buf = std::make_unique<char[]>(buflen += 1024);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900210 }
211 continue;
212 }
chenbruce5b24e212019-03-12 13:45:38 +0800213 StringAppendF(&s, "ns_sprintrr failed");
214 PLOG(VERBOSE) << s;
215 return;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900216 }
chenbruce5b24e212019-03-12 13:45:38 +0800217 StringAppendF(&s, ";; %s\n", buf.get());
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900218 }
219 rrnum++;
220 }
chenbruce5b24e212019-03-12 13:45:38 +0800221 LOG(VERBOSE) << s;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900222}
223
224/*
225 * Print the contents of a query.
226 * This is intended to be primarily a debugging routine.
227 */
chenbrucedd210722019-03-06 13:52:43 +0800228void res_pquery(const u_char* msg, int len) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900229 if (!WOULD_LOG(VERBOSE)) return;
230
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900231 ns_msg handle;
232 int qdcount, ancount, nscount, arcount;
233 u_int opcode, rcode, id;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900234
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900235 if (ns_initparse(msg, len, &handle) < 0) {
chenbruce5b24e212019-03-12 13:45:38 +0800236 PLOG(VERBOSE) << "ns_initparse failed";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900237 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 Innocenti318ed2d2018-08-30 04:05:20 +0900246
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900247 /*
248 * Print header fields.
249 */
chenbruce5b24e212019-03-12 13:45:38 +0800250 std::string s;
251 StringAppendF(&s, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
252 p_rcode((int)rcode), id);
253 StringAppendF(&s, ";; flags:");
254 if (ns_msg_getflag(handle, ns_f_qr)) StringAppendF(&s, " qr");
255 if (ns_msg_getflag(handle, ns_f_aa)) StringAppendF(&s, " aa");
256 if (ns_msg_getflag(handle, ns_f_tc)) StringAppendF(&s, " tc");
257 if (ns_msg_getflag(handle, ns_f_rd)) StringAppendF(&s, " rd");
258 if (ns_msg_getflag(handle, ns_f_ra)) StringAppendF(&s, " ra");
259 if (ns_msg_getflag(handle, ns_f_z)) StringAppendF(&s, " ??");
260 if (ns_msg_getflag(handle, ns_f_ad)) StringAppendF(&s, " ad");
261 if (ns_msg_getflag(handle, ns_f_cd)) StringAppendF(&s, " cd");
262 StringAppendF(&s, "; %s: %d", p_section(ns_s_qd, (int)opcode), qdcount);
263 StringAppendF(&s, ", %s: %d", p_section(ns_s_an, (int)opcode), ancount);
264 StringAppendF(&s, ", %s: %d", p_section(ns_s_ns, (int)opcode), nscount);
265 StringAppendF(&s, ", %s: %d", p_section(ns_s_ar, (int)opcode), arcount);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900266
chenbruce5b24e212019-03-12 13:45:38 +0800267 LOG(VERBOSE) << s;
chenbruceacb832c2019-02-20 19:45:50 +0800268
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900269 /*
270 * Print the various sections.
271 */
chenbrucedd210722019-03-06 13:52:43 +0800272 do_section(&handle, ns_s_qd);
273 do_section(&handle, ns_s_an);
274 do_section(&handle, ns_s_ns);
275 do_section(&handle, ns_s_ar);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900276}
277
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900278/*
279 * Names of RR classes and qclasses. Classes and qclasses are the same, except
280 * that C_ANY is a qclass but not a class. (You can ask for records of class
281 * C_ANY, but you can't have any records of that class in the database.)
282 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900283static const struct res_sym p_class_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900284 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
285 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
286 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900287
288/*
289 * Names of message sections.
290 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900291static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
292 {ns_s_an, "ANSWER", (char*) 0},
293 {ns_s_ns, "AUTHORITY", (char*) 0},
294 {ns_s_ar, "ADDITIONAL", (char*) 0},
295 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900296
Bernie Innocentiac18b122018-10-01 23:10:18 +0900297static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
298 {S_PREREQ, "PREREQUISITE", (char*) 0},
299 {S_UPDATE, "UPDATE", (char*) 0},
300 {S_ADDT, "ADDITIONAL", (char*) 0},
301 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900302
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900303/*
304 * Names of RR types and qtypes. Types and qtypes are the same, except
305 * that T_ANY is a qtype but not a type. (You can ask for records of type
306 * T_ANY, but you can't have any records of that type in the database.)
307 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900308const struct res_sym p_type_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900309 {ns_t_a, "A", "address"},
310 {ns_t_ns, "NS", "name server"},
311 {ns_t_md, "MD", "mail destination (deprecated)"},
312 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
313 {ns_t_cname, "CNAME", "canonical name"},
314 {ns_t_soa, "SOA", "start of authority"},
315 {ns_t_mb, "MB", "mailbox"},
316 {ns_t_mg, "MG", "mail group member"},
317 {ns_t_mr, "MR", "mail rename"},
318 {ns_t_null, "NULL", "null"},
319 {ns_t_wks, "WKS", "well-known service (deprecated)"},
320 {ns_t_ptr, "PTR", "domain name pointer"},
321 {ns_t_hinfo, "HINFO", "host information"},
322 {ns_t_minfo, "MINFO", "mailbox information"},
323 {ns_t_mx, "MX", "mail exchanger"},
324 {ns_t_txt, "TXT", "text"},
325 {ns_t_rp, "RP", "responsible person"},
326 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
327 {ns_t_x25, "X25", "X25 address"},
328 {ns_t_isdn, "ISDN", "ISDN address"},
329 {ns_t_rt, "RT", "router"},
330 {ns_t_nsap, "NSAP", "nsap address"},
331 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
332 {ns_t_sig, "SIG", "signature"},
333 {ns_t_key, "KEY", "key"},
334 {ns_t_px, "PX", "mapping information"},
335 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
336 {ns_t_aaaa, "AAAA", "IPv6 address"},
337 {ns_t_loc, "LOC", "location"},
338 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
339 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
340 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
341 {ns_t_srv, "SRV", "server selection"},
342 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
343 {ns_t_naptr, "NAPTR", "naptr"},
344 {ns_t_kx, "KX", "key exchange"},
345 {ns_t_cert, "CERT", "certificate"},
346 {ns_t_a6, "A", "IPv6 address (experminental)"},
347 {ns_t_dname, "DNAME", "non-terminal redirection"},
348 {ns_t_opt, "OPT", "opt"},
349 {ns_t_apl, "apl", "apl"},
350 {ns_t_ds, "DS", "delegation signer"},
351 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
352 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
353 {ns_t_rrsig, "RRSIG", "rrsig"},
354 {ns_t_nsec, "NSEC", "nsec"},
355 {ns_t_dnskey, "DNSKEY", "DNS key"},
356 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
357 {ns_t_nsec3, "NSEC3", "nsec3"},
358 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
359 {ns_t_hip, "HIP", "host identity protocol"},
360 {ns_t_spf, "SPF", "sender policy framework"},
361 {ns_t_tkey, "TKEY", "tkey"},
362 {ns_t_tsig, "TSIG", "transaction signature"},
363 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
364 {ns_t_axfr, "AXFR", "zone transfer"},
365 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
366 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
367 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
368 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
369 {ns_t_kx, "KX", "Key Exchange"},
370 {ns_t_cert, "CERT", "Certificate"},
371 {ns_t_a6, "A6", "IPv6 Address"},
372 {ns_t_dname, "DNAME", "dname"},
373 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
374 {ns_t_opt, "OPT", "EDNS Options"},
375 {ns_t_any, "ANY", "\"any\""},
376 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
377 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900378
379/*
380 * Names of DNS rcodes.
381 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900382static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
383 {ns_r_formerr, "FORMERR", "format error"},
384 {ns_r_servfail, "SERVFAIL", "server failed"},
385 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
386 {ns_r_notimpl, "NOTIMP", "not implemented"},
387 {ns_r_refused, "REFUSED", "refused"},
388 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
389 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
390 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
391 {ns_r_notauth, "NOTAUTH", "not authoritative"},
392 {ns_r_notzone, "NOTZONE", "Not in zone"},
393 {ns_r_max, "", ""},
394 {ns_r_badsig, "BADSIG", "bad signature"},
395 {ns_r_badkey, "BADKEY", "bad key"},
396 {ns_r_badtime, "BADTIME", "bad time"},
397 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900398
Bernie Innocenti5c6e0b02018-09-11 18:18:22 +0900399static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900400 static char unname[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900401
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900402 for (; syms->name != 0; syms++) {
403 if (number == syms->number) {
404 if (success) *success = 1;
405 return (syms->name);
406 }
407 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900408
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900409 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
410 if (success) *success = 0;
411 return (unname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900412}
413
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900414/*
415 * Return a string for the type.
416 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900417const char* p_type(int type) {
418 int success;
419 const char* result;
420 static char typebuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900421
Bernie Innocentiac18b122018-10-01 23:10:18 +0900422 result = sym_ntos(p_type_syms, type, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900423 if (success) return (result);
424 if (type < 0 || type > 0xffff) return ("BADTYPE");
425 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
426 return (typebuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900427}
428
429/*
430 * Return a string for the type.
431 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900432const char* p_section(int section, int opcode) {
433 const struct res_sym* symbols;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900434
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900435 switch (opcode) {
436 case ns_o_update:
Bernie Innocentiac18b122018-10-01 23:10:18 +0900437 symbols = p_update_section_syms;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900438 break;
439 default:
Bernie Innocentiac18b122018-10-01 23:10:18 +0900440 symbols = p_default_section_syms;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900441 break;
442 }
443 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900444}
445
446/*
447 * Return a mnemonic for class.
448 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900449const char* p_class(int cl) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900450 int success;
451 const char* result;
452 static char classbuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900453
Bernie Innocentiac18b122018-10-01 23:10:18 +0900454 result = sym_ntos(p_class_syms, cl, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900455 if (success) return (result);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900456 if (cl < 0 || cl > 0xffff) return ("BADCLASS");
457 snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900458 return (classbuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900459}
460
461/*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900462 * Return a string for the rcode.
463 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900464const char* p_rcode(int rcode) {
Bernie Innocentiac18b122018-10-01 23:10:18 +0900465 return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900466}
chenbruceacb832c2019-02-20 19:45:50 +0800467
468android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr) {
469 android::base::LogSeverity logSeverityEnum;
Ken Chenffc224a2019-03-19 17:41:28 +0800470
Bernie Innocentiedf67682019-03-29 17:04:48 +0900471 if (logSeverityStr == "VERBOSE") {
Ken Chenffc224a2019-03-19 17:41:28 +0800472 // *** enable verbose logging only when DBG is set. It prints sensitive data ***
Bernie Innocentiedf67682019-03-29 17:04:48 +0900473 logSeverityEnum =
474 RESOLV_ALLOW_VERBOSE_LOGGING ? android::base::VERBOSE : android::base::DEBUG;
475 } else if (logSeverityStr == "DEBUG") {
476 logSeverityEnum = android::base::DEBUG;
chenbruceacb832c2019-02-20 19:45:50 +0800477 } else if (logSeverityStr == "INFO") {
478 logSeverityEnum = android::base::INFO;
479 } else if (logSeverityStr == "WARNING") {
480 logSeverityEnum = android::base::WARNING;
481 } else if (logSeverityStr == "ERROR") {
482 logSeverityEnum = android::base::ERROR;
chenbruceacb832c2019-02-20 19:45:50 +0800483 } else {
484 // Invalid parameter is treated as WARNING (default setting)
chenbruce5b24e212019-03-12 13:45:38 +0800485 LOG(ERROR) << "Invalid parameter is treated as WARNING by default.";
chenbruceacb832c2019-02-20 19:45:50 +0800486 logSeverityEnum = android::base::WARNING;
487 }
Ken Chenffc224a2019-03-19 17:41:28 +0800488 LOG(INFO) << __func__ << ": " << logSeverityEnum;
chenbruceacb832c2019-02-20 19:45:50 +0800489 return logSeverityEnum;
490}