henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | #include "webrtc/p2p/client/basicportallocator.h" |
| 12 | |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 17 | #include "webrtc/p2p/base/common.h" |
| 18 | #include "webrtc/p2p/base/port.h" |
| 19 | #include "webrtc/p2p/base/relayport.h" |
| 20 | #include "webrtc/p2p/base/stunport.h" |
| 21 | #include "webrtc/p2p/base/tcpport.h" |
| 22 | #include "webrtc/p2p/base/turnport.h" |
| 23 | #include "webrtc/p2p/base/udpport.h" |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 24 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 25 | #include "webrtc/base/common.h" |
| 26 | #include "webrtc/base/helpers.h" |
| 27 | #include "webrtc/base/logging.h" |
| 28 | |
| 29 | using rtc::CreateRandomId; |
| 30 | using rtc::CreateRandomString; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | enum { |
| 35 | MSG_CONFIG_START, |
| 36 | MSG_CONFIG_READY, |
| 37 | MSG_ALLOCATE, |
| 38 | MSG_ALLOCATION_PHASE, |
| 39 | MSG_SHAKE, |
| 40 | MSG_SEQUENCEOBJECTS_CREATED, |
| 41 | MSG_CONFIG_STOP, |
| 42 | }; |
| 43 | |
| 44 | const int PHASE_UDP = 0; |
| 45 | const int PHASE_RELAY = 1; |
| 46 | const int PHASE_TCP = 2; |
| 47 | const int PHASE_SSLTCP = 3; |
| 48 | |
| 49 | const int kNumPhases = 4; |
| 50 | |
| 51 | const int SHAKE_MIN_DELAY = 45 * 1000; // 45 seconds |
| 52 | const int SHAKE_MAX_DELAY = 90 * 1000; // 90 seconds |
| 53 | |
| 54 | int ShakeDelay() { |
| 55 | int range = SHAKE_MAX_DELAY - SHAKE_MIN_DELAY + 1; |
| 56 | return SHAKE_MIN_DELAY + CreateRandomId() % range; |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | namespace cricket { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | const uint32 DISABLE_ALL_PHASES = |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 63 | PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP | |
| 64 | PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | |
| 66 | // BasicPortAllocator |
| 67 | BasicPortAllocator::BasicPortAllocator( |
| 68 | rtc::NetworkManager* network_manager, |
| 69 | rtc::PacketSocketFactory* socket_factory) |
| 70 | : network_manager_(network_manager), |
eblima | 894ad94 | 2015-07-03 08:34:33 -0700 | [diff] [blame] | 71 | socket_factory_(socket_factory), |
| 72 | stun_servers_() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | ASSERT(socket_factory_ != NULL); |
| 74 | Construct(); |
| 75 | } |
| 76 | |
| 77 | BasicPortAllocator::BasicPortAllocator( |
| 78 | rtc::NetworkManager* network_manager) |
| 79 | : network_manager_(network_manager), |
eblima | 894ad94 | 2015-07-03 08:34:33 -0700 | [diff] [blame] | 80 | socket_factory_(NULL), |
| 81 | stun_servers_() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | Construct(); |
| 83 | } |
| 84 | |
| 85 | BasicPortAllocator::BasicPortAllocator( |
| 86 | rtc::NetworkManager* network_manager, |
| 87 | rtc::PacketSocketFactory* socket_factory, |
| 88 | const ServerAddresses& stun_servers) |
| 89 | : network_manager_(network_manager), |
| 90 | socket_factory_(socket_factory), |
| 91 | stun_servers_(stun_servers) { |
| 92 | ASSERT(socket_factory_ != NULL); |
| 93 | Construct(); |
| 94 | } |
| 95 | |
| 96 | BasicPortAllocator::BasicPortAllocator( |
| 97 | rtc::NetworkManager* network_manager, |
| 98 | const ServerAddresses& stun_servers, |
| 99 | const rtc::SocketAddress& relay_address_udp, |
| 100 | const rtc::SocketAddress& relay_address_tcp, |
| 101 | const rtc::SocketAddress& relay_address_ssl) |
| 102 | : network_manager_(network_manager), |
| 103 | socket_factory_(NULL), |
| 104 | stun_servers_(stun_servers) { |
| 105 | |
| 106 | RelayServerConfig config(RELAY_GTURN); |
| 107 | if (!relay_address_udp.IsNil()) |
| 108 | config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP)); |
| 109 | if (!relay_address_tcp.IsNil()) |
| 110 | config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP)); |
| 111 | if (!relay_address_ssl.IsNil()) |
| 112 | config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP)); |
| 113 | |
| 114 | if (!config.ports.empty()) |
| 115 | AddRelay(config); |
| 116 | |
| 117 | Construct(); |
| 118 | } |
| 119 | |
| 120 | void BasicPortAllocator::Construct() { |
| 121 | allow_tcp_listen_ = true; |
| 122 | } |
| 123 | |
| 124 | BasicPortAllocator::~BasicPortAllocator() { |
| 125 | } |
| 126 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 127 | PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 128 | const std::string& content_name, int component, |
| 129 | const std::string& ice_ufrag, const std::string& ice_pwd) { |
| 130 | return new BasicPortAllocatorSession( |
| 131 | this, content_name, component, ice_ufrag, ice_pwd); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // BasicPortAllocatorSession |
| 136 | BasicPortAllocatorSession::BasicPortAllocatorSession( |
| 137 | BasicPortAllocator *allocator, |
| 138 | const std::string& content_name, |
| 139 | int component, |
| 140 | const std::string& ice_ufrag, |
| 141 | const std::string& ice_pwd) |
| 142 | : PortAllocatorSession(content_name, component, |
| 143 | ice_ufrag, ice_pwd, allocator->flags()), |
| 144 | allocator_(allocator), network_thread_(NULL), |
| 145 | socket_factory_(allocator->socket_factory()), |
| 146 | allocation_started_(false), |
| 147 | network_manager_started_(false), |
| 148 | running_(false), |
| 149 | allocation_sequences_created_(false) { |
| 150 | allocator_->network_manager()->SignalNetworksChanged.connect( |
| 151 | this, &BasicPortAllocatorSession::OnNetworksChanged); |
| 152 | allocator_->network_manager()->StartUpdating(); |
| 153 | } |
| 154 | |
| 155 | BasicPortAllocatorSession::~BasicPortAllocatorSession() { |
| 156 | allocator_->network_manager()->StopUpdating(); |
| 157 | if (network_thread_ != NULL) |
| 158 | network_thread_->Clear(this); |
| 159 | |
| 160 | for (uint32 i = 0; i < sequences_.size(); ++i) { |
| 161 | // AllocationSequence should clear it's map entry for turn ports before |
| 162 | // ports are destroyed. |
| 163 | sequences_[i]->Clear(); |
| 164 | } |
| 165 | |
| 166 | std::vector<PortData>::iterator it; |
| 167 | for (it = ports_.begin(); it != ports_.end(); it++) |
| 168 | delete it->port(); |
| 169 | |
| 170 | for (uint32 i = 0; i < configs_.size(); ++i) |
| 171 | delete configs_[i]; |
| 172 | |
| 173 | for (uint32 i = 0; i < sequences_.size(); ++i) |
| 174 | delete sequences_[i]; |
| 175 | } |
| 176 | |
| 177 | void BasicPortAllocatorSession::StartGettingPorts() { |
| 178 | network_thread_ = rtc::Thread::Current(); |
| 179 | if (!socket_factory_) { |
| 180 | owned_socket_factory_.reset( |
| 181 | new rtc::BasicPacketSocketFactory(network_thread_)); |
| 182 | socket_factory_ = owned_socket_factory_.get(); |
| 183 | } |
| 184 | |
| 185 | running_ = true; |
| 186 | network_thread_->Post(this, MSG_CONFIG_START); |
| 187 | |
| 188 | if (flags() & PORTALLOCATOR_ENABLE_SHAKER) |
| 189 | network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE); |
| 190 | } |
| 191 | |
| 192 | void BasicPortAllocatorSession::StopGettingPorts() { |
| 193 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 194 | running_ = false; |
| 195 | network_thread_->Clear(this, MSG_ALLOCATE); |
| 196 | for (uint32 i = 0; i < sequences_.size(); ++i) |
| 197 | sequences_[i]->Stop(); |
| 198 | network_thread_->Post(this, MSG_CONFIG_STOP); |
| 199 | } |
| 200 | |
| 201 | void BasicPortAllocatorSession::OnMessage(rtc::Message *message) { |
| 202 | switch (message->message_id) { |
| 203 | case MSG_CONFIG_START: |
| 204 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 205 | GetPortConfigurations(); |
| 206 | break; |
| 207 | |
| 208 | case MSG_CONFIG_READY: |
| 209 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 210 | OnConfigReady(static_cast<PortConfiguration*>(message->pdata)); |
| 211 | break; |
| 212 | |
| 213 | case MSG_ALLOCATE: |
| 214 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 215 | OnAllocate(); |
| 216 | break; |
| 217 | |
| 218 | case MSG_SHAKE: |
| 219 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 220 | OnShake(); |
| 221 | break; |
| 222 | case MSG_SEQUENCEOBJECTS_CREATED: |
| 223 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 224 | OnAllocationSequenceObjectsCreated(); |
| 225 | break; |
| 226 | case MSG_CONFIG_STOP: |
| 227 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 228 | OnConfigStop(); |
| 229 | break; |
| 230 | default: |
| 231 | ASSERT(false); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void BasicPortAllocatorSession::GetPortConfigurations() { |
| 236 | PortConfiguration* config = new PortConfiguration(allocator_->stun_servers(), |
| 237 | username(), |
| 238 | password()); |
| 239 | |
| 240 | for (size_t i = 0; i < allocator_->relays().size(); ++i) { |
| 241 | config->AddRelay(allocator_->relays()[i]); |
| 242 | } |
| 243 | ConfigReady(config); |
| 244 | } |
| 245 | |
| 246 | void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) { |
| 247 | network_thread_->Post(this, MSG_CONFIG_READY, config); |
| 248 | } |
| 249 | |
| 250 | // Adds a configuration to the list. |
| 251 | void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) { |
| 252 | if (config) |
| 253 | configs_.push_back(config); |
| 254 | |
| 255 | AllocatePorts(); |
| 256 | } |
| 257 | |
| 258 | void BasicPortAllocatorSession::OnConfigStop() { |
| 259 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 260 | |
| 261 | // If any of the allocated ports have not completed the candidates allocation, |
| 262 | // mark those as error. Since session doesn't need any new candidates |
| 263 | // at this stage of the allocation, it's safe to discard any new candidates. |
| 264 | bool send_signal = false; |
| 265 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 266 | it != ports_.end(); ++it) { |
| 267 | if (!it->complete()) { |
| 268 | // Updating port state to error, which didn't finish allocating candidates |
| 269 | // yet. |
| 270 | it->set_error(); |
| 271 | send_signal = true; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Did we stop any running sequences? |
| 276 | for (std::vector<AllocationSequence*>::iterator it = sequences_.begin(); |
| 277 | it != sequences_.end() && !send_signal; ++it) { |
| 278 | if ((*it)->state() == AllocationSequence::kStopped) { |
| 279 | send_signal = true; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // If we stopped anything that was running, send a done signal now. |
| 284 | if (send_signal) { |
| 285 | MaybeSignalCandidatesAllocationDone(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void BasicPortAllocatorSession::AllocatePorts() { |
| 290 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 291 | network_thread_->Post(this, MSG_ALLOCATE); |
| 292 | } |
| 293 | |
| 294 | void BasicPortAllocatorSession::OnAllocate() { |
| 295 | if (network_manager_started_) |
| 296 | DoAllocate(); |
| 297 | |
| 298 | allocation_started_ = true; |
| 299 | } |
| 300 | |
| 301 | // For each network, see if we have a sequence that covers it already. If not, |
| 302 | // create a new sequence to create the appropriate ports. |
| 303 | void BasicPortAllocatorSession::DoAllocate() { |
| 304 | bool done_signal_needed = false; |
| 305 | std::vector<rtc::Network*> networks; |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 306 | |
| 307 | // If the adapter enumeration is disabled, we'll just bind to any address |
| 308 | // instead of specific NIC. This is to ensure the same routing for http |
| 309 | // traffic by OS is also used here to avoid any local or public IP leakage |
| 310 | // during stun process. |
| 311 | if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { |
| 312 | allocator_->network_manager()->GetAnyAddressNetworks(&networks); |
| 313 | } else { |
| 314 | allocator_->network_manager()->GetNetworks(&networks); |
| 315 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 316 | if (networks.empty()) { |
| 317 | LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated"; |
| 318 | done_signal_needed = true; |
| 319 | } else { |
| 320 | for (uint32 i = 0; i < networks.size(); ++i) { |
| 321 | PortConfiguration* config = NULL; |
| 322 | if (configs_.size() > 0) |
| 323 | config = configs_.back(); |
| 324 | |
| 325 | uint32 sequence_flags = flags(); |
| 326 | if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
| 327 | // If all the ports are disabled we should just fire the allocation |
| 328 | // done event and return. |
| 329 | done_signal_needed = true; |
| 330 | break; |
| 331 | } |
| 332 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 333 | if (!config || config->relays.empty()) { |
| 334 | // No relay ports specified in this config. |
| 335 | sequence_flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 336 | } |
| 337 | |
| 338 | if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 339 | networks[i]->GetBestIP().family() == AF_INET6) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 340 | // Skip IPv6 networks unless the flag's been set. |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | // Disable phases that would only create ports equivalent to |
| 345 | // ones that we have already made. |
| 346 | DisableEquivalentPhases(networks[i], config, &sequence_flags); |
| 347 | |
| 348 | if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
| 349 | // New AllocationSequence would have nothing to do, so don't make it. |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | AllocationSequence* sequence = |
| 354 | new AllocationSequence(this, networks[i], config, sequence_flags); |
| 355 | if (!sequence->Init()) { |
| 356 | delete sequence; |
| 357 | continue; |
| 358 | } |
| 359 | done_signal_needed = true; |
| 360 | sequence->SignalPortAllocationComplete.connect( |
| 361 | this, &BasicPortAllocatorSession::OnPortAllocationComplete); |
| 362 | if (running_) |
| 363 | sequence->Start(); |
| 364 | sequences_.push_back(sequence); |
| 365 | } |
| 366 | } |
| 367 | if (done_signal_needed) { |
| 368 | network_thread_->Post(this, MSG_SEQUENCEOBJECTS_CREATED); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void BasicPortAllocatorSession::OnNetworksChanged() { |
| 373 | network_manager_started_ = true; |
| 374 | if (allocation_started_) |
| 375 | DoAllocate(); |
| 376 | } |
| 377 | |
| 378 | void BasicPortAllocatorSession::DisableEquivalentPhases( |
| 379 | rtc::Network* network, PortConfiguration* config, uint32* flags) { |
| 380 | for (uint32 i = 0; i < sequences_.size() && |
| 381 | (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; ++i) { |
| 382 | sequences_[i]->DisableEquivalentPhases(network, config, flags); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void BasicPortAllocatorSession::AddAllocatedPort(Port* port, |
| 387 | AllocationSequence * seq, |
| 388 | bool prepare_address) { |
| 389 | if (!port) |
| 390 | return; |
| 391 | |
| 392 | LOG(LS_INFO) << "Adding allocated port for " << content_name(); |
| 393 | port->set_content_name(content_name()); |
| 394 | port->set_component(component_); |
| 395 | port->set_generation(generation()); |
| 396 | if (allocator_->proxy().type != rtc::PROXY_NONE) |
| 397 | port->set_proxy(allocator_->user_agent(), allocator_->proxy()); |
| 398 | port->set_send_retransmit_count_attribute((allocator_->flags() & |
| 399 | PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0); |
| 400 | |
| 401 | // Push down the candidate_filter to individual port. |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 402 | uint32 candidate_filter = allocator_->candidate_filter(); |
| 403 | |
| 404 | // When adapter enumeration is disabled, disable CF_HOST at port level so |
| 405 | // local address is not leaked by stunport in the candidate's related address. |
| 406 | if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { |
| 407 | candidate_filter &= ~CF_HOST; |
| 408 | } |
| 409 | port->set_candidate_filter(candidate_filter); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 410 | |
| 411 | PortData data(port, seq); |
| 412 | ports_.push_back(data); |
| 413 | |
| 414 | port->SignalCandidateReady.connect( |
| 415 | this, &BasicPortAllocatorSession::OnCandidateReady); |
| 416 | port->SignalPortComplete.connect(this, |
| 417 | &BasicPortAllocatorSession::OnPortComplete); |
| 418 | port->SignalDestroyed.connect(this, |
| 419 | &BasicPortAllocatorSession::OnPortDestroyed); |
| 420 | port->SignalPortError.connect( |
| 421 | this, &BasicPortAllocatorSession::OnPortError); |
| 422 | LOG_J(LS_INFO, port) << "Added port to allocator"; |
| 423 | |
| 424 | if (prepare_address) |
| 425 | port->PrepareAddress(); |
| 426 | } |
| 427 | |
| 428 | void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() { |
| 429 | allocation_sequences_created_ = true; |
| 430 | // Send candidate allocation complete signal if we have no sequences. |
| 431 | MaybeSignalCandidatesAllocationDone(); |
| 432 | } |
| 433 | |
| 434 | void BasicPortAllocatorSession::OnCandidateReady( |
| 435 | Port* port, const Candidate& c) { |
| 436 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 437 | PortData* data = FindPort(port); |
| 438 | ASSERT(data != NULL); |
| 439 | // Discarding any candidate signal if port allocation status is |
| 440 | // already in completed state. |
| 441 | if (data->complete()) |
| 442 | return; |
| 443 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 444 | ProtocolType pvalue; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 445 | bool candidate_signalable = CheckCandidateFilter(c); |
| 446 | bool candidate_pairable = |
| 447 | candidate_signalable || |
| 448 | (c.address().IsAnyIP() && |
| 449 | (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME)); |
| 450 | bool candidate_protocol_enabled = |
| 451 | StringToProto(c.protocol().c_str(), &pvalue) && |
| 452 | data->sequence()->ProtocolEnabled(pvalue); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 453 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 454 | if (candidate_signalable && candidate_protocol_enabled) { |
| 455 | std::vector<Candidate> candidates; |
| 456 | candidates.push_back(c); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 457 | SignalCandidatesReady(this, candidates); |
| 458 | } |
| 459 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 460 | // Port has been made ready. Nothing to do here. |
| 461 | if (data->ready()) { |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | // Move the port to the READY state, either because we have a usable candidate |
| 466 | // from the port, or simply because the port is bound to the any address and |
| 467 | // therefore has no host candidate. This will trigger the port to start |
| 468 | // creating candidate pairs (connections) and issue connectivity checks. |
| 469 | if (candidate_pairable) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 470 | data->set_ready(); |
| 471 | SignalPortReady(this, port); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void BasicPortAllocatorSession::OnPortComplete(Port* port) { |
| 476 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 477 | PortData* data = FindPort(port); |
| 478 | ASSERT(data != NULL); |
| 479 | |
| 480 | // Ignore any late signals. |
| 481 | if (data->complete()) |
| 482 | return; |
| 483 | |
| 484 | // Moving to COMPLETE state. |
| 485 | data->set_complete(); |
| 486 | // Send candidate allocation complete signal if this was the last port. |
| 487 | MaybeSignalCandidatesAllocationDone(); |
| 488 | } |
| 489 | |
| 490 | void BasicPortAllocatorSession::OnPortError(Port* port) { |
| 491 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 492 | PortData* data = FindPort(port); |
| 493 | ASSERT(data != NULL); |
| 494 | // We might have already given up on this port and stopped it. |
| 495 | if (data->complete()) |
| 496 | return; |
| 497 | |
| 498 | // SignalAddressError is currently sent from StunPort/TurnPort. |
| 499 | // But this signal itself is generic. |
| 500 | data->set_error(); |
| 501 | // Send candidate allocation complete signal if this was the last port. |
| 502 | MaybeSignalCandidatesAllocationDone(); |
| 503 | } |
| 504 | |
| 505 | void BasicPortAllocatorSession::OnProtocolEnabled(AllocationSequence* seq, |
| 506 | ProtocolType proto) { |
| 507 | std::vector<Candidate> candidates; |
| 508 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 509 | it != ports_.end(); ++it) { |
| 510 | if (it->sequence() != seq) |
| 511 | continue; |
| 512 | |
| 513 | const std::vector<Candidate>& potentials = it->port()->Candidates(); |
| 514 | for (size_t i = 0; i < potentials.size(); ++i) { |
| 515 | if (!CheckCandidateFilter(potentials[i])) |
| 516 | continue; |
| 517 | ProtocolType pvalue; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 518 | bool candidate_protocol_enabled = |
| 519 | StringToProto(potentials[i].protocol().c_str(), &pvalue) && |
| 520 | pvalue == proto; |
| 521 | if (candidate_protocol_enabled) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 522 | candidates.push_back(potentials[i]); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (!candidates.empty()) { |
| 528 | SignalCandidatesReady(this, candidates); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) { |
| 533 | uint32 filter = allocator_->candidate_filter(); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 534 | |
| 535 | // When binding to any address, before sending packets out, the getsockname |
| 536 | // returns all 0s, but after sending packets, it'll be the NIC used to |
| 537 | // send. All 0s is not a valid ICE candidate address and should be filtered |
| 538 | // out. |
| 539 | if (c.address().IsAnyIP()) { |
| 540 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 541 | } |
| 542 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 543 | if (c.type() == RELAY_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 544 | return ((filter & CF_RELAY) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 545 | } else if (c.type() == STUN_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 546 | return ((filter & CF_REFLEXIVE) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 547 | } else if (c.type() == LOCAL_PORT_TYPE) { |
| 548 | if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) { |
| 549 | // We allow host candidates if the filter allows server-reflexive |
| 550 | // candidates and the candidate is a public IP. Because we don't generate |
| 551 | // server-reflexive candidates if they have the same IP as the host |
| 552 | // candidate (i.e. when the host candidate is a public IP), filtering to |
| 553 | // only server-reflexive candidates won't work right when the host |
| 554 | // candidates have public IPs. |
| 555 | return true; |
| 556 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 557 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 558 | // This is just to prevent the case when binding to any address (all 0s), if |
| 559 | // somehow the host candidate address is not all 0s. Either because local |
| 560 | // installed proxy changes the address or a packet has been sent for any |
| 561 | // reason before getsockname is called. |
| 562 | if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { |
| 563 | LOG(LS_WARNING) << "Received non-0 host address: " |
| 564 | << c.address().ToString() |
| 565 | << " when adapter enumeration is disabled"; |
| 566 | return false; |
| 567 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 568 | |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 569 | return ((filter & CF_HOST) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 570 | } |
| 571 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | void BasicPortAllocatorSession::OnPortAllocationComplete( |
| 575 | AllocationSequence* seq) { |
| 576 | // Send candidate allocation complete signal if all ports are done. |
| 577 | MaybeSignalCandidatesAllocationDone(); |
| 578 | } |
| 579 | |
| 580 | void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() { |
| 581 | // Send signal only if all required AllocationSequence objects |
| 582 | // are created. |
| 583 | if (!allocation_sequences_created_) |
| 584 | return; |
| 585 | |
| 586 | // Check that all port allocation sequences are complete. |
| 587 | for (std::vector<AllocationSequence*>::iterator it = sequences_.begin(); |
| 588 | it != sequences_.end(); ++it) { |
| 589 | if ((*it)->state() == AllocationSequence::kRunning) |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | // If all allocated ports are in complete state, session must have got all |
| 594 | // expected candidates. Session will trigger candidates allocation complete |
| 595 | // signal. |
| 596 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 597 | it != ports_.end(); ++it) { |
| 598 | if (!it->complete()) |
| 599 | return; |
| 600 | } |
| 601 | LOG(LS_INFO) << "All candidates gathered for " << content_name_ << ":" |
| 602 | << component_ << ":" << generation(); |
| 603 | SignalCandidatesAllocationDone(this); |
| 604 | } |
| 605 | |
| 606 | void BasicPortAllocatorSession::OnPortDestroyed( |
| 607 | PortInterface* port) { |
| 608 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 609 | for (std::vector<PortData>::iterator iter = ports_.begin(); |
| 610 | iter != ports_.end(); ++iter) { |
| 611 | if (port == iter->port()) { |
| 612 | ports_.erase(iter); |
| 613 | LOG_J(LS_INFO, port) << "Removed port from allocator (" |
| 614 | << static_cast<int>(ports_.size()) << " remaining)"; |
| 615 | return; |
| 616 | } |
| 617 | } |
| 618 | ASSERT(false); |
| 619 | } |
| 620 | |
| 621 | void BasicPortAllocatorSession::OnShake() { |
| 622 | LOG(INFO) << ">>>>> SHAKE <<<<< >>>>> SHAKE <<<<< >>>>> SHAKE <<<<<"; |
| 623 | |
| 624 | std::vector<Port*> ports; |
| 625 | std::vector<Connection*> connections; |
| 626 | |
| 627 | for (size_t i = 0; i < ports_.size(); ++i) { |
| 628 | if (ports_[i].ready()) |
| 629 | ports.push_back(ports_[i].port()); |
| 630 | } |
| 631 | |
| 632 | for (size_t i = 0; i < ports.size(); ++i) { |
| 633 | Port::AddressMap::const_iterator iter; |
| 634 | for (iter = ports[i]->connections().begin(); |
| 635 | iter != ports[i]->connections().end(); |
| 636 | ++iter) { |
| 637 | connections.push_back(iter->second); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | LOG(INFO) << ">>>>> Destroying " << ports.size() << " ports and " |
| 642 | << connections.size() << " connections"; |
| 643 | |
| 644 | for (size_t i = 0; i < connections.size(); ++i) |
| 645 | connections[i]->Destroy(); |
| 646 | |
| 647 | if (running_ || (ports.size() > 0) || (connections.size() > 0)) |
| 648 | network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE); |
| 649 | } |
| 650 | |
| 651 | BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort( |
| 652 | Port* port) { |
| 653 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 654 | it != ports_.end(); ++it) { |
| 655 | if (it->port() == port) { |
| 656 | return &*it; |
| 657 | } |
| 658 | } |
| 659 | return NULL; |
| 660 | } |
| 661 | |
| 662 | // AllocationSequence |
| 663 | |
| 664 | AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session, |
| 665 | rtc::Network* network, |
| 666 | PortConfiguration* config, |
| 667 | uint32 flags) |
| 668 | : session_(session), |
| 669 | network_(network), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 670 | ip_(network->GetBestIP()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 671 | config_(config), |
| 672 | state_(kInit), |
| 673 | flags_(flags), |
| 674 | udp_socket_(), |
| 675 | udp_port_(NULL), |
| 676 | phase_(0) { |
| 677 | } |
| 678 | |
| 679 | bool AllocationSequence::Init() { |
pthatcher | fa30180 | 2015-08-11 04:12:56 -0700 | [diff] [blame] | 680 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && |
| 681 | !IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_UFRAG)) { |
| 682 | LOG(LS_ERROR) << "Shared socket option can't be set without " |
| 683 | << "shared ufrag."; |
| 684 | ASSERT(false); |
| 685 | return false; |
| 686 | } |
| 687 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 688 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 689 | udp_socket_.reset(session_->socket_factory()->CreateUdpSocket( |
| 690 | rtc::SocketAddress(ip_, 0), session_->allocator()->min_port(), |
| 691 | session_->allocator()->max_port())); |
| 692 | if (udp_socket_) { |
| 693 | udp_socket_->SignalReadPacket.connect( |
| 694 | this, &AllocationSequence::OnReadPacket); |
| 695 | } |
| 696 | // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP |
| 697 | // are next available options to setup a communication channel. |
| 698 | } |
| 699 | return true; |
| 700 | } |
| 701 | |
| 702 | void AllocationSequence::Clear() { |
| 703 | udp_port_ = NULL; |
| 704 | turn_ports_.clear(); |
| 705 | } |
| 706 | |
| 707 | AllocationSequence::~AllocationSequence() { |
| 708 | session_->network_thread()->Clear(this); |
| 709 | } |
| 710 | |
| 711 | void AllocationSequence::DisableEquivalentPhases(rtc::Network* network, |
| 712 | PortConfiguration* config, uint32* flags) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 713 | if (!((network == network_) && (ip_ == network->GetBestIP()))) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 714 | // Different network setup; nothing is equivalent. |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | // Else turn off the stuff that we've already got covered. |
| 719 | |
| 720 | // Every config implicitly specifies local, so turn that off right away. |
| 721 | *flags |= PORTALLOCATOR_DISABLE_UDP; |
| 722 | *flags |= PORTALLOCATOR_DISABLE_TCP; |
| 723 | |
| 724 | if (config_ && config) { |
| 725 | if (config_->StunServers() == config->StunServers()) { |
| 726 | // Already got this STUN servers covered. |
| 727 | *flags |= PORTALLOCATOR_DISABLE_STUN; |
| 728 | } |
| 729 | if (!config_->relays.empty()) { |
| 730 | // Already got relays covered. |
| 731 | // NOTE: This will even skip a _different_ set of relay servers if we |
| 732 | // were to be given one, but that never happens in our codebase. Should |
| 733 | // probably get rid of the list in PortConfiguration and just keep a |
| 734 | // single relay server in each one. |
| 735 | *flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void AllocationSequence::Start() { |
| 741 | state_ = kRunning; |
| 742 | session_->network_thread()->Post(this, MSG_ALLOCATION_PHASE); |
| 743 | } |
| 744 | |
| 745 | void AllocationSequence::Stop() { |
| 746 | // If the port is completed, don't set it to stopped. |
| 747 | if (state_ == kRunning) { |
| 748 | state_ = kStopped; |
| 749 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | void AllocationSequence::OnMessage(rtc::Message* msg) { |
| 754 | ASSERT(rtc::Thread::Current() == session_->network_thread()); |
| 755 | ASSERT(msg->message_id == MSG_ALLOCATION_PHASE); |
| 756 | |
| 757 | const char* const PHASE_NAMES[kNumPhases] = { |
| 758 | "Udp", "Relay", "Tcp", "SslTcp" |
| 759 | }; |
| 760 | |
| 761 | // Perform all of the phases in the current step. |
| 762 | LOG_J(LS_INFO, network_) << "Allocation Phase=" |
| 763 | << PHASE_NAMES[phase_]; |
| 764 | |
| 765 | switch (phase_) { |
| 766 | case PHASE_UDP: |
| 767 | CreateUDPPorts(); |
| 768 | CreateStunPorts(); |
| 769 | EnableProtocol(PROTO_UDP); |
| 770 | break; |
| 771 | |
| 772 | case PHASE_RELAY: |
| 773 | CreateRelayPorts(); |
| 774 | break; |
| 775 | |
| 776 | case PHASE_TCP: |
| 777 | CreateTCPPorts(); |
| 778 | EnableProtocol(PROTO_TCP); |
| 779 | break; |
| 780 | |
| 781 | case PHASE_SSLTCP: |
| 782 | state_ = kCompleted; |
| 783 | EnableProtocol(PROTO_SSLTCP); |
| 784 | break; |
| 785 | |
| 786 | default: |
| 787 | ASSERT(false); |
| 788 | } |
| 789 | |
| 790 | if (state() == kRunning) { |
| 791 | ++phase_; |
| 792 | session_->network_thread()->PostDelayed( |
| 793 | session_->allocator()->step_delay(), |
| 794 | this, MSG_ALLOCATION_PHASE); |
| 795 | } else { |
| 796 | // If all phases in AllocationSequence are completed, no allocation |
| 797 | // steps needed further. Canceling pending signal. |
| 798 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 799 | SignalPortAllocationComplete(this); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | void AllocationSequence::EnableProtocol(ProtocolType proto) { |
| 804 | if (!ProtocolEnabled(proto)) { |
| 805 | protocols_.push_back(proto); |
| 806 | session_->OnProtocolEnabled(this, proto); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | bool AllocationSequence::ProtocolEnabled(ProtocolType proto) const { |
| 811 | for (ProtocolList::const_iterator it = protocols_.begin(); |
| 812 | it != protocols_.end(); ++it) { |
| 813 | if (*it == proto) |
| 814 | return true; |
| 815 | } |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | void AllocationSequence::CreateUDPPorts() { |
| 820 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) { |
| 821 | LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping."; |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | // TODO(mallinath) - Remove UDPPort creating socket after shared socket |
| 826 | // is enabled completely. |
| 827 | UDPPort* port = NULL; |
| 828 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) { |
Guo-wei Shieh | 370c884 | 2015-08-18 17:00:13 -0700 | [diff] [blame] | 829 | port = UDPPort::Create(session_->network_thread(), |
| 830 | session_->socket_factory(), network_, |
| 831 | udp_socket_.get(), |
| 832 | session_->username(), session_->password(), |
| 833 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 834 | } else { |
Guo-wei Shieh | 370c884 | 2015-08-18 17:00:13 -0700 | [diff] [blame] | 835 | port = UDPPort::Create(session_->network_thread(), |
| 836 | session_->socket_factory(), |
| 837 | network_, ip_, |
| 838 | session_->allocator()->min_port(), |
| 839 | session_->allocator()->max_port(), |
| 840 | session_->username(), session_->password(), |
| 841 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | if (port) { |
| 845 | // If shared socket is enabled, STUN candidate will be allocated by the |
| 846 | // UDPPort. |
| 847 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 848 | udp_port_ = port; |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 849 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 850 | |
| 851 | // If STUN is not disabled, setting stun server address to port. |
| 852 | if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 853 | if (config_ && !config_->StunServers().empty()) { |
| 854 | LOG(LS_INFO) << "AllocationSequence: UDPPort will be handling the " |
| 855 | << "STUN candidate generation."; |
| 856 | port->set_server_addresses(config_->StunServers()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | session_->AddAllocatedPort(port, this, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
| 865 | void AllocationSequence::CreateTCPPorts() { |
| 866 | if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) { |
| 867 | LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping."; |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | Port* port = TCPPort::Create(session_->network_thread(), |
| 872 | session_->socket_factory(), |
| 873 | network_, ip_, |
| 874 | session_->allocator()->min_port(), |
| 875 | session_->allocator()->max_port(), |
| 876 | session_->username(), session_->password(), |
| 877 | session_->allocator()->allow_tcp_listen()); |
| 878 | if (port) { |
| 879 | session_->AddAllocatedPort(port, this, true); |
| 880 | // Since TCPPort is not created using shared socket, |port| will not be |
| 881 | // added to the dequeue. |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void AllocationSequence::CreateStunPorts() { |
| 886 | if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
| 887 | LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping."; |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 892 | return; |
| 893 | } |
| 894 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 895 | if (!(config_ && !config_->StunServers().empty())) { |
| 896 | LOG(LS_WARNING) |
| 897 | << "AllocationSequence: No STUN server configured, skipping."; |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | StunPort* port = StunPort::Create(session_->network_thread(), |
| 902 | session_->socket_factory(), |
| 903 | network_, ip_, |
| 904 | session_->allocator()->min_port(), |
| 905 | session_->allocator()->max_port(), |
| 906 | session_->username(), session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 907 | config_->StunServers(), |
| 908 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 909 | if (port) { |
| 910 | session_->AddAllocatedPort(port, this, true); |
| 911 | // Since StunPort is not created using shared socket, |port| will not be |
| 912 | // added to the dequeue. |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | void AllocationSequence::CreateRelayPorts() { |
| 917 | if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) { |
| 918 | LOG(LS_VERBOSE) << "AllocationSequence: Relay ports disabled, skipping."; |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we |
| 923 | // ought to have a relay list for them here. |
| 924 | ASSERT(config_ && !config_->relays.empty()); |
| 925 | if (!(config_ && !config_->relays.empty())) { |
| 926 | LOG(LS_WARNING) |
| 927 | << "AllocationSequence: No relay server configured, skipping."; |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | PortConfiguration::RelayList::const_iterator relay; |
| 932 | for (relay = config_->relays.begin(); |
| 933 | relay != config_->relays.end(); ++relay) { |
| 934 | if (relay->type == RELAY_GTURN) { |
| 935 | CreateGturnPort(*relay); |
| 936 | } else if (relay->type == RELAY_TURN) { |
| 937 | CreateTurnPort(*relay); |
| 938 | } else { |
| 939 | ASSERT(false); |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) { |
| 945 | // TODO(mallinath) - Rename RelayPort to GTurnPort. |
| 946 | RelayPort* port = RelayPort::Create(session_->network_thread(), |
| 947 | session_->socket_factory(), |
| 948 | network_, ip_, |
| 949 | session_->allocator()->min_port(), |
| 950 | session_->allocator()->max_port(), |
| 951 | config_->username, config_->password); |
| 952 | if (port) { |
| 953 | // Since RelayPort is not created using shared socket, |port| will not be |
| 954 | // added to the dequeue. |
| 955 | // Note: We must add the allocated port before we add addresses because |
| 956 | // the latter will create candidates that need name and preference |
| 957 | // settings. However, we also can't prepare the address (normally |
| 958 | // done by AddAllocatedPort) until we have these addresses. So we |
| 959 | // wait to do that until below. |
| 960 | session_->AddAllocatedPort(port, this, false); |
| 961 | |
| 962 | // Add the addresses of this protocol. |
| 963 | PortList::const_iterator relay_port; |
| 964 | for (relay_port = config.ports.begin(); |
| 965 | relay_port != config.ports.end(); |
| 966 | ++relay_port) { |
| 967 | port->AddServerAddress(*relay_port); |
| 968 | port->AddExternalAddress(*relay_port); |
| 969 | } |
| 970 | // Start fetching an address for this port. |
| 971 | port->PrepareAddress(); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) { |
| 976 | PortList::const_iterator relay_port; |
| 977 | for (relay_port = config.ports.begin(); |
| 978 | relay_port != config.ports.end(); ++relay_port) { |
| 979 | TurnPort* port = NULL; |
| 980 | // Shared socket mode must be enabled only for UDP based ports. Hence |
| 981 | // don't pass shared socket for ports which will create TCP sockets. |
| 982 | // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled |
| 983 | // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537 |
| 984 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 985 | relay_port->proto == PROTO_UDP && udp_socket_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 986 | port = TurnPort::Create(session_->network_thread(), |
| 987 | session_->socket_factory(), |
| 988 | network_, udp_socket_.get(), |
| 989 | session_->username(), session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 990 | *relay_port, config.credentials, config.priority, |
| 991 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 992 | turn_ports_.push_back(port); |
| 993 | // Listen to the port destroyed signal, to allow AllocationSequence to |
| 994 | // remove entrt from it's map. |
| 995 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
| 996 | } else { |
| 997 | port = TurnPort::Create(session_->network_thread(), |
| 998 | session_->socket_factory(), |
| 999 | network_, ip_, |
| 1000 | session_->allocator()->min_port(), |
| 1001 | session_->allocator()->max_port(), |
| 1002 | session_->username(), |
| 1003 | session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1004 | *relay_port, config.credentials, config.priority, |
| 1005 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1006 | } |
| 1007 | ASSERT(port != NULL); |
| 1008 | session_->AddAllocatedPort(port, this, true); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | void AllocationSequence::OnReadPacket( |
| 1013 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 1014 | const rtc::SocketAddress& remote_addr, |
| 1015 | const rtc::PacketTime& packet_time) { |
| 1016 | ASSERT(socket == udp_socket_.get()); |
| 1017 | |
| 1018 | bool turn_port_found = false; |
| 1019 | |
| 1020 | // Try to find the TurnPort that matches the remote address. Note that the |
| 1021 | // message could be a STUN binding response if the TURN server is also used as |
| 1022 | // a STUN server. We don't want to parse every message here to check if it is |
| 1023 | // a STUN binding response, so we pass the message to TurnPort regardless of |
| 1024 | // the message type. The TurnPort will just ignore the message since it will |
| 1025 | // not find any request by transaction ID. |
| 1026 | for (std::vector<TurnPort*>::const_iterator it = turn_ports_.begin(); |
| 1027 | it != turn_ports_.end(); ++it) { |
| 1028 | TurnPort* port = *it; |
| 1029 | if (port->server_address().address == remote_addr) { |
| 1030 | port->HandleIncomingPacket(socket, data, size, remote_addr, packet_time); |
| 1031 | turn_port_found = true; |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | if (udp_port_) { |
| 1037 | const ServerAddresses& stun_servers = udp_port_->server_addresses(); |
| 1038 | |
| 1039 | // Pass the packet to the UdpPort if there is no matching TurnPort, or if |
| 1040 | // the TURN server is also a STUN server. |
| 1041 | if (!turn_port_found || |
| 1042 | stun_servers.find(remote_addr) != stun_servers.end()) { |
| 1043 | udp_port_->HandleIncomingPacket( |
| 1044 | socket, data, size, remote_addr, packet_time); |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | void AllocationSequence::OnPortDestroyed(PortInterface* port) { |
| 1050 | if (udp_port_ == port) { |
| 1051 | udp_port_ = NULL; |
| 1052 | return; |
| 1053 | } |
| 1054 | |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1055 | auto it = std::find(turn_ports_.begin(), turn_ports_.end(), port); |
| 1056 | if (it != turn_ports_.end()) { |
| 1057 | turn_ports_.erase(it); |
| 1058 | } else { |
| 1059 | LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port."; |
| 1060 | ASSERT(false); |
| 1061 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | // PortConfiguration |
| 1065 | PortConfiguration::PortConfiguration( |
| 1066 | const rtc::SocketAddress& stun_address, |
| 1067 | const std::string& username, |
| 1068 | const std::string& password) |
| 1069 | : stun_address(stun_address), username(username), password(password) { |
| 1070 | if (!stun_address.IsNil()) |
| 1071 | stun_servers.insert(stun_address); |
| 1072 | } |
| 1073 | |
| 1074 | PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers, |
| 1075 | const std::string& username, |
| 1076 | const std::string& password) |
| 1077 | : stun_servers(stun_servers), |
| 1078 | username(username), |
| 1079 | password(password) { |
| 1080 | if (!stun_servers.empty()) |
| 1081 | stun_address = *(stun_servers.begin()); |
| 1082 | } |
| 1083 | |
| 1084 | ServerAddresses PortConfiguration::StunServers() { |
| 1085 | if (!stun_address.IsNil() && |
| 1086 | stun_servers.find(stun_address) == stun_servers.end()) { |
| 1087 | stun_servers.insert(stun_address); |
| 1088 | } |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 1089 | // Every UDP TURN server should also be used as a STUN server. |
| 1090 | ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP); |
| 1091 | for (const rtc::SocketAddress& turn_server : turn_servers) { |
| 1092 | if (stun_servers.find(turn_server) == stun_servers.end()) { |
| 1093 | stun_servers.insert(turn_server); |
| 1094 | } |
| 1095 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1096 | return stun_servers; |
| 1097 | } |
| 1098 | |
| 1099 | void PortConfiguration::AddRelay(const RelayServerConfig& config) { |
| 1100 | relays.push_back(config); |
| 1101 | } |
| 1102 | |
| 1103 | bool PortConfiguration::SupportsProtocol( |
| 1104 | const RelayServerConfig& relay, ProtocolType type) const { |
| 1105 | PortList::const_iterator relay_port; |
| 1106 | for (relay_port = relay.ports.begin(); |
| 1107 | relay_port != relay.ports.end(); |
| 1108 | ++relay_port) { |
| 1109 | if (relay_port->proto == type) |
| 1110 | return true; |
| 1111 | } |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | bool PortConfiguration::SupportsProtocol(RelayType turn_type, |
| 1116 | ProtocolType type) const { |
| 1117 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1118 | if (relays[i].type == turn_type && |
| 1119 | SupportsProtocol(relays[i], type)) |
| 1120 | return true; |
| 1121 | } |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | ServerAddresses PortConfiguration::GetRelayServerAddresses( |
| 1126 | RelayType turn_type, ProtocolType type) const { |
| 1127 | ServerAddresses servers; |
| 1128 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1129 | if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { |
| 1130 | servers.insert(relays[i].ports.front().address); |
| 1131 | } |
| 1132 | } |
| 1133 | return servers; |
| 1134 | } |
| 1135 | |
| 1136 | } // namespace cricket |