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.c,v 1.5 2006/03/09 23:57:56 marka Exp $";
24 * dns.c --- this is the top-level accessor function for the dns
27 #include "port_before.h"
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
40 #include <isc/memcluster.h>
43 #include "port_after.h"
51 static void dns_close(struct irs_acc
*);
52 static struct __res_state
* dns_res_get(struct irs_acc
*);
53 static void dns_res_set(struct irs_acc
*, struct __res_state
*,
59 irs_dns_acc(const char *options
) {
65 if (!(acc
= memget(sizeof *acc
))) {
69 memset(acc
, 0x5e, sizeof *acc
);
70 if (!(dns
= memget(sizeof *dns
))) {
72 memput(acc
, sizeof *acc
);
75 memset(dns
, 0x5e, sizeof *dns
);
78 if (hesiod_init(&dns
->hes_ctx
) < 0) {
80 * We allow the dns accessor class to initialize
81 * despite hesiod failing to initialize correctly,
82 * since dns host queries don't depend on hesiod.
88 acc
->gr_map
= irs_dns_gr
;
93 acc
->pw_map
= irs_dns_pw
;
97 acc
->sv_map
= irs_dns_sv
;
98 acc
->pr_map
= irs_dns_pr
;
99 acc
->ho_map
= irs_dns_ho
;
100 acc
->nw_map
= irs_dns_nw
;
101 acc
->ng_map
= irs_nul_ng
;
102 acc
->res_get
= dns_res_get
;
103 acc
->res_set
= dns_res_set
;
104 acc
->close
= dns_close
;
109 static struct __res_state
*
110 dns_res_get(struct irs_acc
*this) {
111 struct dns_p
*dns
= (struct dns_p
*)this->private;
113 if (dns
->res
== NULL
) {
114 struct __res_state
*res
;
115 res
= (struct __res_state
*)malloc(sizeof *res
);
118 memset(res
, 0, sizeof *res
);
119 dns_res_set(this, res
, free
);
122 if ((dns
->res
->options
& RES_INIT
) == 0U &&
123 res_ninit(dns
->res
) < 0)
130 dns_res_set(struct irs_acc
*this, struct __res_state
*res
,
131 void (*free_res
)(void *)) {
132 struct dns_p
*dns
= (struct dns_p
*)this->private;
134 if (dns
->res
&& dns
->free_res
) {
135 res_nclose(dns
->res
);
136 (*dns
->free_res
)(dns
->res
);
139 dns
->free_res
= free_res
;
143 dns_close(struct irs_acc
*this) {
146 dns
= (struct dns_p
*)this->private;
147 if (dns
->res
&& dns
->free_res
)
148 (*dns
->free_res
)(dns
->res
);
150 hesiod_end(dns
->hes_ctx
);
151 memput(dns
, sizeof *dns
);
152 memput(this, sizeof *this);