blob: bddb3967e544b37ff3ea62039903a9d6091de3e1 [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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 rtc::NetworkManager* network_manager() { return network_manager_; }
51
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;
95 bool IsGettingPorts() override { return running_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -070096 // These will all be cricket::Ports.
97 std::vector<PortInterface*> ReadyPorts() const override;
98 std::vector<Candidate> ReadyCandidates() const override;
99 bool CandidatesAllocationDone() const 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;
159 State state_ = STATE_INPROGRESS;
160 bool has_pairable_candidate_ = false;
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);
honghaiz8c404fa2015-09-28 07:59:43 -0700183 void GetNetworks(std::vector<rtc::Network*>* networks);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700185 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700186 bool CandidatePairable(const Candidate& c, const Port* port) const;
187 // Clear the related address according to the flags and candidate filter
188 // in order to avoid leaking any information.
189 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700191 Port* GetBestTurnPortForNetwork(const std::string& network_name) const;
192 // Returns true if at least one TURN port is pruned.
193 bool PruneTurnPorts(Port* newly_pairable_turn_port);
194
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000195 BasicPortAllocator* allocator_;
196 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700197 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 rtc::PacketSocketFactory* socket_factory_;
199 bool allocation_started_;
200 bool network_manager_started_;
201 bool running_; // set when StartGetAllPorts is called
202 bool allocation_sequences_created_;
203 std::vector<PortConfiguration*> configs_;
204 std::vector<AllocationSequence*> sequences_;
205 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700206 uint32_t candidate_filter_ = CF_ALL;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700207 // Whether to prune low-priority ports, taken from the port allocator.
208 bool prune_turn_ports_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209
210 friend class AllocationSequence;
211};
212
213// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800214// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215struct PortConfiguration : public rtc::MessageData {
216 // TODO(jiayl): remove |stun_address| when Chrome is updated.
217 rtc::SocketAddress stun_address;
218 ServerAddresses stun_servers;
219 std::string username;
220 std::string password;
221
222 typedef std::vector<RelayServerConfig> RelayList;
223 RelayList relays;
224
225 // TODO(jiayl): remove this ctor when Chrome is updated.
226 PortConfiguration(const rtc::SocketAddress& stun_address,
227 const std::string& username,
228 const std::string& password);
229
230 PortConfiguration(const ServerAddresses& stun_servers,
231 const std::string& username,
232 const std::string& password);
233
deadbeefc5d0d952015-07-16 10:22:21 -0700234 // Returns addresses of both the explicitly configured STUN servers,
235 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 ServerAddresses StunServers();
237
238 // Adds another relay server, with the given ports and modifier, to the list.
239 void AddRelay(const RelayServerConfig& config);
240
241 // Determines whether the given relay server supports the given protocol.
242 bool SupportsProtocol(const RelayServerConfig& relay,
243 ProtocolType type) const;
244 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
245 // Helper method returns the server addresses for the matching RelayType and
246 // Protocol type.
247 ServerAddresses GetRelayServerAddresses(
248 RelayType turn_type, ProtocolType type) const;
249};
250
honghaizf421bdc2015-07-17 16:21:55 -0700251class UDPPort;
252class TurnPort;
253
254// Performs the allocation of ports, in a sequenced (timed) manner, for a given
255// network and IP address.
256class AllocationSequence : public rtc::MessageHandler,
257 public sigslot::has_slots<> {
258 public:
259 enum State {
260 kInit, // Initial state.
261 kRunning, // Started allocating ports.
262 kStopped, // Stopped from running.
263 kCompleted, // All ports are allocated.
264
265 // kInit --> kRunning --> {kCompleted|kStopped}
266 };
267 AllocationSequence(BasicPortAllocatorSession* session,
268 rtc::Network* network,
269 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200270 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700271 ~AllocationSequence();
272 bool Init();
273 void Clear();
honghaiz8c404fa2015-09-28 07:59:43 -0700274 void OnNetworkRemoved();
honghaizf421bdc2015-07-17 16:21:55 -0700275
276 State state() const { return state_; }
honghaiz8c404fa2015-09-28 07:59:43 -0700277 const rtc::Network* network() const { return network_; }
278 bool network_removed() const { return network_removed_; }
honghaizf421bdc2015-07-17 16:21:55 -0700279
280 // Disables the phases for a new sequence that this one already covers for an
281 // equivalent network setup.
282 void DisableEquivalentPhases(rtc::Network* network,
283 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200284 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700285
286 // Starts and stops the sequence. When started, it will continue allocating
287 // new ports on its own timed schedule.
288 void Start();
289 void Stop();
290
291 // MessageHandler
292 void OnMessage(rtc::Message* msg);
293
294 void EnableProtocol(ProtocolType proto);
295 bool ProtocolEnabled(ProtocolType proto) const;
296
297 // Signal from AllocationSequence, when it's done with allocating ports.
298 // This signal is useful, when port allocation fails which doesn't result
299 // in any candidates. Using this signal BasicPortAllocatorSession can send
300 // its candidate discovery conclusion signal. Without this signal,
301 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
302 // can also be achieved by starting timer in BPAS.
303 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
304
305 protected:
306 // For testing.
307 void CreateTurnPort(const RelayServerConfig& config);
308
309 private:
310 typedef std::vector<ProtocolType> ProtocolList;
311
Peter Boström0c4e06b2015-10-07 12:23:21 +0200312 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700313 void CreateUDPPorts();
314 void CreateTCPPorts();
315 void CreateStunPorts();
316 void CreateRelayPorts();
317 void CreateGturnPort(const RelayServerConfig& config);
318
319 void OnReadPacket(rtc::AsyncPacketSocket* socket,
320 const char* data,
321 size_t size,
322 const rtc::SocketAddress& remote_addr,
323 const rtc::PacketTime& packet_time);
324
325 void OnPortDestroyed(PortInterface* port);
326
327 BasicPortAllocatorSession* session_;
honghaiz8c404fa2015-09-28 07:59:43 -0700328 bool network_removed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700329 rtc::Network* network_;
330 rtc::IPAddress ip_;
331 PortConfiguration* config_;
332 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200333 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700334 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700335 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700336 // There will be only one udp port per AllocationSequence.
337 UDPPort* udp_port_;
338 std::vector<TurnPort*> turn_ports_;
339 int phase_;
340};
341
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000342} // namespace cricket
343
344#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_