blob: c66ae596c8a282400d7c0078fba33c84dcb584cb [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"
21#include "webrtc/base/scoped_ptr.h"
22#include "webrtc/base/thread.h"
23
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
deadbeef653b8e02015-11-11 12:55:10 -080041 void SetIceServers(
42 const ServerAddresses& stun_servers,
43 const std::vector<RelayServerConfig>& turn_servers) override {
44 stun_servers_ = stun_servers;
45 turn_servers_ = turn_servers;
46 }
47
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080048 // Set to kDefaultNetworkIgnoreMask by default.
49 void SetNetworkIgnoreMask(int network_ignore_mask) override {
50 // TODO(phoglund): implement support for other types than loopback.
51 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
52 // Then remove set_network_ignore_list from NetworkManager.
53 network_ignore_mask_ = network_ignore_mask;
54 }
55
56 int network_ignore_mask() const { return network_ignore_mask_; }
57
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058 rtc::NetworkManager* network_manager() { return network_manager_; }
59
60 // If socket_factory() is set to NULL each PortAllocatorSession
61 // creates its own socket factory.
62 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
63
64 const ServerAddresses& stun_servers() const {
65 return stun_servers_;
66 }
67
deadbeef653b8e02015-11-11 12:55:10 -080068 const std::vector<RelayServerConfig>& turn_servers() const {
69 return turn_servers_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070 }
deadbeef653b8e02015-11-11 12:55:10 -080071 virtual void AddTurnServer(const RelayServerConfig& turn_server) {
72 turn_servers_.push_back(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073 }
74
deadbeef653b8e02015-11-11 12:55:10 -080075 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 const std::string& content_name,
77 int component,
78 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080079 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080
81 private:
82 void Construct();
83
84 rtc::NetworkManager* network_manager_;
85 rtc::PacketSocketFactory* socket_factory_;
deadbeef653b8e02015-11-11 12:55:10 -080086 ServerAddresses stun_servers_;
87 std::vector<RelayServerConfig> turn_servers_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088 bool allow_tcp_listen_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080089 int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090};
91
92struct PortConfiguration;
93class AllocationSequence;
94
95class BasicPortAllocatorSession : public PortAllocatorSession,
96 public rtc::MessageHandler {
97 public:
98 BasicPortAllocatorSession(BasicPortAllocator* allocator,
99 const std::string& content_name,
100 int component,
101 const std::string& ice_ufrag,
102 const std::string& ice_pwd);
103 ~BasicPortAllocatorSession();
104
105 virtual BasicPortAllocator* allocator() { return allocator_; }
106 rtc::Thread* network_thread() { return network_thread_; }
107 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
108
deadbeef653b8e02015-11-11 12:55:10 -0800109 void StartGettingPorts() override;
110 void StopGettingPorts() override;
111 void ClearGettingPorts() override;
112 bool IsGettingPorts() override { return running_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113
114 protected:
115 // Starts the process of getting the port configurations.
116 virtual void GetPortConfigurations();
117
118 // Adds a port configuration that is now ready. Once we have one for each
119 // network (or a timeout occurs), we will start allocating ports.
120 virtual void ConfigReady(PortConfiguration* config);
121
122 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800123 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124
125 private:
126 class PortData {
127 public:
128 PortData() : port_(NULL), sequence_(NULL), state_(STATE_INIT) {}
129 PortData(Port* port, AllocationSequence* seq)
130 : port_(port), sequence_(seq), state_(STATE_INIT) {
131 }
132
133 Port* port() { return port_; }
134 AllocationSequence* sequence() { return sequence_; }
135 bool ready() const { return state_ == STATE_READY; }
136 bool complete() const {
137 // Returns true if candidate allocation has completed one way or another.
138 return ((state_ == STATE_COMPLETE) || (state_ == STATE_ERROR));
139 }
140
141 void set_ready() { ASSERT(state_ == STATE_INIT); state_ = STATE_READY; }
142 void set_complete() {
143 state_ = STATE_COMPLETE;
144 }
145 void set_error() {
146 ASSERT(state_ == STATE_INIT || state_ == STATE_READY);
147 state_ = STATE_ERROR;
148 }
149
150 private:
151 enum State {
152 STATE_INIT, // No candidates allocated yet.
153 STATE_READY, // At least one candidate is ready for process.
154 STATE_COMPLETE, // All candidates allocated and ready for process.
155 STATE_ERROR // Error in gathering candidates.
156 };
157 Port* port_;
158 AllocationSequence* sequence_;
159 State state_;
160 };
161
162 void OnConfigReady(PortConfiguration* config);
163 void OnConfigStop();
164 void AllocatePorts();
165 void OnAllocate();
166 void DoAllocate();
167 void OnNetworksChanged();
168 void OnAllocationSequenceObjectsCreated();
169 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200170 PortConfiguration* config,
171 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 void AddAllocatedPort(Port* port, AllocationSequence* seq,
173 bool prepare_address);
174 void OnCandidateReady(Port* port, const Candidate& c);
175 void OnPortComplete(Port* port);
176 void OnPortError(Port* port);
177 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
178 void OnPortDestroyed(PortInterface* port);
179 void OnShake();
180 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
185 bool CheckCandidateFilter(const Candidate& c);
186
187 BasicPortAllocator* allocator_;
188 rtc::Thread* network_thread_;
kwiberg3ec46792016-04-27 07:22:53 -0700189 std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 rtc::PacketSocketFactory* socket_factory_;
191 bool allocation_started_;
192 bool network_manager_started_;
193 bool running_; // set when StartGetAllPorts is called
194 bool allocation_sequences_created_;
195 std::vector<PortConfiguration*> configs_;
196 std::vector<AllocationSequence*> sequences_;
197 std::vector<PortData> ports_;
198
199 friend class AllocationSequence;
200};
201
202// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800203// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204struct PortConfiguration : public rtc::MessageData {
205 // TODO(jiayl): remove |stun_address| when Chrome is updated.
206 rtc::SocketAddress stun_address;
207 ServerAddresses stun_servers;
208 std::string username;
209 std::string password;
210
211 typedef std::vector<RelayServerConfig> RelayList;
212 RelayList relays;
213
214 // TODO(jiayl): remove this ctor when Chrome is updated.
215 PortConfiguration(const rtc::SocketAddress& stun_address,
216 const std::string& username,
217 const std::string& password);
218
219 PortConfiguration(const ServerAddresses& stun_servers,
220 const std::string& username,
221 const std::string& password);
222
deadbeefc5d0d952015-07-16 10:22:21 -0700223 // Returns addresses of both the explicitly configured STUN servers,
224 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225 ServerAddresses StunServers();
226
227 // Adds another relay server, with the given ports and modifier, to the list.
228 void AddRelay(const RelayServerConfig& config);
229
230 // Determines whether the given relay server supports the given protocol.
231 bool SupportsProtocol(const RelayServerConfig& relay,
232 ProtocolType type) const;
233 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
234 // Helper method returns the server addresses for the matching RelayType and
235 // Protocol type.
236 ServerAddresses GetRelayServerAddresses(
237 RelayType turn_type, ProtocolType type) const;
238};
239
honghaizf421bdc2015-07-17 16:21:55 -0700240class UDPPort;
241class TurnPort;
242
243// Performs the allocation of ports, in a sequenced (timed) manner, for a given
244// network and IP address.
245class AllocationSequence : public rtc::MessageHandler,
246 public sigslot::has_slots<> {
247 public:
248 enum State {
249 kInit, // Initial state.
250 kRunning, // Started allocating ports.
251 kStopped, // Stopped from running.
252 kCompleted, // All ports are allocated.
253
254 // kInit --> kRunning --> {kCompleted|kStopped}
255 };
256 AllocationSequence(BasicPortAllocatorSession* session,
257 rtc::Network* network,
258 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700260 ~AllocationSequence();
261 bool Init();
262 void Clear();
honghaiz8c404fa2015-09-28 07:59:43 -0700263 void OnNetworkRemoved();
honghaizf421bdc2015-07-17 16:21:55 -0700264
265 State state() const { return state_; }
honghaiz8c404fa2015-09-28 07:59:43 -0700266 const rtc::Network* network() const { return network_; }
267 bool network_removed() const { return network_removed_; }
honghaizf421bdc2015-07-17 16:21:55 -0700268
269 // Disables the phases for a new sequence that this one already covers for an
270 // equivalent network setup.
271 void DisableEquivalentPhases(rtc::Network* network,
272 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200273 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700274
275 // Starts and stops the sequence. When started, it will continue allocating
276 // new ports on its own timed schedule.
277 void Start();
278 void Stop();
279
280 // MessageHandler
281 void OnMessage(rtc::Message* msg);
282
283 void EnableProtocol(ProtocolType proto);
284 bool ProtocolEnabled(ProtocolType proto) const;
285
286 // Signal from AllocationSequence, when it's done with allocating ports.
287 // This signal is useful, when port allocation fails which doesn't result
288 // in any candidates. Using this signal BasicPortAllocatorSession can send
289 // its candidate discovery conclusion signal. Without this signal,
290 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
291 // can also be achieved by starting timer in BPAS.
292 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
293
294 protected:
295 // For testing.
296 void CreateTurnPort(const RelayServerConfig& config);
297
298 private:
299 typedef std::vector<ProtocolType> ProtocolList;
300
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700302 void CreateUDPPorts();
303 void CreateTCPPorts();
304 void CreateStunPorts();
305 void CreateRelayPorts();
306 void CreateGturnPort(const RelayServerConfig& config);
307
308 void OnReadPacket(rtc::AsyncPacketSocket* socket,
309 const char* data,
310 size_t size,
311 const rtc::SocketAddress& remote_addr,
312 const rtc::PacketTime& packet_time);
313
314 void OnPortDestroyed(PortInterface* port);
315
316 BasicPortAllocatorSession* session_;
honghaiz8c404fa2015-09-28 07:59:43 -0700317 bool network_removed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700318 rtc::Network* network_;
319 rtc::IPAddress ip_;
320 PortConfiguration* config_;
321 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200322 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700323 ProtocolList protocols_;
kwiberg3ec46792016-04-27 07:22:53 -0700324 std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
honghaizf421bdc2015-07-17 16:21:55 -0700325 // There will be only one udp port per AllocationSequence.
326 UDPPort* udp_port_;
327 std::vector<TurnPort*> turn_ports_;
328 int phase_;
329};
330
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000331} // namespace cricket
332
333#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_