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 * dns.c --- this is the top-level accessor function for the dns
23 #include "port_before.h"
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
36 #include <isc/memcluster.h>
39 #include "port_after.h"
47 static void dns_close(struct irs_acc
*);
48 static struct __res_state
* dns_res_get(struct irs_acc
*);
49 static void dns_res_set(struct irs_acc
*, struct __res_state
*,
55 irs_dns_acc(const char *options
) {
61 if (!(acc
= memget(sizeof *acc
))) {
65 memset(acc
, 0x5e, sizeof *acc
);
66 if (!(dns
= memget(sizeof *dns
))) {
68 memput(acc
, sizeof *acc
);
71 memset(dns
, 0x5e, sizeof *dns
);
74 if (hesiod_init(&dns
->hes_ctx
) < 0) {
76 * We allow the dns accessor class to initialize
77 * despite hesiod failing to initialize correctly,
78 * since dns host queries don't depend on hesiod.
84 acc
->gr_map
= irs_dns_gr
;
89 acc
->pw_map
= irs_dns_pw
;
93 acc
->sv_map
= irs_dns_sv
;
94 acc
->pr_map
= irs_dns_pr
;
95 acc
->ho_map
= irs_dns_ho
;
96 acc
->nw_map
= irs_dns_nw
;
97 acc
->ng_map
= irs_nul_ng
;
98 acc
->res_get
= dns_res_get
;
99 acc
->res_set
= dns_res_set
;
100 acc
->close
= dns_close
;
105 static struct __res_state
*
106 dns_res_get(struct irs_acc
*this) {
107 struct dns_p
*dns
= (struct dns_p
*)this->private;
109 if (dns
->res
== NULL
) {
110 struct __res_state
*res
;
111 res
= (struct __res_state
*)malloc(sizeof *res
);
114 memset(res
, 0, sizeof *res
);
115 dns_res_set(this, res
, free
);
118 if ((dns
->res
->options
& RES_INIT
) == 0U &&
119 res_ninit(dns
->res
) < 0)
126 dns_res_set(struct irs_acc
*this, struct __res_state
*res
,
127 void (*free_res
)(void *)) {
128 struct dns_p
*dns
= (struct dns_p
*)this->private;
130 if (dns
->res
&& dns
->free_res
) {
131 res_nclose(dns
->res
);
132 (*dns
->free_res
)(dns
->res
);
135 dns
->free_res
= free_res
;
139 dns_close(struct irs_acc
*this) {
142 dns
= (struct dns_p
*)this->private;
143 if (dns
->res
&& dns
->free_res
)
144 (*dns
->free_res
)(dns
->res
);
146 hesiod_end(dns
->hes_ctx
);
147 memput(dns
, sizeof *dns
);
148 memput(this, sizeof *this);