blob: cfd486d9dac7df7859881de052cc661a7f255f55 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $ */
2
3/*
4 * Portions Copyright (C) 2004, 2005, 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 1996-2003 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/*
21 * Copyright (c) 1985
22 * The Regents of the University of California. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52
53/*
54 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies, and that
59 * the name of Digital Equipment Corporation not be used in advertising or
60 * publicity pertaining to distribution of the document or software without
61 * specific, written prior permission.
62 *
63 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
64 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
65 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
66 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
67 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
68 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
69 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
70 * SOFTWARE.
71 */
72
73/*
74 * Portions Copyright (c) 1995 by International Business Machines, Inc.
75 *
76 * International Business Machines, Inc. (hereinafter called IBM) grants
77 * permission under its copyrights to use, copy, modify, and distribute this
78 * Software with or without fee, provided that the above copyright notice and
79 * all paragraphs of this notice appear in all copies, and that the name of IBM
80 * not be used in connection with the marketing of any product incorporating
81 * the Software or modifications thereof, without specific, written prior
82 * permission.
83 *
84 * To the extent it has a right to do so, IBM grants an immunity from suit
85 * under its patents, if any, for the use, sale or manufacture of products to
86 * the extent that such products are used for performing Domain Name System
87 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
88 * granted for any product per se or for any other function of any product.
89 *
90 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
91 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
92 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
93 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
94 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
95 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
96 */
97
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090098#include <sys/param.h>
99#include <sys/socket.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900100#include <sys/types.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900101
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900102#include <arpa/inet.h>
103#include <arpa/nameser.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900104#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900105
chenbruceacb832c2019-02-20 19:45:50 +0800106#include <android-base/logging.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900107#include <ctype.h>
108#include <errno.h>
109#include <math.h>
110#include <netdb.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900111#include <stdio.h>
112#include <stdlib.h>
113#include <string.h>
114#include <strings.h>
115#include <time.h>
Bernie Innocentiac18b122018-10-01 23:10:18 +0900116
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900117#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900118
Bernie Innocentiac18b122018-10-01 23:10:18 +0900119struct res_sym {
120 int number; /* Identifying number, like T_MX */
121 const char* name; /* Its symbolic name, like "MX" */
122 const char* humanname; /* Its fun name, like "mail exchanger" */
123};
Bernie Innocentiafaacf72018-08-30 07:34:37 +0900124
chenbruceacb832c2019-02-20 19:45:50 +0800125/* add a formatted string to a bounded buffer */
126static char* dbprint(char* p, char* end, const char* format, ...) {
127 int avail, n;
128 va_list args;
129
130 avail = end - p;
131
132 if (avail <= 0) return p;
133
134 va_start(args, format);
135 n = vsnprintf(p, avail, format, args);
136 va_end(args);
137
138 /* certain C libraries return -1 in case of truncation */
139 if (n < 0 || n > avail) n = avail;
140
141 p += n;
142 /* certain C libraries do not zero-terminate in case of truncation */
143 if (p == end) p[-1] = 0;
144
145 return p;
146}
147static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900148 int n, sflag, rrnum;
149 int buflen = 2048;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900150 ns_opcode opcode;
151 ns_rr rr;
chenbruceacb832c2019-02-20 19:45:50 +0800152 char temp[2048], *p = temp, *end = p + sizeof(temp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900153
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900154 /*
155 * Print answer records.
156 */
157 sflag = (int) (statp->pfcode & pflag);
158 if (statp->pfcode && !sflag) return;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900159
Bernie Innocenti9c575932018-09-07 21:10:25 +0900160 char* buf = (char*) malloc((size_t) buflen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900161 if (buf == NULL) {
chenbruceacb832c2019-02-20 19:45:50 +0800162 dbprint(p, end, ";; memory allocation failure\n");
163 LOG(VERBOSE) << temp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900164 return;
165 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900166
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900167 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
168 rrnum = 0;
169 for (;;) {
170 if (ns_parserr(handle, section, rrnum, &rr)) {
171 if (errno != ENODEV)
chenbruceacb832c2019-02-20 19:45:50 +0800172 dbprint(p, end, ";; ns_parserr: %s", strerror(errno));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900173 else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
chenbruceacb832c2019-02-20 19:45:50 +0800174 dbprint(p, end, "\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900175 goto cleanup;
176 }
177 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
chenbruceacb832c2019-02-20 19:45:50 +0800178 dbprint(p, end, ";; %s SECTION:\n", p_section(section, opcode));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900179 if (section == ns_s_qd)
chenbruceacb832c2019-02-20 19:45:50 +0800180 dbprint(p, end, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr),
181 p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr)));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900182 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
183 size_t rdatalen, ttl;
184 uint16_t optcode, optlen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900185
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900186 rdatalen = ns_rr_rdlen(rr);
187 ttl = ns_rr_ttl(rr);
chenbruceacb832c2019-02-20 19:45:50 +0800188 dbprint(p, end, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900189 ns_rr_class(rr), ttl & 0xffff);
190 while (rdatalen >= 4) {
191 const u_char* cp = ns_rr_rdata(rr);
192 int i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900193
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900194 GETSHORT(optcode, cp);
195 GETSHORT(optlen, cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900196
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900197 if (optcode == NS_OPT_NSID) {
chenbruceacb832c2019-02-20 19:45:50 +0800198 p = dbprint(p, end, "; NSID: ");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900199 if (optlen == 0) {
chenbruceacb832c2019-02-20 19:45:50 +0800200 p = dbprint(p, end, "; NSID\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900201 } else {
chenbruceacb832c2019-02-20 19:45:50 +0800202 p = dbprint(p, end, "; NSID: ");
203 for (i = 0; i < optlen; i++) {
204 p = dbprint(p, end, "%02x ", cp[i]);
205 }
206 p = dbprint(p, end, " (");
207 for (i = 0; i < optlen; i++) {
208 p = dbprint(p, end, "%c", isprint(cp[i]) ? cp[i] : '.');
209 }
210 p = dbprint(p, end, ")\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900211 }
212 } else {
213 if (optlen == 0) {
chenbruceacb832c2019-02-20 19:45:50 +0800214 p = dbprint(p, end, "; OPT=%u\n", optcode);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900215 } else {
chenbruceacb832c2019-02-20 19:45:50 +0800216 p = dbprint(p, end, "; OPT=%u: ", optcode);
217 for (i = 0; i < optlen; i++) {
218 p = dbprint(p, end, "%02x ", cp[i]);
219 }
220 p = dbprint(p, end, " (");
221 for (i = 0; i < optlen; i++) {
222 p = dbprint(p, end, "%c", isprint(cp[i]) ? cp[i] : '.');
223 }
224 p = dbprint(p, end, ")\n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900225 }
226 }
227 rdatalen -= 4 + optlen;
228 }
229 } else {
230 n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
231 if (n < 0) {
232 if (errno == ENOSPC) {
233 free(buf);
234 buf = NULL;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900235 if (buflen < 131072) {
236 buf = (char*) malloc((size_t)(buflen += 1024));
237 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900238 if (buf == NULL) {
chenbruceacb832c2019-02-20 19:45:50 +0800239 p = dbprint(p, end, ";; memory allocation failure\n");
240 LOG(VERBOSE) << temp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900241 return;
242 }
243 continue;
244 }
chenbruceacb832c2019-02-20 19:45:50 +0800245 p = dbprint(p, end, ";; ns_sprintrr: %s\n", strerror(errno));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900246 goto cleanup;
247 }
chenbruceacb832c2019-02-20 19:45:50 +0800248 p = dbprint(p, end, ";; %s\n", buf);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900249 }
250 rrnum++;
251 }
252cleanup:
Bernie Innocenti9c575932018-09-07 21:10:25 +0900253 free(buf);
chenbruceacb832c2019-02-20 19:45:50 +0800254 LOG(VERBOSE) << temp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900255}
256
257/*
258 * Print the contents of a query.
259 * This is intended to be primarily a debugging routine.
260 */
chenbruceacb832c2019-02-20 19:45:50 +0800261
262void res_pquery(const res_state statp, const u_char* msg, int len) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900263 ns_msg handle;
264 int qdcount, ancount, nscount, arcount;
265 u_int opcode, rcode, id;
chenbruceacb832c2019-02-20 19:45:50 +0800266 char temp[2048], *p = temp, *end = p + sizeof(temp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900267
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900268 if (ns_initparse(msg, len, &handle) < 0) {
chenbruceacb832c2019-02-20 19:45:50 +0800269 dbprint(p, end, ";; ns_initparse: %s\n", strerror(errno));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900270 return;
271 }
272 opcode = ns_msg_getflag(handle, ns_f_opcode);
273 rcode = ns_msg_getflag(handle, ns_f_rcode);
274 id = ns_msg_id(handle);
275 qdcount = ns_msg_count(handle, ns_s_qd);
276 ancount = ns_msg_count(handle, ns_s_an);
277 nscount = ns_msg_count(handle, ns_s_ns);
278 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900279
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900280 /*
281 * Print header fields.
282 */
283 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
chenbruceacb832c2019-02-20 19:45:50 +0800284 dbprint(p, end, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
285 p_rcode((int)rcode), id);
286 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) p = dbprint(p, end, ";");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900287 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
chenbruceacb832c2019-02-20 19:45:50 +0800288 p = dbprint(p, end, "; flags:");
289 if (ns_msg_getflag(handle, ns_f_qr)) p = dbprint(p, end, " qr");
290 if (ns_msg_getflag(handle, ns_f_aa)) p = dbprint(p, end, " aa");
291 if (ns_msg_getflag(handle, ns_f_tc)) p = dbprint(p, end, " tc");
292 if (ns_msg_getflag(handle, ns_f_rd)) p = dbprint(p, end, " rd");
293 if (ns_msg_getflag(handle, ns_f_ra)) p = dbprint(p, end, " ra");
294 if (ns_msg_getflag(handle, ns_f_z)) p = dbprint(p, end, " ??");
295 if (ns_msg_getflag(handle, ns_f_ad)) p = dbprint(p, end, " ad");
296 if (ns_msg_getflag(handle, ns_f_cd)) p = dbprint(p, end, " cd");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900297 }
298 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
chenbruceacb832c2019-02-20 19:45:50 +0800299 p = dbprint(p, end, "; %s: %d", p_section(ns_s_qd, (int)opcode), qdcount);
300 p = dbprint(p, end, ", %s: %d", p_section(ns_s_an, (int)opcode), ancount);
301 p = dbprint(p, end, ", %s: %d", p_section(ns_s_ns, (int)opcode), nscount);
302 p = dbprint(p, end, ", %s: %d", p_section(ns_s_ar, (int)opcode), arcount);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900303 }
304 if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
chenbruceacb832c2019-02-20 19:45:50 +0800305 p = dbprint(p, end, " \n");
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900306 }
chenbruceacb832c2019-02-20 19:45:50 +0800307 LOG(VERBOSE) << temp;
308
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900309 /*
310 * Print the various sections.
311 */
chenbruceacb832c2019-02-20 19:45:50 +0800312 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES);
313 do_section(statp, &handle, ns_s_an, RES_PRF_ANS);
314 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH);
315 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD);
316 if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) LOG(VERBOSE) << ";;";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900317}
318
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900319const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) {
320 char name[MAXDNAME];
321 int n;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900322
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900323 if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL);
324 if (name[0] == '\0')
325 putc('.', file);
326 else
327 fputs(name, file);
328 return (cp + n);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900329}
330
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900331const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) {
332 return (p_cdnname(cp, msg, PACKETSZ, file));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900333}
334
335/* Return a fully-qualified domain name from a compressed name (with
336 length supplied). */
337
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900338const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) {
339 int n;
340 size_t newlen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900341
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900342 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL);
343 newlen = strlen(name);
344 if (newlen == 0 || name[newlen - 1] != '.') {
345 if ((int) newlen + 1 >= namelen) /* Lack space for final dot */
346 return (NULL);
347 else
348 strcpy(name + newlen, ".");
349 }
350 return (cp + n);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900351}
352
353/* XXX: the rest of these functions need to become length-limited, too. */
354
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900355const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) {
356 char name[MAXDNAME];
357 const u_char* n;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900358
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900359 n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name);
360 if (n == NULL) return (NULL);
361 fputs(name, file);
362 return (n);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900363}
364
365/*
366 * Names of RR classes and qclasses. Classes and qclasses are the same, except
367 * that C_ANY is a qclass but not a class. (You can ask for records of class
368 * C_ANY, but you can't have any records of that class in the database.)
369 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900370static const struct res_sym p_class_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900371 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
372 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
373 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900374
375/*
376 * Names of message sections.
377 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900378static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
379 {ns_s_an, "ANSWER", (char*) 0},
380 {ns_s_ns, "AUTHORITY", (char*) 0},
381 {ns_s_ar, "ADDITIONAL", (char*) 0},
382 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900383
Bernie Innocentiac18b122018-10-01 23:10:18 +0900384static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
385 {S_PREREQ, "PREREQUISITE", (char*) 0},
386 {S_UPDATE, "UPDATE", (char*) 0},
387 {S_ADDT, "ADDITIONAL", (char*) 0},
388 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900389
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900390/*
391 * Names of RR types and qtypes. Types and qtypes are the same, except
392 * that T_ANY is a qtype but not a type. (You can ask for records of type
393 * T_ANY, but you can't have any records of that type in the database.)
394 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900395const struct res_sym p_type_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900396 {ns_t_a, "A", "address"},
397 {ns_t_ns, "NS", "name server"},
398 {ns_t_md, "MD", "mail destination (deprecated)"},
399 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
400 {ns_t_cname, "CNAME", "canonical name"},
401 {ns_t_soa, "SOA", "start of authority"},
402 {ns_t_mb, "MB", "mailbox"},
403 {ns_t_mg, "MG", "mail group member"},
404 {ns_t_mr, "MR", "mail rename"},
405 {ns_t_null, "NULL", "null"},
406 {ns_t_wks, "WKS", "well-known service (deprecated)"},
407 {ns_t_ptr, "PTR", "domain name pointer"},
408 {ns_t_hinfo, "HINFO", "host information"},
409 {ns_t_minfo, "MINFO", "mailbox information"},
410 {ns_t_mx, "MX", "mail exchanger"},
411 {ns_t_txt, "TXT", "text"},
412 {ns_t_rp, "RP", "responsible person"},
413 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
414 {ns_t_x25, "X25", "X25 address"},
415 {ns_t_isdn, "ISDN", "ISDN address"},
416 {ns_t_rt, "RT", "router"},
417 {ns_t_nsap, "NSAP", "nsap address"},
418 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
419 {ns_t_sig, "SIG", "signature"},
420 {ns_t_key, "KEY", "key"},
421 {ns_t_px, "PX", "mapping information"},
422 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
423 {ns_t_aaaa, "AAAA", "IPv6 address"},
424 {ns_t_loc, "LOC", "location"},
425 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
426 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
427 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
428 {ns_t_srv, "SRV", "server selection"},
429 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
430 {ns_t_naptr, "NAPTR", "naptr"},
431 {ns_t_kx, "KX", "key exchange"},
432 {ns_t_cert, "CERT", "certificate"},
433 {ns_t_a6, "A", "IPv6 address (experminental)"},
434 {ns_t_dname, "DNAME", "non-terminal redirection"},
435 {ns_t_opt, "OPT", "opt"},
436 {ns_t_apl, "apl", "apl"},
437 {ns_t_ds, "DS", "delegation signer"},
438 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
439 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
440 {ns_t_rrsig, "RRSIG", "rrsig"},
441 {ns_t_nsec, "NSEC", "nsec"},
442 {ns_t_dnskey, "DNSKEY", "DNS key"},
443 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
444 {ns_t_nsec3, "NSEC3", "nsec3"},
445 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
446 {ns_t_hip, "HIP", "host identity protocol"},
447 {ns_t_spf, "SPF", "sender policy framework"},
448 {ns_t_tkey, "TKEY", "tkey"},
449 {ns_t_tsig, "TSIG", "transaction signature"},
450 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
451 {ns_t_axfr, "AXFR", "zone transfer"},
452 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
453 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
454 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
455 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
456 {ns_t_kx, "KX", "Key Exchange"},
457 {ns_t_cert, "CERT", "Certificate"},
458 {ns_t_a6, "A6", "IPv6 Address"},
459 {ns_t_dname, "DNAME", "dname"},
460 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
461 {ns_t_opt, "OPT", "EDNS Options"},
462 {ns_t_any, "ANY", "\"any\""},
463 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
464 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900465
466/*
467 * Names of DNS rcodes.
468 */
Bernie Innocentiac18b122018-10-01 23:10:18 +0900469static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
470 {ns_r_formerr, "FORMERR", "format error"},
471 {ns_r_servfail, "SERVFAIL", "server failed"},
472 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
473 {ns_r_notimpl, "NOTIMP", "not implemented"},
474 {ns_r_refused, "REFUSED", "refused"},
475 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
476 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
477 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
478 {ns_r_notauth, "NOTAUTH", "not authoritative"},
479 {ns_r_notzone, "NOTZONE", "Not in zone"},
480 {ns_r_max, "", ""},
481 {ns_r_badsig, "BADSIG", "bad signature"},
482 {ns_r_badkey, "BADKEY", "bad key"},
483 {ns_r_badtime, "BADTIME", "bad time"},
484 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900485
Bernie Innocenti5c6e0b02018-09-11 18:18:22 +0900486static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900487 static char unname[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900488
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900489 for (; syms->name != 0; syms++) {
490 if (number == syms->number) {
491 if (success) *success = 1;
492 return (syms->name);
493 }
494 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900495
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900496 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
497 if (success) *success = 0;
498 return (unname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900499}
500
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900501/*
502 * Return a string for the type.
503 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900504const char* p_type(int type) {
505 int success;
506 const char* result;
507 static char typebuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900508
Bernie Innocentiac18b122018-10-01 23:10:18 +0900509 result = sym_ntos(p_type_syms, type, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900510 if (success) return (result);
511 if (type < 0 || type > 0xffff) return ("BADTYPE");
512 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
513 return (typebuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900514}
515
516/*
517 * Return a string for the type.
518 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900519const char* p_section(int section, int opcode) {
520 const struct res_sym* symbols;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900521
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900522 switch (opcode) {
523 case ns_o_update:
Bernie Innocentiac18b122018-10-01 23:10:18 +0900524 symbols = p_update_section_syms;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900525 break;
526 default:
Bernie Innocentiac18b122018-10-01 23:10:18 +0900527 symbols = p_default_section_syms;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900528 break;
529 }
530 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900531}
532
533/*
534 * Return a mnemonic for class.
535 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900536const char* p_class(int cl) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900537 int success;
538 const char* result;
539 static char classbuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900540
Bernie Innocentiac18b122018-10-01 23:10:18 +0900541 result = sym_ntos(p_class_syms, cl, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900542 if (success) return (result);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900543 if (cl < 0 || cl > 0xffff) return ("BADCLASS");
544 snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900545 return (classbuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900546}
547
548/*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900549 * Return a string for the rcode.
550 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900551const char* p_rcode(int rcode) {
Bernie Innocentiac18b122018-10-01 23:10:18 +0900552 return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900553}
chenbruceacb832c2019-02-20 19:45:50 +0800554
555android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr) {
556 android::base::LogSeverity logSeverityEnum;
557 if (logSeverityStr == "VERBOSE") {
558 logSeverityEnum = android::base::VERBOSE;
559 } else if (logSeverityStr == "DEBUG") {
560 logSeverityEnum = android::base::DEBUG;
561 } else if (logSeverityStr == "INFO") {
562 logSeverityEnum = android::base::INFO;
563 } else if (logSeverityStr == "WARNING") {
564 logSeverityEnum = android::base::WARNING;
565 } else if (logSeverityStr == "ERROR") {
566 logSeverityEnum = android::base::ERROR;
567 } else if (logSeverityStr == "FATAL_WITHOUT_ABORT") {
568 logSeverityEnum = android::base::FATAL_WITHOUT_ABORT;
569 } else if (logSeverityStr == "FATAL") {
570 logSeverityEnum = android::base::FATAL;
571 } else {
572 // Invalid parameter is treated as WARNING (default setting)
573 logSeverityEnum = android::base::WARNING;
574 }
575 LOG(INFO) << "logSeverityEnum " << logSeverityEnum;
576 return logSeverityEnum;
577}