blob: d4fa819150672e4cad39cb3f4225a02caf1e2999 [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 Innocenti8ad893f2018-08-31 14:09:46 +090037#include <sys/types.h>
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +090038#include <unistd.h> /* for gettid() */
39
40#include <android-base/logging.h>
41
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090042#include "resolv_cache.h"
43#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090044
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090045typedef struct {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090046 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090047 struct __res_state _nres[1];
48 struct res_static _rstatic[1];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090049} _res_thread;
50
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090051static _res_thread* res_thread_alloc(void) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090052 _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090053
54 if (rt) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090055 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
56 }
57 return rt;
58}
59
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090060static void res_static_done(struct res_static* rs) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090061 /* 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 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090065 if (rs->hostf) { /* should not happen in theory, but just be safe */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090066 fclose(rs->hostf);
67 rs->hostf = NULL;
68 }
69 free(rs->servent.s_aliases);
70}
71
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090072static void res_thread_free(void* _rt) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090073 _res_thread* rt = (_res_thread*) _rt;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090074
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +090075 LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090076
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090077 res_static_done(rt->_rstatic);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090078 res_ndestroy(rt->_nres);
79 free(rt);
80}
81
82static pthread_key_t _res_key;
83
84__attribute__((constructor)) static void __res_key_init() {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090085 pthread_key_create(&_res_key, res_thread_free);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090086}
87
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090088static _res_thread* res_thread_get(void) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090089 _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090090 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. */
Bernie Innocenti2e8540b2018-09-26 11:52:04 +090096 rt = res_thread_alloc();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090097 if (rt == NULL) {
98 return NULL;
99 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900100 pthread_setspecific(_res_key, rt);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900101
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +0900102 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt
103 << " setting DNS state (options=" << rt->_nres->options << ")";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900104 if (res_ninit(rt->_nres) < 0) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900105 /* This should not happen */
Bernie Innocenti9f05f5e2018-09-12 23:20:10 +0900106 LOG(VERBOSE) << __func__ << ": tid=" << gettid() << " rt=" << rt
107 << ", res_ninit() returned < 0";
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900108 res_thread_free(rt);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900109 pthread_setspecific(_res_key, NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900110 return NULL;
111 }
112 return rt;
113}
114
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900115struct __res_state _nres;
116
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900117res_state res_get_state(void) {
118 _res_thread* rt = res_thread_get();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900119
120 return rt ? rt->_nres : NULL;
121}
122
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900123res_static* res_get_static(void) {
124 _res_thread* rt = res_thread_get();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900125
126 return rt ? rt->_rstatic : NULL;
127}