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 #include "port_before.h"
21 #include <sys/types.h>
22 #include <sys/socket.h>
37 #include <isc/irpmarshall.h>
38 #include <isc/memcluster.h>
44 #include "port_after.h"
49 struct irp_p
*girpdata
;
51 struct servent service
;
56 static void sv_close(struct irs_sv
*);
57 static struct servent
* sv_next(struct irs_sv
*);
58 static struct servent
* sv_byname(struct irs_sv
*, const char *,
60 static struct servent
* sv_byport(struct irs_sv
*, int, const char *);
61 static void sv_rewind(struct irs_sv
*);
62 static void sv_minimize(struct irs_sv
*);
64 static void free_service(struct servent
*sv
);
71 * struct irs_sv * irs_irp_sv(struct irs_acc *this)
76 irs_irp_sv(struct irs_acc
*this) {
80 if ((sv
= memget(sizeof *sv
)) == NULL
) {
84 memset(sv
, 0x0, sizeof *sv
);
86 if ((pvt
= memget(sizeof *pvt
)) == NULL
) {
87 memput(sv
, sizeof *sv
);
91 memset(pvt
, 0, sizeof *pvt
);
92 pvt
->girpdata
= this->private;
97 sv
->byname
= sv_byname
;
98 sv
->byport
= sv_byport
;
99 sv
->rewind
= sv_rewind
;
100 sv
->minimize
= sv_minimize
;
108 * void sv_close(struct irs_sv *this)
113 sv_close(struct irs_sv
*this) {
114 struct pvt
*pvt
= (struct pvt
*)this->private;
118 free_service(&pvt
->service
);
120 memput(pvt
, sizeof *pvt
);
121 memput(this, sizeof *this);
125 * Fills the cache if necessary and returns the next item from it.
129 static struct servent
*
130 sv_next(struct irs_sv
*this) {
131 struct pvt
*pvt
= (struct pvt
*)this->private;
132 struct servent
*sv
= &pvt
->service
;
138 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
142 if (irs_irp_send_command(pvt
->girpdata
, "getservent") != 0) {
146 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
148 &body
, &bodylen
) != 0) {
152 if (code
== IRPD_GETSERVICE_OK
) {
154 if (irp_unmarshall_sv(sv
, body
) != 0) {
162 memput(body
, bodylen
);
169 * struct servent * sv_byname(struct irs_sv *this, const char *name,
174 static struct servent
*
175 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
176 struct pvt
*pvt
= (struct pvt
*)this->private;
177 struct servent
*sv
= &pvt
->service
;
183 if (sv
->s_name
!= NULL
&&
184 strcmp(name
, sv
->s_name
) == 0 &&
185 strcasecmp(proto
, sv
->s_proto
) == 0) {
189 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
193 if (irs_irp_send_command(pvt
->girpdata
, "getservbyname %s %s",
197 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
199 &body
, &bodylen
) != 0) {
203 if (code
== IRPD_GETSERVICE_OK
) {
205 if (irp_unmarshall_sv(sv
, body
) != 0) {
213 memput(body
, bodylen
);
220 * struct servent * sv_byport(struct irs_sv *this, int port,
225 static struct servent
*
226 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
227 struct pvt
*pvt
= (struct pvt
*)this->private;
228 struct servent
*sv
= &pvt
->service
;
234 if (sv
->s_name
!= NULL
&&
235 port
== sv
->s_port
&&
236 strcasecmp(proto
, sv
->s_proto
) == 0) {
240 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
244 if (irs_irp_send_command(pvt
->girpdata
, "getservbyport %d %s",
245 ntohs((short)port
), proto
) != 0) {
249 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
251 &body
, &bodylen
) != 0) {
255 if (code
== IRPD_GETSERVICE_OK
) {
257 if (irp_unmarshall_sv(sv
, body
) != 0) {
265 memput(body
, bodylen
);
272 * void sv_rewind(struct irs_sv *this)
277 sv_rewind(struct irs_sv
*this) {
278 struct pvt
*pvt
= (struct pvt
*)this->private;
282 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
286 if (irs_irp_send_command(pvt
->girpdata
, "setservent") != 0) {
290 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
291 if (code
!= IRPD_GETSERVICE_SETOK
) {
292 if (irp_log_errors
) {
293 syslog(LOG_WARNING
, "setservent failed: %s", text
);
301 * void sv_minimize(struct irs_sv *this)
306 sv_minimize(struct irs_sv
*this) {
307 struct pvt
*pvt
= (struct pvt
*)this->private;
309 irs_irp_disconnect(pvt
->girpdata
);
318 free_service(struct servent
*sv
) {
325 if (sv
->s_name
!= NULL
) {
329 for (p
= sv
->s_aliases
; p
!= NULL
&& *p
!= NULL
; p
++) {
333 if (sv
->s_proto
!= NULL
) {