blob: 9a9787ede419c1fdc55ec8a2f67f93395203d456 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Bernie Innocentie71a28a2019-05-29 00:42:35 +090028
29#pragma once
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090030
Luke Huangba7bef92018-12-26 16:53:03 +080031#include "netd_resolv/resolv.h"
32
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090033#include <stddef.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090034
Luke Huange126fbe2019-07-20 17:36:30 +080035#include <unordered_map>
Bernie Innocentia4be3c82019-03-28 19:25:11 +090036#include <vector>
37
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090038struct __res_state;
Mike Yud1ec2542019-06-06 17:17:53 +080039struct resolv_cache;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090040
Luke Huange126fbe2019-07-20 17:36:30 +080041constexpr int DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY = -1;
42
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090043/* sets the name server addresses to the provided res_state structure. The
44 * name servers are retrieved from the cache which is associated
45 * with the network to which the res_state structure is associated */
Bernie Innocentia74088b2018-09-13 16:00:42 +090046void _resolv_populate_res_for_net(struct __res_state* statp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090047
Bernie Innocentia4be3c82019-03-28 19:25:11 +090048std::vector<unsigned> resolv_list_caches();
49
Luke Huange126fbe2019-07-20 17:36:30 +080050std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid);
51uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code);
52
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090053typedef enum {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090054 RESOLV_CACHE_UNSUPPORTED, /* the cache can't handle that kind of queries */
55 /* or the answer buffer is too small */
56 RESOLV_CACHE_NOTFOUND, /* the cache doesn't know about this query */
Luke Huangba7bef92018-12-26 16:53:03 +080057 RESOLV_CACHE_FOUND, /* the cache found the answer */
58 RESOLV_CACHE_SKIP /* Don't do anything on cache */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090059} ResolvCacheStatus;
60
Mike Yud1ec2542019-06-06 17:17:53 +080061ResolvCacheStatus resolv_cache_lookup(unsigned netid, const void* query, int querylen, void* answer,
62 int answersize, int* answerlen, uint32_t flags);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090063
Mike Yud1ec2542019-06-06 17:17:53 +080064// add a (query,answer) to the cache. If the pair has been in the cache, no new entry will be added
65// in the cache.
66int resolv_cache_add(unsigned netid, const void* query, int querylen, const void* answer,
67 int answerlen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090068
69/* Notify the cache a request failed */
Luke Huangba7bef92018-12-26 16:53:03 +080070void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen, uint32_t flags);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090071
Bernie Innocentie71a28a2019-05-29 00:42:35 +090072// Sets name servers for a given network.
Mike Yuc7649d12019-05-22 15:28:07 +080073int resolv_set_nameservers(unsigned netid, const std::vector<std::string>& servers,
74 const std::vector<std::string>& domains, const res_params& params);
Bernie Innocentie71a28a2019-05-29 00:42:35 +090075
76// Creates the cache associated with the given network.
77int resolv_create_cache_for_net(unsigned netid);
78
79// Deletes the cache associated with the given network.
80void resolv_delete_cache_for_net(unsigned netid);
Mike Yud1ec2542019-06-06 17:17:53 +080081
82// For test only.
83// Return true if the cache is existent in the given network, false otherwise.
84bool has_named_cache(unsigned netid);
85
86// For test only.
87// Get the expiration time of a cache entry. Return 0 on success; otherwise, an negative error is
88// returned if the expiration time can't be acquired.
Mike Yuc7649d12019-05-22 15:28:07 +080089int resolv_cache_get_expiration(unsigned netid, const std::vector<char>& query, time_t* expiration);