blob: 2b02d36a61ab899d3dc462982ac4d39f35faea41 [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>
24#include <string>
25#include <vector>
26#include "webrtc/p2p/base/candidate.h"
27#include "webrtc/p2p/base/p2ptransport.h"
28#include "webrtc/p2p/base/portallocator.h"
29#include "webrtc/p2p/base/portinterface.h"
30#include "webrtc/p2p/base/transport.h"
31#include "webrtc/p2p/base/transportchannelimpl.h"
32#include "webrtc/base/asyncpacketsocket.h"
33#include "webrtc/base/sigslot.h"
34
35namespace cricket {
36
guoweisb0bb77f2015-10-26 15:10:01 -070037extern const uint32_t WEAK_PING_DELAY;
38
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039// Adds the port on which the candidate originated.
40class RemoteCandidate : public Candidate {
41 public:
42 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
43 : Candidate(c), origin_port_(origin_port) {}
44
45 PortInterface* origin_port() { return origin_port_; }
46
47 private:
48 PortInterface* origin_port_;
49};
50
51// P2PTransportChannel manages the candidates and connection process to keep
52// two P2P clients connected to each other.
53class P2PTransportChannel : public TransportChannelImpl,
54 public rtc::MessageHandler {
55 public:
deadbeefcbecd352015-09-23 11:50:27 -070056 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 int component,
58 P2PTransport* transport,
deadbeefcbecd352015-09-23 11:50:27 -070059 PortAllocator* allocator);
60 virtual ~P2PTransportChannel();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061
62 // From TransportChannelImpl:
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020063 Transport* GetTransport() override { return transport_; }
64 TransportChannelState GetState() const override;
65 void SetIceRole(IceRole role) override;
66 IceRole GetIceRole() const override { return ice_role_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +020067 void SetIceTiebreaker(uint64_t tiebreaker) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020068 void SetIceCredentials(const std::string& ice_ufrag,
69 const std::string& ice_pwd) override;
70 void SetRemoteIceCredentials(const std::string& ice_ufrag,
71 const std::string& ice_pwd) override;
72 void SetRemoteIceMode(IceMode mode) override;
73 void Connect() override;
deadbeefcbecd352015-09-23 11:50:27 -070074 void MaybeStartGathering() override;
75 IceGatheringState gathering_state() const override {
76 return gathering_state_;
77 }
78 void AddRemoteCandidate(const Candidate& candidate) override;
honghaiz1f429e32015-09-28 07:57:34 -070079 // Sets the receiving timeout and gather_continually.
honghaiza03cd3f2015-07-13 17:08:08 -070080 // This also sets the check_receiving_delay proportionally.
honghaiz1f429e32015-09-28 07:57:34 -070081 void SetIceConfig(const IceConfig& config) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082
83 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -070084 int SendPacket(const char* data,
85 size_t len,
86 const rtc::PacketOptions& options,
87 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020088 int SetOption(rtc::Socket::Option opt, int value) override;
89 bool GetOption(rtc::Socket::Option opt, int* value) override;
90 int GetError() override { return error_; }
91 bool GetStats(std::vector<ConnectionInfo>* stats) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092
93 const Connection* best_connection() const { return best_connection_; }
94 void set_incoming_only(bool value) { incoming_only_ = value; }
95
96 // Note: This is only for testing purpose.
97 // |ports_| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -070098 const std::vector<PortInterface*>& ports() { return ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099
100 IceMode remote_ice_mode() const { return remote_ice_mode_; }
101
102 // DTLS methods.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200103 bool IsDtlsActive() const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104
105 // Default implementation.
deadbeefcbecd352015-09-23 11:50:27 -0700106 bool GetSslRole(rtc::SSLRole* role) const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107
deadbeefcbecd352015-09-23 11:50:27 -0700108 bool SetSslRole(rtc::SSLRole role) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000109
110 // Set up the ciphers to use for DTLS-SRTP.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800111 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 return false;
113 }
114
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000115 // Find out which DTLS-SRTP cipher was negotiated.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800116 bool GetSrtpCryptoSuite(int* cipher) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000117
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000118 // Find out which DTLS cipher was negotiated.
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700119 bool GetSslCipherSuite(int* cipher) override { return false; }
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000120
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200121 // Returns null because the channel is not encrypted by default.
122 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
123 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124 }
125
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200126 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127 return false;
128 }
129
130 // Allows key material to be extracted for external encryption.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200131 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200132 const uint8_t* context,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200133 size_t context_len,
134 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200135 uint8_t* result,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200136 size_t result_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 return false;
138 }
139
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200140 bool SetLocalCertificate(
141 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142 return false;
143 }
144
145 // Set DTLS Remote fingerprint. Must be after local identity set.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200146 bool SetRemoteFingerprint(const std::string& digest_alg,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 const uint8_t* digest,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200148 size_t digest_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149 return false;
150 }
151
Peter Thatcher54360512015-07-08 11:08:35 -0700152 int receiving_timeout() const { return receiving_timeout_; }
153 int check_receiving_delay() const { return check_receiving_delay_; }
154
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000155 // Helper method used only in unittest.
156 rtc::DiffServCodePoint DefaultDscpValue() const;
157
Peter Thatcher7351f462015-04-02 16:39:16 -0700158 // Public for unit tests.
159 Connection* FindNextPingableConnection();
160
honghaiz77d0d6e2015-10-27 11:34:45 -0700161 // Public for unit tests.
162 const std::vector<Connection*>& connections() const { return connections_; }
163
honghaiz9b669572015-11-04 12:07:44 -0800164 // Public for unit tests.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 PortAllocatorSession* allocator_session() {
166 return allocator_sessions_.back();
167 }
168
honghaiz9b669572015-11-04 12:07:44 -0800169 private:
170 rtc::Thread* thread() { return worker_thread_; }
171 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
172
honghaiza58ea782015-09-24 08:13:36 -0700173 // A transport channel is weak if the current best connection is either
174 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700175 bool weak() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176 void UpdateConnectionStates();
177 void RequestSort();
178 void SortConnections();
179 void SwitchBestConnectionTo(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800180 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800182 void MaybeStopPortAllocatorSessions();
Honghai Zhang381b4212015-12-04 12:24:03 -0800183 TransportChannelState ComputeState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000185 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700186 bool CreateConnections(const Candidate& remote_candidate,
187 PortInterface* origin_port);
188 bool CreateConnection(PortInterface* port,
189 const Candidate& remote_candidate,
190 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 bool FindConnection(cricket::Connection* connection) const;
192
Peter Boström0c4e06b2015-10-07 12:23:21 +0200193 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
195 void RememberRemoteCandidate(const Candidate& remote_candidate,
196 PortInterface* origin_port);
Honghai Zhang381b4212015-12-04 12:24:03 -0800197 bool IsPingable(Connection* conn, uint32_t now);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 void PingConnection(Connection* conn);
199 void AddAllocatorSession(PortAllocatorSession* session);
200 void AddConnection(Connection* connection);
201
202 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
203 void OnCandidatesReady(PortAllocatorSession *session,
204 const std::vector<Candidate>& candidates);
205 void OnCandidatesAllocationDone(PortAllocatorSession* session);
206 void OnUnknownAddress(PortInterface* port,
207 const rtc::SocketAddress& addr,
208 ProtocolType proto,
209 IceMessage* stun_msg,
210 const std::string& remote_username,
211 bool port_muxed);
212 void OnPortDestroyed(PortInterface* port);
213 void OnRoleConflict(PortInterface* port);
214
215 void OnConnectionStateChange(Connection* connection);
216 void OnReadPacket(Connection *connection, const char *data, size_t len,
217 const rtc::PacketTime& packet_time);
stefanc1aeaf02015-10-15 07:26:07 -0700218 void OnSentPacket(PortInterface* port, const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 void OnReadyToSend(Connection* connection);
220 void OnConnectionDestroyed(Connection *connection);
221
honghaiz5a3acd82015-08-20 15:53:17 -0700222 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000223
deadbeefcbecd352015-09-23 11:50:27 -0700224 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225 void OnSort();
honghaiza58ea782015-09-24 08:13:36 -0700226 void OnCheckAndPing();
Peter Thatcher54360512015-07-08 11:08:35 -0700227
honghaiz5a3acd82015-08-20 15:53:17 -0700228 void PruneConnections();
229 Connection* best_nominated_connection() const;
Honghai Zhang381b4212015-12-04 12:24:03 -0800230 bool IsBackupConnection(Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700231
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 P2PTransport* transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700233 PortAllocator* allocator_;
234 rtc::Thread* worker_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 int error_;
237 std::vector<PortAllocatorSession*> allocator_sessions_;
238 std::vector<PortInterface *> ports_;
239 std::vector<Connection *> connections_;
240 Connection* best_connection_;
241 // Connection selected by the controlling agent. This should be used only
242 // at controlled side when protocol type is RFC5245.
243 Connection* pending_best_connection_;
244 std::vector<RemoteCandidate> remote_candidates_;
245 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700246 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 typedef std::map<rtc::Socket::Option, int> OptionMap;
248 OptionMap options_;
249 std::string ice_ufrag_;
250 std::string ice_pwd_;
251 std::string remote_ice_ufrag_;
252 std::string remote_ice_pwd_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253 IceMode remote_ice_mode_;
254 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200255 uint64_t tiebreaker_;
256 uint32_t remote_candidate_generation_;
deadbeefcbecd352015-09-23 11:50:27 -0700257 IceGatheringState gathering_state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000258
Peter Thatcher54360512015-07-08 11:08:35 -0700259 int check_receiving_delay_;
260 int receiving_timeout_;
Honghai Zhang381b4212015-12-04 12:24:03 -0800261 int backup_connection_ping_interval_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200262 uint32_t last_ping_sent_ms_ = 0;
honghaiz1f429e32015-09-28 07:57:34 -0700263 bool gather_continually_ = false;
guoweisb0bb77f2015-10-26 15:10:01 -0700264 int weak_ping_delay_ = WEAK_PING_DELAY;
Honghai Zhang381b4212015-12-04 12:24:03 -0800265 TransportChannelState state_ = TransportChannelState::STATE_INIT;
Peter Thatcher54360512015-07-08 11:08:35 -0700266
henrikg3c089d72015-09-16 05:37:44 -0700267 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268};
269
270} // namespace cricket
271
272#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_