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