11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions in binary form must reproduce the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    the documentation and/or other materials provided with the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "resolv_cache.h"
3023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <resolv.h>
31616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#include <stdarg.h>
32616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#include <stdio.h>
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h>
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <time.h>
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "pthread.h"
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
383e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk#include <errno.h>
39569fb9887d00104509df79fa77d92e3cf9afe7dbCalin Juravle#include <arpa/nameser.h>
403a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk#include <sys/system_properties.h>
4123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <net/if.h>
4223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <netdb.h>
4323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <linux/if.h>
4423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
4523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <arpa/inet.h>
4623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include "resolv_private.h"
47ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak#include "resolv_netid.h"
48c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk#include "res_private.h"
493e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
50616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#include "private/libc_logging.h"
51616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* This code implements a small and *simple* DNS resolver cache.
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
543e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * It is only used to cache DNS answers for a time defined by the smallest TTL
553e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * among the answer records in order to reduce DNS traffic. It is not supposed
563e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * to be a full DNS cache, since we plan to implement that in the future in a
573e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * dedicated process running on the system.
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Note that its design is kept simple very intentionally, i.e.:
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  - it takes raw DNS query packet data as input, and returns raw DNS
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    answer packet data as output
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    (this means that two similar queries that encode the DNS name
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     differently will be treated distinctly).
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
673e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk *    the smallest TTL value among the answer records are used as the time
683e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk *    to keep an answer in the cache.
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    this is bad, but we absolutely want to avoid parsing the answer packets
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    (and should be solved by the later full DNS cache process).
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  - the implementation is just a (query-data) => (answer-data) hash table
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    with a trivial least-recently-used expiration policy.
751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Doing this keeps the code simple and avoids to deal with a lot of things
771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * that a full DNS cache is expected to do.
781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The API is also very simple:
801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - the client calls _resolv_cache_get() to obtain a handle to the cache.
821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     this will initialize the cache on first usage. the result can be NULL
831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     if the cache is disabled.
841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - the client calls _resolv_cache_lookup() before performing a query
861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     if the function returns RESOLV_CACHE_FOUND, a copy of the answer data
881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     has been copied into the client-provided answer buffer.
891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     if the function returns RESOLV_CACHE_NOTFOUND, the client should perform
911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     a request normally, *then* call _resolv_cache_add() to add the received
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     answer to the cache.
931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     if the function returns RESOLV_CACHE_UNSUPPORTED, the client should
951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     perform a request normally, and *not* call _resolv_cache_add()
961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     note that RESOLV_CACHE_UNSUPPORTED is also returned if the answer buffer
981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     is too short to accomodate the cached result.
991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* the name of an environment variable that will be checked the first time
1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * this code is called if its value is "0", then the resolver cache is
1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * disabled.
1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  CONFIG_ENV  "BIONIC_DNSCACHE"
1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1073a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk/* default number of entries kept in the cache. This value has been
1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * determined by browsing through various sites and counting the number
1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * of corresponding requests. Keep in mind that our framework is currently
1101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * performing two requests per name lookup (one for IPv4, the other for IPv6)
1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.google.com      4
1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.ysearch.com     6
1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.amazon.com      8
1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.nytimes.com     22
1161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.espn.com        28
1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.msn.com         28
1181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    www.lemonde.fr      35
1191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (determined in 2009-2-17 from Paris, France, results may vary depending
1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  on location)
1221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * most high-level websites use lots of media/ad servers with different names
1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * but these are generally reused when browsing through the site.
1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1263a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * As such, a value of 64 should be relatively comfortable at the moment.
1273a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk *
12852764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * ******************************************
12952764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * NOTE - this has changed.
13052764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * 1) we've added IPv6 support so each dns query results in 2 responses
13152764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * 2) we've made this a system-wide cache, so the cost is less (it's not
13252764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * *    duplicated in each process) and the need is greater (more processes
13352764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * *    making different requests).
13452764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * Upping by 2x for IPv6
13552764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * Upping by another 5x for the centralized nature
13652764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * *****************************************
1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
13852764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt#define  CONFIG_MAX_ENTRIES    64 * 2 * 5
1393a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk/* name of the system property that can be used to set the cache size */
1403a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk
1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
1441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
1451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
1461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* set to 1 to debug cache operations */
1501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DEBUG       0
1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* set to 1 to debug query data */
1531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DEBUG_DATA  0
1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG
156616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#define __DEBUG__
157616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#else
158616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#define __DEBUG__ __attribute__((unused))
159616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#endif
1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
161616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#undef XLOG
162616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti
163616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti#define XLOG(...) ({ \
164616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti    if (DEBUG) { \
165616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti        __libc_format_log(ANDROID_LOG_DEBUG,"libc",__VA_ARGS__); \
166616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti    } else { \
167616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti        ((void)0); \
168616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti    } \
169616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti})
1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** BOUNDED BUFFER FORMATTING
1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* technical note:
1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   the following debugging routines are used to append data to a bounded
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   buffer they take two parameters that are:
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - p : a pointer to the current cursor position in the buffer
1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *         this value is initially set to the buffer's address.
1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - end : the address of the buffer's limit, i.e. of the first byte
1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *           after the buffer. this address should never be touched.
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *           IMPORTANT: it is assumed that end > buffer_address, i.e.
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *                      that the buffer is at least one byte.
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   the _bprint_() functions return the new value of 'p' after the data
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   has been appended, and also ensure the following:
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - the returned value will never be strictly greater than 'end'
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - a return value equal to 'end' means that truncation occured
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     (in which case, end[-1] will be set to 0)
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - after returning from a _bprint_() function, the content of the buffer
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     is always 0-terminated, even in the event of truncation.
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  these conventions allow you to call _bprint_ functions multiple times and
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  only check for truncation at the end of the sequence, as in:
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     char  buff[1000], *p = buff, *end = p + sizeof(buff);
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     p = _bprint_c(p, end, '"');
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     p = _bprint_s(p, end, my_string);
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     p = _bprint_c(p, end, '"');
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     if (p >= end) {
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *        // buffer was too small
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     }
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *     printf( "%s", buff );
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a char to a bounded buffer */
216616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar*
2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_c( char*  p, char*  end, int  c )
2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p < end) {
2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (p+1 == end)
2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *p++ = 0;
2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        else {
2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *p++ = (char) c;
2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *p   = 0;
2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return p;
2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a sequence of bytes to a bounded buffer */
231616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar*
2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_b( char*  p, char*  end, const char*  buf, int  len )
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int  avail = end - p;
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (avail <= 0 || len <= 0)
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return p;
2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (avail > len)
2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        avail = len;
2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memcpy( p, buf, avail );
2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p += avail;
2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p < end)
2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p[0] = 0;
2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    else
2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        end[-1] = 0;
2491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return p;
2511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a string to a bounded buffer */
254616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar*
2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_s( char*  p, char*  end, const char*  str )
2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return _bprint_b(p, end, str, strlen(str));
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a formatted string to a bounded buffer */
261616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar* _bprint( char*  p, char*  end, const char*  format, ... ) __DEBUG__;
262616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar* _bprint( char*  p, char*  end, const char*  format, ... )
2631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int      avail, n;
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    va_list  args;
2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    avail = end - p;
2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (avail <= 0)
2701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return p;
2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    va_start(args, format);
273d378c68d74cb4fdac450650fe816c9d649c8edafDavid 'Digit' Turner    n = vsnprintf( p, avail, format, args);
2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    va_end(args);
2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* certain C libraries return -1 in case of truncation */
2771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (n < 0 || n > avail)
2781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        n = avail;
2791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p += n;
2811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* certain C libraries do not zero-terminate in case of truncation */
2821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p == end)
2831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p[-1] = 0;
2841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return p;
2861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a hex value to a bounded buffer, up to 8 digits */
289616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar*
2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_hex( char*  p, char*  end, unsigned  value, int  numDigits )
2911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char   text[sizeof(unsigned)*2];
2931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int    nn = 0;
2941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while (numDigits-- > 0) {
2961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        text[nn++] = "0123456789abcdef"[(value >> (numDigits*4)) & 15];
2971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return _bprint_b(p, end, text, nn);
2991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add the hexadecimal dump of some memory area to a bounded buffer */
302616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittichar*
3031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_hexdump( char*  p, char*  end, const uint8_t*  data, int  datalen )
3041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int   lineSize = 16;
3061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while (datalen > 0) {
3081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  avail = datalen;
3091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  nn;
3101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (avail > lineSize)
3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            avail = lineSize;
3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for (nn = 0; nn < avail; nn++) {
3151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if (nn > 0)
3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                p = _bprint_c(p, end, ' ');
3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            p = _bprint_hex(p, end, data[nn], 2);
3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for ( ; nn < lineSize; nn++ ) {
3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            p = _bprint_s(p, end, "   ");
3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _bprint_s(p, end, "  ");
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for (nn = 0; nn < avail; nn++) {
3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            int  c = data[nn];
3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if (c < 32 || c > 127)
3281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                c = '.';
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            p = _bprint_c(p, end, c);
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _bprint_c(p, end, '\n');
3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        data    += avail;
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        datalen -= avail;
3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return p;
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* dump the content of a query of packet to the log */
341616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittivoid XLOG_BYTES( const void*  base, int  len ) __DEBUG__;
342616344d169542aa0549ab19b0ed931e14014907fLorenzo Colittivoid XLOG_BYTES( const void*  base, int  len )
3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
344616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti    if (DEBUG_DATA) {
345616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti        char  buff[1024];
346616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti        char*  p = buff, *end = p + sizeof(buff);
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
348616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti        p = _bprint_hexdump(p, end, base, len);
349616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti        XLOG("%s",buff);
350616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti    }
351616344d169542aa0549ab19b0ed931e14014907fLorenzo Colitti} __DEBUG__
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic time_t
3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_time_now( void )
3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct timeval  tv;
3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday( &tv, NULL );
3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return tv.tv_sec;
3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* reminder: the general format of a DNS packet is the following:
3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    HEADER  (12 bytes)
3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    QUESTION  (variable)
3661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    ANSWER (variable)
3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    AUTHORITY (variable)
3681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    ADDITIONNAL (variable)
3691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the HEADER is made of:
3711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   ID     : 16 : 16-bit unique query identification field
3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   QR     :  1 : set to 0 for queries, and 1 for responses
3751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   Opcode :  4 : set to 0 for queries
3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   AA     :  1 : set to 0 for queries
3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   TC     :  1 : truncation flag, will be set to 0 in queries
3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   RD     :  1 : recursion desired
3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   RA     :  1 : recursion available (0 in queries)
3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   Z      :  3 : three reserved zero bits
3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   RCODE  :  4 : response code (always 0=NOERROR in queries)
3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   QDCount: 16 : question count
3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   ANCount: 16 : Answer count (0 in queries)
3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   NSCount: 16: Authority Record count (0 in queries)
3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   ARCount: 16: Additionnal Record count (0 in queries)
3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the QUESTION is made of QDCount Question Record (QRs)
3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the ANSWER is made of ANCount RRs
3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the AUTHORITY is made of NSCount RRs
3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the ADDITIONNAL is made of ARCount RRs
3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each Question Record (QR) is made of:
3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   QNAME   : variable : Query DNS NAME
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   TYPE    : 16       : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255)
3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   CLASS   : 16       : class of query (IN=1)
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each Resource Record (RR) is made of:
4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   NAME    : variable : DNS NAME
4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   TYPE    : 16       : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255)
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   CLASS   : 16       : class of query (IN=1)
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   TTL     : 32       : seconds to cache this RR (0=none)
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   RDLENGTH: 16       : size of RDDATA in bytes
4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   RDDATA  : variable : RR data (depends on TYPE)
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each QNAME contains a domain name encoded as a sequence of 'labels'
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * terminated by a zero. Each label has the following format:
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    LEN  : 8     : lenght of label (MUST be < 64)
4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    NAME : 8*LEN : label length (must exclude dots)
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A value of 0 in the encoding is interpreted as the 'root' domain and
4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * terminates the encoding. So 'www.android.com' will be encoded as:
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   <3>www<7>android<3>com<0>
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Where <n> represents the byte with value 'n'
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each NAME reflects the QNAME of the question, but has a slightly more
4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * complex encoding in order to provide message compression. This is achieved
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * by using a 2-byte pointer, with format:
4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    TYPE   : 2  : 0b11 to indicate a pointer, 0b01 and 0b10 are reserved
4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    OFFSET : 14 : offset to another part of the DNS packet
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The offset is relative to the start of the DNS packet and must point
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A pointer terminates the encoding.
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The NAME can be encoded in one of the following formats:
4331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - a sequence of simple labels terminated by 0 (like QNAMEs)
4351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - a single pointer
4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   - a sequence of simple labels terminated by a pointer
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A pointer shall always point to either a pointer of a sequence of
4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * labels (which can themselves be terminated by either a 0 or a pointer)
4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The expanded length of a given domain name should not exceed 255 bytes.
4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * NOTE: we don't parse the answer packets, so don't need to deal with NAME
4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *       records, only QNAMEs.
4451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_HEADER_SIZE  12
4481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_TYPE_A   "\00\01"   /* big-endian decimal 1 */
4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_TYPE_PTR "\00\014"  /* big-endian decimal 12 */
4511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_TYPE_MX  "\00\017"  /* big-endian decimal 15 */
4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */
4531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */
4541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  DNS_CLASS_IN "\00\01"   /* big-endian decimal 1 */
4561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecttypedef struct {
4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  base;
4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end;
4601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  cursor;
4611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} DnsPacket;
4621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
4641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_init( DnsPacket*  packet, const uint8_t*  buff, int  bufflen )
4651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->base   = buff;
4671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->end    = buff + bufflen;
4681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = buff;
4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
4721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_rewind( DnsPacket*  packet )
4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = packet->base;
4751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_skip( DnsPacket*  packet, int  count )
4791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p = packet->cursor + count;
4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p > packet->end)
4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = packet->end;
4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = p;
4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
4891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_readInt16( DnsPacket*  packet )
4901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p = packet->cursor;
4921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p+2 > packet->end)
4941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
4951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = p+2;
4971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return (p[0]<< 8) | p[1];
4981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY CHECKING
5011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/
5021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* check bytes in a dns packet. returns 1 on success, 0 on failure.
5041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the cursor is only advanced in the case of success
5051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
5061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
5071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkBytes( DnsPacket*  packet, int  numBytes, const void*  bytes )
5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p = packet->cursor;
5101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p + numBytes > packet->end)
5121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
5131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (memcmp(p, bytes, numBytes) != 0)
5151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
5161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = p + numBytes;
5181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 1;
5191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* parse and skip a given QNAME stored in a query packet,
5221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * from the current cursor position. returns 1 on success,
5231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * or 0 for malformed data.
5241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
5251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
5261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkQName( DnsPacket*  packet )
5271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p   = packet->cursor;
5291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end = packet->end;
5301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (;;) {
5321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  c;
5331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (p >= end)
5351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
5361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        c = *p++;
5381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c == 0) {
5401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            packet->cursor = p;
5411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return 1;
5421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
5431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* we don't expect label compression in QNAMEs */
5451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c >= 64)
5461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
5471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p += c;
5491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* we rely on the bound check at the start
5501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         * of the loop here */
5511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* malformed data */
5531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("malformed QNAME");
5541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
5551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* parse and skip a given QR stored in a packet.
5581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * returns 1 on success, and 0 on failure
5591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
5601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
5611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkQR( DnsPacket*  packet )
5621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!_dnsPacket_checkQName(packet))
5641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
5651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* TYPE must be one of the things we support */
5671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_A) &&
5681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_PTR) &&
5691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_MX) &&
5701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_AAAA) &&
5711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL))
5721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
5731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("unsupported TYPE");
5741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
5751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* CLASS must be IN */
5771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!_dnsPacket_checkBytes(packet, 2, DNS_CLASS_IN)) {
5781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("unsupported CLASS");
5791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
5801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 1;
5831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* check the header of a DNS Query packet, return 1 if it is one
5861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * type of query we can cache, or 0 otherwise
5871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
5891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkQuery( DnsPacket*  packet )
5901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p = packet->base;
5921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int             qdCount, anCount, dnCount, arCount;
5931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (p + DNS_HEADER_SIZE > packet->end) {
5951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("query packet too small");
5961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
5971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* QR must be set to 0, opcode must be 0 and AA must be 0 */
6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* RA, Z, and RCODE must be 0 */
6011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ((p[2] & 0xFC) != 0 || p[3] != 0) {
6021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("query packet flags unsupported");
6031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
6041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Note that we ignore the TC and RD bits here for the
6071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * following reasons:
6081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *
6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * - there is no point for a query packet sent to a server
6101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   to have the TC bit set, but the implementation might
6111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   set the bit in the query buffer for its own needs
6121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   between a _resolv_cache_lookup and a
6131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   _resolv_cache_add. We should not freak out if this
6141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   is the case.
6151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *
6161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * - we consider that the result from a RD=0 or a RD=1
6171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   query might be different, hence that the RD bit
6181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   should be used to differentiate cached result.
6191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *
6201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   this implies that RD is checked when hashing or
6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *   comparing query packets, but not TC
6221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
6231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* ANCOUNT, DNCOUNT and ARCOUNT must be 0 */
6251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    qdCount = (p[4] << 8) | p[5];
6261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    anCount = (p[6] << 8) | p[7];
6271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    dnCount = (p[8] << 8) | p[9];
6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    arCount = (p[10]<< 8) | p[11];
6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (anCount != 0 || dnCount != 0 || arCount != 0) {
6311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("query packet contains non-query records");
6321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
6331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (qdCount == 0) {
6361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("query packet doesn't contain query record");
6371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
6381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Check QDCOUNT QRs */
6411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = p + DNS_HEADER_SIZE;
6421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (;qdCount > 0; qdCount--)
6441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (!_dnsPacket_checkQR(packet))
6451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return 0;
6461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 1;
6481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY DEBUGGING
6511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/
6521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG
6531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char*
6541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_bprintQName(DnsPacket*  packet, char*  bp, char*  bend)
6551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p   = packet->cursor;
6571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end = packet->end;
6581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int             first = 1;
6591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (;;) {
6611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  c;
6621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (p >= end)
6641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
6651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        c = *p++;
6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c == 0) {
6691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            packet->cursor = p;
6701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return bp;
6711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
6721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* we don't expect label compression in QNAMEs */
6741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c >= 64)
6751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (first)
6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            first = 0;
6791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        else
6801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            bp = _bprint_c(bp, bend, '.');
6811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        bp = _bprint_b(bp, bend, (const char*)p, c);
6831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p += c;
6851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* we rely on the bound check at the start
6861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         * of the loop here */
6871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* malformed data */
6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    bp = _bprint_s(bp, bend, "<MALFORMED>");
6901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return bp;
6911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char*
6941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_bprintQR(DnsPacket*  packet, char*  p, char*  end)
6951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  QQ(x)   { DNS_TYPE_##x, #x }
6978f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    static const struct {
6981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        const char*  typeBytes;
6998f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        const char*  typeString;
7001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } qTypes[] =
7011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
7021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        QQ(A), QQ(PTR), QQ(MX), QQ(AAAA), QQ(ALL),
7031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        { NULL, NULL }
7041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    };
7051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int          nn;
7061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char*  typeString = NULL;
7071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* dump QNAME */
7091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p = _dnsPacket_bprintQName(packet, p, end);
7101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* dump TYPE */
7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p = _bprint_s(p, end, " (");
7131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (nn = 0; qTypes[nn].typeBytes != NULL; nn++) {
7151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (_dnsPacket_checkBytes(packet, 2, qTypes[nn].typeBytes)) {
7161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            typeString = qTypes[nn].typeString;
7171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
7181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
7191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (typeString != NULL)
7221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _bprint_s(p, end, typeString);
7231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    else {
7241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  typeCode = _dnsPacket_readInt16(packet);
7251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _bprint(p, end, "UNKNOWN-%d", typeCode);
7261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p = _bprint_c(p, end, ')');
7291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* skip CLASS */
7311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(packet, 2);
7321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return p;
7331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* this function assumes the packet has already been checked */
7361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char*
7371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_bprintQuery( DnsPacket*  packet, char*  p, char*  end )
7381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int   qdCount;
7401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (packet->base[2] & 0x1) {
7421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _bprint_s(p, end, "RECURSIVE ");
7431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(packet, 4);
7461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    qdCount = _dnsPacket_readInt16(packet);
7471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(packet, 6);
7481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for ( ; qdCount > 0; qdCount-- ) {
7501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _dnsPacket_bprintQR(packet, p, end);
7511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return p;
7531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
7551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY HASHING SUPPORT
7581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **
7591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKET HAS ALREADY
7601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** BEEN SUCCESFULLY CHECKED.
7611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/
7621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* use 32-bit FNV hash function */
7641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  FNV_MULT   16777619U
7651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  FNV_BASIS  2166136261U
7661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned
7681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashBytes( DnsPacket*  packet, int  numBytes, unsigned  hash )
7691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p   = packet->cursor;
7711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end = packet->end;
7721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while (numBytes > 0 && p < end) {
7741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        hash = hash*FNV_MULT ^ *p++;
7751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = p;
7771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return hash;
7781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned
7821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashQName( DnsPacket*  packet, unsigned  hash )
7831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p   = packet->cursor;
7851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end = packet->end;
7861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (;;) {
7881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  c;
7891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (p >= end) {  /* should not happen */
7911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__);
7921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
7931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
7941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        c = *p++;
7961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c == 0)
7981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
7991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c >= 64) {
8011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__);
8021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
8031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (p + c >= end) {
8051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n",
8061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    __FUNCTION__);
8071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
8081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        while (c > 0) {
8101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            hash = hash*FNV_MULT ^ *p++;
8111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            c   -= 1;
8121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    packet->cursor = p;
8151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return hash;
8161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned
8191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashQR( DnsPacket*  packet, unsigned  hash )
8201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    hash = _dnsPacket_hashQName(packet, hash);
8221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    hash = _dnsPacket_hashBytes(packet, 4, hash); /* TYPE and CLASS */
8231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return hash;
8241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned
8271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashQuery( DnsPacket*  packet )
8281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned  hash = FNV_BASIS;
8301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int       count;
8311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_rewind(packet);
8321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* we ignore the TC bit for reasons explained in
8341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * _dnsPacket_checkQuery().
8351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     *
8361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * however we hash the RD bit to differentiate
8371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * between answers for recursive and non-recursive
8381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * queries.
8391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
8401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    hash = hash*FNV_MULT ^ (packet->base[2] & 1);
8411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* assume: other flags are 0 */
8431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(packet, 4);
8441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* read QDCOUNT */
8461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    count = _dnsPacket_readInt16(packet);
8471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* assume: ANcount, NScount, ARcount are 0 */
8491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(packet, 6);
8501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* hash QDCOUNT QRs */
8521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for ( ; count > 0; count-- )
8531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        hash = _dnsPacket_hashQR(packet, hash);
8541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return hash;
8561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY COMPARISON
8601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **
8611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKETS HAVE ALREADY
8621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** BEEN SUCCESFULLY CHECKED.
8631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/
8641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
8661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualDomainName( DnsPacket*  pack1, DnsPacket*  pack2 )
8671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p1   = pack1->cursor;
8691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end1 = pack1->end;
8701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p2   = pack2->cursor;
8711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  end2 = pack2->end;
8721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (;;) {
8741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        int  c1, c2;
8751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (p1 >= end1 || p2 >= end2) {
8771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__);
8781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
8791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        c1 = *p1++;
8811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        c2 = *p2++;
8821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c1 != c2)
8831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
8841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c1 == 0) {
8861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            pack1->cursor = p1;
8871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            pack2->cursor = p2;
8881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return 1;
8891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (c1 >= 64) {
8911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__);
8921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
8931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if ((p1+c1 > end1) || (p2+c1 > end2)) {
8951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n",
8961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    __FUNCTION__);
8971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
8981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
8991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (memcmp(p1, p2, c1) != 0)
9001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
9011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p1 += c1;
9021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p2 += c1;
9031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* we rely on the bound checks at the start of the loop */
9041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* not the same, or one is malformed */
9061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("different DN");
9071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
9081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
9111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualBytes( DnsPacket*  pack1, DnsPacket*  pack2, int  numBytes )
9121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p1 = pack1->cursor;
9141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*  p2 = pack2->cursor;
9151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ( p1 + numBytes > pack1->end || p2 + numBytes > pack2->end )
9171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
9181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ( memcmp(p1, p2, numBytes) != 0 )
9201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
9211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    pack1->cursor += numBytes;
9231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    pack2->cursor += numBytes;
9241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 1;
9251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
9281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualQR( DnsPacket*  pack1, DnsPacket*  pack2 )
9291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* compare domain name encoding + TYPE + CLASS */
9311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ( !_dnsPacket_isEqualDomainName(pack1, pack2) ||
9321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         !_dnsPacket_isEqualBytes(pack1, pack2, 2+2) )
9331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
9341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 1;
9361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
9391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualQuery( DnsPacket*  pack1, DnsPacket*  pack2 )
9401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int  count1, count2;
9421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* compare the headers, ignore most fields */
9441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_rewind(pack1);
9451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_rewind(pack2);
9461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* compare RD, ignore TC, see comment in _dnsPacket_checkQuery */
9481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ((pack1->base[2] & 1) != (pack2->base[2] & 1)) {
9491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("different RD");
9501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
9511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* assume: other flags are all 0 */
9541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(pack1, 4);
9551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(pack2, 4);
9561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* compare QDCOUNT */
9581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    count1 = _dnsPacket_readInt16(pack1);
9591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    count2 = _dnsPacket_readInt16(pack2);
9601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (count1 != count2 || count1 < 0) {
9611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("different QDCOUNT");
9621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
9631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* assume: ANcount, NScount and ARcount are all 0 */
9661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(pack1, 6);
9671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_skip(pack2, 6);
9681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* compare the QDCOUNT QRs */
9701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for ( ; count1 > 0; count1-- ) {
9711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (!_dnsPacket_isEqualQR(pack1, pack2)) {
9721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("different QR");
9731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return 0;
9741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
9751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 1;
9771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
9801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
9811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
9821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
9831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
9841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
9851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
9861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* cache entry. for simplicity, 'hash' and 'hlink' are inlined in this
9881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * structure though they are conceptually part of the hash table.
9891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
9901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * similarly, mru_next and mru_prev are part of the global MRU list
9911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
9921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecttypedef struct Entry {
9931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned int     hash;   /* hash value */
9941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct Entry*    hlink;  /* next in collision chain */
9951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct Entry*    mru_prev;
9961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct Entry*    mru_next;
9971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*   query;
9991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int              querylen;
10001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const uint8_t*   answer;
10011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int              answerlen;
10023e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    time_t           expires;   /* time_t when the entry isn't valid any more */
10033e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    int              id;        /* for debugging purpose */
10041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} Entry;
10051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10063e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk/**
100778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * Find the TTL for a negative DNS result.  This is defined as the minimum
100878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * of the SOA records TTL and the MINIMUM-TTL field (RFC-2308).
100978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt *
101078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * Return 0 if not found.
101178851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt */
101278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwaltstatic u_long
101378851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwaltanswer_getNegativeTTL(ns_msg handle) {
101478851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    int n, nscount;
101578851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    u_long result = 0;
101678851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    ns_rr rr;
101778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt
101878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    nscount = ns_msg_count(handle, ns_s_ns);
101978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    for (n = 0; n < nscount; n++) {
102078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt        if ((ns_parserr(&handle, ns_s_ns, n, &rr) == 0) && (ns_rr_type(rr) == ns_t_soa)) {
102178851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            const u_char *rdata = ns_rr_rdata(rr); // find the data
102278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            const u_char *edata = rdata + ns_rr_rdlen(rr); // add the len to find the end
102378851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            int len;
102478851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            u_long ttl, rec_result = ns_rr_ttl(rr);
102578851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt
102678851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // find the MINIMUM-TTL field from the blob of binary data for this record
102778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // skip the server name
102878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            len = dn_skipname(rdata, edata);
102978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            if (len == -1) continue; // error skipping
103078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            rdata += len;
103178851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt
103278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // skip the admin name
103378851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            len = dn_skipname(rdata, edata);
103478851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            if (len == -1) continue; // error skipping
103578851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            rdata += len;
103678851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt
103778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            if (edata - rdata != 5*NS_INT32SZ) continue;
103878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // skip: serial number + refresh interval + retry interval + expiry
103978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            rdata += NS_INT32SZ * 4;
104078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // finally read the MINIMUM TTL
104178851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            ttl = ns_get32(rdata);
104278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            if (ttl < rec_result) {
104378851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                rec_result = ttl;
104478851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            }
104578851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // Now that the record is read successfully, apply the new min TTL
104678851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            if (n == 0 || rec_result < result) {
104778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                result = rec_result;
104878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            }
104978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt        }
105078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    }
105178851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt    return result;
105278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt}
105378851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt
105478851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt/**
105578851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * Parse the answer records and find the appropriate
105678851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * smallest TTL among the records.  This might be from
105778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * the answer records if found or from the SOA record
105878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt * if it's a negative result.
10593e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk *
10603e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * The returned TTL is the number of seconds to
10613e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * keep the answer in the cache.
10623e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk *
10633e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * In case of parse error zero (0) is returned which
10643e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * indicates that the answer shall not be cached.
10653e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk */
10663e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falkstatic u_long
10673e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falkanswer_getTTL(const void* answer, int answerlen)
10683e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk{
10693e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    ns_msg handle;
10703e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    int ancount, n;
10713e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    u_long result, ttl;
10723e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    ns_rr rr;
10733e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
10743e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    result = 0;
10753e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    if (ns_initparse(answer, answerlen, &handle) >= 0) {
10763e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        // get number of answer records
10773e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        ancount = ns_msg_count(handle, ns_s_an);
107878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt
107978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt        if (ancount == 0) {
108078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            // a response with no answers?  Cache this negative result.
108178851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            result = answer_getNegativeTTL(handle);
108278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt        } else {
108378851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt            for (n = 0; n < ancount; n++) {
108478851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                if (ns_parserr(&handle, ns_s_an, n, &rr) == 0) {
108578851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                    ttl = ns_rr_ttl(rr);
108678851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                    if (n == 0 || ttl < result) {
108778851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                        result = ttl;
108878851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                    }
108978851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                } else {
109078851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt                    XLOG("ns_parserr failed ancount no = %d. errno = %s\n", n, strerror(errno));
10913e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk                }
10923e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            }
10933e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        }
10943e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    } else {
10953e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        XLOG("ns_parserr failed. %s\n", strerror(errno));
10963e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    }
10973e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
1098a6a0949b1d629daaf297fe7d1f1d68f636c2bb4aPatrick Tjin    XLOG("TTL = %lu\n", result);
10993e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
11003e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    return result;
11013e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk}
11021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
11041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_free( Entry*  e )
11051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* everything is allocated in a single memory block */
11071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e) {
11081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        free(e);
11091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
11101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic __inline__ void
11131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_mru_remove( Entry*  e )
11141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->mru_prev->mru_next = e->mru_next;
11161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->mru_next->mru_prev = e->mru_prev;
11171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic __inline__ void
11201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_mru_add( Entry*  e, Entry*  list )
11211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*  first = list->mru_next;
11231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->mru_next = first;
11251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->mru_prev = list;
11261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    list->mru_next  = e;
11281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    first->mru_prev = e;
11291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* compute the hash of a given entry, this is a hash of most
11321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * data in the query (key) */
11331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned
11341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_hash( const Entry*  e )
11351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DnsPacket  pack[1];
11371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_init(pack, e->query, e->querylen);
11391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return _dnsPacket_hashQuery(pack);
11401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* initialize an Entry as a search key, this also checks the input query packet
11431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * returns 1 on success, or 0 in case of unsupported/malformed data */
11441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
11451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_init_key( Entry*  e, const void*  query, int  querylen )
11461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DnsPacket  pack[1];
11481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memset(e, 0, sizeof(*e));
11501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->query    = query;
11521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->querylen = querylen;
11531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->hash     = entry_hash(e);
11541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_init(pack, query, querylen);
11561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return _dnsPacket_checkQuery(pack);
11581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* allocate a new entry as a cache node */
11611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Entry*
11621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_alloc( const Entry*  init, const void*  answer, int  answerlen )
11631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*  e;
11651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int     size;
11661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    size = sizeof(*e) + init->querylen + answerlen;
11681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e    = calloc(size, 1);
11691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e == NULL)
11701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return e;
11711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->hash     = init->hash;
11731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->query    = (const uint8_t*)(e+1);
11741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->querylen = init->querylen;
11751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memcpy( (char*)e->query, init->query, e->querylen );
11771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->answer    = e->query + e->querylen;
11791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->answerlen = answerlen;
11801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memcpy( (char*)e->answer, answer, e->answerlen );
11821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return e;
11841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
11871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_equals( const Entry*  e1, const Entry*  e2 )
11881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
11891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DnsPacket  pack1[1], pack2[1];
11901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e1->querylen != e2->querylen) {
11921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return 0;
11931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
11941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_init(pack1, e1->query, e1->querylen);
11951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_init(pack2, e2->query, e2->querylen);
11961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return _dnsPacket_isEqualQuery(pack1, pack2);
11981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
12011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
12021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
12031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
12041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
12051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
12061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
12071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* We use a simple hash table with external collision lists
12091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for simplicity, the hash-table fields 'hash' and 'hlink' are
12101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * inlined in the Entry structure.
12111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
12121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1213a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* Maximum time for a thread to wait for an pending request */
1214a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk#define PENDING_REQUEST_TIMEOUT 20;
1215a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1216a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falktypedef struct pending_req_info {
1217a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    unsigned int                hash;
1218a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    pthread_cond_t              cond;
1219a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    struct pending_req_info*    next;
1220a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk} PendingReqInfo;
1221a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
12221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecttypedef struct resolv_cache {
12233a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk    int              max_entries;
12241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int              num_entries;
12251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry            mru_list;
12261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int              last_id;
12273a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk    Entry*           entries;
1228a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    PendingReqInfo   pending_requests;
12291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} Cache;
12301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1231ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczakstruct resolv_cache_info {
1232ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    unsigned                    netid;
123323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    Cache*                      cache;
123423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    struct resolv_cache_info*   next;
123523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    char*                       nameservers[MAXNS +1];
123623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    struct addrinfo*            nsaddrinfo[MAXNS + 1];
1237c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    char                        defdname[256];
1238c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    int                         dnsrch_offset[MAXDNSRCH+1];  // offsets into defdname
1239ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak};
1240c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
12411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  HTABLE_VALID(x)  ((x) != NULL && (x) != HTABLE_DELETED)
12421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
124341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensenstatic pthread_once_t        _res_cache_once = PTHREAD_ONCE_INIT;
124441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensenstatic void _res_cache_init(void);
124541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
124641d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen// lock protecting everything in the _resolve_cache_info structs (next ptr, etc)
124741d9a503716ed6f3291f4cae097dca08d459429cPaul Jensenstatic pthread_mutex_t _res_cache_list_lock;
124841d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
124941d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen/* gets cache associated with a network, or NULL if none exists */
125041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensenstatic struct resolv_cache* _find_named_cache_locked(unsigned netid);
125141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
12521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
1253a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk_cache_flush_pending_requests_locked( struct resolv_cache* cache )
1254a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{
1255a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    struct pending_req_info *ri, *tmp;
1256a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    if (cache) {
1257a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        ri = cache->pending_requests.next;
1258a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1259a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        while (ri) {
1260a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            tmp = ri;
1261a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            ri = ri->next;
1262a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            pthread_cond_broadcast(&tmp->cond);
1263a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1264a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            pthread_cond_destroy(&tmp->cond);
1265a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            free(tmp);
1266a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        }
1267a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1268a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        cache->pending_requests.next = NULL;
1269a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    }
1270a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk}
1271a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
127241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen/* Return 0 if no pending request is found matching the key.
127341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen * If a matching request is found the calling thread will wait until
127441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen * the matching request completes, then update *cache and return 1. */
1275a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkstatic int
127641d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen_cache_check_pending_request_locked( struct resolv_cache** cache, Entry* key, unsigned netid )
1277a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{
1278a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    struct pending_req_info *ri, *prev;
1279a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    int exist = 0;
1280a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
128141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    if (*cache && key) {
128241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        ri = (*cache)->pending_requests.next;
128341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        prev = &(*cache)->pending_requests;
1284a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        while (ri) {
1285a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            if (ri->hash == key->hash) {
1286a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                exist = 1;
1287a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                break;
1288a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            }
1289a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            prev = ri;
1290a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            ri = ri->next;
1291a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        }
1292a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1293a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        if (!exist) {
1294a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            ri = calloc(1, sizeof(struct pending_req_info));
1295a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            if (ri) {
1296a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                ri->hash = key->hash;
1297a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                pthread_cond_init(&ri->cond, NULL);
1298a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                prev->next = ri;
1299a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            }
1300a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        } else {
1301a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            struct timespec ts = {0,0};
1302c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            XLOG("Waiting for previous request");
1303a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            ts.tv_sec = _time_now() + PENDING_REQUEST_TIMEOUT;
130441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            pthread_cond_timedwait(&ri->cond, &_res_cache_list_lock, &ts);
130541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            /* Must update *cache as it could have been deleted. */
130641d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            *cache = _find_named_cache_locked(netid);
1307a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        }
1308a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    }
1309a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1310a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    return exist;
1311a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk}
1312a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1313a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* notify any waiting thread that waiting on a request
1314a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk * matching the key has been added to the cache */
1315a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkstatic void
1316a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk_cache_notify_waiting_tid_locked( struct resolv_cache* cache, Entry* key )
1317a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{
1318a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    struct pending_req_info *ri, *prev;
1319a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1320a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    if (cache && key) {
1321a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        ri = cache->pending_requests.next;
1322a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        prev = &cache->pending_requests;
1323a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        while (ri) {
1324a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            if (ri->hash == key->hash) {
1325a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                pthread_cond_broadcast(&ri->cond);
1326a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                break;
1327a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            }
1328a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            prev = ri;
1329a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            ri = ri->next;
1330a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        }
1331a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1332a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        // remove item from list and destroy
1333a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        if (ri) {
1334a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            prev->next = ri->next;
1335a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            pthread_cond_destroy(&ri->cond);
1336a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            free(ri);
1337a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        }
1338a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    }
1339a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk}
1340a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1341a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* notify the cache that the query failed */
1342a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkvoid
134341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen_resolv_cache_query_failed( unsigned    netid,
1344a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                   const void* query,
1345a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                   int         querylen)
1346a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{
1347a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    Entry    key[1];
134841d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    Cache*   cache;
134941d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
135041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    if (!entry_init_key(key, query, querylen))
135141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        return;
135241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
135341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_lock(&_res_cache_list_lock);
135441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
135541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    cache = _find_named_cache_locked(netid);
1356a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
135741d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    if (cache) {
1358a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        _cache_notify_waiting_tid_locked(cache, key);
1359a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    }
136041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
136141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_unlock(&_res_cache_list_lock);
1362a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk}
1363a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
1364a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkstatic void
13651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_flush_locked( Cache*  cache )
13661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
13671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int     nn;
13681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13693a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk    for (nn = 0; nn < cache->max_entries; nn++)
13701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
13713a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk        Entry**  pnode = (Entry**) &cache->entries[nn];
13721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        while (*pnode != NULL) {
13741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            Entry*  node = *pnode;
13751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *pnode = node->hlink;
13761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            entry_free(node);
13771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1380a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    // flush pending request
1381a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk    _cache_flush_pending_requests_locked(cache);
1382a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk
13831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    cache->mru_list.mru_next = cache->mru_list.mru_prev = &cache->mru_list;
13841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    cache->num_entries       = 0;
13851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    cache->last_id           = 0;
13861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("*************************\n"
13881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         "*** DNS CACHE FLUSHED ***\n"
13891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         "*************************");
13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13923a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falkstatic int
13933a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk_res_cache_get_max_entries( void )
13943a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk{
1395908e8c24ab4ac6c37524e2286d6e0c69249bd358Elliott Hughes    int cache_size = CONFIG_MAX_ENTRIES;
13963a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk
139752764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt    const char* cache_mode = getenv("ANDROID_DNS_MODE");
139852764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt    if (cache_mode == NULL || strcmp(cache_mode, "local") != 0) {
1399908e8c24ab4ac6c37524e2286d6e0c69249bd358Elliott Hughes        // Don't use the cache in local mode. This is used by the proxy itself.
1400908e8c24ab4ac6c37524e2286d6e0c69249bd358Elliott Hughes        cache_size = 0;
140152764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt    }
140252764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt
1403908e8c24ab4ac6c37524e2286d6e0c69249bd358Elliott Hughes    XLOG("cache size: %d", cache_size);
1404908e8c24ab4ac6c37524e2286d6e0c69249bd358Elliott Hughes    return cache_size;
14053a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk}
14063a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk
14077cc5666d94615d9249163dc7ac0f58c2614497ceJim Huangstatic struct resolv_cache*
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_resolv_cache_create( void )
14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct resolv_cache*  cache;
14111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    cache = calloc(sizeof(*cache), 1);
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (cache) {
14143a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk        cache->max_entries = _res_cache_get_max_entries();
14153a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk        cache->entries = calloc(sizeof(*cache->entries), cache->max_entries);
14163a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk        if (cache->entries) {
14173a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk            cache->mru_list.mru_prev = cache->mru_list.mru_next = &cache->mru_list;
14183a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk            XLOG("%s: cache created\n", __FUNCTION__);
14193a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk        } else {
14203a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk            free(cache);
14213a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk            cache = NULL;
14223a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk        }
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return cache;
14251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG
14291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
14301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dump_query( const uint8_t*  query, int  querylen )
14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
14321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char       temp[256], *p=temp, *end=p+sizeof(temp);
14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DnsPacket  pack[1];
14341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _dnsPacket_init(pack, query, querylen);
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p = _dnsPacket_bprintQuery(pack, p, end);
14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("QUERY: %s", temp);
14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_dump_mru( Cache*  cache )
14421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
14431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char    temp[512], *p=temp, *end=p+sizeof(temp);
14441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*  e;
14451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    p = _bprint(temp, end, "MRU LIST (%2d): ", cache->num_entries);
14471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next)
14481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        p = _bprint(p, end, " %d", e->id);
14491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("%s", temp);
14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14523e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
14533e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falkstatic void
14543e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk_dump_answer(const void* answer, int answerlen)
14553e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk{
14563e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    res_state statep;
14573e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    FILE* fp;
14583e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    char* buf;
14593e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    int fileLen;
14603e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
1461c674edbf27450bbb2396b1434421b1db5325d8f4Elliott Hughes    fp = fopen("/data/reslog.txt", "w+e");
14623e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    if (fp != NULL) {
14633e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        statep = __res_get_state();
14643e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
14653e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        res_pquery(statep, answer, answerlen, fp);
14663e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk
14673e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        //Get file length
14683e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        fseek(fp, 0, SEEK_END);
14693e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        fileLen=ftell(fp);
14703e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        fseek(fp, 0, SEEK_SET);
14713e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        buf = (char *)malloc(fileLen+1);
14723e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        if (buf != NULL) {
14733e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            //Read file contents into buffer
14743e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            fread(buf, fileLen, 1, fp);
14753e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            XLOG("%s\n", buf);
14763e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            free(buf);
14773e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        }
14783e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        fclose(fp);
14793e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        remove("/data/reslog.txt");
14803e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    }
14813e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    else {
148278851f10d1937a5e9477c42a29c1bcf0f47b5ef4Robert Greenwalt        errno = 0; // else debug is introducing error signals
1483ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak        XLOG("%s: can't open file\n", __FUNCTION__);
14843e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    }
14853e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk}
14861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
14871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG
14891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#  define  XLOG_QUERY(q,len)   _dump_query((q), (len))
14903e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk#  define  XLOG_ANSWER(a, len) _dump_answer((a), (len))
14911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else
14921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#  define  XLOG_QUERY(q,len)   ((void)0)
14933e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk#  define  XLOG_ANSWER(a,len)  ((void)0)
14941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
14951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* This function tries to find a key within the hash table
14971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * In case of success, it will return a *pointer* to the hashed key.
14981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * In case of failure, it will return a *pointer* to NULL
14991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
15001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * So, the caller must check '*result' to check for success/failure.
15011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The main idea is that the result can later be used directly in
15031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * calls to _resolv_cache_add or _resolv_cache_remove as the 'lookup'
15041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * parameter. This makes the code simpler and avoids re-searching
15051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for the key position in the htable.
15061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
15071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The result of a lookup_p is only valid until you alter the hash
15081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * table.
15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Entry**
15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_lookup_p( Cache*   cache,
15121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                 Entry*   key )
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
15143a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk    int      index = key->hash % cache->max_entries;
15153a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk    Entry**  pnode = (Entry**) &cache->entries[ index ];
15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while (*pnode != NULL) {
15181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        Entry*  node = *pnode;
15191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (node == NULL)
15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (node->hash == key->hash && entry_equals(node, key))
15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        pnode = &node->hlink;
15271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1528c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    return pnode;
15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Add a new entry to the hash table. 'lookup' must be the
15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * result of an immediate previous failed _lookup_p() call
15331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (i.e. with *lookup == NULL), and 'e' is the pointer to the
15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * newly created entry
15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_add_p( Cache*   cache,
15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              Entry**  lookup,
15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              Entry*   e )
15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
15411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    *lookup = e;
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e->id = ++cache->last_id;
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    entry_mru_add(e, &cache->mru_list);
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    cache->num_entries += 1;
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("%s: entry %d added (count=%d)", __FUNCTION__,
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         e->id, cache->num_entries);
15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Remove an existing entry from the hash table,
15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 'lookup' must be the result of an immediate previous
15521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * and succesful _lookup_p() call.
15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_remove_p( Cache*   cache,
15561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                 Entry**  lookup )
15571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*  e  = *lookup;
15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("%s: entry %d removed (count=%d)", __FUNCTION__,
15611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         e->id, cache->num_entries-1);
15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    entry_mru_remove(e);
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    *lookup = e->hlink;
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    entry_free(e);
15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    cache->num_entries -= 1;
15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Remove the oldest entry from the hash table.
15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_remove_oldest( Cache*  cache )
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
15741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*   oldest = cache->mru_list.mru_prev;
15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry**  lookup = _cache_lookup_p(cache, oldest);
15761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (*lookup == NULL) { /* should not happen */
15781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("%s: OLDEST NOT IN HTABLE ?", __FUNCTION__);
15791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
15801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15817f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt    if (DEBUG) {
15827f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt        XLOG("Cache full - removing oldest");
15837f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt        XLOG_QUERY(oldest->query, oldest->querylen);
15847f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt    }
15851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _cache_remove_p(cache, lookup);
15861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1588dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund/* Remove all expired entries from the hash table.
1589dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund */
1590dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlundstatic void _cache_remove_expired(Cache* cache) {
1591dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund    Entry* e;
1592dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund    time_t now = _time_now();
1593dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund
1594dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund    for (e = cache->mru_list.mru_next; e != &cache->mru_list;) {
1595dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        // Entry is old, remove
1596dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        if (now >= e->expires) {
1597dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            Entry** lookup = _cache_lookup_p(cache, e);
1598dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            if (*lookup == NULL) { /* should not happen */
1599dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund                XLOG("%s: ENTRY NOT IN HTABLE ?", __FUNCTION__);
1600dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund                return;
1601dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            }
1602dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            e = e->mru_next;
1603dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            _cache_remove_p(cache, lookup);
1604dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        } else {
1605dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            e = e->mru_next;
1606dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        }
1607dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund    }
1608dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund}
16091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectResolvCacheStatus
161141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen_resolv_cache_lookup( unsigned              netid,
16121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                      const void*           query,
16131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                      int                   querylen,
16141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                      void*                 answer,
16151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                      int                   answersize,
16161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                      int                  *answerlen )
16171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
16181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry      key[1];
16191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry**    lookup;
16201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*     e;
16211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    time_t     now;
162241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    Cache*     cache;
16231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    ResolvCacheStatus  result = RESOLV_CACHE_NOTFOUND;
16251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG("%s: lookup", __FUNCTION__);
16271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG_QUERY(query, querylen);
16281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* we don't cache malformed queries */
16301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!entry_init_key(key, query, querylen)) {
16311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("%s: unsupported query", __FUNCTION__);
16321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return RESOLV_CACHE_UNSUPPORTED;
16331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* lookup cache */
163541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_once(&_res_cache_once, _res_cache_init);
163641d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_lock(&_res_cache_list_lock);
163741d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
163841d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    cache = _find_named_cache_locked(netid);
163941d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    if (cache == NULL) {
164041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        result = RESOLV_CACHE_UNSUPPORTED;
164141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        goto Exit;
164241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    }
16431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* see the description of _lookup_p to understand this.
16451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * the function always return a non-NULL pointer.
16461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    lookup = _cache_lookup_p(cache, key);
16481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e      = *lookup;
16491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e == NULL) {
16511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG( "NOT IN CACHE");
1652a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        // calling thread will wait if an outstanding request is found
1653a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        // that matching this query
165441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        if (!_cache_check_pending_request_locked(&cache, key, netid) || cache == NULL) {
1655a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            goto Exit;
1656a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        } else {
1657a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            lookup = _cache_lookup_p(cache, key);
1658a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            e = *lookup;
1659a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            if (e == NULL) {
1660a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk                goto Exit;
1661a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk            }
1662a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk        }
16631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    now = _time_now();
16661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* remove stale entries here */
16683e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    if (now >= e->expires) {
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG( " NOT IN CACHE (STALE ENTRY %p DISCARDED)", *lookup );
16707f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt        XLOG_QUERY(e->query, e->querylen);
16711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _cache_remove_p(cache, lookup);
16721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        goto Exit;
16731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    *answerlen = e->answerlen;
16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e->answerlen > answersize) {
16771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* NOTE: we return UNSUPPORTED if the answer buffer is too short */
16781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        result = RESOLV_CACHE_UNSUPPORTED;
16791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG(" ANSWER TOO LONG");
16801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        goto Exit;
16811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memcpy( answer, e->answer, e->answerlen );
16841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* bump up this entry to the top of the MRU list */
16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e != cache->mru_list.mru_next) {
16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        entry_mru_remove( e );
16881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        entry_mru_add( e, &cache->mru_list );
16891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG( "FOUND IN CACHE entry=%p", e );
16921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    result = RESOLV_CACHE_FOUND;
16931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectExit:
169541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_unlock(&_res_cache_list_lock);
16961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return result;
16971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
16981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectvoid
170141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen_resolv_cache_add( unsigned              netid,
17021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                   const void*           query,
17031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                   int                   querylen,
17041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                   const void*           answer,
17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                   int                   answerlen )
17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
17071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry    key[1];
17081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry*   e;
17091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Entry**  lookup;
17103e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    u_long   ttl;
171141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    Cache*   cache = NULL;
17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* don't assume that the query has already been cached
17141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
17151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!entry_init_key( key, query, querylen )) {
17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG( "%s: passed invalid query ?", __FUNCTION__);
17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
17181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
172041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_lock(&_res_cache_list_lock);
172141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
172241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    cache = _find_named_cache_locked(netid);
172341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    if (cache == NULL) {
172441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        goto Exit;
172541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    }
17261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG( "%s: query:", __FUNCTION__ );
17281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG_QUERY(query,querylen);
17293e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    XLOG_ANSWER(answer, answerlen);
17301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG_DATA
17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG( "answer:");
17321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    XLOG_BYTES(answer,answerlen);
17331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
17341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    lookup = _cache_lookup_p(cache, key);
17361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    e      = *lookup;
17371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (e != NULL) { /* should not happen */
17391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD",
17401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project             __FUNCTION__, e);
17411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        goto Exit;
17421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17443a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk    if (cache->num_entries >= cache->max_entries) {
1745dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        _cache_remove_expired(cache);
1746dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        if (cache->num_entries >= cache->max_entries) {
1747dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund            _cache_remove_oldest(cache);
1748dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund        }
17491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* need to lookup again */
17501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        lookup = _cache_lookup_p(cache, key);
17511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        e      = *lookup;
17521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (e != NULL) {
17531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD",
17541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                __FUNCTION__, e);
17551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto Exit;
17561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17593e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    ttl = answer_getTTL(answer, answerlen);
17603e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk    if (ttl > 0) {
17613e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        e = entry_alloc(key, answer, answerlen);
17623e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        if (e != NULL) {
17633e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            e->expires = ttl + _time_now();
17643e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk            _cache_add_p(cache, lookup, e);
17653e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk        }
17661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG
17681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _cache_dump_mru(cache);
17691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
17701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectExit:
177141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    if (cache != NULL) {
177241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen      _cache_notify_waiting_tid_locked(cache, key);
177341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    }
177441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_unlock(&_res_cache_list_lock);
17751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
17761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
17781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
17791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
17801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
17811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*****                                                                  *****/
17821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
17831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/
17841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
178523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk// Head of the list of caches.  Protected by _res_cache_list_lock.
178623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info _res_cache_list;
178723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
178823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* insert resolv_cache_info into the list of resolv_cache_infos */
178923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void _insert_cache_info_locked(struct resolv_cache_info* cache_info);
179023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* creates a resolv_cache_info */
179123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info* _create_cache_info( void );
1792ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak/* gets a resolv_cache_info associated with a network, or NULL if not found */
1793ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczakstatic struct resolv_cache_info* _find_cache_info_locked(unsigned netid);
179423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* look up the named cache, and creates one if needed */
1795ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczakstatic struct resolv_cache* _get_res_cache_for_net_locked(unsigned netid);
179623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* empty the named cache */
1797ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczakstatic void _flush_cache_for_net_locked(unsigned netid);
179823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* empty the nameservers set for the named cache */
179923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void _free_nameservers_locked(struct resolv_cache_info* cache_info);
1800c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk/* return 1 if the provided list of name servers differs from the list of name servers
1801c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk * currently attached to the provided cache_info */
1802c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falkstatic int _resolv_is_nameservers_equal_locked(struct resolv_cache_info* cache_info,
1803fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy        const char** servers, int numservers);
18040c9bb49a9d44e9f1ae98cd56c2dfc24fbb40b53eChad Brubaker
18051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void
180623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_res_cache_init(void)
18071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
18081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char*  env = getenv(CONFIG_ENV);
18091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (env && atoi(env) == 0) {
18111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* the cache is disabled */
18121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
18131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    memset(&_res_cache_list, 0, sizeof(_res_cache_list));
181623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    pthread_mutex_init(&_res_cache_list_lock, NULL);
18171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
18181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache*
1820ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak_get_res_cache_for_net_locked(unsigned netid)
18211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
1822ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    struct resolv_cache* cache = _find_named_cache_locked(netid);
182323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    if (!cache) {
182423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        struct resolv_cache_info* cache_info = _create_cache_info();
182523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        if (cache_info) {
182623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            cache = _resolv_cache_create();
182723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            if (cache) {
182823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk                cache_info->cache = cache;
1829ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak                cache_info->netid = netid;
183023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk                _insert_cache_info_locked(cache_info);
183123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            } else {
183223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk                free(cache_info);
183323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            }
183423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        }
183523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    }
183623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    return cache;
18371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
18381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18391544eae56e2713a199e085438809078ebad8e7d1Paul Jensenvoid
18401544eae56e2713a199e085438809078ebad8e7d1Paul Jensen_resolv_flush_cache_for_net(unsigned netid)
18411544eae56e2713a199e085438809078ebad8e7d1Paul Jensen{
18421544eae56e2713a199e085438809078ebad8e7d1Paul Jensen    pthread_once(&_res_cache_once, _res_cache_init);
18431544eae56e2713a199e085438809078ebad8e7d1Paul Jensen    pthread_mutex_lock(&_res_cache_list_lock);
18441544eae56e2713a199e085438809078ebad8e7d1Paul Jensen
18451544eae56e2713a199e085438809078ebad8e7d1Paul Jensen    _flush_cache_for_net_locked(netid);
18461544eae56e2713a199e085438809078ebad8e7d1Paul Jensen
18471544eae56e2713a199e085438809078ebad8e7d1Paul Jensen    pthread_mutex_unlock(&_res_cache_list_lock);
18481544eae56e2713a199e085438809078ebad8e7d1Paul Jensen}
18491544eae56e2713a199e085438809078ebad8e7d1Paul Jensen
185023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void
1851ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak_flush_cache_for_net_locked(unsigned netid)
185223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{
1853ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    struct resolv_cache* cache = _find_named_cache_locked(netid);
185423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    if (cache) {
185523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        _cache_flush_locked(cache);
185623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    }
185723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
185823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
185941d9a503716ed6f3291f4cae097dca08d459429cPaul Jensenvoid _resolv_delete_cache_for_net(unsigned netid)
186041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen{
186141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_once(&_res_cache_once, _res_cache_init);
186241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_lock(&_res_cache_list_lock);
186341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
186441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    struct resolv_cache_info* prev_cache_info = &_res_cache_list;
186541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
186641d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    while (prev_cache_info->next) {
186741d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        struct resolv_cache_info* cache_info = prev_cache_info->next;
186841d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
186941d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        if (cache_info->netid == netid) {
187041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            prev_cache_info->next = cache_info->next;
187141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            _cache_flush_locked(cache_info->cache);
187241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            free(cache_info->cache->entries);
187341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            free(cache_info->cache);
187441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            _free_nameservers_locked(cache_info);
187541d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            free(cache_info);
187641d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen            break;
187741d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        }
187841d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
187941d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen        prev_cache_info = prev_cache_info->next;
188041d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    }
188141d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
188241d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen    pthread_mutex_unlock(&_res_cache_list_lock);
188341d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen}
188441d9a503716ed6f3291f4cae097dca08d459429cPaul Jensen
188523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info*
188623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_create_cache_info(void)
188723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{
1888ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    struct resolv_cache_info* cache_info;
188923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
189023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    cache_info = calloc(sizeof(*cache_info), 1);
189123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    return cache_info;
189223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
189323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
189423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void
189523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_insert_cache_info_locked(struct resolv_cache_info* cache_info)
189623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{
189723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    struct resolv_cache_info* last;
189823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
189923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    for (last = &_res_cache_list; last->next; last = last->next);
190023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
190123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    last->next = cache_info;
190223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
190323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
190423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
190523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache*
1906ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak_find_named_cache_locked(unsigned netid) {
190723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
1908ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    struct resolv_cache_info* info = _find_cache_info_locked(netid);
190923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
191023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    if (info != NULL) return info->cache;
191123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
191223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    return NULL;
191323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
191423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
191523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info*
1916ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak_find_cache_info_locked(unsigned netid)
191723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{
191823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    struct resolv_cache_info* cache_info = _res_cache_list.next;
19191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
192023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    while (cache_info) {
1921ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak        if (cache_info->netid == netid) {
192223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            break;
192323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        }
192423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
192523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        cache_info = cache_info->next;
192623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    }
192723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    return cache_info;
192823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
192923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
193023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid
1931ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak_resolv_set_nameservers_for_net(unsigned netid, const char** servers, int numservers,
19326f3222ea42f4d2335b089b3d2e5834a5c8c22ecaRobert Greenwalt        const char *domains)
193323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{
193423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    int i, rt, index;
193523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    struct addrinfo hints;
193623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    char sbuf[NI_MAXSERV];
1937c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    register char *cp;
1938c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    int *offset;
193923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
194023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    pthread_once(&_res_cache_once, _res_cache_init);
1941b002a2ff77759b3852a0aa38aa95c07a066faecfRobert Greenwalt    pthread_mutex_lock(&_res_cache_list_lock);
1942c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
194323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    // creates the cache if not created
1944ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    _get_res_cache_for_net_locked(netid);
194523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
1946ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    struct resolv_cache_info* cache_info = _find_cache_info_locked(netid);
194723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
1948c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    if (cache_info != NULL &&
1949c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            !_resolv_is_nameservers_equal_locked(cache_info, servers, numservers)) {
195023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        // free current before adding new
195123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        _free_nameservers_locked(cache_info);
195223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
195323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        memset(&hints, 0, sizeof(hints));
195423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        hints.ai_family = PF_UNSPEC;
195523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
195623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        hints.ai_flags = AI_NUMERICHOST;
1957cfd8c45725d85f3e2bfccb6b14a9bff59fd5c4c7Dan Albert        snprintf(sbuf, sizeof(sbuf), "%u", NAMESERVER_PORT);
195823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
195923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        index = 0;
196023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        for (i = 0; i < numservers && i < MAXNS; i++) {
196123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            rt = getaddrinfo(servers[i], sbuf, &hints, &cache_info->nsaddrinfo[index]);
196223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            if (rt == 0) {
196323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk                cache_info->nameservers[index] = strdup(servers[i]);
196423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk                index++;
1965ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak                XLOG("%s: netid = %u, addr = %s\n", __FUNCTION__, netid, servers[i]);
196623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            } else {
196723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk                cache_info->nsaddrinfo[index] = NULL;
196823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk            }
196923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        }
1970c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
1971c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        // code moved from res_init.c, load_domain_search_list
1972c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        strlcpy(cache_info->defdname, domains, sizeof(cache_info->defdname));
1973c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        if ((cp = strchr(cache_info->defdname, '\n')) != NULL)
1974c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            *cp = '\0';
1975c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        cp = cache_info->defdname;
1976c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        offset = cache_info->dnsrch_offset;
1977c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        while (offset < cache_info->dnsrch_offset + MAXDNSRCH) {
1978c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            while (*cp == ' ' || *cp == '\t') /* skip leading white space */
1979c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                cp++;
1980c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            if (*cp == '\0') /* stop if nothing more to do */
1981c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                break;
1982c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            *offset++ = cp - cache_info->defdname; /* record this search domain */
1983c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            while (*cp) { /* zero-terminate it */
1984c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                if (*cp == ' '|| *cp == '\t') {
1985c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    *cp++ = '\0';
1986c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    break;
1987c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                }
1988c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                cp++;
1989c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            }
1990c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        }
1991c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        *offset = -1; /* cache_info->dnsrch_offset has MAXDNSRCH+1 items */
1992c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
1993c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        // flush cache since new settings
1994ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak        _flush_cache_for_net_locked(netid);
1995c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
199623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    }
1997c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
199823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    pthread_mutex_unlock(&_res_cache_list_lock);
199923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
200023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
2001c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falkstatic int
2002c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk_resolv_is_nameservers_equal_locked(struct resolv_cache_info* cache_info,
2003fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy        const char** servers, int numservers)
2004c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk{
2005c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    int i;
2006c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    char** ns;
2007bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    int currentservers;
2008c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    int equal = 1;
2009c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
2010c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    if (numservers > MAXNS) numservers = MAXNS;
2011bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti
2012bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // Find out how many nameservers we had before.
2013bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    currentservers = 0;
2014bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    for (ns = cache_info->nameservers; *ns; ns++)
2015bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti        currentservers++;
2016bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti
2017bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    if (currentservers != numservers)
2018bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti        return 0;
2019bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti
2020bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // Compare each name server against current name servers.
2021bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // TODO: this is incorrect if the list of current or previous nameservers
2022bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // contains duplicates. This does not really matter because the framework
2023bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // filters out duplicates, but we should probably fix it. It's also
2024bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // insensitive to the order of the nameservers; we should probably fix that
2025bce18c91638e9f37ef2c37555cbb41a0f1f98ab0Lorenzo Colitti    // too.
2026c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    for (i = 0; i < numservers && equal; i++) {
2027c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        ns = cache_info->nameservers;
2028c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        equal = 0;
2029c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        while(*ns) {
2030c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            if (strcmp(*ns, servers[i]) == 0) {
2031c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                equal = 1;
2032c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                break;
2033c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            }
2034c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            ns++;
2035c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        }
2036c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    }
2037c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
2038c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    return equal;
2039c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk}
2040c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
204123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void
204223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_free_nameservers_locked(struct resolv_cache_info* cache_info)
204323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{
204423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    int i;
204523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    for (i = 0; i <= MAXNS; i++) {
204623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        free(cache_info->nameservers[i]);
204723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk        cache_info->nameservers[i] = NULL;
20489363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt        if (cache_info->nsaddrinfo[i] != NULL) {
20499363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt            freeaddrinfo(cache_info->nsaddrinfo[i]);
20509363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt            cache_info->nsaddrinfo[i] = NULL;
20519363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt        }
205223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk    }
205323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk}
205423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk
205523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid
2056ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak_resolv_populate_res_for_net(res_state statp)
2057c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk{
2058fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy    if (statp == NULL) {
2059fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy        return;
2060fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy    }
2061c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
2062fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy    pthread_once(&_res_cache_once, _res_cache_init);
2063fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy    pthread_mutex_lock(&_res_cache_list_lock);
2064fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy
2065ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak    struct resolv_cache_info* info = _find_cache_info_locked(statp->netid);
2066fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy    if (info != NULL) {
2067fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy        int nserv;
2068fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy        struct addrinfo* ai;
2069ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak        XLOG("%s: %u\n", __FUNCTION__, statp->netid);
2070c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        for (nserv = 0; nserv < MAXNS; nserv++) {
2071c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            ai = info->nsaddrinfo[nserv];
2072c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            if (ai == NULL) {
2073c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                break;
2074c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            }
2075c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk
2076c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            if ((size_t) ai->ai_addrlen <= sizeof(statp->_u._ext.ext->nsaddrs[0])) {
2077c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                if (statp->_u._ext.ext != NULL) {
2078c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr, ai->ai_addrlen);
2079c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    statp->nsaddr_list[nserv].sin_family = AF_UNSPEC;
2080c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                } else {
2081c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    if ((size_t) ai->ai_addrlen
2082c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                            <= sizeof(statp->nsaddr_list[0])) {
2083c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                        memcpy(&statp->nsaddr_list[nserv], ai->ai_addr,
2084c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                                ai->ai_addrlen);
2085c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    } else {
2086c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                        statp->nsaddr_list[nserv].sin_family = AF_UNSPEC;
2087c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                    }
2088c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk                }
2089c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            } else {
2090ea9bf6788144b83cc51c454b2bfe6b980d6e947cSzymon Jakubczak                XLOG("%s: found too long addrlen", __FUNCTION__);
2091c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk            }
2092c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        }
2093c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        statp->nscount = nserv;
2094c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        // now do search domains.  Note that we cache the offsets as this code runs alot
2095c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        // but the setting/offset-computer only runs when set/changed
2096c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        strlcpy(statp->defdname, info->defdname, sizeof(statp->defdname));
2097c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        register char **pp = statp->dnsrch;
2098c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        register int *p = info->dnsrch_offset;
2099c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        while (pp < statp->dnsrch + MAXDNSRCH && *p != -1) {
210037b1b5bbcd1cddf833c9ca18a5976efbbf88dabfElliott Hughes            *pp++ = &statp->defdname[0] + *p++;
2101c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk        }
2102c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk    }
2103fbae9f3c30c869ca2fe8609f2d680430f543f0e6Sasha Levitskiy    pthread_mutex_unlock(&_res_cache_list_lock);
2104c63e59039d28c352e3053bb81319e960c392dbd4Mattias Falk}
2105