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