2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: getservent.c,v 1.3.18.1 2005/04/27 05:00:59 sra Exp $";
24 #include "port_before.h"
26 #if !defined(__BIND_NOSTATIC)
28 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
40 #include "port_after.h"
46 static struct net_data *init(void);
52 struct net_data *net_data = init();
54 return (getservent_p(net_data));
58 getservbyname(const char *name, const char *proto) {
59 struct net_data *net_data = init();
61 return (getservbyname_p(name, proto, net_data));
65 getservbyport(int port, const char *proto) {
66 struct net_data *net_data = init();
68 return (getservbyport_p(port, proto, net_data));
72 setservent(int stayopen) {
73 struct net_data *net_data = init();
75 setservent_p(stayopen, net_data);
80 struct net_data *net_data = init();
82 endservent_p(net_data);
88 getservent_p(struct net_data *net_data) {
91 if (!net_data || !(sv = net_data->sv))
93 net_data->sv_last = (*sv->next)(sv);
94 return (net_data->sv_last);
98 getservbyname_p(const char *name, const char *proto,
99 struct net_data *net_data) {
103 if (!net_data || !(sv = net_data->sv))
105 if (net_data->sv_stayopen && net_data->sv_last)
106 if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) {
107 if (!strcmp(net_data->sv_last->s_name, name))
108 return (net_data->sv_last);
109 for (sap = net_data->sv_last->s_aliases;
111 if (!strcmp(name, *sap))
112 return (net_data->sv_last);
114 net_data->sv_last = (*sv->byname)(sv, name, proto);
115 if (!net_data->sv_stayopen)
117 return (net_data->sv_last);
121 getservbyport_p(int port, const char *proto, struct net_data *net_data) {
124 if (!net_data || !(sv = net_data->sv))
126 if (net_data->sv_stayopen && net_data->sv_last)
127 if (port == net_data->sv_last->s_port &&
129 !strcmp(net_data->sv_last->s_proto, proto)))
130 return (net_data->sv_last);
131 net_data->sv_last = (*sv->byport)(sv, port, proto);
132 return (net_data->sv_last);
136 setservent_p(int stayopen, struct net_data *net_data) {
139 if (!net_data || !(sv = net_data->sv))
142 net_data->sv_stayopen = (stayopen != 0);
144 net_data_minimize(net_data);
148 endservent_p(struct net_data *net_data) {
151 if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
157 static struct net_data *
159 struct net_data *net_data;
161 if (!(net_data = net_data_init(NULL)))
164 net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
166 if (!net_data->sv || !net_data->res) {
171 (*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
177 #endif /*__BIND_NOSTATIC*/