port_allocator.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/protocol/port_allocator.h"
  5. #include <algorithm>
  6. #include <map>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "remoting/protocol/network_settings.h"
  10. #include "remoting/protocol/transport_context.h"
  11. #include "third_party/abseil-cpp/absl/strings/string_view.h"
  12. namespace remoting {
  13. namespace protocol {
  14. PortAllocator::PortAllocator(
  15. std::unique_ptr<rtc::NetworkManager> network_manager,
  16. std::unique_ptr<rtc::PacketSocketFactory> socket_factory,
  17. scoped_refptr<TransportContext> transport_context)
  18. : BasicPortAllocator(network_manager.get(), socket_factory.get()),
  19. network_manager_(std::move(network_manager)),
  20. socket_factory_(std::move(socket_factory)),
  21. transport_context_(transport_context) {
  22. // We always use PseudoTcp to provide a reliable channel. It provides poor
  23. // performance when combined with TCP-based transport, so we have to disable
  24. // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username
  25. // fragment is shared between all candidates.
  26. // TODO(crbug.com/488760): Ideally we want to add
  27. // PORTALLOCATOR_DISABLE_COSTLY_NETWORKS, but this is unreliable on iOS and
  28. // may end up removing mobile networks when no WiFi is available. We may want
  29. // to add this flag only if there is WiFi interface.
  30. int flags = cricket::PORTALLOCATOR_DISABLE_TCP |
  31. cricket::PORTALLOCATOR_ENABLE_IPV6 |
  32. cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI;
  33. NetworkSettings network_settings = transport_context_->network_settings();
  34. if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_STUN))
  35. flags |= cricket::PORTALLOCATOR_DISABLE_STUN;
  36. if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_RELAY))
  37. flags |= cricket::PORTALLOCATOR_DISABLE_RELAY;
  38. set_flags(flags);
  39. SetPortRange(network_settings.port_range.min_port,
  40. network_settings.port_range.max_port);
  41. Initialize();
  42. }
  43. PortAllocator::~PortAllocator() = default;
  44. cricket::PortAllocatorSession* PortAllocator::CreateSessionInternal(
  45. absl::string_view content_name,
  46. int component,
  47. absl::string_view ice_username_fragment,
  48. absl::string_view ice_password) {
  49. return new PortAllocatorSession(this, std::string(content_name), component,
  50. std::string(ice_username_fragment),
  51. std::string(ice_password));
  52. }
  53. PortAllocatorSession::PortAllocatorSession(PortAllocator* allocator,
  54. const std::string& content_name,
  55. int component,
  56. const std::string& ice_ufrag,
  57. const std::string& ice_pwd)
  58. : BasicPortAllocatorSession(allocator,
  59. content_name,
  60. component,
  61. ice_ufrag,
  62. ice_pwd),
  63. transport_context_(allocator->transport_context()) {}
  64. PortAllocatorSession::~PortAllocatorSession() = default;
  65. void PortAllocatorSession::GetPortConfigurations() {
  66. transport_context_->GetIceConfig(base::BindOnce(
  67. &PortAllocatorSession::OnIceConfig, weak_factory_.GetWeakPtr()));
  68. }
  69. void PortAllocatorSession::OnIceConfig(const IceConfig& ice_config) {
  70. ice_config_ = ice_config;
  71. ConfigReady(GetPortConfiguration().release());
  72. }
  73. std::unique_ptr<cricket::PortConfiguration>
  74. PortAllocatorSession::GetPortConfiguration() {
  75. cricket::ServerAddresses stun_servers;
  76. for (const auto& host : ice_config_.stun_servers) {
  77. stun_servers.insert(host);
  78. }
  79. std::unique_ptr<cricket::PortConfiguration> config(
  80. new cricket::PortConfiguration(stun_servers, username(), password()));
  81. if (relay_enabled()) {
  82. for (const auto& turn_server : ice_config_.turn_servers) {
  83. config->AddRelay(turn_server);
  84. }
  85. }
  86. return config;
  87. }
  88. } // namespace protocol
  89. } // namespace remoting