deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
| 12 | #define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
| 13 | |
| 14 | #include <map> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | #include <memory> |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 19 | #include "webrtc/base/bind.h" |
| 20 | #include "webrtc/base/buffer.h" |
| 21 | #include "webrtc/base/fakesslidentity.h" |
| 22 | #include "webrtc/base/messagequeue.h" |
| 23 | #include "webrtc/base/sigslot.h" |
| 24 | #include "webrtc/base/sslfingerprint.h" |
| 25 | #include "webrtc/base/thread.h" |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 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" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 31 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 32 | #ifdef HAVE_QUIC |
| 33 | #include "webrtc/p2p/quic/quictransport.h" |
| 34 | #endif |
| 35 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 36 | namespace cricket { |
| 37 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 38 | namespace { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 39 | struct PacketMessageData : public rtc::MessageData { |
| 40 | PacketMessageData(const char* data, size_t len) : packet(data, len) {} |
| 41 | rtc::Buffer packet; |
| 42 | }; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 43 | } // namespace |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 44 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 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 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 266 | // Fake transport channel class, which can be passed to anything that needs a |
| 267 | // transport channel. Can be informed of another FakeTransportChannel via |
| 268 | // SetDestination. |
| 269 | // TODO(hbos): Move implementation to .cc file, this and other classes in file. |
| 270 | class FakeTransportChannel : public TransportChannelImpl, |
| 271 | public rtc::MessageHandler { |
| 272 | public: |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 273 | explicit FakeTransportChannel(const std::string& name, int component) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 274 | : TransportChannelImpl(name, component), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 275 | dtls_fingerprint_("", nullptr, 0) {} |
| 276 | ~FakeTransportChannel() { Reset(); } |
| 277 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 278 | uint64_t IceTiebreaker() const { return tiebreaker_; } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 279 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 280 | const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 281 | const std::string& ice_pwd() const { return ice_pwd_; } |
| 282 | const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } |
| 283 | const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } |
| 284 | const rtc::SSLFingerprint& dtls_fingerprint() const { |
| 285 | return dtls_fingerprint_; |
| 286 | } |
| 287 | |
| 288 | // If async, will send packets by "Post"-ing to message queue instead of |
| 289 | // synchronously "Send"-ing. |
| 290 | void SetAsync(bool async) { async_ = async; } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 291 | void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 292 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 293 | IceTransportState GetState() const override { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 294 | if (connection_count_ == 0) { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 295 | return had_connection_ ? IceTransportState::STATE_FAILED |
| 296 | : IceTransportState::STATE_INIT; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (connection_count_ == 1) { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 300 | return IceTransportState::STATE_COMPLETED; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 301 | } |
| 302 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 303 | return IceTransportState::STATE_CONNECTING; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void SetIceRole(IceRole role) override { role_ = role; } |
| 307 | IceRole GetIceRole() const override { return role_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 308 | void SetIceTiebreaker(uint64_t tiebreaker) override { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 309 | tiebreaker_ = tiebreaker; |
| 310 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 311 | void SetIceParameters(const IceParameters& ice_params) override { |
| 312 | ice_ufrag_ = ice_params.ufrag; |
| 313 | ice_pwd_ = ice_params.pwd; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 314 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 315 | void SetRemoteIceParameters(const IceParameters& params) override { |
| 316 | remote_ice_ufrag_ = params.ufrag; |
| 317 | remote_ice_pwd_ = params.pwd; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } |
| 321 | bool SetRemoteFingerprint(const std::string& alg, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 322 | const uint8_t* digest, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 323 | size_t digest_len) override { |
| 324 | dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
| 325 | return true; |
| 326 | } |
| 327 | bool SetSslRole(rtc::SSLRole role) override { |
| 328 | ssl_role_ = role; |
| 329 | return true; |
| 330 | } |
| 331 | bool GetSslRole(rtc::SSLRole* role) const override { |
| 332 | *role = ssl_role_; |
| 333 | return true; |
| 334 | } |
| 335 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 336 | void MaybeStartGathering() override { |
| 337 | if (gathering_state_ == kIceGatheringNew) { |
| 338 | gathering_state_ = kIceGatheringGathering; |
| 339 | SignalGatheringState(this); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | IceGatheringState gathering_state() const override { |
| 344 | return gathering_state_; |
| 345 | } |
| 346 | |
| 347 | void Reset() { |
| 348 | if (state_ != STATE_INIT) { |
| 349 | state_ = STATE_INIT; |
| 350 | if (dest_) { |
| 351 | dest_->state_ = STATE_INIT; |
| 352 | dest_->dest_ = nullptr; |
| 353 | dest_ = nullptr; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void SetWritable(bool writable) { set_writable(writable); } |
| 359 | |
deadbeef | e84cd2e | 2016-05-04 17:16:34 -0700 | [diff] [blame] | 360 | // Simulates the two transport channels connecting to each other. |
| 361 | // If |asymmetric| is true this method only affects this FakeTransportChannel. |
| 362 | // If false, it affects |dest| as well. |
| 363 | void SetDestination(FakeTransportChannel* dest, bool asymmetric = false) { |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 364 | if (state_ == STATE_INIT && dest) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 365 | // This simulates the delivery of candidates. |
| 366 | dest_ = dest; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 367 | if (local_cert_ && dest_->local_cert_) { |
| 368 | do_dtls_ = true; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 369 | NegotiateSrtpCiphers(); |
| 370 | } |
| 371 | state_ = STATE_CONNECTED; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 372 | set_writable(true); |
deadbeef | e84cd2e | 2016-05-04 17:16:34 -0700 | [diff] [blame] | 373 | if (!asymmetric) { |
| 374 | dest->SetDestination(this, true); |
| 375 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 376 | } else if (state_ == STATE_CONNECTED && !dest) { |
| 377 | // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
| 378 | dest_ = nullptr; |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 379 | state_ = STATE_INIT; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 380 | set_writable(false); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void SetConnectionCount(size_t connection_count) { |
| 385 | size_t old_connection_count = connection_count_; |
| 386 | connection_count_ = connection_count; |
| 387 | if (connection_count) |
| 388 | had_connection_ = true; |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 389 | // In this fake transport channel, |connection_count_| determines the |
| 390 | // transport channel state. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 391 | if (connection_count_ < old_connection_count) |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 392 | SignalStateChanged(this); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void SetCandidatesGatheringComplete() { |
| 396 | if (gathering_state_ != kIceGatheringComplete) { |
| 397 | gathering_state_ = kIceGatheringComplete; |
| 398 | SignalGatheringState(this); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void SetReceiving(bool receiving) { set_receiving(receiving); } |
| 403 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 404 | void SetIceConfig(const IceConfig& config) override { ice_config_ = config; } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 405 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 406 | int receiving_timeout() const { return ice_config_.receiving_timeout; } |
| 407 | bool gather_continually() const { return ice_config_.gather_continually(); } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 408 | |
| 409 | int SendPacket(const char* data, |
| 410 | size_t len, |
| 411 | const rtc::PacketOptions& options, |
| 412 | int flags) override { |
| 413 | if (state_ != STATE_CONNECTED) { |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | if (flags != PF_SRTP_BYPASS && flags != 0) { |
| 418 | return -1; |
| 419 | } |
| 420 | |
| 421 | PacketMessageData* packet = new PacketMessageData(data, len); |
| 422 | if (async_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 423 | if (async_delay_ms_) { |
| 424 | rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, async_delay_ms_, |
| 425 | this, 0, packet); |
| 426 | } else { |
| 427 | rtc::Thread::Current()->Post(RTC_FROM_HERE, this, 0, packet); |
| 428 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 429 | } else { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 430 | rtc::Thread::Current()->Send(RTC_FROM_HERE, this, 0, packet); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 431 | } |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 432 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 433 | SignalSentPacket(this, sent_packet); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 434 | return static_cast<int>(len); |
| 435 | } |
| 436 | int SetOption(rtc::Socket::Option opt, int value) override { return true; } |
| 437 | bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } |
| 438 | int GetError() override { return 0; } |
| 439 | |
| 440 | void AddRemoteCandidate(const Candidate& candidate) override { |
| 441 | remote_candidates_.push_back(candidate); |
| 442 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 443 | |
| 444 | void RemoveRemoteCandidate(const Candidate& candidate) override {} |
| 445 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 446 | const Candidates& remote_candidates() const { return remote_candidates_; } |
| 447 | |
| 448 | void OnMessage(rtc::Message* msg) override { |
| 449 | PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); |
| 450 | dest_->SignalReadPacket(dest_, data->packet.data<char>(), |
| 451 | data->packet.size(), rtc::CreatePacketTime(0), 0); |
| 452 | delete data; |
| 453 | } |
| 454 | |
| 455 | bool SetLocalCertificate( |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 456 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 457 | local_cert_ = certificate; |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { |
| 462 | remote_cert_ = cert; |
| 463 | } |
| 464 | |
| 465 | bool IsDtlsActive() const override { return do_dtls_; } |
| 466 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 467 | bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 468 | srtp_ciphers_ = ciphers; |
| 469 | return true; |
| 470 | } |
| 471 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 472 | bool GetSrtpCryptoSuite(int* crypto_suite) override { |
| 473 | if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 474 | *crypto_suite = chosen_crypto_suite_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 475 | return true; |
| 476 | } |
| 477 | return false; |
| 478 | } |
| 479 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 480 | bool GetSslCipherSuite(int* cipher_suite) override { return false; } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 481 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 482 | rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 483 | return local_cert_; |
| 484 | } |
| 485 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 486 | std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate() |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 487 | const override { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 488 | return remote_cert_ ? std::unique_ptr<rtc::SSLCertificate>( |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 489 | remote_cert_->GetReference()) |
| 490 | : nullptr; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | bool ExportKeyingMaterial(const std::string& label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 494 | const uint8_t* context, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 495 | size_t context_len, |
| 496 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 497 | uint8_t* result, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 498 | size_t result_len) override { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 499 | if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 500 | memset(result, 0xff, result_len); |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | return false; |
| 505 | } |
| 506 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 507 | bool GetStats(ConnectionInfos* infos) override { |
| 508 | ConnectionInfo info; |
| 509 | infos->clear(); |
| 510 | infos->push_back(info); |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { |
| 515 | ssl_max_version_ = version; |
| 516 | } |
| 517 | rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
| 518 | return ssl_max_version_; |
| 519 | } |
| 520 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 521 | void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override { |
| 522 | } |
| 523 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 524 | private: |
deadbeef | e84cd2e | 2016-05-04 17:16:34 -0700 | [diff] [blame] | 525 | void NegotiateSrtpCiphers() { |
| 526 | for (std::vector<int>::const_iterator it1 = srtp_ciphers_.begin(); |
| 527 | it1 != srtp_ciphers_.end(); ++it1) { |
| 528 | for (std::vector<int>::const_iterator it2 = dest_->srtp_ciphers_.begin(); |
| 529 | it2 != dest_->srtp_ciphers_.end(); ++it2) { |
| 530 | if (*it1 == *it2) { |
| 531 | chosen_crypto_suite_ = *it1; |
| 532 | return; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 538 | enum State { STATE_INIT, STATE_CONNECTED }; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 539 | FakeTransportChannel* dest_ = nullptr; |
| 540 | State state_ = STATE_INIT; |
| 541 | bool async_ = false; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 542 | int async_delay_ms_ = 0; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 543 | Candidates remote_candidates_; |
| 544 | rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
| 545 | rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
| 546 | bool do_dtls_ = false; |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 547 | std::vector<int> srtp_ciphers_; |
| 548 | int chosen_crypto_suite_ = rtc::SRTP_INVALID_CRYPTO_SUITE; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 549 | IceConfig ice_config_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 550 | IceRole role_ = ICEROLE_UNKNOWN; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 551 | uint64_t tiebreaker_ = 0; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 552 | std::string ice_ufrag_; |
| 553 | std::string ice_pwd_; |
| 554 | std::string remote_ice_ufrag_; |
| 555 | std::string remote_ice_pwd_; |
| 556 | IceMode remote_ice_mode_ = ICEMODE_FULL; |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 557 | rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 558 | rtc::SSLFingerprint dtls_fingerprint_; |
| 559 | rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT; |
| 560 | size_t connection_count_ = 0; |
| 561 | IceGatheringState gathering_state_ = kIceGatheringNew; |
| 562 | bool had_connection_ = false; |
| 563 | }; |
| 564 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 565 | // Fake candidate pair class, which can be passed to BaseChannel for testing |
| 566 | // purposes. |
| 567 | class FakeCandidatePair : public CandidatePairInterface { |
| 568 | public: |
| 569 | FakeCandidatePair(const Candidate& local_candidate, |
| 570 | const Candidate& remote_candidate) |
| 571 | : local_candidate_(local_candidate), |
| 572 | remote_candidate_(remote_candidate) {} |
| 573 | const Candidate& local_candidate() const override { return local_candidate_; } |
| 574 | const Candidate& remote_candidate() const override { |
| 575 | return remote_candidate_; |
| 576 | } |
| 577 | |
| 578 | private: |
| 579 | Candidate local_candidate_; |
| 580 | Candidate remote_candidate_; |
| 581 | }; |
| 582 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 583 | // Fake TransportController class, which can be passed into a BaseChannel object |
| 584 | // for test purposes. Can be connected to other FakeTransportControllers via |
| 585 | // Connect(). |
| 586 | // |
| 587 | // This fake is unusual in that for the most part, it's implemented with the |
| 588 | // real TransportController code, but with fake TransportChannels underneath. |
| 589 | class FakeTransportController : public TransportController { |
| 590 | public: |
| 591 | FakeTransportController() |
| 592 | : TransportController(rtc::Thread::Current(), |
| 593 | rtc::Thread::Current(), |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 594 | nullptr) {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 595 | |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 596 | explicit FakeTransportController(bool redetermine_role_on_ice_restart) |
| 597 | : TransportController(rtc::Thread::Current(), |
| 598 | rtc::Thread::Current(), |
| 599 | nullptr, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 600 | redetermine_role_on_ice_restart) {} |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 601 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 602 | explicit FakeTransportController(IceRole role) |
| 603 | : TransportController(rtc::Thread::Current(), |
| 604 | rtc::Thread::Current(), |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 605 | nullptr) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 606 | SetIceRole(role); |
| 607 | } |
| 608 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 609 | explicit FakeTransportController(rtc::Thread* network_thread) |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 610 | : TransportController(rtc::Thread::Current(), network_thread, nullptr) {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 611 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 612 | FakeTransportController(rtc::Thread* network_thread, IceRole role) |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 613 | : TransportController(rtc::Thread::Current(), network_thread, nullptr) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 614 | SetIceRole(role); |
| 615 | } |
| 616 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 617 | FakeTransportChannel* GetFakeTransportChannel_n( |
| 618 | const std::string& transport_name, |
| 619 | int component) { |
| 620 | return static_cast<FakeTransportChannel*>( |
| 621 | get_channel_for_testing(transport_name, component)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 622 | } |
| 623 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 624 | // Simulate the exchange of transport descriptions, and the gathering and |
| 625 | // exchange of ICE candidates. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 626 | void Connect(FakeTransportController* dest) { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 627 | for (const std::string& transport_name : transport_names_for_testing()) { |
| 628 | TransportDescription local_desc( |
| 629 | std::vector<std::string>(), |
| 630 | rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), |
| 631 | rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), |
| 632 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, nullptr); |
| 633 | TransportDescription remote_desc( |
| 634 | std::vector<std::string>(), |
| 635 | rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), |
| 636 | rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), |
| 637 | cricket::ICEMODE_FULL, cricket::CONNECTIONROLE_NONE, nullptr); |
| 638 | std::string err; |
| 639 | SetLocalTransportDescription(transport_name, local_desc, |
| 640 | cricket::CA_OFFER, &err); |
| 641 | dest->SetRemoteTransportDescription(transport_name, local_desc, |
| 642 | cricket::CA_OFFER, &err); |
| 643 | dest->SetLocalTransportDescription(transport_name, remote_desc, |
| 644 | cricket::CA_ANSWER, &err); |
| 645 | SetRemoteTransportDescription(transport_name, remote_desc, |
| 646 | cricket::CA_ANSWER, &err); |
| 647 | } |
| 648 | MaybeStartGathering(); |
| 649 | dest->MaybeStartGathering(); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 650 | network_thread()->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 651 | RTC_FROM_HERE, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 652 | rtc::Bind(&FakeTransportController::SetChannelDestinations_n, this, |
| 653 | dest)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 656 | FakeCandidatePair* CreateFakeCandidatePair( |
| 657 | const rtc::SocketAddress& local_address, |
| 658 | int16_t local_network_id, |
| 659 | const rtc::SocketAddress& remote_address, |
| 660 | int16_t remote_network_id) { |
| 661 | Candidate local_candidate(0, "udp", local_address, 0u, "", "", "local", 0, |
| 662 | "foundation", local_network_id, 0); |
| 663 | Candidate remote_candidate(0, "udp", remote_address, 0u, "", "", "local", 0, |
| 664 | "foundation", remote_network_id, 0); |
| 665 | return new FakeCandidatePair(local_candidate, remote_candidate); |
| 666 | } |
| 667 | |
deadbeef | 57fd726 | 2016-12-06 15:28:55 -0800 | [diff] [blame] | 668 | protected: |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 669 | // The ICE channel is never actually used by TransportController directly, |
| 670 | // since (currently) the DTLS channel pretends to be both ICE + DTLS. This |
| 671 | // will change when we get rid of TransportChannelImpl. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 672 | IceTransportInternal* CreateIceTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 673 | const std::string& transport_name, |
| 674 | int component) override { |
| 675 | return nullptr; |
deadbeef | 57fd726 | 2016-12-06 15:28:55 -0800 | [diff] [blame] | 676 | } |
| 677 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 678 | TransportChannelImpl* CreateDtlsTransportChannel_n( |
| 679 | const std::string& transport_name, |
| 680 | int component, |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame^] | 681 | IceTransportInternal*) override { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 682 | return new FakeTransportChannel(transport_name, component); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | private: |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 686 | void SetChannelDestinations_n(FakeTransportController* dest) { |
| 687 | for (TransportChannelImpl* tc : channels_for_testing()) { |
| 688 | FakeTransportChannel* local = static_cast<FakeTransportChannel*>(tc); |
| 689 | FakeTransportChannel* remote = dest->GetFakeTransportChannel_n( |
| 690 | local->transport_name(), local->component()); |
| 691 | if (remote) { |
| 692 | bool asymmetric = false; |
| 693 | local->SetDestination(remote, asymmetric); |
| 694 | } |
| 695 | } |
| 696 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | } // namespace cricket |
| 700 | |
| 701 | #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |