blob: 5405a9c0c7ad1a5b59f05e1cdaa28bf6bb936bba [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:
54 P2PTransportChannel(const std::string& content_name,
55 int component,
56 P2PTransport* transport,
57 PortAllocator *allocator);
58 virtual ~P2PTransportChannel();
59
60 // From TransportChannelImpl:
61 virtual Transport* GetTransport() { return transport_; }
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +000062 virtual TransportChannelState GetState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 virtual void SetIceRole(IceRole role);
64 virtual IceRole GetIceRole() const { return ice_role_; }
65 virtual void SetIceTiebreaker(uint64 tiebreaker);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066 virtual bool GetIceProtocolType(IceProtocolType* type) const;
67 virtual void SetIceProtocolType(IceProtocolType type);
68 virtual void SetIceCredentials(const std::string& ice_ufrag,
69 const std::string& ice_pwd);
70 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
71 const std::string& ice_pwd);
72 virtual void SetRemoteIceMode(IceMode mode);
73 virtual void Connect();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074 virtual void OnSignalingReady();
75 virtual void OnCandidate(const Candidate& candidate);
76
77 // From TransportChannel:
78 virtual int SendPacket(const char *data, size_t len,
79 const rtc::PacketOptions& options, int flags);
80 virtual int SetOption(rtc::Socket::Option opt, int value);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +000081 virtual bool GetOption(rtc::Socket::Option opt, int* value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 virtual int GetError() { return error_; }
83 virtual bool GetStats(std::vector<ConnectionInfo>* stats);
84
85 const Connection* best_connection() const { return best_connection_; }
86 void set_incoming_only(bool value) { incoming_only_ = value; }
87
Peter Thatcher54360512015-07-08 11:08:35 -070088 // Sets the receiving timeout in milliseconds.
89 // This also sets the check_receiving_delay proportionally.
90 void set_receiving_timeout(int receiving_timeout_ms);
91
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092 // Note: This is only for testing purpose.
93 // |ports_| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -070094 const std::vector<PortInterface*>& ports() { return ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095
96 IceMode remote_ice_mode() const { return remote_ice_mode_; }
97
98 // DTLS methods.
99 virtual bool IsDtlsActive() const { return false; }
100
101 // Default implementation.
102 virtual bool GetSslRole(rtc::SSLRole* role) const {
103 return false;
104 }
105
106 virtual bool SetSslRole(rtc::SSLRole role) {
107 return false;
108 }
109
110 // Set up the ciphers to use for DTLS-SRTP.
111 virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
112 return false;
113 }
114
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000115 // Find out which DTLS-SRTP cipher was negotiated.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 virtual bool GetSrtpCipher(std::string* cipher) {
117 return false;
118 }
119
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000120 // Find out which DTLS cipher was negotiated.
121 virtual bool GetSslCipher(std::string* cipher) {
122 return false;
123 }
124
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 // Returns false because the channel is not encrypted by default.
126 virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
127 return false;
128 }
129
130 virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
131 return false;
132 }
133
134 // Allows key material to be extracted for external encryption.
135 virtual bool ExportKeyingMaterial(
136 const std::string& label,
137 const uint8* context,
138 size_t context_len,
139 bool use_context,
140 uint8* result,
141 size_t result_len) {
142 return false;
143 }
144
145 virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
146 return false;
147 }
148
149 // Set DTLS Remote fingerprint. Must be after local identity set.
150 virtual bool SetRemoteFingerprint(
151 const std::string& digest_alg,
152 const uint8* digest,
153 size_t digest_len) {
154 return false;
155 }
156
Peter Thatcher54360512015-07-08 11:08:35 -0700157 int receiving_timeout() const { return receiving_timeout_; }
158 int check_receiving_delay() const { return check_receiving_delay_; }
159
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 // Helper method used only in unittest.
161 rtc::DiffServCodePoint DefaultDscpValue() const;
162
Peter Thatcher7351f462015-04-02 16:39:16 -0700163 // Public for unit tests.
164 Connection* FindNextPingableConnection();
165
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 private:
167 rtc::Thread* thread() { return worker_thread_; }
168 PortAllocatorSession* allocator_session() {
169 return allocator_sessions_.back();
170 }
171
172 void Allocate();
173 void UpdateConnectionStates();
174 void RequestSort();
175 void SortConnections();
176 void SwitchBestConnectionTo(Connection* conn);
177 void UpdateChannelState();
178 void HandleWritable();
179 void HandleNotWritable();
180 void HandleAllTimedOut();
181
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000182 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 bool CreateConnections(const Candidate &remote_candidate,
184 PortInterface* origin_port, bool readable);
185 bool CreateConnection(PortInterface* port, const Candidate& remote_candidate,
186 PortInterface* origin_port, bool readable);
187 bool FindConnection(cricket::Connection* connection) const;
188
189 uint32 GetRemoteCandidateGeneration(const Candidate& candidate);
190 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
191 void RememberRemoteCandidate(const Candidate& remote_candidate,
192 PortInterface* origin_port);
193 bool IsPingable(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 void PingConnection(Connection* conn);
195 void AddAllocatorSession(PortAllocatorSession* session);
196 void AddConnection(Connection* connection);
197
198 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
199 void OnCandidatesReady(PortAllocatorSession *session,
200 const std::vector<Candidate>& candidates);
201 void OnCandidatesAllocationDone(PortAllocatorSession* session);
202 void OnUnknownAddress(PortInterface* port,
203 const rtc::SocketAddress& addr,
204 ProtocolType proto,
205 IceMessage* stun_msg,
206 const std::string& remote_username,
207 bool port_muxed);
208 void OnPortDestroyed(PortInterface* port);
209 void OnRoleConflict(PortInterface* port);
210
211 void OnConnectionStateChange(Connection* connection);
212 void OnReadPacket(Connection *connection, const char *data, size_t len,
213 const rtc::PacketTime& packet_time);
214 void OnReadyToSend(Connection* connection);
215 void OnConnectionDestroyed(Connection *connection);
216
217 void OnUseCandidate(Connection* conn);
218
219 virtual void OnMessage(rtc::Message *pmsg);
220 void OnSort();
221 void OnPing();
222
Peter Thatcher54360512015-07-08 11:08:35 -0700223 void OnCheckReceiving();
224
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225 P2PTransport* transport_;
226 PortAllocator *allocator_;
227 rtc::Thread *worker_thread_;
228 bool incoming_only_;
229 bool waiting_for_signaling_;
230 int error_;
231 std::vector<PortAllocatorSession*> allocator_sessions_;
232 std::vector<PortInterface *> ports_;
233 std::vector<Connection *> connections_;
234 Connection* best_connection_;
235 // Connection selected by the controlling agent. This should be used only
236 // at controlled side when protocol type is RFC5245.
237 Connection* pending_best_connection_;
238 std::vector<RemoteCandidate> remote_candidates_;
239 bool sort_dirty_; // indicates whether another sort is needed right now
240 bool was_writable_;
241 typedef std::map<rtc::Socket::Option, int> OptionMap;
242 OptionMap options_;
243 std::string ice_ufrag_;
244 std::string ice_pwd_;
245 std::string remote_ice_ufrag_;
246 std::string remote_ice_pwd_;
247 IceProtocolType protocol_type_;
248 IceMode remote_ice_mode_;
249 IceRole ice_role_;
250 uint64 tiebreaker_;
251 uint32 remote_candidate_generation_;
252
Peter Thatcher54360512015-07-08 11:08:35 -0700253 int check_receiving_delay_;
254 int receiving_timeout_;
255
Thiago Farinaae0f0ee2015-04-04 23:56:53 +0000256 DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257};
258
259} // namespace cricket
260
261#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_