xref: /titanic_50/usr/src/lib/libnsl/nss/getipnodeby.c (revision c9a6ea2e938727c95af7108c5e00eee4c890c7ae)
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 2010 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 	/*
746 	 * If the process is running without the NET_ACCESS basic privilege,
747 	 * pretend we still have inet/inet6 interfaces.
748 	 */
749 	if (errno == EACCES)
750 		return (1);
751 	return (-1);
752 }
753 
754 /*
755  * This routine will either convert an IPv4 address to a mapped or compat
756  * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped
757  * v4 (he4) addresses. In either case, the results are returned in res.
758  * Caller must provide all buffers.
759  * Inputs:
760  * 		he4	pointer to IPv4 buffer
761  *		he6	pointer to IPv6 buffer (NULL if not merging v4/v6
762  *		res	pointer to results buffer
763  *		mapped	mapped == 1, map IPv4 : mapped == 0, compat IPv4
764  *			mapped flag is ignored if he6 != NULL
765  *
766  * The results are packed into the res->buffer as follows:
767  * <--------------- buffer + buflen -------------------------------------->
768  * |-----------------|-----------------|----------------|----------------|
769  * | pointers vector | pointers vector | aliases grow   | addresses grow |
770  * | for addresses   | for aliases     |                |                |
771  * | this way ->     | this way ->     | <- this way    |<- this way     |
772  * |-----------------|-----------------|----------------|----------------|
773  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
774  */
775 static struct hostent *
776 __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res,
777 		int mapped)
778 {
779 	char	*buffer, *limit;
780 	int	buflen = res->buflen;
781 	struct	in6_addr *addr6p;
782 	char	*buff_locp;
783 	struct	hostent *host;
784 	int	count = 0, len, i;
785 	char	*h_namep;
786 
787 	if (he4 == NULL || res == NULL) {
788 		return (NULL);
789 	}
790 	limit = res->buffer + buflen;
791 	host = (struct hostent *)res->result;
792 	buffer = res->buffer;
793 
794 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr));
795 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
796 	if ((char *)host->h_addr_list >= limit ||
797 	    buff_locp <= (char *)host->h_addr_list) {
798 		return (NULL);
799 	}
800 	if (he6 == NULL) {
801 		/*
802 		 * If he6==NULL, map the v4 address into the v6 address format.
803 		 * This is used for getipnodebyaddr() (single address, mapped or
804 		 * compatible) or for v4 mapped for getipnodebyname(), which
805 		 * could be multiple addresses. This could also be a literal
806 		 * address string, which is why there is a inet_addr() call.
807 		 */
808 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
809 			buff_locp -= sizeof (struct in6_addr);
810 			if (buff_locp <=
811 			    (char *)&(host->h_addr_list[count + 1])) {
812 			/*
813 			 * Has to be room for the pointer to the address we're
814 			 * about to add, as well as the final NULL ptr.
815 			 */
816 				return (NULL);
817 			}
818 			/* LINTED pointer cast */
819 			addr6p = (struct in6_addr *)buff_locp;
820 			host->h_addr_list[count] = (char *)addr6p;
821 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
822 			if (mapped) {
823 				addr6p->s6_addr[10] = 0xff;
824 				addr6p->s6_addr[11] = 0xff;
825 			}
826 			bcopy((char *)he4->h_addr_list[i],
827 			    &addr6p->s6_addr[12], sizeof (struct in_addr));
828 			++count;
829 		}
830 		/*
831 		 * Set last array element to NULL and add cname as first alias
832 		 */
833 		host->h_addr_list[count] = NULL;
834 		host->h_aliases = host->h_addr_list + count + 1;
835 		count = 0;
836 		if ((int)(inet_addr(he4->h_name)) != -1) {
837 		/*
838 		 * Literal address string, since we're mapping, we need the IPv6
839 		 * V4 mapped literal address string for h_name.
840 		 */
841 			char	tmpstr[128];
842 			(void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr,
843 			    sizeof (tmpstr));
844 			buff_locp -= (len = strlen(tmpstr) + 1);
845 			h_namep = tmpstr;
846 			if (buff_locp <= (char *)(host->h_aliases))
847 				return (NULL);
848 			bcopy(h_namep, buff_locp, len);
849 			host->h_name = buff_locp;
850 			host->h_aliases = NULL; /* no aliases for literal */
851 			host->h_length = sizeof (struct in6_addr);
852 			host->h_addrtype = AF_INET6;
853 			return (host); 		/* we're done, return result */
854 		}
855 		/*
856 		 * Not a literal address string, so just copy h_name.
857 		 */
858 		buff_locp -= (len = strlen(he4->h_name) + 1);
859 		h_namep = he4->h_name;
860 		if (buff_locp <= (char *)(host->h_aliases))
861 			return (NULL);
862 		bcopy(h_namep, buff_locp, len);
863 		host->h_name = buff_locp;
864 		/*
865 		 * Pass 2 (IPv4 aliases):
866 		 */
867 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
868 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
869 			if (buff_locp <=
870 			    (char *)&(host->h_aliases[count + 1])) {
871 			/*
872 			 * Has to be room for the pointer to the address we're
873 			 * about to add, as well as the final NULL ptr.
874 			 */
875 				return (NULL);
876 			}
877 			host->h_aliases[count] = buff_locp;
878 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
879 			++count;
880 		}
881 		host->h_aliases[count] = NULL;
882 		host->h_length = sizeof (struct in6_addr);
883 		host->h_addrtype = AF_INET6;
884 		return (host);
885 	} else {
886 		/*
887 		 * Merge IPv4 mapped addresses with IPv6 addresses. The
888 		 * IPv6 address will go in first, followed by the v4 mapped.
889 		 *
890 		 * Pass 1 (IPv6 addresses):
891 		 */
892 		for (i = 0; he6->h_addr_list[i] != NULL; i++) {
893 			buff_locp -= sizeof (struct in6_addr);
894 			if (buff_locp <=
895 			    (char *)&(host->h_addr_list[count + 1])) {
896 			/*
897 			 * Has to be room for the pointer to the address we're
898 			 * about to add, as well as the final NULL ptr.
899 			 */
900 				return (NULL);
901 			}
902 			host->h_addr_list[count] = buff_locp;
903 			bcopy((char *)he6->h_addr_list[i], buff_locp,
904 			    sizeof (struct in6_addr));
905 			++count;
906 		}
907 		/*
908 		 * Pass 1 (IPv4 mapped addresses):
909 		 */
910 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
911 			buff_locp -= sizeof (struct in6_addr);
912 			if (buff_locp <=
913 			    (char *)&(host->h_addr_list[count + 1])) {
914 			/*
915 			 * Has to be room for the pointer to the address we're
916 			 * about to add, as well as the final NULL ptr.
917 			 */
918 				return (NULL);
919 			}
920 			/* LINTED pointer cast */
921 			addr6p = (struct in6_addr *)buff_locp;
922 			host->h_addr_list[count] = (char *)addr6p;
923 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
924 			addr6p->s6_addr[10] = 0xff;
925 			addr6p->s6_addr[11] = 0xff;
926 			bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12],
927 			    sizeof (struct in_addr));
928 			++count;
929 		}
930 		/*
931 		 * Pass 2 (IPv6 aliases, host name first). We start h_aliases
932 		 * one after where h_addr_list array ended. This is where cname
933 		 * is put, followed by all aliases. Reset count to 0, for index
934 		 * in the h_aliases array.
935 		 */
936 		host->h_addr_list[count] = NULL;
937 		host->h_aliases = host->h_addr_list + count + 1;
938 		count = 0;
939 		buff_locp -= (len = strlen(he6->h_name) + 1);
940 		if (buff_locp <= (char *)(host->h_aliases))
941 			return (NULL);
942 		bcopy(he6->h_name, buff_locp, len);
943 		host->h_name = buff_locp;
944 		for (i = 0; he6->h_aliases[i] != NULL; i++) {
945 			buff_locp -= (len = strlen(he6->h_aliases[i]) + 1);
946 			if (buff_locp <=
947 			    (char *)&(host->h_aliases[count + 1])) {
948 			/*
949 			 * Has to be room for the pointer to the address we're
950 			 * about to add, as well as the final NULL ptr.
951 			 */
952 				return (NULL);
953 			}
954 			host->h_aliases[count] = buff_locp;
955 			bcopy((char *)he6->h_aliases[i], buff_locp, len);
956 			++count;
957 		}
958 		/*
959 		 * Pass 2 (IPv4 aliases):
960 		 */
961 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
962 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
963 			if (buff_locp <=
964 			    (char *)&(host->h_aliases[count + 1])) {
965 			/*
966 			 * Has to be room for the pointer to the address we're
967 			 * about to add, as well as the final NULL ptr.
968 			 */
969 				return (NULL);
970 			}
971 			host->h_aliases[count] = buff_locp;
972 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
973 			++count;
974 		}
975 		host->h_aliases[count] = NULL;
976 		host->h_length = sizeof (struct in6_addr);
977 		host->h_addrtype = AF_INET6;
978 		return (host);
979 	}
980 }
981 
982 /*
983  * This routine will convert a mapped v4 hostent (AF_INET6) to a
984  * AF_INET hostent. If no mapped addrs found, then a NULL is returned.
985  * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped
986  * addresses are extracted and copied to it. On sucess, a pointer to a new
987  * hostent is returned.
988  * There are two possible errors in which case a NULL is returned.
989  * One of two error codes are returned:
990  *
991  * NO_RECOVERY - a malloc failed or the like for which there's no recovery.
992  * NO_ADDRESS - after filtering all the v4, there was nothing left!
993  *
994  * Inputs:
995  *              he              pointer to hostent with mapped v4 addresses
996  *              filter_error    pointer to return error code
997  * Return:
998  *		pointer to a malloc'd hostent with v4 addresses.
999  *
1000  * The results are packed into the res->buffer as follows:
1001  * <--------------- buffer + buflen -------------------------------------->
1002  * |-----------------|-----------------|----------------|----------------|
1003  * | pointers vector | pointers vector | aliases grow   | addresses grow |
1004  * | for addresses   | for aliases     |                |                |
1005  * | this way ->     | this way ->     | <- this way    |<- this way     |
1006  * |-----------------|-----------------|----------------|----------------|
1007  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
1008  */
1009 struct hostent *
1010 __mappedtov4(struct hostent *he, int *extract_error)
1011 {
1012 	char	*buffer, *limit;
1013 	nss_XbyY_buf_t *res;
1014 	int	buflen = NSS_BUFLEN_HOSTS;
1015 	struct	in_addr *addr4p;
1016 	char	*buff_locp;
1017 	struct	hostent *host;
1018 	int	count = 0, len, i;
1019 	char	*h_namep;
1020 
1021 	if (he == NULL) {
1022 		*extract_error = NO_ADDRESS;
1023 		return (NULL);
1024 	}
1025 	if ((__find_mapped(he, 0)) == 0) {
1026 		*extract_error = NO_ADDRESS;
1027 		return (NULL);
1028 	}
1029 	if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) {
1030 		*extract_error = NO_RECOVERY;
1031 		return (NULL);
1032 	}
1033 	limit = res->buffer + buflen;
1034 	host = (struct hostent *)res->result;
1035 	buffer = res->buffer;
1036 
1037 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr));
1038 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
1039 	if ((char *)host->h_addr_list >= limit ||
1040 	    buff_locp <= (char *)host->h_addr_list)
1041 		goto cleanup;
1042 	/*
1043 	 * "Unmap" the v4 mapped address(es) into a v4 hostent format.
1044 	 * This is used for getipnodebyaddr() (single address) or for
1045 	 * v4 mapped for getipnodebyname(), which could be multiple
1046 	 * addresses. This could also be a literal address string,
1047 	 * which is why there is a inet_addr() call.
1048 	 */
1049 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
1050 		/* LINTED pointer cast */
1051 		if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *)
1052 		    he->h_addr_list[i]))
1053 			continue;
1054 		buff_locp -= sizeof (struct in6_addr);
1055 		/*
1056 		 * Has to be room for the pointer to the address we're
1057 		 * about to add, as well as the final NULL ptr.
1058 		 */
1059 		if (buff_locp <=
1060 		    (char *)&(host->h_addr_list[count + 1]))
1061 			goto cleanup;
1062 		/* LINTED pointer cast */
1063 		addr4p = (struct in_addr *)buff_locp;
1064 		host->h_addr_list[count] = (char *)addr4p;
1065 		bzero((char *)&addr4p->s_addr,
1066 		    sizeof (struct in_addr));
1067 		/* LINTED pointer cast */
1068 		IN6_V4MAPPED_TO_INADDR(
1069 		    (struct in6_addr *)he->h_addr_list[i], addr4p);
1070 		++count;
1071 	}
1072 	/*
1073 	 * Set last array element to NULL and add cname as first alias
1074 	 */
1075 	host->h_addr_list[count] = NULL;
1076 	host->h_aliases = host->h_addr_list + count + 1;
1077 	count = 0;
1078 	/* Copy official host name */
1079 	buff_locp -= (len = strlen(he->h_name) + 1);
1080 	h_namep = he->h_name;
1081 	if (buff_locp <= (char *)(host->h_aliases))
1082 		goto cleanup;
1083 	bcopy(h_namep, buff_locp, len);
1084 	host->h_name = buff_locp;
1085 	/*
1086 	 * Pass 2 (IPv4 aliases):
1087 	 */
1088 	if (he->h_aliases != NULL) {
1089 		for (i = 0; he->h_aliases[i] != NULL; i++) {
1090 			buff_locp -= (len = strlen(he->h_aliases[i]) + 1);
1091 			/*
1092 			 * Has to be room for the pointer to the address we're
1093 			 * about to add, as well as the final NULL ptr.
1094 			 */
1095 			if (buff_locp <=
1096 			    (char *)&(host->h_aliases[count + 1]))
1097 				goto cleanup;
1098 			host->h_aliases[count] = buff_locp;
1099 			bcopy((char *)he->h_aliases[i], buff_locp, len);
1100 			++count;
1101 		}
1102 	}
1103 	host->h_aliases[count] = NULL;
1104 	host->h_length = sizeof (struct in_addr);
1105 	host->h_addrtype = AF_INET;
1106 	free(res);
1107 	return (host);
1108 cleanup:
1109 	*extract_error = NO_RECOVERY;
1110 	(void) __IPv6_cleanup(res);
1111 	return (NULL);
1112 }
1113 
1114 /*
1115  * This routine takes as input a pointer to a hostent and filters out
1116  * the type of addresses specified by the af argument.  AF_INET
1117  * indicates that the caller wishes to filter out IPv4-mapped
1118  * addresses, and AF_INET6 indicates that the caller wishes to filter
1119  * out IPv6 addresses which aren't IPv4-mapped.  If filtering would
1120  * result in all addresses being filtered out, a NULL pointer is returned.
1121  * Otherwise, the he pointer passed in is returned, even if no addresses
1122  * were filtered out.
1123  */
1124 static struct hostent *
1125 __filter_addresses(int af, struct hostent *he)
1126 {
1127 	struct in6_addr	**in6addrlist, **in6addr;
1128 	boolean_t	isipv4mapped;
1129 	int		i = 0;
1130 
1131 	if (he == NULL)
1132 		return (NULL);
1133 
1134 	in6addrlist = (struct in6_addr **)he->h_addr_list;
1135 	for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
1136 		isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr);
1137 
1138 		if ((af == AF_INET && !isipv4mapped) ||
1139 		    (af == AF_INET6 && isipv4mapped)) {
1140 			if (in6addrlist[i] != *in6addr)
1141 				in6addrlist[i] = *in6addr;
1142 			i++;
1143 		}
1144 	}
1145 
1146 	if (i == 0) {
1147 		/* We filtered everything out. */
1148 		return (NULL);
1149 	} else {
1150 		/* NULL terminate the list and return the hostent */
1151 		in6addrlist[i] = NULL;
1152 		return (he);
1153 	}
1154 }
1155 
1156 /*
1157  * This routine searches a hostent for v4 mapped IPv6 addresses.
1158  * he		hostent structure to seach
1159  * find_both	flag indicating if only want mapped or both map'd and v6
1160  * return values:
1161  * 			0 = No mapped addresses
1162  *			1 = Mapped v4 address found (returns on first one found)
1163  *			2 = Both v6 and v4 mapped are present
1164  *
1165  * If hostent passed in with no addresses, zero will be returned.
1166  */
1167 
1168 static int
1169 __find_mapped(struct hostent *he, int find_both)
1170 {
1171 	int i;
1172 	int mapd_found = 0;
1173 	int v6_found = 0;
1174 
1175 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
1176 		/* LINTED pointer cast */
1177 		if (IN6_IS_ADDR_V4MAPPED(
1178 				(struct in6_addr *)he->h_addr_list[i])) {
1179 			if (find_both)
1180 				mapd_found = 1;
1181 			else
1182 				return (1);
1183 		} else {
1184 			v6_found = 1;
1185 		}
1186 		/* save some iterations once both found */
1187 		if (mapd_found && v6_found)
1188 			return (2);
1189 	}
1190 	return (mapd_found);
1191 }
1192 
1193 /*
1194  * This routine was added specifically for the IPv6 getipnodeby*() APIs. This
1195  * separates the result pointer (ptr to hostent+data buf) from the
1196  * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr
1197  * can be passed to freehostent() and freed independently.
1198  *
1199  *   bufp->result    bufp->buffer
1200  *		|		|
1201  *		V		V
1202  *		------------------------------------------------...--
1203  *		|struct hostent	|addresses		     aliases |
1204  *		------------------------------------------------...--
1205  *		|               |<--------bufp->buflen-------------->|
1206  */
1207 
1208 #define	ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1))
1209 
1210 static nss_XbyY_buf_t *
1211 __IPv6_alloc(int bufsz)
1212 {
1213 	nss_XbyY_buf_t *bufp;
1214 
1215 	if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL)
1216 		return (NULL);
1217 
1218 	if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) ==
1219 	    NULL) {
1220 		free(bufp);
1221 		return (NULL);
1222 	}
1223 	bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent);
1224 	bufp->buflen = bufsz;
1225 	return (bufp);
1226 }
1227 
1228 /*
1229  * This routine is use only for error return cleanup. This will free the
1230  * hostent pointer, so don't use for successful returns.
1231  */
1232 static void
1233 __IPv6_cleanup(nss_XbyY_buf_t *bufp)
1234 {
1235 	if (bufp == NULL)
1236 		return;
1237 	if (bufp->result != NULL)
1238 		free(bufp->result);
1239 	free(bufp);
1240 }
1241