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