• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MAPPED_HOST_RESOLVER_H_
6 #define NET_DNS_MAPPED_HOST_RESOLVER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/strings/string_piece.h"
12 #include "base/values.h"
13 #include "net/base/completion_once_callback.h"
14 #include "net/base/host_mapping_rules.h"
15 #include "net/base/net_export.h"
16 #include "net/base/network_anonymization_key.h"
17 #include "net/dns/dns_config.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/log/net_log_with_source.h"
20 #include "third_party/abseil-cpp/absl/types/optional.h"
21 #include "url/scheme_host_port.h"
22 
23 namespace net {
24 
25 // This class wraps an existing HostResolver instance, but modifies the
26 // request before passing it off to |impl|. This is different from
27 // MockHostResolver which does the remapping at the HostResolverProc
28 // layer, so it is able to preserve the effectiveness of the cache.
29 class NET_EXPORT MappedHostResolver : public HostResolver {
30  public:
31   // Creates a MappedHostResolver that forwards all of its requests through
32   // |impl|.
33   explicit MappedHostResolver(std::unique_ptr<HostResolver> impl);
34   ~MappedHostResolver() override;
35 
36   void OnShutdown() override;
37 
38   // Adds a rule to this mapper. The format of the rule can be one of:
39   //
40   //   "MAP" <hostname_pattern> <replacement_host> [":" <replacement_port>]
41   //   "EXCLUDE" <hostname_pattern>
42   //
43   // The <replacement_host> can be either a hostname, or an IP address literal,
44   // or "~NOTFOUND". If it is "~NOTFOUND" then all matched hostnames will fail
45   // to be resolved with ERR_NAME_NOT_RESOLVED.
46   //
47   // Returns true if the rule was successfully parsed and added.
AddRuleFromString(base::StringPiece rule_string)48   bool AddRuleFromString(base::StringPiece rule_string) {
49     return rules_.AddRuleFromString(rule_string);
50   }
51 
52   // Takes a comma separated list of rules, and assigns them to this resolver.
SetRulesFromString(base::StringPiece rules_string)53   void SetRulesFromString(base::StringPiece rules_string) {
54     rules_.SetRulesFromString(rules_string);
55   }
56 
57   // HostResolver methods:
58   std::unique_ptr<ResolveHostRequest> CreateRequest(
59       url::SchemeHostPort host,
60       NetworkAnonymizationKey network_anonymization_key,
61       NetLogWithSource net_log,
62       absl::optional<ResolveHostParameters> optional_parameters) override;
63   std::unique_ptr<ResolveHostRequest> CreateRequest(
64       const HostPortPair& host,
65       const NetworkAnonymizationKey& network_anonymization_key,
66       const NetLogWithSource& net_log,
67       const absl::optional<ResolveHostParameters>& optional_parameters)
68       override;
69   std::unique_ptr<ProbeRequest> CreateDohProbeRequest() override;
70   HostCache* GetHostCache() override;
71   base::Value::Dict GetDnsConfigAsValue() const override;
72   void SetRequestContext(URLRequestContext* request_context) override;
73   HostResolverManager* GetManagerForTesting() override;
74 
75  private:
76   std::unique_ptr<HostResolver> impl_;
77 
78   HostMappingRules rules_;
79 };
80 
81 }  // namespace net
82 
83 #endif  // NET_DNS_MAPPED_HOST_RESOLVER_H_
84