blob: f133bdfc7ecfa50c1938bb55ea85be3f07923631 [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
77class BasicPortAllocatorSession : public PortAllocatorSession,
78 public rtc::MessageHandler {
79 public:
80 BasicPortAllocatorSession(BasicPortAllocator* allocator,
81 const std::string& content_name,
82 int component,
83 const std::string& ice_ufrag,
84 const std::string& ice_pwd);
85 ~BasicPortAllocatorSession();
86
87 virtual BasicPortAllocator* allocator() { return allocator_; }
88 rtc::Thread* network_thread() { return network_thread_; }
89 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
90
Taylor Brandstetter417eebe2016-05-23 16:02:19 -070091 void SetCandidateFilter(uint32_t filter) override;
deadbeef653b8e02015-11-11 12:55:10 -080092 void StartGettingPorts() override;
93 void StopGettingPorts() override;
94 void ClearGettingPorts() override;
Taylor Brandstettera1c30352016-05-13 08:15:11 -070095 // These will all be cricket::Ports.
96 std::vector<PortInterface*> ReadyPorts() const override;
97 std::vector<Candidate> ReadyCandidates() const override;
98 bool CandidatesAllocationDone() const override;
Honghai Zhang5622c5e2016-07-01 13:59:29 -070099 void RegatherOnFailedNetworks() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100
101 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700102 void UpdateIceParametersInternal() override;
103
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104 // Starts the process of getting the port configurations.
105 virtual void GetPortConfigurations();
106
107 // Adds a port configuration that is now ready. Once we have one for each
108 // network (or a timeout occurs), we will start allocating ports.
109 virtual void ConfigReady(PortConfiguration* config);
110
111 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800112 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113
114 private:
115 class PortData {
116 public:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700117 PortData() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118 PortData(Port* port, AllocationSequence* seq)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700119 : port_(port), sequence_(seq) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700121 Port* port() const { return port_; }
122 AllocationSequence* sequence() const { return sequence_; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700123 bool has_pairable_candidate() const { return has_pairable_candidate_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700124 bool complete() const { return state_ == STATE_COMPLETE; }
125 bool error() const { return state_ == STATE_ERROR; }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700126 bool pruned() const { return state_ == STATE_PRUNED; }
127 bool inprogress() const { return state_ == STATE_INPROGRESS; }
128 // Returns true if this port is ready to be used.
129 bool ready() const {
130 return has_pairable_candidate_ && state_ != STATE_ERROR &&
131 state_ != STATE_PRUNED;
132 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700134 void set_pruned() { state_ = STATE_PRUNED; }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700135 void set_has_pairable_candidate(bool has_pairable_candidate) {
136 if (has_pairable_candidate) {
137 ASSERT(state_ == STATE_INPROGRESS);
138 }
139 has_pairable_candidate_ = has_pairable_candidate;
140 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141 void set_complete() {
142 state_ = STATE_COMPLETE;
143 }
144 void set_error() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700145 ASSERT(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146 state_ = STATE_ERROR;
147 }
148
149 private:
150 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700151 STATE_INPROGRESS, // Still gathering candidates.
152 STATE_COMPLETE, // All candidates allocated and ready for process.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700153 STATE_ERROR, // Error in gathering candidates.
154 STATE_PRUNED // Pruned by higher priority ports on the same network
155 // interface. Only TURN ports may be pruned.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000156 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700157 Port* port_ = nullptr;
158 AllocationSequence* sequence_ = nullptr;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 bool has_pairable_candidate_ = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700160 State state_ = STATE_INPROGRESS;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 };
162
163 void OnConfigReady(PortConfiguration* config);
164 void OnConfigStop();
165 void AllocatePorts();
166 void OnAllocate();
167 void DoAllocate();
168 void OnNetworksChanged();
169 void OnAllocationSequenceObjectsCreated();
170 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200171 PortConfiguration* config,
172 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 void AddAllocatedPort(Port* port, AllocationSequence* seq,
174 bool prepare_address);
175 void OnCandidateReady(Port* port, const Candidate& c);
176 void OnPortComplete(Port* port);
177 void OnPortError(Port* port);
178 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
179 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 void MaybeSignalCandidatesAllocationDone();
181 void OnPortAllocationComplete(AllocationSequence* seq);
182 PortData* FindPort(Port* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700183 std::vector<rtc::Network*> GetNetworks();
184 std::vector<rtc::Network*> GetFailedNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700186 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700187 bool CandidatePairable(const Candidate& c, const Port* port) const;
188 // Clear the related address according to the flags and candidate filter
189 // in order to avoid leaking any information.
190 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700192 // Removes the ports and candidates on given networks.
193 void RemovePortsAndCandidates(const std::vector<rtc::Network*>& networks);
194 // Gets filtered and sanitized candidates generated from a port and
195 // append to |candidates|.
196 void GetCandidatesFromPort(const PortData& data,
197 std::vector<Candidate>* candidates) const;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700198 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
199 // Returns true if at least one TURN port is pruned.
200 bool PruneTurnPorts(Port* newly_pairable_turn_port);
201
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 BasicPortAllocator* allocator_;
203 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700204 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 rtc::PacketSocketFactory* socket_factory_;
206 bool allocation_started_;
207 bool network_manager_started_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 bool allocation_sequences_created_;
209 std::vector<PortConfiguration*> configs_;
210 std::vector<AllocationSequence*> sequences_;
211 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700212 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700213 // Whether to prune low-priority ports, taken from the port allocator.
214 bool prune_turn_ports_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215
216 friend class AllocationSequence;
217};
218
219// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800220// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221struct PortConfiguration : public rtc::MessageData {
222 // TODO(jiayl): remove |stun_address| when Chrome is updated.
223 rtc::SocketAddress stun_address;
224 ServerAddresses stun_servers;
225 std::string username;
226 std::string password;
227
228 typedef std::vector<RelayServerConfig> RelayList;
229 RelayList relays;
230
231 // TODO(jiayl): remove this ctor when Chrome is updated.
232 PortConfiguration(const rtc::SocketAddress& stun_address,
233 const std::string& username,
234 const std::string& password);
235
236 PortConfiguration(const ServerAddresses& stun_servers,
237 const std::string& username,
238 const std::string& password);
239
deadbeefc5d0d952015-07-16 10:22:21 -0700240 // Returns addresses of both the explicitly configured STUN servers,
241 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000242 ServerAddresses StunServers();
243
244 // Adds another relay server, with the given ports and modifier, to the list.
245 void AddRelay(const RelayServerConfig& config);
246
247 // Determines whether the given relay server supports the given protocol.
248 bool SupportsProtocol(const RelayServerConfig& relay,
249 ProtocolType type) const;
250 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
251 // Helper method returns the server addresses for the matching RelayType and
252 // Protocol type.
253 ServerAddresses GetRelayServerAddresses(
254 RelayType turn_type, ProtocolType type) const;
255};
256
honghaizf421bdc2015-07-17 16:21:55 -0700257class UDPPort;
258class TurnPort;
259
260// Performs the allocation of ports, in a sequenced (timed) manner, for a given
261// network and IP address.
262class AllocationSequence : public rtc::MessageHandler,
263 public sigslot::has_slots<> {
264 public:
265 enum State {
266 kInit, // Initial state.
267 kRunning, // Started allocating ports.
268 kStopped, // Stopped from running.
269 kCompleted, // All ports are allocated.
270
271 // kInit --> kRunning --> {kCompleted|kStopped}
272 };
273 AllocationSequence(BasicPortAllocatorSession* session,
274 rtc::Network* network,
275 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200276 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700277 ~AllocationSequence();
278 bool Init();
279 void Clear();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700280 void OnNetworkFailed();
honghaizf421bdc2015-07-17 16:21:55 -0700281
282 State state() const { return state_; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700283 rtc::Network* network() const { return network_; }
284
285 bool network_failed() const { return network_failed_; }
286 void set_network_failed() { network_failed_ = true; }
honghaizf421bdc2015-07-17 16:21:55 -0700287
288 // Disables the phases for a new sequence that this one already covers for an
289 // equivalent network setup.
290 void DisableEquivalentPhases(rtc::Network* network,
291 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200292 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700293
294 // Starts and stops the sequence. When started, it will continue allocating
295 // new ports on its own timed schedule.
296 void Start();
297 void Stop();
298
299 // MessageHandler
300 void OnMessage(rtc::Message* msg);
301
302 void EnableProtocol(ProtocolType proto);
303 bool ProtocolEnabled(ProtocolType proto) const;
304
305 // Signal from AllocationSequence, when it's done with allocating ports.
306 // This signal is useful, when port allocation fails which doesn't result
307 // in any candidates. Using this signal BasicPortAllocatorSession can send
308 // its candidate discovery conclusion signal. Without this signal,
309 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
310 // can also be achieved by starting timer in BPAS.
311 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
312
313 protected:
314 // For testing.
315 void CreateTurnPort(const RelayServerConfig& config);
316
317 private:
318 typedef std::vector<ProtocolType> ProtocolList;
319
Peter Boström0c4e06b2015-10-07 12:23:21 +0200320 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700321 void CreateUDPPorts();
322 void CreateTCPPorts();
323 void CreateStunPorts();
324 void CreateRelayPorts();
325 void CreateGturnPort(const RelayServerConfig& config);
326
327 void OnReadPacket(rtc::AsyncPacketSocket* socket,
328 const char* data,
329 size_t size,
330 const rtc::SocketAddress& remote_addr,
331 const rtc::PacketTime& packet_time);
332
333 void OnPortDestroyed(PortInterface* port);
334
335 BasicPortAllocatorSession* session_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700336 bool network_failed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700337 rtc::Network* network_;
338 rtc::IPAddress ip_;
339 PortConfiguration* config_;
340 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200341 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700342 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700343 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700344 // There will be only one udp port per AllocationSequence.
345 UDPPort* udp_port_;
346 std::vector<TurnPort*> turn_ports_;
347 int phase_;
348};
349
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350} // namespace cricket
351
352#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_