Bernie Innocenti | 5586419 | 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 | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 28 | #include <arpa/inet.h> |
| 29 | #include <arpa/nameser.h> |
| 30 | #include <netdb.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 31 | #include <pthread.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include "resolv_cache.h" |
| 36 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 37 | |
| 38 | /* Set to 1 to enable debug traces */ |
| 39 | #define DEBUG 0 |
| 40 | |
| 41 | #if DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 42 | #include <async_safe/log.h> |
| 43 | #include <unistd.h> /* for gettid() */ |
| 44 | #define D(...) async_safe_format_log(ANDROID_LOG_DEBUG, "libc", __VA_ARGS__) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 45 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 46 | #define D(...) \ |
| 47 | do { \ |
| 48 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | typedef struct { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 52 | int _h_errno; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 53 | // TODO: Have one __res_state per network so we don't have to repopulate frequently. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 54 | struct __res_state _nres[1]; |
| 55 | struct res_static _rstatic[1]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 56 | } _res_thread; |
| 57 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 58 | static _res_thread* _res_thread_alloc(void) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 59 | _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 60 | |
| 61 | if (rt) { |
| 62 | rt->_h_errno = 0; |
| 63 | memset(rt->_rstatic, 0, sizeof rt->_rstatic); |
| 64 | } |
| 65 | return rt; |
| 66 | } |
| 67 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 68 | static void _res_static_done(struct res_static* rs) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 69 | /* fortunately, there is nothing to do here, since the |
| 70 | * points in h_addr_ptrs and host_aliases should all |
| 71 | * point to 'hostbuf' |
| 72 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 73 | if (rs->hostf) { /* should not happen in theory, but just be safe */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 74 | fclose(rs->hostf); |
| 75 | rs->hostf = NULL; |
| 76 | } |
| 77 | free(rs->servent.s_aliases); |
| 78 | } |
| 79 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 80 | static void _res_thread_free(void* _rt) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 81 | _res_thread* rt = (_res_thread*) _rt; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 82 | |
| 83 | D("%s: rt=%p for thread=%d", __FUNCTION__, rt, gettid()); |
| 84 | |
| 85 | _res_static_done(rt->_rstatic); |
| 86 | res_ndestroy(rt->_nres); |
| 87 | free(rt); |
| 88 | } |
| 89 | |
| 90 | static pthread_key_t _res_key; |
| 91 | |
| 92 | __attribute__((constructor)) static void __res_key_init() { |
| 93 | pthread_key_create(&_res_key, _res_thread_free); |
| 94 | } |
| 95 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 96 | static _res_thread* _res_thread_get(void) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 97 | _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 98 | if (rt != NULL) { |
| 99 | return rt; |
| 100 | } |
| 101 | |
| 102 | /* It is the first time this function is called in this thread, |
| 103 | * we need to create a new thread-specific DNS resolver state. */ |
| 104 | rt = _res_thread_alloc(); |
| 105 | if (rt == NULL) { |
| 106 | return NULL; |
| 107 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 108 | pthread_setspecific(_res_key, rt); |
| 109 | D("%s: tid=%d Created new DNS state rt=%p", __FUNCTION__, gettid(), rt); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 110 | |
| 111 | /* Reset the state, note that res_ninit() can now properly reset |
| 112 | * an existing state without leaking memory. |
| 113 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 114 | D("%s: tid=%d, rt=%p, setting DNS state (options RES_INIT=%d)", __FUNCTION__, gettid(), rt, |
| 115 | (rt->_nres->options & RES_INIT) != 0); |
| 116 | if (res_ninit(rt->_nres) < 0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 117 | /* This should not happen */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 118 | D("%s: tid=%d rt=%p, woot, res_ninit() returned < 0", __FUNCTION__, gettid(), rt); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 119 | _res_thread_free(rt); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 120 | pthread_setspecific(_res_key, NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | return rt; |
| 124 | } |
| 125 | |
| 126 | __LIBC_HIDDEN__ |
| 127 | struct __res_state _nres; |
| 128 | |
| 129 | #if 0 |
| 130 | struct resolv_cache* |
| 131 | __get_res_cache(void) |
| 132 | { |
| 133 | _res_thread* rt = _res_thread_get(); |
| 134 | |
| 135 | if (!rt) |
| 136 | return NULL; |
| 137 | |
| 138 | if (!rt->_cache) { |
| 139 | rt->_cache = _resolv_cache_create(); |
| 140 | } |
| 141 | return rt->_cache; |
| 142 | } |
| 143 | #endif |
| 144 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 145 | int* __get_h_errno(void) { |
| 146 | _res_thread* rt = _res_thread_get(); |
| 147 | static int panic = NETDB_INTERNAL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 148 | |
| 149 | return rt ? &rt->_h_errno : &panic; |
| 150 | } |
| 151 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 152 | res_state __res_get_state(void) { |
| 153 | _res_thread* rt = _res_thread_get(); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 154 | |
| 155 | return rt ? rt->_nres : NULL; |
| 156 | } |
| 157 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 158 | void __res_put_state(res_state res __unused) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 159 | /* nothing to do */ |
| 160 | } |
| 161 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 162 | struct res_static* __res_get_static(void) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 163 | _res_thread* rt = _res_thread_get(); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 164 | |
| 165 | return rt ? rt->_rstatic : NULL; |
| 166 | } |