Complete <netdb.h>.
Add all the missing <netdb.h> functions.
Also fix getservbyport to handle a null protocol correctly.
Also fix getservbyname/getservbyport to not interfere with getservent.
Also fix endservent to reset getservent iteration.
Also reduce unnecessary differences from upstream NetBSD sethostent.c.
The servent implementation is still horrific, and we should
probably support protoent too so that debugging tools can use
getprotobyname/getprotobynumber.
Bug: N/A
Test: ran tests
Change-Id: I639108c46df0a768af297cf3bbce857cb1bef9d9
diff --git a/libc/dns/net/sethostent.c b/libc/dns/net/sethostent.c
index 916421e..66c305f 100644
--- a/libc/dns/net/sethostent.c
+++ b/libc/dns/net/sethostent.c
@@ -64,13 +64,26 @@
static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
-static const char *_h_hosts = _PATH_HOSTS;
+void
+/*ARGSUSED*/
+sethostent(int stayopen)
+{
+ res_static rs = __res_get_static();
+ if (rs) sethostent_r(&rs->hostf);
+}
+
+void
+endhostent(void)
+{
+ res_static rs = __res_get_static();
+ if (rs) endhostent_r(&rs->hostf);
+}
void
sethostent_r(FILE **hf)
{
if (!*hf)
- *hf = fopen(_h_hosts, "re");
+ *hf = fopen(_PATH_HOSTS, "re");
else
rewind(*hf);
}
@@ -116,15 +129,13 @@
hp = _hf_gethtbyname2(name, af, info);
#endif
if (hp == NULL) {
- if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
- return NS_UNAVAIL;
- }
+ *info->he = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
-static struct hostent *
+struct hostent *
_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
{
struct hostent *hp, hent;
@@ -145,7 +156,6 @@
}
if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
- endhostent_r(&hf);
*info->he = NETDB_INTERNAL;
return NULL;
}
@@ -161,12 +171,8 @@
hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
info->he);
- if (hp == NULL) {
- if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
- goto nospc;
- }
+ if (hp == NULL)
break;
- }
if (strcasecmp(hp->h_name, name) != 0) {
char **cp;
@@ -230,7 +236,6 @@
free(buf);
return hp;
nospc:
- endhostent_r(&hf);
*info->he = NETDB_INTERNAL;
free(buf);
errno = ENOSPC;
@@ -265,9 +270,6 @@
endhostent_r(&hf);
if (hp == NULL) {
- if (errno == ENOSPC) {
- return NS_UNAVAIL;
- }
*info->he = HOST_NOT_FOUND;
return NS_NOTFOUND;
}