The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [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 | |
| 29 | #include "resolv_cache.h" |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 30 | #include <resolv.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <time.h> |
| 34 | #include "pthread.h" |
| 35 | |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 36 | #include <errno.h> |
| 37 | #include "arpa_nameser.h" |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 38 | #include <sys/system_properties.h> |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 39 | #include <net/if.h> |
| 40 | #include <netdb.h> |
| 41 | #include <linux/if.h> |
| 42 | |
| 43 | #include <arpa/inet.h> |
| 44 | #include "resolv_private.h" |
David 'Digit' Turner | 208898e | 2012-01-13 14:24:08 +0100 | [diff] [blame^] | 45 | #include "resolv_iface.h" |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 46 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | /* This code implements a small and *simple* DNS resolver cache. |
| 48 | * |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 49 | * It is only used to cache DNS answers for a time defined by the smallest TTL |
| 50 | * among the answer records in order to reduce DNS traffic. It is not supposed |
| 51 | * to be a full DNS cache, since we plan to implement that in the future in a |
| 52 | * dedicated process running on the system. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | * |
| 54 | * Note that its design is kept simple very intentionally, i.e.: |
| 55 | * |
| 56 | * - it takes raw DNS query packet data as input, and returns raw DNS |
| 57 | * answer packet data as output |
| 58 | * |
| 59 | * (this means that two similar queries that encode the DNS name |
| 60 | * differently will be treated distinctly). |
| 61 | * |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 62 | * the smallest TTL value among the answer records are used as the time |
| 63 | * to keep an answer in the cache. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | * |
| 65 | * this is bad, but we absolutely want to avoid parsing the answer packets |
| 66 | * (and should be solved by the later full DNS cache process). |
| 67 | * |
| 68 | * - the implementation is just a (query-data) => (answer-data) hash table |
| 69 | * with a trivial least-recently-used expiration policy. |
| 70 | * |
| 71 | * Doing this keeps the code simple and avoids to deal with a lot of things |
| 72 | * that a full DNS cache is expected to do. |
| 73 | * |
| 74 | * The API is also very simple: |
| 75 | * |
| 76 | * - the client calls _resolv_cache_get() to obtain a handle to the cache. |
| 77 | * this will initialize the cache on first usage. the result can be NULL |
| 78 | * if the cache is disabled. |
| 79 | * |
| 80 | * - the client calls _resolv_cache_lookup() before performing a query |
| 81 | * |
| 82 | * if the function returns RESOLV_CACHE_FOUND, a copy of the answer data |
| 83 | * has been copied into the client-provided answer buffer. |
| 84 | * |
| 85 | * if the function returns RESOLV_CACHE_NOTFOUND, the client should perform |
| 86 | * a request normally, *then* call _resolv_cache_add() to add the received |
| 87 | * answer to the cache. |
| 88 | * |
| 89 | * if the function returns RESOLV_CACHE_UNSUPPORTED, the client should |
| 90 | * perform a request normally, and *not* call _resolv_cache_add() |
| 91 | * |
| 92 | * note that RESOLV_CACHE_UNSUPPORTED is also returned if the answer buffer |
| 93 | * is too short to accomodate the cached result. |
| 94 | * |
| 95 | * - when network settings change, the cache must be flushed since the list |
| 96 | * of DNS servers probably changed. this is done by calling |
| 97 | * _resolv_cache_reset() |
| 98 | * |
| 99 | * the parameter to this function must be an ever-increasing generation |
| 100 | * number corresponding to the current network settings state. |
| 101 | * |
| 102 | * This is done because several threads could detect the same network |
| 103 | * settings change (but at different times) and will all end up calling the |
| 104 | * same function. Comparing with the last used generation number ensures |
| 105 | * that the cache is only flushed once per network change. |
| 106 | */ |
| 107 | |
| 108 | /* the name of an environment variable that will be checked the first time |
| 109 | * this code is called if its value is "0", then the resolver cache is |
| 110 | * disabled. |
| 111 | */ |
| 112 | #define CONFIG_ENV "BIONIC_DNSCACHE" |
| 113 | |
| 114 | /* entries older than CONFIG_SECONDS seconds are always discarded. |
| 115 | */ |
| 116 | #define CONFIG_SECONDS (60*10) /* 10 minutes */ |
| 117 | |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 118 | /* default number of entries kept in the cache. This value has been |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | * determined by browsing through various sites and counting the number |
| 120 | * of corresponding requests. Keep in mind that our framework is currently |
| 121 | * performing two requests per name lookup (one for IPv4, the other for IPv6) |
| 122 | * |
| 123 | * www.google.com 4 |
| 124 | * www.ysearch.com 6 |
| 125 | * www.amazon.com 8 |
| 126 | * www.nytimes.com 22 |
| 127 | * www.espn.com 28 |
| 128 | * www.msn.com 28 |
| 129 | * www.lemonde.fr 35 |
| 130 | * |
| 131 | * (determined in 2009-2-17 from Paris, France, results may vary depending |
| 132 | * on location) |
| 133 | * |
| 134 | * most high-level websites use lots of media/ad servers with different names |
| 135 | * but these are generally reused when browsing through the site. |
| 136 | * |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 137 | * As such, a value of 64 should be relatively comfortable at the moment. |
| 138 | * |
| 139 | * The system property ro.net.dns_cache_size can be used to override the default |
| 140 | * value with a custom value |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 141 | */ |
| 142 | #define CONFIG_MAX_ENTRIES 64 |
| 143 | |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 144 | /* name of the system property that can be used to set the cache size */ |
| 145 | #define DNS_CACHE_SIZE_PROP_NAME "ro.net.dns_cache_size" |
| 146 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 147 | /****************************************************************************/ |
| 148 | /****************************************************************************/ |
| 149 | /***** *****/ |
| 150 | /***** *****/ |
| 151 | /***** *****/ |
| 152 | /****************************************************************************/ |
| 153 | /****************************************************************************/ |
| 154 | |
| 155 | /* set to 1 to debug cache operations */ |
| 156 | #define DEBUG 0 |
| 157 | |
| 158 | /* set to 1 to debug query data */ |
| 159 | #define DEBUG_DATA 0 |
| 160 | |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 161 | #undef XLOG |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | #if DEBUG |
| 163 | # include <logd.h> |
| 164 | # define XLOG(...) \ |
| 165 | __libc_android_log_print(ANDROID_LOG_DEBUG,"libc",__VA_ARGS__) |
| 166 | |
| 167 | #include <stdio.h> |
| 168 | #include <stdarg.h> |
| 169 | |
| 170 | /** BOUNDED BUFFER FORMATTING |
| 171 | **/ |
| 172 | |
| 173 | /* technical note: |
| 174 | * |
| 175 | * the following debugging routines are used to append data to a bounded |
| 176 | * buffer they take two parameters that are: |
| 177 | * |
| 178 | * - p : a pointer to the current cursor position in the buffer |
| 179 | * this value is initially set to the buffer's address. |
| 180 | * |
| 181 | * - end : the address of the buffer's limit, i.e. of the first byte |
| 182 | * after the buffer. this address should never be touched. |
| 183 | * |
| 184 | * IMPORTANT: it is assumed that end > buffer_address, i.e. |
| 185 | * that the buffer is at least one byte. |
| 186 | * |
| 187 | * the _bprint_() functions return the new value of 'p' after the data |
| 188 | * has been appended, and also ensure the following: |
| 189 | * |
| 190 | * - the returned value will never be strictly greater than 'end' |
| 191 | * |
| 192 | * - a return value equal to 'end' means that truncation occured |
| 193 | * (in which case, end[-1] will be set to 0) |
| 194 | * |
| 195 | * - after returning from a _bprint_() function, the content of the buffer |
| 196 | * is always 0-terminated, even in the event of truncation. |
| 197 | * |
| 198 | * these conventions allow you to call _bprint_ functions multiple times and |
| 199 | * only check for truncation at the end of the sequence, as in: |
| 200 | * |
| 201 | * char buff[1000], *p = buff, *end = p + sizeof(buff); |
| 202 | * |
| 203 | * p = _bprint_c(p, end, '"'); |
| 204 | * p = _bprint_s(p, end, my_string); |
| 205 | * p = _bprint_c(p, end, '"'); |
| 206 | * |
| 207 | * if (p >= end) { |
| 208 | * // buffer was too small |
| 209 | * } |
| 210 | * |
| 211 | * printf( "%s", buff ); |
| 212 | */ |
| 213 | |
| 214 | /* add a char to a bounded buffer */ |
| 215 | static char* |
| 216 | _bprint_c( char* p, char* end, int c ) |
| 217 | { |
| 218 | if (p < end) { |
| 219 | if (p+1 == end) |
| 220 | *p++ = 0; |
| 221 | else { |
| 222 | *p++ = (char) c; |
| 223 | *p = 0; |
| 224 | } |
| 225 | } |
| 226 | return p; |
| 227 | } |
| 228 | |
| 229 | /* add a sequence of bytes to a bounded buffer */ |
| 230 | static char* |
| 231 | _bprint_b( char* p, char* end, const char* buf, int len ) |
| 232 | { |
| 233 | int avail = end - p; |
| 234 | |
| 235 | if (avail <= 0 || len <= 0) |
| 236 | return p; |
| 237 | |
| 238 | if (avail > len) |
| 239 | avail = len; |
| 240 | |
| 241 | memcpy( p, buf, avail ); |
| 242 | p += avail; |
| 243 | |
| 244 | if (p < end) |
| 245 | p[0] = 0; |
| 246 | else |
| 247 | end[-1] = 0; |
| 248 | |
| 249 | return p; |
| 250 | } |
| 251 | |
| 252 | /* add a string to a bounded buffer */ |
| 253 | static char* |
| 254 | _bprint_s( char* p, char* end, const char* str ) |
| 255 | { |
| 256 | return _bprint_b(p, end, str, strlen(str)); |
| 257 | } |
| 258 | |
| 259 | /* add a formatted string to a bounded buffer */ |
| 260 | static char* |
| 261 | _bprint( char* p, char* end, const char* format, ... ) |
| 262 | { |
| 263 | int avail, n; |
| 264 | va_list args; |
| 265 | |
| 266 | avail = end - p; |
| 267 | |
| 268 | if (avail <= 0) |
| 269 | return p; |
| 270 | |
| 271 | va_start(args, format); |
David 'Digit' Turner | d378c68 | 2010-03-08 15:13:04 -0800 | [diff] [blame] | 272 | n = vsnprintf( p, avail, format, args); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 273 | va_end(args); |
| 274 | |
| 275 | /* certain C libraries return -1 in case of truncation */ |
| 276 | if (n < 0 || n > avail) |
| 277 | n = avail; |
| 278 | |
| 279 | p += n; |
| 280 | /* certain C libraries do not zero-terminate in case of truncation */ |
| 281 | if (p == end) |
| 282 | p[-1] = 0; |
| 283 | |
| 284 | return p; |
| 285 | } |
| 286 | |
| 287 | /* add a hex value to a bounded buffer, up to 8 digits */ |
| 288 | static char* |
| 289 | _bprint_hex( char* p, char* end, unsigned value, int numDigits ) |
| 290 | { |
| 291 | char text[sizeof(unsigned)*2]; |
| 292 | int nn = 0; |
| 293 | |
| 294 | while (numDigits-- > 0) { |
| 295 | text[nn++] = "0123456789abcdef"[(value >> (numDigits*4)) & 15]; |
| 296 | } |
| 297 | return _bprint_b(p, end, text, nn); |
| 298 | } |
| 299 | |
| 300 | /* add the hexadecimal dump of some memory area to a bounded buffer */ |
| 301 | static char* |
| 302 | _bprint_hexdump( char* p, char* end, const uint8_t* data, int datalen ) |
| 303 | { |
| 304 | int lineSize = 16; |
| 305 | |
| 306 | while (datalen > 0) { |
| 307 | int avail = datalen; |
| 308 | int nn; |
| 309 | |
| 310 | if (avail > lineSize) |
| 311 | avail = lineSize; |
| 312 | |
| 313 | for (nn = 0; nn < avail; nn++) { |
| 314 | if (nn > 0) |
| 315 | p = _bprint_c(p, end, ' '); |
| 316 | p = _bprint_hex(p, end, data[nn], 2); |
| 317 | } |
| 318 | for ( ; nn < lineSize; nn++ ) { |
| 319 | p = _bprint_s(p, end, " "); |
| 320 | } |
| 321 | p = _bprint_s(p, end, " "); |
| 322 | |
| 323 | for (nn = 0; nn < avail; nn++) { |
| 324 | int c = data[nn]; |
| 325 | |
| 326 | if (c < 32 || c > 127) |
| 327 | c = '.'; |
| 328 | |
| 329 | p = _bprint_c(p, end, c); |
| 330 | } |
| 331 | p = _bprint_c(p, end, '\n'); |
| 332 | |
| 333 | data += avail; |
| 334 | datalen -= avail; |
| 335 | } |
| 336 | return p; |
| 337 | } |
| 338 | |
| 339 | /* dump the content of a query of packet to the log */ |
| 340 | static void |
| 341 | XLOG_BYTES( const void* base, int len ) |
| 342 | { |
| 343 | char buff[1024]; |
| 344 | char* p = buff, *end = p + sizeof(buff); |
| 345 | |
| 346 | p = _bprint_hexdump(p, end, base, len); |
| 347 | XLOG("%s",buff); |
| 348 | } |
| 349 | |
| 350 | #else /* !DEBUG */ |
| 351 | # define XLOG(...) ((void)0) |
| 352 | # define XLOG_BYTES(a,b) ((void)0) |
| 353 | #endif |
| 354 | |
| 355 | static time_t |
| 356 | _time_now( void ) |
| 357 | { |
| 358 | struct timeval tv; |
| 359 | |
| 360 | gettimeofday( &tv, NULL ); |
| 361 | return tv.tv_sec; |
| 362 | } |
| 363 | |
| 364 | /* reminder: the general format of a DNS packet is the following: |
| 365 | * |
| 366 | * HEADER (12 bytes) |
| 367 | * QUESTION (variable) |
| 368 | * ANSWER (variable) |
| 369 | * AUTHORITY (variable) |
| 370 | * ADDITIONNAL (variable) |
| 371 | * |
| 372 | * the HEADER is made of: |
| 373 | * |
| 374 | * ID : 16 : 16-bit unique query identification field |
| 375 | * |
| 376 | * QR : 1 : set to 0 for queries, and 1 for responses |
| 377 | * Opcode : 4 : set to 0 for queries |
| 378 | * AA : 1 : set to 0 for queries |
| 379 | * TC : 1 : truncation flag, will be set to 0 in queries |
| 380 | * RD : 1 : recursion desired |
| 381 | * |
| 382 | * RA : 1 : recursion available (0 in queries) |
| 383 | * Z : 3 : three reserved zero bits |
| 384 | * RCODE : 4 : response code (always 0=NOERROR in queries) |
| 385 | * |
| 386 | * QDCount: 16 : question count |
| 387 | * ANCount: 16 : Answer count (0 in queries) |
| 388 | * NSCount: 16: Authority Record count (0 in queries) |
| 389 | * ARCount: 16: Additionnal Record count (0 in queries) |
| 390 | * |
| 391 | * the QUESTION is made of QDCount Question Record (QRs) |
| 392 | * the ANSWER is made of ANCount RRs |
| 393 | * the AUTHORITY is made of NSCount RRs |
| 394 | * the ADDITIONNAL is made of ARCount RRs |
| 395 | * |
| 396 | * Each Question Record (QR) is made of: |
| 397 | * |
| 398 | * QNAME : variable : Query DNS NAME |
| 399 | * TYPE : 16 : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255) |
| 400 | * CLASS : 16 : class of query (IN=1) |
| 401 | * |
| 402 | * Each Resource Record (RR) is made of: |
| 403 | * |
| 404 | * NAME : variable : DNS NAME |
| 405 | * TYPE : 16 : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255) |
| 406 | * CLASS : 16 : class of query (IN=1) |
| 407 | * TTL : 32 : seconds to cache this RR (0=none) |
| 408 | * RDLENGTH: 16 : size of RDDATA in bytes |
| 409 | * RDDATA : variable : RR data (depends on TYPE) |
| 410 | * |
| 411 | * Each QNAME contains a domain name encoded as a sequence of 'labels' |
| 412 | * terminated by a zero. Each label has the following format: |
| 413 | * |
| 414 | * LEN : 8 : lenght of label (MUST be < 64) |
| 415 | * NAME : 8*LEN : label length (must exclude dots) |
| 416 | * |
| 417 | * A value of 0 in the encoding is interpreted as the 'root' domain and |
| 418 | * terminates the encoding. So 'www.android.com' will be encoded as: |
| 419 | * |
| 420 | * <3>www<7>android<3>com<0> |
| 421 | * |
| 422 | * Where <n> represents the byte with value 'n' |
| 423 | * |
| 424 | * Each NAME reflects the QNAME of the question, but has a slightly more |
| 425 | * complex encoding in order to provide message compression. This is achieved |
| 426 | * by using a 2-byte pointer, with format: |
| 427 | * |
| 428 | * TYPE : 2 : 0b11 to indicate a pointer, 0b01 and 0b10 are reserved |
| 429 | * OFFSET : 14 : offset to another part of the DNS packet |
| 430 | * |
| 431 | * The offset is relative to the start of the DNS packet and must point |
| 432 | * A pointer terminates the encoding. |
| 433 | * |
| 434 | * The NAME can be encoded in one of the following formats: |
| 435 | * |
| 436 | * - a sequence of simple labels terminated by 0 (like QNAMEs) |
| 437 | * - a single pointer |
| 438 | * - a sequence of simple labels terminated by a pointer |
| 439 | * |
| 440 | * A pointer shall always point to either a pointer of a sequence of |
| 441 | * labels (which can themselves be terminated by either a 0 or a pointer) |
| 442 | * |
| 443 | * The expanded length of a given domain name should not exceed 255 bytes. |
| 444 | * |
| 445 | * NOTE: we don't parse the answer packets, so don't need to deal with NAME |
| 446 | * records, only QNAMEs. |
| 447 | */ |
| 448 | |
| 449 | #define DNS_HEADER_SIZE 12 |
| 450 | |
| 451 | #define DNS_TYPE_A "\00\01" /* big-endian decimal 1 */ |
| 452 | #define DNS_TYPE_PTR "\00\014" /* big-endian decimal 12 */ |
| 453 | #define DNS_TYPE_MX "\00\017" /* big-endian decimal 15 */ |
| 454 | #define DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */ |
| 455 | #define DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */ |
| 456 | |
| 457 | #define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */ |
| 458 | |
| 459 | typedef struct { |
| 460 | const uint8_t* base; |
| 461 | const uint8_t* end; |
| 462 | const uint8_t* cursor; |
| 463 | } DnsPacket; |
| 464 | |
| 465 | static void |
| 466 | _dnsPacket_init( DnsPacket* packet, const uint8_t* buff, int bufflen ) |
| 467 | { |
| 468 | packet->base = buff; |
| 469 | packet->end = buff + bufflen; |
| 470 | packet->cursor = buff; |
| 471 | } |
| 472 | |
| 473 | static void |
| 474 | _dnsPacket_rewind( DnsPacket* packet ) |
| 475 | { |
| 476 | packet->cursor = packet->base; |
| 477 | } |
| 478 | |
| 479 | static void |
| 480 | _dnsPacket_skip( DnsPacket* packet, int count ) |
| 481 | { |
| 482 | const uint8_t* p = packet->cursor + count; |
| 483 | |
| 484 | if (p > packet->end) |
| 485 | p = packet->end; |
| 486 | |
| 487 | packet->cursor = p; |
| 488 | } |
| 489 | |
| 490 | static int |
| 491 | _dnsPacket_readInt16( DnsPacket* packet ) |
| 492 | { |
| 493 | const uint8_t* p = packet->cursor; |
| 494 | |
| 495 | if (p+2 > packet->end) |
| 496 | return -1; |
| 497 | |
| 498 | packet->cursor = p+2; |
| 499 | return (p[0]<< 8) | p[1]; |
| 500 | } |
| 501 | |
| 502 | /** QUERY CHECKING |
| 503 | **/ |
| 504 | |
| 505 | /* check bytes in a dns packet. returns 1 on success, 0 on failure. |
| 506 | * the cursor is only advanced in the case of success |
| 507 | */ |
| 508 | static int |
| 509 | _dnsPacket_checkBytes( DnsPacket* packet, int numBytes, const void* bytes ) |
| 510 | { |
| 511 | const uint8_t* p = packet->cursor; |
| 512 | |
| 513 | if (p + numBytes > packet->end) |
| 514 | return 0; |
| 515 | |
| 516 | if (memcmp(p, bytes, numBytes) != 0) |
| 517 | return 0; |
| 518 | |
| 519 | packet->cursor = p + numBytes; |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | /* parse and skip a given QNAME stored in a query packet, |
| 524 | * from the current cursor position. returns 1 on success, |
| 525 | * or 0 for malformed data. |
| 526 | */ |
| 527 | static int |
| 528 | _dnsPacket_checkQName( DnsPacket* packet ) |
| 529 | { |
| 530 | const uint8_t* p = packet->cursor; |
| 531 | const uint8_t* end = packet->end; |
| 532 | |
| 533 | for (;;) { |
| 534 | int c; |
| 535 | |
| 536 | if (p >= end) |
| 537 | break; |
| 538 | |
| 539 | c = *p++; |
| 540 | |
| 541 | if (c == 0) { |
| 542 | packet->cursor = p; |
| 543 | return 1; |
| 544 | } |
| 545 | |
| 546 | /* we don't expect label compression in QNAMEs */ |
| 547 | if (c >= 64) |
| 548 | break; |
| 549 | |
| 550 | p += c; |
| 551 | /* we rely on the bound check at the start |
| 552 | * of the loop here */ |
| 553 | } |
| 554 | /* malformed data */ |
| 555 | XLOG("malformed QNAME"); |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* parse and skip a given QR stored in a packet. |
| 560 | * returns 1 on success, and 0 on failure |
| 561 | */ |
| 562 | static int |
| 563 | _dnsPacket_checkQR( DnsPacket* packet ) |
| 564 | { |
| 565 | int len; |
| 566 | |
| 567 | if (!_dnsPacket_checkQName(packet)) |
| 568 | return 0; |
| 569 | |
| 570 | /* TYPE must be one of the things we support */ |
| 571 | if (!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_A) && |
| 572 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_PTR) && |
| 573 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_MX) && |
| 574 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_AAAA) && |
| 575 | !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL)) |
| 576 | { |
| 577 | XLOG("unsupported TYPE"); |
| 578 | return 0; |
| 579 | } |
| 580 | /* CLASS must be IN */ |
| 581 | if (!_dnsPacket_checkBytes(packet, 2, DNS_CLASS_IN)) { |
| 582 | XLOG("unsupported CLASS"); |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | /* check the header of a DNS Query packet, return 1 if it is one |
| 590 | * type of query we can cache, or 0 otherwise |
| 591 | */ |
| 592 | static int |
| 593 | _dnsPacket_checkQuery( DnsPacket* packet ) |
| 594 | { |
| 595 | const uint8_t* p = packet->base; |
| 596 | int qdCount, anCount, dnCount, arCount; |
| 597 | |
| 598 | if (p + DNS_HEADER_SIZE > packet->end) { |
| 599 | XLOG("query packet too small"); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* QR must be set to 0, opcode must be 0 and AA must be 0 */ |
| 604 | /* RA, Z, and RCODE must be 0 */ |
| 605 | if ((p[2] & 0xFC) != 0 || p[3] != 0) { |
| 606 | XLOG("query packet flags unsupported"); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /* Note that we ignore the TC and RD bits here for the |
| 611 | * following reasons: |
| 612 | * |
| 613 | * - there is no point for a query packet sent to a server |
| 614 | * to have the TC bit set, but the implementation might |
| 615 | * set the bit in the query buffer for its own needs |
| 616 | * between a _resolv_cache_lookup and a |
| 617 | * _resolv_cache_add. We should not freak out if this |
| 618 | * is the case. |
| 619 | * |
| 620 | * - we consider that the result from a RD=0 or a RD=1 |
| 621 | * query might be different, hence that the RD bit |
| 622 | * should be used to differentiate cached result. |
| 623 | * |
| 624 | * this implies that RD is checked when hashing or |
| 625 | * comparing query packets, but not TC |
| 626 | */ |
| 627 | |
| 628 | /* ANCOUNT, DNCOUNT and ARCOUNT must be 0 */ |
| 629 | qdCount = (p[4] << 8) | p[5]; |
| 630 | anCount = (p[6] << 8) | p[7]; |
| 631 | dnCount = (p[8] << 8) | p[9]; |
| 632 | arCount = (p[10]<< 8) | p[11]; |
| 633 | |
| 634 | if (anCount != 0 || dnCount != 0 || arCount != 0) { |
| 635 | XLOG("query packet contains non-query records"); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | if (qdCount == 0) { |
| 640 | XLOG("query packet doesn't contain query record"); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | /* Check QDCOUNT QRs */ |
| 645 | packet->cursor = p + DNS_HEADER_SIZE; |
| 646 | |
| 647 | for (;qdCount > 0; qdCount--) |
| 648 | if (!_dnsPacket_checkQR(packet)) |
| 649 | return 0; |
| 650 | |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | /** QUERY DEBUGGING |
| 655 | **/ |
| 656 | #if DEBUG |
| 657 | static char* |
| 658 | _dnsPacket_bprintQName(DnsPacket* packet, char* bp, char* bend) |
| 659 | { |
| 660 | const uint8_t* p = packet->cursor; |
| 661 | const uint8_t* end = packet->end; |
| 662 | int first = 1; |
| 663 | |
| 664 | for (;;) { |
| 665 | int c; |
| 666 | |
| 667 | if (p >= end) |
| 668 | break; |
| 669 | |
| 670 | c = *p++; |
| 671 | |
| 672 | if (c == 0) { |
| 673 | packet->cursor = p; |
| 674 | return bp; |
| 675 | } |
| 676 | |
| 677 | /* we don't expect label compression in QNAMEs */ |
| 678 | if (c >= 64) |
| 679 | break; |
| 680 | |
| 681 | if (first) |
| 682 | first = 0; |
| 683 | else |
| 684 | bp = _bprint_c(bp, bend, '.'); |
| 685 | |
| 686 | bp = _bprint_b(bp, bend, (const char*)p, c); |
| 687 | |
| 688 | p += c; |
| 689 | /* we rely on the bound check at the start |
| 690 | * of the loop here */ |
| 691 | } |
| 692 | /* malformed data */ |
| 693 | bp = _bprint_s(bp, bend, "<MALFORMED>"); |
| 694 | return bp; |
| 695 | } |
| 696 | |
| 697 | static char* |
| 698 | _dnsPacket_bprintQR(DnsPacket* packet, char* p, char* end) |
| 699 | { |
| 700 | #define QQ(x) { DNS_TYPE_##x, #x } |
| 701 | static const struct { |
| 702 | const char* typeBytes; |
| 703 | const char* typeString; |
| 704 | } qTypes[] = |
| 705 | { |
| 706 | QQ(A), QQ(PTR), QQ(MX), QQ(AAAA), QQ(ALL), |
| 707 | { NULL, NULL } |
| 708 | }; |
| 709 | int nn; |
| 710 | const char* typeString = NULL; |
| 711 | |
| 712 | /* dump QNAME */ |
| 713 | p = _dnsPacket_bprintQName(packet, p, end); |
| 714 | |
| 715 | /* dump TYPE */ |
| 716 | p = _bprint_s(p, end, " ("); |
| 717 | |
| 718 | for (nn = 0; qTypes[nn].typeBytes != NULL; nn++) { |
| 719 | if (_dnsPacket_checkBytes(packet, 2, qTypes[nn].typeBytes)) { |
| 720 | typeString = qTypes[nn].typeString; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (typeString != NULL) |
| 726 | p = _bprint_s(p, end, typeString); |
| 727 | else { |
| 728 | int typeCode = _dnsPacket_readInt16(packet); |
| 729 | p = _bprint(p, end, "UNKNOWN-%d", typeCode); |
| 730 | } |
| 731 | |
| 732 | p = _bprint_c(p, end, ')'); |
| 733 | |
| 734 | /* skip CLASS */ |
| 735 | _dnsPacket_skip(packet, 2); |
| 736 | return p; |
| 737 | } |
| 738 | |
| 739 | /* this function assumes the packet has already been checked */ |
| 740 | static char* |
| 741 | _dnsPacket_bprintQuery( DnsPacket* packet, char* p, char* end ) |
| 742 | { |
| 743 | int qdCount; |
| 744 | |
| 745 | if (packet->base[2] & 0x1) { |
| 746 | p = _bprint_s(p, end, "RECURSIVE "); |
| 747 | } |
| 748 | |
| 749 | _dnsPacket_skip(packet, 4); |
| 750 | qdCount = _dnsPacket_readInt16(packet); |
| 751 | _dnsPacket_skip(packet, 6); |
| 752 | |
| 753 | for ( ; qdCount > 0; qdCount-- ) { |
| 754 | p = _dnsPacket_bprintQR(packet, p, end); |
| 755 | } |
| 756 | return p; |
| 757 | } |
| 758 | #endif |
| 759 | |
| 760 | |
| 761 | /** QUERY HASHING SUPPORT |
| 762 | ** |
| 763 | ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKET HAS ALREADY |
| 764 | ** BEEN SUCCESFULLY CHECKED. |
| 765 | **/ |
| 766 | |
| 767 | /* use 32-bit FNV hash function */ |
| 768 | #define FNV_MULT 16777619U |
| 769 | #define FNV_BASIS 2166136261U |
| 770 | |
| 771 | static unsigned |
| 772 | _dnsPacket_hashBytes( DnsPacket* packet, int numBytes, unsigned hash ) |
| 773 | { |
| 774 | const uint8_t* p = packet->cursor; |
| 775 | const uint8_t* end = packet->end; |
| 776 | |
| 777 | while (numBytes > 0 && p < end) { |
| 778 | hash = hash*FNV_MULT ^ *p++; |
| 779 | } |
| 780 | packet->cursor = p; |
| 781 | return hash; |
| 782 | } |
| 783 | |
| 784 | |
| 785 | static unsigned |
| 786 | _dnsPacket_hashQName( DnsPacket* packet, unsigned hash ) |
| 787 | { |
| 788 | const uint8_t* p = packet->cursor; |
| 789 | const uint8_t* end = packet->end; |
| 790 | |
| 791 | for (;;) { |
| 792 | int c; |
| 793 | |
| 794 | if (p >= end) { /* should not happen */ |
| 795 | XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__); |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | c = *p++; |
| 800 | |
| 801 | if (c == 0) |
| 802 | break; |
| 803 | |
| 804 | if (c >= 64) { |
| 805 | XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__); |
| 806 | break; |
| 807 | } |
| 808 | if (p + c >= end) { |
| 809 | XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n", |
| 810 | __FUNCTION__); |
| 811 | break; |
| 812 | } |
| 813 | while (c > 0) { |
| 814 | hash = hash*FNV_MULT ^ *p++; |
| 815 | c -= 1; |
| 816 | } |
| 817 | } |
| 818 | packet->cursor = p; |
| 819 | return hash; |
| 820 | } |
| 821 | |
| 822 | static unsigned |
| 823 | _dnsPacket_hashQR( DnsPacket* packet, unsigned hash ) |
| 824 | { |
| 825 | int len; |
| 826 | |
| 827 | hash = _dnsPacket_hashQName(packet, hash); |
| 828 | hash = _dnsPacket_hashBytes(packet, 4, hash); /* TYPE and CLASS */ |
| 829 | return hash; |
| 830 | } |
| 831 | |
| 832 | static unsigned |
| 833 | _dnsPacket_hashQuery( DnsPacket* packet ) |
| 834 | { |
| 835 | unsigned hash = FNV_BASIS; |
| 836 | int count; |
| 837 | _dnsPacket_rewind(packet); |
| 838 | |
| 839 | /* we ignore the TC bit for reasons explained in |
| 840 | * _dnsPacket_checkQuery(). |
| 841 | * |
| 842 | * however we hash the RD bit to differentiate |
| 843 | * between answers for recursive and non-recursive |
| 844 | * queries. |
| 845 | */ |
| 846 | hash = hash*FNV_MULT ^ (packet->base[2] & 1); |
| 847 | |
| 848 | /* assume: other flags are 0 */ |
| 849 | _dnsPacket_skip(packet, 4); |
| 850 | |
| 851 | /* read QDCOUNT */ |
| 852 | count = _dnsPacket_readInt16(packet); |
| 853 | |
| 854 | /* assume: ANcount, NScount, ARcount are 0 */ |
| 855 | _dnsPacket_skip(packet, 6); |
| 856 | |
| 857 | /* hash QDCOUNT QRs */ |
| 858 | for ( ; count > 0; count-- ) |
| 859 | hash = _dnsPacket_hashQR(packet, hash); |
| 860 | |
| 861 | return hash; |
| 862 | } |
| 863 | |
| 864 | |
| 865 | /** QUERY COMPARISON |
| 866 | ** |
| 867 | ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKETS HAVE ALREADY |
| 868 | ** BEEN SUCCESFULLY CHECKED. |
| 869 | **/ |
| 870 | |
| 871 | static int |
| 872 | _dnsPacket_isEqualDomainName( DnsPacket* pack1, DnsPacket* pack2 ) |
| 873 | { |
| 874 | const uint8_t* p1 = pack1->cursor; |
| 875 | const uint8_t* end1 = pack1->end; |
| 876 | const uint8_t* p2 = pack2->cursor; |
| 877 | const uint8_t* end2 = pack2->end; |
| 878 | |
| 879 | for (;;) { |
| 880 | int c1, c2; |
| 881 | |
| 882 | if (p1 >= end1 || p2 >= end2) { |
| 883 | XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__); |
| 884 | break; |
| 885 | } |
| 886 | c1 = *p1++; |
| 887 | c2 = *p2++; |
| 888 | if (c1 != c2) |
| 889 | break; |
| 890 | |
| 891 | if (c1 == 0) { |
| 892 | pack1->cursor = p1; |
| 893 | pack2->cursor = p2; |
| 894 | return 1; |
| 895 | } |
| 896 | if (c1 >= 64) { |
| 897 | XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__); |
| 898 | break; |
| 899 | } |
| 900 | if ((p1+c1 > end1) || (p2+c1 > end2)) { |
| 901 | XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n", |
| 902 | __FUNCTION__); |
| 903 | break; |
| 904 | } |
| 905 | if (memcmp(p1, p2, c1) != 0) |
| 906 | break; |
| 907 | p1 += c1; |
| 908 | p2 += c1; |
| 909 | /* we rely on the bound checks at the start of the loop */ |
| 910 | } |
| 911 | /* not the same, or one is malformed */ |
| 912 | XLOG("different DN"); |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | static int |
| 917 | _dnsPacket_isEqualBytes( DnsPacket* pack1, DnsPacket* pack2, int numBytes ) |
| 918 | { |
| 919 | const uint8_t* p1 = pack1->cursor; |
| 920 | const uint8_t* p2 = pack2->cursor; |
| 921 | |
| 922 | if ( p1 + numBytes > pack1->end || p2 + numBytes > pack2->end ) |
| 923 | return 0; |
| 924 | |
| 925 | if ( memcmp(p1, p2, numBytes) != 0 ) |
| 926 | return 0; |
| 927 | |
| 928 | pack1->cursor += numBytes; |
| 929 | pack2->cursor += numBytes; |
| 930 | return 1; |
| 931 | } |
| 932 | |
| 933 | static int |
| 934 | _dnsPacket_isEqualQR( DnsPacket* pack1, DnsPacket* pack2 ) |
| 935 | { |
| 936 | /* compare domain name encoding + TYPE + CLASS */ |
| 937 | if ( !_dnsPacket_isEqualDomainName(pack1, pack2) || |
| 938 | !_dnsPacket_isEqualBytes(pack1, pack2, 2+2) ) |
| 939 | return 0; |
| 940 | |
| 941 | return 1; |
| 942 | } |
| 943 | |
| 944 | static int |
| 945 | _dnsPacket_isEqualQuery( DnsPacket* pack1, DnsPacket* pack2 ) |
| 946 | { |
| 947 | int count1, count2; |
| 948 | |
| 949 | /* compare the headers, ignore most fields */ |
| 950 | _dnsPacket_rewind(pack1); |
| 951 | _dnsPacket_rewind(pack2); |
| 952 | |
| 953 | /* compare RD, ignore TC, see comment in _dnsPacket_checkQuery */ |
| 954 | if ((pack1->base[2] & 1) != (pack2->base[2] & 1)) { |
| 955 | XLOG("different RD"); |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | /* assume: other flags are all 0 */ |
| 960 | _dnsPacket_skip(pack1, 4); |
| 961 | _dnsPacket_skip(pack2, 4); |
| 962 | |
| 963 | /* compare QDCOUNT */ |
| 964 | count1 = _dnsPacket_readInt16(pack1); |
| 965 | count2 = _dnsPacket_readInt16(pack2); |
| 966 | if (count1 != count2 || count1 < 0) { |
| 967 | XLOG("different QDCOUNT"); |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | /* assume: ANcount, NScount and ARcount are all 0 */ |
| 972 | _dnsPacket_skip(pack1, 6); |
| 973 | _dnsPacket_skip(pack2, 6); |
| 974 | |
| 975 | /* compare the QDCOUNT QRs */ |
| 976 | for ( ; count1 > 0; count1-- ) { |
| 977 | if (!_dnsPacket_isEqualQR(pack1, pack2)) { |
| 978 | XLOG("different QR"); |
| 979 | return 0; |
| 980 | } |
| 981 | } |
| 982 | return 1; |
| 983 | } |
| 984 | |
| 985 | /****************************************************************************/ |
| 986 | /****************************************************************************/ |
| 987 | /***** *****/ |
| 988 | /***** *****/ |
| 989 | /***** *****/ |
| 990 | /****************************************************************************/ |
| 991 | /****************************************************************************/ |
| 992 | |
| 993 | /* cache entry. for simplicity, 'hash' and 'hlink' are inlined in this |
| 994 | * structure though they are conceptually part of the hash table. |
| 995 | * |
| 996 | * similarly, mru_next and mru_prev are part of the global MRU list |
| 997 | */ |
| 998 | typedef struct Entry { |
| 999 | unsigned int hash; /* hash value */ |
| 1000 | struct Entry* hlink; /* next in collision chain */ |
| 1001 | struct Entry* mru_prev; |
| 1002 | struct Entry* mru_next; |
| 1003 | |
| 1004 | const uint8_t* query; |
| 1005 | int querylen; |
| 1006 | const uint8_t* answer; |
| 1007 | int answerlen; |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1008 | time_t expires; /* time_t when the entry isn't valid any more */ |
| 1009 | int id; /* for debugging purpose */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1010 | } Entry; |
| 1011 | |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1012 | /** |
| 1013 | * Parse the answer records and find the smallest |
| 1014 | * TTL among the answer records. |
| 1015 | * |
| 1016 | * The returned TTL is the number of seconds to |
| 1017 | * keep the answer in the cache. |
| 1018 | * |
| 1019 | * In case of parse error zero (0) is returned which |
| 1020 | * indicates that the answer shall not be cached. |
| 1021 | */ |
| 1022 | static u_long |
| 1023 | answer_getTTL(const void* answer, int answerlen) |
| 1024 | { |
| 1025 | ns_msg handle; |
| 1026 | int ancount, n; |
| 1027 | u_long result, ttl; |
| 1028 | ns_rr rr; |
| 1029 | |
| 1030 | result = 0; |
| 1031 | if (ns_initparse(answer, answerlen, &handle) >= 0) { |
| 1032 | // get number of answer records |
| 1033 | ancount = ns_msg_count(handle, ns_s_an); |
| 1034 | for (n = 0; n < ancount; n++) { |
| 1035 | if (ns_parserr(&handle, ns_s_an, n, &rr) == 0) { |
| 1036 | ttl = ns_rr_ttl(rr); |
| 1037 | if (n == 0 || ttl < result) { |
| 1038 | result = ttl; |
| 1039 | } |
| 1040 | } else { |
| 1041 | XLOG("ns_parserr failed ancount no = %d. errno = %s\n", n, strerror(errno)); |
| 1042 | } |
| 1043 | } |
| 1044 | } else { |
| 1045 | XLOG("ns_parserr failed. %s\n", strerror(errno)); |
| 1046 | } |
| 1047 | |
| 1048 | XLOG("TTL = %d\n", result); |
| 1049 | |
| 1050 | return result; |
| 1051 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1052 | |
| 1053 | static void |
| 1054 | entry_free( Entry* e ) |
| 1055 | { |
| 1056 | /* everything is allocated in a single memory block */ |
| 1057 | if (e) { |
| 1058 | free(e); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | static __inline__ void |
| 1063 | entry_mru_remove( Entry* e ) |
| 1064 | { |
| 1065 | e->mru_prev->mru_next = e->mru_next; |
| 1066 | e->mru_next->mru_prev = e->mru_prev; |
| 1067 | } |
| 1068 | |
| 1069 | static __inline__ void |
| 1070 | entry_mru_add( Entry* e, Entry* list ) |
| 1071 | { |
| 1072 | Entry* first = list->mru_next; |
| 1073 | |
| 1074 | e->mru_next = first; |
| 1075 | e->mru_prev = list; |
| 1076 | |
| 1077 | list->mru_next = e; |
| 1078 | first->mru_prev = e; |
| 1079 | } |
| 1080 | |
| 1081 | /* compute the hash of a given entry, this is a hash of most |
| 1082 | * data in the query (key) */ |
| 1083 | static unsigned |
| 1084 | entry_hash( const Entry* e ) |
| 1085 | { |
| 1086 | DnsPacket pack[1]; |
| 1087 | |
| 1088 | _dnsPacket_init(pack, e->query, e->querylen); |
| 1089 | return _dnsPacket_hashQuery(pack); |
| 1090 | } |
| 1091 | |
| 1092 | /* initialize an Entry as a search key, this also checks the input query packet |
| 1093 | * returns 1 on success, or 0 in case of unsupported/malformed data */ |
| 1094 | static int |
| 1095 | entry_init_key( Entry* e, const void* query, int querylen ) |
| 1096 | { |
| 1097 | DnsPacket pack[1]; |
| 1098 | |
| 1099 | memset(e, 0, sizeof(*e)); |
| 1100 | |
| 1101 | e->query = query; |
| 1102 | e->querylen = querylen; |
| 1103 | e->hash = entry_hash(e); |
| 1104 | |
| 1105 | _dnsPacket_init(pack, query, querylen); |
| 1106 | |
| 1107 | return _dnsPacket_checkQuery(pack); |
| 1108 | } |
| 1109 | |
| 1110 | /* allocate a new entry as a cache node */ |
| 1111 | static Entry* |
| 1112 | entry_alloc( const Entry* init, const void* answer, int answerlen ) |
| 1113 | { |
| 1114 | Entry* e; |
| 1115 | int size; |
| 1116 | |
| 1117 | size = sizeof(*e) + init->querylen + answerlen; |
| 1118 | e = calloc(size, 1); |
| 1119 | if (e == NULL) |
| 1120 | return e; |
| 1121 | |
| 1122 | e->hash = init->hash; |
| 1123 | e->query = (const uint8_t*)(e+1); |
| 1124 | e->querylen = init->querylen; |
| 1125 | |
| 1126 | memcpy( (char*)e->query, init->query, e->querylen ); |
| 1127 | |
| 1128 | e->answer = e->query + e->querylen; |
| 1129 | e->answerlen = answerlen; |
| 1130 | |
| 1131 | memcpy( (char*)e->answer, answer, e->answerlen ); |
| 1132 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1133 | return e; |
| 1134 | } |
| 1135 | |
| 1136 | static int |
| 1137 | entry_equals( const Entry* e1, const Entry* e2 ) |
| 1138 | { |
| 1139 | DnsPacket pack1[1], pack2[1]; |
| 1140 | |
| 1141 | if (e1->querylen != e2->querylen) { |
| 1142 | return 0; |
| 1143 | } |
| 1144 | _dnsPacket_init(pack1, e1->query, e1->querylen); |
| 1145 | _dnsPacket_init(pack2, e2->query, e2->querylen); |
| 1146 | |
| 1147 | return _dnsPacket_isEqualQuery(pack1, pack2); |
| 1148 | } |
| 1149 | |
| 1150 | /****************************************************************************/ |
| 1151 | /****************************************************************************/ |
| 1152 | /***** *****/ |
| 1153 | /***** *****/ |
| 1154 | /***** *****/ |
| 1155 | /****************************************************************************/ |
| 1156 | /****************************************************************************/ |
| 1157 | |
| 1158 | /* We use a simple hash table with external collision lists |
| 1159 | * for simplicity, the hash-table fields 'hash' and 'hlink' are |
| 1160 | * inlined in the Entry structure. |
| 1161 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1162 | |
| 1163 | typedef struct resolv_cache { |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1164 | int max_entries; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1165 | int num_entries; |
| 1166 | Entry mru_list; |
| 1167 | pthread_mutex_t lock; |
| 1168 | unsigned generation; |
| 1169 | int last_id; |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1170 | Entry* entries; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1171 | } Cache; |
| 1172 | |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1173 | typedef struct resolv_cache_info { |
| 1174 | char ifname[IF_NAMESIZE + 1]; |
| 1175 | struct in_addr ifaddr; |
| 1176 | Cache* cache; |
| 1177 | struct resolv_cache_info* next; |
| 1178 | char* nameservers[MAXNS +1]; |
| 1179 | struct addrinfo* nsaddrinfo[MAXNS + 1]; |
| 1180 | } CacheInfo; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1181 | |
| 1182 | #define HTABLE_VALID(x) ((x) != NULL && (x) != HTABLE_DELETED) |
| 1183 | |
| 1184 | static void |
| 1185 | _cache_flush_locked( Cache* cache ) |
| 1186 | { |
| 1187 | int nn; |
| 1188 | time_t now = _time_now(); |
| 1189 | |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1190 | for (nn = 0; nn < cache->max_entries; nn++) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1191 | { |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1192 | Entry** pnode = (Entry**) &cache->entries[nn]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1193 | |
| 1194 | while (*pnode != NULL) { |
| 1195 | Entry* node = *pnode; |
| 1196 | *pnode = node->hlink; |
| 1197 | entry_free(node); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | cache->mru_list.mru_next = cache->mru_list.mru_prev = &cache->mru_list; |
| 1202 | cache->num_entries = 0; |
| 1203 | cache->last_id = 0; |
| 1204 | |
| 1205 | XLOG("*************************\n" |
| 1206 | "*** DNS CACHE FLUSHED ***\n" |
| 1207 | "*************************"); |
| 1208 | } |
| 1209 | |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1210 | /* Return max number of entries allowed in the cache, |
| 1211 | * i.e. cache size. The cache size is either defined |
| 1212 | * by system property ro.net.dns_cache_size or by |
| 1213 | * CONFIG_MAX_ENTRIES if system property not set |
| 1214 | * or set to invalid value. */ |
| 1215 | static int |
| 1216 | _res_cache_get_max_entries( void ) |
| 1217 | { |
| 1218 | int result = -1; |
| 1219 | char cache_size[PROP_VALUE_MAX]; |
| 1220 | |
| 1221 | if (__system_property_get(DNS_CACHE_SIZE_PROP_NAME, cache_size) > 0) { |
| 1222 | result = atoi(cache_size); |
| 1223 | } |
| 1224 | |
| 1225 | // ro.net.dns_cache_size not set or set to negative value |
| 1226 | if (result <= 0) { |
| 1227 | result = CONFIG_MAX_ENTRIES; |
| 1228 | } |
| 1229 | |
| 1230 | XLOG("cache size: %d", result); |
| 1231 | return result; |
| 1232 | } |
| 1233 | |
Jim Huang | 7cc5666 | 2010-10-15 02:02:57 +0800 | [diff] [blame] | 1234 | static struct resolv_cache* |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1235 | _resolv_cache_create( void ) |
| 1236 | { |
| 1237 | struct resolv_cache* cache; |
| 1238 | |
| 1239 | cache = calloc(sizeof(*cache), 1); |
| 1240 | if (cache) { |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1241 | cache->max_entries = _res_cache_get_max_entries(); |
| 1242 | cache->entries = calloc(sizeof(*cache->entries), cache->max_entries); |
| 1243 | if (cache->entries) { |
| 1244 | cache->generation = ~0U; |
| 1245 | pthread_mutex_init( &cache->lock, NULL ); |
| 1246 | cache->mru_list.mru_prev = cache->mru_list.mru_next = &cache->mru_list; |
| 1247 | XLOG("%s: cache created\n", __FUNCTION__); |
| 1248 | } else { |
| 1249 | free(cache); |
| 1250 | cache = NULL; |
| 1251 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1252 | } |
| 1253 | return cache; |
| 1254 | } |
| 1255 | |
| 1256 | |
| 1257 | #if DEBUG |
| 1258 | static void |
| 1259 | _dump_query( const uint8_t* query, int querylen ) |
| 1260 | { |
| 1261 | char temp[256], *p=temp, *end=p+sizeof(temp); |
| 1262 | DnsPacket pack[1]; |
| 1263 | |
| 1264 | _dnsPacket_init(pack, query, querylen); |
| 1265 | p = _dnsPacket_bprintQuery(pack, p, end); |
| 1266 | XLOG("QUERY: %s", temp); |
| 1267 | } |
| 1268 | |
| 1269 | static void |
| 1270 | _cache_dump_mru( Cache* cache ) |
| 1271 | { |
| 1272 | char temp[512], *p=temp, *end=p+sizeof(temp); |
| 1273 | Entry* e; |
| 1274 | |
| 1275 | p = _bprint(temp, end, "MRU LIST (%2d): ", cache->num_entries); |
| 1276 | for (e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) |
| 1277 | p = _bprint(p, end, " %d", e->id); |
| 1278 | |
| 1279 | XLOG("%s", temp); |
| 1280 | } |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1281 | |
| 1282 | static void |
| 1283 | _dump_answer(const void* answer, int answerlen) |
| 1284 | { |
| 1285 | res_state statep; |
| 1286 | FILE* fp; |
| 1287 | char* buf; |
| 1288 | int fileLen; |
| 1289 | |
| 1290 | fp = fopen("/data/reslog.txt", "w+"); |
| 1291 | if (fp != NULL) { |
| 1292 | statep = __res_get_state(); |
| 1293 | |
| 1294 | res_pquery(statep, answer, answerlen, fp); |
| 1295 | |
| 1296 | //Get file length |
| 1297 | fseek(fp, 0, SEEK_END); |
| 1298 | fileLen=ftell(fp); |
| 1299 | fseek(fp, 0, SEEK_SET); |
| 1300 | buf = (char *)malloc(fileLen+1); |
| 1301 | if (buf != NULL) { |
| 1302 | //Read file contents into buffer |
| 1303 | fread(buf, fileLen, 1, fp); |
| 1304 | XLOG("%s\n", buf); |
| 1305 | free(buf); |
| 1306 | } |
| 1307 | fclose(fp); |
| 1308 | remove("/data/reslog.txt"); |
| 1309 | } |
| 1310 | else { |
| 1311 | XLOG("_dump_answer: can't open file\n"); |
| 1312 | } |
| 1313 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1314 | #endif |
| 1315 | |
| 1316 | #if DEBUG |
| 1317 | # define XLOG_QUERY(q,len) _dump_query((q), (len)) |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1318 | # define XLOG_ANSWER(a, len) _dump_answer((a), (len)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1319 | #else |
| 1320 | # define XLOG_QUERY(q,len) ((void)0) |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1321 | # define XLOG_ANSWER(a,len) ((void)0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1322 | #endif |
| 1323 | |
| 1324 | /* This function tries to find a key within the hash table |
| 1325 | * In case of success, it will return a *pointer* to the hashed key. |
| 1326 | * In case of failure, it will return a *pointer* to NULL |
| 1327 | * |
| 1328 | * So, the caller must check '*result' to check for success/failure. |
| 1329 | * |
| 1330 | * The main idea is that the result can later be used directly in |
| 1331 | * calls to _resolv_cache_add or _resolv_cache_remove as the 'lookup' |
| 1332 | * parameter. This makes the code simpler and avoids re-searching |
| 1333 | * for the key position in the htable. |
| 1334 | * |
| 1335 | * The result of a lookup_p is only valid until you alter the hash |
| 1336 | * table. |
| 1337 | */ |
| 1338 | static Entry** |
| 1339 | _cache_lookup_p( Cache* cache, |
| 1340 | Entry* key ) |
| 1341 | { |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1342 | int index = key->hash % cache->max_entries; |
| 1343 | Entry** pnode = (Entry**) &cache->entries[ index ]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1344 | |
| 1345 | while (*pnode != NULL) { |
| 1346 | Entry* node = *pnode; |
| 1347 | |
| 1348 | if (node == NULL) |
| 1349 | break; |
| 1350 | |
| 1351 | if (node->hash == key->hash && entry_equals(node, key)) |
| 1352 | break; |
| 1353 | |
| 1354 | pnode = &node->hlink; |
| 1355 | } |
| 1356 | return pnode; |
| 1357 | } |
| 1358 | |
| 1359 | /* Add a new entry to the hash table. 'lookup' must be the |
| 1360 | * result of an immediate previous failed _lookup_p() call |
| 1361 | * (i.e. with *lookup == NULL), and 'e' is the pointer to the |
| 1362 | * newly created entry |
| 1363 | */ |
| 1364 | static void |
| 1365 | _cache_add_p( Cache* cache, |
| 1366 | Entry** lookup, |
| 1367 | Entry* e ) |
| 1368 | { |
| 1369 | *lookup = e; |
| 1370 | e->id = ++cache->last_id; |
| 1371 | entry_mru_add(e, &cache->mru_list); |
| 1372 | cache->num_entries += 1; |
| 1373 | |
| 1374 | XLOG("%s: entry %d added (count=%d)", __FUNCTION__, |
| 1375 | e->id, cache->num_entries); |
| 1376 | } |
| 1377 | |
| 1378 | /* Remove an existing entry from the hash table, |
| 1379 | * 'lookup' must be the result of an immediate previous |
| 1380 | * and succesful _lookup_p() call. |
| 1381 | */ |
| 1382 | static void |
| 1383 | _cache_remove_p( Cache* cache, |
| 1384 | Entry** lookup ) |
| 1385 | { |
| 1386 | Entry* e = *lookup; |
| 1387 | |
| 1388 | XLOG("%s: entry %d removed (count=%d)", __FUNCTION__, |
| 1389 | e->id, cache->num_entries-1); |
| 1390 | |
| 1391 | entry_mru_remove(e); |
| 1392 | *lookup = e->hlink; |
| 1393 | entry_free(e); |
| 1394 | cache->num_entries -= 1; |
| 1395 | } |
| 1396 | |
| 1397 | /* Remove the oldest entry from the hash table. |
| 1398 | */ |
| 1399 | static void |
| 1400 | _cache_remove_oldest( Cache* cache ) |
| 1401 | { |
| 1402 | Entry* oldest = cache->mru_list.mru_prev; |
| 1403 | Entry** lookup = _cache_lookup_p(cache, oldest); |
| 1404 | |
| 1405 | if (*lookup == NULL) { /* should not happen */ |
| 1406 | XLOG("%s: OLDEST NOT IN HTABLE ?", __FUNCTION__); |
| 1407 | return; |
| 1408 | } |
Robert Greenwalt | 7f84da6 | 2011-09-02 07:44:36 -0700 | [diff] [blame] | 1409 | if (DEBUG) { |
| 1410 | XLOG("Cache full - removing oldest"); |
| 1411 | XLOG_QUERY(oldest->query, oldest->querylen); |
| 1412 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1413 | _cache_remove_p(cache, lookup); |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | ResolvCacheStatus |
| 1418 | _resolv_cache_lookup( struct resolv_cache* cache, |
| 1419 | const void* query, |
| 1420 | int querylen, |
| 1421 | void* answer, |
| 1422 | int answersize, |
| 1423 | int *answerlen ) |
| 1424 | { |
| 1425 | DnsPacket pack[1]; |
| 1426 | Entry key[1]; |
| 1427 | int index; |
| 1428 | Entry** lookup; |
| 1429 | Entry* e; |
| 1430 | time_t now; |
| 1431 | |
| 1432 | ResolvCacheStatus result = RESOLV_CACHE_NOTFOUND; |
| 1433 | |
| 1434 | XLOG("%s: lookup", __FUNCTION__); |
| 1435 | XLOG_QUERY(query, querylen); |
| 1436 | |
| 1437 | /* we don't cache malformed queries */ |
| 1438 | if (!entry_init_key(key, query, querylen)) { |
| 1439 | XLOG("%s: unsupported query", __FUNCTION__); |
| 1440 | return RESOLV_CACHE_UNSUPPORTED; |
| 1441 | } |
| 1442 | /* lookup cache */ |
| 1443 | pthread_mutex_lock( &cache->lock ); |
| 1444 | |
| 1445 | /* see the description of _lookup_p to understand this. |
| 1446 | * the function always return a non-NULL pointer. |
| 1447 | */ |
| 1448 | lookup = _cache_lookup_p(cache, key); |
| 1449 | e = *lookup; |
| 1450 | |
| 1451 | if (e == NULL) { |
| 1452 | XLOG( "NOT IN CACHE"); |
| 1453 | goto Exit; |
| 1454 | } |
| 1455 | |
| 1456 | now = _time_now(); |
| 1457 | |
| 1458 | /* remove stale entries here */ |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1459 | if (now >= e->expires) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1460 | XLOG( " NOT IN CACHE (STALE ENTRY %p DISCARDED)", *lookup ); |
Robert Greenwalt | 7f84da6 | 2011-09-02 07:44:36 -0700 | [diff] [blame] | 1461 | XLOG_QUERY(e->query, e->querylen); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1462 | _cache_remove_p(cache, lookup); |
| 1463 | goto Exit; |
| 1464 | } |
| 1465 | |
| 1466 | *answerlen = e->answerlen; |
| 1467 | if (e->answerlen > answersize) { |
| 1468 | /* NOTE: we return UNSUPPORTED if the answer buffer is too short */ |
| 1469 | result = RESOLV_CACHE_UNSUPPORTED; |
| 1470 | XLOG(" ANSWER TOO LONG"); |
| 1471 | goto Exit; |
| 1472 | } |
| 1473 | |
| 1474 | memcpy( answer, e->answer, e->answerlen ); |
| 1475 | |
| 1476 | /* bump up this entry to the top of the MRU list */ |
| 1477 | if (e != cache->mru_list.mru_next) { |
| 1478 | entry_mru_remove( e ); |
| 1479 | entry_mru_add( e, &cache->mru_list ); |
| 1480 | } |
| 1481 | |
| 1482 | XLOG( "FOUND IN CACHE entry=%p", e ); |
| 1483 | result = RESOLV_CACHE_FOUND; |
| 1484 | |
| 1485 | Exit: |
| 1486 | pthread_mutex_unlock( &cache->lock ); |
| 1487 | return result; |
| 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | void |
| 1492 | _resolv_cache_add( struct resolv_cache* cache, |
| 1493 | const void* query, |
| 1494 | int querylen, |
| 1495 | const void* answer, |
| 1496 | int answerlen ) |
| 1497 | { |
| 1498 | Entry key[1]; |
| 1499 | Entry* e; |
| 1500 | Entry** lookup; |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1501 | u_long ttl; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1502 | |
| 1503 | /* don't assume that the query has already been cached |
| 1504 | */ |
| 1505 | if (!entry_init_key( key, query, querylen )) { |
| 1506 | XLOG( "%s: passed invalid query ?", __FUNCTION__); |
| 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | pthread_mutex_lock( &cache->lock ); |
| 1511 | |
| 1512 | XLOG( "%s: query:", __FUNCTION__ ); |
| 1513 | XLOG_QUERY(query,querylen); |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1514 | XLOG_ANSWER(answer, answerlen); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1515 | #if DEBUG_DATA |
| 1516 | XLOG( "answer:"); |
| 1517 | XLOG_BYTES(answer,answerlen); |
| 1518 | #endif |
| 1519 | |
| 1520 | lookup = _cache_lookup_p(cache, key); |
| 1521 | e = *lookup; |
| 1522 | |
| 1523 | if (e != NULL) { /* should not happen */ |
| 1524 | XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD", |
| 1525 | __FUNCTION__, e); |
| 1526 | goto Exit; |
| 1527 | } |
| 1528 | |
Mattias Falk | 3a4910c | 2011-02-14 12:41:11 +0100 | [diff] [blame] | 1529 | if (cache->num_entries >= cache->max_entries) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1530 | _cache_remove_oldest(cache); |
| 1531 | /* need to lookup again */ |
| 1532 | lookup = _cache_lookup_p(cache, key); |
| 1533 | e = *lookup; |
| 1534 | if (e != NULL) { |
| 1535 | XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD", |
| 1536 | __FUNCTION__, e); |
| 1537 | goto Exit; |
| 1538 | } |
| 1539 | } |
| 1540 | |
Mattias Falk | 3e0c510 | 2011-01-31 12:42:26 +0100 | [diff] [blame] | 1541 | ttl = answer_getTTL(answer, answerlen); |
| 1542 | if (ttl > 0) { |
| 1543 | e = entry_alloc(key, answer, answerlen); |
| 1544 | if (e != NULL) { |
| 1545 | e->expires = ttl + _time_now(); |
| 1546 | _cache_add_p(cache, lookup, e); |
| 1547 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1548 | } |
| 1549 | #if DEBUG |
| 1550 | _cache_dump_mru(cache); |
| 1551 | #endif |
| 1552 | Exit: |
| 1553 | pthread_mutex_unlock( &cache->lock ); |
| 1554 | } |
| 1555 | |
| 1556 | /****************************************************************************/ |
| 1557 | /****************************************************************************/ |
| 1558 | /***** *****/ |
| 1559 | /***** *****/ |
| 1560 | /***** *****/ |
| 1561 | /****************************************************************************/ |
| 1562 | /****************************************************************************/ |
| 1563 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1564 | static pthread_once_t _res_cache_once; |
| 1565 | |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1566 | // Head of the list of caches. Protected by _res_cache_list_lock. |
| 1567 | static struct resolv_cache_info _res_cache_list; |
| 1568 | |
| 1569 | // name of the current default inteface |
| 1570 | static char _res_default_ifname[IF_NAMESIZE + 1]; |
| 1571 | |
| 1572 | // lock protecting everything in the _resolve_cache_info structs (next ptr, etc) |
| 1573 | static pthread_mutex_t _res_cache_list_lock; |
| 1574 | |
| 1575 | |
| 1576 | /* lookup the default interface name */ |
| 1577 | static char *_get_default_iface_locked(); |
| 1578 | /* insert resolv_cache_info into the list of resolv_cache_infos */ |
| 1579 | static void _insert_cache_info_locked(struct resolv_cache_info* cache_info); |
| 1580 | /* creates a resolv_cache_info */ |
| 1581 | static struct resolv_cache_info* _create_cache_info( void ); |
| 1582 | /* gets cache associated with an interface name, or NULL if none exists */ |
| 1583 | static struct resolv_cache* _find_named_cache_locked(const char* ifname); |
| 1584 | /* gets a resolv_cache_info associated with an interface name, or NULL if not found */ |
| 1585 | static struct resolv_cache_info* _find_cache_info_locked(const char* ifname); |
| 1586 | /* free dns name server list of a resolv_cache_info structure */ |
| 1587 | static void _free_nameservers(struct resolv_cache_info* cache_info); |
| 1588 | /* look up the named cache, and creates one if needed */ |
| 1589 | static struct resolv_cache* _get_res_cache_for_iface_locked(const char* ifname); |
| 1590 | /* empty the named cache */ |
| 1591 | static void _flush_cache_for_iface_locked(const char* ifname); |
| 1592 | /* empty the nameservers set for the named cache */ |
| 1593 | static void _free_nameservers_locked(struct resolv_cache_info* cache_info); |
| 1594 | /* lookup the namserver for the name interface */ |
| 1595 | static int _get_nameserver_locked(const char* ifname, int n, char* addr, int addrLen); |
| 1596 | /* lookup the addr of the nameserver for the named interface */ |
| 1597 | static struct addrinfo* _get_nameserver_addr_locked(const char* ifname, int n); |
| 1598 | /* lookup the inteface's address */ |
| 1599 | static struct in_addr* _get_addr_locked(const char * ifname); |
| 1600 | |
| 1601 | |
| 1602 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1603 | static void |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1604 | _res_cache_init(void) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1605 | { |
| 1606 | const char* env = getenv(CONFIG_ENV); |
| 1607 | |
| 1608 | if (env && atoi(env) == 0) { |
| 1609 | /* the cache is disabled */ |
| 1610 | return; |
| 1611 | } |
| 1612 | |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1613 | memset(&_res_default_ifname, 0, sizeof(_res_default_ifname)); |
| 1614 | memset(&_res_cache_list, 0, sizeof(_res_cache_list)); |
| 1615 | pthread_mutex_init(&_res_cache_list_lock, NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1618 | struct resolv_cache* |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1619 | __get_res_cache(void) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1620 | { |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1621 | struct resolv_cache *cache; |
| 1622 | |
| 1623 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1624 | |
| 1625 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1626 | |
| 1627 | char* ifname = _get_default_iface_locked(); |
| 1628 | |
| 1629 | // if default interface not set then use the first cache |
| 1630 | // associated with an interface as the default one. |
| 1631 | if (ifname[0] == '\0') { |
| 1632 | struct resolv_cache_info* cache_info = _res_cache_list.next; |
| 1633 | while (cache_info) { |
| 1634 | if (cache_info->ifname[0] != '\0') { |
| 1635 | ifname = cache_info->ifname; |
Robert Greenwalt | 9363d91 | 2011-07-25 12:30:17 -0700 | [diff] [blame] | 1636 | break; |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | cache_info = cache_info->next; |
| 1640 | } |
| 1641 | } |
| 1642 | cache = _get_res_cache_for_iface_locked(ifname); |
| 1643 | |
| 1644 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1645 | XLOG("_get_res_cache. default_ifname = %s\n", ifname); |
| 1646 | return cache; |
| 1647 | } |
| 1648 | |
| 1649 | static struct resolv_cache* |
| 1650 | _get_res_cache_for_iface_locked(const char* ifname) |
| 1651 | { |
| 1652 | if (ifname == NULL) |
| 1653 | return NULL; |
| 1654 | |
| 1655 | struct resolv_cache* cache = _find_named_cache_locked(ifname); |
| 1656 | if (!cache) { |
| 1657 | struct resolv_cache_info* cache_info = _create_cache_info(); |
| 1658 | if (cache_info) { |
| 1659 | cache = _resolv_cache_create(); |
| 1660 | if (cache) { |
| 1661 | int len = sizeof(cache_info->ifname); |
| 1662 | cache_info->cache = cache; |
| 1663 | strncpy(cache_info->ifname, ifname, len - 1); |
| 1664 | cache_info->ifname[len - 1] = '\0'; |
| 1665 | |
| 1666 | _insert_cache_info_locked(cache_info); |
| 1667 | } else { |
| 1668 | free(cache_info); |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | return cache; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | void |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1676 | _resolv_cache_reset(unsigned generation) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1677 | { |
| 1678 | XLOG("%s: generation=%d", __FUNCTION__, generation); |
| 1679 | |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1680 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1681 | pthread_mutex_lock(&_res_cache_list_lock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1682 | |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1683 | char* ifname = _get_default_iface_locked(); |
| 1684 | // if default interface not set then use the first cache |
| 1685 | // associated with an interface as the default one. |
| 1686 | // Note: Copied the code from __get_res_cache since this |
| 1687 | // method will be deleted/obsolete when cache per interface |
| 1688 | // implemented all over |
| 1689 | if (ifname[0] == '\0') { |
| 1690 | struct resolv_cache_info* cache_info = _res_cache_list.next; |
| 1691 | while (cache_info) { |
| 1692 | if (cache_info->ifname[0] != '\0') { |
| 1693 | ifname = cache_info->ifname; |
Robert Greenwalt | 9363d91 | 2011-07-25 12:30:17 -0700 | [diff] [blame] | 1694 | break; |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | cache_info = cache_info->next; |
| 1698 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1699 | } |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1700 | struct resolv_cache* cache = _get_res_cache_for_iface_locked(ifname); |
| 1701 | |
Robert Greenwalt | 9363d91 | 2011-07-25 12:30:17 -0700 | [diff] [blame] | 1702 | if (cache != NULL) { |
| 1703 | pthread_mutex_lock( &cache->lock ); |
| 1704 | if (cache->generation != generation) { |
| 1705 | _cache_flush_locked(cache); |
| 1706 | cache->generation = generation; |
| 1707 | } |
| 1708 | pthread_mutex_unlock( &cache->lock ); |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1709 | } |
| 1710 | |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1711 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1712 | } |
| 1713 | |
| 1714 | void |
| 1715 | _resolv_flush_cache_for_default_iface(void) |
| 1716 | { |
| 1717 | char* ifname; |
| 1718 | |
| 1719 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1720 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1721 | |
| 1722 | ifname = _get_default_iface_locked(); |
| 1723 | _flush_cache_for_iface_locked(ifname); |
| 1724 | |
| 1725 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1726 | } |
| 1727 | |
| 1728 | void |
| 1729 | _resolv_flush_cache_for_iface(const char* ifname) |
| 1730 | { |
| 1731 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1732 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1733 | |
| 1734 | _flush_cache_for_iface_locked(ifname); |
| 1735 | |
| 1736 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1737 | } |
| 1738 | |
| 1739 | static void |
| 1740 | _flush_cache_for_iface_locked(const char* ifname) |
| 1741 | { |
| 1742 | struct resolv_cache* cache = _find_named_cache_locked(ifname); |
| 1743 | if (cache) { |
| 1744 | pthread_mutex_lock(&cache->lock); |
| 1745 | _cache_flush_locked(cache); |
| 1746 | pthread_mutex_unlock(&cache->lock); |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | static struct resolv_cache_info* |
| 1751 | _create_cache_info(void) |
| 1752 | { |
| 1753 | struct resolv_cache_info* cache_info; |
| 1754 | |
| 1755 | cache_info = calloc(sizeof(*cache_info), 1); |
| 1756 | return cache_info; |
| 1757 | } |
| 1758 | |
| 1759 | static void |
| 1760 | _insert_cache_info_locked(struct resolv_cache_info* cache_info) |
| 1761 | { |
| 1762 | struct resolv_cache_info* last; |
| 1763 | |
| 1764 | for (last = &_res_cache_list; last->next; last = last->next); |
| 1765 | |
| 1766 | last->next = cache_info; |
| 1767 | |
| 1768 | } |
| 1769 | |
| 1770 | static struct resolv_cache* |
| 1771 | _find_named_cache_locked(const char* ifname) { |
| 1772 | |
| 1773 | struct resolv_cache_info* info = _find_cache_info_locked(ifname); |
| 1774 | |
| 1775 | if (info != NULL) return info->cache; |
| 1776 | |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | |
| 1780 | static struct resolv_cache_info* |
| 1781 | _find_cache_info_locked(const char* ifname) |
| 1782 | { |
| 1783 | if (ifname == NULL) |
| 1784 | return NULL; |
| 1785 | |
| 1786 | struct resolv_cache_info* cache_info = _res_cache_list.next; |
| 1787 | |
| 1788 | while (cache_info) { |
| 1789 | if (strcmp(cache_info->ifname, ifname) == 0) { |
| 1790 | break; |
| 1791 | } |
| 1792 | |
| 1793 | cache_info = cache_info->next; |
| 1794 | } |
| 1795 | return cache_info; |
| 1796 | } |
| 1797 | |
| 1798 | static char* |
| 1799 | _get_default_iface_locked(void) |
| 1800 | { |
| 1801 | char* iface = _res_default_ifname; |
| 1802 | |
| 1803 | return iface; |
| 1804 | } |
| 1805 | |
| 1806 | void |
| 1807 | _resolv_set_default_iface(const char* ifname) |
| 1808 | { |
| 1809 | XLOG("_resolv_set_default_if ifname %s\n",ifname); |
| 1810 | |
| 1811 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1812 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1813 | |
| 1814 | int size = sizeof(_res_default_ifname); |
| 1815 | memset(_res_default_ifname, 0, size); |
| 1816 | strncpy(_res_default_ifname, ifname, size - 1); |
| 1817 | _res_default_ifname[size - 1] = '\0'; |
| 1818 | |
| 1819 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1820 | } |
| 1821 | |
| 1822 | void |
| 1823 | _resolv_set_nameservers_for_iface(const char* ifname, char** servers, int numservers) |
| 1824 | { |
| 1825 | int i, rt, index; |
| 1826 | struct addrinfo hints; |
| 1827 | char sbuf[NI_MAXSERV]; |
| 1828 | |
| 1829 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1830 | |
| 1831 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1832 | // creates the cache if not created |
| 1833 | _get_res_cache_for_iface_locked(ifname); |
| 1834 | |
| 1835 | struct resolv_cache_info* cache_info = _find_cache_info_locked(ifname); |
| 1836 | |
| 1837 | if (cache_info != NULL) { |
| 1838 | // free current before adding new |
| 1839 | _free_nameservers_locked(cache_info); |
| 1840 | |
| 1841 | memset(&hints, 0, sizeof(hints)); |
| 1842 | hints.ai_family = PF_UNSPEC; |
| 1843 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 1844 | hints.ai_flags = AI_NUMERICHOST; |
| 1845 | sprintf(sbuf, "%u", NAMESERVER_PORT); |
| 1846 | |
| 1847 | index = 0; |
| 1848 | for (i = 0; i < numservers && i < MAXNS; i++) { |
| 1849 | rt = getaddrinfo(servers[i], sbuf, &hints, &cache_info->nsaddrinfo[index]); |
| 1850 | if (rt == 0) { |
| 1851 | cache_info->nameservers[index] = strdup(servers[i]); |
| 1852 | index++; |
| 1853 | } else { |
| 1854 | cache_info->nsaddrinfo[index] = NULL; |
| 1855 | } |
| 1856 | } |
| 1857 | } |
| 1858 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1859 | } |
| 1860 | |
| 1861 | static void |
| 1862 | _free_nameservers_locked(struct resolv_cache_info* cache_info) |
| 1863 | { |
| 1864 | int i; |
| 1865 | for (i = 0; i <= MAXNS; i++) { |
| 1866 | free(cache_info->nameservers[i]); |
| 1867 | cache_info->nameservers[i] = NULL; |
Robert Greenwalt | 9363d91 | 2011-07-25 12:30:17 -0700 | [diff] [blame] | 1868 | if (cache_info->nsaddrinfo[i] != NULL) { |
| 1869 | freeaddrinfo(cache_info->nsaddrinfo[i]); |
| 1870 | cache_info->nsaddrinfo[i] = NULL; |
| 1871 | } |
Mattias Falk | 23d3e6b | 2011-04-04 16:12:35 +0200 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | int |
| 1876 | _resolv_cache_get_nameserver(int n, char* addr, int addrLen) |
| 1877 | { |
| 1878 | char *ifname; |
| 1879 | int result = 0; |
| 1880 | |
| 1881 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1882 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1883 | |
| 1884 | ifname = _get_default_iface_locked(); |
| 1885 | result = _get_nameserver_locked(ifname, n, addr, addrLen); |
| 1886 | |
| 1887 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1888 | return result; |
| 1889 | } |
| 1890 | |
| 1891 | static int |
| 1892 | _get_nameserver_locked(const char* ifname, int n, char* addr, int addrLen) |
| 1893 | { |
| 1894 | int len = 0; |
| 1895 | char* ns; |
| 1896 | struct resolv_cache_info* cache_info; |
| 1897 | |
| 1898 | if (n < 1 || n > MAXNS || !addr) |
| 1899 | return 0; |
| 1900 | |
| 1901 | cache_info = _find_cache_info_locked(ifname); |
| 1902 | if (cache_info) { |
| 1903 | ns = cache_info->nameservers[n - 1]; |
| 1904 | if (ns) { |
| 1905 | len = strlen(ns); |
| 1906 | if (len < addrLen) { |
| 1907 | strncpy(addr, ns, len); |
| 1908 | addr[len] = '\0'; |
| 1909 | } else { |
| 1910 | len = 0; |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | return len; |
| 1916 | } |
| 1917 | |
| 1918 | struct addrinfo* |
| 1919 | _cache_get_nameserver_addr(int n) |
| 1920 | { |
| 1921 | struct addrinfo *result; |
| 1922 | char* ifname; |
| 1923 | |
| 1924 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1925 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1926 | |
| 1927 | ifname = _get_default_iface_locked(); |
| 1928 | |
| 1929 | result = _get_nameserver_addr_locked(ifname, n); |
| 1930 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1931 | return result; |
| 1932 | } |
| 1933 | |
| 1934 | static struct addrinfo* |
| 1935 | _get_nameserver_addr_locked(const char* ifname, int n) |
| 1936 | { |
| 1937 | struct addrinfo* ai = NULL; |
| 1938 | struct resolv_cache_info* cache_info; |
| 1939 | |
| 1940 | if (n < 1 || n > MAXNS) |
| 1941 | return NULL; |
| 1942 | |
| 1943 | cache_info = _find_cache_info_locked(ifname); |
| 1944 | if (cache_info) { |
| 1945 | ai = cache_info->nsaddrinfo[n - 1]; |
| 1946 | } |
| 1947 | return ai; |
| 1948 | } |
| 1949 | |
| 1950 | void |
| 1951 | _resolv_set_addr_of_iface(const char* ifname, struct in_addr* addr) |
| 1952 | { |
| 1953 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1954 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1955 | struct resolv_cache_info* cache_info = _find_cache_info_locked(ifname); |
| 1956 | if (cache_info) { |
| 1957 | memcpy(&cache_info->ifaddr, addr, sizeof(*addr)); |
| 1958 | |
| 1959 | if (DEBUG) { |
| 1960 | char* addr_s = inet_ntoa(cache_info->ifaddr); |
| 1961 | XLOG("address of interface %s is %s\n", ifname, addr_s); |
| 1962 | } |
| 1963 | } |
| 1964 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1965 | } |
| 1966 | |
| 1967 | struct in_addr* |
| 1968 | _resolv_get_addr_of_default_iface(void) |
| 1969 | { |
| 1970 | struct in_addr* ai = NULL; |
| 1971 | char* ifname; |
| 1972 | |
| 1973 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1974 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1975 | ifname = _get_default_iface_locked(); |
| 1976 | ai = _get_addr_locked(ifname); |
| 1977 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1978 | |
| 1979 | return ai; |
| 1980 | } |
| 1981 | |
| 1982 | struct in_addr* |
| 1983 | _resolv_get_addr_of_iface(const char* ifname) |
| 1984 | { |
| 1985 | struct in_addr* ai = NULL; |
| 1986 | |
| 1987 | pthread_once(&_res_cache_once, _res_cache_init); |
| 1988 | pthread_mutex_lock(&_res_cache_list_lock); |
| 1989 | ai =_get_addr_locked(ifname); |
| 1990 | pthread_mutex_unlock(&_res_cache_list_lock); |
| 1991 | return ai; |
| 1992 | } |
| 1993 | |
| 1994 | static struct in_addr* |
| 1995 | _get_addr_locked(const char * ifname) |
| 1996 | { |
| 1997 | struct resolv_cache_info* cache_info = _find_cache_info_locked(ifname); |
| 1998 | if (cache_info) { |
| 1999 | return &cache_info->ifaddr; |
| 2000 | } |
| 2001 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2002 | } |