summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/nss/getipnodeby.c
blob: a4e4515273d3db9fe87a9a11aabb11b90415f807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2016 Joyent, Inc.
 *
 * This file defines and implements the re-entrant getipnodebyname(),
 * getipnodebyaddr(), and freehostent() routines for IPv6. These routines
 * follow use the netdir_getbyYY() (see netdir_inet.c).
 *
 * lib/libnsl/nss/getipnodeby.c
 */

#include "mt.h"
#include <stdlib.h>
#include <unistd.h>
#include <stropts.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <nss_dbdefs.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <nss_netdir.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdir.h>
#include <thread.h>
#include <synch.h>
#include <fcntl.h>
#include <sys/time.h>
#include "nss.h"

#define	IPV6_LITERAL_CHAR	':'

/*
 * The number of nanoseconds getipnodebyname() waits before getting
 * fresh interface count information with SIOCGLIFNUM.  The default is
 * five minutes.
 */
#define	IFNUM_TIMEOUT	((hrtime_t)300 * NANOSEC)

/*
 * Bits in the bitfield returned by getipnodebyname_processflags().
 *
 * IPNODE_WANTIPV6	The user wants IPv6 addresses returned.
 * IPNODE_WANTIPV4	The user wants IPv4 addresses returned.
 * IPNODE_IPV4IFNOIPV6	The user only wants IPv4 addresses returned if no IPv6
 *			addresses are returned.
 * IPNODE_LOOKUPIPNODES	getipnodebyname() needs to lookup the name in ipnodes.
 * IPNODE_LOOKUPHOSTS	getipnodebyname() needs to lookup the name in hosts.
 * IPNODE_ISLITERAL	The name supplied is a literal address string.
 * IPNODE_UNMAP		The user doesn't want v4 mapped addresses if no IPv6
 * 			interfaces are plumbed on the system.
 */
#define	IPNODE_WANTIPV6		0x00000001u
#define	IPNODE_WANTIPV4		0x00000002u
#define	IPNODE_IPV4IFNOIPV6	0x00000004u
#define	IPNODE_LOOKUPIPNODES	0x00000008u
#define	IPNODE_LOOKUPHOSTS	0x00000010u
#define	IPNODE_LITERAL		0x00000020u
#define	IPNODE_UNMAP		0x00000040u
#define	IPNODE_IPV4		(IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6)

/*
 * The private flag between libsocket and libnsl. See
 * lib/libsocket/inet/getaddrinfo.c for more information.
 */
#define	AI_ADDRINFO	0x8000

/*
 * The default set of bits corresponding to a getipnodebyname() flags
 * argument of AI_DEFAULT.
 */
#define	IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \
	IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS)

extern struct netconfig *__rpc_getconfip(char *);

static struct hostent *__mapv4tov6(struct hostent *, struct hostent *,
    nss_XbyY_buf_t *, int);
struct hostent *__mappedtov4(struct hostent *, int *);
static struct hostent *__filter_addresses(int, struct hostent *);
static int __find_mapped(struct hostent *, int);
static nss_XbyY_buf_t *__IPv6_alloc(int);
static void __IPv6_cleanup(nss_XbyY_buf_t *);
static int __ai_addrconfig(int, boolean_t);


#ifdef PIC
struct hostent *
_uncached_getipnodebyname(const char *nam, struct hostent *result,
	char *buffer, int buflen, int af_family, int flags, int *h_errnop)
{
	return (_switch_getipnodebyname_r(nam, result, buffer, buflen,
	    af_family, flags, h_errnop));
}

struct hostent *
_uncached_getipnodebyaddr(const char *addr, int length, int type,
	struct hostent *result, char *buffer, int buflen, int *h_errnop)
{
	if (type == AF_INET)
		return (_switch_gethostbyaddr_r(addr, length, type,
		    result, buffer, buflen, h_errnop));
	else if (type == AF_INET6)
		return (_switch_getipnodebyaddr_r(addr, length, type,
		    result, buffer, buflen, h_errnop));
	return (NULL);
}
#endif

/*
 * Given a name, an address family, and a set of flags, return a
 * bitfield that getipnodebyname() will use.
 */
