mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_P2P_QUIC_QUICSESSION_H_ |
| 12 | #define WEBRTC_P2P_QUIC_QUICSESSION_H_ |
| 13 | |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 14 | #include <memory> |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 15 | #include <string> |
| 16 | |
| 17 | #include "net/quic/quic_crypto_client_stream.h" |
| 18 | #include "net/quic/quic_crypto_server_stream.h" |
| 19 | #include "net/quic/quic_crypto_stream.h" |
| 20 | #include "net/quic/quic_session.h" |
| 21 | #include "webrtc/base/constructormagic.h" |
| 22 | #include "webrtc/base/sigslot.h" |
| 23 | #include "webrtc/base/sslidentity.h" |
| 24 | #include "webrtc/p2p/quic/reliablequicstream.h" |
| 25 | |
| 26 | namespace cricket { |
| 27 | |
| 28 | // This class provides a QUIC session over peer-to-peer transport that |
| 29 | // negotiates the crypto handshake (using QuicCryptoHandshake) and provides |
| 30 | // reading/writing of data using QUIC packets. |
| 31 | class QuicSession : public net::QuicSession, public sigslot::has_slots<> { |
| 32 | public: |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 33 | QuicSession(std::unique_ptr<net::QuicConnection> connection, |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 34 | const net::QuicConfig& config); |
| 35 | ~QuicSession() override; |
| 36 | |
| 37 | // Initiates client crypto handshake by sending client hello. |
| 38 | void StartClientHandshake(net::QuicCryptoClientStream* crypto_stream); |
| 39 | |
| 40 | // Responds to a client who has inititated the crypto handshake. |
| 41 | void StartServerHandshake(net::QuicCryptoServerStream* crypto_stream); |
| 42 | |
| 43 | // QuicSession overrides. |
| 44 | net::QuicCryptoStream* GetCryptoStream() override { |
| 45 | return crypto_stream_.get(); |
| 46 | } |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 47 | ReliableQuicStream* CreateOutgoingDynamicStream( |
| 48 | net::SpdyPriority priority) override; |
| 49 | |
| 50 | // QuicSession optional overrides. |
| 51 | void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) override; |
mikescarlett | 70035ca | 2016-04-30 01:14:37 | [diff] [blame] | 52 | void CloseStream(net::QuicStreamId stream_id) override; |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 53 | |
| 54 | // QuicConnectionVisitorInterface overrides. |
mikescarlett | f537768 | 2016-03-29 19:14:55 | [diff] [blame] | 55 | void OnConnectionClosed(net::QuicErrorCode error, |
mikescarlett | 8d37d29 | 2016-04-29 22:35:00 | [diff] [blame] | 56 | const std::string& error_details, |
mikescarlett | f537768 | 2016-03-29 19:14:55 | [diff] [blame] | 57 | net::ConnectionCloseSource source) override; |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 58 | |
| 59 | // Exports keying material for SRTP. |
| 60 | bool ExportKeyingMaterial(base::StringPiece label, |
| 61 | base::StringPiece context, |
| 62 | size_t result_len, |
mikescarlett | 8d37d29 | 2016-04-29 22:35:00 | [diff] [blame] | 63 | std::string* result); |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 64 | |
| 65 | // Decrypts an incoming QUIC packet to a data stream. |
| 66 | bool OnReadPacket(const char* data, size_t data_len); |
| 67 | |
| 68 | // Called when peers have established forward-secure encryption |
| 69 | sigslot::signal0<> SignalHandshakeComplete; |
| 70 | // Called when connection closes locally, or remotely by peer. |
| 71 | sigslot::signal2<net::QuicErrorCode, bool> SignalConnectionClosed; |
| 72 | // Called when an incoming QUIC stream is created so we can process data |
| 73 | // from it by registering a listener to |
| 74 | // ReliableQuicStream::SignalDataReceived. |
| 75 | sigslot::signal1<ReliableQuicStream*> SignalIncomingStream; |
| 76 | |
| 77 | protected: |
| 78 | // Sets the QUIC crypto stream and takes ownership of it. |
| 79 | void SetCryptoStream(net::QuicCryptoStream* crypto_stream); |
| 80 | |
| 81 | // QuicSession override. |
| 82 | ReliableQuicStream* CreateIncomingDynamicStream( |
| 83 | net::QuicStreamId id) override; |
| 84 | |
mikescarlett | 70035ca | 2016-04-30 01:14:37 | [diff] [blame] | 85 | virtual ReliableQuicStream* CreateDataStream(net::QuicStreamId id, |
| 86 | net::SpdyPriority priority); |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 87 | |
| 88 | private: |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 89 | std::unique_ptr<net::QuicCryptoStream> crypto_stream_; |
mikescarlett | 8d37d29 | 2016-04-29 22:35:00 | [diff] [blame] | 90 | net::QuicClock clock_; // For recording packet receipt time |
mikescarlett | cd0e475 | 2016-02-09 01:35:47 | [diff] [blame] | 91 | |
| 92 | RTC_DISALLOW_COPY_AND_ASSIGN(QuicSession); |
| 93 | }; |
| 94 | |
| 95 | } // namespace cricket |
| 96 | |
| 97 | #endif // WEBRTC_P2P_QUIC_QUICSESSION_H_ |