xref: /illumos-gate/usr/src/lib/libnsl/nss/getipnodeby.c (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55b79143cSGirish Moodalbail  * Common Development and Distribution License (the "License").
65b79143cSGirish Moodalbail  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23634e26ecSCasper H.S. Dik  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
2696088c83SRobert Mustacchi  * Copyright 2016 Joyent, Inc.
2796088c83SRobert Mustacchi  *
287c478bd9Sstevel@tonic-gate  * This file defines and implements the re-entrant getipnodebyname(),
297c478bd9Sstevel@tonic-gate  * getipnodebyaddr(), and freehostent() routines for IPv6. These routines
307c478bd9Sstevel@tonic-gate  * follow use the netdir_getbyYY() (see netdir_inet.c).
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * lib/libnsl/nss/getipnodeby.c
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "mt.h"
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <stropts.h>
397c478bd9Sstevel@tonic-gate #include <ctype.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <strings.h>
427c478bd9Sstevel@tonic-gate #include <netdb.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
457c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
467c478bd9Sstevel@tonic-gate #include <netinet/in.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
497c478bd9Sstevel@tonic-gate #include <nss_netdir.h>
507c478bd9Sstevel@tonic-gate #include <net/if.h>
517c478bd9Sstevel@tonic-gate #include <netinet/in.h>
527c478bd9Sstevel@tonic-gate #include <netdir.h>
537c478bd9Sstevel@tonic-gate #include <thread.h>
547c478bd9Sstevel@tonic-gate #include <synch.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <sys/time.h>
577c478bd9Sstevel@tonic-gate #include "nss.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	IPV6_LITERAL_CHAR	':'
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * The number of nanoseconds getipnodebyname() waits before getting
637c478bd9Sstevel@tonic-gate  * fresh interface count information with SIOCGLIFNUM.  The default is
647c478bd9Sstevel@tonic-gate  * five minutes.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate #define	IFNUM_TIMEOUT	((hrtime_t)300 * NANOSEC)
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Bits in the bitfield returned by getipnodebyname_processflags().
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * IPNODE_WANTIPV6	The user wants IPv6 addresses returned.
727c478bd9Sstevel@tonic-gate  * IPNODE_WANTIPV4	The user wants IPv4 addresses returned.
737c478bd9Sstevel@tonic-gate  * IPNODE_IPV4IFNOIPV6	The user only wants IPv4 addresses returned if no IPv6
747c478bd9Sstevel@tonic-gate  *			addresses are returned.
757c478bd9Sstevel@tonic-gate  * IPNODE_LOOKUPIPNODES	getipnodebyname() needs to lookup the name in ipnodes.
767c478bd9Sstevel@tonic-gate  * IPNODE_LOOKUPHOSTS	getipnodebyname() needs to lookup the name in hosts.
777c478bd9Sstevel@tonic-gate  * IPNODE_ISLITERAL	The name supplied is a literal address string.
782f443e27SRobert Mustacchi  * IPNODE_UNMAP		The user doesn't want v4 mapped addresses if no IPv6
792f443e27SRobert Mustacchi  * 			interfaces are plumbed on the system.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate #define	IPNODE_WANTIPV6		0x00000001u
827c478bd9Sstevel@tonic-gate #define	IPNODE_WANTIPV4		0x00000002u
837c478bd9Sstevel@tonic-gate #define	IPNODE_IPV4IFNOIPV6	0x00000004u
847c478bd9Sstevel@tonic-gate #define	IPNODE_LOOKUPIPNODES	0x00000008u
857c478bd9Sstevel@tonic-gate #define	IPNODE_LOOKUPHOSTS	0x00000010u
867c478bd9Sstevel@tonic-gate #define	IPNODE_LITERAL		0x00000020u
872f443e27SRobert Mustacchi #define	IPNODE_UNMAP		0x00000040u
887c478bd9Sstevel@tonic-gate #define	IPNODE_IPV4		(IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
912f443e27SRobert Mustacchi  * The private flag between libsocket and libnsl. See
922f443e27SRobert Mustacchi  * lib/libsocket/inet/getaddrinfo.c for more information.
932f443e27SRobert Mustacchi  */
942f443e27SRobert Mustacchi #define	AI_ADDRINFO	0x8000
952f443e27SRobert Mustacchi 
962f443e27SRobert Mustacchi /*
977c478bd9Sstevel@tonic-gate  * The default set of bits corresponding to a getipnodebyname() flags
987c478bd9Sstevel@tonic-gate  * argument of AI_DEFAULT.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate #define	IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \
1017c478bd9Sstevel@tonic-gate 	IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS)
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip(char *);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static struct hostent *__mapv4tov6(struct hostent *, struct hostent *,
1067c478bd9Sstevel@tonic-gate     nss_XbyY_buf_t *, int);
1077c478bd9Sstevel@tonic-gate struct hostent *__mappedtov4(struct hostent *, int *);
1087c478bd9Sstevel@tonic-gate static struct hostent *__filter_addresses(int, struct hostent *);
1097c478bd9Sstevel@tonic-gate static int __find_mapped(struct hostent *, int);
1107c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *__IPv6_alloc(int);
1117c478bd9Sstevel@tonic-gate static void __IPv6_cleanup(nss_XbyY_buf_t *);
11296088c83SRobert Mustacchi static int __ai_addrconfig(int, boolean_t);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #ifdef PIC
1167c478bd9Sstevel@tonic-gate struct hostent *
_uncached_getipnodebyname(const char * nam,struct hostent * result,char * buffer,int buflen,int af_family,int flags,int * h_errnop)1177c478bd9Sstevel@tonic-gate _uncached_getipnodebyname(const char *nam, struct hostent *result,
1187c478bd9Sstevel@tonic-gate 	char *buffer, int buflen, int af_family, int flags, int *h_errnop)
1197c478bd9Sstevel@tonic-gate {
12061961e0fSrobinson 	return (_switch_getipnodebyname_r(nam, result, buffer, buflen,
12161961e0fSrobinson 	    af_family, flags, h_errnop));
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate struct hostent *
_uncached_getipnodebyaddr(const char * addr,int length,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)1257c478bd9Sstevel@tonic-gate _uncached_getipnodebyaddr(const char *addr, int length, int type,
1267c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	if (type == AF_INET)
1297c478bd9Sstevel@tonic-gate 		return (_switch_gethostbyaddr_r(addr, length, type,
1307c478bd9Sstevel@tonic-gate 		    result, buffer, buflen, h_errnop));
1317c478bd9Sstevel@tonic-gate 	else if (type == AF_INET6)
1327c478bd9Sstevel@tonic-gate 		return (_switch_getipnodebyaddr_r(addr, length, type,
1337c478bd9Sstevel@tonic-gate 		    result, buffer, buflen, h_errnop));
1347c478bd9Sstevel@tonic-gate 	return (NULL);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Given a name, an address family, and a set of flags, return a
1407c478bd9Sstevel@tonic-gate  * bitfield that getipnodebyname() will use.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate static uint_t
getipnodebyname_processflags(const char * name,int af,int flags)1437c478bd9Sstevel@tonic-gate getipnodebyname_processflags(const char *name, int af, int flags)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	uint_t		ipnode_bits = IPNODE_DEFAULT;
1467c478bd9Sstevel@tonic-gate 	boolean_t	ipv6configured = B_FALSE;
1477c478bd9Sstevel@tonic-gate 	boolean_t	ipv4configured = B_FALSE;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
15096088c83SRobert Mustacchi 	 * If AI_ADDRCONFIG is specified, we need to determine the number of
15196088c83SRobert Mustacchi 	 * addresses of each address family configured on the system as
1527c478bd9Sstevel@tonic-gate 	 * appropriate.
15396088c83SRobert Mustacchi 	 *
15496088c83SRobert Mustacchi 	 * When trying to determine which addresses should be used for
15596088c83SRobert Mustacchi 	 * addrconfig, we first ignore loopback devices. This generally makes
15696088c83SRobert Mustacchi 	 * sense as policy, as most of these queries will be trying to go
15796088c83SRobert Mustacchi 	 * off-box and one should not have an IPv6 loopback address suggest that
15896088c83SRobert Mustacchi 	 * we can now send IPv6 traffic off the box or the equivalent with IPv4.
15996088c83SRobert Mustacchi 	 * However, it's possible that no non-loopback interfaces are up on the
16096088c83SRobert Mustacchi 	 * box. In those cases, we then check which interfaces are up and
16196088c83SRobert Mustacchi 	 * consider loopback devices. While this isn't to the letter of RFC 3493
16296088c83SRobert Mustacchi 	 * (which itself is a bit vague in this case, as is SUS), it matches
16396088c83SRobert Mustacchi 	 * expected user behavior in these situations.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	if (flags & AI_ADDRCONFIG) {
16696088c83SRobert Mustacchi 		boolean_t hv4, hv6;
16796088c83SRobert Mustacchi 
16896088c83SRobert Mustacchi 		hv4 = __ai_addrconfig(AF_INET, B_FALSE) > 0;
16996088c83SRobert Mustacchi 		hv6 = __ai_addrconfig(AF_INET6, B_FALSE) > 0;
17096088c83SRobert Mustacchi 
17196088c83SRobert Mustacchi 		if (hv4 == B_FALSE && hv6 == B_FALSE) {
17296088c83SRobert Mustacchi 			hv4 = __ai_addrconfig(AF_INET, B_TRUE) > 0;
17396088c83SRobert Mustacchi 			hv6 = __ai_addrconfig(AF_INET6, B_TRUE) > 0;
17496088c83SRobert Mustacchi 		}
17596088c83SRobert Mustacchi 
17696088c83SRobert Mustacchi 		ipv6configured = (af == AF_INET6 && hv6);
17796088c83SRobert Mustacchi 		ipv4configured = (af == AF_INET || (flags & AI_V4MAPPED)) &&
17896088c83SRobert Mustacchi 		    hv4;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Determine what kinds of addresses the user is interested
1837c478bd9Sstevel@tonic-gate 	 * in getting back.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	switch (af) {
1867c478bd9Sstevel@tonic-gate 	case AF_INET6:
1877c478bd9Sstevel@tonic-gate 		if ((flags & AI_ADDRCONFIG) && !ipv6configured)
1887c478bd9Sstevel@tonic-gate 			ipnode_bits &= ~IPNODE_WANTIPV6;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		if (flags & AI_V4MAPPED) {
1917c478bd9Sstevel@tonic-gate 			if ((flags & AI_ADDRCONFIG) && !ipv4configured) {
1927c478bd9Sstevel@tonic-gate 				ipnode_bits &= ~IPNODE_IPV4;
1937c478bd9Sstevel@tonic-gate 			} else if (flags & AI_ALL) {
1947c478bd9Sstevel@tonic-gate 				ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
1957c478bd9Sstevel@tonic-gate 			}
1962f443e27SRobert Mustacchi 			if ((flags & AI_ADDRCONFIG) && !ipv6configured &&
1972f443e27SRobert Mustacchi 			    (flags & AI_ADDRINFO)) {
1982f443e27SRobert Mustacchi 				ipnode_bits |= IPNODE_UNMAP;
1992f443e27SRobert Mustacchi 			}
2007c478bd9Sstevel@tonic-gate 		} else {
2017c478bd9Sstevel@tonic-gate 			ipnode_bits &= ~IPNODE_IPV4;
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		break;
2047c478bd9Sstevel@tonic-gate 	case AF_INET:
2057c478bd9Sstevel@tonic-gate 		if ((flags & AI_ADDRCONFIG) && !ipv4configured)
2067c478bd9Sstevel@tonic-gate 			ipnode_bits &= ~IPNODE_IPV4;
2077c478bd9Sstevel@tonic-gate 		ipnode_bits &= ~IPNODE_WANTIPV6;
2087c478bd9Sstevel@tonic-gate 		ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
2097c478bd9Sstevel@tonic-gate 		break;
2107c478bd9Sstevel@tonic-gate 	default:
2117c478bd9Sstevel@tonic-gate 		ipnode_bits = 0;
2127c478bd9Sstevel@tonic-gate 		break;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * If we're not looking for IPv4 addresses, don't bother looking
2177c478bd9Sstevel@tonic-gate 	 * in hosts.
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	if (!(ipnode_bits & IPNODE_WANTIPV4))
2207c478bd9Sstevel@tonic-gate 		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Determine if name is a literal IP address.  This will
2247c478bd9Sstevel@tonic-gate 	 * further narrow down what type of lookup we're going to do.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	if (strchr(name, IPV6_LITERAL_CHAR) != NULL) {
2277c478bd9Sstevel@tonic-gate 		/* Literal IPv6 address */
2287c478bd9Sstevel@tonic-gate 		ipnode_bits |= IPNODE_LITERAL;
2297c478bd9Sstevel@tonic-gate 		/*
2307c478bd9Sstevel@tonic-gate 		 * In s9 we accepted the literal without filtering independent
2317c478bd9Sstevel@tonic-gate 		 * of what family was passed in hints.  We continue to do
2327c478bd9Sstevel@tonic-gate 		 * this.
2337c478bd9Sstevel@tonic-gate 		 */
2347c478bd9Sstevel@tonic-gate 		ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4);
2357c478bd9Sstevel@tonic-gate 		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
23661961e0fSrobinson 	} else if (inet_addr(name) != 0xffffffffU) {
2377c478bd9Sstevel@tonic-gate 		/* Literal IPv4 address */
2387c478bd9Sstevel@tonic-gate 		ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4);
2397c478bd9Sstevel@tonic-gate 		ipnode_bits &= ~IPNODE_WANTIPV6;
2407c478bd9Sstevel@tonic-gate 		ipnode_bits &= ~IPNODE_LOOKUPIPNODES;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	return (ipnode_bits);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate struct hostent *
getipnodebyname(const char * name,int af,int flags,int * error_num)2467c478bd9Sstevel@tonic-gate getipnodebyname(const char *name, int af, int flags, int *error_num)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
2497c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t		*buf4 = NULL;
2507c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t		*buf6 = NULL;
2517c478bd9Sstevel@tonic-gate 	struct netconfig	*nconf;
2527c478bd9Sstevel@tonic-gate 	struct nss_netdirbyname_in	nssin;
2537c478bd9Sstevel@tonic-gate 	union nss_netdirbyname_out	nssout;
2547c478bd9Sstevel@tonic-gate 	int			ret;
2557c478bd9Sstevel@tonic-gate 	uint_t			ipnode_bits;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
2587c478bd9Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
2597c478bd9Sstevel@tonic-gate 		*error_num = NO_RECOVERY;
2607c478bd9Sstevel@tonic-gate 		return (NULL);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	ipnode_bits = getipnodebyname_processflags(name, af, flags);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/* Make sure we have something to look up. */
2667c478bd9Sstevel@tonic-gate 	if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) {
2677c478bd9Sstevel@tonic-gate 		*error_num = HOST_NOT_FOUND;
2687c478bd9Sstevel@tonic-gate 		goto cleanup;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Perform the requested lookups.  We always look through
2737c478bd9Sstevel@tonic-gate 	 * ipnodes first for both IPv4 and IPv6 addresses.  Depending
2747c478bd9Sstevel@tonic-gate 	 * on what was returned and what was needed, we either filter
2757c478bd9Sstevel@tonic-gate 	 * out the garbage, or ask for more using hosts.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	if (ipnode_bits & IPNODE_LOOKUPIPNODES) {
2787c478bd9Sstevel@tonic-gate 		if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) {
2797c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
2807c478bd9Sstevel@tonic-gate 			goto cleanup;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 		nssin.op_t = NSS_HOST6;
2837c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.name = name;
2847c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.buf = buf6->buffer;
2857c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.buflen = buf6->buflen;
2867c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = af;
2877c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = flags;
2887c478bd9Sstevel@tonic-gate 		nssout.nss.host.hent = buf6->result;
2897c478bd9Sstevel@tonic-gate 		nssout.nss.host.herrno_p = error_num;
2907c478bd9Sstevel@tonic-gate 		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
2917c478bd9Sstevel@tonic-gate 		if (ret != ND_OK) {
2927c478bd9Sstevel@tonic-gate 			__IPv6_cleanup(buf6);
2937c478bd9Sstevel@tonic-gate 			buf6 = NULL;
2947c478bd9Sstevel@tonic-gate 		} else if (ipnode_bits & IPNODE_WANTIPV4) {
2957c478bd9Sstevel@tonic-gate 			/*
2967c478bd9Sstevel@tonic-gate 			 * buf6 may have all that we need if we either
2977c478bd9Sstevel@tonic-gate 			 * only wanted IPv4 addresses if there were no
2987c478bd9Sstevel@tonic-gate 			 * IPv6 addresses returned, or if there are
2997c478bd9Sstevel@tonic-gate 			 * IPv4-mapped addresses in buf6.  If either
3007c478bd9Sstevel@tonic-gate 			 * of these are true, then there's no need to
3017c478bd9Sstevel@tonic-gate 			 * look in hosts.
3027c478bd9Sstevel@tonic-gate 			 */
3037c478bd9Sstevel@tonic-gate 			if (ipnode_bits & IPNODE_IPV4IFNOIPV6 ||
3047c478bd9Sstevel@tonic-gate 			    __find_mapped(buf6->result, 0) != 0) {
3057c478bd9Sstevel@tonic-gate 				ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
3067c478bd9Sstevel@tonic-gate 			} else if (!(ipnode_bits & IPNODE_WANTIPV6)) {
3077c478bd9Sstevel@tonic-gate 				/*
3087c478bd9Sstevel@tonic-gate 				 * If all we're looking for are IPv4
3097c478bd9Sstevel@tonic-gate 				 * addresses and there are none in
3107c478bd9Sstevel@tonic-gate 				 * buf6 then buf6 is now useless.
3117c478bd9Sstevel@tonic-gate 				 */
3127c478bd9Sstevel@tonic-gate 				__IPv6_cleanup(buf6);
3137c478bd9Sstevel@tonic-gate 				buf6 = NULL;
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	if (ipnode_bits & IPNODE_LOOKUPHOSTS) {
3187c478bd9Sstevel@tonic-gate 		if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) {
3197c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
3207c478bd9Sstevel@tonic-gate 			goto cleanup;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		nssin.op_t = NSS_HOST;
3237c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.name = name;
3247c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buf = buf4->buffer;
3257c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buflen = buf4->buflen;
3267c478bd9Sstevel@tonic-gate 		nssout.nss.host.hent = buf4->result;
3277c478bd9Sstevel@tonic-gate 		nssout.nss.host.herrno_p = error_num;
3287c478bd9Sstevel@tonic-gate 		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
3297c478bd9Sstevel@tonic-gate 		if (ret != ND_OK) {
3307c478bd9Sstevel@tonic-gate 			__IPv6_cleanup(buf4);
3317c478bd9Sstevel@tonic-gate 			buf4 = NULL;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (buf6 == NULL && buf4 == NULL) {
3367c478bd9Sstevel@tonic-gate 		*error_num = HOST_NOT_FOUND;
3377c478bd9Sstevel@tonic-gate 		goto cleanup;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* Extract the appropriate addresses from the returned buffer(s). */
3417c478bd9Sstevel@tonic-gate 	switch (af) {
3427c478bd9Sstevel@tonic-gate 	case AF_INET6: {
3437c478bd9Sstevel@tonic-gate 		if (buf4 != NULL) {
3447c478bd9Sstevel@tonic-gate 			nss_XbyY_buf_t *mergebuf;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 			/*
3477c478bd9Sstevel@tonic-gate 			 * The IPv4 results we have need to be
3487c478bd9Sstevel@tonic-gate 			 * converted to IPv4-mapped addresses,
3497c478bd9Sstevel@tonic-gate 			 * conditionally merged with the IPv6
3507c478bd9Sstevel@tonic-gate 			 * results, and the end result needs to be
3517c478bd9Sstevel@tonic-gate 			 * re-ordered.
3527c478bd9Sstevel@tonic-gate 			 */
3537c478bd9Sstevel@tonic-gate 			mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES);
3547c478bd9Sstevel@tonic-gate 			if (mergebuf == NULL) {
3557c478bd9Sstevel@tonic-gate 				*error_num = NO_RECOVERY;
3567c478bd9Sstevel@tonic-gate 				goto cleanup;
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			hp = __mapv4tov6(buf4->result,
3597c478bd9Sstevel@tonic-gate 			    ((buf6 != NULL) ? buf6->result : NULL),
3607c478bd9Sstevel@tonic-gate 			    mergebuf, 1);
3617c478bd9Sstevel@tonic-gate 			if (hp != NULL)
3627c478bd9Sstevel@tonic-gate 				order_haddrlist_af(AF_INET6, hp->h_addr_list);
3637c478bd9Sstevel@tonic-gate 			else
3647c478bd9Sstevel@tonic-gate 				*error_num = NO_RECOVERY;
3657c478bd9Sstevel@tonic-gate 			free(mergebuf);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		if (buf4 == NULL && buf6 != NULL) {
3697c478bd9Sstevel@tonic-gate 			hp = buf6->result;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 			/*
3727c478bd9Sstevel@tonic-gate 			 * We have what we need in buf6, but we may need
3737c478bd9Sstevel@tonic-gate 			 * to filter out some addresses depending on what
3747c478bd9Sstevel@tonic-gate 			 * is being asked for.
3757c478bd9Sstevel@tonic-gate 			 */
3767c478bd9Sstevel@tonic-gate 			if (!(ipnode_bits & IPNODE_WANTIPV4))
3777c478bd9Sstevel@tonic-gate 				hp = __filter_addresses(AF_INET, buf6->result);
3787c478bd9Sstevel@tonic-gate 			else if (!(ipnode_bits & IPNODE_WANTIPV6))
3797c478bd9Sstevel@tonic-gate 				hp = __filter_addresses(AF_INET6, buf6->result);
3807c478bd9Sstevel@tonic-gate 
3812f443e27SRobert Mustacchi 			/*
3822f443e27SRobert Mustacchi 			 * We've been asked to unmap v4 addresses. This
3832f443e27SRobert Mustacchi 			 * situation implies IPNODE_WANTIPV4 and
3842f443e27SRobert Mustacchi 			 * !IPNODE_WANTIPV6.
3852f443e27SRobert Mustacchi 			 */
3862f443e27SRobert Mustacchi 			if (hp != NULL && (ipnode_bits & IPNODE_UNMAP)) {
3872f443e27SRobert Mustacchi 				/*
3882f443e27SRobert Mustacchi 				 * Just set hp to a new value, cleanup: will
3892f443e27SRobert Mustacchi 				 * free the old one
3902f443e27SRobert Mustacchi 				 */
3912f443e27SRobert Mustacchi 				hp = __mappedtov4(hp, error_num);
3922f443e27SRobert Mustacchi 			} else if (hp == NULL)
3937c478bd9Sstevel@tonic-gate 				*error_num = NO_ADDRESS;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		break;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	case AF_INET:
4007c478bd9Sstevel@tonic-gate 		/* We could have results in buf6 or buf4, not both */
4017c478bd9Sstevel@tonic-gate 		if (buf6 != NULL) {
4027c478bd9Sstevel@tonic-gate 			/*
4037c478bd9Sstevel@tonic-gate 			 * Extract the IPv4-mapped addresses from buf6
4047c478bd9Sstevel@tonic-gate 			 * into hp.
4057c478bd9Sstevel@tonic-gate 			 */
4067c478bd9Sstevel@tonic-gate 			hp = __mappedtov4(buf6->result, error_num);
4077c478bd9Sstevel@tonic-gate 		} else {
4087c478bd9Sstevel@tonic-gate 			/* We have what we need in buf4. */
4097c478bd9Sstevel@tonic-gate 			hp = buf4->result;
4107c478bd9Sstevel@tonic-gate 			if (ipnode_bits & IPNODE_LITERAL) {
4117c478bd9Sstevel@tonic-gate 				/*
4127c478bd9Sstevel@tonic-gate 				 * There is a special case here for literal
4137c478bd9Sstevel@tonic-gate 				 * IPv4 address strings.  The hosts
4147c478bd9Sstevel@tonic-gate 				 * front-end sets h_aliases to a one
4157c478bd9Sstevel@tonic-gate 				 * element array containing a single NULL
4167c478bd9Sstevel@tonic-gate 				 * pointer (in ndaddr2hent()), while
4177c478bd9Sstevel@tonic-gate 				 * getipnodebyname() requires h_aliases to
4187c478bd9Sstevel@tonic-gate 				 * be a NULL pointer itself.  We're not
4197c478bd9Sstevel@tonic-gate 				 * going to change the front-end since it
4207c478bd9Sstevel@tonic-gate 				 * needs to remain backward compatible for
4217c478bd9Sstevel@tonic-gate 				 * gethostbyname() and friends.  Just set
4227c478bd9Sstevel@tonic-gate 				 * h_aliases to NULL here instead.
4237c478bd9Sstevel@tonic-gate 				 */
4247c478bd9Sstevel@tonic-gate 				hp->h_aliases = NULL;
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	default:
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate cleanup:
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * Free the memory we allocated, but make sure we don't free
4377c478bd9Sstevel@tonic-gate 	 * the memory we're returning to the caller.
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	if (buf6 != NULL) {
4407c478bd9Sstevel@tonic-gate 		if (buf6->result == hp)
4417c478bd9Sstevel@tonic-gate 			buf6->result = NULL;
4427c478bd9Sstevel@tonic-gate 		__IPv6_cleanup(buf6);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	if (buf4 != NULL) {
4457c478bd9Sstevel@tonic-gate 		if (buf4->result == hp)
4467c478bd9Sstevel@tonic-gate 			buf4->result = NULL;
4477c478bd9Sstevel@tonic-gate 		__IPv6_cleanup(buf4);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	return (hp);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * This is the IPv6 interface for "gethostbyaddr".
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate struct hostent *
getipnodebyaddr(const void * src,size_t len,int type,int * error_num)4587c478bd9Sstevel@tonic-gate getipnodebyaddr(const void *src, size_t len, int type, int *error_num)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	struct in6_addr *addr6 = 0;
4617c478bd9Sstevel@tonic-gate 	struct in_addr *addr4 = 0;
4627c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t *buf = 0;
4637c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t *res = 0;
4647c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
4657c478bd9Sstevel@tonic-gate 	struct hostent *hp = 0;
4667c478bd9Sstevel@tonic-gate 	struct	nss_netdirbyaddr_in nssin;
4677c478bd9Sstevel@tonic-gate 	union	nss_netdirbyaddr_out nssout;
4687c478bd9Sstevel@tonic-gate 	int neterr;
4697c478bd9Sstevel@tonic-gate 	char tmpbuf[64];
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (type == AF_INET6) {
4727c478bd9Sstevel@tonic-gate 		if ((addr6 = (struct in6_addr *)src) == NULL) {
4737c478bd9Sstevel@tonic-gate 			*error_num = HOST_NOT_FOUND;
4747c478bd9Sstevel@tonic-gate 			return (NULL);
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 	} else if (type == AF_INET) {
4777c478bd9Sstevel@tonic-gate 		if ((addr4 = (struct in_addr *)src) == NULL) {
4787c478bd9Sstevel@tonic-gate 			*error_num = HOST_NOT_FOUND;
4797c478bd9Sstevel@tonic-gate 			return (NULL);
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		*error_num = HOST_NOT_FOUND;
4837c478bd9Sstevel@tonic-gate 		return (NULL);
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * Specific case: query for "::"
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) {
4897c478bd9Sstevel@tonic-gate 		*error_num = HOST_NOT_FOUND;
4907c478bd9Sstevel@tonic-gate 		return (NULL);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * Step 1: IPv4-mapped address  or IPv4 Compat
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	if ((type == AF_INET6 && len == 16) &&
4967c478bd9Sstevel@tonic-gate 	    ((IN6_IS_ADDR_V4MAPPED(addr6)) ||
4977c478bd9Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr6)))) {
4987c478bd9Sstevel@tonic-gate 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
4997c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
5007c478bd9Sstevel@tonic-gate 			return (NULL);
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
5037c478bd9Sstevel@tonic-gate 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
5047c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
5057c478bd9Sstevel@tonic-gate 			__IPv6_cleanup(buf);
5067c478bd9Sstevel@tonic-gate 			return (NULL);
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		nssin.op_t = NSS_HOST6;
5097c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
51061961e0fSrobinson 			(void) memcpy(tmpbuf, addr6, sizeof (*addr6));
5117c478bd9Sstevel@tonic-gate 			tmpbuf[10] = 0xffU;
5127c478bd9Sstevel@tonic-gate 			tmpbuf[11] = 0xffU;
5137c478bd9Sstevel@tonic-gate 			nssin.arg.nss.host.addr = (const char *)tmpbuf;
5147c478bd9Sstevel@tonic-gate 		} else {
5157c478bd9Sstevel@tonic-gate 			nssin.arg.nss.host.addr = (const char *)addr6;
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.len = sizeof (struct in6_addr);
5187c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.type = AF_INET6;
5197c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buf = buf->buffer;
5207c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buflen = buf->buflen;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		nssout.nss.host.hent = buf->result;
5237c478bd9Sstevel@tonic-gate 		nssout.nss.host.herrno_p = error_num;
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * We pass in nconf and let the implementation of the
5267c478bd9Sstevel@tonic-gate 		 * long-named func decide whether to use the switch based on
5277c478bd9Sstevel@tonic-gate 		 * nc_nlookups.
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		neterr =
5307c478bd9Sstevel@tonic-gate 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		(void) freenetconfigent(nconf);
5337c478bd9Sstevel@tonic-gate 		if (neterr != ND_OK) {
5347c478bd9Sstevel@tonic-gate 			/* Failover case, try hosts db for v4 address */
5357c478bd9Sstevel@tonic-gate 			if (!gethostbyaddr_r(((char *)addr6) + 12,
5367c478bd9Sstevel@tonic-gate 			    sizeof (in_addr_t), AF_INET, buf->result,
5377c478bd9Sstevel@tonic-gate 			    buf->buffer, buf->buflen, error_num)) {
5387c478bd9Sstevel@tonic-gate 				__IPv6_cleanup(buf);
5397c478bd9Sstevel@tonic-gate 				return (NULL);
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 			/* Found one, now format it into mapped/compat addr */
5427c478bd9Sstevel@tonic-gate 			if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
5437c478bd9Sstevel@tonic-gate 				__IPv6_cleanup(buf);
5447c478bd9Sstevel@tonic-gate 				*error_num = NO_RECOVERY;
5457c478bd9Sstevel@tonic-gate 				return (NULL);
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 			/* Convert IPv4 to mapped/compat address w/name */
5487c478bd9Sstevel@tonic-gate 			hp = res->result;
54961961e0fSrobinson 			(void) __mapv4tov6(buf->result, 0, res,
5507c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(addr6));
5517c478bd9Sstevel@tonic-gate 			__IPv6_cleanup(buf);
5527c478bd9Sstevel@tonic-gate 			free(res);
5537c478bd9Sstevel@tonic-gate 			return (hp);
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 		/*
5567c478bd9Sstevel@tonic-gate 		 * At this point, we'll have a v4mapped hostent. If that's
5577c478bd9Sstevel@tonic-gate 		 * what was passed in, just return. If the request was a compat,
5587c478bd9Sstevel@tonic-gate 		 * twiggle the two bytes to make the mapped address a compat.
5597c478bd9Sstevel@tonic-gate 		 */
5607c478bd9Sstevel@tonic-gate 		hp = buf->result;
5617c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
56261961e0fSrobinson 			/* LINTED pointer cast */
5637c478bd9Sstevel@tonic-gate 			addr6 = (struct in6_addr *)hp->h_addr_list[0];
5647c478bd9Sstevel@tonic-gate 			addr6->s6_addr[10] = 0;
5657c478bd9Sstevel@tonic-gate 			addr6->s6_addr[11] = 0;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		free(buf);
5687c478bd9Sstevel@tonic-gate 		return (hp);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 	/*
5717c478bd9Sstevel@tonic-gate 	 * Step 2: AF_INET, v4 lookup. Since we're going to search the
5727c478bd9Sstevel@tonic-gate 	 * ipnodes (v6) path first, we need to treat this as a v4mapped
573*bbf21555SRichard Lowe 	 * address. nscd(8) caches v4 from ipnodes as mapped v6's. The
5747c478bd9Sstevel@tonic-gate 	 * switch backend knows to lookup v4's (not v4mapped) from the
5757c478bd9Sstevel@tonic-gate 	 * name services.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	if (type == AF_INET) {
5787c478bd9Sstevel@tonic-gate 		struct in6_addr v4mapbuf;
5797c478bd9Sstevel@tonic-gate 		addr6 = &v4mapbuf;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		IN6_INADDR_TO_V4MAPPED(addr4, addr6);
5827c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
5837c478bd9Sstevel@tonic-gate 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
5847c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
5857c478bd9Sstevel@tonic-gate 			return (NULL);
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
5887c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
5897c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
5907c478bd9Sstevel@tonic-gate 			return (NULL);
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		nssin.op_t = NSS_HOST6;
5937c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.addr = (const char *)addr6;
5947c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.len = sizeof (struct in6_addr);
5957c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.type = AF_INET6;
5967c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buf = buf->buffer;
5977c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buflen = buf->buflen;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		nssout.nss.host.hent = buf->result;
6007c478bd9Sstevel@tonic-gate 		nssout.nss.host.herrno_p = error_num;
6017c478bd9Sstevel@tonic-gate 		/*
6027c478bd9Sstevel@tonic-gate 		 * We pass in nconf and let the implementation of the
6037c478bd9Sstevel@tonic-gate 		 * long-named func decide whether to use the switch based on
6047c478bd9Sstevel@tonic-gate 		 * nc_nlookups.
6057c478bd9Sstevel@tonic-gate 		 */
6067c478bd9Sstevel@tonic-gate 		neterr =
6077c478bd9Sstevel@tonic-gate 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		(void) freenetconfigent(nconf);
6107c478bd9Sstevel@tonic-gate 		if (neterr != ND_OK) {
6117c478bd9Sstevel@tonic-gate 			/* Failover case, try hosts db for v4 address */
6127c478bd9Sstevel@tonic-gate 			hp = buf->result;
6137c478bd9Sstevel@tonic-gate 			if (!gethostbyaddr_r(src, len, type, buf->result,
6147c478bd9Sstevel@tonic-gate 			    buf->buffer, buf->buflen, error_num)) {
6157c478bd9Sstevel@tonic-gate 				__IPv6_cleanup(buf);
6167c478bd9Sstevel@tonic-gate 				return (NULL);
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			free(buf);
6197c478bd9Sstevel@tonic-gate 			return (hp);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		if ((hp = __mappedtov4(buf->result, error_num)) == NULL) {
6227c478bd9Sstevel@tonic-gate 			__IPv6_cleanup(buf);
6237c478bd9Sstevel@tonic-gate 			return (NULL);
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 		__IPv6_cleanup(buf);
6267c478bd9Sstevel@tonic-gate 		return (hp);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	if (type == AF_INET6) {
6327c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
6337c478bd9Sstevel@tonic-gate 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
6347c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
6357c478bd9Sstevel@tonic-gate 			return (NULL);
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
6387c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
6397c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
6407c478bd9Sstevel@tonic-gate 			return (NULL);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 		nssin.op_t = NSS_HOST6;
6437c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.addr = (const char *)addr6;
6447c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.len = len;
6457c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.type = type;
6467c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buf = buf->buffer;
6477c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host.buflen = buf->buflen;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		nssout.nss.host.hent = buf->result;
6507c478bd9Sstevel@tonic-gate 		nssout.nss.host.herrno_p = error_num;
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * We pass in nconf and let the implementation of the
6537c478bd9Sstevel@tonic-gate 		 * long-named func decide whether to use the switch based on
6547c478bd9Sstevel@tonic-gate 		 * nc_nlookups.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		neterr =
6577c478bd9Sstevel@tonic-gate 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		(void) freenetconfigent(nconf);
6607c478bd9Sstevel@tonic-gate 		if (neterr != ND_OK) {
6617c478bd9Sstevel@tonic-gate 			__IPv6_cleanup(buf);
6627c478bd9Sstevel@tonic-gate 			return (NULL);
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 		free(buf);
6657c478bd9Sstevel@tonic-gate 		return (nssout.nss.host.hent);
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * If we got here, unknown type.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	*error_num = HOST_NOT_FOUND;
6717c478bd9Sstevel@tonic-gate 	return (NULL);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate void
freehostent(struct hostent * hent)6757c478bd9Sstevel@tonic-gate freehostent(struct hostent *hent)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	free(hent);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate static int
__ai_addrconfig(int af,boolean_t loopback)68196088c83SRobert Mustacchi __ai_addrconfig(int af, boolean_t loopback)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	struct lifnum	lifn;
68420d217c8SGirish Moodalbail 	struct lifconf	lifc;
68520d217c8SGirish Moodalbail 	struct lifreq	*lifp, *buf = NULL;
68620d217c8SGirish Moodalbail 	size_t		bufsize;
6877c478bd9Sstevel@tonic-gate 	hrtime_t	now, *then;
6887c478bd9Sstevel@tonic-gate 	static hrtime_t	then4, then6; /* the last time we updated ifnum# */
68996088c83SRobert Mustacchi 	static int	ifnum4 = -1, ifnum6 = -1, iflb4 = 0, iflb6 = 0;
69096088c83SRobert Mustacchi 	int		*num, *lb;
69120d217c8SGirish Moodalbail 	int 		nlifr, count = 0;
69220d217c8SGirish Moodalbail 
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	switch (af) {
6957c478bd9Sstevel@tonic-gate 	case AF_INET:
6967c478bd9Sstevel@tonic-gate 		num = &ifnum4;
6977c478bd9Sstevel@tonic-gate 		then = &then4;
69896088c83SRobert Mustacchi 		lb = &iflb4;
6997c478bd9Sstevel@tonic-gate 		break;
7007c478bd9Sstevel@tonic-gate 	case AF_INET6:
7017c478bd9Sstevel@tonic-gate 		num = &ifnum6;
7027c478bd9Sstevel@tonic-gate 		then = &then6;
70396088c83SRobert Mustacchi 		lb = &iflb6;
7047c478bd9Sstevel@tonic-gate 		break;
7057c478bd9Sstevel@tonic-gate 	default:
7067c478bd9Sstevel@tonic-gate 		return (0);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * We don't need to check this every time someone does a name
7117c478bd9Sstevel@tonic-gate 	 * lookup.  Do it every IFNUM_TIMEOUT for each address family.
7127c478bd9Sstevel@tonic-gate 	 *
7137c478bd9Sstevel@tonic-gate 	 * There's no need to protect all of this with a lock.  The
7147c478bd9Sstevel@tonic-gate 	 * worst that can happen is that we update the interface count
7157c478bd9Sstevel@tonic-gate 	 * twice instead of once.  That's no big deal.
7167c478bd9Sstevel@tonic-gate 	 */
7177c478bd9Sstevel@tonic-gate 	now = gethrtime();
7187c478bd9Sstevel@tonic-gate 	if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) {
7197c478bd9Sstevel@tonic-gate 		lifn.lifn_family = af;
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * We want to determine if this machine knows anything
7227c478bd9Sstevel@tonic-gate 		 * at all about the address family; the status of the
7237c478bd9Sstevel@tonic-gate 		 * interface is less important. Hence, set
7247c478bd9Sstevel@tonic-gate 		 * 'lifn_flags' to zero.
7257c478bd9Sstevel@tonic-gate 		 */
7267c478bd9Sstevel@tonic-gate 		lifn.lifn_flags = 0;
72720d217c8SGirish Moodalbail again:
7287c478bd9Sstevel@tonic-gate 		if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0)
72920d217c8SGirish Moodalbail 			goto fail;
7307c478bd9Sstevel@tonic-gate 
73120d217c8SGirish Moodalbail 		if (lifn.lifn_count == 0) {
73296088c83SRobert Mustacchi 			*lb = 0;
73320d217c8SGirish Moodalbail 			*num = 0;
7347c478bd9Sstevel@tonic-gate 			*then = now;
73520d217c8SGirish Moodalbail 			return (*num);
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 
73820d217c8SGirish Moodalbail 		/*
73920d217c8SGirish Moodalbail 		 * Pad the interface count to detect when additional
74020d217c8SGirish Moodalbail 		 * interfaces have been configured between SIOCGLIFNUM
74120d217c8SGirish Moodalbail 		 * and SIOCGLIFCONF.
74220d217c8SGirish Moodalbail 		 */
74320d217c8SGirish Moodalbail 		lifn.lifn_count += 4;
74420d217c8SGirish Moodalbail 
74520d217c8SGirish Moodalbail 		bufsize = lifn.lifn_count * sizeof (struct lifreq);
74620d217c8SGirish Moodalbail 		if ((buf = realloc(buf, bufsize)) == NULL)
74720d217c8SGirish Moodalbail 			goto fail;
74820d217c8SGirish Moodalbail 
74920d217c8SGirish Moodalbail 		lifc.lifc_family = af;
75020d217c8SGirish Moodalbail 		lifc.lifc_flags = 0;
75120d217c8SGirish Moodalbail 		lifc.lifc_len = bufsize;
75220d217c8SGirish Moodalbail 		lifc.lifc_buf = (caddr_t)buf;
75320d217c8SGirish Moodalbail 		if (nss_ioctl(af, SIOCGLIFCONF, &lifc) < 0)
75420d217c8SGirish Moodalbail 			goto fail;
75520d217c8SGirish Moodalbail 
75620d217c8SGirish Moodalbail 		nlifr = lifc.lifc_len / sizeof (struct lifreq);
75720d217c8SGirish Moodalbail 		if (nlifr >= lifn.lifn_count)
75820d217c8SGirish Moodalbail 			goto again;
75920d217c8SGirish Moodalbail 		/*
76020d217c8SGirish Moodalbail 		 * Do not include any loopback addresses, 127.0.0.1 for AF_INET
76120d217c8SGirish Moodalbail 		 * and ::1 for AF_INET6, while counting the number of available
76220d217c8SGirish Moodalbail 		 * IPv4 or IPv6 addresses. (RFC 3493 requires this, whenever
76396088c83SRobert Mustacchi 		 * AI_ADDRCONFIG flag is set) However, if the loopback flag is
76496088c83SRobert Mustacchi 		 * set to true we'll include it in the output.
76520d217c8SGirish Moodalbail 		 */
76620d217c8SGirish Moodalbail 		for (lifp = buf; lifp < buf + nlifr; lifp++) {
76720d217c8SGirish Moodalbail 			switch (af) {
76820d217c8SGirish Moodalbail 			case AF_INET: {
76920d217c8SGirish Moodalbail 				struct sockaddr_in *in;
77020d217c8SGirish Moodalbail 
77120d217c8SGirish Moodalbail 				in = (struct sockaddr_in *)&lifp->lifr_addr;
77220d217c8SGirish Moodalbail 				if (ntohl(in->sin_addr.s_addr) ==
77320d217c8SGirish Moodalbail 				    INADDR_LOOPBACK) {
77420d217c8SGirish Moodalbail 					count++;
77520d217c8SGirish Moodalbail 				}
77620d217c8SGirish Moodalbail 				break;
77720d217c8SGirish Moodalbail 			}
77820d217c8SGirish Moodalbail 			case AF_INET6: {
77920d217c8SGirish Moodalbail 				struct sockaddr_in6 *in6;
78020d217c8SGirish Moodalbail 
78120d217c8SGirish Moodalbail 				in6 = (struct sockaddr_in6 *)&lifp->lifr_addr;
78220d217c8SGirish Moodalbail 				if (IN6_IS_ADDR_LOOPBACK(&in6->sin6_addr))
78320d217c8SGirish Moodalbail 					count++;
78420d217c8SGirish Moodalbail 				break;
78520d217c8SGirish Moodalbail 			}
78620d217c8SGirish Moodalbail 			}
78720d217c8SGirish Moodalbail 		}
78820d217c8SGirish Moodalbail 		*num = nlifr - count;
78996088c83SRobert Mustacchi 		*lb = count;
79020d217c8SGirish Moodalbail 		*then = now;
79120d217c8SGirish Moodalbail 		free(buf);
79220d217c8SGirish Moodalbail 	}
79396088c83SRobert Mustacchi 	if (loopback == B_TRUE)
79496088c83SRobert Mustacchi 		return (*num + *lb);
79596088c83SRobert Mustacchi 	else
7967c478bd9Sstevel@tonic-gate 		return (*num);
79720d217c8SGirish Moodalbail fail:
79820d217c8SGirish Moodalbail 	free(buf);
799634e26ecSCasper H.S. Dik 	/*
800634e26ecSCasper H.S. Dik 	 * If the process is running without the NET_ACCESS basic privilege,
801634e26ecSCasper H.S. Dik 	 * pretend we still have inet/inet6 interfaces.
802634e26ecSCasper H.S. Dik 	 */
803634e26ecSCasper H.S. Dik 	if (errno == EACCES)
804634e26ecSCasper H.S. Dik 		return (1);
80520d217c8SGirish Moodalbail 	return (-1);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*
8097c478bd9Sstevel@tonic-gate  * This routine will either convert an IPv4 address to a mapped or compat
8107c478bd9Sstevel@tonic-gate  * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped
8117c478bd9Sstevel@tonic-gate  * v4 (he4) addresses. In either case, the results are returned in res.
8127c478bd9Sstevel@tonic-gate  * Caller must provide all buffers.
8137c478bd9Sstevel@tonic-gate  * Inputs:
8147c478bd9Sstevel@tonic-gate  * 		he4	pointer to IPv4 buffer
8157c478bd9Sstevel@tonic-gate  *		he6	pointer to IPv6 buffer (NULL if not merging v4/v6
8167c478bd9Sstevel@tonic-gate  *		res	pointer to results buffer
8177c478bd9Sstevel@tonic-gate  *		mapped	mapped == 1, map IPv4 : mapped == 0, compat IPv4
8187c478bd9Sstevel@tonic-gate  *			mapped flag is ignored if he6 != NULL
8197c478bd9Sstevel@tonic-gate  *
8207c478bd9Sstevel@tonic-gate  * The results are packed into the res->buffer as follows:
8217c478bd9Sstevel@tonic-gate  * <--------------- buffer + buflen -------------------------------------->
8227c478bd9Sstevel@tonic-gate  * |-----------------|-----------------|----------------|----------------|
8237c478bd9Sstevel@tonic-gate  * | pointers vector | pointers vector | aliases grow   | addresses grow |
8247c478bd9Sstevel@tonic-gate  * | for addresses   | for aliases     |                |                |
8257c478bd9Sstevel@tonic-gate  * | this way ->     | this way ->     | <- this way    |<- this way     |
8267c478bd9Sstevel@tonic-gate  * |-----------------|-----------------|----------------|----------------|
8277c478bd9Sstevel@tonic-gate  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate static struct hostent *
__mapv4tov6(struct hostent * he4,struct hostent * he6,nss_XbyY_buf_t * res,int mapped)8307c478bd9Sstevel@tonic-gate __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res,
8317c478bd9Sstevel@tonic-gate 		int mapped)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	char	*buffer, *limit;
8347c478bd9Sstevel@tonic-gate 	int	buflen = res->buflen;
8357c478bd9Sstevel@tonic-gate 	struct	in6_addr *addr6p;
8367c478bd9Sstevel@tonic-gate 	char	*buff_locp;
8377c478bd9Sstevel@tonic-gate 	struct	hostent *host;
8387c478bd9Sstevel@tonic-gate 	int	count = 0, len, i;
8397c478bd9Sstevel@tonic-gate 	char	*h_namep;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	if (he4 == NULL || res == NULL) {
8427c478bd9Sstevel@tonic-gate 		return (NULL);
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 	limit = res->buffer + buflen;
8457c478bd9Sstevel@tonic-gate 	host = (struct hostent *)res->result;
8467c478bd9Sstevel@tonic-gate 	buffer = res->buffer;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr));
8497c478bd9Sstevel@tonic-gate 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
8507c478bd9Sstevel@tonic-gate 	if ((char *)host->h_addr_list >= limit ||
8517c478bd9Sstevel@tonic-gate 	    buff_locp <= (char *)host->h_addr_list) {
8527c478bd9Sstevel@tonic-gate 		return (NULL);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	if (he6 == NULL) {
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * If he6==NULL, map the v4 address into the v6 address format.
8577c478bd9Sstevel@tonic-gate 		 * This is used for getipnodebyaddr() (single address, mapped or
8587c478bd9Sstevel@tonic-gate 		 * compatible) or for v4 mapped for getipnodebyname(), which
8597c478bd9Sstevel@tonic-gate 		 * could be multiple addresses. This could also be a literal
8607c478bd9Sstevel@tonic-gate 		 * address string, which is why there is a inet_addr() call.
8617c478bd9Sstevel@tonic-gate 		 */
8627c478bd9Sstevel@tonic-gate 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
8637c478bd9Sstevel@tonic-gate 			buff_locp -= sizeof (struct in6_addr);
8647c478bd9Sstevel@tonic-gate 			if (buff_locp <=
8657c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_addr_list[count + 1])) {
8667c478bd9Sstevel@tonic-gate 			/*
8677c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
8687c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
8697c478bd9Sstevel@tonic-gate 			 */
8707c478bd9Sstevel@tonic-gate 				return (NULL);
8717c478bd9Sstevel@tonic-gate 			}
87261961e0fSrobinson 			/* LINTED pointer cast */
8737c478bd9Sstevel@tonic-gate 			addr6p = (struct in6_addr *)buff_locp;
8747c478bd9Sstevel@tonic-gate 			host->h_addr_list[count] = (char *)addr6p;
8757c478bd9Sstevel@tonic-gate 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
8767c478bd9Sstevel@tonic-gate 			if (mapped) {
8777c478bd9Sstevel@tonic-gate 				addr6p->s6_addr[10] = 0xff;
8787c478bd9Sstevel@tonic-gate 				addr6p->s6_addr[11] = 0xff;
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 			bcopy((char *)he4->h_addr_list[i],
8817c478bd9Sstevel@tonic-gate 			    &addr6p->s6_addr[12], sizeof (struct in_addr));
8827c478bd9Sstevel@tonic-gate 			++count;
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 		/*
8857c478bd9Sstevel@tonic-gate 		 * Set last array element to NULL and add cname as first alias
8867c478bd9Sstevel@tonic-gate 		 */
8877c478bd9Sstevel@tonic-gate 		host->h_addr_list[count] = NULL;
8887c478bd9Sstevel@tonic-gate 		host->h_aliases = host->h_addr_list + count + 1;
8897c478bd9Sstevel@tonic-gate 		count = 0;
8907c478bd9Sstevel@tonic-gate 		if ((int)(inet_addr(he4->h_name)) != -1) {
8917c478bd9Sstevel@tonic-gate 		/*
8927c478bd9Sstevel@tonic-gate 		 * Literal address string, since we're mapping, we need the IPv6
8937c478bd9Sstevel@tonic-gate 		 * V4 mapped literal address string for h_name.
8947c478bd9Sstevel@tonic-gate 		 */
8957c478bd9Sstevel@tonic-gate 			char	tmpstr[128];
89661961e0fSrobinson 			(void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr,
8977c478bd9Sstevel@tonic-gate 			    sizeof (tmpstr));
8987c478bd9Sstevel@tonic-gate 			buff_locp -= (len = strlen(tmpstr) + 1);
8997c478bd9Sstevel@tonic-gate 			h_namep = tmpstr;
9007c478bd9Sstevel@tonic-gate 			if (buff_locp <= (char *)(host->h_aliases))
9017c478bd9Sstevel@tonic-gate 				return (NULL);
9027c478bd9Sstevel@tonic-gate 			bcopy(h_namep, buff_locp, len);
9037c478bd9Sstevel@tonic-gate 			host->h_name = buff_locp;
9047c478bd9Sstevel@tonic-gate 			host->h_aliases = NULL; /* no aliases for literal */
9057c478bd9Sstevel@tonic-gate 			host->h_length = sizeof (struct in6_addr);
9067c478bd9Sstevel@tonic-gate 			host->h_addrtype = AF_INET6;
9077c478bd9Sstevel@tonic-gate 			return (host); 		/* we're done, return result */
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * Not a literal address string, so just copy h_name.
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate 		buff_locp -= (len = strlen(he4->h_name) + 1);
9137c478bd9Sstevel@tonic-gate 		h_namep = he4->h_name;
9147c478bd9Sstevel@tonic-gate 		if (buff_locp <= (char *)(host->h_aliases))
9157c478bd9Sstevel@tonic-gate 			return (NULL);
9167c478bd9Sstevel@tonic-gate 		bcopy(h_namep, buff_locp, len);
9177c478bd9Sstevel@tonic-gate 		host->h_name = buff_locp;
9187c478bd9Sstevel@tonic-gate 		/*
9197c478bd9Sstevel@tonic-gate 		 * Pass 2 (IPv4 aliases):
9207c478bd9Sstevel@tonic-gate 		 */
9217c478bd9Sstevel@tonic-gate 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
9227c478bd9Sstevel@tonic-gate 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
9237c478bd9Sstevel@tonic-gate 			if (buff_locp <=
9247c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_aliases[count + 1])) {
9257c478bd9Sstevel@tonic-gate 			/*
9267c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
9277c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
9287c478bd9Sstevel@tonic-gate 			 */
9297c478bd9Sstevel@tonic-gate 				return (NULL);
9307c478bd9Sstevel@tonic-gate 			}
9317c478bd9Sstevel@tonic-gate 			host->h_aliases[count] = buff_locp;
9327c478bd9Sstevel@tonic-gate 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
9337c478bd9Sstevel@tonic-gate 			++count;
9347c478bd9Sstevel@tonic-gate 		}
9357c478bd9Sstevel@tonic-gate 		host->h_aliases[count] = NULL;
9367c478bd9Sstevel@tonic-gate 		host->h_length = sizeof (struct in6_addr);
9377c478bd9Sstevel@tonic-gate 		host->h_addrtype = AF_INET6;
9387c478bd9Sstevel@tonic-gate 		return (host);
9397c478bd9Sstevel@tonic-gate 	} else {
9407c478bd9Sstevel@tonic-gate 		/*
9417c478bd9Sstevel@tonic-gate 		 * Merge IPv4 mapped addresses with IPv6 addresses. The
9427c478bd9Sstevel@tonic-gate 		 * IPv6 address will go in first, followed by the v4 mapped.
9437c478bd9Sstevel@tonic-gate 		 *
9447c478bd9Sstevel@tonic-gate 		 * Pass 1 (IPv6 addresses):
9457c478bd9Sstevel@tonic-gate 		 */
9467c478bd9Sstevel@tonic-gate 		for (i = 0; he6->h_addr_list[i] != NULL; i++) {
9477c478bd9Sstevel@tonic-gate 			buff_locp -= sizeof (struct in6_addr);
9487c478bd9Sstevel@tonic-gate 			if (buff_locp <=
9497c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_addr_list[count + 1])) {
9507c478bd9Sstevel@tonic-gate 			/*
9517c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
9527c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
9537c478bd9Sstevel@tonic-gate 			 */
9547c478bd9Sstevel@tonic-gate 				return (NULL);
9557c478bd9Sstevel@tonic-gate 			}
9567c478bd9Sstevel@tonic-gate 			host->h_addr_list[count] = buff_locp;
9577c478bd9Sstevel@tonic-gate 			bcopy((char *)he6->h_addr_list[i], buff_locp,
9587c478bd9Sstevel@tonic-gate 			    sizeof (struct in6_addr));
9597c478bd9Sstevel@tonic-gate 			++count;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 		/*
9627c478bd9Sstevel@tonic-gate 		 * Pass 1 (IPv4 mapped addresses):
9637c478bd9Sstevel@tonic-gate 		 */
9647c478bd9Sstevel@tonic-gate 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
9657c478bd9Sstevel@tonic-gate 			buff_locp -= sizeof (struct in6_addr);
9667c478bd9Sstevel@tonic-gate 			if (buff_locp <=
9677c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_addr_list[count + 1])) {
9687c478bd9Sstevel@tonic-gate 			/*
9697c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
9707c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
9717c478bd9Sstevel@tonic-gate 			 */
9727c478bd9Sstevel@tonic-gate 				return (NULL);
9737c478bd9Sstevel@tonic-gate 			}
97461961e0fSrobinson 			/* LINTED pointer cast */
9757c478bd9Sstevel@tonic-gate 			addr6p = (struct in6_addr *)buff_locp;
9767c478bd9Sstevel@tonic-gate 			host->h_addr_list[count] = (char *)addr6p;
9777c478bd9Sstevel@tonic-gate 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
9787c478bd9Sstevel@tonic-gate 			addr6p->s6_addr[10] = 0xff;
9797c478bd9Sstevel@tonic-gate 			addr6p->s6_addr[11] = 0xff;
9807c478bd9Sstevel@tonic-gate 			bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12],
9817c478bd9Sstevel@tonic-gate 			    sizeof (struct in_addr));
9827c478bd9Sstevel@tonic-gate 			++count;
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 		/*
9857c478bd9Sstevel@tonic-gate 		 * Pass 2 (IPv6 aliases, host name first). We start h_aliases
9867c478bd9Sstevel@tonic-gate 		 * one after where h_addr_list array ended. This is where cname
9877c478bd9Sstevel@tonic-gate 		 * is put, followed by all aliases. Reset count to 0, for index
9887c478bd9Sstevel@tonic-gate 		 * in the h_aliases array.
9897c478bd9Sstevel@tonic-gate 		 */
9907c478bd9Sstevel@tonic-gate 		host->h_addr_list[count] = NULL;
9917c478bd9Sstevel@tonic-gate 		host->h_aliases = host->h_addr_list + count + 1;
9927c478bd9Sstevel@tonic-gate 		count = 0;
9937c478bd9Sstevel@tonic-gate 		buff_locp -= (len = strlen(he6->h_name) + 1);
9947c478bd9Sstevel@tonic-gate 		if (buff_locp <= (char *)(host->h_aliases))
9957c478bd9Sstevel@tonic-gate 			return (NULL);
9967c478bd9Sstevel@tonic-gate 		bcopy(he6->h_name, buff_locp, len);
9977c478bd9Sstevel@tonic-gate 		host->h_name = buff_locp;
9987c478bd9Sstevel@tonic-gate 		for (i = 0; he6->h_aliases[i] != NULL; i++) {
9997c478bd9Sstevel@tonic-gate 			buff_locp -= (len = strlen(he6->h_aliases[i]) + 1);
10007c478bd9Sstevel@tonic-gate 			if (buff_locp <=
10017c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_aliases[count + 1])) {
10027c478bd9Sstevel@tonic-gate 			/*
10037c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
10047c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
10057c478bd9Sstevel@tonic-gate 			 */
10067c478bd9Sstevel@tonic-gate 				return (NULL);
10077c478bd9Sstevel@tonic-gate 			}
10087c478bd9Sstevel@tonic-gate 			host->h_aliases[count] = buff_locp;
10097c478bd9Sstevel@tonic-gate 			bcopy((char *)he6->h_aliases[i], buff_locp, len);
10107c478bd9Sstevel@tonic-gate 			++count;
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 		/*
10137c478bd9Sstevel@tonic-gate 		 * Pass 2 (IPv4 aliases):
10147c478bd9Sstevel@tonic-gate 		 */
10157c478bd9Sstevel@tonic-gate 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
10167c478bd9Sstevel@tonic-gate 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
10177c478bd9Sstevel@tonic-gate 			if (buff_locp <=
10187c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_aliases[count + 1])) {
10197c478bd9Sstevel@tonic-gate 			/*
10207c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
10217c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
10227c478bd9Sstevel@tonic-gate 			 */
10237c478bd9Sstevel@tonic-gate 				return (NULL);
10247c478bd9Sstevel@tonic-gate 			}
10257c478bd9Sstevel@tonic-gate 			host->h_aliases[count] = buff_locp;
10267c478bd9Sstevel@tonic-gate 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
10277c478bd9Sstevel@tonic-gate 			++count;
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 		host->h_aliases[count] = NULL;
10307c478bd9Sstevel@tonic-gate 		host->h_length = sizeof (struct in6_addr);
10317c478bd9Sstevel@tonic-gate 		host->h_addrtype = AF_INET6;
10327c478bd9Sstevel@tonic-gate 		return (host);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate  * This routine will convert a mapped v4 hostent (AF_INET6) to a
10387c478bd9Sstevel@tonic-gate  * AF_INET hostent. If no mapped addrs found, then a NULL is returned.
10397c478bd9Sstevel@tonic-gate  * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped
10407c478bd9Sstevel@tonic-gate  * addresses are extracted and copied to it. On sucess, a pointer to a new
10417c478bd9Sstevel@tonic-gate  * hostent is returned.
10427c478bd9Sstevel@tonic-gate  * There are two possible errors in which case a NULL is returned.
10437c478bd9Sstevel@tonic-gate  * One of two error codes are returned:
10447c478bd9Sstevel@tonic-gate  *
10457c478bd9Sstevel@tonic-gate  * NO_RECOVERY - a malloc failed or the like for which there's no recovery.
10467c478bd9Sstevel@tonic-gate  * NO_ADDRESS - after filtering all the v4, there was nothing left!
10477c478bd9Sstevel@tonic-gate  *
10487c478bd9Sstevel@tonic-gate  * Inputs:
10497c478bd9Sstevel@tonic-gate  *              he              pointer to hostent with mapped v4 addresses
10507c478bd9Sstevel@tonic-gate  *              filter_error    pointer to return error code
10517c478bd9Sstevel@tonic-gate  * Return:
10527c478bd9Sstevel@tonic-gate  *		pointer to a malloc'd hostent with v4 addresses.
10537c478bd9Sstevel@tonic-gate  *
10547c478bd9Sstevel@tonic-gate  * The results are packed into the res->buffer as follows:
10557c478bd9Sstevel@tonic-gate  * <--------------- buffer + buflen -------------------------------------->
10567c478bd9Sstevel@tonic-gate  * |-----------------|-----------------|----------------|----------------|
10577c478bd9Sstevel@tonic-gate  * | pointers vector | pointers vector | aliases grow   | addresses grow |
10587c478bd9Sstevel@tonic-gate  * | for addresses   | for aliases     |                |                |
10597c478bd9Sstevel@tonic-gate  * | this way ->     | this way ->     | <- this way    |<- this way     |
10607c478bd9Sstevel@tonic-gate  * |-----------------|-----------------|----------------|----------------|
10617c478bd9Sstevel@tonic-gate  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate struct hostent *
__mappedtov4(struct hostent * he,int * extract_error)10647c478bd9Sstevel@tonic-gate __mappedtov4(struct hostent *he, int *extract_error)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 	char	*buffer, *limit;
10677c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t *res;
10687c478bd9Sstevel@tonic-gate 	int	buflen = NSS_BUFLEN_HOSTS;
10697c478bd9Sstevel@tonic-gate 	struct	in_addr *addr4p;
10707c478bd9Sstevel@tonic-gate 	char	*buff_locp;
10717c478bd9Sstevel@tonic-gate 	struct	hostent *host;
10727c478bd9Sstevel@tonic-gate 	int	count = 0, len, i;
10737c478bd9Sstevel@tonic-gate 	char	*h_namep;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (he == NULL) {
10767c478bd9Sstevel@tonic-gate 		*extract_error = NO_ADDRESS;
10777c478bd9Sstevel@tonic-gate 		return (NULL);
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 	if ((__find_mapped(he, 0)) == 0) {
10807c478bd9Sstevel@tonic-gate 		*extract_error = NO_ADDRESS;
10817c478bd9Sstevel@tonic-gate 		return (NULL);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 	if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) {
10847c478bd9Sstevel@tonic-gate 		*extract_error = NO_RECOVERY;
10857c478bd9Sstevel@tonic-gate 		return (NULL);
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 	limit = res->buffer + buflen;
10887c478bd9Sstevel@tonic-gate 	host = (struct hostent *)res->result;
10897c478bd9Sstevel@tonic-gate 	buffer = res->buffer;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr));
10927c478bd9Sstevel@tonic-gate 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
10937c478bd9Sstevel@tonic-gate 	if ((char *)host->h_addr_list >= limit ||
10947c478bd9Sstevel@tonic-gate 	    buff_locp <= (char *)host->h_addr_list)
10957c478bd9Sstevel@tonic-gate 		goto cleanup;
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * "Unmap" the v4 mapped address(es) into a v4 hostent format.
10987c478bd9Sstevel@tonic-gate 	 * This is used for getipnodebyaddr() (single address) or for
10997c478bd9Sstevel@tonic-gate 	 * v4 mapped for getipnodebyname(), which could be multiple
11007c478bd9Sstevel@tonic-gate 	 * addresses. This could also be a literal address string,
11017c478bd9Sstevel@tonic-gate 	 * which is why there is a inet_addr() call.
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
110461961e0fSrobinson 		/* LINTED pointer cast */
11057c478bd9Sstevel@tonic-gate 		if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *)
11067c478bd9Sstevel@tonic-gate 		    he->h_addr_list[i]))
11077c478bd9Sstevel@tonic-gate 			continue;
11087c478bd9Sstevel@tonic-gate 		buff_locp -= sizeof (struct in6_addr);
11097c478bd9Sstevel@tonic-gate 		/*
11107c478bd9Sstevel@tonic-gate 		 * Has to be room for the pointer to the address we're
11117c478bd9Sstevel@tonic-gate 		 * about to add, as well as the final NULL ptr.
11127c478bd9Sstevel@tonic-gate 		 */
11137c478bd9Sstevel@tonic-gate 		if (buff_locp <=
11147c478bd9Sstevel@tonic-gate 		    (char *)&(host->h_addr_list[count + 1]))
11157c478bd9Sstevel@tonic-gate 			goto cleanup;
111661961e0fSrobinson 		/* LINTED pointer cast */
11177c478bd9Sstevel@tonic-gate 		addr4p = (struct in_addr *)buff_locp;
11187c478bd9Sstevel@tonic-gate 		host->h_addr_list[count] = (char *)addr4p;
11197c478bd9Sstevel@tonic-gate 		bzero((char *)&addr4p->s_addr,
11207c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
112161961e0fSrobinson 		/* LINTED pointer cast */
11227c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(
11235b79143cSGirish Moodalbail 		    (struct in6_addr *)he->h_addr_list[i], addr4p);
11247c478bd9Sstevel@tonic-gate 		++count;
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 	/*
11277c478bd9Sstevel@tonic-gate 	 * Set last array element to NULL and add cname as first alias
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	host->h_addr_list[count] = NULL;
11307c478bd9Sstevel@tonic-gate 	host->h_aliases = host->h_addr_list + count + 1;
11317c478bd9Sstevel@tonic-gate 	count = 0;
11327c478bd9Sstevel@tonic-gate 	/* Copy official host name */
11337c478bd9Sstevel@tonic-gate 	buff_locp -= (len = strlen(he->h_name) + 1);
11347c478bd9Sstevel@tonic-gate 	h_namep = he->h_name;
11357c478bd9Sstevel@tonic-gate 	if (buff_locp <= (char *)(host->h_aliases))
11367c478bd9Sstevel@tonic-gate 		goto cleanup;
11377c478bd9Sstevel@tonic-gate 	bcopy(h_namep, buff_locp, len);
11387c478bd9Sstevel@tonic-gate 	host->h_name = buff_locp;
11397c478bd9Sstevel@tonic-gate 	/*
11407c478bd9Sstevel@tonic-gate 	 * Pass 2 (IPv4 aliases):
11417c478bd9Sstevel@tonic-gate 	 */
11425b79143cSGirish Moodalbail 	if (he->h_aliases != NULL) {
11437c478bd9Sstevel@tonic-gate 		for (i = 0; he->h_aliases[i] != NULL; i++) {
11447c478bd9Sstevel@tonic-gate 			buff_locp -= (len = strlen(he->h_aliases[i]) + 1);
11457c478bd9Sstevel@tonic-gate 			/*
11467c478bd9Sstevel@tonic-gate 			 * Has to be room for the pointer to the address we're
11477c478bd9Sstevel@tonic-gate 			 * about to add, as well as the final NULL ptr.
11487c478bd9Sstevel@tonic-gate 			 */
11497c478bd9Sstevel@tonic-gate 			if (buff_locp <=
11507c478bd9Sstevel@tonic-gate 			    (char *)&(host->h_aliases[count + 1]))
11517c478bd9Sstevel@tonic-gate 				goto cleanup;
11527c478bd9Sstevel@tonic-gate 			host->h_aliases[count] = buff_locp;
11537c478bd9Sstevel@tonic-gate 			bcopy((char *)he->h_aliases[i], buff_locp, len);
11547c478bd9Sstevel@tonic-gate 			++count;
11557c478bd9Sstevel@tonic-gate 		}
11565b79143cSGirish Moodalbail 	}
11577c478bd9Sstevel@tonic-gate 	host->h_aliases[count] = NULL;
11587c478bd9Sstevel@tonic-gate 	host->h_length = sizeof (struct in_addr);
11597c478bd9Sstevel@tonic-gate 	host->h_addrtype = AF_INET;
11607c478bd9Sstevel@tonic-gate 	free(res);
11617c478bd9Sstevel@tonic-gate 	return (host);
11627c478bd9Sstevel@tonic-gate cleanup:
11637c478bd9Sstevel@tonic-gate 	*extract_error = NO_RECOVERY;
11647c478bd9Sstevel@tonic-gate 	(void) __IPv6_cleanup(res);
11657c478bd9Sstevel@tonic-gate 	return (NULL);
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate /*
11697c478bd9Sstevel@tonic-gate  * This routine takes as input a pointer to a hostent and filters out
11707c478bd9Sstevel@tonic-gate  * the type of addresses specified by the af argument.  AF_INET
11717c478bd9Sstevel@tonic-gate  * indicates that the caller wishes to filter out IPv4-mapped
11727c478bd9Sstevel@tonic-gate  * addresses, and AF_INET6 indicates that the caller wishes to filter
11737c478bd9Sstevel@tonic-gate  * out IPv6 addresses which aren't IPv4-mapped.  If filtering would
11747c478bd9Sstevel@tonic-gate  * result in all addresses being filtered out, a NULL pointer is returned.
11757c478bd9Sstevel@tonic-gate  * Otherwise, the he pointer passed in is returned, even if no addresses
11767c478bd9Sstevel@tonic-gate  * were filtered out.
11777c478bd9Sstevel@tonic-gate  */
11787c478bd9Sstevel@tonic-gate static struct hostent *
__filter_addresses(int af,struct hostent * he)11797c478bd9Sstevel@tonic-gate __filter_addresses(int af, struct hostent *he)
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate 	struct in6_addr	**in6addrlist, **in6addr;
11827c478bd9Sstevel@tonic-gate 	boolean_t	isipv4mapped;
11837c478bd9Sstevel@tonic-gate 	int		i = 0;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	if (he == NULL)
11867c478bd9Sstevel@tonic-gate 		return (NULL);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	in6addrlist = (struct in6_addr **)he->h_addr_list;
11897c478bd9Sstevel@tonic-gate 	for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
11907c478bd9Sstevel@tonic-gate 		isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		if ((af == AF_INET && !isipv4mapped) ||
11937c478bd9Sstevel@tonic-gate 		    (af == AF_INET6 && isipv4mapped)) {
11947c478bd9Sstevel@tonic-gate 			if (in6addrlist[i] != *in6addr)
11957c478bd9Sstevel@tonic-gate 				in6addrlist[i] = *in6addr;
11967c478bd9Sstevel@tonic-gate 			i++;
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	if (i == 0) {
12017c478bd9Sstevel@tonic-gate 		/* We filtered everything out. */
12027c478bd9Sstevel@tonic-gate 		return (NULL);
12037c478bd9Sstevel@tonic-gate 	} else {
12047c478bd9Sstevel@tonic-gate 		/* NULL terminate the list and return the hostent */
12057c478bd9Sstevel@tonic-gate 		in6addrlist[i] = NULL;
12067c478bd9Sstevel@tonic-gate 		return (he);
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate /*
12117c478bd9Sstevel@tonic-gate  * This routine searches a hostent for v4 mapped IPv6 addresses.
12127c478bd9Sstevel@tonic-gate  * he		hostent structure to seach
12137c478bd9Sstevel@tonic-gate  * find_both	flag indicating if only want mapped or both map'd and v6
12147c478bd9Sstevel@tonic-gate  * return values:
12157c478bd9Sstevel@tonic-gate  * 			0 = No mapped addresses
12167c478bd9Sstevel@tonic-gate  *			1 = Mapped v4 address found (returns on first one found)
12177c478bd9Sstevel@tonic-gate  *			2 = Both v6 and v4 mapped are present
12187c478bd9Sstevel@tonic-gate  *
12197c478bd9Sstevel@tonic-gate  * If hostent passed in with no addresses, zero will be returned.
12207c478bd9Sstevel@tonic-gate  */
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate static int
__find_mapped(struct hostent * he,int find_both)12237c478bd9Sstevel@tonic-gate __find_mapped(struct hostent *he, int find_both)
12247c478bd9Sstevel@tonic-gate {
12257c478bd9Sstevel@tonic-gate 	int i;
12267c478bd9Sstevel@tonic-gate 	int mapd_found = 0;
12277c478bd9Sstevel@tonic-gate 	int v6_found = 0;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
123061961e0fSrobinson 		/* LINTED pointer cast */
12317c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(
12327c478bd9Sstevel@tonic-gate 				(struct in6_addr *)he->h_addr_list[i])) {
12337c478bd9Sstevel@tonic-gate 			if (find_both)
12347c478bd9Sstevel@tonic-gate 				mapd_found = 1;
12357c478bd9Sstevel@tonic-gate 			else
12367c478bd9Sstevel@tonic-gate 				return (1);
12377c478bd9Sstevel@tonic-gate 		} else {
12387c478bd9Sstevel@tonic-gate 			v6_found = 1;
12397c478bd9Sstevel@tonic-gate 		}
12407c478bd9Sstevel@tonic-gate 		/* save some iterations once both found */
12417c478bd9Sstevel@tonic-gate 		if (mapd_found && v6_found)
12427c478bd9Sstevel@tonic-gate 			return (2);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	return (mapd_found);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate  * This routine was added specifically for the IPv6 getipnodeby*() APIs. This
12497c478bd9Sstevel@tonic-gate  * separates the result pointer (ptr to hostent+data buf) from the
12507c478bd9Sstevel@tonic-gate  * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr
12517c478bd9Sstevel@tonic-gate  * can be passed to freehostent() and freed independently.
12527c478bd9Sstevel@tonic-gate  *
12537c478bd9Sstevel@tonic-gate  *   bufp->result    bufp->buffer
12547c478bd9Sstevel@tonic-gate  *		|		|
12557c478bd9Sstevel@tonic-gate  *		V		V
12567c478bd9Sstevel@tonic-gate  *		------------------------------------------------...--
12577c478bd9Sstevel@tonic-gate  *		|struct hostent	|addresses		     aliases |
12587c478bd9Sstevel@tonic-gate  *		------------------------------------------------...--
12597c478bd9Sstevel@tonic-gate  *		|               |<--------bufp->buflen-------------->|
12607c478bd9Sstevel@tonic-gate  */
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate #define	ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1))
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *
__IPv6_alloc(int bufsz)12657c478bd9Sstevel@tonic-gate __IPv6_alloc(int bufsz)
12667c478bd9Sstevel@tonic-gate {
12677c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t *bufp;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL)
12707c478bd9Sstevel@tonic-gate 		return (NULL);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) ==
12737c478bd9Sstevel@tonic-gate 	    NULL) {
12747c478bd9Sstevel@tonic-gate 		free(bufp);
12757c478bd9Sstevel@tonic-gate 		return (NULL);
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 	bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent);
12787c478bd9Sstevel@tonic-gate 	bufp->buflen = bufsz;
12797c478bd9Sstevel@tonic-gate 	return (bufp);
12807c478bd9Sstevel@tonic-gate }
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate /*
12837c478bd9Sstevel@tonic-gate  * This routine is use only for error return cleanup. This will free the
12847c478bd9Sstevel@tonic-gate  * hostent pointer, so don't use for successful returns.
12857c478bd9Sstevel@tonic-gate  */
12867c478bd9Sstevel@tonic-gate static void
__IPv6_cleanup(nss_XbyY_buf_t * bufp)12877c478bd9Sstevel@tonic-gate __IPv6_cleanup(nss_XbyY_buf_t *bufp)
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate 	if (bufp == NULL)
12907c478bd9Sstevel@tonic-gate 		return;
12917c478bd9Sstevel@tonic-gate 	if (bufp->result != NULL)
12927c478bd9Sstevel@tonic-gate 		free(bufp->result);
12937c478bd9Sstevel@tonic-gate 	free(bufp);
12947c478bd9Sstevel@tonic-gate }
1295