static uint_t
getipnodebyname_processflags(const char *name, int af, int flags)
{
	uint_t		ipnode_bits = IPNODE_DEFAULT;
	boolean_t	ipv6configured = B_FALSE;
	boolean_t	ipv4configured = B_FALSE;

	/*
	 * If AI_ADDRCONFIG is specified, we need to determine the number of
	 * addresses of each address family configured on the system as
	 * appropriate.
	 *
	 * When trying to determine which addresses should be used for
	 * addrconfig, we first ignore loopback devices. This generally makes
	 * sense as policy, as most of these queries will be trying to go
	 * off-box and one should not have an IPv6 loopback address suggest that
	 * we can now send IPv6 traffic off the box or the equivalent with IPv4.
	 * However, it's possible that no non-loopback interfaces are up on the
	 * box. In those cases, we then check which interfaces are up and
	 * consider loopback devices. While this isn't to the letter of RFC 3493
	 * (which itself is a bit vague in this case, as is SUS), it matches
	 * expected user behavior in these situations.
	 */
	if (flags & AI_ADDRCONFIG) {
		boolean_t hv4, hv6;

		hv4 = __ai_addrconfig(AF_INET, B_FALSE) > 0;
		hv6 = __ai_addrconfig(AF_INET6, B_FALSE) > 0;

		if (hv4 == B_FALSE && hv6 == B_FALSE) {
			hv4 = __ai_addrconfig(AF_INET, B_TRUE) > 0;
			hv6 = __ai_addrconfig(AF_INET6, B_TRUE) > 0;
		}

		ipv6configured = (af == AF_INET6 && hv6);
		ipv4configured = (af == AF_INET || (flags & AI_V4MAPPED)) &&
		    hv4;
	}

	/*
	 * Determine what kinds of addresses the user is interested
	 * in getting back.
	 */
	switch (af) {
	case AF_INET6:
		if ((flags & AI_ADDRCONFIG) && !ipv6configured)
			ipnode_bits &= ~IPNODE_WANTIPV6;

		if (flags & AI_V4MAPPED) {
			if ((flags & AI_ADDRCONFIG) && !ipv4configured) {
				ipnode_bits &= ~IPNODE_IPV4;
			} else if (flags & AI_ALL) {
				ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
			}
			if ((flags & AI_ADDRCONFIG) && !ipv6configured &&
			    (flags & AI_ADDRINFO)) {
				ipnode_bits |= IPNODE_UNMAP;
			}
		} else {
			ipnode_bits &= ~IPNODE_IPV4;
		}
		break;
	case AF_INET:
		if ((flags & AI_ADDRCONFIG) && !ipv4configured)
			ipnode_bits &= ~IPNODE_IPV4;
		ipnode_bits &= ~IPNODE_WANTIPV6;
		ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
		break;
	default:
		ipnode_bits = 0;
		break;
	}

	/*
	 * If we're not looking for IPv4 addresses, don't bother looking
	 * in hosts.
	 */
	if (!(ipnode_bits & IPNODE_WANTIPV4))
		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;

	/*
	 * Determine if name is a literal IP address.  This will
	 * further narrow down what type of lookup we're going to do.
	 */
	if (strchr(name, IPV6_LITERAL_CHAR) != NULL) {
		/* Literal IPv6 address */
		ipnode_bits |= IPNODE_LITERAL;
		/*
		 * In s9 we accepted the literal without filtering independent
		 * of what family was passed in hints.  We continue to do
		 * this.
		 */
		ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4);
		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
	} else if (inet_addr(name) != 0xffffffffU) {
		/* Literal IPv4 address */
		ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4);
		ipnode_bits &= ~IPNODE_WANTIPV6;
		ipnode_bits &= ~IPNODE_LOOKUPIPNODES;
	}
	return (ipnode_bits);
}

