xref: /titanic_51/usr/src/lib/libnsl/nss/getipnodeby.c (revision 269473047d747f7815af570197e4ef7322d3632c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * This file defines and implements the re-entrant getipnodebyname(),
27  * getipnodebyaddr(), and freehostent() routines for IPv6. These routines
28  * follow use the netdir_getbyYY() (see netdir_inet.c).
29  *
30  * lib/libnsl/nss/getipnodeby.c
31  */
32 
33 #include "mt.h"
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stropts.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <arpa/inet.h>
43 #include <nss_dbdefs.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <nss_netdir.h>
48 #include <net/if.h>
49 #include <netinet/in.h>
50 #include <netdir.h>
51 #include <thread.h>
52 #include <synch.h>
53 #include <fcntl.h>
54 #include <sys/time.h>
55 #include "nss.h"
56 
57 #define	IPV6_LITERAL_CHAR	':'
58 
59 /*
60  * The number of nanoseconds getipnodebyname() waits before getting
61  * fresh interface count information with SIOCGLIFNUM.  The default is
62  * five minutes.
63  */
64 #define	IFNUM_TIMEOUT	((hrtime_t)300 * NANOSEC)
65 
66 /*
67  * Bits in the bitfield returned by getipnodebyname_processflags().
68  *
69  * IPNODE_WANTIPV6	The user wants IPv6 addresses returned.
70  * IPNODE_WANTIPV4	The user wants IPv4 addresses returned.
71  * IPNODE_IPV4IFNOIPV6	The user only wants IPv4 addresses returned if no IPv6
72  *			addresses are returned.
73  * IPNODE_LOOKUPIPNODES	getipnodebyname() needs to lookup the name in ipnodes.
74  * IPNODE_LOOKUPHOSTS	getipnodebyname() needs to lookup the name in hosts.
75  * IPNODE_ISLITERAL	The name supplied is a literal address string.
76  */
77 #define	IPNODE_WANTIPV6		0x00000001u
78 #define	IPNODE_WANTIPV4		0x00000002u
79 #define	IPNODE_IPV4IFNOIPV6	0x00000004u
80 #define	IPNODE_LOOKUPIPNODES	0x00000008u
81 #define	IPNODE_LOOKUPHOSTS	0x00000010u
82 #define	IPNODE_LITERAL		0x00000020u
83 #define	IPNODE_IPV4		(IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6)
84 
85 /*
86  * The default set of bits corresponding to a getipnodebyname() flags
87  * argument of AI_DEFAULT.
88  */
89 #define	IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \
90 	IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS)
91 
92 extern struct netconfig *__rpc_getconfip(char *);
93 
94 static struct hostent *__mapv4tov6(struct hostent *, struct hostent *,
95     nss_XbyY_buf_t *, int);
96 struct hostent *__mappedtov4(struct hostent *, int *);
97 static struct hostent *__filter_addresses(int, struct hostent *);
98 static int __find_mapped(struct hostent *, int);
99 static nss_XbyY_buf_t *__IPv6_alloc(int);
100 static void __IPv6_cleanup(nss_XbyY_buf_t *);
101 static int __ai_addrconfig(int);
102 
103 
104 #ifdef PIC
105 struct hostent *
106 _uncached_getipnodebyname(const char *nam, struct hostent *result,
107 	char *buffer, int buflen, int af_family, int flags, int *h_errnop)
108 {
109 	return (_switch_getipnodebyname_r(nam, result, buffer, buflen,
110 	    af_family, flags, h_errnop));
111 }
112 
113 struct hostent *
114 _uncached_getipnodebyaddr(const char *addr, int length, int type,
115 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
116 {
117 	if (type == AF_INET)
118 		return (_switch_gethostbyaddr_r(addr, length, type,
119 		    result, buffer, buflen, h_errnop));
120 	else if (type == AF_INET6)
121 		return (_switch_getipnodebyaddr_r(addr, length, type,
122 		    result, buffer, buflen, h_errnop));
123 	return (NULL);
124 }
125 #endif
126 
127 /*
128  * Given a name, an address family, and a set of flags, return a
129  * bitfield that getipnodebyname() will use.
130  */
131 static uint_t
132 getipnodebyname_processflags(const char *name, int af, int flags)
133 {
134 	uint_t		ipnode_bits = IPNODE_DEFAULT;
135 	boolean_t	ipv6configured = B_FALSE;
136 	boolean_t	ipv4configured = B_FALSE;
137 
138 	/*
139 	 * If AI_ADDRCONFIG is specified, we need to determine the number
140 	 * of addresses of each address family configured on the system as
141 	 * appropriate.
142 	 */
143 	if (flags & AI_ADDRCONFIG) {
144 		ipv6configured = (af == AF_INET6 &&
145 		    __ai_addrconfig(AF_INET6) > 0);
146 		ipv4configured = ((af == AF_INET || (flags & AI_V4MAPPED)) &&
147 		    __ai_addrconfig(AF_INET) > 0);
148 	}
149 
150 	/*
151 	 * Determine what kinds of addresses the user is interested
152 	 * in getting back.
153 	 */
154 	switch (af) {
155 	case AF_INET6:
156 		if ((flags & AI_ADDRCONFIG) && !ipv6configured)
157 			ipnode_bits &= ~IPNODE_WANTIPV6;
158 
159 		if (flags & AI_V4MAPPED) {
160 			if ((flags & AI_ADDRCONFIG) && !ipv4configured) {
161 				ipnode_bits &= ~IPNODE_IPV4;
162 			} else if (flags & AI_ALL) {
163 				ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
164 			}
165 		} else {
166 			ipnode_bits &= ~IPNODE_IPV4;
167 		}
168 		break;
169 	case AF_INET:
170 		if ((flags & AI_ADDRCONFIG) && !ipv4configured)
171 			ipnode_bits &= ~IPNODE_IPV4;
172 		ipnode_bits &= ~IPNODE_WANTIPV6;
173 		ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
174 		break;
175 	default:
176 		ipnode_bits = 0;
177 		break;
178 	}
179 
180 	/*
181 	 * If we're not looking for IPv4 addresses, don't bother looking
182 	 * in hosts.
183 	 */
184 	if (!(ipnode_bits & IPNODE_WANTIPV4))
185 		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
186 
187 	/*
188 	 * Determine if name is a literal IP address.  This will
189 	 * further narrow down what type of lookup we're going to do.
190 	 */
191 	if (strchr(name, IPV6_LITERAL_CHAR) != NULL) {
192 		/* Literal IPv6 address */
193 		ipnode_bits |= IPNODE_LITERAL;
194 		/*
195 		 * In s9 we accepted the literal without filtering independent
196 		 * of what family was passed in hints.  We continue to do
197 		 * this.
198 		 */
199 		ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4);
200 		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
201 	} else if (inet_addr(name) != 0xffffffffU) {
202 		/* Literal IPv4 address */
203 		ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4);
204 		ipnode_bits &= ~IPNODE_WANTIPV6;
205 		ipnode_bits &= ~IPNODE_LOOKUPIPNODES;
206 	}
207 	return (ipnode_bits);
208 }
209 
210 struct hostent *
211 getipnodebyname(const char *name, int af, int flags, int *error_num)
212 {
213 	struct hostent		*hp = NULL;
214 	nss_XbyY_buf_t		*buf4 = NULL;
215 	nss_XbyY_buf_t		*buf6 = NULL;
216 	struct netconfig	*nconf;
217 	struct nss_netdirbyname_in	nssin;
218 	union nss_netdirbyname_out	nssout;
219 	int			ret;
220 	uint_t			ipnode_bits;
221 
222 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
223 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
224 		*error_num = NO_RECOVERY;
225 		return (NULL);
226 	}
227 
228 	ipnode_bits = getipnodebyname_processflags(name, af, flags);
229 
230 	/* Make sure we have something to look up. */
231 	if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) {
232 		*error_num = HOST_NOT_FOUND;
233 		goto cleanup;
234 	}
235 
236 	/*
237 	 * Perform the requested lookups.  We always look through
238 	 * ipnodes first for both IPv4 and IPv6 addresses.  Depending
239 	 * on what was returned and what was needed, we either filter
240 	 * out the garbage, or ask for more using hosts.
241 	 */
242 	if (ipnode_bits & IPNODE_LOOKUPIPNODES) {
243 		if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) {
244 			*error_num = NO_RECOVERY;
245 			goto cleanup;
246 		}
247 		nssin.op_t = NSS_HOST6;
248 		nssin.arg.nss.host6.name = name;
249 		nssin.arg.nss.host6.buf = buf6->buffer;
250 		nssin.arg.nss.host6.buflen = buf6->buflen;
251 		nssin.arg.nss.host6.af_family = af;
252 		nssin.arg.nss.host6.flags = flags;
253 		nssout.nss.host.hent = buf6->result;
254 		nssout.nss.host.herrno_p = error_num;
255 		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
256 		if (ret != ND_OK) {
257 			__IPv6_cleanup(buf6);
258 			buf6 = NULL;
259 		} else if (ipnode_bits & IPNODE_WANTIPV4) {
260 			/*
261 			 * buf6 may have all that we need if we either
262 			 * only wanted IPv4 addresses if there were no
263 			 * IPv6 addresses returned, or if there are
264 			 * IPv4-mapped addresses in buf6.  If either
265 			 * of these are true, then there's no need to
266 			 * look in hosts.
267 			 */
268 			if (ipnode_bits & IPNODE_IPV4IFNOIPV6 ||
269 			    __find_mapped(buf6->result, 0) != 0) {
270 				ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
271 			} else if (!(ipnode_bits & IPNODE_WANTIPV6)) {
272 				/*
273 				 * If all we're looking for are IPv4
274 				 * addresses and there are none in
275 				 * buf6 then buf6 is now useless.
276 				 */
277 				__IPv6_cleanup(buf6);
278 				buf6 = NULL;
279 			}
280 		}
281 	}
282 	if (ipnode_bits & IPNODE_LOOKUPHOSTS) {
283 		if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) {
284 			*error_num = NO_RECOVERY;
285 			goto cleanup;
286 		}
287 		nssin.op_t = NSS_HOST;
288 		nssin.arg.nss.host.name = name;
289 		nssin.arg.nss.host.buf = buf4->buffer;
290 		nssin.arg.nss.host.buflen = buf4->buflen;
291 		nssout.nss.host.hent = buf4->result;
292 		nssout.nss.host.herrno_p = error_num;
293 		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
294 		if (ret != ND_OK) {
295 			__IPv6_cleanup(buf4);
296 			buf4 = NULL;
297 		}
298 	}
299 
300 	if (buf6 == NULL && buf4 == NULL) {
301 		*error_num = HOST_NOT_FOUND;
302 		goto cleanup;
303 	}
304 
305 	/* Extract the appropriate addresses from the returned buffer(s). */
306 	switch (af) {
307 	case AF_INET6: {
308 		if (buf4 != NULL) {
309 			nss_XbyY_buf_t *mergebuf;
310 
311 			/*
312 			 * The IPv4 results we have need to be
313 			 * converted to IPv4-mapped addresses,
314 			 * conditionally merged with the IPv6
315 			 * results, and the end result needs to be
316 			 * re-ordered.
317 			 */
318 			mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES);
319 			if (mergebuf == NULL) {
320 				*error_num = NO_RECOVERY;
321 				goto cleanup;
322 			}
323 			hp = __mapv4tov6(buf4->result,
324 			    ((buf6 != NULL) ? buf6->result : NULL),
325 			    mergebuf, 1);
326 			if (hp != NULL)
327 				order_haddrlist_af(AF_INET6, hp->h_addr_list);
328 			else
329 				*error_num = NO_RECOVERY;
330 			free(mergebuf);
331 		}
332 
333 		if (buf4 == NULL && buf6 != NULL) {
334 			hp = buf6->result;
335 
336 			/*
337 			 * We have what we need in buf6, but we may need
338 			 * to filter out some addresses depending on what
339 			 * is being asked for.
340 			 */
341 			if (!(ipnode_bits & IPNODE_WANTIPV4))
342 				hp = __filter_addresses(AF_INET, buf6->result);
343 			else if (!(ipnode_bits & IPNODE_WANTIPV6))
344 				hp = __filter_addresses(AF_INET6, buf6->result);
345 
346 			if (hp == NULL)
347 				*error_num = NO_ADDRESS;
348 		}
349 
350 		break;
351 	}
352 
353 	case AF_INET:
354 		/* We could have results in buf6 or buf4, not both */
355 		if (buf6 != NULL) {
356 			/*
357 			 * Extract the IPv4-mapped addresses from buf6
358 			 * into hp.
359 			 */
360 			hp = __mappedtov4(buf6->result, error_num);
361 		} else {
362 			/* We have what we need in buf4. */
363 			hp = buf4->result;
364 			if (ipnode_bits & IPNODE_LITERAL) {
365 				/*
366 				 * There is a special case here for literal
367 				 * IPv4 address strings.  The hosts
368 				 * front-end sets h_aliases to a one
369 				 * element array containing a single NULL
370 				 * pointer (in ndaddr2hent()), while
371 				 * getipnodebyname() requires h_aliases to
372 				 * be a NULL pointer itself.  We're not
373 				 * going to change the front-end since it
374 				 * needs to remain backward compatible for
375 				 * gethostbyname() and friends.  Just set
376 				 * h_aliases to NULL here instead.
377 				 */
378 				hp->h_aliases = NULL;
379 			}
380 		}
381 
382 		break;
383 
384 	default:
385 		break;
386 	}
387 
388 cleanup:
389 	/*
390 	 * Free the memory we allocated, but make sure we don't free
391 	 * the memory we're returning to the caller.
392 	 */
393 	if (buf6 != NULL) {
394 		if (buf6->result == hp)
395 			buf6->result = NULL;
396 		__IPv6_cleanup(buf6);
397 	}
398 	if (buf4 != NULL) {
399 		if (buf4->result == hp)
400 			buf4->result = NULL;
401 		__IPv6_cleanup(buf4);
402 	}
403 	(void) freenetconfigent(nconf);
404 
405 	return (hp);
406 }
407 
408 /*
409  * This is the IPv6 interface for "gethostbyaddr".
410  */
411 struct hostent *
412 getipnodebyaddr(const void *src, size_t len, int type, int *error_num)
413 {
414 	struct in6_addr *addr6 = 0;
415 	struct in_addr *addr4 = 0;
416 	nss_XbyY_buf_t *buf = 0;
417 	nss_XbyY_buf_t *res = 0;
418 	struct netconfig *nconf;
419 	struct hostent *hp = 0;
420 	struct	nss_netdirbyaddr_in nssin;
421 	union	nss_netdirbyaddr_out nssout;
422 	int neterr;
423 	char tmpbuf[64];
424 
425 	if (type == AF_INET6) {
426 		if ((addr6 = (struct in6_addr *)src) == NULL) {
427 			*error_num = HOST_NOT_FOUND;
428 			return (NULL);
429 		}
430 	} else if (type == AF_INET) {
431 		if ((addr4 = (struct in_addr *)src) == NULL) {
432 			*error_num = HOST_NOT_FOUND;
433 			return (NULL);
434 		}
435 	} else {
436 		*error_num = HOST_NOT_FOUND;
437 		return (NULL);
438 	}
439 	/*
440 	 * Specific case: query for "::"
441 	 */
442 	if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) {
443 		*error_num = HOST_NOT_FOUND;
444 		return (NULL);
445 	}
446 	/*
447 	 * Step 1: IPv4-mapped address  or IPv4 Compat
448 	 */
449 	if ((type == AF_INET6 && len == 16) &&
450 	    ((IN6_IS_ADDR_V4MAPPED(addr6)) ||
451 	    (IN6_IS_ADDR_V4COMPAT(addr6)))) {
452 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
453 			*error_num = NO_RECOVERY;
454 			return (NULL);
455 		}
456 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
457 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
458 			*error_num = NO_RECOVERY;
459 			__IPv6_cleanup(buf);
460 			return (NULL);
461 		}
462 		nssin.op_t = NSS_HOST6;
463 		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
464 			(void) memcpy(tmpbuf, addr6, sizeof (*addr6));
465 			tmpbuf[10] = 0xffU;
466 			tmpbuf[11] = 0xffU;
467 			nssin.arg.nss.host.addr = (const char *)tmpbuf;
468 		} else {
469 			nssin.arg.nss.host.addr = (const char *)addr6;
470 		}
471 		nssin.arg.nss.host.len = sizeof (struct in6_addr);
472 		nssin.arg.nss.host.type = AF_INET6;
473 		nssin.arg.nss.host.buf = buf->buffer;
474 		nssin.arg.nss.host.buflen = buf->buflen;
475 
476 		nssout.nss.host.hent = buf->result;
477 		nssout.nss.host.herrno_p = error_num;
478 		/*
479 		 * We pass in nconf and let the implementation of the
480 		 * long-named func decide whether to use the switch based on
481 		 * nc_nlookups.
482 		 */
483 		neterr =
484 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
485 
486 		(void) freenetconfigent(nconf);
487 		if (neterr != ND_OK) {
488 			/* Failover case, try hosts db for v4 address */
489 			if (!gethostbyaddr_r(((char *)addr6) + 12,
490 			    sizeof (in_addr_t), AF_INET, buf->result,
491 			    buf->buffer, buf->buflen, error_num)) {
492 				__IPv6_cleanup(buf);
493 				return (NULL);
494 			}
495 			/* Found one, now format it into mapped/compat addr */
496 			if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
497 				__IPv6_cleanup(buf);
498 				*error_num = NO_RECOVERY;
499 				return (NULL);
500 			}
501 			/* Convert IPv4 to mapped/compat address w/name */
502 			hp = res->result;
503 			(void) __mapv4tov6(buf->result, 0, res,
504 			    IN6_IS_ADDR_V4MAPPED(addr6));
505 			__IPv6_cleanup(buf);
506 			free(res);
507 			return (hp);
508 		}
509 		/*
510 		 * At this point, we'll have a v4mapped hostent. If that's
511 		 * what was passed in, just return. If the request was a compat,
512 		 * twiggle the two bytes to make the mapped address a compat.
513 		 */
514 		hp = buf->result;
515 		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
516 			/* LINTED pointer cast */
517 			addr6 = (struct in6_addr *)hp->h_addr_list[0];
518 			addr6->s6_addr[10] = 0;
519 			addr6->s6_addr[11] = 0;
520 		}
521 		free(buf);
522 		return (hp);
523 	}
524 	/*
525 	 * Step 2: AF_INET, v4 lookup. Since we're going to search the
526 	 * ipnodes (v6) path first, we need to treat this as a v4mapped
527 	 * address. nscd(1m) caches v4 from ipnodes as mapped v6's. The
528 	 * switch backend knows to lookup v4's (not v4mapped) from the
529 	 * name services.
530 	 */
531 	if (type == AF_INET) {
532 		struct in6_addr v4mapbuf;
533 		addr6 = &v4mapbuf;
534 
535 		IN6_INADDR_TO_V4MAPPED(addr4, addr6);
536 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
537 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
538 			*error_num = NO_RECOVERY;
539 			return (NULL);
540 		}
541 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
542 			*error_num = NO_RECOVERY;
543 			freenetconfigent(nconf);
544 			return (NULL);
545 		}
546 		nssin.op_t = NSS_HOST6;
547 		nssin.arg.nss.host.addr = (const char *)addr6;
548 		nssin.arg.nss.host.len = sizeof (struct in6_addr);
549 		nssin.arg.nss.host.type = AF_INET6;
550 		nssin.arg.nss.host.buf = buf->buffer;
551 		nssin.arg.nss.host.buflen = buf->buflen;
552 
553 		nssout.nss.host.hent = buf->result;
554 		nssout.nss.host.herrno_p = error_num;
555 		/*
556 		 * We pass in nconf and let the implementation of the
557 		 * long-named func decide whether to use the switch based on
558 		 * nc_nlookups.
559 		 */
560 		neterr =
561 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
562 
563 		(void) freenetconfigent(nconf);
564 		if (neterr != ND_OK) {
565 			/* Failover case, try hosts db for v4 address */
566 			hp = buf->result;
567 			if (!gethostbyaddr_r(src, len, type, buf->result,
568 			    buf->buffer, buf->buflen, error_num)) {
569 				__IPv6_cleanup(buf);
570 				return (NULL);
571 			}
572 			free(buf);
573 			return (hp);
574 		}
575 		if ((hp = __mappedtov4(buf->result, error_num)) == NULL) {
576 			__IPv6_cleanup(buf);
577 			return (NULL);
578 		}
579 		__IPv6_cleanup(buf);
580 		return (hp);
581 	}
582 	/*
583 	 * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call.
584 	 */
585 	if (type == AF_INET6) {
586 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
587 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
588 			*error_num = NO_RECOVERY;
589 			return (NULL);
590 		}
591 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
592 			*error_num = NO_RECOVERY;
593 			freenetconfigent(nconf);
594 			return (NULL);
595 		}
596 		nssin.op_t = NSS_HOST6;
597 		nssin.arg.nss.host.addr = (const char *)addr6;
598 		nssin.arg.nss.host.len = len;
599 		nssin.arg.nss.host.type = type;
600 		nssin.arg.nss.host.buf = buf->buffer;
601 		nssin.arg.nss.host.buflen = buf->buflen;
602 
603 		nssout.nss.host.hent = buf->result;
604 		nssout.nss.host.herrno_p = error_num;
605 		/*
606 		 * We pass in nconf and let the implementation of the
607 		 * long-named func decide whether to use the switch based on
608 		 * nc_nlookups.
609 		 */
610 		neterr =
611 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
612 
613 		(void) freenetconfigent(nconf);
614 		if (neterr != ND_OK) {
615 			__IPv6_cleanup(buf);
616 			return (NULL);
617 		}
618 		free(buf);
619 		return (nssout.nss.host.hent);
620 	}
621 	/*
622 	 * If we got here, unknown type.
623 	 */
624 	*error_num = HOST_NOT_FOUND;
625 	return (NULL);
626 }
627 
628 void
629 freehostent(struct hostent *hent)
630 {
631 	free(hent);
632 }
633 
634 static int
635 __ai_addrconfig(int af)
636 {
637 	struct lifnum	lifn;
638 	struct lifconf	lifc;
639 	struct lifreq	*lifp, *buf = NULL;
640 	size_t		bufsize;
641 	hrtime_t	now, *then;
642 	static hrtime_t	then4, then6; /* the last time we updated ifnum# */
643 	static int	ifnum4 = -1, ifnum6 = -1;
644 	int		*num;
645 	int 		nlifr, count = 0;
646 
647 
648 	switch (af) {
649 	case AF_INET:
650 		num = &ifnum4;
651 		then = &then4;
652 		break;
653 	case AF_INET6:
654 		num = &ifnum6;
655 		then = &then6;
656 		break;
657 	default:
658 		return (0);
659 	}
660 
661 	/*
662 	 * We don't need to check this every time someone does a name
663 	 * lookup.  Do it every IFNUM_TIMEOUT for each address family.
664 	 *
665 	 * There's no need to protect all of this with a lock.  The
666 	 * worst that can happen is that we update the interface count
667 	 * twice instead of once.  That's no big deal.
668 	 */
669 	now = gethrtime();
670 	if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) {
671 		lifn.lifn_family = af;
672 		/*
673 		 * We want to determine if this machine knows anything
674 		 * at all about the address family; the status of the
675 		 * interface is less important. Hence, set
676 		 * 'lifn_flags' to zero.
677 		 */
678 		lifn.lifn_flags = 0;
679 again:
680 		if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0)
681 			goto fail;
682 
683 		if (lifn.lifn_count == 0) {
684 			*num = 0;
685 			*then = now;
686 			return (*num);
687 		}
688 
689 		/*
690 		 * Pad the interface count to detect when additional
691 		 * interfaces have been configured between SIOCGLIFNUM
692 		 * and SIOCGLIFCONF.
693 		 */
694 		lifn.lifn_count += 4;
695 
696 		bufsize = lifn.lifn_count * sizeof (struct lifreq);
697 		if ((buf = realloc(buf, bufsize)) == NULL)
698 			goto fail;
699 
700 		lifc.lifc_family = af;
701 		lifc.lifc_flags = 0;
702 		lifc.lifc_len = bufsize;
703 		lifc.lifc_buf = (caddr_t)buf;
704 		if (nss_ioctl(af, SIOCGLIFCONF, &lifc) < 0)
705 			goto fail;
706 
707 		nlifr = lifc.lifc_len / sizeof (struct lifreq);
708 		if (nlifr >= lifn.lifn_count)
709 			goto again;
710 		/*
711 		 * Do not include any loopback addresses, 127.0.0.1 for AF_INET
712 		 * and ::1 for AF_INET6, while counting the number of available
713 		 * IPv4 or IPv6 addresses. (RFC 3493 requires this, whenever
714 		 * AI_ADDRCONFIG flag is set)
715 		 */
716 		for (lifp = buf; lifp < buf + nlifr; lifp++) {
717 			switch (af) {
718 			case AF_INET: {
719 				struct sockaddr_in *in;
720 
721 				in = (struct sockaddr_in *)&lifp->lifr_addr;
722 				if (ntohl(in->sin_addr.s_addr) ==
723 				    INADDR_LOOPBACK) {
724 					count++;
725 				}
726 				break;
727 			}
728 			case AF_INET6: {
729 				struct sockaddr_in6 *in6;
730 
731 				in6 = (struct sockaddr_in6 *)&lifp->lifr_addr;
732 				if (IN6_IS_ADDR_LOOPBACK(&in6->sin6_addr))
733 					count++;
734 				break;
735 			}
736 			}
737 		}
738 		*num = nlifr - count;
739 		*then = now;
740 		free(buf);
741 	}
742 	return (*num);
743 fail:
744 	free(buf);
745 	return (-1);
746 }
747 
748 /*
749  * This routine will either convert an IPv4 address to a mapped or compat
750  * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped
751  * v4 (he4) addresses. In either case, the results are returned in res.
752  * Caller must provide all buffers.
753  * Inputs:
754  * 		he4	pointer to IPv4 buffer
755  *		he6	pointer to IPv6 buffer (NULL if not merging v4/v6
756  *		res	pointer to results buffer
757  *		mapped	mapped == 1, map IPv4 : mapped == 0, compat IPv4
758  *			mapped flag is ignored if he6 != NULL
759  *
760  * The results are packed into the res->buffer as follows:
761  * <--------------- buffer + buflen -------------------------------------->
762  * |-----------------|-----------------|----------------|----------------|
763  * | pointers vector | pointers vector | aliases grow   | addresses grow |
764  * | for addresses   | for aliases     |                |                |
765  * | this way ->     | this way ->     | <- this way    |<- this way     |
766  * |-----------------|-----------------|----------------|----------------|
767  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
768  */
769 static struct hostent *
770 __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res,
771 		int mapped)
772 {
773 	char	*buffer, *limit;
774 	int	buflen = res->buflen;
775 	struct	in6_addr *addr6p;
776 	char	*buff_locp;
777 	struct	hostent *host;
778 	int	count = 0, len, i;
779 	char	*h_namep;
780 
781 	if (he4 == NULL || res == NULL) {
782 		return (NULL);
783 	}
784 	limit = res->buffer + buflen;
785 	host = (struct hostent *)res->result;
786 	buffer = res->buffer;
787 
788 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr));
789 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
790 	if ((char *)host->h_addr_list >= limit ||
791 	    buff_locp <= (char *)host->h_addr_list) {
792 		return (NULL);
793 	}
794 	if (he6 == NULL) {
795 		/*
796 		 * If he6==NULL, map the v4 address into the v6 address format.
797 		 * This is used for getipnodebyaddr() (single address, mapped or
798 		 * compatible) or for v4 mapped for getipnodebyname(), which
799 		 * could be multiple addresses. This could also be a literal
800 		 * address string, which is why there is a inet_addr() call.
801 		 */
802 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
803 			buff_locp -= sizeof (struct in6_addr);
804 			if (buff_locp <=
805 			    (char *)&(host->h_addr_list[count + 1])) {
806 			/*
807 			 * Has to be room for the pointer to the address we're
808 			 * about to add, as well as the final NULL ptr.
809 			 */
810 				return (NULL);
811 			}
812 			/* LINTED pointer cast */
813 			addr6p = (struct in6_addr *)buff_locp;
814 			host->h_addr_list[count] = (char *)addr6p;
815 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
816 			if (mapped) {
817 				addr6p->s6_addr[10] = 0xff;
818 				addr6p->s6_addr[11] = 0xff;
819 			}
820 			bcopy((char *)he4->h_addr_list[i],
821 			    &addr6p->s6_addr[12], sizeof (struct in_addr));
822 			++count;
823 		}
824 		/*
825 		 * Set last array element to NULL and add cname as first alias
826 		 */
827 		host->h_addr_list[count] = NULL;
828 		host->h_aliases = host->h_addr_list + count + 1;
829 		count = 0;
830 		if ((int)(inet_addr(he4->h_name)) != -1) {
831 		/*
832 		 * Literal address string, since we're mapping, we need the IPv6
833 		 * V4 mapped literal address string for h_name.
834 		 */
835 			char	tmpstr[128];
836 			(void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr,
837 			    sizeof (tmpstr));
838 			buff_locp -= (len = strlen(tmpstr) + 1);
839 			h_namep = tmpstr;
840 			if (buff_locp <= (char *)(host->h_aliases))
841 				return (NULL);
842 			bcopy(h_namep, buff_locp, len);
843 			host->h_name = buff_locp;
844 			host->h_aliases = NULL; /* no aliases for literal */
845 			host->h_length = sizeof (struct in6_addr);
846 			host->h_addrtype = AF_INET6;
847 			return (host); 		/* we're done, return result */
848 		}
849 		/*
850 		 * Not a literal address string, so just copy h_name.
851 		 */
852 		buff_locp -= (len = strlen(he4->h_name) + 1);
853 		h_namep = he4->h_name;
854 		if (buff_locp <= (char *)(host->h_aliases))
855 			return (NULL);
856 		bcopy(h_namep, buff_locp, len);
857 		host->h_name = buff_locp;
858 		/*
859 		 * Pass 2 (IPv4 aliases):
860 		 */
861 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
862 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
863 			if (buff_locp <=
864 			    (char *)&(host->h_aliases[count + 1])) {
865 			/*
866 			 * Has to be room for the pointer to the address we're
867 			 * about to add, as well as the final NULL ptr.
868 			 */
869 				return (NULL);
870 			}
871 			host->h_aliases[count] = buff_locp;
872 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
873 			++count;
874 		}
875 		host->h_aliases[count] = NULL;
876 		host->h_length = sizeof (struct in6_addr);
877 		host->h_addrtype = AF_INET6;
878 		return (host);
879 	} else {
880 		/*
881 		 * Merge IPv4 mapped addresses with IPv6 addresses. The
882 		 * IPv6 address will go in first, followed by the v4 mapped.
883 		 *
884 		 * Pass 1 (IPv6 addresses):
885 		 */
886 		for (i = 0; he6->h_addr_list[i] != NULL; i++) {
887 			buff_locp -= sizeof (struct in6_addr);
888 			if (buff_locp <=
889 			    (char *)&(host->h_addr_list[count + 1])) {
890 			/*
891 			 * Has to be room for the pointer to the address we're
892 			 * about to add, as well as the final NULL ptr.
893 			 */
894 				return (NULL);
895 			}
896 			host->h_addr_list[count] = buff_locp;
897 			bcopy((char *)he6->h_addr_list[i], buff_locp,
898 			    sizeof (struct in6_addr));
899 			++count;
900 		}
901 		/*
902 		 * Pass 1 (IPv4 mapped addresses):
903 		 */
904 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
905 			buff_locp -= sizeof (struct in6_addr);
906 			if (buff_locp <=
907 			    (char *)&(host->h_addr_list[count + 1])) {
908 			/*
909 			 * Has to be room for the pointer to the address we're
910 			 * about to add, as well as the final NULL ptr.
911 			 */
912 				return (NULL);
913 			}
914 			/* LINTED pointer cast */
915 			addr6p = (struct in6_addr *)buff_locp;
916 			host->h_addr_list[count] = (char *)addr6p;
917 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
918 			addr6p->s6_addr[10] = 0xff;
919 			addr6p->s6_addr[11] = 0xff;
920 			bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12],
921 			    sizeof (struct in_addr));
922 			++count;
923 		}
924 		/*
925 		 * Pass 2 (IPv6 aliases, host name first). We start h_aliases
926 		 * one after where h_addr_list array ended. This is where cname
927 		 * is put, followed by all aliases. Reset count to 0, for index
928 		 * in the h_aliases array.
929 		 */
930 		host->h_addr_list[count] = NULL;
931 		host->h_aliases = host->h_addr_list + count + 1;
932 		count = 0;
933 		buff_locp -= (len = strlen(he6->h_name) + 1);
934 		if (buff_locp <= (char *)(host->h_aliases))
935 			return (NULL);
936 		bcopy(he6->h_name, buff_locp, len);
937 		host->h_name = buff_locp;
938 		for (i = 0; he6->h_aliases[i] != NULL; i++) {
939 			buff_locp -= (len = strlen(he6->h_aliases[i]) + 1);
940 			if (buff_locp <=
941 			    (char *)&(host->h_aliases[count + 1])) {
942 			/*
943 			 * Has to be room for the pointer to the address we're
944 			 * about to add, as well as the final NULL ptr.
945 			 */
946 				return (NULL);
947 			}
948 			host->h_aliases[count] = buff_locp;
949 			bcopy((char *)he6->h_aliases[i], buff_locp, len);
950 			++count;
951 		}
952 		/*
953 		 * Pass 2 (IPv4 aliases):
954 		 */
955 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
956 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
957 			if (buff_locp <=
958 			    (char *)&(host->h_aliases[count + 1])) {
959 			/*
960 			 * Has to be room for the pointer to the address we're
961 			 * about to add, as well as the final NULL ptr.
962 			 */
963 				return (NULL);
964 			}
965 			host->h_aliases[count] = buff_locp;
966 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
967 			++count;
968 		}
969 		host->h_aliases[count] = NULL;
970 		host->h_length = sizeof (struct in6_addr);
971 		host->h_addrtype = AF_INET6;
972 		return (host);
973 	}
974 }
975 
976 /*
977  * This routine will convert a mapped v4 hostent (AF_INET6) to a
978  * AF_INET hostent. If no mapped addrs found, then a NULL is returned.
979  * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped
980  * addresses are extracted and copied to it. On sucess, a pointer to a new
981  * hostent is returned.
982  * There are two possible errors in which case a NULL is returned.
983  * One of two error codes are returned:
984  *
985  * NO_RECOVERY - a malloc failed or the like for which there's no recovery.
986  * NO_ADDRESS - after filtering all the v4, there was nothing left!
987  *
988  * Inputs:
989  *              he              pointer to hostent with mapped v4 addresses
990  *              filter_error    pointer to return error code
991  * Return:
992  *		pointer to a malloc'd hostent with v4 addresses.
993  *
994  * The results are packed into the res->buffer as follows:
995  * <--------------- buffer + buflen -------------------------------------->
996  * |-----------------|-----------------|----------------|----------------|
997  * | pointers vector | pointers vector | aliases grow   | addresses grow |
998  * | for addresses   | for aliases     |                |                |
999  * | this way ->     | this way ->     | <- this way    |<- this way     |
1000  * |-----------------|-----------------|----------------|----------------|
1001  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
1002  */
1003 struct hostent *
1004 __mappedtov4(struct hostent *he, int *extract_error)
1005 {
1006 	char	*buffer, *limit;
1007 	nss_XbyY_buf_t *res;
1008 	int	buflen = NSS_BUFLEN_HOSTS;
1009 	struct	in_addr *addr4p;
1010 	char	*buff_locp;
1011 	struct	hostent *host;
1012 	int	count = 0, len, i;
1013 	char	*h_namep;
1014 
1015 	if (he == NULL) {
1016 		*extract_error = NO_ADDRESS;
1017 		return (NULL);
1018 	}
1019 	if ((__find_mapped(he, 0)) == 0) {
1020 		*extract_error = NO_ADDRESS;
1021 		return (NULL);
1022 	}
1023 	if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) {
1024 		*extract_error = NO_RECOVERY;
1025 		return (NULL);
1026 	}
1027 	limit = res->buffer + buflen;
1028 	host = (struct hostent *)res->result;
1029 	buffer = res->buffer;
1030 
1031 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr));
1032 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
1033 	if ((char *)host->h_addr_list >= limit ||
1034 	    buff_locp <= (char *)host->h_addr_list)
1035 		goto cleanup;
1036 	/*
1037 	 * "Unmap" the v4 mapped address(es) into a v4 hostent format.
1038 	 * This is used for getipnodebyaddr() (single address) or for
1039 	 * v4 mapped for getipnodebyname(), which could be multiple
1040 	 * addresses. This could also be a literal address string,
1041 	 * which is why there is a inet_addr() call.
1042 	 */
1043 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
1044 		/* LINTED pointer cast */
1045 		if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *)
1046 		    he->h_addr_list[i]))
1047 			continue;
1048 		buff_locp -= sizeof (struct in6_addr);
1049 		/*
1050 		 * Has to be room for the pointer to the address we're
1051 		 * about to add, as well as the final NULL ptr.
1052 		 */
1053 		if (buff_locp <=
1054 		    (char *)&(host->h_addr_list[count + 1]))
1055 			goto cleanup;
1056 		/* LINTED pointer cast */
1057 		addr4p = (struct in_addr *)buff_locp;
1058 		host->h_addr_list[count] = (char *)addr4p;
1059 		bzero((char *)&addr4p->s_addr,
1060 		    sizeof (struct in_addr));
1061 		/* LINTED pointer cast */
1062 		IN6_V4MAPPED_TO_INADDR(
1063 		    (struct in6_addr *)he->h_addr_list[i], addr4p);
1064 		++count;
1065 	}
1066 	/*
1067 	 * Set last array element to NULL and add cname as first alias
1068 	 */
1069 	host->h_addr_list[count] = NULL;
1070 	host->h_aliases = host->h_addr_list + count + 1;
1071 	count = 0;
1072 	/* Copy official host name */
1073 	buff_locp -= (len = strlen(he->h_name) + 1);
1074 	h_namep = he->h_name;
1075 	if (buff_locp <= (char *)(host->h_aliases))
1076 		goto cleanup;
1077 	bcopy(h_namep, buff_locp, len);
1078 	host->h_name = buff_locp;
1079 	/*
1080 	 * Pass 2 (IPv4 aliases):
1081 	 */
1082 	if (he->h_aliases != NULL) {
1083 		for (i = 0; he->h_aliases[i] != NULL; i++) {
1084 			buff_locp -= (len = strlen(he->h_aliases[i]) + 1);
1085 			/*
1086 			 * Has to be room for the pointer to the address we're
1087 			 * about to add, as well as the final NULL ptr.
1088 			 */
1089 			if (buff_locp <=
1090 			    (char *)&(host->h_aliases[count + 1]))
1091 				goto cleanup;
1092 			host->h_aliases[count] = buff_locp;
1093 			bcopy((char *)he->h_aliases[i], buff_locp, len);
1094 			++count;
1095 		}
1096 	}
1097 	host->h_aliases[count] = NULL;
1098 	host->h_length = sizeof (struct in_addr);
1099 	host->h_addrtype = AF_INET;
1100 	free(res);
1101 	return (host);
1102 cleanup:
1103 	*extract_error = NO_RECOVERY;
1104 	(void) __IPv6_cleanup(res);
1105 	return (NULL);
1106 }
1107 
1108 /*
1109  * This routine takes as input a pointer to a hostent and filters out
1110  * the type of addresses specified by the af argument.  AF_INET
1111  * indicates that the caller wishes to filter out IPv4-mapped
1112  * addresses, and AF_INET6 indicates that the caller wishes to filter
1113  * out IPv6 addresses which aren't IPv4-mapped.  If filtering would
1114  * result in all addresses being filtered out, a NULL pointer is returned.
1115  * Otherwise, the he pointer passed in is returned, even if no addresses
1116  * were filtered out.
1117  */
1118 static struct hostent *
1119 __filter_addresses(int af, struct hostent *he)
1120 {
1121 	struct in6_addr	**in6addrlist, **in6addr;
1122 	boolean_t	isipv4mapped;
1123 	int		i = 0;
1124 
1125 	if (he == NULL)
1126 		return (NULL);
1127 
1128 	in6addrlist = (struct in6_addr **)he->h_addr_list;
1129 	for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
1130 		isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr);
1131 
1132 		if ((af == AF_INET && !isipv4mapped) ||
1133 		    (af == AF_INET6 && isipv4mapped)) {
1134 			if (in6addrlist[i] != *in6addr)
1135 				in6addrlist[i] = *in6addr;
1136 			i++;
1137 		}
1138 	}
1139 
1140 	if (i == 0) {
1141 		/* We filtered everything out. */
1142 		return (NULL);
1143 	} else {
1144 		/* NULL terminate the list and return the hostent */
1145 		in6addrlist[i] = NULL;
1146 		return (he);
1147 	}
1148 }
1149 
1150 /*
1151  * This routine searches a hostent for v4 mapped IPv6 addresses.
1152  * he		hostent structure to seach
1153  * find_both	flag indicating if only want mapped or both map'd and v6
1154  * return values:
1155  * 			0 = No mapped addresses
1156  *			1 = Mapped v4 address found (returns on first one found)
1157  *			2 = Both v6 and v4 mapped are present
1158  *
1159  * If hostent passed in with no addresses, zero will be returned.
1160  */
1161 
1162 static int
1163 __find_mapped(struct hostent *he, int find_both)
1164 {
1165 	int i;
1166 	int mapd_found = 0;
1167 	int v6_found = 0;
1168 
1169 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
1170 		/* LINTED pointer cast */
1171 		if (IN6_IS_ADDR_V4MAPPED(
1172 				(struct in6_addr *)he->h_addr_list[i])) {
1173 			if (find_both)
1174 				mapd_found = 1;
1175 			else
1176 				return (1);
1177 		} else {
1178 			v6_found = 1;
1179 		}
1180 		/* save some iterations once both found */
1181 		if (mapd_found && v6_found)
1182 			return (2);
1183 	}
1184 	return (mapd_found);
1185 }
1186 
1187 /*
1188  * This routine was added specifically for the IPv6 getipnodeby*() APIs. This
1189  * separates the result pointer (ptr to hostent+data buf) from the
1190  * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr
1191  * can be passed to freehostent() and freed independently.
1192  *
1193  *   bufp->result    bufp->buffer
1194  *		|		|
1195  *		V		V
1196  *		------------------------------------------------...--
1197  *		|struct hostent	|addresses		     aliases |
1198  *		------------------------------------------------...--
1199  *		|               |<--------bufp->buflen-------------->|
1200  */
1201 
1202 #define	ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1))
1203 
1204 static nss_XbyY_buf_t *
1205 __IPv6_alloc(int bufsz)
1206 {
1207 	nss_XbyY_buf_t *bufp;
1208 
1209 	if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL)
1210 		return (NULL);
1211 
1212 	if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) ==
1213 	    NULL) {
1214 		free(bufp);
1215 		return (NULL);
1216 	}
1217 	bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent);
1218 	bufp->buflen = bufsz;
1219 	return (bufp);
1220 }
1221 
1222 /*
1223  * This routine is use only for error return cleanup. This will free the
1224  * hostent pointer, so don't use for successful returns.
1225  */
1226 static void
1227 __IPv6_cleanup(nss_XbyY_buf_t *bufp)
1228 {
1229 	if (bufp == NULL)
1230 		return;
1231 	if (bufp->result != NULL)
1232 		free(bufp->result);
1233 	free(bufp);
1234 }
1235