Build netd with a built-in resolver library

Test: atest netd_unit_test netd_integration_test
Change-Id: Ied9f5b1328918793521a8d6f82afdfc6481fd545
diff --git a/resolv/Android.bp b/resolv/Android.bp
new file mode 100644
index 0000000..8de4300
--- /dev/null
+++ b/resolv/Android.bp
@@ -0,0 +1,33 @@
+cc_library_shared {
+    name: "libnetd_resolv",
+    defaults: ["netd_defaults"],
+    srcs: [
+        "getaddrinfo.c",
+        "gethnamaddr.c",
+        "sethostent.c",
+        "res_cache.c",
+        "res_data.c",
+        "res_debug.c",
+        "res_init.c",
+        "res_mkquery.c",
+        "res_query.c",
+        "res_send.c",
+        "res_state.c",
+        "res_stats.c",
+    ],
+    // TODO: un-ifdef resolver code
+    // TODO: fix unused parameter warnings
+    cflags: [
+        "-DANDROID_CHANGES",
+        "-DINET6",
+        "-Wno-unused-parameter",
+    ],
+    // TODO: stop depending on internal bionic headers
+    include_dirs: [
+        "bionic/libc/async_safe/include",  // For async_safe/log.h
+        "bionic/libc/dns/include",  // For nsswitch.h
+    ],
+    export_include_dirs: ["."],  // Export resolv_netid.h
+    // TODO: pie in the sky: make this code clang-tidy clean
+    tidy: false
+}
diff --git a/resolv/getaddrinfo.c b/resolv/getaddrinfo.c
index 97b5b6f..512a7af 100644
--- a/resolv/getaddrinfo.c
+++ b/resolv/getaddrinfo.c
@@ -92,22 +92,20 @@
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
-#include <sys/cdefs.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/un.h>
 #include <unistd.h>
-#include "NetdClientDispatch.h"
 #include "resolv_cache.h"
 #include "resolv_netid.h"
 #include "resolv_private.h"
 
 #include <stdarg.h>
 #include <syslog.h>
+
 #include "nsswitch.h"
