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 | |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 98 | #define LOG_TAG "res_debug" |
| 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> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 109 | #include <ctype.h> |
| 110 | #include <errno.h> |
| 111 | #include <math.h> |
| 112 | #include <netdb.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 113 | #include <stdlib.h> |
| 114 | #include <string.h> |
| 115 | #include <strings.h> |
| 116 | #include <time.h> |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 117 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 118 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 119 | |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame^] | 120 | // Default to disabling verbose logging unless overridden by Android.bp |
| 121 | // for debuggable builds. |
| 122 | // |
| 123 | // NOTE: Verbose resolver logs could contain PII -- do NOT enable in production builds |
| 124 | #ifndef RESOLV_ALLOW_VERBOSE_LOGGING |
| 125 | #define RESOLV_ALLOW_VERBOSE_LOGGING 0 |
| 126 | #endif |
| 127 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 128 | struct res_sym { |
| 129 | int number; /* Identifying number, like T_MX */ |
| 130 | const char* name; /* Its symbolic name, like "MX" */ |
| 131 | const char* humanname; /* Its fun name, like "mail exchanger" */ |
| 132 | }; |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 133 | |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 134 | // add a formatted string to a bounded buffer |
| 135 | // TODO: convert to std::string |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 136 | static char* dbprint(char* p, char* end, const char* format, ...) { |
| 137 | int avail, n; |
| 138 | va_list args; |
| 139 | |
| 140 | avail = end - p; |
| 141 | |
| 142 | if (avail <= 0) return p; |
| 143 | |
| 144 | va_start(args, format); |
| 145 | n = vsnprintf(p, avail, format, args); |
| 146 | va_end(args); |
| 147 | |
| 148 | /* certain C libraries return -1 in case of truncation */ |
| 149 | if (n < 0 || n > avail) n = avail; |
| 150 | |
| 151 | p += n; |
| 152 | /* certain C libraries do not zero-terminate in case of truncation */ |
| 153 | if (p == end) p[-1] = 0; |
| 154 | |
| 155 | return p; |
| 156 | } |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 157 | |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 158 | static void do_section(ns_msg* handle, ns_sect section) { |
| 159 | int n, rrnum; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 160 | int buflen = 2048; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 161 | ns_opcode opcode; |
| 162 | ns_rr rr; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 163 | char temp[2048], *p = temp, *end = p + sizeof(temp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 164 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 165 | /* |
| 166 | * Print answer records. |
| 167 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 168 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 169 | char* buf = (char*) malloc((size_t) buflen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 170 | if (buf == NULL) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 171 | dbprint(p, end, ";; memory allocation failure\n"); |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 172 | LOG(VERBOSE) << __func__ << ": " << temp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 173 | return; |
| 174 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 175 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 176 | opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode); |
| 177 | rrnum = 0; |
| 178 | for (;;) { |
| 179 | if (ns_parserr(handle, section, rrnum, &rr)) { |
| 180 | if (errno != ENODEV) |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 181 | dbprint(p, end, ";; ns_parserr: %s", strerror(errno)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 182 | goto cleanup; |
| 183 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 184 | if (section == ns_s_qd) |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 185 | dbprint(p, end, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), |
| 186 | p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr))); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 187 | else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) { |
| 188 | size_t rdatalen, ttl; |
| 189 | uint16_t optcode, optlen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 190 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 191 | rdatalen = ns_rr_rdlen(rr); |
| 192 | ttl = ns_rr_ttl(rr); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 193 | dbprint(p, end, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff, |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 194 | ns_rr_class(rr), ttl & 0xffff); |
chenbruce | f3cc6e8 | 2019-03-27 15:06:41 +0800 | [diff] [blame] | 195 | const u_char* cp = ns_rr_rdata(rr); |
| 196 | while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 197 | int i; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 198 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 199 | GETSHORT(optcode, cp); |
| 200 | GETSHORT(optlen, cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 201 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 202 | if (optcode == NS_OPT_NSID) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 203 | p = dbprint(p, end, "; NSID: "); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 204 | if (optlen == 0) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 205 | p = dbprint(p, end, "; NSID\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 206 | } else { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 207 | p = dbprint(p, end, "; NSID: "); |
| 208 | for (i = 0; i < optlen; i++) { |
| 209 | p = dbprint(p, end, "%02x ", cp[i]); |
| 210 | } |
| 211 | p = dbprint(p, end, " ("); |
| 212 | for (i = 0; i < optlen; i++) { |
| 213 | p = dbprint(p, end, "%c", isprint(cp[i]) ? cp[i] : '.'); |
| 214 | } |
| 215 | p = dbprint(p, end, ")\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 216 | } |
| 217 | } else { |
| 218 | if (optlen == 0) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 219 | p = dbprint(p, end, "; OPT=%u\n", optcode); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 220 | } else { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 221 | p = dbprint(p, end, "; OPT=%u: ", optcode); |
| 222 | for (i = 0; i < optlen; i++) { |
| 223 | p = dbprint(p, end, "%02x ", cp[i]); |
| 224 | } |
| 225 | p = dbprint(p, end, " ("); |
| 226 | for (i = 0; i < optlen; i++) { |
| 227 | p = dbprint(p, end, "%c", isprint(cp[i]) ? cp[i] : '.'); |
| 228 | } |
| 229 | p = dbprint(p, end, ")\n"); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | rdatalen -= 4 + optlen; |
chenbruce | f3cc6e8 | 2019-03-27 15:06:41 +0800 | [diff] [blame] | 233 | cp += optlen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 234 | } |
| 235 | } else { |
| 236 | n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen); |
| 237 | if (n < 0) { |
| 238 | if (errno == ENOSPC) { |
| 239 | free(buf); |
| 240 | buf = NULL; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 241 | if (buflen < 131072) { |
| 242 | buf = (char*) malloc((size_t)(buflen += 1024)); |
| 243 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 244 | if (buf == NULL) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 245 | p = dbprint(p, end, ";; memory allocation failure\n"); |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 246 | LOG(VERBOSE) << __func__ << ": " << temp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | continue; |
| 250 | } |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 251 | p = dbprint(p, end, ";; ns_sprintrr: %s\n", strerror(errno)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 252 | goto cleanup; |
| 253 | } |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 254 | p = dbprint(p, end, ";; %s\n", buf); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 255 | } |
| 256 | rrnum++; |
| 257 | } |
| 258 | cleanup: |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 259 | free(buf); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 260 | LOG(VERBOSE) << temp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Print the contents of a query. |
| 265 | * This is intended to be primarily a debugging routine. |
| 266 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 267 | void res_pquery(const u_char* msg, int len) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 268 | if (!WOULD_LOG(VERBOSE)) return; |
| 269 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 270 | ns_msg handle; |
| 271 | int qdcount, ancount, nscount, arcount; |
| 272 | u_int opcode, rcode, id; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 273 | char temp[2048], *p = temp, *end = p + sizeof(temp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 274 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 275 | if (ns_initparse(msg, len, &handle) < 0) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 276 | dbprint(p, end, ";; ns_initparse: %s\n", strerror(errno)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | opcode = ns_msg_getflag(handle, ns_f_opcode); |
| 280 | rcode = ns_msg_getflag(handle, ns_f_rcode); |
| 281 | id = ns_msg_id(handle); |
| 282 | qdcount = ns_msg_count(handle, ns_s_qd); |
| 283 | ancount = ns_msg_count(handle, ns_s_an); |
| 284 | nscount = ns_msg_count(handle, ns_s_ns); |
| 285 | arcount = ns_msg_count(handle, ns_s_ar); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 286 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 287 | /* |
| 288 | * Print header fields. |
| 289 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 290 | dbprint(p, end, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode], |
| 291 | p_rcode((int)rcode), id); |
| 292 | p = dbprint(p, end, ";"); |
| 293 | p = dbprint(p, end, "; flags:"); |
| 294 | if (ns_msg_getflag(handle, ns_f_qr)) p = dbprint(p, end, " qr"); |
| 295 | if (ns_msg_getflag(handle, ns_f_aa)) p = dbprint(p, end, " aa"); |
| 296 | if (ns_msg_getflag(handle, ns_f_tc)) p = dbprint(p, end, " tc"); |
| 297 | if (ns_msg_getflag(handle, ns_f_rd)) p = dbprint(p, end, " rd"); |
| 298 | if (ns_msg_getflag(handle, ns_f_ra)) p = dbprint(p, end, " ra"); |
| 299 | if (ns_msg_getflag(handle, ns_f_z)) p = dbprint(p, end, " ??"); |
| 300 | if (ns_msg_getflag(handle, ns_f_ad)) p = dbprint(p, end, " ad"); |
| 301 | if (ns_msg_getflag(handle, ns_f_cd)) p = dbprint(p, end, " cd"); |
| 302 | p = dbprint(p, end, "; %s: %d", p_section(ns_s_qd, (int)opcode), qdcount); |
| 303 | p = dbprint(p, end, ", %s: %d", p_section(ns_s_an, (int)opcode), ancount); |
| 304 | p = dbprint(p, end, ", %s: %d", p_section(ns_s_ns, (int)opcode), nscount); |
| 305 | p = dbprint(p, end, ", %s: %d", p_section(ns_s_ar, (int)opcode), arcount); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 306 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 307 | LOG(VERBOSE) << temp; |
| 308 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 309 | /* |
| 310 | * Print the various sections. |
| 311 | */ |
chenbruce | dd21072 | 2019-03-06 13:52:43 +0800 | [diff] [blame] | 312 | do_section(&handle, ns_s_qd); |
| 313 | do_section(&handle, ns_s_an); |
| 314 | do_section(&handle, ns_s_ns); |
| 315 | do_section(&handle, ns_s_ar); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 316 | if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) LOG(VERBOSE) << ";;"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 319 | /* |
| 320 | * Names of RR classes and qclasses. Classes and qclasses are the same, except |
| 321 | * that C_ANY is a qclass but not a class. (You can ask for records of class |
| 322 | * C_ANY, but you can't have any records of that class in the database.) |
| 323 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 324 | static const struct res_sym p_class_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 325 | {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0}, |
| 326 | {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0}, |
| 327 | {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Names of message sections. |
| 331 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 332 | static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0}, |
| 333 | {ns_s_an, "ANSWER", (char*) 0}, |
| 334 | {ns_s_ns, "AUTHORITY", (char*) 0}, |
| 335 | {ns_s_ar, "ADDITIONAL", (char*) 0}, |
| 336 | {0, (char*) 0, (char*) 0}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 337 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 338 | static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0}, |
| 339 | {S_PREREQ, "PREREQUISITE", (char*) 0}, |
| 340 | {S_UPDATE, "UPDATE", (char*) 0}, |
| 341 | {S_ADDT, "ADDITIONAL", (char*) 0}, |
| 342 | {0, (char*) 0, (char*) 0}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 343 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 344 | /* |
| 345 | * Names of RR types and qtypes. Types and qtypes are the same, except |
| 346 | * that T_ANY is a qtype but not a type. (You can ask for records of type |
| 347 | * T_ANY, but you can't have any records of that type in the database.) |
| 348 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 349 | const struct res_sym p_type_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 350 | {ns_t_a, "A", "address"}, |
| 351 | {ns_t_ns, "NS", "name server"}, |
| 352 | {ns_t_md, "MD", "mail destination (deprecated)"}, |
| 353 | {ns_t_mf, "MF", "mail forwarder (deprecated)"}, |
| 354 | {ns_t_cname, "CNAME", "canonical name"}, |
| 355 | {ns_t_soa, "SOA", "start of authority"}, |
| 356 | {ns_t_mb, "MB", "mailbox"}, |
| 357 | {ns_t_mg, "MG", "mail group member"}, |
| 358 | {ns_t_mr, "MR", "mail rename"}, |
| 359 | {ns_t_null, "NULL", "null"}, |
| 360 | {ns_t_wks, "WKS", "well-known service (deprecated)"}, |
| 361 | {ns_t_ptr, "PTR", "domain name pointer"}, |
| 362 | {ns_t_hinfo, "HINFO", "host information"}, |
| 363 | {ns_t_minfo, "MINFO", "mailbox information"}, |
| 364 | {ns_t_mx, "MX", "mail exchanger"}, |
| 365 | {ns_t_txt, "TXT", "text"}, |
| 366 | {ns_t_rp, "RP", "responsible person"}, |
| 367 | {ns_t_afsdb, "AFSDB", "DCE or AFS server"}, |
| 368 | {ns_t_x25, "X25", "X25 address"}, |
| 369 | {ns_t_isdn, "ISDN", "ISDN address"}, |
| 370 | {ns_t_rt, "RT", "router"}, |
| 371 | {ns_t_nsap, "NSAP", "nsap address"}, |
| 372 | {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"}, |
| 373 | {ns_t_sig, "SIG", "signature"}, |
| 374 | {ns_t_key, "KEY", "key"}, |
| 375 | {ns_t_px, "PX", "mapping information"}, |
| 376 | {ns_t_gpos, "GPOS", "geographical position (withdrawn)"}, |
| 377 | {ns_t_aaaa, "AAAA", "IPv6 address"}, |
| 378 | {ns_t_loc, "LOC", "location"}, |
| 379 | {ns_t_nxt, "NXT", "next valid name (unimplemented)"}, |
| 380 | {ns_t_eid, "EID", "endpoint identifier (unimplemented)"}, |
| 381 | {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"}, |
| 382 | {ns_t_srv, "SRV", "server selection"}, |
| 383 | {ns_t_atma, "ATMA", "ATM address (unimplemented)"}, |
| 384 | {ns_t_naptr, "NAPTR", "naptr"}, |
| 385 | {ns_t_kx, "KX", "key exchange"}, |
| 386 | {ns_t_cert, "CERT", "certificate"}, |
| 387 | {ns_t_a6, "A", "IPv6 address (experminental)"}, |
| 388 | {ns_t_dname, "DNAME", "non-terminal redirection"}, |
| 389 | {ns_t_opt, "OPT", "opt"}, |
| 390 | {ns_t_apl, "apl", "apl"}, |
| 391 | {ns_t_ds, "DS", "delegation signer"}, |
| 392 | {ns_t_sshfp, "SSFP", "SSH fingerprint"}, |
| 393 | {ns_t_ipseckey, "IPSECKEY", "IPSEC key"}, |
| 394 | {ns_t_rrsig, "RRSIG", "rrsig"}, |
| 395 | {ns_t_nsec, "NSEC", "nsec"}, |
| 396 | {ns_t_dnskey, "DNSKEY", "DNS key"}, |
| 397 | {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"}, |
| 398 | {ns_t_nsec3, "NSEC3", "nsec3"}, |
| 399 | {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"}, |
| 400 | {ns_t_hip, "HIP", "host identity protocol"}, |
| 401 | {ns_t_spf, "SPF", "sender policy framework"}, |
| 402 | {ns_t_tkey, "TKEY", "tkey"}, |
| 403 | {ns_t_tsig, "TSIG", "transaction signature"}, |
| 404 | {ns_t_ixfr, "IXFR", "incremental zone transfer"}, |
| 405 | {ns_t_axfr, "AXFR", "zone transfer"}, |
| 406 | {ns_t_zxfr, "ZXFR", "compressed zone transfer"}, |
| 407 | {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"}, |
| 408 | {ns_t_maila, "MAILA", "mail agent (deprecated)"}, |
| 409 | {ns_t_naptr, "NAPTR", "URN Naming Authority"}, |
| 410 | {ns_t_kx, "KX", "Key Exchange"}, |
| 411 | {ns_t_cert, "CERT", "Certificate"}, |
| 412 | {ns_t_a6, "A6", "IPv6 Address"}, |
| 413 | {ns_t_dname, "DNAME", "dname"}, |
| 414 | {ns_t_sink, "SINK", "Kitchen Sink (experimental)"}, |
| 415 | {ns_t_opt, "OPT", "EDNS Options"}, |
| 416 | {ns_t_any, "ANY", "\"any\""}, |
| 417 | {ns_t_dlv, "DLV", "DNSSEC look-aside validation"}, |
| 418 | {0, NULL, NULL}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * Names of DNS rcodes. |
| 422 | */ |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 423 | static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"}, |
| 424 | {ns_r_formerr, "FORMERR", "format error"}, |
| 425 | {ns_r_servfail, "SERVFAIL", "server failed"}, |
| 426 | {ns_r_nxdomain, "NXDOMAIN", "no such domain name"}, |
| 427 | {ns_r_notimpl, "NOTIMP", "not implemented"}, |
| 428 | {ns_r_refused, "REFUSED", "refused"}, |
| 429 | {ns_r_yxdomain, "YXDOMAIN", "domain name exists"}, |
| 430 | {ns_r_yxrrset, "YXRRSET", "rrset exists"}, |
| 431 | {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"}, |
| 432 | {ns_r_notauth, "NOTAUTH", "not authoritative"}, |
| 433 | {ns_r_notzone, "NOTZONE", "Not in zone"}, |
| 434 | {ns_r_max, "", ""}, |
| 435 | {ns_r_badsig, "BADSIG", "bad signature"}, |
| 436 | {ns_r_badkey, "BADKEY", "bad key"}, |
| 437 | {ns_r_badtime, "BADTIME", "bad time"}, |
| 438 | {0, NULL, NULL}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 439 | |
Bernie Innocenti | 5c6e0b0 | 2018-09-11 18:18:22 +0900 | [diff] [blame] | 440 | 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] | 441 | static char unname[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 442 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 443 | for (; syms->name != 0; syms++) { |
| 444 | if (number == syms->number) { |
| 445 | if (success) *success = 1; |
| 446 | return (syms->name); |
| 447 | } |
| 448 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 449 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 450 | snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */ |
| 451 | if (success) *success = 0; |
| 452 | return (unname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 453 | } |
| 454 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 455 | /* |
| 456 | * Return a string for the type. |
| 457 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 458 | const char* p_type(int type) { |
| 459 | int success; |
| 460 | const char* result; |
| 461 | static char typebuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 462 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 463 | result = sym_ntos(p_type_syms, type, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 464 | if (success) return (result); |
| 465 | if (type < 0 || type > 0xffff) return ("BADTYPE"); |
| 466 | snprintf(typebuf, sizeof(typebuf), "TYPE%d", type); |
| 467 | return (typebuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Return a string for the type. |
| 472 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 473 | const char* p_section(int section, int opcode) { |
| 474 | const struct res_sym* symbols; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 475 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 476 | switch (opcode) { |
| 477 | case ns_o_update: |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 478 | symbols = p_update_section_syms; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 479 | break; |
| 480 | default: |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 481 | symbols = p_default_section_syms; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | return (sym_ntos(symbols, section, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Return a mnemonic for class. |
| 489 | */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 490 | const char* p_class(int cl) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 491 | int success; |
| 492 | const char* result; |
| 493 | static char classbuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 494 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 495 | result = sym_ntos(p_class_syms, cl, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 496 | if (success) return (result); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 497 | if (cl < 0 || cl > 0xffff) return ("BADCLASS"); |
| 498 | snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 499 | return (classbuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 503 | * Return a string for the rcode. |
| 504 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 505 | const char* p_rcode(int rcode) { |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 506 | return (sym_ntos(p_rcode_syms, rcode, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 507 | } |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 508 | |
| 509 | android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr) { |
| 510 | android::base::LogSeverity logSeverityEnum; |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 511 | |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame^] | 512 | if (logSeverityStr == "VERBOSE") { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 513 | // *** enable verbose logging only when DBG is set. It prints sensitive data *** |
Bernie Innocenti | edf6768 | 2019-03-29 17:04:48 +0900 | [diff] [blame^] | 514 | logSeverityEnum = |
| 515 | RESOLV_ALLOW_VERBOSE_LOGGING ? android::base::VERBOSE : android::base::DEBUG; |
| 516 | } else if (logSeverityStr == "DEBUG") { |
| 517 | logSeverityEnum = android::base::DEBUG; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 518 | } else if (logSeverityStr == "INFO") { |
| 519 | logSeverityEnum = android::base::INFO; |
| 520 | } else if (logSeverityStr == "WARNING") { |
| 521 | logSeverityEnum = android::base::WARNING; |
| 522 | } else if (logSeverityStr == "ERROR") { |
| 523 | logSeverityEnum = android::base::ERROR; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 524 | } else { |
| 525 | // Invalid parameter is treated as WARNING (default setting) |
| 526 | logSeverityEnum = android::base::WARNING; |
| 527 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 528 | LOG(INFO) << __func__ << ": " << logSeverityEnum; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 529 | return logSeverityEnum; |
| 530 | } |