Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | */ |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 28 | |
Ken Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 29 | #define LOG_TAG "resolv" |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 30 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | #include <arpa/nameser.h> |
| 33 | #include <netdb.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 34 | #include <pthread.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 37 | #include <unistd.h> /* for gettid() */ |
| 38 | |
| 39 | #include <android-base/logging.h> |
| 40 | |
Bernie Innocenti | d35d292 | 2019-10-08 23:34:53 +0900 | [diff] [blame] | 41 | #include "res_init.h" |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 42 | #include "resolv_cache.h" |
| 43 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 44 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 45 | typedef struct { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 46 | // TODO: Have one __res_state per network so we don't have to repopulate frequently. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 47 | struct __res_state _nres[1]; |
| 48 | struct res_static _rstatic[1]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 49 | } _res_thread; |
| 50 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 51 | static _res_thread* res_thread_alloc(void) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 52 | _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 53 | |
| 54 | if (rt) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 55 | memset(rt->_rstatic, 0, sizeof rt->_rstatic); |
| 56 | } |
| 57 | return rt; |
| 58 | } |
| 59 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 60 | static void res_static_done(struct res_static* rs) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 65 | if (rs->hostf) { /* should not happen in theory, but just be safe */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 66 | fclose(rs->hostf); |
| 67 | rs->hostf = NULL; |
| 68 | } |
| 69 | free(rs->servent.s_aliases); |
| 70 | } |
| 71 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 72 | static void res_thread_free(void* _rt) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 73 | _res_thread* rt = (_res_thread*) _rt; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 74 | |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 75 | LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 76 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 77 | res_static_done(rt->_rstatic); |
Bernie Innocenti | c977ab7 | 2019-10-09 00:34:06 +0900 | [diff] [blame] | 78 | res_nclose(rt->_nres); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 79 | free(rt); |
| 80 | } |
| 81 | |
| 82 | static pthread_key_t _res_key; |
| 83 | |
| 84 | __attribute__((constructor)) static void __res_key_init() { |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 85 | pthread_key_create(&_res_key, res_thread_free); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 88 | static _res_thread* res_thread_get(void) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 89 | _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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. */ |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 96 | rt = res_thread_alloc(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 97 | if (rt == NULL) { |
| 98 | return NULL; |
| 99 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 100 | pthread_setspecific(_res_key, rt); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 101 | |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 102 | LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt; |
Bernie Innocenti | d35d292 | 2019-10-08 23:34:53 +0900 | [diff] [blame] | 103 | res_init(rt->_nres); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 104 | return rt; |
| 105 | } |
| 106 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 107 | struct __res_state _nres; |
| 108 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 109 | res_state res_get_state(void) { |
| 110 | _res_thread* rt = res_thread_get(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 111 | |
| 112 | return rt ? rt->_nres : NULL; |
| 113 | } |
| 114 | |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 115 | res_static* res_get_static(void) { |
| 116 | _res_thread* rt = res_thread_get(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 117 | |
| 118 | return rt ? rt->_rstatic : NULL; |
| 119 | } |