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_RAWTRANSPORTCHANNEL_H_
29#define TALK_P2P_BASE_RAWTRANSPORTCHANNEL_H_
30
31#include <string>
32#include <vector>
33#include "talk/base/messagequeue.h"
34#include "talk/p2p/base/transportchannelimpl.h"
35#include "talk/p2p/base/rawtransport.h"
36#include "talk/p2p/base/candidate.h"
37
38#if defined(FEATURE_ENABLE_PSTN)
39
40namespace talk_base {
41class Thread;
42}
43
44namespace cricket {
45
46class Connection;
47class PortAllocator;
48class PortAllocatorSession;
49class PortInterface;
50class RelayPort;
51class StunPort;
52
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.
56class RawTransportChannel : public TransportChannelImpl,
57    public talk_base::MessageHandler {
58 public:
59  RawTransportChannel(const std::string& content_name,
60                      int component,
61                      RawTransport* transport,
62                      talk_base::Thread *worker_thread,
63                      PortAllocator *allocator);
64  virtual ~RawTransportChannel();
65
66  // Implementation of normal channel packet sending.
67  virtual int SendPacket(const char *data, size_t len,
68                         talk_base::DiffServCodePoint dscp, int flags);
69  virtual int SetOption(talk_base::Socket::Option opt, int value);
70  virtual int GetError();
71
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) {}
78
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();
83
84  // Resets state back to unconnected.
85  virtual void Reset();
86
87  // We don't actually worry about signaling since we can't send new candidates.
88  virtual void OnSignalingReady() {}
89
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);
93
94  void OnRemoteAddress(const talk_base::SocketAddress& remote_address);
95
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) {}
100  virtual void SetIceProtocolType(IceProtocolType type) {}
101  virtual void SetIceUfrag(const std::string& ice_ufrag) {}
102  virtual void SetIcePwd(const std::string& ice_pwd) {}
103  virtual void SetRemoteIceMode(IceMode mode) {}
104
105  virtual bool GetStats(ConnectionInfos* infos) {
106    return false;
107  }
108
109  // DTLS methods.
110  virtual bool IsDtlsActive() const { return false; }
111
112  // Default implementation.
113  virtual bool GetSslRole(talk_base::SSLRole* role) const {
114    return false;
115  }
116
117  virtual bool SetSslRole(talk_base::SSLRole role) {
118    return false;
119  }
120
121  // Set up the ciphers to use for DTLS-SRTP.
122  virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
123    return false;
124  }
125
126  // Find out which DTLS-SRTP cipher was negotiated
127  virtual bool GetSrtpCipher(std::string* cipher) {
128    return false;
129  }
130
131  // Returns false because the channel is not DTLS.
132  virtual bool GetLocalIdentity(talk_base::SSLIdentity** identity) const {
133    return false;
134  }
135
136  virtual bool GetRemoteCertificate(talk_base::SSLCertificate** cert) const {
137    return false;
138  }
139
140  // Allows key material to be extracted for external encryption.
141  virtual bool ExportKeyingMaterial(
142      const std::string& label,
143      const uint8* context,
144      size_t context_len,
145      bool use_context,
146      uint8* result,
147      size_t result_len) {
148    return false;
149  }
150
151  virtual bool SetLocalIdentity(talk_base::SSLIdentity* identity) {
152    return false;
153  }
154
155  // Set DTLS Remote fingerprint. Must be after local identity set.
156  virtual bool SetRemoteFingerprint(
157    const std::string& digest_alg,
158    const uint8* digest,
159    size_t digest_len) {
160    return false;
161  }
162
163 private:
164  RawTransport* raw_transport_;
165  talk_base::Thread *worker_thread_;
166  PortAllocator* allocator_;
167  PortAllocatorSession* allocator_session_;
168  StunPort* stun_port_;
169  RelayPort* relay_port_;
170  PortInterface* port_;
171  bool use_relay_;
172  talk_base::SocketAddress remote_address_;
173
174  // Called when the allocator creates another port.
175  void OnPortReady(PortAllocatorSession* session, PortInterface* port);
176
177  // Called when one of the ports we are using has determined its address.
178  void OnCandidatesReady(PortAllocatorSession *session,
179                         const std::vector<Candidate>& candidates);
180
181  // Called once we have chosen the port to use for communication with the
182  // other client.  This will send its address and prepare the port for use.
183  void SetPort(PortInterface* port);
184
185  // Called once we have a port and a remote address.  This will set mark the
186  // channel as writable and signal the route to the client.
187  void SetWritable();
188
189  // Called when we receive a packet from the other client.
190  void OnReadPacket(PortInterface* port, const char* data, size_t size,
191                    const talk_base::SocketAddress& addr);
192
193  // Handles a message to destroy unused ports.
194  virtual void OnMessage(talk_base::Message *msg);
195
196  DISALLOW_EVIL_CONSTRUCTORS(RawTransportChannel);
197};
198
199}  // namespace cricket
200
201#endif  // defined(FEATURE_ENABLE_PSTN)
202#endif  // TALK_P2P_BASE_RAWTRANSPORTCHANNEL_H_
203