blob: 5ec721b8fdcbbd96a80d6e242888d4349b2fb895 [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"
20#include "rtc_base/checks.h"
21#include "rtc_base/messagequeue.h"
22#include "rtc_base/network.h"
23#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027class BasicPortAllocator : public PortAllocator {
28 public:
29 BasicPortAllocator(rtc::NetworkManager* network_manager,
Jonas Orelandbdcee282017-10-10 14:01:40 +020030 rtc::PacketSocketFactory* socket_factory,
31 webrtc::TurnCustomizer* customizer = nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032 explicit BasicPortAllocator(rtc::NetworkManager* network_manager);
33 BasicPortAllocator(rtc::NetworkManager* network_manager,
34 rtc::PacketSocketFactory* socket_factory,
35 const ServerAddresses& stun_servers);
36 BasicPortAllocator(rtc::NetworkManager* network_manager,
37 const ServerAddresses& stun_servers,
38 const rtc::SocketAddress& relay_server_udp,
39 const rtc::SocketAddress& relay_server_tcp,
40 const rtc::SocketAddress& relay_server_ssl);
41 virtual ~BasicPortAllocator();
42
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080043 // Set to kDefaultNetworkIgnoreMask by default.
44 void SetNetworkIgnoreMask(int network_ignore_mask) override {
45 // TODO(phoglund): implement support for other types than loopback.
46 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
47 // Then remove set_network_ignore_list from NetworkManager.
48 network_ignore_mask_ = network_ignore_mask;
49 }
50
51 int network_ignore_mask() const { return network_ignore_mask_; }
52
Honghai Zhang5622c5e2016-07-01 13:59:29 -070053 rtc::NetworkManager* network_manager() const { return network_manager_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054
55 // If socket_factory() is set to NULL each PortAllocatorSession
56 // creates its own socket factory.
57 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
58
deadbeef653b8e02015-11-11 12:55:10 -080059 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 const std::string& content_name,
61 int component,
62 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080063 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064
Taylor Brandstettera1c30352016-05-13 08:15:11 -070065 // Convenience method that adds a TURN server to the configuration.
66 void AddTurnServer(const RelayServerConfig& turn_server);
67
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068 private:
69 void Construct();
70
Honghai Zhangd93f50c2016-10-05 11:47:22 -070071 void OnIceRegathering(PortAllocatorSession* session,
72 IceRegatheringReason reason);
73
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074 rtc::NetworkManager* network_manager_;
75 rtc::PacketSocketFactory* socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080077 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078};
79
80struct PortConfiguration;
81class AllocationSequence;
82
Honghai Zhangd8f6fc42016-07-01 17:31:12 -070083enum class SessionState {
84 GATHERING, // Actively allocating ports and gathering candidates.
85 CLEARED, // Current allocation process has been stopped but may start
86 // new ones.
87 STOPPED // This session has completely stopped, no new allocation
88 // process will be started.
89};
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091class BasicPortAllocatorSession : public PortAllocatorSession,
92 public rtc::MessageHandler {
93 public:
94 BasicPortAllocatorSession(BasicPortAllocator* allocator,
95 const std::string& content_name,
96 int component,
97 const std::string& ice_ufrag,
98 const std::string& ice_pwd);
99 ~BasicPortAllocatorSession();
100
101 virtual BasicPortAllocator* allocator() { return allocator_; }
102 rtc::Thread* network_thread() { return network_thread_; }
103 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
104
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700105 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -0800106 void StartGettingPorts() override;
107 void StopGettingPorts() override;
108 void ClearGettingPorts() override;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700109 bool IsGettingPorts() override { return state_ == SessionState::GATHERING; }
110 bool IsCleared() const override { return state_ == SessionState::CLEARED; }
111 bool IsStopped() const override { return state_ == SessionState::STOPPED; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700112 // These will all be cricket::Ports.
113 std::vector<PortInterface*> ReadyPorts() const override;
114 std::vector<Candidate> ReadyCandidates() const override;
115 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700116 void RegatherOnFailedNetworks() override;
Steve Anton300bf8e2017-07-14 10:13:10 -0700117 void RegatherOnAllNetworks() override;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700118 void PruneAllPorts() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700121 void UpdateIceParametersInternal() override;
122
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123 // Starts the process of getting the port configurations.
124 virtual void GetPortConfigurations();
125
126 // Adds a port configuration that is now ready. Once we have one for each
127 // network (or a timeout occurs), we will start allocating ports.
128 virtual void ConfigReady(PortConfiguration* config);
129
130 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800131 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132
133 private:
134 class PortData {
135 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700136 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700138 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700140 Port* port() const { return port_; }
141 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700142 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700143 bool complete() const { return state_ == STATE_COMPLETE; }
144 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700145 bool pruned() const { return state_ == STATE_PRUNED; }
146 bool inprogress() const { return state_ == STATE_INPROGRESS; }
147 // Returns true if this port is ready to be used.
148 bool ready() const {
149 return has_pairable_candidate_ && state_ != STATE_ERROR &&
150 state_ != STATE_PRUNED;
151 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700152 // Sets the state to "PRUNED" and prunes the Port.
153 void Prune() {
154 state_ = STATE_PRUNED;
155 if (port()) {
156 port()->Prune();
157 }
158 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 void set_has_pairable_candidate(bool has_pairable_candidate) {
160 if (has_pairable_candidate) {
nisseede5da42017-01-12 05:15:36 -0800161 RTC_DCHECK(state_ == STATE_INPROGRESS);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700162 }
163 has_pairable_candidate_ = has_pairable_candidate;
164 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 void set_complete() {
166 state_ = STATE_COMPLETE;
167 }
168 void set_error() {
nisseede5da42017-01-12 05:15:36 -0800169 RTC_DCHECK(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 state_ = STATE_ERROR;
171 }
172
173 private:
174 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700175 STATE_INPROGRESS, // Still gathering candidates.
176 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700177 STATE_ERROR, // Error in gathering candidates.
178 STATE_PRUNED // Pruned by higher priority ports on the same network
179 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700181 Port* port_ = nullptr;
182 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700183 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700184 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 };
186
187 void OnConfigReady(PortConfiguration* config);
188 void OnConfigStop();
189 void AllocatePorts();
190 void OnAllocate();
Steve Anton300bf8e2017-07-14 10:13:10 -0700191 void DoAllocate(bool disable_equivalent_phases);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 void OnNetworksChanged();
193 void OnAllocationSequenceObjectsCreated();
194 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200195 PortConfiguration* config,
196 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 void AddAllocatedPort(Port* port, AllocationSequence* seq,
198 bool prepare_address);
199 void OnCandidateReady(Port* port, const Candidate& c);
200 void OnPortComplete(Port* port);
201 void OnPortError(Port* port);
202 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
203 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 void MaybeSignalCandidatesAllocationDone();
205 void OnPortAllocationComplete(AllocationSequence* seq);
206 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700207 std::vector<rtc::Network*> GetNetworks();
208 std::vector<rtc::Network*> GetFailedNetworks();
Steve Anton300bf8e2017-07-14 10:13:10 -0700209 void Regather(const std::vector<rtc::Network*>& networks,
210 bool disable_equivalent_phases,
211 IceRegatheringReason reason);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700213 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700214 bool CandidatePairable(const Candidate& c, const Port* port) const;
215 // Clear the related address according to the flags and candidate filter
216 // in order to avoid leaking any information.
217 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700219 std::vector<PortData*> GetUnprunedPorts(
220 const std::vector<rtc::Network*>& networks);
221 // Prunes ports and signal the remote side to remove the candidates that
222 // were previously signaled from these ports.
223 void PrunePortsAndRemoveCandidates(
224 const std::vector<PortData*>& port_data_list);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700225 // Gets filtered and sanitized candidates generated from a port and
226 // append to |candidates|.
227 void GetCandidatesFromPort(const PortData& data,
228 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700229 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
230 // Returns true if at least one TURN port is pruned.
231 bool PruneTurnPorts(Port* newly_pairable_turn_port);
232
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 BasicPortAllocator* allocator_;
234 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700235 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 rtc::PacketSocketFactory* socket_factory_;
237 bool allocation_started_;
238 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 bool allocation_sequences_created_;
240 std::vector<PortConfiguration*> configs_;
241 std::vector<AllocationSequence*> sequences_;
242 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700243 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700244 // Whether to prune low-priority ports, taken from the port allocator.
245 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700246 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247
248 friend class AllocationSequence;
249};
250
251// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800252// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253struct PortConfiguration : public rtc::MessageData {
254 // TODO(jiayl): remove |stun_address| when Chrome is updated.
255 rtc::SocketAddress stun_address;
256 ServerAddresses stun_servers;
257 std::string username;
258 std::string password;
259
260 typedef std::vector<RelayServerConfig> RelayList;
261 RelayList relays;
262
263 // TODO(jiayl): remove this ctor when Chrome is updated.
264 PortConfiguration(const rtc::SocketAddress& stun_address,
265 const std::string& username,
266 const std::string& password);
267
268 PortConfiguration(const ServerAddresses& stun_servers,
269 const std::string& username,
270 const std::string& password);
271
deadbeefc5d0d952015-07-16 10:22:21 -0700272 // Returns addresses of both the explicitly configured STUN servers,
273 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 ServerAddresses StunServers();
275
276 // Adds another relay server, with the given ports and modifier, to the list.
277 void AddRelay(const RelayServerConfig& config);
278
279 // Determines whether the given relay server supports the given protocol.
280 bool SupportsProtocol(const RelayServerConfig& relay,
281 ProtocolType type) const;
282 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
283 // Helper method returns the server addresses for the matching RelayType and
284 // Protocol type.
285 ServerAddresses GetRelayServerAddresses(
286 RelayType turn_type, ProtocolType type) const;
287};
288
honghaizf421bdc2015-07-17 16:21:55 -0700289class UDPPort;
290class TurnPort;
291
292// Performs the allocation of ports, in a sequenced (timed) manner, for a given
293// network and IP address.
294class AllocationSequence : public rtc::MessageHandler,
295 public sigslot::has_slots<> {
296 public:
297 enum State {
298 kInit, // Initial state.
299 kRunning, // Started allocating ports.
300 kStopped, // Stopped from running.
301 kCompleted, // All ports are allocated.
302
303 // kInit --> kRunning --> {kCompleted|kStopped}
304 };
305 AllocationSequence(BasicPortAllocatorSession* session,
306 rtc::Network* network,
307 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700309 ~AllocationSequence();
Honghai Zhang5048f572016-08-23 15:47:33 -0700310 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700311 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700312 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700313
314 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700315 rtc::Network* network() const { return network_; }
316
317 bool network_failed() const { return network_failed_; }
318 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700319
320 // Disables the phases for a new sequence that this one already covers for an
321 // equivalent network setup.
322 void DisableEquivalentPhases(rtc::Network* network,
323 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200324 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700325
326 // Starts and stops the sequence. When started, it will continue allocating
327 // new ports on its own timed schedule.
328 void Start();
329 void Stop();
330
331 // MessageHandler
332 void OnMessage(rtc::Message* msg);
333
honghaizf421bdc2015-07-17 16:21:55 -0700334 // Signal from AllocationSequence, when it's done with allocating ports.
335 // This signal is useful, when port allocation fails which doesn't result
336 // in any candidates. Using this signal BasicPortAllocatorSession can send
337 // its candidate discovery conclusion signal. Without this signal,
338 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
339 // can also be achieved by starting timer in BPAS.
340 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
341
342 protected:
343 // For testing.
344 void CreateTurnPort(const RelayServerConfig& config);
345
346 private:
347 typedef std::vector<ProtocolType> ProtocolList;
348
Peter Boström0c4e06b2015-10-07 12:23:21 +0200349 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700350 void CreateUDPPorts();
351 void CreateTCPPorts();
352 void CreateStunPorts();
353 void CreateRelayPorts();
354 void CreateGturnPort(const RelayServerConfig& config);
355
356 void OnReadPacket(rtc::AsyncPacketSocket* socket,
357 const char* data,
358 size_t size,
359 const rtc::SocketAddress& remote_addr,
360 const rtc::PacketTime& packet_time);
361
362 void OnPortDestroyed(PortInterface* port);
363
364 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700365 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700366 rtc::Network* network_;
deadbeef5c3c1042017-08-04 15:01:57 -0700367 // Compared with the new best IP in DisableEquivalentPhases.
368 rtc::IPAddress previous_best_ip_;
honghaizf421bdc2015-07-17 16:21:55 -0700369 PortConfiguration* config_;
370 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200371 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700372 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700373 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700374 // There will be only one udp port per AllocationSequence.
375 UDPPort* udp_port_;
376 std::vector<TurnPort*> turn_ports_;
377 int phase_;
378};
379
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380} // namespace cricket
381
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200382#endif // P2P_CLIENT_BASICPORTALLOCATOR_H_