am 370a3e57: Merge "Sync with upstream for gethnamaddr.c."

* commit '370a3e573d1cdff6070c77322d4a07bd53e5db3c':
  Sync with upstream for gethnamaddr.c.
This commit is contained in:
Yabin Cui 2014-12-18 01:11:56 +00:00 committed by Android Git Automerger
commit 9a73413813
5 changed files with 1262 additions and 558 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,93 @@
/* $NetBSD: hostent.h,v 1.2 2013/08/27 09:56:12 christos Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _DNS_NET_HOSTENT_H
#define _DNS_NET_HOSTENT_H
#include <stdio.h>
#include <netdb.h>
#include <stdarg.h>
/*
* These are not being advertised because the interfaces are non-standard.
* There are versions by linux, aix, qnx, sun, etc. Our versions are used
* internally to provide thread safety; they mostly resemble qnx.
*/
void sethostent_r(FILE **);
struct hostent *netbsd_gethostent_r(FILE *, struct hostent *, char *, size_t, int *);
void endhostent_r(FILE **);
/*
* The following are internal API's and are used only for testing.
*/
struct getnamaddr {
struct hostent *hp;
char *buf;
size_t buflen;
int *he;
};
/* /etc/hosts lookup */
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 { \
size_t _len = (anum + 1) * sizeof(*dst); \
if (_len > len) \
goto nospc; \
dst = (void *)ptr; \
ptr += _len; \
len -= _len; \
} 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 (/* CONSTCOND */0)
#define HENT_SCOPY(dst, src, ptr, len) \
do { \
size_t _len = strlen(src) + 1; \
HENT_COPY(dst, src, _len, ptr, len); \
} while (/* CONSTCOND */0)
#endif /* _DNS_NET_HOSTENT_H */

266
libc/dns/net/sethostent.c Normal file
View file

