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