blob: 8ea66c431e76d560a6299c2831d77a1a83dfeb06 [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; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700127 void set_has_pairable_candidate(bool has_pairable_candidate) {
128 if (has_pairable_candidate) {
129 ASSERT(state_ == STATE_INPROGRESS);
130 }
131 has_pairable_candidate_ = has_pairable_candidate;
132 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 void set_complete() {
134 state_ = STATE_COMPLETE;
135 }
136 void set_error() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700137 ASSERT(state_ == STATE_INPROGRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 state_ = STATE_ERROR;
139 }
140
141 private:
142 enum State {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700143 STATE_INPROGRESS, // Still gathering candidates.
144 STATE_COMPLETE, // All candidates allocated and ready for process.
145 STATE_ERROR // Error in gathering candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146 };
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700147 Port* port_ = nullptr;
148 AllocationSequence* sequence_ = nullptr;
149 State state_ = STATE_INPROGRESS;
150 bool has_pairable_candidate_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 };
152
153 void OnConfigReady(PortConfiguration* config);
154 void OnConfigStop();
155 void AllocatePorts();
156 void OnAllocate();
157 void DoAllocate();
158 void OnNetworksChanged();
159 void OnAllocationSequenceObjectsCreated();
160 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 PortConfiguration* config,
162 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 void AddAllocatedPort(Port* port, AllocationSequence* seq,
164 bool prepare_address);
165 void OnCandidateReady(Port* port, const Candidate& c);
166 void OnPortComplete(Port* port);
167 void OnPortError(Port* port);
168 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
169 void OnPortDestroyed(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 void MaybeSignalCandidatesAllocationDone();
171 void OnPortAllocationComplete(AllocationSequence* seq);
172 PortData* FindPort(Port* port);
honghaiz8c404fa2015-09-28 07:59:43 -0700173 void GetNetworks(std::vector<rtc::Network*>* networks);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000174
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700175 bool CheckCandidateFilter(const Candidate& c) const;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700176 bool CandidatePairable(const Candidate& c, const Port* port) const;
177 // Clear the related address according to the flags and candidate filter
178 // in order to avoid leaking any information.
179 Candidate SanitizeRelatedAddress(const Candidate& c) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180
181 BasicPortAllocator* allocator_;
182 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700183 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 rtc::PacketSocketFactory* socket_factory_;
185 bool allocation_started_;
186 bool network_manager_started_;
187 bool running_; // set when StartGetAllPorts is called
188 bool allocation_sequences_created_;
189 std::vector<PortConfiguration*> configs_;
190 std::vector<AllocationSequence*> sequences_;
191 std::vector<PortData> ports_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700192 uint32_t candidate_filter_ = CF_ALL;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193
194 friend class AllocationSequence;
195};
196
197// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800198// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199struct PortConfiguration : public rtc::MessageData {
200 // TODO(jiayl): remove |stun_address| when Chrome is updated.
201 rtc::SocketAddress stun_address;
202 ServerAddresses stun_servers;
203 std::string username;
204 std::string password;
205
206 typedef std::vector<RelayServerConfig> RelayList;
207 RelayList relays;
208
209 // TODO(jiayl): remove this ctor when Chrome is updated.
210 PortConfiguration(const rtc::SocketAddress& stun_address,
211 const std::string& username,
212 const std::string& password);
213
214 PortConfiguration(const ServerAddresses& stun_servers,
215 const std::string& username,
216 const std::string& password);
217
deadbeefc5d0d952015-07-16 10:22:21 -0700218 // Returns addresses of both the explicitly configured STUN servers,
219 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 ServerAddresses StunServers();
221
222 // Adds another relay server, with the given ports and modifier, to the list.
223 void AddRelay(const RelayServerConfig& config);
224
225 // Determines whether the given relay server supports the given protocol.
226 bool SupportsProtocol(const RelayServerConfig& relay,
227 ProtocolType type) const;
228 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
229 // Helper method returns the server addresses for the matching RelayType and
230 // Protocol type.
231 ServerAddresses GetRelayServerAddresses(
232 RelayType turn_type, ProtocolType type) const;
233};
234
honghaizf421bdc2015-07-17 16:21:55 -0700235class UDPPort;
236class TurnPort;
237
238// Performs the allocation of ports, in a sequenced (timed) manner, for a given
239// network and IP address.
240class AllocationSequence : public rtc::MessageHandler,
241 public sigslot::has_slots<> {
242 public:
243 enum State {
244 kInit, // Initial state.
245 kRunning, // Started allocating ports.
246 kStopped, // Stopped from running.
247 kCompleted, // All ports are allocated.
248
249 // kInit --> kRunning --> {kCompleted|kStopped}
250 };
251 AllocationSequence(BasicPortAllocatorSession* session,
252 rtc::Network* network,
253 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700255 ~AllocationSequence();
256 bool Init();
257 void Clear();
honghaiz8c404fa2015-09-28 07:59:43 -0700258 void OnNetworkRemoved();
honghaizf421bdc2015-07-17 16:21:55 -0700259
260 State state() const { return state_; }
honghaiz8c404fa2015-09-28 07:59:43 -0700261 const rtc::Network* network() const { return network_; }
262 bool network_removed() const { return network_removed_; }
honghaizf421bdc2015-07-17 16:21:55 -0700263
264 // Disables the phases for a new sequence that this one already covers for an
265 // equivalent network setup.
266 void DisableEquivalentPhases(rtc::Network* network,
267 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200268 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700269
270 // Starts and stops the sequence. When started, it will continue allocating
271 // new ports on its own timed schedule.
272 void Start();
273 void Stop();
274
275 // MessageHandler
276 void OnMessage(rtc::Message* msg);
277
278 void EnableProtocol(ProtocolType proto);
279 bool ProtocolEnabled(ProtocolType proto) const;
280
281 // Signal from AllocationSequence, when it's done with allocating ports.
282 // This signal is useful, when port allocation fails which doesn't result
283 // in any candidates. Using this signal BasicPortAllocatorSession can send
284 // its candidate discovery conclusion signal. Without this signal,
285 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
286 // can also be achieved by starting timer in BPAS.
287 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
288
289 protected:
290 // For testing.
291 void CreateTurnPort(const RelayServerConfig& config);
292
293 private:
294 typedef std::vector<ProtocolType> ProtocolList;
295
Peter Boström0c4e06b2015-10-07 12:23:21 +0200296 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700297 void CreateUDPPorts();
298 void CreateTCPPorts();
299 void CreateStunPorts();
300 void CreateRelayPorts();
301 void CreateGturnPort(const RelayServerConfig& config);
302
303 void OnReadPacket(rtc::AsyncPacketSocket* socket,
304 const char* data,
305 size_t size,
306 const rtc::SocketAddress& remote_addr,
307 const rtc::PacketTime& packet_time);
308
309 void OnPortDestroyed(PortInterface* port);
310
311 BasicPortAllocatorSession* session_;
honghaiz8c404fa2015-09-28 07:59:43 -0700312 bool network_removed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700313 rtc::Network* network_;
314 rtc::IPAddress ip_;
315 PortConfiguration* config_;
316 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200317 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700318 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700319 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700320 // There will be only one udp port per AllocationSequence.
321 UDPPort* udp_port_;
322 std::vector<TurnPort*> turn_ports_;
323 int phase_;
324};
325
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000326} // namespace cricket
327
328#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_