blob: 7cdf0baa7dfdb6ebaec6cd564c3037cac08bda23 [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"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/checks.h"
20#include "webrtc/rtc_base/messagequeue.h"
21#include "webrtc/rtc_base/network.h"
22#include "webrtc/rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000023
24namespace cricket {
25
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026class BasicPortAllocator : public PortAllocator {
27 public:
28 BasicPortAllocator(rtc::NetworkManager* network_manager,
29 rtc::PacketSocketFactory* socket_factory);
30 explicit BasicPortAllocator(rtc::NetworkManager* network_manager);
31 BasicPortAllocator(rtc::NetworkManager* network_manager,
32 rtc::PacketSocketFactory* socket_factory,
33 const ServerAddresses& stun_servers);
34 BasicPortAllocator(rtc::NetworkManager* network_manager,
35 const ServerAddresses& stun_servers,
36 const rtc::SocketAddress& relay_server_udp,
37 const rtc::SocketAddress& relay_server_tcp,
38 const rtc::SocketAddress& relay_server_ssl);
39 virtual ~BasicPortAllocator();
40
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080041 // Set to kDefaultNetworkIgnoreMask by default.
42 void SetNetworkIgnoreMask(int network_ignore_mask) override {
43 // TODO(phoglund): implement support for other types than loopback.
44 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
45 // Then remove set_network_ignore_list from NetworkManager.
46 network_ignore_mask_ = network_ignore_mask;
47 }
48
49 int network_ignore_mask() const { return network_ignore_mask_; }
50
Honghai Zhang5622c5e2016-07-01 13:59:29 -070051 rtc::NetworkManager* network_manager() const { return network_manager_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052
53 // If socket_factory() is set to NULL each PortAllocatorSession
54 // creates its own socket factory.
55 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
56
deadbeef653b8e02015-11-11 12:55:10 -080057 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058 const std::string& content_name,
59 int component,
60 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080061 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062
Taylor Brandstettera1c30352016-05-13 08:15:11 -070063 // Convenience method that adds a TURN server to the configuration.
64 void AddTurnServer(const RelayServerConfig& turn_server);
65
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066 private:
67 void Construct();
68
Honghai Zhangd93f50c2016-10-05 11:47:22 -070069 void OnIceRegathering(PortAllocatorSession* session,
70 IceRegatheringReason reason);
71
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 rtc::NetworkManager* network_manager_;
73 rtc::PacketSocketFactory* socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080075 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076};
77
78struct PortConfiguration;
79class AllocationSequence;
80
Honghai Zhangd8f6fc42016-07-01 17:31:12 -070081enum class SessionState {
82 GATHERING, // Actively allocating ports and gathering candidates.
83 CLEARED, // Current allocation process has been stopped but may start
84 // new ones.
85 STOPPED // This session has completely stopped, no new allocation
86 // process will be started.
87};
88
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089class BasicPortAllocatorSession : public PortAllocatorSession,
90 public rtc::MessageHandler {
91 public:
92 BasicPortAllocatorSession(BasicPortAllocator* allocator,
93 const std::string& content_name,
94 int component,
95 const std::string& ice_ufrag,
96 const std::string& ice_pwd);
97 ~BasicPortAllocatorSession();
98
99 virtual BasicPortAllocator* allocator() { return allocator_; }
100 rtc::Thread* network_thread() { return network_thread_; }
101 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
102
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700103 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -0800104 void StartGettingPorts() override;
105 void StopGettingPorts() override;
106 void ClearGettingPorts() override;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700107 bool IsGettingPorts() override { return state_ == SessionState::GATHERING; }
108 bool IsCleared() const override { return state_ == SessionState::CLEARED; }
109 bool IsStopped() const override { return state_ == SessionState::STOPPED; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700110 // These will all be cricket::Ports.
111 std::vector<PortInterface*> ReadyPorts() const override;
112 std::vector<Candidate> ReadyCandidates() const override;
113 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700114 void RegatherOnFailedNetworks() override;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700115 void PruneAllPorts() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116
117 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700118 void UpdateIceParametersInternal() override;
119
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 // Starts the process of getting the port configurations.
121 virtual void GetPortConfigurations();
122
123 // Adds a port configuration that is now ready. Once we have one for each
124 // network (or a timeout occurs), we will start allocating ports.
125 virtual void ConfigReady(PortConfiguration* config);
126
127 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800128 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129
130 private:
131 class PortData {
132 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700133 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700135 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700137 Port* port() const { return port_; }
138 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700139 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700140 bool complete() const { return state_ == STATE_COMPLETE; }
141 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700142 bool pruned() const { return state_ == STATE_PRUNED; }
143 bool inprogress() const { return state_ == STATE_INPROGRESS; }
144 // Returns true if this port is ready to be used.
145 bool ready() const {
146 return has_pairable_candidate_ && state_ != STATE_ERROR &&
147 state_ != STATE_PRUNED;
148 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700149 // Sets the state to "PRUNED" and prunes the Port.
150 void Prune() {
151 state_ = STATE_PRUNED;
152 if (port()) {
153 port()->Prune();
154 }
155 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700156 void set_has_pairable_candidate(bool has_pairable_candidate) {
157 if (has_pairable_candidate) {
nisseede5da42017-01-12 05:15:36 -0800158 RTC_DCHECK(state_ == STATE_INPROGRESS);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 }
160 has_pairable_candidate_ = has_pairable_candidate;
161 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162 void set_complete() {
163 state_ = STATE_COMPLETE;
164 }
165 void set_error() {
nisseede5da42017-01-12 05:15:36 -0800166 RTC_DCHECK(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167 state_ = STATE_ERROR;
168 }
169
170 private:
171 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700172 STATE_INPROGRESS, // Still gathering candidates.
173 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700174 STATE_ERROR, // Error in gathering candidates.
175 STATE_PRUNED // Pruned by higher priority ports on the same network
176 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700178 Port* port_ = nullptr;
179 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700180 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700181 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000182 };
183
184 void OnConfigReady(PortConfiguration* config);
185 void OnConfigStop();
186 void AllocatePorts();
187 void OnAllocate();
188 void DoAllocate();
189 void OnNetworksChanged();
190 void OnAllocationSequenceObjectsCreated();
191 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200192 PortConfiguration* config,
193 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 void AddAllocatedPort(Port* port, AllocationSequence* seq,
195 bool prepare_address);
196 void OnCandidateReady(Port* port, const Candidate& c);
197 void OnPortComplete(Port* port);
198 void OnPortError(Port* port);
199 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
200 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 void MaybeSignalCandidatesAllocationDone();
202 void OnPortAllocationComplete(AllocationSequence* seq);
203 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700204 std::vector<rtc::Network*> GetNetworks();
205 std::vector<rtc::Network*> GetFailedNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700207 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700208 bool CandidatePairable(const Candidate& c, const Port* port) const;
209 // Clear the related address according to the flags and candidate filter
210 // in order to avoid leaking any information.
211 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700213 std::vector<PortData*> GetUnprunedPorts(
214 const std::vector<rtc::Network*>& networks);
215 // Prunes ports and signal the remote side to remove the candidates that
216 // were previously signaled from these ports.
217 void PrunePortsAndRemoveCandidates(
218 const std::vector<PortData*>& port_data_list);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700219 // Gets filtered and sanitized candidates generated from a port and
220 // append to |candidates|.
221 void GetCandidatesFromPort(const PortData& data,
222 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700223 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
224 // Returns true if at least one TURN port is pruned.
225 bool PruneTurnPorts(Port* newly_pairable_turn_port);
226
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227 BasicPortAllocator* allocator_;
228 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700229 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000230 rtc::PacketSocketFactory* socket_factory_;
231 bool allocation_started_;
232 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 bool allocation_sequences_created_;
234 std::vector<PortConfiguration*> configs_;
235 std::vector<AllocationSequence*> sequences_;
236 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700237 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700238 // Whether to prune low-priority ports, taken from the port allocator.
239 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700240 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241
242 friend class AllocationSequence;
243};
244
245// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800246// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247struct PortConfiguration : public rtc::MessageData {
248 // TODO(jiayl): remove |stun_address| when Chrome is updated.
249 rtc::SocketAddress stun_address;
250 ServerAddresses stun_servers;
251 std::string username;
252 std::string password;
253
254 typedef std::vector<RelayServerConfig> RelayList;
255 RelayList relays;
256
257 // TODO(jiayl): remove this ctor when Chrome is updated.
258 PortConfiguration(const rtc::SocketAddress& stun_address,
259 const std::string& username,
260 const std::string& password);
261
262 PortConfiguration(const ServerAddresses& stun_servers,
263 const std::string& username,
264 const std::string& password);
265
deadbeefc5d0d952015-07-16 10:22:21 -0700266 // Returns addresses of both the explicitly configured STUN servers,
267 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268 ServerAddresses StunServers();
269
270 // Adds another relay server, with the given ports and modifier, to the list.
271 void AddRelay(const RelayServerConfig& config);
272
273 // Determines whether the given relay server supports the given protocol.
274 bool SupportsProtocol(const RelayServerConfig& relay,
275 ProtocolType type) const;
276 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
277 // Helper method returns the server addresses for the matching RelayType and
278 // Protocol type.
279 ServerAddresses GetRelayServerAddresses(
280 RelayType turn_type, ProtocolType type) const;
281};
282
honghaizf421bdc2015-07-17 16:21:55 -0700283class UDPPort;
284class TurnPort;
285
286// Performs the allocation of ports, in a sequenced (timed) manner, for a given
287// network and IP address.
288class AllocationSequence : public rtc::MessageHandler,
289 public sigslot::has_slots<> {
290 public:
291 enum State {
292 kInit, // Initial state.
293 kRunning, // Started allocating ports.
294 kStopped, // Stopped from running.
295 kCompleted, // All ports are allocated.
296
297 // kInit --> kRunning --> {kCompleted|kStopped}
298 };
299 AllocationSequence(BasicPortAllocatorSession* session,
300 rtc::Network* network,
301 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200302 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700303 ~AllocationSequence();
Honghai Zhang5048f572016-08-23 15:47:33 -0700304 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700305 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700306 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700307
308 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700309 rtc::Network* network() const { return network_; }
310
311 bool network_failed() const { return network_failed_; }
312 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700313
314 // Disables the phases for a new sequence that this one already covers for an
315 // equivalent network setup.
316 void DisableEquivalentPhases(rtc::Network* network,
317 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200318 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700319
320 // Starts and stops the sequence. When started, it will continue allocating
321 // new ports on its own timed schedule.
322 void Start();
323 void Stop();
324
325 // MessageHandler
326 void OnMessage(rtc::Message* msg);
327
328 void EnableProtocol(ProtocolType proto);
329 bool ProtocolEnabled(ProtocolType proto) const;
330
331 // Signal from AllocationSequence, when it's done with allocating ports.
332 // This signal is useful, when port allocation fails which doesn't result
333 // in any candidates. Using this signal BasicPortAllocatorSession can send
334 // its candidate discovery conclusion signal. Without this signal,
335 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
336 // can also be achieved by starting timer in BPAS.
337 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
338
339 protected:
340 // For testing.
341 void CreateTurnPort(const RelayServerConfig& config);
342
343 private:
344 typedef std::vector<ProtocolType> ProtocolList;
345
Peter Boström0c4e06b2015-10-07 12:23:21 +0200346 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700347 void CreateUDPPorts();
348 void CreateTCPPorts();
349 void CreateStunPorts();
350 void CreateRelayPorts();
351 void CreateGturnPort(const RelayServerConfig& config);
352
353 void OnReadPacket(rtc::AsyncPacketSocket* socket,
354 const char* data,
355 size_t size,
356 const rtc::SocketAddress& remote_addr,
357 const rtc::PacketTime& packet_time);
358
359 void OnPortDestroyed(PortInterface* port);
360
361 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700362 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700363 rtc::Network* network_;
364 rtc::IPAddress ip_;
365 PortConfiguration* config_;
366 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200367 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700368 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700369 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700370 // There will be only one udp port per AllocationSequence.
371 UDPPort* udp_port_;
372 std::vector<TurnPort*> turn_ports_;
373 int phase_;
374};
375
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000376} // namespace cricket
377
378#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_