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