| 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 <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "webrtc/p2p/base/transportchannelimpl.h" | 17 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 18 #include "webrtc/base/buffer.h" | 18 #include "webrtc/base/buffer.h" |
| 19 #include "webrtc/base/bufferqueue.h" | 19 #include "webrtc/base/bufferqueue.h" |
| 20 #include "webrtc/base/scoped_ptr.h" | 20 #include "webrtc/base/scoped_ptr.h" |
| 21 #include "webrtc/base/sslstreamadapter.h" | 21 #include "webrtc/base/sslstreamadapter.h" |
| 22 #include "webrtc/base/stream.h" | 22 #include "webrtc/base/stream.h" |
| 23 | 23 |
| 24 namespace cricket { | 24 namespace cricket { |
| 25 | 25 |
| 26 // A bridge between a packet-oriented/channel-type interface on | 26 // A bridge between a packet-oriented/channel-type interface on |
| 27 // the bottom and a StreamInterface on the top. | 27 // the bottom and a StreamInterface on the top. |
| 28 class StreamInterfaceChannel : public rtc::StreamInterface { | 28 class StreamInterfaceChannel : public rtc::StreamInterface { |
| 29 public: | 29 public: |
| 30 StreamInterfaceChannel(TransportChannel* channel); | 30 explicit StreamInterfaceChannel(TransportChannel* channel); |
| 31 | 31 |
| 32 // Push in a packet; this gets pulled out from Read(). | 32 // Push in a packet; this gets pulled out from Read(). |
| 33 bool OnPacketReceived(const char* data, size_t size); | 33 bool OnPacketReceived(const char* data, size_t size); |
| 34 | 34 |
| 35 // Implementations of StreamInterface | 35 // Implementations of StreamInterface |
| 36 rtc::StreamState GetState() const override { return state_; } | 36 rtc::StreamState GetState() const override { return state_; } |
| 37 void Close() override { state_ = rtc::SS_CLOSED; } | 37 void Close() override { state_ = rtc::SS_CLOSED; } |
| 38 rtc::StreamResult Read(void* buffer, size_t buffer_len, | 38 rtc::StreamResult Read(void* buffer, |
| 39 size_t* read, int* error) override; | 39 size_t buffer_len, |
| 40 rtc::StreamResult Write(const void* data, size_t data_len, | 40 size_t* read, |
| 41 size_t* written, int* error) override; | 41 int* error) override; |
| 42 rtc::StreamResult Write(const void* data, |
| 43 size_t data_len, |
| 44 size_t* written, |
| 45 int* error) override; |
| 42 | 46 |
| 43 private: | 47 private: |
| 44 TransportChannel* channel_; // owned by DtlsTransportChannelWrapper | 48 TransportChannel* channel_; // owned by DtlsTransportChannelWrapper |
| 45 rtc::StreamState state_; | 49 rtc::StreamState state_; |
| 46 rtc::BufferQueue packets_; | 50 rtc::BufferQueue packets_; |
| 47 | 51 |
| 48 DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel); | 52 DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel); |
| 49 }; | 53 }; |
| 50 | 54 |
| 51 | 55 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 STATE_CLOSED // Connection closed. | 90 STATE_CLOSED // Connection closed. |
| 87 }; | 91 }; |
| 88 | 92 |
| 89 // The parameters here are: | 93 // The parameters here are: |
| 90 // transport -- the DtlsTransport that created us | 94 // transport -- the DtlsTransport that created us |
| 91 // channel -- the TransportChannel we are wrapping | 95 // channel -- the TransportChannel we are wrapping |
| 92 DtlsTransportChannelWrapper(Transport* transport, | 96 DtlsTransportChannelWrapper(Transport* transport, |
| 93 TransportChannelImpl* channel); | 97 TransportChannelImpl* channel); |
| 94 ~DtlsTransportChannelWrapper() override; | 98 ~DtlsTransportChannelWrapper() override; |
| 95 | 99 |
| 96 void SetIceRole(IceRole role) override { | 100 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } |
| 97 channel_->SetIceRole(role); | 101 IceRole GetIceRole() const override { return channel_->GetIceRole(); } |
| 98 } | |
| 99 IceRole GetIceRole() const override { | |
| 100 return channel_->GetIceRole(); | |
| 101 } | |
| 102 bool SetLocalCertificate( | 102 bool SetLocalCertificate( |
| 103 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; | 103 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
| 104 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; | 104 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; |
| 105 | 105 |
| 106 bool SetRemoteFingerprint(const std::string& digest_alg, | 106 bool SetRemoteFingerprint(const std::string& digest_alg, |
| 107 const uint8* digest, | 107 const uint8* digest, |
| 108 size_t digest_len) override; | 108 size_t digest_len) override; |
| 109 bool IsDtlsActive() const override { return dtls_state_ != STATE_NONE; } | 109 bool IsDtlsActive() const override { return dtls_state_ != STATE_NONE; } |
| 110 | 110 |
| 111 // Called to send a packet (via DTLS, if turned on). | 111 // Called to send a packet (via DTLS, if turned on). |
| 112 int SendPacket(const char* data, size_t size, | 112 int SendPacket(const char* data, |
| 113 size_t size, |
| 113 const rtc::PacketOptions& options, | 114 const rtc::PacketOptions& options, |
| 114 int flags) override; | 115 int flags) override; |
| 115 | 116 |
| 116 // TransportChannel calls that we forward to the wrapped transport. | 117 // TransportChannel calls that we forward to the wrapped transport. |
| 117 int SetOption(rtc::Socket::Option opt, int value) override { | 118 int SetOption(rtc::Socket::Option opt, int value) override { |
| 118 return channel_->SetOption(opt, value); | 119 return channel_->SetOption(opt, value); |
| 119 } | 120 } |
| 120 bool GetOption(rtc::Socket::Option opt, int* value) override { | 121 bool GetOption(rtc::Socket::Option opt, int* value) override { |
| 121 return channel_->GetOption(opt, value); | 122 return channel_->GetOption(opt, value); |
| 122 } | 123 } |
| 123 int GetError() override { | 124 int GetError() override { return channel_->GetError(); } |
| 124 return channel_->GetError(); | |
| 125 } | |
| 126 bool GetStats(ConnectionInfos* infos) override { | 125 bool GetStats(ConnectionInfos* infos) override { |
| 127 return channel_->GetStats(infos); | 126 return channel_->GetStats(infos); |
| 128 } | 127 } |
| 129 const std::string SessionId() const override { | 128 const std::string SessionId() const override { return channel_->SessionId(); } |
| 130 return channel_->SessionId(); | |
| 131 } | |
| 132 | 129 |
| 133 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); | 130 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); |
| 134 | 131 |
| 135 // Set up the ciphers to use for DTLS-SRTP. If this method is not called | 132 // Set up the ciphers to use for DTLS-SRTP. If this method is not called |
| 136 // before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated. | 133 // before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated. |
| 137 // This method should be called before SetupDtls. | 134 // This method should be called before SetupDtls. |
| 138 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override; | 135 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override; |
| 139 | 136 |
| 140 // Find out which DTLS-SRTP cipher was negotiated | 137 // Find out which DTLS-SRTP cipher was negotiated |
| 141 bool GetSrtpCipher(std::string* cipher) override; | 138 bool GetSrtpCipher(std::string* cipher) override; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 161 uint8* result, | 158 uint8* result, |
| 162 size_t result_len) override { | 159 size_t result_len) override { |
| 163 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, | 160 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, |
| 164 context_len, | 161 context_len, |
| 165 use_context, | 162 use_context, |
| 166 result, result_len) | 163 result, result_len) |
| 167 : false; | 164 : false; |
| 168 } | 165 } |
| 169 | 166 |
| 170 // TransportChannelImpl calls. | 167 // TransportChannelImpl calls. |
| 171 Transport* GetTransport() override { | 168 Transport* GetTransport() override { return transport_; } |
| 172 return transport_; | |
| 173 } | |
| 174 | 169 |
| 175 TransportChannelState GetState() const override { | 170 TransportChannelState GetState() const override { |
| 176 return channel_->GetState(); | 171 return channel_->GetState(); |
| 177 } | 172 } |
| 178 void SetIceTiebreaker(uint64 tiebreaker) override { | 173 void SetIceTiebreaker(uint64 tiebreaker) override { |
| 179 channel_->SetIceTiebreaker(tiebreaker); | 174 channel_->SetIceTiebreaker(tiebreaker); |
| 180 } | 175 } |
| 181 void SetIceCredentials(const std::string& ice_ufrag, | 176 void SetIceCredentials(const std::string& ice_ufrag, |
| 182 const std::string& ice_pwd) override { | 177 const std::string& ice_pwd) override { |
| 183 channel_->SetIceCredentials(ice_ufrag, ice_pwd); | 178 channel_->SetIceCredentials(ice_ufrag, ice_pwd); |
| 184 } | 179 } |
| 185 void SetRemoteIceCredentials(const std::string& ice_ufrag, | 180 void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 186 const std::string& ice_pwd) override { | 181 const std::string& ice_pwd) override { |
| 187 channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd); | 182 channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd); |
| 188 } | 183 } |
| 189 void SetRemoteIceMode(IceMode mode) override { | 184 void SetRemoteIceMode(IceMode mode) override { |
| 190 channel_->SetRemoteIceMode(mode); | 185 channel_->SetRemoteIceMode(mode); |
| 191 } | 186 } |
| 192 | 187 |
| 193 void Connect() override; | 188 void Connect() override; |
| 194 | 189 |
| 195 void OnSignalingReady() override { | 190 IceGatheringState gathering_state() const override { |
| 196 channel_->OnSignalingReady(); | 191 return channel_->gathering_state(); |
| 197 } | 192 } |
| 198 void OnCandidate(const Candidate& candidate) override { | 193 |
| 199 channel_->OnCandidate(candidate); | 194 void AddRemoteCandidate(const Candidate& candidate) override { |
| 195 channel_->AddRemoteCandidate(candidate); |
| 200 } | 196 } |
| 201 | 197 |
| 202 void SetReceivingTimeout(int receiving_timeout_ms) override { | 198 void SetReceivingTimeout(int receiving_timeout_ms) override { |
| 203 channel_->SetReceivingTimeout(receiving_timeout_ms); | 199 channel_->SetReceivingTimeout(receiving_timeout_ms); |
| 204 } | 200 } |
| 205 | 201 |
| 206 // Needed by DtlsTransport. | 202 // Needed by DtlsTransport. |
| 207 TransportChannelImpl* channel() { return channel_; } | 203 TransportChannelImpl* channel() { return channel_; } |
| 208 | 204 |
| 209 private: | 205 private: |
| 210 void OnReadableState(TransportChannel* channel); | 206 void OnReadableState(TransportChannel* channel); |
| 211 void OnWritableState(TransportChannel* channel); | 207 void OnWritableState(TransportChannel* channel); |
| 212 void OnReadPacket(TransportChannel* channel, const char* data, size_t size, | 208 void OnReadPacket(TransportChannel* channel, const char* data, size_t size, |
| 213 const rtc::PacketTime& packet_time, int flags); | 209 const rtc::PacketTime& packet_time, int flags); |
| 214 void OnReadyToSend(TransportChannel* channel); | 210 void OnReadyToSend(TransportChannel* channel); |
| 215 void OnReceivingState(TransportChannel* channel); | 211 void OnReceivingState(TransportChannel* channel); |
| 216 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); | 212 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); |
| 217 bool SetupDtls(); | 213 bool SetupDtls(); |
| 218 bool MaybeStartDtls(); | 214 bool MaybeStartDtls(); |
| 219 bool HandleDtlsPacket(const char* data, size_t size); | 215 bool HandleDtlsPacket(const char* data, size_t size); |
| 220 void OnRequestSignaling(TransportChannelImpl* channel); | 216 void OnGatheringState(TransportChannelImpl* channel); |
| 221 void OnCandidateReady(TransportChannelImpl* channel, const Candidate& c); | 217 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); |
| 222 void OnCandidatesAllocationDone(TransportChannelImpl* channel); | |
| 223 void OnRoleConflict(TransportChannelImpl* channel); | 218 void OnRoleConflict(TransportChannelImpl* channel); |
| 224 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); | 219 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); |
| 225 void OnConnectionRemoved(TransportChannelImpl* channel); | 220 void OnConnectionRemoved(TransportChannelImpl* channel); |
| 226 | 221 |
| 227 Transport* transport_; // The transport_ that created us. | 222 Transport* transport_; // The transport_ that created us. |
| 228 rtc::Thread* worker_thread_; // Everything should occur on this thread. | 223 rtc::Thread* worker_thread_; // Everything should occur on this thread. |
| 229 TransportChannelImpl* channel_; // Underlying channel, owned by transport_. | 224 TransportChannelImpl* channel_; // Underlying channel, owned by transport_. |
| 230 rtc::scoped_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream | 225 rtc::scoped_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream |
| 231 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. | 226 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. |
| 232 std::vector<std::string> srtp_ciphers_; // SRTP ciphers to use with DTLS. | 227 std::vector<std::string> srtp_ciphers_; // SRTP ciphers to use with DTLS. |
| 233 State dtls_state_; | 228 State dtls_state_; |
| 234 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; | 229 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; |
| 235 rtc::SSLRole ssl_role_; | 230 rtc::SSLRole ssl_role_; |
| 236 rtc::SSLProtocolVersion ssl_max_version_; | 231 rtc::SSLProtocolVersion ssl_max_version_; |
| 237 rtc::Buffer remote_fingerprint_value_; | 232 rtc::Buffer remote_fingerprint_value_; |
| 238 std::string remote_fingerprint_algorithm_; | 233 std::string remote_fingerprint_algorithm_; |
| 239 | 234 |
| 240 DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); | 235 DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); |
| 241 }; | 236 }; |
| 242 | 237 |
| 243 } // namespace cricket | 238 } // namespace cricket |
| 244 | 239 |
| 245 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ | 240 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ |
| OLD | NEW |