Minimal changes to bring the resolver into the C++ era

It took surprisingly few changes, considering the leap from C with some
K&R constructs to C++17; most of the diffs are adding explicit pointer
casts and renaming variables called 'class' or 'try'.

As a result of building as C++, some of the names are now mangled,
making clashes with bionic harder. However, some names remain C due to
the __BEGIN_DECLS / __END_DECLS macros, scheduled to be removed in a
later cleanup pass.

Test: atest netd_integration_test

Change-Id: I3aefb9078421ec42f98f97d917785b365545feba
diff --git a/Android.bp b/Android.bp
index 0d24768..999d433 100644
--- a/Android.bp
+++ b/Android.bp
@@ -2,22 +2,19 @@
     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: fix unused parameter warnings
-    cflags: [
-        "-Wno-unused-parameter",
+        "getaddrinfo.cpp",
+        "gethnamaddr.cpp",
+        "sethostent.cpp",
+        "res_cache.cpp",
+        "res_comp.cpp",
+        "res_data.cpp",
+        "res_debug.cpp",
+        "res_init.cpp",
+        "res_mkquery.cpp",
+        "res_query.cpp",
+        "res_send.cpp",
+        "res_state.cpp",
+        "res_stats.cpp",
     ],
     // TODO: stop depending on internal bionic headers
     include_dirs: [
diff --git a/getaddrinfo.c b/getaddrinfo.cpp
similarity index 97%
rename from getaddrinfo.c
rename to getaddrinfo.cpp
index cbe1cc4..d6912ec 100644
--- a/getaddrinfo.c
+++ b/getaddrinfo.cpp
@@ -197,7 +197,7 @@
 static int get_portmatch(const struct addrinfo*, const char*);
 static int get_port(const struct addrinfo*, const char*, int);
 static const struct afd* find_afd(int);
-static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*);
+static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
 
 static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
 static int _dns_getaddrinfo(void*, void*, va_list);
