3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // P2PTransportChannel wraps up the state management of the connection between
29 // two P2P clients. Clients have candidate ports for connecting, and
30 // connections which are combinations of candidates from each end (Alice and
31 // Bob each have candidates, one candidate from Alice and one candidate from
32 // Bob are used to make a connection, repeat to make many connections).
34 // When all of the available connections become invalid (non-writable), we
35 // kick off a process of determining more candidates and more connections.
37 #ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
38 #define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
43 #include "webrtc/p2p/base/candidate.h"
44 #include "webrtc/p2p/base/p2ptransport.h"
45 #include "webrtc/p2p/base/portallocator.h"
46 #include "webrtc/p2p/base/portinterface.h"
47 #include "webrtc/p2p/base/transport.h"
48 #include "webrtc/p2p/base/transportchannelimpl.h"
49 #include "webrtc/base/asyncpacketsocket.h"
50 #include "webrtc/base/sigslot.h"
54 // Adds the port on which the candidate originated.
55 class RemoteCandidate : public Candidate {
57 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
58 : Candidate(c), origin_port_(origin_port) {}
60 PortInterface* origin_port() { return origin_port_; }
63 PortInterface* origin_port_;
66 // P2PTransportChannel manages the candidates and connection process to keep
67 // two P2P clients connected to each other.
68 class P2PTransportChannel : public TransportChannelImpl,
69 public rtc::MessageHandler {
71 P2PTransportChannel(const std::string& content_name,
73 P2PTransport* transport,
74 PortAllocator *allocator);
75 virtual ~P2PTransportChannel();
77 // From TransportChannelImpl:
78 virtual Transport* GetTransport() { return transport_; }
79 virtual void SetIceRole(IceRole role);
80 virtual IceRole GetIceRole() const { return ice_role_; }
81 virtual void SetIceTiebreaker(uint64 tiebreaker);
82 virtual size_t GetConnectionCount() const { return connections_.size(); }
83 virtual bool GetIceProtocolType(IceProtocolType* type) const;
84 virtual void SetIceProtocolType(IceProtocolType type);
85 virtual void SetIceCredentials(const std::string& ice_ufrag,
86 const std::string& ice_pwd);
87 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
88 const std::string& ice_pwd);
89 virtual void SetRemoteIceMode(IceMode mode);
90 virtual void Connect();
92 virtual void OnSignalingReady();
93 virtual void OnCandidate(const Candidate& candidate);
95 // From TransportChannel:
96 virtual int SendPacket(const char *data, size_t len,
97 const rtc::PacketOptions& options, int flags);
98 virtual int SetOption(rtc::Socket::Option opt, int value);
99 virtual int GetError() { return error_; }
100 virtual bool GetStats(std::vector<ConnectionInfo>* stats);
102 const Connection* best_connection() const { return best_connection_; }
103 void set_incoming_only(bool value) { incoming_only_ = value; }
105 // Note: This is only for testing purpose.
106 // |ports_| should not be changed from outside.
107 const std::vector<PortInterface *>& ports() { return ports_; }
109 IceMode remote_ice_mode() const { return remote_ice_mode_; }
112 virtual bool IsDtlsActive() const { return false; }
114 // Default implementation.
115 virtual bool GetSslRole(rtc::SSLRole* role) const {
119 virtual bool SetSslRole(rtc::SSLRole role) {
123 // Set up the ciphers to use for DTLS-SRTP.
124 virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
128 // Find out which DTLS-SRTP cipher was negotiated
129 virtual bool GetSrtpCipher(std::string* cipher) {
133 // Returns false because the channel is not encrypted by default.
134 virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
138 virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
142 // Allows key material to be extracted for external encryption.
143 virtual bool ExportKeyingMaterial(
144 const std::string& label,
145 const uint8* context,
153 virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
157 // Set DTLS Remote fingerprint. Must be after local identity set.
158 virtual bool SetRemoteFingerprint(
159 const std::string& digest_alg,
165 // Helper method used only in unittest.
166 rtc::DiffServCodePoint DefaultDscpValue() const;
169 rtc::Thread* thread() { return worker_thread_; }
170 PortAllocatorSession* allocator_session() {
171 return allocator_sessions_.back();
175 void UpdateConnectionStates();
177 void SortConnections();
178 void SwitchBestConnectionTo(Connection* conn);
179 void UpdateChannelState();
180 void HandleWritable();
181 void HandleNotWritable();
182 void HandleAllTimedOut();
184 Connection* GetBestConnectionOnNetwork(rtc::Network* network);
185 bool CreateConnections(const Candidate &remote_candidate,
186 PortInterface* origin_port, bool readable);
187 bool CreateConnection(PortInterface* port, const Candidate& remote_candidate,
188 PortInterface* origin_port, bool readable);
189 bool FindConnection(cricket::Connection* connection) const;
191 uint32 GetRemoteCandidateGeneration(const Candidate& candidate);
192 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
193 void RememberRemoteCandidate(const Candidate& remote_candidate,
194 PortInterface* origin_port);
195 bool IsPingable(Connection* conn);
196 Connection* FindNextPingableConnection();
197 void PingConnection(Connection* conn);
198 void AddAllocatorSession(PortAllocatorSession* session);
199 void AddConnection(Connection* connection);
201 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
202 void OnCandidatesReady(PortAllocatorSession *session,
203 const std::vector<Candidate>& candidates);
204 void OnCandidatesAllocationDone(PortAllocatorSession* session);
205 void OnUnknownAddress(PortInterface* port,
206 const rtc::SocketAddress& addr,
208 IceMessage* stun_msg,
209 const std::string& remote_username,
211 void OnPortDestroyed(PortInterface* port);
212 void OnRoleConflict(PortInterface* port);
214 void OnConnectionStateChange(Connection* connection);
215 void OnReadPacket(Connection *connection, const char *data, size_t len,
216 const rtc::PacketTime& packet_time);
217 void OnReadyToSend(Connection* connection);
218 void OnConnectionDestroyed(Connection *connection);
220 void OnUseCandidate(Connection* conn);
222 virtual void OnMessage(rtc::Message *pmsg);
226 P2PTransport* transport_;
227 PortAllocator *allocator_;
228 rtc::Thread *worker_thread_;
230 bool waiting_for_signaling_;
232 std::vector<PortAllocatorSession*> allocator_sessions_;
233 std::vector<PortInterface *> ports_;
234 std::vector<Connection *> connections_;
235 Connection* best_connection_;
236 // Connection selected by the controlling agent. This should be used only
237 // at controlled side when protocol type is RFC5245.
238 Connection* pending_best_connection_;
239 std::vector<RemoteCandidate> remote_candidates_;
240 bool sort_dirty_; // indicates whether another sort is needed right now
242 typedef std::map<rtc::Socket::Option, int> OptionMap;
244 std::string ice_ufrag_;
245 std::string ice_pwd_;
246 std::string remote_ice_ufrag_;
247 std::string remote_ice_pwd_;
248 IceProtocolType protocol_type_;
249 IceMode remote_ice_mode_;
252 uint32 remote_candidate_generation_;
254 DISALLOW_EVIL_CONSTRUCTORS(P2PTransportChannel);
257 } // namespace cricket
259 #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_