xref: /titanic_41/usr/src/lib/libresolv2/common/irs/irs_data.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
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 = PTHREAD_ONCE_KEY_NP;
61 #else
62 static struct net_data	*net_data = NULL;
63 #endif
64 
65 void
66 irs_destroy(void) {
67 #ifndef DO_PTHREADS
68 	if (net_data != NULL)
69 		net_data_destroy(net_data);
70 	net_data = NULL;
71 #endif
72 }
73 
74 void
75 net_data_destroy(void *p) {
76 	struct net_data *net_data = p;
77 
78 	res_ndestroy(net_data->res);
79 	if (net_data->gr != NULL) {
80 		(*net_data->gr->close)(net_data->gr);
81 		net_data->gr = NULL;
82 	}
83 	if (net_data->pw != NULL) {
84 		(*net_data->pw->close)(net_data->pw);
85 		net_data->pw = NULL;
86 	}
87 	if (net_data->sv != NULL) {
88 		(*net_data->sv->close)(net_data->sv);
89 		net_data->sv = NULL;
90 	}
91 	if (net_data->pr != NULL) {
92 		(*net_data->pr->close)(net_data->pr);
93 		net_data->pr = NULL;
94 	}
95 	if (net_data->ho != NULL) {
96 		(*net_data->ho->close)(net_data->ho);
97 		net_data->ho = NULL;
98 	}
99 	if (net_data->nw != NULL) {
100 		(*net_data->nw->close)(net_data->nw);
101 		net_data->nw = NULL;
102 	}
103 	if (net_data->ng != NULL) {
104 		(*net_data->ng->close)(net_data->ng);
105 		net_data->ng = NULL;
106 	}
107 	if (net_data->ho_data != NULL) {
108 		free(net_data->ho_data);
109 		net_data->ho_data = NULL;
110 	}
111 	if (net_data->nw_data != NULL) {
112 		free(net_data->nw_data);
113 		net_data->nw_data = NULL;
114 	}
115 
116 	(*net_data->irs->close)(net_data->irs);
117 	memput(net_data, sizeof *net_data);
118 }
119 
120 /* applications that need a specific config file other than
121  * _PATH_IRS_CONF should call net_data_init directly rather than letting
122  *   the various wrapper functions make the first call. - brister
123  */
124 
125 struct net_data *
126 net_data_init(const char *conf_file) {
127 #ifdef	DO_PTHREADS
128 	struct net_data *net_data;
129 
130 	if (pthread_key_create_once_np(&key, net_data_destroy) != 0)
131 		return (NULL);
132 	net_data = pthread_getspecific(key);
133 #endif
134 
135 	if (net_data == NULL) {
136 		net_data = net_data_create(conf_file);
137 		if (net_data == NULL)
138 			return (NULL);
139 #ifdef	DO_PTHREADS
140 		if (pthread_setspecific(key, net_data) != 0) {
141 			net_data_destroy(net_data);
142 			return (NULL);
143 		}
144 #endif
145 	}
146 
147 	return (net_data);
148 }
149 
150 struct net_data *
151 net_data_create(const char *conf_file) {
152 	struct net_data *net_data;
153 
154 	net_data = memget(sizeof (struct net_data));
155 	if (net_data == NULL)
156 		return (NULL);
157 	memset(net_data, 0, sizeof (struct net_data));
158 
159 	if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
160 		memput(net_data, sizeof (struct net_data));
161 		return (NULL);
162 	}
163 #ifndef DO_PTHREADS
164 	(*net_data->irs->res_set)(net_data->irs, &_res, NULL);
165 #endif
166 
167 	net_data->res = (*net_data->irs->res_get)(net_data->irs);
168 	if (net_data->res == NULL) {
169 		(*net_data->irs->close)(net_data->irs);
170 		memput(net_data, sizeof (struct net_data));
171 		return (NULL);
172 	}
173 
174 	if ((net_data->res->options & RES_INIT) == 0 &&
175 	    res_ninit(net_data->res) == -1) {
176 		(*net_data->irs->close)(net_data->irs);
177 		memput(net_data, sizeof (struct net_data));
178 		return (NULL);
179 	}
180 
181 	return (net_data);
182 }
183 
184 
185 
186 void
187 net_data_minimize(struct net_data *net_data) {
188 	res_nclose(net_data->res);
189 }
190 
191 #ifdef _REENTRANT
192 struct __res_state *
193 __res_state(void) {
194 	/* NULL param here means use the default config file. */
195 	struct net_data *net_data = net_data_init(NULL);
196 	if (net_data && net_data->res)
197 		return (net_data->res);
198 
199 	return (&_res);
200 }
201 #endif
202 
203 int *
204 __h_errno(void) {
205 	/* NULL param here means use the default config file. */
206 	struct net_data *net_data = net_data_init(NULL);
207 	if (net_data && net_data->res)
208 		return (&net_data->res->res_h_errno);
209 	return (&h_errno);
210 }
211 
212 void
213 __h_errno_set(struct __res_state *res, int err) {
214 
215 	h_errno = res->res_h_errno = err;
216 }
217 
218 #endif /*__BIND_NOSTATIC*/
219