Merge branch 'vendor/BINUTILS220' into bu220
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / irp_sv.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (c) 1996,1998 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(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: irp_sv.c,v 1.1.206.1 2004/03/09 08:33:37 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 /* extern */
23
24 #include "port_before.h"
25
26 #include <syslog.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29
30 #ifdef IRS_LCL_SV_DB
31 #include <db.h>
32 #endif
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <syslog.h>
40
41 #include <irs.h>
42 #include <irp.h>
43 #include <isc/irpmarshall.h>
44 #include <isc/memcluster.h>
45
46 #include "irs_p.h"
47 #include "lcl_p.h"
48 #include "irp_p.h"
49
50 #include "port_after.h"
51
52 /* Types */
53
54 struct pvt {
55         struct irp_p           *girpdata;
56         int                     warned;
57         struct servent          service;
58 };
59
60 /* Forward */
61
62 static void                     sv_close(struct irs_sv*);
63 static struct servent *         sv_next(struct irs_sv *);
64 static struct servent *         sv_byname(struct irs_sv *, const char *,
65                                           const char *);
66 static struct servent *         sv_byport(struct irs_sv *, int, const char *);
67 static void                     sv_rewind(struct irs_sv *);
68 static void                     sv_minimize(struct irs_sv *);
69
70 static void                     free_service(struct servent *sv);
71
72
73
74 /* Public */
75
76
77
78 /*
79  * struct irs_sv * irs_irp_sv(struct irs_acc *this)
80  *
81  */
82
83 struct irs_sv *
84 irs_irp_sv(struct irs_acc *this) {
85         struct irs_sv *sv;
86         struct pvt *pvt;
87
88         if ((sv = memget(sizeof *sv)) == NULL) {
89                 errno = ENOMEM;
90                 return (NULL);
91         }
92         memset(sv, 0x0, sizeof *sv);
93
94         if ((pvt = memget(sizeof *pvt)) == NULL) {
95                 memput(sv, sizeof *sv);
96                 errno = ENOMEM;
97                 return (NULL);
98         }
99         memset(pvt, 0, sizeof *pvt);
100         pvt->girpdata = this->private;
101
102         sv->private = pvt;
103         sv->close = sv_close;
104         sv->next = sv_next;
105         sv->byname = sv_byname;
106         sv->byport = sv_byport;
107         sv->rewind = sv_rewind;
108         sv->minimize = sv_minimize;
109
110         return (sv);
111 }
112
113 /* Methods */
114
115
116
117 /*
118  * void sv_close(struct irs_sv *this)
119  *
120  */
121
122 static void
123 sv_close(struct irs_sv *this) {
124         struct pvt *pvt = (struct pvt *)this->private;
125
126         sv_minimize(this);
127
128         free_service(&pvt->service);
129
130         memput(pvt, sizeof *pvt);
131         memput(this, sizeof *this);
132 }
133
134
135
136
137 /*
138  * struct servent * sv_next(struct irs_sv *this)
139  *
140  * Notes:
141  *
142  *      Fills the cache if necessary and returns the next item from it.
143  *
144  */
145
146 static struct servent *
147 sv_next(struct irs_sv *this) {
148         struct pvt *pvt = (struct pvt *)this->private;
149         struct servent *sv = &pvt->service;
150         char *body;
151         size_t bodylen;
152         int code;
153         char text[256];
154
155         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
156                 return (NULL);
157         }
158
159         if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
160                 return (NULL);
161         }
162
163         if (irs_irp_get_full_response(pvt->girpdata, &code,
164                                       text, sizeof text,
165                                       &body, &bodylen) != 0) {
166                 return (NULL);
167         }
168
169         if (code == IRPD_GETSERVICE_OK) {
170                 free_service(sv);
171                 if (irp_unmarshall_sv(sv, body) != 0) {
172                         sv = NULL;
173                 }
174         } else {
175                 sv = NULL;
176         }
177
178         if (body != NULL) {
179                 memput(body, bodylen);
180         }
181
182         return (sv);
183 }
184
185
186
187
188 /*
189  * struct servent * sv_byname(struct irs_sv *this, const char *name,
190  *                              const char *proto)
191  *
192  */
193
194 static struct servent *
195 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
196         struct pvt *pvt = (struct pvt *)this->private;
197         struct servent *sv = &pvt->service;
198         char *body;
199         char text[256];
200         size_t bodylen;
201         int code;
202
203         if (sv->s_name != NULL &&
204             strcmp(name, sv->s_name) == 0 &&
205             strcasecmp(proto, sv->s_proto) == 0) {
206                 return (sv);
207         }
208
209         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
210                 return (NULL);
211         }
212
213         if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
214                                  name, proto) != 0)
215                 return (NULL);
216
217         if (irs_irp_get_full_response(pvt->girpdata, &code,
218                                       text, sizeof text,
219                                       &body, &bodylen) != 0) {
220                 return (NULL);
221         }
222
223         if (code == IRPD_GETSERVICE_OK) {
224                 free_service(sv);
225                 if (irp_unmarshall_sv(sv, body) != 0) {
226                         sv = NULL;
227                 }
228         } else {
229                 sv = NULL;
230         }
231
232         if (body != NULL) {
233                 memput(body, bodylen);
234         }
235
236         return (sv);
237 }
238
239
240
241
242 /*
243  * struct servent * sv_byport(struct irs_sv *this, int port,
244  *                              const char *proto)
245  *
246  */
247
248 static struct servent *
249 sv_byport(struct irs_sv *this, int port, const char *proto) {
250         struct pvt *pvt = (struct pvt *)this->private;
251         struct servent *sv = &pvt->service;
252         char *body;
253         size_t bodylen;
254         char text[256];
255         int code;
256
257         if (sv->s_name != NULL &&
258             port == sv->s_port &&
259             strcasecmp(proto, sv->s_proto) == 0) {
260                 return (sv);
261         }
262
263         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
264                 return (NULL);
265         }
266
267         if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
268                                  ntohs((short)port), proto) != 0) {
269                 return (NULL);
270         }
271
272         if (irs_irp_get_full_response(pvt->girpdata, &code,
273                                       text, sizeof text,
274                                       &body, &bodylen) != 0) {
275                 return (NULL);
276         }
277
278         if (code == IRPD_GETSERVICE_OK) {
279                 free_service(sv);
280                 if (irp_unmarshall_sv(sv, body) != 0) {
281                         sv = NULL;
282                 }
283         } else {
284                 sv = NULL;
285         }
286
287         if (body != NULL) {
288                 memput(body, bodylen);
289         }
290
291         return (sv);
292 }
293
294
295
296
297
298 /*
299  * void sv_rewind(struct irs_sv *this)
300  *
301  */
302
303 static void
304 sv_rewind(struct irs_sv *this) {
305         struct pvt *pvt = (struct pvt *)this->private;
306         char text[256];
307         int code;
308
309         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
310                 return;
311         }
312
313         if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
314                 return;
315         }
316
317         code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
318         if (code != IRPD_GETSERVICE_SETOK) {
319                 if (irp_log_errors) {
320                         syslog(LOG_WARNING, "setservent failed: %s", text);
321                 }
322         }
323
324         return;
325 }
326
327
328
329
330
331 /*
332  * void sv_minimize(struct irs_sv *this)
333  *
334  */
335
336 static void
337 sv_minimize(struct irs_sv *this) {
338         struct pvt *pvt = (struct pvt *)this->private;
339
340         irs_irp_disconnect(pvt->girpdata);
341 }
342
343
344
345
346
347
348 static void
349 free_service(struct servent *sv) {
350         char **p;
351
352         if (sv == NULL) {
353                 return;
354         }
355
356         if (sv->s_name != NULL) {
357                 free(sv->s_name);
358         }
359
360         for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
361                 free(*p);
362         }
363
364         if (sv->s_proto != NULL) {
365                 free(sv->s_proto);
366         }
367 }
368
369