blob: 071d6f3c6e41d72dc938dd6eaba3049a850a1396 [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
jonasoc251cb12017-08-29 03:20:58 -070024namespace webrtc {
25class RtcEventLog;
26} // namespace webrtc
27
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028namespace cricket {
29
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030class BasicPortAllocator : public PortAllocator {
31 public:
32 BasicPortAllocator(rtc::NetworkManager* network_manager,
jonasoc251cb12017-08-29 03:20:58 -070033 rtc::PacketSocketFactory* socket_factory,
34 webrtc::RtcEventLog* event_log);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035 explicit BasicPortAllocator(rtc::NetworkManager* network_manager);
36 BasicPortAllocator(rtc::NetworkManager* network_manager,
37 rtc::PacketSocketFactory* socket_factory,
38 const ServerAddresses& stun_servers);
39 BasicPortAllocator(rtc::NetworkManager* network_manager,
40 const ServerAddresses& stun_servers,
41 const rtc::SocketAddress& relay_server_udp,
42 const rtc::SocketAddress& relay_server_tcp,
43 const rtc::SocketAddress& relay_server_ssl);
44 virtual ~BasicPortAllocator();
45
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080046 // Set to kDefaultNetworkIgnoreMask by default.
47 void SetNetworkIgnoreMask(int network_ignore_mask) override {
48 // TODO(phoglund): implement support for other types than loopback.
49 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
50 // Then remove set_network_ignore_list from NetworkManager.
51 network_ignore_mask_ = network_ignore_mask;
52 }
53
54 int network_ignore_mask() const { return network_ignore_mask_; }
55
Honghai Zhang5622c5e2016-07-01 13:59:29 -070056 rtc::NetworkManager* network_manager() const { return network_manager_; }
jonasoc251cb12017-08-29 03:20:58 -070057 webrtc::RtcEventLog* event_log() const { return event_log_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058
59 // If socket_factory() is set to NULL each PortAllocatorSession
60 // creates its own socket factory.
61 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
62
deadbeef653b8e02015-11-11 12:55:10 -080063 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064 const std::string& content_name,
65 int component,
66 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080067 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068
Taylor Brandstettera1c30352016-05-13 08:15:11 -070069 // Convenience method that adds a TURN server to the configuration.
70 void AddTurnServer(const RelayServerConfig& turn_server);
71
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 private:
73 void Construct();
74
Honghai Zhangd93f50c2016-10-05 11:47:22 -070075 void OnIceRegathering(PortAllocatorSession* session,
76 IceRegatheringReason reason);
77
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078 rtc::NetworkManager* network_manager_;
79 rtc::PacketSocketFactory* socket_factory_;
jonasoc251cb12017-08-29 03:20:58 -070080 webrtc::RtcEventLog* event_log_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080082 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083};
84
85struct PortConfiguration;
86class AllocationSequence;
87
Honghai Zhangd8f6fc42016-07-01 17:31:12 -070088enum class SessionState {
89 GATHERING, // Actively allocating ports and gathering candidates.
90 CLEARED, // Current allocation process has been stopped but may start
91 // new ones.
92 STOPPED // This session has completely stopped, no new allocation
93 // process will be started.
94};
95
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096class BasicPortAllocatorSession : public PortAllocatorSession,
97 public rtc::MessageHandler {
98 public:
99 BasicPortAllocatorSession(BasicPortAllocator* allocator,
100 const std::string& content_name,
101 int component,
102 const std::string& ice_ufrag,
103 const std::string& ice_pwd);
104 ~BasicPortAllocatorSession();
105
106 virtual BasicPortAllocator* allocator() { return allocator_; }
107 rtc::Thread* network_thread() { return network_thread_; }
108 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
jonasoc251cb12017-08-29 03:20:58 -0700109 webrtc::RtcEventLog* event_log() { return allocator_->event_log(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700111 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -0800112 void StartGettingPorts() override;
113 void StopGettingPorts() override;
114 void ClearGettingPorts() override;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700115 bool IsGettingPorts() override { return state_ == SessionState::GATHERING; }
116 bool IsCleared() const override { return state_ == SessionState::CLEARED; }
117 bool IsStopped() const override { return state_ == SessionState::STOPPED; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700118 // These will all be cricket::Ports.
119 std::vector<PortInterface*> ReadyPorts() const override;
120 std::vector<Candidate> ReadyCandidates() const override;
121 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700122 void RegatherOnFailedNetworks() override;
Steve Anton300bf8e2017-07-14 10:13:10 -0700123 void RegatherOnAllNetworks() override;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700124 void PruneAllPorts() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125
126 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700127 void UpdateIceParametersInternal() override;
128
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 // Starts the process of getting the port configurations.
130 virtual void GetPortConfigurations();
131
132 // Adds a port configuration that is now ready. Once we have one for each
133 // network (or a timeout occurs), we will start allocating ports.
134 virtual void ConfigReady(PortConfiguration* config);
135
136 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800137 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138
139 private:
140 class PortData {
141 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700142 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700144 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700146 Port* port() const { return port_; }
147 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700148 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700149 bool complete() const { return state_ == STATE_COMPLETE; }
150 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700151 bool pruned() const { return state_ == STATE_PRUNED; }
152 bool inprogress() const { return state_ == STATE_INPROGRESS; }
153 // Returns true if this port is ready to be used.
154 bool ready() const {
155 return has_pairable_candidate_ && state_ != STATE_ERROR &&
156 state_ != STATE_PRUNED;
157 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700158 // Sets the state to "PRUNED" and prunes the Port.
159 void Prune() {
160 state_ = STATE_PRUNED;
161 if (port()) {
162 port()->Prune();
163 }
164 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700165 void set_has_pairable_candidate(bool has_pairable_candidate) {
166 if (has_pairable_candidate) {
nisseede5da42017-01-12 05:15:36 -0800167 RTC_DCHECK(state_ == STATE_INPROGRESS);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700168 }
169 has_pairable_candidate_ = has_pairable_candidate;
170 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 void set_complete() {
172 state_ = STATE_COMPLETE;
173 }
174 void set_error() {
nisseede5da42017-01-12 05:15:36 -0800175 RTC_DCHECK(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176 state_ = STATE_ERROR;
177 }
178
179 private:
180 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700181 STATE_INPROGRESS, // Still gathering candidates.
182 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700183 STATE_ERROR, // Error in gathering candidates.
184 STATE_PRUNED // Pruned by higher priority ports on the same network
185 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700187 Port* port_ = nullptr;
188 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700189 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700190 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 };
192
193 void OnConfigReady(PortConfiguration* config);
194 void OnConfigStop();
195 void AllocatePorts();
196 void OnAllocate();
Steve Anton300bf8e2017-07-14 10:13:10 -0700197 void DoAllocate(bool disable_equivalent_phases);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 void OnNetworksChanged();
199 void OnAllocationSequenceObjectsCreated();
200 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200201 PortConfiguration* config,
202 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203 void AddAllocatedPort(Port* port, AllocationSequence* seq,
204 bool prepare_address);
205 void OnCandidateReady(Port* port, const Candidate& c);
206 void OnPortComplete(Port* port);
207 void OnPortError(Port* port);
208 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
209 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 void MaybeSignalCandidatesAllocationDone();
211 void OnPortAllocationComplete(AllocationSequence* seq);
212 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700213 std::vector<rtc::Network*> GetNetworks();
214 std::vector<rtc::Network*> GetFailedNetworks();
Steve Anton300bf8e2017-07-14 10:13:10 -0700215 void Regather(const std::vector<rtc::Network*>& networks,
216 bool disable_equivalent_phases,
217 IceRegatheringReason reason);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700219 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700220 bool CandidatePairable(const Candidate& c, const Port* port) const;
221 // Clear the related address according to the flags and candidate filter
222 // in order to avoid leaking any information.
223 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700225 std::vector<PortData*> GetUnprunedPorts(
226 const std::vector<rtc::Network*>& networks);
227 // Prunes ports and signal the remote side to remove the candidates that
228 // were previously signaled from these ports.
229 void PrunePortsAndRemoveCandidates(
230 const std::vector<PortData*>& port_data_list);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700231 // Gets filtered and sanitized candidates generated from a port and
232 // append to |candidates|.
233 void GetCandidatesFromPort(const PortData& data,
234 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700235 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
236 // Returns true if at least one TURN port is pruned.
237 bool PruneTurnPorts(Port* newly_pairable_turn_port);
238
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 BasicPortAllocator* allocator_;
240 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700241 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000242 rtc::PacketSocketFactory* socket_factory_;
243 bool allocation_started_;
244 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 bool allocation_sequences_created_;
246 std::vector<PortConfiguration*> configs_;
247 std::vector<AllocationSequence*> sequences_;
248 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700249 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700250 // Whether to prune low-priority ports, taken from the port allocator.
251 bool prune_turn_ports_;
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700252 SessionState state_ = SessionState::CLEARED;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253
254 friend class AllocationSequence;
255};
256
257// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800258// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000259struct PortConfiguration : public rtc::MessageData {
260 // TODO(jiayl): remove |stun_address| when Chrome is updated.
261 rtc::SocketAddress stun_address;
262 ServerAddresses stun_servers;
263 std::string username;
264 std::string password;
265
266 typedef std::vector<RelayServerConfig> RelayList;
267 RelayList relays;
268
269 // TODO(jiayl): remove this ctor when Chrome is updated.
270 PortConfiguration(const rtc::SocketAddress& stun_address,
271 const std::string& username,
272 const std::string& password);
273
274 PortConfiguration(const ServerAddresses& stun_servers,
275 const std::string& username,
276 const std::string& password);
277
deadbeefc5d0d952015-07-16 10:22:21 -0700278 // Returns addresses of both the explicitly configured STUN servers,
279 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000280 ServerAddresses StunServers();
281
282 // Adds another relay server, with the given ports and modifier, to the list.
283 void AddRelay(const RelayServerConfig& config);
284
285 // Determines whether the given relay server supports the given protocol.
286 bool SupportsProtocol(const RelayServerConfig& relay,
287 ProtocolType type) const;
288 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
289 // Helper method returns the server addresses for the matching RelayType and
290 // Protocol type.
291 ServerAddresses GetRelayServerAddresses(
292 RelayType turn_type, ProtocolType type) const;
293};
294
honghaizf421bdc2015-07-17 16:21:55 -0700295class UDPPort;
296class TurnPort;
297
298// Performs the allocation of ports, in a sequenced (timed) manner, for a given
299// network and IP address.
300class AllocationSequence : public rtc::MessageHandler,
301 public sigslot::has_slots<> {
302 public:
303 enum State {
304 kInit, // Initial state.
305 kRunning, // Started allocating ports.
306 kStopped, // Stopped from running.
307 kCompleted, // All ports are allocated.
308
309 // kInit --> kRunning --> {kCompleted|kStopped}
310 };
311 AllocationSequence(BasicPortAllocatorSession* session,
312 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 ~AllocationSequence();
Honghai Zhang5048f572016-08-23 15:47:33 -0700316 void Init();
honghaizf421bdc2015-07-17 16:21:55 -0700317 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700318 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700319
320 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700321 rtc::Network* network() const { return network_; }
322
323 bool network_failed() const { return network_failed_; }
324 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700325
326 // Disables the phases for a new sequence that this one already covers for an
327 // equivalent network setup.
328 void DisableEquivalentPhases(rtc::Network* network,
329 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200330 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700331
332 // Starts and stops the sequence. When started, it will continue allocating
333 // new ports on its own timed schedule.
334 void Start();
335 void Stop();
336
337 // MessageHandler
338 void OnMessage(rtc::Message* msg);
339
340 void EnableProtocol(ProtocolType proto);
341 bool ProtocolEnabled(ProtocolType proto) const;
342
343 // Signal from AllocationSequence, when it's done with allocating ports.
344 // This signal is useful, when port allocation fails which doesn't result
345 // in any candidates. Using this signal BasicPortAllocatorSession can send
346 // its candidate discovery conclusion signal. Without this signal,
347 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
348 // can also be achieved by starting timer in BPAS.
349 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
350
351 protected:
352 // For testing.
353 void CreateTurnPort(const RelayServerConfig& config);
354
355 private:
356 typedef std::vector<ProtocolType> ProtocolList;
357
Peter Boström0c4e06b2015-10-07 12:23:21 +0200358 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700359 void CreateUDPPorts();
360 void CreateTCPPorts();
361 void CreateStunPorts();
362 void CreateRelayPorts();
363 void CreateGturnPort(const RelayServerConfig& config);
364
365 void OnReadPacket(rtc::AsyncPacketSocket* socket,
366 const char* data,
367 size_t size,
368 const rtc::SocketAddress& remote_addr,
369 const rtc::PacketTime& packet_time);
370
371 void OnPortDestroyed(PortInterface* port);
372
373 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700374 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700375 rtc::Network* network_;
deadbeef5c3c1042017-08-04 15:01:57 -0700376 // Compared with the new best IP in DisableEquivalentPhases.
377 rtc::IPAddress previous_best_ip_;
honghaizf421bdc2015-07-17 16:21:55 -0700378 PortConfiguration* config_;
379 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200380 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700381 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700382 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700383 // There will be only one udp port per AllocationSequence.
384 UDPPort* udp_port_;
385 std::vector<TurnPort*> turn_ports_;
386 int phase_;
387};
388
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389} // namespace cricket
390
391#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_