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.3 2005/04/27 04:56:29 sra 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);
73 * struct irs_pr * irs_irp_pr(struct irs_acc *this)
78 irs_irp_pr(struct irs_acc *this) {
82 if (!(pr = memget(sizeof *pr))) {
86 memset(pr, 0x0, sizeof *pr);
88 if (!(pvt = memget(sizeof *pvt))) {
89 memput(pr, sizeof *pr);
93 memset(pvt, 0, sizeof *pvt);
94 pvt->girpdata = this->private;
98 pr->byname = pr_byname;
99 pr->bynumber = pr_bynumber;
101 pr->rewind = pr_rewind;
102 pr->minimize = pr_minimize;
109 * void pr_close(struct irs_pr *this)
114 pr_close(struct irs_pr *this) {
115 struct pvt *pvt = (struct pvt *)this->private;
119 free_proto(&pvt->proto);
121 memput(pvt, sizeof *pvt);
122 memput(this, sizeof *this);
126 * struct protoent * pr_byname(struct irs_pr *this, const char *name)
130 static struct protoent *
131 pr_byname(struct irs_pr *this, const char *name) {
132 struct pvt *pvt = (struct pvt *)this->private;
133 struct protoent *pr = &pvt->proto;
140 if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) {
144 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
148 i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name);
152 if (irs_irp_get_full_response(pvt->girpdata, &code,
154 &body, &bodylen) != 0) {
158 if (code == IRPD_GETPROTO_OK) {
160 if (irp_unmarshall_pr(pr, body) != 0) {
168 memput(body, bodylen);
175 * struct protoent * pr_bynumber(struct irs_pr *this, int proto)
179 static struct protoent *
180 pr_bynumber(struct irs_pr *this, int proto) {
181 struct pvt *pvt = (struct pvt *)this->private;
182 struct protoent *pr = &pvt->proto;
189 if (pr->p_name != NULL && proto == pr->p_proto) {
193 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
197 i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto);
201 if (irs_irp_get_full_response(pvt->girpdata, &code,
203 &body, &bodylen) != 0) {
207 if (code == IRPD_GETPROTO_OK) {
209 if (irp_unmarshall_pr(pr, body) != 0) {
217 memput(body, bodylen);
224 * void pr_rewind(struct irs_pr *this)
229 pr_rewind(struct irs_pr *this) {
230 struct pvt *pvt = (struct pvt *)this->private;
234 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
238 if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) {
242 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
243 if (code != IRPD_GETPROTO_SETOK) {
244 if (irp_log_errors) {
245 syslog(LOG_WARNING, "setprotoent failed: %s", text);
253 * Prepares the cache if necessary and returns the next item in it.
257 static struct protoent *
258 pr_next(struct irs_pr *this) {
259 struct pvt *pvt = (struct pvt *)this->private;
260 struct protoent *pr = &pvt->proto;
266 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
270 if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) {
274 if (irs_irp_get_full_response(pvt->girpdata, &code,
276 &body, &bodylen) != 0) {
280 if (code == IRPD_GETPROTO_OK) {
282 if (irp_unmarshall_pr(pr, body) != 0) {
290 memput(body, bodylen);
297 * void pr_minimize(struct irs_pr *this)
302 pr_minimize(struct irs_pr *this) {
303 struct pvt *pvt = (struct pvt *)this->private;
305 irs_irp_disconnect(pvt->girpdata);
309 * Deallocate all the memory irp_unmarshall_pr allocated.
314 free_proto(struct protoent *pr) {
320 if (pr->p_name != NULL)
323 for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++)