From 0d480fe6c9ba8a3cdeb081b48a37c03ee8a385bf Mon Sep 17 00:00:00 2001 From: ume Date: Tue, 26 Apr 2005 14:55:47 +0000 Subject: add IPv6 awareness for NIS query of gethostby*(). Inspired by: NetBSD --- lib/libc/net/gethostbyname.3 | 10 ------- lib/libc/net/gethostbynis.c | 71 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/lib/libc/net/gethostbyname.3 b/lib/libc/net/gethostbyname.3 index 8fb1e96..3d92af6 100644 --- a/lib/libc/net/gethostbyname.3 +++ b/lib/libc/net/gethostbyname.3 @@ -377,13 +377,3 @@ and families of functions (which should be used instead). Only the Internet address format is currently understood. -.Pp -The -.Fn gethostbyname2 -function -cannot perform -.Dv AF_INET6 -lookups over NIS. -The -.Xr getaddrinfo 3 -function must be used instead. diff --git a/lib/libc/net/gethostbynis.c b/lib/libc/net/gethostbynis.c index 88bd310..0c5b3e8 100644 --- a/lib/libc/net/gethostbynis.c +++ b/lib/libc/net/gethostbynis.c @@ -50,7 +50,7 @@ __FBSDID("$FreeBSD$"); #ifdef YP static char *host_aliases[MAXALIASES]; -static char hostaddr[MAXADDRS]; +static uint32_t host_addr[4]; /* IPv4 or IPv6 */ static char *host_addrs[2]; static struct hostent * @@ -61,19 +61,19 @@ _gethostbynis(name, map, af) { char *cp, **q; char *result; - int resultlen,size; + int resultlen, size, addrok = 0; static struct hostent h; static char *domain = (char *)NULL; static char ypbuf[YPMAXRECORD + 2]; - in_addr_t addr; switch(af) { case AF_INET: size = NS_INADDRSZ; break; - default: case AF_INET6: size = NS_IN6ADDRSZ; + break; + default: errno = EAFNOSUPPORT; h_errno = NETDB_INTERNAL; return NULL; @@ -102,11 +102,21 @@ _gethostbynis(name, map, af) cp = strpbrk(result, " \t"); *cp++ = '\0'; h.h_addr_list = host_addrs; - h.h_addr = hostaddr; - addr = inet_addr(result); - bcopy((char *)&addr, h.h_addr, size); + h.h_addr = (char *)host_addr; + switch (af) { + case AF_INET: + addrok = inet_aton(result, (struct in_addr *)host_addr); + break; + case AF_INET6: + addrok = inet_pton(af, result, host_addr); + break; + } + if (addrok != 1) { + h_errno = HOST_NOT_FOUND; + return NULL; + } h.h_length = size; - h.h_addrtype = AF_INET; + h.h_addrtype = af; while (*cp == ' ' || *cp == '\t') cp++; h.h_name = cp; @@ -128,14 +138,46 @@ _gethostbynis(name, map, af) *q = NULL; return (&h); } + +static struct hostent * +_gethostbynisname_p(const char *name, int af) +{ + char *map; + + switch (af) { + case AF_INET: + map = "hosts.byname"; + break; + default: + map = "ipnodes.byname"; + break; + } + return _gethostbynis(name, map, af); +} + +static struct hostent * +_gethostbynisaddr_p(const char *addr, int len, int af) +{ + char *map; + + switch (af) { + case AF_INET: + map = "hosts.byaddr"; + break; + default: + map = "ipnodes.byaddr"; + break; + } + return _gethostbynis(inet_ntoa(*(struct in_addr *)addr), map, af); +} #endif /* YP */ -/* XXX _gethostbynisname/_gethostbynisaddr only used by getaddrinfo */ +/* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */ struct hostent * _gethostbynisname(const char *name, int af) { #ifdef YP - return _gethostbynis(name, "hosts.byname", af); + return _gethostbynisname_p(name, af); #else return NULL; #endif @@ -145,8 +187,7 @@ struct hostent * _gethostbynisaddr(const char *addr, int len, int af) { #ifdef YP - return _gethostbynis(inet_ntoa(*(struct in_addr *)addr), - "hosts.byaddr", af); + return _gethostbynisaddr_p(addr, len, af); #else return NULL; #endif @@ -163,7 +204,7 @@ _nis_gethostbyname(void *rval, void *cb_data, va_list ap) name = va_arg(ap, const char *); af = va_arg(ap, int); - *(struct hostent **)rval = _gethostbynis(name, "hosts.byname", af); + *(struct hostent **)rval = _gethostbynisname_p(name, af); return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; #else return NS_UNAVAIL; @@ -181,8 +222,8 @@ _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap) addr = va_arg(ap, const char *); len = va_arg(ap, int); af = va_arg(ap, int); - - *(struct hostent **)rval =_gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af); + + *(struct hostent **)rval =_gethostbynisaddr_p(addr, len, af); return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; #else return NS_UNAVAIL; -- cgit v1.1