4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (c) 1996,1998 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_nw.c,v 1.4 2006/03/09 23:57:56 marka Exp";
22 #endif /* LIBC_SCCS and not lint */
30 #include "port_before.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
50 #include <isc/irpmarshall.h>
52 #include <isc/memcluster.h>
59 #include "port_after.h"
65 struct irp_p
*girpdata
;
72 static void nw_close(struct irs_nw
*);
73 static struct nwent
* nw_byname(struct irs_nw
*, const char *, int);
74 static struct nwent
* nw_byaddr(struct irs_nw
*, void *, int, int);
75 static struct nwent
* nw_next(struct irs_nw
*);
76 static void nw_rewind(struct irs_nw
*);
77 static void nw_minimize(struct irs_nw
*);
79 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
;
121 * void nw_close(struct irs_nw *this)
126 nw_close(struct irs_nw
*this) {
127 struct pvt
*pvt
= (struct pvt
*)this->private;
133 memput(pvt
, sizeof *pvt
);
134 memput(this, sizeof *this);
138 * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
139 * int length, int type)
143 static struct nwent
*
144 nw_byaddr(struct irs_nw
*this, void *net
, int length
, int type
) {
145 struct pvt
*pvt
= (struct pvt
*)this->private;
146 struct nwent
*nw
= &pvt
->net
;
150 char paddr
[24]; /*%< bigenough for ip4 w/ cidr spec. */
153 if (inet_net_ntop(type
, net
, length
, paddr
, sizeof paddr
) == NULL
) {
157 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
161 if (irs_irp_send_command(pvt
->girpdata
, "getnetbyaddr %s %s",
162 paddr
, ADDR_T_STR(type
)) != 0)
165 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
167 &body
, &bodylen
) != 0) {
171 if (code
== IRPD_GETNET_OK
) {
173 if (irp_unmarshall_nw(nw
, body
) != 0) {
181 memput(body
, bodylen
);
188 * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
192 static struct nwent
*
193 nw_byname(struct irs_nw
*this, const char *name
, int type
) {
194 struct pvt
*pvt
= (struct pvt
*)this->private;
195 struct nwent
*nw
= &pvt
->net
;
201 if (nw
->n_name
!= NULL
&&
202 strcmp(name
, nw
->n_name
) == 0 &&
203 nw
->n_addrtype
== type
) {
207 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
211 if (irs_irp_send_command(pvt
->girpdata
, "getnetbyname %s", name
) != 0)
214 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
216 &body
, &bodylen
) != 0) {
220 if (code
== IRPD_GETNET_OK
) {
222 if (irp_unmarshall_nw(nw
, body
) != 0) {
230 memput(body
, bodylen
);
237 * void nw_rewind(struct irs_nw *this)
242 nw_rewind(struct irs_nw
*this) {
243 struct pvt
*pvt
= (struct pvt
*)this->private;
247 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
251 if (irs_irp_send_command(pvt
->girpdata
, "setnetent") != 0) {
255 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
256 if (code
!= IRPD_GETNET_SETOK
) {
257 if (irp_log_errors
) {
258 syslog(LOG_WARNING
, "setnetent failed: %s", text
);
266 * Prepares the cache if necessary and returns the first, or
270 static struct nwent
*
271 nw_next(struct irs_nw
*this) {
272 struct pvt
*pvt
= (struct pvt
*)this->private;
273 struct nwent
*nw
= &pvt
->net
;
279 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
283 if (irs_irp_send_command(pvt
->girpdata
, "getnetent") != 0) {
287 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
289 &body
, &bodylen
) != 0) {
293 if (code
== IRPD_GETNET_OK
) {
295 if (irp_unmarshall_nw(nw
, body
) != 0) {
303 memput(body
, bodylen
);
308 * void nw_minimize(struct irs_nw *this)
313 nw_minimize(struct irs_nw
*this) {
314 struct pvt
*pvt
= (struct pvt
*)this->private;
316 irs_irp_disconnect(pvt
->girpdata
);
325 * deallocate all the memory irp_unmarshall_pw allocated.
330 free_nw(struct nwent
*nw
) {
336 if (nw
->n_name
!= NULL
)
339 if (nw
->n_aliases
!= NULL
) {
340 for (p
= nw
->n_aliases
; *p
!= NULL
; p
++) {
346 if (nw
->n_addr
!= NULL
)