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 | */ |
| 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 | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 31 | #include "resolv_cache.h" |
| 32 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 33 | #include <resolv.h> |
| 34 | #include <stdarg.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <time.h> |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 38 | #include <algorithm> |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 39 | #include <mutex> |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 40 | #include <set> |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 41 | #include <string> |
Luke Huang | e126fbe | 2019-07-20 17:36:30 +0800 | [diff] [blame] | 42 | #include <unordered_map> |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 43 | #include <vector> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 44 | |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 45 | #include <arpa/inet.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 46 | #include <arpa/nameser.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 47 | #include <errno.h> |
| 48 | #include <linux/if.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 49 | #include <net/if.h> |
| 50 | #include <netdb.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 51 | |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 52 | #include <android-base/logging.h> |
waynema | a74195e | 2019-01-18 14:02:31 +0800 | [diff] [blame] | 53 | #include <android-base/parseint.h> |
Luke Huang | e126fbe | 2019-07-20 17:36:30 +0800 | [diff] [blame] | 54 | #include <android-base/stringprintf.h> |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 55 | #include <android-base/strings.h> |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 56 | #include <android-base/thread_annotations.h> |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 57 | #include <android/multinetwork.h> // ResNsendFlags |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 58 | |
waynema | a74195e | 2019-01-18 14:02:31 +0800 | [diff] [blame] | 59 | #include <server_configurable_flags/get_flags.h> |
| 60 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 61 | #include "res_debug.h" |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 62 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 63 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 64 | using android::base::StringAppendF; |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 65 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 66 | /* This code implements a small and *simple* DNS resolver cache. |
| 67 | * |
| 68 | * It is only used to cache DNS answers for a time defined by the smallest TTL |
| 69 | * among the answer records in order to reduce DNS traffic. It is not supposed |
| 70 | * to be a full DNS cache, since we plan to implement that in the future in a |
| 71 | * dedicated process running on the system. |
| 72 | * |
| 73 | * Note that its design is kept simple very intentionally, i.e.: |
| 74 | * |
| 75 | * - it takes raw DNS query packet data as input, and returns raw DNS |
| 76 | * answer packet data as output |
| 77 | * |
| 78 | * (this means that two similar queries that encode the DNS name |
| 79 | * differently will be treated distinctly). |
| 80 | * |
| 81 | * the smallest TTL value among the answer records are used as the time |
| 82 | * to keep an answer in the cache. |
| 83 | * |
| 84 | * this is bad, but we absolutely want to avoid parsing the answer packets |
| 85 | * (and should be solved by the later full DNS cache process). |
| 86 | * |
| 87 | * - the implementation is just a (query-data) => (answer-data) hash table |
| 88 | * with a trivial least-recently-used expiration policy. |
| 89 | * |
| 90 | * Doing this keeps the code simple and avoids to deal with a lot of things |
| 91 | * that a full DNS cache is expected to do. |
| 92 | * |
| 93 | * The API is also very simple: |
| 94 | * |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 95 | * - the client calls resolv_cache_lookup() before performing a query |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 96 | * |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 97 | * If the function returns RESOLV_CACHE_FOUND, a copy of the answer data |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 98 | * has been copied into the client-provided answer buffer. |
| 99 | * |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 100 | * If the function returns RESOLV_CACHE_NOTFOUND, the client should perform |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 101 | * a request normally, *then* call resolv_cache_add() to add the received |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | * answer to the cache. |
| 103 | * |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 104 | * If the function returns RESOLV_CACHE_UNSUPPORTED, the client should |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 105 | * perform a request normally, and *not* call resolv_cache_add() |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 106 | * |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 107 | * Note that RESOLV_CACHE_UNSUPPORTED is also returned if the answer buffer |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 108 | * is too short to accomodate the cached result. |
| 109 | */ |
| 110 | |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 111 | /* Default number of entries kept in the cache. This value has been |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | * determined by browsing through various sites and counting the number |
| 113 | * of corresponding requests. Keep in mind that our framework is currently |
| 114 | * performing two requests per name lookup (one for IPv4, the other for IPv6) |
| 115 | * |
| 116 | * www.google.com 4 |
| 117 | * www.ysearch.com 6 |
| 118 | * www.amazon.com 8 |
| 119 | * www.nytimes.com 22 |
| 120 | * www.espn.com 28 |
| 121 | * www.msn.com 28 |
| 122 | * www.lemonde.fr 35 |
| 123 | * |
| 124 | * (determined in 2009-2-17 from Paris, France, results may vary depending |
| 125 | * on location) |
| 126 | * |
| 127 | * most high-level websites use lots of media/ad servers with different names |
| 128 | * but these are generally reused when browsing through the site. |
| 129 | * |
| 130 | * As such, a value of 64 should be relatively comfortable at the moment. |
| 131 | * |
| 132 | * ****************************************** |
| 133 | * * NOTE - this has changed. |
| 134 | * * 1) we've added IPv6 support so each dns query results in 2 responses |
| 135 | * * 2) we've made this a system-wide cache, so the cost is less (it's not |
| 136 | * * duplicated in each process) and the need is greater (more processes |
| 137 | * * making different requests). |
| 138 | * * Upping by 2x for IPv6 |
| 139 | * * Upping by another 5x for the centralized nature |
| 140 | * ***************************************** |
| 141 | */ |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 142 | const int CONFIG_MAX_ENTRIES = 64 * 2 * 5; |
Bernie Innocenti | 136af7b | 2018-10-01 20:46:20 +0900 | [diff] [blame] | 143 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 144 | static time_t _time_now(void) { |
| 145 | struct timeval tv; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 146 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | gettimeofday(&tv, NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 148 | return tv.tv_sec; |
| 149 | } |
| 150 | |
| 151 | /* reminder: the general format of a DNS packet is the following: |
| 152 | * |
| 153 | * HEADER (12 bytes) |
| 154 | * QUESTION (variable) |
| 155 | * ANSWER (variable) |
| 156 | * AUTHORITY (variable) |
| 157 | * ADDITIONNAL (variable) |
| 158 | * |
| 159 | * the HEADER is made of: |
| 160 | * |
| 161 | * ID : 16 : 16-bit unique query identification field |
| 162 | * |
| 163 | * QR : 1 : set to 0 for queries, and 1 for responses |
| 164 | * Opcode : 4 : set to 0 for queries |
| 165 | * AA : 1 : set to 0 for queries |
| 166 | * TC : 1 : truncation flag, will be set to 0 in queries |
| 167 | * RD : 1 : recursion desired |
| 168 | * |
| 169 | * RA : 1 : recursion available (0 in queries) |
| 170 | * Z : 3 : three reserved zero bits |
| 171 | * RCODE : 4 : response code (always 0=NOERROR in queries) |
| 172 | * |
| 173 | * QDCount: 16 : question count |
| 174 | * ANCount: 16 : Answer count (0 in queries) |
| 175 | * NSCount: 16: Authority Record count (0 in queries) |
| 176 | * ARCount: 16: Additionnal Record count (0 in queries) |
| 177 | * |
| 178 | * the QUESTION is made of QDCount Question Record (QRs) |
| 179 | * the ANSWER is made of ANCount RRs |
| 180 | * the AUTHORITY is made of NSCount RRs |
| 181 | * the ADDITIONNAL is made of ARCount RRs |
| 182 | * |
| 183 | * Each Question Record (QR) is made of: |
| 184 | * |
| 185 | * QNAME : variable : Query DNS NAME |
| 186 | * TYPE : 16 : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255) |
| 187 | * CLASS : 16 : class of query (IN=1) |
| 188 | * |
| 189 | * Each Resource Record (RR) is made of: |
| 190 | * |
| 191 | * NAME : variable : DNS NAME |
| 192 | * TYPE : 16 : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255) |
| 193 | * CLASS : 16 : class of query (IN=1) |
| 194 | * TTL : 32 : seconds to cache this RR (0=none) |
| 195 | * RDLENGTH: 16 : size of RDDATA in bytes |
| 196 | * RDDATA : variable : RR data (depends on TYPE) |
| 197 | * |
| 198 | * Each QNAME contains a domain name encoded as a sequence of 'labels' |
| 199 | * terminated by a zero. Each label has the following format: |
| 200 | * |
| 201 | * LEN : 8 : lenght of label (MUST be < 64) |
| 202 | * NAME : 8*LEN : label length (must exclude dots) |
| 203 | * |
| 204 | * A value of 0 in the encoding is interpreted as the 'root' domain and |
| 205 | * terminates the encoding. So 'www.android.com' will be encoded as: |
| 206 | * |
| 207 | * <3>www<7>android<3>com<0> |
| 208 | * |
| 209 | * Where <n> represents the byte with value 'n' |
| 210 | * |
| 211 | * Each NAME reflects the QNAME of the question, but has a slightly more |
| 212 | * complex encoding in order to provide message compression. This is achieved |
| 213 | * by using a 2-byte pointer, with format: |
| 214 | * |
| 215 | * TYPE : 2 : 0b11 to indicate a pointer, 0b01 and 0b10 are reserved |
| 216 | * OFFSET : 14 : offset to another part of the DNS packet |
| 217 | * |
| 218 | * The offset is relative to the start of the DNS packet and must point |
| 219 | * A pointer terminates the encoding. |
| 220 | * |
| 221 | * The NAME can be encoded in one of the following formats: |
| 222 | * |
| 223 | * - a sequence of simple labels terminated by 0 (like QNAMEs) |
| 224 | * - a single pointer |
| 225 | * - a sequence of simple labels terminated by a pointer |
| 226 | * |
| 227 | * A pointer shall always point to either a pointer of a sequence of |
| 228 | * labels (which can themselves be terminated by either a 0 or a pointer) |
| 229 | * |
| 230 | * The expanded length of a given domain name should not exceed 255 bytes. |
| 231 | * |
| 232 | * NOTE: we don't parse the answer packets, so don't need to deal with NAME |
| 233 | * records, only QNAMEs. |
| 234 | */ |
| 235 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 236 | #define DNS_HEADER_SIZE 12 |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 237 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 238 | #define DNS_TYPE_A "\00\01" /* big-endian decimal 1 */ |
| 239 | #define DNS_TYPE_PTR "\00\014" /* big-endian decimal 12 */ |
| 240 | #define DNS_TYPE_MX "\00\017" /* big-endian decimal 15 */ |
| 241 | #define DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */ |
| 242 | #define DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 243 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 244 | #define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 245 | |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 246 | struct DnsPacket { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 247 | const uint8_t* base; |
| 248 | const uint8_t* end; |
| 249 | const uint8_t* cursor; |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 250 | }; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 251 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 252 | static void _dnsPacket_init(DnsPacket* packet, const uint8_t* buff, int bufflen) { |
| 253 | packet->base = buff; |
| 254 | packet->end = buff + bufflen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 255 | packet->cursor = buff; |
| 256 | } |
| 257 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 258 | static void _dnsPacket_rewind(DnsPacket* packet) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 259 | packet->cursor = packet->base; |
| 260 | } |
| 261 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 262 | static void _dnsPacket_skip(DnsPacket* packet, int count) { |
| 263 | const uint8_t* p = packet->cursor + count; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 264 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 265 | if (p > packet->end) p = packet->end; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 266 | |
| 267 | packet->cursor = p; |
| 268 | } |
| 269 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 270 | static int _dnsPacket_readInt16(DnsPacket* packet) { |
| 271 | const uint8_t* p = packet->cursor; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 272 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 273 | if (p + 2 > packet->end) return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 274 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 275 | packet->cursor = p + 2; |
| 276 | return (p[0] << 8) | p[1]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 279 | /** QUERY CHECKING **/ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 280 | |
| 281 | /* check bytes in a dns packet. returns 1 on success, 0 on failure. |
| 282 | * the cursor is only advanced in the case of success |
| 283 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 284 | static int _dnsPacket_checkBytes(DnsPacket* packet, int numBytes, const void* bytes) { |
| 285 | const uint8_t* p = packet->cursor; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 286 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 287 | if (p + numBytes > packet->end) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 288 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 289 | if (memcmp(p, bytes, numBytes) != 0) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 290 | |
| 291 | packet->cursor = p + numBytes; |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | /* parse and skip a given QNAME stored in a query packet, |
| 296 | * from the current cursor position. returns 1 on success, |
| 297 | * or 0 for malformed data. |
| 298 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 299 | static int _dnsPacket_checkQName(DnsPacket* packet) { |
| 300 | const uint8_t* p = packet->cursor; |
| 301 | const uint8_t* end = packet->end; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 302 | |
| 303 | for (;;) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 304 | int c; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 305 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 306 | if (p >= end) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 307 | |
| 308 | c = *p++; |
| 309 | |
| 310 | if (c == 0) { |
| 311 | packet->cursor = p; |
| 312 | return 1; |
| 313 | } |
| 314 | |
| 315 | /* we don't expect label compression in QNAMEs */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 316 | if (c >= 64) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 317 | |
| 318 | p += c; |
| 319 | /* we rely on the bound check at the start |
| 320 | * of the loop here */ |
| 321 | } |
| 322 | /* malformed data */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 323 | LOG(INFO) << __func__ << ": malformed QNAME"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* parse and skip a given QR stored in a packet. |
| 328 | * returns 1 on success, and 0 on failure |
| 329 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 330 | static int _dnsPacket_checkQR(DnsPacket* packet) { |
| 331 | if (!_dnsPacket_checkQName(packet)) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 332 | |
| 333 | /* TYPE must be one of the things we support */ |
| 334 | if (!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_A) && |
| 335 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_PTR) && |
| 336 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_MX) && |
| 337 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_AAAA) && |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 338 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL)) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 339 | LOG(INFO) << __func__ << ": unsupported TYPE"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | /* CLASS must be IN */ |
| 343 | if (!_dnsPacket_checkBytes(packet, 2, DNS_CLASS_IN)) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 344 | LOG(INFO) << __func__ << ": unsupported CLASS"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | /* check the header of a DNS Query packet, return 1 if it is one |
| 352 | * type of query we can cache, or 0 otherwise |
| 353 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 354 | static int _dnsPacket_checkQuery(DnsPacket* packet) { |
| 355 | const uint8_t* p = packet->base; |
| 356 | int qdCount, anCount, dnCount, arCount; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 357 | |
| 358 | if (p + DNS_HEADER_SIZE > packet->end) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 359 | LOG(INFO) << __func__ << ": query packet too small"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* QR must be set to 0, opcode must be 0 and AA must be 0 */ |
| 364 | /* RA, Z, and RCODE must be 0 */ |
| 365 | if ((p[2] & 0xFC) != 0 || (p[3] & 0xCF) != 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 366 | LOG(INFO) << __func__ << ": query packet flags unsupported"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /* Note that we ignore the TC, RD, CD, and AD bits here for the |
| 371 | * following reasons: |
| 372 | * |
| 373 | * - there is no point for a query packet sent to a server |
| 374 | * to have the TC bit set, but the implementation might |
| 375 | * set the bit in the query buffer for its own needs |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 376 | * between a resolv_cache_lookup and a resolv_cache_add. |
| 377 | * We should not freak out if this is the case. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 378 | * |
| 379 | * - we consider that the result from a query might depend on |
| 380 | * the RD, AD, and CD bits, so these bits |
| 381 | * should be used to differentiate cached result. |
| 382 | * |
| 383 | * this implies that these bits are checked when hashing or |
| 384 | * comparing query packets, but not TC |
| 385 | */ |
| 386 | |
| 387 | /* ANCOUNT, DNCOUNT and ARCOUNT must be 0 */ |
| 388 | qdCount = (p[4] << 8) | p[5]; |
| 389 | anCount = (p[6] << 8) | p[7]; |
| 390 | dnCount = (p[8] << 8) | p[9]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 391 | arCount = (p[10] << 8) | p[11]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 392 | |
| 393 | if (anCount != 0 || dnCount != 0 || arCount > 1) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 394 | LOG(INFO) << __func__ << ": query packet contains non-query records"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | if (qdCount == 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 399 | LOG(INFO) << __func__ << ": query packet doesn't contain query record"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /* Check QDCOUNT QRs */ |
| 404 | packet->cursor = p + DNS_HEADER_SIZE; |
| 405 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 406 | for (; qdCount > 0; qdCount--) |
| 407 | if (!_dnsPacket_checkQR(packet)) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 408 | |
| 409 | return 1; |
| 410 | } |
| 411 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 412 | /** QUERY HASHING SUPPORT |
| 413 | ** |
| 414 | ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKET HAS ALREADY |
| 415 | ** BEEN SUCCESFULLY CHECKED. |
| 416 | **/ |
| 417 | |
| 418 | /* use 32-bit FNV hash function */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 419 | #define FNV_MULT 16777619U |
| 420 | #define FNV_BASIS 2166136261U |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 421 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 422 | static unsigned _dnsPacket_hashBytes(DnsPacket* packet, int numBytes, unsigned hash) { |
| 423 | const uint8_t* p = packet->cursor; |
| 424 | const uint8_t* end = packet->end; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 425 | |
| 426 | while (numBytes > 0 && p < end) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 427 | hash = hash * FNV_MULT ^ *p++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 428 | } |
| 429 | packet->cursor = p; |
| 430 | return hash; |
| 431 | } |
| 432 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 433 | static unsigned _dnsPacket_hashQName(DnsPacket* packet, unsigned hash) { |
| 434 | const uint8_t* p = packet->cursor; |
| 435 | const uint8_t* end = packet->end; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 436 | |
| 437 | for (;;) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 438 | int c; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 439 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 440 | if (p >= end) { /* should not happen */ |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 441 | LOG(INFO) << __func__ << ": INTERNAL_ERROR: read-overflow"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | |
| 445 | c = *p++; |
| 446 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 447 | if (c == 0) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 448 | |
| 449 | if (c >= 64) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 450 | LOG(INFO) << __func__ << ": INTERNAL_ERROR: malformed domain"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | if (p + c >= end) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 454 | LOG(INFO) << __func__ << ": INTERNAL_ERROR: simple label read-overflow"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | while (c > 0) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 458 | hash = hash * FNV_MULT ^ *p++; |
| 459 | c -= 1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | packet->cursor = p; |
| 463 | return hash; |
| 464 | } |
| 465 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 466 | static unsigned _dnsPacket_hashQR(DnsPacket* packet, unsigned hash) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 467 | hash = _dnsPacket_hashQName(packet, hash); |
| 468 | hash = _dnsPacket_hashBytes(packet, 4, hash); /* TYPE and CLASS */ |
| 469 | return hash; |
| 470 | } |
| 471 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 472 | static unsigned _dnsPacket_hashRR(DnsPacket* packet, unsigned hash) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 473 | int rdlength; |
| 474 | hash = _dnsPacket_hashQR(packet, hash); |
| 475 | hash = _dnsPacket_hashBytes(packet, 4, hash); /* TTL */ |
| 476 | rdlength = _dnsPacket_readInt16(packet); |
| 477 | hash = _dnsPacket_hashBytes(packet, rdlength, hash); /* RDATA */ |
| 478 | return hash; |
| 479 | } |
| 480 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 481 | static unsigned _dnsPacket_hashQuery(DnsPacket* packet) { |
| 482 | unsigned hash = FNV_BASIS; |
| 483 | int count, arcount; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 484 | _dnsPacket_rewind(packet); |
| 485 | |
| 486 | /* ignore the ID */ |
| 487 | _dnsPacket_skip(packet, 2); |
| 488 | |
| 489 | /* we ignore the TC bit for reasons explained in |
| 490 | * _dnsPacket_checkQuery(). |
| 491 | * |
| 492 | * however we hash the RD bit to differentiate |
| 493 | * between answers for recursive and non-recursive |
| 494 | * queries. |
| 495 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 496 | hash = hash * FNV_MULT ^ (packet->base[2] & 1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 497 | |
| 498 | /* mark the first header byte as processed */ |
| 499 | _dnsPacket_skip(packet, 1); |
| 500 | |
| 501 | /* process the second header byte */ |
| 502 | hash = _dnsPacket_hashBytes(packet, 1, hash); |
| 503 | |
| 504 | /* read QDCOUNT */ |
| 505 | count = _dnsPacket_readInt16(packet); |
| 506 | |
| 507 | /* assume: ANcount and NScount are 0 */ |
| 508 | _dnsPacket_skip(packet, 4); |
| 509 | |
| 510 | /* read ARCOUNT */ |
| 511 | arcount = _dnsPacket_readInt16(packet); |
| 512 | |
| 513 | /* hash QDCOUNT QRs */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 514 | for (; count > 0; count--) hash = _dnsPacket_hashQR(packet, hash); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 515 | |
| 516 | /* hash ARCOUNT RRs */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 517 | for (; arcount > 0; arcount--) hash = _dnsPacket_hashRR(packet, hash); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 518 | |
| 519 | return hash; |
| 520 | } |
| 521 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 522 | /** QUERY COMPARISON |
| 523 | ** |
| 524 | ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKETS HAVE ALREADY |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 525 | ** BEEN SUCCESSFULLY CHECKED. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 526 | **/ |
| 527 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 528 | static int _dnsPacket_isEqualDomainName(DnsPacket* pack1, DnsPacket* pack2) { |
| 529 | const uint8_t* p1 = pack1->cursor; |
| 530 | const uint8_t* end1 = pack1->end; |
| 531 | const uint8_t* p2 = pack2->cursor; |
| 532 | const uint8_t* end2 = pack2->end; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 533 | |
| 534 | for (;;) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 535 | int c1, c2; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 536 | |
| 537 | if (p1 >= end1 || p2 >= end2) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 538 | LOG(INFO) << __func__ << ": INTERNAL_ERROR: read-overflow"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | c1 = *p1++; |
| 542 | c2 = *p2++; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 543 | if (c1 != c2) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 544 | |
| 545 | if (c1 == 0) { |
| 546 | pack1->cursor = p1; |
| 547 | pack2->cursor = p2; |
| 548 | return 1; |
| 549 | } |
| 550 | if (c1 >= 64) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 551 | LOG(INFO) << __func__ << ": INTERNAL_ERROR: malformed domain"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 552 | break; |
| 553 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 554 | if ((p1 + c1 > end1) || (p2 + c1 > end2)) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 555 | LOG(INFO) << __func__ << ": INTERNAL_ERROR: simple label read-overflow"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 556 | break; |
| 557 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 558 | if (memcmp(p1, p2, c1) != 0) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 559 | p1 += c1; |
| 560 | p2 += c1; |
| 561 | /* we rely on the bound checks at the start of the loop */ |
| 562 | } |
| 563 | /* not the same, or one is malformed */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 564 | LOG(INFO) << __func__ << ": different DN"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 568 | static int _dnsPacket_isEqualBytes(DnsPacket* pack1, DnsPacket* pack2, int numBytes) { |
| 569 | const uint8_t* p1 = pack1->cursor; |
| 570 | const uint8_t* p2 = pack2->cursor; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 571 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 572 | if (p1 + numBytes > pack1->end || p2 + numBytes > pack2->end) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 573 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 574 | if (memcmp(p1, p2, numBytes) != 0) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 575 | |
| 576 | pack1->cursor += numBytes; |
| 577 | pack2->cursor += numBytes; |
| 578 | return 1; |
| 579 | } |
| 580 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 581 | static int _dnsPacket_isEqualQR(DnsPacket* pack1, DnsPacket* pack2) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 582 | /* compare domain name encoding + TYPE + CLASS */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 583 | if (!_dnsPacket_isEqualDomainName(pack1, pack2) || |
| 584 | !_dnsPacket_isEqualBytes(pack1, pack2, 2 + 2)) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 585 | return 0; |
| 586 | |
| 587 | return 1; |
| 588 | } |
| 589 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 590 | static int _dnsPacket_isEqualRR(DnsPacket* pack1, DnsPacket* pack2) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 591 | int rdlength1, rdlength2; |
| 592 | /* compare query + TTL */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 593 | if (!_dnsPacket_isEqualQR(pack1, pack2) || !_dnsPacket_isEqualBytes(pack1, pack2, 4)) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 594 | |
| 595 | /* compare RDATA */ |
| 596 | rdlength1 = _dnsPacket_readInt16(pack1); |
| 597 | rdlength2 = _dnsPacket_readInt16(pack2); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 598 | if (rdlength1 != rdlength2 || !_dnsPacket_isEqualBytes(pack1, pack2, rdlength1)) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 599 | |
| 600 | return 1; |
| 601 | } |
| 602 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 603 | static int _dnsPacket_isEqualQuery(DnsPacket* pack1, DnsPacket* pack2) { |
| 604 | int count1, count2, arcount1, arcount2; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 605 | |
| 606 | /* compare the headers, ignore most fields */ |
| 607 | _dnsPacket_rewind(pack1); |
| 608 | _dnsPacket_rewind(pack2); |
| 609 | |
| 610 | /* compare RD, ignore TC, see comment in _dnsPacket_checkQuery */ |
| 611 | if ((pack1->base[2] & 1) != (pack2->base[2] & 1)) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 612 | LOG(INFO) << __func__ << ": different RD"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | if (pack1->base[3] != pack2->base[3]) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 617 | LOG(INFO) << __func__ << ": different CD or AD"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /* mark ID and header bytes as compared */ |
| 622 | _dnsPacket_skip(pack1, 4); |
| 623 | _dnsPacket_skip(pack2, 4); |
| 624 | |
| 625 | /* compare QDCOUNT */ |
| 626 | count1 = _dnsPacket_readInt16(pack1); |
| 627 | count2 = _dnsPacket_readInt16(pack2); |
| 628 | if (count1 != count2 || count1 < 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 629 | LOG(INFO) << __func__ << ": different QDCOUNT"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* assume: ANcount and NScount are 0 */ |
| 634 | _dnsPacket_skip(pack1, 4); |
| 635 | _dnsPacket_skip(pack2, 4); |
| 636 | |
| 637 | /* compare ARCOUNT */ |
| 638 | arcount1 = _dnsPacket_readInt16(pack1); |
| 639 | arcount2 = _dnsPacket_readInt16(pack2); |
| 640 | if (arcount1 != arcount2 || arcount1 < 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 641 | LOG(INFO) << __func__ << ": different ARCOUNT"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* compare the QDCOUNT QRs */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 646 | for (; count1 > 0; count1--) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 647 | if (!_dnsPacket_isEqualQR(pack1, pack2)) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 648 | LOG(INFO) << __func__ << ": different QR"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* compare the ARCOUNT RRs */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 654 | for (; arcount1 > 0; arcount1--) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 655 | if (!_dnsPacket_isEqualRR(pack1, pack2)) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 656 | LOG(INFO) << __func__ << ": different additional RR"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | } |
| 660 | return 1; |
| 661 | } |
| 662 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 663 | /* cache entry. for simplicity, 'hash' and 'hlink' are inlined in this |
| 664 | * structure though they are conceptually part of the hash table. |
| 665 | * |
| 666 | * similarly, mru_next and mru_prev are part of the global MRU list |
| 667 | */ |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 668 | struct Entry { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 669 | unsigned int hash; /* hash value */ |
| 670 | struct Entry* hlink; /* next in collision chain */ |
| 671 | struct Entry* mru_prev; |
| 672 | struct Entry* mru_next; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 673 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 674 | const uint8_t* query; |
| 675 | int querylen; |
| 676 | const uint8_t* answer; |
| 677 | int answerlen; |
| 678 | time_t expires; /* time_t when the entry isn't valid any more */ |
| 679 | int id; /* for debugging purpose */ |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 680 | }; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 681 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 682 | /* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 683 | * Find the TTL for a negative DNS result. This is defined as the minimum |
| 684 | * of the SOA records TTL and the MINIMUM-TTL field (RFC-2308). |
| 685 | * |
| 686 | * Return 0 if not found. |
| 687 | */ |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 688 | static uint32_t answer_getNegativeTTL(ns_msg handle) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 689 | int n, nscount; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 690 | uint32_t result = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 691 | ns_rr rr; |
| 692 | |
| 693 | nscount = ns_msg_count(handle, ns_s_ns); |
| 694 | for (n = 0; n < nscount; n++) { |
| 695 | if ((ns_parserr(&handle, ns_s_ns, n, &rr) == 0) && (ns_rr_type(rr) == ns_t_soa)) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 696 | const uint8_t* rdata = ns_rr_rdata(rr); // find the data |
| 697 | const uint8_t* edata = rdata + ns_rr_rdlen(rr); // add the len to find the end |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 698 | int len; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 699 | uint32_t ttl, rec_result = rr.ttl; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 700 | |
| 701 | // find the MINIMUM-TTL field from the blob of binary data for this record |
| 702 | // skip the server name |
| 703 | len = dn_skipname(rdata, edata); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 704 | if (len == -1) continue; // error skipping |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 705 | rdata += len; |
| 706 | |
| 707 | // skip the admin name |
| 708 | len = dn_skipname(rdata, edata); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 709 | if (len == -1) continue; // error skipping |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 710 | rdata += len; |
| 711 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 712 | if (edata - rdata != 5 * NS_INT32SZ) continue; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 713 | // skip: serial number + refresh interval + retry interval + expiry |
| 714 | rdata += NS_INT32SZ * 4; |
| 715 | // finally read the MINIMUM TTL |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 716 | ttl = ntohl(*reinterpret_cast<const uint32_t*>(rdata)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 717 | if (ttl < rec_result) { |
| 718 | rec_result = ttl; |
| 719 | } |
| 720 | // Now that the record is read successfully, apply the new min TTL |
| 721 | if (n == 0 || rec_result < result) { |
| 722 | result = rec_result; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | return result; |
| 727 | } |
| 728 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 729 | /* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 730 | * Parse the answer records and find the appropriate |
| 731 | * smallest TTL among the records. This might be from |
| 732 | * the answer records if found or from the SOA record |
| 733 | * if it's a negative result. |
| 734 | * |
| 735 | * The returned TTL is the number of seconds to |
| 736 | * keep the answer in the cache. |
| 737 | * |
| 738 | * In case of parse error zero (0) is returned which |
| 739 | * indicates that the answer shall not be cached. |
| 740 | */ |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 741 | static uint32_t answer_getTTL(const void* answer, int answerlen) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 742 | ns_msg handle; |
| 743 | int ancount, n; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 744 | uint32_t result, ttl; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 745 | ns_rr rr; |
| 746 | |
| 747 | result = 0; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 748 | if (ns_initparse((const uint8_t*) answer, answerlen, &handle) >= 0) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 749 | // get number of answer records |
| 750 | ancount = ns_msg_count(handle, ns_s_an); |
| 751 | |
| 752 | if (ancount == 0) { |
| 753 | // a response with no answers? Cache this negative result. |
| 754 | result = answer_getNegativeTTL(handle); |
| 755 | } else { |
| 756 | for (n = 0; n < ancount; n++) { |
| 757 | if (ns_parserr(&handle, ns_s_an, n, &rr) == 0) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 758 | ttl = rr.ttl; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 759 | if (n == 0 || ttl < result) { |
| 760 | result = ttl; |
| 761 | } |
| 762 | } else { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 763 | PLOG(INFO) << __func__ << ": ns_parserr failed ancount no = " << n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
| 767 | } else { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 768 | PLOG(INFO) << __func__ << ": ns_initparse failed"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 769 | } |
| 770 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 771 | LOG(INFO) << __func__ << ": TTL = " << result; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 772 | return result; |
| 773 | } |
| 774 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 775 | static void entry_free(Entry* e) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 776 | /* everything is allocated in a single memory block */ |
| 777 | if (e) { |
| 778 | free(e); |
| 779 | } |
| 780 | } |
| 781 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 782 | static void entry_mru_remove(Entry* e) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 783 | e->mru_prev->mru_next = e->mru_next; |
| 784 | e->mru_next->mru_prev = e->mru_prev; |
| 785 | } |
| 786 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 787 | static void entry_mru_add(Entry* e, Entry* list) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 788 | Entry* first = list->mru_next; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 789 | |
| 790 | e->mru_next = first; |
| 791 | e->mru_prev = list; |
| 792 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 793 | list->mru_next = e; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 794 | first->mru_prev = e; |
| 795 | } |
| 796 | |
| 797 | /* compute the hash of a given entry, this is a hash of most |
| 798 | * data in the query (key) */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 799 | static unsigned entry_hash(const Entry* e) { |
| 800 | DnsPacket pack[1]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 801 | |
| 802 | _dnsPacket_init(pack, e->query, e->querylen); |
| 803 | return _dnsPacket_hashQuery(pack); |
| 804 | } |
| 805 | |
| 806 | /* initialize an Entry as a search key, this also checks the input query packet |
| 807 | * returns 1 on success, or 0 in case of unsupported/malformed data */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 808 | static int entry_init_key(Entry* e, const void* query, int querylen) { |
| 809 | DnsPacket pack[1]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 810 | |
| 811 | memset(e, 0, sizeof(*e)); |
| 812 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 813 | e->query = (const uint8_t*) query; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 814 | e->querylen = querylen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 815 | e->hash = entry_hash(e); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 816 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 817 | _dnsPacket_init(pack, e->query, e->querylen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 818 | |
| 819 | return _dnsPacket_checkQuery(pack); |
| 820 | } |
| 821 | |
| 822 | /* allocate a new entry as a cache node */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 823 | static Entry* entry_alloc(const Entry* init, const void* answer, int answerlen) { |
| 824 | Entry* e; |
| 825 | int size; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 826 | |
| 827 | size = sizeof(*e) + init->querylen + answerlen; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 828 | e = (Entry*) calloc(size, 1); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 829 | if (e == NULL) return e; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 830 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 831 | e->hash = init->hash; |
| 832 | e->query = (const uint8_t*) (e + 1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 833 | e->querylen = init->querylen; |
| 834 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 835 | memcpy((char*) e->query, init->query, e->querylen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 836 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 837 | e->answer = e->query + e->querylen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 838 | e->answerlen = answerlen; |
| 839 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 840 | memcpy((char*) e->answer, answer, e->answerlen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 841 | |
| 842 | return e; |
| 843 | } |
| 844 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 845 | static int entry_equals(const Entry* e1, const Entry* e2) { |
| 846 | DnsPacket pack1[1], pack2[1]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 847 | |
| 848 | if (e1->querylen != e2->querylen) { |
| 849 | return 0; |
| 850 | } |
| 851 | _dnsPacket_init(pack1, e1->query, e1->querylen); |
| 852 | _dnsPacket_init(pack2, e2->query, e2->querylen); |
| 853 | |
| 854 | return _dnsPacket_isEqualQuery(pack1, pack2); |
| 855 | } |
| 856 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 857 | /* We use a simple hash table with external collision lists |
| 858 | * for simplicity, the hash-table fields 'hash' and 'hlink' are |
| 859 | * inlined in the Entry structure. |
| 860 | */ |
| 861 | |
| 862 | /* Maximum time for a thread to wait for an pending request */ |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 863 | constexpr int PENDING_REQUEST_TIMEOUT = 20; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 864 | |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 865 | // lock protecting everything in the resolve_cache_info structs (next ptr, etc) |
| 866 | static std::mutex cache_mutex; |
| 867 | static std::condition_variable cv; |
| 868 | |
| 869 | // Note that Cache is not thread-safe per se, access to its members must be protected |
| 870 | // by an external mutex. |
| 871 | // |
| 872 | // TODO: move all cache manipulation code here and make data members private. |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 873 | struct Cache { |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 874 | Cache() { |
| 875 | entries.resize(CONFIG_MAX_ENTRIES); |
| 876 | mru_list.mru_prev = mru_list.mru_next = &mru_list; |
| 877 | } |
| 878 | ~Cache() { flush(); } |
| 879 | |
| 880 | void flush() { |
| 881 | for (int nn = 0; nn < CONFIG_MAX_ENTRIES; nn++) { |
| 882 | Entry** pnode = (Entry**)&entries[nn]; |
| 883 | |
| 884 | while (*pnode) { |
| 885 | Entry* node = *pnode; |
| 886 | *pnode = node->hlink; |
| 887 | entry_free(node); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | flushPendingRequests(); |
| 892 | |
| 893 | mru_list.mru_next = mru_list.mru_prev = &mru_list; |
| 894 | num_entries = 0; |
| 895 | last_id = 0; |
| 896 | |
| 897 | LOG(INFO) << "DNS cache flushed"; |
| 898 | } |
| 899 | |
| 900 | void flushPendingRequests() { |
| 901 | pending_req_info* ri = pending_requests.next; |
| 902 | while (ri) { |
| 903 | pending_req_info* tmp = ri; |
| 904 | ri = ri->next; |
| 905 | free(tmp); |
| 906 | } |
| 907 | |
| 908 | pending_requests.next = nullptr; |
| 909 | cv.notify_all(); |
| 910 | } |
| 911 | |
| 912 | int num_entries = 0; |
| 913 | |
| 914 | // TODO: convert to std::list |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 915 | Entry mru_list; |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 916 | int last_id = 0; |
| 917 | std::vector<Entry> entries; |
| 918 | |
| 919 | // TODO: convert to std::vector |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 920 | struct pending_req_info { |
| 921 | unsigned int hash; |
| 922 | struct pending_req_info* next; |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 923 | } pending_requests{}; |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 924 | }; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 925 | |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 926 | // TODO: allocate with new |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 927 | struct resolv_cache_info { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 928 | unsigned netid; |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 929 | Cache* cache; // TODO: use unique_ptr or embed |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 930 | struct resolv_cache_info* next; |
| 931 | int nscount; |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 932 | std::vector<std::string> nameservers; |
| 933 | struct addrinfo* nsaddrinfo[MAXNS]; // TODO: Use struct sockaddr_storage. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 934 | int revision_id; // # times the nameservers have been replaced |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 935 | res_params params; |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 936 | struct res_stats nsstats[MAXNS]; |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 937 | std::vector<std::string> search_domains; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 938 | int wait_for_pending_req_timeout_count; |
Luke Huang | e126fbe | 2019-07-20 17:36:30 +0800 | [diff] [blame] | 939 | // Map format: ReturnCode:rate_denom |
| 940 | std::unordered_map<int, uint32_t> dns_event_subsampling_map; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 941 | }; |
| 942 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 943 | /* gets cache associated with a network, or NULL if none exists */ |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 944 | static Cache* find_named_cache_locked(unsigned netid) REQUIRES(cache_mutex); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 945 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 946 | // Return true - if there is a pending request in |cache| matching |key|. |
| 947 | // Return false - if no pending request is found matching the key. Optionally |
| 948 | // link a new one if parameter append_if_not_found is true. |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 949 | static bool cache_has_pending_request_locked(Cache* cache, const Entry* key, |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 950 | bool append_if_not_found) { |
| 951 | if (!cache || !key) return false; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 952 | |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 953 | Cache::pending_req_info* ri = cache->pending_requests.next; |
| 954 | Cache::pending_req_info* prev = &cache->pending_requests; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 955 | while (ri) { |
| 956 | if (ri->hash == key->hash) { |
| 957 | return true; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 958 | } |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 959 | prev = ri; |
| 960 | ri = ri->next; |
| 961 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 962 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 963 | if (append_if_not_found) { |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 964 | ri = (Cache::pending_req_info*)calloc(1, sizeof(Cache::pending_req_info)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 965 | if (ri) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 966 | ri->hash = key->hash; |
| 967 | prev->next = ri; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 968 | } |
| 969 | } |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 970 | return false; |
| 971 | } |
| 972 | |
| 973 | // Notify all threads that the cache entry |key| has become available |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 974 | static void cache_notify_waiting_tid_locked(struct Cache* cache, const Entry* key) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 975 | if (!cache || !key) return; |
| 976 | |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 977 | Cache::pending_req_info* ri = cache->pending_requests.next; |
| 978 | Cache::pending_req_info* prev = &cache->pending_requests; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 979 | while (ri) { |
| 980 | if (ri->hash == key->hash) { |
| 981 | // remove item from list and destroy |
| 982 | prev->next = ri->next; |
| 983 | free(ri); |
| 984 | cv.notify_all(); |
| 985 | return; |
| 986 | } |
| 987 | prev = ri; |
| 988 | ri = ri->next; |
| 989 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 990 | } |
| 991 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 992 | void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen, uint32_t flags) { |
| 993 | // We should not notify with these flags. |
| 994 | if (flags & (ANDROID_RESOLV_NO_CACHE_STORE | ANDROID_RESOLV_NO_CACHE_LOOKUP)) { |
| 995 | return; |
| 996 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 997 | Entry key[1]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 998 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 999 | if (!entry_init_key(key, query, querylen)) return; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1000 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1001 | std::lock_guard guard(cache_mutex); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1002 | |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1003 | Cache* cache = find_named_cache_locked(netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1004 | |
| 1005 | if (cache) { |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1006 | cache_notify_waiting_tid_locked(cache, key); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1007 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1008 | } |
| 1009 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 1010 | static void cache_dump_mru_locked(Cache* cache) { |
| 1011 | std::string buf; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1012 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 1013 | StringAppendF(&buf, "MRU LIST (%2d): ", cache->num_entries); |
| 1014 | for (Entry* e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) { |
| 1015 | StringAppendF(&buf, " %d", e->id); |
| 1016 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1017 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 1018 | LOG(INFO) << __func__ << ": " << buf; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1019 | } |
| 1020 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1021 | /* This function tries to find a key within the hash table |
| 1022 | * In case of success, it will return a *pointer* to the hashed key. |
| 1023 | * In case of failure, it will return a *pointer* to NULL |
| 1024 | * |
| 1025 | * So, the caller must check '*result' to check for success/failure. |
| 1026 | * |
| 1027 | * The main idea is that the result can later be used directly in |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1028 | * calls to resolv_cache_add or _resolv_cache_remove as the 'lookup' |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1029 | * parameter. This makes the code simpler and avoids re-searching |
| 1030 | * for the key position in the htable. |
| 1031 | * |
| 1032 | * The result of a lookup_p is only valid until you alter the hash |
| 1033 | * table. |
| 1034 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1035 | static Entry** _cache_lookup_p(Cache* cache, Entry* key) { |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 1036 | int index = key->hash % CONFIG_MAX_ENTRIES; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1037 | Entry** pnode = (Entry**) &cache->entries[index]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1038 | |
| 1039 | while (*pnode != NULL) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1040 | Entry* node = *pnode; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1041 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1042 | if (node == NULL) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1043 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1044 | if (node->hash == key->hash && entry_equals(node, key)) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1045 | |
| 1046 | pnode = &node->hlink; |
| 1047 | } |
| 1048 | return pnode; |
| 1049 | } |
| 1050 | |
| 1051 | /* Add a new entry to the hash table. 'lookup' must be the |
| 1052 | * result of an immediate previous failed _lookup_p() call |
| 1053 | * (i.e. with *lookup == NULL), and 'e' is the pointer to the |
| 1054 | * newly created entry |
| 1055 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1056 | static void _cache_add_p(Cache* cache, Entry** lookup, Entry* e) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1057 | *lookup = e; |
| 1058 | e->id = ++cache->last_id; |
| 1059 | entry_mru_add(e, &cache->mru_list); |
| 1060 | cache->num_entries += 1; |
| 1061 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1062 | LOG(INFO) << __func__ << ": entry " << e->id << " added (count=" << cache->num_entries << ")"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* Remove an existing entry from the hash table, |
| 1066 | * 'lookup' must be the result of an immediate previous |
| 1067 | * and succesful _lookup_p() call. |
| 1068 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1069 | static void _cache_remove_p(Cache* cache, Entry** lookup) { |
| 1070 | Entry* e = *lookup; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1071 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1072 | LOG(INFO) << __func__ << ": entry " << e->id << " removed (count=" << cache->num_entries - 1 |
| 1073 | << ")"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1074 | |
| 1075 | entry_mru_remove(e); |
| 1076 | *lookup = e->hlink; |
| 1077 | entry_free(e); |
| 1078 | cache->num_entries -= 1; |
| 1079 | } |
| 1080 | |
| 1081 | /* Remove the oldest entry from the hash table. |
| 1082 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1083 | static void _cache_remove_oldest(Cache* cache) { |
| 1084 | Entry* oldest = cache->mru_list.mru_prev; |
| 1085 | Entry** lookup = _cache_lookup_p(cache, oldest); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1086 | |
| 1087 | if (*lookup == NULL) { /* should not happen */ |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1088 | LOG(INFO) << __func__ << ": OLDEST NOT IN HTABLE ?"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1089 | return; |
| 1090 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1091 | LOG(INFO) << __func__ << ": Cache full - removing oldest"; |
Mike Yu | fbfc27d | 2019-08-20 17:42:51 +0800 | [diff] [blame] | 1092 | res_pquery(oldest->query, oldest->querylen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1093 | _cache_remove_p(cache, lookup); |
| 1094 | } |
| 1095 | |
| 1096 | /* Remove all expired entries from the hash table. |
| 1097 | */ |
| 1098 | static void _cache_remove_expired(Cache* cache) { |
| 1099 | Entry* e; |
| 1100 | time_t now = _time_now(); |
| 1101 | |
| 1102 | for (e = cache->mru_list.mru_next; e != &cache->mru_list;) { |
| 1103 | // Entry is old, remove |
| 1104 | if (now >= e->expires) { |
| 1105 | Entry** lookup = _cache_lookup_p(cache, e); |
| 1106 | if (*lookup == NULL) { /* should not happen */ |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1107 | LOG(INFO) << __func__ << ": ENTRY NOT IN HTABLE ?"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1108 | return; |
| 1109 | } |
| 1110 | e = e->mru_next; |
| 1111 | _cache_remove_p(cache, lookup); |
| 1112 | } else { |
| 1113 | e = e->mru_next; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1118 | // gets a resolv_cache_info associated with a network, or NULL if not found |
| 1119 | static resolv_cache_info* find_cache_info_locked(unsigned netid) REQUIRES(cache_mutex); |
| 1120 | |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1121 | ResolvCacheStatus resolv_cache_lookup(unsigned netid, const void* query, int querylen, void* answer, |
| 1122 | int answersize, int* answerlen, uint32_t flags) { |
Luke Huang | a1d7418 | 2019-03-19 17:30:36 +0800 | [diff] [blame] | 1123 | // Skip cache lookup, return RESOLV_CACHE_NOTFOUND directly so that it is |
| 1124 | // possible to cache the answer of this query. |
Luke Huang | 40f5f49 | 2019-05-09 19:37:35 +0800 | [diff] [blame] | 1125 | // If ANDROID_RESOLV_NO_CACHE_STORE is set, return RESOLV_CACHE_SKIP to skip possible cache |
| 1126 | // storing. |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1127 | if (flags & ANDROID_RESOLV_NO_CACHE_LOOKUP) { |
Luke Huang | 40f5f49 | 2019-05-09 19:37:35 +0800 | [diff] [blame] | 1128 | return flags & ANDROID_RESOLV_NO_CACHE_STORE ? RESOLV_CACHE_SKIP : RESOLV_CACHE_NOTFOUND; |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1129 | } |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1130 | Entry key; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1131 | Entry** lookup; |
| 1132 | Entry* e; |
| 1133 | time_t now; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1134 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1135 | LOG(INFO) << __func__ << ": lookup"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1136 | |
| 1137 | /* we don't cache malformed queries */ |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1138 | if (!entry_init_key(&key, query, querylen)) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1139 | LOG(INFO) << __func__ << ": unsupported query"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1140 | return RESOLV_CACHE_UNSUPPORTED; |
| 1141 | } |
| 1142 | /* lookup cache */ |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1143 | std::unique_lock lock(cache_mutex); |
Mike Yu | 411ea7f | 2019-09-17 11:10:08 +0800 | [diff] [blame] | 1144 | android::base::ScopedLockAssertion assume_lock(cache_mutex); |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1145 | Cache* cache = find_named_cache_locked(netid); |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1146 | if (cache == nullptr) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1147 | return RESOLV_CACHE_UNSUPPORTED; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /* see the description of _lookup_p to understand this. |
| 1151 | * the function always return a non-NULL pointer. |
| 1152 | */ |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1153 | lookup = _cache_lookup_p(cache, &key); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1154 | e = *lookup; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1155 | |
| 1156 | if (e == NULL) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1157 | LOG(INFO) << __func__ << ": NOT IN CACHE"; |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1158 | // If it is no-cache-store mode, we won't wait for possible query. |
| 1159 | if (flags & ANDROID_RESOLV_NO_CACHE_STORE) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1160 | return RESOLV_CACHE_SKIP; |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1161 | } |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1162 | |
| 1163 | if (!cache_has_pending_request_locked(cache, &key, true)) { |
| 1164 | return RESOLV_CACHE_NOTFOUND; |
| 1165 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1166 | } else { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1167 | LOG(INFO) << __func__ << ": Waiting for previous request"; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1168 | // wait until (1) timeout OR |
| 1169 | // (2) cv is notified AND no pending request matching the |key| |
| 1170 | // (cv notifier should delete pending request before sending notification.) |
| 1171 | bool ret = cv.wait_for(lock, std::chrono::seconds(PENDING_REQUEST_TIMEOUT), |
| 1172 | [netid, &cache, &key]() REQUIRES(cache_mutex) { |
| 1173 | // Must update cache as it could have been deleted |
| 1174 | cache = find_named_cache_locked(netid); |
| 1175 | return !cache_has_pending_request_locked(cache, &key, false); |
| 1176 | }); |
Ken Chen | 553b117 | 2019-02-21 03:50:11 +0800 | [diff] [blame] | 1177 | if (!cache) { |
| 1178 | return RESOLV_CACHE_NOTFOUND; |
| 1179 | } |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1180 | if (ret == false) { |
| 1181 | resolv_cache_info* info = find_cache_info_locked(netid); |
| 1182 | if (info != NULL) { |
| 1183 | info->wait_for_pending_req_timeout_count++; |
| 1184 | } |
| 1185 | } |
| 1186 | lookup = _cache_lookup_p(cache, &key); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1187 | e = *lookup; |
| 1188 | if (e == NULL) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1189 | return RESOLV_CACHE_NOTFOUND; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | now = _time_now(); |
| 1195 | |
| 1196 | /* remove stale entries here */ |
| 1197 | if (now >= e->expires) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1198 | LOG(INFO) << __func__ << ": NOT IN CACHE (STALE ENTRY " << *lookup << "DISCARDED)"; |
Mike Yu | fbfc27d | 2019-08-20 17:42:51 +0800 | [diff] [blame] | 1199 | res_pquery(e->query, e->querylen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1200 | _cache_remove_p(cache, lookup); |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1201 | return RESOLV_CACHE_NOTFOUND; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | *answerlen = e->answerlen; |
| 1205 | if (e->answerlen > answersize) { |
| 1206 | /* NOTE: we return UNSUPPORTED if the answer buffer is too short */ |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1207 | LOG(INFO) << __func__ << ": ANSWER TOO LONG"; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1208 | return RESOLV_CACHE_UNSUPPORTED; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1209 | } |
| 1210 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1211 | memcpy(answer, e->answer, e->answerlen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1212 | |
| 1213 | /* bump up this entry to the top of the MRU list */ |
| 1214 | if (e != cache->mru_list.mru_next) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1215 | entry_mru_remove(e); |
| 1216 | entry_mru_add(e, &cache->mru_list); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1217 | } |
| 1218 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1219 | LOG(INFO) << __func__ << ": FOUND IN CACHE entry=" << e; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1220 | return RESOLV_CACHE_FOUND; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1221 | } |
| 1222 | |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1223 | int resolv_cache_add(unsigned netid, const void* query, int querylen, const void* answer, |
| 1224 | int answerlen) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1225 | Entry key[1]; |
| 1226 | Entry* e; |
| 1227 | Entry** lookup; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 1228 | uint32_t ttl; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1229 | Cache* cache = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1230 | |
| 1231 | /* don't assume that the query has already been cached |
| 1232 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1233 | if (!entry_init_key(key, query, querylen)) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1234 | LOG(INFO) << __func__ << ": passed invalid query?"; |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1235 | return -EINVAL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1236 | } |
| 1237 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1238 | std::lock_guard guard(cache_mutex); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1239 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1240 | cache = find_named_cache_locked(netid); |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1241 | if (cache == nullptr) { |
| 1242 | return -ENONET; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1243 | } |
| 1244 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1245 | lookup = _cache_lookup_p(cache, key); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1246 | e = *lookup; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1247 | |
Luke Huang | a1d7418 | 2019-03-19 17:30:36 +0800 | [diff] [blame] | 1248 | // Should only happen on ANDROID_RESOLV_NO_CACHE_LOOKUP |
| 1249 | if (e != NULL) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1250 | LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD"; |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1251 | cache_notify_waiting_tid_locked(cache, key); |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1252 | return -EEXIST; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1253 | } |
| 1254 | |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 1255 | if (cache->num_entries >= CONFIG_MAX_ENTRIES) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1256 | _cache_remove_expired(cache); |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 1257 | if (cache->num_entries >= CONFIG_MAX_ENTRIES) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1258 | _cache_remove_oldest(cache); |
| 1259 | } |
Luke Huang | a1d7418 | 2019-03-19 17:30:36 +0800 | [diff] [blame] | 1260 | // TODO: It looks useless, remove below code after having test to prove it. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1261 | lookup = _cache_lookup_p(cache, key); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1262 | e = *lookup; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1263 | if (e != NULL) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1264 | LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD"; |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1265 | cache_notify_waiting_tid_locked(cache, key); |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1266 | return -EEXIST; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | ttl = answer_getTTL(answer, answerlen); |
| 1271 | if (ttl > 0) { |
| 1272 | e = entry_alloc(key, answer, answerlen); |
| 1273 | if (e != NULL) { |
| 1274 | e->expires = ttl + _time_now(); |
| 1275 | _cache_add_p(cache, lookup, e); |
| 1276 | } |
| 1277 | } |
Bernie Innocenti | 9f05f5e | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1278 | |
Mike Yu | 34433c6 | 2019-08-20 20:17:36 +0800 | [diff] [blame] | 1279 | cache_dump_mru_locked(cache); |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1280 | cache_notify_waiting_tid_locked(cache, key); |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1281 | |
| 1282 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1283 | } |
| 1284 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1285 | // Head of the list of caches. |
| 1286 | static struct resolv_cache_info res_cache_list GUARDED_BY(cache_mutex); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1287 | |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1288 | // insert resolv_cache_info into the list of resolv_cache_infos |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1289 | static void insert_cache_info_locked(resolv_cache_info* cache_info); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1290 | // creates a resolv_cache_info |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1291 | static resolv_cache_info* create_cache_info(); |
Bernie Innocenti | 863e39b | 2019-08-28 15:46:58 +0900 | [diff] [blame] | 1292 | // Clears nameservers set for |cache_info| and clears the stats |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1293 | static void free_nameservers_locked(resolv_cache_info* cache_info); |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1294 | // Order-insensitive comparison for the two set of servers. |
| 1295 | static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers, |
| 1296 | const std::vector<std::string>& newServers); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1297 | // clears the stats samples contained withing the given cache_info |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1298 | static void res_cache_clear_stats_locked(resolv_cache_info* cache_info); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1299 | |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1300 | // public API for netd to query if name server is set on specific netid |
| 1301 | bool resolv_has_nameservers(unsigned netid) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1302 | std::lock_guard guard(cache_mutex); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1303 | resolv_cache_info* info = find_cache_info_locked(netid); |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1304 | return (info != nullptr) && (info->nscount > 0); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1305 | } |
| 1306 | |
Luke Huang | e126fbe | 2019-07-20 17:36:30 +0800 | [diff] [blame] | 1307 | namespace { |
| 1308 | |
| 1309 | // Map format: ReturnCode:rate_denom |
| 1310 | // if the ReturnCode is not associated with any rate_denom, use default |
| 1311 | // Sampling rate varies by return code; events to log are chosen randomly, with a |
| 1312 | // probability proportional to the sampling rate. |
| 1313 | constexpr const char DEFAULT_SUBSAMPLING_MAP[] = "default:1 0:100 7:10"; |
| 1314 | |
| 1315 | std::unordered_map<int, uint32_t> resolv_get_dns_event_subsampling_map() { |
| 1316 | using android::base::ParseInt; |
| 1317 | using android::base::ParseUint; |
| 1318 | using android::base::Split; |
| 1319 | using server_configurable_flags::GetServerConfigurableFlag; |
| 1320 | std::unordered_map<int, uint32_t> sampling_rate_map{}; |
| 1321 | std::vector<std::string> subsampling_vector = |
| 1322 | Split(GetServerConfigurableFlag("netd_native", "dns_event_subsample_map", |
| 1323 | DEFAULT_SUBSAMPLING_MAP), |
| 1324 | " "); |
| 1325 | for (const auto& pair : subsampling_vector) { |
| 1326 | std::vector<std::string> rate_denom = Split(pair, ":"); |
| 1327 | int return_code; |
| 1328 | uint32_t denom; |
| 1329 | if (rate_denom.size() != 2) { |
| 1330 | LOG(ERROR) << __func__ << ": invalid subsampling_pair = " << pair; |
| 1331 | continue; |
| 1332 | } |
| 1333 | if (rate_denom[0] == "default") { |
| 1334 | return_code = DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY; |
| 1335 | } else if (!ParseInt(rate_denom[0], &return_code)) { |
| 1336 | LOG(ERROR) << __func__ << ": parse subsampling_pair failed = " << pair; |
| 1337 | continue; |
| 1338 | } |
| 1339 | if (!ParseUint(rate_denom[1], &denom)) { |
| 1340 | LOG(ERROR) << __func__ << ": parse subsampling_pair failed = " << pair; |
| 1341 | continue; |
| 1342 | } |
| 1343 | sampling_rate_map[return_code] = denom; |
| 1344 | } |
| 1345 | return sampling_rate_map; |
| 1346 | } |
| 1347 | |
| 1348 | } // namespace |
| 1349 | |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 1350 | int resolv_create_cache_for_net(unsigned netid) { |
| 1351 | std::lock_guard guard(cache_mutex); |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1352 | Cache* cache = find_named_cache_locked(netid); |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1353 | // Should not happen |
| 1354 | if (cache) { |
| 1355 | LOG(ERROR) << __func__ << ": Cache is already created, netId: " << netid; |
| 1356 | return -EEXIST; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1357 | } |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1358 | |
| 1359 | resolv_cache_info* cache_info = create_cache_info(); |
| 1360 | if (!cache_info) return -ENOMEM; |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1361 | cache_info->netid = netid; |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 1362 | cache_info->cache = new Cache; |
Luke Huang | e126fbe | 2019-07-20 17:36:30 +0800 | [diff] [blame] | 1363 | cache_info->dns_event_subsampling_map = resolv_get_dns_event_subsampling_map(); |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1364 | insert_cache_info_locked(cache_info); |
| 1365 | |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1369 | void resolv_delete_cache_for_net(unsigned netid) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1370 | std::lock_guard guard(cache_mutex); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1371 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1372 | struct resolv_cache_info* prev_cache_info = &res_cache_list; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1373 | |
| 1374 | while (prev_cache_info->next) { |
| 1375 | struct resolv_cache_info* cache_info = prev_cache_info->next; |
| 1376 | |
| 1377 | if (cache_info->netid == netid) { |
| 1378 | prev_cache_info->next = cache_info->next; |
Bernie Innocenti | c4f469a | 2019-10-03 15:50:06 +0900 | [diff] [blame] | 1379 | delete cache_info->cache; |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1380 | free_nameservers_locked(cache_info); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1381 | free(cache_info); |
| 1382 | break; |
| 1383 | } |
| 1384 | |
| 1385 | prev_cache_info = prev_cache_info->next; |
| 1386 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1387 | } |
| 1388 | |
Bernie Innocenti | a4be3c8 | 2019-03-28 19:25:11 +0900 | [diff] [blame] | 1389 | std::vector<unsigned> resolv_list_caches() { |
| 1390 | std::lock_guard guard(cache_mutex); |
| 1391 | struct resolv_cache_info* cache_info = res_cache_list.next; |
| 1392 | std::vector<unsigned> result; |
| 1393 | while (cache_info) { |
| 1394 | result.push_back(cache_info->netid); |
| 1395 | cache_info = cache_info->next; |
| 1396 | } |
| 1397 | return result; |
| 1398 | } |
| 1399 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1400 | static resolv_cache_info* create_cache_info() { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1401 | return (struct resolv_cache_info*) calloc(sizeof(struct resolv_cache_info), 1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1402 | } |
| 1403 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1404 | // TODO: convert this to a simple and efficient C++ container. |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1405 | static void insert_cache_info_locked(struct resolv_cache_info* cache_info) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1406 | struct resolv_cache_info* last; |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1407 | for (last = &res_cache_list; last->next; last = last->next) {} |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1408 | last->next = cache_info; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1409 | } |
| 1410 | |
Bernie Innocenti | 802eccd | 2019-10-03 15:11:22 +0900 | [diff] [blame] | 1411 | static Cache* find_named_cache_locked(unsigned netid) { |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1412 | resolv_cache_info* info = find_cache_info_locked(netid); |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1413 | if (info != nullptr) return info->cache; |
| 1414 | return nullptr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1415 | } |
| 1416 | |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1417 | static resolv_cache_info* find_cache_info_locked(unsigned netid) { |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1418 | struct resolv_cache_info* cache_info = res_cache_list.next; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1419 | |
| 1420 | while (cache_info) { |
| 1421 | if (cache_info->netid == netid) { |
| 1422 | break; |
| 1423 | } |
| 1424 | |
| 1425 | cache_info = cache_info->next; |
| 1426 | } |
| 1427 | return cache_info; |
| 1428 | } |
| 1429 | |
waynema | a74195e | 2019-01-18 14:02:31 +0800 | [diff] [blame] | 1430 | static void resolv_set_experiment_params(res_params* params) { |
| 1431 | using android::base::ParseInt; |
| 1432 | using server_configurable_flags::GetServerConfigurableFlag; |
| 1433 | |
| 1434 | if (params->retry_count == 0) { |
| 1435 | params->retry_count = RES_DFLRETRY; |
| 1436 | ParseInt(GetServerConfigurableFlag("netd_native", "retry_count", ""), ¶ms->retry_count); |
| 1437 | } |
| 1438 | |
| 1439 | if (params->base_timeout_msec == 0) { |
| 1440 | params->base_timeout_msec = RES_TIMEOUT; |
| 1441 | ParseInt(GetServerConfigurableFlag("netd_native", "retransmission_time_interval", ""), |
| 1442 | ¶ms->base_timeout_msec); |
| 1443 | } |
| 1444 | } |
| 1445 | |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1446 | namespace { |
| 1447 | |
| 1448 | // Returns valid domains without duplicates which are limited to max size |MAXDNSRCH|. |
| 1449 | std::vector<std::string> filter_domains(const std::vector<std::string>& domains) { |
| 1450 | std::set<std::string> tmp_set; |
| 1451 | std::vector<std::string> res; |
| 1452 | |
| 1453 | std::copy_if(domains.begin(), domains.end(), std::back_inserter(res), |
| 1454 | [&tmp_set](const std::string& str) { |
| 1455 | return !(str.size() > MAXDNSRCHPATH - 1) && (tmp_set.insert(str).second); |
| 1456 | }); |
| 1457 | if (res.size() > MAXDNSRCH) { |
| 1458 | LOG(WARNING) << __func__ << ": valid domains=" << res.size() |
| 1459 | << ", but MAXDNSRCH=" << MAXDNSRCH; |
| 1460 | res.resize(MAXDNSRCH); |
| 1461 | } |
| 1462 | return res; |
| 1463 | } |
| 1464 | |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1465 | std::vector<std::string> filter_nameservers(const std::vector<std::string>& servers) { |
| 1466 | std::vector<std::string> res = servers; |
| 1467 | if (res.size() > MAXNS) { |
| 1468 | LOG(WARNING) << __func__ << ": too many servers: " << res.size(); |
| 1469 | res.resize(MAXNS); |
| 1470 | } |
| 1471 | return res; |
| 1472 | } |
| 1473 | |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1474 | } // namespace |
| 1475 | |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1476 | int resolv_set_nameservers(unsigned netid, const std::vector<std::string>& servers, |
| 1477 | const std::vector<std::string>& domains, const res_params& params) { |
| 1478 | std::vector<std::string> nameservers = filter_nameservers(servers); |
| 1479 | const int numservers = static_cast<int>(nameservers.size()); |
| 1480 | |
| 1481 | LOG(INFO) << __func__ << ": netId = " << netid << ", numservers = " << numservers; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1482 | |
| 1483 | // Parse the addresses before actually locking or changing any state, in case there is an error. |
| 1484 | // As a side effect this also reduces the time the lock is kept. |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1485 | // TODO: find a better way to replace addrinfo*, something like std::vector<SafeAddrinfo> |
| 1486 | addrinfo* nsaddrinfo[MAXNS]; |
Bernie Innocenti | b102dd2 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 1487 | for (int i = 0; i < numservers; i++) { |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1488 | // The addrinfo structures allocated here are freed in free_nameservers_locked(). |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1489 | const addrinfo hints = { |
| 1490 | .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST}; |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1491 | const int rt = getaddrinfo_numeric(nameservers[i].c_str(), "53", hints, &nsaddrinfo[i]); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1492 | if (rt != 0) { |
Bernie Innocenti | b102dd2 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 1493 | for (int j = 0; j < i; j++) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1494 | freeaddrinfo(nsaddrinfo[j]); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1495 | } |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1496 | LOG(INFO) << __func__ << ": getaddrinfo_numeric(" << nameservers[i] |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1497 | << ") = " << gai_strerror(rt); |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1498 | return -EINVAL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1502 | std::lock_guard guard(cache_mutex); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1503 | resolv_cache_info* cache_info = find_cache_info_locked(netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1504 | |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1505 | if (cache_info == nullptr) return -ENONET; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1506 | |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1507 | uint8_t old_max_samples = cache_info->params.max_samples; |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1508 | cache_info->params = params; |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1509 | resolv_set_experiment_params(&cache_info->params); |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1510 | if (!resolv_is_nameservers_equal(cache_info->nameservers, nameservers)) { |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1511 | // free current before adding new |
| 1512 | free_nameservers_locked(cache_info); |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1513 | cache_info->nameservers = std::move(nameservers); |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1514 | for (int i = 0; i < numservers; i++) { |
| 1515 | cache_info->nsaddrinfo[i] = nsaddrinfo[i]; |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1516 | LOG(INFO) << __func__ << ": netid = " << netid |
| 1517 | << ", addr = " << cache_info->nameservers[i]; |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1518 | } |
| 1519 | cache_info->nscount = numservers; |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1520 | } else { |
| 1521 | if (cache_info->params.max_samples != old_max_samples) { |
| 1522 | // If the maximum number of samples changes, the overhead of keeping the most recent |
| 1523 | // samples around is not considered worth the effort, so they are cleared instead. |
| 1524 | // All other parameters do not affect shared state: Changing these parameters does |
| 1525 | // not invalidate the samples, as they only affect aggregation and the conditions |
| 1526 | // under which servers are considered usable. |
| 1527 | res_cache_clear_stats_locked(cache_info); |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 1528 | } |
| 1529 | for (int j = 0; j < numservers; j++) { |
| 1530 | freeaddrinfo(nsaddrinfo[j]); |
| 1531 | } |
| 1532 | } |
| 1533 | |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1534 | // Always update the search paths. Cache-flushing however is not necessary, |
| 1535 | // since the stored cache entries do contain the domain, not just the host name. |
| 1536 | cache_info->search_domains = filter_domains(domains); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1537 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1538 | return 0; |
| 1539 | } |
| 1540 | |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1541 | static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers, |
| 1542 | const std::vector<std::string>& newServers) { |
| 1543 | const std::set<std::string> olds(oldServers.begin(), oldServers.end()); |
| 1544 | const std::set<std::string> news(newServers.begin(), newServers.end()); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1545 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1546 | // TODO: this is incorrect if the list of current or previous nameservers |
| 1547 | // contains duplicates. This does not really matter because the framework |
| 1548 | // filters out duplicates, but we should probably fix it. It's also |
| 1549 | // insensitive to the order of the nameservers; we should probably fix that |
| 1550 | // too. |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1551 | return olds == news; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1552 | } |
| 1553 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1554 | static void free_nameservers_locked(resolv_cache_info* cache_info) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1555 | int i; |
| 1556 | for (i = 0; i < cache_info->nscount; i++) { |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1557 | cache_info->nameservers.clear(); |
Bernie Innocenti | 863e39b | 2019-08-28 15:46:58 +0900 | [diff] [blame] | 1558 | if (cache_info->nsaddrinfo[i] != nullptr) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1559 | freeaddrinfo(cache_info->nsaddrinfo[i]); |
Bernie Innocenti | 863e39b | 2019-08-28 15:46:58 +0900 | [diff] [blame] | 1560 | cache_info->nsaddrinfo[i] = nullptr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1561 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1562 | } |
| 1563 | cache_info->nscount = 0; |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1564 | res_cache_clear_stats_locked(cache_info); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1565 | } |
| 1566 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1567 | void _resolv_populate_res_for_net(res_state statp) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1568 | if (statp == NULL) { |
| 1569 | return; |
| 1570 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1571 | LOG(INFO) << __func__ << ": netid=" << statp->netid; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1572 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1573 | std::lock_guard guard(cache_mutex); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1574 | resolv_cache_info* info = find_cache_info_locked(statp->netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1575 | if (info != NULL) { |
| 1576 | int nserv; |
| 1577 | struct addrinfo* ai; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1578 | for (nserv = 0; nserv < MAXNS; nserv++) { |
| 1579 | ai = info->nsaddrinfo[nserv]; |
| 1580 | if (ai == NULL) { |
| 1581 | break; |
| 1582 | } |
| 1583 | |
Bernie Innocenti | c977ab7 | 2019-10-09 00:34:06 +0900 | [diff] [blame] | 1584 | if ((size_t)ai->ai_addrlen <= sizeof(statp->nsaddrs[0])) { |
| 1585 | memcpy(&statp->nsaddrs[nserv], ai->ai_addr, ai->ai_addrlen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1586 | } else { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1587 | LOG(INFO) << __func__ << ": found too long addrlen"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | statp->nscount = nserv; |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1591 | statp->search_domains = info->search_domains; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1592 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* Resolver reachability statistics. */ |
| 1596 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1597 | static void _res_cache_add_stats_sample_locked(res_stats* stats, const res_sample* sample, |
| 1598 | int max_samples) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1599 | // Note: This function expects max_samples > 0, otherwise a (harmless) modification of the |
| 1600 | // allocated but supposedly unused memory for samples[0] will happen |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1601 | LOG(INFO) << __func__ << ": adding sample to stats, next = " << unsigned(stats->sample_next) |
| 1602 | << ", count = " << unsigned(stats->sample_count); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1603 | stats->samples[stats->sample_next] = *sample; |
| 1604 | if (stats->sample_count < max_samples) { |
| 1605 | ++stats->sample_count; |
| 1606 | } |
| 1607 | if (++stats->sample_next >= max_samples) { |
| 1608 | stats->sample_next = 0; |
| 1609 | } |
| 1610 | } |
| 1611 | |
Bernie Innocenti | 68bb7aa | 2018-09-27 13:44:29 +0900 | [diff] [blame] | 1612 | static void res_cache_clear_stats_locked(resolv_cache_info* cache_info) { |
Bernie Innocenti | 863e39b | 2019-08-28 15:46:58 +0900 | [diff] [blame] | 1613 | for (int i = 0; i < MAXNS; ++i) { |
| 1614 | cache_info->nsstats[i].sample_count = 0; |
| 1615 | cache_info->nsstats[i].sample_next = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1616 | } |
Bernie Innocenti | 863e39b | 2019-08-28 15:46:58 +0900 | [diff] [blame] | 1617 | |
| 1618 | // Increment the revision id to ensure that sample state is not written back if the |
| 1619 | // servers change; in theory it would suffice to do so only if the servers or |
| 1620 | // max_samples actually change, in practice the overhead of checking is higher than the |
| 1621 | // cost, and overflows are unlikely. |
| 1622 | ++cache_info->revision_id; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1623 | } |
| 1624 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1625 | int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount, |
| 1626 | struct sockaddr_storage servers[MAXNS], int* dcount, |
| 1627 | char domains[MAXDNSRCH][MAXDNSRCHPATH], |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 1628 | res_params* params, struct res_stats stats[MAXNS], |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1629 | int* wait_for_pending_req_timeout_count) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1630 | int revision_id = -1; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1631 | std::lock_guard guard(cache_mutex); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1632 | |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1633 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1634 | if (info) { |
| 1635 | if (info->nscount > MAXNS) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1636 | LOG(INFO) << __func__ << ": nscount " << info->nscount << " > MAXNS " << MAXNS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1637 | errno = EFAULT; |
| 1638 | return -1; |
| 1639 | } |
| 1640 | int i; |
| 1641 | for (i = 0; i < info->nscount; i++) { |
| 1642 | // Verify that the following assumptions are held, failure indicates corruption: |
| 1643 | // - getaddrinfo() may never return a sockaddr > sockaddr_storage |
| 1644 | // - all addresses are valid |
| 1645 | // - there is only one address per addrinfo thanks to numeric resolution |
| 1646 | int addrlen = info->nsaddrinfo[i]->ai_addrlen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1647 | if (addrlen < (int) sizeof(struct sockaddr) || addrlen > (int) sizeof(servers[0])) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1648 | LOG(INFO) << __func__ << ": nsaddrinfo[" << i << "].ai_addrlen == " << addrlen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1649 | errno = EMSGSIZE; |
| 1650 | return -1; |
| 1651 | } |
| 1652 | if (info->nsaddrinfo[i]->ai_addr == NULL) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1653 | LOG(INFO) << __func__ << ": nsaddrinfo[" << i << "].ai_addr == NULL"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1654 | errno = ENOENT; |
| 1655 | return -1; |
| 1656 | } |
| 1657 | if (info->nsaddrinfo[i]->ai_next != NULL) { |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1658 | LOG(INFO) << __func__ << ": nsaddrinfo[" << i << "].ai_next != NULL"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1659 | errno = ENOTUNIQ; |
| 1660 | return -1; |
| 1661 | } |
| 1662 | } |
| 1663 | *nscount = info->nscount; |
| 1664 | for (i = 0; i < info->nscount; i++) { |
| 1665 | memcpy(&servers[i], info->nsaddrinfo[i]->ai_addr, info->nsaddrinfo[i]->ai_addrlen); |
| 1666 | stats[i] = info->nsstats[i]; |
| 1667 | } |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1668 | |
| 1669 | for (i = 0; i < static_cast<int>(info->search_domains.size()); i++) { |
| 1670 | strlcpy(domains[i], info->search_domains[i].c_str(), MAXDNSRCHPATH); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1671 | } |
| 1672 | *dcount = i; |
| 1673 | *params = info->params; |
| 1674 | revision_id = info->revision_id; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1675 | *wait_for_pending_req_timeout_count = info->wait_for_pending_req_timeout_count; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1676 | } |
| 1677 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1678 | return revision_id; |
| 1679 | } |
| 1680 | |
Luke Huang | e126fbe | 2019-07-20 17:36:30 +0800 | [diff] [blame] | 1681 | std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid) { |
| 1682 | using android::base::StringPrintf; |
| 1683 | std::lock_guard guard(cache_mutex); |
| 1684 | resolv_cache_info* cache_info = find_cache_info_locked(netid); |
| 1685 | if (cache_info == nullptr) return {}; |
| 1686 | std::vector<std::string> result; |
| 1687 | for (const auto& pair : cache_info->dns_event_subsampling_map) { |
| 1688 | result.push_back(StringPrintf("%s:%d", |
| 1689 | (pair.first == DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY) |
| 1690 | ? "default" |
| 1691 | : std::to_string(pair.first).c_str(), |
| 1692 | pair.second)); |
| 1693 | } |
| 1694 | return result; |
| 1695 | } |
| 1696 | |
| 1697 | // Decides whether an event should be sampled using a random number generator and |
| 1698 | // a sampling factor derived from the netid and the return code. |
| 1699 | // |
| 1700 | // Returns the subsampling rate if the event should be sampled, or 0 if it should be discarded. |
| 1701 | uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code) { |
| 1702 | std::lock_guard guard(cache_mutex); |
| 1703 | resolv_cache_info* cache_info = find_cache_info_locked(netid); |
| 1704 | if (cache_info == nullptr) return 0; // Don't log anything at all. |
| 1705 | const auto& subsampling_map = cache_info->dns_event_subsampling_map; |
| 1706 | auto search_returnCode = subsampling_map.find(return_code); |
| 1707 | uint32_t denom; |
| 1708 | if (search_returnCode != subsampling_map.end()) { |
| 1709 | denom = search_returnCode->second; |
| 1710 | } else { |
| 1711 | auto search_default = subsampling_map.find(DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY); |
| 1712 | denom = (search_default == subsampling_map.end()) ? 0 : search_default->second; |
| 1713 | } |
| 1714 | return denom; |
| 1715 | } |
| 1716 | |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 1717 | int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]) { |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1718 | std::lock_guard guard(cache_mutex); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1719 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1720 | if (info) { |
| 1721 | memcpy(stats, info->nsstats, sizeof(info->nsstats)); |
| 1722 | *params = info->params; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1723 | return info->revision_id; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1724 | } |
| 1725 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1726 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1727 | } |
| 1728 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1729 | void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns, |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 1730 | const res_sample* sample, int max_samples) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1731 | if (max_samples <= 0) return; |
| 1732 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1733 | std::lock_guard guard(cache_mutex); |
cken | 418cfb6 | 2018-12-01 17:45:18 +0900 | [diff] [blame] | 1734 | resolv_cache_info* info = find_cache_info_locked(netid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1735 | |
| 1736 | if (info && info->revision_id == revision_id) { |
| 1737 | _res_cache_add_stats_sample_locked(&info->nsstats[ns], sample, max_samples); |
| 1738 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1739 | } |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1740 | |
| 1741 | bool has_named_cache(unsigned netid) { |
| 1742 | std::lock_guard guard(cache_mutex); |
| 1743 | return find_named_cache_locked(netid) != nullptr; |
| 1744 | } |
| 1745 | |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1746 | int resolv_cache_get_expiration(unsigned netid, const std::vector<char>& query, |
| 1747 | time_t* expiration) { |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1748 | Entry key; |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1749 | *expiration = -1; |
| 1750 | |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1751 | // A malformed query is not allowed. |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1752 | if (!entry_init_key(&key, query.data(), query.size())) { |
| 1753 | LOG(WARNING) << __func__ << ": unsupported query"; |
| 1754 | return -EINVAL; |
| 1755 | } |
| 1756 | |
| 1757 | // lookup cache. |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1758 | Cache* cache; |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1759 | std::lock_guard guard(cache_mutex); |
| 1760 | if (cache = find_named_cache_locked(netid); cache == nullptr) { |
| 1761 | LOG(WARNING) << __func__ << ": cache not created in the network " << netid; |
| 1762 | return -ENONET; |
| 1763 | } |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 1764 | Entry** lookup = _cache_lookup_p(cache, &key); |
| 1765 | Entry* e = *lookup; |
Mike Yu | d1ec254 | 2019-06-06 17:17:53 +0800 | [diff] [blame] | 1766 | if (e == NULL) { |
| 1767 | LOG(WARNING) << __func__ << ": not in cache"; |
| 1768 | return -ENODATA; |
| 1769 | } |
| 1770 | |
| 1771 | if (_time_now() >= e->expires) { |
| 1772 | LOG(WARNING) << __func__ << ": entry expired"; |
| 1773 | return -ENODATA; |
| 1774 | } |
| 1775 | |
| 1776 | *expiration = e->expires; |
| 1777 | return 0; |
| 1778 | } |