Chromium Code Reviews| Index: webrtc/p2p/quic/quictransportchannel.h |
| diff --git a/webrtc/p2p/quic/quictransportchannel.h b/webrtc/p2p/quic/quictransportchannel.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..faf526af8401a13cc805d2aecac64a4485f2a297 |
| --- /dev/null |
| +++ b/webrtc/p2p/quic/quictransportchannel.h |
| @@ -0,0 +1,284 @@ |
| +/* |
| + * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#ifndef WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |
| +#define WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "net/quic/quic_packet_writer.h" |
| +#include "webrtc/base/scoped_ptr.h" |
| +#include "webrtc/p2p/base/transportchannelimpl.h" |
| +#include "webrtc/p2p/quic/quicconnectionhelper.h" |
| +#include "webrtc/p2p/quic/quicsession.h" |
| + |
| +namespace cricket { |
| + |
| +enum QuicTransportState { |
| + // Haven't started QUIC handshake. |
| + QUIC_TRANSPORT_NEW = 0, |
| + // Started QUIC handshake. |
| + QUIC_TRANSPORT_CONNECTING, |
| + // Negotiated, and has an ecrypted connection. |
|
pthatcher1
2016/02/24 23:31:13
encrypted
mikescarlett
2016/02/26 00:54:14
Done.
|
| + QUIC_TRANSPORT_CONNECTED, |
| + // Failed due to some error in the handshake process. |
| + QUIC_TRANSPORT_FAILED, |
| +}; |
| + |
| +// QuicTransportChannel uses the QUIC protocol to establish encryption with |
| +// another peer, wrapping an existing TransportChannel instance |
| +// (e.g a P2PTransportChannel) responsible for connecting peers. |
| +// Once the wrapped transport channel is connected, QuicTransportChannel |
| +// negotiates the crypto handshake and establishes SRTP keying material. |
| +// |
| +// How it works: |
| +// |
| +// QuicTransportChannel { |
| +// QuicSession* quic_; |
| +// TransportChannelImpl* channel_; |
| +// } |
| +// |
| +// - Data written to SendPacket() is passed directly to |channel_| if the QUIC |
| +// handshake has not begun or if an SRTP packet is sent with the |
| +// PF_SRTP_BYPASS flag after the QUIC handshake. |
|
pthatcher1
2016/02/24 23:31:13
I understand why we pass SRTP packets directly, bu
mikescarlett
2016/02/26 00:54:14
I am removing this behavior. I only did that becau
|
| +// |
| +// - |quic_| passes outgoing packets to WritePacket(), which are transferred |
| +// to |channel_| to be sent across the network. |
| +// |
| +// - Data which comes into QuicTransportChannel::OnReadPacket is checked to |
| +// see if it is QUIC, and if it is, passed to |quic_|. SRTP packets are |
| +// signaled upwards as bypass packets. |
| +// |
| +// - When the QUIC handshake is completed, quic_state() returns |
| +// QUIC_TRANSPORT_CONNECTED and SRTP keying material can be exported. |
| +// |
| +// TODO(mikescarlett): Implement secure QUIC handshake, 0-RTT handshakes, and |
| +// QUIC data streams. |
| +class QuicTransportChannel : public TransportChannelImpl, |
| + public net::QuicPacketWriter, |
| + public net::QuicCryptoClientStream::ProofHandler { |
| + public: |
| + // |channel| - the TransportChannel we are wrapping. |
| + explicit QuicTransportChannel(TransportChannelImpl* channel); |
|
pthatcher1
2016/02/24 23:31:13
Why does this need a TransportChannelImpl instead
mikescarlett
2016/02/26 00:54:14
QuicTransportChannel uses |channel| to implement I
pthatcher1
2016/02/27 01:14:19
OK, that seems fine.
|
| + ~QuicTransportChannel() override; |
| + |
| + // TransportChannel overrides. |
| + // TODO(mikescarlett): Certificate authentication is not implemented yet. |
| + bool SetLocalCertificate( |
| + const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
| + rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; |
| + // TODO(mikescarlett): Fingerprint authentication is not implemented yet. |
| + bool SetRemoteFingerprint(const std::string& digest_alg, |
| + const uint8_t* digest, |
| + size_t digest_len) override; |
| + // Returns true if local certificate is set and this peer supports QUIC. |
| + // TODO(mikescarlett): Method name is DTLS-specific. |
|
pthatcher1
2016/02/24 23:31:13
Can you change the TODOs to be action to take, suc
mikescarlett
2016/02/26 00:54:14
Done.
|
| + bool IsDtlsActive() const override { return quic_active_; } |
|
pthatcher1
2016/02/25 08:24:25
Can we just make this return true? Unlike DTLS, I
mikescarlett
2016/02/26 00:54:14
Done.
|
| + // Sends a non-QUIC packet. Packets are sent only if the QUIC handshake has |
| + // not started, or an RTP packet is sent with the PF_SRTP_BYPASS after the |
| + // QUIC handshake is established. |
| + int SendPacket(const char* data, |
| + size_t size, |
| + const rtc::PacketOptions& options, |
| + int flags) override; |
| + |
| + // TransportChannel calls that we forward to the wrapped transport. |
| + void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } |
| + IceRole GetIceRole() const override { return channel_->GetIceRole(); } |
| + int SetOption(rtc::Socket::Option opt, int value) override { |
| + return channel_->SetOption(opt, value); |
| + } |
| + bool GetOption(rtc::Socket::Option opt, int* value) override { |
| + return channel_->GetOption(opt, value); |
| + } |
| + int GetError() override { return channel_->GetError(); } |
| + bool GetStats(ConnectionInfos* infos) override { |
| + return channel_->GetStats(infos); |
| + } |
| + const std::string SessionId() const override { return channel_->SessionId(); } |
| + TransportChannelState GetState() const override { |
| + return channel_->GetState(); |
| + } |
| + void SetIceTiebreaker(uint64_t tiebreaker) override { |
| + channel_->SetIceTiebreaker(tiebreaker); |
| + } |
| + void SetIceCredentials(const std::string& ice_ufrag, |
| + const std::string& ice_pwd) override { |
| + channel_->SetIceCredentials(ice_ufrag, ice_pwd); |
| + } |
| + void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| + const std::string& ice_pwd) override { |
| + channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd); |
| + } |
| + void SetRemoteIceMode(IceMode mode) override { |
| + channel_->SetRemoteIceMode(mode); |
| + } |
| + void MaybeStartGathering() override { channel_->MaybeStartGathering(); } |
| + IceGatheringState gathering_state() const override { |
| + return channel_->gathering_state(); |
| + } |
| + |
| + void AddRemoteCandidate(const Candidate& candidate) override { |
| + channel_->AddRemoteCandidate(candidate); |
| + } |
| + |
| + void SetIceConfig(const IceConfig& config) override { |
| + channel_->SetIceConfig(config); |
| + } |
| + |
| + // Sets up the ciphers to use for SRTP. |
| + // TODO(mikescarlett): SRTP ciphers are currently ignored by QuicSession. |
| + bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
| + return true; |
| + } |
| + // Determines which SRTP cipher was negotiated. |
| + // TODO(mikescarlett): Cipher negotiation is not implemented by QUIC. This |
| + // always uses SRTP_AES128_CM_SHA1_80. |
| + bool GetSrtpCryptoSuite(int* cipher) override; |
|
pthatcher1
2016/02/24 23:31:13
In voice-only calls, we tend to use SRTP_AES128_CM
mikescarlett
2016/02/26 00:54:14
Agreed. A hacky solution (which isn't exactly nego
|
| + bool SetSslRole(rtc::SSLRole role) override; |
| + bool GetSslRole(rtc::SSLRole* role) const override; |
| + // Determines which SSL cipher was negotiated. |
| + // TODO(mikescarlett): Cipher negotiation is not implemented by QUIC. |
| + bool GetSslCipherSuite(int* cipher) override { return false; } |
| + // Once QUIC has been established, retrieves the certificate in |
| + // use by the remote peer, for use in external identity verification. |
|
pthatcher1
2016/02/24 23:31:13
This is really just used for stats, so we should f
mikescarlett
2016/02/26 00:54:14
Ok. In that case I'll ignore this method and move
|
| + // TODO(mikescarlett): Not implemented unless QUIC connections are secure. |
| + bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
| + return false; |
| + } |
| + // Once QUIC is established (i.e., |quic_state_| is QUIC_TRANSPORT_CONNECTED), |
| + // this extracts the keys negotiated during the QUIC handshake, for use |
| + // in external encryption such as for extracting SRTP keys. |
| + bool ExportKeyingMaterial(const std::string& label, |
| + const uint8_t* context, |
| + size_t context_len, |
| + bool use_context, |
| + uint8_t* result, |
| + size_t result_len) override; |
| + void Connect() override; |
| + |
| + // QuicPacketWriter overrides. |
| + // Called from net::QuicConnection when |quic_| has packets to write. |
| + net::WriteResult WritePacket(const char* buffer, |
| + size_t buf_len, |
| + const net::IPAddressNumber& self_address, |
| + const net::IPEndPoint& peer_address) override; |
| + // Whether QuicTransportChannel buffers data when unable to write. If this is |
| + // set to false, then net::QuicConnection buffers unsent packets. |
| + bool IsWriteBlockedDataBuffered() const override { return false; } |
| + // Whether QuicTransportChannel is write blocked. If this returns true, |
| + // outgoing QUIC packets are queued until OnCanWrite() is called. |
| + bool IsWriteBlocked() const override; |
| + // Maximum size of the QUIC packet which can be written. |
| + net::QuicByteCount GetMaxPacketSize( |
| + const net::IPEndPoint& peer_address) const override { |
| + return net::kMaxPacketSize; |
| + } |
| + |
| + // QuicCryptoClientStream::ProofHandler overrides. |
| + // Called by client crypto handshake when cached proof is marked valid. |
| + void OnProofValid( |
| + const net::QuicCryptoClientConfig::CachedState& cached) override; |
| + // Called by client crypto handshake when proof verification details become |
| + // available, either because proof verification is complete, or when cached |
| + // details are used. |
| + void OnProofVerifyDetailsAvailable( |
| + const net::ProofVerifyDetails& verify_details) override; |
| + |
| + // Returns true if |quic_| has queued data which wasn't written due |
| + // to |channel_| being write blocked. |
| + bool HasDataToWrite() const; |
| + // Writes queued data for |quic_| when |channel_| is no longer write blocked. |
| + void OnCanWrite(); |
| + // Connectivity state of QuicTransportChannel. |
| + QuicTransportState quic_state() const { return quic_state_; } |
| + |
| + private: |
| + // QuicPacketWriter override. |
| + // This method is not used -- call set_writable(bool writable) instead. |
| + void SetWritable() override {} |
| + |
| + // Callbacks for |channel_|. |
| + void OnReadableState(TransportChannel* channel); |
| + void OnWritableState(TransportChannel* channel); |
| + void OnReadPacket(TransportChannel* channel, |
| + const char* data, |
| + size_t size, |
| + const rtc::PacketTime& packet_time, |
| + int flags); |
| + void OnSentPacket(TransportChannel* channel, |
| + const rtc::SentPacket& sent_packet); |
| + void OnReadyToSend(TransportChannel* channel); |
| + void OnReceivingState(TransportChannel* channel); |
| + void OnGatheringState(TransportChannelImpl* channel); |
| + void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); |
| + void OnRoleConflict(TransportChannelImpl* channel); |
| + void OnRouteChange(TransportChannel* channel, const Candidate& candidate); |
| + void OnConnectionRemoved(TransportChannelImpl* channel); |
| + |
| + // Callbacks for |quic_|. |
| + // Called when |quic_| has established crypto handshake. |
| + void OnHandshakeComplete(); |
| + // Called when |quic_| has connection closed. |
| + void OnConnectionClosed(net::QuicErrorCode error, bool from_peer); |
| + |
| + // Called by OnReadPacket() when QUIC packet is received. |
| + bool HandleQuicPacket(const char* data, size_t size); |
| + // Sets up the QUIC handshake. |
| + bool MaybeStartQuic(); |
| + // Creates the QUIC connection and |quic_|. |
| + bool CreateQuicSession(); |
| + // Creates the crypto stream and initializes handshake. |
| + bool StartQuicHandshake(); |
| + // Sets QuicTransportChannel connectivity state. |
| + void set_quic_state(QuicTransportState state); |
| + |
| + // Everything should occur on this thread. |
| + rtc::Thread* worker_thread_; |
| + // Underlying channel which is responsible for connecting with the remote peer |
| + // and sending/receiving packets across the network. |
| + TransportChannelImpl* const channel_; |
| + // Connectivity state of QuicTransportChannel. |
| + QuicTransportState quic_state_ = QUIC_TRANSPORT_NEW; |
| + // QUIC session which establishes the crypto handshake and converts data |
| + // to/from QUIC packets. |
| + rtc::scoped_ptr<QuicSession> quic_; |
| + // Config for |quic_|. |
| + net::QuicConfig config_; |
| + // Helper for net::QuicConnection that provides timing and |
| + // random number generation. |
| + QuicConnectionHelper helper_; |
| + // This peer's role in the QUIC crypto handshake. SSL_CLIENT implies this peer |
| + // initiates the handshake, while SSL_SERVER implies the remote peer initiates |
| + // the handshake. |
| + rtc::SSLRole ssl_role_; |
| + // Config for QUIC crypto client stream, used when |ssl_role_| is SSL_CLIENT. |
| + rtc::scoped_ptr<net::QuicCryptoClientConfig> quic_crypto_client_config_; |
| + // Config for QUIC crypto server stream, used when |ssl_role_| is SSL_SERVER. |
| + rtc::scoped_ptr<net::QuicCryptoServerConfig> quic_crypto_server_config_; |
| + // Remote fingerprint parameters that are established before we start QUIC. |
| + std::string remote_fingerprint_value_; |
| + std::string remote_fingerprint_algorithm_; |
| + // This peer's certificate. |
| + rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; |
| + // True if local certificate is set and this peer supports QUIC. |
| + bool quic_active_ = false; |
|
pthatcher1
2016/02/25 08:24:25
Instead of having a separate bool, I'd suggest usi
mikescarlett
2016/02/26 00:54:14
Done.
|
| + // True if remote fingerprint is set. |
| + bool set_remote_fingerprint_ = false; |
|
pthatcher1
2016/02/25 08:24:25
I'd suggest making a RemoteFingerprint struct that
mikescarlett
2016/02/26 00:54:14
Done.
|
| + // True if SetSslRole() has been called. |
| + bool set_ssl_role_ = false; |
|
pthatcher1
2016/02/25 08:24:25
Same here: Optional<rtc::SSLRole> instead of two s
mikescarlett
2016/02/26 00:54:14
Done.
|
| + |
| + RTC_DISALLOW_COPY_AND_ASSIGN(QuicTransportChannel); |
| +}; |
| + |
| +} // namespace cricket |
| + |
| +#endif // WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |