OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2009 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_FAKESESSION_H_ | 11 #ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
12 #define WEBRTC_P2P_BASE_FAKESESSION_H_ | 12 #define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "webrtc/p2p/base/session.h" | |
19 #include "webrtc/p2p/base/transport.h" | 18 #include "webrtc/p2p/base/transport.h" |
20 #include "webrtc/p2p/base/transportchannel.h" | 19 #include "webrtc/p2p/base/transportchannel.h" |
| 20 #include "webrtc/p2p/base/transportcontroller.h" |
21 #include "webrtc/p2p/base/transportchannelimpl.h" | 21 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 22 #include "webrtc/base/bind.h" |
22 #include "webrtc/base/buffer.h" | 23 #include "webrtc/base/buffer.h" |
23 #include "webrtc/base/fakesslidentity.h" | 24 #include "webrtc/base/fakesslidentity.h" |
24 #include "webrtc/base/messagequeue.h" | 25 #include "webrtc/base/messagequeue.h" |
25 #include "webrtc/base/sigslot.h" | 26 #include "webrtc/base/sigslot.h" |
26 #include "webrtc/base/sslfingerprint.h" | 27 #include "webrtc/base/sslfingerprint.h" |
| 28 #include "webrtc/base/thread.h" |
27 | 29 |
28 namespace cricket { | 30 namespace cricket { |
29 | 31 |
30 class FakeTransport; | 32 class FakeTransport; |
31 | 33 |
32 struct PacketMessageData : public rtc::MessageData { | 34 struct PacketMessageData : public rtc::MessageData { |
33 PacketMessageData(const char* data, size_t len) : packet(data, len) { | 35 PacketMessageData(const char* data, size_t len) : packet(data, len) {} |
34 } | |
35 rtc::Buffer packet; | 36 rtc::Buffer packet; |
36 }; | 37 }; |
37 | 38 |
38 // Fake transport channel class, which can be passed to anything that needs a | 39 // Fake transport channel class, which can be passed to anything that needs a |
39 // transport channel. Can be informed of another FakeTransportChannel via | 40 // transport channel. Can be informed of another FakeTransportChannel via |
40 // SetDestination. | 41 // SetDestination. |
41 // TODO(hbos): Move implementation to .cc file, this and other classes in file. | 42 // TODO(hbos): Move implementation to .cc file, this and other classes in file. |
42 class FakeTransportChannel : public TransportChannelImpl, | 43 class FakeTransportChannel : public TransportChannelImpl, |
43 public rtc::MessageHandler { | 44 public rtc::MessageHandler { |
44 public: | 45 public: |
45 explicit FakeTransportChannel(Transport* transport, | 46 explicit FakeTransportChannel(Transport* transport, |
46 const std::string& content_name, | 47 const std::string& name, |
47 int component) | 48 int component) |
48 : TransportChannelImpl(content_name, component), | 49 : TransportChannelImpl(name, component), |
49 transport_(transport), | 50 transport_(transport), |
50 dest_(nullptr), | 51 dtls_fingerprint_("", nullptr, 0) {} |
51 state_(STATE_INIT), | 52 ~FakeTransportChannel() { Reset(); } |
52 async_(false), | |
53 do_dtls_(false), | |
54 role_(ICEROLE_UNKNOWN), | |
55 tiebreaker_(0), | |
56 remote_ice_mode_(ICEMODE_FULL), | |
57 dtls_fingerprint_("", nullptr, 0), | |
58 ssl_role_(rtc::SSL_CLIENT), | |
59 connection_count_(0) { | |
60 } | |
61 ~FakeTransportChannel() { | |
62 Reset(); | |
63 } | |
64 | 53 |
65 uint64 IceTiebreaker() const { return tiebreaker_; } | 54 uint64 IceTiebreaker() const { return tiebreaker_; } |
66 IceMode remote_ice_mode() const { return remote_ice_mode_; } | 55 IceMode remote_ice_mode() const { return remote_ice_mode_; } |
67 const std::string& ice_ufrag() const { return ice_ufrag_; } | 56 const std::string& ice_ufrag() const { return ice_ufrag_; } |
68 const std::string& ice_pwd() const { return ice_pwd_; } | 57 const std::string& ice_pwd() const { return ice_pwd_; } |
69 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } | 58 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } |
70 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } | 59 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } |
71 const rtc::SSLFingerprint& dtls_fingerprint() const { | 60 const rtc::SSLFingerprint& dtls_fingerprint() const { |
72 return dtls_fingerprint_; | 61 return dtls_fingerprint_; |
73 } | 62 } |
74 | 63 |
75 void SetAsync(bool async) { | 64 // If async, will send packets by "Post"-ing to message queue instead of |
76 async_ = async; | 65 // synchronously "Send"-ing. |
77 } | 66 void SetAsync(bool async) { async_ = async; } |
78 | 67 |
79 Transport* GetTransport() override { | 68 Transport* GetTransport() override { return transport_; } |
80 return transport_; | |
81 } | |
82 | 69 |
83 TransportChannelState GetState() const override { | 70 TransportChannelState GetState() const override { |
84 if (connection_count_ == 0) { | 71 if (connection_count_ == 0) { |
85 return TransportChannelState::STATE_FAILED; | 72 return had_connection_ ? TransportChannelState::STATE_FAILED |
| 73 : TransportChannelState::STATE_INIT; |
86 } | 74 } |
87 | 75 |
88 if (connection_count_ == 1) { | 76 if (connection_count_ == 1) { |
89 return TransportChannelState::STATE_COMPLETED; | 77 return TransportChannelState::STATE_COMPLETED; |
90 } | 78 } |
91 | 79 |
92 return TransportChannelState::STATE_FAILED; | 80 return TransportChannelState::STATE_CONNECTING; |
93 } | 81 } |
94 | 82 |
95 void SetIceRole(IceRole role) override { role_ = role; } | 83 void SetIceRole(IceRole role) override { role_ = role; } |
96 IceRole GetIceRole() const override { return role_; } | 84 IceRole GetIceRole() const override { return role_; } |
97 void SetIceTiebreaker(uint64 tiebreaker) override { | 85 void SetIceTiebreaker(uint64 tiebreaker) override { |
98 tiebreaker_ = tiebreaker; | 86 tiebreaker_ = tiebreaker; |
99 } | 87 } |
100 void SetIceCredentials(const std::string& ice_ufrag, | 88 void SetIceCredentials(const std::string& ice_ufrag, |
101 const std::string& ice_pwd) override { | 89 const std::string& ice_pwd) override { |
102 ice_ufrag_ = ice_ufrag; | 90 ice_ufrag_ = ice_ufrag; |
103 ice_pwd_ = ice_pwd; | 91 ice_pwd_ = ice_pwd; |
104 } | 92 } |
105 void SetRemoteIceCredentials(const std::string& ice_ufrag, | 93 void SetRemoteIceCredentials(const std::string& ice_ufrag, |
106 const std::string& ice_pwd) override { | 94 const std::string& ice_pwd) override { |
107 remote_ice_ufrag_ = ice_ufrag; | 95 remote_ice_ufrag_ = ice_ufrag; |
108 remote_ice_pwd_ = ice_pwd; | 96 remote_ice_pwd_ = ice_pwd; |
109 } | 97 } |
110 | 98 |
111 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } | 99 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } |
112 bool SetRemoteFingerprint(const std::string& alg, const uint8* digest, | 100 bool SetRemoteFingerprint(const std::string& alg, |
| 101 const uint8* digest, |
113 size_t digest_len) override { | 102 size_t digest_len) override { |
114 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); | 103 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
115 return true; | 104 return true; |
116 } | 105 } |
117 bool SetSslRole(rtc::SSLRole role) override { | 106 bool SetSslRole(rtc::SSLRole role) override { |
118 ssl_role_ = role; | 107 ssl_role_ = role; |
119 return true; | 108 return true; |
120 } | 109 } |
121 bool GetSslRole(rtc::SSLRole* role) const override { | 110 bool GetSslRole(rtc::SSLRole* role) const override { |
122 *role = ssl_role_; | 111 *role = ssl_role_; |
123 return true; | 112 return true; |
124 } | 113 } |
125 | 114 |
126 void Connect() override { | 115 void Connect() override { |
127 if (state_ == STATE_INIT) { | 116 if (state_ == STATE_INIT) { |
128 state_ = STATE_CONNECTING; | 117 state_ = STATE_CONNECTING; |
| 118 // Connect is expected to start candidate gathering |
| 119 if (gathering_state_ != kIceGatheringGathering) { |
| 120 gathering_state_ = kIceGatheringGathering; |
| 121 SignalGatheringState(this); |
| 122 } |
129 } | 123 } |
130 } | 124 } |
131 virtual void Reset() { | 125 |
| 126 IceGatheringState gathering_state() const override { |
| 127 return gathering_state_; |
| 128 } |
| 129 |
| 130 void Reset() { |
132 if (state_ != STATE_INIT) { | 131 if (state_ != STATE_INIT) { |
133 state_ = STATE_INIT; | 132 state_ = STATE_INIT; |
134 if (dest_) { | 133 if (dest_) { |
135 dest_->state_ = STATE_INIT; | 134 dest_->state_ = STATE_INIT; |
136 dest_->dest_ = NULL; | 135 dest_->dest_ = nullptr; |
137 dest_ = NULL; | 136 dest_ = nullptr; |
138 } | 137 } |
139 } | 138 } |
140 } | 139 } |
141 | 140 |
142 void SetWritable(bool writable) { | 141 void SetWritable(bool writable) { set_writable(writable); } |
143 set_writable(writable); | |
144 } | |
145 | 142 |
146 void SetDestination(FakeTransportChannel* dest) { | 143 void SetDestination(FakeTransportChannel* dest) { |
147 if (state_ == STATE_CONNECTING && dest) { | 144 if (state_ == STATE_CONNECTING && dest) { |
148 // This simulates the delivery of candidates. | 145 // This simulates the delivery of candidates. |
149 dest_ = dest; | 146 dest_ = dest; |
150 dest_->dest_ = this; | 147 dest_->dest_ = this; |
151 if (certificate_ && dest_->certificate_) { | 148 if (local_cert_ && dest_->local_cert_) { |
152 do_dtls_ = true; | 149 do_dtls_ = true; |
153 dest_->do_dtls_ = true; | 150 dest_->do_dtls_ = true; |
154 NegotiateSrtpCiphers(); | 151 NegotiateSrtpCiphers(); |
155 } | 152 } |
156 state_ = STATE_CONNECTED; | 153 state_ = STATE_CONNECTED; |
157 dest_->state_ = STATE_CONNECTED; | 154 dest_->state_ = STATE_CONNECTED; |
158 set_writable(true); | 155 set_writable(true); |
159 dest_->set_writable(true); | 156 dest_->set_writable(true); |
160 } else if (state_ == STATE_CONNECTED && !dest) { | 157 } else if (state_ == STATE_CONNECTED && !dest) { |
161 // Simulates loss of connectivity, by asymmetrically forgetting dest_. | 158 // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
162 dest_ = NULL; | 159 dest_ = nullptr; |
163 state_ = STATE_CONNECTING; | 160 state_ = STATE_CONNECTING; |
164 set_writable(false); | 161 set_writable(false); |
165 } | 162 } |
166 } | 163 } |
167 | 164 |
168 void SetConnectionCount(size_t connection_count) { | 165 void SetConnectionCount(size_t connection_count) { |
169 size_t old_connection_count = connection_count_; | 166 size_t old_connection_count = connection_count_; |
170 connection_count_ = connection_count; | 167 connection_count_ = connection_count; |
| 168 if (connection_count) |
| 169 had_connection_ = true; |
171 if (connection_count_ < old_connection_count) | 170 if (connection_count_ < old_connection_count) |
172 SignalConnectionRemoved(this); | 171 SignalConnectionRemoved(this); |
173 } | 172 } |
174 | 173 |
175 void SetReceiving(bool receiving) { | 174 void SetCandidatesGatheringComplete() { |
176 set_receiving(receiving); | 175 if (gathering_state_ != kIceGatheringComplete) { |
| 176 gathering_state_ = kIceGatheringComplete; |
| 177 SignalGatheringState(this); |
| 178 } |
177 } | 179 } |
178 | 180 |
179 void SetReceivingTimeout(int timeout) override {} | 181 void SetReceiving(bool receiving) { set_receiving(receiving); } |
180 | 182 |
181 int SendPacket(const char* data, size_t len, | 183 void SetReceivingTimeout(int timeout) override { |
182 const rtc::PacketOptions& options, int flags) override { | 184 receiving_timeout_ = timeout; |
| 185 } |
| 186 |
| 187 int receiving_timeout() const { return receiving_timeout_; } |
| 188 |
| 189 int SendPacket(const char* data, |
| 190 size_t len, |
| 191 const rtc::PacketOptions& options, |
| 192 int flags) override { |
183 if (state_ != STATE_CONNECTED) { | 193 if (state_ != STATE_CONNECTED) { |
184 return -1; | 194 return -1; |
185 } | 195 } |
186 | 196 |
187 if (flags != PF_SRTP_BYPASS && flags != 0) { | 197 if (flags != PF_SRTP_BYPASS && flags != 0) { |
188 return -1; | 198 return -1; |
189 } | 199 } |
190 | 200 |
191 PacketMessageData* packet = new PacketMessageData(data, len); | 201 PacketMessageData* packet = new PacketMessageData(data, len); |
192 if (async_) { | 202 if (async_) { |
193 rtc::Thread::Current()->Post(this, 0, packet); | 203 rtc::Thread::Current()->Post(this, 0, packet); |
194 } else { | 204 } else { |
195 rtc::Thread::Current()->Send(this, 0, packet); | 205 rtc::Thread::Current()->Send(this, 0, packet); |
196 } | 206 } |
197 return static_cast<int>(len); | 207 return static_cast<int>(len); |
198 } | 208 } |
199 int SetOption(rtc::Socket::Option opt, int value) override { | 209 int SetOption(rtc::Socket::Option opt, int value) override { return true; } |
200 return true; | 210 bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } |
| 211 int GetError() override { return 0; } |
| 212 |
| 213 void AddRemoteCandidate(const Candidate& candidate) override { |
| 214 remote_candidates_.push_back(candidate); |
201 } | 215 } |
202 bool GetOption(rtc::Socket::Option opt, int* value) override { | 216 const Candidates& remote_candidates() const { return remote_candidates_; } |
203 return true; | |
204 } | |
205 int GetError() override { | |
206 return 0; | |
207 } | |
208 | |
209 void OnSignalingReady() override { | |
210 } | |
211 void OnCandidate(const Candidate& candidate) override { | |
212 } | |
213 | 217 |
214 void OnMessage(rtc::Message* msg) override { | 218 void OnMessage(rtc::Message* msg) override { |
215 PacketMessageData* data = static_cast<PacketMessageData*>( | 219 PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); |
216 msg->pdata); | |
217 dest_->SignalReadPacket(dest_, data->packet.data<char>(), | 220 dest_->SignalReadPacket(dest_, data->packet.data<char>(), |
218 data->packet.size(), rtc::CreatePacketTime(0), 0); | 221 data->packet.size(), rtc::CreatePacketTime(0), 0); |
219 delete data; | 222 delete data; |
220 } | 223 } |
221 | 224 |
222 bool SetLocalCertificate( | 225 bool SetLocalCertificate( |
223 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { | 226 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
224 certificate_ = certificate; | 227 local_cert_ = certificate; |
225 return true; | 228 return true; |
226 } | 229 } |
227 | 230 |
228 void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { | 231 void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { |
229 remote_cert_ = cert; | 232 remote_cert_ = cert; |
230 } | 233 } |
231 | 234 |
232 bool IsDtlsActive() const override { | 235 bool IsDtlsActive() const override { return do_dtls_; } |
233 return do_dtls_; | |
234 } | |
235 | 236 |
236 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { | 237 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { |
237 srtp_ciphers_ = ciphers; | 238 srtp_ciphers_ = ciphers; |
238 return true; | 239 return true; |
239 } | 240 } |
240 | 241 |
241 bool GetSrtpCipher(std::string* cipher) override { | 242 bool GetSrtpCipher(std::string* cipher) override { |
242 if (!chosen_srtp_cipher_.empty()) { | 243 if (!chosen_srtp_cipher_.empty()) { |
243 *cipher = chosen_srtp_cipher_; | 244 *cipher = chosen_srtp_cipher_; |
244 return true; | 245 return true; |
245 } | 246 } |
246 return false; | 247 return false; |
247 } | 248 } |
248 | 249 |
249 bool GetSslCipher(std::string* cipher) override { | 250 bool GetSslCipher(std::string* cipher) override { return false; } |
250 return false; | |
251 } | |
252 | 251 |
253 rtc::scoped_refptr<rtc::RTCCertificate> | 252 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const { |
254 GetLocalCertificate() const override { | 253 return local_cert_; |
255 return certificate_; | |
256 } | 254 } |
257 | 255 |
258 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { | 256 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
259 if (!remote_cert_) | 257 if (!remote_cert_) |
260 return false; | 258 return false; |
261 | 259 |
262 *cert = remote_cert_->GetReference(); | 260 *cert = remote_cert_->GetReference(); |
263 return true; | 261 return true; |
264 } | 262 } |
265 | 263 |
266 bool ExportKeyingMaterial(const std::string& label, | 264 bool ExportKeyingMaterial(const std::string& label, |
267 const uint8* context, | 265 const uint8* context, |
268 size_t context_len, | 266 size_t context_len, |
269 bool use_context, | 267 bool use_context, |
270 uint8* result, | 268 uint8* result, |
271 size_t result_len) override { | 269 size_t result_len) override { |
272 if (!chosen_srtp_cipher_.empty()) { | 270 if (!chosen_srtp_cipher_.empty()) { |
273 memset(result, 0xff, result_len); | 271 memset(result, 0xff, result_len); |
274 return true; | 272 return true; |
275 } | 273 } |
276 | 274 |
277 return false; | 275 return false; |
278 } | 276 } |
279 | 277 |
280 virtual void NegotiateSrtpCiphers() { | 278 void NegotiateSrtpCiphers() { |
281 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); | 279 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); |
282 it1 != srtp_ciphers_.end(); ++it1) { | 280 it1 != srtp_ciphers_.end(); ++it1) { |
283 for (std::vector<std::string>::const_iterator it2 = | 281 for (std::vector<std::string>::const_iterator it2 = |
284 dest_->srtp_ciphers_.begin(); | 282 dest_->srtp_ciphers_.begin(); |
285 it2 != dest_->srtp_ciphers_.end(); ++it2) { | 283 it2 != dest_->srtp_ciphers_.end(); ++it2) { |
286 if (*it1 == *it2) { | 284 if (*it1 == *it2) { |
287 chosen_srtp_cipher_ = *it1; | 285 chosen_srtp_cipher_ = *it1; |
288 dest_->chosen_srtp_cipher_ = *it2; | 286 dest_->chosen_srtp_cipher_ = *it2; |
289 return; | 287 return; |
290 } | 288 } |
291 } | 289 } |
292 } | 290 } |
293 } | 291 } |
294 | 292 |
295 bool GetStats(ConnectionInfos* infos) override { | 293 bool GetStats(ConnectionInfos* infos) override { |
296 ConnectionInfo info; | 294 ConnectionInfo info; |
297 infos->clear(); | 295 infos->clear(); |
298 infos->push_back(info); | 296 infos->push_back(info); |
299 return true; | 297 return true; |
300 } | 298 } |
301 | 299 |
| 300 void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { |
| 301 ssl_max_version_ = version; |
| 302 } |
| 303 rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
| 304 return ssl_max_version_; |
| 305 } |
| 306 |
302 private: | 307 private: |
303 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; | 308 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; |
304 Transport* transport_; | 309 Transport* transport_; |
305 FakeTransportChannel* dest_; | 310 FakeTransportChannel* dest_ = nullptr; |
306 State state_; | 311 State state_ = STATE_INIT; |
307 bool async_; | 312 bool async_ = false; |
308 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 313 Candidates remote_candidates_; |
309 rtc::FakeSSLCertificate* remote_cert_; | 314 rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
310 bool do_dtls_; | 315 rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
| 316 bool do_dtls_ = false; |
311 std::vector<std::string> srtp_ciphers_; | 317 std::vector<std::string> srtp_ciphers_; |
312 std::string chosen_srtp_cipher_; | 318 std::string chosen_srtp_cipher_; |
313 IceRole role_; | 319 int receiving_timeout_ = -1; |
314 uint64 tiebreaker_; | 320 IceRole role_ = ICEROLE_UNKNOWN; |
| 321 uint64 tiebreaker_ = 0; |
315 std::string ice_ufrag_; | 322 std::string ice_ufrag_; |
316 std::string ice_pwd_; | 323 std::string ice_pwd_; |
317 std::string remote_ice_ufrag_; | 324 std::string remote_ice_ufrag_; |
318 std::string remote_ice_pwd_; | 325 std::string remote_ice_pwd_; |
319 IceMode remote_ice_mode_; | 326 IceMode remote_ice_mode_ = ICEMODE_FULL; |
| 327 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; |
320 rtc::SSLFingerprint dtls_fingerprint_; | 328 rtc::SSLFingerprint dtls_fingerprint_; |
321 rtc::SSLRole ssl_role_; | 329 rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT; |
322 size_t connection_count_; | 330 size_t connection_count_ = 0; |
| 331 IceGatheringState gathering_state_ = kIceGatheringNew; |
| 332 bool had_connection_ = false; |
323 }; | 333 }; |
324 | 334 |
325 // Fake transport class, which can be passed to anything that needs a Transport. | 335 // Fake transport class, which can be passed to anything that needs a Transport. |
326 // Can be informed of another FakeTransport via SetDestination (low-tech way | 336 // Can be informed of another FakeTransport via SetDestination (low-tech way |
327 // of doing candidates) | 337 // of doing candidates) |
328 class FakeTransport : public Transport { | 338 class FakeTransport : public Transport { |
329 public: | 339 public: |
330 typedef std::map<int, FakeTransportChannel*> ChannelMap; | 340 typedef std::map<int, FakeTransportChannel*> ChannelMap; |
331 FakeTransport(rtc::Thread* signaling_thread, | 341 |
332 rtc::Thread* worker_thread, | 342 explicit FakeTransport(const std::string& name) : Transport(name, nullptr) {} |
333 const std::string& content_name, | 343 |
334 PortAllocator* alllocator = nullptr) | 344 // Note that we only have a constructor with the allocator parameter so it can |
335 : Transport(signaling_thread, worker_thread, | 345 // be wrapped by a DtlsTransport. |
336 content_name, nullptr), | 346 FakeTransport(const std::string& name, PortAllocator* allocator) |
337 dest_(nullptr), | 347 : Transport(name, nullptr) {} |
338 async_(false) { | 348 |
339 } | 349 ~FakeTransport() { DestroyAllChannels(); } |
340 ~FakeTransport() { | |
341 DestroyAllChannels(); | |
342 } | |
343 | 350 |
344 const ChannelMap& channels() const { return channels_; } | 351 const ChannelMap& channels() const { return channels_; } |
345 | 352 |
| 353 // If async, will send packets by "Post"-ing to message queue instead of |
| 354 // synchronously "Send"-ing. |
346 void SetAsync(bool async) { async_ = async; } | 355 void SetAsync(bool async) { async_ = async; } |
347 void SetDestination(FakeTransport* dest) { | 356 void SetDestination(FakeTransport* dest) { |
348 dest_ = dest; | 357 dest_ = dest; |
349 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); | 358 for (const auto& kv : channels_) { |
350 ++it) { | 359 kv.second->SetLocalCertificate(certificate_); |
351 it->second->SetLocalCertificate(certificate_); | 360 SetChannelDestination(kv.first, kv.second); |
352 SetChannelDestination(it->first, it->second); | |
353 } | 361 } |
354 } | 362 } |
355 | 363 |
356 void SetWritable(bool writable) { | 364 void SetWritable(bool writable) { |
357 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); | 365 for (const auto& kv : channels_) { |
358 ++it) { | 366 kv.second->SetWritable(writable); |
359 it->second->SetWritable(writable); | |
360 } | 367 } |
361 } | 368 } |
362 | 369 |
363 void set_certificate( | 370 void SetLocalCertificate( |
364 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { | 371 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
365 certificate_ = certificate; | 372 certificate_ = certificate; |
366 } | 373 } |
| 374 bool GetLocalCertificate( |
| 375 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { |
| 376 if (!certificate_) |
| 377 return false; |
| 378 |
| 379 *certificate = certificate_; |
| 380 return true; |
| 381 } |
| 382 |
| 383 bool GetSslRole(rtc::SSLRole* role) const override { |
| 384 if (channels_.empty()) { |
| 385 return false; |
| 386 } |
| 387 return channels_.begin()->second->GetSslRole(role); |
| 388 } |
| 389 |
| 390 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override { |
| 391 ssl_max_version_ = version; |
| 392 for (const auto& kv : channels_) { |
| 393 kv.second->set_ssl_max_protocol_version(ssl_max_version_); |
| 394 } |
| 395 return true; |
| 396 } |
| 397 rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
| 398 return ssl_max_version_; |
| 399 } |
367 | 400 |
368 using Transport::local_description; | 401 using Transport::local_description; |
369 using Transport::remote_description; | 402 using Transport::remote_description; |
370 | 403 |
371 protected: | 404 protected: |
372 TransportChannelImpl* CreateTransportChannel(int component) override { | 405 TransportChannelImpl* CreateTransportChannel(int component) override { |
373 if (channels_.find(component) != channels_.end()) { | 406 if (channels_.find(component) != channels_.end()) { |
374 return NULL; | 407 return nullptr; |
375 } | 408 } |
376 FakeTransportChannel* channel = | 409 FakeTransportChannel* channel = |
377 new FakeTransportChannel(this, content_name(), component); | 410 new FakeTransportChannel(this, name(), component); |
| 411 channel->set_ssl_max_protocol_version(ssl_max_version_); |
378 channel->SetAsync(async_); | 412 channel->SetAsync(async_); |
379 SetChannelDestination(component, channel); | 413 SetChannelDestination(component, channel); |
380 channels_[component] = channel; | 414 channels_[component] = channel; |
381 return channel; | 415 return channel; |
382 } | 416 } |
| 417 |
383 void DestroyTransportChannel(TransportChannelImpl* channel) override { | 418 void DestroyTransportChannel(TransportChannelImpl* channel) override { |
384 channels_.erase(channel->component()); | 419 channels_.erase(channel->component()); |
385 delete channel; | 420 delete channel; |
386 } | 421 } |
387 void SetCertificate_w( | |
388 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { | |
389 certificate_ = certificate; | |
390 } | |
391 bool GetCertificate_w( | |
392 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { | |
393 if (!certificate_) | |
394 return false; | |
395 | |
396 *certificate = certificate_; | |
397 return true; | |
398 } | |
399 | 422 |
400 private: | 423 private: |
401 FakeTransportChannel* GetFakeChannel(int component) { | 424 FakeTransportChannel* GetFakeChannel(int component) { |
402 ChannelMap::iterator it = channels_.find(component); | 425 auto it = channels_.find(component); |
403 return (it != channels_.end()) ? it->second : NULL; | 426 return (it != channels_.end()) ? it->second : nullptr; |
404 } | 427 } |
405 void SetChannelDestination(int component, | 428 |
406 FakeTransportChannel* channel) { | 429 void SetChannelDestination(int component, FakeTransportChannel* channel) { |
407 FakeTransportChannel* dest_channel = NULL; | 430 FakeTransportChannel* dest_channel = nullptr; |
408 if (dest_) { | 431 if (dest_) { |
409 dest_channel = dest_->GetFakeChannel(component); | 432 dest_channel = dest_->GetFakeChannel(component); |
410 if (dest_channel) | 433 if (dest_channel) { |
411 dest_channel->SetLocalCertificate(dest_->certificate_); | 434 dest_channel->SetLocalCertificate(dest_->certificate_); |
| 435 } |
412 } | 436 } |
413 channel->SetDestination(dest_channel); | 437 channel->SetDestination(dest_channel); |
414 } | 438 } |
415 | 439 |
416 // Note, this is distinct from the Channel map owned by Transport. | 440 // Note, this is distinct from the Channel map owned by Transport. |
417 // This map just tracks the FakeTransportChannels created by this class. | 441 // This map just tracks the FakeTransportChannels created by this class. |
| 442 // It's mainly needed so that we can access a FakeTransportChannel directly, |
| 443 // even if wrapped by a DtlsTransportChannelWrapper. |
418 ChannelMap channels_; | 444 ChannelMap channels_; |
419 FakeTransport* dest_; | 445 FakeTransport* dest_ = nullptr; |
420 bool async_; | 446 bool async_ = false; |
421 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 447 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
| 448 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; |
422 }; | 449 }; |
423 | 450 |
424 // Fake session class, which can be passed into a BaseChannel object for | 451 // Fake TransportController class, which can be passed into a BaseChannel object |
425 // test purposes. Can be connected to other FakeSessions via Connect(). | 452 // for test purposes. Can be connected to other FakeTransportControllers via |
426 class FakeSession : public BaseSession { | 453 // Connect(). |
| 454 // |
| 455 // This fake is unusual in that for the most part, it's implemented with the |
| 456 // real TransportController code, but with fake TransportChannels underneath. |
| 457 class FakeTransportController : public TransportController { |
427 public: | 458 public: |
428 explicit FakeSession() | 459 FakeTransportController() |
429 : BaseSession(rtc::Thread::Current(), | 460 : TransportController(rtc::Thread::Current(), |
430 rtc::Thread::Current(), | 461 rtc::Thread::Current(), |
431 NULL, "", "", true), | 462 nullptr), |
432 fail_create_channel_(false) { | 463 fail_create_channel_(false) {} |
433 } | 464 |
434 explicit FakeSession(bool initiator) | 465 explicit FakeTransportController(IceRole role) |
435 : BaseSession(rtc::Thread::Current(), | 466 : TransportController(rtc::Thread::Current(), |
436 rtc::Thread::Current(), | 467 rtc::Thread::Current(), |
437 NULL, "", "", initiator), | 468 nullptr), |
438 fail_create_channel_(false) { | 469 fail_create_channel_(false) { |
439 } | 470 SetIceRole(role); |
440 FakeSession(rtc::Thread* worker_thread, bool initiator) | |
441 : BaseSession(rtc::Thread::Current(), | |
442 worker_thread, | |
443 NULL, "", "", initiator), | |
444 fail_create_channel_(false) { | |
445 } | 471 } |
446 | 472 |
447 FakeTransport* GetTransport(const std::string& content_name) { | 473 explicit FakeTransportController(rtc::Thread* worker_thread) |
448 return static_cast<FakeTransport*>( | 474 : TransportController(rtc::Thread::Current(), worker_thread, nullptr), |
449 BaseSession::GetTransport(content_name)); | 475 fail_create_channel_(false) {} |
| 476 |
| 477 FakeTransportController(rtc::Thread* worker_thread, IceRole role) |
| 478 : TransportController(rtc::Thread::Current(), worker_thread, nullptr), |
| 479 fail_create_channel_(false) { |
| 480 SetIceRole(role); |
450 } | 481 } |
451 | 482 |
452 void Connect(FakeSession* dest) { | 483 FakeTransport* GetTransport_w(const std::string& transport_name) { |
453 // Simulate the exchange of candidates. | 484 return static_cast<FakeTransport*>( |
454 CompleteNegotiation(); | 485 TransportController::GetTransport_w(transport_name)); |
455 dest->CompleteNegotiation(); | |
456 for (TransportMap::const_iterator it = transport_proxies().begin(); | |
457 it != transport_proxies().end(); ++it) { | |
458 static_cast<FakeTransport*>(it->second->impl())->SetDestination( | |
459 dest->GetTransport(it->first)); | |
460 } | |
461 } | 486 } |
462 | 487 |
463 TransportChannel* CreateChannel(const std::string& content_name, | 488 void Connect(FakeTransportController* dest) { |
464 int component) override { | 489 worker_thread()->Invoke<void>( |
| 490 rtc::Bind(&FakeTransportController::Connect_w, this, dest)); |
| 491 } |
| 492 |
| 493 TransportChannel* CreateTransportChannel_w(const std::string& transport_name, |
| 494 int component) override { |
465 if (fail_create_channel_) { | 495 if (fail_create_channel_) { |
466 return NULL; | 496 return nullptr; |
467 } | 497 } |
468 return BaseSession::CreateChannel(content_name, component); | 498 return TransportController::CreateTransportChannel_w(transport_name, |
| 499 component); |
469 } | 500 } |
470 | 501 |
471 void set_fail_channel_creation(bool fail_channel_creation) { | 502 void set_fail_channel_creation(bool fail_channel_creation) { |
472 fail_create_channel_ = fail_channel_creation; | 503 fail_create_channel_ = fail_channel_creation; |
473 } | 504 } |
474 | 505 |
475 // TODO: Hoist this into Session when we re-work the Session code. | 506 protected: |
476 void set_ssl_rtccertificate( | 507 Transport* CreateTransport_w(const std::string& transport_name) override { |
477 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { | 508 return new FakeTransport(transport_name); |
478 for (TransportMap::const_iterator it = transport_proxies().begin(); | 509 } |
479 it != transport_proxies().end(); ++it) { | |
480 // We know that we have a FakeTransport* | |
481 | 510 |
482 static_cast<FakeTransport*>(it->second->impl())->set_certificate | 511 void Connect_w(FakeTransportController* dest) { |
483 (certificate); | 512 // Simulate the exchange of candidates. |
| 513 ConnectChannels_w(); |
| 514 dest->ConnectChannels_w(); |
| 515 for (auto& kv : transports()) { |
| 516 FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
| 517 transport->SetDestination(dest->GetTransport_w(kv.first)); |
484 } | 518 } |
485 } | 519 } |
486 | 520 |
487 protected: | 521 void ConnectChannels_w() { |
488 Transport* CreateTransport(const std::string& content_name) override { | 522 for (auto& kv : transports()) { |
489 return new FakeTransport(signaling_thread(), worker_thread(), content_name); | 523 FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
490 } | 524 transport->ConnectChannels(); |
491 | |
492 void CompleteNegotiation() { | |
493 for (TransportMap::const_iterator it = transport_proxies().begin(); | |
494 it != transport_proxies().end(); ++it) { | |
495 it->second->CompleteNegotiation(); | |
496 it->second->ConnectChannels(); | |
497 } | 525 } |
498 } | 526 } |
499 | 527 |
500 private: | 528 private: |
501 bool fail_create_channel_; | 529 bool fail_create_channel_; |
502 }; | 530 }; |
503 | 531 |
504 } // namespace cricket | 532 } // namespace cricket |
505 | 533 |
506 #endif // WEBRTC_P2P_BASE_FAKESESSION_H_ | 534 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
OLD | NEW |