@ -0,0 +1,266 @@
/* $NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $ */
/*
* Copyright (c) 1985, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* 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 "namespace.h"
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>
#include <assert.h>
#include <string.h>
#include <nsswitch.h>
#include <netdb.h>
#include <resolv.h>
#include <errno.h>
#include <stdlib.h>
#include "hostent.h"
#include "resolv_private.h"
#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 *);
static const char *_h_hosts = _PATH_HOSTS;
void
sethostent_r(FILE **hf)
{
if (!*hf)
*hf = fopen(_h_hosts, "re");
else
rewind(*hf);
}
void
endhostent_r(FILE **hf)
{
if (*hf) {
(void)fclose(*hf);
*hf = NULL;
}
}
/*ARGSUSED*/
int
_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
{
struct hostent *hp;
const char *name;
int af;
struct getnamaddr *info = rv;
_DIAGASSERT(rv != NULL);
name = va_arg(ap, char *);
/* 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) {
*info->he = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
static struct hostent *
_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
{
struct hostent *hp, hent;
char *buf, *ptr;
size_t len, anum, num, i;
FILE *hf;
char *aliases[MAXALIASES];
char *addr_ptrs[MAXADDRS];
_DIAGASSERT(name != NULL);
hf = NULL;
sethostent_r(&hf);
if (hf == NULL) {
errno = EINVAL;
*info->he = NETDB_INTERNAL;
return NULL;
}
if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
endhostent_r(&hf);
*info->he = NETDB_INTERNAL;
return NULL;
}
anum = 0; /* XXX: gcc */
hent.h_name = NULL; /* XXX: gcc */
hent.h_addrtype = 0; /* XXX: gcc */
hent.h_length = 0; /* XXX: gcc */
for (num = 0; num < MAXADDRS;) {
info->hp->h_addrtype = af;
info->hp->h_length = 0;
hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
info->he);
if (hp == NULL)
break;
if (strcasecmp(hp->h_name, name) != 0) {
char **cp;
for (cp = hp->h_aliases; *cp != NULL; cp++)
if (strcasecmp(*cp, name) == 0)
break;
if (*cp == NULL) continue;
}
if (num == 0) {
hent.h_addrtype = af = hp->h_addrtype;
hent.h_length = hp->h_length;
HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
for (anum = 0; hp->h_aliases[anum]; anum++) {
if (anum >= MAXALIASES)
goto nospc;
HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
ptr, len);
}
ptr = (void *)ALIGN(ptr);
if ((size_t)(ptr - buf) >= info->buflen)
goto nospc;
}
if (num >= MAXADDRS)
goto nospc;
HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
len);
num++;
}
endhostent_r(&hf);
if (num == 0) {
*info->he = HOST_NOT_FOUND;
free(buf);
return NULL;
}
hp = info->hp;
ptr = info->buf;
len = info->buflen;
hp->h_addrtype = hent.h_addrtype;
hp->h_length = hent.h_length;
HENT_ARRAY(hp->h_aliases, anum, ptr, len);
HENT_ARRAY(hp->h_addr_list, num, ptr, len);
for (i = 0; i < num; i++)
HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
len);
hp->h_addr_list[num] = NULL;
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);
hp->h_aliases[anum] = NULL;
free(buf);
return hp;
nospc:
endhostent_r(&hf);
*info->he = NETDB_INTERNAL;
free(buf);
errno = ENOSPC;
return NULL;
}
/*ARGSUSED*/
int
_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
{
struct hostent *hp;
const unsigned char *addr;
struct getnamaddr *info = rv;
FILE *hf;
_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;
sethostent_r(&hf);
if (hf == NULL) {
*info->he = NETDB_INTERNAL;
return NS_UNAVAIL;
}
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;
endhostent_r(&hf);
if (hp == NULL) {
*info->he = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}

View file

@ -209,7 +209,7 @@ void endprotoent(void);
void endservent(void);
void freehostent(struct hostent *);
struct hostent *gethostbyaddr(const void *, socklen_t, int);
int gethostbyaddr_r(const void *, int, int, struct hostent *, char *, size_t, struct hostent **, int *);
int gethostbyaddr_r(const void *, socklen_t, int, struct hostent *, char *, size_t, struct hostent **, int *);
struct hostent *gethostbyname(const char *);
int gethostbyname_r(const char *, struct hostent *, char *, size_t, struct hostent **, int *);
struct hostent *gethostbyname2(const char *, int);

View file

@ -14,11 +14,13 @@
* limitations under the License.
*/
#include <netdb.h>
#include <gtest/gtest.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
TEST(netdb, getaddrinfo_NULL_host) {
@ -114,8 +116,7 @@ TEST(netdb, getnameinfo_salen) {
ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
}
TEST(netdb, gethostbyname) {
hostent* hent = gethostbyname("localhost");
void VerifyLocalhost(hostent *hent) {
ASSERT_TRUE(hent != NULL);
ASSERT_EQ(hent->h_addrtype, AF_INET);
ASSERT_EQ(hent->h_addr[0], 127);
@ -124,6 +125,125 @@ TEST(netdb, gethostbyname) {
ASSERT_EQ(hent->h_addr[3], 1);
}
TEST(netdb, gethostbyname) {
hostent* hp = gethostbyname("localhost");
VerifyLocalhost(hp);
}
TEST(netdb, gethostbyname2) {
hostent* hp = gethostbyname2("localhost", AF_INET);
VerifyLocalhost(hp);
}
TEST(netdb, gethostbyname_r) {
hostent hent;
hostent *hp;
char buf[512];
int err;
int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
ASSERT_EQ(0, result);
VerifyLocalhost(hp);
// Change hp->h_addr to test reentrancy.
hp->h_addr[0] = 0;
hostent hent2;
hostent *hp2;
char buf2[512];
result = gethostbyname_r("localhost", &hent2, buf2, sizeof(buf2), &hp2, &err);
ASSERT_EQ(0, result);
VerifyLocalhost(hp2);
ASSERT_EQ(0, hp->h_addr[0]);
}
TEST(netdb, gethostbyname2_r) {
hostent hent;
hostent *hp;
char buf[512];
int err;
int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
ASSERT_EQ(0, result);
VerifyLocalhost(hp);
// Change hp->h_addr to test reentrancy.
hp->h_addr[0] = 0;
hostent hent2;
hostent *hp2;
char buf2[512];
result = gethostbyname2_r("localhost", AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
ASSERT_EQ(0, result);
VerifyLocalhost(hp2);
ASSERT_EQ(0, hp->h_addr[0]);
}
TEST(netdb, gethostbyaddr) {
char addr[4];
ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr));
hostent *hp = gethostbyaddr(addr, sizeof(addr), AF_INET);
VerifyLocalhost(hp);
}
TEST(netdb, gethostbyaddr_r) {
char addr[4];
ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr));
hostent hent;
hostent *hp;
char buf[512];
int err;
int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
ASSERT_EQ(0, result);
VerifyLocalhost(hp);
// Change hp->h_addr to test reentrancy.
hp->h_addr[0] = 0;
hostent hent2;
hostent *hp2;
char buf2[512];
result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent2, buf2, sizeof(buf2), &hp2, &err);
ASSERT_EQ(0, result);
VerifyLocalhost(hp2);
ASSERT_EQ(0, hp->h_addr[0]);
}
TEST(netdb, gethostbyname_r_ERANGE) {
hostent hent;
hostent *hp;
char buf[4]; // Use too small buffer.
int err;
int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
ASSERT_EQ(ERANGE, result);
ASSERT_EQ(NULL, hp);
}
TEST(netdb, gethostbyname2_r_ERANGE) {
hostent hent;
hostent *hp;
char buf[4]; // Use too small buffer.
int err;
int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
ASSERT_EQ(ERANGE, result);
ASSERT_EQ(NULL, hp);
}
TEST(netdb, gethostbyaddr_r_ERANGE) {
char addr[4];
ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", addr));
hostent hent;
hostent *hp;
char buf[4]; // Use too small buffer.
int err;
int result = gethostbyaddr_r(addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
ASSERT_EQ(ERANGE, result);
ASSERT_EQ(NULL, hp);
}
TEST(netdb, getservbyname) {
// smtp is TCP-only, so we know we'll get 25/tcp back.
servent* s = getservbyname("smtp", NULL);