1 /*
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 #ifndef WEBRTC_P2P_BASE_TRANSPORTCHANNEL_H_
12 #define WEBRTC_P2P_BASE_TRANSPORTCHANNEL_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "webrtc/p2p/base/candidate.h"
18 #include "webrtc/p2p/base/transport.h"
19 #include "webrtc/p2p/base/transportdescription.h"
20 #include "webrtc/base/asyncpacketsocket.h"
21 #include "webrtc/base/basictypes.h"
22 #include "webrtc/base/dscp.h"
23 #include "webrtc/base/sigslot.h"
24 #include "webrtc/base/socket.h"
25 #include "webrtc/base/sslidentity.h"
26 #include "webrtc/base/sslstreamadapter.h"
27 
28 namespace cricket {
29 
30 class Candidate;
31 
32 // Flags for SendPacket/SignalReadPacket.
33 enum PacketFlags {
34   PF_NORMAL       = 0x00,  // A normal packet.
35   PF_SRTP_BYPASS  = 0x01,  // An encrypted SRTP packet; bypass any additional
36                            // crypto provided by the transport (e.g. DTLS)
37 };
38 
39 // Used to indicate channel's connection state.
40 enum TransportChannelState {
41   STATE_INIT,
42   STATE_CONNECTING,  // Will enter this state once a connection is created
43   STATE_COMPLETED,
44   STATE_FAILED
45 };
46 
47 // A TransportChannel represents one logical stream of packets that are sent
48 // between the two sides of a session.
49 // TODO(deadbeef): This interface currently represents the unity of an ICE
50 // transport and a DTLS transport. They need to be separated apart.
51 class TransportChannel : public sigslot::has_slots<> {
52  public:
TransportChannel(const std::string & transport_name,int component)53   TransportChannel(const std::string& transport_name, int component)
54       : transport_name_(transport_name),
55         component_(component),
56         writable_(false),
57         receiving_(false) {}
~TransportChannel()58   virtual ~TransportChannel() {}
59 
60   // TODO(guoweis) - Make this pure virtual once all subclasses of
61   // TransportChannel have this defined.
GetState()62   virtual TransportChannelState GetState() const {
63     return TransportChannelState::STATE_CONNECTING;
64   }
65 
66   // TODO(mallinath) - Remove this API, as it's no longer useful.
67   // Returns the session id of this channel.
SessionId()68   virtual const std::string SessionId() const { return std::string(); }
69 
transport_name()70   const std::string& transport_name() const { return transport_name_; }
component()71   int component() const { return component_; }
72 
73   // Returns the states of this channel.  Each time one of these states changes,
74   // a signal is raised.  These states are aggregated by the TransportManager.
writable()75   bool writable() const { return writable_; }
receiving()76   bool receiving() const { return receiving_; }
dtls_state()77   DtlsTransportState dtls_state() const { return dtls_state_; }
78   sigslot::signal1<TransportChannel*> SignalWritableState;
79   // Emitted when the TransportChannel's ability to send has changed.
80   sigslot::signal1<TransportChannel*> SignalReadyToSend;
81   sigslot::signal1<TransportChannel*> SignalReceivingState;
82   // Emitted whenever DTLS-SRTP is setup which will require setting up a new
83   // SRTP context.
84   sigslot::signal2<TransportChannel*, DtlsTransportState> SignalDtlsState;
85 
86   // Attempts to send the given packet.  The return value is < 0 on failure.
87   // TODO: Remove the default argument once channel code is updated.
88   virtual int SendPacket(const char* data, size_t len,
89                          const rtc::PacketOptions& options,
90                          int flags = 0) = 0;
91 
92   // Sets a socket option on this channel.  Note that not all options are
93   // supported by all transport types.
94   virtual int SetOption(rtc::Socket::Option opt, int value) = 0;
95   // TODO(pthatcher): Once Chrome's MockTransportChannel implments
96   // this, remove the default implementation.
GetOption(rtc::Socket::Option opt,int * value)97   virtual bool GetOption(rtc::Socket::Option opt, int* value) { return false; }
98 
99   // Returns the most recent error that occurred on this channel.
100   virtual int GetError() = 0;
101 
102   // Returns the current stats for this connection.
103   virtual bool GetStats(ConnectionInfos* infos) = 0;
104 
105   // Is DTLS active?
106   virtual bool IsDtlsActive() const = 0;
107 
108   // Default implementation.
109   virtual bool GetSslRole(rtc::SSLRole* role) const = 0;
110 
111   // Sets up the ciphers to use for DTLS-SRTP. TODO(guoweis): Make this pure
112   // virtual once all dependencies have implementation.
113   virtual bool SetSrtpCryptoSuites(const std::vector<int>& ciphers);
114 
115   // Keep the original one for backward compatibility until all dependencies
116   // move away. TODO(guoweis): Remove this function.
117   virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers);
118 
119   // Finds out which DTLS-SRTP cipher was negotiated.
120   // TODO(guoweis): Remove this once all dependencies implement this.
GetSrtpCryptoSuite(int * cipher)121   virtual bool GetSrtpCryptoSuite(int* cipher) { return false; }
122 
123   // Finds out which DTLS cipher was negotiated.
124   // TODO(guoweis): Remove this once all dependencies implement this.
GetSslCipherSuite(int * cipher)125   virtual bool GetSslCipherSuite(int* cipher) { return false; }
126 
127   // Gets the local RTCCertificate used for DTLS.
128   virtual rtc::scoped_refptr<rtc::RTCCertificate>
129   GetLocalCertificate() const = 0;
130 
131   // Gets a copy of the remote side's SSL certificate, owned by the caller.
132   virtual bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const = 0;
133 
134   // Allows key material to be extracted for external encryption.
135   virtual bool ExportKeyingMaterial(const std::string& label,
136                                     const uint8_t* context,
137                                     size_t context_len,
138                                     bool use_context,
139                                     uint8_t* result,
140                                     size_t result_len) = 0;
141 
142   // Signalled each time a packet is received on this channel.
143   sigslot::signal5<TransportChannel*, const char*,
144                    size_t, const rtc::PacketTime&, int> SignalReadPacket;
145 
146   // Signalled each time a packet is sent on this channel.
147   sigslot::signal2<TransportChannel*, const rtc::SentPacket&> SignalSentPacket;
148 
149   // This signal occurs when there is a change in the way that packets are
150   // being routed, i.e. to a different remote location. The candidate
151   // indicates where and how we are currently sending media.
152   sigslot::signal2<TransportChannel*, const Candidate&> SignalRouteChange;
153 
154   // Invoked when the channel is being destroyed.
155   sigslot::signal1<TransportChannel*> SignalDestroyed;
156 
157   // Debugging description of this transport channel.
158   std::string ToString() const;
159 
160  protected:
161   // Sets the writable state, signaling if necessary.
162   void set_writable(bool writable);
163 
164   // Sets the receiving state, signaling if necessary.
165   void set_receiving(bool receiving);
166 
167   // Sets the DTLS state, signaling if necessary.
168   void set_dtls_state(DtlsTransportState state);
169 
170  private:
171   // Used mostly for debugging.
172   std::string transport_name_;
173   int component_;
174   bool writable_;
175   bool receiving_;
176   DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW;
177 
178   RTC_DISALLOW_COPY_AND_ASSIGN(TransportChannel);
179 };
180 
181 }  // namespace cricket
182 
183 #endif  // WEBRTC_P2P_BASE_TRANSPORTCHANNEL_H_
184