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
29 #define LOG_TAG "res_state"
30
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
33 #include <netdb.h>
34 #include <pthread.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <unistd.h> /* for gettid() */
39
40 #include <android-base/logging.h>
41
42 #include "resolv_cache.h"
43 #include "resolv_private.h"
44
45 typedef struct {
46 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
47 struct __res_state _nres[1];
48 struct res_static _rstatic[1];
49 } _res_thread;
50
res_thread_alloc(void)51 static _res_thread* res_thread_alloc(void) {
52 _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
53
54 if (rt) {
55 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
56 }
57 return rt;
58 }
59
res_static_done(struct res_static * rs)60 static void res_static_done(struct res_static* rs) {
61 /* fortunately, there is nothing to do here, since the
62 * points in h_addr_ptrs and host_aliases should all
63 * point to 'hostbuf'
64 */
65 if (rs->hostf) { /* should not happen in theory, but just be safe */
66 fclose(rs->hostf);
67 rs->hostf = NULL;
68 }
69 free(rs->servent.s_aliases);
70 }
71
res_thread_free(void * _rt)72 static void res_thread_free(void* _rt) {
73 _res_thread* rt = (_res_thread*) _rt;
74
75 LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();
76
77 res_static_done(rt->_rstatic);
78 res_ndestroy(rt->_nres);
79 free(rt);
80 }
81
82 static pthread_key_t _res_key;
83
__res_key_init()84 __attribute__((constructor)) static void __res_key_init() {
85 pthread_key_create(&_res_key, res_thread_free);
86 }
87
res_thread_get(void)88 static _res_thread* res_thread_get(void) {
89 _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key);
90 if (rt != NULL) {
91 return rt;
92 }
93
94 /* It is the first time this function is called in this thread,
95 * we need to create a new thread-specific DNS resolver state. */
96 rt = res_thread_alloc();
97 if (rt == NULL) {
98 return NULL;
99 }
100 pthread_setspecific(_res_key, rt);
101
102 /* Reset the state, note that res_ninit() can now properly reset
103 * an existing state without leaking memory.
104 */
105 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt
106 << " setting DNS state (options=" << rt->_nres->options << ")";
107 if (res_ninit(rt->_nres) < 0) {
108 /* This should not happen */
109 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << " rt=" << rt
110 << ", res_ninit() returned < 0";
111 res_thread_free(rt);
112 pthread_setspecific(_res_key, NULL);
113 return NULL;
114 }
115 return rt;
116 }
117
118 struct __res_state _nres;
119
res_get_state(void)120 res_state res_get_state(void) {
121 _res_thread* rt = res_thread_get();
122
123 return rt ? rt->_nres : NULL;
124 }
125
res_get_static(void)126 res_static* res_get_static(void) {
127 _res_thread* rt = res_thread_get();
128
129 return rt ? rt->_rstatic : NULL;
130 }
131