blob: 383c44598a62b1a637d2aa306bb693c18eb1d90b [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
Mike Yu34433c62019-08-20 20:17:36 +0800100#include "res_debug.h"
101
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900102#include <sys/param.h>
103#include <sys/socket.h>
104
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900105#include <arpa/inet.h>
106#include <arpa/nameser.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900107#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900108
Ken Chend99022c2019-02-20 21:34:14 +0800109#include <aidl/android/net/IDnsResolver.h>
chenbruceacb832c2019-02-20 19:45:50 +0800110#include <android-base/logging.h>
chenbruce5b24e212019-03-12 13:45:38 +0800111#include <android-base/stringprintf.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900112#include <ctype.h>
113#include <errno.h>
chenbrucec51f1212019-09-12 16:59:33 +0800114#include <inttypes.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900115#include <math.h>
116#include <netdb.h>
Mike Yu34433c62019-08-20 20:17:36 +0800117#include <netdutils/Slice.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900118#include <stdlib.h>
119#include <string.h>
120#include <strings.h>
121#include <time.h>
Bernie Innocentiac18b122018-10-01 23:10:18 +0900122
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900123#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900124
Bernie Innocentiedf67682019-03-29 17:04:48 +0900125// Default to disabling verbose logging unless overridden by Android.bp
126// for debuggable builds.
127//
128// NOTE: Verbose resolver logs could contain PII -- do NOT enable in production builds
129#ifndef RESOLV_ALLOW_VERBOSE_LOGGING
130#define RESOLV_ALLOW_VERBOSE_LOGGING 0
131#endif
132
chenbruce5b24e212019-03-12 13:45:38 +0800133using android::base::StringAppendF;
Mike Yu34433c62019-08-20 20:17:36 +0800134using android::netdutils::Slice;
chenbruce5b24e212019-03-12 13:45:38 +0800135
Bernie Innocentiac18b122018-10-01 23:10:18 +0900136struct res_sym {
137 int number; /* Identifying number, like T_MX */
138 const char* name; /* Its symbolic name, like "MX" */
139 const char* humanname; /* Its fun name, like "mail exchanger" */
140};
Bernie Innocentiafaacf72018-08-30 07:34:37 +0900141
chenbrucedd210722019-03-06 13:52:43 +0800142static void do_section(ns_msg* handle, ns_sect section) {
chenbruce5b24e212019-03-12 13:45:38 +0800143 int n, rrnum = 0;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900144 int buflen = 2048;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900145 ns_rr rr;
chenbruce5b24e212019-03-12 13:45:38 +0800146 std::string s;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900147
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900148 /*
149 * Print answer records.
150 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900151 for (;;) {
152 if (ns_parserr(handle, section, rrnum, &rr)) {
chenbruce5b24e212019-03-12 13:45:38 +0800153 if (errno != ENODEV) StringAppendF(&s, "ns_parserr: %s", strerror(errno));
154 LOG(VERBOSE) << s;
155 return;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900156 }
Mike Yu34433c62019-08-20 20:17:36 +0800157 if (rrnum == 0) {
158 int opcode = ns_msg_getflag(*handle, ns_f_opcode);
159 StringAppendF(&s, ";; %s SECTION:\n", p_section(section, opcode));
160 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900161 if (section == ns_s_qd)
chenbruce5b24e212019-03-12 13:45:38 +0800162 StringAppendF(&s, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr),
163 p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr)));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900164 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
chenbrucec51f1212019-09-12 16:59:33 +0800165 size_t rdatalen;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900166 uint16_t optcode, optlen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900167
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900168 rdatalen = ns_rr_rdlen(rr);
chenbrucec51f1212019-09-12 16:59:33 +0800169 StringAppendF(&s, "; EDNS: version: %" PRIu32 ", udp=%u, flags=%" PRIu32 "\n",
170 (rr.ttl >> 16) & 0xff, ns_rr_class(rr), rr.ttl & 0xffff);
171 const uint8_t* cp = ns_rr_rdata(rr);
chenbrucef3cc6e82019-03-27 15:06:41 +0800172 while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900173 int i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900174
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900175 GETSHORT(optcode, cp);
176 GETSHORT(optlen, cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900177
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900178 if (optcode == NS_OPT_NSID) {
chenbruce5b24e212019-03-12 13:45:38 +0800179 StringAppendF(&s, "; NSID: ");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900180 if (optlen == 0) {
chenbruce5b24e212019-03-12 13:45:38 +0800181 StringAppendF(&s, "; NSID\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900182 } else {
chenbruce5b24e212019-03-12 13:45:38 +0800183 StringAppendF(&s, "; NSID: ");
chenbruceacb832c2019-02-20 19:45:50 +0800184 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800185 StringAppendF(&s, "%02x ", cp[i]);
chenbruceacb832c2019-02-20 19:45:50 +0800186 }
chenbruce5b24e212019-03-12 13:45:38 +0800187 StringAppendF(&s, " (");
chenbruceacb832c2019-02-20 19:45:50 +0800188 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800189 StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.');
chenbruceacb832c2019-02-20 19:45:50 +0800190 }
chenbruce5b24e212019-03-12 13:45:38 +0800191 StringAppendF(&s, ")\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900192 }
193 } else {
194 if (optlen == 0) {
chenbruce5b24e212019-03-12 13:45:38 +0800195 StringAppendF(&s, "; OPT=%u\n", optcode);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900196 } else {
chenbruce5b24e212019-03-12 13:45:38 +0800197 StringAppendF(&s, "; OPT=%u: ", optcode);
chenbruceacb832c2019-02-20 19:45:50 +0800198 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800199 StringAppendF(&s, "%02x ", cp[i]);
chenbruceacb832c2019-02-20 19:45:50 +0800200 }
chenbruce5b24e212019-03-12 13:45:38 +0800201 StringAppendF(&s, " (");
chenbruceacb832c2019-02-20 19:45:50 +0800202 for (i = 0; i < optlen; i++) {
chenbruce5b24e212019-03-12 13:45:38 +0800203 StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.');
chenbruceacb832c2019-02-20 19:45:50 +0800204 }
chenbruce5b24e212019-03-12 13:45:38 +0800205 StringAppendF(&s, ")\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900206 }
207 }
208 rdatalen -= 4 + optlen;
chenbrucef3cc6e82019-03-27 15:06:41 +0800209 cp += optlen;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900210 }
211 } else {
chenbruce68953422019-04-19 21:06:48 +0800212 auto buf = std::make_unique<char[]>(buflen);
chenbrucec51f1212019-09-12 16:59:33 +0800213 n = ns_sprintrr(handle, &rr, NULL, NULL, buf.get(), (uint32_t)buflen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900214 if (n < 0) {
215 if (errno == ENOSPC) {
Bernie Innocenti9c575932018-09-07 21:10:25 +0900216 if (buflen < 131072) {
chenbruce68953422019-04-19 21:06:48 +0800217 buflen += 1024;
218 continue;
219 } else {
220 StringAppendF(&s, "buflen over 131072");
221 PLOG(VERBOSE) << s;
222 return;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900223 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900224 }
chenbruce5b24e212019-03-12 13:45:38 +0800225 StringAppendF(&s, "ns_sprintrr failed");
226 PLOG(VERBOSE) << s;
227 return;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900228 }
chenbruce5b24e212019-03-12 13:45:38 +0800229 StringAppendF(&s, ";; %s\n", buf.get());
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900230 }
231 rrnum++;
232 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900233}
234
235/*
236 * Print the contents of a query.
237 * This is intended to be primarily a debugging routine.
238 */
chenbrucec51f1212019-09-12 16:59:33 +0800239void res_pquery(const uint8_t* msg, int len) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900240 if (!WOULD_LOG(VERBOSE)) return;
241
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900242 ns_msg handle;
243 int qdcount, ancount, nscount, arcount;
chenbrucec51f1212019-09-12 16:59:33 +0800244 uint32_t opcode, rcode, id;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900245
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900246 if (ns_initparse(msg, len, &handle) < 0) {
chenbruce5b24e212019-03-12 13:45:38 +0800247 PLOG(VERBOSE) << "ns_initparse failed";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900248 return;
249 }
250 opcode = ns_msg_getflag(handle, ns_f_opcode);
251 rcode = ns_msg_getflag(handle, ns_f_rcode);
252 id = ns_msg_id(handle);
253 qdcount = ns_msg_count(handle, ns_s_qd);
254 ancount = ns_msg_count(handle, ns_s_an);
255 nscount = ns_msg_count(handle, ns_s_ns);
256 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900257
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900258 /*
259 * Print header fields.
260 */
chenbruce5b24e212019-03-12 13:45:38 +0800261 std::string s;
262 StringAppendF(&s, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
263 p_rcode((int)rcode), id);
264 StringAppendF(&s, ";; flags:");
265 if (ns_msg_getflag(handle, ns_f_qr)) StringAppendF(&s, " qr");
266 if (ns_msg_getflag(handle, ns_f_aa)) StringAppendF(&s, " aa");
267 if (ns_msg_getflag(handle, ns_f_tc)) StringAppendF(&s, " tc");
268 if (ns_msg_getflag(handle, ns_f_rd)) StringAppendF(&s, " rd");
269 if (ns_msg_getflag(handle, ns_f_ra)) StringAppendF(&s, " ra");
270 if (ns_msg_getflag(handle, ns_f_z)) StringAppendF(&s, " ??");
271 if (ns_msg_getflag(handle, ns_f_ad)) StringAppendF(&s, " ad");
272 if (ns_msg_getflag(handle, ns_f_cd)) StringAppendF(&s, " cd");
273 StringAppendF(&s, "; %s: %d", p_section(ns_s_qd, (int)opcode), qdcount);
274 StringAppendF(&s, ", %s: %d", p_section(ns_s_an, (int)opcode), ancount);
275 StringAppendF(&s, ", %s: %d", p_section(ns_s_ns, (int)opcode), nscount);
276 StringAppendF(&s, ", %s: %d", p_section(ns_s_ar, (int)opcode), arcount);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900277
chenbruce5b24e212019-03-12 13:45:38 +0800278 LOG(VERBOSE) << s;
chenbruceacb832c2019-02-20 19:45:50 +0800279
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900280 /*
281 * Print the various sections.
282 */
chenbrucedd210722019-03-06 13:52:43 +0800283 do_section(&handle, ns_s_qd);
284 do_section(&handle, ns_s_an);
285 do_section(&handle, ns_s_ns);
286 do_section(&handle, ns_s_ar);
Mike Yu34433c62019-08-20 20:17:36 +0800287
288 LOG(VERBOSE) << "Hex dump:";
289 LOG(VERBOSE) << android::netdutils::toHex(Slice(const_cast<uint8_t*>(msg), len), 32);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900290}
291
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900292/*
293 * Names of RR classes and qclasses. Classes and qclasses are the same, except
294 * that C_ANY is a qclass but not a class. (You can ask for records of class
295 * C_ANY, but you can't have any records of that class in the database.)
296 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900297static const struct res_sym p_class_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900298 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
299 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
300 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900301
302/*
303 * Names of message sections.
304 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900305static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
306 {ns_s_an, "ANSWER", (char*) 0},
307 {ns_s_ns, "AUTHORITY", (char*) 0},
308 {ns_s_ar, "ADDITIONAL", (char*) 0},
309 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900310
Bernie Innocentiac18b122018-10-01 23:10:18 +0900311static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
312 {S_PREREQ, "PREREQUISITE", (char*) 0},
313 {S_UPDATE, "UPDATE", (char*) 0},
314 {S_ADDT, "ADDITIONAL", (char*) 0},
315 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900316
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900317/*
318 * Names of RR types and qtypes. Types and qtypes are the same, except
319 * that T_ANY is a qtype but not a type. (You can ask for records of type
320 * T_ANY, but you can't have any records of that type in the database.)
321 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900322const struct res_sym p_type_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900323 {ns_t_a, "A", "address"},
324 {ns_t_ns, "NS", "name server"},
325 {ns_t_md, "MD", "mail destination (deprecated)"},
326 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
327 {ns_t_cname, "CNAME", "canonical name"},
328 {ns_t_soa, "SOA", "start of authority"},
329 {ns_t_mb, "MB", "mailbox"},
330 {ns_t_mg, "MG", "mail group member"},
331 {ns_t_mr, "MR", "mail rename"},
332 {ns_t_null, "NULL", "null"},
333 {ns_t_wks, "WKS", "well-known service (deprecated)"},
334 {ns_t_ptr, "PTR", "domain name pointer"},
335 {ns_t_hinfo, "HINFO", "host information"},
336 {ns_t_minfo, "MINFO", "mailbox information"},
337 {ns_t_mx, "MX", "mail exchanger"},
338 {ns_t_txt, "TXT", "text"},
339 {ns_t_rp, "RP", "responsible person"},
340 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
341 {ns_t_x25, "X25", "X25 address"},
342 {ns_t_isdn, "ISDN", "ISDN address"},
343 {ns_t_rt, "RT", "router"},
344 {ns_t_nsap, "NSAP", "nsap address"},
345 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
346 {ns_t_sig, "SIG", "signature"},
347 {ns_t_key, "KEY", "key"},
348 {ns_t_px, "PX", "mapping information"},
349 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
350 {ns_t_aaaa, "AAAA", "IPv6 address"},
351 {ns_t_loc, "LOC", "location"},
352 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
353 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
354 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
355 {ns_t_srv, "SRV", "server selection"},
356 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
357 {ns_t_naptr, "NAPTR", "naptr"},
358 {ns_t_kx, "KX", "key exchange"},
359 {ns_t_cert, "CERT", "certificate"},
360 {ns_t_a6, "A", "IPv6 address (experminental)"},
361 {ns_t_dname, "DNAME", "non-terminal redirection"},
362 {ns_t_opt, "OPT", "opt"},
363 {ns_t_apl, "apl", "apl"},
364 {ns_t_ds, "DS", "delegation signer"},
365 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
366 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
367 {ns_t_rrsig, "RRSIG", "rrsig"},
368 {ns_t_nsec, "NSEC", "nsec"},
369 {ns_t_dnskey, "DNSKEY", "DNS key"},
370 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
371 {ns_t_nsec3, "NSEC3", "nsec3"},
372 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
373 {ns_t_hip, "HIP", "host identity protocol"},
374 {ns_t_spf, "SPF", "sender policy framework"},
375 {ns_t_tkey, "TKEY", "tkey"},
376 {ns_t_tsig, "TSIG", "transaction signature"},
377 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
378 {ns_t_axfr, "AXFR", "zone transfer"},
379 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
380 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
381 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
382 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
383 {ns_t_kx, "KX", "Key Exchange"},
384 {ns_t_cert, "CERT", "Certificate"},
385 {ns_t_a6, "A6", "IPv6 Address"},
386 {ns_t_dname, "DNAME", "dname"},
387 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
388 {ns_t_opt, "OPT", "EDNS Options"},
389 {ns_t_any, "ANY", "\"any\""},
390 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
391 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900392
393/*
394 * Names of DNS rcodes.
395 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900396static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
397 {ns_r_formerr, "FORMERR", "format error"},
398 {ns_r_servfail, "SERVFAIL", "server failed"},
399 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
400 {ns_r_notimpl, "NOTIMP", "not implemented"},
401 {ns_r_refused, "REFUSED", "refused"},
402 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
403 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
404 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
405 {ns_r_notauth, "NOTAUTH", "not authoritative"},
406 {ns_r_notzone, "NOTZONE", "Not in zone"},
407 {ns_r_max, "", ""},
408 {ns_r_badsig, "BADSIG", "bad signature"},
409 {ns_r_badkey, "BADKEY", "bad key"},
410 {ns_r_badtime, "BADTIME", "bad time"},
411 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900412
Bernie Innocenti5c6e0b02018-09-11 18:18:22 +0900413static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900414 static char unname[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900415
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900416 for (; syms->name != 0; syms++) {
417 if (number == syms->number) {
418 if (success) *success = 1;
419 return (syms->name);
420 }
421 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900422
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900423 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
424 if (success) *success = 0;
425 return (unname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900426}
427
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900428/*
429 * Return a string for the type.
430 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900431const char* p_type(int type) {
432 int success;
433 const char* result;
434 static char typebuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900435
Bernie Innocentiac18b122018-10-01 23:10:18 +0900436 result = sym_ntos(p_type_syms, type, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900437 if (success) return (result);
438 if (type < 0 || type > 0xffff) return ("BADTYPE");
439 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
440 return (typebuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900441}
442
443/*
444 * Return a string for the type.
445 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900446const char* p_section(int section, int opcode) {
447 const struct res_sym* symbols;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900448
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900449 switch (opcode) {
450 case ns_o_update:
Bernie Innocentiac18b122018-10-01 23:10:18 +0900451 symbols = p_update_section_syms;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900452 break;
453 default:
Bernie Innocentiac18b122018-10-01 23:10:18 +0900454 symbols = p_default_section_syms;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900455 break;
456 }
457 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900458}
459
460/*
461 * Return a mnemonic for class.
462 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900463const char* p_class(int cl) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900464 int success;
465 const char* result;
466 static char classbuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900467
Bernie Innocentiac18b122018-10-01 23:10:18 +0900468 result = sym_ntos(p_class_syms, cl, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900469 if (success) return (result);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900470 if (cl < 0 || cl > 0xffff) return ("BADCLASS");
471 snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900472 return (classbuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900473}
474
475/*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900476 * Return a string for the rcode.
477 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900478const char* p_rcode(int rcode) {
Bernie Innocentiac18b122018-10-01 23:10:18 +0900479 return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900480}
chenbruceacb832c2019-02-20 19:45:50 +0800481
Ken Chend99022c2019-02-20 21:34:14 +0800482int resolv_set_log_severity(uint32_t logSeverity) {
483 switch (logSeverity) {
484 case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_VERBOSE:
485 logSeverity = android::base::VERBOSE;
486 // *** enable verbose logging only when DBG is set. It prints sensitive data ***
487 if (RESOLV_ALLOW_VERBOSE_LOGGING == false) {
488 logSeverity = android::base::DEBUG;
489 LOG(ERROR) << "Refusing to set VERBOSE logging in non-debuggable build";
490 // TODO: Return EACCES then callers could know if the log
491 // severity is acceptable
492 }
493 break;
494 case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_DEBUG:
495 logSeverity = android::base::DEBUG;
496 break;
497 case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_INFO:
498 logSeverity = android::base::INFO;
499 break;
500 case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_WARNING:
501 logSeverity = android::base::WARNING;
502 break;
503 case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_ERROR:
504 logSeverity = android::base::ERROR;
505 break;
506 default:
507 LOG(ERROR) << __func__ << ": invalid log severity: " << logSeverity;
508 return -EINVAL;
chenbruceacb832c2019-02-20 19:45:50 +0800509 }
Ken Chend99022c2019-02-20 21:34:14 +0800510 android::base::SetMinimumLogSeverity(static_cast<android::base::LogSeverity>(logSeverity));
511 return 0;
chenbruceacb832c2019-02-20 19:45:50 +0800512}