blob: 56f99df3e56f6b39b4cd50abf96bc152fe917933 [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
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000014#include <stdint.h>
15
Steve Anton944c7552018-12-13 14:19:10 -080016#include <memory>
ossu7bb87ee2017-01-23 04:56:25 -080017#include <set>
18#include <string>
19
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000020#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/data_channel_interface.h"
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020022#include "api/priority.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010024#include "api/scoped_refptr.h"
Niels Möller2a707032020-06-16 16:39:13 +020025#include "api/transport/data_channel_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "media/base/media_channel.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070027#include "pc/data_channel_utils.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000028#include "rtc_base/copy_on_write_buffer.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070029#include "rtc_base/ssl_stream_adapter.h" // For SSLRole
Artem Titove41c4332018-07-25 15:04:28 +020030#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000031#include "rtc_base/thread.h"
32#include "rtc_base/thread_annotations.h"
ossu7bb87ee2017-01-23 04:56:25 -080033
34namespace webrtc {
35
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070036class SctpDataChannel;
ossu7bb87ee2017-01-23 04:56:25 -080037
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070038// TODO(deadbeef): Get rid of this and have SctpDataChannel depend on
39// SctpTransportInternal (pure virtual SctpTransport interface) instead.
40class SctpDataChannelProviderInterface {
ossu7bb87ee2017-01-23 04:56:25 -080041 public:
42 // Sends the data to the transport.
Florent Castellid95b1492021-05-10 11:29:56 +020043 virtual bool SendData(int sid,
44 const SendDataParams& params,
ossu7bb87ee2017-01-23 04:56:25 -080045 const rtc::CopyOnWriteBuffer& payload,
46 cricket::SendDataResult* result) = 0;
47 // Connects to the transport signals.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070048 virtual bool ConnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080049 // Disconnects from the transport signals.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070050 virtual void DisconnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080051 // Adds the data channel SID to the transport for SCTP.
52 virtual void AddSctpDataStream(int sid) = 0;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -070053 // 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 04:56:25 -080055 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:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070060 virtual ~SctpDataChannelProviderInterface() {}
ossu7bb87ee2017-01-23 04:56:25 -080061};
62
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +020063// 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 04:56:25 -080065struct InternalDataChannelInit : public DataChannelInit {
Yves Gerey665174f2018-06-19 15:03:05 +020066 enum OpenHandshakeRole { kOpener, kAcker, kNone };
Artem Titov880fa812021-07-30 22:30:23 +020067 // The default role is kOpener because the default `negotiated` is false.
ossu7bb87ee2017-01-23 04:56:25 -080068 InternalDataChannelInit() : open_handshake_role(kOpener) {}
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020069 explicit InternalDataChannelInit(const DataChannelInit& base);
ossu7bb87ee2017-01-23 04:56:25 -080070 OpenHandshakeRole open_handshake_role;
71};
72
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070073// Helper class to allocate unique IDs for SCTP DataChannels.
ossu7bb87ee2017-01-23 04:56:25 -080074class SctpSidAllocator {
75 public:
Artem Titov880fa812021-07-30 22:30:23 +020076 // Gets the first unused odd/even id based on the DTLS role. If `role` is
ossu7bb87ee2017-01-23 04:56:25 -080077 // 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 13:23:32 -070079 // Returns false if no ID can be allocated.
ossu7bb87ee2017-01-23 04:56:25 -080080 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 22:30:23 +020085 // Indicates that `sid` isn't in use any more, and is thus available again.
ossu7bb87ee2017-01-23 04:56:25 -080086 void ReleaseSid(int sid);
87
88 private:
Artem Titov880fa812021-07-30 22:30:23 +020089 // Checks if `sid` is available to be assigned to a new SCTP data channel.
ossu7bb87ee2017-01-23 04:56:25 -080090 bool IsSidAvailable(int sid) const;
91
92 std::set<int> used_sids_;
93};
94
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070095// SctpDataChannel is an implementation of the DataChannelInterface based on
96// SctpTransport. It provides an implementation of unreliable or
97// reliabledata channels.
ossu7bb87ee2017-01-23 04:56:25 -080098
99// DataChannel states:
100// kConnecting: The channel has been created the transport might not yet be
101// ready.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700102// 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 13:23:32 -0700109//
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 15:32:34 -0700116// 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 04:56:25 -0800121 public:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700122 static rtc::scoped_refptr<SctpDataChannel> Create(
123 SctpDataChannelProviderInterface* provider,
ossu7bb87ee2017-01-23 04:56:25 -0800124 const std::string& label,
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200125 const InternalDataChannelInit& config,
126 rtc::Thread* signaling_thread,
127 rtc::Thread* network_thread);
ossu7bb87ee2017-01-23 04:56:25 -0800128
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700129 // Instantiates an API proxy for a SctpDataChannel instance that will be
130 // handed out to external callers.
Tomas Gunnarsson6476d0b2020-06-16 18:39:50 +0200131 static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700132 rtc::scoped_refptr<SctpDataChannel> channel);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800133
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200134 void RegisterObserver(DataChannelObserver* observer) override;
135 void UnregisterObserver() override;
ossu7bb87ee2017-01-23 04:56:25 -0800136
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200137 std::string label() const override { return label_; }
138 bool reliable() const override;
139 bool ordered() const override { return config_.ordered; }
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200140 // Backwards compatible accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200141 uint16_t maxRetransmitTime() const override {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200142 return config_.maxRetransmitTime ? *config_.maxRetransmitTime
143 : static_cast<uint16_t>(-1);
144 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200145 uint16_t maxRetransmits() const override {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200146 return config_.maxRetransmits ? *config_.maxRetransmits
147 : static_cast<uint16_t>(-1);
148 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200149 absl::optional<int> maxPacketLifeTime() const override {
ossu7bb87ee2017-01-23 04:56:25 -0800150 return config_.maxRetransmitTime;
151 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200152 absl::optional<int> maxRetransmitsOpt() const override {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200153 return config_.maxRetransmits;
154 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200155 std::string protocol() const override { return config_.protocol; }
156 bool negotiated() const override { return config_.negotiated; }
157 int id() const override { return config_.id; }
158 Priority priority() const override {
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +0200159 return config_.priority ? *config_.priority : Priority::kLow;
160 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200161
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400162 virtual int internal_id() const { return internal_id_; }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200163
164 uint64_t buffered_amount() const override;
165 void Close() override;
166 DataState state() const override;
167 RTCError error() const override;
168 uint32_t messages_sent() const override;
169 uint64_t bytes_sent() const override;
170 uint32_t messages_received() const override;
171 uint64_t bytes_received() const override;
172 bool Send(const DataBuffer& buffer) override;
ossu7bb87ee2017-01-23 04:56:25 -0800173
Harald Alvestrand1f928d32019-03-28 11:29:38 +0100174 // Close immediately, ignoring any queued data or closing procedure.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700175 // This is called when the underlying SctpTransport is being destroyed.
Harald Alvestrand1f928d32019-03-28 11:29:38 +0100176 // It is also called by the PeerConnection if SCTP ID assignment fails.
Harald Alvestranddfbfb462019-12-08 05:55:43 +0100177 void CloseAbruptlyWithError(RTCError error);
178 // Specializations of CloseAbruptlyWithError
179 void CloseAbruptlyWithDataChannelFailure(const std::string& message);
Harald Alvestrand1f928d32019-03-28 11:29:38 +0100180
ossu7bb87ee2017-01-23 04:56:25 -0800181 // Slots for provider to connect signals to.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700182 //
183 // TODO(deadbeef): Make these private once we're hooking up signals ourselves,
184 // instead of relying on SctpDataChannelProviderInterface.
185
186 // Called when the SctpTransport's ready to use. That can happen when we've
187 // finished negotiation, or if the channel was created after negotiation has
188 // already finished.
189 void OnTransportReady(bool writable);
190
ossu7bb87ee2017-01-23 04:56:25 -0800191 void OnDataReceived(const cricket::ReceiveDataParams& params,
192 const rtc::CopyOnWriteBuffer& payload);
ossu7bb87ee2017-01-23 04:56:25 -0800193
ossu7bb87ee2017-01-23 04:56:25 -0800194 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
195 // be called once.
196 void SetSctpSid(int sid);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700197 // The remote side started the closing procedure by resetting its outgoing
198 // stream (our incoming stream). Sets state to kClosing.
199 void OnClosingProcedureStartedRemotely(int sid);
200 // The closing procedure is complete; both incoming and outgoing stream
201 // resets are done and the channel can transition to kClosed. Called
202 // asynchronously after RemoveSctpDataStream.
203 void OnClosingProcedureComplete(int sid);
ossu7bb87ee2017-01-23 04:56:25 -0800204 // Called when the transport channel is created.
205 // Only needs to be called for SCTP data channels.
206 void OnTransportChannelCreated();
Harald Alvestrand408cb4b2019-11-16 12:09:08 +0100207 // Called when the transport channel is unusable.
ossu7bb87ee2017-01-23 04:56:25 -0800208 // This method makes sure the DataChannel is disconnected and changes state
209 // to kClosed.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200210 void OnTransportChannelClosed(RTCError error);
ossu7bb87ee2017-01-23 04:56:25 -0800211
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700212 DataChannelStats GetStats() const;
ossu7bb87ee2017-01-23 04:56:25 -0800213
214 // Emitted when state transitions to kOpen.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700215 sigslot::signal1<DataChannelInterface*> SignalOpened;
ossu7bb87ee2017-01-23 04:56:25 -0800216 // Emitted when state transitions to kClosed.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700217 // This signal can be used to tell when the channel's sid is free.
218 sigslot::signal1<DataChannelInterface*> SignalClosed;
ossu7bb87ee2017-01-23 04:56:25 -0800219
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400220 // Reset the allocator for internal ID values for testing, so that
221 // the internal IDs generated are predictable. Test only.
222 static void ResetInternalIdAllocatorForTesting(int new_value);
223
ossu7bb87ee2017-01-23 04:56:25 -0800224 protected:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700225 SctpDataChannel(const InternalDataChannelInit& config,
226 SctpDataChannelProviderInterface* client,
227 const std::string& label,
228 rtc::Thread* signaling_thread,
229 rtc::Thread* network_thread);
230 ~SctpDataChannel() override;
ossu7bb87ee2017-01-23 04:56:25 -0800231
232 private:
ossu7bb87ee2017-01-23 04:56:25 -0800233 // The OPEN(_ACK) signaling state.
234 enum HandshakeState {
235 kHandshakeInit,
236 kHandshakeShouldSendOpen,
237 kHandshakeShouldSendAck,
238 kHandshakeWaitingForAck,
239 kHandshakeReady
240 };
241
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +0200242 bool Init();
ossu7bb87ee2017-01-23 04:56:25 -0800243 void UpdateState();
244 void SetState(DataState state);
245 void DisconnectFromProvider();
246
247 void DeliverQueuedReceivedData();
248
249 void SendQueuedDataMessages();
250 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
251 bool QueueSendDataMessage(const DataBuffer& buffer);
252
253 void SendQueuedControlMessages();
254 void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer);
255 bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer);
256
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200257 rtc::Thread* const signaling_thread_;
258 rtc::Thread* const network_thread_;
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400259 const int internal_id_;
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +0200260 const std::string label_;
261 const InternalDataChannelInit config_;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700262 DataChannelObserver* observer_ RTC_GUARDED_BY(signaling_thread_) = nullptr;
263 DataState state_ RTC_GUARDED_BY(signaling_thread_) = kConnecting;
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200264 RTCError error_ RTC_GUARDED_BY(signaling_thread_);
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700265 uint32_t messages_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
266 uint64_t bytes_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
267 uint32_t messages_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
268 uint64_t bytes_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700269 SctpDataChannelProviderInterface* const provider_
270 RTC_GUARDED_BY(signaling_thread_);
271 HandshakeState handshake_state_ RTC_GUARDED_BY(signaling_thread_) =
272 kHandshakeInit;
273 bool connected_to_provider_ RTC_GUARDED_BY(signaling_thread_) = false;
274 bool writable_ RTC_GUARDED_BY(signaling_thread_) = false;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700275 // Did we already start the graceful SCTP closing procedure?
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200276 bool started_closing_procedure_ RTC_GUARDED_BY(signaling_thread_) = false;
ossu7bb87ee2017-01-23 04:56:25 -0800277 // Control messages that always have to get sent out before any queued
278 // data.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200279 PacketQueue queued_control_data_ RTC_GUARDED_BY(signaling_thread_);
280 PacketQueue queued_received_data_ RTC_GUARDED_BY(signaling_thread_);
281 PacketQueue queued_send_data_ RTC_GUARDED_BY(signaling_thread_);
ossu7bb87ee2017-01-23 04:56:25 -0800282};
283
ossu7bb87ee2017-01-23 04:56:25 -0800284} // namespace webrtc
285
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700286#endif // PC_SCTP_DATA_CHANNEL_H_