| 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_FAKETRANSPORTCONTROLLER_H_ | 11 #ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
| 12 #define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ | 12 #define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
| 13 | 13 |
| 14 #include <map> | 14 #include <map> |
| 15 #include <memory> | 15 #include <memory> |
| 16 #include <string> | 16 #include <string> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "webrtc/p2p/base/candidatepairinterface.h" |
| 20 #include "webrtc/p2p/base/transportchannel.h" |
| 21 #include "webrtc/p2p/base/transportcontroller.h" |
| 22 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 19 #include "webrtc/base/bind.h" | 23 #include "webrtc/base/bind.h" |
| 20 #include "webrtc/base/buffer.h" | 24 #include "webrtc/base/buffer.h" |
| 21 #include "webrtc/base/fakesslidentity.h" | 25 #include "webrtc/base/fakesslidentity.h" |
| 22 #include "webrtc/base/messagequeue.h" | 26 #include "webrtc/base/messagequeue.h" |
| 23 #include "webrtc/base/sigslot.h" | 27 #include "webrtc/base/sigslot.h" |
| 24 #include "webrtc/base/sslfingerprint.h" | 28 #include "webrtc/base/sslfingerprint.h" |
| 25 #include "webrtc/base/thread.h" | 29 #include "webrtc/base/thread.h" |
| 26 #include "webrtc/p2p/base/candidatepairinterface.h" | |
| 27 #include "webrtc/p2p/base/icetransportinternal.h" | |
| 28 #include "webrtc/p2p/base/transportchannel.h" | |
| 29 #include "webrtc/p2p/base/transportchannelimpl.h" | |
| 30 #include "webrtc/p2p/base/transportcontroller.h" | |
| 31 | 30 |
| 32 #ifdef HAVE_QUIC | 31 #ifdef HAVE_QUIC |
| 33 #include "webrtc/p2p/quic/quictransport.h" | 32 #include "webrtc/p2p/quic/quictransport.h" |
| 34 #endif | 33 #endif |
| 35 | 34 |
| 36 namespace cricket { | 35 namespace cricket { |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 struct PacketMessageData : public rtc::MessageData { | 38 struct PacketMessageData : public rtc::MessageData { |
| 40 PacketMessageData(const char* data, size_t len) : packet(data, len) {} | 39 PacketMessageData(const char* data, size_t len) : packet(data, len) {} |
| 41 rtc::Buffer packet; | 40 rtc::Buffer packet; |
| 42 }; | 41 }; |
| 43 } // namespace | 42 } // namespace |
| 44 | 43 |
| 45 class FakeIceTransport : public IceTransportInternal, | |
| 46 public rtc::MessageHandler { | |
| 47 public: | |
| 48 explicit FakeIceTransport(const std::string& name, int component) | |
| 49 : name_(name), component_(component) {} | |
| 50 ~FakeIceTransport() { Reset(); } | |
| 51 | |
| 52 const std::string& transport_name() const override { return name_; } | |
| 53 int component() const override { return component_; } | |
| 54 uint64_t IceTiebreaker() const { return tiebreaker_; } | |
| 55 IceMode remote_ice_mode() const { return remote_ice_mode_; } | |
| 56 const std::string& ice_ufrag() const { return ice_ufrag_; } | |
| 57 const std::string& ice_pwd() const { return ice_pwd_; } | |
| 58 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } | |
| 59 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } | |
| 60 | |
| 61 // If async, will send packets by "Post"-ing to message queue instead of | |
| 62 // synchronously "Send"-ing. | |
| 63 void SetAsync(bool async) { async_ = async; } | |
| 64 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } | |
| 65 | |
| 66 IceTransportState GetState() const override { | |
| 67 if (connection_count_ == 0) { | |
| 68 return had_connection_ ? IceTransportState::STATE_FAILED | |
| 69 : IceTransportState::STATE_INIT; | |
| 70 } | |
| 71 | |
| 72 if (connection_count_ == 1) { | |
| 73 return IceTransportState::STATE_COMPLETED; | |
| 74 } | |
| 75 | |
| 76 return IceTransportState::STATE_CONNECTING; | |
| 77 } | |
| 78 | |
| 79 void SetIceRole(IceRole role) override { role_ = role; } | |
| 80 IceRole GetIceRole() const override { return role_; } | |
| 81 void SetIceTiebreaker(uint64_t tiebreaker) override { | |
| 82 tiebreaker_ = tiebreaker; | |
| 83 } | |
| 84 void SetIceParameters(const IceParameters& ice_params) override { | |
| 85 ice_ufrag_ = ice_params.ufrag; | |
| 86 ice_pwd_ = ice_params.pwd; | |
| 87 } | |
| 88 void SetRemoteIceParameters(const IceParameters& params) override { | |
| 89 remote_ice_ufrag_ = params.ufrag; | |
| 90 remote_ice_pwd_ = params.pwd; | |
| 91 } | |
| 92 | |
| 93 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } | |
| 94 | |
| 95 void MaybeStartGathering() override { | |
| 96 if (gathering_state_ == kIceGatheringNew) { | |
| 97 gathering_state_ = kIceGatheringGathering; | |
| 98 SignalGatheringState(this); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 IceGatheringState gathering_state() const override { | |
| 103 return gathering_state_; | |
| 104 } | |
| 105 | |
| 106 void Reset() { | |
| 107 if (state_ != STATE_INIT) { | |
| 108 state_ = STATE_INIT; | |
| 109 if (dest_) { | |
| 110 dest_->state_ = STATE_INIT; | |
| 111 dest_->dest_ = nullptr; | |
| 112 dest_ = nullptr; | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void SetWritable(bool writable) { set_writable(writable); } | |
| 118 | |
| 119 void set_writable(bool writable) { | |
| 120 if (writable_ == writable) { | |
| 121 return; | |
| 122 } | |
| 123 LOG(INFO) << "set_writable from:" << writable_ << " to " << writable; | |
| 124 writable_ = writable; | |
| 125 if (writable_) { | |
| 126 SignalReadyToSend(this); | |
| 127 } | |
| 128 SignalWritableState(this); | |
| 129 } | |
| 130 bool writable() const override { return writable_; } | |
| 131 | |
| 132 // Simulates the two transports connecting to each other. | |
| 133 // If |asymmetric| is true this method only affects this FakeIceTransport. | |
| 134 // If false, it affects |dest| as well. | |
| 135 void SetDestination(FakeIceTransport* dest, bool asymmetric = false) { | |
| 136 if (state_ == STATE_INIT && dest) { | |
| 137 // This simulates the delivery of candidates. | |
| 138 dest_ = dest; | |
| 139 state_ = STATE_CONNECTED; | |
| 140 set_writable(true); | |
| 141 if (!asymmetric) { | |
| 142 dest->SetDestination(this, true); | |
| 143 } | |
| 144 } else if (state_ == STATE_CONNECTED && !dest) { | |
| 145 // Simulates loss of connectivity, by asymmetrically forgetting dest_. | |
| 146 dest_ = nullptr; | |
| 147 state_ = STATE_INIT; | |
| 148 set_writable(false); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void SetConnectionCount(size_t connection_count) { | |
| 153 size_t old_connection_count = connection_count_; | |
| 154 connection_count_ = connection_count; | |
| 155 if (connection_count) | |
| 156 had_connection_ = true; | |
| 157 // In this fake transport channel, |connection_count_| determines the | |
| 158 // transport channel state. | |
| 159 if (connection_count_ < old_connection_count) | |
| 160 SignalStateChanged(this); | |
| 161 } | |
| 162 | |
| 163 void SetCandidatesGatheringComplete() { | |
| 164 if (gathering_state_ != kIceGatheringComplete) { | |
| 165 gathering_state_ = kIceGatheringComplete; | |
| 166 SignalGatheringState(this); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 void SetReceiving(bool receiving) { set_receiving(receiving); } | |
| 171 | |
| 172 void set_receiving(bool receiving) { | |
| 173 if (receiving_ == receiving) { | |
| 174 return; | |
| 175 } | |
| 176 receiving_ = receiving; | |
| 177 SignalReceivingState(this); | |
| 178 } | |
| 179 bool receiving() const override { return receiving_; } | |
| 180 | |
| 181 void SetIceConfig(const IceConfig& config) override { ice_config_ = config; } | |
| 182 | |
| 183 int receiving_timeout() const { return ice_config_.receiving_timeout; } | |
| 184 bool gather_continually() const { return ice_config_.gather_continually(); } | |
| 185 | |
| 186 int SendPacket(const char* data, | |
| 187 size_t len, | |
| 188 const rtc::PacketOptions& options, | |
| 189 int flags) override { | |
| 190 if (state_ != STATE_CONNECTED) { | |
| 191 return -1; | |
| 192 } | |
| 193 | |
| 194 if (flags != PF_SRTP_BYPASS && flags != 0) { | |
| 195 return -1; | |
| 196 } | |
| 197 | |
| 198 PacketMessageData* packet = new PacketMessageData(data, len); | |
| 199 if (async_) { | |
| 200 if (async_delay_ms_) { | |
| 201 rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, async_delay_ms_, | |
| 202 this, 0, packet); | |
| 203 } else { | |
| 204 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, 0, packet); | |
| 205 } | |
| 206 } else { | |
| 207 rtc::Thread::Current()->Send(RTC_FROM_HERE, this, 0, packet); | |
| 208 } | |
| 209 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); | |
| 210 SignalSentPacket(this, sent_packet); | |
| 211 return static_cast<int>(len); | |
| 212 } | |
| 213 int SetOption(rtc::Socket::Option opt, int value) override { return true; } | |
| 214 bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } | |
| 215 int GetError() override { return 0; } | |
| 216 | |
| 217 void AddRemoteCandidate(const Candidate& candidate) override { | |
| 218 remote_candidates_.push_back(candidate); | |
| 219 } | |
| 220 | |
| 221 void RemoveRemoteCandidate(const Candidate& candidate) override {} | |
| 222 | |
| 223 const Candidates& remote_candidates() const { return remote_candidates_; } | |
| 224 | |
| 225 void OnMessage(rtc::Message* msg) override { | |
| 226 PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); | |
| 227 dest_->SignalReadPacket(dest_, data->packet.data<char>(), | |
| 228 data->packet.size(), rtc::CreatePacketTime(0), 0); | |
| 229 delete data; | |
| 230 } | |
| 231 | |
| 232 bool GetStats(ConnectionInfos* infos) override { | |
| 233 ConnectionInfo info; | |
| 234 infos->clear(); | |
| 235 infos->push_back(info); | |
| 236 return true; | |
| 237 } | |
| 238 | |
| 239 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override { | |
| 240 } | |
| 241 | |
| 242 private: | |
| 243 std::string name_; | |
| 244 int component_; | |
| 245 enum State { STATE_INIT, STATE_CONNECTED }; | |
| 246 FakeIceTransport* dest_ = nullptr; | |
| 247 State state_ = STATE_INIT; | |
| 248 bool async_ = false; | |
| 249 int async_delay_ms_ = 0; | |
| 250 Candidates remote_candidates_; | |
| 251 IceConfig ice_config_; | |
| 252 IceRole role_ = ICEROLE_UNKNOWN; | |
| 253 uint64_t tiebreaker_ = 0; | |
| 254 std::string ice_ufrag_; | |
| 255 std::string ice_pwd_; | |
| 256 std::string remote_ice_ufrag_; | |
| 257 std::string remote_ice_pwd_; | |
| 258 IceMode remote_ice_mode_ = ICEMODE_FULL; | |
| 259 size_t connection_count_ = 0; | |
| 260 IceGatheringState gathering_state_ = kIceGatheringNew; | |
| 261 bool had_connection_ = false; | |
| 262 bool writable_ = false; | |
| 263 bool receiving_ = false; | |
| 264 }; | |
| 265 | |
| 266 // Fake transport channel class, which can be passed to anything that needs a | 44 // Fake transport channel class, which can be passed to anything that needs a |
| 267 // transport channel. Can be informed of another FakeTransportChannel via | 45 // transport channel. Can be informed of another FakeTransportChannel via |
| 268 // SetDestination. | 46 // SetDestination. |
| 269 // TODO(hbos): Move implementation to .cc file, this and other classes in file. | 47 // TODO(hbos): Move implementation to .cc file, this and other classes in file. |
| 270 class FakeTransportChannel : public TransportChannelImpl, | 48 class FakeTransportChannel : public TransportChannelImpl, |
| 271 public rtc::MessageHandler { | 49 public rtc::MessageHandler { |
| 272 public: | 50 public: |
| 273 explicit FakeTransportChannel(const std::string& name, int component) | 51 explicit FakeTransportChannel(const std::string& name, int component) |
| 274 : TransportChannelImpl(name, component), | 52 : TransportChannelImpl(name, component), |
| 275 dtls_fingerprint_("", nullptr, 0) {} | 53 dtls_fingerprint_("", nullptr, 0) {} |
| 276 ~FakeTransportChannel() { Reset(); } | 54 ~FakeTransportChannel() { Reset(); } |
| 277 | 55 |
| 278 uint64_t IceTiebreaker() const { return tiebreaker_; } | 56 uint64_t IceTiebreaker() const { return tiebreaker_; } |
| 279 IceMode remote_ice_mode() const { return remote_ice_mode_; } | 57 IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 280 const std::string& ice_ufrag() const { return ice_ufrag_; } | 58 const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 281 const std::string& ice_pwd() const { return ice_pwd_; } | 59 const std::string& ice_pwd() const { return ice_pwd_; } |
| 282 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } | 60 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } |
| 283 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } | 61 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } |
| 284 const rtc::SSLFingerprint& dtls_fingerprint() const { | 62 const rtc::SSLFingerprint& dtls_fingerprint() const { |
| 285 return dtls_fingerprint_; | 63 return dtls_fingerprint_; |
| 286 } | 64 } |
| 287 | 65 |
| 288 // If async, will send packets by "Post"-ing to message queue instead of | 66 // If async, will send packets by "Post"-ing to message queue instead of |
| 289 // synchronously "Send"-ing. | 67 // synchronously "Send"-ing. |
| 290 void SetAsync(bool async) { async_ = async; } | 68 void SetAsync(bool async) { async_ = async; } |
| 291 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } | 69 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } |
| 292 | 70 |
| 293 IceTransportState GetState() const override { | 71 TransportChannelState GetState() const override { |
| 294 if (connection_count_ == 0) { | 72 if (connection_count_ == 0) { |
| 295 return had_connection_ ? IceTransportState::STATE_FAILED | 73 return had_connection_ ? TransportChannelState::STATE_FAILED |
| 296 : IceTransportState::STATE_INIT; | 74 : TransportChannelState::STATE_INIT; |
| 297 } | 75 } |
| 298 | 76 |
| 299 if (connection_count_ == 1) { | 77 if (connection_count_ == 1) { |
| 300 return IceTransportState::STATE_COMPLETED; | 78 return TransportChannelState::STATE_COMPLETED; |
| 301 } | 79 } |
| 302 | 80 |
| 303 return IceTransportState::STATE_CONNECTING; | 81 return TransportChannelState::STATE_CONNECTING; |
| 304 } | 82 } |
| 305 | 83 |
| 306 void SetIceRole(IceRole role) override { role_ = role; } | 84 void SetIceRole(IceRole role) override { role_ = role; } |
| 307 IceRole GetIceRole() const override { return role_; } | 85 IceRole GetIceRole() const override { return role_; } |
| 308 void SetIceTiebreaker(uint64_t tiebreaker) override { | 86 void SetIceTiebreaker(uint64_t tiebreaker) override { |
| 309 tiebreaker_ = tiebreaker; | 87 tiebreaker_ = tiebreaker; |
| 310 } | 88 } |
| 311 void SetIceParameters(const IceParameters& ice_params) override { | 89 void SetIceParameters(const IceParameters& ice_params) override { |
| 312 ice_ufrag_ = ice_params.ufrag; | 90 ice_ufrag_ = ice_params.ufrag; |
| 313 ice_pwd_ = ice_params.pwd; | 91 ice_pwd_ = ice_params.pwd; |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 "foundation", local_network_id, 0); | 440 "foundation", local_network_id, 0); |
| 663 Candidate remote_candidate(0, "udp", remote_address, 0u, "", "", "local", 0, | 441 Candidate remote_candidate(0, "udp", remote_address, 0u, "", "", "local", 0, |
| 664 "foundation", remote_network_id, 0); | 442 "foundation", remote_network_id, 0); |
| 665 return new FakeCandidatePair(local_candidate, remote_candidate); | 443 return new FakeCandidatePair(local_candidate, remote_candidate); |
| 666 } | 444 } |
| 667 | 445 |
| 668 protected: | 446 protected: |
| 669 // The ICE channel is never actually used by TransportController directly, | 447 // The ICE channel is never actually used by TransportController directly, |
| 670 // since (currently) the DTLS channel pretends to be both ICE + DTLS. This | 448 // since (currently) the DTLS channel pretends to be both ICE + DTLS. This |
| 671 // will change when we get rid of TransportChannelImpl. | 449 // will change when we get rid of TransportChannelImpl. |
| 672 IceTransportInternal* CreateIceTransportChannel_n( | 450 TransportChannelImpl* CreateIceTransportChannel_n( |
| 673 const std::string& transport_name, | 451 const std::string& transport_name, |
| 674 int component) override { | 452 int component) override { |
| 675 return nullptr; | 453 return nullptr; |
| 676 } | 454 } |
| 677 | 455 |
| 678 TransportChannelImpl* CreateDtlsTransportChannel_n( | 456 TransportChannelImpl* CreateDtlsTransportChannel_n( |
| 679 const std::string& transport_name, | 457 const std::string& transport_name, |
| 680 int component, | 458 int component, |
| 681 IceTransportInternal*) override { | 459 TransportChannelImpl*) override { |
| 682 return new FakeTransportChannel(transport_name, component); | 460 return new FakeTransportChannel(transport_name, component); |
| 683 } | 461 } |
| 684 | 462 |
| 685 private: | 463 private: |
| 686 void SetChannelDestinations_n(FakeTransportController* dest) { | 464 void SetChannelDestinations_n(FakeTransportController* dest) { |
| 687 for (TransportChannelImpl* tc : channels_for_testing()) { | 465 for (TransportChannelImpl* tc : channels_for_testing()) { |
| 688 FakeTransportChannel* local = static_cast<FakeTransportChannel*>(tc); | 466 FakeTransportChannel* local = static_cast<FakeTransportChannel*>(tc); |
| 689 FakeTransportChannel* remote = dest->GetFakeTransportChannel_n( | 467 FakeTransportChannel* remote = dest->GetFakeTransportChannel_n( |
| 690 local->transport_name(), local->component()); | 468 local->transport_name(), local->component()); |
| 691 if (remote) { | 469 if (remote) { |
| 692 bool asymmetric = false; | 470 bool asymmetric = false; |
| 693 local->SetDestination(remote, asymmetric); | 471 local->SetDestination(remote, asymmetric); |
| 694 } | 472 } |
| 695 } | 473 } |
| 696 } | 474 } |
| 697 }; | 475 }; |
| 698 | 476 |
| 699 } // namespace cricket | 477 } // namespace cricket |
| 700 | 478 |
| 701 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ | 479 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
| OLD | NEW |