1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1997-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) 1996,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(LINT) && !defined(CODECENTER)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: getprotoent.c,v 1.16 2001/11/01 07:34:33 marka Exp $";
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Imports */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include "port_before.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <arpa/nameser.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <resolv.h>
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <irs.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include "port_after.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include "irs_data.h"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /* Forward */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static struct net_data *init(void);
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /* Public */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate struct protoent *
58*0Sstevel@tonic-gate getprotoent() {
59*0Sstevel@tonic-gate 	struct net_data *net_data = init();
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	return (getprotoent_p(net_data));
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate struct protoent *
65*0Sstevel@tonic-gate getprotobyname(const char *name) {
66*0Sstevel@tonic-gate 	struct net_data *net_data = init();
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	return (getprotobyname_p(name, net_data));
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate struct protoent *
72*0Sstevel@tonic-gate getprotobynumber(int proto) {
73*0Sstevel@tonic-gate 	struct net_data *net_data = init();
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	return (getprotobynumber_p(proto, net_data));
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
79*0Sstevel@tonic-gate void
80*0Sstevel@tonic-gate #else
81*0Sstevel@tonic-gate int
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate setprotoent(int stayopen) {
84*0Sstevel@tonic-gate 	struct net_data *net_data = init();
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	setprotoent_p(stayopen, net_data);
87*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
88*0Sstevel@tonic-gate #else
89*0Sstevel@tonic-gate 	return (0);
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
94*0Sstevel@tonic-gate void
95*0Sstevel@tonic-gate #else
96*0Sstevel@tonic-gate int
97*0Sstevel@tonic-gate #endif
98*0Sstevel@tonic-gate endprotoent() {
99*0Sstevel@tonic-gate 	struct net_data *net_data = init();
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	endprotoent_p(net_data);
102*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
103*0Sstevel@tonic-gate #else
104*0Sstevel@tonic-gate 	return (0);
105*0Sstevel@tonic-gate #endif
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /* Shared private. */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate struct protoent *
111*0Sstevel@tonic-gate getprotoent_p(struct net_data *net_data) {
112*0Sstevel@tonic-gate 	struct irs_pr *pr;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
115*0Sstevel@tonic-gate 		return (NULL);
116*0Sstevel@tonic-gate 	net_data->pr_last = (*pr->next)(pr);
117*0Sstevel@tonic-gate 	return (net_data->pr_last);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate struct protoent *
121*0Sstevel@tonic-gate getprotobyname_p(const char *name, struct net_data *net_data) {
122*0Sstevel@tonic-gate 	struct irs_pr *pr;
123*0Sstevel@tonic-gate 	char **pap;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
126*0Sstevel@tonic-gate 		return (NULL);
127*0Sstevel@tonic-gate 	if (net_data->pr_stayopen && net_data->pr_last) {
128*0Sstevel@tonic-gate 		if (!strcmp(net_data->pr_last->p_name, name))
129*0Sstevel@tonic-gate 			return (net_data->pr_last);
130*0Sstevel@tonic-gate 		for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++)
131*0Sstevel@tonic-gate 			if (!strcmp(name, *pap))
132*0Sstevel@tonic-gate 				return (net_data->pr_last);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	net_data->pr_last = (*pr->byname)(pr, name);
135*0Sstevel@tonic-gate 	if (!net_data->pr_stayopen)
136*0Sstevel@tonic-gate 		endprotoent();
137*0Sstevel@tonic-gate 	return (net_data->pr_last);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate struct protoent *
141*0Sstevel@tonic-gate getprotobynumber_p(int proto, struct net_data *net_data) {
142*0Sstevel@tonic-gate 	struct irs_pr *pr;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
145*0Sstevel@tonic-gate 		return (NULL);
146*0Sstevel@tonic-gate 	if (net_data->pr_stayopen && net_data->pr_last)
147*0Sstevel@tonic-gate 		if (net_data->pr_last->p_proto == proto)
148*0Sstevel@tonic-gate 			return (net_data->pr_last);
149*0Sstevel@tonic-gate 	net_data->pr_last = (*pr->bynumber)(pr, proto);
150*0Sstevel@tonic-gate 	if (!net_data->pr_stayopen)
151*0Sstevel@tonic-gate 		endprotoent();
152*0Sstevel@tonic-gate 	return (net_data->pr_last);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate void
156*0Sstevel@tonic-gate setprotoent_p(int stayopen, struct net_data *net_data) {
157*0Sstevel@tonic-gate 	struct irs_pr *pr;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
160*0Sstevel@tonic-gate 		return;
161*0Sstevel@tonic-gate 	(*pr->rewind)(pr);
162*0Sstevel@tonic-gate 	net_data->pr_stayopen = (stayopen != 0);
163*0Sstevel@tonic-gate 	if (stayopen == 0)
164*0Sstevel@tonic-gate 		net_data_minimize(net_data);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate void
168*0Sstevel@tonic-gate endprotoent_p(struct net_data *net_data) {
169*0Sstevel@tonic-gate 	struct irs_pr *pr;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if ((net_data != NULL) && ((pr = net_data->pr) != NULL))
172*0Sstevel@tonic-gate 		(*pr->minimize)(pr);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate /* Private */
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate static struct net_data *
178*0Sstevel@tonic-gate init() {
179*0Sstevel@tonic-gate 	struct net_data *net_data;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
182*0Sstevel@tonic-gate 		goto error;
183*0Sstevel@tonic-gate 	if (!net_data->pr) {
184*0Sstevel@tonic-gate 		net_data->pr = (*net_data->irs->pr_map)(net_data->irs);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		if (!net_data->pr || !net_data->res) {
187*0Sstevel@tonic-gate  error:
188*0Sstevel@tonic-gate 			errno = EIO;
189*0Sstevel@tonic-gate 			return (NULL);
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 		(*net_data->pr->res_set)(net_data->pr, net_data->res, NULL);
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	return (net_data);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
198