struct hostent *
getipnodebyname(const char *name, int af, int flags, int *error_num)
{
	struct hostent		*hp = NULL;
	nss_XbyY_buf_t		*buf4 = NULL;
	nss_XbyY_buf_t		*buf6 = NULL;
	struct netconfig	*nconf;
	struct nss_netdirbyname_in	nssin;
	union nss_netdirbyname_out	nssout;
	int			ret;
	uint_t			ipnode_bits;

	if ((nconf = __rpc_getconfip("udp")) == NULL &&
	    (nconf = __rpc_getconfip("tcp")) == NULL) {
		*error_num = NO_RECOVERY;
		return (NULL);
	}

	ipnode_bits = getipnodebyname_processflags(name, af, flags);

	/* Make sure we have something to look up. */
	if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) {
		*error_num = HOST_NOT_FOUND;
		goto cleanup;
	}

	/*
	 * Perform the requested lookups.  We always look through
	 * ipnodes first for both IPv4 and IPv6 addresses.  Depending
	 * on what was returned and what was needed, we either filter
	 * out the garbage, or ask for more using hosts.
	 */
	if (ipnode_bits & IPNODE_LOOKUPIPNODES) {
		if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) {
			*error_num = NO_RECOVERY;
			goto cleanup;
		}
		nssin.op_t = NSS_HOST6;
		nssin.arg.nss.host6.name = name;
		nssin.arg.nss.host6.buf = buf6->buffer;
		nssin.arg.nss.host6.buflen = buf6->buflen;
		nssin.arg.nss.host6.af_family = af;
		nssin.arg.nss.host6.flags = flags;
		nssout.nss.host.hent = buf6->result;
		nssout.nss.host.herrno_p = error_num;
		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
		if (ret != ND_OK) {
			__IPv6_cleanup(buf6);
			buf6 = NULL;
		} else if (ipnode_bits & IPNODE_WANTIPV4) {
			/*
			 * buf6 may have all that we need if we either
			 * only wanted IPv4 addresses if there were no
			 * IPv6 addresses returned, or if there are
			 * IPv4-mapped addresses in buf6.  If either
			 * of these are true, then there's no need to
			 * look in hosts.
			 */
			if (ipnode_bits & IPNODE_IPV4IFNOIPV6 ||
			    __find_mapped(buf6->result, 0) != 0) {
				ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
			} else if (!(ipnode_bits & IPNODE_WANTIPV6)) {
				/*
				 * If all we're looking for are IPv4
				 * addresses and there are none in
				 * buf6 then buf6 is now useless.
				 */
				__IPv6_cleanup(buf6);
				buf6 = NULL;
			}
		}
	}
	if (ipnode_bits & IPNODE_LOOKUPHOSTS) {
		if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) {
			*error_num = NO_RECOVERY;
			goto cleanup;
		}
		nssin.op_t = NSS_HOST;
		nssin.arg.nss.host.name = name;
		nssin.arg.nss.host.buf = buf4->buffer;
		nssin.arg.nss.host.buflen = buf4->buflen;
		nssout.nss.host.hent = buf4->result;
		nssout.nss.host.herrno_p = error_num;
		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
		if (ret != ND_OK) {
			__IPv6_cleanup(buf4);
			buf4 = NULL;
		}
	}

	if (buf6 == NULL && buf4 == NULL) {
		*error_num = HOST_NOT_FOUND;
		goto cleanup;
	}

	/* Extract the appropriate addresses from the returned buffer(s). */
	switch (af) {
	case AF_INET6: {
		if (buf4 != NULL) {
			nss_XbyY_buf_t *mergebuf;

			/*
			 * The IPv4 results we have need to be
			 * converted to IPv4-mapped addresses,
			 * conditionally merged with the IPv6
			 * results, and the end result needs to be
			 * re-ordered.
			 */
			mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES);
			if (mergebuf == NULL) {
				*error_num = NO_RECOVERY;
				goto cleanup;
			}
			hp = __mapv4tov6(buf4->result,
			    ((buf6 != NULL) ? buf6->result : NULL),
			    mergebuf, 1);
			if (hp != NULL)
				order_haddrlist_af(AF_INET6, hp->h_addr_list);
			else
				*error_num = NO_RECOVERY;
			free(mergebuf);
		}

		if (buf4 == NULL && buf6 != NULL) {
			hp = buf6->result;

			/*
			 * We have what we need in buf6, but we may need
			 * to filter out some addresses depending on what
			 * is being asked for.
			 */
			if (!(ipnode_bits & IPNODE_WANTIPV4))
				hp = __filter_addresses(AF_INET, buf6->result);
			else if (!(ipnode_bits & IPNODE_WANTIPV6))
				hp = __filter_addresses(AF_INET6, buf6->result);

			/*
			 * We've been asked to unmap v4 addresses. This
			 * situation implies IPNODE_WANTIPV4 and
			 * !IPNODE_WANTIPV6.
			 */
			if (hp != NULL && (ipnode_bits & IPNODE_UNMAP)) {
				/*
				 * Just set hp to a new value, cleanup: will
				 * free the old one
				 */
				hp = __mappedtov4(hp, error_num);
			} else if (hp == NULL)
				*error_num = NO_ADDRESS;
		}

		break;
	}

	case AF_INET:
		/* We could have results in buf6 or buf4, not both */
		if (buf6 != NULL) {
			/*
			 * Extract the IPv4-mapped addresses from buf6
			 * into hp.
			 */
			hp = __mappedtov4(buf6->result, error_num);
		} else {
			/* We have what we need in buf4. */
			hp = buf4->result;
			if (ipnode_bits & IPNODE_LITERAL) {
				/*
				 * There is a special case here for literal
				 * IPv4 address strings.  The hosts
				 * front-end sets h_aliases to a one
				 * element array containing a single NULL
				 * pointer (in ndaddr2hent()), while
				 * getipnodebyname() requires h_aliases to
				 * be a NULL pointer itself.  We're not
				 * going to change the front-end since it
				 * needs to remain backward compatible for
				 * gethostbyname() and friends.  Just set
				 * h_aliases to NULL here instead.
				 */
				hp->h_aliases = NULL;
			}
		}

		break;

	default:
		break;
	}

cleanup:
	/*
	 * Free the memory we allocated, but make sure we don't free
	 * the memory we're returning to the caller.
	 */
	if (buf6 != NULL) {
		if (buf6->result == hp)
			buf6->result = NULL;
		__IPv6_cleanup(buf6);
	}
	if (buf4 != NULL) {
		if (buf4->result == hp)
			buf4->result = NULL;
		__IPv6_cleanup(buf4);
	}
	(void) freenetconfigent(nconf);

	return (hp);
}

/*
 * This is the IPv6 interface for "gethostbyaddr".
 */
