xref: /netbsd-src/external/bsd/libbind/dist/irs/dns_pr.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: dns_pr.c,v 1.1.1.2 2012/09/09 16:07:51 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (c) 1996,1999 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: dns_pr.c,v 1.5 2005/04/27 04:56:22 sra Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos /* Imports */
25b5677b36Schristos 
26b5677b36Schristos #include "port_before.h"
27b5677b36Schristos 
28b5677b36Schristos #include <sys/types.h>
29b5677b36Schristos #include <netinet/in.h>
30b5677b36Schristos #include <arpa/nameser.h>
31b5677b36Schristos #include <resolv.h>
32b5677b36Schristos 
33b5677b36Schristos #include <stdio.h>
34b5677b36Schristos #include <string.h>
35b5677b36Schristos #include <netdb.h>
36b5677b36Schristos #include <ctype.h>
37b5677b36Schristos #include <stdlib.h>
38b5677b36Schristos #include <errno.h>
39b5677b36Schristos 
40b5677b36Schristos #include <isc/memcluster.h>
41b5677b36Schristos #include <irs.h>
42b5677b36Schristos 
43b5677b36Schristos #include "port_after.h"
44b5677b36Schristos 
45b5677b36Schristos #include "irs_p.h"
46b5677b36Schristos #include "hesiod.h"
47b5677b36Schristos #include "dns_p.h"
48b5677b36Schristos 
49b5677b36Schristos /* Types. */
50b5677b36Schristos 
51b5677b36Schristos struct pvt {
52b5677b36Schristos 	struct dns_p *		dns;
53b5677b36Schristos 	struct protoent		proto;
54b5677b36Schristos 	char *			prbuf;
55b5677b36Schristos };
56b5677b36Schristos 
57b5677b36Schristos /* Forward. */
58b5677b36Schristos 
59b5677b36Schristos static void 			pr_close(struct irs_pr *);
60b5677b36Schristos static struct protoent *	pr_byname(struct irs_pr *, const char *);
61b5677b36Schristos static struct protoent *	pr_bynumber(struct irs_pr *, int);
62b5677b36Schristos static struct protoent *	pr_next(struct irs_pr *);
63b5677b36Schristos static void			pr_rewind(struct irs_pr *);
64b5677b36Schristos static void			pr_minimize(struct irs_pr *);
65b5677b36Schristos static struct __res_state *	pr_res_get(struct irs_pr *);
66b5677b36Schristos static void			pr_res_set(struct irs_pr *,
67b5677b36Schristos 					   struct __res_state *,
68b5677b36Schristos 					   void (*)(void *));
69b5677b36Schristos 
70b5677b36Schristos static struct protoent *	parse_hes_list(struct irs_pr *, char **);
71b5677b36Schristos 
72b5677b36Schristos /* Public. */
73b5677b36Schristos 
74b5677b36Schristos struct irs_pr *
irs_dns_pr(struct irs_acc * this)75b5677b36Schristos irs_dns_pr(struct irs_acc *this) {
76b5677b36Schristos 	struct dns_p *dns = (struct dns_p *)this->private;
77b5677b36Schristos 	struct pvt *pvt;
78b5677b36Schristos 	struct irs_pr *pr;
79b5677b36Schristos 
80b5677b36Schristos 	if (!dns->hes_ctx) {
81b5677b36Schristos 		errno = ENODEV;
82b5677b36Schristos 		return (NULL);
83b5677b36Schristos 	}
84b5677b36Schristos 	if (!(pvt = memget(sizeof *pvt))) {
85b5677b36Schristos 		errno = ENOMEM;
86b5677b36Schristos 		return (NULL);
87b5677b36Schristos 	}
88b5677b36Schristos 	memset(pvt, 0, sizeof *pvt);
89b5677b36Schristos 	if (!(pr = memget(sizeof *pr))) {
90b5677b36Schristos 		memput(pvt, sizeof *pvt);
91b5677b36Schristos 		errno = ENOMEM;
92b5677b36Schristos 		return (NULL);
93b5677b36Schristos 	}
94b5677b36Schristos 	memset(pr, 0x5e, sizeof *pr);
95b5677b36Schristos 	pvt->dns = dns;
96b5677b36Schristos 	pr->private = pvt;
97b5677b36Schristos 	pr->byname = pr_byname;
98b5677b36Schristos 	pr->bynumber = pr_bynumber;
99b5677b36Schristos 	pr->next = pr_next;
100b5677b36Schristos 	pr->rewind = pr_rewind;
101b5677b36Schristos 	pr->close = pr_close;
102b5677b36Schristos 	pr->minimize = pr_minimize;
103b5677b36Schristos 	pr->res_get = pr_res_get;
104b5677b36Schristos 	pr->res_set = pr_res_set;
105b5677b36Schristos 	return (pr);
106b5677b36Schristos }
107b5677b36Schristos 
108b5677b36Schristos /* Methods. */
109b5677b36Schristos 
110b5677b36Schristos static void
pr_close(struct irs_pr * this)111b5677b36Schristos pr_close(struct irs_pr *this) {
112b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
113b5677b36Schristos 
114b5677b36Schristos 	if (pvt->proto.p_aliases)
115b5677b36Schristos 		free(pvt->proto.p_aliases);
116b5677b36Schristos 	if (pvt->prbuf)
117b5677b36Schristos 		free(pvt->prbuf);
118b5677b36Schristos 
119b5677b36Schristos 	memput(pvt, sizeof *pvt);
120b5677b36Schristos 	memput(this, sizeof *this);
121b5677b36Schristos }
122b5677b36Schristos 
123b5677b36Schristos static struct protoent *
pr_byname(struct irs_pr * this,const char * name)124b5677b36Schristos pr_byname(struct irs_pr *this, const char *name) {
125b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
126b5677b36Schristos 	struct dns_p *dns = pvt->dns;
127b5677b36Schristos 	struct protoent *proto;
128b5677b36Schristos 	char **hes_list;
129b5677b36Schristos 
130b5677b36Schristos 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "protocol")))
131b5677b36Schristos 		return (NULL);
132b5677b36Schristos 
133b5677b36Schristos 	proto = parse_hes_list(this, hes_list);
134b5677b36Schristos 	hesiod_free_list(dns->hes_ctx, hes_list);
135b5677b36Schristos 	return (proto);
136b5677b36Schristos }
137b5677b36Schristos 
138b5677b36Schristos static struct protoent *
pr_bynumber(struct irs_pr * this,int num)139b5677b36Schristos pr_bynumber(struct irs_pr *this, int num) {
140b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
141b5677b36Schristos 	struct dns_p *dns = pvt->dns;
142b5677b36Schristos 	struct protoent *proto;
143b5677b36Schristos 	char numstr[16];
144b5677b36Schristos 	char **hes_list;
145b5677b36Schristos 
146b5677b36Schristos 	sprintf(numstr, "%d", num);
147b5677b36Schristos 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, numstr, "protonum")))
148b5677b36Schristos 		return (NULL);
149b5677b36Schristos 
150b5677b36Schristos 	proto = parse_hes_list(this, hes_list);
151b5677b36Schristos 	hesiod_free_list(dns->hes_ctx, hes_list);
152b5677b36Schristos 	return (proto);
153b5677b36Schristos }
154b5677b36Schristos 
155b5677b36Schristos static struct protoent *
pr_next(struct irs_pr * this)156b5677b36Schristos pr_next(struct irs_pr *this) {
157b5677b36Schristos 	UNUSED(this);
158b5677b36Schristos 	errno = ENODEV;
159b5677b36Schristos 	return (NULL);
160b5677b36Schristos }
161b5677b36Schristos 
162b5677b36Schristos static void
pr_rewind(struct irs_pr * this)163b5677b36Schristos pr_rewind(struct irs_pr *this) {
164b5677b36Schristos 	UNUSED(this);
165b5677b36Schristos 	/* NOOP */
166b5677b36Schristos }
167b5677b36Schristos 
168b5677b36Schristos static void
pr_minimize(struct irs_pr * this)169b5677b36Schristos pr_minimize(struct irs_pr *this) {
170b5677b36Schristos 	UNUSED(this);
171b5677b36Schristos 	/* NOOP */
172b5677b36Schristos }
173b5677b36Schristos 
174b5677b36Schristos static struct __res_state *
pr_res_get(struct irs_pr * this)175b5677b36Schristos pr_res_get(struct irs_pr *this) {
176b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
177b5677b36Schristos 	struct dns_p *dns = pvt->dns;
178b5677b36Schristos 
179b5677b36Schristos 	return (__hesiod_res_get(dns->hes_ctx));
180b5677b36Schristos }
181b5677b36Schristos 
182b5677b36Schristos static void
pr_res_set(struct irs_pr * this,struct __res_state * res,void (* free_res)(void *))183b5677b36Schristos pr_res_set(struct irs_pr *this, struct __res_state * res,
184b5677b36Schristos 	   void (*free_res)(void *)) {
185b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
186b5677b36Schristos 	struct dns_p *dns = pvt->dns;
187b5677b36Schristos 
188b5677b36Schristos 	__hesiod_res_set(dns->hes_ctx, res, free_res);
189b5677b36Schristos }
190b5677b36Schristos 
191b5677b36Schristos /* Private. */
192b5677b36Schristos 
193b5677b36Schristos static struct protoent *
parse_hes_list(struct irs_pr * this,char ** hes_list)194b5677b36Schristos parse_hes_list(struct irs_pr *this, char **hes_list) {
195b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
196b5677b36Schristos 	char *p, *cp, **cpp, **new;
197b5677b36Schristos 	int num = 0;
198b5677b36Schristos 	int max = 0;
199b5677b36Schristos 
200b5677b36Schristos 	for (cpp = hes_list; *cpp; cpp++) {
201b5677b36Schristos 		cp = *cpp;
202b5677b36Schristos 
203b5677b36Schristos 		/* Strip away comments, if any. */
204b5677b36Schristos 		if ((p = strchr(cp, '#')))
205b5677b36Schristos 			*p = 0;
206b5677b36Schristos 
207b5677b36Schristos 		/* Skip blank lines. */
208b5677b36Schristos 		p = cp;
209b5677b36Schristos 		while (*p && !isspace((unsigned char)*p))
210b5677b36Schristos 			p++;
211b5677b36Schristos 		if (!*p)
212b5677b36Schristos 			continue;
213b5677b36Schristos 
214b5677b36Schristos 		/* OK, we've got a live one.  Let's parse it for real. */
215b5677b36Schristos 		if (pvt->prbuf)
216b5677b36Schristos 			free(pvt->prbuf);
217b5677b36Schristos 		pvt->prbuf = strdup(cp);
218b5677b36Schristos 
219b5677b36Schristos 		p = pvt->prbuf;
220b5677b36Schristos 		pvt->proto.p_name = p;
221b5677b36Schristos 		while (*p && !isspace((unsigned char)*p))
222b5677b36Schristos 			p++;
223b5677b36Schristos 		if (!*p)
224b5677b36Schristos 			continue;
225b5677b36Schristos 		*p++ = '\0';
226b5677b36Schristos 
227b5677b36Schristos 		pvt->proto.p_proto = atoi(p);
228b5677b36Schristos 		while (*p && !isspace((unsigned char)*p))
229b5677b36Schristos 			p++;
230b5677b36Schristos 		if (*p)
231b5677b36Schristos 			*p++ = '\0';
232b5677b36Schristos 
233b5677b36Schristos 		while (*p) {
234b5677b36Schristos 			if ((num + 1) >= max || !pvt->proto.p_aliases) {
235b5677b36Schristos 				max += 10;
236b5677b36Schristos 				new = realloc(pvt->proto.p_aliases,
237b5677b36Schristos 					      max * sizeof(char *));
238b5677b36Schristos 				if (!new) {
239b5677b36Schristos 					errno = ENOMEM;
240b5677b36Schristos 					goto cleanup;
241b5677b36Schristos 				}
242b5677b36Schristos 				pvt->proto.p_aliases = new;
243b5677b36Schristos 			}
244b5677b36Schristos 			pvt->proto.p_aliases[num++] = p;
245b5677b36Schristos 			while (*p && !isspace((unsigned char)*p))
246b5677b36Schristos 				p++;
247b5677b36Schristos 			if (*p)
248b5677b36Schristos 				*p++ = '\0';
249b5677b36Schristos 		}
250b5677b36Schristos 		if (!pvt->proto.p_aliases)
251b5677b36Schristos 			pvt->proto.p_aliases = malloc(sizeof(char *));
252b5677b36Schristos 		if (!pvt->proto.p_aliases)
253b5677b36Schristos 			goto cleanup;
254b5677b36Schristos 		pvt->proto.p_aliases[num] = NULL;
255b5677b36Schristos 		return (&pvt->proto);
256b5677b36Schristos 	}
257b5677b36Schristos 
258b5677b36Schristos  cleanup:
259b5677b36Schristos 	if (pvt->proto.p_aliases) {
260b5677b36Schristos 		free(pvt->proto.p_aliases);
261b5677b36Schristos 		pvt->proto.p_aliases = NULL;
262b5677b36Schristos 	}
263b5677b36Schristos 	if (pvt->prbuf) {
264b5677b36Schristos 		free(pvt->prbuf);
265b5677b36Schristos 		pvt->prbuf = NULL;
266b5677b36Schristos 	}
267b5677b36Schristos 	return (NULL);
268b5677b36Schristos }
269b5677b36Schristos 
270b5677b36Schristos /*! \file */
271