xref: /titanic_50/usr/src/lib/libresolv2/common/irs/getservent.c (revision b3700b074e637f8c6991b70754c88a2cfffb246b)
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: getservent.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 servent *
51 getservent(void) {
52 	struct net_data *net_data = init();
53 
54 	return (getservent_p(net_data));
55 }
56 
57 struct servent *
58 getservbyname(const char *name, const char *proto) {
59 	struct net_data *net_data = init();
60 
61 	return (getservbyname_p(name, proto, net_data));
62 }
63 
64 struct servent *
65 getservbyport(int port, const char *proto) {
66 	struct net_data *net_data = init();
67 
68 	return (getservbyport_p(port, proto, net_data));
69 }
70 
71 void
72 setservent(int stayopen) {
73 	struct net_data *net_data = init();
74 
75 	setservent_p(stayopen, net_data);
76 }
77 
78 void
79 endservent() {
80 	struct net_data *net_data = init();
81 
82 	endservent_p(net_data);
83 }
84 
85 /* Shared private. */
86 
87 struct servent *
88 getservent_p(struct net_data *net_data) {
89 	struct irs_sv *sv;
90 
91 	if (!net_data || !(sv = net_data->sv))
92 		return (NULL);
93 	net_data->sv_last = (*sv->next)(sv);
94 	return (net_data->sv_last);
95 }
96 
97 struct servent *
98 getservbyname_p(const char *name, const char *proto,
99 		struct net_data *net_data) {
100 	struct irs_sv *sv;
101 	char **sap;
102 
103 	if (!net_data || !(sv = net_data->sv))
104 		return (NULL);
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;
110 			     sap && *sap; sap++)
111 				if (!strcmp(name, *sap))
112 					return (net_data->sv_last);
113 		}
114 	net_data->sv_last = (*sv->byname)(sv, name, proto);
115 	if (!net_data->sv_stayopen)
116 		endservent();
117 	return (net_data->sv_last);
118 }
119 
120 struct servent *
121 getservbyport_p(int port, const char *proto, struct net_data *net_data) {
122 	struct irs_sv *sv;
123 
124 	if (!net_data || !(sv = net_data->sv))
125 		return (NULL);
126 	if (net_data->sv_stayopen && net_data->sv_last)
127 		if (port == net_data->sv_last->s_port &&
128 		    ( !proto ||
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);
133 }
134 
135 void
136 setservent_p(int stayopen, struct net_data *net_data) {
137 	struct irs_sv *sv;
138 
139 	if (!net_data || !(sv = net_data->sv))
140 		return;
141 	(*sv->rewind)(sv);
142 	net_data->sv_stayopen = (stayopen != 0);
143 	if (stayopen == 0)
144 		net_data_minimize(net_data);
145 }
146 
147 void
148 endservent_p(struct net_data *net_data) {
149 	struct irs_sv *sv;
150 
151 	if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
152 		(*sv->minimize)(sv);
153 }
154 
155 /* Private */
156 
157 static struct net_data *
158 init() {
159 	struct net_data *net_data;
160 
161 	if (!(net_data = net_data_init(NULL)))
162 		goto error;
163 	if (!net_data->sv) {
164 		net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
165 
166 		if (!net_data->sv || !net_data->res) {
167  error:
168 			errno = EIO;
169 			return (NULL);
170 		}
171 		(*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
172 	}
173 
174 	return (net_data);
175 }
176 
177 #endif /*__BIND_NOSTATIC*/
178 
179 /*! \file */
180