blob: 0a90733e041fb35e9ecabe1b00060d9403de40c3 [file] [log] [blame]
Bernie Innocenti318ed2d2018-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 Innocenti9f05f5e2018-09-12 23:20:10 +090028
Ken Chen5471dca2019-04-15 15:25:35 +080029#define LOG_TAG "resolv"
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +090030
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090031#include <arpa/inet.h>
32#include <arpa/nameser.h>
33#include <netdb.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090034#include <pthread.h>
35#include <stdlib.h>
36#include <string.h>
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +090037#include <unistd.h> /* for gettid() */
38
39#include <android-base/logging.h>
40
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090041#include "resolv_cache.h"
42#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090043
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090044typedef struct {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090045 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090046 struct __res_state _nres[1];
47 struct res_static _rstatic[1];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090048} _res_thread;
49
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090050static _res_thread* res_thread_alloc(void) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090051 _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090052
53 if (rt) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090054 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
55 }
56 return rt;
57}
58
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090059static void res_static_done(struct res_static* rs) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090060 /* fortunately, there is nothing to do here, since the
61 * points in h_addr_ptrs and host_aliases should all
62 * point to 'hostbuf'
63 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090064 if (rs->hostf) { /* should not happen in theory, but just be safe */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090065 fclose(rs->hostf);
66 rs->hostf = NULL;
67 }
68 free(rs->servent.s_aliases);
69}
70
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090071static void res_thread_free(void* _rt) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090072 _res_thread* rt = (_res_thread*) _rt;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090073
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +090074 LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090075
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090076 res_static_done(rt->_rstatic);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090077 res_ndestroy(rt->_nres);
78 free(rt);
79}
80
81static pthread_key_t _res_key;
82
83__attribute__((constructor)) static void __res_key_init() {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090084 pthread_key_create(&_res_key, res_thread_free);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090085}
86
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090087static _res_thread* res_thread_get(void) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090088 _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090089 if (rt != NULL) {
90 return rt;
91 }
92
93 /* It is the first time this function is called in this thread,
94 * we need to create a new thread-specific DNS resolver state. */
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090095 rt = res_thread_alloc();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090096 if (rt == NULL) {
97 return NULL;
98 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090099 pthread_setspecific(_res_key, rt);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900100
chenbrucec51f1212019-09-12 16:59:33 +0800101 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900102 if (res_ninit(rt->_nres) < 0) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900103 /* This should not happen */
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +0900104 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << " rt=" << rt
105 << ", res_ninit() returned < 0";
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900106 res_thread_free(rt);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900107 pthread_setspecific(_res_key, NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900108 return NULL;
109 }
110 return rt;
111}
112
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900113struct __res_state _nres;
114
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900115res_state res_get_state(void) {
116 _res_thread* rt = res_thread_get();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900117
118 return rt ? rt->_nres : NULL;
119}
120
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900121res_static* res_get_static(void) {
122 _res_thread* rt = res_thread_get();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900123
124 return rt ? rt->_rstatic : NULL;
125}