-#include "private/bionic_defs.h"
 
 typedef union sockaddr_union {
     struct sockaddr generic;
@@ -127,11 +125,6 @@
 static const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
 #endif
 
-#if defined(__ANDROID__)
-// This should be synchronized to ResponseCode.h
-static const int DnsProxyQueryResult = 222;
-#endif
-
 static const struct afd {
     int a_af;
     int a_addrlen;
@@ -186,7 +179,10 @@
 #endif
 
 static const ns_src default_dns_files[] = {
-        {NSSRC_FILES, NS_SUCCESS}, {NSSRC_DNS, NS_SUCCESS}, {0, 0}};
+    {NSSRC_FILES, NS_SUCCESS},
+    {NSSRC_DNS, NS_SUCCESS},
+    {0, 0}
+};
 
 #define MAXPACKET (8 * 1024)
 
@@ -290,21 +286,15 @@
     ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
 #define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
 
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 const char* gai_strerror(int ecode) {
     if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
     return ai_errlist[ecode];
 }
 
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 void freeaddrinfo(struct addrinfo* ai) {
     struct addrinfo* next;
 
-#if defined(__BIONIC__)
     if (ai == NULL) return;
-#else
-    _DIAGASSERT(ai != NULL);
-#endif
 
     do {
         next = ai->ai_next;
@@ -367,160 +357,11 @@
     return true;
 }
 
-#if defined(__ANDROID__)
-// Returns 0 on success, else returns on error.
-static int android_getaddrinfo_proxy(const char* hostname, const char* servname,
-                                     const struct addrinfo* hints, struct addrinfo** res,
-                                     unsigned netid) {
-    int success = 0;
-
-    // Clear this at start, as we use its non-NULLness later (in the
-    // error path) to decide if we have to free up any memory we
-    // allocated in the process (before failing).
-    *res = NULL;
-
-    // Bogus things we can't serialize.  Don't use the proxy.  These will fail - let them.
-    if ((hostname != NULL && strcspn(hostname, " \n\r\t^'\"") != strlen(hostname)) ||
-        (servname != NULL && strcspn(servname, " \n\r\t^'\"") != strlen(servname))) {
-        return EAI_NODATA;
-    }
-
-    FILE* proxy = android_open_proxy();
-    if (proxy == NULL) {
-        return EAI_SYSTEM;
-    }
-
-    netid = __netdClientDispatch.netIdForResolv(netid);
-
-    // Send the request.
-    if (fprintf(proxy, "getaddrinfo %s %s %d %d %d %d %u", hostname == NULL ? "^" : hostname,
-                servname == NULL ? "^" : servname, hints == NULL ? -1 : hints->ai_flags,
-                hints == NULL ? -1 : hints->ai_family, hints == NULL ? -1 : hints->ai_socktype,
-                hints == NULL ? -1 : hints->ai_protocol, netid) < 0) {
-        goto exit;
-    }
-    // literal NULL byte at end, required by FrameworkListener
-    if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
-        goto exit;
-    }
-
-    char buf[4];
-    // read result code for gethostbyaddr
-    if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) {
-        goto exit;
-    }
-
-    int result_code = (int) strtol(buf, NULL, 10);
-    // verify the code itself
-    if (result_code != DnsProxyQueryResult) {
-        fread(buf, 1, sizeof(buf), proxy);
-        goto exit;
-    }
-
-    struct addrinfo* ai = NULL;
-    struct addrinfo** nextres = res;
-    while (1) {
-        int32_t have_more;
-        if (!readBE32(proxy, &have_more)) {
-            break;
-        }
-        if (have_more == 0) {
-            success = 1;
-            break;
-        }
-
-        struct addrinfo* ai = calloc(1, sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
-        if (ai == NULL) {
-            break;
-        }
-        ai->ai_addr = (struct sockaddr*) (ai + 1);
-
-        // struct addrinfo {
-        //	int	ai_flags;	/* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
-        //	int	ai_family;	/* PF_xxx */
-        //	int	ai_socktype;	/* SOCK_xxx */
-        //	int	ai_protocol;	/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
-        //	socklen_t ai_addrlen;	/* length of ai_addr */
-        //	char	*ai_canonname;	/* canonical name for hostname */
-        //	struct	sockaddr *ai_addr;	/* binary address */
-        //	struct	addrinfo *ai_next;	/* next structure in linked list */
-        // };
-
-        // Read the struct piece by piece because we might be a 32-bit process
-        // talking to a 64-bit netd.
-        int32_t addr_len;
-        bool success = readBE32(proxy, &ai->ai_flags) && readBE32(proxy, &ai->ai_family) &&
-                       readBE32(proxy, &ai->ai_socktype) && readBE32(proxy, &ai->ai_protocol) &&
-                       readBE32(proxy, &addr_len);
-        if (!success) {
-            break;
-        }
-
-        // Set ai_addrlen and read the ai_addr data.
-        ai->ai_addrlen = addr_len;
-        if (addr_len != 0) {
-            if ((size_t) addr_len > sizeof(struct sockaddr_storage)) {
-                // Bogus; too big.
-                break;
-            }
-            if (fread(ai->ai_addr, addr_len, 1, proxy) != 1) {
-                break;
-            }
-        }
-
-        // The string for ai_cannonname.
-        int32_t name_len;
-        if (!readBE32(proxy, &name_len)) {
-            break;
-        }
-        if (name_len != 0) {
-            ai->ai_canonname = (char*) malloc(name_len);
-            if (fread(ai->ai_canonname, name_len, 1, proxy) != 1) {
-                break;
-            }
-            if (ai->ai_canonname[name_len - 1] != '\0') {
-                // The proxy should be returning this
-                // NULL-terminated.
-                break;
-            }
-        }
-
-        *nextres = ai;
-        nextres = &ai->ai_next;
-        ai = NULL;
-    }
-
-    if (ai != NULL) {
-        // Clean up partially-built addrinfo that we never ended up
-        // attaching to the response.
-        freeaddrinfo(ai);
-    }
-exit:
-    if (proxy != NULL) {
-        fclose(proxy);
-    }
-
-    if (success) {
-        return 0;
-    }
-
-    // Proxy failed;
-    // clean up memory we might've allocated.
-    if (*res) {
-        freeaddrinfo(*res);
-        *res = NULL;
-    }
-    return EAI_NODATA;
-}
-#endif
-
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 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);
 }
 
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 int android_getaddrinfofornet(const char* hostname, const char* servname,
                               const struct addrinfo* hints, unsigned netid, unsigned mark,
                               struct addrinfo** res) {
@@ -534,7 +375,6 @@
     return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
 }
 
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
 int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
                                      const struct addrinfo* hints,
                                      const struct android_net_context* netcontext,
