Remap existing EAI_SYSTEM to avoid relying on global errno variable

In order to not rely on errno, remap the existing error code pair of EAI_SYSTEM
and errno to other EAI_* error codes. Unused errno is removed as well.

Test: built, flashed, booted
      system/netd/tests/runtests.sh pass

Change-Id: I8a4450df3c27a12db790756ba545787bd0570402
diff --git a/sethostent.cpp b/sethostent.cpp
index 49cc236..2885fbc 100644
--- a/sethostent.cpp
+++ b/sethostent.cpp
@@ -61,6 +61,8 @@
     }
 }
 
+// TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
+// does not currently return to netd.
 int _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
     struct hostent *hp, hent;
     char *buf, *ptr;
@@ -71,9 +73,11 @@
     FILE* hf = NULL;
     sethostent_r(&hf);
     if (hf == NULL) {
-        errno = EINVAL;
-        // TODO: Consider to remap error code without relying on errno.
-        return EAI_SYSTEM;
+        // TODO: Consider converting to a private extended EAI_* error code.
+        // Currently, the EAI_* value has no corresponding error code for invalid argument socket
+        // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
+        // and EINVAL, to EAI_FAIL.
+        return EAI_FAIL;
     }
 
     if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
@@ -89,10 +93,10 @@
         info->hp->h_addrtype = af;
         info->hp->h_length = 0;
 
-        int herrno;
-        hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &herrno);
+        int he;
+        hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he);
         if (hp == NULL) {
-            if (herrno == NETDB_INTERNAL && errno == ENOSPC) {
+            if (he == NETDB_INTERNAL && errno == ENOSPC) {
                 goto nospc;  // glibc compatibility.
             }
             break;
@@ -165,10 +169,11 @@
     return 0;
 nospc:
     free(buf);
-    errno = ENOSPC;
     return EAI_MEMORY;
 }
 
+// TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
+// does not currently return to netd.
 int _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) {
     info->hp->h_length = len;
     info->hp->h_addrtype = af;
@@ -176,12 +181,15 @@
     FILE* hf = NULL;
     sethostent_r(&hf);
     if (hf == NULL) {
-        // TODO: Consider to remap error code without relying on errno.
-        return EAI_SYSTEM;
+        // TODO: Consider converting to a private extended EAI_* error code.
+        // Currently, the EAI_* value has no corresponding error code for invalid argument socket
+        // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
+        // and EINVAL, to EAI_FAIL.
+        return EAI_FAIL;
     }
     struct hostent* hp;
-    int herrno;
-    while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &herrno)) != NULL)
+    int he;
+    while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he)) != NULL)
         if (!memcmp(hp->h_addr_list[0], uaddr, (size_t) hp->h_length)) break;
     endhostent_r(&hf);