blob: 6c301de30205cda289942d029541dc228a1daa8b [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
14#include <string>
15#include <vector>
16
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/portallocator.h"
18#include "webrtc/base/messagequeue.h"
19#include "webrtc/base/network.h"
20#include "webrtc/base/scoped_ptr.h"
21#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
deadbeef653b8e02015-11-11 12:55:10 -080040 void SetIceServers(
41 const ServerAddresses& stun_servers,
42 const std::vector<RelayServerConfig>& turn_servers) override {
43 stun_servers_ = stun_servers;
44 turn_servers_ = turn_servers;
45 }
46
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 rtc::NetworkManager* network_manager() { return network_manager_; }
48
49 // If socket_factory() is set to NULL each PortAllocatorSession
50 // creates its own socket factory.
51 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
52
53 const ServerAddresses& stun_servers() const {
54 return stun_servers_;
55 }
56
deadbeef653b8e02015-11-11 12:55:10 -080057 const std::vector<RelayServerConfig>& turn_servers() const {
58 return turn_servers_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 }
deadbeef653b8e02015-11-11 12:55:10 -080060 virtual void AddTurnServer(const RelayServerConfig& turn_server) {
61 turn_servers_.push_back(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062 }
63
deadbeef653b8e02015-11-11 12:55:10 -080064 PortAllocatorSession* CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065 const std::string& content_name,
66 int component,
67 const std::string& ice_ufrag,
deadbeef653b8e02015-11-11 12:55:10 -080068 const std::string& ice_pwd) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069
70 private:
71 void Construct();
72
73 rtc::NetworkManager* network_manager_;
74 rtc::PacketSocketFactory* socket_factory_;
deadbeef653b8e02015-11-11 12:55:10 -080075 ServerAddresses stun_servers_;
76 std::vector<RelayServerConfig> turn_servers_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077 bool allow_tcp_listen_;
78};
79
80struct PortConfiguration;
81class AllocationSequence;
82
83class BasicPortAllocatorSession : public PortAllocatorSession,
84 public rtc::MessageHandler {
85 public:
86 BasicPortAllocatorSession(BasicPortAllocator* allocator,
87 const std::string& content_name,
88 int component,
89 const std::string& ice_ufrag,
90 const std::string& ice_pwd);
91 ~BasicPortAllocatorSession();
92
93 virtual BasicPortAllocator* allocator() { return allocator_; }
94 rtc::Thread* network_thread() { return network_thread_; }
95 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
96
deadbeef653b8e02015-11-11 12:55:10 -080097 void StartGettingPorts() override;
98 void StopGettingPorts() override;
99 void ClearGettingPorts() override;
100 bool IsGettingPorts() override { return running_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101
102 protected:
103 // Starts the process of getting the port configurations.
104 virtual void GetPortConfigurations();
105
106 // Adds a port configuration that is now ready. Once we have one for each
107 // network (or a timeout occurs), we will start allocating ports.
108 virtual void ConfigReady(PortConfiguration* config);
109
110 // MessageHandler. Can be overriden if message IDs do not conflict.
deadbeef653b8e02015-11-11 12:55:10 -0800111 void OnMessage(rtc::Message* message) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
113 private:
114 class PortData {
115 public:
116 PortData() : port_(NULL), sequence_(NULL), state_(STATE_INIT) {}
117 PortData(Port* port, AllocationSequence* seq)
118 : port_(port), sequence_(seq), state_(STATE_INIT) {
119 }
120
121 Port* port() { return port_; }
122 AllocationSequence* sequence() { return sequence_; }
123 bool ready() const { return state_ == STATE_READY; }
124 bool complete() const {
125 // Returns true if candidate allocation has completed one way or another.
126 return ((state_ == STATE_COMPLETE) || (state_ == STATE_ERROR));
127 }
128
129 void set_ready() { ASSERT(state_ == STATE_INIT); state_ = STATE_READY; }
130 void set_complete() {
131 state_ = STATE_COMPLETE;
132 }
133 void set_error() {
134 ASSERT(state_ == STATE_INIT || state_ == STATE_READY);
135 state_ = STATE_ERROR;
136 }
137
138 private:
139 enum State {
140 STATE_INIT, // No candidates allocated yet.
141 STATE_READY, // At least one candidate is ready for process.
142 STATE_COMPLETE, // All candidates allocated and ready for process.
143 STATE_ERROR // Error in gathering candidates.
144 };
145 Port* port_;
146 AllocationSequence* sequence_;
147 State state_;
148 };
149
150 void OnConfigReady(PortConfiguration* config);
151 void OnConfigStop();
152 void AllocatePorts();
153 void OnAllocate();
154 void DoAllocate();
155 void OnNetworksChanged();
156 void OnAllocationSequenceObjectsCreated();
157 void DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200158 PortConfiguration* config,
159 uint32_t* flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 void AddAllocatedPort(Port* port, AllocationSequence* seq,
161 bool prepare_address);
162 void OnCandidateReady(Port* port, const Candidate& c);
163 void OnPortComplete(Port* port);
164 void OnPortError(Port* port);
165 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
166 void OnPortDestroyed(PortInterface* port);
167 void OnShake();
168 void MaybeSignalCandidatesAllocationDone();
169 void OnPortAllocationComplete(AllocationSequence* seq);
170 PortData* FindPort(Port* port);
honghaiz8c404fa2015-09-28 07:59:43 -0700171 void GetNetworks(std::vector<rtc::Network*>* networks);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172
173 bool CheckCandidateFilter(const Candidate& c);
174
175 BasicPortAllocator* allocator_;
176 rtc::Thread* network_thread_;
177 rtc::scoped_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
178 rtc::PacketSocketFactory* socket_factory_;
179 bool allocation_started_;
180 bool network_manager_started_;
181 bool running_; // set when StartGetAllPorts is called
182 bool allocation_sequences_created_;
183 std::vector<PortConfiguration*> configs_;
184 std::vector<AllocationSequence*> sequences_;
185 std::vector<PortData> ports_;
186
187 friend class AllocationSequence;
188};
189
190// Records configuration information useful in creating ports.
deadbeef653b8e02015-11-11 12:55:10 -0800191// TODO(deadbeef): Rename "relay" to "turn_server" in this struct.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192struct PortConfiguration : public rtc::MessageData {
193 // TODO(jiayl): remove |stun_address| when Chrome is updated.
194 rtc::SocketAddress stun_address;
195 ServerAddresses stun_servers;
196 std::string username;
197 std::string password;
198
199 typedef std::vector<RelayServerConfig> RelayList;
200 RelayList relays;
201
202 // TODO(jiayl): remove this ctor when Chrome is updated.
203 PortConfiguration(const rtc::SocketAddress& stun_address,
204 const std::string& username,
205 const std::string& password);
206
207 PortConfiguration(const ServerAddresses& stun_servers,
208 const std::string& username,
209 const std::string& password);
210
deadbeefc5d0d952015-07-16 10:22:21 -0700211 // Returns addresses of both the explicitly configured STUN servers,
212 // and TURN servers that should be used as STUN servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 ServerAddresses StunServers();
214
215 // Adds another relay server, with the given ports and modifier, to the list.
216 void AddRelay(const RelayServerConfig& config);
217
218 // Determines whether the given relay server supports the given protocol.
219 bool SupportsProtocol(const RelayServerConfig& relay,
220 ProtocolType type) const;
221 bool SupportsProtocol(RelayType turn_type, ProtocolType type) const;
222 // Helper method returns the server addresses for the matching RelayType and
223 // Protocol type.
224 ServerAddresses GetRelayServerAddresses(
225 RelayType turn_type, ProtocolType type) const;
226};
227
honghaizf421bdc2015-07-17 16:21:55 -0700228class UDPPort;
229class TurnPort;
230
231// Performs the allocation of ports, in a sequenced (timed) manner, for a given
232// network and IP address.
233class AllocationSequence : public rtc::MessageHandler,
234 public sigslot::has_slots<> {
235 public:
236 enum State {
237 kInit, // Initial state.
238 kRunning, // Started allocating ports.
239 kStopped, // Stopped from running.
240 kCompleted, // All ports are allocated.
241
242 // kInit --> kRunning --> {kCompleted|kStopped}
243 };
244 AllocationSequence(BasicPortAllocatorSession* session,
245 rtc::Network* network,
246 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200247 uint32_t flags);
honghaizf421bdc2015-07-17 16:21:55 -0700248 ~AllocationSequence();
249 bool Init();
250 void Clear();
honghaiz8c404fa2015-09-28 07:59:43 -0700251 void OnNetworkRemoved();
honghaizf421bdc2015-07-17 16:21:55 -0700252
253 State state() const { return state_; }
honghaiz8c404fa2015-09-28 07:59:43 -0700254 const rtc::Network* network() const { return network_; }
255 bool network_removed() const { return network_removed_; }
honghaizf421bdc2015-07-17 16:21:55 -0700256
257 // Disables the phases for a new sequence that this one already covers for an
258 // equivalent network setup.
259 void DisableEquivalentPhases(rtc::Network* network,
260 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200261 uint32_t* flags);
honghaizf421bdc2015-07-17 16:21:55 -0700262
263 // Starts and stops the sequence. When started, it will continue allocating
264 // new ports on its own timed schedule.
265 void Start();
266 void Stop();
267
268 // MessageHandler
269 void OnMessage(rtc::Message* msg);
270
271 void EnableProtocol(ProtocolType proto);
272 bool ProtocolEnabled(ProtocolType proto) const;
273
274 // Signal from AllocationSequence, when it's done with allocating ports.
275 // This signal is useful, when port allocation fails which doesn't result
276 // in any candidates. Using this signal BasicPortAllocatorSession can send
277 // its candidate discovery conclusion signal. Without this signal,
278 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
279 // can also be achieved by starting timer in BPAS.
280 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
281
282 protected:
283 // For testing.
284 void CreateTurnPort(const RelayServerConfig& config);
285
286 private:
287 typedef std::vector<ProtocolType> ProtocolList;
288
Peter Boström0c4e06b2015-10-07 12:23:21 +0200289 bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); }
honghaizf421bdc2015-07-17 16:21:55 -0700290 void CreateUDPPorts();
291 void CreateTCPPorts();
292 void CreateStunPorts();
293 void CreateRelayPorts();
294 void CreateGturnPort(const RelayServerConfig& config);
295
296 void OnReadPacket(rtc::AsyncPacketSocket* socket,
297 const char* data,
298 size_t size,
299 const rtc::SocketAddress& remote_addr,
300 const rtc::PacketTime& packet_time);
301
302 void OnPortDestroyed(PortInterface* port);
303
304 BasicPortAllocatorSession* session_;
honghaiz8c404fa2015-09-28 07:59:43 -0700305 bool network_removed_ = false;
honghaizf421bdc2015-07-17 16:21:55 -0700306 rtc::Network* network_;
307 rtc::IPAddress ip_;
308 PortConfiguration* config_;
309 State state_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200310 uint32_t flags_;
honghaizf421bdc2015-07-17 16:21:55 -0700311 ProtocolList protocols_;
312 rtc::scoped_ptr<rtc::AsyncPacketSocket> udp_socket_;
313 // There will be only one udp port per AllocationSequence.
314 UDPPort* udp_port_;
315 std::vector<TurnPort*> turn_ports_;
316 int phase_;
317};
318
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000319} // namespace cricket
320
321#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_