@@ -660,14 +500,6 @@
     if (hostname == NULL) ERR(EAI_NODATA);
     if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
 
-#if defined(__ANDROID__)
-    int gai_error =
-            android_getaddrinfo_proxy(hostname, servname, hints, res, netcontext->app_netid);
-    if (gai_error != EAI_SYSTEM) {
-        return gai_error;
-    }
-#endif
-
     /*
      * hostname as alphabetical name.
      * we would like to prefer AF_INET6 than AF_INET, so we'll make a
@@ -721,9 +553,11 @@
     struct addrinfo* result;
     struct addrinfo* cur;
     int error = 0;
-    static const ns_dtab dtab[] = {NS_FILES_CB(_files_getaddrinfo, NULL){
-                                           NSSRC_DNS, _dns_getaddrinfo, NULL}, /* force -DHESIOD */
-                                   NS_NIS_CB(_yp_getaddrinfo, NULL){0, 0, 0}};
+    static const ns_dtab dtab[] = {
+        {NSSRC_FILES, _files_getaddrinfo, NULL},
+        {NSSRC_DNS, _dns_getaddrinfo, NULL},
+        {0, 0, 0}
+    };
 
     assert(pai != NULL);
     /* hostname may be NULL */
@@ -1663,7 +1497,7 @@
         return 0;
     }
     do {
-        ret = __connect(sock, addr, len);
+        ret = connect(sock, addr, len);
     } while (ret == -1 && errno == EINTR);
 
     if (ret == -1) {
diff --git a/resolv/gethnamaddr.c b/resolv/gethnamaddr.c
index 5924540..c52620e 100644
--- a/resolv/gethnamaddr.c
+++ b/resolv/gethnamaddr.c
@@ -51,9 +51,6 @@
  * --Copyright--
  */
 
-#include <sys/cdefs.h>
-#include <sys/types.h>
-
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
 #include <assert.h>
@@ -67,14 +64,20 @@
 #include <strings.h>
 #include <sys/param.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 #include <sys/un.h>
 #include <syslog.h>
 #include <unistd.h>
-#include "NetdClientDispatch.h"
+
 #include "resolv_cache.h"
 #include "resolv_netid.h"
 #include "resolv_private.h"
 
+// NetBSD uses _DIAGASSERT to null-check arguments and the like,
+// but it's clear from the number of mistakes in their assertions
+// that they don't actually test or ship with this.
+#define _DIAGASSERT(e) /* nothing */
+
 #define ALIGNBYTES (sizeof(uintptr_t) - 1)
 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
 
@@ -112,9 +115,6 @@
         if (arr == NULL) goto nospc;             \
     } while (/*CONSTCOND*/ 0)
 
-// This should be synchronized to ResponseCode.h
-static const int DnsProxyQueryResult = 222;
-
 static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
 
 static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