struct hostent *
getipnodebyaddr(const void *src, size_t len, int type, int *error_num)
{
	struct in6_addr *addr6 = 0;
	struct in_addr *addr4 = 0;
	nss_XbyY_buf_t *buf = 0;
	nss_XbyY_buf_t *res = 0;
	struct netconfig *nconf;
	struct hostent *hp = 0;
	struct	nss_netdirbyaddr_in nssin;
	union	nss_netdirbyaddr_out nssout;
	int neterr;
	char tmpbuf[64];

	if (type == AF_INET6) {
		if ((addr6 = (struct in6_addr *)src) == NULL) {
			*error_num = HOST_NOT_FOUND;
			return (NULL);
		}
	} else if (type == AF_INET) {
		if ((addr4 = (struct in_addr *)src) == NULL) {
			*error_num = HOST_NOT_FOUND;
			return (NULL);
		}
	} else {
		*error_num = HOST_NOT_FOUND;
		return (NULL);
	}
	/*
	 * Specific case: query for "::"
	 */
	if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) {
		*error_num = HOST_NOT_FOUND;
		return (NULL);
	}
	/*
	 * Step 1: IPv4-mapped address  or IPv4 Compat
	 */
	if ((type == AF_INET6 && len == 16) &&
	    ((IN6_IS_ADDR_V4MAPPED(addr6)) ||
	    (IN6_IS_ADDR_V4COMPAT(addr6)))) {
		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
			*error_num = NO_RECOVERY;
			return (NULL);
		}
		if ((nconf = __rpc_getconfip("udp")) == NULL &&
		    (nconf = __rpc_getconfip("tcp")) == NULL) {
			*error_num = NO_RECOVERY;
			__IPv6_cleanup(buf);
			return (NULL);
		}
		nssin.op_t = NSS_HOST6;
		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
			(void) memcpy(tmpbuf, addr6, sizeof (*addr6));
			tmpbuf[10] = 0xffU;
			tmpbuf[11] = 0xffU;
			nssin.arg.nss.host.addr = (const char *)tmpbuf;
		} else {
			nssin.arg.nss.host.addr = (const char *)addr6;
		}
		nssin.arg.nss.host.len = sizeof (struct in6_addr);
		nssin.arg.nss.host.type = AF_INET6;
		nssin.arg.nss.host.buf = buf->buffer;
		nssin.arg.nss.host.buflen = buf->buflen;

		nssout.nss.host.hent = buf->result;
		nssout.nss.host.herrno_p = error_num;
		/*
		 * We pass in nconf and let the implementation of the
		 * long-named func decide whether to use the switch based on
		 * nc_nlookups.
		 */
		neterr =
		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);

		(void) freenetconfigent(nconf);
		if (neterr != ND_OK) {
			/* Failover case, try hosts db for v4 address */
			if (!gethostbyaddr_r(((char *)addr6) + 12,
			    sizeof (in_addr_t), AF_INET, buf->result,
			    buf->buffer, buf->buflen, error_num)) {
				__IPv6_cleanup(buf);
				return (NULL);
			}
			/* Found one, now format it into mapped/compat addr */
			if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
				__IPv6_cleanup(buf);
				*error_num = NO_RECOVERY;
				return (NULL);
			}
			/* Convert IPv4 to mapped/compat address w/name */
			hp = res->result;
			(void) __mapv4tov6(buf->result, 0, res,
			    IN6_IS_ADDR_V4MAPPED(addr6));
			__IPv6_cleanup(buf);
			free(res);
			return (hp);
		}
		/*
		 * At this point, we'll have a v4mapped hostent. If that's
		 * what was passed in, just return. If the request was a compat,
		 * twiggle the two bytes to make the mapped address a compat.
		 */
		hp = buf->result;
		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
			/* LINTED pointer cast */
			addr6 = (struct in6_addr *)hp->h_addr_list[0];
			addr6->s6_addr[10] = 0;
			addr6->s6_addr[11] = 0;
		}
		free(buf);
		return (hp);
	}
	/*
	 * Step 2: AF_INET, v4 lookup. Since we're going to search the
	 * ipnodes (v6) path first, we need to treat this as a v4mapped
	 * address. nscd(1m) caches v4 from ipnodes as mapped v6's. The
	 * switch backend knows to lookup v4's (not v4mapped) from the
	 * name services.
	 */
	if (type == AF_INET) {
		struct in6_addr v4mapbuf;
		addr6 = &v4mapbuf;

		IN6_INADDR_TO_V4MAPPED(addr4, addr6);
		if ((nconf = __rpc_getconfip("udp")) == NULL &&
		    (nconf = __rpc_getconfip("tcp")) == NULL) {
			*error_num = NO_RECOVERY;
			return (NULL);
		}
		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
			*error_num = NO_RECOVERY;
			freenetconfigent(nconf);
			return (NULL);
		}
		nssin.op_t = NSS_HOST6;
		nssin.arg.nss.host.addr = (const char *)addr6;
		nssin.arg.nss.host.len = sizeof (struct in6_addr);
		nssin.arg.nss.host.type = AF_INET6;
		nssin.arg.nss.host.buf = buf->buffer;
		nssin.arg.nss.host.buflen = buf->buflen;

		nssout.nss.host.hent = buf->result;
		nssout.nss.host.herrno_p = error_num;
		/*
		 * We pass in nconf and let the implementation of the
		 * long-named func decide whether to use the switch based on
		 * nc_nlookups.
		 */
		neterr =
		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);

		(void) freenetconfigent(nconf);
		if (neterr != ND_OK) {
			/* Failover case, try hosts db for v4 address */
			hp = buf->result;
			if (!gethostbyaddr_r(src, len, type, buf->result,
			    buf->buffer, buf->buflen, error_num)) {
				__IPv6_cleanup(buf);
				return (NULL);
			}
			free(buf);
			return (hp);
		}
		if ((hp = __mappedtov4(buf->result, error_num)) == NULL) {
			__IPv6_cleanup(buf);
			return (NULL);
		}
		__IPv6_cleanup(buf);
		return (hp);
	}
	/*
	 * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call.
	 */
	if (type == AF_INET6) {
		if ((nconf = __rpc_getconfip("udp")) == NULL &&
		    (nconf = __rpc_getconfip("tcp")) == NULL) {
			*error_num = NO_RECOVERY;
			return (NULL);
		}
		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
			*error_num = NO_RECOVERY;
			freenetconfigent(nconf);
			return (NULL);
		}
		nssin.op_t = NSS_HOST6;
		nssin.arg.nss.host.addr = (const char *)addr6;
		nssin.arg.nss.host.len = len;
		nssin.arg.nss.host.type = type;
		nssin.arg.nss.host.buf = buf->buffer;
		nssin.arg.nss.host.buflen = buf->buflen;

		nssout.nss.host.hent = buf->result;
		nssout.nss.host.herrno_p = error_num;
		/*
		 * We pass in nconf and let the implementation of the
		 * long-named func decide whether to use the switch based on
		 * nc_nlookups.
		 */
		neterr =
		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);

		(void) freenetconfigent(nconf);
		if (neterr != ND_OK) {
			__IPv6_cleanup(buf);
			return (NULL);
		}
		free(buf);
		return (nssout.nss.host.hent);
	}
	/*
	 * If we got here, unknown type.
	 */
	*error_num = HOST_NOT_FOUND;
	return (NULL);
}

