nsdispatch removal from gethostbyaddr()
Test: atest netd_integration_test
Change-Id: I41d1de68296e6201e6b0f20d390eaf1d12aa49f9
diff --git a/resolv/gethnamaddr.cpp b/resolv/gethnamaddr.cpp
index 338f2af..34df84d 100644
--- a/resolv/gethnamaddr.cpp
+++ b/resolv/gethnamaddr.cpp
@@ -89,7 +89,6 @@
#include <stdlib.h>
#include <string.h>
-#include "nsswitch.h"
#include "hostent.h"
@@ -140,8 +139,9 @@
struct hostent* ht_gethostbyname(char*);
struct hostent* ht_gethostbyaddr(const char*, int, int);
-static int _dns_gethtbyaddr(void*, void*, va_list);
-static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info);
+static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
+ const android_net_context* netcontext, getnamaddr* info);
+static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
size_t, int*, const struct android_net_context*);
@@ -149,12 +149,6 @@
const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
const struct android_net_context*);
-static const ns_src default_dns_files[] = {
- {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).
if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
@@ -621,7 +615,7 @@
info.buflen = buflen;
info.he = he;
if (!_hf_gethtbyname2(name, af, &info)) {
- if (_dns_gethtbyname(name, af, &info) != NS_SUCCESS) {
+ if (!_dns_gethtbyname(name, af, &info)) {
return NULL;
}
}
@@ -675,11 +669,6 @@
const u_char* uaddr = (const u_char*) addr;
socklen_t size;
struct getnamaddr info;
- static const ns_dtab dtab[] = {
- {NSSRC_FILES, _hf_gethtbyaddr, NULL},
- {NSSRC_DNS, _dns_gethtbyaddr, NULL},
- {0, 0, 0}
- };
_DIAGASSERT(addr != NULL);
@@ -720,9 +709,11 @@
info.buflen = buflen;
info.he = he;
*he = NETDB_INTERNAL;
- if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr", default_dns_files, uaddr, len, af,
- netcontext) != NS_SUCCESS)
- return NULL;
+ if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
+ if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
+ return NULL;
+ }
+ }
*he = NETDB_SUCCESS;
return hp;
}
@@ -918,7 +909,7 @@
}
}
-static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
+static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
int n, type;
struct hostent* hp;
res_state res;
@@ -935,55 +926,44 @@
type = T_AAAA;
break;
default:
- return NS_UNAVAIL;
+ return false;
}
querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
if (buf == NULL) {
*info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
+ return false;
}
res = __res_get_state();
if (res == NULL) {
free(buf);
- return NS_NOTFOUND;
+ return false;
}
n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
if (n < 0) {
free(buf);
debugprintf("res_nsearch failed (%d)\n", res, n);
__res_put_state(res);
- return NS_NOTFOUND;
+ return false;
}
hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
free(buf);
__res_put_state(res);
- if (hp == NULL) switch (*info->he) {
- case HOST_NOT_FOUND:
- return NS_NOTFOUND;
- case TRY_AGAIN:
- return NS_TRYAGAIN;
- default:
- return NS_UNAVAIL;
- }
- return NS_SUCCESS;
+ if (hp == NULL) {
+ return false;
+ }
+ return true;
}
-static int _dns_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
+static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
+ const android_net_context* netcontext, getnamaddr* info) {
char qbuf[MAXDNAME + 1], *qp, *ep;
int n;
struct hostent* hp;
- const unsigned char* uaddr;
int advance;
res_state res;
- struct getnamaddr* info = (struct getnamaddr*) rv;
- const struct android_net_context* netcontext;
- _DIAGASSERT(rv != NULL);
-
- uaddr = va_arg(ap, unsigned char*);
- info->hp->h_length = va_arg(ap, int);
- info->hp->h_addrtype = va_arg(ap, int);
- netcontext = va_arg(ap, const struct android_net_context*);
+ info->hp->h_length = len;
+ info->hp->h_addrtype = af;
switch (info->hp->h_addrtype) {
case AF_INET:
@@ -1001,27 +981,27 @@
qp += advance;
else {
*info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
+ return false;
}
}
if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
*info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
+ return false;
}
break;
default:
- return NS_UNAVAIL;
+ return false;
}
querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
if (buf == NULL) {
*info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
+ return false;
}
res = __res_get_state();
if (res == NULL) {
free(buf);
- return NS_NOTFOUND;
+ return false;
}
res_setnetcontext(res, netcontext);
n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
@@ -1029,20 +1009,13 @@
free(buf);
debugprintf("res_nquery failed (%d)\n", res, n);
__res_put_state(res);
- return NS_NOTFOUND;
+ return false;
}
hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
free(buf);
if (hp == NULL) {
__res_put_state(res);
- switch (*info->he) {
- case HOST_NOT_FOUND:
- return NS_NOTFOUND;
- case TRY_AGAIN:
- return NS_TRYAGAIN;
- default:
- return NS_UNAVAIL;
- }
+ return false;
}
char* bf = (char*) (hp->h_addr_list + 2);
@@ -1060,12 +1033,12 @@
__res_put_state(res);
*info->he = NETDB_SUCCESS;
- return NS_SUCCESS;
+ return true;
nospc:
errno = ENOSPC;
*info->he = NETDB_INTERNAL;
- return NS_UNAVAIL;
+ return false;
}
/*
diff --git a/resolv/hostent.h b/resolv/hostent.h
index f06e104..ae9ed6d 100644
--- a/resolv/hostent.h
+++ b/resolv/hostent.h
@@ -35,11 +35,6 @@
#include <stdarg.h>
#include <stdio.h>
-struct hostent* netbsd_gethostent_r(FILE*, struct hostent*, char*, size_t, int*);
-
-/*
- * The following are internal API's and are used only for testing.
- */
struct getnamaddr {
struct hostent* hp;
char* buf;
@@ -47,9 +42,10 @@
int* he;
};
-/* /etc/hosts lookup */
-int _hf_gethtbyaddr(void*, void*, va_list);
+// /etc/hosts lookup
+bool _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info);
hostent* _hf_gethtbyname2(const char* name, int af, getnamaddr* info);
+hostent* netbsd_gethostent_r(FILE*, struct hostent*, char*, size_t, int*);
#define HENT_ARRAY(dst, anum, ptr, len) do { \
size_t _len = (anum + 1) * sizeof(*dst); \
diff --git a/resolv/sethostent.cpp b/resolv/sethostent.cpp
index 2a4248f..4365c03 100644
--- a/resolv/sethostent.cpp
+++ b/resolv/sethostent.cpp
@@ -35,7 +35,6 @@
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
-#include <nsswitch.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
@@ -45,11 +44,6 @@
#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)
@@ -70,14 +64,11 @@
hostent* _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
struct hostent *hp, hent;
char *buf, *ptr;
- size_t len, anum, num, i;
- FILE* hf;
+ size_t len, num, i;
char* aliases[MAXALIASES];
char* addr_ptrs[MAXADDRS];
- _DIAGASSERT(name != NULL);
-
- hf = NULL;
+ FILE* hf = NULL;
sethostent_r(&hf);
if (hf == NULL) {
errno = EINVAL;
@@ -90,12 +81,12 @@
return NULL;
}
- anum = 0; /* XXX: gcc */
- hent.h_name = NULL; /* XXX: gcc */
- hent.h_addrtype = 0; /* XXX: gcc */
- hent.h_length = 0; /* XXX: gcc */
+ hent.h_name = NULL;
+ hent.h_addrtype = 0;
+ hent.h_length = 0;
- for (num = 0; num < MAXADDRS;) {
+ size_t anum = 0;
+ for (num = 0; num < MAXADDRS; /**/) {
info->hp->h_addrtype = af;
info->hp->h_length = 0;
@@ -111,6 +102,7 @@
char** cp;
for (cp = hp->h_aliases; *cp != NULL; cp++)
if (strcasecmp(*cp, name) == 0) break;
+ // NOTE: does not increment num
if (*cp == NULL) continue;
}
@@ -129,6 +121,7 @@
if (num >= MAXADDRS) goto nospc;
HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
+
num++;
}
endhostent_r(&hf);
@@ -154,7 +147,9 @@
HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
- for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
+ for (i = 0; i < anum; i++) {
+ HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
+ }
hp->h_aliases[anum] = NULL;
free(buf);
@@ -166,32 +161,25 @@
return NULL;
}
-int _hf_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
- struct hostent* hp;
- const unsigned char* addr;
- struct getnamaddr* info = (struct getnamaddr*) rv;
- FILE* hf;
+bool _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) {
+ info->hp->h_length = len;
+ info->hp->h_addrtype = af;
- _DIAGASSERT(rv != NULL);
-
- addr = va_arg(ap, unsigned char*);
- info->hp->h_length = va_arg(ap, int);
- info->hp->h_addrtype = va_arg(ap, int);
-
- hf = NULL;
+ FILE* hf = NULL;
sethostent_r(&hf);
if (hf == NULL) {
*info->he = NETDB_INTERNAL;
- return NS_UNAVAIL;
+ return false;
}
+ struct hostent* hp;
while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL)
- if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break;
+ if (!memcmp(hp->h_addr_list[0], uaddr, (size_t) hp->h_length)) break;
endhostent_r(&hf);
if (hp == NULL) {
- if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
+ if (errno == ENOSPC) return false; // glibc compatibility.
*info->he = HOST_NOT_FOUND;
- return NS_NOTFOUND;
+ return false;
}
- return NS_SUCCESS;
+ return true;
}