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