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.
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
34 #include <isc/memcluster.h>
37 #include "port_after.h"
47 struct protoent proto
;
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
*,
64 static struct protoent
* parse_hes_list(struct irs_pr
*, char **);
69 irs_dns_pr(struct irs_acc
*this) {
70 struct dns_p
*dns
= (struct dns_p
*)this->private;
78 if (!(pvt
= memget(sizeof *pvt
))) {
82 memset(pvt
, 0, sizeof *pvt
);
83 if (!(pr
= memget(sizeof *pr
))) {
84 memput(pvt
, sizeof *pvt
);
88 memset(pr
, 0x5e, sizeof *pr
);
91 pr
->byname
= pr_byname
;
92 pr
->bynumber
= pr_bynumber
;
94 pr
->rewind
= pr_rewind
;
96 pr
->minimize
= pr_minimize
;
97 pr
->res_get
= pr_res_get
;
98 pr
->res_set
= pr_res_set
;
105 pr_close(struct irs_pr
*this) {
106 struct pvt
*pvt
= (struct pvt
*)this->private;
108 free(pvt
->proto
.p_aliases
);
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
;
122 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, name
, "protocol")))
125 proto
= parse_hes_list(this, hes_list
);
126 hesiod_free_list(dns
->hes_ctx
, hes_list
);
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
;
138 sprintf(numstr
, "%d", num
);
139 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, numstr
, "protonum")))
142 proto
= parse_hes_list(this, hes_list
);
143 hesiod_free_list(dns
->hes_ctx
, hes_list
);
147 static struct protoent
*
148 pr_next(struct irs_pr
*this) {
155 pr_rewind(struct irs_pr
*this) {
161 pr_minimize(struct irs_pr
*this) {
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
));
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
);
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;
192 for (cpp
= hes_list
; *cpp
; cpp
++) {
195 /* Strip away comments, if any. */
196 if ((p
= strchr(cp
, '#')))
199 /* Skip blank lines. */
201 while (*p
&& !isspace((unsigned char)*p
))
206 /* OK, we've got a live one. Let's parse it for real. */
208 pvt
->prbuf
= strdup(cp
);
211 pvt
->proto
.p_name
= p
;
212 while (*p
&& !isspace((unsigned char)*p
))
218 pvt
->proto
.p_proto
= atoi(p
);
219 while (*p
&& !isspace((unsigned char)*p
))
225 if ((num
+ 1) >= max
|| !pvt
->proto
.p_aliases
) {
227 new = reallocarray(pvt
->proto
.p_aliases
, max
,
233 pvt
->proto
.p_aliases
= new;
235 pvt
->proto
.p_aliases
[num
++] = p
;
236 while (*p
&& !isspace((unsigned char)*p
))
241 if (!pvt
->proto
.p_aliases
)
242 pvt
->proto
.p_aliases
= malloc(sizeof(char *));
243 if (!pvt
->proto
.p_aliases
)
245 pvt
->proto
.p_aliases
[num
] = NULL
;
246 return (&pvt
->proto
);
250 if (pvt
->proto
.p_aliases
) {
251 free(pvt
->proto
.p_aliases
);
252 pvt
->proto
.p_aliases
= NULL
;