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