| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ | 11 #ifndef WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ |
| 12 #define WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ | 12 #define WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 18 #include "webrtc/base/buffer.h" | 19 #include "webrtc/base/buffer.h" |
| 19 #include "webrtc/base/bufferqueue.h" | 20 #include "webrtc/base/bufferqueue.h" |
| 20 #include "webrtc/base/constructormagic.h" | 21 #include "webrtc/base/constructormagic.h" |
| 21 #include "webrtc/base/sslstreamadapter.h" | 22 #include "webrtc/base/sslstreamadapter.h" |
| 22 #include "webrtc/base/stream.h" | 23 #include "webrtc/base/stream.h" |
| 23 #include "webrtc/p2p/base/icetransportinternal.h" | |
| 24 #include "webrtc/p2p/base/transportchannelimpl.h" | |
| 25 | 24 |
| 26 namespace rtc { | 25 namespace rtc { |
| 27 class PacketTransportInterface; | 26 class PacketTransportInterface; |
| 28 } | 27 } |
| 29 | 28 |
| 30 namespace cricket { | 29 namespace cricket { |
| 31 | 30 |
| 32 // A bridge between a packet-oriented/transport-type interface on | 31 // A bridge between a packet-oriented/channel-type interface on |
| 33 // the bottom and a StreamInterface on the top. | 32 // the bottom and a StreamInterface on the top. |
| 34 class StreamInterfaceChannel : public rtc::StreamInterface { | 33 class StreamInterfaceChannel : public rtc::StreamInterface { |
| 35 public: | 34 public: |
| 36 explicit StreamInterfaceChannel(IceTransportInternal* channel); | 35 explicit StreamInterfaceChannel(TransportChannel* channel); |
| 37 | 36 |
| 38 // Push in a packet; this gets pulled out from Read(). | 37 // Push in a packet; this gets pulled out from Read(). |
| 39 bool OnPacketReceived(const char* data, size_t size); | 38 bool OnPacketReceived(const char* data, size_t size); |
| 40 | 39 |
| 41 // Implementations of StreamInterface | 40 // Implementations of StreamInterface |
| 42 rtc::StreamState GetState() const override { return state_; } | 41 rtc::StreamState GetState() const override { return state_; } |
| 43 void Close() override; | 42 void Close() override; |
| 44 rtc::StreamResult Read(void* buffer, | 43 rtc::StreamResult Read(void* buffer, |
| 45 size_t buffer_len, | 44 size_t buffer_len, |
| 46 size_t* read, | 45 size_t* read, |
| 47 int* error) override; | 46 int* error) override; |
| 48 rtc::StreamResult Write(const void* data, | 47 rtc::StreamResult Write(const void* data, |
| 49 size_t data_len, | 48 size_t data_len, |
| 50 size_t* written, | 49 size_t* written, |
| 51 int* error) override; | 50 int* error) override; |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 IceTransportInternal* channel_; // owned by DtlsTransportChannelWrapper | 53 TransportChannel* channel_; // owned by DtlsTransportChannelWrapper |
| 55 rtc::StreamState state_; | 54 rtc::StreamState state_; |
| 56 rtc::BufferQueue packets_; | 55 rtc::BufferQueue packets_; |
| 57 | 56 |
| 58 RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel); | 57 RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel); |
| 59 }; | 58 }; |
| 60 | 59 |
| 61 | 60 |
| 62 // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style | 61 // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style |
| 63 // packet-based interface, wrapping an existing TransportChannel instance | 62 // packet-based interface, wrapping an existing TransportChannel instance |
| 64 // (e.g a P2PTransportChannel) | 63 // (e.g a P2PTransportChannel) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 82 // - Data written to DtlsTransportChannelWrapper is passed either to | 81 // - Data written to DtlsTransportChannelWrapper is passed either to |
| 83 // downward_ or directly to channel_, depending on whether DTLS is | 82 // downward_ or directly to channel_, depending on whether DTLS is |
| 84 // negotiated and whether the flags include PF_SRTP_BYPASS | 83 // negotiated and whether the flags include PF_SRTP_BYPASS |
| 85 // | 84 // |
| 86 // - The SSLStreamAdapter writes to downward_->Write() | 85 // - The SSLStreamAdapter writes to downward_->Write() |
| 87 // which translates it into packet writes on channel_. | 86 // which translates it into packet writes on channel_. |
| 88 class DtlsTransportChannelWrapper : public TransportChannelImpl { | 87 class DtlsTransportChannelWrapper : public TransportChannelImpl { |
| 89 public: | 88 public: |
| 90 // The parameters here are: | 89 // The parameters here are: |
| 91 // channel -- the TransportChannel we are wrapping | 90 // channel -- the TransportChannel we are wrapping |
| 92 explicit DtlsTransportChannelWrapper(IceTransportInternal* channel); | 91 explicit DtlsTransportChannelWrapper(TransportChannelImpl* channel); |
| 93 ~DtlsTransportChannelWrapper() override; | 92 ~DtlsTransportChannelWrapper() override; |
| 94 | 93 |
| 95 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } | 94 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } |
| 96 IceRole GetIceRole() const override { return channel_->GetIceRole(); } | 95 IceRole GetIceRole() const override { return channel_->GetIceRole(); } |
| 97 bool SetLocalCertificate( | 96 bool SetLocalCertificate( |
| 98 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; | 97 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
| 99 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; | 98 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; |
| 100 | 99 |
| 101 bool SetRemoteFingerprint(const std::string& digest_alg, | 100 bool SetRemoteFingerprint(const std::string& digest_alg, |
| 102 const uint8_t* digest, | 101 const uint8_t* digest, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 uint8_t* result, | 154 uint8_t* result, |
| 156 size_t result_len) override { | 155 size_t result_len) override { |
| 157 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, | 156 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, |
| 158 context_len, | 157 context_len, |
| 159 use_context, | 158 use_context, |
| 160 result, result_len) | 159 result, result_len) |
| 161 : false; | 160 : false; |
| 162 } | 161 } |
| 163 | 162 |
| 164 // TransportChannelImpl calls. | 163 // TransportChannelImpl calls. |
| 165 IceTransportState GetState() const override { return channel_->GetState(); } | 164 TransportChannelState GetState() const override { |
| 165 return channel_->GetState(); |
| 166 } |
| 166 void SetIceTiebreaker(uint64_t tiebreaker) override { | 167 void SetIceTiebreaker(uint64_t tiebreaker) override { |
| 167 channel_->SetIceTiebreaker(tiebreaker); | 168 channel_->SetIceTiebreaker(tiebreaker); |
| 168 } | 169 } |
| 169 void SetIceParameters(const IceParameters& ice_params) override { | 170 void SetIceParameters(const IceParameters& ice_params) override { |
| 170 channel_->SetIceParameters(ice_params); | 171 channel_->SetIceParameters(ice_params); |
| 171 } | 172 } |
| 172 void SetRemoteIceParameters(const IceParameters& ice_params) override { | 173 void SetRemoteIceParameters(const IceParameters& ice_params) override { |
| 173 channel_->SetRemoteIceParameters(ice_params); | 174 channel_->SetRemoteIceParameters(ice_params); |
| 174 } | 175 } |
| 175 void SetRemoteIceMode(IceMode mode) override { | 176 void SetRemoteIceMode(IceMode mode) override { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 191 | 192 |
| 192 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override { | 193 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override { |
| 193 channel_->SetMetricsObserver(observer); | 194 channel_->SetMetricsObserver(observer); |
| 194 } | 195 } |
| 195 | 196 |
| 196 void SetIceConfig(const IceConfig& config) override { | 197 void SetIceConfig(const IceConfig& config) override { |
| 197 channel_->SetIceConfig(config); | 198 channel_->SetIceConfig(config); |
| 198 } | 199 } |
| 199 | 200 |
| 200 // Needed by DtlsTransport. | 201 // Needed by DtlsTransport. |
| 201 IceTransportInternal* channel() { return channel_; } | 202 TransportChannelImpl* channel() { return channel_; } |
| 202 | 203 |
| 203 // For informational purposes. Tells if the DTLS handshake has finished. | 204 // For informational purposes. Tells if the DTLS handshake has finished. |
| 204 // This may be true even if writable() is false, if the remote fingerprint | 205 // This may be true even if writable() is false, if the remote fingerprint |
| 205 // has not yet been verified. | 206 // has not yet been verified. |
| 206 bool IsDtlsConnected(); | 207 bool IsDtlsConnected(); |
| 207 | 208 |
| 208 private: | 209 private: |
| 209 void OnWritableState(rtc::PacketTransportInterface* transport); | 210 void OnWritableState(rtc::PacketTransportInterface* transport); |
| 210 void OnReadPacket(rtc::PacketTransportInterface* transport, | 211 void OnReadPacket(rtc::PacketTransportInterface* transport, |
| 211 const char* data, | 212 const char* data, |
| 212 size_t size, | 213 size_t size, |
| 213 const rtc::PacketTime& packet_time, | 214 const rtc::PacketTime& packet_time, |
| 214 int flags); | 215 int flags); |
| 215 void OnSentPacket(rtc::PacketTransportInterface* transport, | 216 void OnSentPacket(rtc::PacketTransportInterface* transport, |
| 216 const rtc::SentPacket& sent_packet); | 217 const rtc::SentPacket& sent_packet); |
| 217 void OnReadyToSend(rtc::PacketTransportInterface* transport); | 218 void OnReadyToSend(rtc::PacketTransportInterface* transport); |
| 218 void OnReceivingState(rtc::PacketTransportInterface* transport); | 219 void OnReceivingState(rtc::PacketTransportInterface* transport); |
| 219 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); | 220 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); |
| 220 bool SetupDtls(); | 221 bool SetupDtls(); |
| 221 void MaybeStartDtls(); | 222 void MaybeStartDtls(); |
| 222 bool HandleDtlsPacket(const char* data, size_t size); | 223 bool HandleDtlsPacket(const char* data, size_t size); |
| 223 void OnGatheringState(IceTransportInternal* channel); | 224 void OnGatheringState(TransportChannelImpl* channel); |
| 224 void OnCandidateGathered(IceTransportInternal* channel, const Candidate& c); | 225 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); |
| 225 void OnCandidatesRemoved(IceTransportInternal* channel, | 226 void OnCandidatesRemoved(TransportChannelImpl* channel, |
| 226 const Candidates& candidates); | 227 const Candidates& candidates); |
| 227 void OnRoleConflict(IceTransportInternal* channel); | 228 void OnRoleConflict(TransportChannelImpl* channel); |
| 228 void OnRouteChange(IceTransportInternal* channel, const Candidate& candidate); | 229 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); |
| 229 void OnSelectedCandidatePairChanged( | 230 void OnSelectedCandidatePairChanged( |
| 230 IceTransportInternal* channel, | 231 TransportChannel* channel, |
| 231 CandidatePairInterface* selected_candidate_pair, | 232 CandidatePairInterface* selected_candidate_pair, |
| 232 int last_sent_packet_id, | 233 int last_sent_packet_id, |
| 233 bool ready_to_send); | 234 bool ready_to_send); |
| 234 void OnChannelStateChanged(IceTransportInternal* channel); | 235 void OnChannelStateChanged(TransportChannelImpl* channel); |
| 235 void OnDtlsHandshakeError(rtc::SSLHandshakeError error); | 236 void OnDtlsHandshakeError(rtc::SSLHandshakeError error); |
| 236 | 237 |
| 237 rtc::Thread* network_thread_; // Everything should occur on this thread. | 238 rtc::Thread* network_thread_; // Everything should occur on this thread. |
| 238 // Underlying channel, not owned by this class. | 239 // Underlying channel, not owned by this class. |
| 239 IceTransportInternal* const channel_; | 240 TransportChannelImpl* const channel_; |
| 240 std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream | 241 std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream |
| 241 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. | 242 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. |
| 242 std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS. | 243 std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS. |
| 243 bool dtls_active_ = false; | 244 bool dtls_active_ = false; |
| 244 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; | 245 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; |
| 245 rtc::SSLRole ssl_role_; | 246 rtc::SSLRole ssl_role_; |
| 246 rtc::SSLProtocolVersion ssl_max_version_; | 247 rtc::SSLProtocolVersion ssl_max_version_; |
| 247 rtc::Buffer remote_fingerprint_value_; | 248 rtc::Buffer remote_fingerprint_value_; |
| 248 std::string remote_fingerprint_algorithm_; | 249 std::string remote_fingerprint_algorithm_; |
| 249 | 250 |
| 250 // Cached DTLS ClientHello packet that was received before we started the | 251 // Cached DTLS ClientHello packet that was received before we started the |
| 251 // DTLS handshake. This could happen if the hello was received before the | 252 // DTLS handshake. This could happen if the hello was received before the |
| 252 // transport channel became writable, or before a remote fingerprint was | 253 // transport channel became writable, or before a remote fingerprint was |
| 253 // received. | 254 // received. |
| 254 rtc::Buffer cached_client_hello_; | 255 rtc::Buffer cached_client_hello_; |
| 255 | 256 |
| 256 RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); | 257 RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); |
| 257 }; | 258 }; |
| 258 | 259 |
| 259 } // namespace cricket | 260 } // namespace cricket |
| 260 | 261 |
| 261 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ | 262 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ |
| OLD | NEW |