@@ -161,7 +161,10 @@
         const struct android_net_context*);
 
 static const ns_src default_dns_files[] = {
-        {NSSRC_FILES, NS_SUCCESS}, {NSSRC_DNS, NS_SUCCESS}, {0, 0}};
+    {NSSRC_FILES, NS_SUCCESS},
+    {NSSRC_DNS, NS_SUCCESS},
+    {0, 0}
+};
 
 static int h_errno_to_result(int* herrno_p) {
     // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
@@ -558,111 +561,6 @@
     return fdopen(s, "r+");
 }
 
-static struct hostent* android_read_hostent(FILE* proxy, struct hostent* hp, char* hbuf,
-                                            size_t hbuflen, int* he) {
-    uint32_t size;
-    char buf[4];
-    if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL;
-
-    // This is reading serialized data from system/netd/server/DnsProxyListener.cpp
-    // and changes here need to be matched there.
-    int result_code = strtol(buf, NULL, 10);
-    if (result_code != DnsProxyQueryResult) {
-        fread(&size, 1, sizeof(size), proxy);
-        *he = HOST_NOT_FOUND;
-        return NULL;
-    }
-
-    if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
-    size = ntohl(size);
-
-    memset(hp, 0, sizeof(*hp));
-    char* ptr = hbuf;
-    char* hbuf_end = hbuf + hbuflen;
-
-    if (ptr + size > hbuf_end) {
-        goto nospc;
-    }
-    if (fread(ptr, 1, size, proxy) != size) return NULL;
-    hp->h_name = ptr;
-    ptr += size;
-
-    char* aliases_ptrs[MAXALIASES];
-    char** aliases = &aliases_ptrs[0];
-
-    while (1) {
-        if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
-        size = ntohl(size);
-
-        if (size == 0) {
-            *aliases = NULL;
-            break;
-        }
-        if (ptr + size > hbuf_end) {
-            goto nospc;
-        }
-        if (fread(ptr, 1, size, proxy) != size) return NULL;
-        if (aliases < &aliases_ptrs[MAXALIASES - 1]) {
-            *aliases++ = ptr;
-        }
-        ptr += size;
-    }
-
-    // Fix alignment after variable-length data.
-    ptr = (char*) ALIGN(ptr);
-
-    int aliases_len = ((int) (aliases - aliases_ptrs) + 1) * sizeof(*hp->h_aliases);
-    if (ptr + aliases_len > hbuf_end) {
-        goto nospc;
-    }
-    hp->h_aliases = (void*) ptr;
-    memcpy(ptr, aliases_ptrs, aliases_len);
-    ptr += aliases_len;
-
-    if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
-    hp->h_addrtype = ntohl(size);
-
-    if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
-    hp->h_length = ntohl(size);
-
-    char* addr_ptrs[MAXADDRS];
-    char** addr_p = &addr_ptrs[0];
-
-    while (1) {
-        if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
-        size = ntohl(size);
-        if (size == 0) {
-            *addr_p = NULL;
-            break;
-        }
-        if (ptr + size > hbuf_end) {
-            goto nospc;
-        }
-        if (fread(ptr, 1, size, proxy) != size) return NULL;
-        if (addr_p < &addr_ptrs[MAXADDRS - 1]) {
-            *addr_p++ = ptr;
-        }
-        ptr += size;
-    }
-
-    // Fix alignment after variable-length data.
-    ptr = (char*) ALIGN(ptr);
-
-    int addrs_len = ((int) (addr_p - addr_ptrs) + 1) * sizeof(*hp->h_addr_list);
-    if (ptr + addrs_len > hbuf_end) {
-        goto nospc;
-    }
-    hp->h_addr_list = (void*) ptr;
-    memcpy(ptr, addr_ptrs, addrs_len);
-    *he = NETDB_SUCCESS;
-    return hp;
-
-nospc:
-    *he = NETDB_INTERNAL;
-    errno = ENOSPC;
-    return NULL;
-}
-
 static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
                                                    struct hostent* hp, char* buf, size_t buflen,
                                                    int* he) {
@@ -670,9 +568,11 @@
     struct getnamaddr info;
     char hbuf[MAXHOSTNAMELEN];
     size_t size;
-    static const ns_dtab dtab[] = {NS_FILES_CB(_hf_gethtbyname, NULL){NSSRC_DNS, _dns_gethtbyname,
-                                                                      NULL}, /* force -DHESIOD */
-                                   NS_NIS_CB(_yp_gethtbyname, NULL) NS_NULL_CB};
+    static const ns_dtab dtab[] = {
+        {NSSRC_FILES, _hf_gethtbyname,  NULL},
+        {NSSRC_DNS,   _dns_gethtbyname, NULL},
+        {0, 0, 0}
+    };
 
     _DIAGASSERT(name != NULL);
 
@@ -771,30 +671,8 @@
                                               struct hostent* hp, char* hbuf, size_t hbuflen,
                                               int* errorp,
                                               const struct android_net_context* netcontext) {
-    FILE* proxy = android_open_proxy();
-    if (proxy == NULL) {
-        // Either we're not supposed to be using the proxy or the proxy is unavailable.
-        res_setnetcontext(res, netcontext);
-        return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
-    }
-
-    unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
-
-    // This is writing to system/netd/server/DnsProxyListener.cpp and changes
-    // here need to be matched there.
-    if (fprintf(proxy, "gethostbyname %u %s %d", netid, name == NULL ? "^" : name, af) < 0) {
-        fclose(proxy);
-        return NULL;
-    }
-
-    if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
-        fclose(proxy);
-        return NULL;
-    }
-
-    struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, errorp);
-    fclose(proxy);
-    return result;
+    res_setnetcontext(res, netcontext);
+    return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
 }
 
 /* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
@@ -811,9 +689,11 @@
     const u_char* uaddr = (const u_char*) addr;
     socklen_t size;
     struct getnamaddr info;
-    static const ns_dtab dtab[] = {NS_FILES_CB(_hf_gethtbyaddr, NULL){NSSRC_DNS, _dns_gethtbyaddr,
-                                                                      NULL}, /* force -DHESIOD */
-                                   NS_NIS_CB(_yp_gethtbyaddr, NULL) NS_NULL_CB};
+    static const ns_dtab dtab[] = {
+        {NSSRC_FILES, _hf_gethtbyaddr,  NULL},
+        {NSSRC_DNS,   _dns_gethtbyaddr, NULL},
+        {0, 0, 0}
+    };
 
     _DIAGASSERT(addr != NULL);
 
