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.
20 #include "port_before.h"
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <arpa/nameser.h>
40 #include <isc/irpmarshall.h>
42 #include <isc/memcluster.h>
49 #include "port_after.h"
55 struct irp_p
*girpdata
;
62 static void nw_close(struct irs_nw
*);
63 static struct nwent
* nw_byname(struct irs_nw
*, const char *, int);
64 static struct nwent
* nw_byaddr(struct irs_nw
*, void *, int, int);
65 static struct nwent
* nw_next(struct irs_nw
*);
66 static void nw_rewind(struct irs_nw
*);
67 static void nw_minimize(struct irs_nw
*);
69 static void free_nw(struct nwent
*nw
);
75 * struct irs_nw * irs_irp_nw(struct irs_acc *this)
80 irs_irp_nw(struct irs_acc
*this) {
84 if (!(pvt
= memget(sizeof *pvt
))) {
88 memset(pvt
, 0, sizeof *pvt
);
90 if (!(nw
= memget(sizeof *nw
))) {
91 memput(pvt
, sizeof *pvt
);
95 memset(nw
, 0x0, sizeof *nw
);
96 pvt
->girpdata
= this->private;
100 nw
->byname
= nw_byname
;
101 nw
->byaddr
= nw_byaddr
;
103 nw
->rewind
= nw_rewind
;
104 nw
->minimize
= nw_minimize
;
111 * void nw_close(struct irs_nw *this)
116 nw_close(struct irs_nw
*this) {
117 struct pvt
*pvt
= (struct pvt
*)this->private;
123 memput(pvt
, sizeof *pvt
);
124 memput(this, sizeof *this);
128 * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
129 * int length, int type)
133 static struct nwent
*
134 nw_byaddr(struct irs_nw
*this, void *net
, int length
, int type
) {
135 struct pvt
*pvt
= (struct pvt
*)this->private;
136 struct nwent
*nw
= &pvt
->net
;
140 char paddr
[24]; /*%< bigenough for ip4 w/ cidr spec. */
143 if (inet_net_ntop(type
, net
, length
, paddr
, sizeof paddr
) == NULL
) {
147 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
151 if (irs_irp_send_command(pvt
->girpdata
, "getnetbyaddr %s %s",
152 paddr
, ADDR_T_STR(type
)) != 0)
155 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
157 &body
, &bodylen
) != 0) {
161 if (code
== IRPD_GETNET_OK
) {
163 if (irp_unmarshall_nw(nw
, body
) != 0) {
171 memput(body
, bodylen
);
178 * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
182 static struct nwent
*
183 nw_byname(struct irs_nw
*this, const char *name
, int type
) {
184 struct pvt
*pvt
= (struct pvt
*)this->private;
185 struct nwent
*nw
= &pvt
->net
;
191 if (nw
->n_name
!= NULL
&&
192 strcmp(name
, nw
->n_name
) == 0 &&
193 nw
->n_addrtype
== type
) {
197 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
201 if (irs_irp_send_command(pvt
->girpdata
, "getnetbyname %s", name
) != 0)
204 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
206 &body
, &bodylen
) != 0) {
210 if (code
== IRPD_GETNET_OK
) {
212 if (irp_unmarshall_nw(nw
, body
) != 0) {
220 memput(body
, bodylen
);
227 * void nw_rewind(struct irs_nw *this)
232 nw_rewind(struct irs_nw
*this) {
233 struct pvt
*pvt
= (struct pvt
*)this->private;
237 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
241 if (irs_irp_send_command(pvt
->girpdata
, "setnetent") != 0) {
245 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
246 if (code
!= IRPD_GETNET_SETOK
) {
247 if (irp_log_errors
) {
248 syslog(LOG_WARNING
, "setnetent failed: %s", text
);
256 * Prepares the cache if necessary and returns the first, or
260 static struct nwent
*
261 nw_next(struct irs_nw
*this) {
262 struct pvt
*pvt
= (struct pvt
*)this->private;
263 struct nwent
*nw
= &pvt
->net
;
269 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
273 if (irs_irp_send_command(pvt
->girpdata
, "getnetent") != 0) {
277 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
279 &body
, &bodylen
) != 0) {
283 if (code
== IRPD_GETNET_OK
) {
285 if (irp_unmarshall_nw(nw
, body
) != 0) {
293 memput(body
, bodylen
);
298 * void nw_minimize(struct irs_nw *this)
303 nw_minimize(struct irs_nw
*this) {
304 struct pvt
*pvt
= (struct pvt
*)this->private;
306 irs_irp_disconnect(pvt
->girpdata
);
315 * deallocate all the memory irp_unmarshall_pw allocated.
320 free_nw(struct nwent
*nw
) {
328 if (nw
->n_aliases
!= NULL
) {
329 for (p
= nw
->n_aliases
; *p
!= NULL
; p
++) {