Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 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 P2P_BASE_NO_OP_DTLS_TRANSPORT_H_ |
| 12 | #define P2P_BASE_NO_OP_DTLS_TRANSPORT_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "api/crypto/crypto_options.h" |
| 19 | #include "p2p/base/dtls_transport_internal.h" |
| 20 | #include "p2p/base/ice_transport_internal.h" |
| 21 | #include "p2p/base/packet_transport_internal.h" |
| 22 | #include "rtc_base/buffer.h" |
| 23 | #include "rtc_base/buffer_queue.h" |
| 24 | #include "rtc_base/constructor_magic.h" |
| 25 | #include "rtc_base/ssl_stream_adapter.h" |
| 26 | #include "rtc_base/stream.h" |
| 27 | #include "rtc_base/strings/string_builder.h" |
| 28 | #include "rtc_base/thread_checker.h" |
| 29 | |
| 30 | namespace cricket { |
| 31 | |
| 32 | constexpr int kNoOpDtlsTransportComponent = -1; |
| 33 | |
| 34 | // This implementation wraps a cricket::DtlsTransport, and takes |
| 35 | // ownership of it. |
| 36 | // The implementation does not perform any operations, except of being |
| 37 | // "connected". The purpose of this implementation is to disable RTP transport |
| 38 | // while MediaTransport is used. |
| 39 | // |
| 40 | // This implementation is only temporary. Long-term we will refactor and disable |
| 41 | // RTP transport entirely when MediaTransport is used. Always connected (after |
| 42 | // ICE), no-op, dtls transport. This is used when DTLS is disabled. |
| 43 | // |
| 44 | // MaybeCreateJsepTransport controller expects DTLS connection to send a |
| 45 | // 'connected' signal _after_ it is created (if it is created in a connected |
| 46 | // state, that would not be noticed by jsep transport controller). Therefore, |
| 47 | // the no-op dtls transport will wait for ICE event "writable", and then |
| 48 | // immediately report that it's connected (emulating 0-rtt connection). |
| 49 | // |
| 50 | // We could simply not set a dtls to active (not set a certificate on the DTLS), |
| 51 | // and it would use an underyling connection instead. |
| 52 | // However, when MediaTransport is used, we want to entirely disable |
| 53 | // dtls/srtp/rtp, in order to avoid multiplexing issues, such as "Failed to |
| 54 | // unprotect RTCP packet". |
| 55 | class NoOpDtlsTransport : public DtlsTransportInternal { |
| 56 | public: |
| 57 | NoOpDtlsTransport(std::unique_ptr<IceTransportInternal> ice_transport, |
| 58 | const webrtc::CryptoOptions& crypto_options); |
| 59 | |
| 60 | ~NoOpDtlsTransport() override; |
| 61 | const webrtc::CryptoOptions& crypto_options() const override; |
| 62 | DtlsTransportState dtls_state() const override; |
| 63 | int component() const override; |
| 64 | bool IsDtlsActive() const override; |
| 65 | bool GetDtlsRole(rtc::SSLRole* role) const override; |
| 66 | bool SetDtlsRole(rtc::SSLRole role) override; |
| 67 | bool GetSrtpCryptoSuite(int* cipher) override; |
| 68 | bool GetSslCipherSuite(int* cipher) override; |
| 69 | rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; |
| 70 | bool SetLocalCertificate( |
| 71 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
| 72 | std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain() const override; |
| 73 | bool ExportKeyingMaterial(const std::string& label, |
| 74 | const uint8_t* context, |
| 75 | size_t context_len, |
| 76 | bool use_context, |
| 77 | uint8_t* result, |
| 78 | size_t result_len) override; |
| 79 | bool SetRemoteFingerprint(const std::string& digest_alg, |
| 80 | const uint8_t* digest, |
| 81 | size_t digest_len) override; |
| 82 | bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override; |
| 83 | IceTransportInternal* ice_transport() override; |
| 84 | |
| 85 | const std::string& transport_name() const override; |
| 86 | bool writable() const override; |
| 87 | bool receiving() const override; |
| 88 | |
| 89 | private: |
| 90 | void OnReadyToSend(rtc::PacketTransportInternal* transport); |
| 91 | void OnWritableState(rtc::PacketTransportInternal* transport); |
| 92 | |
| 93 | int SendPacket(const char* data, |
| 94 | size_t len, |
| 95 | const rtc::PacketOptions& options, |
| 96 | int flags) override; |
| 97 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 98 | int GetError() override; |
| 99 | |
| 100 | rtc::ThreadChecker thread_checker_; |
| 101 | |
| 102 | webrtc::CryptoOptions crypto_options_; |
| 103 | std::unique_ptr<IceTransportInternal> ice_transport_; |
| 104 | bool is_writable_ = false; |
| 105 | }; |
| 106 | |
| 107 | } // namespace cricket |
| 108 | |
| 109 | #endif // P2P_BASE_NO_OP_DTLS_TRANSPORT_H_ |