1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1998-1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: gethostent_r.c,v 8.7 2001/11/01 08:02:09 marka Exp $";
27*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <port_before.h>
30*0Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
31*0Sstevel@tonic-gate 	static int gethostent_r_not_required = 0;
32*0Sstevel@tonic-gate #else
33*0Sstevel@tonic-gate #include <errno.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <netdb.h>
39*0Sstevel@tonic-gate #include <sys/param.h>
40*0Sstevel@tonic-gate #include <port_after.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #ifdef HOST_R_RETURN
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static HOST_R_RETURN
45*0Sstevel@tonic-gate copy_hostent(struct hostent *, struct hostent *, HOST_R_COPY_ARGS);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate HOST_R_RETURN
48*0Sstevel@tonic-gate gethostbyname_r(const char *name,  struct hostent *hptr, HOST_R_ARGS) {
49*0Sstevel@tonic-gate 	struct hostent *he = gethostbyname(name);
50*0Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
51*0Sstevel@tonic-gate 	int n = 0;
52*0Sstevel@tonic-gate #endif
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	HOST_R_ERRNO;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
57*0Sstevel@tonic-gate 	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) == 0)
58*0Sstevel@tonic-gate 		*answerp = NULL;
59*0Sstevel@tonic-gate 	else
60*0Sstevel@tonic-gate 		*answerp = hptr;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	return (n);
63*0Sstevel@tonic-gate #else
64*0Sstevel@tonic-gate 	if (he == NULL)
65*0Sstevel@tonic-gate 		return (HOST_R_BAD);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	return (copy_hostent(he, hptr, HOST_R_COPY));
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate HOST_R_RETURN
72*0Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int len, int type,
73*0Sstevel@tonic-gate 		struct hostent *hptr, HOST_R_ARGS) {
74*0Sstevel@tonic-gate 	struct hostent *he = gethostbyaddr(addr, len, type);
75*0Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
76*0Sstevel@tonic-gate 	int n = 0;
77*0Sstevel@tonic-gate #endif
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	HOST_R_ERRNO;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
82*0Sstevel@tonic-gate 	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) == 0)
83*0Sstevel@tonic-gate 		*answerp = NULL;
84*0Sstevel@tonic-gate 	else
85*0Sstevel@tonic-gate 		*answerp = hptr;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	return (n);
88*0Sstevel@tonic-gate #else
89*0Sstevel@tonic-gate 	if (he == NULL)
90*0Sstevel@tonic-gate 		return (HOST_R_BAD);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	return (copy_hostent(he, hptr, HOST_R_COPY));
93*0Sstevel@tonic-gate #endif
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
98*0Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
99*0Sstevel@tonic-gate  *	rather than through the base functions.
100*0Sstevel@tonic-gate  */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate HOST_R_RETURN
103*0Sstevel@tonic-gate gethostent_r(struct hostent *hptr, HOST_R_ARGS) {
104*0Sstevel@tonic-gate 	struct hostent *he = gethostent();
105*0Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
106*0Sstevel@tonic-gate 	int n = 0;
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	HOST_R_ERRNO;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate #ifdef HOST_R_SETANSWER
112*0Sstevel@tonic-gate 	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) == 0)
113*0Sstevel@tonic-gate 		*answerp = NULL;
114*0Sstevel@tonic-gate 	else
115*0Sstevel@tonic-gate 		*answerp = hptr;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (n);
118*0Sstevel@tonic-gate #else
119*0Sstevel@tonic-gate 	if (he == NULL)
120*0Sstevel@tonic-gate 		return (HOST_R_BAD);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	return (copy_hostent(he, hptr, HOST_R_COPY));
123*0Sstevel@tonic-gate #endif
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate HOST_R_SET_RETURN
127*0Sstevel@tonic-gate #ifdef HOST_R_ENT_ARGS
128*0Sstevel@tonic-gate sethostent_r(int stay_open, HOST_R_ENT_ARGS)
129*0Sstevel@tonic-gate #else
130*0Sstevel@tonic-gate sethostent_r(int stay_open)
131*0Sstevel@tonic-gate #endif
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	sethostent(stay_open);
134*0Sstevel@tonic-gate #ifdef	HOST_R_SET_RESULT
135*0Sstevel@tonic-gate 	return (HOST_R_SET_RESULT);
136*0Sstevel@tonic-gate #endif
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate HOST_R_END_RETURN
140*0Sstevel@tonic-gate #ifdef HOST_R_ENT_ARGS
141*0Sstevel@tonic-gate endhostent_r(HOST_R_ENT_ARGS)
142*0Sstevel@tonic-gate #else
143*0Sstevel@tonic-gate endhostent_r(void)
144*0Sstevel@tonic-gate #endif
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	endhostent();
147*0Sstevel@tonic-gate 	HOST_R_END_RESULT(HOST_R_OK);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate /* Private */
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate #ifndef HOSTENT_DATA
153*0Sstevel@tonic-gate static HOST_R_RETURN
154*0Sstevel@tonic-gate copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) {
155*0Sstevel@tonic-gate 	char *cp;
156*0Sstevel@tonic-gate 	char **ptr;
157*0Sstevel@tonic-gate 	int i, n;
158*0Sstevel@tonic-gate 	int nptr, len;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
161*0Sstevel@tonic-gate 	nptr = 2; /* NULL ptrs */
162*0Sstevel@tonic-gate 	len = (char *)ALIGN(buf) - buf;
163*0Sstevel@tonic-gate 	for (i = 0; he->h_addr_list[i]; i++, nptr++) {
164*0Sstevel@tonic-gate 		len += he->h_length;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 	for (i = 0; he->h_aliases[i]; i++, nptr++) {
167*0Sstevel@tonic-gate 		len += strlen(he->h_aliases[i]) + 1;
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 	len += strlen(he->h_name) + 1;
170*0Sstevel@tonic-gate 	len += nptr * sizeof(char*);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (len > buflen) {
173*0Sstevel@tonic-gate 		errno = ERANGE;
174*0Sstevel@tonic-gate 		return (HOST_R_BAD);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/* copy address size and type */
178*0Sstevel@tonic-gate 	hptr->h_addrtype = he->h_addrtype;
179*0Sstevel@tonic-gate 	n = hptr->h_length = he->h_length;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	ptr = (char **)ALIGN(buf);
182*0Sstevel@tonic-gate 	cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/* copy address list */
185*0Sstevel@tonic-gate 	hptr->h_addr_list = ptr;
186*0Sstevel@tonic-gate 	for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
187*0Sstevel@tonic-gate 		memcpy(cp, he->h_addr_list[i], n);
188*0Sstevel@tonic-gate 		hptr->h_addr_list[i] = cp;
189*0Sstevel@tonic-gate 		cp += n;
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 	hptr->h_addr_list[i] = NULL;
192*0Sstevel@tonic-gate 	ptr++;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/* copy official name */
195*0Sstevel@tonic-gate 	n = strlen(he->h_name) + 1;
196*0Sstevel@tonic-gate 	strcpy(cp, he->h_name);
197*0Sstevel@tonic-gate 	hptr->h_name = cp;
198*0Sstevel@tonic-gate 	cp += n;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	/* copy aliases */
201*0Sstevel@tonic-gate 	hptr->h_aliases = ptr;
202*0Sstevel@tonic-gate 	for (i = 0 ; he->h_aliases[i]; i++) {
203*0Sstevel@tonic-gate 		n = strlen(he->h_aliases[i]) + 1;
204*0Sstevel@tonic-gate 		strcpy(cp, he->h_aliases[i]);
205*0Sstevel@tonic-gate 		hptr->h_aliases[i] = cp;
206*0Sstevel@tonic-gate 		cp += n;
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	hptr->h_aliases[i] = NULL;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	return (HOST_R_OK);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate #else /* !HOSTENT_DATA */
213*0Sstevel@tonic-gate static int
214*0Sstevel@tonic-gate copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) {
215*0Sstevel@tonic-gate 	char *cp, *eob;
216*0Sstevel@tonic-gate 	int i, n;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/* copy address size and type */
219*0Sstevel@tonic-gate 	hptr->h_addrtype = he->h_addrtype;
220*0Sstevel@tonic-gate 	n = hptr->h_length = he->h_length;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	/* copy up to first 35 addresses */
223*0Sstevel@tonic-gate 	i = 0;
224*0Sstevel@tonic-gate 	cp = hdptr->hostaddr;
225*0Sstevel@tonic-gate 	eob = hdptr->hostaddr + sizeof(hdptr->hostaddr);
226*0Sstevel@tonic-gate 	hptr->h_addr_list = hdptr->h_addr_ptrs;
227*0Sstevel@tonic-gate 	while (he->h_addr_list[i] && i < (_MAXADDRS)) {
228*0Sstevel@tonic-gate 		if (n < (eob - cp)) {
229*0Sstevel@tonic-gate 			memcpy(cp, he->h_addr_list[i], n);
230*0Sstevel@tonic-gate 			hptr->h_addr_list[i] = cp;
231*0Sstevel@tonic-gate 			cp += n;
232*0Sstevel@tonic-gate 		} else {
233*0Sstevel@tonic-gate 			break;
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 		i++;
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 	hptr->h_addr_list[i] = NULL;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/* copy official name */
240*0Sstevel@tonic-gate 	cp = hdptr->hostbuf;
241*0Sstevel@tonic-gate 	eob = hdptr->hostbuf + sizeof(hdptr->hostbuf);
242*0Sstevel@tonic-gate 	if ((n = strlen(he->h_name) + 1) < (eob - cp)) {
243*0Sstevel@tonic-gate 		strcpy(cp, he->h_name);
244*0Sstevel@tonic-gate 		hptr->h_name = cp;
245*0Sstevel@tonic-gate 		cp += n;
246*0Sstevel@tonic-gate 	} else {
247*0Sstevel@tonic-gate 		return (-1);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	/* copy aliases */
251*0Sstevel@tonic-gate 	i = 0;
252*0Sstevel@tonic-gate 	hptr->h_aliases = hdptr->host_aliases;
253*0Sstevel@tonic-gate 	while (he->h_aliases[i] && i < (_MAXALIASES-1)) {
254*0Sstevel@tonic-gate 		if ((n = strlen(he->h_aliases[i]) + 1) < (eob - cp)) {
255*0Sstevel@tonic-gate 			strcpy(cp, he->h_aliases[i]);
256*0Sstevel@tonic-gate 			hptr->h_aliases[i] = cp;
257*0Sstevel@tonic-gate 			cp += n;
258*0Sstevel@tonic-gate 		} else {
259*0Sstevel@tonic-gate 			break;
260*0Sstevel@tonic-gate 		}
261*0Sstevel@tonic-gate 		i++;
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 	hptr->h_aliases[i] = NULL;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	return (HOST_R_OK);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate #endif /* !HOSTENT_DATA */
268*0Sstevel@tonic-gate #else /* HOST_R_RETURN */
269*0Sstevel@tonic-gate 	static int gethostent_r_unknown_system = 0;
270*0Sstevel@tonic-gate #endif /* HOST_R_RETURN */
271*0Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
272