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.3 2005/04/27 04:56:29 sra Exp $";
20 #endif /* LIBC_SCCS and not lint */
24 #include "port_before.h"
27 #include <sys/types.h>
28 #include <sys/socket.h>
43 #include <isc/irpmarshall.h>
44 #include <isc/memcluster.h>
50 #include "port_after.h"
55 struct irp_p *girpdata;
57 struct servent service;
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 *,
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);
77 * struct irs_sv * irs_irp_sv(struct irs_acc *this)
82 irs_irp_sv(struct irs_acc *this) {
86 if ((sv = memget(sizeof *sv)) == NULL) {
90 memset(sv, 0x0, sizeof *sv);
92 if ((pvt = memget(sizeof *pvt)) == NULL) {
93 memput(sv, sizeof *sv);
97 memset(pvt, 0, sizeof *pvt);
98 pvt->girpdata = this->private;
101 sv->close = sv_close;
103 sv->byname = sv_byname;
104 sv->byport = sv_byport;
105 sv->rewind = sv_rewind;
106 sv->minimize = sv_minimize;
114 * void sv_close(struct irs_sv *this)
119 sv_close(struct irs_sv *this) {
120 struct pvt *pvt = (struct pvt *)this->private;
124 free_service(&pvt->service);
126 memput(pvt, sizeof *pvt);
127 memput(this, sizeof *this);
131 * Fills the cache if necessary and returns the next item from it.
135 static struct servent *
136 sv_next(struct irs_sv *this) {
137 struct pvt *pvt = (struct pvt *)this->private;
138 struct servent *sv = &pvt->service;
144 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
148 if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
152 if (irs_irp_get_full_response(pvt->girpdata, &code,
154 &body, &bodylen) != 0) {
158 if (code == IRPD_GETSERVICE_OK) {
160 if (irp_unmarshall_sv(sv, body) != 0) {
168 memput(body, bodylen);
175 * struct servent * sv_byname(struct irs_sv *this, const char *name,
180 static struct servent *
181 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
182 struct pvt *pvt = (struct pvt *)this->private;
183 struct servent *sv = &pvt->service;
189 if (sv->s_name != NULL &&
190 strcmp(name, sv->s_name) == 0 &&
191 strcasecmp(proto, sv->s_proto) == 0) {
195 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
199 if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
203 if (irs_irp_get_full_response(pvt->girpdata, &code,
205 &body, &bodylen) != 0) {
209 if (code == IRPD_GETSERVICE_OK) {
211 if (irp_unmarshall_sv(sv, body) != 0) {
219 memput(body, bodylen);
226 * struct servent * sv_byport(struct irs_sv *this, int port,
231 static struct servent *
232 sv_byport(struct irs_sv *this, int port, const char *proto) {
233 struct pvt *pvt = (struct pvt *)this->private;
234 struct servent *sv = &pvt->service;
240 if (sv->s_name != NULL &&
241 port == sv->s_port &&
242 strcasecmp(proto, sv->s_proto) == 0) {
246 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
250 if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
251 ntohs((short)port), proto) != 0) {
255 if (irs_irp_get_full_response(pvt->girpdata, &code,
257 &body, &bodylen) != 0) {
261 if (code == IRPD_GETSERVICE_OK) {
263 if (irp_unmarshall_sv(sv, body) != 0) {
271 memput(body, bodylen);
278 * void sv_rewind(struct irs_sv *this)
283 sv_rewind(struct irs_sv *this) {
284 struct pvt *pvt = (struct pvt *)this->private;
288 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
292 if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
296 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
297 if (code != IRPD_GETSERVICE_SETOK) {
298 if (irp_log_errors) {
299 syslog(LOG_WARNING, "setservent failed: %s", text);
307 * void sv_minimize(struct irs_sv *this)
312 sv_minimize(struct irs_sv *this) {
313 struct pvt *pvt = (struct pvt *)this->private;
315 irs_irp_disconnect(pvt->girpdata);
324 free_service(struct servent *sv) {
331 if (sv->s_name != NULL) {
335 for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
339 if (sv->s_proto != NULL) {