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.1.2.2 2004/03/17 00:40:11 marka Exp $";
23 * dns.c --- this is the top-level accessor function for the dns
26 #include "port_before.h"
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
39 #include <isc/memcluster.h>
42 #include "port_after.h"
50 static void dns_close(struct irs_acc
*);
51 static struct __res_state
* dns_res_get(struct irs_acc
*);
52 static void dns_res_set(struct irs_acc
*, struct __res_state
*,
58 irs_dns_acc(const char *options
) {
64 if (!(acc
= memget(sizeof *acc
))) {
68 memset(acc
, 0x5e, sizeof *acc
);
69 if (!(dns
= memget(sizeof *dns
))) {
71 memput(acc
, sizeof *acc
);
74 memset(dns
, 0x5e, sizeof *dns
);
77 if (hesiod_init(&dns
->hes_ctx
) < 0) {
79 * We allow the dns accessor class to initialize
80 * despite hesiod failing to initialize correctly,
81 * since dns host queries don't depend on hesiod.
87 acc
->gr_map
= irs_dns_gr
;
92 acc
->pw_map
= irs_dns_pw
;
96 acc
->sv_map
= irs_dns_sv
;
97 acc
->pr_map
= irs_dns_pr
;
98 acc
->ho_map
= irs_dns_ho
;
99 acc
->nw_map
= irs_dns_nw
;
100 acc
->ng_map
= irs_nul_ng
;
101 acc
->res_get
= dns_res_get
;
102 acc
->res_set
= dns_res_set
;
103 acc
->close
= dns_close
;
108 static struct __res_state
*
109 dns_res_get(struct irs_acc
*this) {
110 struct dns_p
*dns
= (struct dns_p
*)this->private;
112 if (dns
->res
== NULL
) {
113 struct __res_state
*res
;
114 res
= (struct __res_state
*)malloc(sizeof *res
);
117 memset(dns
->res
, 0, sizeof *dns
->res
);
118 dns_res_set(this, res
, free
);
121 if ((dns
->res
->options
& RES_INIT
) == 0U &&
122 res_ninit(dns
->res
) < 0)
129 dns_res_set(struct irs_acc
*this, struct __res_state
*res
,
130 void (*free_res
)(void *)) {
131 struct dns_p
*dns
= (struct dns_p
*)this->private;
133 if (dns
->res
&& dns
->free_res
) {
134 res_nclose(dns
->res
);
135 (*dns
->free_res
)(dns
->res
);
138 dns
->free_res
= free_res
;
142 dns_close(struct irs_acc
*this) {
145 dns
= (struct dns_p
*)this->private;
146 if (dns
->res
&& dns
->free_res
)
147 (*dns
->free_res
)(dns
->res
);
149 hesiod_end(dns
->hes_ctx
);
150 memput(dns
, sizeof *dns
);
151 memput(this, sizeof *this);