blob: a27ff4c6274f66e9b783d45238eaf83cee6fbe9f [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
17#include "webrtc/p2p/base/port.h"
18#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
26struct RelayCredentials {
27 RelayCredentials() {}
28 RelayCredentials(const std::string& username,
29 const std::string& password)
30 : username(username),
31 password(password) {
32 }
33
34 std::string username;
35 std::string password;
36};
37
38typedef std::vector<ProtocolAddress> PortList;
39struct RelayServerConfig {
40 RelayServerConfig(RelayType type) : type(type), priority(0) {}
41
42 RelayType type;
43 PortList ports;
44 RelayCredentials credentials;
45 int priority;
46};
47
48class BasicPortAllocator : public PortAllocator {
49 public:
50 BasicPortAllocator(rtc::NetworkManager* network_manager,
51 rtc::PacketSocketFactory* socket_factory);
52 explicit BasicPortAllocator(rtc::NetworkManager* network_manager);
53 BasicPortAllocator(rtc::NetworkManager* network_manager,
54 rtc::PacketSocketFactory* socket_factory,
55 const ServerAddresses& stun_servers);
56 BasicPortAllocator(rtc::NetworkManager* network_manager,
57 const ServerAddresses& stun_servers,
58 const rtc::SocketAddress& relay_server_udp,
59 const rtc::SocketAddress& relay_server_tcp,
60 const rtc::SocketAddress& relay_server_ssl);
61 virtual ~BasicPortAllocator();
62
63 rtc::NetworkManager* network_manager() { return network_manager_; }
64
65 // If socket_factory() is set to NULL each PortAllocatorSession
66 // creates its own socket factory.
67 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
68
69 const ServerAddresses& stun_servers() const {
70 return stun_servers_;
71 }
72
73 const std::vector<RelayServerConfig>& relays() const {
74 return relays_;
75 }
76 virtual void AddRelay(const RelayServerConfig& relay) {
77 relays_.push_back(relay);
78 }
79
80 virtual PortAllocatorSession* CreateSessionInternal(
81 const std::string& content_name,
82 int component,
83 const std::string& ice_ufrag,
84 const std::string& ice_pwd);
85
86 private:
87 void Construct();
88
89 rtc::NetworkManager* network_manager_;
90 rtc::PacketSocketFactory* socket_factory_;
91 const ServerAddresses stun_servers_;
92 std::vector<RelayServerConfig> relays_;
93 bool allow_tcp_listen_;
94};
95
96struct PortConfiguration;
97class AllocationSequence;
98
99class BasicPortAllocatorSession : public PortAllocatorSession,
100 public rtc::MessageHandler {
101 public:
102 BasicPortAllocatorSession(BasicPortAllocator* allocator,
103 const std::string& content_name,
104 int component,
105 const std::string& ice_ufrag,
106 const std::string& ice_pwd);
107 ~BasicPortAllocatorSession();
108
109 virtual BasicPortAllocator* allocator() { return allocator_; }
110 rtc::Thread* network_thread() { return network_thread_; }
111 rtc::PacketSocketFactory* socket_factory() { return socket_factory_; }
112
113 virtual void StartGettingPorts();
114 virtual void StopGettingPorts();
115 virtual bool IsGettingPorts() { return running_; }
116
117 protected:
118 // Starts the process of getting the port configurations.
119 virtual void GetPortConfigurations();
120
121 // Adds a port configuration that is now ready. Once we have one for each
122 // network (or a timeout occurs), we will start allocating ports.
123 virtual void ConfigReady(PortConfiguration* config);
124
125 // MessageHandler. Can be overriden if message IDs do not conflict.
126 virtual void OnMessage(rtc::Message *message);
127
128 private:
129 class PortData {
130 public:
131 PortData() : port_(NULL), sequence_(NULL), state_(STATE_INIT) {}
132 PortData(Port* port, AllocationSequence* seq)
133 : port_(port), sequence_(seq), state_(STATE_INIT) {
134 }
135
136 Port* port() { return port_; }
137 AllocationSequence* sequence() { return sequence_; }
138 bool ready() const { return state_ == STATE_READY; }
139 bool complete() const {
140 // Returns true if candidate allocation has completed one way or another.
141 return ((state_ == STATE_COMPLETE) || (state_ == STATE_ERROR));
142 }
143
144 void set_ready() { ASSERT(state_ == STATE_INIT); state_ = STATE_READY; }
145 void set_complete() {
146 state_ = STATE_COMPLETE;
147 }
148 void set_error() {
149 ASSERT(state_ == STATE_INIT || state_ == STATE_READY);
150 state_ = STATE_ERROR;
151 }
152
153 private:
154 enum State {
155 STATE_INIT, // No candidates allocated yet.
156 STATE_READY, // At least one candidate is ready for process.
157 STATE_COMPLETE, // All candidates allocated and ready for process.
158 STATE_ERROR // Error in gathering candidates.
159 };
160 Port* port_;
161 AllocationSequence* sequence_;
162 State state_;
163 };
164
165 void OnConfigReady(PortConfiguration* config);
166 void OnConfigStop();
167 void AllocatePorts();
168 void OnAllocate();
169 void DoAllocate();
170 void OnNetworksChanged();
171 void OnAllocationSequenceObjectsCreated();
172 void DisableEquivalentPhases(rtc::Network* network,
173 PortConfiguration* config, uint32* flags);
174 void AddAllocatedPort(Port* port, AllocationSequence* seq,
175 bool prepare_address);
176 void OnCandidateReady(Port* port, const Candidate& c);
177 void OnPortComplete(Port* port);
178 void OnPortError(Port* port);
179 void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
180 void OnPortDestroyed(PortInterface* port);
181 void OnShake();
182 void MaybeSignalCandidatesAllocationDone();
183 void OnPortAllocationComplete(AllocationSequence* seq);
184 PortData* FindPort(Port* port);
185
186 bool CheckCandidateFilter(const Candidate& c);
187
188 BasicPortAllocator* allocator_;
189 rtc::Thread* network_thread_;
190 rtc::scoped_ptr<rtc::PacketSocketFactory> owned_socket_factory_;
191 rtc::PacketSocketFactory* socket_factory_;
192 bool allocation_started_;
193 bool network_manager_started_;
194 bool running_; // set when StartGetAllPorts is called
195 bool allocation_sequences_created_;
196 std::vector<PortConfiguration*> configs_;
197 std::vector<AllocationSequence*> sequences_;
198 std::vector<PortData> ports_;
199
200 friend class AllocationSequence;
201};
202
203// Records configuration information useful in creating ports.
204struct 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,
259 uint32 flags);
260 ~AllocationSequence();
261 bool Init();
262 void Clear();
263
264 State state() const { return state_; }
265
266 // Disables the phases for a new sequence that this one already covers for an
267 // equivalent network setup.
268 void DisableEquivalentPhases(rtc::Network* network,
269 PortConfiguration* config,
270 uint32* flags);
271
272 // Starts and stops the sequence. When started, it will continue allocating
273 // new ports on its own timed schedule.
274 void Start();
275 void Stop();
276
277 // MessageHandler
278 void OnMessage(rtc::Message* msg);
279
280 void EnableProtocol(ProtocolType proto);
281 bool ProtocolEnabled(ProtocolType proto) const;
282
283 // Signal from AllocationSequence, when it's done with allocating ports.
284 // This signal is useful, when port allocation fails which doesn't result
285 // in any candidates. Using this signal BasicPortAllocatorSession can send
286 // its candidate discovery conclusion signal. Without this signal,
287 // BasicPortAllocatorSession doesn't have any event to trigger signal. This
288 // can also be achieved by starting timer in BPAS.
289 sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
290
291 protected:
292 // For testing.
293 void CreateTurnPort(const RelayServerConfig& config);
294
295 private:
296 typedef std::vector<ProtocolType> ProtocolList;
297
298 bool IsFlagSet(uint32 flag) { return ((flags_ & flag) != 0); }
299 void CreateUDPPorts();
300 void CreateTCPPorts();
301 void CreateStunPorts();
302 void CreateRelayPorts();
303 void CreateGturnPort(const RelayServerConfig& config);
304
305 void OnReadPacket(rtc::AsyncPacketSocket* socket,
306 const char* data,
307 size_t size,
308 const rtc::SocketAddress& remote_addr,
309 const rtc::PacketTime& packet_time);
310
311 void OnPortDestroyed(PortInterface* port);
312
313 BasicPortAllocatorSession* session_;
314 rtc::Network* network_;
315 rtc::IPAddress ip_;
316 PortConfiguration* config_;
317 State state_;
318 uint32 flags_;
319 ProtocolList protocols_;
320 rtc::scoped_ptr<rtc::AsyncPacketSocket> udp_socket_;
321 // There will be only one udp port per AllocationSequence.
322 UDPPort* udp_port_;
323 std::vector<TurnPort*> turn_ports_;
324 int phase_;
325};
326
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000327} // namespace cricket
328
329#endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_