blob: 3044559a0bab2cf0d48248c6af4707516d832dce [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
68 rtc::NetworkManager* network_manager_;
69 rtc::PacketSocketFactory* socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080071 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072};
73
74struct PortConfiguration;
75class AllocationSequence;
76
Honghai Zhangd8f6fc42016-07-01 17:31:12 -070077enum class SessionState {
78 GATHERING, // Actively allocating ports and gathering candidates.
79 CLEARED, // Current allocation process has been stopped but may start
80 // new ones.
81 STOPPED // This session has completely stopped, no new allocation
82 // process will be started.
83};
84
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085class BasicPortAllocatorSession : public PortAllocatorSession,
86 public rtc::MessageHandler {
87 public:
88 BasicPortAllocatorSession(BasicPortAllocator* allocator,
89 const std::string& content_name,
90 int component,
91 const std::string& ice_ufrag,
92 const std::string& ice_pwd);
93 ~BasicPortAllocatorSession();
94
95 virtual BasicPortAllocator* allocator() { return allocator_; }
96 rtc::Thread* network_thread() { return network_thread_; }
97 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
98
Taylor Brandstetter417eebe2016-05-23 16:02:19 -070099 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -0800100 void StartGettingPorts() override;
101 void StopGettingPorts() override;
102 void ClearGettingPorts() override;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700103 bool IsGettingPorts() override { return state_ == SessionState::GATHERING; }
104 bool IsCleared() const override { return state_ == SessionState::CLEARED; }
105 bool IsStopped() const override { return state_ == SessionState::STOPPED; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700106 // These will all be cricket::Ports.
107 std::vector<PortInterface*> ReadyPorts() const override;
108 std::vector<Candidate> ReadyCandidates() const override;
109 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700110 void RegatherOnFailedNetworks() override;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700111 void PruneAllPorts() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
113 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700114 void UpdateIceParametersInternal() override;
115
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 // Starts the process of getting the port configurations.
117 virtual void GetPortConfigurations();
118
119 // Adds a port configuration that is now ready. Once we have one for each
120 // network (or a timeout occurs), we will start allocating ports.
121 virtual void ConfigReady(PortConfiguration* config);
122
123 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800124 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125
126 private:
127 class PortData {
128 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700129 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700131 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700133 Port* port() const { return port_; }
134 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700135 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700136 bool complete() const { return state_ == STATE_COMPLETE; }
137 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700138 bool pruned() const { return state_ == STATE_PRUNED; }
139 bool inprogress() const { return state_ == STATE_INPROGRESS; }
140 // Returns true if this port is ready to be used.
141 bool ready() const {
142 return has_pairable_candidate_ && state_ != STATE_ERROR &&
143 state_ != STATE_PRUNED;
144 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700145 // Sets the state to "PRUNED" and prunes the Port.
146 void Prune() {
147 state_ = STATE_PRUNED;
148 if (port()) {
149 port()->Prune();
150 }
151 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700152 void set_has_pairable_candidate(bool has_pairable_candidate) {
153 if (has_pairable_candidate) {
154 ASSERT(state_ == STATE_INPROGRESS);
155 }
156 has_pairable_candidate_ = has_pairable_candidate;
157 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 void set_complete() {
159 state_ = STATE_COMPLETE;
160 }
161 void set_error() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700162 ASSERT(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 state_ = STATE_ERROR;
164 }
165
166 private:
167 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700168 STATE_INPROGRESS, // Still gathering candidates.
169 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700170 STATE_ERROR, // Error in gathering candidates.
171 STATE_PRUNED // Pruned by higher priority ports on the same network
172 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700174 Port* port_ = nullptr;
175 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700176 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700177 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000178 };
179
180 void OnConfigReady(PortConfiguration* config);
181 void OnConfigStop();
182 void AllocatePorts();
183 void OnAllocate();
184 void DoAllocate();
185 void OnNetworksChanged();
186 void OnAllocationSequenceObjectsCreated();
187 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200188 PortConfiguration* config,
189 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 void AddAllocatedPort(Port* port, AllocationSequence* seq,
191 bool prepare_address);
192 void OnCandidateReady(Port* port, const Candidate& c);
193 void OnPortComplete(Port* port);
194 void OnPortError(Port* port);
195 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
196 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 void MaybeSignalCandidatesAllocationDone();
198 void OnPortAllocationComplete(AllocationSequence* seq);
199 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700200 std::vector<rtc::Network*> GetNetworks();
201 std::vector<rtc::Network*> GetFailedNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700203 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700204 bool CandidatePairable(const Candidate& c, const Port* port) const;
205 // Clear the related address according to the flags and candidate filter
206 // in order to avoid leaking any information.
207 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700209 std::vector<PortData*> GetUnprunedPorts(
210 const std::vector<rtc::Network*>& networks);
211 // Prunes ports and signal the remote side to remove the candidates that
212 // were previously signaled from these ports.
213 void PrunePortsAndRemoveCandidates(
214 const std::vector<PortData*>& port_data_list);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700215 // Gets filtered and sanitized candidates generated from a port and
216 // append to |candidates|.
217 void GetCandidatesFromPort(const PortData& data,
218 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700219 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
220 // Returns true if at least one TURN port is pruned.
221 bool PruneTurnPorts(Port* newly_pairable_turn_port);
222
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000223 BasicPortAllocator* allocator_;
224 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700225 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 rtc::PacketSocketFactory* socket_factory_;
227 bool allocation_started_;
228 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 bool allocation_sequences_created_;
230 std::vector<PortConfiguration*> configs_;
231 std::vector<AllocationSequence*> sequences_;
232 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700233 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700234 // Whether to prune low-priority ports, taken from the port allocator.
235 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700236 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000237
238 friend class AllocationSequence;
239};
240
241// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800242// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243struct PortConfiguration : public rtc::MessageData {
244 // TODO(jiayl): remove |stun_address| when Chrome is updated.
245 rtc::SocketAddress stun_address;
246 ServerAddresses stun_servers;
247 std::string username;
248 std::string password;
249
250 typedef std::vector<RelayServerConfig> RelayList;
251 RelayList relays;
252
253 // TODO(jiayl): remove this ctor when Chrome is updated.
254 PortConfiguration(const rtc::SocketAddress& stun_address,
255 const std::string& username,
256 const std::string& password);
257
258 PortConfiguration(const ServerAddresses& stun_servers,
259 const std::string& username,
260 const std::string& password);
261
deadbeefc5d0d952015-07-16 10:22:21 -0700262 // Returns addresses of both the explicitly configured STUN servers,
263 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000264 ServerAddresses StunServers();
265
266 // Adds another relay server, with the given ports and modifier, to the list.
267 void AddRelay(const RelayServerConfig& config);
268
269 // Determines whether the given relay server supports the given protocol.
270 bool SupportsProtocol(const RelayServerConfig& relay,
271 ProtocolType type) const;
272 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
273 // Helper method returns the server addresses for the matching RelayType and
274 // Protocol type.
275 ServerAddresses GetRelayServerAddresses(
276 RelayType turn_type, ProtocolType type) const;
277};
278
honghaizf421bdc2015-07-17 16:21:55 -0700279class UDPPort;
280class TurnPort;
281
282// Performs the allocation of ports, in a sequenced (timed) manner, for a given
283// network and IP address.
284class AllocationSequence : public rtc::MessageHandler,
285 public sigslot::has_slots<> {
286 public:
287 enum State {
288 kInit, // Initial state.
289 kRunning, // Started allocating ports.
290 kStopped, // Stopped from running.
291 kCompleted, // All ports are allocated.
292
293 // kInit --> kRunning --> {kCompleted|kStopped}
294 };
295 AllocationSequence(BasicPortAllocatorSession* session,
296 rtc::Network* network,
297 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200298 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700299 ~AllocationSequence();
Honghai Zhang5048f572016-08-23 15:47:33 -0700300 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700301 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700302 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700303
304 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700305 rtc::Network* network() const { return network_; }
306
307 bool network_failed() const { return network_failed_; }
308 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700309
310 // Disables the phases for a new sequence that this one already covers for an
311 // equivalent network setup.
312 void DisableEquivalentPhases(rtc::Network* network,
313 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200314 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700315
316 // Starts and stops the sequence. When started, it will continue allocating
317 // new ports on its own timed schedule.
318 void Start();
319 void Stop();
320
321 // MessageHandler
322 void OnMessage(rtc::Message* msg);
323
324 void EnableProtocol(ProtocolType proto);
325 bool ProtocolEnabled(ProtocolType proto) const;
326
327 // Signal from AllocationSequence, when it's done with allocating ports.
328 // This signal is useful, when port allocation fails which doesn't result
329 // in any candidates. Using this signal BasicPortAllocatorSession can send
330 // its candidate discovery conclusion signal. Without this signal,
331 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
332 // can also be achieved by starting timer in BPAS.
333 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
334
335 protected:
336 // For testing.
337 void CreateTurnPort(const RelayServerConfig& config);
338
339 private:
340 typedef std::vector<ProtocolType> ProtocolList;
341
Peter Boström0c4e06b2015-10-07 12:23:21 +0200342 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700343 void CreateUDPPorts();
344 void CreateTCPPorts();
345 void CreateStunPorts();
346 void CreateRelayPorts();
347 void CreateGturnPort(const RelayServerConfig& config);
348
349 void OnReadPacket(rtc::AsyncPacketSocket* socket,
350 const char* data,
351 size_t size,
352 const rtc::SocketAddress& remote_addr,
353 const rtc::PacketTime& packet_time);
354
355 void OnPortDestroyed(PortInterface* port);
356
357 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700358 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700359 rtc::Network* network_;
360 rtc::IPAddress ip_;
361 PortConfiguration* config_;
362 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200363 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700364 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700365 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700366 // There will be only one udp port per AllocationSequence.
367 UDPPort* udp_port_;
368 std::vector<TurnPort*> turn_ports_;
369 int phase_;
370};
371
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000372} // namespace cricket
373
374#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_