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 // Return the names of the interfaces used by a given network.
80 std::vector<std::string> resolv_get_interface_names(int netid);
81 
82 // Sets name servers for a given network.
83 int resolv_set_nameservers(const aidl::android::net::ResolverParamsParcel& params);
84 
85 // Sets options for a given network.
86 int resolv_set_options(unsigned netid, const aidl::android::net::ResolverOptionsParcel& options);
87 
88 // Creates the cache associated with the given network.
89 int resolv_create_cache_for_net(unsigned netid);
90 
91 // Deletes the cache associated with the given network.
92 void resolv_delete_cache_for_net(unsigned netid);
93 
94 // Flushes the cache associated with the given network.
95 int resolv_flush_cache_for_net(unsigned netid);
96 
97 // Get transport types to a given network.
98 android::net::NetworkType resolv_get_network_types_for_net(unsigned netid);
99 
100 // Return true if the pass-in network types support mdns.
101 bool is_mdns_supported_transport_types(const std::vector<int32_t>& transportTypes);
102 
103 // Return true if the network can support mdns resolution.
104 bool is_mdns_supported_network(unsigned netid);
105 
106 // Return true if the cache is existent in the given network, false otherwise.
107 bool has_named_cache(unsigned netid);
108 
109 // For test only.
110 // Get the expiration time of a cache entry. Return 0 on success; otherwise, an negative error is
111 // returned if the expiration time can't be acquired.
112 int resolv_cache_get_expiration(unsigned netid, std::span<const uint8_t> query, time_t* expiration);
113 
114 // Set addresses to DnsStats for a given network.
115 int resolv_stats_set_addrs(unsigned netid, android::net::Protocol proto,
116                            const std::vector<std::string>& addrs, int port);
117 
118 // Add a statistics record to DnsStats for a given network.
119 bool resolv_stats_add(unsigned netid, const android::netdutils::IPSockAddr& server,
120                       const android::net::DnsQueryEvent* record);
121 
122 /* Retrieve a local copy of the stats for the given netid. The buffer must have space for
123  * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
124  */
125 int resolv_cache_get_resolver_stats(
126         unsigned netid, res_params* params, res_stats stats[MAXNS],
127         const std::vector<android::netdutils::IPSockAddr>& serverSockAddrs);
128 
129 /* Add a sample to the shared struct for the given netid and server, provided that the
130  * revision_id of the stored servers has not changed.
131  */
132 void resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id,
133                                             const android::netdutils::IPSockAddr& serverSockAddr,
134                                             const res_sample& sample, int max_samples);
135 
136 // Convert TRANSPORT_* to NT_*. It's public only for unit testing.
137 android::net::NetworkType convert_network_type(const std::vector<int32_t>& transportTypes);
138 
139 // Dump net configuration log for a given network.
140 void resolv_netconfig_dump(android::netdutils::DumpWriter& dw, unsigned netid);
141 
142 // Get the maximum cache size of a network.
143 // Return positive value on success, -1 on failure.
144 int resolv_get_max_cache_entries(unsigned netid);
145 
146 // Return true if the enforceDnsUid is enabled on the network.
147 bool resolv_is_enforceDnsUid_enabled_network(unsigned netid);
148 
149 // Return true if the network is metered.
150 bool resolv_is_metered_network(unsigned netid);
151