void
freehostent(struct hostent *hent)
{
	free(hent);
}

static int
__ai_addrconfig(int af, boolean_t loopback)
{
	struct lifnum	lifn;
	struct lifconf	lifc;
	struct lifreq	*lifp, *buf = NULL;
	size_t		bufsize;
	hrtime_t	now, *then;
	static hrtime_t	then4, then6; /* the last time we updated ifnum# */
	static int	ifnum4 = -1, ifnum6 = -1, iflb4 = 0, iflb6 = 0;
	int		*num, *lb;
	int 		nlifr, count = 0;


	switch (af) {
	case AF_INET:
		num = &ifnum4;
		then = &then4;
		lb = &iflb4;
		break;
	case AF_INET6:
		num = &ifnum6;
		then = &then6;
		lb = &iflb6;
		break;
	default:
		return (0);
	}

	/*
	 * We don't need to check this every time someone does a name
	 * lookup.  Do it every IFNUM_TIMEOUT for each address family.
	 *
	 * There's no need to protect all of this with a lock.  The
	 * worst that can happen is that we update the interface count
	 * twice instead of once.  That's no big deal.
	 */
	now = gethrtime();
	if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) {
		lifn.lifn_family = af;
		/*
		 * We want to determine if this machine knows anything
		 * at all about the address family; the status of the
		 * interface is less important. Hence, set
		 * 'lifn_flags' to zero.
		 */
		lifn.lifn_flags = 0;
again:
		if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0)
			goto fail;

		if (lifn.lifn_count == 0) {
			*lb = 0;
			*num = 0;
			*then = now;
			return (*num);
		}

		/*
		 * Pad the interface count to detect when additional
		 * interfaces have been configured between SIOCGLIFNUM
		 * and SIOCGLIFCONF.
		 */
		lifn.lifn_count += 4;

		bufsize = lifn.lifn_count * sizeof (struct lifreq);
		if ((buf = realloc(buf, bufsize)) == NULL)
			goto fail;

		lifc.lifc_family = af;
		lifc.lifc_flags = 0;
		lifc.lifc_len = bufsize;
		lifc.lifc_buf = (caddr_t)buf;
		if (nss_ioctl(af, SIOCGLIFCONF, &lifc) < 0)
			goto fail;

		nlifr = lifc.lifc_len / sizeof (struct lifreq);
		if (nlifr >= lifn.lifn_count)
			goto again;
		/*
		 * Do not include any loopback addresses, 127.0.0.1 for AF_INET
		 * and ::1 for AF_INET6, while counting the number of available
		 * IPv4 or IPv6 addresses. (RFC 3493 requires this, whenever
		 * AI_ADDRCONFIG flag is set) However, if the loopback flag is
		 * set to true we'll include it in the output.
		 */
		for (lifp = buf; lifp < buf + nlifr; lifp++) {
			switch (af) {
			case AF_INET: {
				struct sockaddr_in *in;

				in = (struct sockaddr_in *)&lifp->lifr_addr;
				if (ntohl(in->sin_addr.s_addr) ==
				    INADDR_LOOPBACK) {
					count++;
				}
				break;
			}
			case AF_INET6: {
				struct sockaddr_in6 *in6;

				in6 = (struct sockaddr_in6 *)&lifp->lifr_addr;
				if (IN6_IS_ADDR_LOOPBACK(&in6->sin6_addr))
					count++;
				break;
			}
			}
		}
		*num = nlifr - count;
		*lb = count;
		*then = now;
		free(buf);
	}
	if (loopback == B_TRUE)
		return (*num + *lb);
	else
		return (*num);
fail:
	free(buf);
	/*
	 * If the process is running without the NET_ACCESS basic privilege,
	 * pretend we still have inet/inet6 interfaces.
	 */
	if (errno == EACCES)
		return (1);
	return (-1);
}

