2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (c) 1996,1998 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_nw.c,v 1.1.206.2 2006/03/10 00:17:21 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
28 #include "port_before.h"
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <arpa/nameser.h>
48 #include <isc/irpmarshall.h>
50 #include <isc/memcluster.h>
57 #include "port_after.h"
63 struct irp_p *girpdata;
70 static void nw_close(struct irs_nw *);
71 static struct nwent * nw_byname(struct irs_nw *, const char *, int);
72 static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
73 static struct nwent * nw_next(struct irs_nw *);
74 static void nw_rewind(struct irs_nw *);
75 static void nw_minimize(struct irs_nw *);
77 static void free_nw(struct nwent *nw);
85 * struct irs_nw * irs_irp_nw(struct irs_acc *this)
90 irs_irp_nw(struct irs_acc *this) {
94 if (!(pvt = memget(sizeof *pvt))) {
98 memset(pvt, 0, sizeof *pvt);
100 if (!(nw = memget(sizeof *nw))) {
101 memput(pvt, sizeof *pvt);
105 memset(nw, 0x0, sizeof *nw);
106 pvt->girpdata = this->private;
109 nw->close = nw_close;
110 nw->byname = nw_byname;
111 nw->byaddr = nw_byaddr;
113 nw->rewind = nw_rewind;
114 nw->minimize = nw_minimize;
123 * void nw_close(struct irs_nw *this)
128 nw_close(struct irs_nw *this) {
129 struct pvt *pvt = (struct pvt *)this->private;
135 memput(pvt, sizeof *pvt);
136 memput(this, sizeof *this);
143 * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
144 * int length, int type)
148 static struct nwent *
149 nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
150 struct pvt *pvt = (struct pvt *)this->private;
151 struct nwent *nw = &pvt->net;
155 char paddr[24]; /* bigenough for ip4 w/ cidr spec. */
158 if (inet_net_ntop(type, net, length, paddr, sizeof paddr) == NULL) {
162 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
166 if (irs_irp_send_command(pvt->girpdata, "getnetbyaddr %s %s",
167 paddr, ADDR_T_STR(type)) != 0)
170 if (irs_irp_get_full_response(pvt->girpdata, &code,
172 &body, &bodylen) != 0) {
176 if (code == IRPD_GETNET_OK) {
178 if (irp_unmarshall_nw(nw, body) != 0) {
186 memput(body, bodylen);
196 * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
200 static struct nwent *
201 nw_byname(struct irs_nw *this, const char *name, int type) {
202 struct pvt *pvt = (struct pvt *)this->private;
203 struct nwent *nw = &pvt->net;
209 if (nw->n_name != NULL &&
210 strcmp(name, nw->n_name) == 0 &&
211 nw->n_addrtype == type) {
215 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
219 if (irs_irp_send_command(pvt->girpdata, "getnetbyname %s", name) != 0)
222 if (irs_irp_get_full_response(pvt->girpdata, &code,
224 &body, &bodylen) != 0) {
228 if (code == IRPD_GETNET_OK) {
230 if (irp_unmarshall_nw(nw, body) != 0) {
238 memput(body, bodylen);
248 * void nw_rewind(struct irs_nw *this)
253 nw_rewind(struct irs_nw *this) {
254 struct pvt *pvt = (struct pvt *)this->private;
258 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
262 if (irs_irp_send_command(pvt->girpdata, "setnetent") != 0) {
266 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
267 if (code != IRPD_GETNET_SETOK) {
268 if (irp_log_errors) {
269 syslog(LOG_WARNING, "setnetent failed: %s", text);
282 * struct nwent * nw_next(struct irs_nw *this)
286 * Prepares the cache if necessary and returns the first, or
290 static struct nwent *
291 nw_next(struct irs_nw *this) {
292 struct pvt *pvt = (struct pvt *)this->private;
293 struct nwent *nw = &pvt->net;
299 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
303 if (irs_irp_send_command(pvt->girpdata, "getnetent") != 0) {
307 if (irs_irp_get_full_response(pvt->girpdata, &code,
309 &body, &bodylen) != 0) {
313 if (code == IRPD_GETNET_OK) {
315 if (irp_unmarshall_nw(nw, body) != 0) {
323 memput(body, bodylen);
333 * void nw_minimize(struct irs_nw *this)
338 nw_minimize(struct irs_nw *this) {
339 struct pvt *pvt = (struct pvt *)this->private;
341 irs_irp_disconnect(pvt->girpdata);
352 * static void free_passwd(struct passwd *pw);
354 * deallocate all the memory irp_unmarshall_pw allocated.
359 free_nw(struct nwent *nw) {
365 if (nw->n_name != NULL)
368 if (nw->n_aliases != NULL) {
369 for (p = nw->n_aliases ; *p != NULL ; p++) {
375 if (nw->n_addr != NULL)