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" | |
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 #include "webrtc/p2p/base/icetransportinternal.h" | |
24 | 25 |
25 namespace rtc { | 26 namespace rtc { |
26 class PacketTransportInterface; | 27 class PacketTransportInterface; |
27 } | 28 } |
28 | 29 |
29 namespace cricket { | 30 namespace cricket { |
30 | 31 |
31 // A bridge between a packet-oriented/channel-type interface on | 32 // A bridge between a packet-oriented/transport-type interface on |
32 // the bottom and a StreamInterface on the top. | 33 // the bottom and a StreamInterface on the top. |
33 class StreamInterfaceChannel : public rtc::StreamInterface { | 34 class StreamInterfaceChannel : public rtc::StreamInterface { |
34 public: | 35 public: |
35 explicit StreamInterfaceChannel(TransportChannel* channel); | 36 explicit StreamInterfaceChannel(IceTransportInternal* channel); |
36 | 37 |
37 // Push in a packet; this gets pulled out from Read(). | 38 // Push in a packet; this gets pulled out from Read(). |
38 bool OnPacketReceived(const char* data, size_t size); | 39 bool OnPacketReceived(const char* data, size_t size); |
39 | 40 |
40 // Implementations of StreamInterface | 41 // Implementations of StreamInterface |
41 rtc::StreamState GetState() const override { return state_; } | 42 rtc::StreamState GetState() const override { return state_; } |
42 void Close() override; | 43 void Close() override; |
43 rtc::StreamResult Read(void* buffer, | 44 rtc::StreamResult Read(void* buffer, |
44 size_t buffer_len, | 45 size_t buffer_len, |
45 size_t* read, | 46 size_t* read, |
46 int* error) override; | 47 int* error) override; |
47 rtc::StreamResult Write(const void* data, | 48 rtc::StreamResult Write(const void* data, |
48 size_t data_len, | 49 size_t data_len, |
49 size_t* written, | 50 size_t* written, |
50 int* error) override; | 51 int* error) override; |
51 | 52 |
52 private: | 53 private: |
53 TransportChannel* channel_; // owned by DtlsTransportChannelWrapper | 54 IceTransportInternal* channel_; // owned by DtlsTransportChannelWrapper |
pthatcher1
2017/01/13 22:41:16
We should rename the variable ice_transport_, or m
Zhi Huang
2017/01/16 10:38:24
Done.
| |
54 rtc::StreamState state_; | 55 rtc::StreamState state_; |
55 rtc::BufferQueue packets_; | 56 rtc::BufferQueue packets_; |
56 | 57 |
57 RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel); | 58 RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel); |
58 }; | 59 }; |
59 | 60 |
60 | 61 |
61 // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style | 62 // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style |
62 // packet-based interface, wrapping an existing TransportChannel instance | 63 // packet-based interface, wrapping an existing TransportChannel instance |
63 // (e.g a P2PTransportChannel) | 64 // (e.g a P2PTransportChannel) |
64 // Here's the way this works: | 65 // Here's the way this works: |
65 // | 66 // |
66 // DtlsTransportChannelWrapper { | 67 // DtlsTransportChannelWrapper { |
67 // SSLStreamAdapter* dtls_ { | 68 // SSLStreamAdapter* dtls_ { |
68 // StreamInterfaceChannel downward_ { | 69 // StreamInterfaceChannel downward_ { |
69 // TransportChannelImpl* channel_; | 70 // IceTransportInternal* channel_; |
70 // } | 71 // } |
71 // } | 72 // } |
72 // } | 73 // } |
73 // | 74 // |
74 // - Data which comes into DtlsTransportChannelWrapper from the underlying | 75 // - Data which comes into DtlsTransportChannelWrapper from the underlying |
75 // channel_ via OnReadPacket() is checked for whether it is DTLS | 76 // channel_ via OnReadPacket() is checked for whether it is DTLS |
76 // or not, and if it is, is passed to DtlsTransportChannelWrapper:: | 77 // or not, and if it is, is passed to DtlsTransportChannelWrapper:: |
77 // HandleDtlsPacket, which pushes it into to downward_. | 78 // HandleDtlsPacket, which pushes it into to downward_. |
78 // dtls_ is listening for events on downward_, so it immediately calls | 79 // dtls_ is listening for events on downward_, so it immediately calls |
79 // downward_->Read(). | 80 // downward_->Read(). |
80 // | 81 // |
81 // - Data written to DtlsTransportChannelWrapper is passed either to | 82 // - Data written to DtlsTransportChannelWrapper is passed either to |
82 // downward_ or directly to channel_, depending on whether DTLS is | 83 // downward_ or directly to channel_, depending on whether DTLS is |
83 // negotiated and whether the flags include PF_SRTP_BYPASS | 84 // negotiated and whether the flags include PF_SRTP_BYPASS |
84 // | 85 // |
85 // - The SSLStreamAdapter writes to downward_->Write() | 86 // - The SSLStreamAdapter writes to downward_->Write() |
86 // which translates it into packet writes on channel_. | 87 // which translates it into packet writes on channel_. |
87 class DtlsTransportChannelWrapper : public TransportChannelImpl { | 88 class DtlsTransportChannelWrapper : public DtlsTransportInternal { |
88 public: | 89 public: |
89 // The parameters here are: | 90 // The parameters here are: |
90 // channel -- the TransportChannel we are wrapping | 91 // channel -- the TransportChannel we are wrapping |
91 explicit DtlsTransportChannelWrapper(TransportChannelImpl* channel); | 92 explicit DtlsTransportChannelWrapper(IceTransportInternal* channel); |
92 ~DtlsTransportChannelWrapper() override; | 93 ~DtlsTransportChannelWrapper() override; |
93 | 94 |
94 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } | 95 DtlsTransportState dtls_state() const override { return dtls_state_; } |
95 IceRole GetIceRole() const override { return channel_->GetIceRole(); } | 96 |
97 const std::string& transport_name() const override { return transport_name_; } | |
98 | |
99 int component() const override { return component_; } | |
100 | |
101 // Returns false if no local certificate was set, or if the peer doesn't | |
102 // support DTLS. | |
103 bool IsDtlsActive() const override { return dtls_active_; } | |
104 | |
96 bool SetLocalCertificate( | 105 bool SetLocalCertificate( |
97 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; | 106 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
98 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; | 107 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; |
99 | 108 |
100 bool SetRemoteFingerprint(const std::string& digest_alg, | 109 bool SetRemoteFingerprint(const std::string& digest_alg, |
101 const uint8_t* digest, | 110 const uint8_t* digest, |
102 size_t digest_len) override; | 111 size_t digest_len) override; |
103 | 112 |
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 | 113 |
108 // Called to send a packet (via DTLS, if turned on). | 114 // Called to send a packet (via DTLS, if turned on). |
109 int SendPacket(const char* data, | 115 int SendPacket(const char* data, |
110 size_t size, | 116 size_t size, |
111 const rtc::PacketOptions& options, | 117 const rtc::PacketOptions& options, |
112 int flags) override; | 118 int flags) override; |
113 | 119 |
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 { | 120 bool GetOption(rtc::Socket::Option opt, int* value) override { |
119 return channel_->GetOption(opt, value); | 121 return channel_->GetOption(opt, value); |
120 } | 122 } |
121 int GetError() override { return channel_->GetError(); } | |
122 bool GetStats(ConnectionInfos* infos) override { | |
123 return channel_->GetStats(infos); | |
124 } | |
125 | 123 |
126 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); | 124 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); |
127 | 125 |
128 // Set up the ciphers to use for DTLS-SRTP. If this method is not called | 126 // 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. | 127 // before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated. |
130 // This method should be called before SetupDtls. | 128 // This method should be called before SetupDtls. |
131 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override; | 129 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override; |
132 | 130 |
133 // Find out which DTLS-SRTP cipher was negotiated | 131 // Find out which DTLS-SRTP cipher was negotiated |
134 bool GetSrtpCryptoSuite(int* cipher) override; | 132 bool GetSrtpCryptoSuite(int* cipher) override; |
(...skipping 18 matching lines...) Expand all Loading... | |
153 bool use_context, | 151 bool use_context, |
154 uint8_t* result, | 152 uint8_t* result, |
155 size_t result_len) override { | 153 size_t result_len) override { |
156 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, | 154 return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context, |
157 context_len, | 155 context_len, |
158 use_context, | 156 use_context, |
159 result, result_len) | 157 result, result_len) |
160 : false; | 158 : false; |
161 } | 159 } |
162 | 160 |
163 // TransportChannelImpl calls. | 161 IceTransportInternal* ice_transport() override { return channel_; } |
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 | 162 |
204 // For informational purposes. Tells if the DTLS handshake has finished. | 163 // For informational purposes. Tells if the DTLS handshake has finished. |
205 // This may be true even if writable() is false, if the remote fingerprint | 164 // This may be true even if writable() is false, if the remote fingerprint |
206 // has not yet been verified. | 165 // has not yet been verified. |
207 bool IsDtlsConnected(); | 166 bool IsDtlsConnected(); |
208 | 167 |
168 bool receiving() const override { return receiving_; } | |
169 | |
170 bool writable() const override { return writable_; } | |
171 | |
172 int GetError() override { return channel_->GetError(); } | |
173 | |
174 int SetOption(rtc::Socket::Option opt, int value) override { | |
175 return channel_->SetOption(opt, value); | |
176 } | |
177 | |
178 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { | |
179 std::vector<int> crypto_suites; | |
180 for (const auto cipher : ciphers) { | |
181 crypto_suites.push_back(rtc::SrtpCryptoSuiteFromName(cipher)); | |
182 } | |
183 return SetSrtpCryptoSuites(crypto_suites); | |
184 } | |
185 | |
186 std::string ToString() const { | |
187 const char RECEIVING_ABBREV[2] = {'_', 'R'}; | |
188 const char WRITABLE_ABBREV[2] = {'_', 'W'}; | |
189 std::stringstream ss; | |
190 ss << "DtlsTransport[" << transport_name_ << "|" << component_ << "|" | |
191 << RECEIVING_ABBREV[receiving()] << WRITABLE_ABBREV[writable()] << "]"; | |
192 return ss.str(); | |
193 } | |
194 | |
209 private: | 195 private: |
210 void OnWritableState(rtc::PacketTransportInterface* transport); | 196 void OnWritableState(rtc::PacketTransportInterface* transport); |
211 void OnReadPacket(rtc::PacketTransportInterface* transport, | 197 void OnReadPacket(rtc::PacketTransportInterface* transport, |
212 const char* data, | 198 const char* data, |
213 size_t size, | 199 size_t size, |
214 const rtc::PacketTime& packet_time, | 200 const rtc::PacketTime& packet_time, |
215 int flags); | 201 int flags); |
216 void OnSentPacket(rtc::PacketTransportInterface* transport, | 202 void OnSentPacket(rtc::PacketTransportInterface* transport, |
217 const rtc::SentPacket& sent_packet); | 203 const rtc::SentPacket& sent_packet); |
218 void OnReadyToSend(rtc::PacketTransportInterface* transport); | 204 void OnReadyToSend(rtc::PacketTransportInterface* transport); |
219 void OnReceivingState(rtc::PacketTransportInterface* transport); | 205 void OnReceivingState(rtc::PacketTransportInterface* transport); |
220 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); | 206 void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err); |
221 bool SetupDtls(); | 207 bool SetupDtls(); |
222 void MaybeStartDtls(); | 208 void MaybeStartDtls(); |
223 bool HandleDtlsPacket(const char* data, size_t size); | 209 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); | 210 void OnDtlsHandshakeError(rtc::SSLHandshakeError error); |
237 | 211 |
212 void set_receiving(bool receiving); | |
213 void set_writable(bool writable); | |
214 // Sets the DTLS state, signaling if necessary. | |
215 void set_dtls_state(DtlsTransportState state); | |
216 | |
217 std::string transport_name_; | |
218 int component_; | |
219 DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW; | |
238 rtc::Thread* network_thread_; // Everything should occur on this thread. | 220 rtc::Thread* network_thread_; // Everything should occur on this thread. |
239 // Underlying channel, not owned by this class. | 221 // Underlying channel, not owned by this class. |
240 TransportChannelImpl* const channel_; | 222 IceTransportInternal* const channel_; |
241 std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream | 223 std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream |
242 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. | 224 StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_. |
243 std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS. | 225 std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS. |
244 bool dtls_active_ = false; | 226 bool dtls_active_ = false; |
245 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; | 227 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; |
246 rtc::SSLRole ssl_role_; | 228 rtc::SSLRole ssl_role_; |
247 rtc::SSLProtocolVersion ssl_max_version_; | 229 rtc::SSLProtocolVersion ssl_max_version_; |
248 rtc::Buffer remote_fingerprint_value_; | 230 rtc::Buffer remote_fingerprint_value_; |
249 std::string remote_fingerprint_algorithm_; | 231 std::string remote_fingerprint_algorithm_; |
250 | 232 |
251 // Cached DTLS ClientHello packet that was received before we started the | 233 // Cached DTLS ClientHello packet that was received before we started the |
252 // DTLS handshake. This could happen if the hello was received before the | 234 // DTLS handshake. This could happen if the hello was received before the |
253 // transport channel became writable, or before a remote fingerprint was | 235 // transport channel became writable, or before a remote fingerprint was |
254 // received. | 236 // received. |
255 rtc::Buffer cached_client_hello_; | 237 rtc::Buffer cached_client_hello_; |
256 | 238 |
239 bool receiving_ = false; | |
240 bool writable_ = false; | |
241 | |
257 RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); | 242 RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper); |
258 }; | 243 }; |
259 | 244 |
260 } // namespace cricket | 245 } // namespace cricket |
261 | 246 |
262 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ | 247 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORTCHANNEL_H_ |
OLD | NEW |