Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / dns.c
blobdbd30634792fb8b9f6ae04a55463ed7c694e757e
1 /*
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 $";
20 #endif
23 * dns.c --- this is the top-level accessor function for the dns
26 #include "port_before.h"
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <resolv.h>
37 #include <resolv.h>
39 #include <isc/memcluster.h>
40 #include <irs.h>
42 #include "port_after.h"
44 #include "irs_p.h"
45 #include "hesiod.h"
46 #include "dns_p.h"
48 /* forward */
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 *,
53 void (*)(void *));
55 /* public */
57 struct irs_acc *
58 irs_dns_acc(const char *options) {
59 struct irs_acc *acc;
60 struct dns_p *dns;
62 UNUSED(options);
64 if (!(acc = memget(sizeof *acc))) {
65 errno = ENOMEM;
66 return (NULL);
68 memset(acc, 0x5e, sizeof *acc);
69 if (!(dns = memget(sizeof *dns))) {
70 errno = ENOMEM;
71 memput(acc, sizeof *acc);
72 return (NULL);
74 memset(dns, 0x5e, sizeof *dns);
75 dns->res = NULL;
76 dns->free_res = NULL;
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.
83 dns->hes_ctx = NULL;
85 acc->private = dns;
86 #ifdef WANT_IRS_GR
87 acc->gr_map = irs_dns_gr;
88 #else
89 acc->gr_map = NULL;
90 #endif
91 #ifdef WANT_IRS_PW
92 acc->pw_map = irs_dns_pw;
93 #else
94 acc->pw_map = NULL;
95 #endif
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;
104 return (acc);
107 /* methods */
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);
115 if (res == NULL)
116 return (NULL);
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)
123 return (NULL);
125 return (dns->res);
128 static void
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);
137 dns->res = res;
138 dns->free_res = free_res;
141 static void
142 dns_close(struct irs_acc *this) {
143 struct dns_p *dns;
145 dns = (struct dns_p *)this->private;
146 if (dns->res && dns->free_res)
147 (*dns->free_res)(dns->res);
148 if (dns->hes_ctx)
149 hesiod_end(dns->hes_ctx);
150 memput(dns, sizeof *dns);
151 memput(this, sizeof *this);