blob: be349fb657af9d4b2e396df02e5b34e5a0c4265e [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/*
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 */
Bernie Innocenti55864192018-08-30 04:05:20 +090028#include <arpa/inet.h>
29#include <arpa/nameser.h>
30#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090031#include <pthread.h>
32#include <stdlib.h>
33#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090034#include <sys/cdefs.h>
35#include <sys/types.h>
36#include "resolv_cache.h"
37#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090038
39/* Set to 1 to enable debug traces */
40#define DEBUG 0
41
42#if DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090043#include <async_safe/log.h>
44#include <unistd.h> /* for gettid() */
45#define D(...) async_safe_format_log(ANDROID_LOG_DEBUG, "libc", __VA_ARGS__)
Bernie Innocenti55864192018-08-30 04:05:20 +090046#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090047#define D(...) \
48 do { \
49 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +090050#endif
51
52typedef struct {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090053 int _h_errno;
Bernie Innocenti55864192018-08-30 04:05:20 +090054 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090055 struct __res_state _nres[1];
56 struct res_static _rstatic[1];
Bernie Innocenti55864192018-08-30 04:05:20 +090057} _res_thread;
58
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090059static _res_thread* _res_thread_alloc(void) {
60 _res_thread* rt = calloc(1, sizeof(*rt));
Bernie Innocenti55864192018-08-30 04:05:20 +090061
62 if (rt) {
63 rt->_h_errno = 0;
64 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
65 }
66 return rt;
67}
68
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090069static void _res_static_done(res_static rs) {
Bernie Innocenti55864192018-08-30 04:05:20 +090070 /* fortunately, there is nothing to do here, since the
71 * points in h_addr_ptrs and host_aliases should all
72 * point to 'hostbuf'
73 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090074 if (rs->hostf) { /* should not happen in theory, but just be safe */
Bernie Innocenti55864192018-08-30 04:05:20 +090075 fclose(rs->hostf);
76 rs->hostf = NULL;
77 }
78 free(rs->servent.s_aliases);
79}
80
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090081static void _res_thread_free(void* _rt) {
82 _res_thread* rt = _rt;
Bernie Innocenti55864192018-08-30 04:05:20 +090083
84 D("%s: rt=%p for thread=%d", __FUNCTION__, rt, gettid());
85
86 _res_static_done(rt->_rstatic);
87 res_ndestroy(rt->_nres);
88 free(rt);
89}
90
91static pthread_key_t _res_key;
92
93__attribute__((constructor)) static void __res_key_init() {
94 pthread_key_create(&_res_key, _res_thread_free);
95}
96
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097static _res_thread* _res_thread_get(void) {
98 _res_thread* rt;
99 rt = pthread_getspecific(_res_key);
Bernie Innocenti55864192018-08-30 04:05:20 +0900100
101 if (rt != NULL) {
102 return rt;
103 }
104
105 /* It is the first time this function is called in this thread,
106 * we need to create a new thread-specific DNS resolver state. */
107 rt = _res_thread_alloc();
108 if (rt == NULL) {
109 return NULL;
110 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 pthread_setspecific(_res_key, rt);
112 D("%s: tid=%d Created new DNS state rt=%p", __FUNCTION__, gettid(), rt);
Bernie Innocenti55864192018-08-30 04:05:20 +0900113
114 /* Reset the state, note that res_ninit() can now properly reset
115 * an existing state without leaking memory.
116 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900117 D("%s: tid=%d, rt=%p, setting DNS state (options RES_INIT=%d)", __FUNCTION__, gettid(), rt,
118 (rt->_nres->options & RES_INIT) != 0);
119 if (res_ninit(rt->_nres) < 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900120 /* This should not happen */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121 D("%s: tid=%d rt=%p, woot, res_ninit() returned < 0", __FUNCTION__, gettid(), rt);
Bernie Innocenti55864192018-08-30 04:05:20 +0900122 _res_thread_free(rt);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900123 pthread_setspecific(_res_key, NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900124 return NULL;
125 }
126 return rt;
127}
128
129__LIBC_HIDDEN__
130struct __res_state _nres;
131
132#if 0
133struct resolv_cache*
134__get_res_cache(void)
135{
136 _res_thread* rt = _res_thread_get();
137
138 if (!rt)
139 return NULL;
140
141 if (!rt->_cache) {
142 rt->_cache = _resolv_cache_create();
143 }
144 return rt->_cache;
145}
146#endif
147
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148int* __get_h_errno(void) {
149 _res_thread* rt = _res_thread_get();
150 static int panic = NETDB_INTERNAL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900151
152 return rt ? &rt->_h_errno : &panic;
153}
154
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155res_state __res_get_state(void) {
156 _res_thread* rt = _res_thread_get();
Bernie Innocenti55864192018-08-30 04:05:20 +0900157
158 return rt ? rt->_nres : NULL;
159}
160
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900161void __res_put_state(res_state res __unused) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900162 /* nothing to do */
163}
164
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900165res_static __res_get_static(void) {
166 _res_thread* rt = _res_thread_get();
Bernie Innocenti55864192018-08-30 04:05:20 +0900167
168 return rt ? rt->_rstatic : NULL;
169}