blob: 92969c8b7e0436b798af444bb229ce678809a30e [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
37// Adds the port on which the candidate originated.
38class RemoteCandidate : public Candidate {
39 public:
40 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
41 : Candidate(c), origin_port_(origin_port) {}
42
43 PortInterface* origin_port() { return origin_port_; }
44
45 private:
46 PortInterface* origin_port_;
47};
48
49// P2PTransportChannel manages the candidates and connection process to keep
50// two P2P clients connected to each other.
51class P2PTransportChannel : public TransportChannelImpl,
52 public rtc::MessageHandler {
53 public:
deadbeefcbecd352015-09-23 11:50:27 -070054 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 int component,
56 P2PTransport* transport,
deadbeefcbecd352015-09-23 11:50:27 -070057 PortAllocator* allocator);
58 virtual ~P2PTransportChannel();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059
60 // From TransportChannelImpl:
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020061 Transport* GetTransport() override { return transport_; }
62 TransportChannelState GetState() const override;
63 void SetIceRole(IceRole role) override;
64 IceRole GetIceRole() const override { return ice_role_; }
65 void SetIceTiebreaker(uint64 tiebreaker) override;
66 void SetIceCredentials(const std::string& ice_ufrag,
67 const std::string& ice_pwd) override;
68 void SetRemoteIceCredentials(const std::string& ice_ufrag,
69 const std::string& ice_pwd) override;
70 void SetRemoteIceMode(IceMode mode) override;
71 void Connect() override;
deadbeefcbecd352015-09-23 11:50:27 -070072 void MaybeStartGathering() override;
73 IceGatheringState gathering_state() const override {
74 return gathering_state_;
75 }
76 void AddRemoteCandidate(const Candidate& candidate) override;
honghaiza03cd3f2015-07-13 17:08:08 -070077 // Sets the receiving timeout in milliseconds.
78 // This also sets the check_receiving_delay proportionally.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020079 void SetReceivingTimeout(int receiving_timeout_ms) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080
81 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -070082 int SendPacket(const char* data,
83 size_t len,
84 const rtc::PacketOptions& options,
85 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020086 int SetOption(rtc::Socket::Option opt, int value) override;
87 bool GetOption(rtc::Socket::Option opt, int* value) override;
88 int GetError() override { return error_; }
89 bool GetStats(std::vector<ConnectionInfo>* stats) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090
91 const Connection* best_connection() const { return best_connection_; }
92 void set_incoming_only(bool value) { incoming_only_ = value; }
93
94 // Note: This is only for testing purpose.
95 // |ports_| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -070096 const std::vector<PortInterface*>& ports() { return ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097
98 IceMode remote_ice_mode() const { return remote_ice_mode_; }
99
100 // DTLS methods.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200101 bool IsDtlsActive() const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
103 // Default implementation.
deadbeefcbecd352015-09-23 11:50:27 -0700104 bool GetSslRole(rtc::SSLRole* role) const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105
deadbeefcbecd352015-09-23 11:50:27 -0700106 bool SetSslRole(rtc::SSLRole role) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107
108 // Set up the ciphers to use for DTLS-SRTP.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200109 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110 return false;
111 }
112
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000113 // Find out which DTLS-SRTP cipher was negotiated.
deadbeefcbecd352015-09-23 11:50:27 -0700114 bool GetSrtpCipher(std::string* cipher) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000116 // Find out which DTLS cipher was negotiated.
deadbeefcbecd352015-09-23 11:50:27 -0700117 bool GetSslCipher(std::string* cipher) override { return false; }
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000118
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200119 // Returns null because the channel is not encrypted by default.
120 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
121 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 }
123
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200124 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 return false;
126 }
127
128 // Allows key material to be extracted for external encryption.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200129 bool ExportKeyingMaterial(const std::string& label,
130 const uint8* context,
131 size_t context_len,
132 bool use_context,
133 uint8* result,
134 size_t result_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 return false;
136 }
137
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200138 bool SetLocalCertificate(
139 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 return false;
141 }
142
143 // Set DTLS Remote fingerprint. Must be after local identity set.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200144 bool SetRemoteFingerprint(const std::string& digest_alg,
145 const uint8* digest,
146 size_t digest_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 return false;
148 }
149
Peter Thatcher54360512015-07-08 11:08:35 -0700150 int receiving_timeout() const { return receiving_timeout_; }
151 int check_receiving_delay() const { return check_receiving_delay_; }
152
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 // Helper method used only in unittest.
154 rtc::DiffServCodePoint DefaultDscpValue() const;
155
Peter Thatcher7351f462015-04-02 16:39:16 -0700156 // Public for unit tests.
157 Connection* FindNextPingableConnection();
158
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159 private:
160 rtc::Thread* thread() { return worker_thread_; }
161 PortAllocatorSession* allocator_session() {
162 return allocator_sessions_.back();
163 }
164
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 void UpdateConnectionStates();
166 void RequestSort();
167 void SortConnections();
168 void SwitchBestConnectionTo(Connection* conn);
169 void UpdateChannelState();
170 void HandleWritable();
171 void HandleNotWritable();
172 void HandleAllTimedOut();
173
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000174 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700175 bool CreateConnections(const Candidate& remote_candidate,
176 PortInterface* origin_port);
177 bool CreateConnection(PortInterface* port,
178 const Candidate& remote_candidate,
179 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 bool FindConnection(cricket::Connection* connection) const;
181
182 uint32 GetRemoteCandidateGeneration(const Candidate& candidate);
183 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
184 void RememberRemoteCandidate(const Candidate& remote_candidate,
185 PortInterface* origin_port);
186 bool IsPingable(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187 void PingConnection(Connection* conn);
188 void AddAllocatorSession(PortAllocatorSession* session);
189 void AddConnection(Connection* connection);
190
191 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
192 void OnCandidatesReady(PortAllocatorSession *session,
193 const std::vector<Candidate>& candidates);
194 void OnCandidatesAllocationDone(PortAllocatorSession* session);
195 void OnUnknownAddress(PortInterface* port,
196 const rtc::SocketAddress& addr,
197 ProtocolType proto,
198 IceMessage* stun_msg,
199 const std::string& remote_username,
200 bool port_muxed);
201 void OnPortDestroyed(PortInterface* port);
202 void OnRoleConflict(PortInterface* port);
203
204 void OnConnectionStateChange(Connection* connection);
205 void OnReadPacket(Connection *connection, const char *data, size_t len,
206 const rtc::PacketTime& packet_time);
207 void OnReadyToSend(Connection* connection);
208 void OnConnectionDestroyed(Connection *connection);
209
honghaiz5a3acd82015-08-20 15:53:17 -0700210 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211
deadbeefcbecd352015-09-23 11:50:27 -0700212 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 void OnSort();
214 void OnPing();
215
Peter Thatcher54360512015-07-08 11:08:35 -0700216 void OnCheckReceiving();
217
honghaiz5a3acd82015-08-20 15:53:17 -0700218 void PruneConnections();
219 Connection* best_nominated_connection() const;
220
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 P2PTransport* transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700222 PortAllocator* allocator_;
223 rtc::Thread* worker_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225 int error_;
226 std::vector<PortAllocatorSession*> allocator_sessions_;
227 std::vector<PortInterface *> ports_;
228 std::vector<Connection *> connections_;
229 Connection* best_connection_;
230 // Connection selected by the controlling agent. This should be used only
231 // at controlled side when protocol type is RFC5245.
232 Connection* pending_best_connection_;
233 std::vector<RemoteCandidate> remote_candidates_;
234 bool sort_dirty_; // indicates whether another sort is needed right now
235 bool was_writable_;
deadbeefcbecd352015-09-23 11:50:27 -0700236 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000237 typedef std::map<rtc::Socket::Option, int> OptionMap;
238 OptionMap options_;
239 std::string ice_ufrag_;
240 std::string ice_pwd_;
241 std::string remote_ice_ufrag_;
242 std::string remote_ice_pwd_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243 IceMode remote_ice_mode_;
244 IceRole ice_role_;
245 uint64 tiebreaker_;
246 uint32 remote_candidate_generation_;
deadbeefcbecd352015-09-23 11:50:27 -0700247 IceGatheringState gathering_state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000248
Peter Thatcher54360512015-07-08 11:08:35 -0700249 int check_receiving_delay_;
250 int receiving_timeout_;
251
henrikg3c089d72015-09-16 05:37:44 -0700252 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253};
254
255} // namespace cricket
256
257#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_