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 <optional> 10 #include <string_view> 11 #include <vector> 12 13 #include "base/values.h" 14 #include "net/base/completion_once_callback.h" 15 #include "net/base/host_mapping_rules.h" 16 #include "net/base/net_export.h" 17 #include "net/base/network_anonymization_key.h" 18 #include "net/dns/dns_config.h" 19 #include "net/dns/host_resolver.h" 20 #include "net/log/net_log_with_source.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(std::string_view rule_string)48 bool AddRuleFromString(std::string_view 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(std::string_view rules_string)53 void SetRulesFromString(std::string_view 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 std::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 std::optional<ResolveHostParameters>& optional_parameters) override; 68 std::unique_ptr<ServiceEndpointRequest> CreateServiceEndpointRequest( 69 Host host, 70 NetworkAnonymizationKey network_anonymization_key, 71 NetLogWithSource net_log, 72 ResolveHostParameters parameters) override; 73 std::unique_ptr<ProbeRequest> CreateDohProbeRequest() override; 74 HostCache* GetHostCache() override; 75 base::Value::Dict GetDnsConfigAsValue() const override; 76 void SetRequestContext(URLRequestContext* request_context) override; 77 HostResolverManager* GetManagerForTesting() override; 78 79 private: 80 std::unique_ptr<HostResolver> impl_; 81 82 HostMappingRules rules_; 83 }; 84 85 } // namespace net 86 87 #endif // NET_DNS_MAPPED_HOST_RESOLVER_H_ 88