No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / irp_pr.c
blobb1dd3d37be3638b44fbd6f6b5d89893d82d33daf
1 /* $NetBSD$ */
3 /*
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 */
24 /* extern */
26 #include "port_before.h"
28 #include <syslog.h>
29 #include <sys/types.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <netdb.h>
37 #include <syslog.h>
39 #include <irs.h>
40 #include <irp.h>
41 #include <isc/memcluster.h>
42 #include <isc/irpmarshall.h>
44 #include "irs_p.h"
45 #include "lcl_p.h"
46 #include "irp_p.h"
48 #include "port_after.h"
51 #define MAXALIASES 35
53 /* Types */
55 struct pvt {
56 struct irp_p *girpdata;
57 int warned;
58 struct protoent proto;
61 /* Forward */
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);
72 /* Public */
74 /*%
75 * struct irs_pr * irs_irp_pr(struct irs_acc *this)
79 struct irs_pr *
80 irs_irp_pr(struct irs_acc *this) {
81 struct irs_pr *pr;
82 struct pvt *pvt;
84 if (!(pr = memget(sizeof *pr))) {
85 errno = ENOMEM;
86 return (NULL);
88 memset(pr, 0x0, sizeof *pr);
90 if (!(pvt = memget(sizeof *pvt))) {
91 memput(pr, sizeof *pr);
92 errno = ENOMEM;
93 return (NULL);
95 memset(pvt, 0, sizeof *pvt);
96 pvt->girpdata = this->private;
98 pr->private = pvt;
99 pr->close = pr_close;
100 pr->byname = pr_byname;
101 pr->bynumber = pr_bynumber;
102 pr->next = pr_next;
103 pr->rewind = pr_rewind;
104 pr->minimize = pr_minimize;
105 return (pr);
108 /* Methods */
111 * void pr_close(struct irs_pr *this)
115 static void
116 pr_close(struct irs_pr *this) {
117 struct pvt *pvt = (struct pvt *)this->private;
119 pr_minimize(this);
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;
136 char *body = NULL;
137 size_t bodylen;
138 int code;
139 int i;
140 char text[256];
142 if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) {
143 return (pr);
146 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
147 return (NULL);
150 i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name);
151 if (i != 0)
152 return (NULL);
154 if (irs_irp_get_full_response(pvt->girpdata, &code,
155 text, sizeof text,
156 &body, &bodylen) != 0) {
157 return (NULL);
160 if (code == IRPD_GETPROTO_OK) {
161 free_proto(pr);
162 if (irp_unmarshall_pr(pr, body) != 0) {
163 pr = NULL;
165 } else {
166 pr = NULL;
169 if (body != NULL) {
170 memput(body, bodylen);
173 return (pr);
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;
185 char *body = NULL;
186 size_t bodylen;
187 int code;
188 int i;
189 char text[256];
191 if (pr->p_name != NULL && proto == pr->p_proto) {
192 return (pr);
195 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
196 return (NULL);
199 i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto);
200 if (i != 0)
201 return (NULL);
203 if (irs_irp_get_full_response(pvt->girpdata, &code,
204 text, sizeof text,
205 &body, &bodylen) != 0) {
206 return (NULL);
209 if (code == IRPD_GETPROTO_OK) {
210 free_proto(pr);
211 if (irp_unmarshall_pr(pr, body) != 0) {
212 pr = NULL;
214 } else {
215 pr = NULL;
218 if (body != NULL) {
219 memput(body, bodylen);
222 return (pr);
226 * void pr_rewind(struct irs_pr *this)
230 static void
231 pr_rewind(struct irs_pr *this) {
232 struct pvt *pvt = (struct pvt *)this->private;
233 char text[256];
234 int code;
236 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
237 return;
240 if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) {
241 return;
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);
251 return;
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;
263 char *body;
264 size_t bodylen;
265 int code;
266 char text[256];
268 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
269 return (NULL);
272 if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) {
273 return (NULL);
276 if (irs_irp_get_full_response(pvt->girpdata, &code,
277 text, sizeof text,
278 &body, &bodylen) != 0) {
279 return (NULL);
282 if (code == IRPD_GETPROTO_OK) {
283 free_proto(pr);
284 if (irp_unmarshall_pr(pr, body) != 0) {
285 pr = NULL;
287 } else {
288 pr = NULL;
291 if (body != NULL) {
292 memput(body, bodylen);
295 return (pr);
299 * void pr_minimize(struct irs_pr *this)
303 static void
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.
315 static void
316 free_proto(struct protoent *pr) {
317 char **p;
319 if (pr == NULL)
320 return;
322 if (pr->p_name != NULL)
323 free(pr->p_name);
325 for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++)
326 free(*p);
329 /*! \file */