123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // Copyright 2016 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "remoting/protocol/port_allocator.h"
- #include <algorithm>
- #include <map>
- #include "base/bind.h"
- #include "base/callback.h"
- #include "remoting/protocol/network_settings.h"
- #include "remoting/protocol/transport_context.h"
- #include "third_party/abseil-cpp/absl/strings/string_view.h"
- namespace remoting {
- namespace protocol {
- PortAllocator::PortAllocator(
- std::unique_ptr<rtc::NetworkManager> network_manager,
- std::unique_ptr<rtc::PacketSocketFactory> socket_factory,
- scoped_refptr<TransportContext> transport_context)
- : BasicPortAllocator(network_manager.get(), socket_factory.get()),
- network_manager_(std::move(network_manager)),
- socket_factory_(std::move(socket_factory)),
- transport_context_(transport_context) {
- // We always use PseudoTcp to provide a reliable channel. It provides poor
- // performance when combined with TCP-based transport, so we have to disable
- // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username
- // fragment is shared between all candidates.
- // TODO(crbug.com/488760): Ideally we want to add
- // PORTALLOCATOR_DISABLE_COSTLY_NETWORKS, but this is unreliable on iOS and
- // may end up removing mobile networks when no WiFi is available. We may want
- // to add this flag only if there is WiFi interface.
- int flags = cricket::PORTALLOCATOR_DISABLE_TCP |
- cricket::PORTALLOCATOR_ENABLE_IPV6 |
- cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI;
- NetworkSettings network_settings = transport_context_->network_settings();
- if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_STUN))
- flags |= cricket::PORTALLOCATOR_DISABLE_STUN;
- if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_RELAY))
- flags |= cricket::PORTALLOCATOR_DISABLE_RELAY;
- set_flags(flags);
- SetPortRange(network_settings.port_range.min_port,
- network_settings.port_range.max_port);
- Initialize();
- }
- PortAllocator::~PortAllocator() = default;
- cricket::PortAllocatorSession* PortAllocator::CreateSessionInternal(
- absl::string_view content_name,
- int component,
- absl::string_view ice_username_fragment,
- absl::string_view ice_password) {
- return new PortAllocatorSession(this, std::string(content_name), component,
- std::string(ice_username_fragment),
- std::string(ice_password));
- }
- PortAllocatorSession::PortAllocatorSession(PortAllocator* allocator,
- const std::string& content_name,
- int component,
- const std::string& ice_ufrag,
- const std::string& ice_pwd)
- : BasicPortAllocatorSession(allocator,
- content_name,
- component,
- ice_ufrag,
- ice_pwd),
- transport_context_(allocator->transport_context()) {}
- PortAllocatorSession::~PortAllocatorSession() = default;
- void PortAllocatorSession::GetPortConfigurations() {
- transport_context_->GetIceConfig(base::BindOnce(
- &PortAllocatorSession::OnIceConfig, weak_factory_.GetWeakPtr()));
- }
- void PortAllocatorSession::OnIceConfig(const IceConfig& ice_config) {
- ice_config_ = ice_config;
- ConfigReady(GetPortConfiguration().release());
- }
- std::unique_ptr<cricket::PortConfiguration>
- PortAllocatorSession::GetPortConfiguration() {
- cricket::ServerAddresses stun_servers;
- for (const auto& host : ice_config_.stun_servers) {
- stun_servers.insert(host);
- }
- std::unique_ptr<cricket::PortConfiguration> config(
- new cricket::PortConfiguration(stun_servers, username(), password()));
- if (relay_enabled()) {
- for (const auto& turn_server : ice_config_.turn_servers) {
- config->AddRelay(turn_server);
- }
- }
- return config;
- }
- } // namespace protocol
- } // namespace remoting
|