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