4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 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: dns_pr.c,v 1.5 2005/04/27 04:56:22 sra Exp";
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
40 #include <isc/memcluster.h>
43 #include "port_after.h"
53 struct protoent proto
;
59 static void pr_close(struct irs_pr
*);
60 static struct protoent
* pr_byname(struct irs_pr
*, const char *);
61 static struct protoent
* pr_bynumber(struct irs_pr
*, int);
62 static struct protoent
* pr_next(struct irs_pr
*);
63 static void pr_rewind(struct irs_pr
*);
64 static void pr_minimize(struct irs_pr
*);
65 static struct __res_state
* pr_res_get(struct irs_pr
*);
66 static void pr_res_set(struct irs_pr
*,
70 static struct protoent
* parse_hes_list(struct irs_pr
*, char **);
75 irs_dns_pr(struct irs_acc
*this) {
76 struct dns_p
*dns
= (struct dns_p
*)this->private;
84 if (!(pvt
= memget(sizeof *pvt
))) {
88 memset(pvt
, 0, sizeof *pvt
);
89 if (!(pr
= memget(sizeof *pr
))) {
90 memput(pvt
, sizeof *pvt
);
94 memset(pr
, 0x5e, sizeof *pr
);
97 pr
->byname
= pr_byname
;
98 pr
->bynumber
= pr_bynumber
;
100 pr
->rewind
= pr_rewind
;
101 pr
->close
= pr_close
;
102 pr
->minimize
= pr_minimize
;
103 pr
->res_get
= pr_res_get
;
104 pr
->res_set
= pr_res_set
;
111 pr_close(struct irs_pr
*this) {
112 struct pvt
*pvt
= (struct pvt
*)this->private;
114 if (pvt
->proto
.p_aliases
)
115 free(pvt
->proto
.p_aliases
);
119 memput(pvt
, sizeof *pvt
);
120 memput(this, sizeof *this);
123 static struct protoent
*
124 pr_byname(struct irs_pr
*this, const char *name
) {
125 struct pvt
*pvt
= (struct pvt
*)this->private;
126 struct dns_p
*dns
= pvt
->dns
;
127 struct protoent
*proto
;
130 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, name
, "protocol")))
133 proto
= parse_hes_list(this, hes_list
);
134 hesiod_free_list(dns
->hes_ctx
, hes_list
);
138 static struct protoent
*
139 pr_bynumber(struct irs_pr
*this, int num
) {
140 struct pvt
*pvt
= (struct pvt
*)this->private;
141 struct dns_p
*dns
= pvt
->dns
;
142 struct protoent
*proto
;
146 sprintf(numstr
, "%d", num
);
147 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, numstr
, "protonum")))
150 proto
= parse_hes_list(this, hes_list
);
151 hesiod_free_list(dns
->hes_ctx
, hes_list
);
155 static struct protoent
*
156 pr_next(struct irs_pr
*this) {
163 pr_rewind(struct irs_pr
*this) {
169 pr_minimize(struct irs_pr
*this) {
174 static struct __res_state
*
175 pr_res_get(struct irs_pr
*this) {
176 struct pvt
*pvt
= (struct pvt
*)this->private;
177 struct dns_p
*dns
= pvt
->dns
;
179 return (__hesiod_res_get(dns
->hes_ctx
));
183 pr_res_set(struct irs_pr
*this, struct __res_state
* res
,
184 void (*free_res
)(void *)) {
185 struct pvt
*pvt
= (struct pvt
*)this->private;
186 struct dns_p
*dns
= pvt
->dns
;
188 __hesiod_res_set(dns
->hes_ctx
, res
, free_res
);
193 static struct protoent
*
194 parse_hes_list(struct irs_pr
*this, char **hes_list
) {
195 struct pvt
*pvt
= (struct pvt
*)this->private;
196 char *p
, *cp
, **cpp
, **new;
200 for (cpp
= hes_list
; *cpp
; cpp
++) {
203 /* Strip away comments, if any. */
204 if ((p
= strchr(cp
, '#')))
207 /* Skip blank lines. */
209 while (*p
&& !isspace((unsigned char)*p
))
214 /* OK, we've got a live one. Let's parse it for real. */
217 pvt
->prbuf
= strdup(cp
);
220 pvt
->proto
.p_name
= p
;
221 while (*p
&& !isspace((unsigned char)*p
))
227 pvt
->proto
.p_proto
= atoi(p
);
228 while (*p
&& !isspace((unsigned char)*p
))
234 if ((num
+ 1) >= max
|| !pvt
->proto
.p_aliases
) {
236 new = realloc(pvt
->proto
.p_aliases
,
237 max
* sizeof(char *));
242 pvt
->proto
.p_aliases
= new;
244 pvt
->proto
.p_aliases
[num
++] = p
;
245 while (*p
&& !isspace((unsigned char)*p
))
250 if (!pvt
->proto
.p_aliases
)
251 pvt
->proto
.p_aliases
= malloc(sizeof(char *));
252 if (!pvt
->proto
.p_aliases
)
254 pvt
->proto
.p_aliases
[num
] = NULL
;
255 return (&pvt
->proto
);
259 if (pvt
->proto
.p_aliases
) {
260 free(pvt
->proto
.p_aliases
);
261 pvt
->proto
.p_aliases
= NULL
;