1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate /*
24*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*0Sstevel@tonic-gate  * Use is subject to license terms.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <netdb.h>
32*0Sstevel@tonic-gate #include <arpa/inet.h>
33*0Sstevel@tonic-gate #include <nss_dbdefs.h>
34*0Sstevel@tonic-gate #include <netinet/in.h>
35*0Sstevel@tonic-gate #include <sys/socket.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <ctype.h>
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <net/if.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate extern char *_dgettext(const char *, const char *);
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	ai2sin(x)	((struct sockaddr_in *)((x)->ai_addr))
47*0Sstevel@tonic-gate #define	ai2sin6(x)	((struct sockaddr_in6 *)((x)->ai_addr))
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #define	HOST_BROADCAST	"255.255.255.255"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * getaddrinfo() returns EAI_NONAME in some cases, however
53*0Sstevel@tonic-gate  * since EAI_NONAME is not part of SUSv3 it needed to be
54*0Sstevel@tonic-gate  * masked in the standards compliant environment.
55*0Sstevel@tonic-gate  * GAIV_DEFAULT and GAIV_XPG6 accomplish this.
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate #define	GAIV_DEFAULT	0
58*0Sstevel@tonic-gate #define	GAIV_XPG6	1
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Storage allocation for global variables in6addr_any and
62*0Sstevel@tonic-gate  * in6addr_loopback.  The extern declarations for these
63*0Sstevel@tonic-gate  * variables are defined in <netinet/in.h>.  These two
64*0Sstevel@tonic-gate  * variables could have been defined in any of the "C" files
65*0Sstevel@tonic-gate  * in libsocket. They are defined here with other IPv6
66*0Sstevel@tonic-gate  * related interfaces.
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
69*0Sstevel@tonic-gate const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /* AI_MASK:  all valid flags for addrinfo */
72*0Sstevel@tonic-gate #define	AI_MASK		(AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST \
73*0Sstevel@tonic-gate 	| AI_ADDRCONFIG | AI_NUMERICSERV | AI_V4MAPPED | AI_ALL)
74*0Sstevel@tonic-gate #define	ANY		0
75*0Sstevel@tonic-gate /* function prototypes for used by getaddrinfo() routine */
76*0Sstevel@tonic-gate static int get_addr(int family, const char *hostname, struct addrinfo *aip,
77*0Sstevel@tonic-gate 	struct addrinfo *cur, ushort_t port, int version);
78*0Sstevel@tonic-gate static uint_t getscopeidfromzone(const struct sockaddr_in6 *sa,
79*0Sstevel@tonic-gate     const char *zone, uint32_t *sin6_scope_id);
80*0Sstevel@tonic-gate static boolean_t str_isnumber(const char *p);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * getaddrinfo:
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * Purpose:
86*0Sstevel@tonic-gate  *   Routine for performing Address-to-nodename in a
87*0Sstevel@tonic-gate  *   protocol-independent fashion.
88*0Sstevel@tonic-gate  * Description and history of the routine:
89*0Sstevel@tonic-gate  *   Nodename-to-address translation is done in a protocol-
90*0Sstevel@tonic-gate  *   independent fashion using the getaddrinfo() function
91*0Sstevel@tonic-gate  *   that is taken from the IEEE POSIX 1003.1g.
92*0Sstevel@tonic-gate  *
93*0Sstevel@tonic-gate  *   The official specification for this function will be the
94*0Sstevel@tonic-gate  *   final POSIX standard, with the following additional
95*0Sstevel@tonic-gate  *   requirements:
96*0Sstevel@tonic-gate  *
97*0Sstevel@tonic-gate  *   - getaddrinfo() must be thread safe
98*0Sstevel@tonic-gate  *   - The AI_NUMERICHOST is new.
99*0Sstevel@tonic-gate  *   - All fields in socket address structures returned by
100*0Sstevel@tonic-gate  *
101*0Sstevel@tonic-gate  *  getaddrinfo() that are not filled in through an explicit
102*0Sstevel@tonic-gate  *  argument (e.g., sin6_flowinfo and sin_zero) must be set to 0.
103*0Sstevel@tonic-gate  *  (This makes it easier to compare socket address structures).
104*0Sstevel@tonic-gate  *
105*0Sstevel@tonic-gate  * Input Parameters:
106*0Sstevel@tonic-gate  *  nodename  - pointer to null-terminated strings that represents
107*0Sstevel@tonic-gate  *              a hostname or literal ip address (IPv4/IPv6) or this
108*0Sstevel@tonic-gate  *              pointer can be NULL.
109*0Sstevel@tonic-gate  *  servname  - pointer to null-terminated strings that represents
110*0Sstevel@tonic-gate  *              a servicename or literal port number or this
111*0Sstevel@tonic-gate  *              pointer can be NULL.
112*0Sstevel@tonic-gate  *  hints     - optional argument that points to an addrinfo structure
113*0Sstevel@tonic-gate  *              to provide hints on the type of socket that the caller
114*0Sstevel@tonic-gate  *              supports.
115*0Sstevel@tonic-gate  *   Possible setting of the ai_flags member of the hints structure:
116*0Sstevel@tonic-gate  *   AI_PASSIVE -     If set, the caller plans to use the returned socket
117*0Sstevel@tonic-gate  *                    address in a call to bind().  In this case, it the
118*0Sstevel@tonic-gate  *                    nodename argument is NULL, then the IP address portion
119*0Sstevel@tonic-gate  *                    of the socket address structure will be set to
120*0Sstevel@tonic-gate  *                    INADDR_ANY for IPv4 or IN6ADDR_ANY_INIT for IPv6.
121*0Sstevel@tonic-gate  *   AI_PASSIVE -     If not set, then the returned socket address will be
122*0Sstevel@tonic-gate  *                    ready for a call to connect() (for conn-oriented) or
123*0Sstevel@tonic-gate  *                    connect(), sendto(), or sendmsg() (for connectionless).
124*0Sstevel@tonic-gate  *                    In this case, if nodename is NULL, then the IP address
125*0Sstevel@tonic-gate  *                    portion of the socket address structure will be set to
126*0Sstevel@tonic-gate  *                    the loopback address.
127*0Sstevel@tonic-gate  *   AI_CANONNAME -   If set, then upon successful return the ai_canonname
128*0Sstevel@tonic-gate  *                    field of the first addrinfo structure in the linked
129*0Sstevel@tonic-gate  *                    list will point to a NULL-terminated string
130*0Sstevel@tonic-gate  *                    containing the canonical name of the specified nodename.
131*0Sstevel@tonic-gate  *   AI_NUMERICHOST - If set, then a non-NULL nodename string must be a numeric
132*0Sstevel@tonic-gate  *                    host address string.  Otherwise an error of EAI_NONAME
133*0Sstevel@tonic-gate  *                    is returned.  This flag prevents any type of name
134*0Sstevel@tonic-gate  *                    resolution service from being called.
135*0Sstevel@tonic-gate  *   AI_NUMERICSERV - If set, then a non-null servname string supplied shall
136*0Sstevel@tonic-gate  *                    be a numeric port string. Otherwise, an [EAI_NONAME]
137*0Sstevel@tonic-gate  *                    error shall be returned. This flag shall prevent any
138*0Sstevel@tonic-gate  *                    type of name resolution service from being invoked.
139*0Sstevel@tonic-gate  *   AI_V4MAPPED -    If set, along with an ai_family of AF_INET6, then
140*0Sstevel@tonic-gate  *                    getaddrinfo() shall return IPv4-mapped IPv6 addresses
141*0Sstevel@tonic-gate  *                    on finding no matching IPv6 addresses ( ai_addrlen shall
142*0Sstevel@tonic-gate  *                    be 16). The AI_V4MAPPED flag shall be ignored unless
143*0Sstevel@tonic-gate  *                    ai_family equals AF_INET6.
144*0Sstevel@tonic-gate  *   AI_ALL -	      If the AI_ALL flag is used with the AI_V4MAPPED flag,
145*0Sstevel@tonic-gate  *		      then getaddrinfo() shall return all matching IPv6 and
146*0Sstevel@tonic-gate  *		      IPv4 addresses. The AI_ALL flag without the AI_V4MAPPED
147*0Sstevel@tonic-gate  *		      flag is ignored.
148*0Sstevel@tonic-gate  * Output Parameters:
149*0Sstevel@tonic-gate  *  res       - upon successful return a pointer to a linked list of one
150*0Sstevel@tonic-gate  *              or more addrinfo structures is returned through this
151*0Sstevel@tonic-gate  *              argument.  The caller can process each addrinfo structures
152*0Sstevel@tonic-gate  *              in this list by following the ai_next pointer, until a
153*0Sstevel@tonic-gate  *              NULL pointer is encountered.  In each returned addrinfo
154*0Sstevel@tonic-gate  *              structure the three members ai_family, ai_socktype, and
155*0Sstevel@tonic-gate  *              ai_protocol are corresponding arguments for a call to the
156*0Sstevel@tonic-gate  *              socket() function.  In each addrinfo structure the ai_addr
157*0Sstevel@tonic-gate  *              field points to filled-in socket address structure whose
158*0Sstevel@tonic-gate  *              length is specified by the ai_addrlen member.
159*0Sstevel@tonic-gate  *
160*0Sstevel@tonic-gate  * Return Value:
161*0Sstevel@tonic-gate  *  This function returns 0 upon success or a nonzero error code.  The
162*0Sstevel@tonic-gate  *  following names are nonzero error codes from getaddrinfo(), and are
163*0Sstevel@tonic-gate  *  defined in <netdb.h>.
164*0Sstevel@tonic-gate  *  EAI_ADDRFAMILY - address family not supported
165*0Sstevel@tonic-gate  *  EAI_AGAIN      - DNS temporary failure
166*0Sstevel@tonic-gate  *  EAI_BADFLAGS   - invalid ai_flags
167*0Sstevel@tonic-gate  *  EAI_FAIL       - DNS non-recoverable failure
168*0Sstevel@tonic-gate  *  EAI_FAMILY     - ai_family not supported
169*0Sstevel@tonic-gate  *  EAI_MEMORY     - memory allocation failure
170*0Sstevel@tonic-gate  *  EAI_NODATA     - no address associated with nodename
171*0Sstevel@tonic-gate  *  EAI_NONAME     - host/servname not known
172*0Sstevel@tonic-gate  *  EAI_SERVICE    - servname not supported for ai_socktype
173*0Sstevel@tonic-gate  *  EAI_SOCKTYPE   - ai_socktype not supported
174*0Sstevel@tonic-gate  *  EAI_SYSTEM     - system error in errno
175*0Sstevel@tonic-gate  *
176*0Sstevel@tonic-gate  * Memory Allocation:
177*0Sstevel@tonic-gate  *  All of the information returned by getaddrinfo() is dynamically
178*0Sstevel@tonic-gate  *  allocated:  the addrinfo structures, and the socket address
179*0Sstevel@tonic-gate  *  structures and canonical node name strings pointed to by the
180*0Sstevel@tonic-gate  *  addrinfo structures.
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate static int
185*0Sstevel@tonic-gate _getaddrinfo(const char *hostname, const char *servname,
186*0Sstevel@tonic-gate 	const struct addrinfo *hints, struct addrinfo **res, int version)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	struct addrinfo *cur;
189*0Sstevel@tonic-gate 	struct addrinfo *aip;
190*0Sstevel@tonic-gate 	struct addrinfo ai;
191*0Sstevel@tonic-gate 	int		error;
192*0Sstevel@tonic-gate 	ushort_t	port;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	cur = &ai;
195*0Sstevel@tonic-gate 	aip = &ai;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	aip->ai_flags = 0;
198*0Sstevel@tonic-gate 	aip->ai_family = PF_UNSPEC;
199*0Sstevel@tonic-gate 	aip->ai_socktype = 0;
200*0Sstevel@tonic-gate 	aip->ai_protocol = 0;
201*0Sstevel@tonic-gate #ifdef __sparcv9
202*0Sstevel@tonic-gate 	/*
203*0Sstevel@tonic-gate 	 * We need to clear _ai_pad to preserve binary
204*0Sstevel@tonic-gate 	 * compatibility with previously compiled 64-bit
205*0Sstevel@tonic-gate 	 * applications by guaranteeing the upper 32-bits
206*0Sstevel@tonic-gate 	 * are empty.
207*0Sstevel@tonic-gate 	 */
208*0Sstevel@tonic-gate 	aip->_ai_pad = 0;
209*0Sstevel@tonic-gate #endif /* __sparcv9 */
210*0Sstevel@tonic-gate 	aip->ai_addrlen = 0;
211*0Sstevel@tonic-gate 	aip->ai_canonname = NULL;
212*0Sstevel@tonic-gate 	aip->ai_addr = NULL;
213*0Sstevel@tonic-gate 	aip->ai_next = NULL;
214*0Sstevel@tonic-gate 	port = 0;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	/* if nodename nor servname provided */
217*0Sstevel@tonic-gate 	if (hostname == NULL && servname == NULL) {
218*0Sstevel@tonic-gate 		*res = NULL;
219*0Sstevel@tonic-gate 		return (EAI_NONAME);
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 	if (hints != NULL) {
222*0Sstevel@tonic-gate 		/* check for bad flags in hints */
223*0Sstevel@tonic-gate 		if ((hints->ai_flags != 0) && (hints->ai_flags & ~AI_MASK)) {
224*0Sstevel@tonic-gate 			*res = NULL;
225*0Sstevel@tonic-gate 			return (EAI_BADFLAGS);
226*0Sstevel@tonic-gate 		}
227*0Sstevel@tonic-gate 		if ((hostname == NULL || *hostname == '\0') &&
228*0Sstevel@tonic-gate 			(hints->ai_flags & AI_CANONNAME)) {
229*0Sstevel@tonic-gate 				*res = NULL;
230*0Sstevel@tonic-gate 				return (EAI_BADFLAGS);
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 		if (hints->ai_family != PF_UNSPEC &&
233*0Sstevel@tonic-gate 		    hints->ai_family != PF_INET &&
234*0Sstevel@tonic-gate 		    hints->ai_family != PF_INET6) {
235*0Sstevel@tonic-gate 			*res = NULL;
236*0Sstevel@tonic-gate 			return (EAI_FAMILY);
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 		(void) memcpy(aip, hints, sizeof (*aip));
240*0Sstevel@tonic-gate #ifdef __sparcv9
241*0Sstevel@tonic-gate 		/*
242*0Sstevel@tonic-gate 		 * We need to clear _ai_pad to preserve binary
243*0Sstevel@tonic-gate 		 * compatibility.  See prior comment.
244*0Sstevel@tonic-gate 		 */
245*0Sstevel@tonic-gate 		aip->_ai_pad = 0;
246*0Sstevel@tonic-gate #endif /* __sparcv9 */
247*0Sstevel@tonic-gate 		switch (aip->ai_socktype) {
248*0Sstevel@tonic-gate 		case ANY:
249*0Sstevel@tonic-gate 			switch (aip->ai_protocol) {
250*0Sstevel@tonic-gate 			case ANY:
251*0Sstevel@tonic-gate 				break;
252*0Sstevel@tonic-gate 			case IPPROTO_UDP:
253*0Sstevel@tonic-gate 				aip->ai_socktype = SOCK_DGRAM;
254*0Sstevel@tonic-gate 				break;
255*0Sstevel@tonic-gate 			case IPPROTO_TCP:
256*0Sstevel@tonic-gate 				aip->ai_socktype = SOCK_STREAM;
257*0Sstevel@tonic-gate 				break;
258*0Sstevel@tonic-gate 			default:
259*0Sstevel@tonic-gate 				aip->ai_socktype = SOCK_RAW;
260*0Sstevel@tonic-gate 				break;
261*0Sstevel@tonic-gate 			}
262*0Sstevel@tonic-gate 			break;
263*0Sstevel@tonic-gate 		case SOCK_RAW:
264*0Sstevel@tonic-gate 			break;
265*0Sstevel@tonic-gate 		case SOCK_DGRAM:
266*0Sstevel@tonic-gate 			aip->ai_protocol = IPPROTO_UDP;
267*0Sstevel@tonic-gate 			break;
268*0Sstevel@tonic-gate 		case SOCK_STREAM:
269*0Sstevel@tonic-gate 			aip->ai_protocol = IPPROTO_TCP;
270*0Sstevel@tonic-gate 			break;
271*0Sstevel@tonic-gate 		default:
272*0Sstevel@tonic-gate 			*res = NULL;
273*0Sstevel@tonic-gate 			return (EAI_SOCKTYPE);
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	/*
278*0Sstevel@tonic-gate 	 *  Get the service.
279*0Sstevel@tonic-gate 	 */
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	if (servname != NULL) {
282*0Sstevel@tonic-gate 		struct servent result;
283*0Sstevel@tonic-gate 		int bufsize = 128;
284*0Sstevel@tonic-gate 		char *buf = NULL;
285*0Sstevel@tonic-gate 		struct servent *sp;
286*0Sstevel@tonic-gate 		char *proto = NULL;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 		switch (aip->ai_socktype) {
289*0Sstevel@tonic-gate 		case ANY:
290*0Sstevel@tonic-gate 			proto = NULL;
291*0Sstevel@tonic-gate 			break;
292*0Sstevel@tonic-gate 		case SOCK_DGRAM:
293*0Sstevel@tonic-gate 			proto = "udp";
294*0Sstevel@tonic-gate 			break;
295*0Sstevel@tonic-gate 		case SOCK_STREAM:
296*0Sstevel@tonic-gate 			proto = "tcp";
297*0Sstevel@tonic-gate 			break;
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 		/*
300*0Sstevel@tonic-gate 		 * Servname string can be a decimal port number.
301*0Sstevel@tonic-gate 		 * If we already know the socket type there is no need
302*0Sstevel@tonic-gate 		 * to call getservbyport.
303*0Sstevel@tonic-gate 		 */
304*0Sstevel@tonic-gate 		if (aip->ai_flags & AI_NUMERICSERV) {
305*0Sstevel@tonic-gate 			if (!str_isnumber(servname)) {
306*0Sstevel@tonic-gate 				return (EAI_NONAME);
307*0Sstevel@tonic-gate 			}
308*0Sstevel@tonic-gate 			port = htons(atoi(servname));
309*0Sstevel@tonic-gate 		} else if (str_isnumber(servname)) {
310*0Sstevel@tonic-gate 			port = htons(atoi(servname));
311*0Sstevel@tonic-gate 			if (aip->ai_socktype == ANY) {
312*0Sstevel@tonic-gate 				do {
313*0Sstevel@tonic-gate 					if (buf != NULL)
314*0Sstevel@tonic-gate 						free(buf);
315*0Sstevel@tonic-gate 					bufsize *= 2;
316*0Sstevel@tonic-gate 					buf = malloc(bufsize);
317*0Sstevel@tonic-gate 					if (buf == NULL) {
318*0Sstevel@tonic-gate 						*res = NULL;
319*0Sstevel@tonic-gate 						return (EAI_MEMORY);
320*0Sstevel@tonic-gate 					}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 					sp = getservbyport_r(port, proto,
323*0Sstevel@tonic-gate 					    &result, buf, bufsize);
324*0Sstevel@tonic-gate 					if (sp == NULL && errno != ERANGE) {
325*0Sstevel@tonic-gate 						free(buf);
326*0Sstevel@tonic-gate 						*res = NULL;
327*0Sstevel@tonic-gate 						return (EAI_SERVICE);
328*0Sstevel@tonic-gate 					}
329*0Sstevel@tonic-gate 				/*
330*0Sstevel@tonic-gate 				 * errno == ERANGE so our scratch buffer space
331*0Sstevel@tonic-gate 				 * wasn't big enough.  Double it and try
332*0Sstevel@tonic-gate 				 * again.
333*0Sstevel@tonic-gate 				 */
334*0Sstevel@tonic-gate 				} while (sp == NULL);
335*0Sstevel@tonic-gate 			}
336*0Sstevel@tonic-gate 		} else {
337*0Sstevel@tonic-gate 			do {
338*0Sstevel@tonic-gate 				if (buf != NULL)
339*0Sstevel@tonic-gate 					free(buf);
340*0Sstevel@tonic-gate 				bufsize *= 2;
341*0Sstevel@tonic-gate 				buf = malloc(bufsize);
342*0Sstevel@tonic-gate 				if (buf == NULL) {
343*0Sstevel@tonic-gate 					*res = NULL;
344*0Sstevel@tonic-gate 					return (EAI_MEMORY);
345*0Sstevel@tonic-gate 				}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 				sp = getservbyname_r(servname, proto, &result,
348*0Sstevel@tonic-gate 				    buf, bufsize);
349*0Sstevel@tonic-gate 				if (sp == NULL && errno != ERANGE) {
350*0Sstevel@tonic-gate 					free(buf);
351*0Sstevel@tonic-gate 					*res = NULL;
352*0Sstevel@tonic-gate 					return (EAI_SERVICE);
353*0Sstevel@tonic-gate 				}
354*0Sstevel@tonic-gate 			/*
355*0Sstevel@tonic-gate 			 * errno == ERANGE so our scratch buffer space wasn't
356*0Sstevel@tonic-gate 			 * big enough.  Double it and try again.
357*0Sstevel@tonic-gate 			 */
358*0Sstevel@tonic-gate 			} while (sp == NULL);
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 			port = sp->s_port;
361*0Sstevel@tonic-gate 		}
362*0Sstevel@tonic-gate 		if (aip->ai_socktype == ANY) {
363*0Sstevel@tonic-gate 			if (aip->ai_flags & AI_NUMERICSERV) {
364*0Sstevel@tonic-gate 				/*
365*0Sstevel@tonic-gate 				 * RFC 2553bis doesn't allow us to use the
366*0Sstevel@tonic-gate 				 * any resolver to find out if there is a
367*0Sstevel@tonic-gate 				 * match.  We could walk the service file
368*0Sstevel@tonic-gate 				 * with *servent().  Given the commonality of
369*0Sstevel@tonic-gate 				 * calling getaddrinfo() with a number and
370*0Sstevel@tonic-gate 				 * ANY protocol we won't add that at this time.
371*0Sstevel@tonic-gate 				 */
372*0Sstevel@tonic-gate 				return (EAI_NONAME);
373*0Sstevel@tonic-gate 			}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 			if (strcmp(sp->s_proto, "udp") == 0) {
376*0Sstevel@tonic-gate 				aip->ai_socktype = SOCK_DGRAM;
377*0Sstevel@tonic-gate 				aip->ai_protocol = IPPROTO_UDP;
378*0Sstevel@tonic-gate 			} else if (strcmp(sp->s_proto, "tcp") == 0) {
379*0Sstevel@tonic-gate 				aip->ai_socktype = SOCK_STREAM;
380*0Sstevel@tonic-gate 				aip->ai_protocol = IPPROTO_TCP;
381*0Sstevel@tonic-gate 			} else {
382*0Sstevel@tonic-gate 				if (buf != NULL)
383*0Sstevel@tonic-gate 					free(buf);
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 				*res = NULL;
386*0Sstevel@tonic-gate 				errno = EPROTONOSUPPORT;
387*0Sstevel@tonic-gate 				return (EAI_SYSTEM);
388*0Sstevel@tonic-gate 			}
389*0Sstevel@tonic-gate 		}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 		if (buf != NULL)
392*0Sstevel@tonic-gate 			free(buf);
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	/*
396*0Sstevel@tonic-gate 	 * hostname is NULL
397*0Sstevel@tonic-gate 	 * case 1: AI_PASSIVE bit set : anyaddr 0.0.0.0 or ::
398*0Sstevel@tonic-gate 	 * case 2: AI_PASSIVE bit not set : localhost 127.0.0.1 or ::1
399*0Sstevel@tonic-gate 	 */
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (hostname == NULL) {
402*0Sstevel@tonic-gate 		struct addrinfo *nai;
403*0Sstevel@tonic-gate 		socklen_t addrlen;
404*0Sstevel@tonic-gate 		char *canonname;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 		if (aip->ai_family == PF_INET)
407*0Sstevel@tonic-gate 			goto v4only;
408*0Sstevel@tonic-gate 		/* create IPv6 addrinfo */
409*0Sstevel@tonic-gate 		nai = malloc(sizeof (struct addrinfo));
410*0Sstevel@tonic-gate 		if (nai == NULL)
411*0Sstevel@tonic-gate 			goto nomem;
412*0Sstevel@tonic-gate 		*nai = *aip;
413*0Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in6);
414*0Sstevel@tonic-gate 		nai->ai_addr = malloc(addrlen);
415*0Sstevel@tonic-gate 		if (nai->ai_addr == NULL) {
416*0Sstevel@tonic-gate 			freeaddrinfo(nai);
417*0Sstevel@tonic-gate 			goto nomem;
418*0Sstevel@tonic-gate 		}
419*0Sstevel@tonic-gate 		bzero(nai->ai_addr, addrlen);
420*0Sstevel@tonic-gate 		nai->ai_addrlen = addrlen;
421*0Sstevel@tonic-gate 		nai->ai_family = PF_INET6;
422*0Sstevel@tonic-gate 		nai->ai_protocol = 0;
423*0Sstevel@tonic-gate 		nai->ai_canonname = NULL;
424*0Sstevel@tonic-gate 		if (nai->ai_flags & AI_PASSIVE) {
425*0Sstevel@tonic-gate 			ai2sin6(nai)->sin6_addr = in6addr_any;
426*0Sstevel@tonic-gate 		} else {
427*0Sstevel@tonic-gate 			ai2sin6(nai)->sin6_addr = in6addr_loopback;
428*0Sstevel@tonic-gate 			if (nai->ai_flags & AI_CANONNAME) {
429*0Sstevel@tonic-gate 				canonname = strdup("loopback");
430*0Sstevel@tonic-gate 				if (canonname == NULL) {
431*0Sstevel@tonic-gate 					freeaddrinfo(nai);
432*0Sstevel@tonic-gate 					goto nomem;
433*0Sstevel@tonic-gate 				}
434*0Sstevel@tonic-gate 				nai->ai_canonname = canonname;
435*0Sstevel@tonic-gate 			}
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 		ai2sin6(nai)->sin6_family = PF_INET6;
438*0Sstevel@tonic-gate 		ai2sin6(nai)->sin6_port = port;
439*0Sstevel@tonic-gate 		cur->ai_next = nai;
440*0Sstevel@tonic-gate 		cur = nai;
441*0Sstevel@tonic-gate 		if (aip->ai_family == PF_INET6) {
442*0Sstevel@tonic-gate 			cur->ai_next = NULL;
443*0Sstevel@tonic-gate 			goto success;
444*0Sstevel@tonic-gate 		}
445*0Sstevel@tonic-gate 		/* If address family is PF_UNSPEC or PF_INET */
446*0Sstevel@tonic-gate v4only:
447*0Sstevel@tonic-gate 		/* create IPv4 addrinfo */
448*0Sstevel@tonic-gate 		nai = malloc(sizeof (struct addrinfo));
449*0Sstevel@tonic-gate 		if (nai == NULL)
450*0Sstevel@tonic-gate 			goto nomem;
451*0Sstevel@tonic-gate 		*nai = *aip;
452*0Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in);
453*0Sstevel@tonic-gate 		nai->ai_addr = malloc(addrlen);
454*0Sstevel@tonic-gate 		if (nai->ai_addr == NULL) {
455*0Sstevel@tonic-gate 			freeaddrinfo(nai);
456*0Sstevel@tonic-gate 			goto nomem;
457*0Sstevel@tonic-gate 		}
458*0Sstevel@tonic-gate 		bzero(nai->ai_addr, addrlen);
459*0Sstevel@tonic-gate 		nai->ai_addrlen = addrlen;
460*0Sstevel@tonic-gate 		nai->ai_family = PF_INET;
461*0Sstevel@tonic-gate 		nai->ai_protocol = 0;
462*0Sstevel@tonic-gate 		nai->ai_canonname = NULL;
463*0Sstevel@tonic-gate 		if (nai->ai_flags & AI_PASSIVE) {
464*0Sstevel@tonic-gate 			ai2sin(nai)->sin_addr.s_addr = INADDR_ANY;
465*0Sstevel@tonic-gate 		} else {
466*0Sstevel@tonic-gate 			ai2sin(nai)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
467*0Sstevel@tonic-gate 			if (nai->ai_flags & AI_CANONNAME &&
468*0Sstevel@tonic-gate 			    nai->ai_family != PF_UNSPEC) {
469*0Sstevel@tonic-gate 				canonname = strdup("loopback");
470*0Sstevel@tonic-gate 				if (canonname == NULL) {
471*0Sstevel@tonic-gate 					freeaddrinfo(nai);
472*0Sstevel@tonic-gate 					goto nomem;
473*0Sstevel@tonic-gate 				}
474*0Sstevel@tonic-gate 				nai->ai_canonname = canonname;
475*0Sstevel@tonic-gate 			}
476*0Sstevel@tonic-gate 		}
477*0Sstevel@tonic-gate 		ai2sin(nai)->sin_family = PF_INET;
478*0Sstevel@tonic-gate 		ai2sin(nai)->sin_port = port;
479*0Sstevel@tonic-gate 		cur->ai_next = nai;
480*0Sstevel@tonic-gate 		cur = nai;
481*0Sstevel@tonic-gate 		cur->ai_next = NULL;
482*0Sstevel@tonic-gate 		goto success;
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	/* hostname string is a literal address or an alphabetical name */
486*0Sstevel@tonic-gate 	error = get_addr(aip->ai_family, hostname, aip, cur, port, version);
487*0Sstevel@tonic-gate 	if (error) {
488*0Sstevel@tonic-gate 		*res = NULL;
489*0Sstevel@tonic-gate 		return (error);
490*0Sstevel@tonic-gate 	}
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate success:
493*0Sstevel@tonic-gate 	*res = aip->ai_next;
494*0Sstevel@tonic-gate 	return (0);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate nomem:
497*0Sstevel@tonic-gate 	return (EAI_MEMORY);
498*0Sstevel@tonic-gate }
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate int
501*0Sstevel@tonic-gate getaddrinfo(const char *hostname, const char *servname,
502*0Sstevel@tonic-gate 	const struct addrinfo *hints, struct addrinfo **res)
503*0Sstevel@tonic-gate {
504*0Sstevel@tonic-gate 	return (_getaddrinfo(hostname, servname, hints, res, GAIV_DEFAULT));
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate int
508*0Sstevel@tonic-gate __xnet_getaddrinfo(const char *hostname, const char *servname,
509*0Sstevel@tonic-gate 	const struct addrinfo *hints, struct addrinfo **res)
510*0Sstevel@tonic-gate {
511*0Sstevel@tonic-gate 	return (_getaddrinfo(hostname, servname, hints, res, GAIV_XPG6));
512*0Sstevel@tonic-gate }
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate static int
515*0Sstevel@tonic-gate get_addr(int family, const char *hostname, struct addrinfo *aip, struct
516*0Sstevel@tonic-gate 	addrinfo *cur, ushort_t port, int version)
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate 	struct hostent		*hp;
519*0Sstevel@tonic-gate 	char			_hostname[MAXHOSTNAMELEN];
520*0Sstevel@tonic-gate 	int			i, errnum;
521*0Sstevel@tonic-gate 	struct addrinfo		*nai;
522*0Sstevel@tonic-gate 	int			addrlen;
523*0Sstevel@tonic-gate 	char			*canonname;
524*0Sstevel@tonic-gate 	boolean_t		firsttime = B_TRUE;
525*0Sstevel@tonic-gate 	boolean_t		create_v6_addrinfo;
526*0Sstevel@tonic-gate 	struct in_addr		v4addr;
527*0Sstevel@tonic-gate 	struct in6_addr		v6addr;
528*0Sstevel@tonic-gate 	struct in6_addr		*v6addrp;
529*0Sstevel@tonic-gate 	char			*zonestr = NULL;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	/*
532*0Sstevel@tonic-gate 	 * Check for existence of address-zoneid delimiter '%'
533*0Sstevel@tonic-gate 	 * If the delimiter exists, parse the zoneid portion of
534*0Sstevel@tonic-gate 	 * <addr>%<zone_id>
535*0Sstevel@tonic-gate 	 */
536*0Sstevel@tonic-gate 	if ((zonestr = strchr(hostname, '%')) != NULL) {
537*0Sstevel@tonic-gate 		/* make sure we have room for <addr> portion of hostname */
538*0Sstevel@tonic-gate 		if (((zonestr - hostname) + 1) > sizeof (_hostname)) {
539*0Sstevel@tonic-gate 			return (EAI_MEMORY);
540*0Sstevel@tonic-gate 		}
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 		/* chop off and save <zone_id> portion */
543*0Sstevel@tonic-gate 		(void) strlcpy(_hostname, hostname, (zonestr - hostname) + 1);
544*0Sstevel@tonic-gate 		++zonestr;	/* make zonestr point at start of <zone-id> */
545*0Sstevel@tonic-gate 		/* ensure zone is valid */
546*0Sstevel@tonic-gate 		if ((*zonestr == '\0') || (strlen(zonestr) > LIFNAMSIZ))  {
547*0Sstevel@tonic-gate 			return (EAI_NONAME);
548*0Sstevel@tonic-gate 		}
549*0Sstevel@tonic-gate 	} else {
550*0Sstevel@tonic-gate 		size_t hlen = sizeof (_hostname);
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 		if (strlcpy(_hostname, hostname, hlen) >= hlen) {
553*0Sstevel@tonic-gate 			return (EAI_MEMORY);
554*0Sstevel@tonic-gate 		}
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	/* Check to see if AI_NUMERICHOST bit is set */
558*0Sstevel@tonic-gate 	if (aip->ai_flags & AI_NUMERICHOST) {
559*0Sstevel@tonic-gate 		/* check to see if _hostname points to a literal IP address */
560*0Sstevel@tonic-gate 		if (!((inet_addr(_hostname) != ((in_addr_t)-1)) ||
561*0Sstevel@tonic-gate 		    (strcmp(_hostname, HOST_BROADCAST) == 0) ||
562*0Sstevel@tonic-gate 		    (inet_pton(AF_INET6, _hostname, &v6addr) > 0))) {
563*0Sstevel@tonic-gate 			return (EAI_NONAME);
564*0Sstevel@tonic-gate 		}
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	/* if hostname argument is literal, name service doesn't get called */
568*0Sstevel@tonic-gate 	if (family == PF_UNSPEC) {
569*0Sstevel@tonic-gate 		hp = getipnodebyname(_hostname, AF_INET6, AI_ALL |
570*0Sstevel@tonic-gate 		    aip->ai_flags | AI_V4MAPPED, &errnum);
571*0Sstevel@tonic-gate 	} else {
572*0Sstevel@tonic-gate 		hp = getipnodebyname(_hostname, family, aip->ai_flags, &errnum);
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	if (hp == NULL) {
576*0Sstevel@tonic-gate 		switch (errnum) {
577*0Sstevel@tonic-gate 		case HOST_NOT_FOUND:
578*0Sstevel@tonic-gate 			return (EAI_NONAME);
579*0Sstevel@tonic-gate 		case TRY_AGAIN:
580*0Sstevel@tonic-gate 			return (EAI_AGAIN);
581*0Sstevel@tonic-gate 		case NO_RECOVERY:
582*0Sstevel@tonic-gate 			return (EAI_FAIL);
583*0Sstevel@tonic-gate 		case NO_ADDRESS:
584*0Sstevel@tonic-gate 			if (version == GAIV_XPG6)
585*0Sstevel@tonic-gate 				return (EAI_NONAME);
586*0Sstevel@tonic-gate 			return (EAI_NODATA);
587*0Sstevel@tonic-gate 		default:
588*0Sstevel@tonic-gate 		return (EAI_SYSTEM);
589*0Sstevel@tonic-gate 		}
590*0Sstevel@tonic-gate 	}
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	for (i = 0; hp->h_addr_list[i]; i++) {
593*0Sstevel@tonic-gate 		/* Determine if an IPv6 addrinfo structure should be created */
594*0Sstevel@tonic-gate 		create_v6_addrinfo = B_TRUE;
595*0Sstevel@tonic-gate 		if (hp->h_addrtype == AF_INET6) {
596*0Sstevel@tonic-gate 			v6addrp = (struct in6_addr *)hp->h_addr_list[i];
597*0Sstevel@tonic-gate 			if (!(aip->ai_flags & AI_V4MAPPED) &&
598*0Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(v6addrp)) {
599*0Sstevel@tonic-gate 				create_v6_addrinfo = B_FALSE;
600*0Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(v6addrp, &v4addr);
601*0Sstevel@tonic-gate 			}
602*0Sstevel@tonic-gate 		} else	if (hp->h_addrtype == AF_INET) {
603*0Sstevel@tonic-gate 			create_v6_addrinfo = B_FALSE;
604*0Sstevel@tonic-gate 			(void) memcpy(&v4addr, hp->h_addr_list[i],
605*0Sstevel@tonic-gate 			    sizeof (struct in_addr));
606*0Sstevel@tonic-gate 		} else {
607*0Sstevel@tonic-gate 			return (EAI_SYSTEM);
608*0Sstevel@tonic-gate 		}
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 		if (create_v6_addrinfo) {
611*0Sstevel@tonic-gate 			/* create IPv6 addrinfo */
612*0Sstevel@tonic-gate 			nai = malloc(sizeof (struct addrinfo));
613*0Sstevel@tonic-gate 			if (nai == NULL)
614*0Sstevel@tonic-gate 				goto nomem;
615*0Sstevel@tonic-gate 			*nai = *aip;
616*0Sstevel@tonic-gate 			addrlen = sizeof (struct sockaddr_in6);
617*0Sstevel@tonic-gate 			nai->ai_addr = malloc(addrlen);
618*0Sstevel@tonic-gate 			if (nai->ai_addr == NULL) {
619*0Sstevel@tonic-gate 				freeaddrinfo(nai);
620*0Sstevel@tonic-gate 				goto nomem;
621*0Sstevel@tonic-gate 			}
622*0Sstevel@tonic-gate 			bzero(nai->ai_addr, addrlen);
623*0Sstevel@tonic-gate 			nai->ai_addrlen = addrlen;
624*0Sstevel@tonic-gate 			nai->ai_family = PF_INET6;
625*0Sstevel@tonic-gate 			nai->ai_protocol = 0;
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 			(void) memcpy(ai2sin6(nai)->sin6_addr.s6_addr,
628*0Sstevel@tonic-gate 			    hp->h_addr_list[i], sizeof (struct in6_addr));
629*0Sstevel@tonic-gate 			nai->ai_canonname = NULL;
630*0Sstevel@tonic-gate 			if ((nai->ai_flags & AI_CANONNAME) && firsttime) {
631*0Sstevel@tonic-gate 				canonname = strdup(hp->h_name);
632*0Sstevel@tonic-gate 				if (canonname == NULL) {
633*0Sstevel@tonic-gate 					freeaddrinfo(nai);
634*0Sstevel@tonic-gate 					goto nomem;
635*0Sstevel@tonic-gate 				}
636*0Sstevel@tonic-gate 				nai->ai_canonname = canonname;
637*0Sstevel@tonic-gate 				firsttime = B_FALSE;
638*0Sstevel@tonic-gate 			}
639*0Sstevel@tonic-gate 			ai2sin6(nai)->sin6_family = PF_INET6;
640*0Sstevel@tonic-gate 			ai2sin6(nai)->sin6_port = port;
641*0Sstevel@tonic-gate 			/* set sin6_scope_id */
642*0Sstevel@tonic-gate 			if (zonestr != NULL) {
643*0Sstevel@tonic-gate 				/*
644*0Sstevel@tonic-gate 				 * Translate 'zonestr' into a valid
645*0Sstevel@tonic-gate 				 * sin6_scope_id.
646*0Sstevel@tonic-gate 				 */
647*0Sstevel@tonic-gate 				if ((errnum =
648*0Sstevel@tonic-gate 				    getscopeidfromzone(ai2sin6(nai), zonestr,
649*0Sstevel@tonic-gate 					&ai2sin6(nai)->sin6_scope_id)) != 0) {
650*0Sstevel@tonic-gate 					return (errnum);
651*0Sstevel@tonic-gate 				}
652*0Sstevel@tonic-gate 			} else {
653*0Sstevel@tonic-gate 				ai2sin6(nai)->sin6_scope_id = 0;
654*0Sstevel@tonic-gate 			}
655*0Sstevel@tonic-gate 		} else {
656*0Sstevel@tonic-gate 			/* create IPv4 addrinfo */
657*0Sstevel@tonic-gate 			nai = malloc(sizeof (struct addrinfo));
658*0Sstevel@tonic-gate 			if (nai == NULL)
659*0Sstevel@tonic-gate 				goto nomem;
660*0Sstevel@tonic-gate 			*nai = *aip;
661*0Sstevel@tonic-gate 			addrlen = sizeof (struct sockaddr_in);
662*0Sstevel@tonic-gate 			nai->ai_addr = malloc(addrlen);
663*0Sstevel@tonic-gate 			if (nai->ai_addr == NULL) {
664*0Sstevel@tonic-gate 				freeaddrinfo(nai);
665*0Sstevel@tonic-gate 				goto nomem;
666*0Sstevel@tonic-gate 			}
667*0Sstevel@tonic-gate 			bzero(nai->ai_addr, addrlen);
668*0Sstevel@tonic-gate 			nai->ai_addrlen = addrlen;
669*0Sstevel@tonic-gate 			nai->ai_family = PF_INET;
670*0Sstevel@tonic-gate 			nai->ai_protocol = 0;
671*0Sstevel@tonic-gate 			(void) memcpy(&(ai2sin(nai)->sin_addr.s_addr),
672*0Sstevel@tonic-gate 			    &v4addr, sizeof (struct in_addr));
673*0Sstevel@tonic-gate 			nai->ai_canonname = NULL;
674*0Sstevel@tonic-gate 			if (nai->ai_flags & AI_CANONNAME && firsttime) {
675*0Sstevel@tonic-gate 				canonname = strdup(hp->h_name);
676*0Sstevel@tonic-gate 				if (canonname == NULL) {
677*0Sstevel@tonic-gate 					freeaddrinfo(nai);
678*0Sstevel@tonic-gate 					goto nomem;
679*0Sstevel@tonic-gate 				}
680*0Sstevel@tonic-gate 				nai->ai_canonname = canonname;
681*0Sstevel@tonic-gate 				firsttime = B_FALSE;
682*0Sstevel@tonic-gate 			}
683*0Sstevel@tonic-gate 			ai2sin(nai)->sin_family = PF_INET;
684*0Sstevel@tonic-gate 			ai2sin(nai)->sin_port = port;
685*0Sstevel@tonic-gate 		}
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 		cur->ai_next = nai;
688*0Sstevel@tonic-gate 		cur = nai;
689*0Sstevel@tonic-gate 	}
690*0Sstevel@tonic-gate 	cur->ai_next = NULL;
691*0Sstevel@tonic-gate 	freehostent(hp);
692*0Sstevel@tonic-gate 	return (0);
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate nomem:
695*0Sstevel@tonic-gate 	freehostent(hp);
696*0Sstevel@tonic-gate 	return (EAI_MEMORY);
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate }
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate /*
701*0Sstevel@tonic-gate  * getscopeidfromzone(sa, zone, sin6_scope_id)
702*0Sstevel@tonic-gate  *
703*0Sstevel@tonic-gate  * Converts the string pointed to by 'zone' into a sin6_scope_id.
704*0Sstevel@tonic-gate  * 'zone' will either be a pointer to an interface name or will
705*0Sstevel@tonic-gate  * be a literal sin6_scope_id.
706*0Sstevel@tonic-gate  *
707*0Sstevel@tonic-gate  * 0 is returned on success and the output parameter 'sin6_scope_id' will
708*0Sstevel@tonic-gate  *   be set to a valid sin6_scope_id.
709*0Sstevel@tonic-gate  * EAI_NONAME is returned for either of two reasons:
710*0Sstevel@tonic-gate  *	1.  The IPv6 address pointed to by sa->sin6_addr is not
711*0Sstevel@tonic-gate  *	    part of the 'link scope' (ie link local, nodelocal multicast or
712*0Sstevel@tonic-gate  *	    linklocal multicast address)
713*0Sstevel@tonic-gate  *	2.  The string pointed to by 'zone' can not be translate to a valid
714*0Sstevel@tonic-gate  *	    sin6_scope_id.
715*0Sstevel@tonic-gate  */
716*0Sstevel@tonic-gate static uint_t
717*0Sstevel@tonic-gate getscopeidfromzone(const struct sockaddr_in6 *sa, const char *zone,
718*0Sstevel@tonic-gate     uint32_t *sin6_scope_id) {
719*0Sstevel@tonic-gate 	const in6_addr_t *addr = &sa->sin6_addr;
720*0Sstevel@tonic-gate 	ulong_t ul_scope_id;
721*0Sstevel@tonic-gate 	char *endp;
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKSCOPE(addr)) {
724*0Sstevel@tonic-gate 		/*
725*0Sstevel@tonic-gate 		 * Look up interface index associated with interface name
726*0Sstevel@tonic-gate 		 * pointed to by 'zone'.  Since the address is part of the link
727*0Sstevel@tonic-gate 		 * scope, there is a one-to-one relationship between interface
728*0Sstevel@tonic-gate 		 * index and sin6_scope_id.
729*0Sstevel@tonic-gate 		 * If an interface index can not be found for 'zone', then
730*0Sstevel@tonic-gate 		 * treat 'zone' as a literal sin6_scope_id value.
731*0Sstevel@tonic-gate 		 */
732*0Sstevel@tonic-gate 		if ((*sin6_scope_id = if_nametoindex(zone)) != 0) {
733*0Sstevel@tonic-gate 			return (0);
734*0Sstevel@tonic-gate 		} else {
735*0Sstevel@tonic-gate 			if ((ul_scope_id = strtoul(zone, &endp, 10)) != 0) {
736*0Sstevel@tonic-gate 				/* check that entire string was read */
737*0Sstevel@tonic-gate 				if (*endp != '\0') {
738*0Sstevel@tonic-gate 					return (EAI_NONAME);
739*0Sstevel@tonic-gate 				}
740*0Sstevel@tonic-gate 				*sin6_scope_id =
741*0Sstevel@tonic-gate 				    (uint32_t)(ul_scope_id & 0xffffffffUL);
742*0Sstevel@tonic-gate 			} else {
743*0Sstevel@tonic-gate 				return (EAI_NONAME);
744*0Sstevel@tonic-gate 			}
745*0Sstevel@tonic-gate 		}
746*0Sstevel@tonic-gate 	} else {
747*0Sstevel@tonic-gate 		return (EAI_NONAME);
748*0Sstevel@tonic-gate 	}
749*0Sstevel@tonic-gate 	return (0);
750*0Sstevel@tonic-gate }
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate void
754*0Sstevel@tonic-gate freeaddrinfo(struct addrinfo *ai)
755*0Sstevel@tonic-gate {
756*0Sstevel@tonic-gate 	struct addrinfo *next;
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	do {
759*0Sstevel@tonic-gate 		next = ai->ai_next;
760*0Sstevel@tonic-gate 		if (ai->ai_canonname)
761*0Sstevel@tonic-gate 			free(ai->ai_canonname);
762*0Sstevel@tonic-gate 		if (ai->ai_addr)
763*0Sstevel@tonic-gate 			free(ai->ai_addr);
764*0Sstevel@tonic-gate 		free(ai);
765*0Sstevel@tonic-gate 		ai = next;
766*0Sstevel@tonic-gate 	} while (ai != NULL);
767*0Sstevel@tonic-gate }
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate static boolean_t
770*0Sstevel@tonic-gate str_isnumber(const char *p)
771*0Sstevel@tonic-gate {
772*0Sstevel@tonic-gate 	char *q = (char *)p;
773*0Sstevel@tonic-gate 	while (*q) {
774*0Sstevel@tonic-gate 		if (!isdigit(*q))
775*0Sstevel@tonic-gate 			return (B_FALSE);
776*0Sstevel@tonic-gate 		q++;
777*0Sstevel@tonic-gate 	}
778*0Sstevel@tonic-gate 	return (B_TRUE);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate static const char *gai_errlist[] = {
781*0Sstevel@tonic-gate 	"name translation error 0 (no error)",		/* 0 */
782*0Sstevel@tonic-gate 	"specified address family not supported",	/* 1 EAI_ADDRFAMILY */
783*0Sstevel@tonic-gate 	"temporary name resolution failure",		/* 2 EAI_AGAIN */
784*0Sstevel@tonic-gate 	"invalid flags",				/* 3 EAI_BADFLAGS */
785*0Sstevel@tonic-gate 	"non-recoverable name resolution failure",	/* 4 EAI_FAIL */
786*0Sstevel@tonic-gate 	"specified address family not supported",	/* 5 EAI_FAMILY */
787*0Sstevel@tonic-gate 	"memory allocation failure",			/* 6 EAI_MEMORY */
788*0Sstevel@tonic-gate 	"no address for the specified node name",	/* 7 EAI_NODATA */
789*0Sstevel@tonic-gate 	"node name or service name not known",		/* 8 EAI_NONAME */
790*0Sstevel@tonic-gate 	"service name not available for the specified socket type",
791*0Sstevel@tonic-gate 							/* 9 EAI_SERVICE */
792*0Sstevel@tonic-gate 	"specified socket type not supported",		/* 10 EAI_SOCKTYPE */
793*0Sstevel@tonic-gate 	"system error",					/* 11 EAI_SYSTEM */
794*0Sstevel@tonic-gate };
795*0Sstevel@tonic-gate static int gai_nerr = { sizeof (gai_errlist)/sizeof (gai_errlist[0]) };
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate const char *
798*0Sstevel@tonic-gate gai_strerror(int ecode)
799*0Sstevel@tonic-gate {
800*0Sstevel@tonic-gate 	if (ecode < 0)
801*0Sstevel@tonic-gate 		return (_dgettext(TEXT_DOMAIN,
802*0Sstevel@tonic-gate 		    "name translation internal error"));
803*0Sstevel@tonic-gate 	else if (ecode < gai_nerr)
804*0Sstevel@tonic-gate 		return (_dgettext(TEXT_DOMAIN, gai_errlist[ecode]));
805*0Sstevel@tonic-gate 	return (_dgettext(TEXT_DOMAIN, "unknown name translation error"));
806*0Sstevel@tonic-gate }
807