xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/getservent.c (revision 9c88ac3ab4edaa5e8c0130ed1b4b376ea57c545a)
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 /* Imports */
19 
20 #include "port_before.h"
21 
22 #if !defined(__BIND_NOSTATIC)
23 
24 #include <sys/types.h>
25 
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 
29 #include <errno.h>
30 #include <resolv.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include <irs.h>
35 
36 #include "port_after.h"
37 
38 #include "irs_data.h"
39 
40 /* Forward */
41 
42 static struct net_data *init(void);
43 
44 /* Public */
45 
46 struct servent *
47 getservent(void) {
48 	struct net_data *net_data = init();
49 
50 	return (getservent_p(net_data));
51 }
52 
53 struct servent *
54 getservbyname(const char *name, const char *proto) {
55 	struct net_data *net_data = init();
56 
57 	return (getservbyname_p(name, proto, net_data));
58 }
59 
60 struct servent *
61 getservbyport(int port, const char *proto) {
62 	struct net_data *net_data = init();
63 
64 	return (getservbyport_p(port, proto, net_data));
65 }
66 
67 void
68 setservent(int stayopen) {
69 	struct net_data *net_data = init();
70 
71 	setservent_p(stayopen, net_data);
72 }
73 
74 void
75 endservent() {
76 	struct net_data *net_data = init();
77 
78 	endservent_p(net_data);
79 }
80 
81 /* Shared private. */
82 
83 struct servent *
84 getservent_p(struct net_data *net_data) {
85 	struct irs_sv *sv;
86 
87 	if (!net_data || !(sv = net_data->sv))
88 		return (NULL);
89 	net_data->sv_last = (*sv->next)(sv);
90 	return (net_data->sv_last);
91 }
92 
93 struct servent *
94 getservbyname_p(const char *name, const char *proto,
95 		struct net_data *net_data) {
96 	struct irs_sv *sv;
97 	char **sap;
98 
99 	if (!net_data || !(sv = net_data->sv))
100 		return (NULL);
101 	if (net_data->sv_stayopen && net_data->sv_last)
102 		if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) {
103 			if (!strcmp(net_data->sv_last->s_name, name))
104 				return (net_data->sv_last);
105 			for (sap = net_data->sv_last->s_aliases;
106 			     sap && *sap; sap++)
107 				if (!strcmp(name, *sap))
108 					return (net_data->sv_last);
109 		}
110 	net_data->sv_last = (*sv->byname)(sv, name, proto);
111 	if (!net_data->sv_stayopen)
112 		endservent();
113 	return (net_data->sv_last);
114 }
115 
116 struct servent *
117 getservbyport_p(int port, const char *proto, struct net_data *net_data) {
118 	struct irs_sv *sv;
119 
120 	if (!net_data || !(sv = net_data->sv))
121 		return (NULL);
122 	if (net_data->sv_stayopen && net_data->sv_last)
123 		if (port == net_data->sv_last->s_port &&
124 		    ( !proto ||
125 		     !strcmp(net_data->sv_last->s_proto, proto)))
126 			return (net_data->sv_last);
127 	net_data->sv_last = (*sv->byport)(sv, port, proto);
128 	return (net_data->sv_last);
129 }
130 
131 void
132 setservent_p(int stayopen, struct net_data *net_data) {
133 	struct irs_sv *sv;
134 
135 	if (!net_data || !(sv = net_data->sv))
136 		return;
137 	(*sv->rewind)(sv);
138 	net_data->sv_stayopen = (stayopen != 0);
139 	if (stayopen == 0)
140 		net_data_minimize(net_data);
141 }
142 
143 void
144 endservent_p(struct net_data *net_data) {
145 	struct irs_sv *sv;
146 
147 	if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
148 		(*sv->minimize)(sv);
149 }
150 
151 /* Private */
152 
153 static struct net_data *
154 init() {
155 	struct net_data *net_data;
156 
157 	if (!(net_data = net_data_init(NULL)))
158 		goto error;
159 	if (!net_data->sv) {
160 		net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
161 
162 		if (!net_data->sv || !net_data->res) {
163  error:
164 			errno = EIO;
165 			return (NULL);
166 		}
167 		(*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
168 	}
169 
170 	return (net_data);
171 }
172 
173 #endif /*__BIND_NOSTATIC*/
174 
175 /*! \file */
176