blob: 04bc9512690f50d16331a43d5a9fd1acba8aabb8 [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// P2PTransportChannel wraps up the state management of the connection between
12// two P2P clients. Clients have candidate ports for connecting, and
13// connections which are combinations of candidates from each end (Alice and
14// Bob each have candidates, one candidate from Alice and one candidate from
15// Bob are used to make a connection, repeat to make many connections).
16//
17// When all of the available connections become invalid (non-writable), we
18// kick off a process of determining more candidates and more connections.
19//
20#ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
21#define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
22
23#include <map>
guoweis36f01372016-03-02 18:02:40 -080024#include <set>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025#include <string>
26#include <vector>
27#include "webrtc/p2p/base/candidate.h"
28#include "webrtc/p2p/base/p2ptransport.h"
29#include "webrtc/p2p/base/portallocator.h"
30#include "webrtc/p2p/base/portinterface.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031#include "webrtc/p2p/base/transportchannelimpl.h"
32#include "webrtc/base/asyncpacketsocket.h"
33#include "webrtc/base/sigslot.h"
34
35namespace cricket {
36
Honghai Zhang049fbb12016-03-07 11:13:07 -080037extern const int WEAK_PING_INTERVAL;
guoweisb0bb77f2015-10-26 15:10:01 -070038
honghaiza54a0802015-12-16 18:37:23 -080039struct IceParameters {
40 std::string ufrag;
41 std::string pwd;
42 IceParameters(const std::string& ice_ufrag, const std::string& ice_pwd)
43 : ufrag(ice_ufrag), pwd(ice_pwd) {}
44
45 bool operator==(const IceParameters& other) {
46 return ufrag == other.ufrag && pwd == other.pwd;
47 }
48 bool operator!=(const IceParameters& other) { return !(*this == other); }
49};
50
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051// Adds the port on which the candidate originated.
52class RemoteCandidate : public Candidate {
53 public:
54 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
55 : Candidate(c), origin_port_(origin_port) {}
56
57 PortInterface* origin_port() { return origin_port_; }
58
59 private:
60 PortInterface* origin_port_;
61};
62
63// P2PTransportChannel manages the candidates and connection process to keep
64// two P2P clients connected to each other.
65class P2PTransportChannel : public TransportChannelImpl,
66 public rtc::MessageHandler {
67 public:
deadbeefcbecd352015-09-23 11:50:27 -070068 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 int component,
mikescarlettb9dd7c52016-02-19 20:43:45 -080070 PortAllocator* allocator);
71 // TODO(mikescarlett): Deprecated. Remove when Chromium's
72 // IceTransportChannel does not depend on this.
73 P2PTransportChannel(const std::string& transport_name,
74 int component,
guidou74db7772016-02-18 01:57:49 -080075 P2PTransport* transport,
deadbeefcbecd352015-09-23 11:50:27 -070076 PortAllocator* allocator);
77 virtual ~P2PTransportChannel();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078
79 // From TransportChannelImpl:
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020080 TransportChannelState GetState() const override;
81 void SetIceRole(IceRole role) override;
82 IceRole GetIceRole() const override { return ice_role_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +020083 void SetIceTiebreaker(uint64_t tiebreaker) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020084 void SetIceCredentials(const std::string& ice_ufrag,
85 const std::string& ice_pwd) override;
86 void SetRemoteIceCredentials(const std::string& ice_ufrag,
87 const std::string& ice_pwd) override;
88 void SetRemoteIceMode(IceMode mode) override;
89 void Connect() override;
deadbeefcbecd352015-09-23 11:50:27 -070090 void MaybeStartGathering() override;
91 IceGatheringState gathering_state() const override {
92 return gathering_state_;
93 }
94 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -070095 void RemoveRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang049fbb12016-03-07 11:13:07 -080096 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
97 // only update the parameter if it is considered set in |config|. For example,
98 // a negative value of receiving_timeout will be considered "not set" and we
99 // will not use it to update the respective parameter in |config_|.
honghaiz1f429e32015-09-28 07:57:34 -0700100 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -0800101 const IceConfig& config() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
103 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700104 int SendPacket(const char* data,
105 size_t len,
106 const rtc::PacketOptions& options,
107 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200108 int SetOption(rtc::Socket::Option opt, int value) override;
109 bool GetOption(rtc::Socket::Option opt, int* value) override;
110 int GetError() override { return error_; }
111 bool GetStats(std::vector<ConnectionInfo>* stats) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
113 const Connection* best_connection() const { return best_connection_; }
114 void set_incoming_only(bool value) { incoming_only_ = value; }
115
116 // Note: This is only for testing purpose.
117 // |ports_| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700118 const std::vector<PortInterface*>& ports() { return ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 IceMode remote_ice_mode() const { return remote_ice_mode_; }
121
122 // DTLS methods.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200123 bool IsDtlsActive() const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124
125 // Default implementation.
deadbeefcbecd352015-09-23 11:50:27 -0700126 bool GetSslRole(rtc::SSLRole* role) const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127
deadbeefcbecd352015-09-23 11:50:27 -0700128 bool SetSslRole(rtc::SSLRole role) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129
130 // Set up the ciphers to use for DTLS-SRTP.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800131 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 return false;
133 }
134
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000135 // Find out which DTLS-SRTP cipher was negotiated.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800136 bool GetSrtpCryptoSuite(int* cipher) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000138 // Find out which DTLS cipher was negotiated.
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700139 bool GetSslCipherSuite(int* cipher) override { return false; }
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000140
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200141 // Returns null because the channel is not encrypted by default.
142 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
143 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144 }
145
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200146 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 return false;
148 }
149
150 // Allows key material to be extracted for external encryption.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200151 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152 const uint8_t* context,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200153 size_t context_len,
154 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200155 uint8_t* result,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200156 size_t result_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 return false;
158 }
159
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200160 bool SetLocalCertificate(
161 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162 return false;
163 }
164
165 // Set DTLS Remote fingerprint. Must be after local identity set.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200166 bool SetRemoteFingerprint(const std::string& digest_alg,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 const uint8_t* digest,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200168 size_t digest_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 return false;
170 }
171
Honghai Zhang049fbb12016-03-07 11:13:07 -0800172 int receiving_timeout() const { return config_.receiving_timeout; }
173 int check_receiving_interval() const { return check_receiving_interval_; }
Peter Thatcher54360512015-07-08 11:08:35 -0700174
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175 // Helper method used only in unittest.
176 rtc::DiffServCodePoint DefaultDscpValue() const;
177
Peter Thatcher7351f462015-04-02 16:39:16 -0700178 // Public for unit tests.
179 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800180 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700181
honghaiz77d0d6e2015-10-27 11:34:45 -0700182 // Public for unit tests.
183 const std::vector<Connection*>& connections() const { return connections_; }
184
honghaiz9b669572015-11-04 12:07:44 -0800185 // Public for unit tests.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 PortAllocatorSession* allocator_session() {
187 return allocator_sessions_.back();
188 }
189
honghaiz112fe432015-12-30 13:32:47 -0800190 // Public for unit tests.
191 const std::vector<RemoteCandidate>& remote_candidates() const {
192 return remote_candidates_;
193 }
194
honghaiz9b669572015-11-04 12:07:44 -0800195 private:
196 rtc::Thread* thread() { return worker_thread_; }
197 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
198
honghaiza58ea782015-09-24 08:13:36 -0700199 // A transport channel is weak if the current best connection is either
200 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700201 bool weak() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 void UpdateConnectionStates();
203 void RequestSort();
204 void SortConnections();
205 void SwitchBestConnectionTo(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800206 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800208 void MaybeStopPortAllocatorSessions();
Honghai Zhang381b4212015-12-04 12:24:03 -0800209 TransportChannelState ComputeState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000211 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700212 bool CreateConnections(const Candidate& remote_candidate,
213 PortInterface* origin_port);
214 bool CreateConnection(PortInterface* port,
215 const Candidate& remote_candidate,
216 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217 bool FindConnection(cricket::Connection* connection) const;
218
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
221 void RememberRemoteCandidate(const Candidate& remote_candidate,
222 PortInterface* origin_port);
honghaiz34b11eb2016-03-16 08:55:44 -0700223 bool IsPingable(Connection* conn, int64_t now);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224 void PingConnection(Connection* conn);
225 void AddAllocatorSession(PortAllocatorSession* session);
226 void AddConnection(Connection* connection);
227
228 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
229 void OnCandidatesReady(PortAllocatorSession *session,
230 const std::vector<Candidate>& candidates);
231 void OnCandidatesAllocationDone(PortAllocatorSession* session);
232 void OnUnknownAddress(PortInterface* port,
233 const rtc::SocketAddress& addr,
234 ProtocolType proto,
235 IceMessage* stun_msg,
236 const std::string& remote_username,
237 bool port_muxed);
238 void OnPortDestroyed(PortInterface* port);
honghaize3c6c822016-02-17 13:00:28 -0800239 void OnPortNetworkInactive(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240 void OnRoleConflict(PortInterface* port);
241
242 void OnConnectionStateChange(Connection* connection);
243 void OnReadPacket(Connection *connection, const char *data, size_t len,
244 const rtc::PacketTime& packet_time);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100245 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 void OnReadyToSend(Connection* connection);
247 void OnConnectionDestroyed(Connection *connection);
248
honghaiz5a3acd82015-08-20 15:53:17 -0700249 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000250
deadbeefcbecd352015-09-23 11:50:27 -0700251 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 void OnSort();
honghaiza58ea782015-09-24 08:13:36 -0700253 void OnCheckAndPing();
Peter Thatcher54360512015-07-08 11:08:35 -0700254
honghaiz5a3acd82015-08-20 15:53:17 -0700255 void PruneConnections();
256 Connection* best_nominated_connection() const;
Honghai Zhang381b4212015-12-04 12:24:03 -0800257 bool IsBackupConnection(Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700258
honghaiz34b11eb2016-03-16 08:55:44 -0700259 Connection* FindConnectionToPing(int64_t now);
260 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800261 // Between |conn1| and |conn2|, this function returns the one which should
262 // be pinged first.
263 Connection* SelectMostPingableConnection(Connection* conn1,
264 Connection* conn2);
265 // Select the connection which is Relay/Relay. If both of them are,
266 // UDP relay protocol takes precedence.
267 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
268 // Compare the last_ping_sent time and return the one least recently pinged.
269 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
270
honghaiza54a0802015-12-16 18:37:23 -0800271 // Returns the latest remote ICE parameters or nullptr if there are no remote
272 // ICE parameters yet.
273 IceParameters* remote_ice() {
274 return remote_ice_parameters_.empty() ? nullptr
275 : &remote_ice_parameters_.back();
276 }
honghaiz112fe432015-12-30 13:32:47 -0800277 // Returns the remote IceParameters and generation that match |ufrag|
278 // if found, and returns nullptr otherwise.
279 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
280 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800281 // Returns the index of the latest remote ICE parameters, or 0 if no remote
282 // ICE parameters have been received.
283 uint32_t remote_ice_generation() {
284 return remote_ice_parameters_.empty()
285 ? 0
286 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
287 }
288
deadbeefcbecd352015-09-23 11:50:27 -0700289 PortAllocator* allocator_;
290 rtc::Thread* worker_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000291 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000292 int error_;
293 std::vector<PortAllocatorSession*> allocator_sessions_;
294 std::vector<PortInterface *> ports_;
guoweis36f01372016-03-02 18:02:40 -0800295
296 // |connections_| is a sorted list with the first one always be the
297 // |best_connection_| when it's not nullptr. The combination of
298 // |pinged_connections_| and |unpinged_connections_| has the same
299 // connections as |connections_|. These 2 sets maintain whether a
300 // connection should be pinged next or not.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301 std::vector<Connection *> connections_;
guoweis36f01372016-03-02 18:02:40 -0800302 std::set<Connection*> pinged_connections_;
303 std::set<Connection*> unpinged_connections_;
304
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000305 Connection* best_connection_;
guoweis36f01372016-03-02 18:02:40 -0800306
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000307 // Connection selected by the controlling agent. This should be used only
308 // at controlled side when protocol type is RFC5245.
309 Connection* pending_best_connection_;
310 std::vector<RemoteCandidate> remote_candidates_;
311 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700312 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000313 typedef std::map<rtc::Socket::Option, int> OptionMap;
314 OptionMap options_;
315 std::string ice_ufrag_;
316 std::string ice_pwd_;
honghaiza54a0802015-12-16 18:37:23 -0800317 std::vector<IceParameters> remote_ice_parameters_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000318 IceMode remote_ice_mode_;
319 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200320 uint64_t tiebreaker_;
deadbeefcbecd352015-09-23 11:50:27 -0700321 IceGatheringState gathering_state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000322
Honghai Zhang049fbb12016-03-07 11:13:07 -0800323 int check_receiving_interval_;
honghaiz34b11eb2016-03-16 08:55:44 -0700324 int64_t last_ping_sent_ms_ = 0;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800325 int weak_ping_interval_ = WEAK_PING_INTERVAL;
Honghai Zhang381b4212015-12-04 12:24:03 -0800326 TransportChannelState state_ = TransportChannelState::STATE_INIT;
guoweis36f01372016-03-02 18:02:40 -0800327 IceConfig config_;
Peter Thatcher54360512015-07-08 11:08:35 -0700328
henrikg3c089d72015-09-16 05:37:44 -0700329 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000330};
331
332} // namespace cricket
333
334#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_