1 /*
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
41 /* Set to 1 to enable debug traces */
42 #define DEBUG 0
43
44 #if DEBUG
45 # include "private/libc_logging.h"
46 # include <unistd.h> /* for gettid() */
47 # define D(...) __libc_format_log(ANDROID_LOG_DEBUG,"libc", __VA_ARGS__)
48 #else
49 # define D(...) do{}while(0)
50 #endif
51
52 static pthread_key_t _res_key;
53 static pthread_once_t _res_once = PTHREAD_ONCE_INIT;
54
55 typedef struct {
56 int _h_errno;
57 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
58 struct __res_state _nres[1];
59 unsigned _serial;
60 struct prop_info* _pi;
61 struct res_static _rstatic[1];
62 } _res_thread;
63
64 static _res_thread*
_res_thread_alloc(void)65 _res_thread_alloc(void)
66 {
67 _res_thread* rt = calloc(1, sizeof(*rt));
68
69 if (rt) {
70 rt->_h_errno = 0;
71 /* Special system property which tracks any changes to 'net.*'. */
72 rt->_serial = 0;
73 rt->_pi = (struct prop_info*) __system_property_find("net.change");
74 if (rt->_pi) {
75 rt->_serial = __system_property_serial(rt->_pi);
76 }
77 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
78 }
79 return rt;
80 }
81
82 static void
_res_static_done(res_static rs)83 _res_static_done( res_static rs )
84 {
85 /* fortunately, there is nothing to do here, since the
86 * points in h_addr_ptrs and host_aliases should all
87 * point to 'hostbuf'
88 */
89 if (rs->hostf) { /* should not happen in theory, but just be safe */
90 fclose(rs->hostf);
91 rs->hostf = NULL;
92 }
93 free(rs->servent.s_aliases);
94 }
95
96 static void
_res_thread_free(void * _rt)97 _res_thread_free( void* _rt )
98 {
99 _res_thread* rt = _rt;
100
101 D("%s: rt=%p for thread=%d", __FUNCTION__, rt, gettid());
102
103 _res_static_done(rt->_rstatic);
104 res_ndestroy(rt->_nres);
105 free(rt);
106 }
107
108 static void
_res_init_key(void)109 _res_init_key( void )
110 {
111 pthread_key_create( &_res_key, _res_thread_free );
112 }
113
114 static _res_thread*
_res_thread_get(void)115 _res_thread_get(void)
116 {
117 _res_thread* rt;
118 pthread_once( &_res_once, _res_init_key );
119 rt = pthread_getspecific( _res_key );
120
121 if (rt != NULL) {
122 /* We already have one thread-specific DNS state object.
123 * Check the serial value for any changes to net.* properties */
124 D("%s: Called for tid=%d rt=%p rt->pi=%p rt->serial=%d",
125 __FUNCTION__, gettid(), rt, rt->_pi, rt->_serial);
126 if (rt->_pi == NULL) {
127 /* The property wasn't created when _res_thread_get() was
128 * called the last time. This should only happen very
129 * early during the boot sequence. First, let's try to see if it
130 * is here now. */
131 rt->_pi = (struct prop_info*) __system_property_find("net.change");
132 if (rt->_pi == NULL) {
133 /* Still nothing, return current state */
134 D("%s: exiting for tid=%d rt=%d since system property not found",
135 __FUNCTION__, gettid(), rt);
136 return rt;
137 }
138 }
139 if (rt->_serial == __system_property_serial(rt->_pi)) {
140 /* Nothing changed, so return the current state */
141 D("%s: tid=%d rt=%p nothing changed, returning",
142 __FUNCTION__, gettid(), rt);
143 return rt;
144 }
145 /* Update the recorded serial number, and go reset the state */
146 rt->_serial = __system_property_serial(rt->_pi);
147 goto RESET_STATE;
148 }
149
150 /* It is the first time this function is called in this thread,
151 * we need to create a new thread-specific DNS resolver state. */
152 rt = _res_thread_alloc();
153 if (rt == NULL) {
154 return NULL;
155 }
156 pthread_setspecific( _res_key, rt );
157 D("%s: tid=%d Created new DNS state rt=%p",
158 __FUNCTION__, gettid(), rt);
159
160 RESET_STATE:
161 /* Reset the state, note that res_ninit() can now properly reset
162 * an existing state without leaking memory.
163 */
164 D("%s: tid=%d, rt=%p, resetting DNS state (options RES_INIT=%d)",
165 __FUNCTION__, gettid(), rt, (rt->_nres->options & RES_INIT) != 0);
166 if ( res_ninit( rt->_nres ) < 0 ) {
167 /* This should not happen */
168 D("%s: tid=%d rt=%p, woot, res_ninit() returned < 0",
169 __FUNCTION__, gettid(), rt);
170 _res_thread_free(rt);
171 pthread_setspecific( _res_key, NULL );
172 return NULL;
173 }
174 return rt;
175 }
176
177 __LIBC_HIDDEN__
178 struct __res_state _nres;
179
180 #if 0
181 struct resolv_cache*
182 __get_res_cache(void)
183 {
184 _res_thread* rt = _res_thread_get();
185
186 if (!rt)
187 return NULL;
188
189 if (!rt->_cache) {
190 rt->_cache = _resolv_cache_create();
191 }
192 return rt->_cache;
193 }
194 #endif
195
196 int*
__get_h_errno(void)197 __get_h_errno(void)
198 {
199 _res_thread* rt = _res_thread_get();
200 static int panic = NETDB_INTERNAL;
201
202 return rt ? &rt->_h_errno : &panic;
203 }
204
205 res_state
__res_get_state(void)206 __res_get_state(void)
207 {
208 _res_thread* rt = _res_thread_get();
209
210 return rt ? rt->_nres : NULL;
211 }
212
213 void
__res_put_state(res_state res __unused)214 __res_put_state(res_state res __unused)
215 {
216 /* nothing to do */
217 }
218
219 res_static
__res_get_static(void)220 __res_get_static(void)
221 {
222 _res_thread* rt = _res_thread_get();
223
224 return rt ? rt->_rstatic : NULL;
225 }
226