Dead code removal - remove res->options
Bug: 135660701
Test: built, flashed, booted
Test: atest
Change-Id: Ic604d18e635a9cb373b9e258f4524a20fb1e393f
diff --git a/DnsProxyListener.cpp b/DnsProxyListener.cpp
index b77a172..0c26b17 100644
--- a/DnsProxyListener.cpp
+++ b/DnsProxyListener.cpp
@@ -164,7 +164,7 @@
// DNS-over-TLS is in use as an indicator for when to use more modern
// DNS resolution mechanics.
if (queryingViaTls(ctx->dns_netid)) {
- ctx->flags |= NET_CONTEXT_FLAG_USE_EDNS;
+ ctx->flags |= NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS;
}
}
}
diff --git a/getaddrinfo.cpp b/getaddrinfo.cpp
index 2cb2034..cc5e7ff 100644
--- a/getaddrinfo.cpp
+++ b/getaddrinfo.cpp
@@ -1614,7 +1614,10 @@
LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
- if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
+ if (n > 0 &&
+ (res->netcontext_flags &
+ (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
+ !retried) // TODO: remove the retry flag and provide a sufficient test coverage.
n = res_nopt(res, n, buf, sizeof(buf), anslen);
if (n <= 0) {
LOG(ERROR) << __func__ << ": res_nmkquery failed";
@@ -1626,9 +1629,12 @@
if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
// Record rcode from DNS response header only if no timeout.
// Keep rcode timeout for reporting later if any.
- if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
- /* if the query choked with EDNS0, retry without EDNS0 */
- if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
+ if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; // record most recent error
+ // if the query choked with EDNS0, retry without EDNS0 that when the server
+ // has no response, resovler won't retry and do nothing. Even fallback to UDP,
+ // we also has the same symptom if EDNS is enabled.
+ if ((res->netcontext_flags &
+ (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
(res->_flags & RES_F_EDNS0ERR) && !retried) {
LOG(DEBUG) << __func__ << ": retry without EDNS0";
retried = true;
diff --git a/include/netd_resolv/resolv.h b/include/netd_resolv/resolv.h
index 2739a00..400d56d 100644
--- a/include/netd_resolv/resolv.h
+++ b/include/netd_resolv/resolv.h
@@ -73,6 +73,7 @@
#define NET_CONTEXT_INVALID_UID ((uid_t) -1)
#define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
#define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
+#define NET_CONTEXT_FLAG_USE_DNS_OVER_TLS 0x00000004
// TODO: investigate having the resolver check permissions itself, either by adding support to
// libbinder_ndk or by converting IPermissionController into a stable AIDL interface.
diff --git a/res_init.cpp b/res_init.cpp
index 6ce5140..55c5afe 100644
--- a/res_init.cpp
+++ b/res_init.cpp
@@ -94,48 +94,17 @@
#include "res_state_ext.h"
#include "resolv_private.h"
-/*
- * Resolver state default settings.
- */
-
-/*
- * Set up default settings. If the configuration file exist, the values
- * there will have precedence. Otherwise, the server address is set to
- * INADDR_ANY and the default domain name comes from the gethostname().
- *
- * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
- * rather than INADDR_ANY ("0.0.0.0") as the default name server address
- * since it was noted that INADDR_ANY actually meant ``the first interface
- * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
- * it had to be "up" in order for you to reach your own name server. It
- * was later decided that since the recommended practice is to always
- * install local static routes through 127.0.0.1 for all your network
- * interfaces, that we could solve this problem without a code change.
- *
- * The configuration file should always be used, since it is the only way
- * to specify a default domain. If you are running a server on your local
- * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
- * in the configuration file.
- *
- * Return 0 if completes successfully, -1 on error
- */
+// Set up Resolver state default settings.
+// Note that res_ninit() is called with an initialized res_state,
+// the memories it allocated must be freed after the task is done.
+// Or memory leak will happen.
int res_ninit(res_state statp) {
- return res_vinit(statp, 0);
-}
-
-/* This function has to be reachable by res_data.c but not publicly. */
-int res_vinit(res_state statp, int preinit) {
- int nserv = 0; /* number of nameserver records read from file */
+ int nserv = 0; // number of nameserver records
sockaddr_union u[2];
- if ((statp->options & RES_INIT) != 0U) res_ndestroy(statp);
-
- if (!preinit) {
- statp->netid = NETID_UNSET;
- statp->options = RES_DEFAULT;
- statp->id = arc4random_uniform(65536);
- statp->_mark = MARK_UNSET;
- }
+ statp->netid = NETID_UNSET;
+ statp->id = arc4random_uniform(65536);
+ statp->_mark = MARK_UNSET;
memset(u, 0, sizeof(u));
u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
@@ -148,7 +117,7 @@
statp->_flags = 0;
statp->_u._ext.nscount = 0;
statp->_u._ext.ext = (res_state_ext*) malloc(sizeof(*statp->_u._ext.ext));
- statp->use_local_nameserver = false;
+ statp->netcontext_flags = 0;
if (statp->_u._ext.ext != NULL) {
memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
@@ -160,7 +129,6 @@
if (nserv > 0) {
statp->nscount = nserv;
- statp->options |= RES_INIT;
}
return (0);
}
@@ -192,7 +160,6 @@
void res_ndestroy(res_state statp) {
res_nclose(statp);
if (statp->_u._ext.ext != NULL) free(statp->_u._ext.ext);
- statp->options &= ~RES_INIT;
statp->_u._ext.ext = NULL;
}
@@ -282,16 +249,11 @@
void res_setnetcontext(res_state statp, const struct android_net_context* netcontext,
android::net::NetworkDnsEventReported* _Nonnull event) {
- if (statp != NULL) {
+ if (statp != nullptr) {
statp->netid = netcontext->dns_netid;
statp->uid = netcontext->uid;
statp->_mark = netcontext->dns_mark;
- if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) {
- statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC;
- }
- if (netcontext->flags & NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS) {
- statp->use_local_nameserver = true;
- }
+ statp->netcontext_flags = netcontext->flags;
statp->event = event;
}
}
diff --git a/res_mkquery.cpp b/res_mkquery.cpp
index 669f0bd..002f4e3 100644
--- a/res_mkquery.cpp
+++ b/res_mkquery.cpp
@@ -124,7 +124,7 @@
hp->id = htons(arc4random_uniform(65536));
hp->opcode = op;
hp->rd = true;
- hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
+ hp->ad = (statp->netcontext_flags & NET_CONTEXT_FLAG_USE_DNS_OVER_TLS) != 0U;
hp->rcode = NOERROR;
cp = buf + HFIXEDSZ;
ep = buf + buflen;
@@ -220,7 +220,7 @@
cp += INT16SZ;
*cp++ = NOERROR; /* extended RCODE */
*cp++ = 0; /* EDNS version */
- if (statp->options & RES_USE_DNSSEC) {
+ if (statp->netcontext_flags & NET_CONTEXT_FLAG_USE_DNS_OVER_TLS) {
LOG(DEBUG) << __func__ << ": ENDS0 DNSSEC";
flags |= NS_OPT_DNSSEC_OK;
}
diff --git a/res_query.cpp b/res_query.cpp
index 3776772..f4289e4 100644
--- a/res_query.cpp
+++ b/res_query.cpp
@@ -119,12 +119,15 @@
bool retried = false;
again:
- hp->rcode = NOERROR; /* default */
+ hp->rcode = NOERROR; // default
LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
- if (n > 0 && (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U && !retried)
+ if (n > 0 &&
+ (statp->netcontext_flags &
+ (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
+ !retried)
n = res_nopt(statp, n, buf, sizeof(buf), anslen);
if (n <= 0) {
LOG(DEBUG) << __func__ << ": mkquery failed";
@@ -133,8 +136,11 @@
}
n = res_nsend(statp, buf, n, answer, anslen, &rcode, 0);
if (n < 0) {
- /* if the query choked with EDNS0, retry without EDNS0 */
- if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
+ // If the query choked with EDNS0, retry without EDNS0 that when the server
+ // has no response, resovler won't retry and do nothing. Even fallback to UDP,
+ // we also has the same symptom if EDNS is enabled.
+ if ((statp->netcontext_flags &
+ (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
(statp->_flags & RES_F_EDNS0ERR) && !retried) {
LOG(DEBUG) << __func__ << ": retry without EDNS0";
retried = true;
diff --git a/res_send.cpp b/res_send.cpp
index 4f9e793..9595c9b 100644
--- a/res_send.cpp
+++ b/res_send.cpp
@@ -534,7 +534,7 @@
// TODO: Since we expect there is only one DNS server being queried here while this
// function tries to query all of private DNS servers. Consider moving it to other
// reasonable place. In addition, maybe add stats for private DNS.
- if (!statp->use_local_nameserver) {
+ if (!(statp->netcontext_flags & NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS)) {
bool fallback = false;
resplen = res_tls_send(statp, Slice(const_cast<u_char*>(buf), buflen),
Slice(ans, anssiz), rcode, &fallback);
@@ -1124,7 +1124,7 @@
res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
goto retry;
}
- if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
+ if (anhp->rcode == FORMERR && (statp->netcontext_flags & NET_CONTEXT_FLAG_USE_EDNS)) {
/*
* Do not retry if the server do not understand EDNS0.
* The case has to be captured here, as FORMERR packet do not
diff --git a/res_state.cpp b/res_state.cpp
index 480d5c5..d4fa819 100644
--- a/res_state.cpp
+++ b/res_state.cpp
@@ -99,9 +99,6 @@
}
pthread_setspecific(_res_key, rt);
- /* Reset the state, note that res_ninit() can now properly reset
- * an existing state without leaking memory.
- */
LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt
<< " setting DNS state (options=" << rt->_nres->options << ")";
if (res_ninit(rt->_nres) < 0) {
diff --git a/resolv_private.h b/resolv_private.h
index 1e10849..0e0cbad 100644
--- a/resolv_private.h
+++ b/resolv_private.h
@@ -116,8 +116,8 @@
} _ext;
} _u;
struct res_static rstatic[1];
- bool use_local_nameserver; /* DNS-over-TLS bypass */
android::net::NetworkDnsEventReported* event;
+ uint32_t netcontext_flags;
};
typedef struct __res_state* res_state;
@@ -153,15 +153,6 @@
#define RES_F_EDNS0ERR 0x00000004 // EDNS0 caused errors
/*
- * Resolver options (keep these in synch with res_debug.c, please)
- */
-#define RES_INIT 0x00000001 /* address initialized */
-#define RES_USE_DNSSEC 0x00200000 /* use DNSSEC using OK bit in OPT */
-#define RES_USE_EDNS0 0x40000000 /* use EDNS0 if configured */
-
-#define RES_DEFAULT 0
-
-/*
* Error code extending h_errno codes defined in bionic/libc/include/netdb.h.
*
* This error code, including legacy h_errno, is returned from res_nquery(), res_nsearch(),
@@ -205,7 +196,6 @@
int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t);
void res_nclose(res_state);
int res_nopt(res_state, int, u_char*, int, int);
-int res_vinit(res_state, int);
void res_ndestroy(res_state);
void res_setservers(res_state, const sockaddr_union*, int);
int res_getservers(res_state, sockaddr_union*, int);