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