Remove all non-public resolver APIs

These functions were effectively dead code since when we started hiding
all symbols in libnetd_resolv except the few that are part of the public
interface. Removing them brings us a bit closer to removing h_errno.

However, netd also calls getaddrinfo() from various places, and
currently it's taking a longer trip through bionic before landing into
android_getaddrinfofornetcontext(). This will break as soon as we change
the signature of android_getaddrinfofornetcontext(), so we should decide
what to do about this. One option would be renaming our copy of
getaddrinfo() and switch netd to it.

Test: atest netd_integration_test
Change-Id: Iacfde97203365ce4d09d8e1c1d67930d729f46b1
diff --git a/getaddrinfo.cpp b/getaddrinfo.cpp
index f1aff7a..0364be4 100644
--- a/getaddrinfo.cpp
+++ b/getaddrinfo.cpp
@@ -88,7 +88,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -316,22 +315,19 @@
     return true;
 }
 
-int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
-                struct addrinfo** res) {
-    return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
-}
-
-int android_getaddrinfofornet(const char* hostname, const char* servname,
-                              const struct addrinfo* hints, unsigned netid, unsigned mark,
-                              struct addrinfo** res) {
-    struct android_net_context netcontext = {
-            .app_netid = netid,
-            .app_mark = mark,
-            .dns_netid = netid,
-            .dns_mark = mark,
+// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
+// NOTE: also called by resolv_set_nameservers_for_net().
+int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
+                        addrinfo** result) {
+    hints.ai_flags = AI_NUMERICHOST;
+    const android_net_context netcontext = {
+            .app_netid = NETID_UNSET,
+            .app_mark = MARK_UNSET,
+            .dns_netid = NETID_UNSET,
+            .dns_mark = MARK_UNSET,
             .uid = NET_CONTEXT_INVALID_UID,
     };
-    return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
+    return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
 }
 
 int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
@@ -1583,7 +1579,7 @@
 static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
     char* p;
     char *cp, *tname, *cname;
-    struct addrinfo hints, *res0, *res;
+    struct addrinfo *res0, *res;
     int error;
     const char* addr;
     char hostbuf[8 * 1024];
@@ -1616,9 +1612,7 @@
     goto again;
 
 found:
-    hints = *pai;
-    hints.ai_flags = AI_NUMERICHOST;
-    error = getaddrinfo(addr, NULL, &hints, &res0);
+    error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
     if (error) goto again;
     for (res = res0; res; res = res->ai_next) {
         /* cover it up */
diff --git a/gethnamaddr.cpp b/gethnamaddr.cpp
index 017d06d..834a962 100644
--- a/gethnamaddr.cpp
+++ b/gethnamaddr.cpp
@@ -61,7 +61,8 @@
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
-#include <strings.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -70,6 +71,7 @@
 #include <unistd.h>
 #include <functional>
 
+#include "hostent.h"
 #include "netd_resolv/resolv.h"
 #include "resolv_cache.h"
 #include "resolv_private.h"
@@ -90,11 +92,6 @@
 
 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
 
-#include <stdlib.h>
-#include <string.h>
-
-#include "hostent.h"
-
 #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
 #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
 #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
@@ -113,12 +110,6 @@
 
 static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
 
-static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
-                                                            .app_netid = NETID_UNSET,
-                                                            .dns_mark = MARK_UNSET,
-                                                            .dns_netid = NETID_UNSET,
-                                                            .uid = NET_CONTEXT_INVALID_UID};
-
 #define MAXPACKET (8 * 1024)
 
 typedef union {
@@ -144,8 +135,6 @@
 static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
 static void addrsort(char**, int, res_state);
 
-struct hostent* ht_gethostbyname(char*);
-struct hostent* ht_gethostbyaddr(const char*, int, int);
 static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
                              const android_net_context* netcontext, getnamaddr* info);
 static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
@@ -158,16 +147,6 @@
 static struct hostent* android_gethostbyaddrfornetcontext_proxy(
         const void* addr, socklen_t len, int af, const struct android_net_context* netcontext);
 
-static int h_errno_to_result(int* herrno_p) {
-    // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
-    if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
-        errno = ERANGE;
-        return errno;
-    }
-    // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
-    return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
-}
-
 #ifdef DEBUG
 static void debugprintf(const char* msg, res_state res, ...) {
     _DIAGASSERT(msg != NULL);
@@ -487,44 +466,6 @@
     return NULL;
 }
 
-/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
-int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
-                    struct hostent** result, int* errorp) {
-    res_state res = res_get_state();
-    if (res == NULL) {
-        *result = NULL;
-        *errorp = NETDB_INTERNAL;
-        return -1;
-    }
-
-    _DIAGASSERT(name != NULL);
-
-    if (res->options & RES_USE_INET6) {
-        *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
-                                         &NETCONTEXT_UNSET);
-        if (*result) {
-            res_put_state(res);
-            return 0;
-        }
-    }
-    *result =
-            gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
-    return h_errno_to_result(errorp);
-}
-
-/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
-int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
-                     struct hostent** result, int* errorp) {
-    res_state res = res_get_state();
-    if (res == NULL) {
-        *result = NULL;
-        *errorp = NETDB_INTERNAL;
-        return -1;
-    }
-    *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
-    return h_errno_to_result(errorp);
-}
-
 static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
                                                    struct hostent* hp, char* buf, size_t buflen,
                                                    int* he) {
@@ -629,14 +570,6 @@
     return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
 }
 
