Unleashed v1.4
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / dns_pr.c
blob4cf563e668255495ab78cd4c1cbc7a111337ea7d
1 /*
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 /* Imports */
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 #include <resolv.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <netdb.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <errno.h>
34 #include <isc/memcluster.h>
35 #include <irs.h>
37 #include "port_after.h"
39 #include "irs_p.h"
40 #include "hesiod.h"
41 #include "dns_p.h"
43 /* Types. */
45 struct pvt {
46 struct dns_p * dns;
47 struct protoent proto;
48 char * prbuf;
51 /* Forward. */
53 static void pr_close(struct irs_pr *);
54 static struct protoent * pr_byname(struct irs_pr *, const char *);
55 static struct protoent * pr_bynumber(struct irs_pr *, int);
56 static struct protoent * pr_next(struct irs_pr *);
57 static void pr_rewind(struct irs_pr *);
58 static void pr_minimize(struct irs_pr *);
59 static struct __res_state * pr_res_get(struct irs_pr *);
60 static void pr_res_set(struct irs_pr *,
61 struct __res_state *,
62 void (*)(void *));
64 static struct protoent * parse_hes_list(struct irs_pr *, char **);
66 /* Public. */
68 struct irs_pr *
69 irs_dns_pr(struct irs_acc *this) {
70 struct dns_p *dns = (struct dns_p *)this->private;
71 struct pvt *pvt;
72 struct irs_pr *pr;
74 if (!dns->hes_ctx) {
75 errno = ENODEV;
76 return (NULL);
78 if (!(pvt = memget(sizeof *pvt))) {
79 errno = ENOMEM;
80 return (NULL);
82 memset(pvt, 0, sizeof *pvt);
83 if (!(pr = memget(sizeof *pr))) {
84 memput(pvt, sizeof *pvt);
85 errno = ENOMEM;
86 return (NULL);
88 memset(pr, 0x5e, sizeof *pr);
89 pvt->dns = dns;
90 pr->private = pvt;
91 pr->byname = pr_byname;
92 pr->bynumber = pr_bynumber;
93 pr->next = pr_next;
94 pr->rewind = pr_rewind;
95 pr->close = pr_close;
96 pr->minimize = pr_minimize;
97 pr->res_get = pr_res_get;
98 pr->res_set = pr_res_set;
99 return (pr);
102 /* Methods. */
104 static void
105 pr_close(struct irs_pr *this) {
106 struct pvt *pvt = (struct pvt *)this->private;
108 free(pvt->proto.p_aliases);
109 free(pvt->prbuf);
111 memput(pvt, sizeof *pvt);
112 memput(this, sizeof *this);
115 static struct protoent *
116 pr_byname(struct irs_pr *this, const char *name) {
117 struct pvt *pvt = (struct pvt *)this->private;
118 struct dns_p *dns = pvt->dns;
119 struct protoent *proto;
120 char **hes_list;
122 if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "protocol")))
123 return (NULL);
125 proto = parse_hes_list(this, hes_list);
126 hesiod_free_list(dns->hes_ctx, hes_list);
127 return (proto);
130 static struct protoent *
131 pr_bynumber(struct irs_pr *this, int num) {
132 struct pvt *pvt = (struct pvt *)this->private;
133 struct dns_p *dns = pvt->dns;
134 struct protoent *proto;
135 char numstr[16];
136 char **hes_list;
138 sprintf(numstr, "%d", num);
139 if (!(hes_list = hesiod_resolve(dns->hes_ctx, numstr, "protonum")))
140 return (NULL);
142 proto = parse_hes_list(this, hes_list);
143 hesiod_free_list(dns->hes_ctx, hes_list);
144 return (proto);
147 static struct protoent *
148 pr_next(struct irs_pr *this) {
149 UNUSED(this);
150 errno = ENODEV;
151 return (NULL);
154 static void
155 pr_rewind(struct irs_pr *this) {
156 UNUSED(this);
157 /* NOOP */
160 static void
161 pr_minimize(struct irs_pr *this) {
162 UNUSED(this);
163 /* NOOP */
166 static struct __res_state *
167 pr_res_get(struct irs_pr *this) {
168 struct pvt *pvt = (struct pvt *)this->private;
169 struct dns_p *dns = pvt->dns;
171 return (__hesiod_res_get(dns->hes_ctx));
174 static void
175 pr_res_set(struct irs_pr *this, struct __res_state * res,
176 void (*free_res)(void *)) {
177 struct pvt *pvt = (struct pvt *)this->private;
178 struct dns_p *dns = pvt->dns;
180 __hesiod_res_set(dns->hes_ctx, res, free_res);
183 /* Private. */
185 static struct protoent *
186 parse_hes_list(struct irs_pr *this, char **hes_list) {
187 struct pvt *pvt = (struct pvt *)this->private;
188 char *p, *cp, **cpp, **new;
189 int num = 0;
190 int max = 0;
192 for (cpp = hes_list; *cpp; cpp++) {
193 cp = *cpp;
195 /* Strip away comments, if any. */
196 if ((p = strchr(cp, '#')))
197 *p = 0;
199 /* Skip blank lines. */
200 p = cp;
201 while (*p && !isspace((unsigned char)*p))
202 p++;
203 if (!*p)
204 continue;
206 /* OK, we've got a live one. Let's parse it for real. */
207 free(pvt->prbuf);
208 pvt->prbuf = strdup(cp);
210 p = pvt->prbuf;
211 pvt->proto.p_name = p;
212 while (*p && !isspace((unsigned char)*p))
213 p++;
214 if (!*p)
215 continue;
216 *p++ = '\0';
218 pvt->proto.p_proto = atoi(p);
219 while (*p && !isspace((unsigned char)*p))
220 p++;
221 if (*p)
222 *p++ = '\0';
224 while (*p) {
225 if ((num + 1) >= max || !pvt->proto.p_aliases) {
226 max += 10;
227 new = reallocarray(pvt->proto.p_aliases, max,
228 sizeof (char *));
229 if (!new) {
230 errno = ENOMEM;
231 goto cleanup;
233 pvt->proto.p_aliases = new;
235 pvt->proto.p_aliases[num++] = p;
236 while (*p && !isspace((unsigned char)*p))
237 p++;
238 if (*p)
239 *p++ = '\0';
241 if (!pvt->proto.p_aliases)
242 pvt->proto.p_aliases = malloc(sizeof(char *));
243 if (!pvt->proto.p_aliases)
244 goto cleanup;
245 pvt->proto.p_aliases[num] = NULL;
246 return (&pvt->proto);
249 cleanup:
250 if (pvt->proto.p_aliases) {
251 free(pvt->proto.p_aliases);
252 pvt->proto.p_aliases = NULL;
254 if (pvt->prbuf) {
255 free(pvt->prbuf);
256 pvt->prbuf = NULL;
258 return (NULL);
261 /*! \file */