blob: 411c94c08f4d3a9e8892f7665fdb2b5bad510561 [file] [log] [blame]
QUICHE teamc9b2cec2019-01-07 17:54:15 -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_SESSION_H_
6#define QUICHE_QUIC_QUARTC_QUARTC_SESSION_H_
7
8#include <memory>
9#include <string>
10
11#include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h"
12#include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream.h"
13#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
14#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
15#include "net/third_party/quiche/src/quic/core/quic_session.h"
16#include "net/third_party/quiche/src/quic/core/quic_types.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
18#include "net/third_party/quiche/src/quic/quartc/quartc_packet_writer.h"
19#include "net/third_party/quiche/src/quic/quartc/quartc_stream.h"
20
21namespace quic {
22
23// A helper class is used by the QuicCryptoServerStream.
24class QuartcCryptoServerStreamHelper : public QuicCryptoServerStream::Helper {
25 public:
26 QuicConnectionId GenerateConnectionIdForReject(
QUICHE teamdc5ce1a2019-01-30 16:01:47 -050027 QuicTransportVersion version,
QUICHE teamc9b2cec2019-01-07 17:54:15 -050028 QuicConnectionId connection_id) const override;
29
30 bool CanAcceptClientHello(const CryptoHandshakeMessage& message,
31 const QuicSocketAddress& client_address,
32 const QuicSocketAddress& peer_address,
33 const QuicSocketAddress& self_address,
34 QuicString* error_details) const override;
35};
36
37// QuartcSession owns and manages a QUIC connection.
38class QUIC_EXPORT_PRIVATE QuartcSession
39 : public QuicSession,
40 public QuartcPacketTransport::Delegate,
41 public QuicCryptoClientStream::ProofHandler {
42 public:
43 QuartcSession(std::unique_ptr<QuicConnection> connection,
44 const QuicConfig& config,
45 const ParsedQuicVersionVector& supported_versions,
46 const QuicString& unique_remote_server_id,
47 Perspective perspective,
48 QuicConnectionHelperInterface* helper,
49 const QuicClock* clock,
50 std::unique_ptr<QuartcPacketWriter> packet_writer);
51 QuartcSession(const QuartcSession&) = delete;
52 QuartcSession& operator=(const QuartcSession&) = delete;
53 ~QuartcSession() override;
54
55 // QuicSession overrides.
56 QuicCryptoStream* GetMutableCryptoStream() override;
57
58 const QuicCryptoStream* GetCryptoStream() const override;
59
60 QuartcStream* CreateOutgoingBidirectionalStream();
61
62 // Sends short unreliable message using quic message frame (message must fit
63 // in one quic packet). If connection is blocked by congestion control,
64 // message will be queued and resent later after receiving an OnCanWrite
65 // notification.
66 //
67 // Message size must be <= GetLargestMessagePayload().
68 //
69 // Supported in quic version 45 or later.
70 //
71 // Returns false and logs error if message is too long or session does not
72 // support SendMessage API. Other unexpected errors during send will not be
73 // returned, because messages can be sent later if connection is congestion
74 // controlled.
75 bool SendOrQueueMessage(QuicString message);
76
77 // Returns largest message payload acceptable in SendQuartcMessage.
78 QuicPacketLength GetLargestMessagePayload() const {
79 return connection()->GetLargestMessagePayload();
80 }
81
82 // Return true if transport support message frame.
83 bool CanSendMessage() const {
84 return connection()->transport_version() >= QUIC_VERSION_45;
85 }
86
87 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) override;
88
89 // QuicConnectionVisitorInterface overrides.
90 void OnCongestionWindowChange(QuicTime now) override;
91
92 void OnCanWrite() override;
93
94 void OnConnectionClosed(QuicErrorCode error,
95 const QuicString& error_details,
96 ConnectionCloseSource source) override;
97
98 // QuartcSession methods.
99
100 // Sets a pre-shared key for use during the crypto handshake. Must be set
101 // before StartCryptoHandshake() is called.
102 void SetPreSharedKey(QuicStringPiece key);
103
104 void StartCryptoHandshake();
105
106 // Closes the connection with the given human-readable error details.
107 // The connection closes with the QUIC_CONNECTION_CANCELLED error code to
108 // indicate the application closed it.
109 //
110 // Informs the peer that the connection has been closed. This prevents the
111 // peer from waiting until the connection times out.
112 //
113 // Cleans up the underlying QuicConnection's state. Closing the connection
114 // makes it safe to delete the QuartcSession.
115 void CloseConnection(const QuicString& details);
116
117 // If the given stream is still open, sends a reset frame to cancel it.
118 // Note: This method cancels a stream by QuicStreamId rather than by pointer
119 // (or by a method on QuartcStream) because QuartcSession (and not
120 // the caller) owns the streams. Streams may finish and be deleted before the
121 // caller tries to cancel them, rendering the caller's pointers invalid.
122 void CancelStream(QuicStreamId stream_id);
123
124 // Callbacks called by the QuartcSession to notify the user of the
125 // QuartcSession of certain events.
126 class Delegate {
127 public:
128 virtual ~Delegate() {}
129
QUICHE teamdc5ce1a2019-01-30 16:01:47 -0500130 // Called when the crypto handshake is complete. Crypto handshake on the
131 // client is only completed _after_ SHLO is received, but we can actually
132 // start sending media data right after CHLO is sent.
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500133 virtual void OnCryptoHandshakeComplete() = 0;
134
QUICHE teamdc5ce1a2019-01-30 16:01:47 -0500135 // Connection can be writable even before crypto handshake is complete.
136 // In particular, on the client, we can start sending data after sending
137 // full CHLO, without waiting for SHLO. This reduces a send delay by 1-rtt.
138 //
139 // This may be called multiple times.
140 virtual void OnConnectionWritable() = 0;
141
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500142 // Called when a new stream is received from the remote endpoint.
143 virtual void OnIncomingStream(QuartcStream* stream) = 0;
144
145 // Called when network parameters change in response to an ack frame.
146 virtual void OnCongestionControlChange(QuicBandwidth bandwidth_estimate,
147 QuicBandwidth pacing_rate,
148 QuicTime::Delta latest_rtt) = 0;
149
150 // Called when the connection is closed. This means all of the streams will
151 // be closed and no new streams can be created.
152 virtual void OnConnectionClosed(QuicErrorCode error_code,
153 const QuicString& error_details,
154 ConnectionCloseSource source) = 0;
155
156 // Called when message (sent as SendMessage) is received.
157 virtual void OnMessageReceived(QuicStringPiece message) = 0;
158
159 // TODO(zhihuang): Add proof verification.
160 };
161
162 // The |delegate| is not owned by QuartcSession.
163 void SetDelegate(Delegate* session_delegate);
164
165 // Called when CanWrite() changes from false to true.
166 void OnTransportCanWrite() override;
167
168 // Called when a packet has been received and should be handled by the
169 // QuicConnection.
170 void OnTransportReceived(const char* data, size_t data_len) override;
171
172 void OnMessageReceived(QuicStringPiece message) override;
173
174 // ProofHandler overrides.
175 void OnProofValid(const QuicCryptoClientConfig::CachedState& cached) override;
176
177 // Called by the client crypto handshake when proof verification details
178 // become available, either because proof verification is complete, or when
179 // cached details are used.
180 void OnProofVerifyDetailsAvailable(
181 const ProofVerifyDetails& verify_details) override;
182
183 // Returns number of queued (not sent) messages submitted by
184 // SendOrQueueMessage. Messages are queued if connection is congestion
185 // controlled.
186 size_t send_message_queue_size() const { return send_message_queue_.size(); }
187
188 protected:
189 // QuicSession override.
190 QuicStream* CreateIncomingStream(QuicStreamId id) override;
191 QuicStream* CreateIncomingStream(PendingStream pending) override;
192
193 std::unique_ptr<QuartcStream> CreateDataStream(QuicStreamId id,
194 spdy::SpdyPriority priority);
195 std::unique_ptr<QuartcStream> CreateDataStream(PendingStream pending,
196 spdy::SpdyPriority priority);
197 // Activates a QuartcStream. The session takes ownership of the stream, but
198 // returns an unowned pointer to the stream for convenience.
199 QuartcStream* ActivateDataStream(std::unique_ptr<QuartcStream> stream);
200
201 void ResetStream(QuicStreamId stream_id, QuicRstStreamErrorCode error);
202
203 private:
204 std::unique_ptr<QuartcStream> InitializeDataStream(
205 std::unique_ptr<QuartcStream> stream,
206 spdy::SpdyPriority priority);
207
208 void ProcessSendMessageQueue();
209
210 // For crypto handshake.
211 std::unique_ptr<QuicCryptoStream> crypto_stream_;
212 const QuicString unique_remote_server_id_;
213 Perspective perspective_;
214
215 // Packet writer used by |connection_|.
216 std::unique_ptr<QuartcPacketWriter> packet_writer_;
217
218 // Take ownership of the QuicConnection. Note: if |connection_| changes,
219 // the new value of |connection_| must be given to |packet_writer_| before any
220 // packets are written. Otherwise, |packet_writer_| will crash.
221 std::unique_ptr<QuicConnection> connection_;
222 // Not owned by QuartcSession. From the QuartcFactory.
223 QuicConnectionHelperInterface* helper_;
224 // For recording packet receipt time
225 const QuicClock* clock_;
226 // Not owned by QuartcSession.
227 Delegate* session_delegate_ = nullptr;
228 // Used by QUIC crypto server stream to track most recently compressed certs.
229 std::unique_ptr<QuicCompressedCertsCache> quic_compressed_certs_cache_;
230 // This helper is needed when create QuicCryptoServerStream.
231 QuartcCryptoServerStreamHelper stream_helper_;
232 // Config for QUIC crypto client stream, used by the client.
233 std::unique_ptr<QuicCryptoClientConfig> quic_crypto_client_config_;
234 // Config for QUIC crypto server stream, used by the server.
235 std::unique_ptr<QuicCryptoServerConfig> quic_crypto_server_config_;
236
237 // Queue of pending messages sent by SendQuartcMessage that were not sent
238 // yet or blocked by congestion control. Messages are queued in the order
239 // of sent by SendOrQueueMessage().
240 QuicDeque<QuicString> send_message_queue_;
241};
242
243} // namespace quic
244
245#endif // QUICHE_QUIC_QUARTC_QUARTC_SESSION_H_