4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid
[] = "Id: getservent.c,v 1.4 2005/04/27 04:56:26 sra Exp";
26 #include "port_before.h"
28 #if !defined(__BIND_NOSTATIC)
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
42 #include "port_after.h"
48 static struct net_data
*init(void);
54 struct net_data
*net_data
= init();
56 return (getservent_p(net_data
));
60 getservbyname(const char *name
, const char *proto
) {
61 struct net_data
*net_data
= init();
63 return (getservbyname_p(name
, proto
, net_data
));
67 getservbyport(int port
, const char *proto
) {
68 struct net_data
*net_data
= init();
70 return (getservbyport_p(port
, proto
, net_data
));
74 setservent(int stayopen
) {
75 struct net_data
*net_data
= init();
77 setservent_p(stayopen
, net_data
);
82 struct net_data
*net_data
= init();
84 endservent_p(net_data
);
90 getservent_p(struct net_data
*net_data
) {
93 if (!net_data
|| !(sv
= net_data
->sv
))
95 net_data
->sv_last
= (*sv
->next
)(sv
);
96 return (net_data
->sv_last
);
100 getservbyname_p(const char *name
, const char *proto
,
101 struct net_data
*net_data
) {
105 if (!net_data
|| !(sv
= net_data
->sv
))
107 if (net_data
->sv_stayopen
&& net_data
->sv_last
)
108 if (!proto
|| !strcmp(net_data
->sv_last
->s_proto
, proto
)) {
109 if (!strcmp(net_data
->sv_last
->s_name
, name
))
110 return (net_data
->sv_last
);
111 for (sap
= net_data
->sv_last
->s_aliases
;
113 if (!strcmp(name
, *sap
))
114 return (net_data
->sv_last
);
116 net_data
->sv_last
= (*sv
->byname
)(sv
, name
, proto
);
117 if (!net_data
->sv_stayopen
)
119 return (net_data
->sv_last
);
123 getservbyport_p(int port
, const char *proto
, struct net_data
*net_data
) {
126 if (!net_data
|| !(sv
= net_data
->sv
))
128 if (net_data
->sv_stayopen
&& net_data
->sv_last
)
129 if (port
== net_data
->sv_last
->s_port
&&
131 !strcmp(net_data
->sv_last
->s_proto
, proto
)))
132 return (net_data
->sv_last
);
133 net_data
->sv_last
= (*sv
->byport
)(sv
, port
, proto
);
134 return (net_data
->sv_last
);
138 setservent_p(int stayopen
, struct net_data
*net_data
) {
141 if (!net_data
|| !(sv
= net_data
->sv
))
144 net_data
->sv_stayopen
= (stayopen
!= 0);
146 net_data_minimize(net_data
);
150 endservent_p(struct net_data
*net_data
) {
153 if ((net_data
!= NULL
) && ((sv
= net_data
->sv
) != NULL
))
159 static struct net_data
*
161 struct net_data
*net_data
;
163 if (!(net_data
= net_data_init(NULL
)))
166 net_data
->sv
= (*net_data
->irs
->sv_map
)(net_data
->irs
);
168 if (!net_data
->sv
|| !net_data
->res
) {
173 (*net_data
->sv
->res_set
)(net_data
->sv
, net_data
->res
, NULL
);
179 #endif /*__BIND_NOSTATIC*/