blob: 2afe03e0047b968809bd18eaecdd53930bf8871a [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>
kwiberg3ec46792016-04-27 07:22:53 -070024#include <memory>
guoweis36f01372016-03-02 18:02:40 -080025#include <set>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026#include <string>
27#include <vector>
kwiberg4485ffb2016-04-26 08:14:39 -070028
29#include "webrtc/base/constructormagic.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030#include "webrtc/p2p/base/candidate.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070031#include "webrtc/p2p/base/candidatepairinterface.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/p2ptransport.h"
33#include "webrtc/p2p/base/portallocator.h"
34#include "webrtc/p2p/base/portinterface.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035#include "webrtc/p2p/base/transportchannelimpl.h"
36#include "webrtc/base/asyncpacketsocket.h"
37#include "webrtc/base/sigslot.h"
38
39namespace cricket {
40
Honghai Zhang049fbb12016-03-07 11:13:07 -080041extern const int WEAK_PING_INTERVAL;
guoweisb0bb77f2015-10-26 15:10:01 -070042
honghaiza54a0802015-12-16 18:37:23 -080043struct IceParameters {
44 std::string ufrag;
45 std::string pwd;
46 IceParameters(const std::string& ice_ufrag, const std::string& ice_pwd)
47 : ufrag(ice_ufrag), pwd(ice_pwd) {}
48
49 bool operator==(const IceParameters& other) {
50 return ufrag == other.ufrag && pwd == other.pwd;
51 }
52 bool operator!=(const IceParameters& other) { return !(*this == other); }
53};
54
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055// Adds the port on which the candidate originated.
56class RemoteCandidate : public Candidate {
57 public:
58 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
59 : Candidate(c), origin_port_(origin_port) {}
60
61 PortInterface* origin_port() { return origin_port_; }
62
63 private:
64 PortInterface* origin_port_;
65};
66
67// P2PTransportChannel manages the candidates and connection process to keep
68// two P2P clients connected to each other.
69class P2PTransportChannel : public TransportChannelImpl,
70 public rtc::MessageHandler {
71 public:
deadbeefcbecd352015-09-23 11:50:27 -070072 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073 int component,
mikescarlettb9dd7c52016-02-19 20:43:45 -080074 PortAllocator* allocator);
75 // TODO(mikescarlett): Deprecated. Remove when Chromium's
76 // IceTransportChannel does not depend on this.
77 P2PTransportChannel(const std::string& transport_name,
78 int component,
guidou74db7772016-02-18 01:57:49 -080079 P2PTransport* transport,
deadbeefcbecd352015-09-23 11:50:27 -070080 PortAllocator* allocator);
81 virtual ~P2PTransportChannel();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082
83 // From TransportChannelImpl:
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020084 TransportChannelState GetState() const override;
85 void SetIceRole(IceRole role) override;
86 IceRole GetIceRole() const override { return ice_role_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 void SetIceTiebreaker(uint64_t tiebreaker) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020088 void SetIceCredentials(const std::string& ice_ufrag,
89 const std::string& ice_pwd) override;
90 void SetRemoteIceCredentials(const std::string& ice_ufrag,
91 const std::string& ice_pwd) override;
92 void SetRemoteIceMode(IceMode mode) override;
93 void Connect() override;
deadbeefcbecd352015-09-23 11:50:27 -070094 void MaybeStartGathering() override;
95 IceGatheringState gathering_state() const override {
96 return gathering_state_;
97 }
98 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -070099 void RemoveRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800100 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
101 // only update the parameter if it is considered set in |config|. For example,
102 // a negative value of receiving_timeout will be considered "not set" and we
103 // will not use it to update the respective parameter in |config_|.
honghaiz1f429e32015-09-28 07:57:34 -0700104 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -0800105 const IceConfig& config() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106
107 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700108 int SendPacket(const char* data,
109 size_t len,
110 const rtc::PacketOptions& options,
111 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200112 int SetOption(rtc::Socket::Option opt, int value) override;
113 bool GetOption(rtc::Socket::Option opt, int* value) override;
114 int GetError() override { return error_; }
115 bool GetStats(std::vector<ConnectionInfo>* stats) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116
117 const Connection* best_connection() const { return best_connection_; }
118 void set_incoming_only(bool value) { incoming_only_ = value; }
119
120 // Note: This is only for testing purpose.
121 // |ports_| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700122 const std::vector<PortInterface*>& ports() { return ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123
124 IceMode remote_ice_mode() const { return remote_ice_mode_; }
125
126 // DTLS methods.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200127 bool IsDtlsActive() const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
129 // Default implementation.
deadbeefcbecd352015-09-23 11:50:27 -0700130 bool GetSslRole(rtc::SSLRole* role) const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131
deadbeefcbecd352015-09-23 11:50:27 -0700132 bool SetSslRole(rtc::SSLRole role) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133
134 // Set up the ciphers to use for DTLS-SRTP.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800135 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136 return false;
137 }
138
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000139 // Find out which DTLS-SRTP cipher was negotiated.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800140 bool GetSrtpCryptoSuite(int* cipher) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000142 // Find out which DTLS cipher was negotiated.
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700143 bool GetSslCipherSuite(int* cipher) override { return false; }
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000144
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200145 // Returns null because the channel is not encrypted by default.
146 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
147 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148 }
149
jbauch555604a2016-04-26 03:13:22 -0700150 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700151 const override {
152 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 }
154
155 // Allows key material to be extracted for external encryption.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200156 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200157 const uint8_t* context,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200158 size_t context_len,
159 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200160 uint8_t* result,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200161 size_t result_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162 return false;
163 }
164
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200165 bool SetLocalCertificate(
166 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167 return false;
168 }
169
170 // Set DTLS Remote fingerprint. Must be after local identity set.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200171 bool SetRemoteFingerprint(const std::string& digest_alg,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200172 const uint8_t* digest,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200173 size_t digest_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000174 return false;
175 }
176
Honghai Zhang049fbb12016-03-07 11:13:07 -0800177 int receiving_timeout() const { return config_.receiving_timeout; }
178 int check_receiving_interval() const { return check_receiving_interval_; }
Peter Thatcher54360512015-07-08 11:08:35 -0700179
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 // Helper method used only in unittest.
181 rtc::DiffServCodePoint DefaultDscpValue() const;
182
Peter Thatcher7351f462015-04-02 16:39:16 -0700183 // Public for unit tests.
184 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800185 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700186
honghaiz77d0d6e2015-10-27 11:34:45 -0700187 // Public for unit tests.
188 const std::vector<Connection*>& connections() const { return connections_; }
189
honghaiz9b669572015-11-04 12:07:44 -0800190 // Public for unit tests.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 PortAllocatorSession* allocator_session() {
192 return allocator_sessions_.back();
193 }
194
honghaiz112fe432015-12-30 13:32:47 -0800195 // Public for unit tests.
196 const std::vector<RemoteCandidate>& remote_candidates() const {
197 return remote_candidates_;
198 }
199
honghaiz9b669572015-11-04 12:07:44 -0800200 private:
201 rtc::Thread* thread() { return worker_thread_; }
202 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
203
honghaiza58ea782015-09-24 08:13:36 -0700204 // A transport channel is weak if the current best connection is either
205 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700206 bool weak() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 void UpdateConnectionStates();
208 void RequestSort();
209 void SortConnections();
210 void SwitchBestConnectionTo(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800211 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800213 void MaybeStopPortAllocatorSessions();
Honghai Zhang381b4212015-12-04 12:24:03 -0800214 TransportChannelState ComputeState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000216 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700217 bool CreateConnections(const Candidate& remote_candidate,
218 PortInterface* origin_port);
219 bool CreateConnection(PortInterface* port,
220 const Candidate& remote_candidate,
221 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 bool FindConnection(cricket::Connection* connection) const;
223
Peter Boström0c4e06b2015-10-07 12:23:21 +0200224 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
226 void RememberRemoteCandidate(const Candidate& remote_candidate,
227 PortInterface* origin_port);
honghaiz34b11eb2016-03-16 08:55:44 -0700228 bool IsPingable(Connection* conn, int64_t now);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 void PingConnection(Connection* conn);
230 void AddAllocatorSession(PortAllocatorSession* session);
231 void AddConnection(Connection* connection);
232
233 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
234 void OnCandidatesReady(PortAllocatorSession *session,
235 const std::vector<Candidate>& candidates);
236 void OnCandidatesAllocationDone(PortAllocatorSession* session);
237 void OnUnknownAddress(PortInterface* port,
238 const rtc::SocketAddress& addr,
239 ProtocolType proto,
240 IceMessage* stun_msg,
241 const std::string& remote_username,
242 bool port_muxed);
243 void OnPortDestroyed(PortInterface* port);
honghaize3c6c822016-02-17 13:00:28 -0800244 void OnPortNetworkInactive(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 void OnRoleConflict(PortInterface* port);
246
247 void OnConnectionStateChange(Connection* connection);
248 void OnReadPacket(Connection *connection, const char *data, size_t len,
249 const rtc::PacketTime& packet_time);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100250 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 void OnReadyToSend(Connection* connection);
252 void OnConnectionDestroyed(Connection *connection);
253
honghaiz5a3acd82015-08-20 15:53:17 -0700254 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255
deadbeefcbecd352015-09-23 11:50:27 -0700256 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257 void OnSort();
honghaiza58ea782015-09-24 08:13:36 -0700258 void OnCheckAndPing();
Peter Thatcher54360512015-07-08 11:08:35 -0700259
honghaiz5a3acd82015-08-20 15:53:17 -0700260 void PruneConnections();
261 Connection* best_nominated_connection() const;
Honghai Zhang381b4212015-12-04 12:24:03 -0800262 bool IsBackupConnection(Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700263
honghaiz34b11eb2016-03-16 08:55:44 -0700264 Connection* FindConnectionToPing(int64_t now);
265 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800266 // Between |conn1| and |conn2|, this function returns the one which should
267 // be pinged first.
268 Connection* SelectMostPingableConnection(Connection* conn1,
269 Connection* conn2);
270 // Select the connection which is Relay/Relay. If both of them are,
271 // UDP relay protocol takes precedence.
272 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
273 // Compare the last_ping_sent time and return the one least recently pinged.
274 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
275
honghaiza54a0802015-12-16 18:37:23 -0800276 // Returns the latest remote ICE parameters or nullptr if there are no remote
277 // ICE parameters yet.
278 IceParameters* remote_ice() {
279 return remote_ice_parameters_.empty() ? nullptr
280 : &remote_ice_parameters_.back();
281 }
honghaiz112fe432015-12-30 13:32:47 -0800282 // Returns the remote IceParameters and generation that match |ufrag|
283 // if found, and returns nullptr otherwise.
284 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
285 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800286 // Returns the index of the latest remote ICE parameters, or 0 if no remote
287 // ICE parameters have been received.
288 uint32_t remote_ice_generation() {
289 return remote_ice_parameters_.empty()
290 ? 0
291 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
292 }
293
deadbeefcbecd352015-09-23 11:50:27 -0700294 PortAllocator* allocator_;
295 rtc::Thread* worker_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000297 int error_;
298 std::vector<PortAllocatorSession*> allocator_sessions_;
299 std::vector<PortInterface *> ports_;
guoweis36f01372016-03-02 18:02:40 -0800300
301 // |connections_| is a sorted list with the first one always be the
302 // |best_connection_| when it's not nullptr. The combination of
303 // |pinged_connections_| and |unpinged_connections_| has the same
304 // connections as |connections_|. These 2 sets maintain whether a
305 // connection should be pinged next or not.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000306 std::vector<Connection *> connections_;
guoweis36f01372016-03-02 18:02:40 -0800307 std::set<Connection*> pinged_connections_;
308 std::set<Connection*> unpinged_connections_;
309
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000310 Connection* best_connection_;
guoweis36f01372016-03-02 18:02:40 -0800311
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000312 // Connection selected by the controlling agent. This should be used only
313 // at controlled side when protocol type is RFC5245.
314 Connection* pending_best_connection_;
315 std::vector<RemoteCandidate> remote_candidates_;
316 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700317 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000318 typedef std::map<rtc::Socket::Option, int> OptionMap;
319 OptionMap options_;
320 std::string ice_ufrag_;
321 std::string ice_pwd_;
honghaiza54a0802015-12-16 18:37:23 -0800322 std::vector<IceParameters> remote_ice_parameters_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323 IceMode remote_ice_mode_;
324 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200325 uint64_t tiebreaker_;
deadbeefcbecd352015-09-23 11:50:27 -0700326 IceGatheringState gathering_state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000327
Honghai Zhang049fbb12016-03-07 11:13:07 -0800328 int check_receiving_interval_;
honghaiz34b11eb2016-03-16 08:55:44 -0700329 int64_t last_ping_sent_ms_ = 0;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800330 int weak_ping_interval_ = WEAK_PING_INTERVAL;
Honghai Zhang381b4212015-12-04 12:24:03 -0800331 TransportChannelState state_ = TransportChannelState::STATE_INIT;
guoweis36f01372016-03-02 18:02:40 -0800332 IceConfig config_;
Honghai Zhang52dce732016-03-31 12:37:31 -0700333 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before.
Peter Thatcher54360512015-07-08 11:08:35 -0700334
henrikg3c089d72015-09-16 05:37:44 -0700335 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336};
337
338} // namespace cricket
339
340#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_