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.c,v 1.5 2006/03/09 23:57:56 marka Exp";
26 * dns.c --- this is the top-level accessor function for the dns
29 #include "port_before.h"
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
42 #include <isc/memcluster.h>
45 #include "port_after.h"
53 static void dns_close(struct irs_acc
*);
54 static struct __res_state
* dns_res_get(struct irs_acc
*);
55 static void dns_res_set(struct irs_acc
*, struct __res_state
*,
61 irs_dns_acc(const char *options
) {
67 if (!(acc
= memget(sizeof *acc
))) {
71 memset(acc
, 0x5e, sizeof *acc
);
72 if (!(dns
= memget(sizeof *dns
))) {
74 memput(acc
, sizeof *acc
);
77 memset(dns
, 0x5e, sizeof *dns
);
80 if (hesiod_init(&dns
->hes_ctx
) < 0) {
82 * We allow the dns accessor class to initialize
83 * despite hesiod failing to initialize correctly,
84 * since dns host queries don't depend on hesiod.
90 acc
->gr_map
= irs_dns_gr
;
95 acc
->pw_map
= irs_dns_pw
;
99 acc
->sv_map
= irs_dns_sv
;
100 acc
->pr_map
= irs_dns_pr
;
101 acc
->ho_map
= irs_dns_ho
;
102 acc
->nw_map
= irs_dns_nw
;
103 acc
->ng_map
= irs_nul_ng
;
104 acc
->res_get
= dns_res_get
;
105 acc
->res_set
= dns_res_set
;
106 acc
->close
= dns_close
;
111 static struct __res_state
*
112 dns_res_get(struct irs_acc
*this) {
113 struct dns_p
*dns
= (struct dns_p
*)this->private;
115 if (dns
->res
== NULL
) {
116 struct __res_state
*res
;
117 res
= (struct __res_state
*)malloc(sizeof *res
);
120 memset(res
, 0, sizeof *res
);
121 dns_res_set(this, res
, free
);
124 if ((dns
->res
->options
& RES_INIT
) == 0U &&
125 res_ninit(dns
->res
) < 0)
132 dns_res_set(struct irs_acc
*this, struct __res_state
*res
,
133 void (*free_res
)(void *)) {
134 struct dns_p
*dns
= (struct dns_p
*)this->private;
136 if (dns
->res
&& dns
->free_res
) {
137 res_nclose(dns
->res
);
138 (*dns
->free_res
)(dns
->res
);
141 dns
->free_res
= free_res
;
145 dns_close(struct irs_acc
*this) {
148 dns
= (struct dns_p
*)this->private;
149 if (dns
->res
&& dns
->free_res
)
150 (*dns
->free_res
)(dns
->res
);
152 hesiod_end(dns
->hes_ctx
);
153 memput(dns
, sizeof *dns
);
154 memput(this, sizeof *this);