blob: a8442c59cc4d0a7d9ec2a2106dda536fef4dc7b1 [file] [log] [blame]
ossu7bb87ee2017-01-23 12:56:251/*
Taylor Brandstetter3a034e12020-07-09 22:32:342 * Copyright 2020 The WebRTC project authors. All Rights Reserved.
ossu7bb87ee2017-01-23 12:56:253 *
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 22:32:3411#ifndef PC_SCTP_DATA_CHANNEL_H_
12#define PC_SCTP_DATA_CHANNEL_H_
ossu7bb87ee2017-01-23 12:56:2513
Harald Alvestrand5761e7b2021-01-29 14:45:0814#include <stdint.h>
15
Steve Anton944c7552018-12-13 22:19:1016#include <memory>
ossu7bb87ee2017-01-23 12:56:2517#include <set>
18#include <string>
19
Harald Alvestrand5761e7b2021-01-29 14:45:0820#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0021#include "api/data_channel_interface.h"
Harald Alvestrandfd5ae7f2020-05-16 06:37:4922#include "api/priority.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 19:26:4824#include "api/scoped_refptr.h"
Niels Möller2a707032020-06-16 14:39:1325#include "api/transport/data_channel_transport_interface.h"
Steve Anton10542f22019-01-11 17:11:0026#include "media/base/media_channel.h"
Taylor Brandstetter3a034e12020-07-09 22:32:3427#include "pc/data_channel_utils.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0828#include "rtc_base/copy_on_write_buffer.h"
Taylor Brandstetter3a034e12020-07-09 22:32:3429#include "rtc_base/ssl_stream_adapter.h" // For SSLRole
Artem Titove41c4332018-07-25 13:04:2830#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0831#include "rtc_base/thread.h"
32#include "rtc_base/thread_annotations.h"
ossu7bb87ee2017-01-23 12:56:2533
34namespace webrtc {
35
Taylor Brandstetter3a034e12020-07-09 22:32:3436class SctpDataChannel;
ossu7bb87ee2017-01-23 12:56:2537
Taylor Brandstetter3a034e12020-07-09 22:32:3438// TODO(deadbeef): Get rid of this and have SctpDataChannel depend on
39// SctpTransportInternal (pure virtual SctpTransport interface) instead.
Harald Alvestrand9e5aeb92022-05-11 09:35:3640class SctpDataChannelControllerInterface {
ossu7bb87ee2017-01-23 12:56:2541 public:
42 // Sends the data to the transport.
Florent Castellid95b1492021-05-10 09:29:5643 virtual bool SendData(int sid,
44 const SendDataParams& params,
ossu7bb87ee2017-01-23 12:56:2545 const rtc::CopyOnWriteBuffer& payload,
46 cricket::SendDataResult* result) = 0;
47 // Connects to the transport signals.
Taylor Brandstetter3a034e12020-07-09 22:32:3448 virtual bool ConnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 12:56:2549 // Disconnects from the transport signals.
Taylor Brandstetter3a034e12020-07-09 22:32:3450 virtual void DisconnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 12:56:2551 // Adds the data channel SID to the transport for SCTP.
52 virtual void AddSctpDataStream(int sid) = 0;
Taylor Brandstettercdd05f02018-05-31 20:23:3253 // Begins the closing procedure by sending an outgoing stream reset. Still
54 // need to wait for callbacks to tell when this completes.
ossu7bb87ee2017-01-23 12:56:2555 virtual void RemoveSctpDataStream(int sid) = 0;
56 // Returns true if the transport channel is ready to send data.
57 virtual bool ReadyToSendData() const = 0;
58
59 protected:
Harald Alvestrand9e5aeb92022-05-11 09:35:3660 virtual ~SctpDataChannelControllerInterface() {}
ossu7bb87ee2017-01-23 12:56:2561};
62
Tomas Gunnarsson0ca13d92020-06-10 10:17:5063// TODO(tommi): Change to not inherit from DataChannelInit but to have it as
64// a const member. Block access to the 'id' member since it cannot be const.
ossu7bb87ee2017-01-23 12:56:2565struct InternalDataChannelInit : public DataChannelInit {
Yves Gerey665174f2018-06-19 13:03:0566 enum OpenHandshakeRole { kOpener, kAcker, kNone };
Artem Titov880fa812021-07-30 20:30:2367 // The default role is kOpener because the default `negotiated` is false.
ossu7bb87ee2017-01-23 12:56:2568 InternalDataChannelInit() : open_handshake_role(kOpener) {}
Harald Alvestrandf3736ed2019-04-08 11:09:3069 explicit InternalDataChannelInit(const DataChannelInit& base);
ossu7bb87ee2017-01-23 12:56:2570 OpenHandshakeRole open_handshake_role;
71};
72
Taylor Brandstetter3a034e12020-07-09 22:32:3473// Helper class to allocate unique IDs for SCTP DataChannels.
ossu7bb87ee2017-01-23 12:56:2574class SctpSidAllocator {
75 public:
Artem Titov880fa812021-07-30 20:30:2376 // Gets the first unused odd/even id based on the DTLS role. If `role` is
ossu7bb87ee2017-01-23 12:56:2577 // SSL_CLIENT, the allocated id starts from 0 and takes even numbers;
78 // otherwise, the id starts from 1 and takes odd numbers.
Taylor Brandstettercdd05f02018-05-31 20:23:3279 // Returns false if no ID can be allocated.
ossu7bb87ee2017-01-23 12:56:2580 bool AllocateSid(rtc::SSLRole role, int* sid);
81
82 // Attempts to reserve a specific sid. Returns false if it's unavailable.
83 bool ReserveSid(int sid);
84
Artem Titov880fa812021-07-30 20:30:2385 // Indicates that `sid` isn't in use any more, and is thus available again.
ossu7bb87ee2017-01-23 12:56:2586 void ReleaseSid(int sid);
87
88 private:
Artem Titov880fa812021-07-30 20:30:2389 // Checks if `sid` is available to be assigned to a new SCTP data channel.
ossu7bb87ee2017-01-23 12:56:2590 bool IsSidAvailable(int sid) const;
91
92 std::set<int> used_sids_;
93};
94
Taylor Brandstetter3a034e12020-07-09 22:32:3495// SctpDataChannel is an implementation of the DataChannelInterface based on
96// SctpTransport. It provides an implementation of unreliable or
97// reliabledata channels.
ossu7bb87ee2017-01-23 12:56:2598
99// DataChannel states:
100// kConnecting: The channel has been created the transport might not yet be
101// ready.
Taylor Brandstetter3a034e12020-07-09 22:32:34102// kOpen: The open handshake has been performed (if relevant) and the data
103// channel is able to send messages.
104// kClosing: DataChannelInterface::Close has been called, or the remote side
105// initiated the closing procedure, but the closing procedure has not
106// yet finished.
107// kClosed: The closing handshake is finished (possibly initiated from this,
108// side, possibly from the peer).
Taylor Brandstettercdd05f02018-05-31 20:23:32109//
110// How the closing procedure works for SCTP:
111// 1. Alice calls Close(), state changes to kClosing.
112// 2. Alice finishes sending any queued data.
113// 3. Alice calls RemoveSctpDataStream, sends outgoing stream reset.
114// 4. Bob receives incoming stream reset; OnClosingProcedureStartedRemotely
115// called.
Taylor Brandstetter3a034e12020-07-09 22:32:34116// 5. Bob sends outgoing stream reset.
117// 6. Alice receives incoming reset, Bob receives acknowledgement. Both receive
118// OnClosingProcedureComplete callback and transition to kClosed.
119class SctpDataChannel : public DataChannelInterface,
120 public sigslot::has_slots<> {
ossu7bb87ee2017-01-23 12:56:25121 public:
Taylor Brandstetter3a034e12020-07-09 22:32:34122 static rtc::scoped_refptr<SctpDataChannel> Create(
Harald Alvestrand9e5aeb92022-05-11 09:35:36123 SctpDataChannelControllerInterface* controller,
ossu7bb87ee2017-01-23 12:56:25124 const std::string& label,
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42125 const InternalDataChannelInit& config,
126 rtc::Thread* signaling_thread,
127 rtc::Thread* network_thread);
ossu7bb87ee2017-01-23 12:56:25128
Taylor Brandstetter3a034e12020-07-09 22:32:34129 // Instantiates an API proxy for a SctpDataChannel instance that will be
130 // handed out to external callers.
Tomas Gunnarsson6476d0b2020-06-16 16:39:50131 static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
Taylor Brandstetter3a034e12020-07-09 22:32:34132 rtc::scoped_refptr<SctpDataChannel> channel);
Bjorn Mellem175aa2e2018-11-08 19:23:22133
Harald Alvestrand9e5aeb92022-05-11 09:35:36134 // Invalidate the link to the controller (DataChannelController);
135 void DetachFromController();
136
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42137 void RegisterObserver(DataChannelObserver* observer) override;
138 void UnregisterObserver() override;
ossu7bb87ee2017-01-23 12:56:25139
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42140 std::string label() const override { return label_; }
141 bool reliable() const override;
142 bool ordered() const override { return config_.ordered; }
Harald Alvestrandf3736ed2019-04-08 11:09:30143 // Backwards compatible accessors
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42144 uint16_t maxRetransmitTime() const override {
Harald Alvestrandf3736ed2019-04-08 11:09:30145 return config_.maxRetransmitTime ? *config_.maxRetransmitTime
146 : static_cast<uint16_t>(-1);
147 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42148 uint16_t maxRetransmits() const override {
Harald Alvestrandf3736ed2019-04-08 11:09:30149 return config_.maxRetransmits ? *config_.maxRetransmits
150 : static_cast<uint16_t>(-1);
151 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42152 absl::optional<int> maxPacketLifeTime() const override {
ossu7bb87ee2017-01-23 12:56:25153 return config_.maxRetransmitTime;
154 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42155 absl::optional<int> maxRetransmitsOpt() const override {
Harald Alvestrandf3736ed2019-04-08 11:09:30156 return config_.maxRetransmits;
157 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42158 std::string protocol() const override { return config_.protocol; }
159 bool negotiated() const override { return config_.negotiated; }
160 int id() const override { return config_.id; }
161 Priority priority() const override {
Harald Alvestrandfd5ae7f2020-05-16 06:37:49162 return config_.priority ? *config_.priority : Priority::kLow;
163 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42164
Harald Alvestrand928e7a32019-07-31 11:16:45165 virtual int internal_id() const { return internal_id_; }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42166
167 uint64_t buffered_amount() const override;
168 void Close() override;
169 DataState state() const override;
170 RTCError error() const override;
171 uint32_t messages_sent() const override;
172 uint64_t bytes_sent() const override;
173 uint32_t messages_received() const override;
174 uint64_t bytes_received() const override;
175 bool Send(const DataBuffer& buffer) override;
ossu7bb87ee2017-01-23 12:56:25176
Harald Alvestrand1f928d32019-03-28 10:29:38177 // Close immediately, ignoring any queued data or closing procedure.
Taylor Brandstetter3a034e12020-07-09 22:32:34178 // This is called when the underlying SctpTransport is being destroyed.
Harald Alvestrand1f928d32019-03-28 10:29:38179 // It is also called by the PeerConnection if SCTP ID assignment fails.
Harald Alvestranddfbfb462019-12-08 04:55:43180 void CloseAbruptlyWithError(RTCError error);
181 // Specializations of CloseAbruptlyWithError
182 void CloseAbruptlyWithDataChannelFailure(const std::string& message);
Harald Alvestrand1f928d32019-03-28 10:29:38183
Harald Alvestrand9e5aeb92022-05-11 09:35:36184 // Slots for controller to connect signals to.
Taylor Brandstetter3a034e12020-07-09 22:32:34185 //
186 // TODO(deadbeef): Make these private once we're hooking up signals ourselves,
Harald Alvestrand9e5aeb92022-05-11 09:35:36187 // instead of relying on SctpDataChannelControllerInterface.
Taylor Brandstetter3a034e12020-07-09 22:32:34188
189 // Called when the SctpTransport's ready to use. That can happen when we've
190 // finished negotiation, or if the channel was created after negotiation has
191 // already finished.
192 void OnTransportReady(bool writable);
193
ossu7bb87ee2017-01-23 12:56:25194 void OnDataReceived(const cricket::ReceiveDataParams& params,
195 const rtc::CopyOnWriteBuffer& payload);
ossu7bb87ee2017-01-23 12:56:25196
ossu7bb87ee2017-01-23 12:56:25197 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
198 // be called once.
199 void SetSctpSid(int sid);
Taylor Brandstettercdd05f02018-05-31 20:23:32200 // The remote side started the closing procedure by resetting its outgoing
201 // stream (our incoming stream). Sets state to kClosing.
202 void OnClosingProcedureStartedRemotely(int sid);
203 // The closing procedure is complete; both incoming and outgoing stream
204 // resets are done and the channel can transition to kClosed. Called
205 // asynchronously after RemoveSctpDataStream.
206 void OnClosingProcedureComplete(int sid);
ossu7bb87ee2017-01-23 12:56:25207 // Called when the transport channel is created.
208 // Only needs to be called for SCTP data channels.
209 void OnTransportChannelCreated();
Harald Alvestrand408cb4b2019-11-16 11:09:08210 // Called when the transport channel is unusable.
ossu7bb87ee2017-01-23 12:56:25211 // This method makes sure the DataChannel is disconnected and changes state
212 // to kClosed.
Florent Castellidcb9ffc2021-06-29 12:58:23213 void OnTransportChannelClosed(RTCError error);
ossu7bb87ee2017-01-23 12:56:25214
Taylor Brandstetter3a034e12020-07-09 22:32:34215 DataChannelStats GetStats() const;
ossu7bb87ee2017-01-23 12:56:25216
217 // Emitted when state transitions to kOpen.
Taylor Brandstetter3a034e12020-07-09 22:32:34218 sigslot::signal1<DataChannelInterface*> SignalOpened;
ossu7bb87ee2017-01-23 12:56:25219 // Emitted when state transitions to kClosed.
Taylor Brandstetter3a034e12020-07-09 22:32:34220 // This signal can be used to tell when the channel's sid is free.
221 sigslot::signal1<DataChannelInterface*> SignalClosed;
ossu7bb87ee2017-01-23 12:56:25222
Harald Alvestrand928e7a32019-07-31 11:16:45223 // Reset the allocator for internal ID values for testing, so that
224 // the internal IDs generated are predictable. Test only.
225 static void ResetInternalIdAllocatorForTesting(int new_value);
226
ossu7bb87ee2017-01-23 12:56:25227 protected:
Taylor Brandstetter3a034e12020-07-09 22:32:34228 SctpDataChannel(const InternalDataChannelInit& config,
Harald Alvestrand9e5aeb92022-05-11 09:35:36229 SctpDataChannelControllerInterface* client,
Taylor Brandstetter3a034e12020-07-09 22:32:34230 const std::string& label,
231 rtc::Thread* signaling_thread,
232 rtc::Thread* network_thread);
233 ~SctpDataChannel() override;
ossu7bb87ee2017-01-23 12:56:25234
235 private:
ossu7bb87ee2017-01-23 12:56:25236 // The OPEN(_ACK) signaling state.
237 enum HandshakeState {
238 kHandshakeInit,
239 kHandshakeShouldSendOpen,
240 kHandshakeShouldSendAck,
241 kHandshakeWaitingForAck,
242 kHandshakeReady
243 };
244
Tomas Gunnarsson0ca13d92020-06-10 10:17:50245 bool Init();
ossu7bb87ee2017-01-23 12:56:25246 void UpdateState();
247 void SetState(DataState state);
Harald Alvestrand9e5aeb92022-05-11 09:35:36248 void DisconnectFromTransport();
ossu7bb87ee2017-01-23 12:56:25249
250 void DeliverQueuedReceivedData();
251
252 void SendQueuedDataMessages();
253 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
254 bool QueueSendDataMessage(const DataBuffer& buffer);
255
256 void SendQueuedControlMessages();
257 void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer);
258 bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer);
259
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42260 rtc::Thread* const signaling_thread_;
261 rtc::Thread* const network_thread_;
Harald Alvestrand928e7a32019-07-31 11:16:45262 const int internal_id_;
Tomas Gunnarsson0ca13d92020-06-10 10:17:50263 const std::string label_;
264 const InternalDataChannelInit config_;
Taylor Brandstetter3a034e12020-07-09 22:32:34265 DataChannelObserver* observer_ RTC_GUARDED_BY(signaling_thread_) = nullptr;
266 DataState state_ RTC_GUARDED_BY(signaling_thread_) = kConnecting;
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42267 RTCError error_ RTC_GUARDED_BY(signaling_thread_);
Taylor Brandstetter3a034e12020-07-09 22:32:34268 uint32_t messages_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
269 uint64_t bytes_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
270 uint32_t messages_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
271 uint64_t bytes_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
Harald Alvestrand9e5aeb92022-05-11 09:35:36272 SctpDataChannelControllerInterface* const controller_
Taylor Brandstetter3a034e12020-07-09 22:32:34273 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestrand9e5aeb92022-05-11 09:35:36274 bool controller_detached_ RTC_GUARDED_BY(signaling_thread_) = false;
Taylor Brandstetter3a034e12020-07-09 22:32:34275 HandshakeState handshake_state_ RTC_GUARDED_BY(signaling_thread_) =
276 kHandshakeInit;
Harald Alvestrand9e5aeb92022-05-11 09:35:36277 bool connected_to_transport_ RTC_GUARDED_BY(signaling_thread_) = false;
Taylor Brandstetter3a034e12020-07-09 22:32:34278 bool writable_ RTC_GUARDED_BY(signaling_thread_) = false;
Taylor Brandstettercdd05f02018-05-31 20:23:32279 // Did we already start the graceful SCTP closing procedure?
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42280 bool started_closing_procedure_ RTC_GUARDED_BY(signaling_thread_) = false;
ossu7bb87ee2017-01-23 12:56:25281 // Control messages that always have to get sent out before any queued
282 // data.
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42283 PacketQueue queued_control_data_ RTC_GUARDED_BY(signaling_thread_);
284 PacketQueue queued_received_data_ RTC_GUARDED_BY(signaling_thread_);
285 PacketQueue queued_send_data_ RTC_GUARDED_BY(signaling_thread_);
ossu7bb87ee2017-01-23 12:56:25286};
287
Harald Alvestrand9e5aeb92022-05-11 09:35:36288// Downcast a PeerConnectionInterface that points to a proxy object
289// to its underlying SctpDataChannel object. For testing only.
290SctpDataChannel* DowncastProxiedDataChannelInterfaceToSctpDataChannelForTesting(
291 DataChannelInterface* channel);
292
ossu7bb87ee2017-01-23 12:56:25293} // namespace webrtc
294
Taylor Brandstetter3a034e12020-07-09 22:32:34295#endif // PC_SCTP_DATA_CHANNEL_H_