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