1 // Copyright 2024 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_TASK_RESULTS_MANAGER_H_ 6 #define NET_DNS_DNS_TASK_RESULTS_MANAGER_H_ 7 8 #include <map> 9 #include <memory> 10 #include <set> 11 #include <string> 12 #include <vector> 13 14 #include "base/memory/raw_ptr.h" 15 #include "base/time/time.h" 16 #include "base/timer/timer.h" 17 #include "net/base/ip_endpoint.h" 18 #include "net/base/net_export.h" 19 #include "net/dns/host_resolver.h" 20 #include "net/dns/host_resolver_dns_task.h" 21 #include "net/dns/public/dns_query_type.h" 22 #include "net/dns/public/host_resolver_results.h" 23 #include "net/log/net_log_with_source.h" 24 #include "third_party/abseil-cpp/absl/types/variant.h" 25 #include "url/scheme_host_port.h" 26 27 namespace net { 28 29 // Creates and updates intermediate service endpoints while resolving a host. 30 // This class is designed to have a 1:1 relationship with a HostResolverDnsTask 31 // and expects to be notified every time a DnsTransaction is completed. When 32 // notified, tries to create and update service endpoints from DNS responses 33 // received so far. 34 // 35 // If the A response comes before the AAAA response, delays service endpoints 36 // creation/update until an AAAA response is received or the AAAA query is 37 // timed out. 38 class NET_EXPORT_PRIVATE DnsTaskResultsManager { 39 public: 40 // Time to wait for a AAAA response after receiving an A response. 41 static constexpr base::TimeDelta kResolutionDelay = base::Milliseconds(50); 42 43 // Interface for watching for intermediate service endpoints updates. 44 class Delegate { 45 public: 46 virtual ~Delegate() = default; 47 48 // Called when service endpoints are updated. 49 virtual void OnServiceEndpointsUpdated() = 0; 50 }; 51 52 // TODO(crbug.com/41493696): Update HostResolverManager::JobKey to use 53 // HostResolver::Host so that HostResolverManager::Job can create an instance 54 // of this class. 55 DnsTaskResultsManager(Delegate* delegate, 56 HostResolver::Host host, 57 DnsQueryTypeSet query_types, 58 const NetLogWithSource& net_log); 59 ~DnsTaskResultsManager(); 60 61 DnsTaskResultsManager(const DnsTaskResultsManager&) = delete; 62 DnsTaskResultsManager& operator=(const DnsTaskResultsManager&) = delete; 63 64 // Processes a query response represented by HostResolverInternalResults. 65 // Expected be called when a DnsTransaction is completed. 66 void ProcessDnsTransactionResults( 67 DnsQueryType query_type, 68 const std::set<std::unique_ptr<HostResolverInternalResult>>& results); 69 70 // Returns the current service endpoints. The results could change over time. 71 // Use the delegate's OnServiceEndpointsUpdated() to watch for updates. 72 const std::vector<ServiceEndpoint>& GetCurrentEndpoints() const; 73 74 // Returns all DNS record aliases, found as a result of A, AAAA, and HTTPS 75 // queries. The results could change over time. 76 const std::set<std::string>& GetAliases() const; 77 78 // True when a HTTP response is received. When true, call sites can start 79 // cryptographic handshakes since Chrome doesn't support HTTPS follow-up 80 // queries yet. 81 bool IsMetadataReady() const; 82 IsResolutionDelayTimerRunningForTest()83 bool IsResolutionDelayTimerRunningForTest() { 84 return resolution_delay_timer_.IsRunning(); 85 } 86 87 private: 88 struct PerDomainResult; 89 90 PerDomainResult& GetOrCreatePerDomainResult(const std::string& domain_name); 91 92 void OnAaaaResolutionTimedout(); 93 94 void UpdateEndpoints(); 95 96 // Checks all per domain results and return true when there is at least one 97 // valid address. 98 bool HasIpv4Addresses(); 99 100 void RecordResolutionDelayResult(bool timedout); 101 102 const raw_ptr<Delegate> delegate_; 103 const HostResolver::Host host_; 104 const DnsQueryTypeSet query_types_; 105 const NetLogWithSource net_log_; 106 107 std::vector<ServiceEndpoint> current_endpoints_; 108 109 bool is_metadata_ready_ = false; 110 bool aaaa_response_received_ = false; 111 112 std::set<std::string> aliases_; 113 114 std::map</*domain_name=*/std::string, std::unique_ptr<PerDomainResult>> 115 per_domain_results_; 116 117 base::TimeTicks resolution_delay_start_time_; 118 base::OneShotTimer resolution_delay_timer_; 119 }; 120 121 } // namespace net 122 123 #endif // NET_DNS_DNS_TASK_RESULTS_MANAGER_H_ 124