blob: d9cfc4d81a74ffdf1217f8685371258b771b87b7 [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;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111
112 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700113 void UpdateIceParametersInternal() override;
114
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 // Starts the process of getting the port configurations.
116 virtual void GetPortConfigurations();
117
118 // Adds a port configuration that is now ready. Once we have one for each
119 // network (or a timeout occurs), we will start allocating ports.
120 virtual void ConfigReady(PortConfiguration* config);
121
122 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800123 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124
125 private:
126 class PortData {
127 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700128 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700130 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700132 Port* port() const { return port_; }
133 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700134 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700135 bool complete() const { return state_ == STATE_COMPLETE; }
136 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700137 bool pruned() const { return state_ == STATE_PRUNED; }
138 bool inprogress() const { return state_ == STATE_INPROGRESS; }
139 // Returns true if this port is ready to be used.
140 bool ready() const {
141 return has_pairable_candidate_ && state_ != STATE_ERROR &&
142 state_ != STATE_PRUNED;
143 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700145 void set_pruned() { state_ = STATE_PRUNED; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700146 void set_has_pairable_candidate(bool has_pairable_candidate) {
147 if (has_pairable_candidate) {
148 ASSERT(state_ == STATE_INPROGRESS);
149 }
150 has_pairable_candidate_ = has_pairable_candidate;
151 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 void set_complete() {
153 state_ = STATE_COMPLETE;
154 }
155 void set_error() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700156 ASSERT(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 state_ = STATE_ERROR;
158 }
159
160 private:
161 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700162 STATE_INPROGRESS, // Still gathering candidates.
163 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700164 STATE_ERROR, // Error in gathering candidates.
165 STATE_PRUNED // Pruned by higher priority ports on the same network
166 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700168 Port* port_ = nullptr;
169 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700170 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700171 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 };
173
174 void OnConfigReady(PortConfiguration* config);
175 void OnConfigStop();
176 void AllocatePorts();
177 void OnAllocate();
178 void DoAllocate();
179 void OnNetworksChanged();
180 void OnAllocationSequenceObjectsCreated();
181 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200182 PortConfiguration* config,
183 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 void AddAllocatedPort(Port* port, AllocationSequence* seq,
185 bool prepare_address);
186 void OnCandidateReady(Port* port, const Candidate& c);
187 void OnPortComplete(Port* port);
188 void OnPortError(Port* port);
189 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
190 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 void MaybeSignalCandidatesAllocationDone();
192 void OnPortAllocationComplete(AllocationSequence* seq);
193 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700194 std::vector<rtc::Network*> GetNetworks();
195 std::vector<rtc::Network*> GetFailedNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700197 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700198 bool CandidatePairable(const Candidate& c, const Port* port) const;
199 // Clear the related address according to the flags and candidate filter
200 // in order to avoid leaking any information.
201 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700203 // Removes the ports and candidates on given networks.
204 void RemovePortsAndCandidates(const std::vector<rtc::Network*>& networks);
205 // Gets filtered and sanitized candidates generated from a port and
206 // append to |candidates|.
207 void GetCandidatesFromPort(const PortData& data,
208 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700209 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
210 // Returns true if at least one TURN port is pruned.
211 bool PruneTurnPorts(Port* newly_pairable_turn_port);
212
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 BasicPortAllocator* allocator_;
214 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700215 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 rtc::PacketSocketFactory* socket_factory_;
217 bool allocation_started_;
218 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 bool allocation_sequences_created_;
220 std::vector<PortConfiguration*> configs_;
221 std::vector<AllocationSequence*> sequences_;
222 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700223 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700224 // Whether to prune low-priority ports, taken from the port allocator.
225 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700226 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227
228 friend class AllocationSequence;
229};
230
231// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800232// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233struct PortConfiguration : public rtc::MessageData {
234 // TODO(jiayl): remove |stun_address| when Chrome is updated.
235 rtc::SocketAddress stun_address;
236 ServerAddresses stun_servers;
237 std::string username;
238 std::string password;
239
240 typedef std::vector<RelayServerConfig> RelayList;
241 RelayList relays;
242
243 // TODO(jiayl): remove this ctor when Chrome is updated.
244 PortConfiguration(const rtc::SocketAddress& stun_address,
245 const std::string& username,
246 const std::string& password);
247
248 PortConfiguration(const ServerAddresses& stun_servers,
249 const std::string& username,
250 const std::string& password);
251
deadbeefc5d0d952015-07-16 10:22:21 -0700252 // Returns addresses of both the explicitly configured STUN servers,
253 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 ServerAddresses StunServers();
255
256 // Adds another relay server, with the given ports and modifier, to the list.
257 void AddRelay(const RelayServerConfig& config);
258
259 // Determines whether the given relay server supports the given protocol.
260 bool SupportsProtocol(const RelayServerConfig& relay,
261 ProtocolType type) const;
262 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
263 // Helper method returns the server addresses for the matching RelayType and
264 // Protocol type.
265 ServerAddresses GetRelayServerAddresses(
266 RelayType turn_type, ProtocolType type) const;
267};
268
honghaizf421bdc2015-07-17 16:21:55 -0700269class UDPPort;
270class TurnPort;
271
272// Performs the allocation of ports, in a sequenced (timed) manner, for a given
273// network and IP address.
274class AllocationSequence : public rtc::MessageHandler,
275 public sigslot::has_slots<> {
276 public:
277 enum State {
278 kInit, // Initial state.
279 kRunning, // Started allocating ports.
280 kStopped, // Stopped from running.
281 kCompleted, // All ports are allocated.
282
283 // kInit --> kRunning --> {kCompleted|kStopped}
284 };
285 AllocationSequence(BasicPortAllocatorSession* session,
286 rtc::Network* network,
287 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200288 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700289 ~AllocationSequence();
290 bool Init();
291 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700292 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700293
294 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700295 rtc::Network* network() const { return network_; }
296
297 bool network_failed() const { return network_failed_; }
298 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700299
300 // Disables the phases for a new sequence that this one already covers for an
301 // equivalent network setup.
302 void DisableEquivalentPhases(rtc::Network* network,
303 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200304 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700305
306 // Starts and stops the sequence. When started, it will continue allocating
307 // new ports on its own timed schedule.
308 void Start();
309 void Stop();
310
311 // MessageHandler
312 void OnMessage(rtc::Message* msg);
313
314 void EnableProtocol(ProtocolType proto);
315 bool ProtocolEnabled(ProtocolType proto) const;
316
317 // Signal from AllocationSequence, when it's done with allocating ports.
318 // This signal is useful, when port allocation fails which doesn't result
319 // in any candidates. Using this signal BasicPortAllocatorSession can send
320 // its candidate discovery conclusion signal. Without this signal,
321 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
322 // can also be achieved by starting timer in BPAS.
323 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
324
325 protected:
326 // For testing.
327 void CreateTurnPort(const RelayServerConfig& config);
328
329 private:
330 typedef std::vector<ProtocolType> ProtocolList;
331
Peter Boström0c4e06b2015-10-07 12:23:21 +0200332 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700333 void CreateUDPPorts();
334 void CreateTCPPorts();
335 void CreateStunPorts();
336 void CreateRelayPorts();
337 void CreateGturnPort(const RelayServerConfig& config);
338
339 void OnReadPacket(rtc::AsyncPacketSocket* socket,
340 const char* data,
341 size_t size,
342 const rtc::SocketAddress& remote_addr,
343 const rtc::PacketTime& packet_time);
344
345 void OnPortDestroyed(PortInterface* port);
346
347 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700348 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700349 rtc::Network* network_;
350 rtc::IPAddress ip_;
351 PortConfiguration* config_;
352 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200353 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700354 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700355 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700356 // There will be only one udp port per AllocationSequence.
357 UDPPort* udp_port_;
358 std::vector<TurnPort*> turn_ports_;
359 int phase_;
360};
361
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000362} // namespace cricket
363
364#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_