4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (c) 1996 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_pr.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>
41 #include <isc/memcluster.h>
42 #include <isc/irpmarshall.h>
48 #include "port_after.h"
56 struct irp_p
*girpdata
;
58 struct protoent proto
;
63 static void pr_close(struct irs_pr
*);
64 static struct protoent
* pr_next(struct irs_pr
*);
65 static struct protoent
* pr_byname(struct irs_pr
*, const char *);
66 static struct protoent
* pr_bynumber(struct irs_pr
*, int);
67 static void pr_rewind(struct irs_pr
*);
68 static void pr_minimize(struct irs_pr
*);
70 static void free_proto(struct protoent
*pr
);
75 * struct irs_pr * irs_irp_pr(struct irs_acc *this)
80 irs_irp_pr(struct irs_acc
*this) {
84 if (!(pr
= memget(sizeof *pr
))) {
88 memset(pr
, 0x0, sizeof *pr
);
90 if (!(pvt
= memget(sizeof *pvt
))) {
91 memput(pr
, sizeof *pr
);
95 memset(pvt
, 0, sizeof *pvt
);
96 pvt
->girpdata
= this->private;
100 pr
->byname
= pr_byname
;
101 pr
->bynumber
= pr_bynumber
;
103 pr
->rewind
= pr_rewind
;
104 pr
->minimize
= pr_minimize
;
111 * void pr_close(struct irs_pr *this)
116 pr_close(struct irs_pr
*this) {
117 struct pvt
*pvt
= (struct pvt
*)this->private;
121 free_proto(&pvt
->proto
);
123 memput(pvt
, sizeof *pvt
);
124 memput(this, sizeof *this);
128 * struct protoent * pr_byname(struct irs_pr *this, const char *name)
132 static struct protoent
*
133 pr_byname(struct irs_pr
*this, const char *name
) {
134 struct pvt
*pvt
= (struct pvt
*)this->private;
135 struct protoent
*pr
= &pvt
->proto
;
142 if (pr
->p_name
!= NULL
&& strcmp(name
, pr
->p_name
) == 0) {
146 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
150 i
= irs_irp_send_command(pvt
->girpdata
, "getprotobyname %s", name
);
154 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
156 &body
, &bodylen
) != 0) {
160 if (code
== IRPD_GETPROTO_OK
) {
162 if (irp_unmarshall_pr(pr
, body
) != 0) {
170 memput(body
, bodylen
);
177 * struct protoent * pr_bynumber(struct irs_pr *this, int proto)
181 static struct protoent
*
182 pr_bynumber(struct irs_pr
*this, int proto
) {
183 struct pvt
*pvt
= (struct pvt
*)this->private;
184 struct protoent
*pr
= &pvt
->proto
;
191 if (pr
->p_name
!= NULL
&& proto
== pr
->p_proto
) {
195 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
199 i
= irs_irp_send_command(pvt
->girpdata
, "getprotobynumber %d", proto
);
203 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
205 &body
, &bodylen
) != 0) {
209 if (code
== IRPD_GETPROTO_OK
) {
211 if (irp_unmarshall_pr(pr
, body
) != 0) {
219 memput(body
, bodylen
);
226 * void pr_rewind(struct irs_pr *this)
231 pr_rewind(struct irs_pr
*this) {
232 struct pvt
*pvt
= (struct pvt
*)this->private;
236 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
240 if (irs_irp_send_command(pvt
->girpdata
, "setprotoent") != 0) {
244 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
245 if (code
!= IRPD_GETPROTO_SETOK
) {
246 if (irp_log_errors
) {
247 syslog(LOG_WARNING
, "setprotoent failed: %s", text
);
255 * Prepares the cache if necessary and returns the next item in it.
259 static struct protoent
*
260 pr_next(struct irs_pr
*this) {
261 struct pvt
*pvt
= (struct pvt
*)this->private;
262 struct protoent
*pr
= &pvt
->proto
;
268 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
272 if (irs_irp_send_command(pvt
->girpdata
, "getprotoent") != 0) {
276 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
278 &body
, &bodylen
) != 0) {
282 if (code
== IRPD_GETPROTO_OK
) {
284 if (irp_unmarshall_pr(pr
, body
) != 0) {
292 memput(body
, bodylen
);
299 * void pr_minimize(struct irs_pr *this)
304 pr_minimize(struct irs_pr
*this) {
305 struct pvt
*pvt
= (struct pvt
*)this->private;
307 irs_irp_disconnect(pvt
->girpdata
);
311 * Deallocate all the memory irp_unmarshall_pr allocated.
316 free_proto(struct protoent
*pr
) {
322 if (pr
->p_name
!= NULL
)
325 for (p
= pr
->p_aliases
; p
!= NULL
&& *p
!= NULL
; p
++)