@@ -240,21 +240,21 @@
             error = EAI_MEMORY;                              \
             goto free;                                       \
         }                                                    \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 #define GET_PORT(ai, serv)                             \
     do {                                               \
         /* external reference: error and label free */ \
         error = get_port((ai), (serv), 0);             \
         if (error != 0) goto free;                     \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 #define GET_CANONNAME(ai, str)                              \
     do {                                                    \
         /* external reference: pai, error and label free */ \
         error = get_canonname(pai, (ai), (str));            \
         if (error != 0) goto free;                          \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 #define ERR(err)                                       \
     do {                                               \
@@ -262,11 +262,11 @@
         error = (err);                                 \
         goto bad;                                      \
         /*NOTREACHED*/                                 \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 #define MATCH_FAMILY(x, y, w) \
-    ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
-#define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
+    ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
+#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
 
 const char* gai_strerror(int ecode) {
     if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
@@ -519,9 +519,7 @@
     return error;
 }
 
-/*
- * FQDN hostname, DNS lookup
- */
+// FQDN hostname, DNS lookup
 static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
                         struct addrinfo** res, const struct android_net_context* netcontext) {
     struct addrinfo* result;
@@ -723,7 +721,7 @@
     const struct afd* afd;
     struct addrinfo* cur;
     int error;
-    char *cp, *hostname2 = NULL, *scope, *addr;
+    const char *cp, *scope, *addr;
     struct sockaddr_in6* sin6;
 
     assert(pai != NULL);
@@ -747,7 +745,7 @@
     /*
      * Handle special case of <scoped_address><delimiter><scope id>
      */
-    hostname2 = strdup(hostname);
+    char* hostname2 = strdup(hostname);
     if (hostname2 == NULL) return EAI_MEMORY;
     /* terminate at the delimiter */
     hostname2[cp - hostname] = '\0';
@@ -899,8 +897,8 @@
     return NULL;
 }
 
-/* convert a string to a scope identifier. */
-static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
+// Convert a string to a scope identifier.
+static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
     u_long lscopeid;
     struct in6_addr* a6;
     char* ep;
@@ -951,7 +949,7 @@
     do {                     \
         BOUNDS_CHECK(cp, x); \
         cp += (x);           \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 #define BOUNDS_CHECK(ptr, count)     \
     do {                             \
@@ -959,7 +957,7 @@
             h_errno = NO_RECOVERY;   \
             return NULL;             \
         }                            \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
                                   const struct addrinfo* pai) {
@@ -972,7 +970,7 @@
     int n;
     const u_char* eom;
     char *bp, *ep;
-    int type, class, ancount, qdcount;
+    int type, ancount, qdcount;
     int haveanswer, had_error;
     char tbuf[MAXDNAME];
     int (*name_ok)(const char*);
@@ -1043,12 +1041,12 @@
         BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
         type = _getshort(cp);
         cp += INT16SZ; /* type */
-        class = _getshort(cp);
+        int cl = _getshort(cp);
         cp += INT16SZ + INT32SZ; /* class, TTL */
         n = _getshort(cp);
         cp += INT16SZ; /* len */
         BOUNDS_CHECK(cp, n);
-        if (class != C_IN) {
+        if (cl != C_IN) {
             /* XXX - debug? syslog? */
             cp += n;
             continue; /* XXX - had_error++ ? */
@@ -1514,9 +1512,8 @@
     free(elems);
 }
 
-static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) {
+static int _dns_getaddrinfo(void* rv, void* /*cb_data*/, va_list ap) {
     struct addrinfo* ai;
-    querybuf *buf, *buf2;
     const char* name;
     const struct addrinfo* pai;
     struct addrinfo sentinel, *cur;
@@ -1534,12 +1531,12 @@
     memset(&sentinel, 0, sizeof(sentinel));
     cur = &sentinel;
 
-    buf = malloc(sizeof(*buf));
+    querybuf* buf = (querybuf*) malloc(sizeof(*buf));
     if (buf == NULL) {
         h_errno = NETDB_INTERNAL;
         return NS_NOTFOUND;
     }
-    buf2 = malloc(sizeof(*buf2));
+    querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
     if (buf2 == NULL) {
         free(buf);
         h_errno = NETDB_INTERNAL;
@@ -1547,7 +1544,7 @@
     }
 
     switch (pai->ai_family) {
-        case AF_UNSPEC:
+        case AF_UNSPEC: {
             /* prefer IPv6 */
             q.name = name;
             q.qclass = C_IN;
@@ -1576,6 +1573,7 @@
                 return NS_NOTFOUND;
             }
             break;
+        }
         case AF_INET:
             q.name = name;
             q.qclass = C_IN;
@@ -1715,7 +1713,7 @@
     return res0;
 }
 
-static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) {
+static int _files_getaddrinfo(void* rv, void* /*cb_data*/, va_list ap) {
     const char* name;
     const struct addrinfo* pai;
     struct addrinfo sentinel, *cur;
@@ -1769,7 +1767,6 @@
     ancount = 0;
 
     for (t = target; t; t = t->next) {
-        int class, type;
         u_char* answer;
         int anslen;
         u_int oflags;
@@ -1781,15 +1778,15 @@
         hp->rcode = NOERROR; /* default */
 
         /* make it easier... */
-        class = t->qclass;
-        type = t->qtype;
+        int cl = t->qclass;
+        int type = t->qtype;
         answer = t->answer;
         anslen = t->anslen;
 #ifdef DEBUG
-        if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
+        if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
 #endif
 
-        n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
+        n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
         if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
             (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
             n = res_nopt(res, n, buf, sizeof(buf), anslen);
diff --git a/gethnamaddr.c b/gethnamaddr.cpp
similarity index 94%
rename from gethnamaddr.c
rename to gethnamaddr.cpp
index 83cfe72..604f9c1 100644
--- a/gethnamaddr.c
+++ b/gethnamaddr.cpp
@@ -97,23 +97,17 @@
 #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
 #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
 
-#define addalias(d, s, arr, siz)                                   \
-    do {                                                           \
-        if (d >= &arr[siz]) {                                      \
-            char** xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \
-            if (xptr == NULL) goto nospc;                          \
-            d = xptr + (d - arr);                                  \
-            arr = xptr;                                            \
-            siz += 10;                                             \
-        }                                                          \
-        *d++ = s;                                                  \
-    } while (/*CONSTCOND*/ 0)
-
-#define setup(arr, siz)                          \
-    do {                                         \
-        arr = malloc((siz = 10) * sizeof(*arr)); \
-        if (arr == NULL) goto nospc;             \
-    } while (/*CONSTCOND*/ 0)
+#define addalias(d, s, arr, siz)                                            \
+    do {                                                                    \
+        if (d >= &arr[siz]) {                                               \
+            char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \
+            if (xptr == NULL) goto nospc;                                   \
+            d = xptr + (d - arr);                                           \
+            arr = xptr;                                                     \
+            siz += 10;                                                      \
+        }                                                                   \
+        *d++ = s;                                                           \
+    } while (0)
 
 static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
 
@@ -199,12 +193,12 @@
     do {                     \
         BOUNDS_CHECK(cp, x); \
         cp += (x);           \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 #define BOUNDS_CHECK(ptr, count)                     \
     do {                                             \
         if (eom - (ptr) < (count)) goto no_recovery; \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
 static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
                                  res_state res, struct hostent* hent, char* buf, size_t buflen,
@@ -215,12 +209,10 @@
     size_t qlen;
     const u_char *eom, *erdata;
     char *bp, **ap, **hap, *ep;
-    int type, class, ancount, qdcount;
+    int ancount, qdcount;
     int haveanswer, had_error;
     int toobig = 0;
     char tbuf[MAXDNAME];
-    char** aliases;
-    size_t maxaliases;
     char* addr_ptrs[MAXADDRS];
     const char* tname;
     int (*name_ok)(const char*);
@@ -244,7 +236,9 @@
             return NULL; /* XXX should be abort(); */
     }
 
-    setup(aliases, maxaliases);
+    size_t maxaliases = 10;
+    char** aliases = (char**) malloc(maxaliases * sizeof(char*));
+    if (!aliases) goto nospc;
     /*
      * find first satisfactory answer
      */
@@ -287,15 +281,15 @@
         }
         cp += n; /* name */
         BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
-        type = _getshort(cp);
+        int type = _getshort(cp);
         cp += INT16SZ; /* type */
-        class = _getshort(cp);
+        int cl = _getshort(cp);
         cp += INT16SZ + INT32SZ; /* class, TTL */
         n = _getshort(cp);
         cp += INT16SZ; /* len */
         BOUNDS_CHECK(cp, n);
         erdata = cp + n;
-        if (class != C_IN) {
+        if (cl != C_IN) {
             /* XXX - debug? syslog? */
             cp += n;
             continue; /* XXX - had_error++ ? */
@@ -474,7 +468,7 @@
     n = (int) (ap - aliases);
     qlen = (n + 1) * sizeof(*hent->h_aliases);
     if ((size_t)(ep - bp) < qlen) goto nospc;
-    hent->h_aliases = (void*) bp;
+    hent->h_aliases = (char**) bp;
     memcpy(bp, aliases, qlen);
     free(aliases);
     aliases = NULL;
@@ -483,7 +477,7 @@
     n = (int) (hap - addr_ptrs);
     qlen = (n + 1) * sizeof(*hent->h_addr_list);
     if ((size_t)(ep - bp) < qlen) goto nospc;
-    hent->h_addr_list = (void*) bp;
+    hent->h_addr_list = (char**) bp;
     memcpy(bp, addr_ptrs, qlen);
     *he = NETDB_SUCCESS;
     return hent;
@@ -750,12 +744,11 @@
 
 struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
                                     int* he) {
-    char *p, *name;
+    const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
+    char *name;
     char *cp, **q;
     int af, len;
     size_t anum;
-    char** aliases;
-    size_t maxaliases;
     struct in6_addr host_addr;
 
     if (hf == NULL) {
@@ -763,16 +756,17 @@
         errno = EINVAL;
         return NULL;
     }
-    p = NULL;
-    setup(aliases, maxaliases);
+    char* p = NULL;
+    size_t maxaliases = 10;
+    char** aliases = (char**) malloc(maxaliases * sizeof(char*));
+    if (!aliases) goto nospc;
 
     /* Allocate a new space to read file lines like upstream does.
      * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
      * as the buffer may be used to store content for a previous hostent
      * returned by non-reentrant functions like gethostbyname().
      */
-    const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
-    if ((p = malloc(line_buf_size)) == NULL) {
+    if ((p = (char*) malloc(line_buf_size)) == NULL) {
         goto nospc;
     }
     for (;;) {
@@ -860,13 +854,13 @@
     _DIAGASSERT(dst != NULL);
 
     /* Stash a temporary copy so our caller can update in place. */
-    (void) memcpy(tmp, src, NS_INADDRSZ);
+    memcpy(tmp, src, NS_INADDRSZ);
     /* Mark this ipv6 addr as a mapped ipv4. */
     for (i = 0; i < 10; i++) *p++ = 0x00;
     *p++ = 0xff;
     *p++ = 0xff;
     /* Retrieve the saved copy and we're done. */
-    (void) memcpy(p, tmp, NS_INADDRSZ);
+    memcpy(p, tmp, NS_INADDRSZ);
 }
 
 static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
@@ -932,13 +926,12 @@
     }
 }
 
-static int _dns_gethtbyname(void* rv, void* cb_data, va_list ap) {
-    querybuf* buf;
+static int _dns_gethtbyname(void* rv, void* /*cb_data*/, va_list ap) {
     int n, type;
     struct hostent* hp;
     const char* name;
     res_state res;
-    struct getnamaddr* info = rv;
+    struct getnamaddr* info = (struct getnamaddr*) rv;
 
     _DIAGASSERT(rv != NULL);
 
@@ -958,7 +951,7 @@
         default:
             return NS_UNAVAIL;
     }
-    buf = malloc(sizeof(*buf));
+    querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
     if (buf == NULL) {
         *info->he = NETDB_INTERNAL;
         return NS_NOTFOUND;
@@ -989,17 +982,14 @@
     return NS_SUCCESS;
 }
 
-static int _dns_gethtbyaddr(void* rv, void* cb_data, va_list ap) {
+static int _dns_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
     char qbuf[MAXDNAME + 1], *qp, *ep;
     int n;
-    querybuf* buf;
     struct hostent* hp;
     const unsigned char* uaddr;
     int advance;
     res_state res;
-    char* bf;
-    size_t blen;
-    struct getnamaddr* info = rv;
+    struct getnamaddr* info = (struct getnamaddr*) rv;
     const struct android_net_context* netcontext;
 
     _DIAGASSERT(rv != NULL);
@@ -1037,7 +1027,7 @@
             return NS_UNAVAIL;
     }
 
-    buf = malloc(sizeof(*buf));
+    querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
     if (buf == NULL) {
         *info->he = NETDB_INTERNAL;
         return NS_NOTFOUND;
@@ -1069,12 +1059,12 @@
         }
     }
 
-    bf = (void*) (hp->h_addr_list + 2);
-    blen = (size_t)(bf - info->buf);
+    char* bf = (char*) (hp->h_addr_list + 2);
+    size_t blen = (size_t)(bf - info->buf);
     if (blen + info->hp->h_length > info->buflen) goto nospc;
     hp->h_addr_list[0] = bf;
     hp->h_addr_list[1] = NULL;
-    (void) memcpy(bf, uaddr, (size_t) info->hp->h_length);
+    memcpy(bf, uaddr, (size_t) info->hp->h_length);
     if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
         if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
         map_v4v6_address(bf, bf);
@@ -1085,6 +1075,7 @@
     __res_put_state(res);
     *info->he = NETDB_SUCCESS;
     return NS_SUCCESS;
+
 nospc:
     errno = ENOSPC;
     *info->he = NETDB_INTERNAL;
@@ -1097,7 +1088,7 @@
 
 struct hostent* gethostbyname(const char* name) {
     struct hostent* result = NULL;
-    res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+    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;
@@ -1105,7 +1096,7 @@
 
 struct hostent* gethostbyname2(const char* name, int af) {
     struct hostent* result = NULL;
-    res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+    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;
@@ -1137,7 +1128,7 @@
     struct hostent* hp;
     res_state res = __res_get_state();
     if (res == NULL) return NULL;
-    res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+    struct res_static* rs = __res_get_static();  // For thread-safety.
     hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
                                 &h_errno, netcontext);
     __res_put_state(res);
@@ -1161,13 +1152,13 @@
 
 __LIBC_HIDDEN__ struct hostent* android_gethostbyaddrfornetcontext_proxy(
         const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
-    res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+    struct res_static* rs = __res_get_static();  // For thread-safety.
     return android_gethostbyaddrfornetcontext_proxy_internal(
             addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
 }
 
 struct hostent* gethostent(void) {
-    res_static rs = __res_get_static();
+    struct res_static* rs = __res_get_static();  // For thread-safety.
     if (!rs->hostf) {
         sethostent_r(&rs->hostf);
         if (!rs->hostf) {
diff --git a/hostent.h b/hostent.h
index b3ae316..c0f4ea9 100644
--- a/hostent.h
+++ b/hostent.h
@@ -58,34 +58,25 @@
 int _hf_gethtbyaddr(void*, void*, va_list);
 int _hf_gethtbyname(void*, void*, va_list);
 
-#ifdef YP
-/* NIS lookup */
-int _yp_gethtbyaddr(void*, void*, va_list);
-int _yp_gethtbyname(void*, void*, va_list);
-#endif
-
-#define HENT_ARRAY(dst, anum, ptr, len)          \
-    do {                                         \
+#define HENT_ARRAY(dst, anum, ptr, len) do {     \
         size_t _len = (anum + 1) * sizeof(*dst); \
         if (_len > len) goto nospc;              \
-        dst = (void*) ptr;                       \
+        dst = (char**) ptr;                      \
         ptr += _len;                             \
         len -= _len;                             \
-    } while (/*CONSTCOND*/ 0)
+    } while (0)
 
-#define HENT_COPY(dst, src, slen, ptr, len)  \
-    do {                                     \
-        if ((size_t) slen > len) goto nospc; \
-        memcpy(ptr, src, (size_t) slen);     \
-        dst = ptr;                           \
-        ptr += slen;                         \
-        len -= slen;                         \
-    } while (/* CONSTCOND */ 0)
+#define HENT_COPY(dst, src, slen, ptr, len) do { \
+        if ((size_t) slen > len) goto nospc;     \
+        memcpy(ptr, src, (size_t) slen);         \
+        dst = ptr;                               \
+        ptr += slen;                             \
+        len -= slen;                             \
+    } while (0)
 
-#define HENT_SCOPY(dst, src, ptr, len)       \
-    do {                                     \
+#define HENT_SCOPY(dst, src, ptr, len) do {  \
         size_t _len = strlen(src) + 1;       \
         HENT_COPY(dst, src, _len, ptr, len); \
-    } while (/* CONSTCOND */ 0)
+    } while (0)
 
 #endif /* _DNS_NET_HOSTENT_H */
diff --git a/res_cache.c b/res_cache.cpp
similarity index 95%
rename from res_cache.c
rename to res_cache.cpp
index 230ebef..4d13126 100644
--- a/res_cache.c
+++ b/res_cache.cpp
@@ -131,26 +131,12 @@
  */
 #define CONFIG_MAX_ENTRIES 64 * 2 * 5
 
-/****************************************************************************/
-/****************************************************************************/
-/*****                                                                  *****/
-/*****                                                                  *****/
-/*****                                                                  *****/
-/****************************************************************************/
-/****************************************************************************/
-
 /* set to 1 to debug cache operations */
 #define DEBUG 0
 
 /* set to 1 to debug query data */
 #define DEBUG_DATA 0
 
-#if DEBUG
-#define __DEBUG__
-#else
-#define __DEBUG__ __attribute__((unused))
-#endif
-
 #undef XLOG
 
 #define XLOG(...)                                                          \
@@ -162,8 +148,7 @@
         }                                                                  \
     })
 
-/** BOUNDED BUFFER FORMATTING
- **/
+/** BOUNDED BUFFER FORMATTING **/
 
 /* technical note:
  *
@@ -207,7 +192,7 @@
  */
 
 /* add a char to a bounded buffer */
-char* _bprint_c(char* p, char* end, int c) {
+static char* _bprint_c(char* p, char* end, int c) {
     if (p < end) {
         if (p + 1 == end)
             *p++ = 0;
@@ -220,7 +205,7 @@
 }
 
 /* add a sequence of bytes to a bounded buffer */
-char* _bprint_b(char* p, char* end, const char* buf, int len) {
+static char* _bprint_b(char* p, char* end, const char* buf, int len) {
     int avail = end - p;
 
     if (avail <= 0 || len <= 0) return p;
@@ -239,13 +224,12 @@
 }
 
 /* add a string to a bounded buffer */
-char* _bprint_s(char* p, char* end, const char* str) {
+static char* _bprint_s(char* p, char* end, const char* str) {
     return _bprint_b(p, end, str, strlen(str));
 }
 
 /* add a formatted string to a bounded buffer */
-char* _bprint(char* p, char* end, const char* format, ...) __DEBUG__;
-char* _bprint(char* p, char* end, const char* format, ...) {
+[[maybe_unused]] static char* _bprint(char* p, char* end, const char* format, ...) {
     int avail, n;
     va_list args;
 
@@ -268,7 +252,7 @@
 }
 
 /* add a hex value to a bounded buffer, up to 8 digits */
-char* _bprint_hex(char* p, char* end, unsigned value, int numDigits) {
+static char* _bprint_hex(char* p, char* end, unsigned value, int numDigits) {
     char text[sizeof(unsigned) * 2];
     int nn = 0;
 
@@ -279,7 +263,7 @@
 }
 
 /* add the hexadecimal dump of some memory area to a bounded buffer */
-char* _bprint_hexdump(char* p, char* end, const uint8_t* data, int datalen) {
+static char* _bprint_hexdump(char* p, char* end, const uint8_t* data, int datalen) {
     int lineSize = 16;
 
     while (datalen > 0) {
@@ -313,8 +297,7 @@
 }
 
 /* dump the content of a query of packet to the log */
-void XLOG_BYTES(const void* base, int len) __DEBUG__;
-void XLOG_BYTES(const void* base, int len) {
+[[maybe_unused]] static void XLOG_BYTES(const uint8_t* base, int len) {
     if (DEBUG_DATA) {
         char buff[1024];
         char *p = buff, *end = p + sizeof(buff);
@@ -323,7 +306,6 @@
         XLOG("%s", buff);
     }
 }
-__DEBUG__
 
 static time_t _time_now(void) {
     struct timeval tv;
@@ -460,8 +442,7 @@
     return (p[0] << 8) | p[1];
 }
 
-/** QUERY CHECKING
- **/
+/** QUERY CHECKING **/
 
 /* check bytes in a dns packet. returns 1 on success, 0 on failure.
  * the cursor is only advanced in the case of success
@@ -595,8 +576,7 @@
     return 1;
 }
 
-/** QUERY DEBUGGING
- **/
+/** QUERY DEBUGGING **/
 #if DEBUG
 static char* _dnsPacket_bprintQName(DnsPacket* packet, char* bp, char* bend) {
     const uint8_t* p = packet->cursor;
@@ -941,14 +921,6 @@
     return 1;
 }
 
-/****************************************************************************/
-/****************************************************************************/
-/*****                                                                  *****/
-/*****                                                                  *****/
-/*****                                                                  *****/
-/****************************************************************************/
-/****************************************************************************/
-
 /* cache entry. for simplicity, 'hash' and 'hlink' are inlined in this
  * structure though they are conceptually part of the hash table.
  *
@@ -968,7 +940,7 @@
     int id;         /* for debugging purpose */
 } Entry;
 
-/**
+/*
  * Find the TTL for a negative DNS result.  This is defined as the minimum
  * of the SOA records TTL and the MINIMUM-TTL field (RFC-2308).
  *
@@ -1015,7 +987,7 @@
     return result;
 }
 
-/**
+/*
  * Parse the answer records and find the appropriate
  * smallest TTL among the records.  This might be from
  * the answer records if found or from the SOA record
@@ -1034,7 +1006,7 @@
     ns_rr rr;
 
     result = 0;
-    if (ns_initparse(answer, answerlen, &handle) >= 0) {
+    if (ns_initparse((const uint8_t*) answer, answerlen, &handle) >= 0) {
         // get number of answer records
         ancount = ns_msg_count(handle, ns_s_an);
 
@@ -1100,11 +1072,11 @@
 
     memset(e, 0, sizeof(*e));
 
-    e->query = query;
+    e->query = (const uint8_t*) query;
     e->querylen = querylen;
     e->hash = entry_hash(e);
 
-    _dnsPacket_init(pack, query, querylen);
+    _dnsPacket_init(pack, e->query, e->querylen);
 
     return _dnsPacket_checkQuery(pack);
 }
@@ -1115,7 +1087,7 @@
     int size;
 
     size = sizeof(*e) + init->querylen + answerlen;
-    e = calloc(size, 1);
+    e = (Entry*) calloc(size, 1);
     if (e == NULL) return e;
 
     e->hash = init->hash;
@@ -1239,7 +1211,7 @@
         }
 
         if (!exist) {
-            ri = calloc(1, sizeof(struct pending_req_info));
+            ri = (struct pending_req_info*) calloc(1, sizeof(struct pending_req_info));
             if (ri) {
                 ri->hash = key->hash;
                 pthread_cond_init(&ri->cond, NULL);
@@ -1345,10 +1317,10 @@
 static struct resolv_cache* _resolv_cache_create(void) {
     struct resolv_cache* cache;
 
-    cache = calloc(sizeof(*cache), 1);
+    cache = (struct resolv_cache*) calloc(sizeof(*cache), 1);
     if (cache) {
         cache->max_entries = _res_cache_get_max_entries();
-        cache->entries = calloc(sizeof(*cache->entries), cache->max_entries);
+        cache->entries = (Entry*) calloc(sizeof(*cache->entries), cache->max_entries);
         if (cache->entries) {
             cache->mru_list.mru_prev = cache->mru_list.mru_next = &cache->mru_list;
             XLOG("%s: cache created\n", __FUNCTION__);
@@ -1672,14 +1644,6 @@
     pthread_mutex_unlock(&_res_cache_list_lock);
 }
 
-/****************************************************************************/
-/****************************************************************************/
-/*****                                                                  *****/
-/*****                                                                  *****/
-/*****                                                                  *****/
-/****************************************************************************/
-/****************************************************************************/
-
 // Head of the list of caches.  Protected by _res_cache_list_lock.
 static struct resolv_cache_info _res_cache_list;
 
@@ -1771,26 +1735,18 @@
 }
 
 static struct resolv_cache_info* _create_cache_info(void) {
-    struct resolv_cache_info* cache_info;
-
-    cache_info = calloc(sizeof(*cache_info), 1);
-    return cache_info;
+    return (struct resolv_cache_info*) calloc(sizeof(struct resolv_cache_info), 1);
 }
 
 static void _insert_cache_info_locked(struct resolv_cache_info* cache_info) {
     struct resolv_cache_info* last;
-
-    for (last = &_res_cache_list; last->next; last = last->next)
-        ;
-
+    for (last = &_res_cache_list; last->next; last = last->next) {}
     last->next = cache_info;
 }
 
 static struct resolv_cache* _find_named_cache_locked(unsigned netid) {
     struct resolv_cache_info* info = _find_cache_info_locked(netid);
-
     if (info != NULL) return info->cache;
-
     return NULL;
 }
 
@@ -1818,7 +1774,7 @@
 int _resolv_set_nameservers_for_net(unsigned netid, const char** servers, unsigned numservers,
                                     const char* domains, const struct __res_params* params) {
     char sbuf[NI_MAXSERV];
-    register char* cp;
+    char* cp;
     int* offset;
     struct addrinfo* nsaddrinfo[MAXNS];
 
@@ -2002,8 +1958,8 @@
         // but the setting/offset-computer only runs when set/changed
         // WARNING: Don't use str*cpy() here, this string contains zeroes.
         memcpy(statp->defdname, info->defdname, sizeof(statp->defdname));
-        register char** pp = statp->dnsrch;
-        register int* p = info->dnsrch_offset;
+        char** pp = statp->dnsrch;
+        int* p = info->dnsrch_offset;
         while (pp < statp->dnsrch + MAXDNSRCH && *p != -1) {
             *pp++ = &statp->defdname[0] + *p++;
         }
diff --git a/res_comp.c b/res_comp.cpp
similarity index 88%
rename from res_comp.c
rename to res_comp.cpp
index c194d92..1eb00c7 100644
--- a/res_comp.c
+++ b/res_comp.cpp
@@ -69,26 +69,13 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  * 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_comp.c	8.1 (Berkeley) 6/4/93";
-static const char rcsid[] = "Id: res_comp.c,v 1.1.2.1.4.1 2004/03/09 08:33:54 marka Exp";
-#else
-__RCSID("$NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
 
 #include <arpa/nameser.h>
 #include <ctype.h>
 #include <netinet/in.h>
 #include <sys/param.h>
 #include <sys/types.h>
-#ifdef ANDROID_CHANGES
 #include "resolv_private.h"
-#else
-#include <resolv.h>
-#endif
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -222,28 +209,3 @@
         if (!domainchar(ch)) return (0);
     return (1);
 }
-
-#ifdef BIND_4_COMPAT
-/*
- * This module must export the following externally-visible symbols:
- *	___putlong
- *	___putshort
- *	__getlong
- *	__getshort
- * Note that one _ comes from C and the others come from us.
- */
-void __putlong(u_int32_t src, u_char* dst) {
-    ns_put32(src, dst);
-}
-void __putshort(u_int16_t src, u_char* dst) {
-    ns_put16(src, dst);
-}
-#ifndef __ultrix__
-u_int32_t _getlong(const u_char* src) {
-    return (ns_get32(src));
-}
-u_int16_t _getshort(const u_char* src) {
-    return (ns_get16(src));
-}
-#endif /*__ultrix__*/
-#endif /*BIND_4_COMPAT*/
diff --git a/res_data.c b/res_data.cpp
similarity index 69%
rename from res_data.c
rename to res_data.cpp
index f0f7d02..45c311a 100644
--- a/res_data.c
+++ b/res_data.cpp
@@ -32,10 +32,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "res_private.h"  // res_ourserver_p()
 #include "resolv_private.h"
 
 __LIBC_HIDDEN__
-const char* const _res_opcodes[] = {
+extern const char* const _res_opcodes[] = {
         "QUERY",  "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
         "NOTIFY",                                 /* experimental */
         "UPDATE", "6",      "7",       "8",        "9",       "10",
@@ -44,10 +45,6 @@
 
 extern struct __res_state _nres;
 
-/* Proto. */
-
-int res_ourserver_p(const res_state, const struct sockaddr*);
-
 #define res_need_init() ((_nres.options & RES_INIT) == 0U)
 
 int res_init(void) {
@@ -101,32 +98,32 @@
     res_pquery(&_nres, msg, len, file);
 }
 
-int res_mkquery(int op,                 /* opcode of query */
-                const char* dname,      /* domain name */
-                int class, int type,    /* class and type of query */
-                const u_char* data,     /* resource record data */
-                int datalen,            /* length of data */
-                const u_char* newrr_in, /* new rr for modify or append */
-                u_char* buf,            /* buffer to put query */
-                int buflen)             /* size of buffer */
+int res_mkquery(int op,                 // opcode of query
+                const char* dname,      // domain name
+                int cl, int type,       // class and type of query
+                const u_char* data,     // resource record data
+                int datalen,            // length of data
+                const u_char* newrr_in, // new rr for modify or append
+                u_char* buf,            // buffer to put query
+                int buflen)             // size of buffer
 {
     if (res_need_init() && res_init() == -1) {
         RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
-        return (-1);
+        return -1;
     }
-    return (res_nmkquery(&_nres, op, dname, class, type, data, datalen, newrr_in, buf, buflen));
+    return res_nmkquery(&_nres, op, dname, cl, type, data, datalen, newrr_in, buf, buflen);
 }
 
-int res_query(const char* name,    /* domain name */
-              int class, int type, /* class and type of query */
-              u_char* answer,      /* buffer to put answer */
-              int anslen)          /* size of answer buffer */
+int res_query(const char* name,    // domain name
+              int cl, int type,    // class and type of query
+              u_char* answer,      // buffer to put answer
+              int anslen)          // size of answer buffer
 {
     if (res_need_init() && res_init() == -1) {
         RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
-        return (-1);
+        return -1;
     }
-    return (res_nquery(&_nres, name, class, type, answer, anslen));
+    return res_nquery(&_nres, name, cl, type, answer, anslen);
 }
 
 void res_send_setqhook(res_send_qhook hook) {
@@ -138,52 +135,52 @@
 }
 
 int res_isourserver(const struct sockaddr_in* inp) {
-    return (res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp));
+    return res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp);
 }
 
 int res_send(const u_char* buf, int buflen, u_char* ans, int anssiz) {
     if (res_need_init() && res_init() == -1) {
         /* errno should have been set by res_init() in this case. */
-        return (-1);
+        return -1;
     }
 
-    return (res_nsend(&_nres, buf, buflen, ans, anssiz));
+    return res_nsend(&_nres, buf, buflen, ans, anssiz);
 }
 
 void res_close(void) {
     res_nclose(&_nres);
 }
 
-int res_search(const char* name,    /* domain name */
-               int class, int type, /* class and type of query */
-               u_char* answer,      /* buffer to put answer */
-               int anslen)          /* size of answer */
+int res_search(const char* name,    // domain name
+               int cl, int type,    // class and type of query
+               u_char* answer,      // buffer to put answer
+               int anslen)          // size of answer
 {
     if (res_need_init() && res_init() == -1) {
         RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
-        return (-1);
+        return -1;
     }
 
-    return (res_nsearch(&_nres, name, class, type, answer, anslen));
+    return res_nsearch(&_nres, name, cl, type, answer, anslen);
 }
 
-int res_querydomain(const char* name, const char* domain, int class,
-                    int type,       /* class and type of query */
-                    u_char* answer, /* buffer to put answer */
-                    int anslen)     /* size of answer */
+int res_querydomain(const char* name, const char* domain,
+                    int cl, int type,  // class and type of query
+                    u_char* answer,    // buffer to put answer
+                    int anslen)        // size of answer
 {
     if (res_need_init() && res_init() == -1) {
         RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
-        return (-1);
+        return -1;
     }
 
-    return (res_nquerydomain(&_nres, name, domain, class, type, answer, anslen));
+    return res_nquerydomain(&_nres, name, domain, cl, type, answer, anslen);
 }
 
 int res_opt(int a, u_char* b, int c, int d) {
     return res_nopt(&_nres, a, b, c, d);
 }
 
-const char* hostalias(const char* name) {
+const char* hostalias(const char* /*name*/) {
     return NULL;
 }
diff --git a/res_debug.c b/res_debug.cpp
similarity index 98%
rename from res_debug.c
rename to res_debug.cpp
index 76d958d..41eb93e 100644
--- a/res_debug.c
+++ b/res_debug.cpp
@@ -126,7 +126,6 @@
                        FILE* file) {
     int n, sflag, rrnum;
     int buflen = 2048;
-    char* buf;
     ns_opcode opcode;
     ns_rr rr;
 
@@ -136,7 +135,7 @@
     sflag = (int) (statp->pfcode & pflag);
     if (statp->pfcode && !sflag) return;
 
-    buf = malloc((size_t) buflen);
+    char* buf = (char*) malloc((size_t) buflen);
     if (buf == NULL) {
         fprintf(file, ";; memory allocation failure\n");
         return;
@@ -204,7 +203,9 @@
                 if (errno == ENOSPC) {
                     free(buf);
                     buf = NULL;
-                    if (buflen < 131072) buf = malloc((size_t)(buflen += 1024));
+                    if (buflen < 131072) {
+                        buf = (char*) malloc((size_t)(buflen += 1024));
+                    }
                     if (buf == NULL) {
                         fprintf(file, ";; memory allocation failure\n");
                         return;
@@ -220,7 +221,7 @@
         rrnum++;
     }
 cleanup:
-    if (buf != NULL) free(buf);
+    free(buf);
 }
 
 /*
@@ -538,15 +539,15 @@
 /*
  * Return a mnemonic for class.
  */
-const char* p_class(int class) {
+const char* p_class(int cl) {
     int success;
     const char* result;
     static char classbuf[20];
 
-    result = sym_ntos(__p_class_syms, class, &success);
+    result = sym_ntos(__p_class_syms, cl, &success);
     if (success) return (result);
-    if (class < 0 || class > 0xffff) return ("BADCLASS");
-    snprintf(classbuf, sizeof(classbuf), "CLASS%d", class);
+    if (cl < 0 || cl > 0xffff) return ("BADCLASS");
+    snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
     return (classbuf);
 }
 
@@ -994,9 +995,9 @@
              eastwest, altsign, altmeters, altfrac, (sizestr != NULL) ? sizestr : error,
              (hpstr != NULL) ? hpstr : error, (vpstr != NULL) ? vpstr : error);
 
-    if (sizestr != NULL) free(sizestr);
-    if (hpstr != NULL) free(hpstr);
-    if (vpstr != NULL) free(vpstr);
+    free(sizestr);
+    free(hpstr);
+    free(vpstr);
 
     return (ascii);
 }
diff --git a/res_init.c b/res_init.cpp
similarity index 98%
rename from res_init.c
rename to res_init.cpp
index 188df66..f21ebad 100644
--- a/res_init.c
+++ b/res_init.cpp
@@ -134,7 +134,7 @@
 
 /* This function has to be reachable by res_data.c but not publicly. */
 int __res_vinit(res_state statp, int preinit) {
-    register char *cp, **pp;
+    char *cp, **pp;
     char buf[BUFSIZ];
     int nserv = 0; /* number of nameserver records read from file */
     int havesearch = 0;
@@ -165,7 +165,7 @@
     statp->qhook = NULL;
     statp->rhook = NULL;
     statp->_u._ext.nscount = 0;
-    statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
+    statp->_u._ext.ext = (struct __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;
diff --git a/res_mkquery.c b/res_mkquery.cpp
similarity index 96%
rename from res_mkquery.c
rename to res_mkquery.cpp
index 6d9a269..bda6ad9 100644
--- a/res_mkquery.c
+++ b/res_mkquery.cpp
@@ -95,23 +95,23 @@
  */
 int res_nmkquery(res_state statp, int op, /* opcode of query */
                  const char* dname,       /* domain name */
-                 int class, int type,     /* class and type of query */
+                 int cl, int type,        /* class and type of query */
                  const u_char* data,      /* resource record data */
                  int datalen,             /* length of data */
                  const u_char* newrr_in,  /* new rr for modify or append */
                  u_char* buf,             /* buffer to put query */
                  int buflen)              /* size of buffer */
 {
-    register HEADER* hp;
-    register u_char *cp, *ep;
-    register int n;
+    HEADER* hp;
+    u_char *cp, *ep;
+    int n;
     u_char *dnptrs[20], **dpp, **lastdnptr;
 
     UNUSED(newrr_in);
 
 #ifdef DEBUG
     if (statp->options & RES_DEBUG)
-        printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(class),
+        printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(cl),
                p_type(type));
 #endif
     /*
@@ -142,7 +142,7 @@
             cp += n;
             ns_put16(type, cp);
             cp += INT16SZ;
-            ns_put16(class, cp);
+            ns_put16(cl, cp);
             cp += INT16SZ;
             hp->qdcount = htons(1);
             if (op == QUERY || data == NULL) break;
@@ -155,7 +155,7 @@
             cp += n;
             ns_put16(T_NULL, cp);
             cp += INT16SZ;
-            ns_put16(class, cp);
+            ns_put16(cl, cp);
             cp += INT16SZ;
             ns_put32(0, cp);
             cp += INT32SZ;
@@ -172,7 +172,7 @@
             *cp++ = '\0'; /* no domain name */
             ns_put16(type, cp);
             cp += INT16SZ;
-            ns_put16(class, cp);
+            ns_put16(cl, cp);
             cp += INT16SZ;
             ns_put32(0, cp);
             cp += INT32SZ;
@@ -202,8 +202,8 @@
              int buflen,              /* size of buffer */
              int anslen)              /* UDP answer buffer size */
 {
-    register HEADER* hp;
-    register u_char *cp, *ep;
+    HEADER* hp;
+    u_char *cp, *ep;
     u_int16_t flags = 0;
 
 #ifdef DEBUG
diff --git a/res_query.c b/res_query.cpp
similarity index 88%
rename from res_query.c
rename to res_query.cpp
index 30b7921..2a0ce82 100644
--- a/res_query.c
+++ b/res_query.cpp
@@ -106,10 +106,10 @@
  *
  * Caller must parse answer and determine whether it answers the question.
  */
-int res_nquery(res_state statp, const char* name, /* domain name */
-               int class, int type,               /* class and type of query */
-               u_char* answer,                    /* buffer to put answer */
-               int anslen)                        /* size of answer buffer */
+int res_nquery(res_state statp, const char* name, // domain name
+               int cl, int type,                  // class and type of query
+               u_char* answer,                    // buffer to put answer
+               int anslen)                        // size of answer buffer
 {
     u_char buf[MAXPACKET];
     HEADER* hp = (HEADER*) (void*) answer;
@@ -122,10 +122,10 @@
     hp->rcode = NOERROR; /* default */
 
 #ifdef DEBUG
-    if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, class, type);
+    if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, cl, type);
 #endif
 
-    n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
+    n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
 #ifdef RES_USE_EDNS0
     if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
         (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U)
@@ -136,7 +136,7 @@
         if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
 #endif
         RES_SET_H_ERRNO(statp, NO_RECOVERY);
-        return (n);
+        return n;
     }
     n = res_nsend(statp, buf, n, answer, anslen);
     if (n < 0) {
@@ -153,7 +153,7 @@
         if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
 #endif
         RES_SET_H_ERRNO(statp, TRY_AGAIN);
-        return (n);
+        return n;
     }
 
     if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
@@ -179,9 +179,9 @@
                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
                 break;
         }
-        return (-1);
+        return -1;
     }
-    return (n);
+    return n;
 }
 
 /*
@@ -191,7 +191,7 @@
  * is detected.  Error code, if any, is left in H_ERRNO.
  */
 int res_nsearch(res_state statp, const char* name, /* domain name */
-                int class, int type,               /* class and type of query */
+                int cl, int type,                  /* class and type of query */
                 u_char* answer,                    /* buffer to put answer */
                 int anslen)                        /* size of answer */
 {
@@ -214,7 +214,7 @@
 
     /* If there aren't any dots, it could be a user-level alias. */
     if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp)) != NULL)
-        return (res_nquery(statp, cp, class, type, answer, anslen));
+        return res_nquery(statp, cp, cl, type, answer, anslen);
 
     /*
      * If there are enough dots in the name, let's just give it a
@@ -223,8 +223,8 @@
      */
     saved_herrno = -1;
     if (dots >= statp->ndots || trailing_dot) {
-        ret = res_nquerydomain(statp, name, NULL, class, type, answer, anslen);
-        if (ret > 0 || trailing_dot) return (ret);
+        ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen);
+        if (ret > 0 || trailing_dot) return ret;
         saved_herrno = statp->res_h_errno;
         tried_as_is++;
     }
@@ -254,8 +254,8 @@
             if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
                 root_on_list++;
 
-            ret = res_nquerydomain(statp, name, *domain, class, type, answer, anslen);
-            if (ret > 0) return (ret);
+            ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen);
+            if (ret > 0) return ret;
 
             /*
              * If no server present, give up.
@@ -272,7 +272,7 @@
              */
             if (errno == ECONNREFUSED) {
                 RES_SET_H_ERRNO(statp, TRY_AGAIN);
-                return (-1);
+                return -1;
             }
 
             switch (statp->res_h_errno) {
@@ -307,8 +307,8 @@
      */
     if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
         !(tried_as_is || root_on_list)) {
-        ret = res_nquerydomain(statp, name, NULL, class, type, answer, anslen);
-        if (ret > 0) return (ret);
+        ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen);
+        if (ret > 0) return ret;
     }
 
     /* if we got here, we didn't satisfy the search.
@@ -324,17 +324,17 @@
         RES_SET_H_ERRNO(statp, NO_DATA);
     else if (got_servfail)
         RES_SET_H_ERRNO(statp, TRY_AGAIN);
-    return (-1);
+    return -1;
 }
 
 /*
  * Perform a call on res_query on the concatenation of name and domain,
  * removing a trailing dot from name if domain is NULL.
  */
-int res_nquerydomain(res_state statp, const char* name, const char* domain, int class,
-                     int type,       /* class and type of query */
-                     u_char* answer, /* buffer to put answer */
-                     int anslen)     /* size of answer */
+int res_nquerydomain(res_state statp, const char* name, const char* domain,
+                     int cl, int type,  /* class and type of query */
+                     u_char* answer,    /* buffer to put answer */
+                     int anslen)        /* size of answer */
 {
     char nbuf[MAXDNAME];
     const char* longname = nbuf;
@@ -342,7 +342,7 @@
 
 #ifdef DEBUG
     if (statp->options & RES_DEBUG)
-        printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", class,
+        printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", cl,
                type);
 #endif
     if (domain == NULL) {
@@ -353,7 +353,7 @@
         n = strlen(name);
         if (n >= MAXDNAME) {
             RES_SET_H_ERRNO(statp, NO_RECOVERY);
-            return (-1);
+            return -1;
         }
         n--;
         if (n >= 0 && name[n] == '.') {
@@ -366,13 +366,14 @@
         d = strlen(domain);
         if (n + d + 1 >= MAXDNAME) {
             RES_SET_H_ERRNO(statp, NO_RECOVERY);
-            return (-1);
+            return -1;
         }
         snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
     }
-    return (res_nquery(statp, longname, class, type, answer, anslen));
+    return res_nquery(statp, longname, cl, type, answer, anslen);
 }
 
-const char* res_hostalias(const res_state statp, const char* name, char* dst, size_t siz) {
-    return (NULL);
+const char* res_hostalias(const res_state /*statp*/, const char* /*name*/, char* /*dst*/,
+                          size_t /*siz*/) {
+    return NULL;
 }
diff --git a/res_send.c b/res_send.cpp
similarity index 95%
rename from res_send.c
rename to res_send.cpp
index ebac862..fad08ee 100644
--- a/res_send.c
+++ b/res_send.cpp
@@ -104,11 +104,6 @@
 #include "resolv_private.h"
 #include "resolv_stats.h"
 
-#ifndef DE_CONST
-#define DE_CONST(c, v) \
-    v = ((c) ? strchr((const void*) (c), *(const char*) (const void*) (c)) : NULL)
-#endif
-
 /* Options.  Leave them on. */
 #ifndef DEBUG
 #define DEBUG
@@ -121,8 +116,8 @@
 
 /* Forward. */
 
-static int get_salen __P((const struct sockaddr*) );
-static struct sockaddr* get_nsaddr __P((res_state, size_t));
+static int get_salen(const struct sockaddr*);
+static struct sockaddr* get_nsaddr(res_state, size_t);
 static int send_vc(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
                    int, time_t*, int*, int*);
 static int send_dg(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
@@ -242,8 +237,8 @@
         if (!bind(s, &u.sa, slen)) return 0;
     }
 
-    /* nothing after 10 tries, our network table is probably busy */
-    /* let the system decide which port is best */
+    // nothing after 10 attempts, our network table is probably busy
+    // let the system decide which port is best
     if (family == AF_INET)
         u.sin.sin_port = 0;
     else
@@ -303,8 +298,8 @@
 }
 
 /* int
- * res_nameinquery(name, type, class, buf, eom)
- *	look for (name,type,class) in the query section of packet (buf,eom)
+ * res_nameinquery(name, type, cl, buf, eom)
+ *	look for (name, type, cl) in the query section of packet (buf, eom)
  * requires:
  *	buf + HFIXEDSZ <= eom
  * returns:
@@ -314,23 +309,21 @@
  * author:
  *	paul vixie, 29may94
  */
-int res_nameinquery(const char* name, int type, int class, const u_char* buf, const u_char* eom) {
+int res_nameinquery(const char* name, int type, int cl, const u_char* buf, const u_char* eom) {
     const u_char* cp = buf + HFIXEDSZ;
     int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount);
 
     while (qdcount-- > 0) {
         char tname[MAXDNAME + 1];
-        int n, ttype, tclass;
-
-        n = dn_expand(buf, eom, cp, tname, sizeof tname);
+        int n = dn_expand(buf, eom, cp, tname, sizeof tname);
         if (n < 0) return (-1);
         cp += n;
         if (cp + 2 * INT16SZ > eom) return (-1);
-        ttype = ns_get16(cp);
+        int ttype = ns_get16(cp);
         cp += INT16SZ;
-        tclass = ns_get16(cp);
+        int tclass = ns_get16(cp);
         cp += INT16SZ;
-        if (ttype == type && tclass == class && ns_samename(tname, name) == 1) return (1);
+        if (ttype == type && tclass == cl && ns_samename(tname, name) == 1) return (1);
     }
     return (0);
 }
@@ -364,15 +357,13 @@
     if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0);
     while (qdcount-- > 0) {
         char tname[MAXDNAME + 1];
-        int n, ttype, tclass;
-
-        n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
+        int n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
         if (n < 0) return (-1);
         cp += n;
         if (cp + 2 * INT16SZ > eom1) return (-1);
-        ttype = ns_get16(cp);
+        int ttype = ns_get16(cp);
         cp += INT16SZ;
-        tclass = ns_get16(cp);
+        int tclass = ns_get16(cp);
         cp += INT16SZ;
         if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0);
     }
@@ -380,8 +371,7 @@
 }
 
 int res_nsend(res_state statp, const u_char* buf, int buflen, u_char* ans, int anssiz) {
-    int gotsomewhere, terrno, try
-        , v_circuit, resplen, ns, n;
+    int gotsomewhere, terrno, v_circuit, resplen, n;
     char abuf[NI_MAXHOST];
     ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
 
@@ -426,7 +416,7 @@
         if (EXT(statp).nscount != statp->nscount) {
             needclose++;
         } else {
-            for (ns = 0; ns < statp->nscount; ns++) {
+            for (int ns = 0; ns < statp->nscount; ns++) {
                 if (statp->nsaddr_list[ns].sin_family &&
                     !sock_eq((struct sockaddr*) (void*) &statp->nsaddr_list[ns],
                              (struct sockaddr*) (void*) &EXT(statp).ext->nsaddrs[ns])) {
@@ -457,7 +447,7 @@
      * Maybe initialize our private copy of the ns_addr_list.
      */
     if (EXT(statp).nscount == 0) {
-        for (ns = 0; ns < statp->nscount; ns++) {
+        for (int ns = 0; ns < statp->nscount; ns++) {
             EXT(statp).nstimes[ns] = RES_MAXTIME;
             EXT(statp).nssocks[ns] = -1;
             if (!statp->nsaddr_list[ns].sin_family) continue;
@@ -481,7 +471,7 @@
         ina = statp->nsaddr_list[0];
         fd = EXT(statp).nssocks[0];
         nstime = EXT(statp).nstimes[0];
-        for (ns = 0; ns < lastns; ns++) {
+        for (int ns = 0; ns < lastns; ns++) {
             if (EXT(statp).ext != NULL)
                 EXT(statp).ext->nsaddrs[ns] = EXT(statp).ext->nsaddrs[ns + 1];
             statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
@@ -497,14 +487,14 @@
     /*
      * Send request, RETRY times, or until successful.
      */
-    for (try = 0; try < statp->retry; try ++) {
+    for (int attempt = 0; attempt < statp->retry; ++attempt) {
         struct __res_stats stats[MAXNS];
         struct __res_params params;
         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);
 
-        for (ns = 0; ns < statp->nscount; ns++) {
+        for (int ns = 0; ns < statp->nscount; ns++) {
             if (!usable_servers[ns]) continue;
             struct sockaddr* nsap;
             int nsaplen;
@@ -556,8 +546,7 @@
 
             if (v_circuit) {
                 /* Use VC; at most one attempt per server. */
-                try
-                    = statp->retry;
+                attempt = statp->retry;
 
                 n = send_vc(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &now, &rcode,
                             &delay);
@@ -567,7 +556,7 @@
                  * queries that deterministically fail (e.g., a name that always returns
                  * SERVFAIL or times out) do not unduly affect the stats.
                  */
-                if (try == 0) {
+                if (attempt == 0) {
                     struct __res_sample sample;
                     _res_stats_set_sample(&sample, now, rcode, delay);
                     _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
@@ -591,7 +580,7 @@
                             &gotsomewhere, &now, &rcode, &delay);
 
                 /* Only record stats the first time we try a query. See above. */
-                if (try == 0) {
+                if (attempt == 0) {
                     struct __res_sample sample;
                     _res_stats_set_sample(&sample, now, rcode, delay);
                     _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
@@ -659,8 +648,8 @@
             }
             return (resplen);
         next_ns:;
-        } /*foreach ns*/
-    }     /*foreach retry*/
+        }  // for each ns
+    }  // for each retry
     res_nclose(statp);
     if (!v_circuit) {
         if (!gotsomewhere)
@@ -682,13 +671,7 @@
 
 /* Private */
 
-static int get_salen(sa) const struct sockaddr* sa;
-{
-#ifdef HAVE_SA_LEN
-    /* There are people do not set sa_len.  Be forgiving to them. */
-    if (sa->sa_len) return (sa->sa_len);
-#endif
-
+static int get_salen(const struct sockaddr* sa) {
     if (sa->sa_family == AF_INET)
         return (sizeof(struct sockaddr_in));
     else if (sa->sa_family == AF_INET6)
@@ -700,9 +683,7 @@
 /*
  * pick appropriate nsaddr_list for use.  see res_init() for initialization.
  */
-static struct sockaddr* get_nsaddr(statp, n) res_state statp;
-size_t n;
-{
+static struct sockaddr* get_nsaddr(res_state statp, size_t n) {
     if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
         /*
          * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
@@ -762,7 +743,6 @@
     struct iovec iov[2];
     u_short len;
     u_char* cp;
-    void* tmp;
 
     if (DBG) {
         async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
@@ -849,8 +829,7 @@
      */
     ns_put16((u_short) buflen, (u_char*) (void*) &len);
     iov[0] = evConsIovec(&len, INT16SZ);
-    DE_CONST(buf, tmp);
-    iov[1] = evConsIovec(tmp, (size_t) buflen);
+    iov[1] = evConsIovec((void*) buf, (size_t) buflen);
     if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
         *terrno = errno;
         Perror(statp, stderr, "write failed", errno);
diff --git a/res_state.c b/res_state.cpp
similarity index 94%
rename from res_state.c
rename to res_state.cpp
index 7c4b488..91e9044 100644
--- a/res_state.c
+++ b/res_state.cpp
@@ -56,7 +56,7 @@
 } _res_thread;
 
 static _res_thread* _res_thread_alloc(void) {
-    _res_thread* rt = calloc(1, sizeof(*rt));
+    _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
 
     if (rt) {
         rt->_h_errno = 0;
@@ -65,7 +65,7 @@
     return rt;
 }
 
-static void _res_static_done(res_static rs) {
+static void _res_static_done(struct res_static* rs) {
     /* fortunately, there is nothing to do here, since the
      * points in h_addr_ptrs and host_aliases should all
      * point to 'hostbuf'
@@ -78,7 +78,7 @@
 }
 
 static void _res_thread_free(void* _rt) {
-    _res_thread* rt = _rt;
+    _res_thread* rt = (_res_thread*) _rt;
 
     D("%s: rt=%p for thread=%d", __FUNCTION__, rt, gettid());
 
@@ -94,9 +94,7 @@
 }
 
 static _res_thread* _res_thread_get(void) {
-    _res_thread* rt;
-    rt = pthread_getspecific(_res_key);
-
+    _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key);
     if (rt != NULL) {
         return rt;
     }
@@ -161,7 +159,7 @@
     /* nothing to do */
 }
 
-res_static __res_get_static(void) {
+struct res_static* __res_get_static(void) {
     _res_thread* rt = _res_thread_get();
 
     return rt ? rt->_rstatic : NULL;
diff --git a/res_stats.c b/res_stats.cpp
similarity index 100%
rename from res_stats.c
rename to res_stats.cpp
diff --git a/resolv_static.h b/resolv_static.h
index 26863fd..8990e2d 100644
--- a/resolv_static.h
+++ b/resolv_static.h
@@ -2,6 +2,7 @@
 #define _RESOLV_STATIC_H_
 
 #include <netdb.h>
+#include <stdio.h>
 
 /* this structure contains all the variables that were declared
  * 'static' in the original NetBSD resolver code.
@@ -15,7 +16,7 @@
 #define MAXALIASES 35
 #define MAXADDRS 35
 
-typedef struct res_static {
+struct res_static {
     char* h_addr_ptrs[MAXADDRS + 1];
     char* host_aliases[MAXALIASES];
     char hostbuf[8 * 1024];
@@ -25,8 +26,8 @@
     const char* servent_ptr;
     struct servent servent;
     struct hostent host;
-} * res_static;
+};
 
-extern res_static __res_get_static(void);
+extern struct res_static* __res_get_static(void);
 
 #endif // _RESOLV_STATIC_H_
diff --git a/sethostent.c b/sethostent.cpp
similarity index 92%
rename from sethostent.c
rename to sethostent.cpp
index 8390f5c..5a7ab78 100644
--- a/sethostent.c
+++ b/sethostent.cpp
@@ -55,13 +55,13 @@
 static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*);
 
 void
-sethostent(int stayopen) {
-    res_static rs = __res_get_static();
+sethostent(int /*stayopen*/) {
+    struct res_static* rs = __res_get_static();
     if (rs) sethostent_r(&rs->hostf);
 }
 
 void endhostent(void) {
-    res_static rs = __res_get_static();
+    struct res_static* rs = __res_get_static();
     if (rs) endhostent_r(&rs->hostf);
 }
 
@@ -79,11 +79,11 @@
     }
 }
 
-int _hf_gethtbyname(void* rv, void* cb_data, va_list ap) {
+int _hf_gethtbyname(void* rv, void* /*cb_data*/, va_list ap) {
     struct hostent* hp;
     const char* name;
     int af;
-    struct getnamaddr* info = rv;
+    struct getnamaddr* info = (struct getnamaddr*) rv;
 
     _DIAGASSERT(rv != NULL);
 
@@ -120,7 +120,7 @@
         return NULL;
     }
 
-    if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
+    if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
         *info->he = NETDB_INTERNAL;
         return NULL;
     }
@@ -158,7 +158,7 @@
                 if (anum >= MAXALIASES) goto nospc;
                 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
             }
-            ptr = (void*) ALIGN(ptr);
+            ptr = (char*) ALIGN(ptr);
             if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
         }
 
@@ -201,10 +201,10 @@
     return NULL;
 }
 
-int _hf_gethtbyaddr(void* rv, void* cb_data, va_list ap) {
+int _hf_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
     struct hostent* hp;
     const unsigned char* addr;
-    struct getnamaddr* info = rv;
+    struct getnamaddr* info = (struct getnamaddr*) rv;
     FILE* hf;
 
     _DIAGASSERT(rv != NULL);