2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
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).
17 // When all of the available connections become invalid (non-writable), we
18 // kick off a process of determining more candidates and more connections.
20 #ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
21 #define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
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"
37 // Adds the port on which the candidate originated.
38 class RemoteCandidate : public Candidate {
40 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
41 : Candidate(c), origin_port_(origin_port) {}
43 PortInterface* origin_port() { return origin_port_; }
46 PortInterface* origin_port_;
49 // P2PTransportChannel manages the candidates and connection process to keep
50 // two P2P clients connected to each other.
51 class P2PTransportChannel : public TransportChannelImpl,
52 public rtc::MessageHandler {
54 P2PTransportChannel(const std::string& content_name,
56 P2PTransport* transport,
57 PortAllocator *allocator);
58 virtual ~P2PTransportChannel();
60 // From TransportChannelImpl:
61 virtual Transport* GetTransport() { return transport_; }
62 virtual void SetIceRole(IceRole role);
63 virtual IceRole GetIceRole() const { return ice_role_; }
64 virtual void SetIceTiebreaker(uint64 tiebreaker);
65 virtual size_t GetConnectionCount() const { return connections_.size(); }
66 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();
75 virtual void OnSignalingReady();
76 virtual void OnCandidate(const Candidate& candidate);
78 // From TransportChannel:
79 virtual int SendPacket(const char *data, size_t len,
80 const rtc::PacketOptions& options, int flags);
81 virtual int SetOption(rtc::Socket::Option opt, int value);
82 virtual int GetError() { return error_; }
83 virtual bool GetStats(std::vector<ConnectionInfo>* stats);
85 const Connection* best_connection() const { return best_connection_; }
86 void set_incoming_only(bool value) { incoming_only_ = value; }
88 // Note: This is only for testing purpose.
89 // |ports_| should not be changed from outside.
90 const std::vector<PortInterface *>& ports() { return ports_; }
92 IceMode remote_ice_mode() const { return remote_ice_mode_; }
95 virtual bool IsDtlsActive() const { return false; }
97 // Default implementation.
98 virtual bool GetSslRole(rtc::SSLRole* role) const {
102 virtual bool SetSslRole(rtc::SSLRole role) {
106 // Set up the ciphers to use for DTLS-SRTP.
107 virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
111 // Find out which DTLS-SRTP cipher was negotiated
112 virtual bool GetSrtpCipher(std::string* cipher) {
116 // Returns false because the channel is not encrypted by default.
117 virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
121 virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
125 // Allows key material to be extracted for external encryption.
126 virtual bool ExportKeyingMaterial(
127 const std::string& label,
128 const uint8* context,
136 virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
140 // Set DTLS Remote fingerprint. Must be after local identity set.
141 virtual bool SetRemoteFingerprint(
142 const std::string& digest_alg,
148 // Helper method used only in unittest.
149 rtc::DiffServCodePoint DefaultDscpValue() const;
152 rtc::Thread* thread() { return worker_thread_; }
153 PortAllocatorSession* allocator_session() {
154 return allocator_sessions_.back();
158 void UpdateConnectionStates();
160 void SortConnections();
161 void SwitchBestConnectionTo(Connection* conn);
162 void UpdateChannelState();
163 void HandleWritable();
164 void HandleNotWritable();
165 void HandleAllTimedOut();
167 Connection* GetBestConnectionOnNetwork(rtc::Network* network);
168 bool CreateConnections(const Candidate &remote_candidate,
169 PortInterface* origin_port, bool readable);
170 bool CreateConnection(PortInterface* port, const Candidate& remote_candidate,
171 PortInterface* origin_port, bool readable);
172 bool FindConnection(cricket::Connection* connection) const;
174 uint32 GetRemoteCandidateGeneration(const Candidate& candidate);
175 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
176 void RememberRemoteCandidate(const Candidate& remote_candidate,
177 PortInterface* origin_port);
178 bool IsPingable(Connection* conn);
179 Connection* FindNextPingableConnection();
180 void PingConnection(Connection* conn);
181 void AddAllocatorSession(PortAllocatorSession* session);
182 void AddConnection(Connection* connection);
184 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
185 void OnCandidatesReady(PortAllocatorSession *session,
186 const std::vector<Candidate>& candidates);
187 void OnCandidatesAllocationDone(PortAllocatorSession* session);
188 void OnUnknownAddress(PortInterface* port,
189 const rtc::SocketAddress& addr,
191 IceMessage* stun_msg,
192 const std::string& remote_username,
194 void OnPortDestroyed(PortInterface* port);
195 void OnRoleConflict(PortInterface* port);
197 void OnConnectionStateChange(Connection* connection);
198 void OnReadPacket(Connection *connection, const char *data, size_t len,
199 const rtc::PacketTime& packet_time);
200 void OnReadyToSend(Connection* connection);
201 void OnConnectionDestroyed(Connection *connection);
203 void OnUseCandidate(Connection* conn);
205 virtual void OnMessage(rtc::Message *pmsg);
209 P2PTransport* transport_;
210 PortAllocator *allocator_;
211 rtc::Thread *worker_thread_;
213 bool waiting_for_signaling_;
215 std::vector<PortAllocatorSession*> allocator_sessions_;
216 std::vector<PortInterface *> ports_;
217 std::vector<Connection *> connections_;
218 Connection* best_connection_;
219 // Connection selected by the controlling agent. This should be used only
220 // at controlled side when protocol type is RFC5245.
221 Connection* pending_best_connection_;
222 std::vector<RemoteCandidate> remote_candidates_;
223 bool sort_dirty_; // indicates whether another sort is needed right now
225 typedef std::map<rtc::Socket::Option, int> OptionMap;
227 std::string ice_ufrag_;
228 std::string ice_pwd_;
229 std::string remote_ice_ufrag_;
230 std::string remote_ice_pwd_;
231 IceProtocolType protocol_type_;
232 IceMode remote_ice_mode_;
235 uint32 remote_candidate_generation_;
237 DISALLOW_EVIL_CONSTRUCTORS(P2PTransportChannel);
240 } // namespace cricket
242 #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_