xref: /aosp_15_r20/external/cronet/net/dns/dns_client.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_DNS_CLIENT_H_
6 #define NET_DNS_DNS_CLIENT_H_
7 
8 #include <memory>
9 #include <optional>
10 
11 #include "base/values.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_export.h"
14 #include "net/base/rand_callback.h"
15 #include "net/dns/dns_config.h"
16 #include "net/dns/dns_hosts.h"
17 #include "net/dns/public/dns_config_overrides.h"
18 
19 namespace url {
20 
21 class SchemeHostPort;
22 
23 }  // namespace url
24 
25 namespace net {
26 
27 class AddressSorter;
28 class ClientSocketFactory;
29 class DnsSession;
30 class DnsTransactionFactory;
31 class NetLog;
32 class ResolveContext;
33 
34 // Entry point for HostResolverManager to interact with the built-in async
35 // resolver, as implemented by DnsTransactionFactory. Manages configuration and
36 // status of the resolver.
37 class NET_EXPORT DnsClient {
38  public:
39   static const int kMaxInsecureFallbackFailures = 16;
40 
41   virtual ~DnsClient() = default;
42 
43   // Returns true if the DnsClient is able and allowed to make secure DNS
44   // transactions and DoH probe runners. If false, secure transactions and DoH
45   // probe runners should not be created.
46   virtual bool CanUseSecureDnsTransactions() const = 0;
47 
48   // Returns true if the DnsClient is able and allowed to make insecure DNS
49   // transactions. If false, insecure transactions should not be created. Will
50   // always be false unless SetInsecureEnabled(true) has been called.
51   virtual bool CanUseInsecureDnsTransactions() const = 0;
52   virtual bool CanQueryAdditionalTypesViaInsecureDns() const = 0;
53   virtual void SetInsecureEnabled(bool enabled,
54                                   bool additional_types_enabled) = 0;
55 
56   // When true, DoH should not be used in AUTOMATIC mode since no DoH servers
57   // have a successful probe state.
58   virtual bool FallbackFromSecureTransactionPreferred(
59       ResolveContext* context) const = 0;
60 
61   // When true, insecure DNS transactions should not be used when reasonable
62   // fallback alternatives, e.g. system resolution can be used instead.
63   virtual bool FallbackFromInsecureTransactionPreferred() const = 0;
64 
65   // Updates DNS config.  If effective config has changed, destroys the current
66   // DnsTransactionFactory and creates a new one according to the effective
67   // config, unless it is invalid or has |unhandled_options|.
68   //
69   // Returns whether or not the effective config changed.
70   virtual bool SetSystemConfig(std::optional<DnsConfig> system_config) = 0;
71   virtual bool SetConfigOverrides(DnsConfigOverrides config_overrides) = 0;
72 
73   // If there is a current session, forces replacement with a new current
74   // session with the same effective config, and creates a new
75   // DnsTransactionFactory for the new session.
76   virtual void ReplaceCurrentSession() = 0;
77 
78   // Used for tracking per-context-per-session data.
79   // TODO(crbug.com/1022059): Once more per-context-per-session data has been
80   // moved to ResolveContext and it doesn't need to call back into DnsSession,
81   // convert this to a more limited session handle to prevent overuse of
82   // DnsSession outside the DnsClient code.
83   virtual DnsSession* GetCurrentSession() = 0;
84 
85   // Retrieve the current DNS configuration that would be used if transactions
86   // were otherwise currently allowed. Returns null if configuration is
87   // invalid or a configuration has not yet been read from the system.
88   virtual const DnsConfig* GetEffectiveConfig() const = 0;
89   virtual const DnsHosts* GetHosts() const = 0;
90 
91   // Returns all preset addresses for the specified endpoint, if any are
92   // present in the current effective DnsConfig.
93   virtual std::optional<std::vector<IPEndPoint>> GetPresetAddrs(
94       const url::SchemeHostPort& endpoint) const = 0;
95 
96   // Returns null if the current config is not valid.
97   virtual DnsTransactionFactory* GetTransactionFactory() = 0;
98 
99   virtual AddressSorter* GetAddressSorter() = 0;
100 
101   virtual void IncrementInsecureFallbackFailures() = 0;
102   virtual void ClearInsecureFallbackFailures() = 0;
103 
104   // Return the effective DNS configuration as a value that can be recorded in
105   // the NetLog. This also synthesizes interpretative data to the Value, e.g.
106   // whether secure and insecure transactions are enabled.
107   virtual base::Value::Dict GetDnsConfigAsValueForNetLog() const = 0;
108 
109   virtual std::optional<DnsConfig> GetSystemConfigForTesting() const = 0;
110   virtual DnsConfigOverrides GetConfigOverridesForTesting() const = 0;
111 
112   virtual void SetTransactionFactoryForTesting(
113       std::unique_ptr<DnsTransactionFactory> factory) = 0;
114   virtual void SetAddressSorterForTesting(
115       std::unique_ptr<AddressSorter> address_sorter) = 0;
116 
117   // Creates default client.
118   static std::unique_ptr<DnsClient> CreateClient(NetLog* net_log);
119 
120   // Creates a client for testing.  Allows using a mock ClientSocketFactory and
121   // a deterministic random number generator. |socket_factory| must outlive
122   // the returned DnsClient.
123   static std::unique_ptr<DnsClient> CreateClientForTesting(
124       NetLog* net_log,
125       const RandIntCallback& rand_int_callback);
126 };
127 
128 }  // namespace net
129 
130 #endif  // NET_DNS_DNS_CLIENT_H_
131