blob: 871f18af5c0b5da6c822b4a862da559ae3005d59 [file] [log] [blame]
ossu7bb87ee2017-01-23 04:56:25 -08001/*
Taylor Brandstetter3a034e12020-07-09 15:32:34 -07002 * Copyright 2020 The WebRTC project authors. All Rights Reserved.
ossu7bb87ee2017-01-23 04:56:25 -08003 *
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
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070011#ifndef PC_SCTP_DATA_CHANNEL_H_
12#define PC_SCTP_DATA_CHANNEL_H_
ossu7bb87ee2017-01-23 04:56:25 -080013
Steve Anton944c7552018-12-13 14:19:10 -080014#include <memory>
ossu7bb87ee2017-01-23 04:56:25 -080015#include <set>
16#include <string>
17
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/data_channel_interface.h"
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020019#include "api/priority.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010020#include "api/scoped_refptr.h"
Niels Möller2a707032020-06-16 16:39:13 +020021#include "api/transport/data_channel_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "media/base/media_channel.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070023#include "pc/data_channel_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/async_invoker.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070025#include "rtc_base/ssl_stream_adapter.h" // For SSLRole
Artem Titove41c4332018-07-25 15:04:28 +020026#include "rtc_base/third_party/sigslot/sigslot.h"
ossu7bb87ee2017-01-23 04:56:25 -080027
28namespace webrtc {
29
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070030class SctpDataChannel;
ossu7bb87ee2017-01-23 04:56:25 -080031
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070032// TODO(deadbeef): Get rid of this and have SctpDataChannel depend on
33// SctpTransportInternal (pure virtual SctpTransport interface) instead.
34class SctpDataChannelProviderInterface {
ossu7bb87ee2017-01-23 04:56:25 -080035 public:
36 // Sends the data to the transport.
37 virtual bool SendData(const cricket::SendDataParams& params,
38 const rtc::CopyOnWriteBuffer& payload,
39 cricket::SendDataResult* result) = 0;
40 // Connects to the transport signals.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070041 virtual bool ConnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080042 // Disconnects from the transport signals.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070043 virtual void DisconnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080044 // Adds the data channel SID to the transport for SCTP.
45 virtual void AddSctpDataStream(int sid) = 0;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -070046 // Begins the closing procedure by sending an outgoing stream reset. Still
47 // need to wait for callbacks to tell when this completes.
ossu7bb87ee2017-01-23 04:56:25 -080048 virtual void RemoveSctpDataStream(int sid) = 0;
49 // Returns true if the transport channel is ready to send data.
50 virtual bool ReadyToSendData() const = 0;
51
52 protected:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070053 virtual ~SctpDataChannelProviderInterface() {}
ossu7bb87ee2017-01-23 04:56:25 -080054};
55
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +020056// TODO(tommi): Change to not inherit from DataChannelInit but to have it as
57// a const member. Block access to the 'id' member since it cannot be const.
ossu7bb87ee2017-01-23 04:56:25 -080058struct InternalDataChannelInit : public DataChannelInit {
Yves Gerey665174f2018-06-19 15:03:05 +020059 enum OpenHandshakeRole { kOpener, kAcker, kNone };
ossu7bb87ee2017-01-23 04:56:25 -080060 // The default role is kOpener because the default |negotiated| is false.
61 InternalDataChannelInit() : open_handshake_role(kOpener) {}
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020062 explicit InternalDataChannelInit(const DataChannelInit& base);
ossu7bb87ee2017-01-23 04:56:25 -080063 OpenHandshakeRole open_handshake_role;
64};
65
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070066// Helper class to allocate unique IDs for SCTP DataChannels.
ossu7bb87ee2017-01-23 04:56:25 -080067class SctpSidAllocator {
68 public:
69 // Gets the first unused odd/even id based on the DTLS role. If |role| is
70 // SSL_CLIENT, the allocated id starts from 0 and takes even numbers;
71 // otherwise, the id starts from 1 and takes odd numbers.
Taylor Brandstettercdd05f02018-05-31 13:23:32 -070072 // Returns false if no ID can be allocated.
ossu7bb87ee2017-01-23 04:56:25 -080073 bool AllocateSid(rtc::SSLRole role, int* sid);
74
75 // Attempts to reserve a specific sid. Returns false if it's unavailable.
76 bool ReserveSid(int sid);
77
78 // Indicates that |sid| isn't in use any more, and is thus available again.
79 void ReleaseSid(int sid);
80
81 private:
82 // Checks if |sid| is available to be assigned to a new SCTP data channel.
83 bool IsSidAvailable(int sid) const;
84
85 std::set<int> used_sids_;
86};
87
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070088// SctpDataChannel is an implementation of the DataChannelInterface based on
89// SctpTransport. It provides an implementation of unreliable or
90// reliabledata channels.
ossu7bb87ee2017-01-23 04:56:25 -080091
92// DataChannel states:
93// kConnecting: The channel has been created the transport might not yet be
94// ready.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070095// kOpen: The open handshake has been performed (if relevant) and the data
96// channel is able to send messages.
97// kClosing: DataChannelInterface::Close has been called, or the remote side
98// initiated the closing procedure, but the closing procedure has not
99// yet finished.
100// kClosed: The closing handshake is finished (possibly initiated from this,
101// side, possibly from the peer).
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700102//
103// How the closing procedure works for SCTP:
104// 1. Alice calls Close(), state changes to kClosing.
105// 2. Alice finishes sending any queued data.
106// 3. Alice calls RemoveSctpDataStream, sends outgoing stream reset.
107// 4. Bob receives incoming stream reset; OnClosingProcedureStartedRemotely
108// called.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700109// 5. Bob sends outgoing stream reset.
110// 6. Alice receives incoming reset, Bob receives acknowledgement. Both receive
111// OnClosingProcedureComplete callback and transition to kClosed.
112class SctpDataChannel : public DataChannelInterface,
113 public sigslot::has_slots<> {
ossu7bb87ee2017-01-23 04:56:25 -0800114 public:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700115 static rtc::scoped_refptr<SctpDataChannel> Create(
116 SctpDataChannelProviderInterface* provider,
ossu7bb87ee2017-01-23 04:56:25 -0800117 const std::string& label,
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200118 const InternalDataChannelInit& config,
119 rtc::Thread* signaling_thread,
120 rtc::Thread* network_thread);
ossu7bb87ee2017-01-23 04:56:25 -0800121
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700122 // Instantiates an API proxy for a SctpDataChannel instance that will be
123 // handed out to external callers.
Tomas Gunnarsson6476d0b2020-06-16 18:39:50 +0200124 static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700125 rtc::scoped_refptr<SctpDataChannel> channel);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800126
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200127 void RegisterObserver(DataChannelObserver* observer) override;
128 void UnregisterObserver() override;
ossu7bb87ee2017-01-23 04:56:25 -0800129
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200130 std::string label() const override { return label_; }
131 bool reliable() const override;
132 bool ordered() const override { return config_.ordered; }
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200133 // Backwards compatible accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200134 uint16_t maxRetransmitTime() const override {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200135 return config_.maxRetransmitTime ? *config_.maxRetransmitTime
136 : static_cast<uint16_t>(-1);
137 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200138 uint16_t maxRetransmits() const override {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200139 return config_.maxRetransmits ? *config_.maxRetransmits
140 : static_cast<uint16_t>(-1);
141 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200142 absl::optional<int> maxPacketLifeTime() const override {
ossu7bb87ee2017-01-23 04:56:25 -0800143 return config_.maxRetransmitTime;
144 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200145 absl::optional<int> maxRetransmitsOpt() const override {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200146 return config_.maxRetransmits;
147 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200148 std::string protocol() const override { return config_.protocol; }
149 bool negotiated() const override { return config_.negotiated; }
150 int id() const override { return config_.id; }
151 Priority priority() const override {
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +0200152 return config_.priority ? *config_.priority : Priority::kLow;
153 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200154
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400155 virtual int internal_id() const { return internal_id_; }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200156
157 uint64_t buffered_amount() const override;
158 void Close() override;
159 DataState state() const override;
160 RTCError error() const override;
161 uint32_t messages_sent() const override;
162 uint64_t bytes_sent() const override;
163 uint32_t messages_received() const override;
164 uint64_t bytes_received() const override;
165 bool Send(const DataBuffer& buffer) override;
ossu7bb87ee2017-01-23 04:56:25 -0800166
Harald Alvestrand1f928d32019-03-28 11:29:38 +0100167 // Close immediately, ignoring any queued data or closing procedure.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700168 // This is called when the underlying SctpTransport is being destroyed.
Harald Alvestrand1f928d32019-03-28 11:29:38 +0100169 // It is also called by the PeerConnection if SCTP ID assignment fails.
Harald Alvestranddfbfb462019-12-08 05:55:43 +0100170 void CloseAbruptlyWithError(RTCError error);
171 // Specializations of CloseAbruptlyWithError
172 void CloseAbruptlyWithDataChannelFailure(const std::string& message);
173 void CloseAbruptlyWithSctpCauseCode(const std::string& message,
174 uint16_t cause_code);
Harald Alvestrand1f928d32019-03-28 11:29:38 +0100175
ossu7bb87ee2017-01-23 04:56:25 -0800176 // Slots for provider to connect signals to.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700177 //
178 // TODO(deadbeef): Make these private once we're hooking up signals ourselves,
179 // instead of relying on SctpDataChannelProviderInterface.
180
181 // Called when the SctpTransport's ready to use. That can happen when we've
182 // finished negotiation, or if the channel was created after negotiation has
183 // already finished.
184 void OnTransportReady(bool writable);
185
ossu7bb87ee2017-01-23 04:56:25 -0800186 void OnDataReceived(const cricket::ReceiveDataParams& params,
187 const rtc::CopyOnWriteBuffer& payload);
ossu7bb87ee2017-01-23 04:56:25 -0800188
ossu7bb87ee2017-01-23 04:56:25 -0800189 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
190 // be called once.
191 void SetSctpSid(int sid);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700192 // The remote side started the closing procedure by resetting its outgoing
193 // stream (our incoming stream). Sets state to kClosing.
194 void OnClosingProcedureStartedRemotely(int sid);
195 // The closing procedure is complete; both incoming and outgoing stream
196 // resets are done and the channel can transition to kClosed. Called
197 // asynchronously after RemoveSctpDataStream.
198 void OnClosingProcedureComplete(int sid);
ossu7bb87ee2017-01-23 04:56:25 -0800199 // Called when the transport channel is created.
200 // Only needs to be called for SCTP data channels.
201 void OnTransportChannelCreated();
Harald Alvestrand408cb4b2019-11-16 12:09:08 +0100202 // Called when the transport channel is unusable.
ossu7bb87ee2017-01-23 04:56:25 -0800203 // This method makes sure the DataChannel is disconnected and changes state
204 // to kClosed.
Harald Alvestrand408cb4b2019-11-16 12:09:08 +0100205 void OnTransportChannelClosed();
ossu7bb87ee2017-01-23 04:56:25 -0800206
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700207 DataChannelStats GetStats() const;
ossu7bb87ee2017-01-23 04:56:25 -0800208
209 // Emitted when state transitions to kOpen.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700210 sigslot::signal1<DataChannelInterface*> SignalOpened;
ossu7bb87ee2017-01-23 04:56:25 -0800211 // Emitted when state transitions to kClosed.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700212 // This signal can be used to tell when the channel's sid is free.
213 sigslot::signal1<DataChannelInterface*> SignalClosed;
ossu7bb87ee2017-01-23 04:56:25 -0800214
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400215 // Reset the allocator for internal ID values for testing, so that
216 // the internal IDs generated are predictable. Test only.
217 static void ResetInternalIdAllocatorForTesting(int new_value);
218
ossu7bb87ee2017-01-23 04:56:25 -0800219 protected:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700220 SctpDataChannel(const InternalDataChannelInit& config,
221 SctpDataChannelProviderInterface* client,
222 const std::string& label,
223 rtc::Thread* signaling_thread,
224 rtc::Thread* network_thread);
225 ~SctpDataChannel() override;
ossu7bb87ee2017-01-23 04:56:25 -0800226
227 private:
ossu7bb87ee2017-01-23 04:56:25 -0800228 // The OPEN(_ACK) signaling state.
229 enum HandshakeState {
230 kHandshakeInit,
231 kHandshakeShouldSendOpen,
232 kHandshakeShouldSendAck,
233 kHandshakeWaitingForAck,
234 kHandshakeReady
235 };
236
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +0200237 bool Init();
ossu7bb87ee2017-01-23 04:56:25 -0800238 void UpdateState();
239 void SetState(DataState state);
240 void DisconnectFromProvider();
241
242 void DeliverQueuedReceivedData();
243
244 void SendQueuedDataMessages();
245 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
246 bool QueueSendDataMessage(const DataBuffer& buffer);
247
248 void SendQueuedControlMessages();
249 void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer);
250 bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer);
251
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200252 rtc::Thread* const signaling_thread_;
253 rtc::Thread* const network_thread_;
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400254 const int internal_id_;
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +0200255 const std::string label_;
256 const InternalDataChannelInit config_;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700257 DataChannelObserver* observer_ RTC_GUARDED_BY(signaling_thread_) = nullptr;
258 DataState state_ RTC_GUARDED_BY(signaling_thread_) = kConnecting;
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200259 RTCError error_ RTC_GUARDED_BY(signaling_thread_);
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700260 uint32_t messages_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
261 uint64_t bytes_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
262 uint32_t messages_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
263 uint64_t bytes_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
Marina Cioceae448a3f2019-03-04 15:52:21 +0100264 // Number of bytes of data that have been queued using Send(). Increased
265 // before each transport send and decreased after each successful send.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700266 uint64_t buffered_amount_ RTC_GUARDED_BY(signaling_thread_) = 0;
267 SctpDataChannelProviderInterface* const provider_
268 RTC_GUARDED_BY(signaling_thread_);
269 HandshakeState handshake_state_ RTC_GUARDED_BY(signaling_thread_) =
270 kHandshakeInit;
271 bool connected_to_provider_ RTC_GUARDED_BY(signaling_thread_) = false;
272 bool writable_ RTC_GUARDED_BY(signaling_thread_) = false;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700273 // Did we already start the graceful SCTP closing procedure?
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200274 bool started_closing_procedure_ RTC_GUARDED_BY(signaling_thread_) = false;
ossu7bb87ee2017-01-23 04:56:25 -0800275 // Control messages that always have to get sent out before any queued
276 // data.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200277 PacketQueue queued_control_data_ RTC_GUARDED_BY(signaling_thread_);
278 PacketQueue queued_received_data_ RTC_GUARDED_BY(signaling_thread_);
279 PacketQueue queued_send_data_ RTC_GUARDED_BY(signaling_thread_);
280 rtc::AsyncInvoker invoker_ RTC_GUARDED_BY(signaling_thread_);
ossu7bb87ee2017-01-23 04:56:25 -0800281};
282
ossu7bb87ee2017-01-23 04:56:25 -0800283} // namespace webrtc
284
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700285#endif // PC_SCTP_DATA_CHANNEL_H_