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.2.18.2 2006/03/10 00:20:08 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);
83 * struct irs_nw * irs_irp_nw(struct irs_acc *this)
88 irs_irp_nw(struct irs_acc *this) {
92 if (!(pvt = memget(sizeof *pvt))) {
96 memset(pvt, 0, sizeof *pvt);
98 if (!(nw = memget(sizeof *nw))) {
99 memput(pvt, sizeof *pvt);
103 memset(nw, 0x0, sizeof *nw);
104 pvt->girpdata = this->private;
107 nw->close = nw_close;
108 nw->byname = nw_byname;
109 nw->byaddr = nw_byaddr;
111 nw->rewind = nw_rewind;
112 nw->minimize = nw_minimize;
119 * void nw_close(struct irs_nw *this)
124 nw_close(struct irs_nw *this) {
125 struct pvt *pvt = (struct pvt *)this->private;
131 memput(pvt, sizeof *pvt);
132 memput(this, sizeof *this);
136 * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
137 * int length, int type)
141 static struct nwent *
142 nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
143 struct pvt *pvt = (struct pvt *)this->private;
144 struct nwent *nw = &pvt->net;
148 char paddr[24]; /*%< bigenough for ip4 w/ cidr spec. */
151 if (inet_net_ntop(type, net, length, paddr, sizeof paddr) == NULL) {
155 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
159 if (irs_irp_send_command(pvt->girpdata, "getnetbyaddr %s %s",
160 paddr, ADDR_T_STR(type)) != 0)
163 if (irs_irp_get_full_response(pvt->girpdata, &code,
165 &body, &bodylen) != 0) {
169 if (code == IRPD_GETNET_OK) {
171 if (irp_unmarshall_nw(nw, body) != 0) {
179 memput(body, bodylen);
186 * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
190 static struct nwent *
191 nw_byname(struct irs_nw *this, const char *name, int type) {
192 struct pvt *pvt = (struct pvt *)this->private;
193 struct nwent *nw = &pvt->net;
199 if (nw->n_name != NULL &&
200 strcmp(name, nw->n_name) == 0 &&
201 nw->n_addrtype == type) {
205 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
209 if (irs_irp_send_command(pvt->girpdata, "getnetbyname %s", name) != 0)
212 if (irs_irp_get_full_response(pvt->girpdata, &code,
214 &body, &bodylen) != 0) {
218 if (code == IRPD_GETNET_OK) {
220 if (irp_unmarshall_nw(nw, body) != 0) {
228 memput(body, bodylen);
235 * void nw_rewind(struct irs_nw *this)
240 nw_rewind(struct irs_nw *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, "setnetent") != 0) {
253 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
254 if (code != IRPD_GETNET_SETOK) {
255 if (irp_log_errors) {
256 syslog(LOG_WARNING, "setnetent failed: %s", text);
264 * Prepares the cache if necessary and returns the first, or
268 static struct nwent *
269 nw_next(struct irs_nw *this) {
270 struct pvt *pvt = (struct pvt *)this->private;
271 struct nwent *nw = &pvt->net;
277 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
281 if (irs_irp_send_command(pvt->girpdata, "getnetent") != 0) {
285 if (irs_irp_get_full_response(pvt->girpdata, &code,
287 &body, &bodylen) != 0) {
291 if (code == IRPD_GETNET_OK) {
293 if (irp_unmarshall_nw(nw, body) != 0) {
301 memput(body, bodylen);
306 * void nw_minimize(struct irs_nw *this)
311 nw_minimize(struct irs_nw *this) {
312 struct pvt *pvt = (struct pvt *)this->private;
314 irs_irp_disconnect(pvt->girpdata);
323 * deallocate all the memory irp_unmarshall_pw allocated.
328 free_nw(struct nwent *nw) {
334 if (nw->n_name != NULL)
337 if (nw->n_aliases != NULL) {
338 for (p = nw->n_aliases ; *p != NULL ; p++) {
344 if (nw->n_addr != NULL)