Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / dns.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996-1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: dns.c,v 1.1.206.3 2006/03/10 00:17:21 marka Exp $";
20 #endif
21
22 /*
23  * dns.c --- this is the top-level accessor function for the dns
24  */
25
26 #include "port_before.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <resolv.h>
36
37 #include <resolv.h>
38
39 #include <isc/memcluster.h>
40 #include <irs.h>
41
42 #include "port_after.h"
43
44 #include "irs_p.h"
45 #include "hesiod.h"
46 #include "dns_p.h"
47
48 /* forward */
49
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 *));
54
55 /* public */
56
57 struct irs_acc *
58 irs_dns_acc(const char *options) {
59         struct irs_acc *acc;
60         struct dns_p *dns;
61
62         UNUSED(options);
63
64         if (!(acc = memget(sizeof *acc))) {
65                 errno = ENOMEM;
66                 return (NULL);
67         }
68         memset(acc, 0x5e, sizeof *acc);
69         if (!(dns = memget(sizeof *dns))) {
70                 errno = ENOMEM;
71                 memput(acc, sizeof *acc);
72                 return (NULL);
73         }
74         memset(dns, 0x5e, sizeof *dns);
75         dns->res = NULL;
76         dns->free_res = NULL;
77         if (hesiod_init(&dns->hes_ctx) < 0) {
78                 /*
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.
82                  */
83                 dns->hes_ctx = NULL;
84         }
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);
105 }
106
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;
111
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(res, 0, sizeof *res);
118                 dns_res_set(this, res, free);
119         }
120
121         if ((dns->res->options & RES_INIT) == 0U &&
122             res_ninit(dns->res) < 0)
123                 return (NULL);
124
125         return (dns->res);
126 }
127
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;
132
133         if (dns->res && dns->free_res) {
134                 res_nclose(dns->res);
135                 (*dns->free_res)(dns->res);
136         }
137         dns->res = res;
138         dns->free_res = free_res;
139 }
140
141 static void
142 dns_close(struct irs_acc *this) {
143         struct dns_p *dns;
144
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);
152 }
153