4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp";
22 #endif /* LIBC_SCCS and not lint */
26 #include "port_before.h"
29 #include <sys/types.h>
30 #include <sys/socket.h>
45 #include <isc/irpmarshall.h>
46 #include <isc/memcluster.h>
52 #include "port_after.h"
57 struct irp_p
*girpdata
;
59 struct servent service
;
64 static void sv_close(struct irs_sv
*);
65 static struct servent
* sv_next(struct irs_sv
*);
66 static struct servent
* sv_byname(struct irs_sv
*, const char *,
68 static struct servent
* sv_byport(struct irs_sv
*, int, const char *);
69 static void sv_rewind(struct irs_sv
*);
70 static void sv_minimize(struct irs_sv
*);
72 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
;
116 * void sv_close(struct irs_sv *this)
121 sv_close(struct irs_sv
*this) {
122 struct pvt
*pvt
= (struct pvt
*)this->private;
126 free_service(&pvt
->service
);
128 memput(pvt
, sizeof *pvt
);
129 memput(this, sizeof *this);
133 * Fills the cache if necessary and returns the next item from it.
137 static struct servent
*
138 sv_next(struct irs_sv
*this) {
139 struct pvt
*pvt
= (struct pvt
*)this->private;
140 struct servent
*sv
= &pvt
->service
;
146 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
150 if (irs_irp_send_command(pvt
->girpdata
, "getservent") != 0) {
154 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
156 &body
, &bodylen
) != 0) {
160 if (code
== IRPD_GETSERVICE_OK
) {
162 if (irp_unmarshall_sv(sv
, body
) != 0) {
170 memput(body
, bodylen
);
177 * struct servent * sv_byname(struct irs_sv *this, const char *name,
182 static struct servent
*
183 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
184 struct pvt
*pvt
= (struct pvt
*)this->private;
185 struct servent
*sv
= &pvt
->service
;
191 if (sv
->s_name
!= NULL
&&
192 strcmp(name
, sv
->s_name
) == 0 &&
193 strcasecmp(proto
, sv
->s_proto
) == 0) {
197 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
201 if (irs_irp_send_command(pvt
->girpdata
, "getservbyname %s %s",
205 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
207 &body
, &bodylen
) != 0) {
211 if (code
== IRPD_GETSERVICE_OK
) {
213 if (irp_unmarshall_sv(sv
, body
) != 0) {
221 memput(body
, bodylen
);
228 * struct servent * sv_byport(struct irs_sv *this, int port,
233 static struct servent
*
234 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
235 struct pvt
*pvt
= (struct pvt
*)this->private;
236 struct servent
*sv
= &pvt
->service
;
242 if (sv
->s_name
!= NULL
&&
243 port
== sv
->s_port
&&
244 strcasecmp(proto
, sv
->s_proto
) == 0) {
248 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
252 if (irs_irp_send_command(pvt
->girpdata
, "getservbyport %d %s",
253 ntohs((short)port
), proto
) != 0) {
257 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
259 &body
, &bodylen
) != 0) {
263 if (code
== IRPD_GETSERVICE_OK
) {
265 if (irp_unmarshall_sv(sv
, body
) != 0) {
273 memput(body
, bodylen
);
280 * void sv_rewind(struct irs_sv *this)
285 sv_rewind(struct irs_sv
*this) {
286 struct pvt
*pvt
= (struct pvt
*)this->private;
290 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
294 if (irs_irp_send_command(pvt
->girpdata
, "setservent") != 0) {
298 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
299 if (code
!= IRPD_GETSERVICE_SETOK
) {
300 if (irp_log_errors
) {
301 syslog(LOG_WARNING
, "setservent failed: %s", text
);
309 * void sv_minimize(struct irs_sv *this)
314 sv_minimize(struct irs_sv
*this) {
315 struct pvt
*pvt
= (struct pvt
*)this->private;
317 irs_irp_disconnect(pvt
->girpdata
);
326 free_service(struct servent
*sv
) {
333 if (sv
->s_name
!= NULL
) {
337 for (p
= sv
->s_aliases
; p
!= NULL
&& *p
!= NULL
; p
++) {
341 if (sv
->s_proto
!= NULL
) {