1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1999-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: getprotoent_r.c,v 8.6 2001/11/01 08:02:14 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 getprotoent_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 <port_after.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #ifdef PROTO_R_RETURN
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static PROTO_R_RETURN
44*0Sstevel@tonic-gate copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS);
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate PROTO_R_RETURN
47*0Sstevel@tonic-gate getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) {
48*0Sstevel@tonic-gate 	struct protoent *pe = getprotobyname(name);
49*0Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER
50*0Sstevel@tonic-gate 	int n = 0;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
53*0Sstevel@tonic-gate 		*answerp = NULL;
54*0Sstevel@tonic-gate 	else
55*0Sstevel@tonic-gate 		*answerp = pptr;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	return (n);
58*0Sstevel@tonic-gate #else
59*0Sstevel@tonic-gate 	if (pe == NULL)
60*0Sstevel@tonic-gate 		return (PROTO_R_BAD);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
63*0Sstevel@tonic-gate #endif
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate PROTO_R_RETURN
67*0Sstevel@tonic-gate getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) {
68*0Sstevel@tonic-gate 	struct protoent *pe = getprotobynumber(proto);
69*0Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER
70*0Sstevel@tonic-gate 	int n = 0;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
73*0Sstevel@tonic-gate 		*answerp = NULL;
74*0Sstevel@tonic-gate 	else
75*0Sstevel@tonic-gate 		*answerp = pptr;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	return (n);
78*0Sstevel@tonic-gate #else
79*0Sstevel@tonic-gate 	if (pe == NULL)
80*0Sstevel@tonic-gate 		return (PROTO_R_BAD);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
83*0Sstevel@tonic-gate #endif
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
88*0Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
89*0Sstevel@tonic-gate  *	rather than through the base functions.
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate PROTO_R_RETURN
93*0Sstevel@tonic-gate getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) {
94*0Sstevel@tonic-gate 	struct protoent *pe = getprotoent();
95*0Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER
96*0Sstevel@tonic-gate 	int n = 0;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
99*0Sstevel@tonic-gate 		*answerp = NULL;
100*0Sstevel@tonic-gate 	else
101*0Sstevel@tonic-gate 		*answerp = pptr;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	return (n);
104*0Sstevel@tonic-gate #else
105*0Sstevel@tonic-gate 	if (pe == NULL)
106*0Sstevel@tonic-gate 		return (PROTO_R_BAD);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
109*0Sstevel@tonic-gate #endif
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate PROTO_R_SET_RETURN
113*0Sstevel@tonic-gate #ifdef PROTO_R_ENT_ARGS
114*0Sstevel@tonic-gate setprotoent_r(int stay_open, PROTO_R_ENT_ARGS)
115*0Sstevel@tonic-gate #else
116*0Sstevel@tonic-gate setprotoent_r(int stay_open)
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	setprotoent(stay_open);
120*0Sstevel@tonic-gate #ifdef PROTO_R_SET_RESULT
121*0Sstevel@tonic-gate 	return (PROTO_R_SET_RESULT);
122*0Sstevel@tonic-gate #endif
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate PROTO_R_END_RETURN
126*0Sstevel@tonic-gate #ifdef PROTO_R_ENT_ARGS
127*0Sstevel@tonic-gate endprotoent_r(PROTO_R_ENT_ARGS)
128*0Sstevel@tonic-gate #else
129*0Sstevel@tonic-gate endprotoent_r()
130*0Sstevel@tonic-gate #endif
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate 	endprotoent();
133*0Sstevel@tonic-gate 	PROTO_R_END_RESULT(PROTO_R_OK);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /* Private */
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate #ifndef PROTOENT_DATA
139*0Sstevel@tonic-gate static PROTO_R_RETURN
140*0Sstevel@tonic-gate copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
141*0Sstevel@tonic-gate 	char *cp;
142*0Sstevel@tonic-gate 	int i, n;
143*0Sstevel@tonic-gate 	int numptr, len;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
146*0Sstevel@tonic-gate 	numptr = 1; /* NULL ptr */
147*0Sstevel@tonic-gate 	len = (char *)ALIGN(buf) - buf;
148*0Sstevel@tonic-gate 	for (i = 0; pe->p_aliases[i]; i++, numptr++) {
149*0Sstevel@tonic-gate 		len += strlen(pe->p_aliases[i]) + 1;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	len += strlen(pe->p_name) + 1;
152*0Sstevel@tonic-gate 	len += numptr * sizeof(char*);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if (len > (int)buflen) {
155*0Sstevel@tonic-gate 		errno = ERANGE;
156*0Sstevel@tonic-gate 		return (PROTO_R_BAD);
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	/* copy protocol value*/
160*0Sstevel@tonic-gate 	pptr->p_proto = pe->p_proto;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	/* copy official name */
165*0Sstevel@tonic-gate 	n = strlen(pe->p_name) + 1;
166*0Sstevel@tonic-gate 	strcpy(cp, pe->p_name);
167*0Sstevel@tonic-gate 	pptr->p_name = cp;
168*0Sstevel@tonic-gate 	cp += n;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/* copy aliases */
171*0Sstevel@tonic-gate 	pptr->p_aliases = (char **)ALIGN(buf);
172*0Sstevel@tonic-gate 	for (i = 0 ; pe->p_aliases[i]; i++) {
173*0Sstevel@tonic-gate 		n = strlen(pe->p_aliases[i]) + 1;
174*0Sstevel@tonic-gate 		strcpy(cp, pe->p_aliases[i]);
175*0Sstevel@tonic-gate 		pptr->p_aliases[i] = cp;
176*0Sstevel@tonic-gate 		cp += n;
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 	pptr->p_aliases[i] = NULL;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	return (PROTO_R_OK);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate #else /* !PROTOENT_DATA */
183*0Sstevel@tonic-gate static int
184*0Sstevel@tonic-gate copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
185*0Sstevel@tonic-gate 	char *cp, *eob;
186*0Sstevel@tonic-gate 	int i, n;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/* copy protocol value */
189*0Sstevel@tonic-gate 	pptr->p_proto = pe->p_proto;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	/* copy official name */
192*0Sstevel@tonic-gate 	cp = pdptr->line;
193*0Sstevel@tonic-gate 	eob = pdptr->line + sizeof(pdptr->line);
194*0Sstevel@tonic-gate 	if ((n = strlen(pe->p_name) + 1) < (eob - cp)) {
195*0Sstevel@tonic-gate 		strcpy(cp, pe->p_name);
196*0Sstevel@tonic-gate 		pptr->p_name = cp;
197*0Sstevel@tonic-gate 		cp += n;
198*0Sstevel@tonic-gate 	} else {
199*0Sstevel@tonic-gate 		return (-1);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/* copy aliases */
203*0Sstevel@tonic-gate 	i = 0;
204*0Sstevel@tonic-gate 	pptr->p_aliases = pdptr->proto_aliases;
205*0Sstevel@tonic-gate 	while (pe->p_aliases[i] && i < (_MAXALIASES-1)) {
206*0Sstevel@tonic-gate 		if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) {
207*0Sstevel@tonic-gate 			strcpy(cp, pe->p_aliases[i]);
208*0Sstevel@tonic-gate 			pptr->p_aliases[i] = cp;
209*0Sstevel@tonic-gate 			cp += n;
210*0Sstevel@tonic-gate 		} else {
211*0Sstevel@tonic-gate 			break;
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 		i++;
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 	pptr->p_aliases[i] = NULL;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	return (PROTO_R_OK);
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate #endif /* PROTOENT_DATA */
220*0Sstevel@tonic-gate #else /* PROTO_R_RETURN */
221*0Sstevel@tonic-gate 	static int getprotoent_r_unknown_system = 0;
222*0Sstevel@tonic-gate #endif /* PROTO_R_RETURN */
223*0Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
224