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 */
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
);
79 * struct irs_sv * irs_irp_sv(struct irs_acc *this)
84 irs_irp_sv(struct irs_acc
*this) {
88 if ((sv
= memget(sizeof *sv
)) == NULL
) {
92 memset(sv
, 0x0, sizeof *sv
);
94 if ((pvt
= memget(sizeof *pvt
)) == NULL
) {
95 memput(sv
, sizeof *sv
);
99 memset(pvt
, 0, sizeof *pvt
);
100 pvt
->girpdata
= this->private;
103 sv
->close
= sv_close
;
105 sv
->byname
= sv_byname
;
106 sv
->byport
= sv_byport
;
107 sv
->rewind
= sv_rewind
;
108 sv
->minimize
= sv_minimize
;
118 * void sv_close(struct irs_sv *this)
123 sv_close(struct irs_sv
*this) {
124 struct pvt
*pvt
= (struct pvt
*)this->private;
128 free_service(&pvt
->service
);
130 memput(pvt
, sizeof *pvt
);
131 memput(this, sizeof *this);
138 * struct servent * sv_next(struct irs_sv *this)
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
;
155 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
159 if (irs_irp_send_command(pvt
->girpdata
, "getservent") != 0) {
163 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
165 &body
, &bodylen
) != 0) {
169 if (code
== IRPD_GETSERVICE_OK
) {
171 if (irp_unmarshall_sv(sv
, body
) != 0) {
179 memput(body
, bodylen
);
189 * struct servent * sv_byname(struct irs_sv *this, const char *name,
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
;
203 if (sv
->s_name
!= NULL
&&
204 strcmp(name
, sv
->s_name
) == 0 &&
205 strcasecmp(proto
, sv
->s_proto
) == 0) {
209 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
213 if (irs_irp_send_command(pvt
->girpdata
, "getservbyname %s %s",
217 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
219 &body
, &bodylen
) != 0) {
223 if (code
== IRPD_GETSERVICE_OK
) {
225 if (irp_unmarshall_sv(sv
, body
) != 0) {
233 memput(body
, bodylen
);
243 * struct servent * sv_byport(struct irs_sv *this, int port,
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
;
257 if (sv
->s_name
!= NULL
&&
258 port
== sv
->s_port
&&
259 strcasecmp(proto
, sv
->s_proto
) == 0) {
263 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
267 if (irs_irp_send_command(pvt
->girpdata
, "getservbyport %d %s",
268 ntohs((short)port
), proto
) != 0) {
272 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
274 &body
, &bodylen
) != 0) {
278 if (code
== IRPD_GETSERVICE_OK
) {
280 if (irp_unmarshall_sv(sv
, body
) != 0) {
288 memput(body
, bodylen
);
299 * void sv_rewind(struct irs_sv *this)
304 sv_rewind(struct irs_sv
*this) {
305 struct pvt
*pvt
= (struct pvt
*)this->private;
309 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
313 if (irs_irp_send_command(pvt
->girpdata
, "setservent") != 0) {
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
);
332 * void sv_minimize(struct irs_sv *this)
337 sv_minimize(struct irs_sv
*this) {
338 struct pvt
*pvt
= (struct pvt
*)this->private;
340 irs_irp_disconnect(pvt
->girpdata
);
349 free_service(struct servent
*sv
) {
356 if (sv
->s_name
!= NULL
) {
360 for (p
= sv
->s_aliases
; p
!= NULL
&& *p
!= NULL
; p
++) {
364 if (sv
->s_proto
!= NULL
) {