xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/gethostent_r.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: gethostent_r.c,v 1.9 2005/09/03 12:41:37 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <port_before.h>
237c478bd9Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
247c478bd9Sstevel@tonic-gate 	static int gethostent_r_not_required = 0;
257c478bd9Sstevel@tonic-gate #else
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <netinet/in.h>
317c478bd9Sstevel@tonic-gate #include <netdb.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <port_after.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef HOST_R_RETURN
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static HOST_R_RETURN
387c478bd9Sstevel@tonic-gate copy_hostent(struct hostent *, struct hostent *, HOST_R_COPY_ARGS);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate HOST_R_RETURN
417c478bd9Sstevel@tonic-gate gethostbyname_r(const char *name,  struct hostent *hptr, HOST_R_ARGS) {
427c478bd9Sstevel@tonic-gate 	struct hostent *he = gethostbyname(name);
437c478bd9Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
447c478bd9Sstevel@tonic-gate 	int n = 0;
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate 
47*9525b14bSRao Shoaib #ifdef HOST_R_ERRNO
487c478bd9Sstevel@tonic-gate 	HOST_R_ERRNO;
49*9525b14bSRao Shoaib #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
52*9525b14bSRao Shoaib 	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
537c478bd9Sstevel@tonic-gate 		*answerp = NULL;
547c478bd9Sstevel@tonic-gate 	else
557c478bd9Sstevel@tonic-gate 		*answerp = hptr;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	return (n);
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate 	if (he == NULL)
607c478bd9Sstevel@tonic-gate 		return (HOST_R_BAD);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	return (copy_hostent(he, hptr, HOST_R_COPY));
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate HOST_R_RETURN
677c478bd9Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int len, int type,
687c478bd9Sstevel@tonic-gate 		struct hostent *hptr, HOST_R_ARGS) {
697c478bd9Sstevel@tonic-gate 	struct hostent *he = gethostbyaddr(addr, len, type);
707c478bd9Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
717c478bd9Sstevel@tonic-gate 	int n = 0;
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
74*9525b14bSRao Shoaib #ifdef HOST_R_ERRNO
757c478bd9Sstevel@tonic-gate 	HOST_R_ERRNO;
76*9525b14bSRao Shoaib #endif
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
79*9525b14bSRao Shoaib 	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
807c478bd9Sstevel@tonic-gate 		*answerp = NULL;
817c478bd9Sstevel@tonic-gate 	else
827c478bd9Sstevel@tonic-gate 		*answerp = hptr;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	return (n);
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate 	if (he == NULL)
877c478bd9Sstevel@tonic-gate 		return (HOST_R_BAD);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	return (copy_hostent(he, hptr, HOST_R_COPY));
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
93*9525b14bSRao Shoaib /*%
947c478bd9Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
957c478bd9Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
967c478bd9Sstevel@tonic-gate  *	rather than through the base functions.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate HOST_R_RETURN
1007c478bd9Sstevel@tonic-gate gethostent_r(struct hostent *hptr, HOST_R_ARGS) {
1017c478bd9Sstevel@tonic-gate 	struct hostent *he = gethostent();
1027c478bd9Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
1037c478bd9Sstevel@tonic-gate 	int n = 0;
1047c478bd9Sstevel@tonic-gate #endif
1057c478bd9Sstevel@tonic-gate 
106*9525b14bSRao Shoaib #ifdef HOST_R_ERRNO
1077c478bd9Sstevel@tonic-gate 	HOST_R_ERRNO;
108*9525b14bSRao Shoaib #endif
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
111*9525b14bSRao Shoaib 	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
1127c478bd9Sstevel@tonic-gate 		*answerp = NULL;
1137c478bd9Sstevel@tonic-gate 	else
1147c478bd9Sstevel@tonic-gate 		*answerp = hptr;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	return (n);
1177c478bd9Sstevel@tonic-gate #else
1187c478bd9Sstevel@tonic-gate 	if (he == NULL)
1197c478bd9Sstevel@tonic-gate 		return (HOST_R_BAD);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (copy_hostent(he, hptr, HOST_R_COPY));
1227c478bd9Sstevel@tonic-gate #endif
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate HOST_R_SET_RETURN
1267c478bd9Sstevel@tonic-gate #ifdef HOST_R_ENT_ARGS
1277c478bd9Sstevel@tonic-gate sethostent_r(int stay_open, HOST_R_ENT_ARGS)
1287c478bd9Sstevel@tonic-gate #else
1297c478bd9Sstevel@tonic-gate sethostent_r(int stay_open)
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate {
132*9525b14bSRao Shoaib #ifdef HOST_R_ENT_ARGS
133*9525b14bSRao Shoaib 	UNUSED(hdptr);
134*9525b14bSRao Shoaib #endif
1357c478bd9Sstevel@tonic-gate 	sethostent(stay_open);
1367c478bd9Sstevel@tonic-gate #ifdef	HOST_R_SET_RESULT
1377c478bd9Sstevel@tonic-gate 	return (HOST_R_SET_RESULT);
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate HOST_R_END_RETURN
1427c478bd9Sstevel@tonic-gate #ifdef HOST_R_ENT_ARGS
1437c478bd9Sstevel@tonic-gate endhostent_r(HOST_R_ENT_ARGS)
1447c478bd9Sstevel@tonic-gate #else
1457c478bd9Sstevel@tonic-gate endhostent_r(void)
1467c478bd9Sstevel@tonic-gate #endif
1477c478bd9Sstevel@tonic-gate {
148*9525b14bSRao Shoaib #ifdef HOST_R_ENT_ARGS
149*9525b14bSRao Shoaib 	UNUSED(hdptr);
150*9525b14bSRao Shoaib #endif
1517c478bd9Sstevel@tonic-gate 	endhostent();
1527c478bd9Sstevel@tonic-gate 	HOST_R_END_RESULT(HOST_R_OK);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* Private */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #ifndef HOSTENT_DATA
1587c478bd9Sstevel@tonic-gate static HOST_R_RETURN
1597c478bd9Sstevel@tonic-gate copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) {
1607c478bd9Sstevel@tonic-gate 	char *cp;
1617c478bd9Sstevel@tonic-gate 	char **ptr;
1627c478bd9Sstevel@tonic-gate 	int i, n;
1637c478bd9Sstevel@tonic-gate 	int nptr, len;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
166*9525b14bSRao Shoaib 	nptr = 2; /*%< NULL ptrs */
1677c478bd9Sstevel@tonic-gate 	len = (char *)ALIGN(buf) - buf;
1687c478bd9Sstevel@tonic-gate 	for (i = 0; he->h_addr_list[i]; i++, nptr++) {
1697c478bd9Sstevel@tonic-gate 		len += he->h_length;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	for (i = 0; he->h_aliases[i]; i++, nptr++) {
1727c478bd9Sstevel@tonic-gate 		len += strlen(he->h_aliases[i]) + 1;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	len += strlen(he->h_name) + 1;
1757c478bd9Sstevel@tonic-gate 	len += nptr * sizeof(char*);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (len > buflen) {
1787c478bd9Sstevel@tonic-gate 		errno = ERANGE;
1797c478bd9Sstevel@tonic-gate 		return (HOST_R_BAD);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* copy address size and type */
1837c478bd9Sstevel@tonic-gate 	hptr->h_addrtype = he->h_addrtype;
1847c478bd9Sstevel@tonic-gate 	n = hptr->h_length = he->h_length;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	ptr = (char **)ALIGN(buf);
1877c478bd9Sstevel@tonic-gate 	cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* copy address list */
1907c478bd9Sstevel@tonic-gate 	hptr->h_addr_list = ptr;
1917c478bd9Sstevel@tonic-gate 	for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
1927c478bd9Sstevel@tonic-gate 		memcpy(cp, he->h_addr_list[i], n);
1937c478bd9Sstevel@tonic-gate 		hptr->h_addr_list[i] = cp;
1947c478bd9Sstevel@tonic-gate 		cp += n;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	hptr->h_addr_list[i] = NULL;
1977c478bd9Sstevel@tonic-gate 	ptr++;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* copy official name */
2007c478bd9Sstevel@tonic-gate 	n = strlen(he->h_name) + 1;
2017c478bd9Sstevel@tonic-gate 	strcpy(cp, he->h_name);
2027c478bd9Sstevel@tonic-gate 	hptr->h_name = cp;
2037c478bd9Sstevel@tonic-gate 	cp += n;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* copy aliases */
2067c478bd9Sstevel@tonic-gate 	hptr->h_aliases = ptr;
2077c478bd9Sstevel@tonic-gate 	for (i = 0 ; he->h_aliases[i]; i++) {
2087c478bd9Sstevel@tonic-gate 		n = strlen(he->h_aliases[i]) + 1;
2097c478bd9Sstevel@tonic-gate 		strcpy(cp, he->h_aliases[i]);
2107c478bd9Sstevel@tonic-gate 		hptr->h_aliases[i] = cp;
2117c478bd9Sstevel@tonic-gate 		cp += n;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	hptr->h_aliases[i] = NULL;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return (HOST_R_OK);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate #else /* !HOSTENT_DATA */
2187c478bd9Sstevel@tonic-gate static int
2197c478bd9Sstevel@tonic-gate copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) {
2207c478bd9Sstevel@tonic-gate 	char *cp, *eob;
2217c478bd9Sstevel@tonic-gate 	int i, n;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* copy address size and type */
2247c478bd9Sstevel@tonic-gate 	hptr->h_addrtype = he->h_addrtype;
2257c478bd9Sstevel@tonic-gate 	n = hptr->h_length = he->h_length;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/* copy up to first 35 addresses */
2287c478bd9Sstevel@tonic-gate 	i = 0;
229*9525b14bSRao Shoaib 	cp = hdptr->hostbuf;
230*9525b14bSRao Shoaib 	eob = hdptr->hostbuf + sizeof(hdptr->hostbuf);
2317c478bd9Sstevel@tonic-gate 	hptr->h_addr_list = hdptr->h_addr_ptrs;
2327c478bd9Sstevel@tonic-gate 	while (he->h_addr_list[i] && i < (_MAXADDRS)) {
2337c478bd9Sstevel@tonic-gate 		if (n < (eob - cp)) {
2347c478bd9Sstevel@tonic-gate 			memcpy(cp, he->h_addr_list[i], n);
2357c478bd9Sstevel@tonic-gate 			hptr->h_addr_list[i] = cp;
2367c478bd9Sstevel@tonic-gate 			cp += n;
2377c478bd9Sstevel@tonic-gate 		} else {
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 		i++;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	hptr->h_addr_list[i] = NULL;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* copy official name */
2457c478bd9Sstevel@tonic-gate 	if ((n = strlen(he->h_name) + 1) < (eob - cp)) {
2467c478bd9Sstevel@tonic-gate 		strcpy(cp, he->h_name);
2477c478bd9Sstevel@tonic-gate 		hptr->h_name = cp;
2487c478bd9Sstevel@tonic-gate 		cp += n;
2497c478bd9Sstevel@tonic-gate 	} else {
2507c478bd9Sstevel@tonic-gate 		return (-1);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/* copy aliases */
2547c478bd9Sstevel@tonic-gate 	i = 0;
2557c478bd9Sstevel@tonic-gate 	hptr->h_aliases = hdptr->host_aliases;
2567c478bd9Sstevel@tonic-gate 	while (he->h_aliases[i] && i < (_MAXALIASES-1)) {
2577c478bd9Sstevel@tonic-gate 		if ((n = strlen(he->h_aliases[i]) + 1) < (eob - cp)) {
2587c478bd9Sstevel@tonic-gate 			strcpy(cp, he->h_aliases[i]);
2597c478bd9Sstevel@tonic-gate 			hptr->h_aliases[i] = cp;
2607c478bd9Sstevel@tonic-gate 			cp += n;
2617c478bd9Sstevel@tonic-gate 		} else {
2627c478bd9Sstevel@tonic-gate 			break;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		i++;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	hptr->h_aliases[i] = NULL;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	return (HOST_R_OK);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate #endif /* !HOSTENT_DATA */
2717c478bd9Sstevel@tonic-gate #else /* HOST_R_RETURN */
2727c478bd9Sstevel@tonic-gate 	static int gethostent_r_unknown_system = 0;
2737c478bd9Sstevel@tonic-gate #endif /* HOST_R_RETURN */
2747c478bd9Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
275*9525b14bSRao Shoaib /*! \file */
276