blob: ea1a63f0e33441aa593cb36ea8a63fed5b020ac7 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2017 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 QUICHE_QUIC_QUARTC_QUARTC_ENDPOINT_H_
6#define QUICHE_QUIC_QUARTC_QUARTC_ENDPOINT_H_
7
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
9
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#include "net/third_party/quiche/src/quic/core/quic_alarm_factory.h"
vasilvv89713d02020-02-11 14:33:26 -080011#include "net/third_party/quiche/src/quic/core/quic_clock.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "net/third_party/quiche/src/quic/quartc/quartc_connection_helper.h"
14#include "net/third_party/quiche/src/quic/quartc/quartc_crypto_helpers.h"
15#include "net/third_party/quiche/src/quic/quartc/quartc_dispatcher.h"
16#include "net/third_party/quiche/src/quic/quartc/quartc_factory.h"
dmcardlec60e87a2019-12-12 09:43:19 -080017#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050018
19namespace quic {
20
QUICHE teama6ef0a62019-03-07 20:34:33 -050021// Endpoint (client or server) in a peer-to-peer Quartc connection.
22class QuartcEndpoint {
23 public:
QUICHE team6939de52019-05-15 12:05:21 -070024 class Delegate : public QuartcSession::Delegate {
QUICHE teama6ef0a62019-03-07 20:34:33 -050025 public:
26 virtual ~Delegate() = default;
27
28 // Called when an endpoint creates a new session, before any packets are
29 // processed or sent. The callee should perform any additional
QUICHE team6939de52019-05-15 12:05:21 -070030 // configuration required, such as setting up congestion control, before
QUICHE teama6ef0a62019-03-07 20:34:33 -050031 // returning. |session| is owned by the endpoint, but remains safe to use
QUICHE team6939de52019-05-15 12:05:21 -070032 // until another call to |OnSessionCreated| or |OnConnectionClosed| occurs,
33 // at which point previous session may be destroyed.
34 //
35 // Callees must not change the |session|'s delegate. The Endpoint itself
36 // manages the delegate and will forward calls.
37 //
38 // New calls to |OnSessionCreated| will only occur prior to
39 // |OnConnectionWritable|, during initial connection negotiation.
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 virtual void OnSessionCreated(QuartcSession* session) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050041 };
42
43 virtual ~QuartcEndpoint() = default;
44
45 // Connects the endpoint using the given session config. After |Connect| is
46 // called, the endpoint will asynchronously create a session, then call
47 // |Delegate::OnSessionCreated|.
48 virtual void Connect(QuartcPacketTransport* packet_transport) = 0;
49};
50
51// Implementation of QuartcEndpoint which immediately (but asynchronously)
52// creates a session by scheduling a QuicAlarm. Only suitable for use with the
53// client perspective.
QUICHE team6939de52019-05-15 12:05:21 -070054class QuartcClientEndpoint : public QuartcEndpoint,
55 public QuartcSession::Delegate {
QUICHE teama6ef0a62019-03-07 20:34:33 -050056 public:
57 // |alarm_factory|, |clock|, and |delegate| are owned by the caller and must
58 // outlive the endpoint.
59 QuartcClientEndpoint(
60 QuicAlarmFactory* alarm_factory,
61 const QuicClock* clock,
QUICHE team73957f12019-04-18 16:21:52 -070062 QuicRandom* random,
QUICHE team6939de52019-05-15 12:05:21 -070063 QuartcEndpoint::Delegate* delegate,
QUICHE teama6ef0a62019-03-07 20:34:33 -050064 const QuartcSessionConfig& config,
dmcardlec60e87a2019-12-12 09:43:19 -080065 quiche::QuicheStringPiece serialized_server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050066 std::unique_ptr<QuicVersionManager> version_manager = nullptr);
67
68 void Connect(QuartcPacketTransport* packet_transport) override;
69
QUICHE team6939de52019-05-15 12:05:21 -070070 // QuartcSession::Delegate overrides.
71 void OnCryptoHandshakeComplete() override;
72 void OnConnectionWritable() override;
73 void OnIncomingStream(QuartcStream* stream) override;
74 void OnCongestionControlChange(QuicBandwidth bandwidth_estimate,
75 QuicBandwidth pacing_rate,
76 QuicTime::Delta latest_rtt) override;
fkastenholz5d880a92019-06-21 09:01:56 -070077 void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
QUICHE team6939de52019-05-15 12:05:21 -070078 ConnectionCloseSource source) override;
dmcardlec60e87a2019-12-12 09:43:19 -080079 void OnMessageReceived(quiche::QuicheStringPiece message) override;
QUICHE team6939de52019-05-15 12:05:21 -070080 void OnMessageSent(int64_t datagram_id) override;
QUICHE team68d15a82019-05-31 15:27:25 -070081 void OnMessageAcked(int64_t datagram_id, QuicTime receive_timestamp) override;
82 void OnMessageLost(int64_t datagram_id) override;
QUICHE team6939de52019-05-15 12:05:21 -070083
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 private:
85 friend class CreateSessionDelegate;
86 class CreateSessionDelegate : public QuicAlarm::Delegate {
87 public:
88 CreateSessionDelegate(QuartcClientEndpoint* endpoint)
89 : endpoint_(endpoint) {}
90
91 void OnAlarm() override { endpoint_->OnCreateSessionAlarm(); }
92
93 private:
94 QuartcClientEndpoint* endpoint_;
95 };
96
97 // Callback which occurs when |create_session_alarm_| fires.
98 void OnCreateSessionAlarm();
99
100 // Implementation of QuicAlarmFactory used by this endpoint. Unowned.
101 QuicAlarmFactory* alarm_factory_;
102
103 // Implementation of QuicClock used by this endpoint. Unowned.
104 const QuicClock* clock_;
105
106 // Delegate which receives callbacks for newly created sessions.
107 QuartcEndpoint::Delegate* delegate_;
108
109 // Server config. If valid, used to perform a 0-RTT connection.
vasilvvc48c8712019-03-11 13:38:16 -0700110 const std::string serialized_server_config_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111
112 // Version manager. May be injected to control version negotiation in tests.
113 std::unique_ptr<QuicVersionManager> version_manager_;
114
QUICHE team6939de52019-05-15 12:05:21 -0700115 // Versions to be used when the next session is created. The session will
116 // choose one of these versions for its connection attempt.
117 //
118 // If the connection does not succeed, the client session MAY try again using
119 // another version from this list, or it MAY simply fail with a
120 // QUIC_INVALID_VERSION error. The latter occurs when it is not possible to
121 // upgrade a connection in-place (for example, if the way stream ids are
122 // allocated changes between versions). This failure mode is handled by
123 // narrowing |current_versions_| to one of that is mutually-supported and
124 // reconnecting (with a new session).
125 ParsedQuicVersionVector current_versions_;
126
QUICHE teama6ef0a62019-03-07 20:34:33 -0500127 // Alarm for creating sessions asynchronously. The alarm is set when
128 // Connect() is called. When it fires, the endpoint creates a session and
129 // calls the delegate.
130 std::unique_ptr<QuicAlarm> create_session_alarm_;
131
QUICHE teamb1140922019-03-22 05:24:05 -0700132 // Helper used by QuicConnection.
133 std::unique_ptr<QuicConnectionHelperInterface> connection_helper_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134
135 // Config to be used for new sessions.
136 QuartcSessionConfig config_;
137
138 // The currently-active session. Nullptr until |Connect| and
139 // |Delegate::OnSessionCreated| are called.
140 std::unique_ptr<QuartcSession> session_;
141
142 QuartcPacketTransport* packet_transport_;
143};
144
145// Implementation of QuartcEndpoint which uses a QuartcDispatcher to listen for
146// an incoming CHLO and create a session when one arrives. Only suitable for
147// use with the server perspective.
148class QuartcServerEndpoint : public QuartcEndpoint,
149 public QuartcDispatcher::Delegate {
150 public:
151 QuartcServerEndpoint(
152 QuicAlarmFactory* alarm_factory,
153 const QuicClock* clock,
QUICHE team73957f12019-04-18 16:21:52 -0700154 QuicRandom* random,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155 QuartcEndpoint::Delegate* delegate,
156 const QuartcSessionConfig& config,
157 std::unique_ptr<QuicVersionManager> version_manager = nullptr);
158
159 // Implements QuartcEndpoint.
160 void Connect(QuartcPacketTransport* packet_transport) override;
161
162 // Implements QuartcDispatcher::Delegate.
163 void OnSessionCreated(QuartcSession* session) override;
164
165 // Accessor to retrieve the server crypto config. May only be called after
166 // Connect().
dmcardlec60e87a2019-12-12 09:43:19 -0800167 quiche::QuicheStringPiece server_crypto_config() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 return crypto_config_.serialized_crypto_config;
169 }
170
171 const std::vector<ParsedQuicVersion> GetSupportedQuicVersions() const {
172 return version_manager_->GetSupportedVersions();
173 }
174
175 private:
176 // Implementation of QuicAlarmFactory used by this endpoint. Unowned.
177 QuicAlarmFactory* alarm_factory_;
178
179 // Delegate which receives callbacks for newly created sessions.
180 QuartcEndpoint::Delegate* delegate_;
181
182 // Config to be used for new sessions.
183 QuartcSessionConfig config_;
184
185 // Version manager. May be injected to control version negotiation in tests.
186 std::unique_ptr<QuicVersionManager> version_manager_;
187
188 // QuartcDispatcher waits for an incoming CHLO, then either rejects it or
189 // creates a session to respond to it. The dispatcher owns all sessions it
190 // creates.
191 std::unique_ptr<QuartcDispatcher> dispatcher_;
192
193 // This field is only available before connection was started.
194 std::unique_ptr<QuartcConnectionHelper> pre_connection_helper_;
195
196 // A configuration, containing public key, that may need to be passed to the
197 // client to enable 0rtt.
198 CryptoServerConfig crypto_config_;
199};
200
201} // namespace quic
202
203#endif // QUICHE_QUIC_QUARTC_QUARTC_ENDPOINT_H_