Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / p2p / base / rawtransportchannel.h
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_RAWTRANSPORTCHANNEL_H_
12 #define WEBRTC_P2P_BASE_RAWTRANSPORTCHANNEL_H_
13
14 #include <string>
15 #include <vector>
16 #include "webrtc/p2p/base/candidate.h"
17 #include "webrtc/p2p/base/rawtransport.h"
18 #include "webrtc/p2p/base/transportchannelimpl.h"
19 #include "webrtc/base/messagequeue.h"
20
21 #if defined(FEATURE_ENABLE_PSTN)
22
23 namespace rtc {
24 class Thread;
25 }
26
27 namespace cricket {
28
29 class Connection;
30 class PortAllocator;
31 class PortAllocatorSession;
32 class PortInterface;
33 class RelayPort;
34 class StunPort;
35
36 // Implements a channel that just sends bare packets once we have received the
37 // address of the other side.  We pick a single address to send them based on
38 // a simple investigation of NAT type.
39 class RawTransportChannel : public TransportChannelImpl,
40     public rtc::MessageHandler {
41  public:
42   RawTransportChannel(const std::string& content_name,
43                       int component,
44                       RawTransport* transport,
45                       rtc::Thread *worker_thread,
46                       PortAllocator *allocator);
47   virtual ~RawTransportChannel();
48
49   // Implementation of normal channel packet sending.
50   virtual int SendPacket(const char *data, size_t len,
51                          const rtc::PacketOptions& options, int flags);
52   virtual int SetOption(rtc::Socket::Option opt, int value);
53   virtual int GetError();
54
55   // Implements TransportChannelImpl.
56   virtual Transport* GetTransport() { return raw_transport_; }
57   virtual void SetIceCredentials(const std::string& ice_ufrag,
58                                  const std::string& ice_pwd) {}
59   virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
60                                        const std::string& ice_pwd) {}
61
62   // Creates an allocator session to start figuring out which type of
63   // port we should send to the other client.  This will send
64   // SignalAvailableCandidate once we have decided.
65   virtual void Connect();
66
67   // Resets state back to unconnected.
68   virtual void Reset();
69
70   // We don't actually worry about signaling since we can't send new candidates.
71   virtual void OnSignalingReady() {}
72
73   // Handles a message setting the remote address.  We are writable once we
74   // have this since we now know where to send.
75   virtual void OnCandidate(const Candidate& candidate);
76
77   void OnRemoteAddress(const rtc::SocketAddress& remote_address);
78
79   // Below ICE specific virtual methods not implemented.
80   virtual IceRole GetIceRole() const { return ICEROLE_UNKNOWN; }
81   virtual void SetIceRole(IceRole role) {}
82   virtual void SetIceTiebreaker(uint64 tiebreaker) {}
83
84   virtual bool GetIceProtocolType(IceProtocolType* type) const { return false; }
85   virtual void SetIceProtocolType(IceProtocolType type) {}
86
87   virtual void SetIceUfrag(const std::string& ice_ufrag) {}
88   virtual void SetIcePwd(const std::string& ice_pwd) {}
89   virtual void SetRemoteIceMode(IceMode mode) {}
90   virtual size_t GetConnectionCount() const { return 1; }
91
92   virtual bool GetStats(ConnectionInfos* infos) {
93     return false;
94   }
95
96   // DTLS methods.
97   virtual bool IsDtlsActive() const { return false; }
98
99   // Default implementation.
100   virtual bool GetSslRole(rtc::SSLRole* role) const {
101     return false;
102   }
103
104   virtual bool SetSslRole(rtc::SSLRole role) {
105     return false;
106   }
107
108   // Set up the ciphers to use for DTLS-SRTP.
109   virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
110     return false;
111   }
112
113   // Find out which DTLS-SRTP cipher was negotiated
114   virtual bool GetSrtpCipher(std::string* cipher) {
115     return false;
116   }
117
118   // Returns false because the channel is not DTLS.
119   virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
120     return false;
121   }
122
123   virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
124     return false;
125   }
126
127   // Allows key material to be extracted for external encryption.
128   virtual bool ExportKeyingMaterial(
129       const std::string& label,
130       const uint8* context,
131       size_t context_len,
132       bool use_context,
133       uint8* result,
134       size_t result_len) {
135     return false;
136   }
137
138   virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
139     return false;
140   }
141
142   // Set DTLS Remote fingerprint. Must be after local identity set.
143   virtual bool SetRemoteFingerprint(
144     const std::string& digest_alg,
145     const uint8* digest,
146     size_t digest_len) {
147     return false;
148   }
149
150  private:
151   RawTransport* raw_transport_;
152   rtc::Thread *worker_thread_;
153   PortAllocator* allocator_;
154   PortAllocatorSession* allocator_session_;
155   StunPort* stun_port_;
156   RelayPort* relay_port_;
157   PortInterface* port_;
158   bool use_relay_;
159   rtc::SocketAddress remote_address_;
160
161   // Called when the allocator creates another port.
162   void OnPortReady(PortAllocatorSession* session, PortInterface* port);
163
164   // Called when one of the ports we are using has determined its address.
165   void OnCandidatesReady(PortAllocatorSession *session,
166                          const std::vector<Candidate>& candidates);
167
168   // Called once we have chosen the port to use for communication with the
169   // other client.  This will send its address and prepare the port for use.
170   void SetPort(PortInterface* port);
171
172   // Called once we have a port and a remote address.  This will set mark the
173   // channel as writable and signal the route to the client.
174   void SetWritable();
175
176   // Called when we receive a packet from the other client.
177   void OnReadPacket(PortInterface* port, const char* data, size_t size,
178                     const rtc::SocketAddress& addr);
179
180   // Handles a message to destroy unused ports.
181   virtual void OnMessage(rtc::Message *msg);
182
183   DISALLOW_EVIL_CONSTRUCTORS(RawTransportChannel);
184 };
185
186 }  // namespace cricket
187
188 #endif  // defined(FEATURE_ENABLE_PSTN)
189 #endif  // WEBRTC_P2P_BASE_RAWTRANSPORTCHANNEL_H_