10Sstevel@tonic-gate /* 2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 30Sstevel@tonic-gate * Copyright (c) 1998-1999 by Internet Software Consortium. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 80Sstevel@tonic-gate * 9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: gethostent_r.c,v 1.9 2005/09/03 12:41:37 marka Exp $"; 200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 210Sstevel@tonic-gate 220Sstevel@tonic-gate #include <port_before.h> 230Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS) 240Sstevel@tonic-gate static int gethostent_r_not_required = 0; 250Sstevel@tonic-gate #else 260Sstevel@tonic-gate #include <errno.h> 270Sstevel@tonic-gate #include <string.h> 280Sstevel@tonic-gate #include <stdio.h> 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <netinet/in.h> 310Sstevel@tonic-gate #include <netdb.h> 320Sstevel@tonic-gate #include <sys/param.h> 330Sstevel@tonic-gate #include <port_after.h> 340Sstevel@tonic-gate 350Sstevel@tonic-gate #ifdef HOST_R_RETURN 360Sstevel@tonic-gate 370Sstevel@tonic-gate static HOST_R_RETURN 380Sstevel@tonic-gate copy_hostent(struct hostent *, struct hostent *, HOST_R_COPY_ARGS); 390Sstevel@tonic-gate 400Sstevel@tonic-gate HOST_R_RETURN 410Sstevel@tonic-gate gethostbyname_r(const char *name, struct hostent *hptr, HOST_R_ARGS) { 420Sstevel@tonic-gate struct hostent *he = gethostbyname(name); 430Sstevel@tonic-gate #ifdef HOST_R_SETANSWER 440Sstevel@tonic-gate int n = 0; 450Sstevel@tonic-gate #endif 460Sstevel@tonic-gate 47*11038SRao.Shoaib@Sun.COM #ifdef HOST_R_ERRNO 480Sstevel@tonic-gate HOST_R_ERRNO; 49*11038SRao.Shoaib@Sun.COM #endif 500Sstevel@tonic-gate 510Sstevel@tonic-gate #ifdef HOST_R_SETANSWER 52*11038SRao.Shoaib@Sun.COM if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0) 530Sstevel@tonic-gate *answerp = NULL; 540Sstevel@tonic-gate else 550Sstevel@tonic-gate *answerp = hptr; 560Sstevel@tonic-gate 570Sstevel@tonic-gate return (n); 580Sstevel@tonic-gate #else 590Sstevel@tonic-gate if (he == NULL) 600Sstevel@tonic-gate return (HOST_R_BAD); 610Sstevel@tonic-gate 620Sstevel@tonic-gate return (copy_hostent(he, hptr, HOST_R_COPY)); 630Sstevel@tonic-gate #endif 640Sstevel@tonic-gate } 650Sstevel@tonic-gate 660Sstevel@tonic-gate HOST_R_RETURN 670Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int len, int type, 680Sstevel@tonic-gate struct hostent *hptr, HOST_R_ARGS) { 690Sstevel@tonic-gate struct hostent *he = gethostbyaddr(addr, len, type); 700Sstevel@tonic-gate #ifdef HOST_R_SETANSWER 710Sstevel@tonic-gate int n = 0; 720Sstevel@tonic-gate #endif 730Sstevel@tonic-gate 74*11038SRao.Shoaib@Sun.COM #ifdef HOST_R_ERRNO 750Sstevel@tonic-gate HOST_R_ERRNO; 76*11038SRao.Shoaib@Sun.COM #endif 770Sstevel@tonic-gate 780Sstevel@tonic-gate #ifdef HOST_R_SETANSWER 79*11038SRao.Shoaib@Sun.COM if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0) 800Sstevel@tonic-gate *answerp = NULL; 810Sstevel@tonic-gate else 820Sstevel@tonic-gate *answerp = hptr; 830Sstevel@tonic-gate 840Sstevel@tonic-gate return (n); 850Sstevel@tonic-gate #else 860Sstevel@tonic-gate if (he == NULL) 870Sstevel@tonic-gate return (HOST_R_BAD); 880Sstevel@tonic-gate 890Sstevel@tonic-gate return (copy_hostent(he, hptr, HOST_R_COPY)); 900Sstevel@tonic-gate #endif 910Sstevel@tonic-gate } 920Sstevel@tonic-gate 93*11038SRao.Shoaib@Sun.COM /*% 940Sstevel@tonic-gate * These assume a single context is in operation per thread. 950Sstevel@tonic-gate * If this is not the case we will need to call irs directly 960Sstevel@tonic-gate * rather than through the base functions. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate HOST_R_RETURN 1000Sstevel@tonic-gate gethostent_r(struct hostent *hptr, HOST_R_ARGS) { 1010Sstevel@tonic-gate struct hostent *he = gethostent(); 1020Sstevel@tonic-gate #ifdef HOST_R_SETANSWER 1030Sstevel@tonic-gate int n = 0; 1040Sstevel@tonic-gate #endif 1050Sstevel@tonic-gate 106*11038SRao.Shoaib@Sun.COM #ifdef HOST_R_ERRNO 1070Sstevel@tonic-gate HOST_R_ERRNO; 108*11038SRao.Shoaib@Sun.COM #endif 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate #ifdef HOST_R_SETANSWER 111*11038SRao.Shoaib@Sun.COM if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0) 1120Sstevel@tonic-gate *answerp = NULL; 1130Sstevel@tonic-gate else 1140Sstevel@tonic-gate *answerp = hptr; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate return (n); 1170Sstevel@tonic-gate #else 1180Sstevel@tonic-gate if (he == NULL) 1190Sstevel@tonic-gate return (HOST_R_BAD); 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate return (copy_hostent(he, hptr, HOST_R_COPY)); 1220Sstevel@tonic-gate #endif 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate HOST_R_SET_RETURN 1260Sstevel@tonic-gate #ifdef HOST_R_ENT_ARGS 1270Sstevel@tonic-gate sethostent_r(int stay_open, HOST_R_ENT_ARGS) 1280Sstevel@tonic-gate #else 1290Sstevel@tonic-gate sethostent_r(int stay_open) 1300Sstevel@tonic-gate #endif 1310Sstevel@tonic-gate { 132*11038SRao.Shoaib@Sun.COM #ifdef HOST_R_ENT_ARGS 133*11038SRao.Shoaib@Sun.COM UNUSED(hdptr); 134*11038SRao.Shoaib@Sun.COM #endif 1350Sstevel@tonic-gate sethostent(stay_open); 1360Sstevel@tonic-gate #ifdef HOST_R_SET_RESULT 1370Sstevel@tonic-gate return (HOST_R_SET_RESULT); 1380Sstevel@tonic-gate #endif 1390Sstevel@tonic-gate } 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate HOST_R_END_RETURN 1420Sstevel@tonic-gate #ifdef HOST_R_ENT_ARGS 1430Sstevel@tonic-gate endhostent_r(HOST_R_ENT_ARGS) 1440Sstevel@tonic-gate #else 1450Sstevel@tonic-gate endhostent_r(void) 1460Sstevel@tonic-gate #endif 1470Sstevel@tonic-gate { 148*11038SRao.Shoaib@Sun.COM #ifdef HOST_R_ENT_ARGS 149*11038SRao.Shoaib@Sun.COM UNUSED(hdptr); 150*11038SRao.Shoaib@Sun.COM #endif 1510Sstevel@tonic-gate endhostent(); 1520Sstevel@tonic-gate HOST_R_END_RESULT(HOST_R_OK); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* Private */ 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate #ifndef HOSTENT_DATA 1580Sstevel@tonic-gate static HOST_R_RETURN 1590Sstevel@tonic-gate copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) { 1600Sstevel@tonic-gate char *cp; 1610Sstevel@tonic-gate char **ptr; 1620Sstevel@tonic-gate int i, n; 1630Sstevel@tonic-gate int nptr, len; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* Find out the amount of space required to store the answer. */ 166*11038SRao.Shoaib@Sun.COM nptr = 2; /*%< NULL ptrs */ 1670Sstevel@tonic-gate len = (char *)ALIGN(buf) - buf; 1680Sstevel@tonic-gate for (i = 0; he->h_addr_list[i]; i++, nptr++) { 1690Sstevel@tonic-gate len += he->h_length; 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate for (i = 0; he->h_aliases[i]; i++, nptr++) { 1720Sstevel@tonic-gate len += strlen(he->h_aliases[i]) + 1; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate len += strlen(he->h_name) + 1; 1750Sstevel@tonic-gate len += nptr * sizeof(char*); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate if (len > buflen) { 1780Sstevel@tonic-gate errno = ERANGE; 1790Sstevel@tonic-gate return (HOST_R_BAD); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* copy address size and type */ 1830Sstevel@tonic-gate hptr->h_addrtype = he->h_addrtype; 1840Sstevel@tonic-gate n = hptr->h_length = he->h_length; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate ptr = (char **)ALIGN(buf); 1870Sstevel@tonic-gate cp = (char *)ALIGN(buf) + nptr * sizeof(char *); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* copy address list */ 1900Sstevel@tonic-gate hptr->h_addr_list = ptr; 1910Sstevel@tonic-gate for (i = 0; he->h_addr_list[i]; i++ , ptr++) { 1920Sstevel@tonic-gate memcpy(cp, he->h_addr_list[i], n); 1930Sstevel@tonic-gate hptr->h_addr_list[i] = cp; 1940Sstevel@tonic-gate cp += n; 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate hptr->h_addr_list[i] = NULL; 1970Sstevel@tonic-gate ptr++; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* copy official name */ 2000Sstevel@tonic-gate n = strlen(he->h_name) + 1; 2010Sstevel@tonic-gate strcpy(cp, he->h_name); 2020Sstevel@tonic-gate hptr->h_name = cp; 2030Sstevel@tonic-gate cp += n; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* copy aliases */ 2060Sstevel@tonic-gate hptr->h_aliases = ptr; 2070Sstevel@tonic-gate for (i = 0 ; he->h_aliases[i]; i++) { 2080Sstevel@tonic-gate n = strlen(he->h_aliases[i]) + 1; 2090Sstevel@tonic-gate strcpy(cp, he->h_aliases[i]); 2100Sstevel@tonic-gate hptr->h_aliases[i] = cp; 2110Sstevel@tonic-gate cp += n; 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate hptr->h_aliases[i] = NULL; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate return (HOST_R_OK); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate #else /* !HOSTENT_DATA */ 2180Sstevel@tonic-gate static int 2190Sstevel@tonic-gate copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) { 2200Sstevel@tonic-gate char *cp, *eob; 2210Sstevel@tonic-gate int i, n; 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* copy address size and type */ 2240Sstevel@tonic-gate hptr->h_addrtype = he->h_addrtype; 2250Sstevel@tonic-gate n = hptr->h_length = he->h_length; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* copy up to first 35 addresses */ 2280Sstevel@tonic-gate i = 0; 229*11038SRao.Shoaib@Sun.COM cp = hdptr->hostbuf; 230*11038SRao.Shoaib@Sun.COM eob = hdptr->hostbuf + sizeof(hdptr->hostbuf); 2310Sstevel@tonic-gate hptr->h_addr_list = hdptr->h_addr_ptrs; 2320Sstevel@tonic-gate while (he->h_addr_list[i] && i < (_MAXADDRS)) { 2330Sstevel@tonic-gate if (n < (eob - cp)) { 2340Sstevel@tonic-gate memcpy(cp, he->h_addr_list[i], n); 2350Sstevel@tonic-gate hptr->h_addr_list[i] = cp; 2360Sstevel@tonic-gate cp += n; 2370Sstevel@tonic-gate } else { 2380Sstevel@tonic-gate break; 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate i++; 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate hptr->h_addr_list[i] = NULL; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* copy official name */ 2450Sstevel@tonic-gate if ((n = strlen(he->h_name) + 1) < (eob - cp)) { 2460Sstevel@tonic-gate strcpy(cp, he->h_name); 2470Sstevel@tonic-gate hptr->h_name = cp; 2480Sstevel@tonic-gate cp += n; 2490Sstevel@tonic-gate } else { 2500Sstevel@tonic-gate return (-1); 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* copy aliases */ 2540Sstevel@tonic-gate i = 0; 2550Sstevel@tonic-gate hptr->h_aliases = hdptr->host_aliases; 2560Sstevel@tonic-gate while (he->h_aliases[i] && i < (_MAXALIASES-1)) { 2570Sstevel@tonic-gate if ((n = strlen(he->h_aliases[i]) + 1) < (eob - cp)) { 2580Sstevel@tonic-gate strcpy(cp, he->h_aliases[i]); 2590Sstevel@tonic-gate hptr->h_aliases[i] = cp; 2600Sstevel@tonic-gate cp += n; 2610Sstevel@tonic-gate } else { 2620Sstevel@tonic-gate break; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate i++; 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate hptr->h_aliases[i] = NULL; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate return (HOST_R_OK); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate #endif /* !HOSTENT_DATA */ 2710Sstevel@tonic-gate #else /* HOST_R_RETURN */ 2720Sstevel@tonic-gate static int gethostent_r_unknown_system = 0; 2730Sstevel@tonic-gate #endif /* HOST_R_RETURN */ 2740Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ 275*11038SRao.Shoaib@Sun.COM /*! \file */ 276