xref: /titanic_50/usr/src/lib/libresolv2/common/irs/irs_data.c (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
1 /*
2  * Copyright 2003 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 		pthread_mutex_lock(&keylock);
134 		if (!once++)
135 			pthread_key_create(&key, net_data_destroy);
136 		pthread_mutex_unlock(&keylock);
137 	}
138 	net_data = pthread_getspecific(key);
139 #endif
140 
141 	if (net_data == NULL) {
142 		net_data = net_data_create(conf_file);
143 		if (net_data == NULL)
144 			return (NULL);
145 #ifdef	DO_PTHREADS
146 		pthread_setspecific(key, net_data);
147 #endif
148 	}
149 
150 	return (net_data);
151 }
152 
153 struct net_data *
154 net_data_create(const char *conf_file) {
155 	struct net_data *net_data;
156 
157 	net_data = memget(sizeof (struct net_data));
158 	if (net_data == NULL)
159 		return (NULL);
160 	memset(net_data, 0, sizeof (struct net_data));
161 
162 	if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
163 		memput(net_data, sizeof (struct net_data));
164 		return (NULL);
165 	}
166 #ifndef DO_PTHREADS
167 	(*net_data->irs->res_set)(net_data->irs, &_res, NULL);
168 #endif
169 
170 	net_data->res = (*net_data->irs->res_get)(net_data->irs);
171 	if (net_data->res == NULL) {
172 		(*net_data->irs->close)(net_data->irs);
173 		memput(net_data, sizeof (struct net_data));
174 		return (NULL);
175 	}
176 
177 	if ((net_data->res->options & RES_INIT) == 0 &&
178 	    res_ninit(net_data->res) == -1) {
179 		(*net_data->irs->close)(net_data->irs);
180 		memput(net_data, sizeof (struct net_data));
181 		return (NULL);
182 	}
183 
184 	return (net_data);
185 }
186 
187 
188 
189 void
190 net_data_minimize(struct net_data *net_data) {
191 	res_nclose(net_data->res);
192 }
193 
194 #ifdef _REENTRANT
195 struct __res_state *
196 __res_state(void) {
197 	/* NULL param here means use the default config file. */
198 	struct net_data *net_data = net_data_init(NULL);
199 	if (net_data && net_data->res)
200 		return (net_data->res);
201 
202 	return (&_res);
203 }
204 #endif
205 
206 int *
207 __h_errno(void) {
208 	/* NULL param here means use the default config file. */
209 	struct net_data *net_data = net_data_init(NULL);
210 	if (net_data && net_data->res)
211 		return (&net_data->res->res_h_errno);
212 	return (&h_errno);
213 }
214 
215 void
216 __h_errno_set(struct __res_state *res, int err) {
217 
218 	h_errno = res->res_h_errno = err;
219 }
220 
221 #endif /*__BIND_NOSTATIC*/
222