blob: ca8d424660022991890264477df0681d595dcdde [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 Innocentie9ba09c2018-09-12 23:20:10 +090028
29#define LOG_TAG "res_state"
30
Bernie Innocenti55864192018-08-30 04:05:20 +090031#include <arpa/inet.h>
32#include <arpa/nameser.h>
33#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090034#include <pthread.h>
35#include <stdlib.h>
36#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090037#include <sys/types.h>
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090038#include <unistd.h> /* for gettid() */
39
40#include <android-base/logging.h>
41
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090042#include "resolv_cache.h"
43#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090044
Bernie Innocenti55864192018-08-30 04:05:20 +090045typedef struct {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090046 int _h_errno;
Bernie Innocenti55864192018-08-30 04:05:20 +090047 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090048 struct __res_state _nres[1];
49 struct res_static _rstatic[1];
Bernie Innocenti55864192018-08-30 04:05:20 +090050} _res_thread;
51
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090052static _res_thread* _res_thread_alloc(void) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090053 _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
Bernie Innocenti55864192018-08-30 04:05:20 +090054
55 if (rt) {
56 rt->_h_errno = 0;
57 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
58 }
59 return rt;
60}
61
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090062static void _res_static_done(struct res_static* rs) {
Bernie Innocenti55864192018-08-30 04:05:20 +090063 /* fortunately, there is nothing to do here, since the
64 * points in h_addr_ptrs and host_aliases should all
65 * point to 'hostbuf'
66 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090067 if (rs->hostf) { /* should not happen in theory, but just be safe */
Bernie Innocenti55864192018-08-30 04:05:20 +090068 fclose(rs->hostf);
69 rs->hostf = NULL;
70 }
71 free(rs->servent.s_aliases);
72}
73
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090074static void _res_thread_free(void* _rt) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090075 _res_thread* rt = (_res_thread*) _rt;
Bernie Innocenti55864192018-08-30 04:05:20 +090076
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090077 LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();
Bernie Innocenti55864192018-08-30 04:05:20 +090078
79 _res_static_done(rt->_rstatic);
80 res_ndestroy(rt->_nres);
81 free(rt);
82}
83
84static pthread_key_t _res_key;
85
86__attribute__((constructor)) static void __res_key_init() {
87 pthread_key_create(&_res_key, _res_thread_free);
88}
89
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090090static _res_thread* _res_thread_get(void) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090091 _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key);
Bernie Innocenti55864192018-08-30 04:05:20 +090092 if (rt != NULL) {
93 return rt;
94 }
95
96 /* It is the first time this function is called in this thread,
97 * we need to create a new thread-specific DNS resolver state. */
98 rt = _res_thread_alloc();
99 if (rt == NULL) {
100 return NULL;
101 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900102 pthread_setspecific(_res_key, rt);
Bernie Innocenti55864192018-08-30 04:05:20 +0900103
104 /* Reset the state, note that res_ninit() can now properly reset
105 * an existing state without leaking memory.
106 */
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900107 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt
108 << " setting DNS state (options=" << rt->_nres->options << ")";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900109 if (res_ninit(rt->_nres) < 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900110 /* This should not happen */
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900111 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << " rt=" << rt
112 << ", res_ninit() returned < 0";
Bernie Innocenti55864192018-08-30 04:05:20 +0900113 _res_thread_free(rt);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 pthread_setspecific(_res_key, NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900115 return NULL;
116 }
117 return rt;
118}
119
Bernie Innocenti55864192018-08-30 04:05:20 +0900120struct __res_state _nres;
121
122#if 0
123struct resolv_cache*
124__get_res_cache(void)
125{
126 _res_thread* rt = _res_thread_get();
127
128 if (!rt)
129 return NULL;
130
131 if (!rt->_cache) {
132 rt->_cache = _resolv_cache_create();
133 }
134 return rt->_cache;
135}
136#endif
137
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138int* __get_h_errno(void) {
139 _res_thread* rt = _res_thread_get();
140 static int panic = NETDB_INTERNAL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900141
142 return rt ? &rt->_h_errno : &panic;
143}
144
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145res_state __res_get_state(void) {
146 _res_thread* rt = _res_thread_get();
Bernie Innocenti55864192018-08-30 04:05:20 +0900147
148 return rt ? rt->_nres : NULL;
149}
150
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151void __res_put_state(res_state res __unused) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900152 /* nothing to do */
153}
154
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900155struct res_static* __res_get_static(void) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900156 _res_thread* rt = _res_thread_get();
Bernie Innocenti55864192018-08-30 04:05:20 +0900157
158 return rt ? rt->_rstatic : NULL;
159}