irs_data.c revision 1.1.1.2 1 1.1 christos /* $NetBSD: irs_data.c,v 1.1.1.2 2012/09/09 16:07:57 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #if !defined(LINT) && !defined(CODECENTER)
21 1.1.1.2 christos static const char rcsid[] = "Id: irs_data.c,v 1.12 2007/08/27 03:32:26 marka Exp ";
22 1.1 christos #endif
23 1.1 christos
24 1.1 christos #include "port_before.h"
25 1.1 christos
26 1.1 christos #ifndef __BIND_NOSTATIC
27 1.1 christos
28 1.1 christos #include <sys/types.h>
29 1.1 christos
30 1.1 christos #include <netinet/in.h>
31 1.1 christos #include <arpa/nameser.h>
32 1.1 christos
33 1.1 christos #include <resolv.h>
34 1.1 christos #include <stdio.h>
35 1.1 christos #include <string.h>
36 1.1 christos #include <isc/memcluster.h>
37 1.1 christos
38 1.1 christos #ifdef DO_PTHREADS
39 1.1 christos #include <pthread.h>
40 1.1 christos #endif
41 1.1 christos
42 1.1 christos #include <irs.h>
43 1.1 christos #include <stdlib.h>
44 1.1 christos
45 1.1 christos #include "port_after.h"
46 1.1 christos
47 1.1 christos #include "irs_data.h"
48 1.1 christos #undef _res
49 1.1 christos #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
50 1.1 christos #undef h_errno
51 1.1 christos extern int h_errno;
52 1.1 christos #endif
53 1.1 christos
54 1.1 christos extern struct __res_state _res;
55 1.1 christos
56 1.1 christos #ifdef DO_PTHREADS
57 1.1 christos static pthread_key_t key;
58 1.1 christos static int once = 0;
59 1.1 christos #else
60 1.1 christos static struct net_data *net_data;
61 1.1 christos #endif
62 1.1 christos
63 1.1 christos void
64 1.1 christos irs_destroy(void) {
65 1.1 christos #ifndef DO_PTHREADS
66 1.1 christos if (net_data != NULL)
67 1.1 christos net_data_destroy(net_data);
68 1.1 christos net_data = NULL;
69 1.1 christos #endif
70 1.1 christos }
71 1.1 christos
72 1.1 christos void
73 1.1 christos net_data_destroy(void *p) {
74 1.1 christos struct net_data *net_data = p;
75 1.1 christos
76 1.1 christos res_ndestroy(net_data->res);
77 1.1 christos if (net_data->gr != NULL) {
78 1.1 christos (*net_data->gr->close)(net_data->gr);
79 1.1 christos net_data->gr = NULL;
80 1.1 christos }
81 1.1 christos if (net_data->pw != NULL) {
82 1.1 christos (*net_data->pw->close)(net_data->pw);
83 1.1 christos net_data->pw = NULL;
84 1.1 christos }
85 1.1 christos if (net_data->sv != NULL) {
86 1.1 christos (*net_data->sv->close)(net_data->sv);
87 1.1 christos net_data->sv = NULL;
88 1.1 christos }
89 1.1 christos if (net_data->pr != NULL) {
90 1.1 christos (*net_data->pr->close)(net_data->pr);
91 1.1 christos net_data->pr = NULL;
92 1.1 christos }
93 1.1 christos if (net_data->ho != NULL) {
94 1.1 christos (*net_data->ho->close)(net_data->ho);
95 1.1 christos net_data->ho = NULL;
96 1.1 christos }
97 1.1 christos if (net_data->nw != NULL) {
98 1.1 christos (*net_data->nw->close)(net_data->nw);
99 1.1 christos net_data->nw = NULL;
100 1.1 christos }
101 1.1 christos if (net_data->ng != NULL) {
102 1.1 christos (*net_data->ng->close)(net_data->ng);
103 1.1 christos net_data->ng = NULL;
104 1.1 christos }
105 1.1 christos if (net_data->ho_data != NULL) {
106 1.1 christos free(net_data->ho_data);
107 1.1 christos net_data->ho_data = NULL;
108 1.1 christos }
109 1.1 christos if (net_data->nw_data != NULL) {
110 1.1 christos free(net_data->nw_data);
111 1.1 christos net_data->nw_data = NULL;
112 1.1 christos }
113 1.1 christos
114 1.1 christos (*net_data->irs->close)(net_data->irs);
115 1.1 christos memput(net_data, sizeof *net_data);
116 1.1 christos }
117 1.1 christos
118 1.1 christos /*%
119 1.1 christos * applications that need a specific config file other than
120 1.1 christos * _PATH_IRS_CONF should call net_data_init directly rather than letting
121 1.1 christos * the various wrapper functions make the first call. - brister
122 1.1 christos */
123 1.1 christos
124 1.1 christos struct net_data *
125 1.1 christos net_data_init(const char *conf_file) {
126 1.1 christos #ifdef DO_PTHREADS
127 1.1 christos #ifndef LIBBIND_MUTEX_INITIALIZER
128 1.1 christos #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
129 1.1 christos #endif
130 1.1 christos static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER;
131 1.1 christos struct net_data *net_data;
132 1.1 christos
133 1.1 christos if (!once) {
134 1.1 christos if (pthread_mutex_lock(&keylock) != 0)
135 1.1 christos return (NULL);
136 1.1 christos if (!once) {
137 1.1 christos if (pthread_key_create(&key, net_data_destroy) != 0) {
138 1.1 christos (void)pthread_mutex_unlock(&keylock);
139 1.1 christos return (NULL);
140 1.1 christos }
141 1.1 christos once = 1;
142 1.1 christos }
143 1.1 christos if (pthread_mutex_unlock(&keylock) != 0)
144 1.1 christos return (NULL);
145 1.1 christos }
146 1.1 christos net_data = pthread_getspecific(key);
147 1.1 christos #endif
148 1.1 christos
149 1.1 christos if (net_data == NULL) {
150 1.1 christos net_data = net_data_create(conf_file);
151 1.1 christos if (net_data == NULL)
152 1.1 christos return (NULL);
153 1.1 christos #ifdef DO_PTHREADS
154 1.1 christos if (pthread_setspecific(key, net_data) != 0) {
155 1.1 christos net_data_destroy(net_data);
156 1.1 christos return (NULL);
157 1.1 christos }
158 1.1 christos #endif
159 1.1 christos }
160 1.1 christos
161 1.1 christos return (net_data);
162 1.1 christos }
163 1.1 christos
164 1.1 christos struct net_data *
165 1.1 christos net_data_create(const char *conf_file) {
166 1.1 christos struct net_data *net_data;
167 1.1 christos
168 1.1 christos net_data = memget(sizeof (struct net_data));
169 1.1 christos if (net_data == NULL)
170 1.1 christos return (NULL);
171 1.1 christos memset(net_data, 0, sizeof (struct net_data));
172 1.1 christos
173 1.1 christos if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
174 1.1 christos memput(net_data, sizeof (struct net_data));
175 1.1 christos return (NULL);
176 1.1 christos }
177 1.1 christos #ifndef DO_PTHREADS
178 1.1 christos (*net_data->irs->res_set)(net_data->irs, &_res, NULL);
179 1.1 christos #endif
180 1.1 christos
181 1.1 christos net_data->res = (*net_data->irs->res_get)(net_data->irs);
182 1.1 christos if (net_data->res == NULL) {
183 1.1 christos (*net_data->irs->close)(net_data->irs);
184 1.1 christos memput(net_data, sizeof (struct net_data));
185 1.1 christos return (NULL);
186 1.1 christos }
187 1.1 christos
188 1.1 christos if ((net_data->res->options & RES_INIT) == 0U &&
189 1.1 christos res_ninit(net_data->res) == -1) {
190 1.1 christos (*net_data->irs->close)(net_data->irs);
191 1.1 christos memput(net_data, sizeof (struct net_data));
192 1.1 christos return (NULL);
193 1.1 christos }
194 1.1 christos
195 1.1 christos return (net_data);
196 1.1 christos }
197 1.1 christos
198 1.1 christos void
199 1.1 christos net_data_minimize(struct net_data *net_data) {
200 1.1 christos res_nclose(net_data->res);
201 1.1 christos }
202 1.1 christos
203 1.1 christos #ifdef _REENTRANT
204 1.1 christos struct __res_state *
205 1.1 christos __res_state(void) {
206 1.1 christos /* NULL param here means use the default config file. */
207 1.1 christos struct net_data *net_data = net_data_init(NULL);
208 1.1 christos if (net_data && net_data->res)
209 1.1 christos return (net_data->res);
210 1.1 christos
211 1.1 christos return (&_res);
212 1.1 christos }
213 1.1 christos #else
214 1.1 christos #ifdef __linux
215 1.1 christos struct __res_state *
216 1.1 christos __res_state(void) {
217 1.1 christos return (&_res);
218 1.1 christos }
219 1.1 christos #endif
220 1.1 christos #endif
221 1.1 christos
222 1.1 christos int *
223 1.1 christos __h_errno(void) {
224 1.1 christos /* NULL param here means use the default config file. */
225 1.1 christos struct net_data *net_data = net_data_init(NULL);
226 1.1 christos if (net_data && net_data->res)
227 1.1 christos return (&net_data->res->res_h_errno);
228 1.1 christos #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
229 1.1 christos return(&_res.res_h_errno);
230 1.1 christos #else
231 1.1 christos return (&h_errno);
232 1.1 christos #endif
233 1.1 christos }
234 1.1 christos
235 1.1 christos void
236 1.1 christos __h_errno_set(struct __res_state *res, int err) {
237 1.1 christos
238 1.1 christos
239 1.1 christos #if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
240 1.1 christos res->res_h_errno = err;
241 1.1 christos #else
242 1.1 christos h_errno = res->res_h_errno = err;
243 1.1 christos #endif
244 1.1 christos }
245 1.1 christos
246 1.1 christos #endif /*__BIND_NOSTATIC*/
247 1.1 christos
248 1.1 christos /*! \file */
249