xref: /aosp_15_r20/external/cronet/net/dns/host_resolver_nat64_task.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 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_HOST_RESOLVER_NAT64_TASK_H_
6 #define NET_DNS_HOST_RESOLVER_NAT64_TASK_H_
7 
8 #include <memory>
9 #include <string>
10 #include <string_view>
11 #include <vector>
12 
13 #include "base/functional/callback_forward.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/sequence_checker.h"
17 #include "net/dns/host_resolver.h"
18 #include "net/dns/host_resolver_manager.h"
19 #include "net/dns/public/dns_query_type.h"
20 
21 namespace net {
22 
23 class HostCache;
24 
25 // Representation of a single HostResolverImpl::Job task to convert an IPv4
26 // address literal to an IPv4-Embedded IPv6 according to rfc6052.
27 // https://www.rfc-editor.org/rfc/rfc6052
28 // When a DNS64 is not found returns the original IPv4 address.
29 // Destruction cancels the task and prevents any callbacks from being invoked.
30 class HostResolverNat64Task {
31  public:
32   HostResolverNat64Task(std::string_view hostname,
33                         NetworkAnonymizationKey network_anonymization_key,
34                         NetLogWithSource net_log,
35                         ResolveContext* resolve_context,
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   base::OnceClosure completion_closure_;
55   base::WeakPtr<HostResolverManager> resolver_;
56 
57   SEQUENCE_CHECKER(sequence_checker_);
58 
59   int DoResolve();
60   int DoResolveComplete(int result);
61   int DoSynthesizeToIpv6();
62 
63   void OnIOComplete(int result);
64   int DoLoop(int result);
65 
66   enum class State {
67     kResolve,
68     kResolveComplete,
69     kSynthesizeToIpv6,
70     kStateNone,
71   };
72 
73   State next_state_ = State::kStateNone;
74 
75   std::unique_ptr<HostResolver::ResolveHostRequest> request_ipv4onlyarpa_;
76 
77   HostCache::Entry results_ =
78       HostCache::Entry(ERR_FAILED, HostCache::Entry::SOURCE_UNKNOWN);
79   base::WeakPtrFactory<HostResolverNat64Task> weak_ptr_factory_{this};
80 };
81 
82 }  // namespace net
83 
84 #endif  // NET_DNS_HOST_RESOLVER_NAT64_TASK_H_
85