blob: 8f2851ab0e4dedd86db848d416325af079db98bf [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <sys/cdefs.h>
29#include <sys/types.h>
30#include <arpa/inet.h>
31#include "arpa_nameser.h"
32#include <netdb.h>
33#include "resolv_private.h"
34#include "resolv_cache.h"
35#include <pthread.h>
36#include <stdlib.h>
37
38#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
39#include <sys/_system_properties.h>
40
41static pthread_key_t _res_key;
42static pthread_once_t _res_once;
43
44typedef struct {
45 int _h_errno;
46 struct __res_state _nres[1];
47 unsigned _serial;
48 struct prop_info* _pi;
49 struct hostent* _hostent;
50 struct res_static _rstatic[1];
51} _res_thread;
52
53static _res_thread*
54_res_thread_alloc(void)
55{
56 _res_thread* rt = malloc(sizeof(*rt));
57
58 if (rt) {
59 rt->_h_errno = 0;
60 /* Special system property which tracks any changes to 'net.*'. */
61 rt->_serial = 0;
62 rt->_pi = (struct prop_info*) __system_property_find("net.change");
63 if (rt->_pi) {
64 rt->_serial = rt->_pi->serial;
65 }
66 if ( res_ninit( rt->_nres ) < 0 ) {
67 free(rt);
68 rt = NULL;
69 }
70 rt->_hostent = NULL;
71 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
72 }
73 return rt;
74}
75
76static void
77_res_static_done( res_static rs )
78{
79 /* fortunately, there is nothing to do here, since the
80 * points in h_addr_ptrs and host_aliases should all
81 * point to 'hostbuf'
82 */
83 if (rs->hostf) { /* should not happen in theory, but just be safe */
84 fclose(rs->hostf);
85 rs->hostf = NULL;
86 }
87 free(rs->servent.s_aliases);
88}
89
90static void
91_res_thread_free( void* _rt )
92{
93 _res_thread* rt = _rt;
94
95 _res_static_done(rt->_rstatic);
96 _resolv_hostent_free(rt->_hostent);
97 res_ndestroy(rt->_nres);
98 free(rt);
99}
100
101static void
102_res_init_key( void )
103{
104 pthread_key_create( &_res_key, _res_thread_free );
105}
106
107static _res_thread*
108_res_thread_get(void)
109{
110 _res_thread* rt;
111 pthread_once( &_res_once, _res_init_key );
112 rt = pthread_getspecific( _res_key );
113 if (rt == NULL) {
114 if ((rt = _res_thread_alloc()) == NULL) {
115 return NULL;
116 }
117 rt->_h_errno = 0;
118 rt->_serial = 0;
119 pthread_setspecific( _res_key, rt );
120 }
121 /* Check the serial value for any chanes to net.* properties. */
122 if (rt->_pi == NULL) {
123 rt->_pi = (struct prop_info*) __system_property_find("net.change");
124 }
125 if (rt->_pi == NULL || rt->_serial == rt->_pi->serial) {
126 return rt;
127 }
128 rt->_serial = rt->_pi->serial;
129 /* Reload from system properties. */
130 if ( res_ninit( rt->_nres ) < 0 ) {
131 free(rt);
132 rt = NULL;
133 pthread_setspecific( _res_key, rt );
134 }
135 return rt;
136}
137
138struct __res_state _nres;
139
140#if 0
141struct resolv_cache*
142__get_res_cache(void)
143{
144 _res_thread* rt = _res_thread_get();
145
146 if (!rt)
147 return NULL;
148
149 if (!rt->_cache) {
150 rt->_cache = _resolv_cache_create();
151 }
152 return rt->_cache;
153}
154#endif
155
156int*
157__get_h_errno(void)
158{
159 _res_thread* rt = _res_thread_get();
160 static int panic = NETDB_INTERNAL;
161
162 return rt ? &rt->_h_errno : &panic;
163}
164
165res_state
166__res_get_state(void)
167{
168 _res_thread* rt = _res_thread_get();
169
170 return rt ? rt->_nres : NULL;
171}
172
173void
174__res_put_state(res_state res)
175{
176 /* nothing to do */
177 res=res;
178}
179
180struct hostent**
181__get_res_cache_hostent_p(void)
182{
183 _res_thread* rt = _res_thread_get();
184
185 return rt ? &rt->_hostent : NULL;
186}
187
188res_static
189__res_get_static(void)
190{
191 _res_thread* rt = _res_thread_get();
192
193 return rt ? rt->_rstatic : NULL;
194}