• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <span>
32 #include <unordered_map>
33 #include <vector>
34 
35 #include <aidl/android/net/IDnsResolver.h>
36 #include <aidl/android/net/ResolverOptionsParcel.h>
37 
38 #include <netdutils/DumpWriter.h>
39 #include <netdutils/InternetAddresses.h>
40 #include <stats.pb.h>
41 
42 #include "ResolverStats.h"
43 #include "params.h"
44 #include "stats.h"
45 
46 // Sets the name server addresses to the provided ResState.
47 // The name servers are retrieved from the cache which is associated
48 // with the network to which ResState is associated.
49 struct ResState;
50 void resolv_populate_res_for_net(ResState* statp);
51 
52 std::vector<unsigned> resolv_list_caches();
53 
54 std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid, bool is_mdns);
55 uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code, bool is_mdns);
56 
57 typedef enum {
58     RESOLV_CACHE_UNSUPPORTED, /* the cache can't handle that kind of queries */
59                               /* or the answer buffer is too small */
60     RESOLV_CACHE_NOTFOUND,    /* the cache doesn't know about this query */
61     RESOLV_CACHE_FOUND,       /* the cache found the answer */
62     RESOLV_CACHE_SKIP         /* Don't do anything on cache */
63 } ResolvCacheStatus;
64 
65 ResolvCacheStatus resolv_cache_lookup(unsigned netid, std::span<const uint8_t> query,
66                                       std::span<uint8_t> answer, int* answerlen, uint32_t flags);
67 
68 // add a (query,answer) to the cache. If the pair has been in the cache, no new entry will be added
69 // in the cache.
70 int resolv_cache_add(unsigned netid, std::span<const uint8_t> query,
71                      std::span<const uint8_t> answer);
72 
73 /* Notify the cache a request failed */
74 void _resolv_cache_query_failed(unsigned netid, std::span<const uint8_t> query, uint32_t flags);
75 
76 // Get a customized table for a given network.
77 std::vector<std::string> getCustomizedTableByName(const size_t netid, const char* hostname);
78 
79 // Sets name servers for a given network.
80 // TODO: Pass all of ResolverParamsParcel and remove the res_params argument.
81 int resolv_set_nameservers(unsigned netid, const std::vector<std::string>& servers,
82                            const std::vector<std::string>& domains, const res_params& params,
83                            std::optional<aidl::android::net::ResolverOptionsParcel> resolverOptions,
84                            const std::vector<int32_t>& transportTypes = {});
85 
86 // Sets options for a given network.
87 int resolv_set_options(unsigned netid, const aidl::android::net::ResolverOptionsParcel& options);
88 
89 // Creates the cache associated with the given network.
90 int resolv_create_cache_for_net(unsigned netid);
91 
92 // Deletes the cache associated with the given network.
93 void resolv_delete_cache_for_net(unsigned netid);
94 
95 // Flushes the cache associated with the given network.
96 int resolv_flush_cache_for_net(unsigned netid);
97 
98 // Get transport types to a given network.
99 android::net::NetworkType resolv_get_network_types_for_net(unsigned netid);
100 
101 // Return true if the pass-in network types support mdns.
102 bool is_mdns_supported_transport_types(const std::vector<int32_t>& transportTypes);
103 
104 // Return true if the network can support mdns resolution.
105 bool is_mdns_supported_network(unsigned netid);
106 
107 // Return true if the cache is existent in the given network, false otherwise.
108 bool has_named_cache(unsigned netid);
109 
110 // For test only.
111 // Get the expiration time of a cache entry. Return 0 on success; otherwise, an negative error is
112 // returned if the expiration time can't be acquired.
113 int resolv_cache_get_expiration(unsigned netid, std::span<const uint8_t> query, time_t* expiration);
114 
115 // Set addresses to DnsStats for a given network.
116 int resolv_stats_set_addrs(unsigned netid, android::net::Protocol proto,
117                            const std::vector<std::string>& addrs, int port);
118 
119 // Add a statistics record to DnsStats for a given network.
120 bool resolv_stats_add(unsigned netid, const android::netdutils::IPSockAddr& server,
121                       const android::net::DnsQueryEvent* record);
122 
123 /* Retrieve a local copy of the stats for the given netid. The buffer must have space for
124  * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
125  */
126 int resolv_cache_get_resolver_stats(
127         unsigned netid, res_params* params, res_stats stats[MAXNS],
128         const std::vector<android::netdutils::IPSockAddr>& serverSockAddrs);
129 
130 /* Add a sample to the shared struct for the given netid and server, provided that the
131  * revision_id of the stored servers has not changed.
132  */
133 void resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id,
134                                             const android::netdutils::IPSockAddr& serverSockAddr,
135                                             const res_sample& sample, int max_samples);
136 
137 // Convert TRANSPORT_* to NT_*. It's public only for unit testing.
138 android::net::NetworkType convert_network_type(const std::vector<int32_t>& transportTypes);
139 
140 // Dump net configuration log for a given network.
141 void resolv_netconfig_dump(android::netdutils::DumpWriter& dw, unsigned netid);
142