• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2004--2005, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27 
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).
33 //
34 // When all of the available connections become invalid (non-writable), we
35 // kick off a process of determining more candidates and more connections.
36 //
37 #ifndef TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
38 #define TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
39 
40 #include <map>
41 #include <string>
42 #include <vector>
43 #include "talk/p2p/base/candidate.h"
44 #include "talk/p2p/base/p2ptransport.h"
45 #include "talk/p2p/base/portallocator.h"
46 #include "talk/p2p/base/portinterface.h"
47 #include "talk/p2p/base/transport.h"
48 #include "talk/p2p/base/transportchannelimpl.h"
49 #include "webrtc/base/asyncpacketsocket.h"
50 #include "webrtc/base/sigslot.h"
51 
52 namespace cricket {
53 
54 // Adds the port on which the candidate originated.
55 class RemoteCandidate : public Candidate {
56  public:
RemoteCandidate(const Candidate & c,PortInterface * origin_port)57   RemoteCandidate(const Candidate& c, PortInterface* origin_port)
58       : Candidate(c), origin_port_(origin_port) {}
59 
origin_port()60   PortInterface* origin_port() { return origin_port_; }
61 
62  private:
63   PortInterface* origin_port_;
64 };
65 
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 {
70  public:
71   P2PTransportChannel(const std::string& content_name,
72                       int component,
73                       P2PTransport* transport,
74                       PortAllocator *allocator);
75   virtual ~P2PTransportChannel();
76 
77   // From TransportChannelImpl:
GetTransport()78   virtual Transport* GetTransport() { return transport_; }
79   virtual void SetIceRole(IceRole role);
GetIceRole()80   virtual IceRole GetIceRole() const { return ice_role_; }
81   virtual void SetIceTiebreaker(uint64 tiebreaker);
GetConnectionCount()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();
91   virtual void Reset();
92   virtual void OnSignalingReady();
93   virtual void OnCandidate(const Candidate& candidate);
94 
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);
GetError()99   virtual int GetError() { return error_; }
100   virtual bool GetStats(std::vector<ConnectionInfo>* stats);
101 
best_connection()102   const Connection* best_connection() const { return best_connection_; }
set_incoming_only(bool value)103   void set_incoming_only(bool value) { incoming_only_ = value; }
104 
105   // Note: This is only for testing purpose.
106   // |ports_| should not be changed from outside.
ports()107   const std::vector<PortInterface *>& ports() { return ports_; }
108 
remote_ice_mode()109   IceMode remote_ice_mode() const { return remote_ice_mode_; }
110 
111   // DTLS methods.
IsDtlsActive()112   virtual bool IsDtlsActive() const { return false; }
113 
114   // Default implementation.
GetSslRole(rtc::SSLRole * role)115   virtual bool GetSslRole(rtc::SSLRole* role) const {
116     return false;
117   }
118 
SetSslRole(rtc::SSLRole role)119   virtual bool SetSslRole(rtc::SSLRole role) {
120     return false;
121   }
122 
123   // Set up the ciphers to use for DTLS-SRTP.
SetSrtpCiphers(const std::vector<std::string> & ciphers)124   virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
125     return false;
126   }
127 
128   // Find out which DTLS-SRTP cipher was negotiated
GetSrtpCipher(std::string * cipher)129   virtual bool GetSrtpCipher(std::string* cipher) {
130     return false;
131   }
132 
133   // Returns false because the channel is not encrypted by default.
GetLocalIdentity(rtc::SSLIdentity ** identity)134   virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
135     return false;
136   }
137 
GetRemoteCertificate(rtc::SSLCertificate ** cert)138   virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
139     return false;
140   }
141 
142   // Allows key material to be extracted for external encryption.
ExportKeyingMaterial(const std::string & label,const uint8 * context,size_t context_len,bool use_context,uint8 * result,size_t result_len)143   virtual bool ExportKeyingMaterial(
144       const std::string& label,
145       const uint8* context,
146       size_t context_len,
147       bool use_context,
148       uint8* result,
149       size_t result_len) {
150     return false;
151   }
152 
SetLocalIdentity(rtc::SSLIdentity * identity)153   virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
154     return false;
155   }
156 
157   // Set DTLS Remote fingerprint. Must be after local identity set.
SetRemoteFingerprint(const std::string & digest_alg,const uint8 * digest,size_t digest_len)158   virtual bool SetRemoteFingerprint(
159     const std::string& digest_alg,
160     const uint8* digest,
161     size_t digest_len) {
162     return false;
163   }
164 
165   // Helper method used only in unittest.
166   rtc::DiffServCodePoint DefaultDscpValue() const;
167 
168  private:
thread()169   rtc::Thread* thread() { return worker_thread_; }
allocator_session()170   PortAllocatorSession* allocator_session() {
171     return allocator_sessions_.back();
172   }
173 
174   void Allocate();
175   void UpdateConnectionStates();
176   void RequestSort();
177   void SortConnections();
178   void SwitchBestConnectionTo(Connection* conn);
179   void UpdateChannelState();
180   void HandleWritable();
181   void HandleNotWritable();
182   void HandleAllTimedOut();
183 
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;
190 
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);
200 
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,
207                         ProtocolType proto,
208                         IceMessage* stun_msg,
209                         const std::string& remote_username,
210                         bool port_muxed);
211   void OnPortDestroyed(PortInterface* port);
212   void OnRoleConflict(PortInterface* port);
213 
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);
219 
220   void OnUseCandidate(Connection* conn);
221 
222   virtual void OnMessage(rtc::Message *pmsg);
223   void OnSort();
224   void OnPing();
225 
226   P2PTransport* transport_;
227   PortAllocator *allocator_;
228   rtc::Thread *worker_thread_;
229   bool incoming_only_;
230   bool waiting_for_signaling_;
231   int error_;
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
241   bool was_writable_;
242   typedef std::map<rtc::Socket::Option, int> OptionMap;
243   OptionMap options_;
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_;
250   IceRole ice_role_;
251   uint64 tiebreaker_;
252   uint32 remote_candidate_generation_;
253 
254   DISALLOW_EVIL_CONSTRUCTORS(P2PTransportChannel);
255 };
256 
257 }  // namespace cricket
258 
259 #endif  // TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
260