xref: /titanic_41/usr/src/lib/libresolv2/common/irs/getprotoent.c (revision a2bb96e7d59f447f59bc306e53b7d00e38594667)
1 /*
2  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1996,1999 by Internet Software Consortium.
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 #if !defined(LINT) && !defined(CODECENTER)
26 static const char rcsid[] = "$Id: getprotoent.c,v 1.16 2001/11/01 07:34:33 marka Exp $";
27 #endif
28 
29 /* Imports */
30 
31 #include "port_before.h"
32 
33 #if !defined(__BIND_NOSTATIC)
34 
35 #include <sys/types.h>
36 
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 
40 #include <errno.h>
41 #include <resolv.h>
42 #include <stdio.h>
43 #include <string.h>
44 
45 #include <irs.h>
46 
47 #include "port_after.h"
48 
49 #include "irs_data.h"
50 
51 /* Forward */
52 
53 static struct net_data *init(void);
54 
55 /* Public */
56 
57 struct protoent *
58 getprotoent() {
59 	struct net_data *net_data = init();
60 
61 	return (getprotoent_p(net_data));
62 }
63 
64 struct protoent *
65 getprotobyname(const char *name) {
66 	struct net_data *net_data = init();
67 
68 	return (getprotobyname_p(name, net_data));
69 }
70 
71 struct protoent *
72 getprotobynumber(int proto) {
73 	struct net_data *net_data = init();
74 
75 	return (getprotobynumber_p(proto, net_data));
76 }
77 
78 #ifdef	ORIGINAL_ISC_CODE
79 void
80 #else
81 int
82 #endif
83 setprotoent(int stayopen) {
84 	struct net_data *net_data = init();
85 
86 	setprotoent_p(stayopen, net_data);
87 #ifdef	ORIGINAL_ISC_CODE
88 #else
89 	return (0);
90 #endif
91 }
92 
93 #ifdef	ORIGINAL_ISC_CODE
94 void
95 #else
96 int
97 #endif
98 endprotoent() {
99 	struct net_data *net_data = init();
100 
101 	endprotoent_p(net_data);
102 #ifdef	ORIGINAL_ISC_CODE
103 #else
104 	return (0);
105 #endif
106 }
107 
108 /* Shared private. */
109 
110 struct protoent *
111 getprotoent_p(struct net_data *net_data) {
112 	struct irs_pr *pr;
113 
114 	if (!net_data || !(pr = net_data->pr))
115 		return (NULL);
116 	net_data->pr_last = (*pr->next)(pr);
117 	return (net_data->pr_last);
118 }
119 
120 struct protoent *
121 getprotobyname_p(const char *name, struct net_data *net_data) {
122 	struct irs_pr *pr;
123 	char **pap;
124 
125 	if (!net_data || !(pr = net_data->pr))
126 		return (NULL);
127 	if (net_data->pr_stayopen && net_data->pr_last) {
128 		if (!strcmp(net_data->pr_last->p_name, name))
129 			return (net_data->pr_last);
130 		for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++)
131 			if (!strcmp(name, *pap))
132 				return (net_data->pr_last);
133 	}
134 	net_data->pr_last = (*pr->byname)(pr, name);
135 	if (!net_data->pr_stayopen)
136 		endprotoent();
137 	return (net_data->pr_last);
138 }
139 
140 struct protoent *
141 getprotobynumber_p(int proto, struct net_data *net_data) {
142 	struct irs_pr *pr;
143 
144 	if (!net_data || !(pr = net_data->pr))
145 		return (NULL);
146 	if (net_data->pr_stayopen && net_data->pr_last)
147 		if (net_data->pr_last->p_proto == proto)
148 			return (net_data->pr_last);
149 	net_data->pr_last = (*pr->bynumber)(pr, proto);
150 	if (!net_data->pr_stayopen)
151 		endprotoent();
152 	return (net_data->pr_last);
153 }
154 
155 void
156 setprotoent_p(int stayopen, struct net_data *net_data) {
157 	struct irs_pr *pr;
158 
159 	if (!net_data || !(pr = net_data->pr))
160 		return;
161 	(*pr->rewind)(pr);
162 	net_data->pr_stayopen = (stayopen != 0);
163 	if (stayopen == 0)
164 		net_data_minimize(net_data);
165 }
166 
167 void
168 endprotoent_p(struct net_data *net_data) {
169 	struct irs_pr *pr;
170 
171 	if ((net_data != NULL) && ((pr = net_data->pr) != NULL))
172 		(*pr->minimize)(pr);
173 }
174 
175 /* Private */
176 
177 static struct net_data *
178 init() {
179 	struct net_data *net_data;
180 
181 	if (!(net_data = net_data_init(NULL)))
182 		goto error;
183 	if (!net_data->pr) {
184 		net_data->pr = (*net_data->irs->pr_map)(net_data->irs);
185 
186 		if (!net_data->pr || !net_data->res) {
187  error:
188 			errno = EIO;
189 			return (NULL);
190 		}
191 		(*net_data->pr->res_set)(net_data->pr, net_data->res, NULL);
192 	}
193 
194 	return (net_data);
195 }
196 
197 #endif /*__BIND_NOSTATIC*/
198