1 // Copyright 2022 The Chromium Authors. All rights reserved. 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_HOST_RESOLVER_NAT64_TASK_H_ 6 #define NET_DNS_HOST_RESOLVER_NAT64_TASK_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/functional/callback_forward.h" 13 #include "base/memory/raw_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/sequence_checker.h" 16 #include "net/dns/host_resolver.h" 17 #include "net/dns/host_resolver_manager.h" 18 #include "net/dns/public/dns_query_type.h" 19 20 namespace net { 21 22 class HostCache; 23 24 // Representation of a single HostResolverImpl::Job task to convert an IPv4 25 // address literal to an IPv4-Embedded IPv6 according to rfc6052. 26 // https://www.rfc-editor.org/rfc/rfc6052 27 // When a DNS64 is not found returns the original IPv4 address. 28 // Destruction cancels the task and prevents any callbacks from being invoked. 29 class HostResolverNat64Task { 30 public: 31 HostResolverNat64Task(base::StringPiece hostname, 32 NetworkAnonymizationKey network_anonymization_key, 33 NetLogWithSource net_log, 34 ResolveContext* resolve_context, 35 HostCache* host_cache, 36 base::WeakPtr<HostResolverManager> resolver); 37 38 HostResolverNat64Task(const HostResolverNat64Task&) = delete; 39 HostResolverNat64Task& operator=(const HostResolverNat64Task&) = delete; 40 41 ~HostResolverNat64Task(); 42 43 // Should only be called once. 44 void Start(base::OnceClosure completion_closure); 45 46 // Results only available after invocation of the completion closure. 47 HostCache::Entry GetResults() const; 48 49 private: 50 const std::string hostname_; 51 const NetworkAnonymizationKey network_anonymization_key_; 52 NetLogWithSource net_log_; 53 const raw_ptr<ResolveContext> resolve_context_; 54 const raw_ptr<HostCache> host_cache_; 55 base::OnceClosure completion_closure_; 56 base::WeakPtr<HostResolverManager> resolver_; 57 58 SEQUENCE_CHECKER(sequence_checker_); 59 60 int DoResolve(); 61 int DoResolveComplete(int result); 62 int DoSynthesizeToIpv6(); 63 64 void OnIOComplete(int result); 65 int DoLoop(int result); 66 67 enum class State { 68 kResolve, 69 kResolveComplete, 70 kSynthesizeToIpv6, 71 kStateNone, 72 }; 73 74 State next_state_ = State::kStateNone; 75 76 std::unique_ptr<HostResolver::ResolveHostRequest> request_ipv4onlyarpa_; 77 78 HostCache::Entry results_ = 79 HostCache::Entry(ERR_FAILED, HostCache::Entry::SOURCE_UNKNOWN); 80 base::WeakPtrFactory<HostResolverNat64Task> weak_ptr_factory_{this}; 81 }; 82 83 } // namespace net 84 85 #endif // NET_DNS_HOST_RESOLVER_NAT64_TASK_H_ 86