/*
 * This routine will either convert an IPv4 address to a mapped or compat
 * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped
 * v4 (he4) addresses. In either case, the results are returned in res.
 * Caller must provide all buffers.
 * Inputs:
 * 		he4	pointer to IPv4 buffer
 *		he6	pointer to IPv6 buffer (NULL if not merging v4/v6
 *		res	pointer to results buffer
 *		mapped	mapped == 1, map IPv4 : mapped == 0, compat IPv4
 *			mapped flag is ignored if he6 != NULL
 *
 * The results are packed into the res->buffer as follows:
 * <--------------- buffer + buflen -------------------------------------->
 * |-----------------|-----------------|----------------|----------------|
 * | pointers vector | pointers vector | aliases grow   | addresses grow |
 * | for addresses   | for aliases     |                |                |
 * | this way ->     | this way ->     | <- this way    |<- this way     |
 * |-----------------|-----------------|----------------|----------------|
 * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
 */
static struct hostent *
__mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res,
		int mapped)
{
	char	*buffer, *limit;
	int	buflen = res->buflen;
	struct	in6_addr *addr6p;
	char	*buff_locp;
	struct	hostent *host;
	int	count = 0, len, i;
	char	*h_namep;

	if (he4 == NULL || res == NULL) {
		return (NULL);
	}
	limit = res->buffer + buflen;
	host = (struct hostent *)res->result;
	buffer = res->buffer;

	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr));
	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
	if ((char *)host->h_addr_list >= limit ||
	    buff_locp <= (char *)host->h_addr_list) {
		return (NULL);
	}
	if (he6 == NULL) {
		/*
		 * If he6==NULL, map the v4 address into the v6 address format.
		 * This is used for getipnodebyaddr() (single address, mapped or
		 * compatible) or for v4 mapped for getipnodebyname(), which
		 * could be multiple addresses. This could also be a literal
		 * address string, which is why there is a inet_addr() call.
		 */
		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
			buff_locp -= sizeof (struct in6_addr);
			if (buff_locp <=
			    (char *)&(host->h_addr_list[count + 1])) {
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
				return (NULL);
			}
			/* LINTED pointer cast */
			addr6p = (struct in6_addr *)buff_locp;
			host->h_addr_list[count] = (char *)addr6p;
			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
			if (mapped) {
				addr6p->s6_addr[10] = 0xff;
				addr6p->s6_addr[11] = 0xff;
			}
			bcopy((char *)he4->h_addr_list[i],
			    &addr6p->s6_addr[12], sizeof (struct in_addr));
			++count;
		}
		/*
		 * Set last array element to NULL and add cname as first alias
		 */
		host->h_addr_list[count] = NULL;
		host->h_aliases = host->h_addr_list + count + 1;
		count = 0;
		if ((int)(inet_addr(he4->h_name)) != -1) {
		/*
		 * Literal address string, since we're mapping, we need the IPv6
		 * V4 mapped literal address string for h_name.
		 */
			char	tmpstr[128];
			(void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr,
			    sizeof (tmpstr));
			buff_locp -= (len = strlen(tmpstr) + 1);
			h_namep = tmpstr;
			if (buff_locp <= (char *)(host->h_aliases))
				return (NULL);
			bcopy(h_namep, buff_locp, len);
			host->h_name = buff_locp;
			host->h_aliases = NULL; /* no aliases for literal */
			host->h_length = sizeof (struct in6_addr);
			host->h_addrtype = AF_INET6;
			return (host); 		/* we're done, return result */
		}
		/*
		 * Not a literal address string, so just copy h_name.
		 */
		buff_locp -= (len = strlen(he4->h_name) + 1);
		h_namep = he4->h_name;
		if (buff_locp <= (char *)(host->h_aliases))
			return (NULL);
		bcopy(h_namep, buff_locp, len);
		host->h_name = buff_locp;
		/*
		 * Pass 2 (IPv4 aliases):
		 */
		for (i = 0; he4->h_aliases[i] != NULL; i++) {
			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
			if (buff_locp <=
			    (char *)&(host->h_aliases[count + 1])) {
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
				return (NULL);
			}
			host->h_aliases[count] = buff_locp;
			bcopy((char *)he4->h_aliases[i], buff_locp, len);
			++count;
		}
		host->h_aliases[count] = NULL;
		host->h_length = sizeof (struct in6_addr);
		host->h_addrtype = AF_INET6;
		return (host);
	} else {
		/*
		 * Merge IPv4 mapped addresses with IPv6 addresses. The
		 * IPv6 address will go in first, followed by the v4 mapped.
		 *
		 * Pass 1 (IPv6 addresses):
		 */
		for (i = 0; he6->h_addr_list[i] != NULL; i++) {
			buff_locp -= sizeof (struct in6_addr);
			if (buff_locp <=
			    (char *)&(host->h_addr_list[count + 1])) {
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
				return (NULL);
			}
			host->h_addr_list[count] = buff_locp;
			bcopy((char *)he6->h_addr_list[i], buff_locp,
			    sizeof (struct in6_addr));
			++count;
		}
		/*
		 * Pass 1 (IPv4 mapped addresses):
		 */
		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
			buff_locp -= sizeof (struct in6_addr);
			if (buff_locp <=
			    (char *)&(host->h_addr_list[count + 1])) {
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
				return (NULL);
			}
			/* LINTED pointer cast */
			addr6p = (struct in6_addr *)buff_locp;
			host->h_addr_list[count] = (char *)addr6p;
			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
			addr6p->s6_addr[10] = 0xff;
			addr6p->s6_addr[11] = 0xff;
			bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12],
			    sizeof (struct in_addr));
			++count;
		}
		/*
		 * Pass 2 (IPv6 aliases, host name first). We start h_aliases
		 * one after where h_addr_list array ended. This is where cname
		 * is put, followed by all aliases. Reset count to 0, for index
		 * in the h_aliases array.
		 */
		host->h_addr_list[count] = NULL;
		host->h_aliases = host->h_addr_list + count + 1;
		count = 0;
		buff_locp -= (len = strlen(he6->h_name) + 1);
		if (buff_locp <= (char *)(host->h_aliases))
			return (NULL);
		bcopy(he6->h_name, buff_locp, len);
		host->h_name = buff_locp;
		for (i = 0; he6->h_aliases[i] != NULL; i++) {
			buff_locp -= (len = strlen(he6->h_aliases[i]) + 1);
			if (buff_locp <=
			    (char *)&(host->h_aliases[count + 1])) {
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
				return (NULL);
			}
			host->h_aliases[count] = buff_locp;
			bcopy((char *)he6->h_aliases[i], buff_locp, len);
			++count;
		}
		/*
		 * Pass 2 (IPv4 aliases):
		 */
		for (i = 0; he4->h_aliases[i] != NULL; i++) {
			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
			if (buff_locp <=
			    (char *)&(host->h_aliases[count + 1])) {
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
				return (NULL);
			}
			host->h_aliases[count] = buff_locp;
			bcopy((char *)he4->h_aliases[i], buff_locp, len);
			++count;
		}
		host->h_aliases[count] = NULL;
		host->h_length = sizeof (struct in6_addr);
		host->h_addrtype = AF_INET6;
		return (host);
	}
}

