blob: d705fc528e0da50b34a3e597da4ff31f96e5bcf7 [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
11#ifndef WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_
12#define WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_
13
kwiberg3ec46792016-04-27 07:22:53 -070014#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015#include <string>
16#include <vector>
17
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include "webrtc/p2p/base/portallocator.h"
19#include "webrtc/base/messagequeue.h"
20#include "webrtc/base/network.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021#include "webrtc/base/thread.h"
22
23namespace cricket {
24
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025class BasicPortAllocator : public PortAllocator {
26 public:
27 BasicPortAllocator(rtc::NetworkManager* network_manager,
28 rtc::PacketSocketFactory* socket_factory);
29 explicit BasicPortAllocator(rtc::NetworkManager* network_manager);
30 BasicPortAllocator(rtc::NetworkManager* network_manager,
31 rtc::PacketSocketFactory* socket_factory,
32 const ServerAddresses& stun_servers);
33 BasicPortAllocator(rtc::NetworkManager* network_manager,
34 const ServerAddresses& stun_servers,
35 const rtc::SocketAddress& relay_server_udp,
36 const rtc::SocketAddress& relay_server_tcp,
37 const rtc::SocketAddress& relay_server_ssl);
38 virtual ~BasicPortAllocator();
39
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080040 // Set to kDefaultNetworkIgnoreMask by default.
41 void SetNetworkIgnoreMask(int network_ignore_mask) override {
42 // TODO(phoglund): implement support for other types than loopback.
43 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
44 // Then remove set_network_ignore_list from NetworkManager.
45 network_ignore_mask_ = network_ignore_mask;
46 }
47
48 int network_ignore_mask() const { return network_ignore_mask_; }
49
Honghai Zhang5622c5e2016-07-01 13:59:29 -070050 rtc::NetworkManager* network_manager() const { return network_manager_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051
52 // If socket_factory() is set to NULL each PortAllocatorSession
53 // creates its own socket factory.
54 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
55
deadbeef653b8e02015-11-11 12:55:10 -080056 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 const std::string& content_name,
58 int component,
59 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080060 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061
Taylor Brandstettera1c30352016-05-13 08:15:11 -070062 // Convenience method that adds a TURN server to the configuration.
63 void AddTurnServer(const RelayServerConfig& turn_server);
64
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065 private:
66 void Construct();
67
Honghai Zhangd93f50c2016-10-05 11:47:22 -070068 void OnIceRegathering(PortAllocatorSession* session,
69 IceRegatheringReason reason);
70
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071 rtc::NetworkManager* network_manager_;
72 rtc::PacketSocketFactory* socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080074 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075};
76
77struct PortConfiguration;
78class AllocationSequence;
79
Honghai Zhangd8f6fc42016-07-01 17:31:12 -070080enum class SessionState {
81 GATHERING, // Actively allocating ports and gathering candidates.
82 CLEARED, // Current allocation process has been stopped but may start
83 // new ones.
84 STOPPED // This session has completely stopped, no new allocation
85 // process will be started.
86};
87
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088class BasicPortAllocatorSession : public PortAllocatorSession,
89 public rtc::MessageHandler {
90 public:
91 BasicPortAllocatorSession(BasicPortAllocator* allocator,
92 const std::string& content_name,
93 int component,
94 const std::string& ice_ufrag,
95 const std::string& ice_pwd);
96 ~BasicPortAllocatorSession();
97
98 virtual BasicPortAllocator* allocator() { return allocator_; }
99 rtc::Thread* network_thread() { return network_thread_; }
100 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
101
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700102 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -0800103 void StartGettingPorts() override;
104 void StopGettingPorts() override;
105 void ClearGettingPorts() override;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700106 bool IsGettingPorts() override { return state_ == SessionState::GATHERING; }
107 bool IsCleared() const override { return state_ == SessionState::CLEARED; }
108 bool IsStopped() const override { return state_ == SessionState::STOPPED; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700109 // These will all be cricket::Ports.
110 std::vector<PortInterface*> ReadyPorts() const override;
111 std::vector<Candidate> ReadyCandidates() const override;
112 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700113 void RegatherOnFailedNetworks() override;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700114 void PruneAllPorts() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115
116 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700117 void UpdateIceParametersInternal() override;
118
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 // Starts the process of getting the port configurations.
120 virtual void GetPortConfigurations();
121
122 // Adds a port configuration that is now ready. Once we have one for each
123 // network (or a timeout occurs), we will start allocating ports.
124 virtual void ConfigReady(PortConfiguration* config);
125
126 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800127 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
129 private:
130 class PortData {
131 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700132 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700134 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700136 Port* port() const { return port_; }
137 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700138 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700139 bool complete() const { return state_ == STATE_COMPLETE; }
140 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700141 bool pruned() const { return state_ == STATE_PRUNED; }
142 bool inprogress() const { return state_ == STATE_INPROGRESS; }
143 // Returns true if this port is ready to be used.
144 bool ready() const {
145 return has_pairable_candidate_ && state_ != STATE_ERROR &&
146 state_ != STATE_PRUNED;
147 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700148 // Sets the state to "PRUNED" and prunes the Port.
149 void Prune() {
150 state_ = STATE_PRUNED;
151 if (port()) {
152 port()->Prune();
153 }
154 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700155 void set_has_pairable_candidate(bool has_pairable_candidate) {
156 if (has_pairable_candidate) {
157 ASSERT(state_ == STATE_INPROGRESS);
158 }
159 has_pairable_candidate_ = has_pairable_candidate;
160 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 void set_complete() {
162 state_ = STATE_COMPLETE;
163 }
164 void set_error() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700165 ASSERT(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 state_ = STATE_ERROR;
167 }
168
169 private:
170 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700171 STATE_INPROGRESS, // Still gathering candidates.
172 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700173 STATE_ERROR, // Error in gathering candidates.
174 STATE_PRUNED // Pruned by higher priority ports on the same network
175 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700177 Port* port_ = nullptr;
178 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700179 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700180 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181 };
182
183 void OnConfigReady(PortConfiguration* config);
184 void OnConfigStop();
185 void AllocatePorts();
186 void OnAllocate();
187 void DoAllocate();
188 void OnNetworksChanged();
189 void OnAllocationSequenceObjectsCreated();
190 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200191 PortConfiguration* config,
192 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 void AddAllocatedPort(Port* port, AllocationSequence* seq,
194 bool prepare_address);
195 void OnCandidateReady(Port* port, const Candidate& c);
196 void OnPortComplete(Port* port);
197 void OnPortError(Port* port);
198 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
199 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 void MaybeSignalCandidatesAllocationDone();
201 void OnPortAllocationComplete(AllocationSequence* seq);
202 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700203 std::vector<rtc::Network*> GetNetworks();
204 std::vector<rtc::Network*> GetFailedNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700206 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700207 bool CandidatePairable(const Candidate& c, const Port* port) const;
208 // Clear the related address according to the flags and candidate filter
209 // in order to avoid leaking any information.
210 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700212 std::vector<PortData*> GetUnprunedPorts(
213 const std::vector<rtc::Network*>& networks);
214 // Prunes ports and signal the remote side to remove the candidates that
215 // were previously signaled from these ports.
216 void PrunePortsAndRemoveCandidates(
217 const std::vector<PortData*>& port_data_list);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700218 // Gets filtered and sanitized candidates generated from a port and
219 // append to |candidates|.
220 void GetCandidatesFromPort(const PortData& data,
221 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700222 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
223 // Returns true if at least one TURN port is pruned.
224 bool PruneTurnPorts(Port* newly_pairable_turn_port);
225
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 BasicPortAllocator* allocator_;
227 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700228 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 rtc::PacketSocketFactory* socket_factory_;
230 bool allocation_started_;
231 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 bool allocation_sequences_created_;
233 std::vector<PortConfiguration*> configs_;
234 std::vector<AllocationSequence*> sequences_;
235 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700236 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700237 // Whether to prune low-priority ports, taken from the port allocator.
238 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700239 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240
241 friend class AllocationSequence;
242};
243
244// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800245// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246struct PortConfiguration : public rtc::MessageData {
247 // TODO(jiayl): remove |stun_address| when Chrome is updated.
248 rtc::SocketAddress stun_address;
249 ServerAddresses stun_servers;
250 std::string username;
251 std::string password;
252
253 typedef std::vector<RelayServerConfig> RelayList;
254 RelayList relays;
255
256 // TODO(jiayl): remove this ctor when Chrome is updated.
257 PortConfiguration(const rtc::SocketAddress& stun_address,
258 const std::string& username,
259 const std::string& password);
260
261 PortConfiguration(const ServerAddresses& stun_servers,
262 const std::string& username,
263 const std::string& password);
264
deadbeefc5d0d952015-07-16 10:22:21 -0700265 // Returns addresses of both the explicitly configured STUN servers,
266 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000267 ServerAddresses StunServers();
268
269 // Adds another relay server, with the given ports and modifier, to the list.
270 void AddRelay(const RelayServerConfig& config);
271
272 // Determines whether the given relay server supports the given protocol.
273 bool SupportsProtocol(const RelayServerConfig& relay,
274 ProtocolType type) const;
275 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
276 // Helper method returns the server addresses for the matching RelayType and
277 // Protocol type.
278 ServerAddresses GetRelayServerAddresses(
279 RelayType turn_type, ProtocolType type) const;
280};
281
honghaizf421bdc2015-07-17 16:21:55 -0700282class UDPPort;
283class TurnPort;
284
285// Performs the allocation of ports, in a sequenced (timed) manner, for a given
286// network and IP address.
287class AllocationSequence : public rtc::MessageHandler,
288 public sigslot::has_slots<> {
289 public:
290 enum State {
291 kInit, // Initial state.
292 kRunning, // Started allocating ports.
293 kStopped, // Stopped from running.
294 kCompleted, // All ports are allocated.
295
296 // kInit --> kRunning --> {kCompleted|kStopped}
297 };
298 AllocationSequence(BasicPortAllocatorSession* session,
299 rtc::Network* network,
300 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700302 ~AllocationSequence();
Honghai Zhang5048f572016-08-23 15:47:33 -0700303 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700304 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700305 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700306
307 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700308 rtc::Network* network() const { return network_; }
309
310 bool network_failed() const { return network_failed_; }
311 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700312
313 // Disables the phases for a new sequence that this one already covers for an
314 // equivalent network setup.
315 void DisableEquivalentPhases(rtc::Network* network,
316 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200317 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700318
319 // Starts and stops the sequence. When started, it will continue allocating
320 // new ports on its own timed schedule.
321 void Start();
322 void Stop();
323
324 // MessageHandler
325 void OnMessage(rtc::Message* msg);
326
327 void EnableProtocol(ProtocolType proto);
328 bool ProtocolEnabled(ProtocolType proto) const;
329
330 // Signal from AllocationSequence, when it's done with allocating ports.
331 // This signal is useful, when port allocation fails which doesn't result
332 // in any candidates. Using this signal BasicPortAllocatorSession can send
333 // its candidate discovery conclusion signal. Without this signal,
334 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
335 // can also be achieved by starting timer in BPAS.
336 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
337
338 protected:
339 // For testing.
340 void CreateTurnPort(const RelayServerConfig& config);
341
342 private:
343 typedef std::vector<ProtocolType> ProtocolList;
344
Peter Boström0c4e06b2015-10-07 12:23:21 +0200345 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700346 void CreateUDPPorts();
347 void CreateTCPPorts();
348 void CreateStunPorts();
349 void CreateRelayPorts();
350 void CreateGturnPort(const RelayServerConfig& config);
351
352 void OnReadPacket(rtc::AsyncPacketSocket* socket,
353 const char* data,
354 size_t size,
355 const rtc::SocketAddress& remote_addr,
356 const rtc::PacketTime& packet_time);
357
358 void OnPortDestroyed(PortInterface* port);
359
360 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700361 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700362 rtc::Network* network_;
363 rtc::IPAddress ip_;
364 PortConfiguration* config_;
365 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200366 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700367 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700368 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700369 // There will be only one udp port per AllocationSequence.
370 UDPPort* udp_port_;
371 std::vector<TurnPort*> turn_ports_;
372 int phase_;
373};
374
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375} // namespace cricket
376
377#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_