17c478bd9Sstevel@tonic-gate /*
2*20d217c8SGirish Moodalbail  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
107c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
117c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
147c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
167c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
177c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
187c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
197c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
207c478bd9Sstevel@tonic-gate  * SOFTWARE.
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
247c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: gethostent.c,v 1.34 2003/05/29 00:05:18 marka Exp $";
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /* Imports */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "port_before.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <net/if.h>
397c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
407c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <netdb.h>
467c478bd9Sstevel@tonic-gate #include <resolv.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <irs.h>
527c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "port_after.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include "irs_p.h"
577c478bd9Sstevel@tonic-gate #include "irs_data.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Definitions */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct pvt {
627c478bd9Sstevel@tonic-gate 	char *		aliases[1];
637c478bd9Sstevel@tonic-gate 	char *		addrs[2];
647c478bd9Sstevel@tonic-gate 	char		addr[NS_IN6ADDRSZ];
657c478bd9Sstevel@tonic-gate 	char		name[NS_MAXDNAME + 1];
667c478bd9Sstevel@tonic-gate 	struct hostent	host;
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* Forward */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static struct net_data *init(void);
727c478bd9Sstevel@tonic-gate static void		freepvt(struct net_data *);
737c478bd9Sstevel@tonic-gate static struct hostent  *fakeaddr(const char *, int, struct net_data *);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef	SUNW_OVERRIDE_RETRY
767c478bd9Sstevel@tonic-gate extern int	__res_retry(int);
777c478bd9Sstevel@tonic-gate extern int	__res_retry_reset(void);
787c478bd9Sstevel@tonic-gate #endif	/* SUNW_OVERRIDE_RETRY */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* Public */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate struct hostent *
837c478bd9Sstevel@tonic-gate gethostbyname(const char *name) {
847c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	return (gethostbyname_p(name, net_data));
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate struct hostent *
907c478bd9Sstevel@tonic-gate gethostbyname2(const char *name, int af) {
917c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	return (gethostbyname2_p(name, af, net_data));
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate struct hostent *
977c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE
987c478bd9Sstevel@tonic-gate gethostbyaddr(const char *addr, int len, int af) {
997c478bd9Sstevel@tonic-gate #else
1007c478bd9Sstevel@tonic-gate gethostbyaddr(const void *addr, socklen_t len, int af) {
1017c478bd9Sstevel@tonic-gate #endif /* ORIGINAL_ISC_CODE */
1027c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	return (gethostbyaddr_p(addr, len, af, net_data));
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate struct hostent *
1087c478bd9Sstevel@tonic-gate gethostent() {
1097c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	return (gethostent_p(net_data));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
1157c478bd9Sstevel@tonic-gate void
1167c478bd9Sstevel@tonic-gate #else
1177c478bd9Sstevel@tonic-gate int
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate sethostent(int stayopen) {
1207c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
1217c478bd9Sstevel@tonic-gate 	sethostent_p(stayopen, net_data);
1227c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
1237c478bd9Sstevel@tonic-gate #else
1247c478bd9Sstevel@tonic-gate 	return (0);
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
1307c478bd9Sstevel@tonic-gate void
1317c478bd9Sstevel@tonic-gate #else
1327c478bd9Sstevel@tonic-gate int
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate endhostent() {
1357c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
1367c478bd9Sstevel@tonic-gate 	endhostent_p(net_data);
1377c478bd9Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
1387c478bd9Sstevel@tonic-gate #else
1397c478bd9Sstevel@tonic-gate 	return (0);
1407c478bd9Sstevel@tonic-gate #endif
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* Shared private. */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate struct hostent *
1467c478bd9Sstevel@tonic-gate gethostbyname_p(const char *name, struct net_data *net_data) {
1477c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (!net_data)
1507c478bd9Sstevel@tonic-gate #ifdef	SUNW_SETHERRNO
1517c478bd9Sstevel@tonic-gate 	{
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 		*  Should set the context h_errno, but since net_data
1547c478bd9Sstevel@tonic-gate 		 * is NULL, we don't have a context.
1557c478bd9Sstevel@tonic-gate 		 */
1567c478bd9Sstevel@tonic-gate 		h_errno = NETDB_INTERNAL;
1577c478bd9Sstevel@tonic-gate 		return (NULL);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate #else
1607c478bd9Sstevel@tonic-gate 		return (NULL);
1617c478bd9Sstevel@tonic-gate #endif	/* SUNW_SETHERRNO */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (net_data->res->options & RES_USE_INET6) {
1647c478bd9Sstevel@tonic-gate 		hp = gethostbyname2_p(name, AF_INET6, net_data);
1657c478bd9Sstevel@tonic-gate 		if (hp)
1667c478bd9Sstevel@tonic-gate 			return (hp);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	return (gethostbyname2_p(name, AF_INET, net_data));
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate struct hostent *
1727c478bd9Sstevel@tonic-gate gethostbyname2_p(const char *name, int af, struct net_data *net_data) {
1737c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
1747c478bd9Sstevel@tonic-gate 	char tmp[NS_MAXDNAME];
1757c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1767c478bd9Sstevel@tonic-gate 	const char *cp;
1777c478bd9Sstevel@tonic-gate 	char **hap;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (!net_data || !(ho = net_data->ho))
1807c478bd9Sstevel@tonic-gate #ifdef	SUNW_SETHERRNO
1817c478bd9Sstevel@tonic-gate 	{
1827c478bd9Sstevel@tonic-gate 		h_errno = NETDB_INTERNAL;
1837c478bd9Sstevel@tonic-gate 		return (NULL);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate #else
1867c478bd9Sstevel@tonic-gate 		return (NULL);
1877c478bd9Sstevel@tonic-gate #endif	/* SUNW_SETHERRNO */
1887c478bd9Sstevel@tonic-gate 	if (net_data->ho_stayopen && net_data->ho_last &&
1897c478bd9Sstevel@tonic-gate 	net_data->ho_last->h_addrtype == af) {
1907c478bd9Sstevel@tonic-gate 		if (ns_samename(name, net_data->ho_last->h_name) == 1)
1917c478bd9Sstevel@tonic-gate 			return (net_data->ho_last);
1927c478bd9Sstevel@tonic-gate 		for (hap = net_data->ho_last->h_aliases; hap && *hap; hap++)
1937c478bd9Sstevel@tonic-gate 			if (ns_samename(name, *hap) == 1)
1947c478bd9Sstevel@tonic-gate 				return (net_data->ho_last);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	if (!strchr(name, '.') && (cp = res_hostalias(net_data->res, name,
1977c478bd9Sstevel@tonic-gate 						      tmp, sizeof tmp)))
1987c478bd9Sstevel@tonic-gate 		name = cp;
1997c478bd9Sstevel@tonic-gate 	if ((hp = fakeaddr(name, af, net_data)) != NULL)
2007c478bd9Sstevel@tonic-gate 		return (hp);
2017c478bd9Sstevel@tonic-gate #ifdef	SUNW_OVERRIDE_RETRY
2027c478bd9Sstevel@tonic-gate 	net_data->res->retry = __res_retry(net_data->res->retry);
2037c478bd9Sstevel@tonic-gate #endif	/* SUNW_OVERRIDE_RETRY */
2047c478bd9Sstevel@tonic-gate 	net_data->ho_last = (*ho->byname2)(ho, name, af);
2057c478bd9Sstevel@tonic-gate #ifdef	SUNW_OVERRIDE_RETRY
2067c478bd9Sstevel@tonic-gate 	net_data->res->retry = __res_retry_reset();
2077c478bd9Sstevel@tonic-gate #endif	/* SUNW_OVERRIDE_RETRY */
2087c478bd9Sstevel@tonic-gate 	if (!net_data->ho_stayopen)
2097c478bd9Sstevel@tonic-gate 		endhostent();
2107c478bd9Sstevel@tonic-gate 	return (net_data->ho_last);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate struct hostent *
2147c478bd9Sstevel@tonic-gate gethostbyaddr_p(const char *addr, int len, int af, struct net_data *net_data) {
2157c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2167c478bd9Sstevel@tonic-gate 	char **hap;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (!net_data || !(ho = net_data->ho))
2197c478bd9Sstevel@tonic-gate #ifdef	SUNW_SETHERRNO
2207c478bd9Sstevel@tonic-gate 	{
2217c478bd9Sstevel@tonic-gate 		h_errno = NETDB_INTERNAL;
2227c478bd9Sstevel@tonic-gate 		return (NULL);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate #else
2257c478bd9Sstevel@tonic-gate 		return (NULL);
2267c478bd9Sstevel@tonic-gate #endif	/* SUNW_SETHERRNO */
2277c478bd9Sstevel@tonic-gate 	if (net_data->ho_stayopen && net_data->ho_last &&
2287c478bd9Sstevel@tonic-gate 	    net_data->ho_last->h_length == len)
2297c478bd9Sstevel@tonic-gate 		for (hap = net_data->ho_last->h_addr_list;
2307c478bd9Sstevel@tonic-gate 		     hap && *hap;
2317c478bd9Sstevel@tonic-gate 		     hap++)
2327c478bd9Sstevel@tonic-gate 			if (!memcmp(addr, *hap, len))
2337c478bd9Sstevel@tonic-gate 				return (net_data->ho_last);
2347c478bd9Sstevel@tonic-gate 	net_data->ho_last = (*ho->byaddr)(ho, addr, len, af);
2357c478bd9Sstevel@tonic-gate 	if (!net_data->ho_stayopen)
2367c478bd9Sstevel@tonic-gate 		endhostent();
2377c478bd9Sstevel@tonic-gate 	return (net_data->ho_last);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate struct hostent *
2427c478bd9Sstevel@tonic-gate gethostent_p(struct net_data *net_data) {
2437c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2447c478bd9Sstevel@tonic-gate 	struct hostent *hp;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (!net_data || !(ho = net_data->ho))
2477c478bd9Sstevel@tonic-gate #ifdef	SUNW_SETHERRNO
2487c478bd9Sstevel@tonic-gate 	{
2497c478bd9Sstevel@tonic-gate 		h_errno = NETDB_INTERNAL;
2507c478bd9Sstevel@tonic-gate 		return (NULL);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate #else
2537c478bd9Sstevel@tonic-gate 		return (NULL);
2547c478bd9Sstevel@tonic-gate #endif	/* SUNW_SETHERRNO */
2557c478bd9Sstevel@tonic-gate 	while ((hp = (*ho->next)(ho)) != NULL &&
2567c478bd9Sstevel@tonic-gate 	       hp->h_addrtype == AF_INET6 &&
2577c478bd9Sstevel@tonic-gate 	       (net_data->res->options & RES_USE_INET6) == 0)
2587c478bd9Sstevel@tonic-gate 		continue;
2597c478bd9Sstevel@tonic-gate 	net_data->ho_last = hp;
2607c478bd9Sstevel@tonic-gate 	return (net_data->ho_last);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate void
2657c478bd9Sstevel@tonic-gate sethostent_p(int stayopen, struct net_data *net_data) {
2667c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (!net_data || !(ho = net_data->ho))
2697c478bd9Sstevel@tonic-gate 		return;
2707c478bd9Sstevel@tonic-gate 	freepvt(net_data);
2717c478bd9Sstevel@tonic-gate 	(*ho->rewind)(ho);
2727c478bd9Sstevel@tonic-gate 	net_data->ho_stayopen = (stayopen != 0);
2737c478bd9Sstevel@tonic-gate 	if (stayopen == 0)
2747c478bd9Sstevel@tonic-gate 		net_data_minimize(net_data);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate void
2787c478bd9Sstevel@tonic-gate endhostent_p(struct net_data *net_data) {
2797c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if ((net_data != NULL) && ((ho = net_data->ho) != NULL))
2827c478bd9Sstevel@tonic-gate 		(*ho->minimize)(ho);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate #ifndef IN6_IS_ADDR_V4COMPAT
2867c478bd9Sstevel@tonic-gate static const unsigned char in6addr_compat[12] = {
2877c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2887c478bd9Sstevel@tonic-gate #define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \
2897c478bd9Sstevel@tonic-gate 				 ((x)->s6_addr[12] != 0 || \
2907c478bd9Sstevel@tonic-gate 				  (x)->s6_addr[13] != 0 || \
2917c478bd9Sstevel@tonic-gate 				  (x)->s6_addr[14] != 0 || \
2927c478bd9Sstevel@tonic-gate 				   ((x)->s6_addr[15] != 0 && \
2937c478bd9Sstevel@tonic-gate 				    (x)->s6_addr[15] != 1)))
2947c478bd9Sstevel@tonic-gate #endif
2957c478bd9Sstevel@tonic-gate #ifndef IN6_IS_ADDR_V4MAPPED
2967c478bd9Sstevel@tonic-gate #define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12))
2977c478bd9Sstevel@tonic-gate #endif
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static const unsigned char in6addr_mapped[12] = {
3007c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0xff, 0xff };
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static int scan_interfaces(int *, int *);
3037c478bd9Sstevel@tonic-gate static struct hostent *copyandmerge(struct hostent *, struct hostent *, int, int *);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  *	Public functions
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  *	AI_V4MAPPED + AF_INET6
3117c478bd9Sstevel@tonic-gate  *	If no IPv6 address then a query for IPv4 and map returned values.
3127c478bd9Sstevel@tonic-gate  *
3137c478bd9Sstevel@tonic-gate  *	AI_ALL + AI_V4MAPPED + AF_INET6
3147c478bd9Sstevel@tonic-gate  *	Return IPv6 and IPv4 mapped.
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  *	AI_ADDRCONFIG
3177c478bd9Sstevel@tonic-gate  *	Only return IPv6 / IPv4 address if there is an interface of that
3187c478bd9Sstevel@tonic-gate  *	type active.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate struct hostent *
3227c478bd9Sstevel@tonic-gate getipnodebyname(const char *name, int af, int flags, int *error_num) {
3237c478bd9Sstevel@tonic-gate 	int have_v4 = 1, have_v6 = 1;
3247c478bd9Sstevel@tonic-gate 	struct in_addr in4;
3257c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
3267c478bd9Sstevel@tonic-gate 	struct hostent he, *he1 = NULL, *he2 = NULL, *he3;
3277c478bd9Sstevel@tonic-gate 	int v4 = 0, v6 = 0;
3287c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
3297c478bd9Sstevel@tonic-gate 	u_long options;
3307c478bd9Sstevel@tonic-gate 	int tmp_err;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (net_data == NULL) {
3337c478bd9Sstevel@tonic-gate 		*error_num = NO_RECOVERY;
3347c478bd9Sstevel@tonic-gate 		return (NULL);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* If we care about active interfaces then check. */
3387c478bd9Sstevel@tonic-gate 	if ((flags & AI_ADDRCONFIG) != 0)
3397c478bd9Sstevel@tonic-gate 		if (scan_interfaces(&have_v4, &have_v6) == -1) {
3407c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
3417c478bd9Sstevel@tonic-gate 			return (NULL);
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* Check for literal address. */
3457c478bd9Sstevel@tonic-gate 	if ((v4 = inet_pton(AF_INET, name, &in4)) != 1)
3467c478bd9Sstevel@tonic-gate 		v6 = inet_pton(AF_INET6, name, &in6);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* Impossible combination? */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) ||
3517c478bd9Sstevel@tonic-gate 	    (af == AF_INET && v6 == 1) ||
3527c478bd9Sstevel@tonic-gate 	    (have_v4 == 0 && v4 == 1) ||
3537c478bd9Sstevel@tonic-gate 	    (have_v6 == 0 && v6 == 1) ||
3547c478bd9Sstevel@tonic-gate 	    (have_v4 == 0 && af == AF_INET) ||
3557c478bd9Sstevel@tonic-gate 	    (have_v6 == 0 && af == AF_INET6)) {
3567c478bd9Sstevel@tonic-gate 		*error_num = HOST_NOT_FOUND;
3577c478bd9Sstevel@tonic-gate 		return (NULL);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* Literal address? */
3617c478bd9Sstevel@tonic-gate 	if (v4 == 1 || v6 == 1) {
3627c478bd9Sstevel@tonic-gate 		char *addr_list[2];
3637c478bd9Sstevel@tonic-gate 		char *aliases[1];
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		DE_CONST(name, he.h_name);
3667c478bd9Sstevel@tonic-gate 		he.h_addr_list = addr_list;
3677c478bd9Sstevel@tonic-gate 		he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6;
3687c478bd9Sstevel@tonic-gate 		he.h_addr_list[1] = NULL;
3697c478bd9Sstevel@tonic-gate 		he.h_aliases = aliases;
3707c478bd9Sstevel@tonic-gate 		he.h_aliases[0] = NULL;
3717c478bd9Sstevel@tonic-gate 		he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ;
3727c478bd9Sstevel@tonic-gate 		he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6;
3737c478bd9Sstevel@tonic-gate 		return (copyandmerge(&he, NULL, af, error_num));
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	options = net_data->res->options;
3777c478bd9Sstevel@tonic-gate 	net_data->res->options &= ~RES_USE_INET6;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	tmp_err = NO_RECOVERY;
3807c478bd9Sstevel@tonic-gate 	if (have_v6 && af == AF_INET6) {
3817c478bd9Sstevel@tonic-gate 		he2 = gethostbyname2_p(name, AF_INET6, net_data);
3827c478bd9Sstevel@tonic-gate 		if (he2 != NULL) {
3837c478bd9Sstevel@tonic-gate 			he1 = copyandmerge(he2, NULL, af, error_num);
3847c478bd9Sstevel@tonic-gate 			if (he1 == NULL)
3857c478bd9Sstevel@tonic-gate 				return (NULL);
3867c478bd9Sstevel@tonic-gate 			he2 = NULL;
3877c478bd9Sstevel@tonic-gate 		} else {
3887c478bd9Sstevel@tonic-gate 			tmp_err = net_data->res->res_h_errno;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (have_v4 &&
3937c478bd9Sstevel@tonic-gate 	    ((af == AF_INET) ||
3947c478bd9Sstevel@tonic-gate 	     (af == AF_INET6 && (flags & AI_V4MAPPED) != 0 &&
3957c478bd9Sstevel@tonic-gate 	      (he1 == NULL || (flags & AI_ALL) != 0)))) {
3967c478bd9Sstevel@tonic-gate 		he2 = gethostbyname2_p(name, AF_INET, net_data);
3977c478bd9Sstevel@tonic-gate 		if (he1 == NULL && he2 == NULL) {
3987c478bd9Sstevel@tonic-gate 			*error_num = net_data->res->res_h_errno;
3997c478bd9Sstevel@tonic-gate 			return (NULL);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	} else
4027c478bd9Sstevel@tonic-gate 		*error_num = tmp_err;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	net_data->res->options = options;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	he3 = copyandmerge(he1, he2, af, error_num);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (he1 != NULL)
4097c478bd9Sstevel@tonic-gate 		freehostent(he1);
4107c478bd9Sstevel@tonic-gate 	return (he3);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate struct hostent *
4147c478bd9Sstevel@tonic-gate getipnodebyaddr(const void *src, size_t len, int af, int *error_num) {
4157c478bd9Sstevel@tonic-gate 	struct hostent *he1, *he2;
4167c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/* Sanity Checks. */
419*20d217c8SGirish Moodalbail 	if (src == NULL || net_data == NULL) {
4207c478bd9Sstevel@tonic-gate 		*error_num = NO_RECOVERY;
4217c478bd9Sstevel@tonic-gate 		return (NULL);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	switch (af) {
4257c478bd9Sstevel@tonic-gate 	case AF_INET:
4267c478bd9Sstevel@tonic-gate 		if (len != INADDRSZ) {
4277c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
4287c478bd9Sstevel@tonic-gate 			return (NULL);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		break;
4317c478bd9Sstevel@tonic-gate 	case AF_INET6:
4327c478bd9Sstevel@tonic-gate 		if (len != IN6ADDRSZ) {
4337c478bd9Sstevel@tonic-gate 			*error_num = NO_RECOVERY;
4347c478bd9Sstevel@tonic-gate 			return (NULL);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		break;
4377c478bd9Sstevel@tonic-gate 	default:
4387c478bd9Sstevel@tonic-gate 		*error_num = NO_RECOVERY;
4397c478bd9Sstevel@tonic-gate 		return (NULL);
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Lookup IPv4 and IPv4 mapped/compatible addresses
4447c478bd9Sstevel@tonic-gate 	 */
4457c478bd9Sstevel@tonic-gate 	if ((af == AF_INET6 &&
4467c478bd9Sstevel@tonic-gate 	     IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)src)) ||
4477c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
4487c478bd9Sstevel@tonic-gate 	     IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)src)) ||
4497c478bd9Sstevel@tonic-gate 	    (af == AF_INET)) {
4507c478bd9Sstevel@tonic-gate 		const char *cp = src;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		if (af == AF_INET6)
4537c478bd9Sstevel@tonic-gate 			cp += 12;
4547c478bd9Sstevel@tonic-gate 		he1 = gethostbyaddr_p(cp, 4, AF_INET, net_data);
4557c478bd9Sstevel@tonic-gate 		if (he1 == NULL) {
4567c478bd9Sstevel@tonic-gate 			*error_num = net_data->res->res_h_errno;
4577c478bd9Sstevel@tonic-gate 			return (NULL);
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		he2 = copyandmerge(he1, NULL, af, error_num);
4607c478bd9Sstevel@tonic-gate 		if (he2 == NULL)
4617c478bd9Sstevel@tonic-gate 			return (NULL);
4627c478bd9Sstevel@tonic-gate 		/*
4637c478bd9Sstevel@tonic-gate 		 * Restore original address if mapped/compatible.
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		if (af == AF_INET6)
4667c478bd9Sstevel@tonic-gate 			memcpy(he1->h_addr, src, len);
4677c478bd9Sstevel@tonic-gate 		return (he2);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Lookup IPv6 address.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	if (memcmp((const struct in6_addr *)src, &in6addr_any, 16) == 0) {
4747c478bd9Sstevel@tonic-gate 		*error_num = HOST_NOT_FOUND;
4757c478bd9Sstevel@tonic-gate 		return (NULL);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	he1 = gethostbyaddr_p(src, 16, AF_INET6, net_data);
4797c478bd9Sstevel@tonic-gate 	if (he1 == NULL) {
4807c478bd9Sstevel@tonic-gate 		*error_num = net_data->res->res_h_errno;
4817c478bd9Sstevel@tonic-gate 		return (NULL);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	return (copyandmerge(he1, NULL, af, error_num));
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate void
4877c478bd9Sstevel@tonic-gate freehostent(struct hostent *he) {
4887c478bd9Sstevel@tonic-gate 	char **cpp;
4897c478bd9Sstevel@tonic-gate 	int names = 1;
4907c478bd9Sstevel@tonic-gate 	int addresses = 1;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	memput(he->h_name, strlen(he->h_name) + 1);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	cpp = he->h_addr_list;
4957c478bd9Sstevel@tonic-gate 	while (*cpp != NULL) {
4967c478bd9Sstevel@tonic-gate 		memput(*cpp, (he->h_addrtype == AF_INET) ?
4977c478bd9Sstevel@tonic-gate 			     INADDRSZ : IN6ADDRSZ);
4987c478bd9Sstevel@tonic-gate 		*cpp = NULL;
4997c478bd9Sstevel@tonic-gate 		cpp++;
5007c478bd9Sstevel@tonic-gate 		addresses++;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	cpp = he->h_aliases;
5047c478bd9Sstevel@tonic-gate 	while (*cpp != NULL) {
5057c478bd9Sstevel@tonic-gate 		memput(*cpp, strlen(*cpp) + 1);
5067c478bd9Sstevel@tonic-gate 		cpp++;
5077c478bd9Sstevel@tonic-gate 		names++;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	memput(he->h_aliases, sizeof(char *) * (names));
5117c478bd9Sstevel@tonic-gate 	memput(he->h_addr_list, sizeof(char *) * (addresses));
5127c478bd9Sstevel@tonic-gate 	memput(he, sizeof *he);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate  * Private
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * Scan the interface table and set have_v4 and have_v6 depending
5217c478bd9Sstevel@tonic-gate  * upon whether there are IPv4 and IPv6 interface addresses.
5227c478bd9Sstevel@tonic-gate  *
5237c478bd9Sstevel@tonic-gate  * Returns:
5247c478bd9Sstevel@tonic-gate  *	0 on success
5257c478bd9Sstevel@tonic-gate  *	-1 on failure.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
5297c478bd9Sstevel@tonic-gate     !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate #define SETFAMILYFLAGS
5327c478bd9Sstevel@tonic-gate #define LIFCONF lifconf
5337c478bd9Sstevel@tonic-gate #define LIFREQ lifreq
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate static void
5367c478bd9Sstevel@tonic-gate scan_interfaces6(int *have_v4, int *have_v6) {
5377c478bd9Sstevel@tonic-gate 	struct LIFCONF lifc;
5387c478bd9Sstevel@tonic-gate 	struct LIFREQ lifreq;
5397c478bd9Sstevel@tonic-gate 	struct in_addr in4;
5407c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
5417c478bd9Sstevel@tonic-gate 	char *buf = NULL, *cp, *cplim;
5427c478bd9Sstevel@tonic-gate 	static unsigned int bufsiz = 4095;
5437c478bd9Sstevel@tonic-gate 	int s, cpsize, n;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* Get interface list from system. */
5467c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
5477c478bd9Sstevel@tonic-gate 		goto cleanup;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * Grow buffer until large enough to contain all interface
5517c478bd9Sstevel@tonic-gate 	 * descriptions.
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	for (;;) {
5547c478bd9Sstevel@tonic-gate 		buf = memget(bufsiz);
5557c478bd9Sstevel@tonic-gate 		if (buf == NULL)
5567c478bd9Sstevel@tonic-gate 			goto cleanup;
5577c478bd9Sstevel@tonic-gate #ifdef SETFAMILYFLAGS
5587c478bd9Sstevel@tonic-gate 		lifc.lifc_family = AF_UNSPEC;	/* request all families */
5597c478bd9Sstevel@tonic-gate 		lifc.lifc_flags = 0;
5607c478bd9Sstevel@tonic-gate #endif
5617c478bd9Sstevel@tonic-gate 		lifc.lifc_len = bufsiz;
5627c478bd9Sstevel@tonic-gate 		lifc.lifc_buf = buf;
5637c478bd9Sstevel@tonic-gate 		if ((n = ioctl(s, SIOCGLIFCONF, (char *)&lifc)) != -1) {
5647c478bd9Sstevel@tonic-gate 			/*
5657c478bd9Sstevel@tonic-gate 			 * Some OS's just return what will fit rather
5667c478bd9Sstevel@tonic-gate 			 * than set EINVAL if the buffer is too small
5677c478bd9Sstevel@tonic-gate 			 * to fit all the interfaces in.  If
5687c478bd9Sstevel@tonic-gate 			 * lifc.lifc_len is too near to the end of the
5697c478bd9Sstevel@tonic-gate 			 * buffer we will grow it just in case and
5707c478bd9Sstevel@tonic-gate 			 * retry.
5717c478bd9Sstevel@tonic-gate 			 */
5727c478bd9Sstevel@tonic-gate 			if (lifc.lifc_len + 2 * sizeof(lifreq) < bufsiz)
5737c478bd9Sstevel@tonic-gate 				break;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		if ((n == -1) && errno != EINVAL)
5767c478bd9Sstevel@tonic-gate 			goto cleanup;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		if (bufsiz > 1000000)
5797c478bd9Sstevel@tonic-gate 			goto cleanup;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		memput(buf, bufsiz);
5827c478bd9Sstevel@tonic-gate 		bufsiz += 4096;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/* Parse system's interface list. */
5867c478bd9Sstevel@tonic-gate 	cplim = buf + lifc.lifc_len;    /* skip over if's with big ifr_addr's */
5877c478bd9Sstevel@tonic-gate 	for (cp = buf;
5887c478bd9Sstevel@tonic-gate 	     (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
5897c478bd9Sstevel@tonic-gate 	     cp += cpsize) {
5907c478bd9Sstevel@tonic-gate 		memcpy(&lifreq, cp, sizeof lifreq);
5917c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
5927c478bd9Sstevel@tonic-gate #ifdef FIX_ZERO_SA_LEN
5937c478bd9Sstevel@tonic-gate 		if (lifreq.lifr_addr.sa_len == 0)
5947c478bd9Sstevel@tonic-gate 			lifreq.lifr_addr.sa_len = 16;
5957c478bd9Sstevel@tonic-gate #endif
5967c478bd9Sstevel@tonic-gate #ifdef HAVE_MINIMUM_IFREQ
5977c478bd9Sstevel@tonic-gate 		cpsize = sizeof lifreq;
5987c478bd9Sstevel@tonic-gate 		if (lifreq.lifr_addr.sa_len > sizeof (struct sockaddr))
5997c478bd9Sstevel@tonic-gate 			cpsize += (int)lifreq.lifr_addr.sa_len -
6007c478bd9Sstevel@tonic-gate 				(int)(sizeof (struct sockaddr));
6017c478bd9Sstevel@tonic-gate #else
6027c478bd9Sstevel@tonic-gate 		cpsize = sizeof lifreq.lifr_name + lifreq.lifr_addr.sa_len;
6037c478bd9Sstevel@tonic-gate #endif /* HAVE_MINIMUM_IFREQ */
6047c478bd9Sstevel@tonic-gate #elif defined SIOCGIFCONF_ADDR
6057c478bd9Sstevel@tonic-gate 		cpsize = sizeof lifreq;
6067c478bd9Sstevel@tonic-gate #else
6077c478bd9Sstevel@tonic-gate 		cpsize = sizeof lifreq.lifr_name;
6087c478bd9Sstevel@tonic-gate 		/* XXX maybe this should be a hard error? */
6097c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (char *)&lifreq) < 0)
6107c478bd9Sstevel@tonic-gate 			continue;
6117c478bd9Sstevel@tonic-gate #endif
6127c478bd9Sstevel@tonic-gate 		switch (lifreq.lifr_addr.ss_family) {
6137c478bd9Sstevel@tonic-gate 		case AF_INET:
6147c478bd9Sstevel@tonic-gate 			if (*have_v4 == 0) {
6157c478bd9Sstevel@tonic-gate 				memcpy(&in4,
6167c478bd9Sstevel@tonic-gate 				       &((struct sockaddr_in *)
6177c478bd9Sstevel@tonic-gate 				       &lifreq.lifr_addr)->sin_addr,
6187c478bd9Sstevel@tonic-gate 				       sizeof in4);
6197c478bd9Sstevel@tonic-gate 				if (in4.s_addr == INADDR_ANY)
6207c478bd9Sstevel@tonic-gate 					break;
6217c478bd9Sstevel@tonic-gate 				n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
6227c478bd9Sstevel@tonic-gate 				if (n < 0)
6237c478bd9Sstevel@tonic-gate 					break;
6247c478bd9Sstevel@tonic-gate 				if ((lifreq.lifr_flags & IFF_UP) == 0)
6257c478bd9Sstevel@tonic-gate 					break;
6267c478bd9Sstevel@tonic-gate 				*have_v4 = 1;
6277c478bd9Sstevel@tonic-gate 			}
6287c478bd9Sstevel@tonic-gate 			break;
6297c478bd9Sstevel@tonic-gate 		case AF_INET6:
6307c478bd9Sstevel@tonic-gate 			if (*have_v6 == 0) {
6317c478bd9Sstevel@tonic-gate 				memcpy(&in6,
6327c478bd9Sstevel@tonic-gate 				       &((struct sockaddr_in6 *)
6337c478bd9Sstevel@tonic-gate 				       &lifreq.lifr_addr)->sin6_addr, sizeof in6);
6347c478bd9Sstevel@tonic-gate 				if (memcmp(&in6, &in6addr_any, sizeof in6) == 0)
6357c478bd9Sstevel@tonic-gate 					break;
6367c478bd9Sstevel@tonic-gate 				n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
6377c478bd9Sstevel@tonic-gate 				if (n < 0)
6387c478bd9Sstevel@tonic-gate 					break;
6397c478bd9Sstevel@tonic-gate 				if ((lifreq.lifr_flags & IFF_UP) == 0)
6407c478bd9Sstevel@tonic-gate 					break;
6417c478bd9Sstevel@tonic-gate 				*have_v6 = 1;
6427c478bd9Sstevel@tonic-gate 			}
6437c478bd9Sstevel@tonic-gate 			break;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 	if (buf != NULL)
6477c478bd9Sstevel@tonic-gate 		memput(buf, bufsiz);
6487c478bd9Sstevel@tonic-gate 	close(s);
6497c478bd9Sstevel@tonic-gate 	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
6507c478bd9Sstevel@tonic-gate 	return;
6517c478bd9Sstevel@tonic-gate  cleanup:
6527c478bd9Sstevel@tonic-gate 	if (buf != NULL)
6537c478bd9Sstevel@tonic-gate 		memput(buf, bufsiz);
6547c478bd9Sstevel@tonic-gate 	if (s != -1)
6557c478bd9Sstevel@tonic-gate 		close(s);
6567c478bd9Sstevel@tonic-gate 	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
6577c478bd9Sstevel@tonic-gate 	return;
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate #endif
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate static int
6627c478bd9Sstevel@tonic-gate scan_interfaces(int *have_v4, int *have_v6) {
6637c478bd9Sstevel@tonic-gate 	struct ifconf ifc;
6647c478bd9Sstevel@tonic-gate 	union {
6657c478bd9Sstevel@tonic-gate 		char _pad[256];		/* leave space for IPv6 addresses */
6667c478bd9Sstevel@tonic-gate 		struct ifreq ifreq;
6677c478bd9Sstevel@tonic-gate 	} u;
6687c478bd9Sstevel@tonic-gate 	struct in_addr in4;
6697c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
6707c478bd9Sstevel@tonic-gate 	char *buf = NULL, *cp, *cplim;
6717c478bd9Sstevel@tonic-gate 	static unsigned int bufsiz = 4095;
6727c478bd9Sstevel@tonic-gate 	int s, n;
6737c478bd9Sstevel@tonic-gate 	size_t cpsize;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	/* Set to zero.  Used as loop terminators below. */
6767c478bd9Sstevel@tonic-gate 	*have_v4 = *have_v6 = 0;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
6797c478bd9Sstevel@tonic-gate     !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * Try to scan the interfaces using IPv6 ioctls().
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	scan_interfaces6(have_v4, have_v6);
6847c478bd9Sstevel@tonic-gate 	if (*have_v4 != 0 && *have_v6 != 0)
6857c478bd9Sstevel@tonic-gate 		return (0);
6867c478bd9Sstevel@tonic-gate #endif
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/* Get interface list from system. */
6897c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
6907c478bd9Sstevel@tonic-gate 		goto err_ret;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/*
6937c478bd9Sstevel@tonic-gate 	 * Grow buffer until large enough to contain all interface
6947c478bd9Sstevel@tonic-gate 	 * descriptions.
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	for (;;) {
6977c478bd9Sstevel@tonic-gate 		buf = memget(bufsiz);
6987c478bd9Sstevel@tonic-gate 		if (buf == NULL)
6997c478bd9Sstevel@tonic-gate 			goto err_ret;
7007c478bd9Sstevel@tonic-gate 		ifc.ifc_len = bufsiz;
7017c478bd9Sstevel@tonic-gate 		ifc.ifc_buf = buf;
7027c478bd9Sstevel@tonic-gate #ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * This is a fix for IRIX OS in which the call to ioctl with
7057c478bd9Sstevel@tonic-gate 		 * the flag SIOCGIFCONF may not return an entry for all the
7067c478bd9Sstevel@tonic-gate 		 * interfaces like most flavors of Unix.
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		if (emul_ioctl(&ifc) >= 0)
7097c478bd9Sstevel@tonic-gate 			break;
7107c478bd9Sstevel@tonic-gate #else
7117c478bd9Sstevel@tonic-gate 		if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
7127c478bd9Sstevel@tonic-gate 			/*
7137c478bd9Sstevel@tonic-gate 			 * Some OS's just return what will fit rather
7147c478bd9Sstevel@tonic-gate 			 * than set EINVAL if the buffer is too small
7157c478bd9Sstevel@tonic-gate 			 * to fit all the interfaces in.  If
7167c478bd9Sstevel@tonic-gate 			 * ifc.ifc_len is too near to the end of the
7177c478bd9Sstevel@tonic-gate 			 * buffer we will grow it just in case and
7187c478bd9Sstevel@tonic-gate 			 * retry.
7197c478bd9Sstevel@tonic-gate 			 */
7207c478bd9Sstevel@tonic-gate 			if (ifc.ifc_len + 2 * sizeof(u.ifreq) < bufsiz)
7217c478bd9Sstevel@tonic-gate 				break;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate #endif
7247c478bd9Sstevel@tonic-gate 		if ((n == -1) && errno != EINVAL)
7257c478bd9Sstevel@tonic-gate 			goto err_ret;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		if (bufsiz > 1000000)
7287c478bd9Sstevel@tonic-gate 			goto err_ret;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		memput(buf, bufsiz);
7317c478bd9Sstevel@tonic-gate 		bufsiz += 4096;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* Parse system's interface list. */
7357c478bd9Sstevel@tonic-gate 	cplim = buf + ifc.ifc_len;    /* skip over if's with big ifr_addr's */
7367c478bd9Sstevel@tonic-gate 	for (cp = buf;
7377c478bd9Sstevel@tonic-gate 	     (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
7387c478bd9Sstevel@tonic-gate 	     cp += cpsize) {
7397c478bd9Sstevel@tonic-gate 		memcpy(&u.ifreq, cp, sizeof u.ifreq);
7407c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
7417c478bd9Sstevel@tonic-gate #ifdef FIX_ZERO_SA_LEN
7427c478bd9Sstevel@tonic-gate 		if (u.ifreq.ifr_addr.sa_len == 0)
7437c478bd9Sstevel@tonic-gate 			u.ifreq.ifr_addr.sa_len = 16;
7447c478bd9Sstevel@tonic-gate #endif
7457c478bd9Sstevel@tonic-gate #ifdef HAVE_MINIMUM_IFREQ
7467c478bd9Sstevel@tonic-gate 		cpsize = sizeof u.ifreq;
7477c478bd9Sstevel@tonic-gate 		if (u.ifreq.ifr_addr.sa_len > sizeof (struct sockaddr))
7487c478bd9Sstevel@tonic-gate 			cpsize += (int)u.ifreq.ifr_addr.sa_len -
7497c478bd9Sstevel@tonic-gate 				(int)(sizeof (struct sockaddr));
7507c478bd9Sstevel@tonic-gate #else
7517c478bd9Sstevel@tonic-gate 		cpsize = sizeof u.ifreq.ifr_name + u.ifreq.ifr_addr.sa_len;
7527c478bd9Sstevel@tonic-gate #endif /* HAVE_MINIMUM_IFREQ */
7537c478bd9Sstevel@tonic-gate 		if (cpsize > sizeof u.ifreq && cpsize <= sizeof u)
7547c478bd9Sstevel@tonic-gate 			memcpy(&u.ifreq, cp, cpsize);
7557c478bd9Sstevel@tonic-gate #elif defined SIOCGIFCONF_ADDR
7567c478bd9Sstevel@tonic-gate 		cpsize = sizeof u.ifreq;
7577c478bd9Sstevel@tonic-gate #else
7587c478bd9Sstevel@tonic-gate 		cpsize = sizeof u.ifreq.ifr_name;
7597c478bd9Sstevel@tonic-gate 		/* XXX maybe this should be a hard error? */
7607c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFADDR, (char *)&u.ifreq) < 0)
7617c478bd9Sstevel@tonic-gate 			continue;
7627c478bd9Sstevel@tonic-gate #endif
7637c478bd9Sstevel@tonic-gate 		switch (u.ifreq.ifr_addr.sa_family) {
7647c478bd9Sstevel@tonic-gate 		case AF_INET:
7657c478bd9Sstevel@tonic-gate 			if (*have_v4 == 0) {
7667c478bd9Sstevel@tonic-gate 				memcpy(&in4,
7677c478bd9Sstevel@tonic-gate 				       &((struct sockaddr_in *)
7687c478bd9Sstevel@tonic-gate 				       &u.ifreq.ifr_addr)->sin_addr,
7697c478bd9Sstevel@tonic-gate 				       sizeof in4);
7707c478bd9Sstevel@tonic-gate 				if (in4.s_addr == INADDR_ANY)
7717c478bd9Sstevel@tonic-gate 					break;
7727c478bd9Sstevel@tonic-gate 				n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
7737c478bd9Sstevel@tonic-gate 				if (n < 0)
7747c478bd9Sstevel@tonic-gate 					break;
7757c478bd9Sstevel@tonic-gate 				if ((u.ifreq.ifr_flags & IFF_UP) == 0)
7767c478bd9Sstevel@tonic-gate 					break;
7777c478bd9Sstevel@tonic-gate 				*have_v4 = 1;
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 			break;
7807c478bd9Sstevel@tonic-gate 		case AF_INET6:
7817c478bd9Sstevel@tonic-gate 			if (*have_v6 == 0) {
7827c478bd9Sstevel@tonic-gate 				memcpy(&in6,
7837c478bd9Sstevel@tonic-gate 				       &((struct sockaddr_in6 *)
7847c478bd9Sstevel@tonic-gate 				       &u.ifreq.ifr_addr)->sin6_addr,
7857c478bd9Sstevel@tonic-gate 				       sizeof in6);
7867c478bd9Sstevel@tonic-gate 				if (memcmp(&in6, &in6addr_any, sizeof in6) == 0)
7877c478bd9Sstevel@tonic-gate 					break;
7887c478bd9Sstevel@tonic-gate 				n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
7897c478bd9Sstevel@tonic-gate 				if (n < 0)
7907c478bd9Sstevel@tonic-gate 					break;
7917c478bd9Sstevel@tonic-gate 				if ((u.ifreq.ifr_flags & IFF_UP) == 0)
7927c478bd9Sstevel@tonic-gate 					break;
7937c478bd9Sstevel@tonic-gate 				*have_v6 = 1;
7947c478bd9Sstevel@tonic-gate 			}
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	if (buf != NULL)
7997c478bd9Sstevel@tonic-gate 		memput(buf, bufsiz);
8007c478bd9Sstevel@tonic-gate 	close(s);
8017c478bd9Sstevel@tonic-gate 	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
8027c478bd9Sstevel@tonic-gate 	return (0);
8037c478bd9Sstevel@tonic-gate  err_ret:
8047c478bd9Sstevel@tonic-gate 	if (buf != NULL)
8057c478bd9Sstevel@tonic-gate 		memput(buf, bufsiz);
8067c478bd9Sstevel@tonic-gate 	if (s != -1)
8077c478bd9Sstevel@tonic-gate 		close(s);
8087c478bd9Sstevel@tonic-gate 	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
8097c478bd9Sstevel@tonic-gate 	return (-1);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate static struct hostent *
8137c478bd9Sstevel@tonic-gate copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num) {
8147c478bd9Sstevel@tonic-gate 	struct hostent *he = NULL;
8157c478bd9Sstevel@tonic-gate 	int addresses = 1;	/* NULL terminator */
8167c478bd9Sstevel@tonic-gate 	int names = 1;		/* NULL terminator */
8177c478bd9Sstevel@tonic-gate 	int len = 0;
8187c478bd9Sstevel@tonic-gate 	char **cpp, **npp;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * Work out array sizes;
8227c478bd9Sstevel@tonic-gate 	 */
8237c478bd9Sstevel@tonic-gate 	if (he1 != NULL) {
8247c478bd9Sstevel@tonic-gate 		cpp = he1->h_addr_list;
8257c478bd9Sstevel@tonic-gate 		while (*cpp != NULL) {
8267c478bd9Sstevel@tonic-gate 			addresses++;
8277c478bd9Sstevel@tonic-gate 			cpp++;
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 		cpp = he1->h_aliases;
8307c478bd9Sstevel@tonic-gate 		while (*cpp != NULL) {
8317c478bd9Sstevel@tonic-gate 			names++;
8327c478bd9Sstevel@tonic-gate 			cpp++;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if (he2 != NULL) {
8377c478bd9Sstevel@tonic-gate 		cpp = he2->h_addr_list;
8387c478bd9Sstevel@tonic-gate 		while (*cpp != NULL) {
8397c478bd9Sstevel@tonic-gate 			addresses++;
8407c478bd9Sstevel@tonic-gate 			cpp++;
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 		if (he1 == NULL) {
8437c478bd9Sstevel@tonic-gate 			cpp = he2->h_aliases;
8447c478bd9Sstevel@tonic-gate 			while (*cpp != NULL) {
8457c478bd9Sstevel@tonic-gate 				names++;
8467c478bd9Sstevel@tonic-gate 				cpp++;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (addresses == 1) {
8527c478bd9Sstevel@tonic-gate 		*error_num = NO_ADDRESS;
8537c478bd9Sstevel@tonic-gate 		return (NULL);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	he = memget(sizeof *he);
8577c478bd9Sstevel@tonic-gate 	if (he == NULL)
8587c478bd9Sstevel@tonic-gate 		goto no_recovery;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	he->h_addr_list = memget(sizeof(char *) * (addresses));
8617c478bd9Sstevel@tonic-gate 	if (he->h_addr_list == NULL)
8627c478bd9Sstevel@tonic-gate 		goto cleanup0;
8637c478bd9Sstevel@tonic-gate 	memset(he->h_addr_list, 0, sizeof(char *) * (addresses));
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/* copy addresses */
8667c478bd9Sstevel@tonic-gate 	npp = he->h_addr_list;
8677c478bd9Sstevel@tonic-gate 	if (he1 != NULL) {
8687c478bd9Sstevel@tonic-gate 		cpp = he1->h_addr_list;
8697c478bd9Sstevel@tonic-gate 		while (*cpp != NULL) {
8707c478bd9Sstevel@tonic-gate 			*npp = memget((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
8717c478bd9Sstevel@tonic-gate 			if (*npp == NULL)
8727c478bd9Sstevel@tonic-gate 				goto cleanup1;
8737c478bd9Sstevel@tonic-gate 			/* convert to mapped if required */
8747c478bd9Sstevel@tonic-gate 			if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
8757c478bd9Sstevel@tonic-gate 				memcpy(*npp, in6addr_mapped,
8767c478bd9Sstevel@tonic-gate 				       sizeof in6addr_mapped);
8777c478bd9Sstevel@tonic-gate 				memcpy(*npp + sizeof in6addr_mapped, *cpp,
8787c478bd9Sstevel@tonic-gate 				       INADDRSZ);
8797c478bd9Sstevel@tonic-gate 			} else {
8807c478bd9Sstevel@tonic-gate 				memcpy(*npp, *cpp,
8817c478bd9Sstevel@tonic-gate 				       (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 			cpp++;
8847c478bd9Sstevel@tonic-gate 			npp++;
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (he2 != NULL) {
8897c478bd9Sstevel@tonic-gate 		cpp = he2->h_addr_list;
8907c478bd9Sstevel@tonic-gate 		while (*cpp != NULL) {
8917c478bd9Sstevel@tonic-gate 			*npp = memget((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
8927c478bd9Sstevel@tonic-gate 			if (*npp == NULL)
8937c478bd9Sstevel@tonic-gate 				goto cleanup1;
8947c478bd9Sstevel@tonic-gate 			/* convert to mapped if required */
8957c478bd9Sstevel@tonic-gate 			if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
8967c478bd9Sstevel@tonic-gate 				memcpy(*npp, in6addr_mapped,
8977c478bd9Sstevel@tonic-gate 				       sizeof in6addr_mapped);
8987c478bd9Sstevel@tonic-gate 				memcpy(*npp + sizeof in6addr_mapped, *cpp,
8997c478bd9Sstevel@tonic-gate 				       INADDRSZ);
9007c478bd9Sstevel@tonic-gate 			} else {
9017c478bd9Sstevel@tonic-gate 				memcpy(*npp, *cpp,
9027c478bd9Sstevel@tonic-gate 				       (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
9037c478bd9Sstevel@tonic-gate 			}
9047c478bd9Sstevel@tonic-gate 			cpp++;
9057c478bd9Sstevel@tonic-gate 			npp++;
9067c478bd9Sstevel@tonic-gate 		}
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	he->h_aliases = memget(sizeof(char *) * (names));
9107c478bd9Sstevel@tonic-gate 	if (he->h_aliases == NULL)
9117c478bd9Sstevel@tonic-gate 		goto cleanup1;
9127c478bd9Sstevel@tonic-gate 	memset(he->h_aliases, 0, sizeof(char *) * (names));
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* copy aliases */
9157c478bd9Sstevel@tonic-gate 	npp = he->h_aliases;
9167c478bd9Sstevel@tonic-gate 	cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
9177c478bd9Sstevel@tonic-gate 	while (*cpp != NULL) {
9187c478bd9Sstevel@tonic-gate 		len = strlen (*cpp) + 1;
9197c478bd9Sstevel@tonic-gate 		*npp = memget(len);
9207c478bd9Sstevel@tonic-gate 		if (*npp == NULL)
9217c478bd9Sstevel@tonic-gate 			goto cleanup2;
9227c478bd9Sstevel@tonic-gate 		strcpy(*npp, *cpp);
9237c478bd9Sstevel@tonic-gate 		npp++;
9247c478bd9Sstevel@tonic-gate 		cpp++;
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/* copy hostname */
9287c478bd9Sstevel@tonic-gate 	he->h_name = memget(strlen((he1 != NULL) ?
9297c478bd9Sstevel@tonic-gate 			    he1->h_name : he2->h_name) + 1);
9307c478bd9Sstevel@tonic-gate 	if (he->h_name == NULL)
9317c478bd9Sstevel@tonic-gate 		goto cleanup2;
9327c478bd9Sstevel@tonic-gate 	strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	/* set address type and length */
9357c478bd9Sstevel@tonic-gate 	he->h_addrtype = af;
9367c478bd9Sstevel@tonic-gate 	he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
9377c478bd9Sstevel@tonic-gate 	return(he);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate  cleanup2:
9407c478bd9Sstevel@tonic-gate 	cpp = he->h_aliases;
9417c478bd9Sstevel@tonic-gate 	while (*cpp != NULL) {
9427c478bd9Sstevel@tonic-gate 		memput(*cpp, strlen(*cpp) + 1);
9437c478bd9Sstevel@tonic-gate 		cpp++;
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	memput(he->h_aliases, sizeof(char *) * (names));
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate  cleanup1:
9487c478bd9Sstevel@tonic-gate 	cpp = he->h_addr_list;
9497c478bd9Sstevel@tonic-gate 	while (*cpp != NULL) {
9507c478bd9Sstevel@tonic-gate 		memput(*cpp, (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
9517c478bd9Sstevel@tonic-gate 		*cpp = NULL;
9527c478bd9Sstevel@tonic-gate 		cpp++;
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 	memput(he->h_addr_list, sizeof(char *) * (addresses));
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate  cleanup0:
9577c478bd9Sstevel@tonic-gate 	memput(he, sizeof *he);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate  no_recovery:
9607c478bd9Sstevel@tonic-gate 	*error_num = NO_RECOVERY;
9617c478bd9Sstevel@tonic-gate 	return (NULL);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate static struct net_data *
9657c478bd9Sstevel@tonic-gate init() {
9667c478bd9Sstevel@tonic-gate 	struct net_data *net_data;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
9697c478bd9Sstevel@tonic-gate 		goto error;
9707c478bd9Sstevel@tonic-gate 	if (!net_data->ho) {
9717c478bd9Sstevel@tonic-gate 		net_data->ho = (*net_data->irs->ho_map)(net_data->irs);
9727c478bd9Sstevel@tonic-gate 		if (!net_data->ho || !net_data->res) {
9737c478bd9Sstevel@tonic-gate   error:
9747c478bd9Sstevel@tonic-gate 			errno = EIO;
9757c478bd9Sstevel@tonic-gate 			if (net_data && net_data->res)
9767c478bd9Sstevel@tonic-gate 				RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
9777c478bd9Sstevel@tonic-gate 			return (NULL);
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		(*net_data->ho->res_set)(net_data->ho, net_data->res, NULL);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	return (net_data);
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate static void
9877c478bd9Sstevel@tonic-gate freepvt(struct net_data *net_data) {
9887c478bd9Sstevel@tonic-gate 	if (net_data->ho_data) {
9897c478bd9Sstevel@tonic-gate 		free(net_data->ho_data);
9907c478bd9Sstevel@tonic-gate 		net_data->ho_data = NULL;
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate static struct hostent *
9957c478bd9Sstevel@tonic-gate fakeaddr(const char *name, int af, struct net_data *net_data) {
9967c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	freepvt(net_data);
9997c478bd9Sstevel@tonic-gate 	net_data->ho_data = malloc(sizeof (struct pvt));
10007c478bd9Sstevel@tonic-gate 	if (!net_data->ho_data) {
10017c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
10027c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
10037c478bd9Sstevel@tonic-gate 		return (NULL);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 	pvt = net_data->ho_data;
10067c478bd9Sstevel@tonic-gate #ifndef __bsdi__
10077c478bd9Sstevel@tonic-gate 	/*
10087c478bd9Sstevel@tonic-gate 	 * Unlike its forebear(inet_aton), our friendly inet_pton() is strict
10097c478bd9Sstevel@tonic-gate 	 * in its interpretation of its input, and it will only return "1" if
10107c478bd9Sstevel@tonic-gate 	 * the input string is a formally valid(and thus unambiguous with
10117c478bd9Sstevel@tonic-gate 	 * respect to host names) internet address specification for this AF.
10127c478bd9Sstevel@tonic-gate 	 *
10137c478bd9Sstevel@tonic-gate 	 * This means "telnet 0xdeadbeef" and "telnet 127.1" are dead now.
10147c478bd9Sstevel@tonic-gate 	 */
10157c478bd9Sstevel@tonic-gate 	if (inet_pton(af, name, pvt->addr) != 1) {
10167c478bd9Sstevel@tonic-gate #else
10177c478bd9Sstevel@tonic-gate 	/* BSDI XXX
10187c478bd9Sstevel@tonic-gate 	 * We put this back to inet_aton -- we really want the old behavior
10197c478bd9Sstevel@tonic-gate 	 * Long live 127.1...
10207c478bd9Sstevel@tonic-gate 	 */
10217c478bd9Sstevel@tonic-gate 	if ((af != AF_INET ||
10227c478bd9Sstevel@tonic-gate 	    inet_aton(name, (struct in_addr *)pvt->addr) != 1) &&
10237c478bd9Sstevel@tonic-gate 	    inet_pton(af, name, pvt->addr) != 1) {
10247c478bd9Sstevel@tonic-gate #endif
10257c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND);
10267c478bd9Sstevel@tonic-gate 		return (NULL);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 	strncpy(pvt->name, name, NS_MAXDNAME);
10297c478bd9Sstevel@tonic-gate 	pvt->name[NS_MAXDNAME] = '\0';
10307c478bd9Sstevel@tonic-gate 	if (af == AF_INET && (net_data->res->options & RES_USE_INET6) != 0) {
10317c478bd9Sstevel@tonic-gate 		map_v4v6_address(pvt->addr, pvt->addr);
10327c478bd9Sstevel@tonic-gate 		af = AF_INET6;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 	pvt->host.h_addrtype = af;
10357c478bd9Sstevel@tonic-gate 	switch(af) {
10367c478bd9Sstevel@tonic-gate 	case AF_INET:
10377c478bd9Sstevel@tonic-gate 		pvt->host.h_length = NS_INADDRSZ;
10387c478bd9Sstevel@tonic-gate 		break;
10397c478bd9Sstevel@tonic-gate 	case AF_INET6:
10407c478bd9Sstevel@tonic-gate 		pvt->host.h_length = NS_IN6ADDRSZ;
10417c478bd9Sstevel@tonic-gate 		break;
10427c478bd9Sstevel@tonic-gate 	default:
10437c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
10447c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
10457c478bd9Sstevel@tonic-gate 		return (NULL);
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 	pvt->host.h_name = pvt->name;
10487c478bd9Sstevel@tonic-gate 	pvt->host.h_aliases = pvt->aliases;
10497c478bd9Sstevel@tonic-gate 	pvt->aliases[0] = NULL;
10507c478bd9Sstevel@tonic-gate 	pvt->addrs[0] = (char *)pvt->addr;
10517c478bd9Sstevel@tonic-gate 	pvt->addrs[1] = NULL;
10527c478bd9Sstevel@tonic-gate 	pvt->host.h_addr_list = pvt->addrs;
10537c478bd9Sstevel@tonic-gate 	RES_SET_H_ERRNO(net_data->res, NETDB_SUCCESS);
10547c478bd9Sstevel@tonic-gate 	return (&pvt->host);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate #ifdef grot	/* for future use in gethostbyaddr(), for "SUNSECURITY" */
10587c478bd9Sstevel@tonic-gate 	struct hostent *rhp;
10597c478bd9Sstevel@tonic-gate 	char **haddr;
10607c478bd9Sstevel@tonic-gate 	u_long old_options;
10617c478bd9Sstevel@tonic-gate 	char hname2[MAXDNAME+1];
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
10647c478bd9Sstevel@tonic-gate 	    /*
10657c478bd9Sstevel@tonic-gate 	     * turn off search as the name should be absolute,
10667c478bd9Sstevel@tonic-gate 	     * 'localhost' should be matched by defnames
10677c478bd9Sstevel@tonic-gate 	     */
10687c478bd9Sstevel@tonic-gate 	    strncpy(hname2, hp->h_name, MAXDNAME);
10697c478bd9Sstevel@tonic-gate 	    hname2[MAXDNAME] = '\0';
10707c478bd9Sstevel@tonic-gate 	    old_options = net_data->res->options;
10717c478bd9Sstevel@tonic-gate 	    net_data->res->options &= ~RES_DNSRCH;
10727c478bd9Sstevel@tonic-gate 	    net_data->res->options |= RES_DEFNAMES;
10737c478bd9Sstevel@tonic-gate 	    if (!(rhp = gethostbyname(hname2))) {
10747c478bd9Sstevel@tonic-gate 		net_data->res->options = old_options;
10757c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND);
10767c478bd9Sstevel@tonic-gate 		return (NULL);
10777c478bd9Sstevel@tonic-gate 	    }
10787c478bd9Sstevel@tonic-gate 	    net_data->res->options = old_options;
10797c478bd9Sstevel@tonic-gate 	    for (haddr = rhp->h_addr_list; *haddr; haddr++)
10807c478bd9Sstevel@tonic-gate 		if (!memcmp(*haddr, addr, INADDRSZ))
10817c478bd9Sstevel@tonic-gate 			break;
10827c478bd9Sstevel@tonic-gate 	    if (!*haddr) {
10837c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND);
10847c478bd9Sstevel@tonic-gate 		return (NULL);
10857c478bd9Sstevel@tonic-gate 	    }
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate #endif /* grot */
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
1090