Move resolver public headers to separate include path

Additionally rename functions and structs to eliminate those ugly
underscores from the public API (with the notable exception of
__res_params, which will come in its own separate change to keep this
one from growing too large).

Test: build, flash, then 'atest netd_integration_test'
Change-Id: I7e9b0ee6cdbec889e9c35b5a17b5daa65533686e
diff --git a/Android.bp b/Android.bp
index 470b219..ff97e9b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -31,7 +31,7 @@
     ldflags: [
         "-Wl,--exclude-libs,ALL",
     ],
-    export_include_dirs: ["."],  // Export resolv_netid.h
+    export_include_dirs: ["include"],
     // TODO: pie in the sky: make this code clang-tidy clean
     tidy: false,
     product_variables: {
diff --git a/getaddrinfo.cpp b/getaddrinfo.cpp
index 123f609..6edff5a 100644
--- a/getaddrinfo.cpp
+++ b/getaddrinfo.cpp
@@ -94,14 +94,12 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/un.h>
-#include <unistd.h>
-#include "resolv_cache.h"
-#include "resolv_netid.h"
-#include "resolv_private.h"
-
-#include <stdarg.h>
 #include <syslog.h>
+#include <unistd.h>
 
+#include "netd_resolv/resolv.h"
+#include "resolv_cache.h"
+#include "resolv_private.h"
 
 typedef union sockaddr_union {
     struct sockaddr generic;
diff --git a/gethnamaddr.cpp b/gethnamaddr.cpp
index 5cdcd1d..dfed7a9 100644
--- a/gethnamaddr.cpp
+++ b/gethnamaddr.cpp
@@ -69,8 +69,8 @@
 #include <syslog.h>
 #include <unistd.h>
 
+#include "netd_resolv/resolv.h"
 #include "resolv_cache.h"
-#include "resolv_netid.h"
 #include "resolv_private.h"
 
 // NetBSD uses _DIAGASSERT to null-check arguments and the like,
diff --git a/hostent.h b/hostent.h
index ae9ed6d..9dda26f 100644
--- a/hostent.h
+++ b/hostent.h
@@ -28,11 +28,10 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-#ifndef _DNS_NET_HOSTENT_H
-#define _DNS_NET_HOSTENT_H
+#ifndef NETD_RESOLV_HOSTENT_H
+#define NETD_RESOLV_HOSTENT_H
 
 #include <netdb.h>
-#include <stdarg.h>
 #include <stdio.h>
 
 struct getnamaddr {
@@ -68,4 +67,4 @@
         HENT_COPY(dst, src, _len, ptr, len); \
     } while (0)
 
-#endif /* _DNS_NET_HOSTENT_H */
+#endif  // NETD_RESOLV_HOSTENT_H
diff --git a/resolv_params.h b/include/netd_resolv/params.h
similarity index 84%
rename from resolv_params.h
rename to include/netd_resolv/params.h
index 4930d74..60b9b54 100644
--- a/resolv_params.h
+++ b/include/netd_resolv/params.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef _RESOLV_PARAMS_H
-#define _RESOLV_PARAMS_H
+#ifndef NETD_RESOLV_PARAMS_H
+#define NETD_RESOLV_PARAMS_H
 
 #include <stdint.h>
 
@@ -25,7 +25,8 @@
 #define MAXDNSRCHPATH 256 /* max length of domain search paths */
 #define MAXNSSAMPLES 64   /* max # samples to store per server */
 
-/* per-netid configuration parameters passed from netd to the resolver */
+// Per-netid configuration parameters passed from netd to the resolver
+// TODO: rename to res_params
 struct __res_params {
     uint16_t sample_validity;  // sample lifetime in s
     // threshold of success / total samples below which a server is considered broken
@@ -37,12 +38,12 @@
 
 typedef enum { res_goahead, res_nextns, res_modified, res_done, res_error } res_sendhookact;
 
-typedef res_sendhookact (*res_send_qhook)(struct sockaddr* const*, const u_char**, int*, u_char*,
+typedef res_sendhookact (*res_send_qhook)(struct sockaddr* const*, const uint8_t**, int*, uint8_t*,
                                           int, int*);
 
-typedef res_sendhookact (*res_send_rhook)(const struct sockaddr*, const u_char*, int, u_char*, int,
-                                          int*);
+typedef res_sendhookact (*res_send_rhook)(const struct sockaddr*, const uint8_t*, int, uint8_t*,
+                                          int, int*);
 
 #define LIBNETD_RESOLV_PUBLIC extern "C" [[gnu::visibility("default")]]
 
-#endif  // _RESOLV_PARAMS_H
+#endif  // NETD_RESOLV_PARAMS_H
diff --git a/resolv_netid.h b/include/netd_resolv/resolv.h
similarity index 80%
rename from resolv_netid.h
rename to include/netd_resolv/resolv.h
index aa4757f..e921958 100644
--- a/resolv_netid.h
+++ b/include/netd_resolv/resolv.h
@@ -25,17 +25,16 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#ifndef _RESOLV_NETID_H
-#define _RESOLV_NETID_H
+#ifndef NETD_RESOLV_RESOLV_H
+#define NETD_RESOLV_RESOLV_H
 
-/* This header contains declarations related to per-network DNS
- * server selection. They are used by system/netd/ and should not be
- * exposed by the C library's public NDK headers.
+/*
+ * This header contains declarations related to per-network DNS server selection.
+ * They are used by system/netd/ and should not be exposed by the public NDK headers.
  */
 #include <netinet/in.h>
-#include <stdio.h>
 
-#include "resolv_params.h"
+#include "params.h"
 
 /*
  * Passing NETID_UNSET as the netId causes system/netd/server/DnsProxyListener.cpp to
@@ -81,9 +80,8 @@
 LIBNETD_RESOLV_PUBLIC hostent* android_gethostbyaddrfornet(const void*, socklen_t, int, unsigned,
                                                            unsigned);
 LIBNETD_RESOLV_PUBLIC hostent* android_gethostbynamefornet(const char*, int, unsigned, unsigned);
-LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornet(const char*, const char*,
-                                                    const addrinfo*, unsigned, unsigned,
-                                                    addrinfo**);
+LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornet(const char*, const char*, const addrinfo*,
+                                                    unsigned, unsigned, addrinfo**);
 /*
  * TODO: consider refactoring android_getaddrinfo_proxy() to serve as an
  * explore_fqdn() dispatch table method, with the below function only making DNS calls.
@@ -97,14 +95,14 @@
                                                            const android_net_context*, addrinfo**);
 
 // Set name servers for a network
-LIBNETD_RESOLV_PUBLIC int _resolv_set_nameservers_for_net(unsigned netid, const char** servers,
-                                                          unsigned numservers, const char* domains,
-                                                          const __res_params* params);
+LIBNETD_RESOLV_PUBLIC int resolv_set_nameservers_for_net(unsigned netid, const char** servers,
+                                                         unsigned numservers, const char* domains,
+                                                         const __res_params* params);
 
 // Flush the cache associated with a certain network
-LIBNETD_RESOLV_PUBLIC void _resolv_flush_cache_for_net(unsigned netid);
+LIBNETD_RESOLV_PUBLIC void resolv_flush_cache_for_net(unsigned netid);
 
 // Delete the cache associated with a certain network
-LIBNETD_RESOLV_PUBLIC void _resolv_delete_cache_for_net(unsigned netid);
+LIBNETD_RESOLV_PUBLIC void resolv_delete_cache_for_net(unsigned netid);
 
-#endif /* _RESOLV_NETID_H */
+#endif  // NETD_RESOLV_RESOLV_H
diff --git a/resolv_stats.h b/include/netd_resolv/stats.h
similarity index 74%
rename from resolv_stats.h
rename to include/netd_resolv/stats.h
index 2d88be6..c504115 100644
--- a/resolv_stats.h
+++ b/include/netd_resolv/stats.h
@@ -14,32 +14,29 @@
  * limitations under the License.
  */
 
-#ifndef _RES_STATS_H_
-#define _RES_STATS_H_
+#ifndef NETD_RES_STATS_H
+#define NETD_RES_STATS_H
 
 #include <stdbool.h>
 #include <stdint.h>
 #include <sys/socket.h>
 #include <time.h>
 
-#include "resolv_params.h"
+#include "params.h"
 
 #define RCODE_INTERNAL_ERROR 254
 #define RCODE_TIMEOUT 255
 
-/*
- * Resolver reachability statistics and run-time parameters.
- */
-
-struct __res_sample {
+struct res_sample {
     time_t at;      // time in s at which the sample was recorded
     uint16_t rtt;   // round-trip time in ms
     uint8_t rcode;  // the DNS rcode or RCODE_XXX defined above
 };
 
-struct __res_stats {
+// Resolver reachability statistics and run-time parameters.
+struct res_stats {
     // Stats of the last <sample_count> queries.
-    struct __res_sample samples[MAXNSSAMPLES];
+    res_sample samples[MAXNSSAMPLES];
     // The number of samples stored.
     uint8_t sample_count;
     // The next sample to modify.
@@ -47,19 +44,18 @@
 };
 
 // Aggregates the reachability statistics for the given server based on on the stored samples.
-LIBNETD_RESOLV_PUBLIC void android_net_res_stats_aggregate(__res_stats* stats,
-                                                           int* successes, int* errors,
-                                                           int* timeouts, int* internal_errors,
-                                                           int* rtt_avg, time_t* last_sample_time);
+LIBNETD_RESOLV_PUBLIC void android_net_res_stats_aggregate(res_stats* stats, int* successes,
+                                                           int* errors, int* timeouts,
+                                                           int* internal_errors, int* rtt_avg,
+                                                           time_t* last_sample_time);
 
 LIBNETD_RESOLV_PUBLIC int android_net_res_stats_get_info_for_net(
         unsigned netid, int* nscount, sockaddr_storage servers[MAXNS], int* dcount,
-        char domains[MAXDNSRCH][MAXDNSRCHPATH], __res_params* params, __res_stats stats[MAXNS]);
+        char domains[MAXDNSRCH][MAXDNSRCHPATH], __res_params* params, res_stats stats[MAXNS]);
 
 // Returns an array of bools indicating which servers are considered good
 LIBNETD_RESOLV_PUBLIC void android_net_res_stats_get_usable_servers(const __res_params* params,
-                                                                    __res_stats stats[],
-                                                                    int nscount,
+                                                                    res_stats stats[], int nscount,
                                                                     bool valid_servers[]);
 
-#endif  // _RES_STATS_H_
+#endif  // NETD_RES_STATS_H
diff --git a/res_cache.cpp b/res_cache.cpp
index 880037d..8eb20e2 100644
--- a/res_cache.cpp
+++ b/res_cache.cpp
@@ -49,9 +49,9 @@
 
 #include <android-base/logging.h>
 
-#include "res_private.h"
+#include "netd_resolv/resolv.h"
+#include "res_state_ext.h"
 #include "resolv_cache.h"
-#include "resolv_netid.h"
 #include "resolv_private.h"
 
 #define VLOG if (!kVerboseLogging) {} else LOG(INFO)
@@ -1149,7 +1149,7 @@
     struct addrinfo* nsaddrinfo[MAXNS];
     int revision_id;  // # times the nameservers have been replaced
     struct __res_params params;
-    struct __res_stats nsstats[MAXNS];
+    struct res_stats nsstats[MAXNS];
     char defdname[MAXDNSRCHPATH];
     int dnsrch_offset[MAXDNSRCH + 1];  // offsets into defdname
 };
@@ -1693,7 +1693,7 @@
     res_cache_clear_stats_locked(cache_info);
 }
 
-void _resolv_delete_cache_for_net(unsigned netid) {
+void resolv_delete_cache_for_net(unsigned netid) {
     pthread_once(&_res_cache_once, res_cache_init);
     pthread_mutex_lock(&res_cache_list_lock);
 
@@ -1755,8 +1755,8 @@
     params->base_timeout_msec = 0;  // 0 = legacy algorithm
 }
 
-int _resolv_set_nameservers_for_net(unsigned netid, const char** servers, unsigned numservers,
-                                    const char* domains, const struct __res_params* params) {
+int resolv_set_nameservers_for_net(unsigned netid, const char** servers, unsigned numservers,
+                                   const char* domains, const __res_params* params) {
     char sbuf[NI_MAXSERV];
     char* cp;
     int* offset;
@@ -1953,8 +1953,8 @@
 
 /* Resolver reachability statistics. */
 
-static void _res_cache_add_stats_sample_locked(struct __res_stats* stats,
-                                               const struct __res_sample* sample, int max_samples) {
+static void _res_cache_add_stats_sample_locked(res_stats* stats, const res_sample* sample,
+                                               int max_samples) {
     // Note: This function expects max_samples > 0, otherwise a (harmless) modification of the
     // allocated but supposedly unused memory for samples[0] will happen
     VLOG << __func__ << ": adding sample to stats, next = " << stats->sample_next
@@ -1980,7 +1980,7 @@
                                            struct sockaddr_storage servers[MAXNS], int* dcount,
                                            char domains[MAXDNSRCH][MAXDNSRCHPATH],
                                            struct __res_params* params,
-                                           struct __res_stats stats[MAXNS]) {
+                                           struct res_stats stats[MAXNS]) {
     int revision_id = -1;
     pthread_mutex_lock(&res_cache_list_lock);
 
@@ -2028,7 +2028,7 @@
             // dnsrch_offset[i] can either be -1 or point to an empty string to indicate the end
             // of the search offsets. Checking for < 0 is not strictly necessary, but safer.
             // TODO: Pass in a search domain array instead of a string to
-            // _resolv_set_nameservers_for_net() and make this double check unnecessary.
+            // resolv_set_nameservers_for_net() and make this double check unnecessary.
             if (info->dnsrch_offset[i] < 0 ||
                 ((size_t) info->dnsrch_offset[i]) >= sizeof(info->defdname) || !cur_domain[0]) {
                 break;
@@ -2044,8 +2044,7 @@
     return revision_id;
 }
 
-int _resolv_cache_get_resolver_stats(unsigned netid, struct __res_params* params,
-                                     struct __res_stats stats[MAXNS]) {
+int resolv_cache_get_resolver_stats(unsigned netid, __res_params* params, res_stats stats[MAXNS]) {
     int revision_id = -1;
     pthread_mutex_lock(&res_cache_list_lock);
 
@@ -2061,7 +2060,7 @@
 }
 
 void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
-                                             const struct __res_sample* sample, int max_samples) {
+                                             const res_sample* sample, int max_samples) {
     if (max_samples <= 0) return;
 
     pthread_mutex_lock(&res_cache_list_lock);
diff --git a/res_comp.cpp b/res_comp.cpp
index 1eb00c7..98461b3 100644
--- a/res_comp.cpp
+++ b/res_comp.cpp
@@ -75,11 +75,12 @@
 #include <netinet/in.h>
 #include <sys/param.h>
 #include <sys/types.h>
-#include "resolv_private.h"
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
+#include "resolv_private.h"
+
 /*
  * Expand compressed domain name 'src' to full domain name.
  * 'msg' is a pointer to the begining of the message,
diff --git a/res_data.cpp b/res_data.cpp
index 477d2a9..fc5a6a3 100644
--- a/res_data.cpp
+++ b/res_data.cpp
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+
 #include "resolv_private.h"
 
 extern const char* const _res_opcodes[] = {
diff --git a/res_debug.cpp b/res_debug.cpp
index e78ef53..56390db 100644
--- a/res_debug.cpp
+++ b/res_debug.cpp
@@ -112,8 +112,14 @@
 #include <string.h>
 #include <strings.h>
 #include <time.h>
+
 #include "resolv_private.h"
 
+struct res_sym {
+    int number;            /* Identifying number, like T_MX */
+    const char* name;      /* Its symbolic name, like "MX" */
+    const char* humanname; /* Its fun name, like "mail exchanger" */
+};
 
 extern const char* const _res_opcodes[];
 
@@ -328,7 +334,7 @@
  * that C_ANY is a qclass but not a class.  (You can ask for records of class
  * C_ANY, but you can't have any records of that class in the database.)
  */
-const struct res_sym __p_class_syms[] = {
+static const struct res_sym p_class_syms[] = {
         {C_IN, "IN", (char*) 0},     {C_CHAOS, "CH", (char*) 0},  {C_CHAOS, "CHAOS", (char*) 0},
         {C_HS, "HS", (char*) 0},     {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
         {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
@@ -336,24 +342,24 @@
 /*
  * Names of message sections.
  */
-static const struct res_sym __p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
-                                                          {ns_s_an, "ANSWER", (char*) 0},
-                                                          {ns_s_ns, "AUTHORITY", (char*) 0},
-                                                          {ns_s_ar, "ADDITIONAL", (char*) 0},
-                                                          {0, (char*) 0, (char*) 0}};
+static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
+                                                        {ns_s_an, "ANSWER", (char*) 0},
+                                                        {ns_s_ns, "AUTHORITY", (char*) 0},
+                                                        {ns_s_ar, "ADDITIONAL", (char*) 0},
+                                                        {0, (char*) 0, (char*) 0}};
 
-static const struct res_sym __p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
-                                                         {S_PREREQ, "PREREQUISITE", (char*) 0},
-                                                         {S_UPDATE, "UPDATE", (char*) 0},
-                                                         {S_ADDT, "ADDITIONAL", (char*) 0},
-                                                         {0, (char*) 0, (char*) 0}};
+static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
+                                                       {S_PREREQ, "PREREQUISITE", (char*) 0},
+                                                       {S_UPDATE, "UPDATE", (char*) 0},
+                                                       {S_ADDT, "ADDITIONAL", (char*) 0},
+                                                       {0, (char*) 0, (char*) 0}};
 
 /*
  * Names of RR types and qtypes.  Types and qtypes are the same, except
  * that T_ANY is a qtype but not a type.  (You can ask for records of type
  * T_ANY, but you can't have any records of that type in the database.)
  */
-const struct res_sym __p_type_syms[] = {
+const struct res_sym p_type_syms[] = {
         {ns_t_a, "A", "address"},
         {ns_t_ns, "NS", "name server"},
         {ns_t_md, "MD", "mail destination (deprecated)"},
@@ -427,22 +433,22 @@
 /*
  * Names of DNS rcodes.
  */
-const struct res_sym __p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
-                                         {ns_r_formerr, "FORMERR", "format error"},
-                                         {ns_r_servfail, "SERVFAIL", "server failed"},
-                                         {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
-                                         {ns_r_notimpl, "NOTIMP", "not implemented"},
-                                         {ns_r_refused, "REFUSED", "refused"},
-                                         {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
-                                         {ns_r_yxrrset, "YXRRSET", "rrset exists"},
-                                         {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
-                                         {ns_r_notauth, "NOTAUTH", "not authoritative"},
-                                         {ns_r_notzone, "NOTZONE", "Not in zone"},
-                                         {ns_r_max, "", ""},
-                                         {ns_r_badsig, "BADSIG", "bad signature"},
-                                         {ns_r_badkey, "BADKEY", "bad key"},
-                                         {ns_r_badtime, "BADTIME", "bad time"},
-                                         {0, NULL, NULL}};
+static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
+                                              {ns_r_formerr, "FORMERR", "format error"},
+                                              {ns_r_servfail, "SERVFAIL", "server failed"},
+                                              {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
+                                              {ns_r_notimpl, "NOTIMP", "not implemented"},
+                                              {ns_r_refused, "REFUSED", "refused"},
+                                              {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
+                                              {ns_r_yxrrset, "YXRRSET", "rrset exists"},
+                                              {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
+                                              {ns_r_notauth, "NOTAUTH", "not authoritative"},
+                                              {ns_r_notzone, "NOTZONE", "Not in zone"},
+                                              {ns_r_max, "", ""},
+                                              {ns_r_badsig, "BADSIG", "bad signature"},
+                                              {ns_r_badkey, "BADKEY", "bad key"},
+                                              {ns_r_badtime, "BADTIME", "bad time"},
+                                              {0, NULL, NULL}};
 
 static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
     static char unname[20];
@@ -467,7 +473,7 @@
     const char* result;
     static char typebuf[20];
 
-    result = sym_ntos(__p_type_syms, type, &success);
+    result = sym_ntos(p_type_syms, type, &success);
     if (success) return (result);
     if (type < 0 || type > 0xffff) return ("BADTYPE");
     snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
@@ -482,10 +488,10 @@
 
     switch (opcode) {
         case ns_o_update:
-            symbols = __p_update_section_syms;
+            symbols = p_update_section_syms;
             break;
         default:
-            symbols = __p_default_section_syms;
+            symbols = p_default_section_syms;
             break;
     }
     return (sym_ntos(symbols, section, (int*) 0));
@@ -499,7 +505,7 @@
     const char* result;
     static char classbuf[20];
 
-    result = sym_ntos(__p_class_syms, cl, &success);
+    result = sym_ntos(p_class_syms, cl, &success);
     if (success) return (result);
     if (cl < 0 || cl > 0xffff) return ("BADCLASS");
     snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
@@ -510,5 +516,5 @@
  * Return a string for the rcode.
  */
 const char* p_rcode(int rcode) {
-    return (sym_ntos(__p_rcode_syms, rcode, (int*) 0));
+    return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
 }
diff --git a/res_init.cpp b/res_init.cpp
index d222ac2..05404cb 100644
--- a/res_init.cpp
+++ b/res_init.cpp
@@ -88,12 +88,10 @@
 #include <string.h>
 #include <unistd.h>
 
-/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
-#include "resolv_netid.h"
+#include "netd_resolv/resolv.h"
+#include "res_state_ext.h"
 #include "resolv_private.h"
 
-#include "res_private.h"
-
 /* Options.  Should all be left alone. */
 #ifndef DEBUG
 #define DEBUG
@@ -163,7 +161,7 @@
     statp->qhook = NULL;
     statp->rhook = NULL;
     statp->_u._ext.nscount = 0;
-    statp->_u._ext.ext = (struct __res_state_ext*) malloc(sizeof(*statp->_u._ext.ext));
+    statp->_u._ext.ext = (res_state_ext*) malloc(sizeof(*statp->_u._ext.ext));
     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;
@@ -214,7 +212,7 @@
 static void res_setoptions(res_state statp, const char* options, const char* source) {
     const char* cp = options;
     int i;
-    struct __res_state_ext* ext = statp->_u._ext.ext;
+    res_state_ext* ext = statp->_u._ext.ext;
 
 #ifdef DEBUG
     if (statp->options & RES_DEBUG)
diff --git a/res_query.cpp b/res_query.cpp
index 0b46409..a9e4220 100644
--- a/res_query.cpp
+++ b/res_query.cpp
@@ -78,13 +78,14 @@
 #include <netinet/in.h>
 #include <sys/param.h>
 #include <sys/types.h>
-#include "resolv_cache.h"
-#include "resolv_private.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#include "resolv_cache.h"
+#include "resolv_private.h"
+
 /* Options.  Leave them on. */
 #ifndef DEBUG
 #define DEBUG
diff --git a/res_send.cpp b/res_send.cpp
index 95605f4..4f73b59 100644
--- a/res_send.cpp
+++ b/res_send.cpp
@@ -100,12 +100,12 @@
 
 #include <android-base/logging.h>
 
+#include "netd_resolv/resolv.h"
+#include "netd_resolv/stats.h"
 #include "private/android_filesystem_config.h"
-#include "res_private.h"
+#include "res_state_ext.h"
 #include "resolv_cache.h"
-#include "resolv_netid.h"
 #include "resolv_private.h"
-#include "resolv_stats.h"
 
 #define EXT(res) ((res)->_u._ext)
 
@@ -501,9 +501,9 @@
      * Send request, RETRY times, or until successful.
      */
     for (int attempt = 0; attempt < statp->retry; ++attempt) {
-        struct __res_stats stats[MAXNS];
+        struct res_stats stats[MAXNS];
         struct __res_params params;
-        int revision_id = _resolv_cache_get_resolver_stats(statp->netid, &params, stats);
+        int revision_id = resolv_cache_get_resolver_stats(statp->netid, &params, stats);
         bool usable_servers[MAXNS];
         android_net_res_stats_get_usable_servers(&params, stats, statp->nscount, usable_servers);
 
@@ -572,7 +572,7 @@
                  * SERVFAIL or times out) do not unduly affect the stats.
                  */
                 if (attempt == 0) {
-                    struct __res_sample sample;
+                    res_sample sample;
                     _res_stats_set_sample(&sample, now, rcode, delay);
                     _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
                                                             params.max_samples);
@@ -592,7 +592,7 @@
 
                 /* Only record stats the first time we try a query. See above. */
                 if (attempt == 0) {
-                    struct __res_sample sample;
+                    res_sample sample;
                     _res_stats_set_sample(&sample, now, rcode, delay);
                     _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
                                                             params.max_samples);
diff --git a/res_state.cpp b/res_state.cpp
index 738637b..900c56a 100644
--- a/res_state.cpp
+++ b/res_state.cpp
@@ -132,7 +132,7 @@
     return rt ? rt->_nres : NULL;
 }
 
-void res_put_state(res_state res __unused) {
+void res_put_state(res_state /*res*/) {
     /* nothing to do */
 }
 
diff --git a/res_private.h b/res_state_ext.h
similarity index 70%
rename from res_private.h
rename to res_state_ext.h
index 0fb9b5c..cf906f0 100644
--- a/res_private.h
+++ b/res_state_ext.h
@@ -1,11 +1,12 @@
 /*	$NetBSD: res_private.h,v 1.1.1.1 2004/05/20 17:18:54 christos Exp $	*/
 
-#ifndef _RES_PRIVATE_H_
-#define _RES_PRIVATE_H_
+#ifndef NETD_RES_STATE_EXT_H
+#define NETD_RES_STATE_EXT_H
 
 #include "resolv_private.h"
 
-struct __res_state_ext {
+// TODO: consider inlining into res_state
+struct res_state_ext {
     union res_sockaddr_union nsaddrs[MAXNS];
     struct sort_list {
         int af;
@@ -18,4 +19,4 @@
     char nsuffix2[64];
 };
 
-#endif  // _RES_PRIVATE_H_
+#endif  // NETD_RES_STATE_EXT_H
diff --git a/res_stats.cpp b/res_stats.cpp
index e6c2b65..4001206 100644
--- a/res_stats.cpp
+++ b/res_stats.cpp
@@ -23,7 +23,7 @@
 
 #include <android-base/logging.h>
 
-#include "resolv_stats.h"
+#include "netd_resolv/stats.h"
 
 #define VLOG if (!kVerboseLogging) {} else LOG(INFO)
 
@@ -42,7 +42,7 @@
 }
 
 // Create a sample for calculating server reachability statistics.
-void _res_stats_set_sample(__res_sample* sample, time_t now, int rcode, int rtt) {
+void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt) {
     VLOG << __func__ << ": rcode = " << rcode << ", sec = " << rtt;
     sample->at = now;
     sample->rcode = rcode;
@@ -50,14 +50,13 @@
 }
 
 /* Clears all stored samples for the given server. */
-void _res_stats_clear_samples(struct __res_stats* stats) {
+void _res_stats_clear_samples(res_stats* stats) {
     stats->sample_count = stats->sample_next = 0;
 }
 
 /* Aggregates the reachability statistics for the given server based on on the stored samples. */
-void android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
-                                     int* timeouts, int* internal_errors, int* rtt_avg,
-                                     time_t* last_sample_time) {
+void android_net_res_stats_aggregate(res_stats* stats, int* successes, int* errors, int* timeouts,
+                                     int* internal_errors, int* rtt_avg, time_t* last_sample_time) {
     int s = 0;   // successes
     int e = 0;   // errors
     int t = 0;   // timouts
@@ -119,7 +118,7 @@
 // Returns true if the server is considered unusable, i.e. if the success rate is not lower than the
 // threshold for the stored stored samples. If not enough samples are stored, the server is
 // considered usable.
-static bool res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
+static bool res_stats_usable_server(const struct __res_params* params, res_stats* stats) {
     int successes = -1;
     int errors = -1;
     int timeouts = -1;
@@ -158,9 +157,8 @@
     return 1;
 }
 
-void android_net_res_stats_get_usable_servers(const struct __res_params* params,
-                                              struct __res_stats stats[], int nscount,
-                                              bool usable_servers[]) {
+void android_net_res_stats_get_usable_servers(const struct __res_params* params, res_stats stats[],
+                                              int nscount, bool usable_servers[]) {
     unsigned usable_servers_found = 0;
     for (int ns = 0; ns < nscount; ns++) {
         bool usable = res_stats_usable_server(params, &stats[ns]);
diff --git a/resolv_cache.h b/resolv_cache.h
index cc7b5b7..dd8aa80 100644
--- a/resolv_cache.h
+++ b/resolv_cache.h
@@ -25,8 +25,8 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#ifndef _RESOLV_CACHE_H_
-#define _RESOLV_CACHE_H_
+#ifndef NETD_RESOLV_CACHE_H
+#define NETD_RESOLV_CACHE_H
 
 #include <stddef.h>
 
@@ -57,4 +57,4 @@
 /* Notify the cache a request failed */
 void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen);
 
-#endif /* _RESOLV_CACHE_H_ */
+#endif  // NETD_RESOLV_CACHE_H
diff --git a/resolv_private.h b/resolv_private.h
index a26ef69..72fbe7c 100644
--- a/resolv_private.h
+++ b/resolv_private.h
@@ -51,15 +51,16 @@
  *	Id: resolv.h,v 1.7.2.11.4.2 2004/06/25 00:41:05 marka Exp
  */
 
-#ifndef _RESOLV_PRIVATE_H_
-#define _RESOLV_PRIVATE_H_
+#ifndef NETD_RESOLV_PRIVATE_H
+#define NETD_RESOLV_PRIVATE_H
 
 #include <net/if.h>
 #include <resolv.h>
 #include <time.h>
-#include "resolv_params.h"
+
+#include "netd_resolv/params.h"
+#include "netd_resolv/stats.h"
 #include "resolv_static.h"
-#include "resolv_stats.h"
 
 // Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
 // RFC 1034 and RFC 1035 is 255 octets.
@@ -89,12 +90,6 @@
 
 #define RES_SET_H_ERRNO(r, x) (h_errno = (r)->res_h_errno = (x))
 
-struct res_sym {
-    int number;            /* Identifying number, like T_MX */
-    const char* name;      /* Its symbolic name, like "MX" */
-    const char* humanname; /* Its fun name, like "mail exchanger" */
-};
-
 /*
  * Global defines and variables for resolver stub.
  */
@@ -109,7 +104,7 @@
 #define RES_DFLRETRY 2    /* Default #/tries. */
 #define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
 
-struct __res_state_ext;
+struct res_state_ext;
 
 struct __res_state {
     unsigned netid;                        /* NetId: cache key and socket mark */
@@ -144,7 +139,7 @@
             uint16_t nscount;
             uint16_t nstimes[MAXNS]; /* ms. */
             int nssocks[MAXNS];
-            struct __res_state_ext* ext; /* extention for IPv6 */
+            struct res_state_ext* ext; /* extention for IPv6 */
         } _ext;
     } _u;
     struct res_static rstatic[1];
@@ -155,21 +150,19 @@
 /* Retrieve a local copy of the stats for the given netid. The buffer must have space for
  * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
  */
-int _resolv_cache_get_resolver_stats(unsigned netid, struct __res_params* params,
-                                            struct __res_stats stats[MAXNS]);
+int resolv_cache_get_resolver_stats(unsigned netid, __res_params* params, res_stats stats[MAXNS]);
 
 /* Add a sample to the shared struct for the given netid and server, provided that the
  * revision_id of the stored servers has not changed.
  */
 void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
-                                             const struct __res_sample* sample, int max_samples);
-
+                                             const res_sample* sample, int max_samples);
 
 // Calculate the round-trip-time from start time t0 and end time t1.
 int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
 
 // Create a sample for calculating server reachability statistics.
-void _res_stats_set_sample(__res_sample* sample, time_t now, int rcode, int rtt);
+void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
 
 /* End of stats related definitions */
 
@@ -308,4 +301,4 @@
 
 u_int res_randomid(void);
 
-#endif /* !_RESOLV_PRIVATE_H_ */
+#endif  // NETD_RESOLV_PRIVATE_H
diff --git a/resolv_static.h b/resolv_static.h
index 87bdce2..b250892 100644
--- a/resolv_static.h
+++ b/resolv_static.h
@@ -1,5 +1,5 @@
-#ifndef _RESOLV_STATIC_H_
-#define _RESOLV_STATIC_H_
+#ifndef NETD_RESOLV_STATIC_H
+#define NETD_RESOLV_STATIC_H
 
 #include <netdb.h>
 #include <stdio.h>
@@ -28,6 +28,6 @@
     struct hostent host;
 };
 
-res_static* res_get_static(void);
+res_static* res_get_static();
 
-#endif // _RESOLV_STATIC_H_
+#endif  // NETD_RESOLV_STATIC_H