OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/port_allocator_base.h" | 5 #include "remoting/protocol/port_allocator_base.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 | 9 |
| 10 #include "base/bind.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
13 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
| 15 #include "remoting/protocol/network_settings.h" |
| 16 #include "remoting/protocol/transport_context.h" |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 typedef std::map<std::string, std::string> StringMap; | 20 typedef std::map<std::string, std::string> StringMap; |
18 | 21 |
19 // Parses the lines in the result of the HTTP request that are of the form | 22 // Parses the lines in the result of the HTTP request that are of the form |
20 // 'a=b' and returns them in a map. | 23 // 'a=b' and returns them in a map. |
21 StringMap ParseMap(const std::string& string) { | 24 StringMap ParseMap(const std::string& string) { |
22 StringMap map; | 25 StringMap map; |
23 base::StringPairs pairs; | 26 base::StringPairs pairs; |
24 base::SplitStringIntoKeyValuePairs(string, '\r', '=', &pairs); | 27 base::SplitStringIntoKeyValuePairs(string, '\r', '=', &pairs); |
25 | 28 |
26 for (auto& pair : pairs) { | 29 for (auto& pair : pairs) { |
27 map[pair.first] = pair.second; | 30 map[pair.first] = pair.second; |
28 } | 31 } |
29 return map; | 32 return map; |
30 } | 33 } |
31 | 34 |
32 } // namespace | 35 } // namespace |
33 | 36 |
34 namespace remoting { | 37 namespace remoting { |
35 namespace protocol { | 38 namespace protocol { |
36 | 39 |
37 const int PortAllocatorBase::kNumRetries = 5; | 40 const int PortAllocatorBase::kNumRetries = 5; |
38 | 41 |
39 PortAllocatorBase::PortAllocatorBase(rtc::NetworkManager* network_manager, | 42 PortAllocatorBase::PortAllocatorBase( |
40 rtc::PacketSocketFactory* socket_factory) | 43 scoped_ptr<rtc::NetworkManager> network_manager, |
41 : BasicPortAllocator(network_manager, socket_factory) {} | 44 scoped_ptr<rtc::PacketSocketFactory> socket_factory, |
| 45 scoped_refptr<TransportContext> transport_context) |
| 46 : BasicPortAllocator(network_manager.get(), socket_factory.get()), |
| 47 network_manager_(std::move(network_manager)), |
| 48 socket_factory_(std::move(socket_factory)), |
| 49 transport_context_(transport_context) { |
| 50 // We always use PseudoTcp to provide a reliable channel. It provides poor |
| 51 // performance when combined with TCP-based transport, so we have to disable |
| 52 // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username |
| 53 // fragment is shared between all candidates. |
| 54 int flags = cricket::PORTALLOCATOR_DISABLE_TCP | |
| 55 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
| 56 cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 57 |
| 58 NetworkSettings network_settings = transport_context_->network_settings(); |
| 59 |
| 60 if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_STUN)) |
| 61 flags |= cricket::PORTALLOCATOR_DISABLE_STUN; |
| 62 |
| 63 if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_RELAY)) |
| 64 flags |= cricket::PORTALLOCATOR_DISABLE_RELAY; |
| 65 |
| 66 set_flags(flags); |
| 67 SetPortRange(network_settings.port_range.min_port, |
| 68 network_settings.port_range.max_port); |
| 69 } |
42 | 70 |
43 PortAllocatorBase::~PortAllocatorBase() {} | 71 PortAllocatorBase::~PortAllocatorBase() {} |
44 | 72 |
45 void PortAllocatorBase::SetStunHosts( | |
46 const std::vector<rtc::SocketAddress>& hosts) { | |
47 stun_hosts_ = hosts; | |
48 } | |
49 | |
50 void PortAllocatorBase::SetRelayHosts(const std::vector<std::string>& hosts) { | |
51 relay_hosts_ = hosts; | |
52 } | |
53 | |
54 void PortAllocatorBase::SetRelayToken(const std::string& relay) { | |
55 relay_token_ = relay; | |
56 } | |
57 | |
58 PortAllocatorSessionBase::PortAllocatorSessionBase( | 73 PortAllocatorSessionBase::PortAllocatorSessionBase( |
59 PortAllocatorBase* allocator, | 74 PortAllocatorBase* allocator, |
60 const std::string& content_name, | 75 const std::string& content_name, |
61 int component, | 76 int component, |
62 const std::string& ice_ufrag, | 77 const std::string& ice_ufrag, |
63 const std::string& ice_pwd, | 78 const std::string& ice_pwd) |
64 const std::vector<rtc::SocketAddress>& stun_hosts, | |
65 const std::vector<std::string>& relay_hosts, | |
66 const std::string& relay_token) | |
67 : BasicPortAllocatorSession(allocator, | 79 : BasicPortAllocatorSession(allocator, |
68 content_name, | 80 content_name, |
69 component, | 81 component, |
70 ice_ufrag, | 82 ice_ufrag, |
71 ice_pwd), | 83 ice_pwd), |
72 relay_hosts_(relay_hosts), | 84 weak_factory_(this) {} |
73 stun_hosts_(stun_hosts), | |
74 relay_token_(relay_token), | |
75 attempts_(0) {} | |
76 | 85 |
77 PortAllocatorSessionBase::~PortAllocatorSessionBase() {} | 86 PortAllocatorSessionBase::~PortAllocatorSessionBase() {} |
78 | 87 |
79 void PortAllocatorSessionBase::GetPortConfigurations() { | 88 void PortAllocatorSessionBase::GetPortConfigurations() { |
| 89 allocator()->transport_context()->GetJingleInfo(base::Bind( |
| 90 &PortAllocatorSessionBase::OnJingleInfo, weak_factory_.GetWeakPtr())); |
| 91 } |
| 92 |
| 93 void PortAllocatorSessionBase::OnJingleInfo( |
| 94 std::vector<rtc::SocketAddress> stun_hosts, |
| 95 std::vector<std::string> relay_hosts, |
| 96 std::string relay_token) { |
| 97 stun_hosts_ = stun_hosts; |
| 98 relay_hosts_ = relay_hosts; |
| 99 relay_token_ = relay_token; |
| 100 |
80 // Creating relay sessions can take time and is done asynchronously. | 101 // Creating relay sessions can take time and is done asynchronously. |
81 // Creating stun sessions could also take time and could be done aysnc also, | 102 // Creating stun sessions could also take time and could be done aysnc also, |
82 // but for now is done here and added to the initial config. Note any later | 103 // but for now is done here and added to the initial config. Note any later |
83 // configs will have unresolved stun ips and will be discarded by the | 104 // configs will have unresolved stun ips and will be discarded by the |
84 // AllocationSequence. | 105 // AllocationSequence. |
85 cricket::ServerAddresses hosts; | 106 cricket::ServerAddresses hosts; |
86 for (const auto& host : stun_hosts_) { | 107 for (const auto& host : stun_hosts_) { |
87 hosts.insert(host); | 108 hosts.insert(host); |
88 } | 109 } |
89 | 110 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 relay_config.ports.push_back( | 183 relay_config.ports.push_back( |
163 cricket::ProtocolAddress(address, cricket::PROTO_UDP)); | 184 cricket::ProtocolAddress(address, cricket::PROTO_UDP)); |
164 config->AddRelay(relay_config); | 185 config->AddRelay(relay_config); |
165 } | 186 } |
166 | 187 |
167 ConfigReady(config); | 188 ConfigReady(config); |
168 } | 189 } |
169 | 190 |
170 } // namespace protocol | 191 } // namespace protocol |
171 } // namespace remoting | 192 } // namespace remoting |
OLD | NEW |