4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid
[] = "Id: irs_data.c,v 1.12 2007/08/27 03:32:26 marka Exp";
24 #include "port_before.h"
26 #ifndef __BIND_NOSTATIC
28 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
36 #include <isc/memcluster.h>
45 #include "port_after.h"
49 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
54 extern struct __res_state _res
;
57 static pthread_key_t key
;
60 static struct net_data
*net_data
;
67 net_data_destroy(net_data
);
73 net_data_destroy(void *p
) {
74 struct net_data
*net_data
= p
;
76 res_ndestroy(net_data
->res
);
77 if (net_data
->gr
!= NULL
) {
78 (*net_data
->gr
->close
)(net_data
->gr
);
81 if (net_data
->pw
!= NULL
) {
82 (*net_data
->pw
->close
)(net_data
->pw
);
85 if (net_data
->sv
!= NULL
) {
86 (*net_data
->sv
->close
)(net_data
->sv
);
89 if (net_data
->pr
!= NULL
) {
90 (*net_data
->pr
->close
)(net_data
->pr
);
93 if (net_data
->ho
!= NULL
) {
94 (*net_data
->ho
->close
)(net_data
->ho
);
97 if (net_data
->nw
!= NULL
) {
98 (*net_data
->nw
->close
)(net_data
->nw
);
101 if (net_data
->ng
!= NULL
) {
102 (*net_data
->ng
->close
)(net_data
->ng
);
105 if (net_data
->ho_data
!= NULL
) {
106 free(net_data
->ho_data
);
107 net_data
->ho_data
= NULL
;
109 if (net_data
->nw_data
!= NULL
) {
110 free(net_data
->nw_data
);
111 net_data
->nw_data
= NULL
;
114 (*net_data
->irs
->close
)(net_data
->irs
);
115 memput(net_data
, sizeof *net_data
);
119 * applications that need a specific config file other than
120 * _PATH_IRS_CONF should call net_data_init directly rather than letting
121 * the various wrapper functions make the first call. - brister
125 net_data_init(const char *conf_file
) {
127 #ifndef LIBBIND_MUTEX_INITIALIZER
128 #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
130 static pthread_mutex_t keylock
= LIBBIND_MUTEX_INITIALIZER
;
131 struct net_data
*net_data
;
134 if (pthread_mutex_lock(&keylock
) != 0)
137 if (pthread_key_create(&key
, net_data_destroy
) != 0) {
138 (void)pthread_mutex_unlock(&keylock
);
143 if (pthread_mutex_unlock(&keylock
) != 0)
146 net_data
= pthread_getspecific(key
);
149 if (net_data
== NULL
) {
150 net_data
= net_data_create(conf_file
);
151 if (net_data
== NULL
)
154 if (pthread_setspecific(key
, net_data
) != 0) {
155 net_data_destroy(net_data
);
165 net_data_create(const char *conf_file
) {
166 struct net_data
*net_data
;
168 net_data
= memget(sizeof (struct net_data
));
169 if (net_data
== NULL
)
171 memset(net_data
, 0, sizeof (struct net_data
));
173 if ((net_data
->irs
= irs_gen_acc("", conf_file
)) == NULL
) {
174 memput(net_data
, sizeof (struct net_data
));
178 (*net_data
->irs
->res_set
)(net_data
->irs
, &_res
, NULL
);
181 net_data
->res
= (*net_data
->irs
->res_get
)(net_data
->irs
);
182 if (net_data
->res
== NULL
) {
183 (*net_data
->irs
->close
)(net_data
->irs
);
184 memput(net_data
, sizeof (struct net_data
));
188 if ((net_data
->res
->options
& RES_INIT
) == 0U &&
189 res_ninit(net_data
->res
) == -1) {
190 (*net_data
->irs
->close
)(net_data
->irs
);
191 memput(net_data
, sizeof (struct net_data
));
199 net_data_minimize(struct net_data
*net_data
) {
200 res_nclose(net_data
->res
);
206 /* NULL param here means use the default config file. */
207 struct net_data
*net_data
= net_data_init(NULL
);
208 if (net_data
&& net_data
->res
)
209 return (net_data
->res
);
224 /* NULL param here means use the default config file. */
225 struct net_data
*net_data
= net_data_init(NULL
);
226 if (net_data
&& net_data
->res
)
227 return (&net_data
->res
->res_h_errno
);
228 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
229 return(&_res
.res_h_errno
);
236 __h_errno_set(struct __res_state
*res
, int err
) {
239 #if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
240 res
->res_h_errno
= err
;
242 h_errno
= res
->res_h_errno
= err
;
246 #endif /*__BIND_NOSTATIC*/