Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 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_SOCKET_TRANSPORT_CONNECT_JOB_H_ |
| 6 | #define NET_SOCKET_TRANSPORT_CONNECT_JOB_H_ |
| 7 | |
| 8 | #include <memory> |
Arthur Sonzogni | 4787fce | 2024-02-08 13:42:48 | [diff] [blame] | 9 | #include <optional> |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
Victor Hugo Vianna Silva | c6de5ce | 2025-03-18 16:33:11 | [diff] [blame] | 12 | #include <variant> |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 13 | #include <vector> |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 14 | |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 15 | #include "base/containers/flat_set.h" |
David Benjamin | 6e089be | 2022-02-11 18:22:21 | [diff] [blame] | 16 | #include "base/containers/span.h" |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 19 | #include "base/time/time.h" |
| 20 | #include "base/timer/timer.h" |
Eric Orth | 7287eb60 | 2019-02-13 19:49:55 | [diff] [blame] | 21 | #include "net/base/host_port_pair.h" |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 22 | #include "net/base/net_export.h" |
Brianna Goldstein | c8ae139 | 2022-10-10 20:09:47 | [diff] [blame] | 23 | #include "net/base/network_anonymization_key.h" |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 24 | #include "net/dns/host_resolver.h" |
Yoichiro Hibara | 6a4e282b | 2022-09-05 04:20:02 | [diff] [blame] | 25 | #include "net/dns/public/host_resolver_results.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 26 | #include "net/dns/public/resolve_error_info.h" |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 27 | #include "net/dns/public/secure_dns_policy.h" |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 28 | #include "net/socket/connect_job.h" |
| 29 | #include "net/socket/connection_attempts.h" |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 30 | #include "url/scheme_host_port.h" |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 31 | |
| 32 | namespace net { |
| 33 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 34 | class NetLogWithSource; |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 35 | class SocketTag; |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 36 | class TransportConnectSubJob; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 37 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 38 | class NET_EXPORT_PRIVATE TransportSocketParams |
| 39 | : public base::RefCounted<TransportSocketParams> { |
| 40 | public: |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 41 | // Representation of the destination endpoint of the transport |
| 42 | // socket/connection. Unlike ConnectJobFactory::Endpoint, this does not have a |
| 43 | // `using_ssl` field for schemeless endpoints because that has no meaning for |
| 44 | // transport parameters. |
Victor Hugo Vianna Silva | c6de5ce | 2025-03-18 16:33:11 | [diff] [blame] | 45 | using Endpoint = std::variant<url::SchemeHostPort, HostPortPair>; |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 46 | |
Andrew Williams | 08a81e5d8 | 2023-12-19 17:10:34 | [diff] [blame] | 47 | // `host_resolution_callback` will be invoked after the the hostname is |
| 48 | // resolved. `network_anonymization_key` is passed to the HostResolver to |
| 49 | // prevent cross-NAK leaks. If `host_resolution_callback` does not return OK, |
| 50 | // then the connection will be aborted with that value. `supported_alpns` |
Brianna Goldstein | c8ae139 | 2022-10-10 20:09:47 | [diff] [blame] | 51 | // specifies ALPN protocols for selecting HTTPS/SVCB records. If empty, |
| 52 | // addresses from HTTPS/SVCB records will be ignored and only A/AAAA will be |
| 53 | // used. |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 54 | TransportSocketParams(Endpoint destination, |
Brianna Goldstein | c8ae139 | 2022-10-10 20:09:47 | [diff] [blame] | 55 | NetworkAnonymizationKey network_anonymization_key, |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 56 | SecureDnsPolicy secure_dns_policy, |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 57 | OnHostResolutionCallback host_resolution_callback, |
| 58 | base::flat_set<std::string> supported_alpns); |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 59 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 60 | TransportSocketParams(const TransportSocketParams&) = delete; |
| 61 | TransportSocketParams& operator=(const TransportSocketParams&) = delete; |
| 62 | |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 63 | const Endpoint& destination() const { return destination_; } |
Brianna Goldstein | c8ae139 | 2022-10-10 20:09:47 | [diff] [blame] | 64 | const NetworkAnonymizationKey& network_anonymization_key() const { |
| 65 | return network_anonymization_key_; |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame] | 66 | } |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 67 | SecureDnsPolicy secure_dns_policy() const { return secure_dns_policy_; } |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 68 | const OnHostResolutionCallback& host_resolution_callback() const { |
| 69 | return host_resolution_callback_; |
| 70 | } |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 71 | const base::flat_set<std::string>& supported_alpns() const { |
| 72 | return supported_alpns_; |
| 73 | } |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | friend class base::RefCounted<TransportSocketParams>; |
| 77 | ~TransportSocketParams(); |
| 78 | |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 79 | const Endpoint destination_; |
Brianna Goldstein | c8ae139 | 2022-10-10 20:09:47 | [diff] [blame] | 80 | const NetworkAnonymizationKey network_anonymization_key_; |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 81 | const SecureDnsPolicy secure_dns_policy_; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 82 | const OnHostResolutionCallback host_resolution_callback_; |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 83 | const base::flat_set<std::string> supported_alpns_; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // TransportConnectJob handles the host resolution necessary for socket creation |
| 87 | // and the transport (likely TCP) connect. TransportConnectJob also has fallback |
| 88 | // logic for IPv6 connect() timeouts (which may happen due to networks / routers |
| 89 | // with broken IPv6 support). Those timeouts take 20s, so rather than make the |
| 90 | // user wait 20s for the timeout to fire, we use a fallback timer |
David Benjamin | fac74eb | 2022-05-03 17:37:37 | [diff] [blame] | 91 | // (kIPv6FallbackTime) and start a connect() to a IPv4 address if the timer |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 92 | // fires. Then we race the IPv4 connect() against the IPv6 connect() (which has |
| 93 | // a headstart) and return the one that completes first to the socket pool. |
| 94 | class NET_EXPORT_PRIVATE TransportConnectJob : public ConnectJob { |
| 95 | public: |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 96 | class NET_EXPORT_PRIVATE Factory { |
| 97 | public: |
| 98 | Factory() = default; |
| 99 | virtual ~Factory() = default; |
| 100 | |
| 101 | virtual std::unique_ptr<TransportConnectJob> Create( |
| 102 | RequestPriority priority, |
| 103 | const SocketTag& socket_tag, |
| 104 | const CommonConnectJobParams* common_connect_job_params, |
| 105 | const scoped_refptr<TransportSocketParams>& params, |
| 106 | Delegate* delegate, |
| 107 | const NetLogWithSource* net_log); |
| 108 | }; |
| 109 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 110 | // In cases where both IPv6 and IPv4 addresses were returned from DNS, |
| 111 | // TransportConnectJobs will start a second connection attempt to just the |
David Benjamin | fac74eb | 2022-05-03 17:37:37 | [diff] [blame] | 112 | // IPv4 addresses after this much time. (This is "Happy Eyeballs".) |
| 113 | // |
| 114 | // TODO(willchan): Base this off RTT instead of statically setting it. Note we |
| 115 | // choose a timeout that is different from the backup connect job timer so |
| 116 | // they don't synchronize. |
| 117 | static constexpr base::TimeDelta kIPv6FallbackTime = base::Milliseconds(300); |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 118 | |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 119 | struct NET_EXPORT_PRIVATE EndpointResultOverride { |
| 120 | EndpointResultOverride(HostResolverEndpointResult result, |
| 121 | std::set<std::string> dns_aliases); |
| 122 | EndpointResultOverride(EndpointResultOverride&&); |
| 123 | EndpointResultOverride(const EndpointResultOverride&); |
| 124 | ~EndpointResultOverride(); |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 125 | EndpointResultOverride& operator=(EndpointResultOverride&&) = default; |
| 126 | EndpointResultOverride& operator=(const EndpointResultOverride&) = default; |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 127 | |
| 128 | HostResolverEndpointResult result; |
| 129 | std::set<std::string> dns_aliases; |
| 130 | }; |
| 131 | |
Matt Menke | ab90584b0 | 2019-01-18 08:22:34 | [diff] [blame] | 132 | TransportConnectJob(RequestPriority priority, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 133 | const SocketTag& socket_tag, |
| 134 | const CommonConnectJobParams* common_connect_job_params, |
Matt Menke | ab90584b0 | 2019-01-18 08:22:34 | [diff] [blame] | 135 | const scoped_refptr<TransportSocketParams>& params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 136 | Delegate* delegate, |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 137 | const NetLogWithSource* net_log, |
Arthur Sonzogni | 4787fce | 2024-02-08 13:42:48 | [diff] [blame] | 138 | std::optional<EndpointResultOverride> |
| 139 | endpoint_result_override = std::nullopt); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 140 | |
| 141 | TransportConnectJob(const TransportConnectJob&) = delete; |
| 142 | TransportConnectJob& operator=(const TransportConnectJob&) = delete; |
| 143 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 144 | ~TransportConnectJob() override; |
| 145 | |
| 146 | // ConnectJob methods. |
| 147 | LoadState GetLoadState() const override; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 148 | bool HasEstablishedConnection() const override; |
Matt Menke | 6030ed9f | 2019-04-11 20:25:55 | [diff] [blame] | 149 | ConnectionAttempts GetConnectionAttempts() const override; |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 150 | ResolveErrorInfo GetResolveErrorInfo() const override; |
Arthur Sonzogni | 4787fce | 2024-02-08 13:42:48 | [diff] [blame] | 151 | std::optional<HostResolverEndpointResult> GetHostResolverEndpointResult() |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 152 | const override; |
| 153 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 154 | static base::TimeDelta ConnectionTimeout(); |
| 155 | |
| 156 | private: |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 157 | friend class TransportConnectSubJob; |
| 158 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 159 | enum State { |
| 160 | STATE_RESOLVE_HOST, |
| 161 | STATE_RESOLVE_HOST_COMPLETE, |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 162 | STATE_RESOLVE_HOST_CALLBACK_COMPLETE, |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 163 | STATE_TRANSPORT_CONNECT, |
| 164 | STATE_TRANSPORT_CONNECT_COMPLETE, |
| 165 | STATE_NONE, |
| 166 | }; |
| 167 | |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 168 | // Although it is not strictly necessary, it makes the code simpler if each |
| 169 | // subjob knows what type it is. |
| 170 | enum SubJobType { SUB_JOB_IPV4, SUB_JOB_IPV6 }; |
| 171 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 172 | void OnIOComplete(int result); |
| 173 | int DoLoop(int result); |
| 174 | |
| 175 | int DoResolveHost(); |
| 176 | int DoResolveHostComplete(int result); |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 177 | int DoResolveHostCallbackComplete(); |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 178 | int DoTransportConnect(); |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 179 | int DoTransportConnectComplete(int result); |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 180 | |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 181 | // Helper method called called when a SubJob completes, synchronously |
| 182 | // or asynchronously. Returns `ERR_IO_PENDING` if there is more work to |
| 183 | // do and another error if completed. It's up to the caller to manage |
| 184 | // advancing `DoLoop` if a value other than `ERR_IO_PENDING` is returned. |
| 185 | int HandleSubJobComplete(int result, TransportConnectSubJob* job); |
| 186 | // Called back from a SubJob when it completes. Invokes `OnIOComplete`, |
| 187 | // re-entering `DoLoop`, if there is no more work to do. Must not |
| 188 | // be called from within `DoLoop`. |
| 189 | void OnSubJobComplete(int result, TransportConnectSubJob* job); |
| 190 | |
| 191 | // Called from |fallback_timer_|. |
| 192 | void StartIPv4JobAsync(); |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 193 | |
| 194 | // Begins the host resolution and the TCP connect. Returns OK on success |
| 195 | // and ERR_IO_PENDING if it cannot immediately service the request. |
| 196 | // Otherwise, it returns a net error code. |
| 197 | int ConnectInternal() override; |
| 198 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 199 | void ChangePriorityInternal(RequestPriority priority) override; |
| 200 | |
David Benjamin | 6e089be | 2022-02-11 18:22:21 | [diff] [blame] | 201 | // Returns whether the client should be SVCB-optional when connecting to |
| 202 | // `results`. |
| 203 | bool IsSvcbOptional( |
| 204 | base::span<const HostResolverEndpointResult> results) const; |
| 205 | |
| 206 | // Returns whether `result` is usable for this connection. If `svcb_optional` |
| 207 | // is true, the non-HTTPS/SVCB fallback is allowed. |
| 208 | bool IsEndpointResultUsable(const HostResolverEndpointResult& result, |
| 209 | bool svcb_optional) const; |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 210 | |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 211 | // Returns the `HostResolverEndpointResult` for the current subjobs. |
| 212 | const HostResolverEndpointResult& GetEndpointResultForCurrentSubJobs() const; |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 213 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 214 | scoped_refptr<TransportSocketParams> params_; |
Eric Orth | 7287eb60 | 2019-02-13 19:49:55 | [diff] [blame] | 215 | std::unique_ptr<HostResolver::ResolveHostRequest> request_; |
David Benjamin | 13072a4 | 2022-02-10 22:23:21 | [diff] [blame] | 216 | std::vector<HostResolverEndpointResult> endpoint_results_; |
| 217 | size_t current_endpoint_result_ = 0; |
| 218 | std::set<std::string> dns_aliases_; |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 219 | bool has_dns_override_ = false; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 220 | |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 221 | State next_state_ = STATE_NONE; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 222 | |
David Benjamin | 9f47d66 | 2022-05-13 21:09:02 | [diff] [blame] | 223 | // The addresses are divided into IPv4 and IPv6, which are performed partially |
| 224 | // in parallel. If the list of IPv6 addresses is non-empty, then the IPv6 jobs |
| 225 | // go first, followed after `kIPv6FallbackTime` by the IPv4 addresses. The |
| 226 | // first sub-job to establish a connection wins. If one sub-job fails, the |
| 227 | // other one is launched if needed, and we wait for it to complete. |
| 228 | std::unique_ptr<TransportConnectSubJob> ipv4_job_; |
| 229 | std::unique_ptr<TransportConnectSubJob> ipv6_job_; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 230 | |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 231 | base::OneShotTimer fallback_timer_; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 232 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 233 | ResolveErrorInfo resolve_error_info_; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 234 | ConnectionAttempts connection_attempts_; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 235 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 236 | base::WeakPtrFactory<TransportConnectJob> weak_ptr_factory_{this}; |
Matt Menke | 3abc57c | 2019-01-10 21:48:42 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } // namespace net |
| 240 | |
| 241 | #endif // NET_SOCKET_TRANSPORT_CONNECT_JOB_H_ |