@@ -864,35 +744,8 @@
 static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
         const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
         int* he, const struct android_net_context* netcontext) {
-    FILE* proxy = android_open_proxy();
-    if (proxy == NULL) {
-        // Either we're not supposed to be using the proxy or the proxy is unavailable.
-        return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
-                                                       netcontext);
-    }
-
-    char buf[INET6_ADDRSTRLEN];  // big enough for IPv4 and IPv6
-    const char* addrStr = inet_ntop(af, addr, buf, sizeof(buf));
-    if (addrStr == NULL) {
-        fclose(proxy);
-        return NULL;
-    }
-
-    unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
-
-    if (fprintf(proxy, "gethostbyaddr %s %d %d %u", addrStr, len, af, netid) < 0) {
-        fclose(proxy);
-        return NULL;
-    }
-
-    if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
-        fclose(proxy);
-        return NULL;
-    }
-
-    struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, he);
-    fclose(proxy);
-    return result;
+    return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
+                                                   netcontext);
 }
 
 struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
diff --git a/resolv/res_cache.c b/resolv/res_cache.c
index 958e981..230ebef 100644
--- a/resolv/res_cache.c
+++ b/resolv/res_cache.c
@@ -26,15 +26,13 @@
  * SUCH DAMAGE.
  */
 
