irp_pr.c revision 1.1.1.1.4.2 1 1.1.1.1.4.2 riz /* $NetBSD: irp_pr.c,v 1.1.1.1.4.2 2011/01/06 21:42:17 riz Exp $ */
2 1.1.1.1.4.2 riz
3 1.1.1.1.4.2 riz /*
4 1.1.1.1.4.2 riz * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1.1.1.4.2 riz * Portions Copyright (c) 1996 by Internet Software Consortium.
6 1.1.1.1.4.2 riz *
7 1.1.1.1.4.2 riz * Permission to use, copy, modify, and distribute this software for any
8 1.1.1.1.4.2 riz * purpose with or without fee is hereby granted, provided that the above
9 1.1.1.1.4.2 riz * copyright notice and this permission notice appear in all copies.
10 1.1.1.1.4.2 riz *
11 1.1.1.1.4.2 riz * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1.1.1.4.2 riz * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.1.1.4.2 riz * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1.1.1.4.2 riz * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.1.1.4.2 riz * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.1.1.4.2 riz * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1.1.1.4.2 riz * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.1.1.4.2 riz */
19 1.1.1.1.4.2 riz
20 1.1.1.1.4.2 riz #if defined(LIBC_SCCS) && !defined(lint)
21 1.1.1.1.4.2 riz static const char rcsid[] = "Id: irp_pr.c,v 1.3 2005/04/27 04:56:29 sra Exp";
22 1.1.1.1.4.2 riz #endif /* LIBC_SCCS and not lint */
23 1.1.1.1.4.2 riz
24 1.1.1.1.4.2 riz /* extern */
25 1.1.1.1.4.2 riz
26 1.1.1.1.4.2 riz #include "port_before.h"
27 1.1.1.1.4.2 riz
28 1.1.1.1.4.2 riz #include <syslog.h>
29 1.1.1.1.4.2 riz #include <sys/types.h>
30 1.1.1.1.4.2 riz
31 1.1.1.1.4.2 riz #include <errno.h>
32 1.1.1.1.4.2 riz #include <fcntl.h>
33 1.1.1.1.4.2 riz #include <string.h>
34 1.1.1.1.4.2 riz #include <stdio.h>
35 1.1.1.1.4.2 riz #include <stdlib.h>
36 1.1.1.1.4.2 riz #include <netdb.h>
37 1.1.1.1.4.2 riz #include <syslog.h>
38 1.1.1.1.4.2 riz
39 1.1.1.1.4.2 riz #include <irs.h>
40 1.1.1.1.4.2 riz #include <irp.h>
41 1.1.1.1.4.2 riz #include <isc/memcluster.h>
42 1.1.1.1.4.2 riz #include <isc/irpmarshall.h>
43 1.1.1.1.4.2 riz
44 1.1.1.1.4.2 riz #include "irs_p.h"
45 1.1.1.1.4.2 riz #include "lcl_p.h"
46 1.1.1.1.4.2 riz #include "irp_p.h"
47 1.1.1.1.4.2 riz
48 1.1.1.1.4.2 riz #include "port_after.h"
49 1.1.1.1.4.2 riz
50 1.1.1.1.4.2 riz
51 1.1.1.1.4.2 riz #define MAXALIASES 35
52 1.1.1.1.4.2 riz
53 1.1.1.1.4.2 riz /* Types */
54 1.1.1.1.4.2 riz
55 1.1.1.1.4.2 riz struct pvt {
56 1.1.1.1.4.2 riz struct irp_p *girpdata;
57 1.1.1.1.4.2 riz int warned;
58 1.1.1.1.4.2 riz struct protoent proto;
59 1.1.1.1.4.2 riz };
60 1.1.1.1.4.2 riz
61 1.1.1.1.4.2 riz /* Forward */
62 1.1.1.1.4.2 riz
63 1.1.1.1.4.2 riz static void pr_close(struct irs_pr *);
64 1.1.1.1.4.2 riz static struct protoent * pr_next(struct irs_pr *);
65 1.1.1.1.4.2 riz static struct protoent * pr_byname(struct irs_pr *, const char *);
66 1.1.1.1.4.2 riz static struct protoent * pr_bynumber(struct irs_pr *, int);
67 1.1.1.1.4.2 riz static void pr_rewind(struct irs_pr *);
68 1.1.1.1.4.2 riz static void pr_minimize(struct irs_pr *);
69 1.1.1.1.4.2 riz
70 1.1.1.1.4.2 riz static void free_proto(struct protoent *pr);
71 1.1.1.1.4.2 riz
72 1.1.1.1.4.2 riz /* Public */
73 1.1.1.1.4.2 riz
74 1.1.1.1.4.2 riz /*%
75 1.1.1.1.4.2 riz * struct irs_pr * irs_irp_pr(struct irs_acc *this)
76 1.1.1.1.4.2 riz *
77 1.1.1.1.4.2 riz */
78 1.1.1.1.4.2 riz
79 1.1.1.1.4.2 riz struct irs_pr *
80 1.1.1.1.4.2 riz irs_irp_pr(struct irs_acc *this) {
81 1.1.1.1.4.2 riz struct irs_pr *pr;
82 1.1.1.1.4.2 riz struct pvt *pvt;
83 1.1.1.1.4.2 riz
84 1.1.1.1.4.2 riz if (!(pr = memget(sizeof *pr))) {
85 1.1.1.1.4.2 riz errno = ENOMEM;
86 1.1.1.1.4.2 riz return (NULL);
87 1.1.1.1.4.2 riz }
88 1.1.1.1.4.2 riz memset(pr, 0x0, sizeof *pr);
89 1.1.1.1.4.2 riz
90 1.1.1.1.4.2 riz if (!(pvt = memget(sizeof *pvt))) {
91 1.1.1.1.4.2 riz memput(pr, sizeof *pr);
92 1.1.1.1.4.2 riz errno = ENOMEM;
93 1.1.1.1.4.2 riz return (NULL);
94 1.1.1.1.4.2 riz }
95 1.1.1.1.4.2 riz memset(pvt, 0, sizeof *pvt);
96 1.1.1.1.4.2 riz pvt->girpdata = this->private;
97 1.1.1.1.4.2 riz
98 1.1.1.1.4.2 riz pr->private = pvt;
99 1.1.1.1.4.2 riz pr->close = pr_close;
100 1.1.1.1.4.2 riz pr->byname = pr_byname;
101 1.1.1.1.4.2 riz pr->bynumber = pr_bynumber;
102 1.1.1.1.4.2 riz pr->next = pr_next;
103 1.1.1.1.4.2 riz pr->rewind = pr_rewind;
104 1.1.1.1.4.2 riz pr->minimize = pr_minimize;
105 1.1.1.1.4.2 riz return (pr);
106 1.1.1.1.4.2 riz }
107 1.1.1.1.4.2 riz
108 1.1.1.1.4.2 riz /* Methods */
109 1.1.1.1.4.2 riz
110 1.1.1.1.4.2 riz /*%
111 1.1.1.1.4.2 riz * void pr_close(struct irs_pr *this)
112 1.1.1.1.4.2 riz *
113 1.1.1.1.4.2 riz */
114 1.1.1.1.4.2 riz
115 1.1.1.1.4.2 riz static void
116 1.1.1.1.4.2 riz pr_close(struct irs_pr *this) {
117 1.1.1.1.4.2 riz struct pvt *pvt = (struct pvt *)this->private;
118 1.1.1.1.4.2 riz
119 1.1.1.1.4.2 riz pr_minimize(this);
120 1.1.1.1.4.2 riz
121 1.1.1.1.4.2 riz free_proto(&pvt->proto);
122 1.1.1.1.4.2 riz
123 1.1.1.1.4.2 riz memput(pvt, sizeof *pvt);
124 1.1.1.1.4.2 riz memput(this, sizeof *this);
125 1.1.1.1.4.2 riz }
126 1.1.1.1.4.2 riz
127 1.1.1.1.4.2 riz /*%
128 1.1.1.1.4.2 riz * struct protoent * pr_byname(struct irs_pr *this, const char *name)
129 1.1.1.1.4.2 riz *
130 1.1.1.1.4.2 riz */
131 1.1.1.1.4.2 riz
132 1.1.1.1.4.2 riz static struct protoent *
133 1.1.1.1.4.2 riz pr_byname(struct irs_pr *this, const char *name) {
134 1.1.1.1.4.2 riz struct pvt *pvt = (struct pvt *)this->private;
135 1.1.1.1.4.2 riz struct protoent *pr = &pvt->proto;
136 1.1.1.1.4.2 riz char *body = NULL;
137 1.1.1.1.4.2 riz size_t bodylen;
138 1.1.1.1.4.2 riz int code;
139 1.1.1.1.4.2 riz int i;
140 1.1.1.1.4.2 riz char text[256];
141 1.1.1.1.4.2 riz
142 1.1.1.1.4.2 riz if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) {
143 1.1.1.1.4.2 riz return (pr);
144 1.1.1.1.4.2 riz }
145 1.1.1.1.4.2 riz
146 1.1.1.1.4.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
147 1.1.1.1.4.2 riz return (NULL);
148 1.1.1.1.4.2 riz }
149 1.1.1.1.4.2 riz
150 1.1.1.1.4.2 riz i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name);
151 1.1.1.1.4.2 riz if (i != 0)
152 1.1.1.1.4.2 riz return (NULL);
153 1.1.1.1.4.2 riz
154 1.1.1.1.4.2 riz if (irs_irp_get_full_response(pvt->girpdata, &code,
155 1.1.1.1.4.2 riz text, sizeof text,
156 1.1.1.1.4.2 riz &body, &bodylen) != 0) {
157 1.1.1.1.4.2 riz return (NULL);
158 1.1.1.1.4.2 riz }
159 1.1.1.1.4.2 riz
160 1.1.1.1.4.2 riz if (code == IRPD_GETPROTO_OK) {
161 1.1.1.1.4.2 riz free_proto(pr);
162 1.1.1.1.4.2 riz if (irp_unmarshall_pr(pr, body) != 0) {
163 1.1.1.1.4.2 riz pr = NULL;
164 1.1.1.1.4.2 riz }
165 1.1.1.1.4.2 riz } else {
166 1.1.1.1.4.2 riz pr = NULL;
167 1.1.1.1.4.2 riz }
168 1.1.1.1.4.2 riz
169 1.1.1.1.4.2 riz if (body != NULL) {
170 1.1.1.1.4.2 riz memput(body, bodylen);
171 1.1.1.1.4.2 riz }
172 1.1.1.1.4.2 riz
173 1.1.1.1.4.2 riz return (pr);
174 1.1.1.1.4.2 riz }
175 1.1.1.1.4.2 riz
176 1.1.1.1.4.2 riz /*%
177 1.1.1.1.4.2 riz * struct protoent * pr_bynumber(struct irs_pr *this, int proto)
178 1.1.1.1.4.2 riz *
179 1.1.1.1.4.2 riz */
180 1.1.1.1.4.2 riz
181 1.1.1.1.4.2 riz static struct protoent *
182 1.1.1.1.4.2 riz pr_bynumber(struct irs_pr *this, int proto) {
183 1.1.1.1.4.2 riz struct pvt *pvt = (struct pvt *)this->private;
184 1.1.1.1.4.2 riz struct protoent *pr = &pvt->proto;
185 1.1.1.1.4.2 riz char *body = NULL;
186 1.1.1.1.4.2 riz size_t bodylen;
187 1.1.1.1.4.2 riz int code;
188 1.1.1.1.4.2 riz int i;
189 1.1.1.1.4.2 riz char text[256];
190 1.1.1.1.4.2 riz
191 1.1.1.1.4.2 riz if (pr->p_name != NULL && proto == pr->p_proto) {
192 1.1.1.1.4.2 riz return (pr);
193 1.1.1.1.4.2 riz }
194 1.1.1.1.4.2 riz
195 1.1.1.1.4.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
196 1.1.1.1.4.2 riz return (NULL);
197 1.1.1.1.4.2 riz }
198 1.1.1.1.4.2 riz
199 1.1.1.1.4.2 riz i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto);
200 1.1.1.1.4.2 riz if (i != 0)
201 1.1.1.1.4.2 riz return (NULL);
202 1.1.1.1.4.2 riz
203 1.1.1.1.4.2 riz if (irs_irp_get_full_response(pvt->girpdata, &code,
204 1.1.1.1.4.2 riz text, sizeof text,
205 1.1.1.1.4.2 riz &body, &bodylen) != 0) {
206 1.1.1.1.4.2 riz return (NULL);
207 1.1.1.1.4.2 riz }
208 1.1.1.1.4.2 riz
209 1.1.1.1.4.2 riz if (code == IRPD_GETPROTO_OK) {
210 1.1.1.1.4.2 riz free_proto(pr);
211 1.1.1.1.4.2 riz if (irp_unmarshall_pr(pr, body) != 0) {
212 1.1.1.1.4.2 riz pr = NULL;
213 1.1.1.1.4.2 riz }
214 1.1.1.1.4.2 riz } else {
215 1.1.1.1.4.2 riz pr = NULL;
216 1.1.1.1.4.2 riz }
217 1.1.1.1.4.2 riz
218 1.1.1.1.4.2 riz if (body != NULL) {
219 1.1.1.1.4.2 riz memput(body, bodylen);
220 1.1.1.1.4.2 riz }
221 1.1.1.1.4.2 riz
222 1.1.1.1.4.2 riz return (pr);
223 1.1.1.1.4.2 riz }
224 1.1.1.1.4.2 riz
225 1.1.1.1.4.2 riz /*%
226 1.1.1.1.4.2 riz * void pr_rewind(struct irs_pr *this)
227 1.1.1.1.4.2 riz *
228 1.1.1.1.4.2 riz */
229 1.1.1.1.4.2 riz
230 1.1.1.1.4.2 riz static void
231 1.1.1.1.4.2 riz pr_rewind(struct irs_pr *this) {
232 1.1.1.1.4.2 riz struct pvt *pvt = (struct pvt *)this->private;
233 1.1.1.1.4.2 riz char text[256];
234 1.1.1.1.4.2 riz int code;
235 1.1.1.1.4.2 riz
236 1.1.1.1.4.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
237 1.1.1.1.4.2 riz return;
238 1.1.1.1.4.2 riz }
239 1.1.1.1.4.2 riz
240 1.1.1.1.4.2 riz if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) {
241 1.1.1.1.4.2 riz return;
242 1.1.1.1.4.2 riz }
243 1.1.1.1.4.2 riz
244 1.1.1.1.4.2 riz code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
245 1.1.1.1.4.2 riz if (code != IRPD_GETPROTO_SETOK) {
246 1.1.1.1.4.2 riz if (irp_log_errors) {
247 1.1.1.1.4.2 riz syslog(LOG_WARNING, "setprotoent failed: %s", text);
248 1.1.1.1.4.2 riz }
249 1.1.1.1.4.2 riz }
250 1.1.1.1.4.2 riz
251 1.1.1.1.4.2 riz return;
252 1.1.1.1.4.2 riz }
253 1.1.1.1.4.2 riz
254 1.1.1.1.4.2 riz /*%
255 1.1.1.1.4.2 riz * Prepares the cache if necessary and returns the next item in it.
256 1.1.1.1.4.2 riz *
257 1.1.1.1.4.2 riz */
258 1.1.1.1.4.2 riz
259 1.1.1.1.4.2 riz static struct protoent *
260 1.1.1.1.4.2 riz pr_next(struct irs_pr *this) {
261 1.1.1.1.4.2 riz struct pvt *pvt = (struct pvt *)this->private;
262 1.1.1.1.4.2 riz struct protoent *pr = &pvt->proto;
263 1.1.1.1.4.2 riz char *body;
264 1.1.1.1.4.2 riz size_t bodylen;
265 1.1.1.1.4.2 riz int code;
266 1.1.1.1.4.2 riz char text[256];
267 1.1.1.1.4.2 riz
268 1.1.1.1.4.2 riz if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
269 1.1.1.1.4.2 riz return (NULL);
270 1.1.1.1.4.2 riz }
271 1.1.1.1.4.2 riz
272 1.1.1.1.4.2 riz if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) {
273 1.1.1.1.4.2 riz return (NULL);
274 1.1.1.1.4.2 riz }
275 1.1.1.1.4.2 riz
276 1.1.1.1.4.2 riz if (irs_irp_get_full_response(pvt->girpdata, &code,
277 1.1.1.1.4.2 riz text, sizeof text,
278 1.1.1.1.4.2 riz &body, &bodylen) != 0) {
279 1.1.1.1.4.2 riz return (NULL);
280 1.1.1.1.4.2 riz }
281 1.1.1.1.4.2 riz
282 1.1.1.1.4.2 riz if (code == IRPD_GETPROTO_OK) {
283 1.1.1.1.4.2 riz free_proto(pr);
284 1.1.1.1.4.2 riz if (irp_unmarshall_pr(pr, body) != 0) {
285 1.1.1.1.4.2 riz pr = NULL;
286 1.1.1.1.4.2 riz }
287 1.1.1.1.4.2 riz } else {
288 1.1.1.1.4.2 riz pr = NULL;
289 1.1.1.1.4.2 riz }
290 1.1.1.1.4.2 riz
291 1.1.1.1.4.2 riz if (body != NULL) {
292 1.1.1.1.4.2 riz memput(body, bodylen);
293 1.1.1.1.4.2 riz }
294 1.1.1.1.4.2 riz
295 1.1.1.1.4.2 riz return (pr);
296 1.1.1.1.4.2 riz }
297 1.1.1.1.4.2 riz
298 1.1.1.1.4.2 riz /*%
299 1.1.1.1.4.2 riz * void pr_minimize(struct irs_pr *this)
300 1.1.1.1.4.2 riz *
301 1.1.1.1.4.2 riz */
302 1.1.1.1.4.2 riz
303 1.1.1.1.4.2 riz static void
304 1.1.1.1.4.2 riz pr_minimize(struct irs_pr *this) {
305 1.1.1.1.4.2 riz struct pvt *pvt = (struct pvt *)this->private;
306 1.1.1.1.4.2 riz
307 1.1.1.1.4.2 riz irs_irp_disconnect(pvt->girpdata);
308 1.1.1.1.4.2 riz }
309 1.1.1.1.4.2 riz
310 1.1.1.1.4.2 riz /*%
311 1.1.1.1.4.2 riz * Deallocate all the memory irp_unmarshall_pr allocated.
312 1.1.1.1.4.2 riz *
313 1.1.1.1.4.2 riz */
314 1.1.1.1.4.2 riz
315 1.1.1.1.4.2 riz static void
316 1.1.1.1.4.2 riz free_proto(struct protoent *pr) {
317 1.1.1.1.4.2 riz char **p;
318 1.1.1.1.4.2 riz
319 1.1.1.1.4.2 riz if (pr == NULL)
320 1.1.1.1.4.2 riz return;
321 1.1.1.1.4.2 riz
322 1.1.1.1.4.2 riz if (pr->p_name != NULL)
323 1.1.1.1.4.2 riz free(pr->p_name);
324 1.1.1.1.4.2 riz
325 1.1.1.1.4.2 riz for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++)
326 1.1.1.1.4.2 riz free(*p);
327 1.1.1.1.4.2 riz }
328 1.1.1.1.4.2 riz
329 1.1.1.1.4.2 riz /*! \file */
330