• 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 #ifndef TALK_P2P_BASE_TRANSPORTCHANNEL_H_
29 #define TALK_P2P_BASE_TRANSPORTCHANNEL_H_
30 
31 #include <string>
32 #include <vector>
33 
34 #include "talk/base/asyncpacketsocket.h"
35 #include "talk/base/basictypes.h"
36 #include "talk/base/dscp.h"
37 #include "talk/base/sigslot.h"
38 #include "talk/base/socket.h"
39 #include "talk/base/sslidentity.h"
40 #include "talk/base/sslstreamadapter.h"
41 #include "talk/p2p/base/candidate.h"
42 #include "talk/p2p/base/transport.h"
43 #include "talk/p2p/base/transportdescription.h"
44 
45 namespace cricket {
46 
47 class Candidate;
48 
49 // Flags for SendPacket/SignalReadPacket.
50 enum PacketFlags {
51   PF_NORMAL       = 0x00,  // A normal packet.
52   PF_SRTP_BYPASS  = 0x01,  // An encrypted SRTP packet; bypass any additional
53                            // crypto provided by the transport (e.g. DTLS)
54 };
55 
56 // A TransportChannel represents one logical stream of packets that are sent
57 // between the two sides of a session.
58 class TransportChannel : public sigslot::has_slots<> {
59  public:
TransportChannel(const std::string & content_name,int component)60   explicit TransportChannel(const std::string& content_name, int component)
61       : content_name_(content_name),
62         component_(component),
63         readable_(false), writable_(false) {}
~TransportChannel()64   virtual ~TransportChannel() {}
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 
content_name()70   const std::string& content_name() const { return content_name_; }
component()71   int component() const { return component_; }
72 
73   // Returns the readable and states of this channel.  Each time one of these
74   // states changes, a signal is raised.  These states are aggregated by the
75   // TransportManager.
readable()76   bool readable() const { return readable_; }
writable()77   bool writable() const { return writable_; }
78   sigslot::signal1<TransportChannel*> SignalReadableState;
79   sigslot::signal1<TransportChannel*> SignalWritableState;
80   // Emitted when the TransportChannel's ability to send has changed.
81   sigslot::signal1<TransportChannel*> SignalReadyToSend;
82 
83   // Attempts to send the given packet.  The return value is < 0 on failure.
84   // TODO: Remove the default argument once channel code is updated.
85   virtual int SendPacket(const char* data, size_t len,
86                          const talk_base::PacketOptions& options,
87                          int flags = 0) = 0;
88 
89   // Sets a socket option on this channel.  Note that not all options are
90   // supported by all transport types.
91   virtual int SetOption(talk_base::Socket::Option opt, int value) = 0;
92 
93   // Returns the most recent error that occurred on this channel.
94   virtual int GetError() = 0;
95 
96   // Returns the current stats for this connection.
97   virtual bool GetStats(ConnectionInfos* infos) = 0;
98 
99   // Is DTLS active?
100   virtual bool IsDtlsActive() const = 0;
101 
102   // Default implementation.
103   virtual bool GetSslRole(talk_base::SSLRole* role) const = 0;
104 
105   // Sets up the ciphers to use for DTLS-SRTP.
106   virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) = 0;
107 
108   // Finds out which DTLS-SRTP cipher was negotiated
109   virtual bool GetSrtpCipher(std::string* cipher) = 0;
110 
111   // Gets a copy of the local SSL identity, owned by the caller.
112   virtual bool GetLocalIdentity(talk_base::SSLIdentity** identity) const = 0;
113 
114   // Gets a copy of the remote side's SSL certificate, owned by the caller.
115   virtual bool GetRemoteCertificate(talk_base::SSLCertificate** cert) const = 0;
116 
117   // Allows key material to be extracted for external encryption.
118   virtual bool ExportKeyingMaterial(const std::string& label,
119       const uint8* context,
120       size_t context_len,
121       bool use_context,
122       uint8* result,
123       size_t result_len) = 0;
124 
125   // Signalled each time a packet is received on this channel.
126   sigslot::signal5<TransportChannel*, const char*,
127                    size_t, const talk_base::PacketTime&, int> SignalReadPacket;
128 
129   // This signal occurs when there is a change in the way that packets are
130   // being routed, i.e. to a different remote location. The candidate
131   // indicates where and how we are currently sending media.
132   sigslot::signal2<TransportChannel*, const Candidate&> SignalRouteChange;
133 
134   // Invoked when the channel is being destroyed.
135   sigslot::signal1<TransportChannel*> SignalDestroyed;
136 
137   // Debugging description of this transport channel.
138   std::string ToString() const;
139 
140  protected:
141   // Sets the readable state, signaling if necessary.
142   void set_readable(bool readable);
143 
144   // Sets the writable state, signaling if necessary.
145   void set_writable(bool writable);
146 
147 
148  private:
149   // Used mostly for debugging.
150   std::string content_name_;
151   int component_;
152   bool readable_;
153   bool writable_;
154 
155   DISALLOW_EVIL_CONSTRUCTORS(TransportChannel);
156 };
157 
158 }  // namespace cricket
159 
160 #endif  // TALK_P2P_BASE_TRANSPORTCHANNEL_H_
161