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 #ifndef WEBRTC_P2P_BASE_RAWTRANSPORTCHANNEL_H_
29 #define WEBRTC_P2P_BASE_RAWTRANSPORTCHANNEL_H_
33 #include "webrtc/p2p/base/candidate.h"
34 #include "webrtc/p2p/base/rawtransport.h"
35 #include "webrtc/p2p/base/transportchannelimpl.h"
36 #include "webrtc/base/messagequeue.h"
38 #if defined(FEATURE_ENABLE_PSTN)
48 class PortAllocatorSession;
53 // Implements a channel that just sends bare packets once we have received the
54 // address of the other side. We pick a single address to send them based on
55 // a simple investigation of NAT type.
56 class RawTransportChannel : public TransportChannelImpl,
57 public rtc::MessageHandler {
59 RawTransportChannel(const std::string& content_name,
61 RawTransport* transport,
62 rtc::Thread *worker_thread,
63 PortAllocator *allocator);
64 virtual ~RawTransportChannel();
66 // Implementation of normal channel packet sending.
67 virtual int SendPacket(const char *data, size_t len,
68 const rtc::PacketOptions& options, int flags);
69 virtual int SetOption(rtc::Socket::Option opt, int value);
70 virtual int GetError();
72 // Implements TransportChannelImpl.
73 virtual Transport* GetTransport() { return raw_transport_; }
74 virtual void SetIceCredentials(const std::string& ice_ufrag,
75 const std::string& ice_pwd) {}
76 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
77 const std::string& ice_pwd) {}
79 // Creates an allocator session to start figuring out which type of
80 // port we should send to the other client. This will send
81 // SignalAvailableCandidate once we have decided.
82 virtual void Connect();
84 // Resets state back to unconnected.
87 // We don't actually worry about signaling since we can't send new candidates.
88 virtual void OnSignalingReady() {}
90 // Handles a message setting the remote address. We are writable once we
91 // have this since we now know where to send.
92 virtual void OnCandidate(const Candidate& candidate);
94 void OnRemoteAddress(const rtc::SocketAddress& remote_address);
96 // Below ICE specific virtual methods not implemented.
97 virtual IceRole GetIceRole() const { return ICEROLE_UNKNOWN; }
98 virtual void SetIceRole(IceRole role) {}
99 virtual void SetIceTiebreaker(uint64 tiebreaker) {}
101 virtual bool GetIceProtocolType(IceProtocolType* type) const { return false; }
102 virtual void SetIceProtocolType(IceProtocolType type) {}
104 virtual void SetIceUfrag(const std::string& ice_ufrag) {}
105 virtual void SetIcePwd(const std::string& ice_pwd) {}
106 virtual void SetRemoteIceMode(IceMode mode) {}
107 virtual size_t GetConnectionCount() const { return 1; }
109 virtual bool GetStats(ConnectionInfos* infos) {
114 virtual bool IsDtlsActive() const { return false; }
116 // Default implementation.
117 virtual bool GetSslRole(rtc::SSLRole* role) const {
121 virtual bool SetSslRole(rtc::SSLRole role) {
125 // Set up the ciphers to use for DTLS-SRTP.
126 virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
130 // Find out which DTLS-SRTP cipher was negotiated
131 virtual bool GetSrtpCipher(std::string* cipher) {
135 // Returns false because the channel is not DTLS.
136 virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
140 virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
144 // Allows key material to be extracted for external encryption.
145 virtual bool ExportKeyingMaterial(
146 const std::string& label,
147 const uint8* context,
155 virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
159 // Set DTLS Remote fingerprint. Must be after local identity set.
160 virtual bool SetRemoteFingerprint(
161 const std::string& digest_alg,
168 RawTransport* raw_transport_;
169 rtc::Thread *worker_thread_;
170 PortAllocator* allocator_;
171 PortAllocatorSession* allocator_session_;
172 StunPort* stun_port_;
173 RelayPort* relay_port_;
174 PortInterface* port_;
176 rtc::SocketAddress remote_address_;
178 // Called when the allocator creates another port.
179 void OnPortReady(PortAllocatorSession* session, PortInterface* port);
181 // Called when one of the ports we are using has determined its address.
182 void OnCandidatesReady(PortAllocatorSession *session,
183 const std::vector<Candidate>& candidates);
185 // Called once we have chosen the port to use for communication with the
186 // other client. This will send its address and prepare the port for use.
187 void SetPort(PortInterface* port);
189 // Called once we have a port and a remote address. This will set mark the
190 // channel as writable and signal the route to the client.
193 // Called when we receive a packet from the other client.
194 void OnReadPacket(PortInterface* port, const char* data, size_t size,
195 const rtc::SocketAddress& addr);
197 // Handles a message to destroy unused ports.
198 virtual void OnMessage(rtc::Message *msg);
200 DISALLOW_EVIL_CONSTRUCTORS(RawTransportChannel);
203 } // namespace cricket
205 #endif // defined(FEATURE_ENABLE_PSTN)
206 #endif // WEBRTC_P2P_BASE_RAWTRANSPORTCHANNEL_H_