2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 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: dns_pr.c,v 1.3.206.1 2004/03/09 08:33:34 marka Exp $";
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
38 #include <isc/memcluster.h>
41 #include "port_after.h"
51 struct protoent proto;
57 static void pr_close(struct irs_pr *);
58 static struct protoent * pr_byname(struct irs_pr *, const char *);
59 static struct protoent * pr_bynumber(struct irs_pr *, int);
60 static struct protoent * pr_next(struct irs_pr *);
61 static void pr_rewind(struct irs_pr *);
62 static void pr_minimize(struct irs_pr *);
63 static struct __res_state * pr_res_get(struct irs_pr *);
64 static void pr_res_set(struct irs_pr *,
68 static struct protoent * parse_hes_list(struct irs_pr *, char **);
73 irs_dns_pr(struct irs_acc *this) {
74 struct dns_p *dns = (struct dns_p *)this->private;
82 if (!(pvt = memget(sizeof *pvt))) {
86 memset(pvt, 0, sizeof *pvt);
87 if (!(pr = memget(sizeof *pr))) {
88 memput(pvt, sizeof *pvt);
92 memset(pr, 0x5e, sizeof *pr);
95 pr->byname = pr_byname;
96 pr->bynumber = pr_bynumber;
98 pr->rewind = pr_rewind;
100 pr->minimize = pr_minimize;
101 pr->res_get = pr_res_get;
102 pr->res_set = pr_res_set;
109 pr_close(struct irs_pr *this) {
110 struct pvt *pvt = (struct pvt *)this->private;
112 if (pvt->proto.p_aliases)
113 free(pvt->proto.p_aliases);
117 memput(pvt, sizeof *pvt);
118 memput(this, sizeof *this);
121 static struct protoent *
122 pr_byname(struct irs_pr *this, const char *name) {
123 struct pvt *pvt = (struct pvt *)this->private;
124 struct dns_p *dns = pvt->dns;
125 struct protoent *proto;
128 if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "protocol")))
131 proto = parse_hes_list(this, hes_list);
132 hesiod_free_list(dns->hes_ctx, hes_list);
136 static struct protoent *
137 pr_bynumber(struct irs_pr *this, int num) {
138 struct pvt *pvt = (struct pvt *)this->private;
139 struct dns_p *dns = pvt->dns;
140 struct protoent *proto;
144 sprintf(numstr, "%d", num);
145 if (!(hes_list = hesiod_resolve(dns->hes_ctx, numstr, "protonum")))
148 proto = parse_hes_list(this, hes_list);
149 hesiod_free_list(dns->hes_ctx, hes_list);
153 static struct protoent *
154 pr_next(struct irs_pr *this) {
161 pr_rewind(struct irs_pr *this) {
167 pr_minimize(struct irs_pr *this) {
172 static struct __res_state *
173 pr_res_get(struct irs_pr *this) {
174 struct pvt *pvt = (struct pvt *)this->private;
175 struct dns_p *dns = pvt->dns;
177 return (__hesiod_res_get(dns->hes_ctx));
181 pr_res_set(struct irs_pr *this, struct __res_state * res,
182 void (*free_res)(void *)) {
183 struct pvt *pvt = (struct pvt *)this->private;
184 struct dns_p *dns = pvt->dns;
186 __hesiod_res_set(dns->hes_ctx, res, free_res);
191 static struct protoent *
192 parse_hes_list(struct irs_pr *this, char **hes_list) {
193 struct pvt *pvt = (struct pvt *)this->private;
194 char *p, *cp, **cpp, **new;
198 for (cpp = hes_list; *cpp; cpp++) {
201 /* Strip away comments, if any. */
202 if ((p = strchr(cp, '#')))
205 /* Skip blank lines. */
207 while (*p && !isspace((unsigned char)*p))
212 /* OK, we've got a live one. Let's parse it for real. */
215 pvt->prbuf = strdup(cp);
218 pvt->proto.p_name = p;
219 while (*p && !isspace((unsigned char)*p))
225 pvt->proto.p_proto = atoi(p);
226 while (*p && !isspace((unsigned char)*p))
232 if ((num + 1) >= max || !pvt->proto.p_aliases) {
234 new = realloc(pvt->proto.p_aliases,
235 max * sizeof(char *));
240 pvt->proto.p_aliases = new;
242 pvt->proto.p_aliases[num++] = p;
243 while (*p && !isspace((unsigned char)*p))
248 if (!pvt->proto.p_aliases)
249 pvt->proto.p_aliases = malloc(sizeof(char *));
250 if (!pvt->proto.p_aliases)
252 pvt->proto.p_aliases[num] = NULL;
253 return (&pvt->proto);
257 if (pvt->proto.p_aliases) {
258 free(pvt->proto.p_aliases);
259 pvt->proto.p_aliases = NULL;