Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / irp_sv.c
blob316bec34460803fd8557cd8b041aacfac2a997fc
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
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.
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.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: irp_sv.c,v 1.1.2.1 2004/03/09 09:17:32 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 /* extern */
24 #include "port_before.h"
26 #include <syslog.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
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>
41 #include <irs.h>
42 #include <irp.h>
43 #include <isc/irpmarshall.h>
44 #include <isc/memcluster.h>
46 #include "irs_p.h"
47 #include "lcl_p.h"
48 #include "irp_p.h"
50 #include "port_after.h"
52 /* Types */
54 struct pvt {
55 struct irp_p *girpdata;
56 int warned;
57 struct servent service;
60 /* Forward */
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 *);
70 static void free_service(struct servent *sv);
74 /* Public */
79 * struct irs_sv * irs_irp_sv(struct irs_acc *this)
83 struct irs_sv *
84 irs_irp_sv(struct irs_acc *this) {
85 struct irs_sv *sv;
86 struct pvt *pvt;
88 if ((sv = memget(sizeof *sv)) == NULL) {
89 errno = ENOMEM;
90 return (NULL);
92 memset(sv, 0x0, sizeof *sv);
94 if ((pvt = memget(sizeof *pvt)) == NULL) {
95 memput(sv, sizeof *sv);
96 errno = ENOMEM;
97 return (NULL);
99 memset(pvt, 0, sizeof *pvt);
100 pvt->girpdata = this->private;
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;
110 return (sv);
113 /* Methods */
118 * void sv_close(struct irs_sv *this)
122 static void
123 sv_close(struct irs_sv *this) {
124 struct pvt *pvt = (struct pvt *)this->private;
126 sv_minimize(this);
128 free_service(&pvt->service);
130 memput(pvt, sizeof *pvt);
131 memput(this, sizeof *this);
138 * struct servent * sv_next(struct irs_sv *this)
140 * Notes:
142 * Fills the cache if necessary and returns the next item from it.
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];
155 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
156 return (NULL);
159 if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
160 return (NULL);
163 if (irs_irp_get_full_response(pvt->girpdata, &code,
164 text, sizeof text,
165 &body, &bodylen) != 0) {
166 return (NULL);
169 if (code == IRPD_GETSERVICE_OK) {
170 free_service(sv);
171 if (irp_unmarshall_sv(sv, body) != 0) {
172 sv = NULL;
174 } else {
175 sv = NULL;
178 if (body != NULL) {
179 memput(body, bodylen);
182 return (sv);
189 * struct servent * sv_byname(struct irs_sv *this, const char *name,
190 * const char *proto)
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;
203 if (sv->s_name != NULL &&
204 strcmp(name, sv->s_name) == 0 &&
205 strcasecmp(proto, sv->s_proto) == 0) {
206 return (sv);
209 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
210 return (NULL);
213 if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
214 name, proto) != 0)
215 return (NULL);
217 if (irs_irp_get_full_response(pvt->girpdata, &code,
218 text, sizeof text,
219 &body, &bodylen) != 0) {
220 return (NULL);
223 if (code == IRPD_GETSERVICE_OK) {
224 free_service(sv);
225 if (irp_unmarshall_sv(sv, body) != 0) {
226 sv = NULL;
228 } else {
229 sv = NULL;
232 if (body != NULL) {
233 memput(body, bodylen);
236 return (sv);
243 * struct servent * sv_byport(struct irs_sv *this, int port,
244 * const char *proto)
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;
257 if (sv->s_name != NULL &&
258 port == sv->s_port &&
259 strcasecmp(proto, sv->s_proto) == 0) {
260 return (sv);
263 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
264 return (NULL);
267 if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
268 ntohs((short)port), proto) != 0) {
269 return (NULL);
272 if (irs_irp_get_full_response(pvt->girpdata, &code,
273 text, sizeof text,
274 &body, &bodylen) != 0) {
275 return (NULL);
278 if (code == IRPD_GETSERVICE_OK) {
279 free_service(sv);
280 if (irp_unmarshall_sv(sv, body) != 0) {
281 sv = NULL;
283 } else {
284 sv = NULL;
287 if (body != NULL) {
288 memput(body, bodylen);
291 return (sv);
299 * void sv_rewind(struct irs_sv *this)
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;
309 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
310 return;
313 if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
314 return;
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);
324 return;
332 * void sv_minimize(struct irs_sv *this)
336 static void
337 sv_minimize(struct irs_sv *this) {
338 struct pvt *pvt = (struct pvt *)this->private;
340 irs_irp_disconnect(pvt->girpdata);
348 static void
349 free_service(struct servent *sv) {
350 char **p;
352 if (sv == NULL) {
353 return;
356 if (sv->s_name != NULL) {
357 free(sv->s_name);
360 for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
361 free(*p);
364 if (sv->s_proto != NULL) {
365 free(sv->s_proto);