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 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 if (dest_) { | 133 if (dest_) { |
134 dest_->state_ = STATE_INIT; | 134 dest_->state_ = STATE_INIT; |
135 dest_->dest_ = nullptr; | 135 dest_->dest_ = nullptr; |
136 dest_ = nullptr; | 136 dest_ = nullptr; |
137 } | 137 } |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 void SetWritable(bool writable) { set_writable(writable); } | 141 void SetWritable(bool writable) { set_writable(writable); } |
142 | 142 |
143 void SetDestination(FakeTransportChannel* dest) { | 143 // Simulates the two transport channels connecting to each other. |
| 144 // If |asymmetric| is true this method only affects this FakeTransportChannel. |
| 145 // If false, it affects |dest| as well. |
| 146 void SetDestination(FakeTransportChannel* dest, bool asymmetric = false) { |
144 if (state_ == STATE_CONNECTING && dest) { | 147 if (state_ == STATE_CONNECTING && dest) { |
145 // This simulates the delivery of candidates. | 148 // This simulates the delivery of candidates. |
146 dest_ = dest; | 149 dest_ = dest; |
147 dest_->dest_ = this; | |
148 if (local_cert_ && dest_->local_cert_) { | 150 if (local_cert_ && dest_->local_cert_) { |
149 do_dtls_ = true; | 151 do_dtls_ = true; |
150 dest_->do_dtls_ = true; | |
151 NegotiateSrtpCiphers(); | 152 NegotiateSrtpCiphers(); |
152 } | 153 } |
153 state_ = STATE_CONNECTED; | 154 state_ = STATE_CONNECTED; |
154 dest_->state_ = STATE_CONNECTED; | |
155 set_writable(true); | 155 set_writable(true); |
156 dest_->set_writable(true); | 156 if (!asymmetric) { |
| 157 dest->SetDestination(this, true); |
| 158 } |
157 } else if (state_ == STATE_CONNECTED && !dest) { | 159 } else if (state_ == STATE_CONNECTED && !dest) { |
158 // Simulates loss of connectivity, by asymmetrically forgetting dest_. | 160 // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
159 dest_ = nullptr; | 161 dest_ = nullptr; |
160 state_ = STATE_CONNECTING; | 162 state_ = STATE_CONNECTING; |
161 set_writable(false); | 163 set_writable(false); |
162 } | 164 } |
163 } | 165 } |
164 | 166 |
165 void SetConnectionCount(size_t connection_count) { | 167 void SetConnectionCount(size_t connection_count) { |
166 size_t old_connection_count = connection_count_; | 168 size_t old_connection_count = connection_count_; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 uint8_t* result, | 276 uint8_t* result, |
275 size_t result_len) override { | 277 size_t result_len) override { |
276 if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { | 278 if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
277 memset(result, 0xff, result_len); | 279 memset(result, 0xff, result_len); |
278 return true; | 280 return true; |
279 } | 281 } |
280 | 282 |
281 return false; | 283 return false; |
282 } | 284 } |
283 | 285 |
284 void NegotiateSrtpCiphers() { | |
285 for (std::vector<int>::const_iterator it1 = srtp_ciphers_.begin(); | |
286 it1 != srtp_ciphers_.end(); ++it1) { | |
287 for (std::vector<int>::const_iterator it2 = dest_->srtp_ciphers_.begin(); | |
288 it2 != dest_->srtp_ciphers_.end(); ++it2) { | |
289 if (*it1 == *it2) { | |
290 chosen_crypto_suite_ = *it1; | |
291 dest_->chosen_crypto_suite_ = *it2; | |
292 return; | |
293 } | |
294 } | |
295 } | |
296 } | |
297 | |
298 bool GetStats(ConnectionInfos* infos) override { | 286 bool GetStats(ConnectionInfos* infos) override { |
299 ConnectionInfo info; | 287 ConnectionInfo info; |
300 infos->clear(); | 288 infos->clear(); |
301 infos->push_back(info); | 289 infos->push_back(info); |
302 return true; | 290 return true; |
303 } | 291 } |
304 | 292 |
305 void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { | 293 void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { |
306 ssl_max_version_ = version; | 294 ssl_max_version_ = version; |
307 } | 295 } |
308 rtc::SSLProtocolVersion ssl_max_protocol_version() const { | 296 rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
309 return ssl_max_version_; | 297 return ssl_max_version_; |
310 } | 298 } |
311 | 299 |
312 private: | 300 private: |
| 301 void NegotiateSrtpCiphers() { |
| 302 for (std::vector<int>::const_iterator it1 = srtp_ciphers_.begin(); |
| 303 it1 != srtp_ciphers_.end(); ++it1) { |
| 304 for (std::vector<int>::const_iterator it2 = dest_->srtp_ciphers_.begin(); |
| 305 it2 != dest_->srtp_ciphers_.end(); ++it2) { |
| 306 if (*it1 == *it2) { |
| 307 chosen_crypto_suite_ = *it1; |
| 308 return; |
| 309 } |
| 310 } |
| 311 } |
| 312 } |
| 313 |
313 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; | 314 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; |
314 FakeTransportChannel* dest_ = nullptr; | 315 FakeTransportChannel* dest_ = nullptr; |
315 State state_ = STATE_INIT; | 316 State state_ = STATE_INIT; |
316 bool async_ = false; | 317 bool async_ = false; |
317 Candidates remote_candidates_; | 318 Candidates remote_candidates_; |
318 rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; | 319 rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
319 rtc::FakeSSLCertificate* remote_cert_ = nullptr; | 320 rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
320 bool do_dtls_ = false; | 321 bool do_dtls_ = false; |
321 std::vector<int> srtp_ciphers_; | 322 std::vector<int> srtp_ciphers_; |
322 int chosen_crypto_suite_ = rtc::SRTP_INVALID_CRYPTO_SUITE; | 323 int chosen_crypto_suite_ = rtc::SRTP_INVALID_CRYPTO_SUITE; |
(...skipping 28 matching lines...) Expand all Loading... |
351 FakeTransport(const std::string& name, PortAllocator* allocator) | 352 FakeTransport(const std::string& name, PortAllocator* allocator) |
352 : Transport(name, nullptr) {} | 353 : Transport(name, nullptr) {} |
353 | 354 |
354 ~FakeTransport() { DestroyAllChannels(); } | 355 ~FakeTransport() { DestroyAllChannels(); } |
355 | 356 |
356 const ChannelMap& channels() const { return channels_; } | 357 const ChannelMap& channels() const { return channels_; } |
357 | 358 |
358 // If async, will send packets by "Post"-ing to message queue instead of | 359 // If async, will send packets by "Post"-ing to message queue instead of |
359 // synchronously "Send"-ing. | 360 // synchronously "Send"-ing. |
360 void SetAsync(bool async) { async_ = async; } | 361 void SetAsync(bool async) { async_ = async; } |
361 void SetDestination(FakeTransport* dest) { | 362 |
| 363 // If |asymmetric| is true, only set the destination for this transport, and |
| 364 // not |dest|. |
| 365 void SetDestination(FakeTransport* dest, bool asymmetric = false) { |
362 dest_ = dest; | 366 dest_ = dest; |
363 for (const auto& kv : channels_) { | 367 for (const auto& kv : channels_) { |
364 kv.second->SetLocalCertificate(certificate_); | 368 kv.second->SetLocalCertificate(certificate_); |
365 SetChannelDestination(kv.first, kv.second); | 369 SetChannelDestination(kv.first, kv.second, asymmetric); |
366 } | 370 } |
367 } | 371 } |
368 | 372 |
369 void SetWritable(bool writable) { | 373 void SetWritable(bool writable) { |
370 for (const auto& kv : channels_) { | 374 for (const auto& kv : channels_) { |
371 kv.second->SetWritable(writable); | 375 kv.second->SetWritable(writable); |
372 } | 376 } |
373 } | 377 } |
374 | 378 |
375 void SetLocalCertificate( | 379 void SetLocalCertificate( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 using Transport::remote_description; | 411 using Transport::remote_description; |
408 | 412 |
409 protected: | 413 protected: |
410 TransportChannelImpl* CreateTransportChannel(int component) override { | 414 TransportChannelImpl* CreateTransportChannel(int component) override { |
411 if (channels_.find(component) != channels_.end()) { | 415 if (channels_.find(component) != channels_.end()) { |
412 return nullptr; | 416 return nullptr; |
413 } | 417 } |
414 FakeTransportChannel* channel = new FakeTransportChannel(name(), component); | 418 FakeTransportChannel* channel = new FakeTransportChannel(name(), component); |
415 channel->set_ssl_max_protocol_version(ssl_max_version_); | 419 channel->set_ssl_max_protocol_version(ssl_max_version_); |
416 channel->SetAsync(async_); | 420 channel->SetAsync(async_); |
417 SetChannelDestination(component, channel); | 421 SetChannelDestination(component, channel, false); |
418 channels_[component] = channel; | 422 channels_[component] = channel; |
419 return channel; | 423 return channel; |
420 } | 424 } |
421 | 425 |
422 void DestroyTransportChannel(TransportChannelImpl* channel) override { | 426 void DestroyTransportChannel(TransportChannelImpl* channel) override { |
423 channels_.erase(channel->component()); | 427 channels_.erase(channel->component()); |
424 delete channel; | 428 delete channel; |
425 } | 429 } |
426 | 430 |
427 private: | 431 private: |
428 FakeTransportChannel* GetFakeChannel(int component) { | 432 FakeTransportChannel* GetFakeChannel(int component) { |
429 auto it = channels_.find(component); | 433 auto it = channels_.find(component); |
430 return (it != channels_.end()) ? it->second : nullptr; | 434 return (it != channels_.end()) ? it->second : nullptr; |
431 } | 435 } |
432 | 436 |
433 void SetChannelDestination(int component, FakeTransportChannel* channel) { | 437 void SetChannelDestination(int component, |
| 438 FakeTransportChannel* channel, |
| 439 bool asymmetric) { |
434 FakeTransportChannel* dest_channel = nullptr; | 440 FakeTransportChannel* dest_channel = nullptr; |
435 if (dest_) { | 441 if (dest_) { |
436 dest_channel = dest_->GetFakeChannel(component); | 442 dest_channel = dest_->GetFakeChannel(component); |
437 if (dest_channel) { | 443 if (dest_channel && !asymmetric) { |
438 dest_channel->SetLocalCertificate(dest_->certificate_); | 444 dest_channel->SetLocalCertificate(dest_->certificate_); |
439 } | 445 } |
440 } | 446 } |
441 channel->SetDestination(dest_channel); | 447 channel->SetDestination(dest_channel, asymmetric); |
442 } | 448 } |
443 | 449 |
444 // Note, this is distinct from the Channel map owned by Transport. | 450 // Note, this is distinct from the Channel map owned by Transport. |
445 // This map just tracks the FakeTransportChannels created by this class. | 451 // This map just tracks the FakeTransportChannels created by this class. |
446 // It's mainly needed so that we can access a FakeTransportChannel directly, | 452 // It's mainly needed so that we can access a FakeTransportChannel directly, |
447 // even if wrapped by a DtlsTransportChannelWrapper. | 453 // even if wrapped by a DtlsTransportChannelWrapper. |
448 ChannelMap channels_; | 454 ChannelMap channels_; |
449 FakeTransport* dest_ = nullptr; | 455 FakeTransport* dest_ = nullptr; |
450 bool async_ = false; | 456 bool async_ = false; |
451 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 457 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 } | 577 } |
572 } | 578 } |
573 | 579 |
574 private: | 580 private: |
575 bool fail_create_channel_; | 581 bool fail_create_channel_; |
576 }; | 582 }; |
577 | 583 |
578 } // namespace cricket | 584 } // namespace cricket |
579 | 585 |
580 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ | 586 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
OLD | NEW |