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 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 100 | #include <sys/param.h> |
| 101 | #include <sys/socket.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 102 | #include <sys/types.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 103 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 104 | #include <arpa/inet.h> |
| 105 | #include <arpa/nameser.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 106 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 107 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 108 | #include <android-base/logging.h> |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 109 | #include <android-base/stringprintf.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 110 | #include <ctype.h> |
| 111 | #include <errno.h> |
| 112 | #include <math.h> |
| 113 | #include <netdb.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 114 | #include <stdlib.h> |
| 115 | #include <string.h> |
| 116 | #include <strings.h> |
| 117 | #include <time.h> |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 118 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame] | 121 | // 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 | |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 129 | using android::base::StringAppendF; |
| 130 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 131 | struct 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 Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 136 | |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 137 | static void do_section(ns_msg* handle, ns_sect section) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 138 | int n, rrnum = 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 139 | int buflen = 2048; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 140 | ns_rr rr; |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 141 | std::string s; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 142 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 143 | /* |
| 144 | * Print answer records. |
| 145 | */ |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 146 | auto buf = std::make_unique<char[]>(buflen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | for (;;) { |
| 148 | if (ns_parserr(handle, section, rrnum, &rr)) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 149 | if (errno != ENODEV) StringAppendF(&s, "ns_parserr: %s", strerror(errno)); |
| 150 | LOG(VERBOSE) << s; |
| 151 | return; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 152 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 153 | if (section == ns_s_qd) |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 154 | 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 Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 156 | else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) { |
| 157 | size_t rdatalen, ttl; |
| 158 | uint16_t optcode, optlen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 159 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 160 | rdatalen = ns_rr_rdlen(rr); |
| 161 | ttl = ns_rr_ttl(rr); |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 162 | StringAppendF(&s, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff, |
| 163 | ns_rr_class(rr), ttl & 0xffff); |
chenbruce | f3cc6e8 | 2019-03-27 15:06:41 +0800 | [diff] [blame] | 164 | const u_char* cp = ns_rr_rdata(rr); |
| 165 | while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 166 | int i; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 167 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 168 | GETSHORT(optcode, cp); |
| 169 | GETSHORT(optlen, cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 170 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 171 | if (optcode == NS_OPT_NSID) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 172 | StringAppendF(&s, "; NSID: "); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 173 | if (optlen == 0) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 174 | StringAppendF(&s, "; NSID\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 175 | } else { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 176 | StringAppendF(&s, "; NSID: "); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 177 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 178 | StringAppendF(&s, "%02x ", cp[i]); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 179 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 180 | StringAppendF(&s, " ("); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 181 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 182 | StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.'); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 183 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 184 | StringAppendF(&s, ")\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 185 | } |
| 186 | } else { |
| 187 | if (optlen == 0) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 188 | StringAppendF(&s, "; OPT=%u\n", optcode); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 189 | } else { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 190 | StringAppendF(&s, "; OPT=%u: ", optcode); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 191 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 192 | StringAppendF(&s, "%02x ", cp[i]); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 193 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 194 | StringAppendF(&s, " ("); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 195 | for (i = 0; i < optlen; i++) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 196 | StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.'); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 197 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 198 | StringAppendF(&s, ")\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | rdatalen -= 4 + optlen; |
chenbruce | f3cc6e8 | 2019-03-27 15:06:41 +0800 | [diff] [blame] | 202 | cp += optlen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 203 | } |
| 204 | } else { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 205 | n = ns_sprintrr(handle, &rr, NULL, NULL, buf.get(), (u_int)buflen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 206 | if (n < 0) { |
| 207 | if (errno == ENOSPC) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 208 | if (buflen < 131072) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 209 | buf = std::make_unique<char[]>(buflen += 1024); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | } |
| 211 | continue; |
| 212 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 213 | StringAppendF(&s, "ns_sprintrr failed"); |
| 214 | PLOG(VERBOSE) << s; |
| 215 | return; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 216 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 217 | StringAppendF(&s, ";; %s\n", buf.get()); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 218 | } |
| 219 | rrnum++; |
| 220 | } |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 221 | LOG(VERBOSE) << s; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Print the contents of a query. |
| 226 | * This is intended to be primarily a debugging routine. |
| 227 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 228 | void res_pquery(const u_char* msg, int len) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 229 | if (!WOULD_LOG(VERBOSE)) return; |
| 230 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 231 | ns_msg handle; |
| 232 | int qdcount, ancount, nscount, arcount; |
| 233 | u_int opcode, rcode, id; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 234 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 235 | if (ns_initparse(msg, len, &handle) < 0) { |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 236 | PLOG(VERBOSE) << "ns_initparse failed"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | opcode = ns_msg_getflag(handle, ns_f_opcode); |
| 240 | rcode = ns_msg_getflag(handle, ns_f_rcode); |
| 241 | id = ns_msg_id(handle); |
| 242 | qdcount = ns_msg_count(handle, ns_s_qd); |
| 243 | ancount = ns_msg_count(handle, ns_s_an); |
| 244 | nscount = ns_msg_count(handle, ns_s_ns); |
| 245 | arcount = ns_msg_count(handle, ns_s_ar); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 246 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 247 | /* |
| 248 | * Print header fields. |
| 249 | */ |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 250 | 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 Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 266 | |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 267 | LOG(VERBOSE) << s; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 268 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 269 | /* |
| 270 | * Print the various sections. |
| 271 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 272 | 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 278 | /* |
| 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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 283 | static const struct res_sym p_class_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 284 | {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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * Names of message sections. |
| 290 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 291 | static 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 296 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 297 | static 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 302 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 303 | /* |
| 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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 308 | const struct res_sym p_type_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 309 | {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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Names of DNS rcodes. |
| 381 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 382 | static 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 398 | |
Bernie Innocenti | 5c6e0b0 | 2018-09-11 18:18:22 +0900 | [diff] [blame] | 399 | 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] | 400 | static char unname[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 401 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 402 | for (; syms->name != 0; syms++) { |
| 403 | if (number == syms->number) { |
| 404 | if (success) *success = 1; |
| 405 | return (syms->name); |
| 406 | } |
| 407 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 408 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 409 | snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */ |
| 410 | if (success) *success = 0; |
| 411 | return (unname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 412 | } |
| 413 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 414 | /* |
| 415 | * Return a string for the type. |
| 416 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 417 | const char* p_type(int type) { |
| 418 | int success; |
| 419 | const char* result; |
| 420 | static char typebuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 421 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 422 | result = sym_ntos(p_type_syms, type, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 423 | if (success) return (result); |
| 424 | if (type < 0 || type > 0xffff) return ("BADTYPE"); |
| 425 | snprintf(typebuf, sizeof(typebuf), "TYPE%d", type); |
| 426 | return (typebuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Return a string for the type. |
| 431 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 432 | const char* p_section(int section, int opcode) { |
| 433 | const struct res_sym* symbols; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 434 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 435 | switch (opcode) { |
| 436 | case ns_o_update: |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 437 | symbols = p_update_section_syms; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 438 | break; |
| 439 | default: |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 440 | symbols = p_default_section_syms; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 441 | break; |
| 442 | } |
| 443 | return (sym_ntos(symbols, section, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Return a mnemonic for class. |
| 448 | */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 449 | const char* p_class(int cl) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 450 | int success; |
| 451 | const char* result; |
| 452 | static char classbuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 453 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 454 | result = sym_ntos(p_class_syms, cl, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 455 | if (success) return (result); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 456 | if (cl < 0 || cl > 0xffff) return ("BADCLASS"); |
| 457 | snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 458 | return (classbuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 462 | * Return a string for the rcode. |
| 463 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 464 | const char* p_rcode(int rcode) { |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 465 | return (sym_ntos(p_rcode_syms, rcode, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 466 | } |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 467 | |
| 468 | android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr) { |
| 469 | android::base::LogSeverity logSeverityEnum; |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 470 | |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame] | 471 | if (logSeverityStr == "VERBOSE") { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 472 | // *** enable verbose logging only when DBG is set. It prints sensitive data *** |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame] | 473 | logSeverityEnum = |
| 474 | RESOLV_ALLOW_VERBOSE_LOGGING ? android::base::VERBOSE : android::base::DEBUG; |
| 475 | } else if (logSeverityStr == "DEBUG") { |
| 476 | logSeverityEnum = android::base::DEBUG; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 477 | } 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; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 483 | } else { |
| 484 | // Invalid parameter is treated as WARNING (default setting) |
chenbruce | 5b24e21 | 2019-03-12 13:45:38 +0800 | [diff] [blame^] | 485 | LOG(ERROR) << "Invalid parameter is treated as WARNING by default."; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 486 | logSeverityEnum = android::base::WARNING; |
| 487 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 488 | LOG(INFO) << __func__ << ": " << logSeverityEnum; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 489 | return logSeverityEnum; |
| 490 | } |