/*
 * This routine will convert a mapped v4 hostent (AF_INET6) to a
 * AF_INET hostent. If no mapped addrs found, then a NULL is returned.
 * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped
 * addresses are extracted and copied to it. On sucess, a pointer to a new
 * hostent is returned.
 * There are two possible errors in which case a NULL is returned.
 * One of two error codes are returned:
 *
 * NO_RECOVERY - a malloc failed or the like for which there's no recovery.
 * NO_ADDRESS - after filtering all the v4, there was nothing left!
 *
 * Inputs:
 *              he              pointer to hostent with mapped v4 addresses
 *              filter_error    pointer to return error code
 * Return:
 *		pointer to a malloc'd hostent with v4 addresses.
 *
 * The results are packed into the res->buffer as follows:
 * <--------------- buffer + buflen -------------------------------------->
 * |-----------------|-----------------|----------------|----------------|
 * | pointers vector | pointers vector | aliases grow   | addresses grow |
 * | for addresses   | for aliases     |                |                |
 * | this way ->     | this way ->     | <- this way    |<- this way     |
 * |-----------------|-----------------|----------------|----------------|
 * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
 */
struct hostent *
__mappedtov4(struct hostent *he, int *extract_error)
{
	char	*buffer, *limit;
	nss_XbyY_buf_t *res;
	int	buflen = NSS_BUFLEN_HOSTS;
	struct	in_addr *addr4p;
	char	*buff_locp;
	struct	hostent *host;
	int	count = 0, len, i;
	char	*h_namep;

	if (he == NULL) {
		*extract_error = NO_ADDRESS;
		return (NULL);
	}
	if ((__find_mapped(he, 0)) == 0) {
		*extract_error = NO_ADDRESS;
		return (NULL);
	}
	if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) {
		*extract_error = NO_RECOVERY;
		return (NULL);
	}
	limit = res->buffer + buflen;
	host = (struct hostent *)res->result;
	buffer = res->buffer;

	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr));
	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
	if ((char *)host->h_addr_list >= limit ||
	    buff_locp <= (char *)host->h_addr_list)
		goto cleanup;
	/*
	 * "Unmap" the v4 mapped address(es) into a v4 hostent format.
	 * This is used for getipnodebyaddr() (single address) or for
	 * v4 mapped for getipnodebyname(), which could be multiple
	 * addresses. This could also be a literal address string,
	 * which is why there is a inet_addr() call.
	 */
	for (i = 0; he->h_addr_list[i] != NULL; i++) {
		/* LINTED pointer cast */
		if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *)
		    he->h_addr_list[i]))
			continue;
		buff_locp -= sizeof (struct in6_addr);
		/*
		 * Has to be room for the pointer to the address we're
		 * about to add, as well as the final NULL ptr.
		 */
		if (buff_locp <=
		    (char *)&(host->h_addr_list[count + 1]))
			goto cleanup;
		/* LINTED pointer cast */
		addr4p = (struct in_addr *)buff_locp;
		host->h_addr_list[count] = (char *)addr4p;
		bzero((char *)&addr4p->s_addr,
		    sizeof (struct in_addr));
		/* LINTED pointer cast */
		IN6_V4MAPPED_TO_INADDR(
		    (struct in6_addr *)he->h_addr_list[i], addr4p);
		++count;
	}
	/*
	 * Set last array element to NULL and add cname as first alias
	 */
	host->h_addr_list[count] = NULL;
	host->h_aliases = host->h_addr_list + count + 1;
	count = 0;
	/* Copy official host name */
	buff_locp -= (len = strlen(he->h_name) + 1);
	h_namep = he->h_name;
	if (buff_locp <= (char *)(host->h_aliases))
		goto cleanup;
	bcopy(h_namep, buff_locp, len);
	host->h_name = buff_locp;
	/*
	 * Pass 2 (IPv4 aliases):
	 */
	if (he->h_aliases != NULL) {
		for (i = 0; he->h_aliases[i] != NULL; i++) {
			buff_locp -= (len = strlen(he->h_aliases[i]) + 1);
			/*
			 * Has to be room for the pointer to the address we're
			 * about to add, as well as the final NULL ptr.
			 */
			if (buff_locp <=
			    (char *)&(host->h_aliases[count + 1]))
				goto cleanup;
			host->h_aliases[count] = buff_locp;
			bcopy((char *)he->h_aliases[i], buff_locp, len);
			++count;
		}
	}
	host->h_aliases[count] = NULL;
	host->h_length = sizeof (struct in_addr);
	host->h_addrtype = AF_INET;
	free(res);
	return (host);
