res_cache.c revision 6f3222ea42f4d2335b089b3d2e5834a5c8c22eca
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> 311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h> 321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h> 331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <time.h> 341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "pthread.h" 351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 363e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk#include <errno.h> 373e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk#include "arpa_nameser.h" 383a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk#include <sys/system_properties.h> 3923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <net/if.h> 4023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <netdb.h> 4123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <linux/if.h> 4223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 4323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include <arpa/inet.h> 4423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk#include "resolv_private.h" 45208898ee776117f42815c2fcb15903b2781bf0aaDavid 'Digit' Turner#include "resolv_iface.h" 463e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* This code implements a small and *simple* DNS resolver cache. 481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 493e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * It is only used to cache DNS answers for a time defined by the smallest TTL 503e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * among the answer records in order to reduce DNS traffic. It is not supposed 513e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * to be a full DNS cache, since we plan to implement that in the future in a 523e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * dedicated process running on the system. 531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Note that its design is kept simple very intentionally, i.e.: 551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - it takes raw DNS query packet data as input, and returns raw DNS 571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * answer packet data as output 581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (this means that two similar queries that encode the DNS name 601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * differently will be treated distinctly). 611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 623e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * the smallest TTL value among the answer records are used as the time 633e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * to keep an answer in the cache. 641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * this is bad, but we absolutely want to avoid parsing the answer packets 661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (and should be solved by the later full DNS cache process). 671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - the implementation is just a (query-data) => (answer-data) hash table 691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * with a trivial least-recently-used expiration policy. 701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Doing this keeps the code simple and avoids to deal with a lot of things 721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * that a full DNS cache is expected to do. 731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The API is also very simple: 751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - the client calls _resolv_cache_get() to obtain a handle to the cache. 771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * this will initialize the cache on first usage. the result can be NULL 781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if the cache is disabled. 791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - the client calls _resolv_cache_lookup() before performing a query 811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if the function returns RESOLV_CACHE_FOUND, a copy of the answer data 831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * has been copied into the client-provided answer buffer. 841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if the function returns RESOLV_CACHE_NOTFOUND, the client should perform 861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * a request normally, *then* call _resolv_cache_add() to add the received 871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * answer to the cache. 881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if the function returns RESOLV_CACHE_UNSUPPORTED, the client should 901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * perform a request normally, and *not* call _resolv_cache_add() 911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * note that RESOLV_CACHE_UNSUPPORTED is also returned if the answer buffer 931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * is too short to accomodate the cached result. 941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - when network settings change, the cache must be flushed since the list 961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * of DNS servers probably changed. this is done by calling 971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * _resolv_cache_reset() 981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the parameter to this function must be an ever-increasing generation 1001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * number corresponding to the current network settings state. 1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This is done because several threads could detect the same network 1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * settings change (but at different times) and will all end up calling the 1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * same function. Comparing with the last used generation number ensures 1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * that the cache is only flushed once per network change. 1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* the name of an environment variable that will be checked the first time 1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * this code is called if its value is "0", then the resolver cache is 1101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * disabled. 1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define CONFIG_ENV "BIONIC_DNSCACHE" 1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* entries older than CONFIG_SECONDS seconds are always discarded. 1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define CONFIG_SECONDS (60*10) /* 10 minutes */ 1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1183a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk/* default number of entries kept in the cache. This value has been 1191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * determined by browsing through various sites and counting the number 1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * of corresponding requests. Keep in mind that our framework is currently 1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * performing two requests per name lookup (one for IPv4, the other for IPv6) 1221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.google.com 4 1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.ysearch.com 6 1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.amazon.com 8 1261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.nytimes.com 22 1271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.espn.com 28 1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.msn.com 28 1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * www.lemonde.fr 35 1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (determined in 2009-2-17 from Paris, France, results may vary depending 1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * on location) 1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * most high-level websites use lots of media/ad servers with different names 1351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * but these are generally reused when browsing through the site. 1361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1373a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * As such, a value of 64 should be relatively comfortable at the moment. 1383a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * 1393a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * The system property ro.net.dns_cache_size can be used to override the default 1403a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * value with a custom value 14152764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * 14252764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * 14352764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * ****************************************** 14452764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * NOTE - this has changed. 14552764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * 1) we've added IPv6 support so each dns query results in 2 responses 14652764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * 2) we've made this a system-wide cache, so the cost is less (it's not 14752764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * duplicated in each process) and the need is greater (more processes 14852764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * making different requests). 14952764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * Upping by 2x for IPv6 15052764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * * Upping by another 5x for the centralized nature 15152764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt * ***************************************** 1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 15352764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt#define CONFIG_MAX_ENTRIES 64 * 2 * 5 1543a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk/* name of the system property that can be used to set the cache size */ 1553a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk#define DNS_CACHE_SIZE_PROP_NAME "ro.net.dns_cache_size" 1563a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk 1571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 1581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 1631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 1641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* set to 1 to debug cache operations */ 1661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DEBUG 0 1671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* set to 1 to debug query data */ 1691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DEBUG_DATA 0 1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1713e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk#undef XLOG 1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG 1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project# include <logd.h> 1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project# define XLOG(...) \ 1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project __libc_android_log_print(ANDROID_LOG_DEBUG,"libc",__VA_ARGS__) 1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h> 1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdarg.h> 1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** BOUNDED BUFFER FORMATTING 1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/ 1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* technical note: 1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the following debugging routines are used to append data to a bounded 1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * buffer they take two parameters that are: 1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - p : a pointer to the current cursor position in the buffer 1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * this value is initially set to the buffer's address. 1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - end : the address of the buffer's limit, i.e. of the first byte 1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * after the buffer. this address should never be touched. 1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPORTANT: it is assumed that end > buffer_address, i.e. 1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * that the buffer is at least one byte. 1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the _bprint_() functions return the new value of 'p' after the data 1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * has been appended, and also ensure the following: 1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - the returned value will never be strictly greater than 'end' 2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - a return value equal to 'end' means that truncation occured 2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (in which case, end[-1] will be set to 0) 2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after returning from a _bprint_() function, the content of the buffer 2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * is always 0-terminated, even in the event of truncation. 2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * these conventions allow you to call _bprint_ functions multiple times and 2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * only check for truncation at the end of the sequence, as in: 2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * char buff[1000], *p = buff, *end = p + sizeof(buff); 2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * p = _bprint_c(p, end, '"'); 2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * p = _bprint_s(p, end, my_string); 2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * p = _bprint_c(p, end, '"'); 2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if (p >= end) { 2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * // buffer was too small 2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * } 2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * printf( "%s", buff ); 2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a char to a bounded buffer */ 2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_c( char* p, char* end, int c ) 2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p < end) { 2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p+1 == end) 2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *p++ = 0; 2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else { 2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *p++ = (char) c; 2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *p = 0; 2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a sequence of bytes to a bounded buffer */ 2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_b( char* p, char* end, const char* buf, int len ) 2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int avail = end - p; 2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (avail <= 0 || len <= 0) 2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (avail > len) 2491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project avail = len; 2501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project memcpy( p, buf, avail ); 2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p += avail; 2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p < end) 2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p[0] = 0; 2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else 2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project end[-1] = 0; 2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a string to a bounded buffer */ 2631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_s( char* p, char* end, const char* str ) 2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return _bprint_b(p, end, str, strlen(str)); 2671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a formatted string to a bounded buffer */ 2701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint( char* p, char* end, const char* format, ... ) 2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int avail, n; 2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project va_list args; 2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project avail = end - p; 2771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (avail <= 0) 2791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 2801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project va_start(args, format); 282d378c68d74cb4fdac450650fe816c9d649c8edafDavid 'Digit' Turner n = vsnprintf( p, avail, format, args); 2831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project va_end(args); 2841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* certain C libraries return -1 in case of truncation */ 2861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (n < 0 || n > avail) 2871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = avail; 2881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p += n; 2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* certain C libraries do not zero-terminate in case of truncation */ 2911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p == end) 2921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p[-1] = 0; 2931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 2951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add a hex value to a bounded buffer, up to 8 digits */ 2981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 2991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_hex( char* p, char* end, unsigned value, int numDigits ) 3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char text[sizeof(unsigned)*2]; 3021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int nn = 0; 3031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project while (numDigits-- > 0) { 3051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project text[nn++] = "0123456789abcdef"[(value >> (numDigits*4)) & 15]; 3061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return _bprint_b(p, end, text, nn); 3081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* add the hexadecimal dump of some memory area to a bounded buffer */ 3111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_bprint_hexdump( char* p, char* end, const uint8_t* data, int datalen ) 3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int lineSize = 16; 3151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project while (datalen > 0) { 3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int avail = datalen; 3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int nn; 3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (avail > lineSize) 3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project avail = lineSize; 3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (nn = 0; nn < avail; nn++) { 3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (nn > 0) 3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_c(p, end, ' '); 3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_hex(p, end, data[nn], 2); 3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for ( ; nn < lineSize; nn++ ) { 3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_s(p, end, " "); 3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_s(p, end, " "); 3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (nn = 0; nn < avail; nn++) { 3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int c = data[nn]; 3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c < 32 || c > 127) 3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c = '.'; 3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_c(p, end, c); 3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_c(p, end, '\n'); 3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project data += avail; 3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project datalen -= avail; 3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* dump the content of a query of packet to the log */ 3501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 3511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectXLOG_BYTES( const void* base, int len ) 3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char buff[1024]; 3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char* p = buff, *end = p + sizeof(buff); 3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_hexdump(p, end, base, len); 3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s",buff); 3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else /* !DEBUG */ 3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project# define XLOG(...) ((void)0) 3621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project# define XLOG_BYTES(a,b) ((void)0) 3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic time_t 3661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_time_now( void ) 3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct timeval tv; 3691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project gettimeofday( &tv, NULL ); 3711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return tv.tv_sec; 3721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* reminder: the general format of a DNS packet is the following: 3751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HEADER (12 bytes) 3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * QUESTION (variable) 3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANSWER (variable) 3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AUTHORITY (variable) 3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ADDITIONNAL (variable) 3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the HEADER is made of: 3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ID : 16 : 16-bit unique query identification field 3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * QR : 1 : set to 0 for queries, and 1 for responses 3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Opcode : 4 : set to 0 for queries 3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AA : 1 : set to 0 for queries 3891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * TC : 1 : truncation flag, will be set to 0 in queries 3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * RD : 1 : recursion desired 3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * RA : 1 : recursion available (0 in queries) 3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Z : 3 : three reserved zero bits 3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * RCODE : 4 : response code (always 0=NOERROR in queries) 3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * QDCount: 16 : question count 3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANCount: 16 : Answer count (0 in queries) 3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * NSCount: 16: Authority Record count (0 in queries) 3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARCount: 16: Additionnal Record count (0 in queries) 4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the QUESTION is made of QDCount Question Record (QRs) 4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the ANSWER is made of ANCount RRs 4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the AUTHORITY is made of NSCount RRs 4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the ADDITIONNAL is made of ARCount RRs 4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each Question Record (QR) is made of: 4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * QNAME : variable : Query DNS NAME 4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * TYPE : 16 : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255) 4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * CLASS : 16 : class of query (IN=1) 4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each Resource Record (RR) is made of: 4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * NAME : variable : DNS NAME 4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * TYPE : 16 : type of query (A=1, PTR=12, MX=15, AAAA=28, ALL=255) 4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * CLASS : 16 : class of query (IN=1) 4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * TTL : 32 : seconds to cache this RR (0=none) 4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * RDLENGTH: 16 : size of RDDATA in bytes 4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * RDDATA : variable : RR data (depends on TYPE) 4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each QNAME contains a domain name encoded as a sequence of 'labels' 4221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * terminated by a zero. Each label has the following format: 4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LEN : 8 : lenght of label (MUST be < 64) 4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * NAME : 8*LEN : label length (must exclude dots) 4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A value of 0 in the encoding is interpreted as the 'root' domain and 4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * terminates the encoding. So 'www.android.com' will be encoded as: 4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * <3>www<7>android<3>com<0> 4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Where <n> represents the byte with value 'n' 4331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Each NAME reflects the QNAME of the question, but has a slightly more 4351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * complex encoding in order to provide message compression. This is achieved 4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * by using a 2-byte pointer, with format: 4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * TYPE : 2 : 0b11 to indicate a pointer, 0b01 and 0b10 are reserved 4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OFFSET : 14 : offset to another part of the DNS packet 4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The offset is relative to the start of the DNS packet and must point 4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A pointer terminates the encoding. 4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The NAME can be encoded in one of the following formats: 4451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - a sequence of simple labels terminated by 0 (like QNAMEs) 4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - a single pointer 4481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - a sequence of simple labels terminated by a pointer 4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A pointer shall always point to either a pointer of a sequence of 4511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * labels (which can themselves be terminated by either a 0 or a pointer) 4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The expanded length of a given domain name should not exceed 255 bytes. 4541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * NOTE: we don't parse the answer packets, so don't need to deal with NAME 4561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * records, only QNAMEs. 4571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_HEADER_SIZE 12 4601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_TYPE_A "\00\01" /* big-endian decimal 1 */ 4621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_TYPE_PTR "\00\014" /* big-endian decimal 12 */ 4631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_TYPE_MX "\00\017" /* big-endian decimal 15 */ 4641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */ 4651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */ 4661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */ 4681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecttypedef struct { 4701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* base; 4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* end; 4721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* cursor; 4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} DnsPacket; 4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 4761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_init( DnsPacket* packet, const uint8_t* buff, int bufflen ) 4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->base = buff; 4791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->end = buff + bufflen; 4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = buff; 4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_rewind( DnsPacket* packet ) 4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = packet->base; 4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 4901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_skip( DnsPacket* packet, int count ) 4911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 4921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->cursor + count; 4931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p > packet->end) 4951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = packet->end; 4961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p; 4981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 5011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_readInt16( DnsPacket* packet ) 5021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 5031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->cursor; 5041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p+2 > packet->end) 5061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 5071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p+2; 5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return (p[0]<< 8) | p[1]; 5101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY CHECKING 5131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/ 5141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* check bytes in a dns packet. returns 1 on success, 0 on failure. 5161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the cursor is only advanced in the case of success 5171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 5181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 5191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkBytes( DnsPacket* packet, int numBytes, const void* bytes ) 5201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 5211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->cursor; 5221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p + numBytes > packet->end) 5241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (memcmp(p, bytes, numBytes) != 0) 5271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p + numBytes; 5301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 5311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* parse and skip a given QNAME stored in a query packet, 5341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * from the current cursor position. returns 1 on success, 5351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * or 0 for malformed data. 5361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 5371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 5381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkQName( DnsPacket* packet ) 5391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 5401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->cursor; 5411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* end = packet->end; 5421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (;;) { 5441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int c; 5451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p >= end) 5471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 5481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c = *p++; 5501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c == 0) { 5521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p; 5531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 5541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we don't expect label compression in QNAMEs */ 5571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c >= 64) 5581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 5591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p += c; 5611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we rely on the bound check at the start 5621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * of the loop here */ 5631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* malformed data */ 5651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("malformed QNAME"); 5661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* parse and skip a given QR stored in a packet. 5701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * returns 1 on success, and 0 on failure 5711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 5721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 5731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkQR( DnsPacket* packet ) 5741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 5751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int len; 5761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!_dnsPacket_checkQName(packet)) 5781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* TYPE must be one of the things we support */ 5811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_A) && 5821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_PTR) && 5831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_MX) && 5841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_AAAA) && 5851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL)) 5861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 5871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("unsupported TYPE"); 5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* CLASS must be IN */ 5911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!_dnsPacket_checkBytes(packet, 2, DNS_CLASS_IN)) { 5921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("unsupported CLASS"); 5931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 5971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* check the header of a DNS Query packet, return 1 if it is one 6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * type of query we can cache, or 0 otherwise 6011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 6021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 6031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_checkQuery( DnsPacket* packet ) 6041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 6051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->base; 6061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int qdCount, anCount, dnCount, arCount; 6071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p + DNS_HEADER_SIZE > packet->end) { 6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("query packet too small"); 6101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 6111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* QR must be set to 0, opcode must be 0 and AA must be 0 */ 6141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* RA, Z, and RCODE must be 0 */ 6151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((p[2] & 0xFC) != 0 || p[3] != 0) { 6161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("query packet flags unsupported"); 6171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 6181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Note that we ignore the TC and RD bits here for the 6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * following reasons: 6221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 6231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - there is no point for a query packet sent to a server 6241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to have the TC bit set, but the implementation might 6251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * set the bit in the query buffer for its own needs 6261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * between a _resolv_cache_lookup and a 6271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * _resolv_cache_add. We should not freak out if this 6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * is the case. 6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - we consider that the result from a RD=0 or a RD=1 6311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * query might be different, hence that the RD bit 6321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * should be used to differentiate cached result. 6331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 6341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * this implies that RD is checked when hashing or 6351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * comparing query packets, but not TC 6361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 6371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* ANCOUNT, DNCOUNT and ARCOUNT must be 0 */ 6391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project qdCount = (p[4] << 8) | p[5]; 6401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project anCount = (p[6] << 8) | p[7]; 6411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dnCount = (p[8] << 8) | p[9]; 6421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project arCount = (p[10]<< 8) | p[11]; 6431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (anCount != 0 || dnCount != 0 || arCount != 0) { 6451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("query packet contains non-query records"); 6461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 6471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (qdCount == 0) { 6501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("query packet doesn't contain query record"); 6511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 6521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Check QDCOUNT QRs */ 6551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p + DNS_HEADER_SIZE; 6561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (;qdCount > 0; qdCount--) 6581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!_dnsPacket_checkQR(packet)) 6591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 6621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 6631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY DEBUGGING 6651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/ 6661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG 6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_bprintQName(DnsPacket* packet, char* bp, char* bend) 6691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 6701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->cursor; 6711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* end = packet->end; 6721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int first = 1; 6731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (;;) { 6751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int c; 6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p >= end) 6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 6791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c = *p++; 6811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c == 0) { 6831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p; 6841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return bp; 6851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we don't expect label compression in QNAMEs */ 6881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c >= 64) 6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 6901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (first) 6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project first = 0; 6931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else 6941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project bp = _bprint_c(bp, bend, '.'); 6951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project bp = _bprint_b(bp, bend, (const char*)p, c); 6971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p += c; 6991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we rely on the bound check at the start 7001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * of the loop here */ 7011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* malformed data */ 7031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project bp = _bprint_s(bp, bend, "<MALFORMED>"); 7041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return bp; 7051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 7081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_bprintQR(DnsPacket* packet, char* p, char* end) 7091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 7101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define QQ(x) { DNS_TYPE_##x, #x } 7111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project static const struct { 7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char* typeBytes; 7131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char* typeString; 7141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } qTypes[] = 7151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 7161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project QQ(A), QQ(PTR), QQ(MX), QQ(AAAA), QQ(ALL), 7171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { NULL, NULL } 7181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project }; 7191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int nn; 7201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char* typeString = NULL; 7211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* dump QNAME */ 7231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _dnsPacket_bprintQName(packet, p, end); 7241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* dump TYPE */ 7261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_s(p, end, " ("); 7271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (nn = 0; qTypes[nn].typeBytes != NULL; nn++) { 7291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (_dnsPacket_checkBytes(packet, 2, qTypes[nn].typeBytes)) { 7301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project typeString = qTypes[nn].typeString; 7311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 7321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (typeString != NULL) 7361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_s(p, end, typeString); 7371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else { 7381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int typeCode = _dnsPacket_readInt16(packet); 7391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint(p, end, "UNKNOWN-%d", typeCode); 7401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_c(p, end, ')'); 7431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* skip CLASS */ 7451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(packet, 2); 7461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 7471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* this function assumes the packet has already been checked */ 7501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic char* 7511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_bprintQuery( DnsPacket* packet, char* p, char* end ) 7521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 7531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int qdCount; 7541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (packet->base[2] & 0x1) { 7561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint_s(p, end, "RECURSIVE "); 7571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(packet, 4); 7601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project qdCount = _dnsPacket_readInt16(packet); 7611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(packet, 6); 7621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for ( ; qdCount > 0; qdCount-- ) { 7641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _dnsPacket_bprintQR(packet, p, end); 7651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return p; 7671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 7691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY HASHING SUPPORT 7721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** 7731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKET HAS ALREADY 7741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** BEEN SUCCESFULLY CHECKED. 7751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/ 7761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* use 32-bit FNV hash function */ 7781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define FNV_MULT 16777619U 7791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define FNV_BASIS 2166136261U 7801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned 7821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashBytes( DnsPacket* packet, int numBytes, 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 while (numBytes > 0 && p < end) { 7881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hash = hash*FNV_MULT ^ *p++; 7891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p; 7911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return hash; 7921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned 7961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashQName( DnsPacket* packet, unsigned hash ) 7971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 7981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p = packet->cursor; 7991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* end = packet->end; 8001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (;;) { 8021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int c; 8031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p >= end) { /* should not happen */ 8051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__); 8061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 8071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c = *p++; 8101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c == 0) 8121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 8131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c >= 64) { 8151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__); 8161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 8171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p + c >= end) { 8191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n", 8201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project __FUNCTION__); 8211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 8221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project while (c > 0) { 8241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hash = hash*FNV_MULT ^ *p++; 8251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c -= 1; 8261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project packet->cursor = p; 8291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return hash; 8301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned 8331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashQR( DnsPacket* packet, unsigned hash ) 8341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 8351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int len; 8361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hash = _dnsPacket_hashQName(packet, hash); 8381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hash = _dnsPacket_hashBytes(packet, 4, hash); /* TYPE and CLASS */ 8391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return hash; 8401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned 8431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_hashQuery( DnsPacket* packet ) 8441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 8451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned hash = FNV_BASIS; 8461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int count; 8471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_rewind(packet); 8481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we ignore the TC bit for reasons explained in 8501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * _dnsPacket_checkQuery(). 8511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 8521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * however we hash the RD bit to differentiate 8531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * between answers for recursive and non-recursive 8541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * queries. 8551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 8561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hash = hash*FNV_MULT ^ (packet->base[2] & 1); 8571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* assume: other flags are 0 */ 8591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(packet, 4); 8601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* read QDCOUNT */ 8621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project count = _dnsPacket_readInt16(packet); 8631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* assume: ANcount, NScount, ARcount are 0 */ 8651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(packet, 6); 8661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* hash QDCOUNT QRs */ 8681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for ( ; count > 0; count-- ) 8691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hash = _dnsPacket_hashQR(packet, hash); 8701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return hash; 8721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/** QUERY COMPARISON 8761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** 8771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKETS HAVE ALREADY 8781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ** BEEN SUCCESFULLY CHECKED. 8791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project **/ 8801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 8821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualDomainName( DnsPacket* pack1, DnsPacket* pack2 ) 8831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 8841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p1 = pack1->cursor; 8851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* end1 = pack1->end; 8861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p2 = pack2->cursor; 8871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* end2 = pack2->end; 8881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (;;) { 8901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int c1, c2; 8911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p1 >= end1 || p2 >= end2) { 8931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__); 8941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 8951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c1 = *p1++; 8971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c2 = *p2++; 8981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c1 != c2) 8991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 9001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c1 == 0) { 9021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pack1->cursor = p1; 9031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pack2->cursor = p2; 9041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 9051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (c1 >= 64) { 9071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__); 9081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 9091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((p1+c1 > end1) || (p2+c1 > end2)) { 9111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n", 9121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project __FUNCTION__); 9131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 9141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (memcmp(p1, p2, c1) != 0) 9161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 9171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p1 += c1; 9181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p2 += c1; 9191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we rely on the bound checks at the start of the loop */ 9201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* not the same, or one is malformed */ 9221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("different DN"); 9231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 9271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualBytes( DnsPacket* pack1, DnsPacket* pack2, int numBytes ) 9281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 9291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p1 = pack1->cursor; 9301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* p2 = pack2->cursor; 9311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ( p1 + numBytes > pack1->end || p2 + numBytes > pack2->end ) 9331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ( memcmp(p1, p2, numBytes) != 0 ) 9361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pack1->cursor += numBytes; 9391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pack2->cursor += numBytes; 9401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 9411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 9441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualQR( DnsPacket* pack1, DnsPacket* pack2 ) 9451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 9461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* compare domain name encoding + TYPE + CLASS */ 9471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ( !_dnsPacket_isEqualDomainName(pack1, pack2) || 9481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project !_dnsPacket_isEqualBytes(pack1, pack2, 2+2) ) 9491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 9521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 9551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dnsPacket_isEqualQuery( DnsPacket* pack1, DnsPacket* pack2 ) 9561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 9571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int count1, count2; 9581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* compare the headers, ignore most fields */ 9601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_rewind(pack1); 9611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_rewind(pack2); 9621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* compare RD, ignore TC, see comment in _dnsPacket_checkQuery */ 9641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((pack1->base[2] & 1) != (pack2->base[2] & 1)) { 9651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("different RD"); 9661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* assume: other flags are all 0 */ 9701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(pack1, 4); 9711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(pack2, 4); 9721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* compare QDCOUNT */ 9741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project count1 = _dnsPacket_readInt16(pack1); 9751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project count2 = _dnsPacket_readInt16(pack2); 9761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (count1 != count2 || count1 < 0) { 9771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("different QDCOUNT"); 9781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* assume: ANcount, NScount and ARcount are all 0 */ 9821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(pack1, 6); 9831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_skip(pack2, 6); 9841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* compare the QDCOUNT QRs */ 9861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for ( ; count1 > 0; count1-- ) { 9871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!_dnsPacket_isEqualQR(pack1, pack2)) { 9881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("different QR"); 9891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 9901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 1; 9931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 9961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 9971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 9981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 9991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 10001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 10011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 10021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* cache entry. for simplicity, 'hash' and 'hlink' are inlined in this 10041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * structure though they are conceptually part of the hash table. 10051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 10061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * similarly, mru_next and mru_prev are part of the global MRU list 10071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 10081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecttypedef struct Entry { 10091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned int hash; /* hash value */ 10101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct Entry* hlink; /* next in collision chain */ 10111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct Entry* mru_prev; 10121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct Entry* mru_next; 10131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* query; 10151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int querylen; 10161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const uint8_t* answer; 10171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int answerlen; 10183e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk time_t expires; /* time_t when the entry isn't valid any more */ 10193e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk int id; /* for debugging purpose */ 10201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} Entry; 10211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10223e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk/** 10233e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * Parse the answer records and find the smallest 10243e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * TTL among the answer records. 10253e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * 10263e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * The returned TTL is the number of seconds to 10273e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * keep the answer in the cache. 10283e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * 10293e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * In case of parse error zero (0) is returned which 10303e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk * indicates that the answer shall not be cached. 10313e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk */ 10323e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falkstatic u_long 10333e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falkanswer_getTTL(const void* answer, int answerlen) 10343e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk{ 10353e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk ns_msg handle; 10363e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk int ancount, n; 10373e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk u_long result, ttl; 10383e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk ns_rr rr; 10393e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 10403e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk result = 0; 10413e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (ns_initparse(answer, answerlen, &handle) >= 0) { 10423e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk // get number of answer records 10433e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk ancount = ns_msg_count(handle, ns_s_an); 10443e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk for (n = 0; n < ancount; n++) { 10453e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (ns_parserr(&handle, ns_s_an, n, &rr) == 0) { 10463e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk ttl = ns_rr_ttl(rr); 10473e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (n == 0 || ttl < result) { 10483e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk result = ttl; 10493e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 10503e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } else { 10513e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk XLOG("ns_parserr failed ancount no = %d. errno = %s\n", n, strerror(errno)); 10523e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 10533e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 10543e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } else { 10553e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk XLOG("ns_parserr failed. %s\n", strerror(errno)); 10563e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 10573e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 10583e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk XLOG("TTL = %d\n", result); 10593e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 10603e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk return result; 10613e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk} 10621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 10641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_free( Entry* e ) 10651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 10661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* everything is allocated in a single memory block */ 10671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e) { 10681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project free(e); 10691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 10701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 10711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic __inline__ void 10731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_mru_remove( Entry* e ) 10741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 10751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->mru_prev->mru_next = e->mru_next; 10761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->mru_next->mru_prev = e->mru_prev; 10771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 10781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic __inline__ void 10801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_mru_add( Entry* e, Entry* list ) 10811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 10821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* first = list->mru_next; 10831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->mru_next = first; 10851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->mru_prev = list; 10861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project list->mru_next = e; 10881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project first->mru_prev = e; 10891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 10901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* compute the hash of a given entry, this is a hash of most 10921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * data in the query (key) */ 10931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned 10941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_hash( const Entry* e ) 10951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 10961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project DnsPacket pack[1]; 10971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_init(pack, e->query, e->querylen); 10991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return _dnsPacket_hashQuery(pack); 11001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 11011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* initialize an Entry as a search key, this also checks the input query packet 11031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * returns 1 on success, or 0 in case of unsupported/malformed data */ 11041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 11051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_init_key( Entry* e, const void* query, int querylen ) 11061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 11071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project DnsPacket pack[1]; 11081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project memset(e, 0, sizeof(*e)); 11101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->query = query; 11121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->querylen = querylen; 11131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->hash = entry_hash(e); 11141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_init(pack, query, querylen); 11161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return _dnsPacket_checkQuery(pack); 11181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 11191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* allocate a new entry as a cache node */ 11211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Entry* 11221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_alloc( const Entry* init, const void* answer, int answerlen ) 11231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 11241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* e; 11251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int size; 11261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project size = sizeof(*e) + init->querylen + answerlen; 11281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e = calloc(size, 1); 11291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e == NULL) 11301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return e; 11311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->hash = init->hash; 11331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->query = (const uint8_t*)(e+1); 11341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->querylen = init->querylen; 11351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project memcpy( (char*)e->query, init->query, e->querylen ); 11371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->answer = e->query + e->querylen; 11391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->answerlen = answerlen; 11401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project memcpy( (char*)e->answer, answer, e->answerlen ); 11421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return e; 11441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 11451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectentry_equals( const Entry* e1, const Entry* e2 ) 11481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project DnsPacket pack1[1], pack2[1]; 11501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e1->querylen != e2->querylen) { 11521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 11531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 11541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_init(pack1, e1->query, e1->querylen); 11551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_init(pack2, e2->query, e2->querylen); 11561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return _dnsPacket_isEqualQuery(pack1, pack2); 11581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 11591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 11611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 11621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 11631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 11641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 11651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 11661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 11671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* We use a simple hash table with external collision lists 11691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for simplicity, the hash-table fields 'hash' and 'hlink' are 11701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * inlined in the Entry structure. 11711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 11721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1173a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* Maximum time for a thread to wait for an pending request */ 1174a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk#define PENDING_REQUEST_TIMEOUT 20; 1175a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1176a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falktypedef struct pending_req_info { 1177a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk unsigned int hash; 1178a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_cond_t cond; 1179a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk struct pending_req_info* next; 1180a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk} PendingReqInfo; 1181a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 11821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecttypedef struct resolv_cache { 11833a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk int max_entries; 11841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int num_entries; 11851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry mru_list; 11861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pthread_mutex_t lock; 11871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned generation; 11881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int last_id; 11893a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk Entry* entries; 1190a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk PendingReqInfo pending_requests; 11911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} Cache; 11921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 119323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falktypedef struct resolv_cache_info { 119423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char ifname[IF_NAMESIZE + 1]; 119523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct in_addr ifaddr; 119623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk Cache* cache; 119723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* next; 119823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* nameservers[MAXNS +1]; 119923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct addrinfo* nsaddrinfo[MAXNS + 1]; 12006f3222ea42f4d2335b089b3d2e5834a5c8c22ecaRobert Greenwalt char* domains; 120123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} CacheInfo; 12021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 12031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define HTABLE_VALID(x) ((x) != NULL && (x) != HTABLE_DELETED) 12041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 12051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 1206a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk_cache_flush_pending_requests_locked( struct resolv_cache* cache ) 1207a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{ 1208a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk struct pending_req_info *ri, *tmp; 1209a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (cache) { 1210a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = cache->pending_requests.next; 1211a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1212a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk while (ri) { 1213a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk tmp = ri; 1214a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = ri->next; 1215a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_cond_broadcast(&tmp->cond); 1216a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1217a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_cond_destroy(&tmp->cond); 1218a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk free(tmp); 1219a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1220a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1221a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk cache->pending_requests.next = NULL; 1222a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1223a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk} 1224a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1225a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* return 0 if no pending request is found matching the key 1226a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk * if a matching request is found the calling thread will wait 1227a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk * and return 1 when released */ 1228a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkstatic int 1229a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk_cache_check_pending_request_locked( struct resolv_cache* cache, Entry* key ) 1230a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{ 1231a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk struct pending_req_info *ri, *prev; 1232a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk int exist = 0; 1233a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1234a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (cache && key) { 1235a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = cache->pending_requests.next; 1236a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk prev = &cache->pending_requests; 1237a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk while (ri) { 1238a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (ri->hash == key->hash) { 1239a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk exist = 1; 1240a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk break; 1241a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1242a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk prev = ri; 1243a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = ri->next; 1244a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1245a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1246a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (!exist) { 1247a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = calloc(1, sizeof(struct pending_req_info)); 1248a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (ri) { 1249a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri->hash = key->hash; 1250a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_cond_init(&ri->cond, NULL); 1251a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk prev->next = ri; 1252a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1253a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } else { 1254a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk struct timespec ts = {0,0}; 1255a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ts.tv_sec = _time_now() + PENDING_REQUEST_TIMEOUT; 1256a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk int rv = pthread_cond_timedwait(&ri->cond, &cache->lock, &ts); 1257a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1258a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1259a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1260a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk return exist; 1261a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk} 1262a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1263a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* notify any waiting thread that waiting on a request 1264a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk * matching the key has been added to the cache */ 1265a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkstatic void 1266a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk_cache_notify_waiting_tid_locked( struct resolv_cache* cache, Entry* key ) 1267a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{ 1268a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk struct pending_req_info *ri, *prev; 1269a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1270a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (cache && key) { 1271a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = cache->pending_requests.next; 1272a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk prev = &cache->pending_requests; 1273a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk while (ri) { 1274a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (ri->hash == key->hash) { 1275a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_cond_broadcast(&ri->cond); 1276a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk break; 1277a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1278a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk prev = ri; 1279a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk ri = ri->next; 1280a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1281a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1282a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk // remove item from list and destroy 1283a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (ri) { 1284a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk prev->next = ri->next; 1285a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_cond_destroy(&ri->cond); 1286a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk free(ri); 1287a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1288a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1289a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk} 1290a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1291a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk/* notify the cache that the query failed */ 1292a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkvoid 1293a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk_resolv_cache_query_failed( struct resolv_cache* cache, 1294a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk const void* query, 1295a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk int querylen) 1296a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk{ 1297a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk Entry key[1]; 1298a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1299a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (cache && entry_init_key(key, query, querylen)) { 1300a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_mutex_lock(&cache->lock); 1301a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk _cache_notify_waiting_tid_locked(cache, key); 1302a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk pthread_mutex_unlock(&cache->lock); 1303a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1304a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk} 1305a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 1306a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falkstatic void 13071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_flush_locked( Cache* cache ) 13081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 13091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int nn; 13101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project time_t now = _time_now(); 13111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13123a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk for (nn = 0; nn < cache->max_entries; nn++) 13131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 13143a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk Entry** pnode = (Entry**) &cache->entries[nn]; 13151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project while (*pnode != NULL) { 13171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* node = *pnode; 13181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *pnode = node->hlink; 13191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project entry_free(node); 13201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 13211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 13221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1323a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk // flush pending request 1324a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk _cache_flush_pending_requests_locked(cache); 1325a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk 13261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project cache->mru_list.mru_next = cache->mru_list.mru_prev = &cache->mru_list; 13271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project cache->num_entries = 0; 13281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project cache->last_id = 0; 13291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("*************************\n" 13311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project "*** DNS CACHE FLUSHED ***\n" 13321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project "*************************"); 13331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 13341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13353a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk/* Return max number of entries allowed in the cache, 13363a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * i.e. cache size. The cache size is either defined 13373a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * by system property ro.net.dns_cache_size or by 13383a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * CONFIG_MAX_ENTRIES if system property not set 13393a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk * or set to invalid value. */ 13403a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falkstatic int 13413a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk_res_cache_get_max_entries( void ) 13423a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk{ 13433a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk int result = -1; 13443a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk char cache_size[PROP_VALUE_MAX]; 13453a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk 134652764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt const char* cache_mode = getenv("ANDROID_DNS_MODE"); 134752764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt 134852764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt if (cache_mode == NULL || strcmp(cache_mode, "local") != 0) { 134952764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt // Don't use the cache in local mode. This is used by the 135052764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt // proxy itself. 135152764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt // TODO - change this to 0 when all dns stuff uses proxy (5918973) 135252764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt XLOG("setup cache for non-cache process. size=1"); 135352764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt return 1; 135452764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt } 135552764f5546362d0ffab99afaffe8e8c7f21f8ef2Robert Greenwalt 13563a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk if (__system_property_get(DNS_CACHE_SIZE_PROP_NAME, cache_size) > 0) { 13573a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk result = atoi(cache_size); 13583a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk } 13593a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk 13603a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk // ro.net.dns_cache_size not set or set to negative value 13613a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk if (result <= 0) { 13623a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk result = CONFIG_MAX_ENTRIES; 13633a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk } 13643a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk 13653a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk XLOG("cache size: %d", result); 13663a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk return result; 13673a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk} 13683a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk 13697cc5666d94615d9249163dc7ac0f58c2614497ceJim Huangstatic struct resolv_cache* 13701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_resolv_cache_create( void ) 13711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 13721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct resolv_cache* cache; 13731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project cache = calloc(sizeof(*cache), 1); 13751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (cache) { 13763a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk cache->max_entries = _res_cache_get_max_entries(); 13773a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk cache->entries = calloc(sizeof(*cache->entries), cache->max_entries); 13783a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk if (cache->entries) { 13793a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk cache->generation = ~0U; 13803a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk pthread_mutex_init( &cache->lock, NULL ); 13813a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk cache->mru_list.mru_prev = cache->mru_list.mru_next = &cache->mru_list; 13823a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk XLOG("%s: cache created\n", __FUNCTION__); 13833a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk } else { 13843a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk free(cache); 13853a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk cache = NULL; 13863a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk } 13871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 13881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return cache; 13891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG 13931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 13941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_dump_query( const uint8_t* query, int querylen ) 13951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 13961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char temp[256], *p=temp, *end=p+sizeof(temp); 13971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project DnsPacket pack[1]; 13981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _dnsPacket_init(pack, query, querylen); 14001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _dnsPacket_bprintQuery(pack, p, end); 14011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("QUERY: %s", temp); 14021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 14031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_dump_mru( Cache* cache ) 14061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char temp[512], *p=temp, *end=p+sizeof(temp); 14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* e; 14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint(temp, end, "MRU LIST (%2d): ", cache->num_entries); 14111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) 14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = _bprint(p, end, " %d", e->id); 14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s", temp); 14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 14163e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 14173e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falkstatic void 14183e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk_dump_answer(const void* answer, int answerlen) 14193e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk{ 14203e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk res_state statep; 14213e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk FILE* fp; 14223e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk char* buf; 14233e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk int fileLen; 14243e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 14253e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk fp = fopen("/data/reslog.txt", "w+"); 14263e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (fp != NULL) { 14273e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk statep = __res_get_state(); 14283e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 14293e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk res_pquery(statep, answer, answerlen, fp); 14303e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk 14313e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk //Get file length 14323e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk fseek(fp, 0, SEEK_END); 14333e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk fileLen=ftell(fp); 14343e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk fseek(fp, 0, SEEK_SET); 14353e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk buf = (char *)malloc(fileLen+1); 14363e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (buf != NULL) { 14373e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk //Read file contents into buffer 14383e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk fread(buf, fileLen, 1, fp); 14393e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk XLOG("%s\n", buf); 14403e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk free(buf); 14413e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 14423e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk fclose(fp); 14433e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk remove("/data/reslog.txt"); 14443e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 14453e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk else { 14463e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk XLOG("_dump_answer: can't open file\n"); 14473e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 14483e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk} 14491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG 14521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project# define XLOG_QUERY(q,len) _dump_query((q), (len)) 14533e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk# define XLOG_ANSWER(a, len) _dump_answer((a), (len)) 14541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else 14551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project# define XLOG_QUERY(q,len) ((void)0) 14563e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk# define XLOG_ANSWER(a,len) ((void)0) 14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 14581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* This function tries to find a key within the hash table 14601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * In case of success, it will return a *pointer* to the hashed key. 14611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * In case of failure, it will return a *pointer* to NULL 14621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 14631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * So, the caller must check '*result' to check for success/failure. 14641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 14651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The main idea is that the result can later be used directly in 14661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * calls to _resolv_cache_add or _resolv_cache_remove as the 'lookup' 14671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * parameter. This makes the code simpler and avoids re-searching 14681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for the key position in the htable. 14691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 14701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The result of a lookup_p is only valid until you alter the hash 14711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * table. 14721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 14731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Entry** 14741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_lookup_p( Cache* cache, 14751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* key ) 14761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 14773a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk int index = key->hash % cache->max_entries; 14783a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk Entry** pnode = (Entry**) &cache->entries[ index ]; 14791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project while (*pnode != NULL) { 14811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* node = *pnode; 14821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (node == NULL) 14841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 14851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (node->hash == key->hash && entry_equals(node, key)) 14871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 14881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pnode = &node->hlink; 14901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return pnode; 14921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 14931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Add a new entry to the hash table. 'lookup' must be the 14951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * result of an immediate previous failed _lookup_p() call 14961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (i.e. with *lookup == NULL), and 'e' is the pointer to the 14971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * newly created entry 14981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 14991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 15001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_add_p( Cache* cache, 15011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry** lookup, 15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* e ) 15031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 15041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *lookup = e; 15051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->id = ++cache->last_id; 15061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project entry_mru_add(e, &cache->mru_list); 15071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project cache->num_entries += 1; 15081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: entry %d added (count=%d)", __FUNCTION__, 15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->id, cache->num_entries); 15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 15121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Remove an existing entry from the hash table, 15141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 'lookup' must be the result of an immediate previous 15151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * and succesful _lookup_p() call. 15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 15181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_remove_p( Cache* cache, 15191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry** lookup ) 15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* e = *lookup; 15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: entry %d removed (count=%d)", __FUNCTION__, 15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e->id, cache->num_entries-1); 15251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project entry_mru_remove(e); 15271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *lookup = e->hlink; 15281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project entry_free(e); 15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project cache->num_entries -= 1; 15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Remove the oldest entry from the hash table. 15331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_cache_remove_oldest( Cache* cache ) 15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* oldest = cache->mru_list.mru_prev; 15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry** lookup = _cache_lookup_p(cache, oldest); 15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (*lookup == NULL) { /* should not happen */ 15411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: OLDEST NOT IN HTABLE ?", __FUNCTION__); 15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15447f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt if (DEBUG) { 15457f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt XLOG("Cache full - removing oldest"); 15467f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt XLOG_QUERY(oldest->query, oldest->querylen); 15477f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt } 15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _cache_remove_p(cache, lookup); 15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1551dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund/* Remove all expired entries from the hash table. 1552dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund */ 1553dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlundstatic void _cache_remove_expired(Cache* cache) { 1554dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund Entry* e; 1555dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund time_t now = _time_now(); 1556dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund 1557dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund for (e = cache->mru_list.mru_next; e != &cache->mru_list;) { 1558dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund // Entry is old, remove 1559dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund if (now >= e->expires) { 1560dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund Entry** lookup = _cache_lookup_p(cache, e); 1561dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund if (*lookup == NULL) { /* should not happen */ 1562dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund XLOG("%s: ENTRY NOT IN HTABLE ?", __FUNCTION__); 1563dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund return; 1564dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund } 1565dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund e = e->mru_next; 1566dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund _cache_remove_p(cache, lookup); 1567dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund } else { 1568dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund e = e->mru_next; 1569dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund } 1570dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund } 1571dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund} 15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectResolvCacheStatus 15741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_resolv_cache_lookup( struct resolv_cache* cache, 15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const void* query, 15761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int querylen, 15771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project void* answer, 15781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int answersize, 15791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int *answerlen ) 15801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 15811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project DnsPacket pack[1]; 15821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry key[1]; 15831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int index; 15841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry** lookup; 15851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* e; 15861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project time_t now; 15871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ResolvCacheStatus result = RESOLV_CACHE_NOTFOUND; 15891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: lookup", __FUNCTION__); 15911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG_QUERY(query, querylen); 15921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* we don't cache malformed queries */ 15941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!entry_init_key(key, query, querylen)) { 15951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: unsupported query", __FUNCTION__); 15961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return RESOLV_CACHE_UNSUPPORTED; 15971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* lookup cache */ 15991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pthread_mutex_lock( &cache->lock ); 16001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* see the description of _lookup_p to understand this. 16021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the function always return a non-NULL pointer. 16031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 16041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project lookup = _cache_lookup_p(cache, key); 16051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e = *lookup; 16061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e == NULL) { 16081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG( "NOT IN CACHE"); 1609a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk // calling thread will wait if an outstanding request is found 1610a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk // that matching this query 1611a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (!_cache_check_pending_request_locked(cache, key)) { 1612a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk goto Exit; 1613a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } else { 1614a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk lookup = _cache_lookup_p(cache, key); 1615a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk e = *lookup; 1616a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk if (e == NULL) { 1617a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk goto Exit; 1618a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 1619a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk } 16201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project now = _time_now(); 16231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* remove stale entries here */ 16253e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (now >= e->expires) { 16261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG( " NOT IN CACHE (STALE ENTRY %p DISCARDED)", *lookup ); 16277f84da69f86ed9daf610c8d1129392ba3f7c4405Robert Greenwalt XLOG_QUERY(e->query, e->querylen); 16281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _cache_remove_p(cache, lookup); 16291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project goto Exit; 16301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *answerlen = e->answerlen; 16331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e->answerlen > answersize) { 16341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* NOTE: we return UNSUPPORTED if the answer buffer is too short */ 16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project result = RESOLV_CACHE_UNSUPPORTED; 16361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG(" ANSWER TOO LONG"); 16371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project goto Exit; 16381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project memcpy( answer, e->answer, e->answerlen ); 16411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* bump up this entry to the top of the MRU list */ 16431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e != cache->mru_list.mru_next) { 16441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project entry_mru_remove( e ); 16451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project entry_mru_add( e, &cache->mru_list ); 16461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG( "FOUND IN CACHE entry=%p", e ); 16491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project result = RESOLV_CACHE_FOUND; 16501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectExit: 16521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pthread_mutex_unlock( &cache->lock ); 16531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return result; 16541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 16551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectvoid 16581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_resolv_cache_add( struct resolv_cache* cache, 16591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const void* query, 16601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int querylen, 16611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const void* answer, 16621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int answerlen ) 16631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 16641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry key[1]; 16651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry* e; 16661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Entry** lookup; 16673e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk u_long ttl; 16681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* don't assume that the query has already been cached 16701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 16711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (!entry_init_key( key, query, querylen )) { 16721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG( "%s: passed invalid query ?", __FUNCTION__); 16731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 16741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pthread_mutex_lock( &cache->lock ); 16771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG( "%s: query:", __FUNCTION__ ); 16791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG_QUERY(query,querylen); 16803e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk XLOG_ANSWER(answer, answerlen); 16811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG_DATA 16821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG( "answer:"); 16831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG_BYTES(answer,answerlen); 16841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project lookup = _cache_lookup_p(cache, key); 16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e = *lookup; 16881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e != NULL) { /* should not happen */ 16901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD", 16911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project __FUNCTION__, e); 16921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project goto Exit; 16931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16953a4910c6161d57e40ba01cc9693f1a4514892b03Mattias Falk if (cache->num_entries >= cache->max_entries) { 1696dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund _cache_remove_expired(cache); 1697dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund if (cache->num_entries >= cache->max_entries) { 1698dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund _cache_remove_oldest(cache); 1699dd16182b4b3f107d5c26015622c1cb8c06619ad7Anders Fredlund } 17001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* need to lookup again */ 17011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project lookup = _cache_lookup_p(cache, key); 17021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project e = *lookup; 17031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (e != NULL) { 17041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD", 17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project __FUNCTION__, e); 17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project goto Exit; 17071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17103e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk ttl = answer_getTTL(answer, answerlen); 17113e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (ttl > 0) { 17123e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk e = entry_alloc(key, answer, answerlen); 17133e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk if (e != NULL) { 17143e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk e->expires = ttl + _time_now(); 17153e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk _cache_add_p(cache, lookup, e); 17163e0c5102e6d57e5b7296f95e1b318fda6114b48eMattias Falk } 17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if DEBUG 17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _cache_dump_mru(cache); 17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 17211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectExit: 1722a59cfcfd0821f51ee77f13105099f700ccbf9252Mattias Falk _cache_notify_waiting_tid_locked(cache, key); 17231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project pthread_mutex_unlock( &cache->lock ); 17241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 17251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 17271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 17281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 17291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 17301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/***** *****/ 17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 17321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/****************************************************************************/ 17331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic pthread_once_t _res_cache_once; 17351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 173623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk// Head of the list of caches. Protected by _res_cache_list_lock. 173723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info _res_cache_list; 173823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 173923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk// name of the current default inteface 174023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic char _res_default_ifname[IF_NAMESIZE + 1]; 174123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 174223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk// lock protecting everything in the _resolve_cache_info structs (next ptr, etc) 174323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic pthread_mutex_t _res_cache_list_lock; 174423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 174523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 174623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* lookup the default interface name */ 174723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic char *_get_default_iface_locked(); 174823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* insert resolv_cache_info into the list of resolv_cache_infos */ 174923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void _insert_cache_info_locked(struct resolv_cache_info* cache_info); 175023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* creates a resolv_cache_info */ 175123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info* _create_cache_info( void ); 175223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* gets cache associated with an interface name, or NULL if none exists */ 175323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache* _find_named_cache_locked(const char* ifname); 175423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* gets a resolv_cache_info associated with an interface name, or NULL if not found */ 175523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info* _find_cache_info_locked(const char* ifname); 175623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* free dns name server list of a resolv_cache_info structure */ 175723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void _free_nameservers(struct resolv_cache_info* cache_info); 175823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* look up the named cache, and creates one if needed */ 175923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache* _get_res_cache_for_iface_locked(const char* ifname); 176023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* empty the named cache */ 176123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void _flush_cache_for_iface_locked(const char* ifname); 176223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* empty the nameservers set for the named cache */ 176323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void _free_nameservers_locked(struct resolv_cache_info* cache_info); 176423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* lookup the namserver for the name interface */ 176523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic int _get_nameserver_locked(const char* ifname, int n, char* addr, int addrLen); 176623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* lookup the addr of the nameserver for the named interface */ 176723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct addrinfo* _get_nameserver_addr_locked(const char* ifname, int n); 176823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk/* lookup the inteface's address */ 176923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct in_addr* _get_addr_locked(const char * ifname); 177023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 177123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 177223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 17731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void 177423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_res_cache_init(void) 17751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 17761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char* env = getenv(CONFIG_ENV); 17771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (env && atoi(env) == 0) { 17791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* the cache is disabled */ 17801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 17811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 178323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk memset(&_res_default_ifname, 0, sizeof(_res_default_ifname)); 178423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk memset(&_res_cache_list, 0, sizeof(_res_cache_list)); 178523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_init(&_res_cache_list_lock, NULL); 17861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 17871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstruct resolv_cache* 178923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk__get_res_cache(void) 179023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 179123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache *cache; 179223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 179323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 179423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 179523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 179623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 179723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* ifname = _get_default_iface_locked(); 179823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 179923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // if default interface not set then use the first cache 180023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // associated with an interface as the default one. 180123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (ifname[0] == '\0') { 180223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _res_cache_list.next; 180323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk while (cache_info) { 180423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info->ifname[0] != '\0') { 180523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ifname = cache_info->ifname; 18069363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt break; 180723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 180823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 180923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info = cache_info->next; 181023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 181123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 181223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache = _get_res_cache_for_iface_locked(ifname); 181323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 181423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 181523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk XLOG("_get_res_cache. default_ifname = %s\n", ifname); 181623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return cache; 181723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 181823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 181923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache* 182023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_get_res_cache_for_iface_locked(const char* ifname) 18211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 182223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (ifname == NULL) 182323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return NULL; 182423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 182523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache* cache = _find_named_cache_locked(ifname); 182623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (!cache) { 182723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _create_cache_info(); 182823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info) { 182923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache = _resolv_cache_create(); 183023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache) { 183123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk int len = sizeof(cache_info->ifname); 183223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info->cache = cache; 183323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk strncpy(cache_info->ifname, ifname, len - 1); 183423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info->ifname[len - 1] = '\0'; 183523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 183623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _insert_cache_info_locked(cache_info); 183723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } else { 183823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk free(cache_info); 183923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 184023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 184123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 184223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return cache; 18431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 18441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectvoid 184623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_cache_reset(unsigned generation) 18471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 18481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project XLOG("%s: generation=%d", __FUNCTION__, generation); 18491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 185023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 185123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 185223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 185323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* ifname = _get_default_iface_locked(); 185423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // if default interface not set then use the first cache 185523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // associated with an interface as the default one. 185623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // Note: Copied the code from __get_res_cache since this 185723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // method will be deleted/obsolete when cache per interface 185823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // implemented all over 185923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (ifname[0] == '\0') { 186023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _res_cache_list.next; 186123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk while (cache_info) { 186223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info->ifname[0] != '\0') { 186323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ifname = cache_info->ifname; 18649363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt break; 186523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 186623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 186723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info = cache_info->next; 186823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 186923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 187023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache* cache = _get_res_cache_for_iface_locked(ifname); 187123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 18729363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt if (cache != NULL) { 18739363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt pthread_mutex_lock( &cache->lock ); 18749363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt if (cache->generation != generation) { 18759363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt _cache_flush_locked(cache); 18769363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt cache->generation = generation; 18779363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt } 18789363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt pthread_mutex_unlock( &cache->lock ); 187923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 188023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 188123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 188223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 188323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 188423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid 188523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_flush_cache_for_default_iface(void) 188623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 188723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* ifname; 188823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 188923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 189023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 189123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 189223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ifname = _get_default_iface_locked(); 189323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _flush_cache_for_iface_locked(ifname); 189423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 189523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 189623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 189723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 189823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid 189923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_flush_cache_for_iface(const char* ifname) 190023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 190123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 190223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 190323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 190423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _flush_cache_for_iface_locked(ifname); 190523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 190623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 190723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 190823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 190923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void 191023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_flush_cache_for_iface_locked(const char* ifname) 191123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 191223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache* cache = _find_named_cache_locked(ifname); 191323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache) { 191423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&cache->lock); 191523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _cache_flush_locked(cache); 191623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&cache->lock); 191723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 191823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 191923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 192023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info* 192123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_create_cache_info(void) 192223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 192323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info; 192423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 192523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info = calloc(sizeof(*cache_info), 1); 192623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return cache_info; 192723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 192823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 192923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void 193023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_insert_cache_info_locked(struct resolv_cache_info* cache_info) 193123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 193223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* last; 193323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 193423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk for (last = &_res_cache_list; last->next; last = last->next); 193523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 193623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk last->next = cache_info; 193723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 193823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 193923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 194023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache* 194123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_find_named_cache_locked(const char* ifname) { 194223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 194323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* info = _find_cache_info_locked(ifname); 194423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 194523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (info != NULL) return info->cache; 194623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 194723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return NULL; 194823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 194923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 195023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct resolv_cache_info* 195123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_find_cache_info_locked(const char* ifname) 195223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 195323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (ifname == NULL) 195423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return NULL; 195523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 195623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _res_cache_list.next; 19571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 195823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk while (cache_info) { 195923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (strcmp(cache_info->ifname, ifname) == 0) { 196023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk break; 196123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 196223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 196323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info = cache_info->next; 196423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 196523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return cache_info; 196623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 196723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 196823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic char* 196923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_get_default_iface_locked(void) 197023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 197123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* iface = _res_default_ifname; 197223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 197323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return iface; 197423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 197523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 197623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid 197723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_set_default_iface(const char* ifname) 197823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 197923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk XLOG("_resolv_set_default_if ifname %s\n",ifname); 198023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 198123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 198223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 198323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 198423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk int size = sizeof(_res_default_ifname); 198523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk memset(_res_default_ifname, 0, size); 198623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk strncpy(_res_default_ifname, ifname, size - 1); 198723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _res_default_ifname[size - 1] = '\0'; 198823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 198923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 199023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 199123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 199223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid 19936f3222ea42f4d2335b089b3d2e5834a5c8c22ecaRobert Greenwalt_resolv_set_nameservers_for_iface(const char* ifname, char** servers, int numservers, 19946f3222ea42f4d2335b089b3d2e5834a5c8c22ecaRobert Greenwalt const char *domains) 199523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 199623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk int i, rt, index; 199723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct addrinfo hints; 199823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char sbuf[NI_MAXSERV]; 199923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 200023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 200123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 200223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 200323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // creates the cache if not created 200423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _get_res_cache_for_iface_locked(ifname); 200523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 200623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _find_cache_info_locked(ifname); 200723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 200823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info != NULL) { 200923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk // free current before adding new 201023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk _free_nameservers_locked(cache_info); 201123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 201223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk memset(&hints, 0, sizeof(hints)); 201323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk hints.ai_family = PF_UNSPEC; 201423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 201523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk hints.ai_flags = AI_NUMERICHOST; 201623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk sprintf(sbuf, "%u", NAMESERVER_PORT); 201723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 201823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk index = 0; 201923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk for (i = 0; i < numservers && i < MAXNS; i++) { 202023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk rt = getaddrinfo(servers[i], sbuf, &hints, &cache_info->nsaddrinfo[index]); 202123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (rt == 0) { 202223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info->nameservers[index] = strdup(servers[i]); 202323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk index++; 202423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } else { 202523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info->nsaddrinfo[index] = NULL; 202623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 202723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 20286f3222ea42f4d2335b089b3d2e5834a5c8c22ecaRobert Greenwalt cache_info->domains = strdup(domains); 202923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 203023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 203123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 203223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 203323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic void 203423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_free_nameservers_locked(struct resolv_cache_info* cache_info) 203523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 203623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk int i; 203723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk for (i = 0; i <= MAXNS; i++) { 203823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk free(cache_info->nameservers[i]); 203923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info->nameservers[i] = NULL; 20409363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt if (cache_info->nsaddrinfo[i] != NULL) { 20419363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt freeaddrinfo(cache_info->nsaddrinfo[i]); 20429363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt cache_info->nsaddrinfo[i] = NULL; 20439363d91218c7ed727c36ffaf82ff28d7755375aeRobert Greenwalt } 204423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 204523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 204623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 204723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkint 204823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_cache_get_nameserver(int n, char* addr, int addrLen) 204923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 205023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char *ifname; 205123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk int result = 0; 205223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 205323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 205423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 205523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 205623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ifname = _get_default_iface_locked(); 205723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk result = _get_nameserver_locked(ifname, n, addr, addrLen); 205823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 205923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 206023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return result; 206123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 206223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 206323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic int 206423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_get_nameserver_locked(const char* ifname, int n, char* addr, int addrLen) 206523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 206623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk int len = 0; 206723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* ns; 206823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info; 206923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 207023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (n < 1 || n > MAXNS || !addr) 207123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return 0; 207223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 207323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info = _find_cache_info_locked(ifname); 207423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info) { 207523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ns = cache_info->nameservers[n - 1]; 207623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (ns) { 207723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk len = strlen(ns); 207823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (len < addrLen) { 207923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk strncpy(addr, ns, len); 208023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk addr[len] = '\0'; 208123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } else { 208223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk len = 0; 208323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 208423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 208523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 208623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 208723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return len; 208823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 208923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 209023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstruct addrinfo* 209123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_cache_get_nameserver_addr(int n) 209223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 209323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct addrinfo *result; 209423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* ifname; 209523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 209623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 209723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 209823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 209923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ifname = _get_default_iface_locked(); 210023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 210123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk result = _get_nameserver_addr_locked(ifname, n); 210223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 210323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return result; 210423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 210523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 210623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct addrinfo* 210723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_get_nameserver_addr_locked(const char* ifname, int n) 210823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 210923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct addrinfo* ai = NULL; 211023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info; 211123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 211223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (n < 1 || n > MAXNS) 211323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return NULL; 211423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 211523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk cache_info = _find_cache_info_locked(ifname); 211623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info) { 211723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ai = cache_info->nsaddrinfo[n - 1]; 211823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 211923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return ai; 212023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 212123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 212223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkvoid 212323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_set_addr_of_iface(const char* ifname, struct in_addr* addr) 212423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 212523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 212623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 212723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _find_cache_info_locked(ifname); 212823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info) { 212923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk memcpy(&cache_info->ifaddr, addr, sizeof(*addr)); 213023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 213123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (DEBUG) { 213223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* addr_s = inet_ntoa(cache_info->ifaddr); 213323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk XLOG("address of interface %s is %s\n", ifname, addr_s); 213423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 213523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk } 213623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 213723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 213823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 213923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstruct in_addr* 214023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_get_addr_of_default_iface(void) 214123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 214223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct in_addr* ai = NULL; 214323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk char* ifname; 214423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 214523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 214623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 214723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ifname = _get_default_iface_locked(); 214823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ai = _get_addr_locked(ifname); 214923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 215023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 215123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return ai; 215223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 215323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 215423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstruct in_addr* 215523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_resolv_get_addr_of_iface(const char* ifname) 215623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 215723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct in_addr* ai = NULL; 215823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 215923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_once(&_res_cache_once, _res_cache_init); 216023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_lock(&_res_cache_list_lock); 216123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk ai =_get_addr_locked(ifname); 216223d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk pthread_mutex_unlock(&_res_cache_list_lock); 216323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return ai; 216423d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk} 216523d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk 216623d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falkstatic struct in_addr* 216723d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk_get_addr_locked(const char * ifname) 216823d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk{ 216923d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk struct resolv_cache_info* cache_info = _find_cache_info_locked(ifname); 217023d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk if (cache_info) { 217123d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return &cache_info->ifaddr; 21721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 217323d3e6b21b4b6debff87da8c6558495e564cc38eMattias Falk return NULL; 21741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2175