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