blob: 445ae1dbabd66fb26b600b9e2765be8a9752edba [file] [log] [blame]
[email protected]e13201d82012-12-12 05:00:321// Copyright (c) 2012 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
Ryan Hamiltona3ee93a72018-08-01 22:03:085#ifndef NET_QUIC_QUIC_STREAM_FACTORY_H_
6#define NET_QUIC_QUIC_STREAM_FACTORY_H_
[email protected]e13201d82012-12-12 05:00:327
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9#include <stdint.h>
10
[email protected]1cd2a5f2014-03-14 06:33:2511#include <list>
[email protected]e13201d82012-12-12 05:00:3212#include <map>
Matt Menkefca05b62019-09-20 23:15:5613#include <memory>
bnc614a92d32016-04-04 13:56:0714#include <set>
[email protected]41d6b172013-01-29 16:10:5715#include <string>
[email protected]6e12d702013-11-13 00:17:1716#include <vector>
[email protected]e13201d82012-12-12 05:00:3217
Matt Menkefca05b62019-09-20 23:15:5618#include "base/containers/mru_cache.h"
thestiga0e18cd2015-09-25 04:58:3619#include "base/gtest_prod_util.h"
Avi Drissman13fc8932015-12-20 04:40:4620#include "base/macros.h"
[email protected]e13201d82012-12-12 05:00:3221#include "base/memory/weak_ptr.h"
Zhongyi Shic16b4102019-02-12 00:37:4022#include "base/time/tick_clock.h"
rtenneti8332ba52015-09-17 19:33:4123#include "base/time/time.h"
[email protected]e13201d82012-12-12 05:00:3224#include "net/base/address_list.h"
Bence Békyd8a21fc32018-06-27 18:29:5825#include "net/base/completion_once_callback.h"
[email protected]e13201d82012-12-12 05:00:3226#include "net/base/host_port_pair.h"
bnc81c46c1f2016-10-04 16:25:5927#include "net/base/net_export.h"
[email protected]f698a012013-05-06 20:18:5928#include "net/base/network_change_notifier.h"
Lily Houghton582d4622018-01-22 22:43:4029#include "net/base/proxy_server.h"
[email protected]d7d1e50b2013-11-25 22:08:0930#include "net/cert/cert_database.h"
Ben Schwartz3ff4dc1e62021-04-27 21:15:2331#include "net/dns/public/secure_dns_policy.h"
zhongyi32569c62016-01-08 02:54:3032#include "net/http/http_server_properties.h"
xunjieli2608f9b2016-03-14 13:39:2333#include "net/http/http_stream_factory.h"
mikecironef22f9812016-10-04 03:40:1934#include "net/log/net_log_with_source.h"
Ryan Hamiltona3ee93a72018-08-01 22:03:0835#include "net/quic/network_connection.h"
36#include "net/quic/quic_chromium_client_session.h"
37#include "net/quic/quic_clock_skew_detector.h"
Zhongyi Shic6b3f03f2020-06-25 21:28:2238#include "net/quic/quic_connectivity_monitor.h"
Victor Vasilieva1e66d72019-12-05 17:55:3839#include "net/quic/quic_context.h"
Matt Menkefca05b62019-09-20 23:15:5640#include "net/quic/quic_crypto_client_config_handle.h"
Ryan Hamiltona3ee93a72018-08-01 22:03:0841#include "net/quic/quic_session_key.h"
Yixin Wang7f3cdc3f2017-11-10 01:44:1442#include "net/socket/client_socket_pool.h"
rch02d87792015-09-09 09:05:5343#include "net/ssl/ssl_config_service.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5144#include "net/third_party/quiche/src/quic/core/quic_config.h"
45#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
46#include "net/third_party/quiche/src/quic/core/quic_packets.h"
47#include "net/third_party/quiche/src/quic/core/quic_server_id.h"
Dan McArdle68a5f622021-07-09 20:56:5348#include "url/scheme_host_port.h"
[email protected]e13201d82012-12-12 05:00:3249
mikecironef22f9812016-10-04 03:40:1950namespace base {
51class Value;
Ryan Hamiltona3ee93a72018-08-01 22:03:0852} // namespace base
mikecironef22f9812016-10-04 03:40:1953
Ryan Hamilton9835e662018-08-02 05:36:2754namespace quic {
55class QuicAlarmFactory;
56class QuicClock;
57class QuicRandom;
58} // namespace quic
59
[email protected]e13201d82012-12-12 05:00:3260namespace net {
61
estark6f9b3d82016-01-12 21:37:0562class CTPolicyEnforcer;
[email protected]6d1b4ed2013-07-10 03:57:5463class CertVerifier;
[email protected]e13201d82012-12-12 05:00:3264class ClientSocketFactory;
[email protected]6d1b4ed2013-07-10 03:57:5465class HostResolver;
[email protected]77c6c162013-08-17 02:57:4566class HttpServerProperties;
mikecironef22f9812016-10-04 03:40:1967class NetLog;
Matt Menke26e41542019-06-05 01:09:5168class NetworkIsolationKey;
rch12fef552016-01-15 16:26:3169class QuicChromiumConnectionHelper;
[email protected]e8ff26842013-03-22 21:02:0570class QuicCryptoClientStreamFactory;
rtenneti13c9d38d2015-10-12 21:25:0171class QuicServerInfo;
[email protected]e13201d82012-12-12 05:00:3272class QuicStreamFactory;
Victor Vasiliev7752898d2019-11-14 21:30:2273class QuicContext;
Chris Thompsonf31b2492020-07-21 05:47:4274class SCTAuditingDelegate;
tbansalfdf5665b2015-09-21 22:46:4075class SocketPerformanceWatcherFactory;
Paul Jensen8e3c5d32018-02-19 17:06:3376class SocketTag;
[email protected]080b77932014-08-04 01:22:4677class TransportSecurityState;
[email protected]e13201d82012-12-12 05:00:3278
[email protected]c49ff182013-09-28 08:33:2679namespace test {
80class QuicStreamFactoryPeer;
81} // namespace test
82
Matt Menkefca05b62019-09-20 23:15:5683// Maximum number of not currently in use QuicCryptoClientConfig that can be
84// stored in |recent_crypto_config_map_|.
85//
86// TODO(mmenke): Should figure out a reasonable value of this, using field
87// trials. The optimal value may increase over time, as QUIC becomes more
88// prevalent. Whether or not NetworkIsolationKeys end up including subframe URLs
89// will also influence the ideal value.
90const int kMaxRecentCryptoConfigs = 100;
91
Zhongyi Shib4737c62017-09-28 00:49:3192enum QuicPlatformNotification {
93 NETWORK_CONNECTED,
94 NETWORK_MADE_DEFAULT,
95 NETWORK_DISCONNECTED,
96 NETWORK_SOON_TO_DISCONNECT,
97 NETWORK_IP_ADDRESS_CHANGED,
98 NETWORK_NOTIFICATION_MAX
99};
100
Zhongyi Shic6b3f03f2020-06-25 21:28:22101enum AllActiveSessionsGoingAwayReason {
102 kClockSkewDetected,
103 kIPAddressChanged,
104 kCertDBChanged
105};
106
Yixin Wang7891a39d2017-11-08 20:59:24107// Encapsulates a pending request for a QuicChromiumClientSession.
[email protected]e13201d82012-12-12 05:00:32108// If the request is still pending when it is destroyed, it will
109// cancel the request with the factory.
110class NET_EXPORT_PRIVATE QuicStreamRequest {
111 public:
zhongyi98d6a9262017-05-19 02:47:45112 explicit QuicStreamRequest(QuicStreamFactory* factory);
Peter Boström293b1342021-09-22 17:31:43113
114 QuicStreamRequest(const QuicStreamRequest&) = delete;
115 QuicStreamRequest& operator=(const QuicStreamRequest&) = delete;
116
[email protected]e13201d82012-12-12 05:00:32117 ~QuicStreamRequest();
118
rtennetia75df622015-06-21 23:59:50119 // |cert_verify_flags| is bitwise OR'd of CertVerifier::VerifyFlags and it is
120 // passed to CertVerifier::Verify.
bnc359ed2a2016-04-29 20:43:45121 // |destination| will be resolved and resulting IPEndPoint used to open a
Ryan Hamilton8d9ee76e2018-05-29 23:52:52122 // quic::QuicConnection. This can be different than
123 // HostPortPair::FromURL(url).
Cammie Smith Barnes0f38aff2021-03-02 23:14:35124 // When |use_dns_aliases| is true, any DNS aliases found in host resolution
125 // are stored in the |dns_aliases_by_session_key_| map. |use_dns_aliases|
126 // should be false in the case of a proxy.
Dan McArdle68a5f622021-07-09 20:56:53127 int Request(url::SchemeHostPort destination,
Ryan Hamilton9ef8c102019-06-28 03:58:52128 quic::ParsedQuicVersion quic_version,
[email protected]9dd3ff0f2014-03-26 09:51:28129 PrivacyMode privacy_mode,
Yixin Wang247ea642017-11-15 01:15:50130 RequestPriority priority,
Paul Jensen8e3c5d32018-02-19 17:06:33131 const SocketTag& socket_tag,
Matt Menke26e41542019-06-05 01:09:51132 const NetworkIsolationKey& network_isolation_key,
Ben Schwartz3ff4dc1e62021-04-27 21:15:23133 SecureDnsPolicy secure_dns_policy,
Cammie Smith Barnes0f38aff2021-03-02 23:14:35134 bool use_dns_aliases,
rtennetia75df622015-06-21 23:59:50135 int cert_verify_flags,
ckrasic3865ee0f2016-02-29 22:04:56136 const GURL& url,
tfarina42834112016-09-22 13:38:20137 const NetLogWithSource& net_log,
Ryan Hamilton75f197262017-08-17 14:00:07138 NetErrorDetails* net_error_details,
Zhongyi Shia6b68d112018-09-24 07:49:03139 CompletionOnceCallback failed_on_default_network_callback,
Bence Békyd8a21fc32018-06-27 18:29:58140 CompletionOnceCallback callback);
[email protected]e13201d82012-12-12 05:00:32141
Yixin Wang7c5d11a82017-12-21 02:40:00142 // This function must be called after Request() returns ERR_IO_PENDING.
143 // Returns true if Request() requires host resolution and it hasn't completed
144 // yet. If true is returned, |callback| will run when host resolution
145 // completes. It will be called with the result after host resolution during
146 // the connection process. For example, if host resolution returns OK and then
147 // crypto handshake returns ERR_IO_PENDING, then |callback| will run with
148 // ERR_IO_PENDING.
Bence Békyd8a21fc32018-06-27 18:29:58149 bool WaitForHostResolution(CompletionOnceCallback callback);
Yixin Wang7c5d11a82017-12-21 02:40:00150
151 // Tells QuicStreamRequest it should expect OnHostResolutionComplete()
152 // to be called in the future.
153 void ExpectOnHostResolution();
154
155 // Will be called by the associated QuicStreamFactory::Job when host
156 // resolution completes asynchronously after Request().
157 void OnHostResolutionComplete(int rv);
158
[email protected]e13201d82012-12-12 05:00:32159 void OnRequestComplete(int rv);
160
Zhongyi Shia6b68d112018-09-24 07:49:03161 // Called when the original connection created on the default network for
162 // |this| fails and a new connection has been created on the alternate
163 // network.
164 void OnConnectionFailedOnDefaultNetwork();
165
rtenneti8332ba52015-09-17 19:33:41166 // Helper method that calls |factory_|'s GetTimeDelayForWaitingJob(). It
167 // returns the amount of time waiting job should be delayed.
168 base::TimeDelta GetTimeDelayForWaitingJob() const;
169
Lily Chenf11e1292018-11-29 16:42:09170 // If host resolution is underway, changes the priority of the host resolver
171 // request.
172 void SetPriority(RequestPriority priority);
173
Yixin Wang7891a39d2017-11-08 20:59:24174 // Releases the handle to the QUIC session retrieved as a result of Request().
175 std::unique_ptr<QuicChromiumClientSession::Handle> ReleaseSessionHandle();
xunjieli2608f9b2016-03-14 13:39:23176
177 // Sets |session_|.
rchf0b18c8a2017-05-05 19:31:57178 void SetSession(std::unique_ptr<QuicChromiumClientSession::Handle> session);
[email protected]e13201d82012-12-12 05:00:32179
Ryan Hamilton75f197262017-08-17 14:00:07180 NetErrorDetails* net_error_details() { return net_error_details_; }
181
Paul Jensen8e3c5d32018-02-19 17:06:33182 const QuicSessionKey& session_key() const { return session_key_; }
bnccb7ff3c2015-05-21 20:51:55183
tfarina42834112016-09-22 13:38:20184 const NetLogWithSource& net_log() const { return net_log_; }
[email protected]e13201d82012-12-12 05:00:32185
186 private:
187 QuicStreamFactory* factory_;
Paul Jensen8e3c5d32018-02-19 17:06:33188 QuicSessionKey session_key_;
tfarina42834112016-09-22 13:38:20189 NetLogWithSource net_log_;
Bence Békyd8a21fc32018-06-27 18:29:58190 CompletionOnceCallback callback_;
Zhongyi Shia6b68d112018-09-24 07:49:03191 CompletionOnceCallback failed_on_default_network_callback_;
Ryan Hamilton75f197262017-08-17 14:00:07192 NetErrorDetails* net_error_details_; // Unowned.
rchf0b18c8a2017-05-05 19:31:57193 std::unique_ptr<QuicChromiumClientSession::Handle> session_;
[email protected]e13201d82012-12-12 05:00:32194
Yixin Wang7c5d11a82017-12-21 02:40:00195 // Set in Request(). If true, then OnHostResolutionComplete() is expected to
196 // be called in the future.
197 bool expect_on_host_resolution_;
198 // Callback passed to WaitForHostResolution().
Bence Békyd8a21fc32018-06-27 18:29:58199 CompletionOnceCallback host_resolution_callback_;
[email protected]e13201d82012-12-12 05:00:32200};
201
Yixin Wang7891a39d2017-11-08 20:59:24202// A factory for fetching QuicChromiumClientSessions.
[email protected]f698a012013-05-06 20:18:59203class NET_EXPORT_PRIVATE QuicStreamFactory
[email protected]d7d1e50b2013-11-25 22:08:09204 : public NetworkChangeNotifier::IPAddressObserver,
jri7e636642016-01-14 06:57:08205 public NetworkChangeNotifier::NetworkObserver,
[email protected]d7d1e50b2013-11-25 22:08:09206 public CertDatabase::Observer {
[email protected]e13201d82012-12-12 05:00:32207 public:
bnc359ed2a2016-04-29 20:43:45208 // This class encompasses |destination| and |server_id|.
209 // |destination| is a HostPortPair which is resolved
Ryan Hamilton8d9ee76e2018-05-29 23:52:52210 // and a quic::QuicConnection is made to the resulting IP address.
bnc359ed2a2016-04-29 20:43:45211 // |server_id| identifies the origin of the request,
212 // the crypto handshake advertises |server_id.host()| to the server,
213 // and the certificate is also matched against |server_id.host()|.
Paul Jensen8e3c5d32018-02-19 17:06:33214 class NET_EXPORT_PRIVATE QuicSessionAliasKey {
bnc359ed2a2016-04-29 20:43:45215 public:
Paul Jensen8e3c5d32018-02-19 17:06:33216 QuicSessionAliasKey() = default;
Dan McArdle68a5f622021-07-09 20:56:53217 QuicSessionAliasKey(url::SchemeHostPort destination,
218 QuicSessionKey session_key);
Paul Jensen8e3c5d32018-02-19 17:06:33219 ~QuicSessionAliasKey() = default;
bnc359ed2a2016-04-29 20:43:45220
221 // Needed to be an element of std::set.
Paul Jensen8e3c5d32018-02-19 17:06:33222 bool operator<(const QuicSessionAliasKey& other) const;
223 bool operator==(const QuicSessionAliasKey& other) const;
bnc359ed2a2016-04-29 20:43:45224
Dan McArdle68a5f622021-07-09 20:56:53225 const url::SchemeHostPort& destination() const { return destination_; }
Ryan Hamilton8d9ee76e2018-05-29 23:52:52226 const quic::QuicServerId& server_id() const {
227 return session_key_.server_id();
228 }
Paul Jensen8e3c5d32018-02-19 17:06:33229 const QuicSessionKey& session_key() const { return session_key_; }
bnc359ed2a2016-04-29 20:43:45230
xunjieli69720dd2017-01-30 15:36:29231 // Returns the estimate of dynamically allocated memory in bytes.
xunjieli69720dd2017-01-30 15:36:29232
bnc359ed2a2016-04-29 20:43:45233 private:
Dan McArdle68a5f622021-07-09 20:56:53234 url::SchemeHostPort destination_;
Paul Jensen8e3c5d32018-02-19 17:06:33235 QuicSessionKey session_key_;
bnc359ed2a2016-04-29 20:43:45236 };
237
[email protected]e8ff26842013-03-22 21:02:05238 QuicStreamFactory(
jridf673d22016-06-02 22:06:33239 NetLog* net_log,
[email protected]e8ff26842013-03-22 21:02:05240 HostResolver* host_resolver,
nharper642ae4b2016-06-30 00:40:36241 SSLConfigService* ssl_config_service,
[email protected]e8ff26842013-03-22 21:02:05242 ClientSocketFactory* client_socket_factory,
bnc525e175a2016-06-20 12:36:40243 HttpServerProperties* http_server_properties,
[email protected]59c0bbd2014-03-22 04:08:12244 CertVerifier* cert_verifier,
estark6f9b3d82016-01-12 21:37:05245 CTPolicyEnforcer* ct_policy_enforcer,
[email protected]080b77932014-08-04 01:22:46246 TransportSecurityState* transport_security_state,
Chris Thompsonf31b2492020-07-21 05:47:42247 SCTAuditingDelegate* sct_auditing_delegate,
tbansalc8a94ea2015-11-02 23:58:51248 SocketPerformanceWatcherFactory* socket_performance_watcher_factory,
[email protected]e8ff26842013-03-22 21:02:05249 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
Victor Vasilieva1e66d72019-12-05 17:55:38250 QuicContext* context);
Peter Boström407869b2021-10-07 04:42:48251
252 QuicStreamFactory(const QuicStreamFactory&) = delete;
253 QuicStreamFactory& operator=(const QuicStreamFactory&) = delete;
254
dchengb03027d2014-10-21 12:00:20255 ~QuicStreamFactory() override;
[email protected]e13201d82012-12-12 05:00:32256
Paul Jensen8e3c5d32018-02-19 17:06:33257 // Returns true if there is an existing session for |session_key| or if the
bnc359ed2a2016-04-29 20:43:45258 // request can be pooled to an existing session to the IP address of
259 // |destination|.
Paul Jensen8e3c5d32018-02-19 17:06:33260 bool CanUseExistingSession(const QuicSessionKey& session_key,
Dan McArdle68a5f622021-07-09 20:56:53261 const url::SchemeHostPort& destination);
zhongyi32569c62016-01-08 02:54:30262
Yixin Wang7891a39d2017-11-08 20:59:24263 // Fetches a QuicChromiumClientSession to |host_port_pair| which will be
rchf114d982015-10-21 01:34:56264 // owned by |request|.
[email protected]0cceb922014-07-01 02:00:56265 // If a matching session already exists, this method will return OK. If no
266 // matching session exists, this will return ERR_IO_PENDING and will invoke
267 // OnRequestComplete asynchronously.
Cammie Smith Barnes0f38aff2021-03-02 23:14:35268 // When |use_dns_aliases| is true, any DNS aliases found in host resolution
269 // are stored in the |dns_aliases_by_session_key_| map. |use_dns_aliases|
270 // should be false in the case of a proxy.
Paul Jensen8e3c5d32018-02-19 17:06:33271 int Create(const QuicSessionKey& session_key,
Dan McArdle68a5f622021-07-09 20:56:53272 url::SchemeHostPort destination,
Ryan Hamilton9ef8c102019-06-28 03:58:52273 quic::ParsedQuicVersion quic_version,
Yixin Wang247ea642017-11-15 01:15:50274 RequestPriority priority,
Cammie Smith Barnes0f38aff2021-03-02 23:14:35275 bool use_dns_aliases,
rtennetia75df622015-06-21 23:59:50276 int cert_verify_flags,
ckrasic3865ee0f2016-02-29 22:04:56277 const GURL& url,
tfarina42834112016-09-22 13:38:20278 const NetLogWithSource& net_log,
[email protected]e13201d82012-12-12 05:00:32279 QuicStreamRequest* request);
280
[email protected]4d283b32013-10-17 12:57:27281 // Called by a session when it is going away and no more streams should be
282 // created on it.
ckrasic4f9d88d2015-07-22 22:23:16283 void OnSessionGoingAway(QuicChromiumClientSession* session);
[email protected]4d283b32013-10-17 12:57:27284
[email protected]e13201d82012-12-12 05:00:32285 // Called by a session after it shuts down.
ckrasic4f9d88d2015-07-22 22:23:16286 void OnSessionClosed(QuicChromiumClientSession* session);
[email protected]e13201d82012-12-12 05:00:32287
rch9ecde09b2017-04-08 00:18:23288 // Called by a session when it blackholes after the handshake is confirmed.
289 void OnBlackholeAfterHandshakeConfirmed(QuicChromiumClientSession* session);
zhongyidd1439f62016-09-02 02:02:26290
[email protected]e13201d82012-12-12 05:00:32291 // Cancels a pending request.
292 void CancelRequest(QuicStreamRequest* request);
293
Lily Chenf11e1292018-11-29 16:42:09294 // Sets priority of a request.
295 void SetRequestPriority(QuicStreamRequest* request, RequestPriority priority);
296
Renjieba55fae2018-09-20 03:05:16297 // Closes all current sessions with specified network, QUIC error codes.
298 // It sends connection close packet when closing connections.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52299 void CloseAllSessions(int error, quic::QuicErrorCode quic_error);
[email protected]56dfb902013-01-03 23:17:55300
danakjad1777e2016-04-16 00:56:42301 std::unique_ptr<base::Value> QuicStreamFactoryInfoToValue() const;
[email protected]c5b061b2013-01-05 00:31:34302
msramek992625ec2016-08-04 18:33:58303 // Delete cached state objects in |crypto_config_|. If |origin_filter| is not
304 // null, only objects on matching origins will be deleted.
305 void ClearCachedStatesInCryptoConfig(
Anna Malova997d73f62020-03-02 19:01:13306 const base::RepeatingCallback<bool(const GURL&)>& origin_filter);
[email protected]f7e21a432014-04-21 22:17:57307
jri7e636642016-01-14 06:57:08308 // Helper method that configures a DatagramClientSocket. Socket is
309 // bound to the default network if the |network| param is
310 // NetworkChangeNotifier::kInvalidNetworkHandle.
311 // Returns net_error code.
312 int ConfigureSocket(DatagramClientSocket* socket,
313 IPEndPoint addr,
Paul Jensen8e3c5d32018-02-19 17:06:33314 NetworkChangeNotifier::NetworkHandle network,
315 const SocketTag& socket_tag);
jri7e636642016-01-14 06:57:08316
jriae8cfdc2016-01-21 19:44:35317 // Finds an alternative to |old_network| from the platform's list of connected
318 // networks. Returns NetworkChangeNotifier::kInvalidNetworkHandle if no
319 // alternative is found.
320 NetworkChangeNotifier::NetworkHandle FindAlternateNetwork(
321 NetworkChangeNotifier::NetworkHandle old_network);
322
Zhongyi Shi673f22ef2017-10-18 00:00:47323 // Creates a datagram socket. |source| is the NetLogSource for the entity
324 // trying to create the socket, if it has one.
325 std::unique_ptr<DatagramClientSocket> CreateSocket(
326 NetLog* net_log,
327 const NetLogSource& source);
328
[email protected]f698a012013-05-06 20:18:59329 // NetworkChangeNotifier::IPAddressObserver methods:
330
Jana Iyengar903dec22017-11-28 00:44:23331 // Until the servers support roaming, close all connections when the local
332 // IP address changes.
dchengb03027d2014-10-21 12:00:20333 void OnIPAddressChanged() override;
[email protected]f698a012013-05-06 20:18:59334
jri7e636642016-01-14 06:57:08335 // NetworkChangeNotifier::NetworkObserver methods:
336 void OnNetworkConnected(
337 NetworkChangeNotifier::NetworkHandle network) override;
338 void OnNetworkDisconnected(
339 NetworkChangeNotifier::NetworkHandle network) override;
340 void OnNetworkSoonToDisconnect(
341 NetworkChangeNotifier::NetworkHandle network) override;
342 void OnNetworkMadeDefault(
343 NetworkChangeNotifier::NetworkHandle network) override;
344
[email protected]d7d1e50b2013-11-25 22:08:09345 // CertDatabase::Observer methods:
346
347 // We close all sessions when certificate database is changed.
mattmfd05a1f2017-02-18 06:18:44348 void OnCertDBChanged() override;
[email protected]d7d1e50b2013-11-25 22:08:09349
Matt Menkeb566c392019-09-11 23:22:43350 bool is_quic_known_to_work_on_current_network() const {
351 return is_quic_known_to_work_on_current_network_;
352 }
[email protected]11c05872013-08-20 02:04:12353
Nick Harper72ade192019-07-17 03:30:42354 bool allow_server_migration() const { return params_.allow_server_migration; }
Zhongyi Shif124a582017-11-02 00:15:04355
Renjie Tangb23d99982020-08-20 01:51:17356 // Returns true is gQUIC 0-RTT is disabled from quic_context.
357 bool gquic_zero_rtt_disabled() const {
358 return params_.disable_gquic_zero_rtt;
359 }
360
Matt Menkeb566c392019-09-11 23:22:43361 void set_is_quic_known_to_work_on_current_network(
362 bool is_quic_known_to_work_on_current_network);
[email protected]11c05872013-08-20 02:04:12363
rtenneti8332ba52015-09-17 19:33:41364 // It returns the amount of time waiting job should be delayed.
Renjie Tangdd708972019-10-03 20:56:21365 base::TimeDelta GetTimeDelayForWaitingJob(const QuicSessionKey& session_key);
rtenneti8332ba52015-09-17 19:33:41366
rch12fef552016-01-15 16:26:31367 QuicChromiumConnectionHelper* helper() { return helper_.get(); }
[email protected]2cfc6bb82013-10-27 03:40:44368
Ryan Hamilton8d9ee76e2018-05-29 23:52:52369 quic::QuicAlarmFactory* alarm_factory() { return alarm_factory_.get(); }
rch16c74d1d2016-04-22 06:14:07370
zhongyiaf257542016-12-19 03:36:01371 void set_server_push_delegate(ServerPushDelegate* push_delegate) {
372 push_delegate_ = push_delegate;
373 }
374
Zhongyi Shi8de43832018-08-15 23:40:00375 NetworkChangeNotifier::NetworkHandle default_network() const {
376 return default_network_;
377 }
378
Cammie Smith Barnes0f38aff2021-03-02 23:14:35379 // Returns the stored DNS aliases for the session key.
380 const std::vector<std::string>& GetDnsAliasesForSessionKey(
381 const QuicSessionKey& key) const;
382
[email protected]e13201d82012-12-12 05:00:32383 private:
384 class Job;
Matt Menkefca05b62019-09-20 23:15:56385 class QuicCryptoClientConfigOwner;
386 class CryptoClientConfigHandle;
[email protected]c49ff182013-09-28 08:33:26387 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32388
Cammie Smith Barnes0f38aff2021-03-02 23:14:35389 using SessionMap = std::map<QuicSessionKey, QuicChromiumClientSession*>;
390 using SessionIdMap =
391 std::map<QuicChromiumClientSession*, QuicSessionAliasKey>;
392 using AliasSet = std::set<QuicSessionAliasKey>;
393 using SessionAliasMap = std::map<QuicChromiumClientSession*, AliasSet>;
394 using SessionSet = std::set<QuicChromiumClientSession*>;
395 using IPAliasMap = std::map<IPEndPoint, SessionSet>;
396 using SessionPeerIPMap = std::map<QuicChromiumClientSession*, IPEndPoint>;
397 using JobMap = std::map<QuicSessionKey, std::unique_ptr<Job>>;
398 using DnsAliasesBySessionKeyMap =
399 std::map<QuicSessionKey, std::vector<std::string>>;
Matt Menkefca05b62019-09-20 23:15:56400 using QuicCryptoClientConfigMap =
401 std::map<NetworkIsolationKey,
402 std::unique_ptr<QuicCryptoClientConfigOwner>>;
rtenneti14abd312015-02-06 21:56:01403
Paul Jensen8e3c5d32018-02-19 17:06:33404 bool HasMatchingIpSession(const QuicSessionAliasKey& key,
Cammie Smith Barnes0f38aff2021-03-02 23:14:35405 const AddressList& address_list,
406 bool use_dns_aliases);
[email protected]e13201d82012-12-12 05:00:32407 void OnJobComplete(Job* job, int rv);
Paul Jensen8e3c5d32018-02-19 17:06:33408 bool HasActiveSession(const QuicSessionKey& session_key) const;
409 bool HasActiveJob(const QuicSessionKey& session_key) const;
Paul Jensen8e3c5d32018-02-19 17:06:33410 int CreateSession(const QuicSessionAliasKey& key,
Ryan Hamilton9ef8c102019-06-28 03:58:52411 quic::ParsedQuicVersion quic_version,
rtennetia75df622015-06-21 23:59:50412 int cert_verify_flags,
rch433bf5f2017-02-14 04:10:47413 bool require_confirmation,
[email protected]338e7982013-12-13 11:15:32414 const AddressList& address_list,
xunjieli100937eb52016-09-15 20:09:37415 base::TimeTicks dns_resolution_start_time,
rtennetif4f08852015-02-27 17:50:04416 base::TimeTicks dns_resolution_end_time,
tfarina42834112016-09-22 13:38:20417 const NetLogWithSource& net_log,
Zhongyi Shi55ec9532018-07-24 03:57:39418 QuicChromiumClientSession** session,
419 NetworkChangeNotifier::NetworkHandle* network);
Paul Jensen8e3c5d32018-02-19 17:06:33420 void ActivateSession(const QuicSessionAliasKey& key,
Cammie Smith Barnes0f38aff2021-03-02 23:14:35421 QuicChromiumClientSession* session,
422 std::vector<std::string> dns_aliases);
Zhongyi Shic6b3f03f2020-06-25 21:28:22423 // Go away all active sessions. May disable session's connectivity monitoring
424 // based on the |reason|.
425 void MarkAllActiveSessionsGoingAway(AllActiveSessionsGoingAwayReason reason);
[email protected]e13201d82012-12-12 05:00:32426
Matt Menke19475f72019-08-21 18:57:44427 void ConfigureInitialRttEstimate(
428 const quic::QuicServerId& server_id,
429 const NetworkIsolationKey& network_isolation_key,
430 quic::QuicConfig* config);
rchd6163f32017-01-30 23:50:38431
rtenneti2912825c2015-01-06 01:19:46432 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
433 // is no |http_server_properties_| or if |http_server_properties_| doesn't
434 // have ServerNetworkStats for the given |server_id|.
Avi Drissman13fc8932015-12-20 04:40:46435 int64_t GetServerNetworkStatsSmoothedRttInMicroseconds(
Matt Menke19475f72019-08-21 18:57:44436 const quic::QuicServerId& server_id,
437 const NetworkIsolationKey& network_isolation_key) const;
rtenneti2912825c2015-01-06 01:19:46438
rchd6163f32017-01-30 23:50:38439 // Returns |srtt| from ServerNetworkStats. Returns null if there
440 // is no |http_server_properties_| or if |http_server_properties_| doesn't
441 // have ServerNetworkStats for the given |server_id|.
442 const base::TimeDelta* GetServerNetworkStatsSmoothedRtt(
Matt Menke19475f72019-08-21 18:57:44443 const quic::QuicServerId& server_id,
444 const NetworkIsolationKey& network_isolation_key) const;
rchd6163f32017-01-30 23:50:38445
bnccacc0992015-03-20 20:22:22446 // Helper methods.
Matt Menkeb32ba5122019-09-10 19:17:05447 bool WasQuicRecentlyBroken(const QuicSessionKey& session_key) const;
rtenneti8332ba52015-09-17 19:33:41448
Zhongyi Shid04bd412019-08-26 23:31:08449 // Helper method to initialize the following migration options and check
450 // pre-requisites:
451 // - |params_.migrate_sessions_on_network_change_v2|
452 // - |params_.migrate_sessions_early_v2|
453 // - |params_.migrate_idle_sessions|
454 // - |params_.retry_on_alternate_network_before_handshake|
455 // If pre-requisites are not met, turn off the corresponding options.
456 void InitializeMigrationOptions();
457
[email protected]257f24f2014-04-01 09:15:37458 // Initializes the cached state associated with |server_id| in
David Benjamin62566912021-10-14 00:56:16459 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18460 void InitializeCachedStateInCryptoConfig(
Matt Menkefca05b62019-09-20 23:15:56461 const CryptoClientConfigHandle& crypto_config_handle,
Ryan Hamilton8d9ee76e2018-05-29 23:52:52462 const quic::QuicServerId& server_id,
David Benjamin62566912021-10-14 00:56:16463 const std::unique_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13464
ckrasic4f9d88d2015-07-22 22:23:16465 void ProcessGoingAwaySession(QuicChromiumClientSession* session,
Ryan Hamilton8d9ee76e2018-05-29 23:52:52466 const quic::QuicServerId& server_id,
[email protected]eb71ab62014-05-23 07:57:53467 bool was_session_active);
[email protected]4d590c9c2014-05-02 05:14:33468
Cammie Smith Barnes0f38aff2021-03-02 23:14:35469 // Insert the given alias `key` in the AliasSet for the given `session` in
470 // the map `session_aliases_`, and add the given `dns_aliases` for
471 // `key.session_key()` in `dns_aliases_by_session_key_`.
472 void MapSessionToAliasKey(QuicChromiumClientSession* session,
473 const QuicSessionAliasKey& key,
474 std::vector<std::string> dns_aliases);
475
476 // For all alias keys for `session` in `session_aliases_`, erase the
477 // corresponding DNS aliases in `dns_aliases_by_session_key_`. Then erase
478 // `session` from `session_aliases_`.
479 void UnmapSessionFromSessionAliases(QuicChromiumClientSession* session);
480
Matt Menkefca05b62019-09-20 23:15:56481 // Creates a CreateCryptoConfigHandle for the specified NetworkIsolationKey.
482 // If there's already a corresponding entry in |active_crypto_config_map_|,
483 // reuses it. If there's a corresponding entry in |recent_crypto_config_map_|,
484 // promotes it to |active_crypto_config_map_| and then reuses it. Otherwise,
485 // creates a new entry in |active_crypto_config_map_|.
486 std::unique_ptr<CryptoClientConfigHandle> CreateCryptoConfigHandle(
487 const NetworkIsolationKey& network_isolation_key);
488
489 // Salled when the indicated member of |active_crypto_config_map_| has no
490 // outstanding references. The QuicCryptoClientConfigOwner is then moved to
491 // |recent_crypto_config_map_|, an MRU cache.
492 void OnAllCryptoClientRefReleased(
493 QuicCryptoClientConfigMap::iterator& map_iterator);
494
Zhongyi Shic6b3f03f2020-06-25 21:28:22495 // Called when a network change happens.
496 // Collect platform notification metrics, and if the change affects the
497 // original default network interface, collect connectivity degradation
498 // metrics from |connectivity_monitor_| and add to histograms.
499 void CollectDataOnPlatformNotification(
500 enum QuicPlatformNotification notification,
501 NetworkChangeNotifier::NetworkHandle affected_network) const;
502
Matt Menkefca05b62019-09-20 23:15:56503 std::unique_ptr<QuicCryptoClientConfigHandle> GetCryptoConfigForTesting(
504 const NetworkIsolationKey& network_isolation_key);
505
Matt Menkeca5253312019-09-23 18:59:37506 bool CryptoConfigCacheIsEmptyForTesting(
507 const quic::QuicServerId& server_id,
508 const NetworkIsolationKey& network_isolation_key);
509
Matt Menkeb566c392019-09-11 23:22:43510 // Whether QUIC is known to work on current network. This is true when QUIC is
511 // expected to work in general, rather than whether QUIC was broken / recently
512 // broken when used with a particular server. That information is stored in
513 // the broken alternative service map in HttpServerProperties.
514 bool is_quic_known_to_work_on_current_network_;
515
jridf673d22016-06-02 22:06:33516 NetLog* net_log_;
[email protected]e13201d82012-12-12 05:00:32517 HostResolver* host_resolver_;
518 ClientSocketFactory* client_socket_factory_;
bnc525e175a2016-06-20 12:36:40519 HttpServerProperties* http_server_properties_;
zhongyiaf257542016-12-19 03:36:01520 ServerPushDelegate* push_delegate_;
Matt Menkefca05b62019-09-20 23:15:56521 CertVerifier* const cert_verifier_;
522 CTPolicyEnforcer* const ct_policy_enforcer_;
523 TransportSecurityState* const transport_security_state_;
Chris Thompsonf31b2492020-07-21 05:47:42524 SCTAuditingDelegate* const sct_auditing_delegate_;
[email protected]e8ff26842013-03-22 21:02:05525 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52526 quic::QuicRandom* random_generator_; // Unowned.
Victor Vasiliev7752898d2019-11-14 21:30:22527 const quic::QuicClock* clock_; // Unowned.
Nick Harper72ade192019-07-17 03:30:42528 QuicParams params_;
rchf11cc0f2016-11-15 03:04:32529 QuicClockSkewDetector clock_skew_detector_;
[email protected]e13201d82012-12-12 05:00:32530
tbansalfdf5665b2015-09-21 22:46:40531 // Factory which is used to create socket performance watcher. A new watcher
532 // is created for every QUIC connection.
533 // |socket_performance_watcher_factory_| may be null.
tbansalc8a94ea2015-11-02 23:58:51534 SocketPerformanceWatcherFactory* socket_performance_watcher_factory_;
tbansalfdf5665b2015-09-21 22:46:40535
[email protected]2cfc6bb82013-10-27 03:40:44536 // The helper used for all connections.
danakjad1777e2016-04-16 00:56:42537 std::unique_ptr<QuicChromiumConnectionHelper> helper_;
[email protected]2cfc6bb82013-10-27 03:40:44538
rch16c74d1d2016-04-22 06:14:07539 // The alarm factory used for all connections.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52540 std::unique_ptr<quic::QuicAlarmFactory> alarm_factory_;
rch16c74d1d2016-04-22 06:14:07541
[email protected]e13201d82012-12-12 05:00:32542 // Contains owning pointers to all sessions that currently exist.
[email protected]4d590c9c2014-05-02 05:14:33543 SessionIdMap all_sessions_;
[email protected]e13201d82012-12-12 05:00:32544 // Contains non-owning pointers to currently active session
545 // (not going away session, once they're implemented).
546 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38547 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32548 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38549 // Map from IP address to sessions which are connected to this address.
550 IPAliasMap ip_aliases_;
jri94ddc3142016-08-26 01:32:43551 // Map from session to its original peer IP address.
552 SessionPeerIPMap session_peer_ip_;
[email protected]e13201d82012-12-12 05:00:32553
[email protected]d8e2abf82014-03-06 10:30:10554 // Origins which have gone away recently.
555 AliasSet gone_away_aliases_;
556
Cammie Smith Barnes0f38aff2021-03-02 23:14:35557 // A map of DNS alias vectors by session keys.
558 DnsAliasesBySessionKeyMap dns_aliases_by_session_key_;
559
Matt Menkefca05b62019-09-20 23:15:56560 // When a QuicCryptoClientConfig is in use, it has one or more live
561 // CryptoClientConfigHandles, and is stored in |active_crypto_config_map_|.
562 // Once all the handles are deleted, it's moved to
563 // |recent_crypto_config_map_|. If reused before it is evicted from MRUCache,
564 // it will be removed from the cache and return to the active config map.
565 // These two maps should never both have entries with the same
566 // NetworkIsolationKey.
567 QuicCryptoClientConfigMap active_crypto_config_map_;
568 base::MRUCache<NetworkIsolationKey,
569 std::unique_ptr<QuicCryptoClientConfigOwner>>
570 recent_crypto_config_map_;
571
Ryan Hamilton8d9ee76e2018-05-29 23:52:52572 const quic::QuicConfig config_;
[email protected]b064310782013-05-30 21:12:17573
[email protected]e13201d82012-12-12 05:00:32574 JobMap active_jobs_;
[email protected]e13201d82012-12-12 05:00:32575
zhongyidd1439f62016-09-02 02:02:26576 // PING timeout for connections.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52577 quic::QuicTime::Delta ping_timeout_;
578 quic::QuicTime::Delta reduced_ping_timeout_;
zhongyidd1439f62016-09-02 02:02:26579
Zhongyi Shie01f2db2019-02-22 19:53:23580 // Timeout for how long the wire can have no retransmittable packets.
581 quic::QuicTime::Delta retransmittable_on_wire_timeout_;
582
rtenneti1cd3b162015-09-29 02:58:28583 // If more than |yield_after_packets_| packets have been read or more than
584 // |yield_after_duration_| time has passed, then
rcha02807b42016-01-29 21:56:15585 // QuicChromiumPacketReader::StartReading() yields by doing a PostTask().
rtenneti1cd3b162015-09-29 02:58:28586 int yield_after_packets_;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52587 quic::QuicTime::Delta yield_after_duration_;
rtenneti1cd3b162015-09-29 02:58:28588
Zhongyi Shi6db13462018-05-19 01:36:26589 // If |migrate_sessions_early_v2_| is true, tracks the current default
590 // network, and is updated OnNetworkMadeDefault.
591 // Otherwise, always set to NetworkChangeNotifier::kInvalidNetwork.
592 NetworkChangeNotifier::NetworkHandle default_network_;
593
rtennetifc47e0e2014-09-26 02:54:05594 // Local address of socket that was created in CreateSession.
595 IPEndPoint local_address_;
Zhongyi Shia0cef1082017-08-25 01:49:50596 // True if we need to check HttpServerProperties if QUIC was supported last
597 // time.
598 bool need_to_check_persisted_supports_quic_;
Matt Menkefca05b62019-09-20 23:15:56599 bool prefer_aes_gcm_recorded_;
rtennetifc47e0e2014-09-26 02:54:05600
rtenneti041b2992015-02-23 23:03:28601 NetworkConnection network_connection_;
602
ckrasic3865ee0f2016-02-29 22:04:56603 int num_push_streams_created_;
604
Zhongyi Shic6b3f03f2020-06-25 21:28:22605 QuicConnectivityMonitor connectivity_monitor_;
606
Zhongyi Shic16b4102019-02-12 00:37:40607 const base::TickClock* tick_clock_;
608
Zhongyi Shi8fff75b2017-11-19 21:36:36609 base::SequencedTaskRunner* task_runner_;
rtenneti38f5cd52014-10-28 20:28:28610
Ryan Sleevib8449e02018-07-15 04:31:07611 SSLConfigService* const ssl_config_service_;
nharper642ae4b2016-06-30 00:40:36612
Matt Menkefca05b62019-09-20 23:15:56613 // Whether NetworkIsolationKeys should be used for
614 // |active_crypto_config_map_|. If false, there will just be one config with
615 // an empty NetworkIsolationKey. Whether QuicSessionAliasKeys all have an
616 // empty NIK is based on whether socket pools are respecting NIKs, but whether
617 // those NIKs are also used when accessing |active_crypto_config_map_| is also
618 // gated this, which is set based on whether HttpServerProperties is
619 // respecting NIKs, as that data is fed into the crypto config map using the
620 // corresponding NIK.
621 const bool use_network_isolation_key_for_crypto_configs_;
622
Jeremy Romand54000b22019-07-08 18:40:16623 base::WeakPtrFactory<QuicStreamFactory> weak_factory_{this};
[email protected]e13201d82012-12-12 05:00:32624};
625
626} // namespace net
627
Ryan Hamiltona3ee93a72018-08-01 22:03:08628#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_