cleanup:
	*extract_error = NO_RECOVERY;
	(void) __IPv6_cleanup(res);
	return (NULL);
}

/*
 * This routine takes as input a pointer to a hostent and filters out
 * the type of addresses specified by the af argument.  AF_INET
 * indicates that the caller wishes to filter out IPv4-mapped
 * addresses, and AF_INET6 indicates that the caller wishes to filter
 * out IPv6 addresses which aren't IPv4-mapped.  If filtering would
 * result in all addresses being filtered out, a NULL pointer is returned.
 * Otherwise, the he pointer passed in is returned, even if no addresses
 * were filtered out.
 */
static struct hostent *
__filter_addresses(int af, struct hostent *he)
{
	struct in6_addr	**in6addrlist, **in6addr;
	boolean_t	isipv4mapped;
	int		i = 0;

	if (he == NULL)
		return (NULL);

	in6addrlist = (struct in6_addr **)he->h_addr_list;
	for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
		isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr);

		if ((af == AF_INET && !isipv4mapped) ||
		    (af == AF_INET6 && isipv4mapped)) {
			if (in6addrlist[i] != *in6addr)
				in6addrlist[i] = *in6addr;
			i++;
		}
	}

	if (i == 0) {
		/* We filtered everything out. */
		return (NULL);
	} else {
		/* NULL terminate the list and return the hostent */
		in6addrlist[i] = NULL;
		return (he);
	}
}

/*
 * This routine searches a hostent for v4 mapped IPv6 addresses.
 * he		hostent structure to seach
 * find_both	flag indicating if only want mapped or both map'd and v6
 * return values:
 * 			0 = No mapped addresses
 *			1 = Mapped v4 address found (returns on first one found)
 *			2 = Both v6 and v4 mapped are present
 *
 * If hostent passed in with no addresses, zero will be returned.
 */

static int
__find_mapped(struct hostent *he, int find_both)
{
	int i;
	int mapd_found = 0;
	int v6_found = 0;

	for (i = 0; he->h_addr_list[i] != NULL; i++) {
		/* LINTED pointer cast */
		if (IN6_IS_ADDR_V4MAPPED(
				(struct in6_addr *)he->h_addr_list[i])) {
			if (find_both)
				mapd_found = 1;
			else
				return (1);
		} else {
			v6_found = 1;
		}
		/* save some iterations once both found */
		if (mapd_found && v6_found)
			return (2);
	}
	return (mapd_found);
}

/*
 * This routine was added specifically for the IPv6 getipnodeby*() APIs. This
 * separates the result pointer (ptr to hostent+data buf) from the
 * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr
 * can be passed to freehostent() and freed independently.
 *
 *   bufp->result    bufp->buffer
 *		|		|
 *		V		V
 *		------------------------------------------------...--
 *		|struct hostent	|addresses		     aliases |
 *		------------------------------------------------...--
 *		|               |<--------bufp->buflen-------------->|
 */

#define	ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1))

static nss_XbyY_buf_t *
__IPv6_alloc(int bufsz)
{
	nss_XbyY_buf_t *bufp;

	if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL)
		return (NULL);

	if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) ==
	    NULL) {
		free(bufp);
		return (NULL);
	}
	bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent);
	bufp->buflen = bufsz;
	return (bufp);
}

/*
 * This routine is use only for error return cleanup. This will free the
 * hostent pointer, so don't use for successful returns.
 */
static void
__IPv6_cleanup(nss_XbyY_buf_t *bufp)
{
	if (bufp == NULL)
		return;
	if (bufp->result != NULL)
		free(bufp->result);
	free(bufp);
}