Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 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 PC_JSEPTRANSPORT_H_ |
| 12 | #define PC_JSEPTRANSPORT_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "api/candidate.h" |
| 20 | #include "api/jsep.h" |
| 21 | #include "api/optional.h" |
| 22 | #include "p2p/base/dtlstransport.h" |
| 23 | #include "p2p/base/p2pconstants.h" |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 24 | #include "p2p/base/transportinfo.h" |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 25 | #include "pc/sessiondescription.h" |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 26 | #include "rtc_base/constructormagic.h" |
| 27 | #include "rtc_base/messagequeue.h" |
| 28 | #include "rtc_base/rtccertificate.h" |
| 29 | #include "rtc_base/sigslot.h" |
| 30 | #include "rtc_base/sslstreamadapter.h" |
| 31 | |
| 32 | namespace cricket { |
| 33 | |
| 34 | class DtlsTransportInternal; |
| 35 | |
| 36 | struct TransportChannelStats { |
| 37 | TransportChannelStats(); |
| 38 | TransportChannelStats(const TransportChannelStats&); |
| 39 | ~TransportChannelStats(); |
| 40 | |
| 41 | int component = 0; |
| 42 | ConnectionInfos connection_infos; |
| 43 | int srtp_crypto_suite = rtc::SRTP_INVALID_CRYPTO_SUITE; |
| 44 | int ssl_cipher_suite = rtc::TLS_NULL_WITH_NULL_NULL; |
| 45 | DtlsTransportState dtls_state = DTLS_TRANSPORT_NEW; |
| 46 | }; |
| 47 | |
| 48 | // Information about all the channels of a transport. |
| 49 | // TODO(hta): Consider if a simple vector is as good as a map. |
| 50 | typedef std::vector<TransportChannelStats> TransportChannelStatsList; |
| 51 | |
| 52 | // Information about the stats of a transport. |
| 53 | struct TransportStats { |
| 54 | TransportStats(); |
| 55 | ~TransportStats(); |
| 56 | |
| 57 | std::string transport_name; |
| 58 | TransportChannelStatsList channel_stats; |
| 59 | }; |
| 60 | |
| 61 | bool BadTransportDescription(const std::string& desc, std::string* err_desc); |
| 62 | |
| 63 | // Helper class used by TransportController that processes |
| 64 | // TransportDescriptions. A TransportDescription represents the |
| 65 | // transport-specific properties of an SDP m= section, processed according to |
| 66 | // JSEP. Each transport consists of DTLS and ICE transport channels for RTP |
| 67 | // (and possibly RTCP, if rtcp-mux isn't used). |
| 68 | // |
| 69 | // On Threading: Transport performs work solely on the network thread, and so |
| 70 | // its methods should only be called on the network thread. |
| 71 | class JsepTransport : public sigslot::has_slots<> { |
| 72 | public: |
| 73 | // |mid| is just used for log statements in order to identify the Transport. |
| 74 | // Note that |certificate| is allowed to be null since a remote description |
| 75 | // may be set before a local certificate is generated. |
| 76 | JsepTransport(const std::string& mid, |
| 77 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
| 78 | ~JsepTransport() override; |
| 79 | |
| 80 | // Returns the MID of this transport. |
| 81 | const std::string& mid() const { return mid_; } |
| 82 | |
| 83 | // Add or remove channel that is affected when a local/remote transport |
| 84 | // description is set on this transport. Need to add all channels before |
| 85 | // setting a transport description. |
| 86 | bool AddChannel(DtlsTransportInternal* dtls, int component); |
| 87 | bool RemoveChannel(int component); |
| 88 | bool HasChannels() const; |
| 89 | |
| 90 | bool ready_for_remote_candidates() const { |
| 91 | return local_description_set_ && remote_description_set_; |
| 92 | } |
| 93 | |
| 94 | // Must be called before applying local session description. |
| 95 | // Needed in order to verify the local fingerprint. |
| 96 | void SetLocalCertificate( |
| 97 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
| 98 | |
| 99 | // Get a copy of the local certificate provided by SetLocalCertificate. |
| 100 | bool GetLocalCertificate( |
| 101 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const; |
| 102 | |
| 103 | // Set the local TransportDescription to be used by DTLS and ICE channels |
| 104 | // that are part of this Transport. |
| 105 | bool SetLocalTransportDescription(const TransportDescription& description, |
| 106 | webrtc::SdpType type, |
| 107 | std::string* error_desc); |
| 108 | |
| 109 | // Set the remote TransportDescription to be used by DTLS and ICE channels |
| 110 | // that are part of this Transport. |
| 111 | bool SetRemoteTransportDescription(const TransportDescription& description, |
| 112 | webrtc::SdpType type, |
| 113 | std::string* error_desc); |
| 114 | |
| 115 | // Set the "needs-ice-restart" flag as described in JSEP. After the flag is |
| 116 | // set, offers should generate new ufrags/passwords until an ICE restart |
| 117 | // occurs. |
| 118 | // |
| 119 | // This and the below method can be called safely from any thread as long as |
| 120 | // SetXTransportDescription is not in progress. |
| 121 | void SetNeedsIceRestartFlag(); |
| 122 | // Returns true if the ICE restart flag above was set, and no ICE restart has |
| 123 | // occurred yet for this transport (by applying a local description with |
| 124 | // changed ufrag/password). |
| 125 | bool NeedsIceRestart() const; |
| 126 | |
| 127 | // Returns role if negotiated, or empty Optional if it hasn't been negotiated |
| 128 | // yet. |
| 129 | rtc::Optional<rtc::SSLRole> GetSslRole() const; |
| 130 | |
| 131 | // TODO(deadbeef): Make this const. See comment in transportcontroller.h. |
| 132 | bool GetStats(TransportStats* stats); |
| 133 | |
| 134 | // The current local transport description, possibly used |
| 135 | // by the transport controller. |
| 136 | const TransportDescription* local_description() const { |
| 137 | return local_description_.get(); |
| 138 | } |
| 139 | |
| 140 | // The current remote transport description, possibly used |
| 141 | // by the transport controller. |
| 142 | const TransportDescription* remote_description() const { |
| 143 | return remote_description_.get(); |
| 144 | } |
| 145 | |
| 146 | // TODO(deadbeef): The methods below are only public for testing. Should make |
| 147 | // them utility functions or objects so they can be tested independently from |
| 148 | // this class. |
| 149 | |
| 150 | // Returns false if the certificate's identity does not match the fingerprint, |
| 151 | // or either is NULL. |
| 152 | bool VerifyCertificateFingerprint(const rtc::RTCCertificate* certificate, |
| 153 | const rtc::SSLFingerprint* fingerprint, |
| 154 | std::string* error_desc) const; |
| 155 | |
| 156 | private: |
| 157 | // Negotiates the transport parameters based on the current local and remote |
| 158 | // transport description, such as the ICE role to use, and whether DTLS |
| 159 | // should be activated. |
| 160 | // |
| 161 | // Called when an answer TransportDescription is applied. |
| 162 | bool NegotiateTransportDescription(webrtc::SdpType local_description_type, |
| 163 | std::string* error_desc); |
| 164 | |
| 165 | // Negotiates the SSL role based off the offer and answer as specified by |
| 166 | // RFC 4145, section-4.1. Returns false if the SSL role cannot be determined |
| 167 | // from the local description and remote description. |
| 168 | bool NegotiateRole(webrtc::SdpType local_description_type, |
| 169 | std::string* error_desc); |
| 170 | |
| 171 | // Pushes down the transport parameters from the local description, such |
| 172 | // as the ICE ufrag and pwd. |
| 173 | bool ApplyLocalTransportDescription(DtlsTransportInternal* dtls_transport, |
| 174 | std::string* error_desc); |
| 175 | |
| 176 | // Pushes down the transport parameters from the remote description to the |
| 177 | // transport channel. |
| 178 | bool ApplyRemoteTransportDescription(DtlsTransportInternal* dtls_transport, |
| 179 | std::string* error_desc); |
| 180 | |
| 181 | // Pushes down the transport parameters obtained via negotiation. |
| 182 | bool ApplyNegotiatedTransportDescription( |
| 183 | DtlsTransportInternal* dtls_transport, |
| 184 | std::string* error_desc); |
| 185 | |
| 186 | const std::string mid_; |
| 187 | // needs-ice-restart bit as described in JSEP. |
| 188 | bool needs_ice_restart_ = false; |
| 189 | rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
| 190 | rtc::Optional<rtc::SSLRole> ssl_role_; |
| 191 | std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint_; |
| 192 | std::unique_ptr<TransportDescription> local_description_; |
| 193 | std::unique_ptr<TransportDescription> remote_description_; |
| 194 | bool local_description_set_ = false; |
| 195 | bool remote_description_set_ = false; |
| 196 | |
| 197 | // Candidate component => DTLS channel |
| 198 | std::map<int, DtlsTransportInternal*> channels_; |
| 199 | |
| 200 | RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport); |
| 201 | }; |
| 202 | |
| 203 | } // namespace cricket |
| 204 | |
| 205 | #endif // PC_JSEPTRANSPORT_H_ |