Home | History | Annotate | Download | only in base
      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/p2p/base/candidate.h"
     34 #include "talk/p2p/base/rawtransport.h"
     35 #include "talk/p2p/base/transportchannelimpl.h"
     36 #include "webrtc/base/messagequeue.h"
     37 
     38 #if defined(FEATURE_ENABLE_PSTN)
     39 
     40 namespace rtc {
     41 class Thread;
     42 }
     43 
     44 namespace cricket {
     45 
     46 class Connection;
     47 class PortAllocator;
     48 class PortAllocatorSession;
     49 class PortInterface;
     50 class RelayPort;
     51 class 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.
     56 class RawTransportChannel : public TransportChannelImpl,
     57     public rtc::MessageHandler {
     58  public:
     59   RawTransportChannel(const std::string& content_name,
     60                       int component,
     61                       RawTransport* transport,
     62                       rtc::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                          const rtc::PacketOptions& options, int flags);
     69   virtual int SetOption(rtc::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 rtc::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 
    101   virtual bool GetIceProtocolType(IceProtocolType* type) const { return false; }
    102   virtual void SetIceProtocolType(IceProtocolType type) {}
    103 
    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; }
    108 
    109   virtual bool GetStats(ConnectionInfos* infos) {
    110     return false;
    111   }
    112 
    113   // DTLS methods.
    114   virtual bool IsDtlsActive() const { return false; }
    115 
    116   // Default implementation.
    117   virtual bool GetSslRole(rtc::SSLRole* role) const {
    118     return false;
    119   }
    120 
    121   virtual bool SetSslRole(rtc::SSLRole role) {
    122     return false;
    123   }
    124 
    125   // Set up the ciphers to use for DTLS-SRTP.
    126   virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
    127     return false;
    128   }
    129 
    130   // Find out which DTLS-SRTP cipher was negotiated
    131   virtual bool GetSrtpCipher(std::string* cipher) {
    132     return false;
    133   }
    134 
    135   // Returns false because the channel is not DTLS.
    136   virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
    137     return false;
    138   }
    139 
    140   virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const {
    141     return false;
    142   }
    143 
    144   // Allows key material to be extracted for external encryption.
    145   virtual bool ExportKeyingMaterial(
    146       const std::string& label,
    147       const uint8* context,
    148       size_t context_len,
    149       bool use_context,
    150       uint8* result,
    151       size_t result_len) {
    152     return false;
    153   }
    154 
    155   virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) {
    156     return false;
    157   }
    158 
    159   // Set DTLS Remote fingerprint. Must be after local identity set.
    160   virtual bool SetRemoteFingerprint(
    161     const std::string& digest_alg,
    162     const uint8* digest,
    163     size_t digest_len) {
    164     return false;
    165   }
    166 
    167  private:
    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_;
    175   bool use_relay_;
    176   rtc::SocketAddress remote_address_;
    177 
    178   // Called when the allocator creates another port.
    179   void OnPortReady(PortAllocatorSession* session, PortInterface* port);
    180 
    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);
    184 
    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);
    188 
    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.
    191   void SetWritable();
    192 
    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);
    196 
    197   // Handles a message to destroy unused ports.
    198   virtual void OnMessage(rtc::Message *msg);
    199 
    200   DISALLOW_EVIL_CONSTRUCTORS(RawTransportChannel);
    201 };
    202 
    203 }  // namespace cricket
    204 
    205 #endif  // defined(FEATURE_ENABLE_PSTN)
    206 #endif  // TALK_P2P_BASE_RAWTRANSPORTCHANNEL_H_
    207