-/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
-int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
-                    size_t buflen, struct hostent** result, int* h_errnop) {
-    *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
-                                                                h_errnop, &NETCONTEXT_UNSET);
-    return h_errno_to_result(h_errnop);
-}
-
 static struct hostent* android_gethostbyaddrfornetcontext_real(
         const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
         int* he, const struct android_net_context* netcontext) {
@@ -1051,43 +984,6 @@
  * Non-reentrant versions.
  */
 
-struct hostent* gethostbyname(const char* name) {
-    struct hostent* result = NULL;
-    struct res_static* rs = res_get_static();  // For thread-safety.
-
-    gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
-    return result;
-}
-
-struct hostent* gethostbyname2(const char* name, int af) {
-    struct hostent* result = NULL;
-    struct res_static* rs = res_get_static();  // For thread-safety.
-
-    gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
-    return result;
-}
-
-// android_gethostby*fornet can be called in two different contexts.
-//  - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
-//  - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
-// The netcontext is constructed before checking which context we are in.
-// Therefore, we have to populate both fields, and rely on the downstream code to check whether
-// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
-static struct android_net_context make_context(unsigned netid, unsigned mark) {
-    struct android_net_context netcontext = NETCONTEXT_UNSET;
-    netcontext.app_netid = netid;
-    netcontext.app_mark = mark;
-    netcontext.dns_netid = netid;
-    netcontext.dns_mark = mark;
-    return netcontext;
-}
-
-struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
-                                            unsigned mark) {
-    const struct android_net_context netcontext = make_context(netid, mark);
-    return android_gethostbynamefornetcontext(name, af, &netcontext);
-}
-
 struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
                                                    const struct android_net_context* netcontext) {
     struct hostent* hp;
@@ -1100,16 +996,6 @@
     return hp;
 }
 
-struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
-    return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
-}
-
-struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
-                                            unsigned mark) {
-    const struct android_net_context netcontext = make_context(netid, mark);
-    return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
-}
-
 struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
                                                    const struct android_net_context* netcontext) {
     return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
diff --git a/include/netd_resolv/resolv.h b/include/netd_resolv/resolv.h
index 6bf1bdd..96133e5 100644
--- a/include/netd_resolv/resolv.h
+++ b/include/netd_resolv/resolv.h
@@ -83,15 +83,6 @@
 #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
 #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
 
-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**);
-/*
- * TODO: consider refactoring android_getaddrinfo_proxy() to serve as an
- * explore_fqdn() dispatch table method, with the below function only making DNS calls.
- */
 LIBNETD_RESOLV_PUBLIC hostent* android_gethostbyaddrfornetcontext(const void*, socklen_t, int,
                                                                   const android_net_context*);
 LIBNETD_RESOLV_PUBLIC hostent* android_gethostbynamefornetcontext(const char*, int,
diff --git a/libnetd_resolv.map.txt b/libnetd_resolv.map.txt
index cec50c2..63e0cb8 100644
--- a/libnetd_resolv.map.txt
+++ b/libnetd_resolv.map.txt
@@ -20,11 +20,8 @@
 
 LIBNETD_RESOLV {
   global:
-    android_getaddrinfofornet;
     android_getaddrinfofornetcontext;
-    android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
-    android_gethostbynamefornet;
     android_gethostbynamefornetcontext;
     android_net_res_stats_aggregate;
     android_net_res_stats_get_info_for_net;
diff --git a/res_cache.cpp b/res_cache.cpp
index 8eb20e2..a6bc6bf 100644
--- a/res_cache.cpp
+++ b/res_cache.cpp
@@ -1757,7 +1757,6 @@
 
 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;
     struct addrinfo* nsaddrinfo[MAXNS];
@@ -1769,18 +1768,20 @@
 
     // Parse the addresses before actually locking or changing any state, in case there is an error.
     // As a side effect this also reduces the time the lock is kept.
-    struct addrinfo hints = {
-            .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST};
+    char sbuf[NI_MAXSERV];
     snprintf(sbuf, sizeof(sbuf), "%u", NAMESERVER_PORT);
     for (unsigned i = 0; i < numservers; i++) {
         // The addrinfo structures allocated here are freed in free_nameservers_locked().
-        int rt = getaddrinfo(servers[i], sbuf, &hints, &nsaddrinfo[i]);
+        const addrinfo hints = {
+                .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST};
+        int rt = getaddrinfo_numeric(servers[i], sbuf, hints, &nsaddrinfo[i]);
         if (rt != 0) {
             for (unsigned j = 0; j < i; j++) {
                 freeaddrinfo(nsaddrinfo[j]);
                 nsaddrinfo[j] = NULL;
             }
-            VLOG << __func__ << ": getaddrinfo(" << servers[i] << ") = " << gai_strerror(rt);
+            VLOG << __func__ << ": getaddrinfo_numeric(" << servers[i]
+                 << ") = " << gai_strerror(rt);
             return EINVAL;
         }
     }
diff --git a/resolv_private.h b/resolv_private.h
index 8fd168c..d4ee044 100644
--- a/resolv_private.h
+++ b/resolv_private.h
@@ -288,4 +288,7 @@
 
 u_int res_randomid(void);
 
+int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
+                        addrinfo** result);
+
 #endif  // NETD_RESOLV_PRIVATE_H