blob: 204ff040a1deda0cff458a705c9d8beb708206bc [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 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700146 void set_pruned() { state_ = STATE_PRUNED; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700147 void set_has_pairable_candidate(bool has_pairable_candidate) {
148 if (has_pairable_candidate) {
149 ASSERT(state_ == STATE_INPROGRESS);
150 }
151 has_pairable_candidate_ = has_pairable_candidate;
152 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 void set_complete() {
154 state_ = STATE_COMPLETE;
155 }
156 void set_error() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700157 ASSERT(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 state_ = STATE_ERROR;
159 }
160
161 private:
162 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700163 STATE_INPROGRESS, // Still gathering candidates.
164 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700165 STATE_ERROR, // Error in gathering candidates.
166 STATE_PRUNED // Pruned by higher priority ports on the same network
167 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700169 Port* port_ = nullptr;
170 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700171 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700172 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 };
174
175 void OnConfigReady(PortConfiguration* config);
176 void OnConfigStop();
177 void AllocatePorts();
178 void OnAllocate();
179 void DoAllocate();
180 void OnNetworksChanged();
181 void OnAllocationSequenceObjectsCreated();
182 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200183 PortConfiguration* config,
184 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 void AddAllocatedPort(Port* port, AllocationSequence* seq,
186 bool prepare_address);
187 void OnCandidateReady(Port* port, const Candidate& c);
188 void OnPortComplete(Port* port);
189 void OnPortError(Port* port);
190 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
191 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 void MaybeSignalCandidatesAllocationDone();
193 void OnPortAllocationComplete(AllocationSequence* seq);
194 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700195 std::vector<rtc::Network*> GetNetworks();
196 std::vector<rtc::Network*> GetFailedNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700198 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700199 bool CandidatePairable(const Candidate& c, const Port* port) const;
200 // Clear the related address according to the flags and candidate filter
201 // in order to avoid leaking any information.
202 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700204 // Removes the ports and candidates on given networks.
205 void RemovePortsAndCandidates(const std::vector<rtc::Network*>& networks);
206 // Gets filtered and sanitized candidates generated from a port and
207 // append to |candidates|.
208 void GetCandidatesFromPort(const PortData& data,
209 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700210 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
211 // Returns true if at least one TURN port is pruned.
212 bool PruneTurnPorts(Port* newly_pairable_turn_port);
213
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 BasicPortAllocator* allocator_;
215 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700216 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217 rtc::PacketSocketFactory* socket_factory_;
218 bool allocation_started_;
219 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 bool allocation_sequences_created_;
221 std::vector<PortConfiguration*> configs_;
222 std::vector<AllocationSequence*> sequences_;
223 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700224 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700225 // Whether to prune low-priority ports, taken from the port allocator.
226 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700227 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228
229 friend class AllocationSequence;
230};
231
232// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800233// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234struct PortConfiguration : public rtc::MessageData {
235 // TODO(jiayl): remove |stun_address| when Chrome is updated.
236 rtc::SocketAddress stun_address;
237 ServerAddresses stun_servers;
238 std::string username;
239 std::string password;
240
241 typedef std::vector<RelayServerConfig> RelayList;
242 RelayList relays;
243
244 // TODO(jiayl): remove this ctor when Chrome is updated.
245 PortConfiguration(const rtc::SocketAddress& stun_address,
246 const std::string& username,
247 const std::string& password);
248
249 PortConfiguration(const ServerAddresses& stun_servers,
250 const std::string& username,
251 const std::string& password);
252
deadbeefc5d0d952015-07-16 10:22:21 -0700253 // Returns addresses of both the explicitly configured STUN servers,
254 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255 ServerAddresses StunServers();
256
257 // Adds another relay server, with the given ports and modifier, to the list.
258 void AddRelay(const RelayServerConfig& config);
259
260 // Determines whether the given relay server supports the given protocol.
261 bool SupportsProtocol(const RelayServerConfig& relay,
262 ProtocolType type) const;
263 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
264 // Helper method returns the server addresses for the matching RelayType and
265 // Protocol type.
266 ServerAddresses GetRelayServerAddresses(
267 RelayType turn_type, ProtocolType type) const;
268};
269
honghaizf421bdc2015-07-17 16:21:55 -0700270class UDPPort;
271class TurnPort;
272
273// Performs the allocation of ports, in a sequenced (timed) manner, for a given
274// network and IP address.
275class AllocationSequence : public rtc::MessageHandler,
276 public sigslot::has_slots<> {
277 public:
278 enum State {
279 kInit, // Initial state.
280 kRunning, // Started allocating ports.
281 kStopped, // Stopped from running.
282 kCompleted, // All ports are allocated.
283
284 // kInit --> kRunning --> {kCompleted|kStopped}
285 };
286 AllocationSequence(BasicPortAllocatorSession* session,
287 rtc::Network* network,
288 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200289 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700290 ~AllocationSequence();
Honghai Zhang5048f572016-08-23 15:47:33 -0700291 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700292 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700293 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700294
295 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700296 rtc::Network* network() const { return network_; }
297
298 bool network_failed() const { return network_failed_; }
299 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700300
301 // Disables the phases for a new sequence that this one already covers for an
302 // equivalent network setup.
303 void DisableEquivalentPhases(rtc::Network* network,
304 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200305 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700306
307 // Starts and stops the sequence. When started, it will continue allocating
308 // new ports on its own timed schedule.
309 void Start();
310 void Stop();
311
312 // MessageHandler
313 void OnMessage(rtc::Message* msg);
314
315 void EnableProtocol(ProtocolType proto);
316 bool ProtocolEnabled(ProtocolType proto) const;
317
318 // Signal from AllocationSequence, when it's done with allocating ports.
319 // This signal is useful, when port allocation fails which doesn't result
320 // in any candidates. Using this signal BasicPortAllocatorSession can send
321 // its candidate discovery conclusion signal. Without this signal,
322 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
323 // can also be achieved by starting timer in BPAS.
324 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
325
326 protected:
327 // For testing.
328 void CreateTurnPort(const RelayServerConfig& config);
329
330 private:
331 typedef std::vector<ProtocolType> ProtocolList;
332
Peter Boström0c4e06b2015-10-07 12:23:21 +0200333 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700334 void CreateUDPPorts();
335 void CreateTCPPorts();
336 void CreateStunPorts();
337 void CreateRelayPorts();
338 void CreateGturnPort(const RelayServerConfig& config);
339
340 void OnReadPacket(rtc::AsyncPacketSocket* socket,
341 const char* data,
342 size_t size,
343 const rtc::SocketAddress& remote_addr,
344 const rtc::PacketTime& packet_time);
345
346 void OnPortDestroyed(PortInterface* port);
347
348 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700349 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700350 rtc::Network* network_;
351 rtc::IPAddress ip_;
352 PortConfiguration* config_;
353 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200354 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700355 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700356 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700357 // There will be only one udp port per AllocationSequence.
358 UDPPort* udp_port_;
359 std::vector<TurnPort*> turn_ports_;
360 int phase_;
361};
362
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000363} // namespace cricket
364
365#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_