2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: irs_data.c,v 1.3.2.2.4.3 2004/11/30 01:15:43 marka Exp $";
22 #include "port_before.h"
24 #ifndef __BIND_NOSTATIC
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
34 #include <isc/memcluster.h>
43 #include "port_after.h"
47 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
52 extern struct __res_state _res;
55 static pthread_key_t key;
58 static struct net_data *net_data;
65 net_data_destroy(net_data);
71 net_data_destroy(void *p) {
72 struct net_data *net_data = p;
74 res_ndestroy(net_data->res);
75 if (net_data->gr != NULL) {
76 (*net_data->gr->close)(net_data->gr);
79 if (net_data->pw != NULL) {
80 (*net_data->pw->close)(net_data->pw);
83 if (net_data->sv != NULL) {
84 (*net_data->sv->close)(net_data->sv);
87 if (net_data->pr != NULL) {
88 (*net_data->pr->close)(net_data->pr);
91 if (net_data->ho != NULL) {
92 (*net_data->ho->close)(net_data->ho);
95 if (net_data->nw != NULL) {
96 (*net_data->nw->close)(net_data->nw);
99 if (net_data->ng != NULL) {
100 (*net_data->ng->close)(net_data->ng);
103 if (net_data->ho_data != NULL) {
104 free(net_data->ho_data);
105 net_data->ho_data = NULL;
107 if (net_data->nw_data != NULL) {
108 free(net_data->nw_data);
109 net_data->nw_data = NULL;
112 (*net_data->irs->close)(net_data->irs);
113 memput(net_data, sizeof *net_data);
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
122 net_data_init(const char *conf_file) {
124 #ifndef LIBBIND_MUTEX_INITIALIZER
125 #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
127 static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER;
128 struct net_data *net_data;
131 pthread_mutex_lock(&keylock);
133 pthread_key_create(&key, net_data_destroy);
134 pthread_mutex_unlock(&keylock);
136 net_data = pthread_getspecific(key);
139 if (net_data == NULL) {
140 net_data = net_data_create(conf_file);
141 if (net_data == NULL)
144 pthread_setspecific(key, net_data);
152 net_data_create(const char *conf_file) {
153 struct net_data *net_data;
155 net_data = memget(sizeof (struct net_data));
156 if (net_data == NULL)
158 memset(net_data, 0, sizeof (struct net_data));
160 if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
161 memput(net_data, sizeof (struct net_data));
165 (*net_data->irs->res_set)(net_data->irs, &_res, NULL);
168 net_data->res = (*net_data->irs->res_get)(net_data->irs);
169 if (net_data->res == NULL) {
170 (*net_data->irs->close)(net_data->irs);
171 memput(net_data, sizeof (struct net_data));
175 if ((net_data->res->options & RES_INIT) == 0U &&
176 res_ninit(net_data->res) == -1) {
177 (*net_data->irs->close)(net_data->irs);
178 memput(net_data, sizeof (struct net_data));
186 net_data_minimize(struct net_data *net_data) {
187 res_nclose(net_data->res);
193 /* NULL param here means use the default config file. */
194 struct net_data *net_data = net_data_init(NULL);
195 if (net_data && net_data->res)
196 return (net_data->res);
211 /* NULL param here means use the default config file. */
212 struct net_data *net_data = net_data_init(NULL);
213 if (net_data && net_data->res)
214 return (&net_data->res->res_h_errno);
215 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
216 return(&_res.res_h_errno);
223 __h_errno_set(struct __res_state *res, int err) {
226 #if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
227 res->res_h_errno = err;
229 h_errno = res->res_h_errno = err;
233 #endif /*__BIND_NOSTATIC*/