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 /*! \file
19 * \brief
20 * dns.c --- this is the top-level accessor function for the dns
21 */
22
23 #include "port_before.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <resolv.h>
33
34 #include <resolv.h>
35
36 #include <isc/memcluster.h>
37 #include <irs.h>
38
39 #include "port_after.h"
40
41 #include "irs_p.h"
42 #include "hesiod.h"
43 #include "dns_p.h"
44
45 /* forward */
46
47 static void dns_close(struct irs_acc *);
48 static struct __res_state * dns_res_get(struct irs_acc *);
49 static void dns_res_set(struct irs_acc *, struct __res_state *,
50 void (*)(void *));
51
52 /* public */
53
54 struct irs_acc *
irs_dns_acc(const char * options)55 irs_dns_acc(const char *options) {
56 struct irs_acc *acc;
57 struct dns_p *dns;
58
59 UNUSED(options);
60
61 if (!(acc = memget(sizeof *acc))) {
62 errno = ENOMEM;
63 return (NULL);
64 }
65 memset(acc, 0x5e, sizeof *acc);
66 if (!(dns = memget(sizeof *dns))) {
67 errno = ENOMEM;
68 memput(acc, sizeof *acc);
69 return (NULL);
70 }
71 memset(dns, 0x5e, sizeof *dns);
72 dns->res = NULL;
73 dns->free_res = NULL;
74 if (hesiod_init(&dns->hes_ctx) < 0) {
75 /*
76 * We allow the dns accessor class to initialize
77 * despite hesiod failing to initialize correctly,
78 * since dns host queries don't depend on hesiod.
79 */
80 dns->hes_ctx = NULL;
81 }
82 acc->private = dns;
83 #ifdef WANT_IRS_GR
84 acc->gr_map = irs_dns_gr;
85 #else
86 acc->gr_map = NULL;
87 #endif
88 #ifdef WANT_IRS_PW
89 acc->pw_map = irs_dns_pw;
90 #else
91 acc->pw_map = NULL;
92 #endif
93 acc->sv_map = irs_dns_sv;
94 acc->pr_map = irs_dns_pr;
95 acc->ho_map = irs_dns_ho;
96 acc->nw_map = irs_dns_nw;
97 acc->ng_map = irs_nul_ng;
98 acc->res_get = dns_res_get;
99 acc->res_set = dns_res_set;
100 acc->close = dns_close;
101 return (acc);
102 }
103
104 /* methods */
105 static struct __res_state *
dns_res_get(struct irs_acc * this)106 dns_res_get(struct irs_acc *this) {
107 struct dns_p *dns = (struct dns_p *)this->private;
108
109 if (dns->res == NULL) {
110 struct __res_state *res;
111 res = (struct __res_state *)malloc(sizeof *res);
112 if (res == NULL)
113 return (NULL);
114 memset(res, 0, sizeof *res);
115 dns_res_set(this, res, free);
116 }
117
118 if ((dns->res->options & RES_INIT) == 0U &&
119 res_ninit(dns->res) < 0)
120 return (NULL);
121
122 return (dns->res);
123 }
124
125 static void
dns_res_set(struct irs_acc * this,struct __res_state * res,void (* free_res)(void *))126 dns_res_set(struct irs_acc *this, struct __res_state *res,
127 void (*free_res)(void *)) {
128 struct dns_p *dns = (struct dns_p *)this->private;
129
130 if (dns->res && dns->free_res) {
131 res_nclose(dns->res);
132 (*dns->free_res)(dns->res);
133 }
134 dns->res = res;
135 dns->free_res = free_res;
136 }
137
138 static void
dns_close(struct irs_acc * this)139 dns_close(struct irs_acc *this) {
140 struct dns_p *dns;
141
142 dns = (struct dns_p *)this->private;
143 if (dns->res && dns->free_res)
144 (*dns->free_res)(dns->res);
145 if (dns->hes_ctx)
146 hesiod_end(dns->hes_ctx);
147 memput(dns, sizeof *dns);
148 memput(this, sizeof *this);
149 }
150
151