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