10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2011, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2011, RTFM, Inc.
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <vector>
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
35cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/p2p/base/transportchannelimpl.h"
362a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/buffer.h"
372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/scoped_ptr.h"
382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/sslstreamadapter.h"
392a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/stream.h"
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// A bridge between a packet-oriented/channel-type interface on
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// the bottom and a StreamInterface on the top.
452a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgclass StreamInterfaceChannel : public rtc::StreamInterface,
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                               public sigslot::has_slots<> {
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
482a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  StreamInterfaceChannel(rtc::Thread* owner, TransportChannel* channel)
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      : channel_(channel),
502a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org        state_(rtc::SS_OPEN),
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        fifo_(kFifoSize, owner) {
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    fifo_.SignalEvent.connect(this, &StreamInterfaceChannel::OnEvent);
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Push in a packet; this gets pulled out from Read().
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnPacketReceived(const char* data, size_t size);
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Implementations of StreamInterface
592a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual rtc::StreamState GetState() const { return state_; }
602a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual void Close() { state_ = rtc::SS_CLOSED; }
612a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual rtc::StreamResult Read(void* buffer, size_t buffer_len,
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                       size_t* read, int* error);
632a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual rtc::StreamResult Write(const void* data, size_t data_len,
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                        size_t* written, int* error);
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const size_t kFifoSize = 8192;
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Forward events
702a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual void OnEvent(rtc::StreamInterface* stream, int sig, int err);
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannel* channel_;  // owned by DtlsTransportChannelWrapper
732a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::StreamState state_;
742a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::FifoBuffer fifo_;
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel);
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// packet-based interface, wrapping an existing TransportChannel instance
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// (e.g a P2PTransportChannel)
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Here's the way this works:
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//   DtlsTransportChannelWrapper {
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//       SSLStreamAdapter* dtls_ {
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//           StreamInterfaceChannel downward_ {
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//               TransportChannelImpl* channel_;
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//           }
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//       }
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//   }
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//   - Data which comes into DtlsTransportChannelWrapper from the underlying
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     channel_ via OnReadPacket() is checked for whether it is DTLS
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     or not, and if it is, is passed to DtlsTransportChannelWrapper::
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     HandleDtlsPacket, which pushes it into to downward_.
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     dtls_ is listening for events on downward_, so it immediately calls
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     downward_->Read().
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//   - Data written to DtlsTransportChannelWrapper is passed either to
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//      downward_ or directly to channel_, depending on whether DTLS is
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     negotiated and whether the flags include PF_SRTP_BYPASS
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//   - The SSLStreamAdapter writes to downward_->Write()
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     which translates it into packet writes on channel_.
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass DtlsTransportChannelWrapper : public TransportChannelImpl {
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    enum State {
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      STATE_NONE,      // No state or rejected.
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      STATE_OFFERED,   // Our identity has been set.
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      STATE_ACCEPTED,  // The other side sent a fingerprint.
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      STATE_STARTED,   // We are negotiating.
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      STATE_OPEN,      // Negotiation complete.
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      STATE_CLOSED     // Connection closed.
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    };
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The parameters here are:
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // transport -- the DtlsTransport that created us
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // channel -- the TransportChannel we are wrapping
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DtlsTransportChannelWrapper(Transport* transport,
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              TransportChannelImpl* channel);
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~DtlsTransportChannelWrapper();
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
124a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org  virtual void SetIceRole(IceRole role) {
125a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org    channel_->SetIceRole(role);
126a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org  }
127e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  virtual IceRole GetIceRole() const {
128e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org    return channel_->GetIceRole();
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
130f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  virtual size_t GetConnectionCount() const {
131f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org    return channel_->GetConnectionCount();
132f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  }
1332a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool SetLocalIdentity(rtc::SSLIdentity *identity);
1342a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const;
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool SetRemoteFingerprint(const std::string& digest_alg,
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const uint8* digest,
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    size_t digest_len);
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsDtlsActive() const { return dtls_state_ != STATE_NONE; }
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called to send a packet (via DTLS, if turned on).
142391247d05a663265807c400947ab6eb01ae3d690mallinath@webrtc.org  virtual int SendPacket(const char* data, size_t size,
1432a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                         const rtc::PacketOptions& options,
144391247d05a663265807c400947ab6eb01ae3d690mallinath@webrtc.org                         int flags);
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TransportChannel calls that we forward to the wrapped transport.
1472a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual int SetOption(rtc::Socket::Option opt, int value) {
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return channel_->SetOption(opt, value);
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual int GetError() {
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return channel_->GetError();
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetStats(ConnectionInfos* infos) {
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return channel_->GetStats(infos);
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
156e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  virtual const std::string SessionId() const {
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return channel_->SessionId();
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Set up the ciphers to use for DTLS-SRTP. If this method is not called
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated.
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This method should be called before SetupDtls.
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers);
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Find out which DTLS-SRTP cipher was negotiated
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetSrtpCipher(std::string* cipher);
1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1682a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool GetSslRole(rtc::SSLRole* role) const;
1692a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool SetSslRole(rtc::SSLRole role);
170a487db2aeda23ade81f0b2e5fd4d50f874d06a9csergeyu@chromium.org
17162fe97f10a7a3200c9724851f6a18537ed270cddwu@webrtc.org  // Once DTLS has been established, this method retrieves the certificate in
17262fe97f10a7a3200c9724851f6a18537ed270cddwu@webrtc.org  // use by the remote peer, for use in external identity verification.
1732a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const;
17462fe97f10a7a3200c9724851f6a18537ed270cddwu@webrtc.org
1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Once DTLS has established (i.e., this channel is writable), this method
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // extracts the keys negotiated during the DTLS handshake, for use in external
1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // encryption. DTLS-SRTP uses this to extract the needed SRTP keys.
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // See the SSLStreamAdapter documentation for info on the specific parameters.
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool ExportKeyingMaterial(const std::string& label,
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const uint8* context,
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    size_t context_len,
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    bool use_context,
1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    uint8* result,
1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    size_t result_len) {
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context,
1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                       context_len,
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                       use_context,
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                       result, result_len)
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        : false;
1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TransportChannelImpl calls.
1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual Transport* GetTransport() {
1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return transport_;
1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
196e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  virtual void SetIceTiebreaker(uint64 tiebreaker) {
197e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org    channel_->SetIceTiebreaker(tiebreaker);
1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
199b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  virtual bool GetIceProtocolType(IceProtocolType* type) const {
200b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org    return channel_->GetIceProtocolType(type);
201b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org  }
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetIceProtocolType(IceProtocolType type) {
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_->SetIceProtocolType(type);
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetIceCredentials(const std::string& ice_ufrag,
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 const std::string& ice_pwd) {
2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_->SetIceCredentials(ice_ufrag, ice_pwd);
2080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                       const std::string& ice_pwd) {
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd);
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetRemoteIceMode(IceMode mode) {
2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_->SetRemoteIceMode(mode);
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void Connect();
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void Reset();
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnSignalingReady() {
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_->OnSignalingReady();
2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnCandidate(const Candidate& candidate) {
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    channel_->OnCandidate(candidate);
2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Needed by DtlsTransport.
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannelImpl* channel() { return channel_; }
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnReadableState(TransportChannel* channel);
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnWritableState(TransportChannel* channel);
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnReadPacket(TransportChannel* channel, const char* data, size_t size,
2342a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                    const rtc::PacketTime& packet_time, int flags);
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnReadyToSend(TransportChannel* channel);
2362a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err);
2370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetupDtls();
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool MaybeStartDtls();
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool HandleDtlsPacket(const char* data, size_t size);
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnRequestSignaling(TransportChannelImpl* channel);
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnCandidateReady(TransportChannelImpl* channel, const Candidate& c);
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnCandidatesAllocationDone(TransportChannelImpl* channel);
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnRoleConflict(TransportChannelImpl* channel);
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnRouteChange(TransportChannel* channel, const Candidate& candidate);
245f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  void OnConnectionRemoved(TransportChannelImpl* channel);
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Transport* transport_;  // The transport_ that created us.
2482a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* worker_thread_;  // Everything should occur on this thread.
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannelImpl* channel_;  // Underlying channel, owned by transport_.
2502a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<rtc::SSLStreamAdapter> dtls_;  // The DTLS stream
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StreamInterfaceChannel* downward_;  // Wrapper for channel_, owned by dtls_.
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::vector<std::string> srtp_ciphers_;  // SRTP ciphers to use with DTLS.
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  State dtls_state_;
2542a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::SSLIdentity* local_identity_;
2552a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::SSLRole ssl_role_;
2562a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Buffer remote_fingerprint_value_;
2570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string remote_fingerprint_algorithm_;
2580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper);
2600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
2610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
2630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
265