Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $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 Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 98 | #define LOG_TAG "resolv" |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 99 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame^] | 100 | #include "res_debug.h" |
| 101 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | #include <sys/param.h> |
| 103 | #include <sys/socket.h> |
| 104 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | #include <arpa/inet.h> |
| 106 | #include <arpa/nameser.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 107 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 108 | |
Ken Chen | d99022c | 2019-02-20 21:34:14 +0800 | [diff] [blame] | 109 | #include <aidl/android/net/IDnsResolver.h> |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 110 | #include <android-base/logging.h> |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 111 | #include <android-base/stringprintf.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | #include <ctype.h> |
| 113 | #include <errno.h> |
| 114 | #include <math.h> |
| 115 | #include <netdb.h> |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame^] | 116 | #include <netdutils/Slice.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 117 | #include <stdlib.h> |
| 118 | #include <string.h> |
| 119 | #include <strings.h> |
| 120 | #include <time.h> |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 121 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 122 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 123 | |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame] | 124 | // Default to disabling verbose logging unless overridden by Android.bp |
| 125 | // for debuggable builds. |
| 126 | // |
| 127 | // NOTE: Verbose resolver logs could contain PII -- do NOT enable in production builds |
| 128 | #ifndef RESOLV_ALLOW_VERBOSE_LOGGING |
| 129 | #define RESOLV_ALLOW_VERBOSE_LOGGING 0 |
| 130 | #endif |
| 131 | |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 132 | using android::base::StringAppendF; |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame^] | 133 | using android::netdutils::Slice; |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 134 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 135 | struct res_sym { |
| 136 | int number; /* Identifying number, like T_MX */ |
| 137 | const char* name; /* Its symbolic name, like "MX" */ |
| 138 | const char* humanname; /* Its fun name, like "mail exchanger" */ |
| 139 | }; |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 140 | |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 141 | static void do_section(ns_msg* handle, ns_sect section) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 142 | int n, rrnum = 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 143 | int buflen = 2048; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 144 | ns_rr rr; |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 145 | std::string s; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 146 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | /* |
| 148 | * Print answer records. |
| 149 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 150 | for (;;) { |
| 151 | if (ns_parserr(handle, section, rrnum, &rr)) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 152 | if (errno != ENODEV) StringAppendF(&s, "ns_parserr: %s", strerror(errno)); |
| 153 | LOG(VERBOSE) << s; |
| 154 | return; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 155 | } |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame^] | 156 | if (rrnum == 0) { |
| 157 | int opcode = ns_msg_getflag(*handle, ns_f_opcode); |
| 158 | StringAppendF(&s, ";; %s SECTION:\n", p_section(section, opcode)); |
| 159 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 160 | if (section == ns_s_qd) |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 161 | StringAppendF(&s, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), |
| 162 | p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr))); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 163 | else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) { |
| 164 | size_t rdatalen, ttl; |
| 165 | uint16_t optcode, optlen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 166 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 167 | rdatalen = ns_rr_rdlen(rr); |
| 168 | ttl = ns_rr_ttl(rr); |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 169 | StringAppendF(&s, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff, |
| 170 | ns_rr_class(rr), ttl & 0xffff); |
chenbruce | f3cc6e8 | 2019-03-27 15:06:41 +0800 | [diff] [blame] | 171 | const u_char* cp = ns_rr_rdata(rr); |
| 172 | while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 173 | int i; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 174 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 175 | GETSHORT(optcode, cp); |
| 176 | GETSHORT(optlen, cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 177 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 178 | if (optcode == NS_OPT_NSID) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 179 | StringAppendF(&s, "; NSID: "); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 180 | if (optlen == 0) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 181 | StringAppendF(&s, "; NSID\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 182 | } else { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 183 | StringAppendF(&s, "; NSID: "); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 184 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 185 | StringAppendF(&s, "%02x ", cp[i]); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 186 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 187 | StringAppendF(&s, " ("); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 188 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 189 | StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.'); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 190 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 191 | StringAppendF(&s, ")\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 192 | } |
| 193 | } else { |
| 194 | if (optlen == 0) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 195 | StringAppendF(&s, "; OPT=%u\n", optcode); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 196 | } else { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 197 | StringAppendF(&s, "; OPT=%u: ", optcode); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 198 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 199 | StringAppendF(&s, "%02x ", cp[i]); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 200 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 201 | StringAppendF(&s, " ("); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 202 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 203 | StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.'); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 204 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 205 | StringAppendF(&s, ")\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | rdatalen -= 4 + optlen; |
chenbruce | f3cc6e8 | 2019-03-27 15:06:41 +0800 | [diff] [blame] | 209 | cp += optlen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | } |
| 211 | } else { |
chenbruce | 6895342 | 2019-04-19 21:06:48 +0800 | [diff] [blame] | 212 | auto buf = std::make_unique<char[]>(buflen); |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 213 | n = ns_sprintrr(handle, &rr, NULL, NULL, buf.get(), (u_int)buflen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 214 | if (n < 0) { |
| 215 | if (errno == ENOSPC) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 216 | if (buflen < 131072) { |
chenbruce | 6895342 | 2019-04-19 21:06:48 +0800 | [diff] [blame] | 217 | buflen += 1024; |
| 218 | continue; |
| 219 | } else { |
| 220 | StringAppendF(&s, "buflen over 131072"); |
| 221 | PLOG(VERBOSE) << s; |
| 222 | return; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 223 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 224 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 225 | StringAppendF(&s, "ns_sprintrr failed"); |
| 226 | PLOG(VERBOSE) << s; |
| 227 | return; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 228 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 229 | StringAppendF(&s, ";; %s\n", buf.get()); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 230 | } |
| 231 | rrnum++; |
| 232 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Print the contents of a query. |
| 237 | * This is intended to be primarily a debugging routine. |
| 238 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 239 | void res_pquery(const u_char* msg, int len) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 240 | if (!WOULD_LOG(VERBOSE)) return; |
| 241 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 242 | ns_msg handle; |
| 243 | int qdcount, ancount, nscount, arcount; |
| 244 | u_int opcode, rcode, id; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 245 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 246 | if (ns_initparse(msg, len, &handle) < 0) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 247 | PLOG(VERBOSE) << "ns_initparse failed"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 248 | 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 257 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 258 | /* |
| 259 | * Print header fields. |
| 260 | */ |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 261 | 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 Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 277 | |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame] | 278 | LOG(VERBOSE) << s; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 279 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 280 | /* |
| 281 | * Print the various sections. |
| 282 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 283 | 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 Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame^] | 287 | |
| 288 | LOG(VERBOSE) << "Hex dump:"; |
| 289 | LOG(VERBOSE) << android::netdutils::toHex(Slice(const_cast<uint8_t*>(msg), len), 32); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 290 | } |
| 291 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 292 | /* |
| 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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 297 | static const struct res_sym p_class_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 298 | {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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Names of message sections. |
| 304 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 305 | static 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 310 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 311 | static 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 316 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 317 | /* |
| 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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 322 | const struct res_sym p_type_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 323 | {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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * Names of DNS rcodes. |
| 395 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 396 | static 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 412 | |
Bernie Innocenti | 5c6e0b0 | 2018-09-11 18:18:22 +0900 | [diff] [blame] | 413 | static const char* sym_ntos(const struct res_sym* syms, int number, int* success) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 414 | static char unname[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 415 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 416 | for (; syms->name != 0; syms++) { |
| 417 | if (number == syms->number) { |
| 418 | if (success) *success = 1; |
| 419 | return (syms->name); |
| 420 | } |
| 421 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 422 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 423 | snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */ |
| 424 | if (success) *success = 0; |
| 425 | return (unname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 426 | } |
| 427 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 428 | /* |
| 429 | * Return a string for the type. |
| 430 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 431 | const char* p_type(int type) { |
| 432 | int success; |
| 433 | const char* result; |
| 434 | static char typebuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 435 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 436 | result = sym_ntos(p_type_syms, type, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 437 | if (success) return (result); |
| 438 | if (type < 0 || type > 0xffff) return ("BADTYPE"); |
| 439 | snprintf(typebuf, sizeof(typebuf), "TYPE%d", type); |
| 440 | return (typebuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Return a string for the type. |
| 445 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 446 | const char* p_section(int section, int opcode) { |
| 447 | const struct res_sym* symbols; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 448 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 449 | switch (opcode) { |
| 450 | case ns_o_update: |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 451 | symbols = p_update_section_syms; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 452 | break; |
| 453 | default: |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 454 | symbols = p_default_section_syms; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | return (sym_ntos(symbols, section, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Return a mnemonic for class. |
| 462 | */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 463 | const char* p_class(int cl) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 464 | int success; |
| 465 | const char* result; |
| 466 | static char classbuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 467 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 468 | result = sym_ntos(p_class_syms, cl, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 469 | if (success) return (result); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 470 | if (cl < 0 || cl > 0xffff) return ("BADCLASS"); |
| 471 | snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 472 | return (classbuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 476 | * Return a string for the rcode. |
| 477 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 478 | const char* p_rcode(int rcode) { |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 479 | return (sym_ntos(p_rcode_syms, rcode, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 480 | } |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 481 | |
Ken Chen | d99022c | 2019-02-20 21:34:14 +0800 | [diff] [blame] | 482 | int 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; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 509 | } |
Ken Chen | d99022c | 2019-02-20 21:34:14 +0800 | [diff] [blame] | 510 | android::base::SetMinimumLogSeverity(static_cast<android::base::LogSeverity>(logSeverity)); |
| 511 | return 0; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 512 | } |