irp_sv.c revision 1.1.1.1.6.2 1 1.1.1.1.6.2 riz /* $NetBSD: irp_sv.c,v 1.1.1.1.6.2 2011/01/09 20:42:55 riz Exp $ */
2 1.1.1.1.6.2 riz
3 1.1.1.1.6.2 riz /*
4 1.1.1.1.6.2 riz * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1.1.1.6.2 riz * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
6 1.1.1.1.6.2 riz *
7 1.1.1.1.6.2 riz * Permission to use, copy, modify, and distribute this software for any
8 1.1.1.1.6.2 riz * purpose with or without fee is hereby granted, provided that the above
9 1.1.1.1.6.2 riz * copyright notice and this permission notice appear in all copies.
10 1.1.1.1.6.2 riz *
11 1.1.1.1.6.2 riz * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1.1.1.6.2 riz * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.1.1.6.2 riz * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1.1.1.6.2 riz * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.1.1.6.2 riz * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.1.1.6.2 riz * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1.1.1.6.2 riz * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.1.1.6.2 riz */
19 1.1.1.1.6.2 riz
20 1.1.1.1.6.2 riz #if defined(LIBC_SCCS) && !defined(lint)
21 1.1.1.1.6.2 riz static const char rcsid[] = "Id: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp";
22 1.1.1.1.6.2 riz #endif /* LIBC_SCCS and not lint */
23 1.1.1.1.6.2 riz
24 1.1.1.1.6.2 riz /* extern */
25 1.1.1.1.6.2 riz
26 1.1.1.1.6.2 riz #include "port_before.h"
27 1.1.1.1.6.2 riz
28 1.1.1.1.6.2 riz #include <syslog.h>
29 1.1.1.1.6.2 riz #include <sys/types.h>
30 1.1.1.1.6.2 riz #include <sys/socket.h>
31 1.1.1.1.6.2 riz
32 1.1.1.1.6.2 riz #ifdef IRS_LCL_SV_DB
33 1.1.1.1.6.2 riz #include <db.h>
34 1.1.1.1.6.2 riz #endif
35 1.1.1.1.6.2 riz #include <errno.h>
36 1.1.1.1.6.2 riz #include <fcntl.h>
37 1.1.1.1.6.2 riz #include <limits.h>
38 1.1.1.1.6.2 riz #include <stdio.h>
39 1.1.1.1.6.2 riz #include <string.h>
40 1.1.1.1.6.2 riz #include <stdlib.h>
41 1.1.1.1.6.2 riz #include <syslog.h>
42 1.1.1.1.6.2 riz
43 1.1.1.1.6.2 riz #include <irs.h>
44 1.1.1.1.6.2 riz #include <irp.h>
45 1.1.1.1.6.2 riz #include <isc/irpmarshall.h>
46 1.1.1.1.6.2 riz #include <isc/memcluster.h>
47 1.1.1.1.6.2 riz
48 1.1.1.1.6.2 riz #include "irs_p.h"
49 1.1.1.1.6.2 riz #include "lcl_p.h"
50 1.1.1.1.6.2 riz #include "irp_p.h"
51 1.1.1.1.6.2 riz
52 1.1.1.1.6.2 riz #include "port_after.h"
53 1.1.1.1.6.2 riz
54 1.1.1.1.6.2 riz /* Types */
55 1.1.1.1.6.2 riz
56 1.1.1.1.6.2 riz struct pvt {
57 1.1.1.1.6.2 riz struct irp_p *girpdata;
58 1.1.1.1.6.2 riz int warned;
59 1.1.1.1.6.2 riz struct servent service;
60 1.1.1.1.6.2 riz };
61 1.1.1.1.6.2 riz
62 1.1.1.1.6.2 riz /* Forward */
63 1.1.1.1.6.2 riz
64 1.1.1.1.6.2 riz static void sv_close(struct irs_sv*);
65 1.1.1.1.6.2 riz static struct servent * sv_next(struct irs_sv *);
66 1.1.1.1.6.2 riz static struct servent * sv_byname(struct irs_sv *, const char *,
67 1.1.1.1.6.2 riz const char *);
68 1.1.1.1.6.2 riz static struct servent * sv_byport(struct irs_sv *, int, const char *);
69 1.1.1.1.6.2 riz static void sv_rewind(struct irs_sv *);
70 1.1.1.1.6.2 riz static void sv_minimize(struct irs_sv *);
71 1.1.1.1.6.2 riz
72 1.1.1.1.6.2 riz static void free_service(struct servent *sv);
73 1.1.1.1.6.2 riz
74 1.1.1.1.6.2 riz
75 1.1.1.1.6.2 riz
76 1.1.1.1.6.2 riz /* Public */
77 1.1.1.1.6.2 riz
78 1.1.1.1.6.2 riz /*%
79 1.1.1.1.6.2 riz * struct irs_sv * irs_irp_sv(struct irs_acc *this)
80 1.1.1.1.6.2 riz *
81 1.1.1.1.6.2 riz */
82 1.1.1.1.6.2 riz
83 1.1.1.1.6.2 riz struct irs_sv *
84 1.1.1.1.6.2 riz irs_irp_sv(struct irs_acc *this) {
85 1.1.1.1.6.2 riz struct irs_sv *sv;
86 1.1.1.1.6.2 riz struct pvt *pvt;
87 1.1.1.1.6.2 riz
88 1.1.1.1.6.2 riz if ((sv = memget(sizeof *sv)) == NULL) {
89 1.1.1.1.6.2 riz errno = ENOMEM;
90 1.1.1.1.6.2 riz return (NULL);
91 1.1.1.1.6.2 riz }
92 1.1.1.1.6.2 riz memset(sv, 0x0, sizeof *sv);
93 1.1.1.1.6.2 riz
94 1.1.1.1.6.2 riz if ((pvt = memget(sizeof *pvt)) == NULL) {
95 1.1.1.1.6.2 riz memput(sv, sizeof *sv);
96 1.1.1.1.6.2 riz errno = ENOMEM;
97 1.1.1.1.6.2 riz return (NULL);
98 1.1.1.1.6.2 riz }
99 1.1.1.1.6.2 riz memset(pvt, 0, sizeof *pvt);
100 1.1.1.1.6.2 riz pvt->girpdata = this->private;
101 1.1.1.1.6.2 riz
102 1.1.1.1.6.2 riz sv->private = pvt;
103 1.1.1.1.6.2 riz sv->close = sv_close;
104 1.1.1.1.6.2 riz sv->next = sv_next;
105 1.1.1.1.6.2 riz sv->byname = sv_byname;
106 1.1.1.1.6.2 riz sv->byport = sv_byport;
107 1.1.1.1.6.2 riz sv->rewind = sv_rewind;
108 1.1.1.1.6.2 riz sv->minimize = sv_minimize;
109 1.1.1.1.6.2 riz
110 1.1.1.1.6.2 riz return (sv);
111 1.1.1.1.6.2 riz }
112 1.1.1.1.6.2 riz
113 1.1.1.1.6.2 riz /* Methods */
114 1.1.1.1.6.2 riz
115 1.1.1.1.6.2 riz /*%
116 1.1.1.1.6.2 riz * void sv_close(struct irs_sv *this)
117 1.1.1.1.6.2 riz *
118 1.1.1.1.6.2 riz */
119 1.1.1.1.6.2 riz
120 1.1.1.1.6.2 riz static void
121 1.1.1.1.6.2 riz sv_close(struct irs_sv *this) {
122 1.1.1.1.6.2 riz struct pvt *pvt = (struct pvt *)this->private;
123 1.1.1.1.6.2 riz
124 1.1.1.1.6.2 riz sv_minimize(this);
125 1.1.1.1.6.2 riz
126 1.1.1.1.6.2 riz free_service(&pvt->service);
127 1.1.1.1.6.2 riz
128 1.1.1.1.6.2 riz memput(pvt, sizeof *pvt);
129 1.1.1.1.6.2 riz memput(this, sizeof *this);
130 1.1.1.1.6.2 riz }
131 1.1.1.1.6.2 riz
132 1.1.1.1.6.2 riz /*%
133 1.1.1.1.6.2 riz * Fills the cache if necessary and returns the next item from it.
134 1.1.1.1.6.2 riz *
135 1.1.1.1.6.2 riz */
136 1.1.1.1.6.2 riz
137 1.1.1.1.6.2 riz static struct servent *
138 1.1.1.1.6.2 riz sv_next(struct irs_sv *this) {
139 1.1.1.1.6.2 riz struct pvt *pvt = (struct pvt *)this->private;
140 1.1.1.1.6.2 riz struct servent *sv = &pvt->service;
141 1.1.1.1.6.2 riz char *body;
142 1.1.1.1.6.2 riz size_t bodylen;
143 1.1.1.1.6.2 riz int code;
144 1.1.1.1.6.2 riz char text[256];
145 1.1.1.1.6.2 riz
146 1.1.1.1.6.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
147 1.1.1.1.6.2 riz return (NULL);
148 1.1.1.1.6.2 riz }
149 1.1.1.1.6.2 riz
150 1.1.1.1.6.2 riz if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
151 1.1.1.1.6.2 riz return (NULL);
152 1.1.1.1.6.2 riz }
153 1.1.1.1.6.2 riz
154 1.1.1.1.6.2 riz if (irs_irp_get_full_response(pvt->girpdata, &code,
155 1.1.1.1.6.2 riz text, sizeof text,
156 1.1.1.1.6.2 riz &body, &bodylen) != 0) {
157 1.1.1.1.6.2 riz return (NULL);
158 1.1.1.1.6.2 riz }
159 1.1.1.1.6.2 riz
160 1.1.1.1.6.2 riz if (code == IRPD_GETSERVICE_OK) {
161 1.1.1.1.6.2 riz free_service(sv);
162 1.1.1.1.6.2 riz if (irp_unmarshall_sv(sv, body) != 0) {
163 1.1.1.1.6.2 riz sv = NULL;
164 1.1.1.1.6.2 riz }
165 1.1.1.1.6.2 riz } else {
166 1.1.1.1.6.2 riz sv = NULL;
167 1.1.1.1.6.2 riz }
168 1.1.1.1.6.2 riz
169 1.1.1.1.6.2 riz if (body != NULL) {
170 1.1.1.1.6.2 riz memput(body, bodylen);
171 1.1.1.1.6.2 riz }
172 1.1.1.1.6.2 riz
173 1.1.1.1.6.2 riz return (sv);
174 1.1.1.1.6.2 riz }
175 1.1.1.1.6.2 riz
176 1.1.1.1.6.2 riz /*%
177 1.1.1.1.6.2 riz * struct servent * sv_byname(struct irs_sv *this, const char *name,
178 1.1.1.1.6.2 riz * const char *proto)
179 1.1.1.1.6.2 riz *
180 1.1.1.1.6.2 riz */
181 1.1.1.1.6.2 riz
182 1.1.1.1.6.2 riz static struct servent *
183 1.1.1.1.6.2 riz sv_byname(struct irs_sv *this, const char *name, const char *proto) {
184 1.1.1.1.6.2 riz struct pvt *pvt = (struct pvt *)this->private;
185 1.1.1.1.6.2 riz struct servent *sv = &pvt->service;
186 1.1.1.1.6.2 riz char *body;
187 1.1.1.1.6.2 riz char text[256];
188 1.1.1.1.6.2 riz size_t bodylen;
189 1.1.1.1.6.2 riz int code;
190 1.1.1.1.6.2 riz
191 1.1.1.1.6.2 riz if (sv->s_name != NULL &&
192 1.1.1.1.6.2 riz strcmp(name, sv->s_name) == 0 &&
193 1.1.1.1.6.2 riz strcasecmp(proto, sv->s_proto) == 0) {
194 1.1.1.1.6.2 riz return (sv);
195 1.1.1.1.6.2 riz }
196 1.1.1.1.6.2 riz
197 1.1.1.1.6.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
198 1.1.1.1.6.2 riz return (NULL);
199 1.1.1.1.6.2 riz }
200 1.1.1.1.6.2 riz
201 1.1.1.1.6.2 riz if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
202 1.1.1.1.6.2 riz name, proto) != 0)
203 1.1.1.1.6.2 riz return (NULL);
204 1.1.1.1.6.2 riz
205 1.1.1.1.6.2 riz if (irs_irp_get_full_response(pvt->girpdata, &code,
206 1.1.1.1.6.2 riz text, sizeof text,
207 1.1.1.1.6.2 riz &body, &bodylen) != 0) {
208 1.1.1.1.6.2 riz return (NULL);
209 1.1.1.1.6.2 riz }
210 1.1.1.1.6.2 riz
211 1.1.1.1.6.2 riz if (code == IRPD_GETSERVICE_OK) {
212 1.1.1.1.6.2 riz free_service(sv);
213 1.1.1.1.6.2 riz if (irp_unmarshall_sv(sv, body) != 0) {
214 1.1.1.1.6.2 riz sv = NULL;
215 1.1.1.1.6.2 riz }
216 1.1.1.1.6.2 riz } else {
217 1.1.1.1.6.2 riz sv = NULL;
218 1.1.1.1.6.2 riz }
219 1.1.1.1.6.2 riz
220 1.1.1.1.6.2 riz if (body != NULL) {
221 1.1.1.1.6.2 riz memput(body, bodylen);
222 1.1.1.1.6.2 riz }
223 1.1.1.1.6.2 riz
224 1.1.1.1.6.2 riz return (sv);
225 1.1.1.1.6.2 riz }
226 1.1.1.1.6.2 riz
227 1.1.1.1.6.2 riz /*%
228 1.1.1.1.6.2 riz * struct servent * sv_byport(struct irs_sv *this, int port,
229 1.1.1.1.6.2 riz * const char *proto)
230 1.1.1.1.6.2 riz *
231 1.1.1.1.6.2 riz */
232 1.1.1.1.6.2 riz
233 1.1.1.1.6.2 riz static struct servent *
234 1.1.1.1.6.2 riz sv_byport(struct irs_sv *this, int port, const char *proto) {
235 1.1.1.1.6.2 riz struct pvt *pvt = (struct pvt *)this->private;
236 1.1.1.1.6.2 riz struct servent *sv = &pvt->service;
237 1.1.1.1.6.2 riz char *body;
238 1.1.1.1.6.2 riz size_t bodylen;
239 1.1.1.1.6.2 riz char text[256];
240 1.1.1.1.6.2 riz int code;
241 1.1.1.1.6.2 riz
242 1.1.1.1.6.2 riz if (sv->s_name != NULL &&
243 1.1.1.1.6.2 riz port == sv->s_port &&
244 1.1.1.1.6.2 riz strcasecmp(proto, sv->s_proto) == 0) {
245 1.1.1.1.6.2 riz return (sv);
246 1.1.1.1.6.2 riz }
247 1.1.1.1.6.2 riz
248 1.1.1.1.6.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
249 1.1.1.1.6.2 riz return (NULL);
250 1.1.1.1.6.2 riz }
251 1.1.1.1.6.2 riz
252 1.1.1.1.6.2 riz if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
253 1.1.1.1.6.2 riz ntohs((short)port), proto) != 0) {
254 1.1.1.1.6.2 riz return (NULL);
255 1.1.1.1.6.2 riz }
256 1.1.1.1.6.2 riz
257 1.1.1.1.6.2 riz if (irs_irp_get_full_response(pvt->girpdata, &code,
258 1.1.1.1.6.2 riz text, sizeof text,
259 1.1.1.1.6.2 riz &body, &bodylen) != 0) {
260 1.1.1.1.6.2 riz return (NULL);
261 1.1.1.1.6.2 riz }
262 1.1.1.1.6.2 riz
263 1.1.1.1.6.2 riz if (code == IRPD_GETSERVICE_OK) {
264 1.1.1.1.6.2 riz free_service(sv);
265 1.1.1.1.6.2 riz if (irp_unmarshall_sv(sv, body) != 0) {
266 1.1.1.1.6.2 riz sv = NULL;
267 1.1.1.1.6.2 riz }
268 1.1.1.1.6.2 riz } else {
269 1.1.1.1.6.2 riz sv = NULL;
270 1.1.1.1.6.2 riz }
271 1.1.1.1.6.2 riz
272 1.1.1.1.6.2 riz if (body != NULL) {
273 1.1.1.1.6.2 riz memput(body, bodylen);
274 1.1.1.1.6.2 riz }
275 1.1.1.1.6.2 riz
276 1.1.1.1.6.2 riz return (sv);
277 1.1.1.1.6.2 riz }
278 1.1.1.1.6.2 riz
279 1.1.1.1.6.2 riz /*%
280 1.1.1.1.6.2 riz * void sv_rewind(struct irs_sv *this)
281 1.1.1.1.6.2 riz *
282 1.1.1.1.6.2 riz */
283 1.1.1.1.6.2 riz
284 1.1.1.1.6.2 riz static void
285 1.1.1.1.6.2 riz sv_rewind(struct irs_sv *this) {
286 1.1.1.1.6.2 riz struct pvt *pvt = (struct pvt *)this->private;
287 1.1.1.1.6.2 riz char text[256];
288 1.1.1.1.6.2 riz int code;
289 1.1.1.1.6.2 riz
290 1.1.1.1.6.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
291 1.1.1.1.6.2 riz return;
292 1.1.1.1.6.2 riz }
293 1.1.1.1.6.2 riz
294 1.1.1.1.6.2 riz if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
295 1.1.1.1.6.2 riz return;
296 1.1.1.1.6.2 riz }
297 1.1.1.1.6.2 riz
298 1.1.1.1.6.2 riz code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
299 1.1.1.1.6.2 riz if (code != IRPD_GETSERVICE_SETOK) {
300 1.1.1.1.6.2 riz if (irp_log_errors) {
301 1.1.1.1.6.2 riz syslog(LOG_WARNING, "setservent failed: %s", text);
302 1.1.1.1.6.2 riz }
303 1.1.1.1.6.2 riz }
304 1.1.1.1.6.2 riz
305 1.1.1.1.6.2 riz return;
306 1.1.1.1.6.2 riz }
307 1.1.1.1.6.2 riz
308 1.1.1.1.6.2 riz /*%
309 1.1.1.1.6.2 riz * void sv_minimize(struct irs_sv *this)
310 1.1.1.1.6.2 riz *
311 1.1.1.1.6.2 riz */
312 1.1.1.1.6.2 riz
313 1.1.1.1.6.2 riz static void
314 1.1.1.1.6.2 riz sv_minimize(struct irs_sv *this) {
315 1.1.1.1.6.2 riz struct pvt *pvt = (struct pvt *)this->private;
316 1.1.1.1.6.2 riz
317 1.1.1.1.6.2 riz irs_irp_disconnect(pvt->girpdata);
318 1.1.1.1.6.2 riz }
319 1.1.1.1.6.2 riz
320 1.1.1.1.6.2 riz
321 1.1.1.1.6.2 riz
322 1.1.1.1.6.2 riz
323 1.1.1.1.6.2 riz
324 1.1.1.1.6.2 riz
325 1.1.1.1.6.2 riz static void
326 1.1.1.1.6.2 riz free_service(struct servent *sv) {
327 1.1.1.1.6.2 riz char **p;
328 1.1.1.1.6.2 riz
329 1.1.1.1.6.2 riz if (sv == NULL) {
330 1.1.1.1.6.2 riz return;
331 1.1.1.1.6.2 riz }
332 1.1.1.1.6.2 riz
333 1.1.1.1.6.2 riz if (sv->s_name != NULL) {
334 1.1.1.1.6.2 riz free(sv->s_name);
335 1.1.1.1.6.2 riz }
336 1.1.1.1.6.2 riz
337 1.1.1.1.6.2 riz for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
338 1.1.1.1.6.2 riz free(*p);
339 1.1.1.1.6.2 riz }
340 1.1.1.1.6.2 riz
341 1.1.1.1.6.2 riz if (sv->s_proto != NULL) {
342 1.1.1.1.6.2 riz free(sv->s_proto);
343 1.1.1.1.6.2 riz }
344 1.1.1.1.6.2 riz }
345 1.1.1.1.6.2 riz
346 1.1.1.1.6.2 riz
347 1.1.1.1.6.2 riz
348 1.1.1.1.6.2 riz /*! \file */
349