]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/contrib/bind9/lib/bind/irs/irs_data.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / contrib / bind9 / lib / bind / irs / irs_data.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(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: irs_data.c,v 1.3.2.2.4.6 2007/08/27 03:40:01 marka Exp $";
20 #endif
21
22 #include "port_before.h"
23
24 #ifndef __BIND_NOSTATIC
25
26 #include <sys/types.h>
27
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30
31 #include <resolv.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <isc/memcluster.h>
35
36 #ifdef DO_PTHREADS
37 #include <pthread.h>
38 #endif
39
40 #include <irs.h>
41 #include <stdlib.h>
42
43 #include "port_after.h"
44
45 #include "irs_data.h"
46 #undef _res
47 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
48 #undef h_errno
49 extern int h_errno;
50 #endif
51
52 extern struct __res_state _res;
53
54 #ifdef  DO_PTHREADS
55 static pthread_key_t    key;
56 static int              once = 0;
57 #else
58 static struct net_data  *net_data;
59 #endif
60
61 void
62 irs_destroy(void) {
63 #ifndef DO_PTHREADS
64         if (net_data != NULL)
65                 net_data_destroy(net_data);
66         net_data = NULL;
67 #endif
68 }
69
70 void
71 net_data_destroy(void *p) {
72         struct net_data *net_data = p;
73
74         res_ndestroy(net_data->res);
75         if (net_data->gr != NULL) {
76                 (*net_data->gr->close)(net_data->gr);
77                 net_data->gr = NULL;
78         }
79         if (net_data->pw != NULL) {
80                 (*net_data->pw->close)(net_data->pw);
81                 net_data->pw = NULL;
82         }
83         if (net_data->sv != NULL) {
84                 (*net_data->sv->close)(net_data->sv);
85                 net_data->sv = NULL;
86         }
87         if (net_data->pr != NULL) {
88                 (*net_data->pr->close)(net_data->pr);
89                 net_data->pr = NULL;
90         }
91         if (net_data->ho != NULL) {
92                 (*net_data->ho->close)(net_data->ho);
93                 net_data->ho = NULL;
94         }
95         if (net_data->nw != NULL) {
96                 (*net_data->nw->close)(net_data->nw);
97                 net_data->nw = NULL;
98         }
99         if (net_data->ng != NULL) {
100                 (*net_data->ng->close)(net_data->ng);
101                 net_data->ng = NULL;
102         }
103         if (net_data->ho_data != NULL) {
104                 free(net_data->ho_data);
105                 net_data->ho_data = NULL;
106         }
107         if (net_data->nw_data != NULL) {
108                 free(net_data->nw_data);
109                 net_data->nw_data = NULL;
110         }
111
112         (*net_data->irs->close)(net_data->irs);
113         memput(net_data, sizeof *net_data);
114 }
115
116 /* applications that need a specific config file other than
117  * _PATH_IRS_CONF should call net_data_init directly rather than letting
118  *   the various wrapper functions make the first call. - brister
119  */
120
121 struct net_data *
122 net_data_init(const char *conf_file) {
123 #ifdef  DO_PTHREADS
124 #ifndef LIBBIND_MUTEX_INITIALIZER
125 #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
126 #endif
127         static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER;
128         struct net_data *net_data;
129
130         if (!once) {
131                 if (pthread_mutex_lock(&keylock) != 0)
132                         return (NULL);
133                 if (!once) {
134                         if (pthread_key_create(&key, net_data_destroy) != 0) {
135                                 (void)pthread_mutex_unlock(&keylock);
136                                 return (NULL);
137                         }
138                         once = 1;
139                 }
140                 if (pthread_mutex_unlock(&keylock) != 0)
141                         return (NULL);
142         }
143         net_data = pthread_getspecific(key);
144 #endif
145
146         if (net_data == NULL) {
147                 net_data = net_data_create(conf_file);
148                 if (net_data == NULL)
149                         return (NULL);
150 #ifdef  DO_PTHREADS
151                 if (pthread_setspecific(key, net_data) != 0) {
152                         net_data_destroy(net_data);
153                         return (NULL);
154                 }
155 #endif
156         }
157
158         return (net_data);
159 }
160
161 struct net_data *
162 net_data_create(const char *conf_file) {
163         struct net_data *net_data;
164
165         net_data = memget(sizeof (struct net_data));
166         if (net_data == NULL)
167                 return (NULL);
168         memset(net_data, 0, sizeof (struct net_data));
169
170         if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
171                 memput(net_data, sizeof (struct net_data));
172                 return (NULL);
173         }
174 #ifndef DO_PTHREADS
175         (*net_data->irs->res_set)(net_data->irs, &_res, NULL);
176 #endif
177
178         net_data->res = (*net_data->irs->res_get)(net_data->irs);
179         if (net_data->res == NULL) {
180                 (*net_data->irs->close)(net_data->irs);
181                 memput(net_data, sizeof (struct net_data));
182                 return (NULL);
183         }
184
185         if ((net_data->res->options & RES_INIT) == 0U &&
186             res_ninit(net_data->res) == -1) {
187                 (*net_data->irs->close)(net_data->irs);
188                 memput(net_data, sizeof (struct net_data));
189                 return (NULL);
190         }
191
192         return (net_data);
193 }
194
195 void
196 net_data_minimize(struct net_data *net_data) {
197         res_nclose(net_data->res);
198 }
199
200 #ifdef _REENTRANT
201 struct __res_state *
202 __res_state(void) {
203         /* NULL param here means use the default config file. */
204         struct net_data *net_data = net_data_init(NULL);
205         if (net_data && net_data->res)
206                 return (net_data->res);
207
208         return (&_res);
209 }
210 #else
211 #ifdef __linux
212 struct __res_state *
213 __res_state(void) {
214         return (&_res);
215 }
216 #endif
217 #endif
218
219 int *
220 __h_errno(void) {
221         /* NULL param here means use the default config file. */
222         struct net_data *net_data = net_data_init(NULL);
223         if (net_data && net_data->res)
224                 return (&net_data->res->res_h_errno);
225 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
226         return(&_res.res_h_errno);
227 #else
228         return (&h_errno);
229 #endif
230 }
231
232 void
233 __h_errno_set(struct __res_state *res, int err) {
234
235
236 #if (__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
237         res->res_h_errno = err;
238 #else
239         h_errno = res->res_h_errno = err;
240 #endif
241 }
242
243 #endif /*__BIND_NOSTATIC*/