2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (c) 1996 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_pr.c,v 1.1.2.1 2004/03/09 09:17:31 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
24 #include "port_before.h"
27 #include <sys/types.h>
39 #include <isc/memcluster.h>
40 #include <isc/irpmarshall.h>
46 #include "port_after.h"
54 struct irp_p
*girpdata
;
56 struct protoent proto
;
61 static void pr_close(struct irs_pr
*);
62 static struct protoent
* pr_next(struct irs_pr
*);
63 static struct protoent
* pr_byname(struct irs_pr
*, const char *);
64 static struct protoent
* pr_bynumber(struct irs_pr
*, int);
65 static void pr_rewind(struct irs_pr
*);
66 static void pr_minimize(struct irs_pr
*);
68 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
;
113 * void pr_close(struct irs_pr *this)
118 pr_close(struct irs_pr
*this) {
119 struct pvt
*pvt
= (struct pvt
*)this->private;
123 free_proto(&pvt
->proto
);
125 memput(pvt
, sizeof *pvt
);
126 memput(this, sizeof *this);
132 * struct protoent * pr_byname(struct irs_pr *this, const char *name)
136 static struct protoent
*
137 pr_byname(struct irs_pr
*this, const char *name
) {
138 struct pvt
*pvt
= (struct pvt
*)this->private;
139 struct protoent
*pr
= &pvt
->proto
;
146 if (pr
->p_name
!= NULL
&& strcmp(name
, pr
->p_name
) == 0) {
150 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
154 i
= irs_irp_send_command(pvt
->girpdata
, "getprotobyname %s", name
);
158 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
160 &body
, &bodylen
) != 0) {
164 if (code
== IRPD_GETPROTO_OK
) {
166 if (irp_unmarshall_pr(pr
, body
) != 0) {
174 memput(body
, bodylen
);
183 * struct protoent * pr_bynumber(struct irs_pr *this, int proto)
187 static struct protoent
*
188 pr_bynumber(struct irs_pr
*this, int proto
) {
189 struct pvt
*pvt
= (struct pvt
*)this->private;
190 struct protoent
*pr
= &pvt
->proto
;
197 if (pr
->p_name
!= NULL
&& proto
== pr
->p_proto
) {
201 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
205 i
= irs_irp_send_command(pvt
->girpdata
, "getprotobynumber %d", proto
);
209 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
211 &body
, &bodylen
) != 0) {
215 if (code
== IRPD_GETPROTO_OK
) {
217 if (irp_unmarshall_pr(pr
, body
) != 0) {
225 memput(body
, bodylen
);
235 * void pr_rewind(struct irs_pr *this)
240 pr_rewind(struct irs_pr
*this) {
241 struct pvt
*pvt
= (struct pvt
*)this->private;
245 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
249 if (irs_irp_send_command(pvt
->girpdata
, "setprotoent") != 0) {
253 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
254 if (code
!= IRPD_GETPROTO_SETOK
) {
255 if (irp_log_errors
) {
256 syslog(LOG_WARNING
, "setprotoent failed: %s", text
);
267 * struct protoent * pr_next(struct irs_pr *this)
271 * Prepares the cache if necessary and returns the next item in it.
275 static struct protoent
*
276 pr_next(struct irs_pr
*this) {
277 struct pvt
*pvt
= (struct pvt
*)this->private;
278 struct protoent
*pr
= &pvt
->proto
;
284 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
288 if (irs_irp_send_command(pvt
->girpdata
, "getprotoent") != 0) {
292 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
294 &body
, &bodylen
) != 0) {
298 if (code
== IRPD_GETPROTO_OK
) {
300 if (irp_unmarshall_pr(pr
, body
) != 0) {
308 memput(body
, bodylen
);
318 * void pr_minimize(struct irs_pr *this)
323 pr_minimize(struct irs_pr
*this) {
324 struct pvt
*pvt
= (struct pvt
*)this->private;
326 irs_irp_disconnect(pvt
->girpdata
);
335 * static void free_proto(struct protoent *pw);
337 * Deallocate all the memory irp_unmarshall_pr allocated.
342 free_proto(struct protoent
*pr
) {
348 if (pr
->p_name
!= NULL
)
351 for (p
= pr
->p_aliases
; p
!= NULL
&& *p
!= NULL
; p
++)