nsdispatch removal from gethostbyname()

This gets rid of several layers of obfuscation in between nsdispatch()
and the functions which actually lookup for a hostname in the hosts
file and dns.

This opens the way for even more cleanups, which I will leave for
another day; let's first rip off nsdispatch() from everywhere, which
lets us kill another dependency on an internal bionic header.

Test: atest netd_integration_test

Change-Id: I911fb254939ecc9661695d0ca5474c3caeb4c1d9
diff --git a/resolv/gethnamaddr.cpp b/resolv/gethnamaddr.cpp
index 604f9c1..338f2af 100644
--- a/resolv/gethnamaddr.cpp
+++ b/resolv/gethnamaddr.cpp
@@ -138,15 +138,10 @@
 static void map_v4v6_hostent(struct hostent*, char**, char*);
 static void addrsort(char**, int, res_state);
 
-void ht_sethostent(int);
-void ht_endhostent(void);
 struct hostent* ht_gethostbyname(char*);
 struct hostent* ht_gethostbyaddr(const char*, int, int);
-void dns_service(void);
-#undef dn_skipname
-int dn_skipname(const u_char*, const u_char*);
 static int _dns_gethtbyaddr(void*, void*, va_list);
-static int _dns_gethtbyname(void*, void*, va_list);
+static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info);
 
 static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
                                               size_t, int*, const struct android_net_context*);
@@ -562,11 +557,6 @@
     struct getnamaddr info;
     char hbuf[MAXHOSTNAMELEN];
     size_t size;
-    static const ns_dtab dtab[] = {
-        {NSSRC_FILES, _hf_gethtbyname,  NULL},
-        {NSSRC_DNS,   _dns_gethtbyname, NULL},
-        {0, 0, 0}
-    };
 
     _DIAGASSERT(name != NULL);
 
@@ -630,9 +620,11 @@
     info.buf = buf;
     info.buflen = buflen;
     info.he = he;
-    if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname", default_dns_files, name, strlen(name),
-                   af) != NS_SUCCESS)
-        return NULL;
+    if (!_hf_gethtbyname2(name, af, &info)) {
+        if (_dns_gethtbyname(name, af, &info) != NS_SUCCESS) {
+            return NULL;
+        }
+    }
     *he = NETDB_SUCCESS;
     return hp;
 nospc:
@@ -926,18 +918,12 @@
     }
 }
 
-static int _dns_gethtbyname(void* rv, void* /*cb_data*/, va_list ap) {
+static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
     int n, type;
     struct hostent* hp;
-    const char* name;
     res_state res;
-    struct getnamaddr* info = (struct getnamaddr*) rv;
 
-    _DIAGASSERT(rv != NULL);
-
-    name = va_arg(ap, char*);
-    /* NOSTRICT skip string len */ (void) va_arg(ap, int);
-    info->hp->h_addrtype = va_arg(ap, int);
+    info->hp->h_addrtype = addr_type;
 
     switch (info->hp->h_addrtype) {
         case AF_INET:
@@ -1156,16 +1142,3 @@
     return android_gethostbyaddrfornetcontext_proxy_internal(
             addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
 }
-
-struct hostent* gethostent(void) {
-    struct res_static* rs = __res_get_static();  // For thread-safety.
-    if (!rs->hostf) {
-        sethostent_r(&rs->hostf);
-        if (!rs->hostf) {
-            h_errno = NETDB_INTERNAL;
-            return NULL;
-        }
-    }
-    memset(&rs->host, 0, sizeof(rs->host));
-    return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno);
-}
diff --git a/resolv/hostent.h b/resolv/hostent.h
index c0f4ea9..f06e104 100644
--- a/resolv/hostent.h
+++ b/resolv/hostent.h
@@ -35,14 +35,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 
-/*
- * These are not being advertised because the interfaces are non-standard.
- * There are versions by linux, aix, qnx, sun, etc. Our versions are used
- * internally to provide thread safety; they mostly resemble qnx.
- */
-void sethostent_r(FILE**);
 struct hostent* netbsd_gethostent_r(FILE*, struct hostent*, char*, size_t, int*);
-void endhostent_r(FILE**);
 
 /*
  * The following are internal API's and are used only for testing.
@@ -56,7 +49,7 @@
 
 /* /etc/hosts lookup */
 int _hf_gethtbyaddr(void*, void*, va_list);
-int _hf_gethtbyname(void*, void*, va_list);
+hostent* _hf_gethtbyname2(const char* name, int af, getnamaddr* info);
 
 #define HENT_ARRAY(dst, anum, ptr, len) do {     \
         size_t _len = (anum + 1) * sizeof(*dst); \
diff --git a/resolv/sethostent.cpp b/resolv/sethostent.cpp
index 5a7ab78..2a4248f 100644
--- a/resolv/sethostent.cpp
+++ b/resolv/sethostent.cpp
@@ -37,6 +37,7 @@
 #include <netinet/in.h>
 #include <nsswitch.h>
 #include <resolv.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/param.h>
@@ -52,57 +53,21 @@
 #define ALIGNBYTES (sizeof(uintptr_t) - 1)
 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
 
-static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*);
-
-void
-sethostent(int /*stayopen*/) {
-    struct res_static* rs = __res_get_static();
-    if (rs) sethostent_r(&rs->hostf);
-}
-
-void endhostent(void) {
-    struct res_static* rs = __res_get_static();
-    if (rs) endhostent_r(&rs->hostf);
-}
-
-void sethostent_r(FILE** hf) {
+static void sethostent_r(FILE** hf) {
     if (!*hf)
         *hf = fopen(_PATH_HOSTS, "re");
     else
         rewind(*hf);
 }
 
-void endhostent_r(FILE** hf) {
+static void endhostent_r(FILE** hf) {
     if (*hf) {
         (void) fclose(*hf);
         *hf = NULL;
     }
 }
 
-int _hf_gethtbyname(void* rv, void* /*cb_data*/, va_list ap) {
-    struct hostent* hp;
-    const char* name;
-    int af;
-    struct getnamaddr* info = (struct getnamaddr*) rv;
-
-    _DIAGASSERT(rv != NULL);
-
-    name = va_arg(ap, char*);
-    /* NOSTRICT skip string len */ (void) va_arg(ap, int);
-    af = va_arg(ap, int);
-
-    hp = _hf_gethtbyname2(name, af, info);
-    if (hp == NULL) {
-        if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
-            return NS_UNAVAIL;  // glibc compatibility.
-        }
-        *info->he = HOST_NOT_FOUND;
-        return NS_NOTFOUND;
-    }
-    return NS_SUCCESS;
-}
-
-struct hostent* _hf_gethtbyname2(const char* name, int af, struct getnamaddr* info) {
+hostent* _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
     struct hostent *hp, hent;
     char *buf, *ptr;
     size_t len, anum, num, i;
@@ -150,7 +115,7 @@
         }
 
         if (num == 0) {
-            hent.h_addrtype = af = hp->h_addrtype;
+            hent.h_addrtype = hp->h_addrtype;
             hent.h_length = hp->h_length;
 
             HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);