blob: 23faff1cba4c53a93b6c7887ddeebc2f1c1553da [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_CLIENT_BASICPORTALLOCATOR_H_
12#define P2P_CLIENT_BASICPORTALLOCATOR_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
kwiberg3ec46792016-04-27 07:22:53 -070014#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015#include <string>
16#include <vector>
17
Jonas Orelandbdcee282017-10-10 14:01:40 +020018#include "api/turncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/portallocator.h"
Jonas Oreland26246ca2017-12-14 08:56:55 +010020#include "p2p/client/turnportfactory.h"
21#include "p2p/client/relayportfactoryinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/messagequeue.h"
24#include "rtc_base/network.h"
25#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026
27namespace cricket {
28
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000029class BasicPortAllocator : public PortAllocator {
30 public:
Jonas Oreland26246ca2017-12-14 08:56:55 +010031 // note: The (optional) relay_port_factory is owned by caller
32 // and must have a life time that exceeds that of BasicPortAllocator.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033 BasicPortAllocator(rtc::NetworkManager* network_manager,
Jonas Orelandbdcee282017-10-10 14:01:40 +020034 rtc::PacketSocketFactory* socket_factory,
Jonas Oreland26246ca2017-12-14 08:56:55 +010035 webrtc::TurnCustomizer* customizer = nullptr,
36 RelayPortFactoryInterface* relay_port_factory = nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037 explicit BasicPortAllocator(rtc::NetworkManager* network_manager);
38 BasicPortAllocator(rtc::NetworkManager* network_manager,
39 rtc::PacketSocketFactory* socket_factory,
40 const ServerAddresses& stun_servers);
41 BasicPortAllocator(rtc::NetworkManager* network_manager,
42 const ServerAddresses& stun_servers,
Jonas Oreland26246ca2017-12-14 08:56:55 +010043 const rtc::SocketAddress& relay_address_udp,
44 const rtc::SocketAddress& relay_address_tcp,
45 const rtc::SocketAddress& relay_address_ssl);
Steve Anton7995d8c2017-10-30 16:23:38 -070046 ~BasicPortAllocator() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080048 // Set to kDefaultNetworkIgnoreMask by default.
Steve Anton7995d8c2017-10-30 16:23:38 -070049 void SetNetworkIgnoreMask(int network_ignore_mask) override;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080050 int network_ignore_mask() const { return network_ignore_mask_; }
51
Honghai Zhang5622c5e2016-07-01 13:59:29 -070052 rtc::NetworkManager* network_manager() const { return network_manager_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053
54 // If socket_factory() is set to NULL each PortAllocatorSession
55 // creates its own socket factory.
56 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
57
deadbeef653b8e02015-11-11 12:55:10 -080058 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 const std::string& content_name,
60 int component,
61 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080062 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063
Taylor Brandstettera1c30352016-05-13 08:15:11 -070064 // Convenience method that adds a TURN server to the configuration.
65 void AddTurnServer(const RelayServerConfig& turn_server);
66
Jonas Oreland26246ca2017-12-14 08:56:55 +010067 RelayPortFactoryInterface* relay_port_factory() {
68 return relay_port_factory_;
69 }
70
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071 private:
72 void Construct();
73
Honghai Zhangd93f50c2016-10-05 11:47:22 -070074 void OnIceRegathering(PortAllocatorSession* session,
75 IceRegatheringReason reason);
76
Jonas Oreland26246ca2017-12-14 08:56:55 +010077 // This function makes sure that relay_port_factory_ is set properly.
78 void InitRelayPortFactory(RelayPortFactoryInterface* relay_port_factory);
79
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080 rtc::NetworkManager* network_manager_;
81 rtc::PacketSocketFactory* socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080083 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
Jonas Oreland26246ca2017-12-14 08:56:55 +010084
85 // This is the factory being used.
86 RelayPortFactoryInterface* relay_port_factory_;
87
88 // This instance is created if caller does pass a factory.
89 std::unique_ptr<RelayPortFactoryInterface> default_relay_port_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090};
91
92struct PortConfiguration;
93class AllocationSequence;
94
Honghai Zhangd8f6fc42016-07-01 17:31:12 -070095enum class SessionState {
96 GATHERING, // Actively allocating ports and gathering candidates.
97 CLEARED, // Current allocation process has been stopped but may start
98 // new ones.
99 STOPPED // This session has completely stopped, no new allocation
100 // process will be started.
101};
102
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103class BasicPortAllocatorSession : public PortAllocatorSession,
104 public rtc::MessageHandler {
105 public:
106 BasicPortAllocatorSession(BasicPortAllocator* allocator,
107 const std::string& content_name,
108 int component,
109 const std::string& ice_ufrag,
110 const std::string& ice_pwd);
Steve Anton7995d8c2017-10-30 16:23:38 -0700111 ~BasicPortAllocatorSession() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
Steve Anton7995d8c2017-10-30 16:23:38 -0700113 virtual BasicPortAllocator* allocator();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 rtc::Thread* network_thread() { return network_thread_; }
115 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
116
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700117 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -0800118 void StartGettingPorts() override;
119 void StopGettingPorts() override;
120 void ClearGettingPorts() override;
Steve Anton7995d8c2017-10-30 16:23:38 -0700121 bool IsGettingPorts() override;
122 bool IsCleared() const override;
123 bool IsStopped() const override;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700124 // These will all be cricket::Ports.
125 std::vector<PortInterface*> ReadyPorts() const override;
126 std::vector<Candidate> ReadyCandidates() const override;
127 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700128 void RegatherOnFailedNetworks() override;
Steve Anton300bf8e2017-07-14 10:13:10 -0700129 void RegatherOnAllNetworks() override;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700130 void PruneAllPorts() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131
132 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700133 void UpdateIceParametersInternal() override;
134
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 // Starts the process of getting the port configurations.
136 virtual void GetPortConfigurations();
137
138 // Adds a port configuration that is now ready. Once we have one for each
139 // network (or a timeout occurs), we will start allocating ports.
140 virtual void ConfigReady(PortConfiguration* config);
141
142 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800143 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144
145 private:
146 class PortData {
147 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700148 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700150 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700152 Port* port() const { return port_; }
153 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700154 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700155 bool complete() const { return state_ == STATE_COMPLETE; }
156 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700157 bool pruned() const { return state_ == STATE_PRUNED; }
158 bool inprogress() const { return state_ == STATE_INPROGRESS; }
159 // Returns true if this port is ready to be used.
160 bool ready() const {
161 return has_pairable_candidate_ && state_ != STATE_ERROR &&
162 state_ != STATE_PRUNED;
163 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700164 // Sets the state to "PRUNED" and prunes the Port.
165 void Prune() {
166 state_ = STATE_PRUNED;
167 if (port()) {
168 port()->Prune();
169 }
170 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700171 void set_has_pairable_candidate(bool has_pairable_candidate) {
172 if (has_pairable_candidate) {
nisseede5da42017-01-12 05:15:36 -0800173 RTC_DCHECK(state_ == STATE_INPROGRESS);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700174 }
175 has_pairable_candidate_ = has_pairable_candidate;
176 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 void set_complete() {
178 state_ = STATE_COMPLETE;
179 }
180 void set_error() {
nisseede5da42017-01-12 05:15:36 -0800181 RTC_DCHECK(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000182 state_ = STATE_ERROR;
183 }
184
185 private:
186 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700187 STATE_INPROGRESS, // Still gathering candidates.
188 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700189 STATE_ERROR, // Error in gathering candidates.
190 STATE_PRUNED // Pruned by higher priority ports on the same network
191 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700193 Port* port_ = nullptr;
194 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700195 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700196 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 };
198
199 void OnConfigReady(PortConfiguration* config);
200 void OnConfigStop();
201 void AllocatePorts();
202 void OnAllocate();
Steve Anton300bf8e2017-07-14 10:13:10 -0700203 void DoAllocate(bool disable_equivalent_phases);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 void OnNetworksChanged();
205 void OnAllocationSequenceObjectsCreated();
206 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200207 PortConfiguration* config,
208 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 void AddAllocatedPort(Port* port, AllocationSequence* seq,
210 bool prepare_address);
211 void OnCandidateReady(Port* port, const Candidate& c);
212 void OnPortComplete(Port* port);
213 void OnPortError(Port* port);
214 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
215 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 void MaybeSignalCandidatesAllocationDone();
217 void OnPortAllocationComplete(AllocationSequence* seq);
218 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700219 std::vector<rtc::Network*> GetNetworks();
220 std::vector<rtc::Network*> GetFailedNetworks();
Steve Anton300bf8e2017-07-14 10:13:10 -0700221 void Regather(const std::vector<rtc::Network*>& networks,
222 bool disable_equivalent_phases,
223 IceRegatheringReason reason);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700226 bool CandidatePairable(const Candidate& c, const Port* port) const;
227 // Clear the related address according to the flags and candidate filter
228 // in order to avoid leaking any information.
229 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000230
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700231 std::vector<PortData*> GetUnprunedPorts(
232 const std::vector<rtc::Network*>& networks);
233 // Prunes ports and signal the remote side to remove the candidates that
234 // were previously signaled from these ports.
235 void PrunePortsAndRemoveCandidates(
236 const std::vector<PortData*>& port_data_list);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700237 // Gets filtered and sanitized candidates generated from a port and
238 // append to |candidates|.
239 void GetCandidatesFromPort(const PortData& data,
240 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700241 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
242 // Returns true if at least one TURN port is pruned.
243 bool PruneTurnPorts(Port* newly_pairable_turn_port);
244
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 BasicPortAllocator* allocator_;
246 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700247 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000248 rtc::PacketSocketFactory* socket_factory_;
249 bool allocation_started_;
250 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 bool allocation_sequences_created_;
252 std::vector<PortConfiguration*> configs_;
253 std::vector<AllocationSequence*> sequences_;
254 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700255 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700256 // Whether to prune low-priority ports, taken from the port allocator.
257 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700258 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000259
260 friend class AllocationSequence;
261};
262
263// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800264// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000265struct PortConfiguration : public rtc::MessageData {
266 // TODO(jiayl): remove |stun_address| when Chrome is updated.
267 rtc::SocketAddress stun_address;
268 ServerAddresses stun_servers;
269 std::string username;
270 std::string password;
271
272 typedef std::vector<RelayServerConfig> RelayList;
273 RelayList relays;
274
275 // TODO(jiayl): remove this ctor when Chrome is updated.
276 PortConfiguration(const rtc::SocketAddress& stun_address,
277 const std::string& username,
278 const std::string& password);
279
280 PortConfiguration(const ServerAddresses& stun_servers,
281 const std::string& username,
282 const std::string& password);
283
Steve Anton7995d8c2017-10-30 16:23:38 -0700284 ~PortConfiguration() override;
285
deadbeefc5d0d952015-07-16 10:22:21 -0700286 // Returns addresses of both the explicitly configured STUN servers,
287 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000288 ServerAddresses StunServers();
289
290 // Adds another relay server, with the given ports and modifier, to the list.
291 void AddRelay(const RelayServerConfig& config);
292
293 // Determines whether the given relay server supports the given protocol.
294 bool SupportsProtocol(const RelayServerConfig& relay,
295 ProtocolType type) const;
296 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
297 // Helper method returns the server addresses for the matching RelayType and
298 // Protocol type.
299 ServerAddresses GetRelayServerAddresses(
300 RelayType turn_type, ProtocolType type) const;
301};
302
honghaizf421bdc2015-07-17 16:21:55 -0700303class UDPPort;
304class TurnPort;
305
306// Performs the allocation of ports, in a sequenced (timed) manner, for a given
307// network and IP address.
308class AllocationSequence : public rtc::MessageHandler,
309 public sigslot::has_slots<> {
310 public:
311 enum State {
312 kInit, // Initial state.
313 kRunning, // Started allocating ports.
314 kStopped, // Stopped from running.
315 kCompleted, // All ports are allocated.
316
317 // kInit --> kRunning --> {kCompleted|kStopped}
318 };
319 AllocationSequence(BasicPortAllocatorSession* session,
320 rtc::Network* network,
321 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200322 uint32_t flags);
Steve Anton7995d8c2017-10-30 16:23:38 -0700323 ~AllocationSequence() override;
Honghai Zhang5048f572016-08-23 15:47:33 -0700324 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700325 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700326 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700327
328 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700329 rtc::Network* network() const { return network_; }
330
331 bool network_failed() const { return network_failed_; }
332 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700333
334 // Disables the phases for a new sequence that this one already covers for an
335 // equivalent network setup.
336 void DisableEquivalentPhases(rtc::Network* network,
337 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200338 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700339
340 // Starts and stops the sequence. When started, it will continue allocating
341 // new ports on its own timed schedule.
342 void Start();
343 void Stop();
344
345 // MessageHandler
Steve Anton7995d8c2017-10-30 16:23:38 -0700346 void OnMessage(rtc::Message* msg) override;
honghaizf421bdc2015-07-17 16:21:55 -0700347
honghaizf421bdc2015-07-17 16:21:55 -0700348 // Signal from AllocationSequence, when it's done with allocating ports.
349 // This signal is useful, when port allocation fails which doesn't result
350 // in any candidates. Using this signal BasicPortAllocatorSession can send
351 // its candidate discovery conclusion signal. Without this signal,
352 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
353 // can also be achieved by starting timer in BPAS.
354 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
355
356 protected:
357 // For testing.
358 void CreateTurnPort(const RelayServerConfig& config);
359
360 private:
361 typedef std::vector<ProtocolType> ProtocolList;
362
Peter Boström0c4e06b2015-10-07 12:23:21 +0200363 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700364 void CreateUDPPorts();
365 void CreateTCPPorts();
366 void CreateStunPorts();
367 void CreateRelayPorts();
368 void CreateGturnPort(const RelayServerConfig& config);
369
370 void OnReadPacket(rtc::AsyncPacketSocket* socket,
371 const char* data,
372 size_t size,
373 const rtc::SocketAddress& remote_addr,
374 const rtc::PacketTime& packet_time);
375
376 void OnPortDestroyed(PortInterface* port);
377
378 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700379 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700380 rtc::Network* network_;
deadbeef5c3c1042017-08-04 15:01:57 -0700381 // Compared with the new best IP in DisableEquivalentPhases.
382 rtc::IPAddress previous_best_ip_;
honghaizf421bdc2015-07-17 16:21:55 -0700383 PortConfiguration* config_;
384 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700386 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700387 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700388 // There will be only one udp port per AllocationSequence.
389 UDPPort* udp_port_;
Jonas Oreland26246ca2017-12-14 08:56:55 +0100390 std::vector<Port*> relay_ports_;
honghaizf421bdc2015-07-17 16:21:55 -0700391 int phase_;
392};
393
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394} // namespace cricket
395
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200396#endif // P2P_CLIENT_BASICPORTALLOCATOR_H_