-#include "resolv_cache.h"
-
+#include <pthread.h>
 #include <resolv.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include "pthread.h"
 
 #include <arpa/nameser.h>
 #include <errno.h>
@@ -43,7 +41,9 @@
 #include <netdb.h>
 
 #include <arpa/inet.h>
+
 #include "res_private.h"
+#include "resolv_cache.h"
 #include "resolv_netid.h"
 #include "resolv_private.h"
 
diff --git a/resolv/res_data.c b/resolv/res_data.c
index 8c35fcd..20e2da8 100644
--- a/resolv/res_data.c
+++ b/resolv/res_data.c
@@ -17,15 +17,6 @@
  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#ifdef notdef
-static const char rcsid[] = "Id: res_data.c,v 1.1.206.2 2004/03/16 12:34:18 marka Exp";
-#else
-__RCSID("$NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
diff --git a/resolv/res_debug.c b/resolv/res_debug.c
index 331d0c4..931ffde 100644
--- a/resolv/res_debug.c
+++ b/resolv/res_debug.c
@@ -95,16 +95,6 @@
  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#ifdef notdef
-static const char sccsid[] = "@(#)res_debug.c	8.1 (Berkeley) 6/4/93";
-static const char rcsid[] = "Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp";
-#else
-__RCSID("$NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -124,23 +114,14 @@
 #include <time.h>
 #include "resolv_private.h"
 
+// NetBSD uses _DIAGASSERT to null-check arguments and the like,
+// but it's clear from the number of mistakes in their assertions
+// that they don't actually test or ship with this.
+#define _DIAGASSERT(e) /* nothing */
+
 extern const char* const _res_opcodes[];
 extern const char* const _res_sectioncodes[];
 
-#ifndef _LIBC
-/*
- * Print the current options.
- */
-void fp_resstat(const res_state statp, FILE* file) {
-    u_long mask;
-
-    fprintf(file, ";; res options:");
-    for (mask = 1; mask != 0U; mask <<= 1)
-        if (statp->options & mask) fprintf(file, " %s", p_option(mask));
-    putc('\n', file);
-}
-#endif
-
 static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag,
                        FILE* file) {
     int n, sflag, rrnum;
diff --git a/resolv/res_init.c b/resolv/res_init.c
index a4167a2..e2d276d 100644
--- a/resolv/res_init.c
+++ b/resolv/res_init.c
@@ -70,16 +70,6 @@
  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#ifdef notdef
-static const char sccsid[] = "@(#)res_init.c	8.1 (Berkeley) 6/7/93";
-static const char rcsid[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp";
-#else
-__RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c
index 7a2a3f7..8ebf3e7 100644
--- a/resolv/res_mkquery.c
+++ b/resolv/res_mkquery.c
@@ -70,16 +70,6 @@
  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#ifdef notdef
-static const char sccsid[] = "@(#)res_mkquery.c	8.1 (Berkeley) 6/4/93";
-static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
-#else
-__RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
 #include <arpa/nameser.h>
 #include <netdb.h>
 #include <netinet/in.h>
diff --git a/resolv/res_private.h b/resolv/res_private.h
index ec73cba..2f48768 100644
--- a/resolv/res_private.h
+++ b/resolv/res_private.h
@@ -1,7 +1,9 @@
 /*	$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 _RES_PRIVATE_H_
+#define _RES_PRIVATE_H_
+
+#include "resolv_private.h"
 
 struct __res_state_ext {
     union res_sockaddr_union nsaddrs[MAXNS];
@@ -18,4 +20,4 @@
 
 extern int res_ourserver_p(const res_state statp, const struct sockaddr* sa);
 
-#endif
+#endif  // _RES_PRIVATE_H_
diff --git a/resolv/res_send.c b/resolv/res_send.c
index f2563fb..2517b5b 100644
--- a/resolv/res_send.c
+++ b/resolv/res_send.c
@@ -70,16 +70,6 @@
  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#ifdef notdef
-static const char sccsid[] = "@(#)res_send.c	8.1 (Berkeley) 6/4/93";
-static const char rcsid[] = "Id: res_send.c,v 1.5.2.2.4.5 2004/08/10 02:19:56 marka Exp";
-#else
-__RCSID("$NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
 /*
  * Send query to name server and wait for reply.
  */
@@ -112,8 +102,6 @@
 #include <time.h>
 #include <unistd.h>
 
-#include <isc/eventlib.h>
-
 #include <resolv_cache.h>
 
 #include <async_safe/log.h>
@@ -157,6 +145,86 @@
     struct sockaddr_in6 sin6;
 } _sockaddr_union;
 
+// BEGIN: Code copied from ISC eventlib
+// TODO: move away from this code
+
+#define BILLION 1000000000
+
+static struct timespec evConsTime(time_t sec, long nsec) {
+    struct timespec x;
+
+    x.tv_sec = sec;
+    x.tv_nsec = nsec;
+    return (x);
+}
+
+static struct timespec evAddTime(struct timespec addend1, struct timespec addend2) {
+    struct timespec x;
+
+    x.tv_sec = addend1.tv_sec + addend2.tv_sec;
+    x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
+    if (x.tv_nsec >= BILLION) {
+        x.tv_sec++;
+        x.tv_nsec -= BILLION;
+    }
+    return (x);
+}
+
+static struct timespec evSubTime(struct timespec minuend, struct timespec subtrahend) {
+    struct timespec x;
+
+    x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
+    if (minuend.tv_nsec >= subtrahend.tv_nsec)
+        x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
+    else {
+        x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
+        x.tv_sec--;
+    }
+    return (x);
+}
+
+static int evCmpTime(struct timespec a, struct timespec b) {
+#define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
+    time_t s = a.tv_sec - b.tv_sec;
+    long n;
+
+    if (s != 0) return SGN(s);
+
+    n = a.tv_nsec - b.tv_nsec;
+    return SGN(n);
+}
+
+static struct timespec evTimeSpec(struct timeval tv) {
+    struct timespec ts;
+
+    ts.tv_sec = tv.tv_sec;
+    ts.tv_nsec = tv.tv_usec * 1000;
+    return (ts);
+}
+
+static struct timespec evNowTime(void) {
+    struct timeval now;
+#ifdef CLOCK_REALTIME
+    struct timespec tsnow;
+    int m = CLOCK_REALTIME;
+
+    if (clock_gettime(m, &tsnow) == 0) return (tsnow);
+#endif
+    if (gettimeofday(&now, NULL) < 0) return (evConsTime((time_t) 0, 0L));
+    return (evTimeSpec(now));
+}
+
+static struct iovec evConsIovec(void* buf, size_t cnt) {
+    struct iovec ret;
+
+    memset(&ret, 0xf5, sizeof ret);
+    ret.iov_base = buf;
+    ret.iov_len = cnt;
+    return (ret);
+}
+
+// END: Code copied from ISC eventlib
+
 static int random_bind(int s, int family) {
     _sockaddr_union u;
     int j;
@@ -917,7 +985,7 @@
     origflags = fcntl(sock, F_GETFL, 0);
     fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
 
-    res = __connect(sock, nsap, salen);
+    res = connect(sock, nsap, salen);
     if (res < 0 && errno != EINPROGRESS) {
         res = -1;
         goto done;
@@ -1054,7 +1122,7 @@
             res_nclose(statp);
             return (0);
         }
-        if (__connect(EXT(statp).nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
+        if (connect(EXT(statp).nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
             Aerror(statp, stderr, "connect(dg)", errno, nsap, nsaplen);
             res_nclose(statp);
             return (0);
diff --git a/resolv/res_state.c b/resolv/res_state.c
index be349fb..7c4b488 100644
--- a/resolv/res_state.c
+++ b/resolv/res_state.c
@@ -31,7 +31,6 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/cdefs.h>
 #include <sys/types.h>
 #include "resolv_cache.h"
 #include "resolv_private.h"
diff --git a/resolv/res_stats.c b/resolv/res_stats.c
index 7ac5a4c..e5a41ab 100644
--- a/resolv/res_stats.c
+++ b/resolv/res_stats.c
@@ -20,7 +20,6 @@
 
 #include <async_safe/log.h>
 
-#include "isc/eventlib.h"
 #include "resolv_stats.h"
 
 #define DBG 0
@@ -135,9 +134,7 @@
                                       success_rate);
             }
             if (success_rate < params->success_threshold) {
-                // evNowTime() is used here instead of time() to stay consistent with the rest of
-                // the code base
-                time_t now = evNowTime().tv_sec;
+                time_t now = time(NULL);
                 if (now - last_sample_time > params->sample_validity) {
                     // Note: It might be worth considering to expire old servers after their expiry
                     // date has been reached, however the code for returning the ring buffer to its
diff --git a/resolv/resolv_static.h b/resolv/resolv_static.h
index e92b83b..26863fd 100644
--- a/resolv/resolv_static.h
+++ b/resolv/resolv_static.h
@@ -1,5 +1,5 @@
-#ifndef _RESOLV_STATIC_H
-#define _RESOLV_STATIC_H
+#ifndef _RESOLV_STATIC_H_
+#define _RESOLV_STATIC_H_
 
 #include <netdb.h>
 
@@ -29,4 +29,4 @@
 
 extern res_static __res_get_static(void);
 
-#endif /* _RESOLV_STATIC_H */
+#endif // _RESOLV_STATIC_H_
diff --git a/resolv/resolv_stats.h b/resolv/resolv_stats.h
index 18f884e..37e5a58 100644
--- a/resolv/resolv_stats.h
+++ b/resolv/resolv_stats.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef _RES_STATS_H
-#define _RES_STATS_H
+#ifndef _RES_STATS_H_
+#define _RES_STATS_H_
 
 #include <stdbool.h>
 #include <stdint.h>
@@ -77,4 +77,4 @@
         __attribute__((visibility("default")));
 __END_DECLS
 
-#endif  // _RES_STATS_H
+#endif  // _RES_STATS_H_
diff --git a/resolv/sethostent.c b/resolv/sethostent.c
index 8b1c5ab..b1625ea 100644
--- a/resolv/sethostent.c
+++ b/resolv/sethostent.c
@@ -29,16 +29,6 @@
  * SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)sethostent.c	8.1 (Berkeley) 6/4/93";
-static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
-#else
-__RCSID("$NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
 #include <assert.h>
@@ -50,18 +40,18 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/param.h>
-#include "namespace.h"
 
 #include "hostent.h"
 #include "resolv_private.h"
 
+// NetBSD uses _DIAGASSERT to null-check arguments and the like,
+// but it's clear from the number of mistakes in their assertions
+// that they don't actually test or ship with this.
+#define _DIAGASSERT(e) /* nothing */
+
 #define ALIGNBYTES (sizeof(uintptr_t) - 1)
 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
 
-#ifndef _REENTRANT
-void res_close(void);
-#endif
-
 static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*);
 
 void
@@ -103,22 +93,7 @@
     /* NOSTRICT skip string len */ (void) va_arg(ap, int);
     af = va_arg(ap, int);
 
-#if 0
-	{
-		res_state res = __res_get_state();
-		if (res == NULL)
-			return NS_NOTFOUND;
-		if (res->options & RES_USE_INET6)
-			hp = _hf_gethtbyname2(name, AF_INET6, info);
-		else
-			hp = NULL;
-		if (hp == NULL)
-			hp = _hf_gethtbyname2(name, AF_INET, info);
-		__res_put_state(res);
-	}
-#else
     hp = _hf_gethtbyname2(name, af, info);
-#endif
     if (hp == NULL) {
         if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
             return NS_UNAVAIL;  // glibc compatibility.