Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: webrtc/p2p/base/dtlstransportchannel.h

Issue 2606123002: Remove the dependency of TransportChannel and TransportChannelImpl. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
19 #include "webrtc/base/buffer.h" 18 #include "webrtc/base/buffer.h"
20 #include "webrtc/base/bufferqueue.h" 19 #include "webrtc/base/bufferqueue.h"
21 #include "webrtc/base/constructormagic.h" 20 #include "webrtc/base/constructormagic.h"
22 #include "webrtc/base/sslstreamadapter.h" 21 #include "webrtc/base/sslstreamadapter.h"
23 #include "webrtc/base/stream.h" 22 #include "webrtc/base/stream.h"
23 #include "webrtc/p2p/base/dtlstransportinternal.h"
24 24
25 namespace rtc { 25 namespace rtc {
26 class PacketTransportInterface; 26 class PacketTransportInterface;
27 } 27 }
28 28
29 namespace cricket { 29 namespace cricket {
30 30
31 // A bridge between a packet-oriented/channel-type interface on 31 // A bridge between a packet-oriented/channel-type interface on
32 // the bottom and a StreamInterface on the top. 32 // the bottom and a StreamInterface on the top.
33 class StreamInterfaceChannel : public rtc::StreamInterface { 33 class StreamInterfaceChannel : public rtc::StreamInterface {
(...skipping 25 matching lines...) Expand all
59 59
60 60
61 // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style 61 // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style
62 // packet-based interface, wrapping an existing TransportChannel instance 62 // packet-based interface, wrapping an existing TransportChannel instance
63 // (e.g a P2PTransportChannel) 63 // (e.g a P2PTransportChannel)
64 // Here's the way this works: 64 // Here's the way this works:
65 // 65 //
66 // DtlsTransportChannelWrapper { 66 // DtlsTransportChannelWrapper {
67 // SSLStreamAdapter* dtls_ { 67 // SSLStreamAdapter* dtls_ {
68 // StreamInterfaceChannel downward_ { 68 // StreamInterfaceChannel downward_ {
69 // TransportChannelImpl* channel_; 69 // IceTransportInternal* channel_;
70 // } 70 // }
71 // } 71 // }
72 // } 72 // }
73 // 73 //
74 // - Data which comes into DtlsTransportChannelWrapper from the underlying 74 // - Data which comes into DtlsTransportChannelWrapper from the underlying
75 // channel_ via OnReadPacket() is checked for whether it is DTLS 75 // channel_ via OnReadPacket() is checked for whether it is DTLS
76 // or not, and if it is, is passed to DtlsTransportChannelWrapper:: 76 // or not, and if it is, is passed to DtlsTransportChannelWrapper::
77 // HandleDtlsPacket, which pushes it into to downward_. 77 // HandleDtlsPacket, which pushes it into to downward_.
78 // dtls_ is listening for events on downward_, so it immediately calls 78 // dtls_ is listening for events on downward_, so it immediately calls
79 // downward_->Read(). 79 // downward_->Read().
80 // 80 //
81 // - Data written to DtlsTransportChannelWrapper is passed either to 81 // - Data written to DtlsTransportChannelWrapper is passed either to
82 // downward_ or directly to channel_, depending on whether DTLS is 82 // downward_ or directly to channel_, depending on whether DTLS is
83 // negotiated and whether the flags include PF_SRTP_BYPASS 83 // negotiated and whether the flags include PF_SRTP_BYPASS
84 // 84 //
85 // - The SSLStreamAdapter writes to downward_->Write() 85 // - The SSLStreamAdapter writes to downward_->Write()
86 // which translates it into packet writes on channel_. 86 // which translates it into packet writes on channel_.
87 class DtlsTransportChannelWrapper : public TransportChannelImpl { 87 class DtlsTransportChannelWrapper : public DtlsTransportInternal {
88 public: 88 public:
89 // The parameters here are: 89 // The parameters here are:
90 // channel -- the TransportChannel we are wrapping 90 // channel -- the TransportChannel we are wrapping
91 explicit DtlsTransportChannelWrapper(TransportChannelImpl* channel); 91 explicit DtlsTransportChannelWrapper(TransportChannelImpl* channel);
92 ~DtlsTransportChannelWrapper() override; 92 ~DtlsTransportChannelWrapper() override;
93 93
94 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } 94 DtlsTransportState dtls_state() const override { return dtls_state_; }
95 IceRole GetIceRole() const override { return channel_->GetIceRole(); } 95
96 const std::string& transport_name() const override { return transport_name_; }
97
98 int component() const override { return component_; }
99
100 // Returns false if no local certificate was set, or if the peer doesn't
101 // support DTLS.
102 bool IsDtlsActive() const override { return dtls_active_; }
103
96 bool SetLocalCertificate( 104 bool SetLocalCertificate(
97 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; 105 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
98 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; 106 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override;
99 107
100 bool SetRemoteFingerprint(const std::string& digest_alg, 108 bool SetRemoteFingerprint(const std::string& digest_alg,
101 const uint8_t* digest, 109 const uint8_t* digest,
102 size_t digest_len) override; 110 size_t digest_len) override;
103 111
104 // Returns false if no local certificate was set, or if the peer doesn't
105 // support DTLS.
106 bool IsDtlsActive() const override { return dtls_active_; }
107 112
108 // Called to send a packet (via DTLS, if turned on). 113 // Called to send a packet (via DTLS, if turned on).
109 int SendPacket(const char* data, 114 int SendPacket(const char* data,
110 size_t size, 115 size_t size,
111 const rtc::PacketOptions& options, 116 const rtc::PacketOptions& options,
112 int flags) override; 117 int flags) override;
113 118
114 // TransportChannel calls that we forward to the wrapped transport.
115 int SetOption(rtc::Socket::Option opt, int value) override {
116 return channel_->SetOption(opt, value);
117 }
118 bool GetOption(rtc::Socket::Option opt, int* value) override { 119 bool GetOption(rtc::Socket::Option opt, int* value) override {
119 return channel_->GetOption(opt, value); 120 return channel_->GetOption(opt, value);
120 } 121 }
121 int GetError() override { return channel_->GetError(); }
122 bool GetStats(ConnectionInfos* infos) override {
123 return channel_->GetStats(infos);
124 }
125 122
126 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); 123 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
127 124
128 // Set up the ciphers to use for DTLS-SRTP. If this method is not called 125 // Set up the ciphers to use for DTLS-SRTP. If this method is not called
129 // before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated. 126 // before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated.
130 // This method should be called before SetupDtls. 127 // This method should be called before SetupDtls.
131 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override; 128 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override;
132 129
133 // Find out which DTLS-SRTP cipher was negotiated 130 // Find out which DTLS-SRTP cipher was negotiated
134 bool GetSrtpCryptoSuite(int* cipher) override; 131 bool GetSrtpCryptoSuite(int* cipher) override;
(...skipping 18 matching lines...) Expand all
153 bool use_context, 150 bool use_context,
154 uint8_t* result, 151 uint8_t* result,
155 size_t result_len) override { 152 size_t result_len) override {
156 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, 153 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context,
157 context_len, 154 context_len,
158 use_context, 155 use_context,
159 result, result_len) 156 result, result_len)
160 : false; 157 : false;
161 } 158 }
162 159
163 // TransportChannelImpl calls. 160 TransportChannelImpl* ice_transport() override { return channel_; }
Taylor Brandstetter 2017/01/03 23:21:59 Can this be IceTransportInternal instead now (assu
Zhi Huang 2017/01/10 18:30:51 Yes, I'll merge the IceTransportInternal change in
164 TransportChannelState GetState() const override {
165 return channel_->GetState();
166 }
167 void SetIceTiebreaker(uint64_t tiebreaker) override {
168 channel_->SetIceTiebreaker(tiebreaker);
169 }
170 void SetIceParameters(const IceParameters& ice_params) override {
171 channel_->SetIceParameters(ice_params);
172 }
173 void SetRemoteIceParameters(const IceParameters& ice_params) override {
174 channel_->SetRemoteIceParameters(ice_params);
175 }
176 void SetRemoteIceMode(IceMode mode) override {
177 channel_->SetRemoteIceMode(mode);
178 }
179
180 void MaybeStartGathering() override { channel_->MaybeStartGathering(); }
181
182 IceGatheringState gathering_state() const override {
183 return channel_->gathering_state();
184 }
185
186 void AddRemoteCandidate(const Candidate& candidate) override {
187 channel_->AddRemoteCandidate(candidate);
188 }
189 void RemoveRemoteCandidate(const Candidate& candidate) override {
190 channel_->RemoveRemoteCandidate(candidate);
191 }
192
193 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override {
194 channel_->SetMetricsObserver(observer);
195 }
196
197 void SetIceConfig(const IceConfig& config) override {
198 channel_->SetIceConfig(config);
199 }
200
201 // Needed by DtlsTransport.
202 TransportChannelImpl* channel() { return channel_; }
203 161
204 // For informational purposes. Tells if the DTLS handshake has finished. 162 // For informational purposes. Tells if the DTLS handshake has finished.
205 // This may be true even if writable() is false, if the remote fingerprint 163 // This may be true even if writable() is false, if the remote fingerprint
206 // has not yet been verified. 164 // has not yet been verified.
207 bool IsDtlsConnected(); 165 bool IsDtlsConnected();
208 166
167 bool receiving() const override { return receiving_; }
168
169 bool writable() const override { return writable_; }
170
171 int GetError() override { return channel_->GetError(); }
Taylor Brandstetter 2017/01/03 23:21:59 Since this and SetOption just pass through to the
Zhi Huang 2017/01/10 18:30:51 These functions are from the PacketTransportInterf
172
173 int SetOption(rtc::Socket::Option opt, int value) override {
174 return channel_->SetOption(opt, value);
175 }
176
177 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override {
178 std::vector<int> crypto_suites;
179 for (const auto cipher : ciphers) {
180 crypto_suites.push_back(rtc::SrtpCryptoSuiteFromName(cipher));
181 }
182 return SetSrtpCryptoSuites(crypto_suites);
183 }
184
185 std::string ToString() const {
186 const char RECEIVING_ABBREV[2] = {'_', 'R'};
187 const char WRITABLE_ABBREV[2] = {'_', 'W'};
188 std::stringstream ss;
189 ss << "DtlsTransport[" << transport_name_ << "|" << component_ << "|"
190 << RECEIVING_ABBREV[receiving()] << WRITABLE_ABBREV[writable()] << "]";
191 return ss.str();
192 }
193
209 private: 194 private:
210 void OnWritableState(rtc::PacketTransportInterface* transport); 195 void OnWritableState(rtc::PacketTransportInterface* transport);
211 void OnReadPacket(rtc::PacketTransportInterface* transport, 196 void OnReadPacket(rtc::PacketTransportInterface* transport,
212 const char* data, 197 const char* data,
213 size_t size, 198 size_t size,
214 const rtc::PacketTime& packet_time, 199 const rtc::PacketTime& packet_time,
215 int flags); 200 int flags);
216 void OnSentPacket(rtc::PacketTransportInterface* transport, 201 void OnSentPacket(rtc::PacketTransportInterface* transport,
217 const rtc::SentPacket& sent_packet); 202 const rtc::SentPacket& sent_packet);
218 void OnReadyToSend(rtc::PacketTransportInterface* transport); 203 void OnReadyToSend(rtc::PacketTransportInterface* transport);
219 void OnReceivingState(rtc::PacketTransportInterface* transport); 204 void OnReceivingState(rtc::PacketTransportInterface* transport);
220 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); 205 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err);
221 bool SetupDtls(); 206 bool SetupDtls();
222 void MaybeStartDtls(); 207 void MaybeStartDtls();
223 bool HandleDtlsPacket(const char* data, size_t size); 208 bool HandleDtlsPacket(const char* data, size_t size);
224 void OnGatheringState(TransportChannelImpl* channel);
225 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c);
226 void OnCandidatesRemoved(TransportChannelImpl* channel,
227 const Candidates& candidates);
228 void OnRoleConflict(TransportChannelImpl* channel);
229 void OnRouteChange(TransportChannel* channel, const Candidate& candidate);
230 void OnSelectedCandidatePairChanged(
231 TransportChannel* channel,
232 CandidatePairInterface* selected_candidate_pair,
233 int last_sent_packet_id,
234 bool ready_to_send);
235 void OnChannelStateChanged(TransportChannelImpl* channel);
236 void OnDtlsHandshakeError(rtc::SSLHandshakeError error); 209 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
210 void set_receiving(bool receiving);
211 void set_writable(bool writable);
212 // Sets the DTLS state, signaling if necessary.
213 void set_dtls_state(DtlsTransportState state);
237 214
215 std::string transport_name_;
216 int component_;
217 DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW;
238 rtc::Thread* network_thread_; // Everything should occur on this thread. 218 rtc::Thread* network_thread_; // Everything should occur on this thread.
239 // Underlying channel, not owned by this class. 219 // Underlying channel, not owned by this class.
240 TransportChannelImpl* const channel_; 220 TransportChannelImpl* const channel_;
241 std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream 221 std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream
242 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. 222 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_.
243 std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS. 223 std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS.
244 bool dtls_active_ = false; 224 bool dtls_active_ = false;
245 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; 225 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_;
246 rtc::SSLRole ssl_role_; 226 rtc::SSLRole ssl_role_;
247 rtc::SSLProtocolVersion ssl_max_version_; 227 rtc::SSLProtocolVersion ssl_max_version_;
248 rtc::Buffer remote_fingerprint_value_; 228 rtc::Buffer remote_fingerprint_value_;
249 std::string remote_fingerprint_algorithm_; 229 std::string remote_fingerprint_algorithm_;
250 230
251 // Cached DTLS ClientHello packet that was received before we started the 231 // Cached DTLS ClientHello packet that was received before we started the
252 // DTLS handshake. This could happen if the hello was received before the 232 // DTLS handshake. This could happen if the hello was received before the
253 // transport channel became writable, or before a remote fingerprint was 233 // transport channel became writable, or before a remote fingerprint was
254 // received. 234 // received.
255 rtc::Buffer cached_client_hello_; 235 rtc::Buffer cached_client_hello_;
256 236
237 bool receiving_ = false;
238 bool writable_ = false;
239
257 RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); 240 RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper);
258 }; 241 };
259 242
260 } // namespace cricket 243 } // namespace cricket
261 244
262 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ 245 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698