deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/transportcontroller.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 12 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 13 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 14 | #include <memory> |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "p2p/base/port.h" |
| 17 | #include "rtc_base/bind.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/thread.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 20 | |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 21 | namespace { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 22 | |
| 23 | enum { |
| 24 | MSG_ICECONNECTIONSTATE, |
| 25 | MSG_RECEIVING, |
| 26 | MSG_ICEGATHERINGSTATE, |
| 27 | MSG_CANDIDATESGATHERED, |
| 28 | }; |
| 29 | |
| 30 | struct CandidatesData : public rtc::MessageData { |
| 31 | CandidatesData(const std::string& transport_name, |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 32 | const cricket::Candidates& candidates) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 33 | : transport_name(transport_name), candidates(candidates) {} |
| 34 | |
| 35 | std::string transport_name; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 36 | cricket::Candidates candidates; |
| 37 | }; |
| 38 | |
| 39 | } // namespace { |
| 40 | |
| 41 | namespace cricket { |
| 42 | |
| 43 | // This class groups the DTLS and ICE channels, and helps keep track of |
| 44 | // how many external objects (BaseChannels) reference each channel. |
| 45 | class TransportController::ChannelPair { |
| 46 | public: |
| 47 | // TODO(deadbeef): Change the types of |dtls| and |ice| to |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 48 | // DtlsTransport and P2PTransportChannelWrapper, once TransportChannelImpl is |
| 49 | // removed. |
| 50 | ChannelPair(DtlsTransportInternal* dtls, IceTransportInternal* ice) |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 51 | : ice_(ice), dtls_(dtls) {} |
| 52 | |
| 53 | // Currently, all ICE-related calls still go through this DTLS channel. But |
| 54 | // that will change once we get rid of TransportChannelImpl, and the DTLS |
| 55 | // channel interface no longer includes ICE-specific methods. |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 56 | const DtlsTransportInternal* dtls() const { return dtls_.get(); } |
| 57 | DtlsTransportInternal* dtls() { return dtls_.get(); } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 58 | const IceTransportInternal* ice() const { return ice_.get(); } |
| 59 | IceTransportInternal* ice() { return ice_.get(); } |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 60 | |
| 61 | private: |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 62 | std::unique_ptr<IceTransportInternal> ice_; |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 63 | std::unique_ptr<DtlsTransportInternal> dtls_; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 64 | |
| 65 | RTC_DISALLOW_COPY_AND_ASSIGN(ChannelPair); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 68 | TransportController::TransportController( |
| 69 | rtc::Thread* signaling_thread, |
| 70 | rtc::Thread* network_thread, |
| 71 | PortAllocator* port_allocator, |
| 72 | bool redetermine_role_on_ice_restart, |
| 73 | const rtc::CryptoOptions& crypto_options) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 74 | : signaling_thread_(signaling_thread), |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 75 | network_thread_(network_thread), |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 76 | port_allocator_(port_allocator), |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 77 | redetermine_role_on_ice_restart_(redetermine_role_on_ice_restart), |
| 78 | crypto_options_(crypto_options) {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 79 | |
| 80 | TransportController::~TransportController() { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 81 | // Channel destructors may try to send packets, so this needs to happen on |
| 82 | // the network thread. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 83 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 84 | RTC_FROM_HERE, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 85 | rtc::Bind(&TransportController::DestroyAllChannels_n, this)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool TransportController::SetSslMaxProtocolVersion( |
| 89 | rtc::SSLProtocolVersion version) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 90 | return network_thread_->Invoke<bool>( |
| 91 | RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n, |
| 92 | this, version)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 93 | } |
| 94 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 95 | void TransportController::SetIceConfig(const IceConfig& config) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 96 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 97 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 98 | rtc::Bind(&TransportController::SetIceConfig_n, this, config)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void TransportController::SetIceRole(IceRole ice_role) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 102 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 103 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 104 | rtc::Bind(&TransportController::SetIceRole_n, this, ice_role)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 107 | void TransportController::SetNeedsIceRestartFlag() { |
| 108 | for (auto& kv : transports_) { |
| 109 | kv.second->SetNeedsIceRestartFlag(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool TransportController::NeedsIceRestart( |
| 114 | const std::string& transport_name) const { |
| 115 | const JsepTransport* transport = GetJsepTransport(transport_name); |
| 116 | if (!transport) { |
| 117 | return false; |
| 118 | } |
| 119 | return transport->NeedsIceRestart(); |
| 120 | } |
| 121 | |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 122 | bool TransportController::GetSslRole(const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 123 | rtc::SSLRole* role) const { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 124 | return network_thread_->Invoke<bool>( |
| 125 | RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this, |
| 126 | transport_name, role)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool TransportController::SetLocalCertificate( |
| 130 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 131 | return network_thread_->Invoke<bool>( |
| 132 | RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n, |
| 133 | this, certificate)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool TransportController::GetLocalCertificate( |
| 137 | const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 138 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 139 | if (network_thread_->IsCurrent()) { |
| 140 | return GetLocalCertificate_n(transport_name, certificate); |
| 141 | } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 142 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 143 | RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n, |
| 144 | this, transport_name, certificate)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 147 | std::unique_ptr<rtc::SSLCertificate> |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 148 | TransportController::GetRemoteSSLCertificate( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 149 | const std::string& transport_name) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 150 | if (network_thread_->IsCurrent()) { |
| 151 | return GetRemoteSSLCertificate_n(transport_name); |
| 152 | } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 153 | return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 154 | RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n, |
| 155 | this, transport_name)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool TransportController::SetLocalTransportDescription( |
| 159 | const std::string& transport_name, |
| 160 | const TransportDescription& tdesc, |
| 161 | ContentAction action, |
| 162 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 163 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 164 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 165 | rtc::Bind(&TransportController::SetLocalTransportDescription_n, this, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 166 | transport_name, tdesc, action, err)); |
| 167 | } |
| 168 | |
| 169 | bool TransportController::SetRemoteTransportDescription( |
| 170 | const std::string& transport_name, |
| 171 | const TransportDescription& tdesc, |
| 172 | ContentAction action, |
| 173 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 174 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 175 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 176 | rtc::Bind(&TransportController::SetRemoteTransportDescription_n, this, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 177 | transport_name, tdesc, action, err)); |
| 178 | } |
| 179 | |
| 180 | void TransportController::MaybeStartGathering() { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 181 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 182 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 183 | rtc::Bind(&TransportController::MaybeStartGathering_n, this)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool TransportController::AddRemoteCandidates(const std::string& transport_name, |
| 187 | const Candidates& candidates, |
| 188 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 189 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 190 | RTC_FROM_HERE, rtc::Bind(&TransportController::AddRemoteCandidates_n, |
| 191 | this, transport_name, candidates, err)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 194 | bool TransportController::RemoveRemoteCandidates(const Candidates& candidates, |
| 195 | std::string* err) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 196 | return network_thread_->Invoke<bool>( |
| 197 | RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n, |
| 198 | this, candidates, err)); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 199 | } |
| 200 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 201 | bool TransportController::ReadyForRemoteCandidates( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 202 | const std::string& transport_name) const { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 203 | return network_thread_->Invoke<bool>( |
| 204 | RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n, |
| 205 | this, transport_name)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool TransportController::GetStats(const std::string& transport_name, |
| 209 | TransportStats* stats) { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 210 | if (network_thread_->IsCurrent()) { |
| 211 | return GetStats_n(transport_name, stats); |
| 212 | } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 213 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 214 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 215 | rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 216 | } |
| 217 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 218 | void TransportController::SetMetricsObserver( |
| 219 | webrtc::MetricsObserverInterface* metrics_observer) { |
| 220 | return network_thread_->Invoke<void>( |
| 221 | RTC_FROM_HERE, rtc::Bind(&TransportController::SetMetricsObserver_n, this, |
| 222 | metrics_observer)); |
| 223 | } |
| 224 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 225 | DtlsTransportInternal* TransportController::CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 226 | const std::string& transport_name, |
| 227 | int component) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 228 | return network_thread_->Invoke<DtlsTransportInternal*>( |
| 229 | RTC_FROM_HERE, rtc::Bind(&TransportController::CreateDtlsTransport_n, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 230 | this, transport_name, component)); |
| 231 | } |
| 232 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 233 | DtlsTransportInternal* TransportController::CreateDtlsTransport_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 234 | const std::string& transport_name, |
| 235 | int component) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 236 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 237 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 238 | RefCountedChannel* existing_channel = GetChannel_n(transport_name, component); |
| 239 | if (existing_channel) { |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 240 | // Channel already exists; increment reference count and return. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 241 | existing_channel->AddRef(); |
| 242 | return existing_channel->dtls(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Need to create a new channel. |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 246 | JsepTransport* transport = GetOrCreateJsepTransport(transport_name); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 247 | |
| 248 | // Create DTLS channel wrapping ICE channel, and configure it. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 249 | IceTransportInternal* ice = |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 250 | CreateIceTransportChannel_n(transport_name, component); |
| 251 | // TODO(deadbeef): To support QUIC, would need to create a |
| 252 | // QuicTransportChannel here. What is "dtls" in this file would then become |
| 253 | // "dtls or quic". |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 254 | DtlsTransportInternal* dtls = |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 255 | CreateDtlsTransportChannel_n(transport_name, component, ice); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 256 | dtls->ice_transport()->SetMetricsObserver(metrics_observer_); |
| 257 | dtls->ice_transport()->SetIceRole(ice_role_); |
| 258 | dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_); |
| 259 | dtls->ice_transport()->SetIceConfig(ice_config_); |
zhihuang | b19012e | 2017-09-19 13:47:59 -0700 | [diff] [blame] | 260 | if (certificate_) { |
| 261 | bool set_cert_success = dtls->SetLocalCertificate(certificate_); |
| 262 | RTC_DCHECK(set_cert_success); |
| 263 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 264 | |
| 265 | // Connect to signals offered by the channels. Currently, the DTLS channel |
| 266 | // forwards signals from the ICE channel, so we only need to connect to the |
| 267 | // DTLS channel. In the future this won't be the case. |
| 268 | dtls->SignalWritableState.connect( |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 269 | this, &TransportController::OnChannelWritableState_n); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 270 | dtls->SignalReceivingState.connect( |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 271 | this, &TransportController::OnChannelReceivingState_n); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 272 | dtls->SignalDtlsHandshakeError.connect( |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 273 | this, &TransportController::OnDtlsHandshakeError); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 274 | dtls->ice_transport()->SignalGatheringState.connect( |
| 275 | this, &TransportController::OnChannelGatheringState_n); |
| 276 | dtls->ice_transport()->SignalCandidateGathered.connect( |
| 277 | this, &TransportController::OnChannelCandidateGathered_n); |
| 278 | dtls->ice_transport()->SignalCandidatesRemoved.connect( |
| 279 | this, &TransportController::OnChannelCandidatesRemoved_n); |
| 280 | dtls->ice_transport()->SignalRoleConflict.connect( |
| 281 | this, &TransportController::OnChannelRoleConflict_n); |
| 282 | dtls->ice_transport()->SignalStateChanged.connect( |
| 283 | this, &TransportController::OnChannelStateChanged_n); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 284 | RefCountedChannel* new_pair = new RefCountedChannel(dtls, ice); |
| 285 | new_pair->AddRef(); |
| 286 | channels_.insert(channels_.end(), new_pair); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 287 | bool channel_added = transport->AddChannel(dtls, component); |
| 288 | RTC_DCHECK(channel_added); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 289 | // Adding a channel could cause aggregate state to change. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 290 | UpdateAggregateStates_n(); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 291 | return dtls; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 292 | } |
| 293 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 294 | void TransportController::DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 295 | const std::string& transport_name, |
| 296 | int component) { |
| 297 | network_thread_->Invoke<void>( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 298 | RTC_FROM_HERE, rtc::Bind(&TransportController::DestroyDtlsTransport_n, |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 299 | this, transport_name, component)); |
| 300 | } |
| 301 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 302 | void TransportController::DestroyDtlsTransport_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 303 | const std::string& transport_name, |
| 304 | int component) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 305 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 306 | auto it = GetChannelIterator_n(transport_name, component); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 307 | if (it == channels_.end()) { |
| 308 | LOG(LS_WARNING) << "Attempting to delete " << transport_name |
| 309 | << " TransportChannel " << component |
| 310 | << ", which doesn't exist."; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 313 | if ((*it)->Release() > 0) { |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 314 | return; |
| 315 | } |
deadbeef | 57fd726 | 2016-12-06 15:28:55 -0800 | [diff] [blame] | 316 | channels_.erase(it); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 317 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 318 | JsepTransport* t = GetJsepTransport(transport_name); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 319 | bool channel_removed = t->RemoveChannel(component); |
| 320 | RTC_DCHECK(channel_removed); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 321 | // Just as we create a Transport when its first channel is created, |
| 322 | // we delete it when its last channel is deleted. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 323 | if (!t->HasChannels()) { |
| 324 | transports_.erase(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 325 | } |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 326 | // Removing a channel could cause aggregate state to change. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 327 | UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 328 | } |
| 329 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 330 | std::vector<std::string> TransportController::transport_names_for_testing() { |
| 331 | std::vector<std::string> ret; |
| 332 | for (const auto& kv : transports_) { |
| 333 | ret.push_back(kv.first); |
mikescarlett | e774867 | 2016-04-29 20:20:54 -0700 | [diff] [blame] | 334 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 335 | return ret; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 336 | } |
| 337 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 338 | std::vector<DtlsTransportInternal*> |
| 339 | TransportController::channels_for_testing() { |
| 340 | std::vector<DtlsTransportInternal*> ret; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 341 | for (RefCountedChannel* channel : channels_) { |
| 342 | ret.push_back(channel->dtls()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 343 | } |
| 344 | return ret; |
| 345 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 346 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 347 | DtlsTransportInternal* TransportController::get_channel_for_testing( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 348 | const std::string& transport_name, |
| 349 | int component) { |
| 350 | RefCountedChannel* ch = GetChannel_n(transport_name, component); |
| 351 | return ch ? ch->dtls() : nullptr; |
| 352 | } |
| 353 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 354 | IceTransportInternal* TransportController::CreateIceTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 355 | const std::string& transport_name, |
| 356 | int component) { |
| 357 | return new P2PTransportChannel(transport_name, component, port_allocator_); |
| 358 | } |
| 359 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 360 | DtlsTransportInternal* TransportController::CreateDtlsTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 361 | const std::string&, |
| 362 | int, |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 363 | IceTransportInternal* ice) { |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 364 | DtlsTransport* dtls = new DtlsTransport(ice, crypto_options_); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 365 | dtls->SetSslMaxProtocolVersion(ssl_max_version_); |
| 366 | return dtls; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void TransportController::OnMessage(rtc::Message* pmsg) { |
| 370 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 371 | |
| 372 | switch (pmsg->message_id) { |
| 373 | case MSG_ICECONNECTIONSTATE: { |
| 374 | rtc::TypedMessageData<IceConnectionState>* data = |
| 375 | static_cast<rtc::TypedMessageData<IceConnectionState>*>(pmsg->pdata); |
| 376 | SignalConnectionState(data->data()); |
| 377 | delete data; |
| 378 | break; |
| 379 | } |
| 380 | case MSG_RECEIVING: { |
| 381 | rtc::TypedMessageData<bool>* data = |
| 382 | static_cast<rtc::TypedMessageData<bool>*>(pmsg->pdata); |
| 383 | SignalReceiving(data->data()); |
| 384 | delete data; |
| 385 | break; |
| 386 | } |
| 387 | case MSG_ICEGATHERINGSTATE: { |
| 388 | rtc::TypedMessageData<IceGatheringState>* data = |
| 389 | static_cast<rtc::TypedMessageData<IceGatheringState>*>(pmsg->pdata); |
| 390 | SignalGatheringState(data->data()); |
| 391 | delete data; |
| 392 | break; |
| 393 | } |
| 394 | case MSG_CANDIDATESGATHERED: { |
| 395 | CandidatesData* data = static_cast<CandidatesData*>(pmsg->pdata); |
| 396 | SignalCandidatesGathered(data->transport_name, data->candidates); |
| 397 | delete data; |
| 398 | break; |
| 399 | } |
| 400 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 401 | RTC_NOTREACHED(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 405 | std::vector<TransportController::RefCountedChannel*>::iterator |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 406 | TransportController::GetChannelIterator_n(const std::string& transport_name, |
| 407 | int component) { |
| 408 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 409 | return std::find_if(channels_.begin(), channels_.end(), |
| 410 | [transport_name, component](RefCountedChannel* channel) { |
| 411 | return channel->dtls()->transport_name() == |
| 412 | transport_name && |
| 413 | channel->dtls()->component() == component; |
| 414 | }); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 415 | } |
| 416 | |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 417 | std::vector<TransportController::RefCountedChannel*>::const_iterator |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 418 | TransportController::GetChannelIterator_n(const std::string& transport_name, |
| 419 | int component) const { |
| 420 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 421 | return std::find_if( |
| 422 | channels_.begin(), channels_.end(), |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 423 | [transport_name, component](const RefCountedChannel* channel) { |
| 424 | return channel->dtls()->transport_name() == transport_name && |
| 425 | channel->dtls()->component() == component; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 426 | }); |
| 427 | } |
| 428 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 429 | const JsepTransport* TransportController::GetJsepTransport( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 430 | const std::string& transport_name) const { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 431 | auto it = transports_.find(transport_name); |
| 432 | return (it == transports_.end()) ? nullptr : it->second.get(); |
| 433 | } |
| 434 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 435 | JsepTransport* TransportController::GetJsepTransport( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 436 | const std::string& transport_name) { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 437 | auto it = transports_.find(transport_name); |
| 438 | return (it == transports_.end()) ? nullptr : it->second.get(); |
| 439 | } |
| 440 | |
| 441 | const TransportController::RefCountedChannel* TransportController::GetChannel_n( |
| 442 | const std::string& transport_name, |
| 443 | int component) const { |
| 444 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 445 | auto it = GetChannelIterator_n(transport_name, component); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 446 | return (it == channels_.end()) ? nullptr : *it; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | TransportController::RefCountedChannel* TransportController::GetChannel_n( |
| 450 | const std::string& transport_name, |
| 451 | int component) { |
| 452 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 453 | auto it = GetChannelIterator_n(transport_name, component); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 454 | return (it == channels_.end()) ? nullptr : *it; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 455 | } |
| 456 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 457 | JsepTransport* TransportController::GetOrCreateJsepTransport( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 458 | const std::string& transport_name) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 459 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 460 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 461 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 462 | if (transport) { |
| 463 | return transport; |
| 464 | } |
| 465 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 466 | transport = new JsepTransport(transport_name, certificate_); |
| 467 | transports_[transport_name] = std::unique_ptr<JsepTransport>(transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 468 | return transport; |
| 469 | } |
| 470 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 471 | void TransportController::DestroyAllChannels_n() { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 472 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 473 | transports_.clear(); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 474 | for (RefCountedChannel* channel : channels_) { |
| 475 | // Even though these objects are normally ref-counted, if |
| 476 | // TransportController is deleted while they still have references, just |
| 477 | // remove all references. |
| 478 | while (channel->Release() > 0) { |
| 479 | } |
| 480 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 481 | channels_.clear(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 484 | bool TransportController::SetSslMaxProtocolVersion_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 485 | rtc::SSLProtocolVersion version) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 486 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 487 | |
| 488 | // Max SSL version can only be set before transports are created. |
| 489 | if (!transports_.empty()) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | ssl_max_version_ = version; |
| 494 | return true; |
| 495 | } |
| 496 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 497 | void TransportController::SetIceConfig_n(const IceConfig& config) { |
| 498 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 499 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 500 | ice_config_ = config; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 501 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 502 | channel->dtls()->ice_transport()->SetIceConfig(ice_config_); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 506 | void TransportController::SetIceRole_n(IceRole ice_role) { |
| 507 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 508 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 509 | ice_role_ = ice_role; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 510 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 511 | channel->dtls()->ice_transport()->SetIceRole(ice_role_); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 515 | bool TransportController::GetSslRole_n(const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 516 | rtc::SSLRole* role) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 517 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 518 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 519 | const JsepTransport* t = GetJsepTransport(transport_name); |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 520 | if (!t) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 521 | return false; |
| 522 | } |
deadbeef | d8cfa1a | 2017-03-27 10:33:26 -0700 | [diff] [blame] | 523 | rtc::Optional<rtc::SSLRole> current_role = t->GetSslRole(); |
| 524 | if (!current_role) { |
| 525 | return false; |
| 526 | } |
| 527 | *role = *current_role; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 528 | return true; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 531 | bool TransportController::SetLocalCertificate_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 532 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 533 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 534 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 535 | // Can't change a certificate, or set a null certificate. |
| 536 | if (certificate_ || !certificate) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | certificate_ = certificate; |
| 540 | |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 541 | // Set certificate for JsepTransport, which verifies it matches the |
zhihuang | b19012e | 2017-09-19 13:47:59 -0700 | [diff] [blame] | 542 | // fingerprint in SDP, and DTLS transport. |
| 543 | // Fallback from DTLS to SDES is not supported. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 544 | for (auto& kv : transports_) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 545 | kv.second->SetLocalCertificate(certificate_); |
| 546 | } |
zhihuang | b19012e | 2017-09-19 13:47:59 -0700 | [diff] [blame] | 547 | for (auto& channel : channels_) { |
| 548 | bool set_cert_success = channel->dtls()->SetLocalCertificate(certificate_); |
| 549 | RTC_DCHECK(set_cert_success); |
| 550 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 554 | bool TransportController::GetLocalCertificate_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 555 | const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 556 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 557 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 558 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 559 | const JsepTransport* t = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 560 | if (!t) { |
| 561 | return false; |
| 562 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 563 | return t->GetLocalCertificate(certificate); |
| 564 | } |
| 565 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 566 | std::unique_ptr<rtc::SSLCertificate> |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 567 | TransportController::GetRemoteSSLCertificate_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 568 | const std::string& transport_name) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 569 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 570 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 571 | // Get the certificate from the RTP channel's DTLS handshake. Should be |
| 572 | // identical to the RTCP channel's, since they were given the same remote |
| 573 | // fingerprint. |
| 574 | const RefCountedChannel* ch = GetChannel_n(transport_name, 1); |
| 575 | if (!ch) { |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 576 | return nullptr; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 577 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 578 | return ch->dtls()->GetRemoteSSLCertificate(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 581 | bool TransportController::SetLocalTransportDescription_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 582 | const std::string& transport_name, |
| 583 | const TransportDescription& tdesc, |
| 584 | ContentAction action, |
| 585 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 586 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 587 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 588 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 589 | if (!transport) { |
| 590 | // If we didn't find a transport, that's not an error; |
| 591 | // it could have been deleted as a result of bundling. |
| 592 | // TODO(deadbeef): Make callers smarter so they won't attempt to set a |
| 593 | // description on a deleted transport. |
| 594 | return true; |
| 595 | } |
| 596 | |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 597 | // Older versions of Chrome expect the ICE role to be re-determined when an |
| 598 | // ICE restart occurs, and also don't perform conflict resolution correctly, |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 599 | // so for now we can't safely stop doing this, unless the application opts in |
| 600 | // by setting |redetermine_role_on_ice_restart_| to false. |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 601 | // See: https://bugs.chromium.org/p/chromium/issues/detail?id=628676 |
| 602 | // TODO(deadbeef): Remove this when these old versions of Chrome reach a low |
| 603 | // enough population. |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 604 | if (redetermine_role_on_ice_restart_ && transport->local_description() && |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 605 | IceCredentialsChanged(transport->local_description()->ice_ufrag, |
| 606 | transport->local_description()->ice_pwd, |
deadbeef | 897d08e | 2017-04-20 00:57:25 -0700 | [diff] [blame] | 607 | tdesc.ice_ufrag, tdesc.ice_pwd) && |
| 608 | // Don't change the ICE role if the remote endpoint is ICE lite; we |
| 609 | // should always be controlling in that case. |
| 610 | (!transport->remote_description() || |
| 611 | transport->remote_description()->ice_mode != ICEMODE_LITE)) { |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 612 | IceRole new_ice_role = |
| 613 | (action == CA_OFFER) ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED; |
| 614 | SetIceRole(new_ice_role); |
| 615 | } |
| 616 | |
Honghai Zhang | 9ecb085 | 2016-09-15 16:41:04 -0700 | [diff] [blame] | 617 | LOG(LS_INFO) << "Set local transport description on " << transport_name; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 618 | return transport->SetLocalTransportDescription(tdesc, action, err); |
| 619 | } |
| 620 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 621 | bool TransportController::SetRemoteTransportDescription_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 622 | const std::string& transport_name, |
| 623 | const TransportDescription& tdesc, |
| 624 | ContentAction action, |
| 625 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 626 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 627 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 628 | // If our role is ICEROLE_CONTROLLED and the remote endpoint supports only |
| 629 | // ice_lite, this local endpoint should take the CONTROLLING role. |
| 630 | // TODO(deadbeef): This is a session-level attribute, so it really shouldn't |
| 631 | // be in a TransportDescription in the first place... |
| 632 | if (ice_role_ == ICEROLE_CONTROLLED && tdesc.ice_mode == ICEMODE_LITE) { |
| 633 | SetIceRole_n(ICEROLE_CONTROLLING); |
| 634 | } |
| 635 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 636 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 637 | if (!transport) { |
| 638 | // If we didn't find a transport, that's not an error; |
| 639 | // it could have been deleted as a result of bundling. |
| 640 | // TODO(deadbeef): Make callers smarter so they won't attempt to set a |
| 641 | // description on a deleted transport. |
| 642 | return true; |
| 643 | } |
| 644 | |
Honghai Zhang | 9ecb085 | 2016-09-15 16:41:04 -0700 | [diff] [blame] | 645 | LOG(LS_INFO) << "Set remote transport description on " << transport_name; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 646 | return transport->SetRemoteTransportDescription(tdesc, action, err); |
| 647 | } |
| 648 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 649 | void TransportController::MaybeStartGathering_n() { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 650 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 651 | channel->dtls()->ice_transport()->MaybeStartGathering(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 655 | bool TransportController::AddRemoteCandidates_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 656 | const std::string& transport_name, |
| 657 | const Candidates& candidates, |
| 658 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 659 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 660 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 661 | // Verify each candidate before passing down to the transport layer. |
| 662 | if (!VerifyCandidates(candidates, err)) { |
| 663 | return false; |
| 664 | } |
| 665 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 666 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 667 | if (!transport) { |
| 668 | // If we didn't find a transport, that's not an error; |
| 669 | // it could have been deleted as a result of bundling. |
| 670 | return true; |
| 671 | } |
| 672 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 673 | for (const Candidate& candidate : candidates) { |
| 674 | RefCountedChannel* channel = |
| 675 | GetChannel_n(transport_name, candidate.component()); |
| 676 | if (!channel) { |
| 677 | *err = "Candidate has an unknown component: " + candidate.ToString() + |
| 678 | " for content: " + transport_name; |
| 679 | return false; |
| 680 | } |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 681 | channel->dtls()->ice_transport()->AddRemoteCandidate(candidate); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 682 | } |
| 683 | return true; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 686 | bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 687 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 688 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 689 | |
| 690 | // Verify each candidate before passing down to the transport layer. |
| 691 | if (!VerifyCandidates(candidates, err)) { |
| 692 | return false; |
| 693 | } |
| 694 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 695 | std::map<std::string, Candidates> candidates_by_transport_name; |
| 696 | for (const Candidate& cand : candidates) { |
| 697 | RTC_DCHECK(!cand.transport_name().empty()); |
| 698 | candidates_by_transport_name[cand.transport_name()].push_back(cand); |
| 699 | } |
| 700 | |
| 701 | bool result = true; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 702 | for (const auto& kv : candidates_by_transport_name) { |
| 703 | const std::string& transport_name = kv.first; |
| 704 | const Candidates& candidates = kv.second; |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 705 | JsepTransport* transport = GetJsepTransport(transport_name); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 706 | if (!transport) { |
| 707 | // If we didn't find a transport, that's not an error; |
| 708 | // it could have been deleted as a result of bundling. |
| 709 | continue; |
| 710 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 711 | for (const Candidate& candidate : candidates) { |
| 712 | RefCountedChannel* channel = |
| 713 | GetChannel_n(transport_name, candidate.component()); |
| 714 | if (channel) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 715 | channel->dtls()->ice_transport()->RemoveRemoteCandidate(candidate); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 718 | } |
| 719 | return result; |
| 720 | } |
| 721 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 722 | bool TransportController::ReadyForRemoteCandidates_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 723 | const std::string& transport_name) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 724 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 725 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 726 | const JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 727 | if (!transport) { |
| 728 | return false; |
| 729 | } |
| 730 | return transport->ready_for_remote_candidates(); |
| 731 | } |
| 732 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 733 | bool TransportController::GetStats_n(const std::string& transport_name, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 734 | TransportStats* stats) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 735 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 736 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 737 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 738 | if (!transport) { |
| 739 | return false; |
| 740 | } |
| 741 | return transport->GetStats(stats); |
| 742 | } |
| 743 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 744 | void TransportController::SetMetricsObserver_n( |
| 745 | webrtc::MetricsObserverInterface* metrics_observer) { |
| 746 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 747 | metrics_observer_ = metrics_observer; |
| 748 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 749 | channel->dtls()->ice_transport()->SetMetricsObserver(metrics_observer); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 753 | void TransportController::OnChannelWritableState_n( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 754 | rtc::PacketTransportInternal* transport) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 755 | RTC_DCHECK(network_thread_->IsCurrent()); |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 756 | LOG(LS_INFO) << " TransportChannel " << transport->debug_name() |
| 757 | << " writability changed to " << transport->writable() << "."; |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 758 | UpdateAggregateStates_n(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 759 | } |
| 760 | |
johan | 15ca8f6 | 2016-11-01 01:47:41 -0700 | [diff] [blame] | 761 | void TransportController::OnChannelReceivingState_n( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 762 | rtc::PacketTransportInternal* transport) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 763 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 764 | UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 767 | void TransportController::OnChannelGatheringState_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 768 | IceTransportInternal* channel) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 769 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 770 | UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 773 | void TransportController::OnChannelCandidateGathered_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 774 | IceTransportInternal* channel, |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 775 | const Candidate& candidate) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 776 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 777 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 778 | // We should never signal peer-reflexive candidates. |
| 779 | if (candidate.type() == PRFLX_PORT_TYPE) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 780 | RTC_NOTREACHED(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 781 | return; |
| 782 | } |
| 783 | std::vector<Candidate> candidates; |
| 784 | candidates.push_back(candidate); |
| 785 | CandidatesData* data = |
| 786 | new CandidatesData(channel->transport_name(), candidates); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 787 | signaling_thread_->Post(RTC_FROM_HERE, this, MSG_CANDIDATESGATHERED, data); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 790 | void TransportController::OnChannelCandidatesRemoved_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 791 | IceTransportInternal* channel, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 792 | const Candidates& candidates) { |
| 793 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 794 | RTC_FROM_HERE, signaling_thread_, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 795 | rtc::Bind(&TransportController::OnChannelCandidatesRemoved, this, |
| 796 | candidates)); |
| 797 | } |
| 798 | |
| 799 | void TransportController::OnChannelCandidatesRemoved( |
| 800 | const Candidates& candidates) { |
| 801 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 802 | SignalCandidatesRemoved(candidates); |
| 803 | } |
| 804 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 805 | void TransportController::OnChannelRoleConflict_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 806 | IceTransportInternal* channel) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 807 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 1c20610 | 2016-05-27 13:34:37 -0700 | [diff] [blame] | 808 | // Note: since the role conflict is handled entirely on the network thread, |
| 809 | // we don't need to worry about role conflicts occurring on two ports at once. |
| 810 | // The first one encountered should immediately reverse the role. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 811 | IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING) |
| 812 | ? ICEROLE_CONTROLLED |
| 813 | : ICEROLE_CONTROLLING; |
deadbeef | 1c20610 | 2016-05-27 13:34:37 -0700 | [diff] [blame] | 814 | LOG(LS_INFO) << "Got role conflict; switching to " |
| 815 | << (reversed_role == ICEROLE_CONTROLLING ? "controlling" |
| 816 | : "controlled") |
| 817 | << " role."; |
| 818 | SetIceRole_n(reversed_role); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 821 | void TransportController::OnChannelStateChanged_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 822 | IceTransportInternal* channel) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 823 | RTC_DCHECK(network_thread_->IsCurrent()); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 824 | LOG(LS_INFO) << channel->transport_name() << " TransportChannel " |
| 825 | << channel->component() |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 826 | << " state changed. Check if state is complete."; |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 827 | UpdateAggregateStates_n(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 830 | void TransportController::UpdateAggregateStates_n() { |
| 831 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 832 | |
| 833 | IceConnectionState new_connection_state = kIceConnectionConnecting; |
| 834 | IceGatheringState new_gathering_state = kIceGatheringNew; |
| 835 | bool any_receiving = false; |
| 836 | bool any_failed = false; |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 837 | bool all_connected = !channels_.empty(); |
| 838 | bool all_completed = !channels_.empty(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 839 | bool any_gathering = false; |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 840 | bool all_done_gathering = !channels_.empty(); |
| 841 | for (const auto& channel : channels_) { |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 842 | any_receiving = any_receiving || channel->dtls()->receiving(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 843 | any_failed = any_failed || |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 844 | channel->dtls()->ice_transport()->GetState() == |
| 845 | IceTransportState::STATE_FAILED; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 846 | all_connected = all_connected && channel->dtls()->writable(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 847 | all_completed = |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 848 | all_completed && channel->dtls()->writable() && |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 849 | channel->dtls()->ice_transport()->GetState() == |
| 850 | IceTransportState::STATE_COMPLETED && |
| 851 | channel->dtls()->ice_transport()->GetIceRole() == ICEROLE_CONTROLLING && |
| 852 | channel->dtls()->ice_transport()->gathering_state() == |
| 853 | kIceGatheringComplete; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 854 | any_gathering = |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 855 | any_gathering || |
| 856 | channel->dtls()->ice_transport()->gathering_state() != kIceGatheringNew; |
| 857 | all_done_gathering = all_done_gathering && |
| 858 | channel->dtls()->ice_transport()->gathering_state() == |
| 859 | kIceGatheringComplete; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 860 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 861 | if (any_failed) { |
| 862 | new_connection_state = kIceConnectionFailed; |
| 863 | } else if (all_completed) { |
| 864 | new_connection_state = kIceConnectionCompleted; |
| 865 | } else if (all_connected) { |
| 866 | new_connection_state = kIceConnectionConnected; |
| 867 | } |
| 868 | if (connection_state_ != new_connection_state) { |
| 869 | connection_state_ = new_connection_state; |
| 870 | signaling_thread_->Post( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 871 | RTC_FROM_HERE, this, MSG_ICECONNECTIONSTATE, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 872 | new rtc::TypedMessageData<IceConnectionState>(new_connection_state)); |
| 873 | } |
| 874 | |
| 875 | if (receiving_ != any_receiving) { |
| 876 | receiving_ = any_receiving; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 877 | signaling_thread_->Post(RTC_FROM_HERE, this, MSG_RECEIVING, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 878 | new rtc::TypedMessageData<bool>(any_receiving)); |
| 879 | } |
| 880 | |
| 881 | if (all_done_gathering) { |
| 882 | new_gathering_state = kIceGatheringComplete; |
| 883 | } else if (any_gathering) { |
| 884 | new_gathering_state = kIceGatheringGathering; |
| 885 | } |
| 886 | if (gathering_state_ != new_gathering_state) { |
| 887 | gathering_state_ = new_gathering_state; |
| 888 | signaling_thread_->Post( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 889 | RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 890 | new rtc::TypedMessageData<IceGatheringState>(new_gathering_state)); |
| 891 | } |
| 892 | } |
| 893 | |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 894 | void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) { |
| 895 | SignalDtlsHandshakeError(error); |
| 896 | } |
| 897 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 898 | } // namespace cricket |